bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "gm.h" |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 9 | |
| 10 | #include "GrContext.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 11 | #include "sk_tool_utils.h" |
bsalomon | b62da80 | 2015-01-31 07:51:14 -0800 | [diff] [blame] | 12 | #include "SkShader.h" |
Mike Klein | b323a5e | 2017-07-24 15:21:31 -0400 | [diff] [blame] | 13 | #include "SkTraceEvent.h" |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 14 | using namespace skiagm; |
| 15 | |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 16 | constexpr char GM::kErrorMsg_DrawSkippedGpuOnly[]; |
| 17 | |
| 18 | static void draw_failure_message(SkCanvas* canvas, const char format[], ...) { |
| 19 | SkString failureMsg; |
| 20 | |
| 21 | va_list argp; |
| 22 | va_start(argp, format); |
| 23 | failureMsg.appendVAList(format, argp); |
| 24 | va_end(argp); |
| 25 | |
| 26 | constexpr SkScalar kOffset = 5.0f; |
| 27 | canvas->drawColor(SkColorSetRGB(200,0,0)); |
| 28 | SkFont font; |
| 29 | SkRect bounds; |
| 30 | font.measureText(failureMsg.c_str(), failureMsg.size(), kUTF8_SkTextEncoding, &bounds); |
| 31 | SkPaint textPaint; |
| 32 | textPaint.setColor(SK_ColorWHITE); |
| 33 | canvas->drawString(failureMsg, kOffset, bounds.height() + kOffset, font, textPaint); |
| 34 | } |
| 35 | |
| 36 | static void draw_gpu_only_message(SkCanvas* canvas) { |
| 37 | SkBitmap bmp; |
| 38 | bmp.allocN32Pixels(128, 64); |
| 39 | SkCanvas bmpCanvas(bmp); |
| 40 | bmpCanvas.drawColor(SK_ColorWHITE); |
| 41 | SkFont font(sk_tool_utils::create_portable_typeface(), 20); |
| 42 | SkPaint paint; |
| 43 | paint.setColor(SK_ColorRED); |
| 44 | bmpCanvas.drawString("GPU Only", 20, 40, font, paint); |
| 45 | SkMatrix localM; |
| 46 | localM.setRotate(35.f); |
| 47 | localM.postTranslate(10.f, 0.f); |
| 48 | paint.setShader(SkShader::MakeBitmapShader( |
| 49 | bmp, SkShader::kMirror_TileMode, SkShader::kMirror_TileMode, &localM)); |
| 50 | paint.setFilterQuality(kMedium_SkFilterQuality); |
| 51 | canvas->drawPaint(paint); |
| 52 | } |
| 53 | |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 54 | GM::GM(SkColor bgColor) { |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 55 | fMode = kGM_Mode; |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 56 | fBGColor = bgColor; |
reed@google.com | aef7361 | 2012-11-16 13:41:45 +0000 | [diff] [blame] | 57 | fCanvasIsDeferred = false; |
reed@google.com | 7775d66 | 2012-11-27 15:15:58 +0000 | [diff] [blame] | 58 | fHaveCalledOnceBeforeDraw = false; |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 59 | } |
tfarina | 880914c | 2014-06-09 12:05:34 -0700 | [diff] [blame] | 60 | |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 61 | GM::~GM() {} |
| 62 | |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 63 | DrawResult GM::draw(SkCanvas* canvas, SkString* errorMsg) { |
Mike Klein | b323a5e | 2017-07-24 15:21:31 -0400 | [diff] [blame] | 64 | TRACE_EVENT1("GM", TRACE_FUNC, "name", TRACE_STR_COPY(this->getName())); |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 65 | this->drawBackground(canvas); |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 66 | return this->drawContent(canvas, errorMsg); |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 69 | DrawResult GM::drawContent(SkCanvas* canvas, SkString* errorMsg) { |
Mike Klein | b323a5e | 2017-07-24 15:21:31 -0400 | [diff] [blame] | 70 | TRACE_EVENT0("GM", TRACE_FUNC); |
reed@google.com | 7775d66 | 2012-11-27 15:15:58 +0000 | [diff] [blame] | 71 | if (!fHaveCalledOnceBeforeDraw) { |
| 72 | fHaveCalledOnceBeforeDraw = true; |
| 73 | this->onOnceBeforeDraw(); |
| 74 | } |
Mike Reed | 36c4fb3 | 2018-12-18 11:48:01 -0500 | [diff] [blame] | 75 | SkAutoCanvasRestore acr(canvas, true); |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 76 | DrawResult drawResult = this->onDraw(canvas, errorMsg); |
| 77 | if (DrawResult::kOk != drawResult) { |
| 78 | if (DrawResult::kFail == drawResult) { |
| 79 | draw_failure_message(canvas, "DRAW FAILED: %s", errorMsg->c_str()); |
| 80 | } else if (SkString(kErrorMsg_DrawSkippedGpuOnly) == *errorMsg) { |
| 81 | draw_gpu_only_message(canvas); |
| 82 | } else { |
| 83 | draw_failure_message(canvas, "DRAW SKIPPED: %s", errorMsg->c_str()); |
| 84 | } |
| 85 | } |
| 86 | return drawResult; |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void GM::drawBackground(SkCanvas* canvas) { |
Mike Klein | b323a5e | 2017-07-24 15:21:31 -0400 | [diff] [blame] | 90 | TRACE_EVENT0("GM", TRACE_FUNC); |
reed@google.com | 7775d66 | 2012-11-27 15:15:58 +0000 | [diff] [blame] | 91 | if (!fHaveCalledOnceBeforeDraw) { |
| 92 | fHaveCalledOnceBeforeDraw = true; |
| 93 | this->onOnceBeforeDraw(); |
| 94 | } |
Mike Reed | 36c4fb3 | 2018-12-18 11:48:01 -0500 | [diff] [blame] | 95 | SkAutoCanvasRestore acr(canvas, true); |
Chris Dalton | 21ca370 | 2019-02-01 12:15:42 -0700 | [diff] [blame] | 96 | canvas->drawColor(fBGColor, SkBlendMode::kSrc); |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Mike Klein | 9707e90 | 2019-02-07 16:18:22 -0500 | [diff] [blame] | 99 | DrawResult GM::onDraw(SkCanvas* canvas, SkString* errorMsg) { |
| 100 | this->onDraw(canvas); |
| 101 | return DrawResult::kOk; |
| 102 | } |
| 103 | void GM::onDraw(SkCanvas*) { SK_ABORT("Not implemented."); } |
| 104 | |
| 105 | |
| 106 | SkISize SimpleGM::onISize() { return fSize; } |
| 107 | SkString SimpleGM::onShortName() { return fName; } |
| 108 | DrawResult SimpleGM::onDraw(SkCanvas* canvas, SkString* errorMsg) { |
| 109 | return fDrawProc(canvas, errorMsg); |
| 110 | } |
| 111 | |
| 112 | SkISize SimpleGpuGM::onISize() { return fSize; } |
| 113 | SkString SimpleGpuGM::onShortName() { return fName; } |
| 114 | DrawResult SimpleGpuGM::onDraw(GrContext* ctx, GrRenderTargetContext* rtc, SkCanvas* canvas, |
| 115 | SkString* errorMsg) { |
| 116 | return fDrawProc(ctx, rtc, canvas, errorMsg); |
| 117 | } |
| 118 | |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 119 | const char* GM::getName() { |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 120 | if (fShortName.size() == 0) { |
| 121 | fShortName = this->onShortName(); |
| 122 | } |
| 123 | return fShortName.c_str(); |
| 124 | } |
| 125 | |
| 126 | void GM::setBGColor(SkColor color) { |
| 127 | fBGColor = color; |
| 128 | } |
| 129 | |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 130 | bool GM::animate(const SkAnimTimer& timer) { |
| 131 | return this->onAnimate(timer); |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Mike Klein | 9707e90 | 2019-02-07 16:18:22 -0500 | [diff] [blame] | 134 | bool GM::runAsBench() const { return false; } |
| 135 | void GM::modifyGrContextOptions(GrContextOptions* options) {} |
| 136 | |
| 137 | void GM::onOnceBeforeDraw() {} |
| 138 | |
| 139 | bool GM::onAnimate(const SkAnimTimer&) { return false; } |
| 140 | bool GM::onHandleKey(SkUnichar uni) { return false; } |
| 141 | bool GM::onGetControls(SkMetaData*) { return false; } |
| 142 | void GM::onSetControls(const SkMetaData&) {} |
| 143 | |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 144 | ///////////////////////////////////////////////////////////////////////////////////////////// |
| 145 | |
reed@google.com | 2d6ef52 | 2012-01-03 17:20:38 +0000 | [diff] [blame] | 146 | void GM::drawSizeBounds(SkCanvas* canvas, SkColor color) { |
| 147 | SkISize size = this->getISize(); |
| 148 | SkRect r = SkRect::MakeWH(SkIntToScalar(size.width()), |
| 149 | SkIntToScalar(size.height())); |
| 150 | SkPaint paint; |
| 151 | paint.setColor(color); |
| 152 | canvas->drawRect(r, paint); |
| 153 | } |
| 154 | |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 155 | // need to explicitly declare this, or we get some weird infinite loop llist |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 156 | template GMRegistry* GMRegistry::gHead; |
halcanary | f62c634 | 2015-01-12 15:27:46 -0800 | [diff] [blame] | 157 | |
Mike Klein | 9707e90 | 2019-02-07 16:18:22 -0500 | [diff] [blame] | 158 | DrawResult GpuGM::onDraw(GrContext* ctx, GrRenderTargetContext* rtc, SkCanvas* canvas, |
| 159 | SkString* errorMsg) { |
| 160 | this->onDraw(ctx, rtc, canvas); |
| 161 | return DrawResult::kOk; |
| 162 | } |
| 163 | void GpuGM::onDraw(GrContext*, GrRenderTargetContext*, SkCanvas*) { |
| 164 | SK_ABORT("Not implemented."); |
| 165 | } |
| 166 | |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 167 | DrawResult GpuGM::onDraw(SkCanvas* canvas, SkString* errorMsg) { |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 168 | GrContext* ctx = canvas->getGrContext(); |
| 169 | GrRenderTargetContext* rtc = canvas->internal_private_accessTopLayerRenderTargetContext(); |
| 170 | if (!ctx || !rtc) { |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 171 | *errorMsg = kErrorMsg_DrawSkippedGpuOnly; |
| 172 | return DrawResult::kSkip; |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 173 | } |
| 174 | if (ctx->abandoned()) { |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 175 | *errorMsg = "GrContext abandoned."; |
| 176 | return DrawResult::kFail; |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 177 | } |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 178 | return this->onDraw(ctx, rtc, canvas, errorMsg); |
halcanary | f62c634 | 2015-01-12 15:27:46 -0800 | [diff] [blame] | 179 | } |
Mike Klein | c9eace8 | 2018-10-31 10:49:38 -0400 | [diff] [blame] | 180 | |
| 181 | template <typename Fn> |
| 182 | static void mark(SkCanvas* canvas, SkScalar x, SkScalar y, Fn&& fn) { |
| 183 | SkPaint alpha; |
| 184 | alpha.setAlpha(0x50); |
| 185 | canvas->saveLayer(nullptr, &alpha); |
| 186 | canvas->translate(x,y); |
| 187 | canvas->scale(2,2); |
| 188 | fn(); |
| 189 | canvas->restore(); |
| 190 | } |
| 191 | |
| 192 | void MarkGMGood(SkCanvas* canvas, SkScalar x, SkScalar y) { |
| 193 | mark(canvas, x,y, [&]{ |
| 194 | SkPaint paint; |
| 195 | |
| 196 | // A green circle. |
| 197 | paint.setColor(SkColorSetRGB(27, 158, 119)); |
| 198 | canvas->drawCircle(0,0, 12, paint); |
| 199 | |
| 200 | // Cut out a check mark. |
| 201 | paint.setBlendMode(SkBlendMode::kSrc); |
| 202 | paint.setColor(0x00000000); |
| 203 | paint.setStrokeWidth(2); |
| 204 | paint.setStyle(SkPaint::kStroke_Style); |
| 205 | canvas->drawLine(-6, 0, |
| 206 | -1, 5, paint); |
| 207 | canvas->drawLine(-1, +5, |
| 208 | +7, -5, paint); |
| 209 | }); |
| 210 | } |
| 211 | |
| 212 | void MarkGMBad(SkCanvas* canvas, SkScalar x, SkScalar y) { |
| 213 | mark(canvas, x,y, [&] { |
| 214 | SkPaint paint; |
| 215 | |
| 216 | // A red circle. |
| 217 | paint.setColor(SkColorSetRGB(231, 41, 138)); |
| 218 | canvas->drawCircle(0,0, 12, paint); |
| 219 | |
| 220 | // Cut out an 'X'. |
| 221 | paint.setBlendMode(SkBlendMode::kSrc); |
| 222 | paint.setColor(0x00000000); |
| 223 | paint.setStrokeWidth(2); |
| 224 | paint.setStyle(SkPaint::kStroke_Style); |
| 225 | canvas->drawLine(-5,-5, |
| 226 | +5,+5, paint); |
| 227 | canvas->drawLine(+5,-5, |
| 228 | -5,+5, paint); |
| 229 | }); |
| 230 | } |