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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Tyler Denniston | 45f94f8 | 2020-02-04 16:09:08 -0500 | [diff] [blame] | 9 | #include "gm/verifiers/gmverifier.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 10 | #include "include/core/SkBitmap.h" |
| 11 | #include "include/core/SkBlendMode.h" |
| 12 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 13 | #include "include/core/SkFont.h" |
| 14 | #include "include/core/SkFontTypes.h" |
| 15 | #include "include/core/SkMatrix.h" |
| 16 | #include "include/core/SkPaint.h" |
| 17 | #include "include/core/SkRect.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "include/core/SkShader.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 19 | #include "include/core/SkTileMode.h" |
| 20 | #include "include/core/SkTypeface.h" |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 21 | #include "include/gpu/GrRecordingContext.h" |
Brian Salomon | 8f7d953 | 2020-12-23 09:16:59 -0500 | [diff] [blame] | 22 | #include "src/core/SkCanvasPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "src/core/SkTraceEvent.h" |
| 24 | #include "tools/ToolUtils.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 25 | |
| 26 | #include <stdarg.h> |
| 27 | |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 28 | using namespace skiagm; |
| 29 | |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 30 | constexpr char GM::kErrorMsg_DrawSkippedGpuOnly[]; |
| 31 | |
| 32 | static void draw_failure_message(SkCanvas* canvas, const char format[], ...) { |
| 33 | SkString failureMsg; |
| 34 | |
| 35 | va_list argp; |
| 36 | va_start(argp, format); |
| 37 | failureMsg.appendVAList(format, argp); |
| 38 | va_end(argp); |
| 39 | |
| 40 | constexpr SkScalar kOffset = 5.0f; |
| 41 | canvas->drawColor(SkColorSetRGB(200,0,0)); |
| 42 | SkFont font; |
| 43 | SkRect bounds; |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 44 | font.measureText(failureMsg.c_str(), failureMsg.size(), SkTextEncoding::kUTF8, &bounds); |
Hal Canary | 0a7b393 | 2019-05-02 11:31:28 -0400 | [diff] [blame] | 45 | SkPaint textPaint(SkColors::kWhite); |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 46 | canvas->drawString(failureMsg, kOffset, bounds.height() + kOffset, font, textPaint); |
| 47 | } |
| 48 | |
| 49 | static void draw_gpu_only_message(SkCanvas* canvas) { |
| 50 | SkBitmap bmp; |
| 51 | bmp.allocN32Pixels(128, 64); |
| 52 | SkCanvas bmpCanvas(bmp); |
| 53 | bmpCanvas.drawColor(SK_ColorWHITE); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 54 | SkFont font(ToolUtils::create_portable_typeface(), 20); |
Hal Canary | 0a7b393 | 2019-05-02 11:31:28 -0400 | [diff] [blame] | 55 | SkPaint paint(SkColors::kRed); |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 56 | bmpCanvas.drawString("GPU Only", 20, 40, font, paint); |
| 57 | SkMatrix localM; |
| 58 | localM.setRotate(35.f); |
| 59 | localM.postTranslate(10.f, 0.f); |
Mike Reed | b41bd15 | 2020-12-12 11:18:31 -0500 | [diff] [blame] | 60 | paint.setShader(bmp.makeShader(SkTileMode::kMirror, SkTileMode::kMirror, |
| 61 | SkSamplingOptions(SkFilterMode::kLinear, |
| 62 | SkMipmapMode::kNearest), |
| 63 | localM)); |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 64 | canvas->drawPaint(paint); |
| 65 | } |
| 66 | |
Robert Phillips | d26d25e | 2020-06-25 13:26:22 -0400 | [diff] [blame] | 67 | static void handle_gm_failure(SkCanvas* canvas, DrawResult result, const SkString& errorMsg) { |
| 68 | if (DrawResult::kFail == result) { |
| 69 | draw_failure_message(canvas, "DRAW FAILED: %s", errorMsg.c_str()); |
| 70 | } else if (SkString(GM::kErrorMsg_DrawSkippedGpuOnly) == errorMsg) { |
| 71 | draw_gpu_only_message(canvas); |
| 72 | } else { |
| 73 | draw_failure_message(canvas, "DRAW SKIPPED: %s", errorMsg.c_str()); |
| 74 | } |
| 75 | } |
| 76 | |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 77 | GM::GM(SkColor bgColor) { |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 78 | fMode = kGM_Mode; |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 79 | fBGColor = bgColor; |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 80 | } |
tfarina | 880914c | 2014-06-09 12:05:34 -0700 | [diff] [blame] | 81 | |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 82 | GM::~GM() {} |
| 83 | |
Robert Phillips | b87b39b | 2020-07-01 14:45:24 -0400 | [diff] [blame] | 84 | DrawResult GM::gpuSetup(GrDirectContext* context, SkCanvas* canvas, SkString* errorMsg) { |
Robert Phillips | 889d613 | 2020-06-16 11:11:33 -0400 | [diff] [blame] | 85 | TRACE_EVENT1("GM", TRACE_FUNC, "name", TRACE_STR_COPY(this->getName())); |
Robert Phillips | e922953 | 2020-06-26 10:10:49 -0400 | [diff] [blame] | 86 | if (!fGpuSetup) { |
| 87 | // When drawn in viewer, gpuSetup will be called multiple times with the same |
| 88 | // GrContext. |
| 89 | fGpuSetup = true; |
| 90 | fGpuSetupResult = this->onGpuSetup(context, errorMsg); |
Robert Phillips | d26d25e | 2020-06-25 13:26:22 -0400 | [diff] [blame] | 91 | } |
Robert Phillips | e922953 | 2020-06-26 10:10:49 -0400 | [diff] [blame] | 92 | if (DrawResult::kOk != fGpuSetupResult) { |
| 93 | handle_gm_failure(canvas, fGpuSetupResult, *errorMsg); |
| 94 | } |
| 95 | |
| 96 | return fGpuSetupResult; |
Robert Phillips | 889d613 | 2020-06-16 11:11:33 -0400 | [diff] [blame] | 97 | } |
| 98 | |
Robert Phillips | b795bea | 2020-06-25 12:38:53 -0400 | [diff] [blame] | 99 | void GM::gpuTeardown() { |
| 100 | this->onGpuTeardown(); |
Robert Phillips | e922953 | 2020-06-26 10:10:49 -0400 | [diff] [blame] | 101 | |
| 102 | // After 'gpuTeardown' a GM can be reused with a different GrContext. Reset the flag |
| 103 | // so 'onGpuSetup' will be called. |
| 104 | fGpuSetup = false; |
Robert Phillips | b795bea | 2020-06-25 12:38:53 -0400 | [diff] [blame] | 105 | } |
| 106 | |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 107 | DrawResult GM::draw(SkCanvas* canvas, SkString* errorMsg) { |
Mike Klein | b323a5e | 2017-07-24 15:21:31 -0400 | [diff] [blame] | 108 | TRACE_EVENT1("GM", TRACE_FUNC, "name", TRACE_STR_COPY(this->getName())); |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 109 | this->drawBackground(canvas); |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 110 | return this->drawContent(canvas, errorMsg); |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 113 | DrawResult GM::drawContent(SkCanvas* canvas, SkString* errorMsg) { |
Mike Klein | b323a5e | 2017-07-24 15:21:31 -0400 | [diff] [blame] | 114 | TRACE_EVENT0("GM", TRACE_FUNC); |
Robert Phillips | 83b749a | 2020-06-25 11:41:19 -0400 | [diff] [blame] | 115 | this->onceBeforeDraw(); |
Mike Reed | 36c4fb3 | 2018-12-18 11:48:01 -0500 | [diff] [blame] | 116 | SkAutoCanvasRestore acr(canvas, true); |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 117 | DrawResult drawResult = this->onDraw(canvas, errorMsg); |
| 118 | if (DrawResult::kOk != drawResult) { |
Robert Phillips | d26d25e | 2020-06-25 13:26:22 -0400 | [diff] [blame] | 119 | handle_gm_failure(canvas, drawResult, *errorMsg); |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 120 | } |
| 121 | return drawResult; |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void GM::drawBackground(SkCanvas* canvas) { |
Mike Klein | b323a5e | 2017-07-24 15:21:31 -0400 | [diff] [blame] | 125 | TRACE_EVENT0("GM", TRACE_FUNC); |
Robert Phillips | 83b749a | 2020-06-25 11:41:19 -0400 | [diff] [blame] | 126 | this->onceBeforeDraw(); |
Chris Dalton | 21ca370 | 2019-02-01 12:15:42 -0700 | [diff] [blame] | 127 | canvas->drawColor(fBGColor, SkBlendMode::kSrc); |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Mike Klein | 9707e90 | 2019-02-07 16:18:22 -0500 | [diff] [blame] | 130 | DrawResult GM::onDraw(SkCanvas* canvas, SkString* errorMsg) { |
| 131 | this->onDraw(canvas); |
| 132 | return DrawResult::kOk; |
| 133 | } |
| 134 | void GM::onDraw(SkCanvas*) { SK_ABORT("Not implemented."); } |
| 135 | |
| 136 | |
| 137 | SkISize SimpleGM::onISize() { return fSize; } |
| 138 | SkString SimpleGM::onShortName() { return fName; } |
| 139 | DrawResult SimpleGM::onDraw(SkCanvas* canvas, SkString* errorMsg) { |
| 140 | return fDrawProc(canvas, errorMsg); |
| 141 | } |
| 142 | |
| 143 | SkISize SimpleGpuGM::onISize() { return fSize; } |
| 144 | SkString SimpleGpuGM::onShortName() { return fName; } |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 145 | DrawResult SimpleGpuGM::onDraw(GrRecordingContext* rContext, SkCanvas* canvas, SkString* errorMsg) { |
| 146 | return fDrawProc(rContext, canvas, errorMsg); |
Mike Klein | 9707e90 | 2019-02-07 16:18:22 -0500 | [diff] [blame] | 147 | } |
| 148 | |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 149 | const char* GM::getName() { |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 150 | if (fShortName.size() == 0) { |
| 151 | fShortName = this->onShortName(); |
| 152 | } |
| 153 | return fShortName.c_str(); |
| 154 | } |
| 155 | |
| 156 | void GM::setBGColor(SkColor color) { |
| 157 | fBGColor = color; |
| 158 | } |
| 159 | |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 160 | bool GM::animate(double nanos) { return this->onAnimate(nanos); } |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 161 | |
Mike Klein | 9707e90 | 2019-02-07 16:18:22 -0500 | [diff] [blame] | 162 | bool GM::runAsBench() const { return false; } |
| 163 | void GM::modifyGrContextOptions(GrContextOptions* options) {} |
| 164 | |
Tyler Denniston | 45f94f8 | 2020-02-04 16:09:08 -0500 | [diff] [blame] | 165 | std::unique_ptr<verifiers::VerifierList> GM::getVerifiers() const { |
| 166 | // No verifiers by default. |
| 167 | return nullptr; |
| 168 | } |
| 169 | |
Mike Klein | 9707e90 | 2019-02-07 16:18:22 -0500 | [diff] [blame] | 170 | void GM::onOnceBeforeDraw() {} |
| 171 | |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 172 | bool GM::onAnimate(double /*nanos*/) { return false; } |
Hal Canary | c74a550 | 2019-07-08 14:55:15 -0400 | [diff] [blame] | 173 | |
| 174 | bool GM::onChar(SkUnichar uni) { return false; } |
| 175 | |
Mike Klein | 9707e90 | 2019-02-07 16:18:22 -0500 | [diff] [blame] | 176 | bool GM::onGetControls(SkMetaData*) { return false; } |
Hal Canary | c74a550 | 2019-07-08 14:55:15 -0400 | [diff] [blame] | 177 | |
Mike Klein | 9707e90 | 2019-02-07 16:18:22 -0500 | [diff] [blame] | 178 | void GM::onSetControls(const SkMetaData&) {} |
| 179 | |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 180 | ///////////////////////////////////////////////////////////////////////////////////////////// |
| 181 | |
reed@google.com | 2d6ef52 | 2012-01-03 17:20:38 +0000 | [diff] [blame] | 182 | void GM::drawSizeBounds(SkCanvas* canvas, SkColor color) { |
Hal Canary | 0a7b393 | 2019-05-02 11:31:28 -0400 | [diff] [blame] | 183 | canvas->drawRect(SkRect::Make(this->getISize()), SkPaint(SkColor4f::FromColor(color))); |
reed@google.com | 2d6ef52 | 2012-01-03 17:20:38 +0000 | [diff] [blame] | 184 | } |
| 185 | |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 186 | // need to explicitly declare this, or we get some weird infinite loop llist |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 187 | template GMRegistry* GMRegistry::gHead; |
halcanary | f62c634 | 2015-01-12 15:27:46 -0800 | [diff] [blame] | 188 | |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 189 | DrawResult GpuGM::onDraw(GrRecordingContext* rContext, SkCanvas* canvas, SkString* errorMsg) { |
| 190 | this->onDraw(rContext, canvas); |
Mike Klein | 9707e90 | 2019-02-07 16:18:22 -0500 | [diff] [blame] | 191 | return DrawResult::kOk; |
| 192 | } |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 193 | void GpuGM::onDraw(GrRecordingContext*, SkCanvas*) { |
Mike Klein | 9707e90 | 2019-02-07 16:18:22 -0500 | [diff] [blame] | 194 | SK_ABORT("Not implemented."); |
| 195 | } |
| 196 | |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 197 | DrawResult GpuGM::onDraw(SkCanvas* canvas, SkString* errorMsg) { |
Robert Phillips | 95c250c | 2020-06-29 15:36:12 -0400 | [diff] [blame] | 198 | |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 199 | auto rContext = canvas->recordingContext(); |
| 200 | if (!rContext) { |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 201 | *errorMsg = kErrorMsg_DrawSkippedGpuOnly; |
| 202 | return DrawResult::kSkip; |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 203 | } |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 204 | if (rContext->abandoned()) { |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 205 | *errorMsg = "GrContext abandoned."; |
Mike Klein | 290690c | 2019-04-02 18:11:57 -0400 | [diff] [blame] | 206 | return DrawResult::kSkip; |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 207 | } |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 208 | return this->onDraw(rContext, canvas, errorMsg); |
halcanary | f62c634 | 2015-01-12 15:27:46 -0800 | [diff] [blame] | 209 | } |
Mike Klein | c9eace8 | 2018-10-31 10:49:38 -0400 | [diff] [blame] | 210 | |
| 211 | template <typename Fn> |
| 212 | static void mark(SkCanvas* canvas, SkScalar x, SkScalar y, Fn&& fn) { |
| 213 | SkPaint alpha; |
| 214 | alpha.setAlpha(0x50); |
| 215 | canvas->saveLayer(nullptr, &alpha); |
| 216 | canvas->translate(x,y); |
| 217 | canvas->scale(2,2); |
| 218 | fn(); |
| 219 | canvas->restore(); |
| 220 | } |
| 221 | |
| 222 | void MarkGMGood(SkCanvas* canvas, SkScalar x, SkScalar y) { |
| 223 | mark(canvas, x,y, [&]{ |
Mike Klein | c9eace8 | 2018-10-31 10:49:38 -0400 | [diff] [blame] | 224 | // A green circle. |
Hal Canary | 0a7b393 | 2019-05-02 11:31:28 -0400 | [diff] [blame] | 225 | canvas->drawCircle(0, 0, 12, SkPaint(SkColor4f::FromColor(SkColorSetRGB(27, 158, 119)))); |
Mike Klein | c9eace8 | 2018-10-31 10:49:38 -0400 | [diff] [blame] | 226 | |
| 227 | // Cut out a check mark. |
Hal Canary | 0a7b393 | 2019-05-02 11:31:28 -0400 | [diff] [blame] | 228 | SkPaint paint(SkColors::kTransparent); |
Mike Klein | c9eace8 | 2018-10-31 10:49:38 -0400 | [diff] [blame] | 229 | paint.setBlendMode(SkBlendMode::kSrc); |
Mike Klein | c9eace8 | 2018-10-31 10:49:38 -0400 | [diff] [blame] | 230 | paint.setStrokeWidth(2); |
| 231 | paint.setStyle(SkPaint::kStroke_Style); |
| 232 | canvas->drawLine(-6, 0, |
| 233 | -1, 5, paint); |
| 234 | canvas->drawLine(-1, +5, |
| 235 | +7, -5, paint); |
| 236 | }); |
| 237 | } |
| 238 | |
| 239 | void MarkGMBad(SkCanvas* canvas, SkScalar x, SkScalar y) { |
| 240 | mark(canvas, x,y, [&] { |
Mike Klein | c9eace8 | 2018-10-31 10:49:38 -0400 | [diff] [blame] | 241 | // A red circle. |
Hal Canary | 0a7b393 | 2019-05-02 11:31:28 -0400 | [diff] [blame] | 242 | canvas->drawCircle(0,0, 12, SkPaint(SkColor4f::FromColor(SkColorSetRGB(231, 41, 138)))); |
Mike Klein | c9eace8 | 2018-10-31 10:49:38 -0400 | [diff] [blame] | 243 | |
| 244 | // Cut out an 'X'. |
Hal Canary | 0a7b393 | 2019-05-02 11:31:28 -0400 | [diff] [blame] | 245 | SkPaint paint(SkColors::kTransparent); |
Mike Klein | c9eace8 | 2018-10-31 10:49:38 -0400 | [diff] [blame] | 246 | paint.setBlendMode(SkBlendMode::kSrc); |
Mike Klein | c9eace8 | 2018-10-31 10:49:38 -0400 | [diff] [blame] | 247 | paint.setStrokeWidth(2); |
| 248 | paint.setStyle(SkPaint::kStroke_Style); |
| 249 | canvas->drawLine(-5,-5, |
| 250 | +5,+5, paint); |
| 251 | canvas->drawLine(+5,-5, |
| 252 | -5,+5, paint); |
| 253 | }); |
| 254 | } |