blob: 6bf34cb4b8353f5eb827340e3ff7701f0d69bbb1 [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"
Kevin Lubick4c491702023-04-24 09:08:06 -04009
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkBitmap.h"
11#include "include/core/SkBlendMode.h"
12#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#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"
Kevin Lubick4c491702023-04-24 09:08:06 -040018#include "include/core/SkSamplingOptions.h"
19#include "include/core/SkShader.h" // IWYU pragma: keep
Ben Wagner7fde8e12019-05-01 17:28:53 -040020#include "include/core/SkTileMode.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/core/SkTraceEvent.h"
22#include "tools/ToolUtils.h"
Kevin Lubicke836c3a2023-10-20 06:55:35 -040023#include "tools/fonts/FontToolUtils.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040024
Leandro Lovisoloaef875a2023-06-16 15:36:55 +000025#if defined(SK_GANESH)
26#include "include/gpu/GrRecordingContext.h"
27#endif
28
Kevin Lubick4c491702023-04-24 09:08:06 -040029#include <cstdarg>
30#include <cstdint>
Ben Wagner7fde8e12019-05-01 17:28:53 -040031
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000032using namespace skiagm;
33
John Stiles87aa7a92022-02-04 18:17:59 -050034static void draw_failure_message(SkCanvas* canvas, const char format[], ...) SK_PRINTF_LIKE(2, 3);
35
36static void draw_failure_message(SkCanvas* canvas, const char format[], ...) {
Chris Dalton50e24d72019-02-07 16:20:09 -070037 SkString failureMsg;
38
39 va_list argp;
40 va_start(argp, format);
41 failureMsg.appendVAList(format, argp);
42 va_end(argp);
43
44 constexpr SkScalar kOffset = 5.0f;
45 canvas->drawColor(SkColorSetRGB(200,0,0));
Kevin Lubickbca43ec2023-10-30 10:11:22 -040046 SkFont font = ToolUtils::DefaultPortableFont();
Chris Dalton50e24d72019-02-07 16:20:09 -070047 SkRect bounds;
Ben Wagner51e15a62019-05-07 15:38:46 -040048 font.measureText(failureMsg.c_str(), failureMsg.size(), SkTextEncoding::kUTF8, &bounds);
Hal Canary0a7b3932019-05-02 11:31:28 -040049 SkPaint textPaint(SkColors::kWhite);
Chris Dalton50e24d72019-02-07 16:20:09 -070050 canvas->drawString(failureMsg, kOffset, bounds.height() + kOffset, font, textPaint);
51}
52
53static void draw_gpu_only_message(SkCanvas* canvas) {
54 SkBitmap bmp;
55 bmp.allocN32Pixels(128, 64);
56 SkCanvas bmpCanvas(bmp);
57 bmpCanvas.drawColor(SK_ColorWHITE);
Kevin Lubicke836c3a2023-10-20 06:55:35 -040058 SkFont font(ToolUtils::DefaultPortableTypeface(), 20);
Hal Canary0a7b3932019-05-02 11:31:28 -040059 SkPaint paint(SkColors::kRed);
Chris Dalton50e24d72019-02-07 16:20:09 -070060 bmpCanvas.drawString("GPU Only", 20, 40, font, paint);
61 SkMatrix localM;
62 localM.setRotate(35.f);
63 localM.postTranslate(10.f, 0.f);
Mike Reedb41bd152020-12-12 11:18:31 -050064 paint.setShader(bmp.makeShader(SkTileMode::kMirror, SkTileMode::kMirror,
65 SkSamplingOptions(SkFilterMode::kLinear,
66 SkMipmapMode::kNearest),
67 localM));
Chris Dalton50e24d72019-02-07 16:20:09 -070068 canvas->drawPaint(paint);
69}
70
Robert Phillipsd26d25e2020-06-25 13:26:22 -040071static void handle_gm_failure(SkCanvas* canvas, DrawResult result, const SkString& errorMsg) {
72 if (DrawResult::kFail == result) {
73 draw_failure_message(canvas, "DRAW FAILED: %s", errorMsg.c_str());
74 } else if (SkString(GM::kErrorMsg_DrawSkippedGpuOnly) == errorMsg) {
75 draw_gpu_only_message(canvas);
76 } else {
77 draw_failure_message(canvas, "DRAW SKIPPED: %s", errorMsg.c_str());
78 }
79}
80
Chris Dalton3a778372019-02-07 15:23:36 -070081GM::GM(SkColor bgColor) {
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +000082 fMode = kGM_Mode;
Chris Dalton3a778372019-02-07 15:23:36 -070083 fBGColor = bgColor;
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000084}
tfarina880914c2014-06-09 12:05:34 -070085
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000086GM::~GM() {}
87
Brian Salomonc759bbf2023-12-05 11:11:27 -050088DrawResult GM::gpuSetup(SkCanvas* canvas,
89 SkString* errorMsg,
90 GraphiteTestContext* graphiteTestContext) {
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000091 TRACE_EVENT1("GM", TRACE_FUNC, "name", TRACE_STR_COPY(this->getName().c_str()));
Robert Phillipse9229532020-06-26 10:10:49 -040092 if (!fGpuSetup) {
93 // When drawn in viewer, gpuSetup will be called multiple times with the same
Brian Salomonc759bbf2023-12-05 11:11:27 -050094 // GrContext or graphite::Context.
Robert Phillipse9229532020-06-26 10:10:49 -040095 fGpuSetup = true;
Brian Salomonc759bbf2023-12-05 11:11:27 -050096 fGpuSetupResult = this->onGpuSetup(canvas, errorMsg, graphiteTestContext);
Robert Phillipsd26d25e2020-06-25 13:26:22 -040097 }
Brian Salomonc759bbf2023-12-05 11:11:27 -050098 if (fGpuSetupResult == DrawResult::kOk) {
99 fGraphiteTestContext = graphiteTestContext;
100 } else {
Robert Phillipse9229532020-06-26 10:10:49 -0400101 handle_gm_failure(canvas, fGpuSetupResult, *errorMsg);
102 }
103
104 return fGpuSetupResult;
Robert Phillips889d6132020-06-16 11:11:33 -0400105}
106
Robert Phillipsb795bea2020-06-25 12:38:53 -0400107void GM::gpuTeardown() {
108 this->onGpuTeardown();
Robert Phillipse9229532020-06-26 10:10:49 -0400109
Brian Salomonc759bbf2023-12-05 11:11:27 -0500110 // After 'gpuTeardown' a GM can be reused with a different GrContext or graphite::Context. Reset
111 // the flag so 'onGpuSetup' will be called.
Robert Phillipse9229532020-06-26 10:10:49 -0400112 fGpuSetup = false;
Brian Salomonc759bbf2023-12-05 11:11:27 -0500113 fGraphiteTestContext = nullptr;
Robert Phillipsb795bea2020-06-25 12:38:53 -0400114}
115
Chris Dalton50e24d72019-02-07 16:20:09 -0700116DrawResult GM::draw(SkCanvas* canvas, SkString* errorMsg) {
Leandro Lovisolo24fa2112023-08-15 19:05:17 +0000117 TRACE_EVENT1("GM", TRACE_FUNC, "name", TRACE_STR_COPY(this->getName().c_str()));
James Godfrey-Kittleb47d7ed2023-02-14 11:09:38 -0500118 this->drawBackground(canvas);
119 return this->drawContent(canvas, errorMsg);
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000120}
121
Chris Dalton50e24d72019-02-07 16:20:09 -0700122DrawResult GM::drawContent(SkCanvas* canvas, SkString* errorMsg) {
Mike Kleinb323a5e2017-07-24 15:21:31 -0400123 TRACE_EVENT0("GM", TRACE_FUNC);
Robert Phillips83b749a2020-06-25 11:41:19 -0400124 this->onceBeforeDraw();
Mike Reed36c4fb32018-12-18 11:48:01 -0500125 SkAutoCanvasRestore acr(canvas, true);
James Godfrey-Kittleb47d7ed2023-02-14 11:09:38 -0500126 DrawResult drawResult = this->onDraw(canvas, errorMsg);
Chris Dalton50e24d72019-02-07 16:20:09 -0700127 if (DrawResult::kOk != drawResult) {
Robert Phillipsd26d25e2020-06-25 13:26:22 -0400128 handle_gm_failure(canvas, drawResult, *errorMsg);
Chris Dalton50e24d72019-02-07 16:20:09 -0700129 }
130 return drawResult;
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000131}
132
133void GM::drawBackground(SkCanvas* canvas) {
Mike Kleinb323a5e2017-07-24 15:21:31 -0400134 TRACE_EVENT0("GM", TRACE_FUNC);
Robert Phillips83b749a2020-06-25 11:41:19 -0400135 this->onceBeforeDraw();
Chris Dalton21ca3702019-02-01 12:15:42 -0700136 canvas->drawColor(fBGColor, SkBlendMode::kSrc);
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000137}
138
Mike Klein9707e902019-02-07 16:18:22 -0500139DrawResult GM::onDraw(SkCanvas* canvas, SkString* errorMsg) {
140 this->onDraw(canvas);
141 return DrawResult::kOk;
142}
143void GM::onDraw(SkCanvas*) { SK_ABORT("Not implemented."); }
144
Leandro Lovisolo8f023882023-08-15 21:13:52 +0000145SkISize SimpleGM::getISize() { return fSize; }
Leandro Lovisolo24fa2112023-08-15 19:05:17 +0000146SkString SimpleGM::getName() const { return fName; }
Mike Klein9707e902019-02-07 16:18:22 -0500147DrawResult SimpleGM::onDraw(SkCanvas* canvas, SkString* errorMsg) {
148 return fDrawProc(canvas, errorMsg);
149}
150
Leandro Lovisoloaef875a2023-06-16 15:36:55 +0000151#if defined(SK_GANESH)
Leandro Lovisolo8f023882023-08-15 21:13:52 +0000152SkISize SimpleGpuGM::getISize() { return fSize; }
Leandro Lovisolo24fa2112023-08-15 19:05:17 +0000153SkString SimpleGpuGM::getName() const { return fName; }
Robert Phillips7a0d3c32021-07-21 15:39:51 -0400154DrawResult SimpleGpuGM::onDraw(GrRecordingContext* rContext, SkCanvas* canvas, SkString* errorMsg) {
155 return fDrawProc(rContext, canvas, errorMsg);
Mike Klein9707e902019-02-07 16:18:22 -0500156}
Leandro Lovisoloaef875a2023-06-16 15:36:55 +0000157#endif
Mike Klein9707e902019-02-07 16:18:22 -0500158
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000159void GM::setBGColor(SkColor color) {
160 fBGColor = color;
161}
162
Hal Canary41248072019-07-11 16:32:53 -0400163bool GM::animate(double nanos) { return this->onAnimate(nanos); }
reedd9adfe62015-02-01 19:01:04 -0800164
Mike Klein9707e902019-02-07 16:18:22 -0500165bool GM::runAsBench() const { return false; }
Mike Klein9707e902019-02-07 16:18:22 -0500166
167void GM::onOnceBeforeDraw() {}
168
Hal Canary41248072019-07-11 16:32:53 -0400169bool GM::onAnimate(double /*nanos*/) { return false; }
Hal Canaryc74a5502019-07-08 14:55:15 -0400170
171bool GM::onChar(SkUnichar uni) { return false; }
172
Mike Klein9707e902019-02-07 16:18:22 -0500173bool GM::onGetControls(SkMetaData*) { return false; }
Hal Canaryc74a5502019-07-08 14:55:15 -0400174
Mike Klein9707e902019-02-07 16:18:22 -0500175void GM::onSetControls(const SkMetaData&) {}
176
reedd9adfe62015-02-01 19:01:04 -0800177/////////////////////////////////////////////////////////////////////////////////////////////
178
reed@google.com2d6ef522012-01-03 17:20:38 +0000179void GM::drawSizeBounds(SkCanvas* canvas, SkColor color) {
Hal Canary0a7b3932019-05-02 11:31:28 -0400180 canvas->drawRect(SkRect::Make(this->getISize()), SkPaint(SkColor4f::FromColor(color)));
reed@google.com2d6ef522012-01-03 17:20:38 +0000181}
182
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000183// need to explicitly declare this, or we get some weird infinite loop llist
tfarinabcbc1782014-06-18 14:32:48 -0700184template GMRegistry* GMRegistry::gHead;
halcanaryf62c6342015-01-12 15:27:46 -0800185
Leandro Lovisoloaef875a2023-06-16 15:36:55 +0000186#if defined(SK_GANESH)
Robert Phillips7a0d3c32021-07-21 15:39:51 -0400187DrawResult GpuGM::onDraw(GrRecordingContext* rContext, SkCanvas* canvas, SkString* errorMsg) {
188 this->onDraw(rContext, canvas);
Mike Klein9707e902019-02-07 16:18:22 -0500189 return DrawResult::kOk;
190}
Robert Phillips7a0d3c32021-07-21 15:39:51 -0400191void GpuGM::onDraw(GrRecordingContext*, SkCanvas*) {
Mike Klein9707e902019-02-07 16:18:22 -0500192 SK_ABORT("Not implemented.");
193}
194
Chris Dalton50e24d72019-02-07 16:20:09 -0700195DrawResult GpuGM::onDraw(SkCanvas* canvas, SkString* errorMsg) {
Robert Phillips95c250c2020-06-29 15:36:12 -0400196
Robert Phillips7a0d3c32021-07-21 15:39:51 -0400197 auto rContext = canvas->recordingContext();
198 if (!rContext) {
Chris Dalton50e24d72019-02-07 16:20:09 -0700199 *errorMsg = kErrorMsg_DrawSkippedGpuOnly;
200 return DrawResult::kSkip;
Chris Dalton3a778372019-02-07 15:23:36 -0700201 }
Robert Phillips7a0d3c32021-07-21 15:39:51 -0400202 if (rContext->abandoned()) {
Chris Dalton50e24d72019-02-07 16:20:09 -0700203 *errorMsg = "GrContext abandoned.";
Mike Klein290690c2019-04-02 18:11:57 -0400204 return DrawResult::kSkip;
Chris Dalton3a778372019-02-07 15:23:36 -0700205 }
Robert Phillips7a0d3c32021-07-21 15:39:51 -0400206 return this->onDraw(rContext, canvas, errorMsg);
halcanaryf62c6342015-01-12 15:27:46 -0800207}
Leandro Lovisoloaef875a2023-06-16 15:36:55 +0000208#endif
Mike Kleinc9eace82018-10-31 10:49:38 -0400209
210template <typename Fn>
211static void mark(SkCanvas* canvas, SkScalar x, SkScalar y, Fn&& fn) {
212 SkPaint alpha;
213 alpha.setAlpha(0x50);
214 canvas->saveLayer(nullptr, &alpha);
215 canvas->translate(x,y);
216 canvas->scale(2,2);
217 fn();
218 canvas->restore();
219}
220
221void MarkGMGood(SkCanvas* canvas, SkScalar x, SkScalar y) {
222 mark(canvas, x,y, [&]{
Mike Kleinc9eace82018-10-31 10:49:38 -0400223 // A green circle.
Hal Canary0a7b3932019-05-02 11:31:28 -0400224 canvas->drawCircle(0, 0, 12, SkPaint(SkColor4f::FromColor(SkColorSetRGB(27, 158, 119))));
Mike Kleinc9eace82018-10-31 10:49:38 -0400225
226 // Cut out a check mark.
Hal Canary0a7b3932019-05-02 11:31:28 -0400227 SkPaint paint(SkColors::kTransparent);
Mike Kleinc9eace82018-10-31 10:49:38 -0400228 paint.setBlendMode(SkBlendMode::kSrc);
Mike Kleinc9eace82018-10-31 10:49:38 -0400229 paint.setStrokeWidth(2);
230 paint.setStyle(SkPaint::kStroke_Style);
231 canvas->drawLine(-6, 0,
232 -1, 5, paint);
233 canvas->drawLine(-1, +5,
234 +7, -5, paint);
235 });
236}
237
238void MarkGMBad(SkCanvas* canvas, SkScalar x, SkScalar y) {
239 mark(canvas, x,y, [&] {
Mike Kleinc9eace82018-10-31 10:49:38 -0400240 // A red circle.
Hal Canary0a7b3932019-05-02 11:31:28 -0400241 canvas->drawCircle(0,0, 12, SkPaint(SkColor4f::FromColor(SkColorSetRGB(231, 41, 138))));
Mike Kleinc9eace82018-10-31 10:49:38 -0400242
243 // Cut out an 'X'.
Hal Canary0a7b3932019-05-02 11:31:28 -0400244 SkPaint paint(SkColors::kTransparent);
Mike Kleinc9eace82018-10-31 10:49:38 -0400245 paint.setBlendMode(SkBlendMode::kSrc);
Mike Kleinc9eace82018-10-31 10:49:38 -0400246 paint.setStrokeWidth(2);
247 paint.setStyle(SkPaint::kStroke_Style);
248 canvas->drawLine(-5,-5,
249 +5,+5, paint);
250 canvas->drawLine(+5,-5,
251 -5,+5, paint);
252 });
253}
Leandro Lovisolo7da0df12023-08-07 19:19:36 +0000254
255namespace skiagm {
256void Register(skiagm::GM* gm) {
257 // The skiagm::GMRegistry class is a subclass of sk_tools::Registry. Instances of
258 // sk_tools::Registry form a linked list (there is one such list for each subclass), where each
259 // instance holds a value and a pointer to the next sk_tools::Registry instance. The head of
260 // this linked list is stored in a global variable. The sk_tools::Registry constructor
261 // automatically pushes a new instance to the head of said linked list. Therefore, in order to
262 // register a value in the GM registry, it suffices to just instantiate skiagm::GMRegistry with
263 // the value we wish to register.
264 new skiagm::GMRegistry([=]() { return std::unique_ptr<skiagm::GM>(gm); });
265}
266} // namespace skiagm