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 <mtklein@google.com>
Commit-Queue: Chris Dalton <csmartdalton@google.com>
This commit is contained in:
Chris Dalton 2019-02-04 01:20:52 -07:00 committed by Skia Commit-Bot
parent 5ded993429
commit d7a5a1d27e
32 changed files with 67 additions and 41 deletions

View File

@ -103,6 +103,7 @@ protected:
void onDraw(SkCanvas* canvas) override {
if (!fAnim) {
DrawFailureMessage(canvas, "No animation.");
return;
}
SkMatrix44 camera,

View File

@ -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<SkCodec> 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;
}

View File

@ -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);

View File

@ -31,7 +31,8 @@ protected:
const char* path = "images/mandrill_512_q075.jpg";
sk_sp<SkImage> 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;
}

View File

@ -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;
}

View File

@ -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);

View File

@ -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;

View File

@ -20,6 +20,8 @@ DEF_SIMPLE_GM(cross_context_image, canvas, 512 * 3 + 60, 512 + 128 + 30) {
sk_sp<SkData> 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;
}

View File

@ -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;
}

View File

@ -45,6 +45,8 @@ protected:
void onDraw(SkCanvas* canvas) override {
sk_sp<SkImage> 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() *

View File

@ -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));

View File

@ -72,6 +72,7 @@ protected:
GrContext* context = canvas->getGrContext();
if (!context || context->abandoned()) {
DrawFailureMessage(canvas, "GrContext unavailable or abandoned.");
return;
}

View File

@ -190,6 +190,7 @@ protected:
}
}
if (nullptr == fset.get()) {
DrawFailureMessage(canvas, "No SkFontStyleSet");
return;
}

View File

@ -41,6 +41,7 @@ protected:
sk_sp<SkTypeface> distortable(MakeResourceAsTypeface("fonts/Distortable.ttf"));
if (!distortableStream) {
DrawFailureMessage(canvas, "No distortableStream");
return;
}
const char* text = "abc";

View File

@ -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;
}

View File

@ -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;

View File

@ -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;

View File

@ -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;
}

View File

@ -133,6 +133,7 @@ protected:
sk_sp<SkSurface> surface(make_color_matching_surface(canvas, WIDTH, HEIGHT,
kPremul_SkAlphaType));
if (!surface) {
DrawFailureMessage(canvas, "make_color_matching_surface failed");
return;
}

View File

@ -57,6 +57,8 @@ protected:
sk_sp<SkImage> opaqueImage = GetResourceAsImage("images/mandrill_128.png");
sk_sp<SkImage> 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);

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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);

View File

@ -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);

View File

@ -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;
}

View File

@ -105,6 +105,8 @@ protected:
sk_sp<SkImage> bottomLImg = make_image(context, kImgSize, kBottomLeft_GrSurfaceOrigin);
sk_sp<SkImage> topLImg = make_image(context, kImgSize, kTopLeft_GrSurfaceOrigin);
if (!bottomLImg || !topLImg) {
DrawFailureMessage(canvas, "Could not load images. "
"Did you forget to set the resourcePath?");
return;
}

View File

@ -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());

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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(); )
}

View File

@ -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;
}
}

View File

@ -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;
}