blob: bec5fe6a72db1768ffd1d787a3d25f7ad6db5922 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +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 */
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +00007
reed@android.com00dae862009-06-10 15:38:48 +00008#ifndef skiagm_DEFINED
9#define skiagm_DEFINED
10
Tyler Denniston45f94f82020-02-04 16:09:08 -050011#include "gm/verifiers/gmverifier.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040012#include "include/core/SkColor.h"
13#include "include/core/SkScalar.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/core/SkSize.h"
15#include "include/core/SkString.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040016#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/private/SkMacros.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "tools/Registry.h"
reed@android.com00dae862009-06-10 15:38:48 +000019
Hal Canaryedda5652019-08-05 10:28:09 -040020#include <memory>
21
Robert Phillipsb87b39b2020-07-01 14:45:24 -040022class GrDirectContext;
Robert Phillips95c250c2020-06-29 15:36:12 -040023class GrRecordingContext;
Ben Wagner7fde8e12019-05-01 17:28:53 -040024class GrRenderTargetContext;
25class SkCanvas;
Mike Reed88f56712019-02-04 16:50:10 -050026class SkMetaData;
bsalomon4ee6bd82015-05-27 13:23:23 -070027struct GrContextOptions;
mtklein@google.com62b50b72013-09-16 20:42:15 +000028
Hal Canaryedda5652019-08-05 10:28:09 -040029#define DEF_GM(CODE) \
30 static skiagm::GMRegistry SK_MACRO_APPEND_LINE(REG_)(\
31 [](){return std::unique_ptr<skiagm::GM>([](){ CODE ; }());});
reed@google.com4117a242012-10-30 20:26:58 +000032
Chris Dalton3a778372019-02-07 15:23:36 -070033// A Simple GM is a rendering test that does not store state between rendering calls or make use of
34// the onOnceBeforeDraw() virtual; it consists of:
halcanary2a243382015-09-09 08:16:41 -070035// * A name.
36// * Prefered width and height.
37// * Optionally, a background color (default is white).
Chris Dalton3a778372019-02-07 15:23:36 -070038// * A standalone function pointer that implements its onDraw method.
halcanary2a243382015-09-09 08:16:41 -070039#define DEF_SIMPLE_GM(NAME, CANVAS, W, H) \
40 DEF_SIMPLE_GM_BG_NAME(NAME, CANVAS, W, H, SK_ColorWHITE, SkString(#NAME))
Chris Dalton3a778372019-02-07 15:23:36 -070041#define DEF_SIMPLE_GM_BG(NAME, CANVAS, W, H, BGCOLOR) \
halcanary2a243382015-09-09 08:16:41 -070042 DEF_SIMPLE_GM_BG_NAME(NAME, CANVAS, W, H, BGCOLOR, SkString(#NAME))
Chris Dalton3a778372019-02-07 15:23:36 -070043#define DEF_SIMPLE_GM_BG_NAME(NAME, CANVAS, W, H, BGCOLOR, NAME_STR) \
Chris Dalton50e24d72019-02-07 16:20:09 -070044 static void SK_MACRO_CONCAT(NAME,_GM_inner)(SkCanvas*); \
45 DEF_SIMPLE_GM_BG_NAME_CAN_FAIL(NAME, CANVAS,, W, H, BGCOLOR, NAME_STR) { \
46 SK_MACRO_CONCAT(NAME,_GM_inner)(CANVAS); \
47 return skiagm::DrawResult::kOk; \
48 } \
49 void SK_MACRO_CONCAT(NAME,_GM_inner)(SkCanvas* CANVAS)
50
51#define DEF_SIMPLE_GM_CAN_FAIL(NAME, CANVAS, ERR_MSG, W, H) \
52 DEF_SIMPLE_GM_BG_NAME_CAN_FAIL(NAME, CANVAS, ERR_MSG, W, H, SK_ColorWHITE, SkString(#NAME))
53#define DEF_SIMPLE_GM_BG_CAN_FAIL(NAME, CANVAS, ERR_MSG, W, H, BGCOLOR) \
54 DEF_SIMPLE_GM_BG_NAME_CAN_FAIL(NAME, CANVAS, ERR_MSG, W, H, BGCOLOR, SkString(#NAME))
55#define DEF_SIMPLE_GM_BG_NAME_CAN_FAIL(NAME, CANVAS, ERR_MSG, W, H, BGCOLOR, NAME_STR) \
56 static skiagm::DrawResult SK_MACRO_CONCAT(NAME,_GM)(SkCanvas*, SkString*); \
57 DEF_GM(return new skiagm::SimpleGM(BGCOLOR, NAME_STR, {W,H}, SK_MACRO_CONCAT(NAME,_GM));) \
58 skiagm::DrawResult SK_MACRO_CONCAT(NAME,_GM)(SkCanvas* CANVAS, SkString* ERR_MSG)
59
halcanary30b83d42014-10-26 05:23:53 -070060
Chris Dalton3a778372019-02-07 15:23:36 -070061// A Simple GpuGM makes direct GPU calls. Its onDraw hook that includes GPU objects as params, and
62// is only invoked on GPU configs. Non-GPU configs automatically draw a GPU-only message and abort.
63#define DEF_SIMPLE_GPU_GM(NAME, GR_CONTEXT, RENDER_TARGET_CONTEXT, CANVAS, W, H) \
64 DEF_SIMPLE_GPU_GM_BG(NAME, GR_CONTEXT, RENDER_TARGET_CONTEXT, CANVAS, W, H, SK_ColorWHITE)
Chris Dalton50e24d72019-02-07 16:20:09 -070065#define DEF_SIMPLE_GPU_GM_BG(NAME, GR_CONTEXT, RENDER_TARGET_CONTEXT, CANVAS, W, H, BGCOLOR) \
Robert Phillips95c250c2020-06-29 15:36:12 -040066 static void SK_MACRO_CONCAT(NAME,_GM_inner)(GrRecordingContext*, GrRenderTargetContext*, \
67 SkCanvas*); \
Chris Dalton50e24d72019-02-07 16:20:09 -070068 DEF_SIMPLE_GPU_GM_BG_CAN_FAIL(NAME, GR_CONTEXT, RENDER_TARGET_CONTEXT, CANVAS,, W, H, \
69 BGCOLOR) { \
70 SK_MACRO_CONCAT(NAME,_GM_inner)(GR_CONTEXT, RENDER_TARGET_CONTEXT, CANVAS); \
71 return skiagm::DrawResult::kOk; \
72 } \
73 void SK_MACRO_CONCAT(NAME,_GM_inner)( \
Robert Phillips95c250c2020-06-29 15:36:12 -040074 GrRecordingContext* GR_CONTEXT, GrRenderTargetContext* RENDER_TARGET_CONTEXT, \
75 SkCanvas* CANVAS)
Chris Dalton3a778372019-02-07 15:23:36 -070076
Chris Dalton50e24d72019-02-07 16:20:09 -070077#define DEF_SIMPLE_GPU_GM_CAN_FAIL(NAME, GR_CONTEXT, RENDER_TARGET_CONTEXT, CANVAS, ERR_MSG, W, H) \
78 DEF_SIMPLE_GPU_GM_BG_CAN_FAIL(NAME, GR_CONTEXT, RENDER_TARGET_CONTEXT, CANVAS, \
79 ERR_MSG, W, H, SK_ColorWHITE)
80#define DEF_SIMPLE_GPU_GM_BG_CAN_FAIL(NAME, GR_CONTEXT, RENDER_TARGET_CONTEXT, CANVAS, ERR_MSG, W, \
81 H, BGCOLOR) \
82 static skiagm::DrawResult SK_MACRO_CONCAT(NAME,_GM)( \
Robert Phillips95c250c2020-06-29 15:36:12 -040083 GrRecordingContext*, GrRenderTargetContext*, SkCanvas*, SkString*); \
Chris Dalton50e24d72019-02-07 16:20:09 -070084 DEF_GM(return new skiagm::SimpleGpuGM(BGCOLOR, SkString(#NAME), {W,H}, \
85 SK_MACRO_CONCAT(NAME,_GM));) \
86 skiagm::DrawResult SK_MACRO_CONCAT(NAME,_GM)( \
Robert Phillips95c250c2020-06-29 15:36:12 -040087 GrRecordingContext* GR_CONTEXT, GrRenderTargetContext* RENDER_TARGET_CONTEXT, \
88 SkCanvas* CANVAS, SkString* ERR_MSG)
Chris Dalton50e24d72019-02-07 16:20:09 -070089
reed@android.com00dae862009-06-10 15:38:48 +000090namespace skiagm {
rmistry@google.comd6176b02012-08-23 18:14:13 +000091
Chris Dalton50e24d72019-02-07 16:20:09 -070092 enum class DrawResult {
Robert Phillips83b749a2020-06-25 11:41:19 -040093 kOk, // Test drew successfully.
94 kFail, // Test failed to draw.
Chris Dalton50e24d72019-02-07 16:20:09 -070095 kSkip // Test is not applicable in this context and should be skipped.
96 };
97
reed@android.com00dae862009-06-10 15:38:48 +000098 class GM {
99 public:
Chris Dalton50e24d72019-02-07 16:20:09 -0700100 using DrawResult = skiagm::DrawResult;
101
Chris Dalton3a778372019-02-07 15:23:36 -0700102 GM(SkColor backgroundColor = SK_ColorWHITE);
reed@android.com00dae862009-06-10 15:38:48 +0000103 virtual ~GM();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000104
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000105 enum Mode {
106 kGM_Mode,
107 kSample_Mode,
108 kBench_Mode,
109 };
110
111 void setMode(Mode mode) { fMode = mode; }
112 Mode getMode() const { return fMode; }
113
Chris Dalton50e24d72019-02-07 16:20:09 -0700114 static constexpr char kErrorMsg_DrawSkippedGpuOnly[] = "This test is for GPU configs only.";
115
Robert Phillipsb87b39b2020-07-01 14:45:24 -0400116 DrawResult gpuSetup(GrDirectContext* context, SkCanvas* canvas) {
Robert Phillipsd26d25e2020-06-25 13:26:22 -0400117 SkString errorMsg;
118 return this->gpuSetup(context, canvas, &errorMsg);
119 }
Robert Phillipsb87b39b2020-07-01 14:45:24 -0400120 DrawResult gpuSetup(GrDirectContext*, SkCanvas*, SkString* errorMsg);
Robert Phillipsb795bea2020-06-25 12:38:53 -0400121 void gpuTeardown();
Robert Phillips889d6132020-06-16 11:11:33 -0400122
Robert Phillips83b749a2020-06-25 11:41:19 -0400123 void onceBeforeDraw() {
124 if (!fHaveCalledOnceBeforeDraw) {
125 fHaveCalledOnceBeforeDraw = true;
126 this->onOnceBeforeDraw();
127 }
128 }
129
Chris Dalton50e24d72019-02-07 16:20:09 -0700130 DrawResult draw(SkCanvas* canvas) {
131 SkString errorMsg;
132 return this->draw(canvas, &errorMsg);
133 }
134 DrawResult draw(SkCanvas*, SkString* errorMsg);
135
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000136 void drawBackground(SkCanvas*);
Chris Dalton50e24d72019-02-07 16:20:09 -0700137 DrawResult drawContent(SkCanvas* canvas) {
138 SkString errorMsg;
139 return this->drawContent(canvas, &errorMsg);
140 }
141 DrawResult drawContent(SkCanvas*, SkString* errorMsg);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000142
mtkleinf4ba3212015-01-28 15:32:24 -0800143 SkISize getISize() { return this->onISize(); }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000144 const char* getName();
reed@android.com00dae862009-06-10 15:38:48 +0000145
Mike Klein9707e902019-02-07 16:18:22 -0500146 virtual bool runAsBench() const;
mtkleincf5d9c92015-01-23 10:31:45 -0800147
reed@google.com487b5602013-02-01 15:01:24 +0000148 SkScalar width() {
149 return SkIntToScalar(this->getISize().width());
150 }
151 SkScalar height() {
scroggo@google.comab026272013-04-12 22:14:03 +0000152 return SkIntToScalar(this->getISize().height());
reed@google.com487b5602013-02-01 15:01:24 +0000153 }
154
reed@google.comb4b49cc2011-12-06 16:15:42 +0000155 SkColor getBGColor() const { return fBGColor; }
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000156 void setBGColor(SkColor);
reed@google.com30db5992011-08-29 17:41:02 +0000157
Chris Dalton3a778372019-02-07 15:23:36 -0700158 // helper: fill a rect in the specified color based on the GM's getISize bounds.
reed@google.com2d6ef522012-01-03 17:20:38 +0000159 void drawSizeBounds(SkCanvas*, SkColor);
160
Hal Canary41248072019-07-11 16:32:53 -0400161 bool animate(double /*nanos*/);
Hal Canaryc74a5502019-07-08 14:55:15 -0400162 virtual bool onChar(SkUnichar);
reedd9adfe62015-02-01 19:01:04 -0800163
Mike Reed81f60ec2018-05-15 10:09:52 -0400164 bool getControls(SkMetaData* controls) { return this->onGetControls(controls); }
165 void setControls(const SkMetaData& controls) { this->onSetControls(controls); }
166
Chris Dalton5b5403e2019-06-05 11:54:39 -0600167 virtual void modifyGrContextOptions(GrContextOptions*);
bsalomon4ee6bd82015-05-27 13:23:23 -0700168
Tyler Denniston45f94f82020-02-04 16:09:08 -0500169 virtual std::unique_ptr<verifiers::VerifierList> getVerifiers() const;
170
halcanary2a243382015-09-09 08:16:41 -0700171 protected:
Robert Phillips889d6132020-06-16 11:11:33 -0400172 // onGpuSetup is called once before any other processing with a direct context.
Robert Phillipsb87b39b2020-07-01 14:45:24 -0400173 virtual DrawResult onGpuSetup(GrDirectContext*, SkString*) { return DrawResult::kOk; }
Robert Phillipsb795bea2020-06-25 12:38:53 -0400174 virtual void onGpuTeardown() {}
Mike Klein9707e902019-02-07 16:18:22 -0500175 virtual void onOnceBeforeDraw();
Robert Phillips889d6132020-06-16 11:11:33 -0400176 virtual DrawResult onDraw(SkCanvas*, SkString* errorMsg);
Mike Klein9707e902019-02-07 16:18:22 -0500177 virtual void onDraw(SkCanvas*);
178
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000179 virtual SkISize onISize() = 0;
reed@android.com8015dd82009-06-21 00:49:18 +0000180 virtual SkString onShortName() = 0;
mtklein1c402922015-01-23 11:07:07 -0800181
Hal Canary41248072019-07-11 16:32:53 -0400182 virtual bool onAnimate(double /*nanos*/);
Mike Klein9707e902019-02-07 16:18:22 -0500183 virtual bool onGetControls(SkMetaData*);
184 virtual void onSetControls(const SkMetaData&);
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000185
reed@android.com8015dd82009-06-21 00:49:18 +0000186 private:
Robert Phillipse9229532020-06-26 10:10:49 -0400187 Mode fMode;
188 SkString fShortName;
189 SkColor fBGColor;
190 bool fHaveCalledOnceBeforeDraw = false;
191 bool fGpuSetup = false;
192 DrawResult fGpuSetupResult = DrawResult::kOk;
reed@android.com00dae862009-06-10 15:38:48 +0000193 };
194
Hal Canaryedda5652019-08-05 10:28:09 -0400195 using GMFactory = std::unique_ptr<skiagm::GM> (*)();
196 using GMRegistry = sk_tools::Registry<GMFactory>;
halcanaryf62c6342015-01-12 15:27:46 -0800197
Chris Dalton3a778372019-02-07 15:23:36 -0700198 // A GpuGM replaces the onDraw method with one that also accepts GPU objects alongside the
199 // SkCanvas. Its onDraw is only invoked on GPU configs; on non-GPU configs it will automatically
200 // draw a GPU-only message and abort.
201 class GpuGM : public GM {
halcanaryf62c6342015-01-12 15:27:46 -0800202 public:
Chris Dalton3a778372019-02-07 15:23:36 -0700203 GpuGM(SkColor backgroundColor = SK_ColorWHITE) : GM(backgroundColor) {}
Tyler Denniston45f94f82020-02-04 16:09:08 -0500204
205 // TODO(tdenniston): Currently GpuGMs don't have verifiers (because they do not render on
206 // CPU), but we may want to be able to verify the output images standalone, without
207 // requiring a gold image for comparison.
208 std::unique_ptr<verifiers::VerifierList> getVerifiers() const override { return nullptr; }
209
Chris Dalton382b1222019-02-07 10:05:55 +0000210 private:
Chris Dalton50e24d72019-02-07 16:20:09 -0700211 using GM::onDraw;
212 DrawResult onDraw(SkCanvas*, SkString* errorMsg) final;
Mike Klein9707e902019-02-07 16:18:22 -0500213
Robert Phillips95c250c2020-06-29 15:36:12 -0400214 virtual DrawResult onDraw(GrRecordingContext*, GrRenderTargetContext*, SkCanvas*,
Mike Klein9707e902019-02-07 16:18:22 -0500215 SkString* errorMsg);
Robert Phillips95c250c2020-06-29 15:36:12 -0400216 virtual void onDraw(GrRecordingContext*, GrRenderTargetContext*, SkCanvas*);
Chris Daltonf5efa782019-02-05 17:42:14 -0700217 };
Chris Dalton3a778372019-02-07 15:23:36 -0700218
219 // SimpleGM is intended for basic GMs that can define their entire implementation inside a
220 // single "draw" function pointer.
221 class SimpleGM : public GM {
222 public:
Chris Dalton50e24d72019-02-07 16:20:09 -0700223 using DrawProc = DrawResult(*)(SkCanvas*, SkString*);
Chris Dalton3a778372019-02-07 15:23:36 -0700224 SimpleGM(SkColor bgColor, const SkString& name, const SkISize& size, DrawProc drawProc)
225 : GM(bgColor), fName(name), fSize(size), fDrawProc(drawProc) {}
226
227 private:
Mike Klein9707e902019-02-07 16:18:22 -0500228 SkISize onISize() override;
229 SkString onShortName() override;
230 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override;
Chris Dalton3a778372019-02-07 15:23:36 -0700231
232 const SkString fName;
233 const SkISize fSize;
234 const DrawProc fDrawProc;
235 };
236
237 class SimpleGpuGM : public GpuGM {
238 public:
Robert Phillips95c250c2020-06-29 15:36:12 -0400239 using DrawProc = DrawResult(*)(GrRecordingContext*, GrRenderTargetContext*,
240 SkCanvas*, SkString* errorMsg);
Chris Dalton3a778372019-02-07 15:23:36 -0700241 SimpleGpuGM(SkColor bgColor, const SkString& name, const SkISize& size, DrawProc drawProc)
242 : GpuGM(bgColor), fName(name), fSize(size), fDrawProc(drawProc) {}
243
244 private:
Mike Klein9707e902019-02-07 16:18:22 -0500245 SkISize onISize() override;
246 SkString onShortName() override;
Robert Phillips95c250c2020-06-29 15:36:12 -0400247 DrawResult onDraw(GrRecordingContext* ctx, GrRenderTargetContext* rtc, SkCanvas* canvas,
Mike Klein9707e902019-02-07 16:18:22 -0500248 SkString* errorMsg) override;
Chris Dalton3a778372019-02-07 15:23:36 -0700249
250 const SkString fName;
251 const SkISize fSize;
252 const DrawProc fDrawProc;
253 };
reed@android.com00dae862009-06-10 15:38:48 +0000254}
255
Mike Kleinc9eace82018-10-31 10:49:38 -0400256void MarkGMGood(SkCanvas*, SkScalar x, SkScalar y);
257void MarkGMBad (SkCanvas*, SkScalar x, SkScalar y);
258
reed@android.com00dae862009-06-10 15:38:48 +0000259#endif