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