Debian Codegraph @

gm/gm.h

raw
1 /*
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
8 #ifndef skiagm_DEFINED
9 #define skiagm_DEFINED
10
11 #include "include/core/SkColor.h"
12 #include "include/core/SkScalar.h"
13 #include "include/core/SkSize.h"
14 #include "include/core/SkString.h"
15 #include "include/core/SkSurfaceProps.h"
16 #include "include/core/SkTypes.h"
17 #include "include/private/base/SkMacros.h"
18 #include "tools/Registry.h"
19
20 #include <functional>
21 #include <map>
22 #include <memory>
23
24 class GrRecordingContext;
25 class SkCanvas;
26 class SkMetaData;
27 struct GrContextOptions;
28
29 namespace skiagm::verifiers {
30 class VerifierList;
31 }
32
33 namespace skgpu::graphite {
34 struct ContextOptions;
35 }
36
37 namespace skiatest::graphite {
38 class GraphiteTestContext;
39 }
40
41 #define DEF_GM(CODE) \
42 static skiagm::GMRegistry SK_MACRO_APPEND_COUNTER(REG_)( \
43 []() { return std::unique_ptr<skiagm::GM>([]() { CODE; }()); });
44
45 // A Simple GM is a rendering test that does not store state between rendering calls or make use of
46 // the onOnceBeforeDraw() virtual; it consists of:
47 // * A name.
48 // * Prefered width and height.
49 // * Optionally, a background color (default is white).
50 // * A standalone function pointer that implements its onDraw method.
51 #define DEF_SIMPLE_GM(NAME, CANVAS, W, H) \
52 DEF_SIMPLE_GM_BG_NAME(NAME, CANVAS, W, H, SK_ColorWHITE, SkString(#NAME))
53 #define DEF_SIMPLE_GM_BG(NAME, CANVAS, W, H, BGCOLOR) \
54 DEF_SIMPLE_GM_BG_NAME(NAME, CANVAS, W, H, BGCOLOR, SkString(#NAME))
55 #define DEF_SIMPLE_GM_BG_NAME(NAME, CANVAS, W, H, BGCOLOR, NAME_STR) \
56 static void SK_MACRO_CONCAT(NAME,_GM_inner)(SkCanvas*); \
57 DEF_SIMPLE_GM_BG_NAME_CAN_FAIL(NAME, CANVAS,, W, H, BGCOLOR, NAME_STR) { \
58 SK_MACRO_CONCAT(NAME,_GM_inner)(CANVAS); \
59 return skiagm::DrawResult::kOk; \
60 } \
61 void SK_MACRO_CONCAT(NAME,_GM_inner)(SkCanvas* CANVAS)
62
63 #define DEF_SIMPLE_GM_CAN_FAIL(NAME, CANVAS, ERR_MSG, W, H) \
64 DEF_SIMPLE_GM_BG_NAME_CAN_FAIL(NAME, CANVAS, ERR_MSG, W, H, SK_ColorWHITE, SkString(#NAME))
65 #define DEF_SIMPLE_GM_BG_CAN_FAIL(NAME, CANVAS, ERR_MSG, W, H, BGCOLOR) \
66 DEF_SIMPLE_GM_BG_NAME_CAN_FAIL(NAME, CANVAS, ERR_MSG, W, H, BGCOLOR, SkString(#NAME))
67 #define DEF_SIMPLE_GM_BG_NAME_CAN_FAIL(NAME, CANVAS, ERR_MSG, W, H, BGCOLOR, NAME_STR) \
68 static skiagm::DrawResult SK_MACRO_CONCAT(NAME,_GM)(SkCanvas*, SkString*); \
69 DEF_GM(return new skiagm::SimpleGM(BGCOLOR, NAME_STR, {W,H}, SK_MACRO_CONCAT(NAME,_GM));) \
70 skiagm::DrawResult SK_MACRO_CONCAT(NAME,_GM)(SkCanvas* CANVAS, SkString* ERR_MSG)
71
72 // TODO: Rename these macros from GPU to GANESH
73 #if defined(SK_GANESH)
74 // A Simple GpuGM makes direct GPU calls. Its onDraw hook that includes GPU objects as params, and
75 // is only invoked on GPU configs. Non-GPU configs automatically draw a GPU-only message and abort.
76 #define DEF_SIMPLE_GPU_GM(NAME, GR_CONTEXT, CANVAS, W, H) \
77 DEF_SIMPLE_GPU_GM_BG(NAME, GR_CONTEXT, CANVAS, W, H, SK_ColorWHITE)
78
79 #define DEF_SIMPLE_GPU_GM_BG(NAME, GR_CONTEXT, CANVAS, W, H, BGCOLOR) \
80 static void SK_MACRO_CONCAT(NAME,_GM_inner)(GrRecordingContext*, SkCanvas*); \
81 DEF_SIMPLE_GPU_GM_BG_CAN_FAIL(NAME, GR_CONTEXT, CANVAS, /* ERR_MSG */, W, H, BGCOLOR) { \
82 SK_MACRO_CONCAT(NAME,_GM_inner)(GR_CONTEXT, CANVAS); \
83 return skiagm::DrawResult::kOk; \
84 } \
85 void SK_MACRO_CONCAT(NAME,_GM_inner)(GrRecordingContext* GR_CONTEXT, SkCanvas* CANVAS)
86
87 #define DEF_SIMPLE_GPU_GM_CAN_FAIL(NAME, GR_CONTEXT, CANVAS, ERR_MSG, W, H) \
88 DEF_SIMPLE_GPU_GM_BG_CAN_FAIL(NAME, GR_CONTEXT, CANVAS, ERR_MSG, W, H, SK_ColorWHITE)
89
90 #define DEF_SIMPLE_GPU_GM_BG_CAN_FAIL(NAME, GR_CONTEXT, CANVAS, ERR_MSG, W, H, BGCOLOR) \
91 static skiagm::DrawResult SK_MACRO_CONCAT(NAME,_GM)( \
92 GrRecordingContext*, SkCanvas*, SkString*); \
93 DEF_GM(return new skiagm::SimpleGpuGM(BGCOLOR, SkString(#NAME), {W,H}, \
94 SK_MACRO_CONCAT(NAME,_GM));) \
95 skiagm::DrawResult SK_MACRO_CONCAT(NAME,_GM)( \
96 GrRecordingContext* GR_CONTEXT, SkCanvas* CANVAS, SkString* ERR_MSG)
97 #endif
98
99 namespace skiagm {
100
101 enum class DrawResult {
102 kOk, // Test drew successfully.
103 kFail, // Test failed to draw.
104 kSkip // Test is not applicable in this context and should be skipped.
105 };
106
107 class GM {
108 public:
109 using DrawResult = skiagm::DrawResult;
110 using GraphiteTestContext = skiatest::graphite::GraphiteTestContext;
111
112 explicit GM(SkColor backgroundColor = SK_ColorWHITE);
113 virtual ~GM();
114
115 enum Mode {
116 kGM_Mode,
117 kSample_Mode,
118 kBench_Mode,
119 };
120
121 void setMode(Mode mode) { fMode = mode; }
122 Mode getMode() const { return fMode; }
123
124 inline static constexpr char kErrorMsg_DrawSkippedGpuOnly[] =
125 "This test is for GPU configs only.";
126
127 DrawResult gpuSetup(SkCanvas*, SkString* errorMsg, GraphiteTestContext* = nullptr);
128 void gpuTeardown();
129
130 void onceBeforeDraw() {
131 if (!fHaveCalledOnceBeforeDraw) {
132 fHaveCalledOnceBeforeDraw = true;
133 this->onOnceBeforeDraw();
134 }
135 }
136
137 DrawResult draw(SkCanvas* canvas) {
138 SkString errorMsg;
139 return this->draw(canvas, &errorMsg);
140 }
141 DrawResult draw(SkCanvas*, SkString* errorMsg);
142
143 void drawBackground(SkCanvas*);
144 DrawResult drawContent(SkCanvas* canvas) {
145 SkString errorMsg;
146 return this->drawContent(canvas, &errorMsg);
147 }
148 DrawResult drawContent(SkCanvas*, SkString* errorMsg);
149
150 virtual SkISize getISize() = 0;
151
152 virtual SkString getName() const = 0;
153
154 virtual bool runAsBench() const;
155
156 SkScalar width() {
157 return SkIntToScalar(this->getISize().width());
158 }
159 SkScalar height() {
160 return SkIntToScalar(this->getISize().height());
161 }
162
163 SkColor getBGColor() const { return fBGColor; }
164 void setBGColor(SkColor);
165
166 // helper: fill a rect in the specified color based on the GM's getISize bounds.
167 void drawSizeBounds(SkCanvas*, SkColor);
168
169 bool animate(double /*nanos*/);
170 virtual bool onChar(SkUnichar);
171
172 bool getControls(SkMetaData* controls) { return this->onGetControls(controls); }
173 void setControls(const SkMetaData& controls) { this->onSetControls(controls); }
174
175 // Override to modify the default surface properties of the canvas to be used.
176 // The value may be further modified or ignored if the canvas used cannot support it.
177 virtual void modifySurfaceProps(SkSurfaceProps*) const {}
178
179 virtual void modifyGrContextOptions(GrContextOptions*) {}
180 virtual void modifyGraphiteContextOptions(skgpu::graphite::ContextOptions*) const {}
181
182 // Ignored by DM. Returns the set of Gold key/value pairs specific to this GM, such as the
183 // GM name and corpus. GMs may define additional keys. For example, codec GMs define keys
184 // for the parameters utilized to initialize the codec.
185 virtual std::map<std::string, std::string> getGoldKeys() const {
186 return std::map<std::string, std::string>{
187 {"name", getName().c_str()},
188 {"source_type", "gm"},
189 };
190 }
191
192 protected:
193 // onGpuSetup is called once before any other processing with a direct context.
194 virtual DrawResult onGpuSetup(SkCanvas*, SkString*, GraphiteTestContext*) {
195 return DrawResult::kOk;
196 }
197 virtual void onGpuTeardown() {}
198 virtual void onOnceBeforeDraw();
199 virtual DrawResult onDraw(SkCanvas*, SkString* errorMsg);
200 virtual void onDraw(SkCanvas*);
201
202 virtual bool onAnimate(double /*nanos*/);
203 virtual bool onGetControls(SkMetaData*);
204 virtual void onSetControls(const SkMetaData&);
205
206 GraphiteTestContext* graphiteTestContext() const { return fGraphiteTestContext; }
207
208 private:
209 Mode fMode;
210 SkColor fBGColor;
211 bool fHaveCalledOnceBeforeDraw = false;
212 bool fGpuSetup = false;
213 DrawResult fGpuSetupResult = DrawResult::kOk;
214 GraphiteTestContext* fGraphiteTestContext;
215 };
216
217 using GMFactory = std::function<std::unique_ptr<skiagm::GM>()>;
218 using GMRegistry = sk_tools::Registry<GMFactory>;
219
220 // Adds a GM to the GMRegistry.
221 void Register(skiagm::GM* gm);
222
223 #if defined(SK_GANESH)
224 // A GpuGM replaces the onDraw method with one that also accepts GPU objects alongside the
225 // SkCanvas. Its onDraw is only invoked on GPU configs; on non-GPU configs it will automatically
226 // draw a GPU-only message and abort.
227 class GpuGM : public GM {
228 public:
229 GpuGM(SkColor backgroundColor = SK_ColorWHITE) : GM(backgroundColor) {}
230
231 private:
232 using GM::onDraw;
233 DrawResult onDraw(SkCanvas*, SkString* errorMsg) final;
234
235 virtual DrawResult onDraw(GrRecordingContext*, SkCanvas*, SkString* errorMsg);
236 virtual void onDraw(GrRecordingContext*, SkCanvas*);
237 };
238 #endif
239
240 // SimpleGM is intended for basic GMs that can define their entire implementation inside a
241 // single "draw" function pointer.
242 class SimpleGM : public GM {
243 public:
244 using DrawProc = DrawResult(*)(SkCanvas*, SkString*);
245
246 SimpleGM(SkColor bgColor, const SkString& name, const SkISize& size, DrawProc drawProc)
247 : GM(bgColor), fName(name), fSize(size), fDrawProc(drawProc) {}
248
249 SkString getName() const override;
250 SkISize getISize() override;
251
252 private:
253 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override;
254
255 const SkString fName;
256 const SkISize fSize;
257 const DrawProc fDrawProc;
258 };
259
260 #if defined(SK_GANESH)
261 class SimpleGpuGM : public GpuGM {
262 public:
263 using DrawProc = DrawResult (*)(GrRecordingContext*, SkCanvas*, SkString* errorMsg);
264
265 SimpleGpuGM(SkColor bgColor, const SkString& name, const SkISize& size, DrawProc drawProc)
266 : GpuGM(bgColor), fName(name), fSize(size), fDrawProc(drawProc) {}
267
268 SkString getName() const override;
269 SkISize getISize() override;
270
271 private:
272 DrawResult onDraw(GrRecordingContext*, SkCanvas*, SkString* errorMsg) override;
273
274 const SkString fName;
275 const SkISize fSize;
276 const DrawProc fDrawProc;
277 };
278 #endif
279 } // namespace skiagm
280
281 void MarkGMGood(SkCanvas*, SkScalar x, SkScalar y);
282 void MarkGMBad (SkCanvas*, SkScalar x, SkScalar y);
283
284 #endif