2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
16 #include "../tools/Registry.h"
17 #include "SkClipOpPriv.h"
20 struct GrContextOptions;
22 #define DEF_GM(code) \
23 static skiagm::GM* SK_MACRO_APPEND_LINE(F_)(void*) { code; } \
24 static skiagm::GMRegistry SK_MACRO_APPEND_LINE(R_)(SK_MACRO_APPEND_LINE(F_));
26 // a Simple GM is a rendering test that does not store state between
27 // rendering calls or make use of the onOnceBeforeDraw() virtual; it
29 // * A single void(*)(SkCanvas*) function.
31 // * Prefered width and height.
32 // * Optionally, a background color (default is white).
33 #define DEF_SIMPLE_GM(NAME, CANVAS, W, H) \
34 DEF_SIMPLE_GM_BG_NAME(NAME, CANVAS, W, H, SK_ColorWHITE, SkString(#NAME))
35 #define DEF_SIMPLE_GM_BG(NAME, CANVAS, W, H, BGCOLOR)\
36 DEF_SIMPLE_GM_BG_NAME(NAME, CANVAS, W, H, BGCOLOR, SkString(#NAME))
37 #define DEF_SIMPLE_GM_BG_NAME(NAME, CANVAS, W, H, BGCOLOR, NAME_STR) \
38 static void SK_MACRO_CONCAT(NAME, _GM)(SkCanvas * CANVAS); \
39 DEF_GM(return new skiagm::SimpleGM(NAME_STR, SK_MACRO_CONCAT(NAME, _GM), \
40 SkISize::Make(W, H), BGCOLOR);) \
41 void SK_MACRO_CONCAT(NAME, _GM)(SkCanvas * CANVAS)
56 void setMode(Mode mode) { fMode = mode; }
57 Mode getMode() const { return fMode; }
60 void drawBackground(SkCanvas*);
61 void drawContent(SkCanvas*);
63 SkISize getISize() { return this->onISize(); }
64 const char* getName();
66 virtual bool runAsBench() const { return false; }
69 return SkIntToScalar(this->getISize().width());
72 return SkIntToScalar(this->getISize().height());
75 // TODO(vandebo) Instead of exposing this, we should run all the GMs
76 // with and without an initial transform.
77 // Most GMs will return the identity matrix, but some PDFs tests
78 // require setting the initial transform.
79 SkMatrix getInitialTransform() const {
80 SkMatrix matrix = fStarterMatrix;
81 matrix.preConcat(this->onGetInitialTransform());
85 SkColor getBGColor() const { return fBGColor; }
86 void setBGColor(SkColor);
88 // helper: fill a rect in the specified color based on the
89 // GM's getISize bounds.
90 void drawSizeBounds(SkCanvas*, SkColor);
92 bool isCanvasDeferred() const { return fCanvasIsDeferred; }
93 void setCanvasIsDeferred(bool isDeferred) {
94 fCanvasIsDeferred = isDeferred;
97 const SkMatrix& getStarterMatrix() { return fStarterMatrix; }
98 void setStarterMatrix(const SkMatrix& matrix) {
99 fStarterMatrix = matrix;
102 bool animate(const SkAnimTimer&);
103 bool handleKey(SkUnichar uni) {
104 return this->onHandleKey(uni);
107 virtual void modifyGrContextOptions(GrContextOptions* options) {}
109 /** draws a standard message that the GM is only intended to be used with the GPU.*/
110 static void DrawGpuOnlyMessage(SkCanvas*);
113 virtual void onOnceBeforeDraw() {}
114 virtual void onDraw(SkCanvas*) = 0;
115 virtual void onDrawBackground(SkCanvas*);
116 virtual SkISize onISize() = 0;
117 virtual SkString onShortName() = 0;
119 virtual bool onAnimate(const SkAnimTimer&) { return false; }
120 virtual bool onHandleKey(SkUnichar uni) { return false; }
121 virtual SkMatrix onGetInitialTransform() const { return SkMatrix::I(); }
127 bool fCanvasIsDeferred; // work-around problem in srcmode.cpp
128 bool fHaveCalledOnceBeforeDraw;
129 SkMatrix fStarterMatrix;
132 typedef sk_tools::Registry<GM*(*)(void*)> GMRegistry;
134 class SimpleGM : public skiagm::GM {
136 SimpleGM(const SkString& name,
137 void (*drawProc)(SkCanvas*),
139 SkColor backgroundColor)
140 : fName(name), fDrawProc(drawProc), fSize(size) {
141 if (backgroundColor != SK_ColorWHITE) {
142 this->setBGColor(backgroundColor);
146 void onDraw(SkCanvas* canvas) override;
147 SkISize onISize() override;
148 SkString onShortName() override;
151 void (*fDrawProc)(SkCanvas*);