blob: 4d5bb0b36e0648d47e12530933f3857188d5da35 [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
Ben Wagner7fde8e12019-05-01 17:28:53 -040011#include "include/core/SkColor.h"
12#include "include/core/SkScalar.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkSize.h"
14#include "include/core/SkString.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040015#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/private/SkMacros.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "tools/Registry.h"
reed@android.com00dae862009-06-10 15:38:48 +000018
Hal Canaryedda5652019-08-05 10:28:09 -040019#include <memory>
20
Ben Wagner7fde8e12019-05-01 17:28:53 -040021class GrContext;
22class GrRenderTargetContext;
23class SkCanvas;
Mike Reed88f56712019-02-04 16:50:10 -050024class SkMetaData;
bsalomon4ee6bd82015-05-27 13:23:23 -070025struct GrContextOptions;
mtklein@google.com62b50b72013-09-16 20:42:15 +000026
Hal Canaryedda5652019-08-05 10:28:09 -040027#define DEF_GM(CODE) \
28 static skiagm::GMRegistry SK_MACRO_APPEND_LINE(REG_)(\
29 [](){return std::unique_ptr<skiagm::GM>([](){ CODE ; }());});
reed@google.com4117a242012-10-30 20:26:58 +000030
Chris Dalton3a778372019-02-07 15:23:36 -070031// A Simple GM is a rendering test that does not store state between rendering calls or make use of
32// the onOnceBeforeDraw() virtual; it consists of:
halcanary2a243382015-09-09 08:16:41 -070033// * A name.
34// * Prefered width and height.
35// * Optionally, a background color (default is white).
Chris Dalton3a778372019-02-07 15:23:36 -070036// * A standalone function pointer that implements its onDraw method.
halcanary2a243382015-09-09 08:16:41 -070037#define DEF_SIMPLE_GM(NAME, CANVAS, W, H) \
38 DEF_SIMPLE_GM_BG_NAME(NAME, CANVAS, W, H, SK_ColorWHITE, SkString(#NAME))
Chris Dalton3a778372019-02-07 15:23:36 -070039#define DEF_SIMPLE_GM_BG(NAME, CANVAS, W, H, BGCOLOR) \
halcanary2a243382015-09-09 08:16:41 -070040 DEF_SIMPLE_GM_BG_NAME(NAME, CANVAS, W, H, BGCOLOR, SkString(#NAME))
Chris Dalton3a778372019-02-07 15:23:36 -070041#define DEF_SIMPLE_GM_BG_NAME(NAME, CANVAS, W, H, BGCOLOR, NAME_STR) \
Chris Dalton50e24d72019-02-07 16:20:09 -070042 static void SK_MACRO_CONCAT(NAME,_GM_inner)(SkCanvas*); \
43 DEF_SIMPLE_GM_BG_NAME_CAN_FAIL(NAME, CANVAS,, W, H, BGCOLOR, NAME_STR) { \
44 SK_MACRO_CONCAT(NAME,_GM_inner)(CANVAS); \
45 return skiagm::DrawResult::kOk; \
46 } \
47 void SK_MACRO_CONCAT(NAME,_GM_inner)(SkCanvas* CANVAS)
48
49#define DEF_SIMPLE_GM_CAN_FAIL(NAME, CANVAS, ERR_MSG, W, H) \
50 DEF_SIMPLE_GM_BG_NAME_CAN_FAIL(NAME, CANVAS, ERR_MSG, W, H, SK_ColorWHITE, SkString(#NAME))
51#define DEF_SIMPLE_GM_BG_CAN_FAIL(NAME, CANVAS, ERR_MSG, W, H, BGCOLOR) \
52 DEF_SIMPLE_GM_BG_NAME_CAN_FAIL(NAME, CANVAS, ERR_MSG, W, H, BGCOLOR, SkString(#NAME))
53#define DEF_SIMPLE_GM_BG_NAME_CAN_FAIL(NAME, CANVAS, ERR_MSG, W, H, BGCOLOR, NAME_STR) \
54 static skiagm::DrawResult SK_MACRO_CONCAT(NAME,_GM)(SkCanvas*, SkString*); \
55 DEF_GM(return new skiagm::SimpleGM(BGCOLOR, NAME_STR, {W,H}, SK_MACRO_CONCAT(NAME,_GM));) \
56 skiagm::DrawResult SK_MACRO_CONCAT(NAME,_GM)(SkCanvas* CANVAS, SkString* ERR_MSG)
57
halcanary30b83d42014-10-26 05:23:53 -070058
Chris Dalton3a778372019-02-07 15:23:36 -070059// A Simple GpuGM makes direct GPU calls. Its onDraw hook that includes GPU objects as params, and
60// is only invoked on GPU configs. Non-GPU configs automatically draw a GPU-only message and abort.
61#define DEF_SIMPLE_GPU_GM(NAME, GR_CONTEXT, RENDER_TARGET_CONTEXT, CANVAS, W, H) \
62 DEF_SIMPLE_GPU_GM_BG(NAME, GR_CONTEXT, RENDER_TARGET_CONTEXT, CANVAS, W, H, SK_ColorWHITE)
Chris Dalton50e24d72019-02-07 16:20:09 -070063#define DEF_SIMPLE_GPU_GM_BG(NAME, GR_CONTEXT, RENDER_TARGET_CONTEXT, CANVAS, W, H, BGCOLOR) \
64 static void SK_MACRO_CONCAT(NAME,_GM_inner)(GrContext*, GrRenderTargetContext*, SkCanvas*); \
65 DEF_SIMPLE_GPU_GM_BG_CAN_FAIL(NAME, GR_CONTEXT, RENDER_TARGET_CONTEXT, CANVAS,, W, H, \
66 BGCOLOR) { \
67 SK_MACRO_CONCAT(NAME,_GM_inner)(GR_CONTEXT, RENDER_TARGET_CONTEXT, CANVAS); \
68 return skiagm::DrawResult::kOk; \
69 } \
70 void SK_MACRO_CONCAT(NAME,_GM_inner)( \
Chris Dalton3a778372019-02-07 15:23:36 -070071 GrContext* GR_CONTEXT, GrRenderTargetContext* RENDER_TARGET_CONTEXT, SkCanvas* CANVAS)
72
Chris Dalton50e24d72019-02-07 16:20:09 -070073#define DEF_SIMPLE_GPU_GM_CAN_FAIL(NAME, GR_CONTEXT, RENDER_TARGET_CONTEXT, CANVAS, ERR_MSG, W, H) \
74 DEF_SIMPLE_GPU_GM_BG_CAN_FAIL(NAME, GR_CONTEXT, RENDER_TARGET_CONTEXT, CANVAS, \
75 ERR_MSG, W, H, SK_ColorWHITE)
76#define DEF_SIMPLE_GPU_GM_BG_CAN_FAIL(NAME, GR_CONTEXT, RENDER_TARGET_CONTEXT, CANVAS, ERR_MSG, W, \
77 H, BGCOLOR) \
78 static skiagm::DrawResult SK_MACRO_CONCAT(NAME,_GM)( \
79 GrContext*, GrRenderTargetContext*, SkCanvas*, SkString*); \
80 DEF_GM(return new skiagm::SimpleGpuGM(BGCOLOR, SkString(#NAME), {W,H}, \
81 SK_MACRO_CONCAT(NAME,_GM));) \
82 skiagm::DrawResult SK_MACRO_CONCAT(NAME,_GM)( \
83 GrContext* GR_CONTEXT, GrRenderTargetContext* RENDER_TARGET_CONTEXT, SkCanvas* CANVAS, \
84 SkString* ERR_MSG)
85
reed@android.com00dae862009-06-10 15:38:48 +000086namespace skiagm {
rmistry@google.comd6176b02012-08-23 18:14:13 +000087
Chris Dalton50e24d72019-02-07 16:20:09 -070088 enum class DrawResult {
89 kOk, // Test drew successfully.
90 kFail, // Test failed to draw.
91 kSkip // Test is not applicable in this context and should be skipped.
92 };
93
reed@android.com00dae862009-06-10 15:38:48 +000094 class GM {
95 public:
Chris Dalton50e24d72019-02-07 16:20:09 -070096 using DrawResult = skiagm::DrawResult;
97
Chris Dalton3a778372019-02-07 15:23:36 -070098 GM(SkColor backgroundColor = SK_ColorWHITE);
reed@android.com00dae862009-06-10 15:38:48 +000099 virtual ~GM();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000100
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000101 enum Mode {
102 kGM_Mode,
103 kSample_Mode,
104 kBench_Mode,
105 };
106
107 void setMode(Mode mode) { fMode = mode; }
108 Mode getMode() const { return fMode; }
109
Chris Dalton50e24d72019-02-07 16:20:09 -0700110 static constexpr char kErrorMsg_DrawSkippedGpuOnly[] = "This test is for GPU configs only.";
111
112 DrawResult draw(SkCanvas* canvas) {
113 SkString errorMsg;
114 return this->draw(canvas, &errorMsg);
115 }
116 DrawResult draw(SkCanvas*, SkString* errorMsg);
117
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000118 void drawBackground(SkCanvas*);
Chris Dalton50e24d72019-02-07 16:20:09 -0700119 DrawResult drawContent(SkCanvas* canvas) {
120 SkString errorMsg;
121 return this->drawContent(canvas, &errorMsg);
122 }
123 DrawResult drawContent(SkCanvas*, SkString* errorMsg);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000124
mtkleinf4ba3212015-01-28 15:32:24 -0800125 SkISize getISize() { return this->onISize(); }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000126 const char* getName();
reed@android.com00dae862009-06-10 15:38:48 +0000127
Mike Klein9707e902019-02-07 16:18:22 -0500128 virtual bool runAsBench() const;
mtkleincf5d9c92015-01-23 10:31:45 -0800129
reed@google.com487b5602013-02-01 15:01:24 +0000130 SkScalar width() {
131 return SkIntToScalar(this->getISize().width());
132 }
133 SkScalar height() {
scroggo@google.comab026272013-04-12 22:14:03 +0000134 return SkIntToScalar(this->getISize().height());
reed@google.com487b5602013-02-01 15:01:24 +0000135 }
136
reed@google.comb4b49cc2011-12-06 16:15:42 +0000137 SkColor getBGColor() const { return fBGColor; }
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000138 void setBGColor(SkColor);
reed@google.com30db5992011-08-29 17:41:02 +0000139
Chris Dalton3a778372019-02-07 15:23:36 -0700140 // helper: fill a rect in the specified color based on the GM's getISize bounds.
reed@google.com2d6ef522012-01-03 17:20:38 +0000141 void drawSizeBounds(SkCanvas*, SkColor);
142
Hal Canary41248072019-07-11 16:32:53 -0400143 bool animate(double /*nanos*/);
Hal Canaryc74a5502019-07-08 14:55:15 -0400144 virtual bool onChar(SkUnichar);
reedd9adfe62015-02-01 19:01:04 -0800145
Mike Reed81f60ec2018-05-15 10:09:52 -0400146 bool getControls(SkMetaData* controls) { return this->onGetControls(controls); }
147 void setControls(const SkMetaData& controls) { this->onSetControls(controls); }
148
Chris Dalton5b5403e2019-06-05 11:54:39 -0600149 virtual void modifyGrContextOptions(GrContextOptions*);
bsalomon4ee6bd82015-05-27 13:23:23 -0700150
halcanary2a243382015-09-09 08:16:41 -0700151 protected:
Mike Klein9707e902019-02-07 16:18:22 -0500152 virtual void onOnceBeforeDraw();
153 virtual DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg);
154 virtual void onDraw(SkCanvas*);
155
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000156 virtual SkISize onISize() = 0;
reed@android.com8015dd82009-06-21 00:49:18 +0000157 virtual SkString onShortName() = 0;
mtklein1c402922015-01-23 11:07:07 -0800158
Hal Canary41248072019-07-11 16:32:53 -0400159 virtual bool onAnimate(double /*nanos*/);
Mike Klein9707e902019-02-07 16:18:22 -0500160 virtual bool onGetControls(SkMetaData*);
161 virtual void onSetControls(const SkMetaData&);
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000162
reed@android.com8015dd82009-06-21 00:49:18 +0000163 private:
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000164 Mode fMode;
reed@android.com8015dd82009-06-21 00:49:18 +0000165 SkString fShortName;
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000166 SkColor fBGColor;
reed@google.com7775d662012-11-27 15:15:58 +0000167 bool fHaveCalledOnceBeforeDraw;
reed@android.com00dae862009-06-10 15:38:48 +0000168 };
169
Hal Canaryedda5652019-08-05 10:28:09 -0400170 using GMFactory = std::unique_ptr<skiagm::GM> (*)();
171 using GMRegistry = sk_tools::Registry<GMFactory>;
halcanaryf62c6342015-01-12 15:27:46 -0800172
Chris Dalton3a778372019-02-07 15:23:36 -0700173 // A GpuGM replaces the onDraw method with one that also accepts GPU objects alongside the
174 // SkCanvas. Its onDraw is only invoked on GPU configs; on non-GPU configs it will automatically
175 // draw a GPU-only message and abort.
176 class GpuGM : public GM {
halcanaryf62c6342015-01-12 15:27:46 -0800177 public:
Chris Dalton3a778372019-02-07 15:23:36 -0700178 GpuGM(SkColor backgroundColor = SK_ColorWHITE) : GM(backgroundColor) {}
Chris Dalton382b1222019-02-07 10:05:55 +0000179 private:
Chris Dalton50e24d72019-02-07 16:20:09 -0700180 using GM::onDraw;
181 DrawResult onDraw(SkCanvas*, SkString* errorMsg) final;
Mike Klein9707e902019-02-07 16:18:22 -0500182
Chris Dalton50e24d72019-02-07 16:20:09 -0700183 virtual DrawResult onDraw(GrContext* ctx, GrRenderTargetContext* rtc, SkCanvas* canvas,
Mike Klein9707e902019-02-07 16:18:22 -0500184 SkString* errorMsg);
185 virtual void onDraw(GrContext*, GrRenderTargetContext*, SkCanvas*);
Chris Daltonf5efa782019-02-05 17:42:14 -0700186 };
Chris Dalton3a778372019-02-07 15:23:36 -0700187
188 // SimpleGM is intended for basic GMs that can define their entire implementation inside a
189 // single "draw" function pointer.
190 class SimpleGM : public GM {
191 public:
Chris Dalton50e24d72019-02-07 16:20:09 -0700192 using DrawProc = DrawResult(*)(SkCanvas*, SkString*);
Chris Dalton3a778372019-02-07 15:23:36 -0700193 SimpleGM(SkColor bgColor, const SkString& name, const SkISize& size, DrawProc drawProc)
194 : GM(bgColor), fName(name), fSize(size), fDrawProc(drawProc) {}
195
196 private:
Mike Klein9707e902019-02-07 16:18:22 -0500197 SkISize onISize() override;
198 SkString onShortName() override;
199 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override;
Chris Dalton3a778372019-02-07 15:23:36 -0700200
201 const SkString fName;
202 const SkISize fSize;
203 const DrawProc fDrawProc;
204 };
205
206 class SimpleGpuGM : public GpuGM {
207 public:
Chris Dalton50e24d72019-02-07 16:20:09 -0700208 using DrawProc = DrawResult(*)(GrContext*, GrRenderTargetContext*, SkCanvas*, SkString*);
Chris Dalton3a778372019-02-07 15:23:36 -0700209 SimpleGpuGM(SkColor bgColor, const SkString& name, const SkISize& size, DrawProc drawProc)
210 : GpuGM(bgColor), fName(name), fSize(size), fDrawProc(drawProc) {}
211
212 private:
Mike Klein9707e902019-02-07 16:18:22 -0500213 SkISize onISize() override;
214 SkString onShortName() override;
Chris Dalton50e24d72019-02-07 16:20:09 -0700215 DrawResult onDraw(GrContext* ctx, GrRenderTargetContext* rtc, SkCanvas* canvas,
Mike Klein9707e902019-02-07 16:18:22 -0500216 SkString* errorMsg) override;
Chris Dalton3a778372019-02-07 15:23:36 -0700217
218 const SkString fName;
219 const SkISize fSize;
220 const DrawProc fDrawProc;
221 };
reed@android.com00dae862009-06-10 15:38:48 +0000222}
223
Mike Kleinc9eace82018-10-31 10:49:38 -0400224void MarkGMGood(SkCanvas*, SkScalar x, SkScalar y);
225void MarkGMBad (SkCanvas*, SkScalar x, SkScalar y);
226
reed@android.com00dae862009-06-10 15:38:48 +0000227#endif