blob: 070c7606f1efe0ceacceabc1a3c8819033113c65 [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
Mike Kleincd5104e2019-03-20 11:55:08 -050019class AnimTimer;
Ben Wagner7fde8e12019-05-01 17:28:53 -040020class GrContext;
21class GrRenderTargetContext;
22class SkCanvas;
Mike Reed88f56712019-02-04 16:50:10 -050023class SkMetaData;
bsalomon4ee6bd82015-05-27 13:23:23 -070024struct GrContextOptions;
mtklein@google.com62b50b72013-09-16 20:42:15 +000025
reed@google.com4117a242012-10-30 20:26:58 +000026#define DEF_GM(code) \
sugoi@google.com0f0d9b72013-02-27 15:41:12 +000027 static skiagm::GM* SK_MACRO_APPEND_LINE(F_)(void*) { code; } \
reed@google.com4117a242012-10-30 20:26:58 +000028 static skiagm::GMRegistry SK_MACRO_APPEND_LINE(R_)(SK_MACRO_APPEND_LINE(F_));
29
Chris Dalton3a778372019-02-07 15:23:36 -070030// A Simple GM is a rendering test that does not store state between rendering calls or make use of
31// the onOnceBeforeDraw() virtual; it consists of:
halcanary2a243382015-09-09 08:16:41 -070032// * A name.
33// * Prefered width and height.
34// * Optionally, a background color (default is white).
Chris Dalton3a778372019-02-07 15:23:36 -070035// * A standalone function pointer that implements its onDraw method.
halcanary2a243382015-09-09 08:16:41 -070036#define DEF_SIMPLE_GM(NAME, CANVAS, W, H) \
37 DEF_SIMPLE_GM_BG_NAME(NAME, CANVAS, W, H, SK_ColorWHITE, SkString(#NAME))
Chris Dalton3a778372019-02-07 15:23:36 -070038#define DEF_SIMPLE_GM_BG(NAME, CANVAS, W, H, BGCOLOR) \
halcanary2a243382015-09-09 08:16:41 -070039 DEF_SIMPLE_GM_BG_NAME(NAME, CANVAS, W, H, BGCOLOR, SkString(#NAME))
Chris Dalton3a778372019-02-07 15:23:36 -070040#define DEF_SIMPLE_GM_BG_NAME(NAME, CANVAS, W, H, BGCOLOR, NAME_STR) \
Chris Dalton50e24d72019-02-07 16:20:09 -070041 static void SK_MACRO_CONCAT(NAME,_GM_inner)(SkCanvas*); \
42 DEF_SIMPLE_GM_BG_NAME_CAN_FAIL(NAME, CANVAS,, W, H, BGCOLOR, NAME_STR) { \
43 SK_MACRO_CONCAT(NAME,_GM_inner)(CANVAS); \
44 return skiagm::DrawResult::kOk; \
45 } \
46 void SK_MACRO_CONCAT(NAME,_GM_inner)(SkCanvas* CANVAS)
47
48#define DEF_SIMPLE_GM_CAN_FAIL(NAME, CANVAS, ERR_MSG, W, H) \
49 DEF_SIMPLE_GM_BG_NAME_CAN_FAIL(NAME, CANVAS, ERR_MSG, W, H, SK_ColorWHITE, SkString(#NAME))
50#define DEF_SIMPLE_GM_BG_CAN_FAIL(NAME, CANVAS, ERR_MSG, W, H, BGCOLOR) \
51 DEF_SIMPLE_GM_BG_NAME_CAN_FAIL(NAME, CANVAS, ERR_MSG, W, H, BGCOLOR, SkString(#NAME))
52#define DEF_SIMPLE_GM_BG_NAME_CAN_FAIL(NAME, CANVAS, ERR_MSG, W, H, BGCOLOR, NAME_STR) \
53 static skiagm::DrawResult SK_MACRO_CONCAT(NAME,_GM)(SkCanvas*, SkString*); \
54 DEF_GM(return new skiagm::SimpleGM(BGCOLOR, NAME_STR, {W,H}, SK_MACRO_CONCAT(NAME,_GM));) \
55 skiagm::DrawResult SK_MACRO_CONCAT(NAME,_GM)(SkCanvas* CANVAS, SkString* ERR_MSG)
56
halcanary30b83d42014-10-26 05:23:53 -070057
Chris Dalton3a778372019-02-07 15:23:36 -070058// A Simple GpuGM makes direct GPU calls. Its onDraw hook that includes GPU objects as params, and
59// is only invoked on GPU configs. Non-GPU configs automatically draw a GPU-only message and abort.
60#define DEF_SIMPLE_GPU_GM(NAME, GR_CONTEXT, RENDER_TARGET_CONTEXT, CANVAS, W, H) \
61 DEF_SIMPLE_GPU_GM_BG(NAME, GR_CONTEXT, RENDER_TARGET_CONTEXT, CANVAS, W, H, SK_ColorWHITE)
Chris Dalton50e24d72019-02-07 16:20:09 -070062#define DEF_SIMPLE_GPU_GM_BG(NAME, GR_CONTEXT, RENDER_TARGET_CONTEXT, CANVAS, W, H, BGCOLOR) \
63 static void SK_MACRO_CONCAT(NAME,_GM_inner)(GrContext*, GrRenderTargetContext*, SkCanvas*); \
64 DEF_SIMPLE_GPU_GM_BG_CAN_FAIL(NAME, GR_CONTEXT, RENDER_TARGET_CONTEXT, CANVAS,, W, H, \
65 BGCOLOR) { \
66 SK_MACRO_CONCAT(NAME,_GM_inner)(GR_CONTEXT, RENDER_TARGET_CONTEXT, CANVAS); \
67 return skiagm::DrawResult::kOk; \
68 } \
69 void SK_MACRO_CONCAT(NAME,_GM_inner)( \
Chris Dalton3a778372019-02-07 15:23:36 -070070 GrContext* GR_CONTEXT, GrRenderTargetContext* RENDER_TARGET_CONTEXT, SkCanvas* CANVAS)
71
Chris Dalton50e24d72019-02-07 16:20:09 -070072#define DEF_SIMPLE_GPU_GM_CAN_FAIL(NAME, GR_CONTEXT, RENDER_TARGET_CONTEXT, CANVAS, ERR_MSG, W, H) \
73 DEF_SIMPLE_GPU_GM_BG_CAN_FAIL(NAME, GR_CONTEXT, RENDER_TARGET_CONTEXT, CANVAS, \
74 ERR_MSG, W, H, SK_ColorWHITE)
75#define DEF_SIMPLE_GPU_GM_BG_CAN_FAIL(NAME, GR_CONTEXT, RENDER_TARGET_CONTEXT, CANVAS, ERR_MSG, W, \
76 H, BGCOLOR) \
77 static skiagm::DrawResult SK_MACRO_CONCAT(NAME,_GM)( \
78 GrContext*, GrRenderTargetContext*, SkCanvas*, SkString*); \
79 DEF_GM(return new skiagm::SimpleGpuGM(BGCOLOR, SkString(#NAME), {W,H}, \
80 SK_MACRO_CONCAT(NAME,_GM));) \
81 skiagm::DrawResult SK_MACRO_CONCAT(NAME,_GM)( \
82 GrContext* GR_CONTEXT, GrRenderTargetContext* RENDER_TARGET_CONTEXT, SkCanvas* CANVAS, \
83 SkString* ERR_MSG)
84
reed@android.com00dae862009-06-10 15:38:48 +000085namespace skiagm {
rmistry@google.comd6176b02012-08-23 18:14:13 +000086
Chris Dalton50e24d72019-02-07 16:20:09 -070087 enum class DrawResult {
88 kOk, // Test drew successfully.
89 kFail, // Test failed to draw.
90 kSkip // Test is not applicable in this context and should be skipped.
91 };
92
reed@android.com00dae862009-06-10 15:38:48 +000093 class GM {
94 public:
Chris Dalton50e24d72019-02-07 16:20:09 -070095 using DrawResult = skiagm::DrawResult;
96
Chris Dalton3a778372019-02-07 15:23:36 -070097 GM(SkColor backgroundColor = SK_ColorWHITE);
reed@android.com00dae862009-06-10 15:38:48 +000098 virtual ~GM();
rmistry@google.comd6176b02012-08-23 18:14:13 +000099
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000100 enum Mode {
101 kGM_Mode,
102 kSample_Mode,
103 kBench_Mode,
104 };
105
106 void setMode(Mode mode) { fMode = mode; }
107 Mode getMode() const { return fMode; }
108
Chris Dalton50e24d72019-02-07 16:20:09 -0700109 static constexpr char kErrorMsg_DrawSkippedGpuOnly[] = "This test is for GPU configs only.";
110
111 DrawResult draw(SkCanvas* canvas) {
112 SkString errorMsg;
113 return this->draw(canvas, &errorMsg);
114 }
115 DrawResult draw(SkCanvas*, SkString* errorMsg);
116
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000117 void drawBackground(SkCanvas*);
Chris Dalton50e24d72019-02-07 16:20:09 -0700118 DrawResult drawContent(SkCanvas* canvas) {
119 SkString errorMsg;
120 return this->drawContent(canvas, &errorMsg);
121 }
122 DrawResult drawContent(SkCanvas*, SkString* errorMsg);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000123
mtkleinf4ba3212015-01-28 15:32:24 -0800124 SkISize getISize() { return this->onISize(); }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000125 const char* getName();
reed@android.com00dae862009-06-10 15:38:48 +0000126
Mike Klein9707e902019-02-07 16:18:22 -0500127 virtual bool runAsBench() const;
mtkleincf5d9c92015-01-23 10:31:45 -0800128
reed@google.com487b5602013-02-01 15:01:24 +0000129 SkScalar width() {
130 return SkIntToScalar(this->getISize().width());
131 }
132 SkScalar height() {
scroggo@google.comab026272013-04-12 22:14:03 +0000133 return SkIntToScalar(this->getISize().height());
reed@google.com487b5602013-02-01 15:01:24 +0000134 }
135
reed@google.comb4b49cc2011-12-06 16:15:42 +0000136 SkColor getBGColor() const { return fBGColor; }
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000137 void setBGColor(SkColor);
reed@google.com30db5992011-08-29 17:41:02 +0000138
Chris Dalton3a778372019-02-07 15:23:36 -0700139 // helper: fill a rect in the specified color based on the GM's getISize bounds.
reed@google.com2d6ef522012-01-03 17:20:38 +0000140 void drawSizeBounds(SkCanvas*, SkColor);
141
reed@google.comaef73612012-11-16 13:41:45 +0000142 bool isCanvasDeferred() const { return fCanvasIsDeferred; }
143 void setCanvasIsDeferred(bool isDeferred) {
144 fCanvasIsDeferred = isDeferred;
145 }
146
Mike Kleincd5104e2019-03-20 11:55:08 -0500147 bool animate(const AnimTimer&);
robertphillips05a4cf52016-09-08 09:02:43 -0700148 bool handleKey(SkUnichar uni) {
149 return this->onHandleKey(uni);
150 }
reedd9adfe62015-02-01 19:01:04 -0800151
Mike Reed81f60ec2018-05-15 10:09:52 -0400152 bool getControls(SkMetaData* controls) { return this->onGetControls(controls); }
153 void setControls(const SkMetaData& controls) { this->onSetControls(controls); }
154
Mike Klein9707e902019-02-07 16:18:22 -0500155 virtual void modifyGrContextOptions(GrContextOptions* options);
bsalomon4ee6bd82015-05-27 13:23:23 -0700156
halcanary2a243382015-09-09 08:16:41 -0700157 protected:
Mike Klein9707e902019-02-07 16:18:22 -0500158 virtual void onOnceBeforeDraw();
159 virtual DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg);
160 virtual void onDraw(SkCanvas*);
161
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000162 virtual SkISize onISize() = 0;
reed@android.com8015dd82009-06-21 00:49:18 +0000163 virtual SkString onShortName() = 0;
mtklein1c402922015-01-23 11:07:07 -0800164
Mike Kleincd5104e2019-03-20 11:55:08 -0500165 virtual bool onAnimate(const AnimTimer&);
Mike Klein9707e902019-02-07 16:18:22 -0500166 virtual bool onHandleKey(SkUnichar uni);
167 virtual bool onGetControls(SkMetaData*);
168 virtual void onSetControls(const SkMetaData&);
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000169
reed@android.com8015dd82009-06-21 00:49:18 +0000170 private:
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000171 Mode fMode;
reed@android.com8015dd82009-06-21 00:49:18 +0000172 SkString fShortName;
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000173 SkColor fBGColor;
reed@google.comaef73612012-11-16 13:41:45 +0000174 bool fCanvasIsDeferred; // work-around problem in srcmode.cpp
reed@google.com7775d662012-11-27 15:15:58 +0000175 bool fHaveCalledOnceBeforeDraw;
reed@android.com00dae862009-06-10 15:38:48 +0000176 };
177
Hal Canary972eba32018-07-30 17:07:07 -0400178 typedef GM*(*GMFactory)(void*) ;
179 typedef sk_tools::Registry<GMFactory> GMRegistry;
halcanaryf62c6342015-01-12 15:27:46 -0800180
Chris Dalton3a778372019-02-07 15:23:36 -0700181 // A GpuGM replaces the onDraw method with one that also accepts GPU objects alongside the
182 // SkCanvas. Its onDraw is only invoked on GPU configs; on non-GPU configs it will automatically
183 // draw a GPU-only message and abort.
184 class GpuGM : public GM {
halcanaryf62c6342015-01-12 15:27:46 -0800185 public:
Chris Dalton3a778372019-02-07 15:23:36 -0700186 GpuGM(SkColor backgroundColor = SK_ColorWHITE) : GM(backgroundColor) {}
Chris Dalton382b1222019-02-07 10:05:55 +0000187 private:
Chris Dalton50e24d72019-02-07 16:20:09 -0700188 using GM::onDraw;
189 DrawResult onDraw(SkCanvas*, SkString* errorMsg) final;
Mike Klein9707e902019-02-07 16:18:22 -0500190
Chris Dalton50e24d72019-02-07 16:20:09 -0700191 virtual DrawResult onDraw(GrContext* ctx, GrRenderTargetContext* rtc, SkCanvas* canvas,
Mike Klein9707e902019-02-07 16:18:22 -0500192 SkString* errorMsg);
193 virtual void onDraw(GrContext*, GrRenderTargetContext*, SkCanvas*);
Chris Daltonf5efa782019-02-05 17:42:14 -0700194 };
Chris Dalton3a778372019-02-07 15:23:36 -0700195
196 // SimpleGM is intended for basic GMs that can define their entire implementation inside a
197 // single "draw" function pointer.
198 class SimpleGM : public GM {
199 public:
Chris Dalton50e24d72019-02-07 16:20:09 -0700200 using DrawProc = DrawResult(*)(SkCanvas*, SkString*);
Chris Dalton3a778372019-02-07 15:23:36 -0700201 SimpleGM(SkColor bgColor, const SkString& name, const SkISize& size, DrawProc drawProc)
202 : GM(bgColor), fName(name), fSize(size), fDrawProc(drawProc) {}
203
204 private:
Mike Klein9707e902019-02-07 16:18:22 -0500205 SkISize onISize() override;
206 SkString onShortName() override;
207 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override;
Chris Dalton3a778372019-02-07 15:23:36 -0700208
209 const SkString fName;
210 const SkISize fSize;
211 const DrawProc fDrawProc;
212 };
213
214 class SimpleGpuGM : public GpuGM {
215 public:
Chris Dalton50e24d72019-02-07 16:20:09 -0700216 using DrawProc = DrawResult(*)(GrContext*, GrRenderTargetContext*, SkCanvas*, SkString*);
Chris Dalton3a778372019-02-07 15:23:36 -0700217 SimpleGpuGM(SkColor bgColor, const SkString& name, const SkISize& size, DrawProc drawProc)
218 : GpuGM(bgColor), fName(name), fSize(size), fDrawProc(drawProc) {}
219
220 private:
Mike Klein9707e902019-02-07 16:18:22 -0500221 SkISize onISize() override;
222 SkString onShortName() override;
Chris Dalton50e24d72019-02-07 16:20:09 -0700223 DrawResult onDraw(GrContext* ctx, GrRenderTargetContext* rtc, SkCanvas* canvas,
Mike Klein9707e902019-02-07 16:18:22 -0500224 SkString* errorMsg) override;
Chris Dalton3a778372019-02-07 15:23:36 -0700225
226 const SkString fName;
227 const SkISize fSize;
228 const DrawProc fDrawProc;
229 };
230
reed@android.com00dae862009-06-10 15:38:48 +0000231}
232
Mike Kleinc9eace82018-10-31 10:49:38 -0400233void MarkGMGood(SkCanvas*, SkScalar x, SkScalar y);
234void MarkGMBad (SkCanvas*, SkScalar x, SkScalar y);
235
reed@android.com00dae862009-06-10 15:38:48 +0000236#endif