From d7a5a1d27e1d3ecc1ef4dfa1cef38195daf99f67 Mon Sep 17 00:00:00 2001 From: Chris Dalton Date: Mon, 4 Feb 2019 01:20:52 -0700 Subject: [PATCH] Add a central skiagm::GM::DrawFailureMessage for error messages Bug: skia:8731 Change-Id: If73216bd427a1ce773fa41044a45c1bbd7ea08e9 Reviewed-on: https://skia-review.googlesource.com/c/189124 Reviewed-by: Mike Klein Commit-Queue: Chris Dalton --- gm/3dgm.cpp | 1 + gm/animatedGif.cpp | 16 +--------------- gm/atlastext.cpp | 2 +- gm/bitmapimage.cpp | 3 ++- gm/blurimagevmask.cpp | 2 ++ gm/copyTo4444.cpp | 4 ++-- gm/crbug_691386.cpp | 1 + gm/crosscontextimage.cpp | 2 ++ gm/discard.cpp | 1 + gm/encode-alpha-jpeg.cpp | 2 ++ gm/encode-platform.cpp | 4 ++++ gm/etc1.cpp | 1 + gm/fontmgr.cpp | 1 + gm/fontscalerdistortable.cpp | 1 + gm/fwidth_squircle.cpp | 3 +-- gm/gm.cpp | 18 ++++++++++++++++++ gm/gm.h | 2 ++ gm/image.cpp | 1 + gm/imagealphathreshold.cpp | 1 + gm/makecolorspace.cpp | 2 ++ gm/pdf_never_embed.cpp | 2 ++ gm/rectangletexture.cpp | 4 +--- gm/repeated_bitmap.cpp | 1 + gm/shadermaskfilter.cpp | 2 ++ gm/shapes_as_paths.cpp | 1 + gm/simple_magnification.cpp | 2 ++ gm/subsetshader.cpp | 2 ++ gm/textblobrandomfont.cpp | 2 +- gm/texturedomaineffect.cpp | 2 ++ gm/windowrectangles.cpp | 17 +---------------- gm/yuvtorgbeffect.cpp | 2 ++ modules/skottie/gm/SkottieGM.cpp | 3 +++ 32 files changed, 67 insertions(+), 41 deletions(-) diff --git a/gm/3dgm.cpp b/gm/3dgm.cpp index 8c9b8a8fa1..9184a76a83 100644 --- a/gm/3dgm.cpp +++ b/gm/3dgm.cpp @@ -103,6 +103,7 @@ protected: void onDraw(SkCanvas* canvas) override { if (!fAnim) { + DrawFailureMessage(canvas, "No animation."); return; } SkMatrix44 camera, diff --git a/gm/animatedGif.cpp b/gm/animatedGif.cpp index 34df9dee87..7074fa81d6 100644 --- a/gm/animatedGif.cpp +++ b/gm/animatedGif.cpp @@ -21,18 +21,6 @@ DEFINE_string(animatedGif, "images/test640x479.gif", "Animated gif in resources folder"); -namespace { - void error(SkCanvas* canvas, const SkString& errorText) { - constexpr SkScalar kOffset = 5.0f; - canvas->drawColor(SK_ColorRED); - SkPaint paint; - SkFont font; - SkRect bounds; - font.measureText(errorText.c_str(), errorText.size(), kUTF8_SkTextEncoding, &bounds); - canvas->drawString(errorText, kOffset, bounds.height() + kOffset, font, paint); - } -} - class AnimatedGifGM : public skiagm::GM { private: std::unique_ptr fCodec; @@ -115,7 +103,6 @@ private: fCodec = SkCodec::MakeFromStream(std::move(stream)); if (!fCodec) { - SkDebugf("Could create codec from %s", FLAGS_animatedGif[0]); return false; } @@ -127,8 +114,7 @@ private: void onDraw(SkCanvas* canvas) override { if (!this->initCodec()) { - SkString errorText = SkStringPrintf("Nothing to draw; %s", FLAGS_animatedGif[0]); - error(canvas, errorText); + DrawFailureMessage(canvas, "Could not create codec from %s", FLAGS_animatedGif[0]); return; } diff --git a/gm/atlastext.cpp b/gm/atlastext.cpp index 9fc7d6cc25..a7e95b43ab 100644 --- a/gm/atlastext.cpp +++ b/gm/atlastext.cpp @@ -88,7 +88,7 @@ protected: void onDraw(SkCanvas* canvas) override { if (!fRenderer || !fTarget || !fTarget->handle()) { - canvas->clear(SK_ColorRED); + DrawFailureMessage(canvas, "No renderer and/or target."); return; } fRenderer->clearTarget(fTarget->handle(), 0xFF808080); diff --git a/gm/bitmapimage.cpp b/gm/bitmapimage.cpp index 82f9626d8b..2dc692c08e 100644 --- a/gm/bitmapimage.cpp +++ b/gm/bitmapimage.cpp @@ -31,7 +31,8 @@ protected: const char* path = "images/mandrill_512_q075.jpg"; sk_sp image = GetResourceAsImage(path); if (!image) { - SkDebugf("Failure: Is the resource path set properly?"); + DrawFailureMessage(canvas, "Couldn't load images/mandrill_512_q075.jpg. " + "Did you forget to set the resource path?"); return; } diff --git a/gm/blurimagevmask.cpp b/gm/blurimagevmask.cpp index c2dd51663c..0056a05e2d 100644 --- a/gm/blurimagevmask.cpp +++ b/gm/blurimagevmask.cpp @@ -55,6 +55,8 @@ DEF_SIMPLE_GM(blurimagevmask, canvas, 700, 1200) { DEF_SIMPLE_GM(blur_image, canvas, 500, 500) { auto image = GetResourceAsImage("images/mandrill_128.png"); if (!image) { + skiagm::GM::DrawFailureMessage(canvas, "Could not load mandrill_128.png. " + "Did you forget to set the resourcePath?"); return; } diff --git a/gm/copyTo4444.cpp b/gm/copyTo4444.cpp index bdc2fe6068..46b034fc2e 100644 --- a/gm/copyTo4444.cpp +++ b/gm/copyTo4444.cpp @@ -33,8 +33,8 @@ protected: virtual void onDraw(SkCanvas* canvas) { SkBitmap bm, bm4444; if (!GetResourceAsBitmap("images/dog.jpg", &bm)) { - SkDebugf("Could not decode the file. Did you forget to set the " - "resourcePath?\n"); + DrawFailureMessage(canvas, "Could not decode the file. " + "Did you forget to set the resourcePath?"); return; } canvas->drawBitmap(bm, 0, 0); diff --git a/gm/crbug_691386.cpp b/gm/crbug_691386.cpp index b08c315634..76572b7945 100644 --- a/gm/crbug_691386.cpp +++ b/gm/crbug_691386.cpp @@ -11,6 +11,7 @@ DEF_SIMPLE_GM(crbug_691386, canvas, 256, 256) { SkPath path; if (!SkParsePath::FromSVGString("M -1 0 A 1 1 0 0 0 1 0 Z", &path)) { + skiagm::GM::DrawFailureMessage(canvas, "Failed to parse path."); return; } SkPaint p; diff --git a/gm/crosscontextimage.cpp b/gm/crosscontextimage.cpp index 5dfefbd979..265331a722 100644 --- a/gm/crosscontextimage.cpp +++ b/gm/crosscontextimage.cpp @@ -20,6 +20,8 @@ DEF_SIMPLE_GM(cross_context_image, canvas, 512 * 3 + 60, 512 + 128 + 30) { sk_sp encodedData = GetResourceAsData("images/mandrill_512.png"); if (!encodedData) { + skiagm::GM::DrawFailureMessage(canvas, "Could not load mandrill_512.png. " + "Did you forget to set the resourcePath?"); return; } diff --git a/gm/discard.cpp b/gm/discard.cpp index 7a1e9d9f44..d0c17a8bab 100644 --- a/gm/discard.cpp +++ b/gm/discard.cpp @@ -45,6 +45,7 @@ protected: SkImageInfo info = SkImageInfo::MakeN32Premul(size); auto surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info); if (nullptr == surface) { + DrawFailureMessage(canvas, "Could not create render target."); return; } diff --git a/gm/encode-alpha-jpeg.cpp b/gm/encode-alpha-jpeg.cpp index e2eeac6aa3..f716c77e0d 100644 --- a/gm/encode-alpha-jpeg.cpp +++ b/gm/encode-alpha-jpeg.cpp @@ -45,6 +45,8 @@ protected: void onDraw(SkCanvas* canvas) override { sk_sp srcImg = GetResourceAsImage("images/rainbow-gradient.png"); if (!srcImg) { + DrawFailureMessage(canvas, "Could not load images/rainbow-gradient.png. " + "Did you forget to set the resourcePath?"); return; } fStorage.reset(srcImg->width() * srcImg->height() * diff --git a/gm/encode-platform.cpp b/gm/encode-platform.cpp index eb35a4aa3e..c9ea8ce42b 100644 --- a/gm/encode-platform.cpp +++ b/gm/encode-platform.cpp @@ -85,10 +85,14 @@ protected: SkBitmap opaqueBm, premulBm, unpremulBm; if (!GetResourceAsBitmap("images/mandrill_256.png", &opaqueBm)) { + DrawFailureMessage(canvas, "Could not load images/mandrill_256.png.png. " + "Did you forget to set the resourcePath?"); return; } SkBitmap tmp; if (!GetResourceAsBitmap("images/yellow_rose.png", &tmp)) { + DrawFailureMessage(canvas, "Could not load images/yellow_rose.png. " + "Did you forget to set the resourcePath?"); return; } tmp.extractSubset(&premulBm, SkIRect::MakeWH(256, 256)); diff --git a/gm/etc1.cpp b/gm/etc1.cpp index f8432db309..d9895d815d 100644 --- a/gm/etc1.cpp +++ b/gm/etc1.cpp @@ -72,6 +72,7 @@ protected: GrContext* context = canvas->getGrContext(); if (!context || context->abandoned()) { + DrawFailureMessage(canvas, "GrContext unavailable or abandoned."); return; } diff --git a/gm/fontmgr.cpp b/gm/fontmgr.cpp index f16af88249..2249b51041 100644 --- a/gm/fontmgr.cpp +++ b/gm/fontmgr.cpp @@ -190,6 +190,7 @@ protected: } } if (nullptr == fset.get()) { + DrawFailureMessage(canvas, "No SkFontStyleSet"); return; } diff --git a/gm/fontscalerdistortable.cpp b/gm/fontscalerdistortable.cpp index b060a0af18..e112f7562e 100644 --- a/gm/fontscalerdistortable.cpp +++ b/gm/fontscalerdistortable.cpp @@ -41,6 +41,7 @@ protected: sk_sp distortable(MakeResourceAsTypeface("fonts/Distortable.ttf")); if (!distortableStream) { + DrawFailureMessage(canvas, "No distortableStream"); return; } const char* text = "abc"; diff --git a/gm/fwidth_squircle.cpp b/gm/fwidth_squircle.cpp index eb8ffa0023..eb5438d403 100644 --- a/gm/fwidth_squircle.cpp +++ b/gm/fwidth_squircle.cpp @@ -175,8 +175,7 @@ void FwidthSquircleGM::onDraw(SkCanvas* canvas) { if (!ctx->contextPriv().caps()->shaderCaps()->shaderDerivativeSupport()) { SkFont font(sk_tool_utils::create_portable_typeface(), 15); - SkTextUtils::DrawString(canvas, "Shader derivatives not supported.", 150, - 150 - 8, font, SkPaint(), SkTextUtils::kCenter_Align); + DrawFailureMessage(canvas, "Shader derivatives not supported."); return; } diff --git a/gm/gm.cpp b/gm/gm.cpp index 0dbcf5ad3d..25ab29afe1 100644 --- a/gm/gm.cpp +++ b/gm/gm.cpp @@ -92,6 +92,24 @@ void GM::DrawGpuOnlyMessage(SkCanvas* canvas) { return; } +void GM::DrawFailureMessage(SkCanvas* canvas, const char format[], ...) { + SkString failureMsg; + + va_list argp; + va_start(argp, format); + failureMsg.appendVAList(format, argp); + va_end(argp); + + constexpr SkScalar kOffset = 5.0f; + canvas->drawColor(SkColorSetRGB(200,0,0)); + SkFont font; + SkRect bounds; + font.measureText(failureMsg.c_str(), failureMsg.size(), kUTF8_SkTextEncoding, &bounds); + SkPaint textPaint; + textPaint.setColor(SK_ColorWHITE); + canvas->drawString(failureMsg, kOffset, bounds.height() + kOffset, font, textPaint); +} + // need to explicitly declare this, or we get some weird infinite loop llist template GMRegistry* GMRegistry::gHead; diff --git a/gm/gm.h b/gm/gm.h index 35fc9d2641..5e8d8f6e4e 100644 --- a/gm/gm.h +++ b/gm/gm.h @@ -99,6 +99,8 @@ namespace skiagm { /** draws a standard message that the GM is only intended to be used with the GPU.*/ static void DrawGpuOnlyMessage(SkCanvas*); + static void DrawFailureMessage(SkCanvas*, const char[], ...) SK_PRINTF_LIKE(2, 3); + protected: virtual void onOnceBeforeDraw() {} virtual void onDraw(SkCanvas*) = 0; diff --git a/gm/image.cpp b/gm/image.cpp index f1afa941f3..743fa220ca 100644 --- a/gm/image.cpp +++ b/gm/image.cpp @@ -407,6 +407,7 @@ DEF_SIMPLE_GM(image_subset, canvas, 440, 220) { auto surf = sk_tool_utils::makeSurface(canvas, info, nullptr); auto img = make_lazy_image(surf.get()); if (!img) { + skiagm::GM::DrawFailureMessage(canvas, "Failed to make lazy image."); return; } diff --git a/gm/imagealphathreshold.cpp b/gm/imagealphathreshold.cpp index 4bdf6eb5a2..9e66fef99d 100644 --- a/gm/imagealphathreshold.cpp +++ b/gm/imagealphathreshold.cpp @@ -133,6 +133,7 @@ protected: sk_sp surface(make_color_matching_surface(canvas, WIDTH, HEIGHT, kPremul_SkAlphaType)); if (!surface) { + DrawFailureMessage(canvas, "make_color_matching_surface failed"); return; } diff --git a/gm/makecolorspace.cpp b/gm/makecolorspace.cpp index aa28efd0a3..39afc69a3e 100644 --- a/gm/makecolorspace.cpp +++ b/gm/makecolorspace.cpp @@ -57,6 +57,8 @@ protected: sk_sp opaqueImage = GetResourceAsImage("images/mandrill_128.png"); sk_sp premulImage = GetResourceAsImage("images/color_wheel.png"); if (!opaqueImage || !premulImage) { + DrawFailureMessage(canvas, "Failed to load images. " + "Did you forget to set the resourcePath?"); return; } canvas->drawImage(opaqueImage, 0.0f, 0.0f); diff --git a/gm/pdf_never_embed.cpp b/gm/pdf_never_embed.cpp index dd69ea7f25..914bd79aa1 100644 --- a/gm/pdf_never_embed.cpp +++ b/gm/pdf_never_embed.cpp @@ -29,6 +29,8 @@ DEF_SIMPLE_GM(pdf_never_embed, canvas, 512, 512) { SkFont font(MakeResourceAsTypeface("fonts/Roboto2-Regular_NoEmbed.ttf"), 60); if (!font.getTypefaceOrDefault()) { + skiagm::GM::DrawFailureMessage(canvas, "Could not load fonts/Roboto2-Regular_NoEmbed.ttf. " + "Did you forget to set the resourcePath?"); return; } diff --git a/gm/rectangletexture.cpp b/gm/rectangletexture.cpp index 857fecbf51..c82b61dc2f 100644 --- a/gm/rectangletexture.cpp +++ b/gm/rectangletexture.cpp @@ -145,9 +145,7 @@ protected: }; SkASSERT(SkToBool(rectImgs[0]) == SkToBool(rectImgs[1])); if (!rectImgs[0]) { - SkPaint paint; - SkFont font; - canvas->drawString("Could not create rectangle texture image.", 10, 100, font, paint); + DrawFailureMessage(canvas, "Could not create rectangle texture image."); return; } diff --git a/gm/repeated_bitmap.cpp b/gm/repeated_bitmap.cpp index 09f30fb7c9..6f23ea2456 100644 --- a/gm/repeated_bitmap.cpp +++ b/gm/repeated_bitmap.cpp @@ -14,6 +14,7 @@ static void draw_rotated_image(SkCanvas* canvas, const SkImage* image) { sk_tool_utils::draw_checkerboard(canvas, SkColorSetRGB(156, 154, 156), SK_ColorWHITE, 12); if (!image) { + skiagm::GM::DrawFailureMessage(canvas, "No image. Did you forget to set the resourcePath?"); return; } SkRect rect = SkRect::MakeLTRB(-68.0f, -68.0f, 68.0f, 68.0f); diff --git a/gm/shadermaskfilter.cpp b/gm/shadermaskfilter.cpp index fedab48889..cd3af7019f 100644 --- a/gm/shadermaskfilter.cpp +++ b/gm/shadermaskfilter.cpp @@ -61,6 +61,8 @@ DEF_SIMPLE_GM(shadermaskfilter_image, canvas, 560, 370) { auto image = GetResourceAsImage("images/mandrill_128.png"); auto mask = GetResourceAsImage("images/color_wheel.png"); if (!image || !mask) { + skiagm::GM::DrawFailureMessage(canvas, "Could not load images. " + "Did you forget to set the resourcePath?"); return; } auto blurmf = SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 5); diff --git a/gm/shapes_as_paths.cpp b/gm/shapes_as_paths.cpp index 4ad760dcc7..8305715961 100644 --- a/gm/shapes_as_paths.cpp +++ b/gm/shapes_as_paths.cpp @@ -34,6 +34,7 @@ static void draw_diff(SkCanvas* canvas, SkImage* imgA, SkImage* imgB) { pmapA.alloc(info); pmapB.alloc(info); if (!imgA->readPixels(pmapA, 0, 0) || !imgB->readPixels(pmapB, 0, 0)) { + skiagm::GM::DrawFailureMessage(canvas, "Failed to read pixels."); return; } diff --git a/gm/simple_magnification.cpp b/gm/simple_magnification.cpp index d772ed91f5..6b70a43e9b 100644 --- a/gm/simple_magnification.cpp +++ b/gm/simple_magnification.cpp @@ -105,6 +105,8 @@ protected: sk_sp bottomLImg = make_image(context, kImgSize, kBottomLeft_GrSurfaceOrigin); sk_sp topLImg = make_image(context, kImgSize, kTopLeft_GrSurfaceOrigin); if (!bottomLImg || !topLImg) { + DrawFailureMessage(canvas, "Could not load images. " + "Did you forget to set the resourcePath?"); return; } diff --git a/gm/subsetshader.cpp b/gm/subsetshader.cpp index 4657aa59e5..545788b383 100644 --- a/gm/subsetshader.cpp +++ b/gm/subsetshader.cpp @@ -15,6 +15,8 @@ DEF_SIMPLE_GM(bitmap_subset_shader, canvas, 256, 256) { SkBitmap source; if (!GetResourceAsBitmap("images/color_wheel.png", &source)) { + skiagm::GM::DrawFailureMessage(canvas, "Could not load images/color_wheel.png. " + "Did you forget to set the resourcePath?"); return; } SkIRect left = SkIRect::MakeWH(source.width()/2, source.height()); diff --git a/gm/textblobrandomfont.cpp b/gm/textblobrandomfont.cpp index 72578494c6..e0f1cf5a71 100644 --- a/gm/textblobrandomfont.cpp +++ b/gm/textblobrandomfont.cpp @@ -110,7 +110,7 @@ protected: SkSurfaceProps props(0, kUnknown_SkPixelGeometry); auto surface(sk_tool_utils::makeSurface(canvas, info, &props)); if (!surface) { - canvas->drawString("This test requires a surface", 10, 100, SkFont(), SkPaint()); + DrawFailureMessage(canvas, "This test requires a surface"); return; } diff --git a/gm/texturedomaineffect.cpp b/gm/texturedomaineffect.cpp index 99680f149d..05d0611c9e 100644 --- a/gm/texturedomaineffect.cpp +++ b/gm/texturedomaineffect.cpp @@ -101,6 +101,7 @@ protected: SkBitmap copy; SkImageInfo info = as_IB(fImage)->onImageInfo().makeColorType(kN32_SkColorType); if (!copy.tryAllocPixels(info) || !fImage->readPixels(copy.pixmap(), 0, 0)) { + DrawFailureMessage(canvas, "Failed to read pixels."); return; } proxy = proxyProvider->createMipMapProxyFromBitmap(copy); @@ -109,6 +110,7 @@ protected: fImage, kNone_GrSurfaceFlags, 1, SkBudgeted::kYes, SkBackingFit::kExact); } if (!proxy) { + DrawFailureMessage(canvas, "Failed to create proxy."); return; } diff --git a/gm/windowrectangles.cpp b/gm/windowrectangles.cpp index dc12d1d0e3..9362e683f3 100644 --- a/gm/windowrectangles.cpp +++ b/gm/windowrectangles.cpp @@ -125,7 +125,6 @@ private: void visualizeAlphaMask(GrContext*, GrRenderTargetContext*, const GrReducedClip&, GrPaint&&); void visualizeStencilMask(GrContext*, GrRenderTargetContext*, const GrReducedClip&, GrPaint&&); void stencilCheckerboard(GrRenderTargetContext*, bool flip); - void fail(SkCanvas*); }; /** @@ -176,7 +175,7 @@ void WindowRectanglesMaskGM::onCoverClipStack(const SkClipStack& stack, SkCanvas GrRenderTargetContext* rtc = canvas->internal_private_accessTopLayerRenderTargetContext(); if (!ctx || !rtc || rtc->priv().maxWindowRectangles() < kNumWindows) { - this->fail(canvas); + DrawFailureMessage(canvas, "Requires GPU with %i window rectangles", kNumWindows); return; } @@ -265,20 +264,6 @@ void WindowRectanglesMaskGM::stencilCheckerboard(GrRenderTargetContext* rtc, boo } } -void WindowRectanglesMaskGM::fail(SkCanvas* canvas) { - SkFont font(sk_tool_utils::create_portable_typeface(), 20); - - SkString errorMsg; - errorMsg.printf("Requires GPU with %i window rectangles", kNumWindows); - - canvas->clipRect(SkRect::Make(kCoverRect)); - canvas->clear(SK_ColorWHITE); - - SkTextUtils::DrawString(canvas, errorMsg.c_str(), SkIntToScalar((kCoverRect.left() + kCoverRect.right())/2), - SkIntToScalar((kCoverRect.top() + kCoverRect.bottom())/2 - 10), - font, SkPaint(), SkTextUtils::kCenter_Align); -} - DEF_GM( return new WindowRectanglesMaskGM(); ) } diff --git a/gm/yuvtorgbeffect.cpp b/gm/yuvtorgbeffect.cpp index 18c7b77085..6a16acade8 100644 --- a/gm/yuvtorgbeffect.cpp +++ b/gm/yuvtorgbeffect.cpp @@ -92,6 +92,7 @@ protected: proxies[i] = proxyProvider->createTextureProxy(fImage[i], kNone_GrSurfaceFlags, 1, SkBudgeted::kYes, SkBackingFit::kExact); if (!proxies[i]) { + DrawFailureMessage(canvas, "Failed to create proxy"); return; } } @@ -219,6 +220,7 @@ protected: proxies[i] = proxyProvider->createTextureProxy(fImage[i], kNone_GrSurfaceFlags, 1, SkBudgeted::kYes, SkBackingFit::kExact); if (!proxies[i]) { + DrawFailureMessage(canvas, "Failed to create proxy"); return; } } diff --git a/modules/skottie/gm/SkottieGM.cpp b/modules/skottie/gm/SkottieGM.cpp index c8a4a2790f..09908b8548 100644 --- a/modules/skottie/gm/SkottieGM.cpp +++ b/modules/skottie/gm/SkottieGM.cpp @@ -63,6 +63,7 @@ protected: void onDraw(SkCanvas* canvas) override { if (!fAnimation) { + DrawFailureMessage(canvas, "No animation"); return; } @@ -114,6 +115,7 @@ protected: void onDraw(SkCanvas* canvas) override { if (!fAnimation) { + DrawFailureMessage(canvas, "No animation"); return; } @@ -185,6 +187,7 @@ protected: void onDraw(SkCanvas* canvas) override { if (!fAnimation) { + DrawFailureMessage(canvas, "No animation"); return; }