blob: b196b91783b7c04558302176b96fd5e52097d915 [file] [log] [blame]
bsalomon@google.com48dd1a22011-10-31 14:18:20 +00001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#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 Kleinc0bd9f92019-04-23 12:05:21 -050018#include "include/core/SkShader.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040019#include "include/core/SkTileMode.h"
20#include "include/core/SkTypeface.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "include/gpu/GrContext.h"
22#include "src/core/SkTraceEvent.h"
23#include "tools/ToolUtils.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040024
25#include <stdarg.h>
26
27class GrRenderTargetContext;
28
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000029using namespace skiagm;
30
Chris Dalton50e24d72019-02-07 16:20:09 -070031constexpr char GM::kErrorMsg_DrawSkippedGpuOnly[];
32
33static 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 Wagner51e15a62019-05-07 15:38:46 -040045 font.measureText(failureMsg.c_str(), failureMsg.size(), SkTextEncoding::kUTF8, &bounds);
Hal Canary0a7b3932019-05-02 11:31:28 -040046 SkPaint textPaint(SkColors::kWhite);
Chris Dalton50e24d72019-02-07 16:20:09 -070047 canvas->drawString(failureMsg, kOffset, bounds.height() + kOffset, font, textPaint);
48}
49
50static 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 Kleinea3f0142019-03-20 11:12:10 -050055 SkFont font(ToolUtils::create_portable_typeface(), 20);
Hal Canary0a7b3932019-05-02 11:31:28 -040056 SkPaint paint(SkColors::kRed);
Chris Dalton50e24d72019-02-07 16:20:09 -070057 bmpCanvas.drawString("GPU Only", 20, 40, font, paint);
58 SkMatrix localM;
59 localM.setRotate(35.f);
60 localM.postTranslate(10.f, 0.f);
Mike Reed50acf8f2019-04-08 13:20:23 -040061 paint.setShader(bmp.makeShader(SkTileMode::kMirror, SkTileMode::kMirror, &localM));
Chris Dalton50e24d72019-02-07 16:20:09 -070062 paint.setFilterQuality(kMedium_SkFilterQuality);
63 canvas->drawPaint(paint);
64}
65
Chris Dalton3a778372019-02-07 15:23:36 -070066GM::GM(SkColor bgColor) {
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +000067 fMode = kGM_Mode;
Chris Dalton3a778372019-02-07 15:23:36 -070068 fBGColor = bgColor;
reed@google.com7775d662012-11-27 15:15:58 +000069 fHaveCalledOnceBeforeDraw = false;
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000070}
tfarina880914c2014-06-09 12:05:34 -070071
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000072GM::~GM() {}
73
Chris Dalton50e24d72019-02-07 16:20:09 -070074DrawResult GM::draw(SkCanvas* canvas, SkString* errorMsg) {
Mike Kleinb323a5e2017-07-24 15:21:31 -040075 TRACE_EVENT1("GM", TRACE_FUNC, "name", TRACE_STR_COPY(this->getName()));
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000076 this->drawBackground(canvas);
Chris Dalton50e24d72019-02-07 16:20:09 -070077 return this->drawContent(canvas, errorMsg);
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000078}
79
Chris Dalton50e24d72019-02-07 16:20:09 -070080DrawResult GM::drawContent(SkCanvas* canvas, SkString* errorMsg) {
Mike Kleinb323a5e2017-07-24 15:21:31 -040081 TRACE_EVENT0("GM", TRACE_FUNC);
reed@google.com7775d662012-11-27 15:15:58 +000082 if (!fHaveCalledOnceBeforeDraw) {
83 fHaveCalledOnceBeforeDraw = true;
84 this->onOnceBeforeDraw();
85 }
Mike Reed36c4fb32018-12-18 11:48:01 -050086 SkAutoCanvasRestore acr(canvas, true);
Chris Dalton50e24d72019-02-07 16:20:09 -070087 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.com48dd1a22011-10-31 14:18:20 +000098}
99
100void GM::drawBackground(SkCanvas* canvas) {
Mike Kleinb323a5e2017-07-24 15:21:31 -0400101 TRACE_EVENT0("GM", TRACE_FUNC);
reed@google.com7775d662012-11-27 15:15:58 +0000102 if (!fHaveCalledOnceBeforeDraw) {
103 fHaveCalledOnceBeforeDraw = true;
104 this->onOnceBeforeDraw();
105 }
Mike Reed36c4fb32018-12-18 11:48:01 -0500106 SkAutoCanvasRestore acr(canvas, true);
Chris Dalton21ca3702019-02-01 12:15:42 -0700107 canvas->drawColor(fBGColor, SkBlendMode::kSrc);
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000108}
109
Mike Klein9707e902019-02-07 16:18:22 -0500110DrawResult GM::onDraw(SkCanvas* canvas, SkString* errorMsg) {
111 this->onDraw(canvas);
112 return DrawResult::kOk;
113}
114void GM::onDraw(SkCanvas*) { SK_ABORT("Not implemented."); }
115
116
117SkISize SimpleGM::onISize() { return fSize; }
118SkString SimpleGM::onShortName() { return fName; }
119DrawResult SimpleGM::onDraw(SkCanvas* canvas, SkString* errorMsg) {
120 return fDrawProc(canvas, errorMsg);
121}
122
123SkISize SimpleGpuGM::onISize() { return fSize; }
124SkString SimpleGpuGM::onShortName() { return fName; }
125DrawResult SimpleGpuGM::onDraw(GrContext* ctx, GrRenderTargetContext* rtc, SkCanvas* canvas,
126 SkString* errorMsg) {
127 return fDrawProc(ctx, rtc, canvas, errorMsg);
128}
129
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000130const char* GM::getName() {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000131 if (fShortName.size() == 0) {
132 fShortName = this->onShortName();
133 }
134 return fShortName.c_str();
135}
136
137void GM::setBGColor(SkColor color) {
138 fBGColor = color;
139}
140
Hal Canary41248072019-07-11 16:32:53 -0400141bool GM::animate(double nanos) { return this->onAnimate(nanos); }
reedd9adfe62015-02-01 19:01:04 -0800142
Mike Klein9707e902019-02-07 16:18:22 -0500143bool GM::runAsBench() const { return false; }
144void GM::modifyGrContextOptions(GrContextOptions* options) {}
145
146void GM::onOnceBeforeDraw() {}
147
Hal Canary41248072019-07-11 16:32:53 -0400148bool GM::onAnimate(double /*nanos*/) { return false; }
Hal Canaryc74a5502019-07-08 14:55:15 -0400149
150bool GM::onChar(SkUnichar uni) { return false; }
151
Mike Klein9707e902019-02-07 16:18:22 -0500152bool GM::onGetControls(SkMetaData*) { return false; }
Hal Canaryc74a5502019-07-08 14:55:15 -0400153
Mike Klein9707e902019-02-07 16:18:22 -0500154void GM::onSetControls(const SkMetaData&) {}
155
reedd9adfe62015-02-01 19:01:04 -0800156/////////////////////////////////////////////////////////////////////////////////////////////
157
reed@google.com2d6ef522012-01-03 17:20:38 +0000158void GM::drawSizeBounds(SkCanvas* canvas, SkColor color) {
Hal Canary0a7b3932019-05-02 11:31:28 -0400159 canvas->drawRect(SkRect::Make(this->getISize()), SkPaint(SkColor4f::FromColor(color)));
reed@google.com2d6ef522012-01-03 17:20:38 +0000160}
161
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000162// need to explicitly declare this, or we get some weird infinite loop llist
tfarinabcbc1782014-06-18 14:32:48 -0700163template GMRegistry* GMRegistry::gHead;
halcanaryf62c6342015-01-12 15:27:46 -0800164
Mike Klein9707e902019-02-07 16:18:22 -0500165DrawResult GpuGM::onDraw(GrContext* ctx, GrRenderTargetContext* rtc, SkCanvas* canvas,
166 SkString* errorMsg) {
167 this->onDraw(ctx, rtc, canvas);
168 return DrawResult::kOk;
169}
170void GpuGM::onDraw(GrContext*, GrRenderTargetContext*, SkCanvas*) {
171 SK_ABORT("Not implemented.");
172}
173
Chris Dalton50e24d72019-02-07 16:20:09 -0700174DrawResult GpuGM::onDraw(SkCanvas* canvas, SkString* errorMsg) {
Chris Dalton3a778372019-02-07 15:23:36 -0700175 GrContext* ctx = canvas->getGrContext();
176 GrRenderTargetContext* rtc = canvas->internal_private_accessTopLayerRenderTargetContext();
177 if (!ctx || !rtc) {
Chris Dalton50e24d72019-02-07 16:20:09 -0700178 *errorMsg = kErrorMsg_DrawSkippedGpuOnly;
179 return DrawResult::kSkip;
Chris Dalton3a778372019-02-07 15:23:36 -0700180 }
181 if (ctx->abandoned()) {
Chris Dalton50e24d72019-02-07 16:20:09 -0700182 *errorMsg = "GrContext abandoned.";
Mike Klein290690c2019-04-02 18:11:57 -0400183 return DrawResult::kSkip;
Chris Dalton3a778372019-02-07 15:23:36 -0700184 }
Chris Dalton50e24d72019-02-07 16:20:09 -0700185 return this->onDraw(ctx, rtc, canvas, errorMsg);
halcanaryf62c6342015-01-12 15:27:46 -0800186}
Mike Kleinc9eace82018-10-31 10:49:38 -0400187
188template <typename Fn>
189static 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
199void MarkGMGood(SkCanvas* canvas, SkScalar x, SkScalar y) {
200 mark(canvas, x,y, [&]{
Mike Kleinc9eace82018-10-31 10:49:38 -0400201 // A green circle.
Hal Canary0a7b3932019-05-02 11:31:28 -0400202 canvas->drawCircle(0, 0, 12, SkPaint(SkColor4f::FromColor(SkColorSetRGB(27, 158, 119))));
Mike Kleinc9eace82018-10-31 10:49:38 -0400203
204 // Cut out a check mark.
Hal Canary0a7b3932019-05-02 11:31:28 -0400205 SkPaint paint(SkColors::kTransparent);
Mike Kleinc9eace82018-10-31 10:49:38 -0400206 paint.setBlendMode(SkBlendMode::kSrc);
Mike Kleinc9eace82018-10-31 10:49:38 -0400207 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
216void MarkGMBad(SkCanvas* canvas, SkScalar x, SkScalar y) {
217 mark(canvas, x,y, [&] {
Mike Kleinc9eace82018-10-31 10:49:38 -0400218 // A red circle.
Hal Canary0a7b3932019-05-02 11:31:28 -0400219 canvas->drawCircle(0,0, 12, SkPaint(SkColor4f::FromColor(SkColorSetRGB(231, 41, 138))));
Mike Kleinc9eace82018-10-31 10:49:38 -0400220
221 // Cut out an 'X'.
Hal Canary0a7b3932019-05-02 11:31:28 -0400222 SkPaint paint(SkColors::kTransparent);
Mike Kleinc9eace82018-10-31 10:49:38 -0400223 paint.setBlendMode(SkBlendMode::kSrc);
Mike Kleinc9eace82018-10-31 10:49:38 -0400224 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}