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" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
bsalomon | b62da80 | 2015-01-31 07:51:14 -0800 | [diff] [blame] | 10 | #include "SkShader.h" |
Mike Klein | b323a5e | 2017-07-24 15:21:31 -0400 | [diff] [blame] | 11 | #include "SkTraceEvent.h" |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 12 | using namespace skiagm; |
| 13 | |
| 14 | GM::GM() { |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 15 | fMode = kGM_Mode; |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 16 | fBGColor = SK_ColorWHITE; |
reed@google.com | aef7361 | 2012-11-16 13:41:45 +0000 | [diff] [blame] | 17 | fCanvasIsDeferred = false; |
reed@google.com | 7775d66 | 2012-11-27 15:15:58 +0000 | [diff] [blame] | 18 | fHaveCalledOnceBeforeDraw = false; |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 19 | } |
tfarina | 880914c | 2014-06-09 12:05:34 -0700 | [diff] [blame] | 20 | |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 21 | GM::~GM() {} |
| 22 | |
| 23 | void GM::draw(SkCanvas* canvas) { |
Mike Klein | b323a5e | 2017-07-24 15:21:31 -0400 | [diff] [blame] | 24 | TRACE_EVENT1("GM", TRACE_FUNC, "name", TRACE_STR_COPY(this->getName())); |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 25 | this->drawBackground(canvas); |
| 26 | this->drawContent(canvas); |
| 27 | } |
| 28 | |
| 29 | void GM::drawContent(SkCanvas* canvas) { |
Mike Klein | b323a5e | 2017-07-24 15:21:31 -0400 | [diff] [blame] | 30 | TRACE_EVENT0("GM", TRACE_FUNC); |
reed@google.com | 7775d66 | 2012-11-27 15:15:58 +0000 | [diff] [blame] | 31 | if (!fHaveCalledOnceBeforeDraw) { |
| 32 | fHaveCalledOnceBeforeDraw = true; |
| 33 | this->onOnceBeforeDraw(); |
| 34 | } |
Mike Reed | 36c4fb3 | 2018-12-18 11:48:01 -0500 | [diff] [blame] | 35 | SkAutoCanvasRestore acr(canvas, true); |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 36 | this->onDraw(canvas); |
| 37 | } |
| 38 | |
| 39 | void GM::drawBackground(SkCanvas* canvas) { |
Mike Klein | b323a5e | 2017-07-24 15:21:31 -0400 | [diff] [blame] | 40 | TRACE_EVENT0("GM", TRACE_FUNC); |
reed@google.com | 7775d66 | 2012-11-27 15:15:58 +0000 | [diff] [blame] | 41 | if (!fHaveCalledOnceBeforeDraw) { |
| 42 | fHaveCalledOnceBeforeDraw = true; |
| 43 | this->onOnceBeforeDraw(); |
| 44 | } |
Mike Reed | 36c4fb3 | 2018-12-18 11:48:01 -0500 | [diff] [blame] | 45 | SkAutoCanvasRestore acr(canvas, true); |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 46 | this->onDrawBackground(canvas); |
| 47 | } |
| 48 | |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 49 | const char* GM::getName() { |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 50 | if (fShortName.size() == 0) { |
| 51 | fShortName = this->onShortName(); |
| 52 | } |
| 53 | return fShortName.c_str(); |
| 54 | } |
| 55 | |
| 56 | void GM::setBGColor(SkColor color) { |
| 57 | fBGColor = color; |
| 58 | } |
| 59 | |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 60 | bool GM::animate(const SkAnimTimer& timer) { |
| 61 | return this->onAnimate(timer); |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | ///////////////////////////////////////////////////////////////////////////////////////////// |
| 65 | |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 66 | void GM::onDrawBackground(SkCanvas* canvas) { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 67 | canvas->drawColor(fBGColor, SkBlendMode::kSrc); |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 68 | } |
| 69 | |
reed@google.com | 2d6ef52 | 2012-01-03 17:20:38 +0000 | [diff] [blame] | 70 | void GM::drawSizeBounds(SkCanvas* canvas, SkColor color) { |
| 71 | SkISize size = this->getISize(); |
| 72 | SkRect r = SkRect::MakeWH(SkIntToScalar(size.width()), |
| 73 | SkIntToScalar(size.height())); |
| 74 | SkPaint paint; |
| 75 | paint.setColor(color); |
| 76 | canvas->drawRect(r, paint); |
| 77 | } |
| 78 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 79 | void GM::DrawGpuOnlyMessage(SkCanvas* canvas) { |
bsalomon | b62da80 | 2015-01-31 07:51:14 -0800 | [diff] [blame] | 80 | SkBitmap bmp; |
| 81 | bmp.allocN32Pixels(128, 64); |
| 82 | SkCanvas bmpCanvas(bmp); |
| 83 | bmpCanvas.drawColor(SK_ColorWHITE); |
Hal Canary | 6ac0df8 | 2019-01-07 16:01:22 -0500 | [diff] [blame] | 84 | SkFont font(sk_tool_utils::create_portable_typeface(), 20); |
bsalomon | b62da80 | 2015-01-31 07:51:14 -0800 | [diff] [blame] | 85 | SkPaint paint; |
bsalomon | b62da80 | 2015-01-31 07:51:14 -0800 | [diff] [blame] | 86 | paint.setColor(SK_ColorRED); |
Hal Canary | 6ac0df8 | 2019-01-07 16:01:22 -0500 | [diff] [blame] | 87 | bmpCanvas.drawString("GPU Only", 20, 40, font, paint); |
bsalomon | b62da80 | 2015-01-31 07:51:14 -0800 | [diff] [blame] | 88 | SkMatrix localM; |
| 89 | localM.setRotate(35.f); |
| 90 | localM.postTranslate(10.f, 0.f); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 91 | paint.setShader(SkShader::MakeBitmapShader(bmp, SkShader::kMirror_TileMode, |
| 92 | SkShader::kMirror_TileMode, |
| 93 | &localM)); |
bsalomon | b62da80 | 2015-01-31 07:51:14 -0800 | [diff] [blame] | 94 | paint.setFilterQuality(kMedium_SkFilterQuality); |
| 95 | canvas->drawPaint(paint); |
| 96 | return; |
| 97 | } |
| 98 | |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 99 | // need to explicitly declare this, or we get some weird infinite loop llist |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 100 | template GMRegistry* GMRegistry::gHead; |
halcanary | f62c634 | 2015-01-12 15:27:46 -0800 | [diff] [blame] | 101 | |
| 102 | void skiagm::SimpleGM::onDraw(SkCanvas* canvas) { |
| 103 | fDrawProc(canvas); |
| 104 | } |
| 105 | |
| 106 | SkISize skiagm::SimpleGM::onISize() { |
| 107 | return fSize; |
| 108 | } |
| 109 | |
| 110 | SkString skiagm::SimpleGM::onShortName() { |
| 111 | return fName; |
| 112 | } |
Mike Klein | c9eace8 | 2018-10-31 10:49:38 -0400 | [diff] [blame] | 113 | |
| 114 | template <typename Fn> |
| 115 | static void mark(SkCanvas* canvas, SkScalar x, SkScalar y, Fn&& fn) { |
| 116 | SkPaint alpha; |
| 117 | alpha.setAlpha(0x50); |
| 118 | canvas->saveLayer(nullptr, &alpha); |
| 119 | canvas->translate(x,y); |
| 120 | canvas->scale(2,2); |
| 121 | fn(); |
| 122 | canvas->restore(); |
| 123 | } |
| 124 | |
| 125 | void MarkGMGood(SkCanvas* canvas, SkScalar x, SkScalar y) { |
| 126 | mark(canvas, x,y, [&]{ |
| 127 | SkPaint paint; |
| 128 | |
| 129 | // A green circle. |
| 130 | paint.setColor(SkColorSetRGB(27, 158, 119)); |
| 131 | canvas->drawCircle(0,0, 12, paint); |
| 132 | |
| 133 | // Cut out a check mark. |
| 134 | paint.setBlendMode(SkBlendMode::kSrc); |
| 135 | paint.setColor(0x00000000); |
| 136 | paint.setStrokeWidth(2); |
| 137 | paint.setStyle(SkPaint::kStroke_Style); |
| 138 | canvas->drawLine(-6, 0, |
| 139 | -1, 5, paint); |
| 140 | canvas->drawLine(-1, +5, |
| 141 | +7, -5, paint); |
| 142 | }); |
| 143 | } |
| 144 | |
| 145 | void MarkGMBad(SkCanvas* canvas, SkScalar x, SkScalar y) { |
| 146 | mark(canvas, x,y, [&] { |
| 147 | SkPaint paint; |
| 148 | |
| 149 | // A red circle. |
| 150 | paint.setColor(SkColorSetRGB(231, 41, 138)); |
| 151 | canvas->drawCircle(0,0, 12, paint); |
| 152 | |
| 153 | // Cut out an 'X'. |
| 154 | paint.setBlendMode(SkBlendMode::kSrc); |
| 155 | paint.setColor(0x00000000); |
| 156 | paint.setStrokeWidth(2); |
| 157 | paint.setStyle(SkPaint::kStroke_Style); |
| 158 | canvas->drawLine(-5,-5, |
| 159 | +5,+5, paint); |
| 160 | canvas->drawLine(+5,-5, |
| 161 | -5,+5, paint); |
| 162 | }); |
| 163 | } |