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 "SkBitmap.h" 12 #include "SkCanvas.h" 13 #include "SkPaint.h" 14 #include "SkSize.h" 15 #include "SkString.h" 16 #include "SkTRegistry.h" 17 #include "sk_tool_utils.h" 18 19 class SkAnimTimer; 20 struct GrContextOptions; 21 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_)); 25 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 28 // consists of: 29 // * A single void(*)(SkCanvas*) function. 30 // * A name. 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) 42 43 namespace skiagm { 44 45 class GM { 46 public: 47 GM(); 48 virtual ~GM(); 49 50 enum Mode { 51 kGM_Mode, 52 kSample_Mode, 53 kBench_Mode, 54 }; 55 56 void setMode(Mode mode) { fMode = mode; } 57 Mode getMode() const { return fMode; } 58 59 void draw(SkCanvas*); 60 void drawBackground(SkCanvas*); 61 void drawContent(SkCanvas*); 62 63 SkISize getISize() { return this->onISize(); } 64 const char* getName(); 65 66 virtual bool runAsBench() const { return false; } 67 68 SkScalar width() { 69 return SkIntToScalar(this->getISize().width()); 70 } 71 SkScalar height() { 72 return SkIntToScalar(this->getISize().height()); 73 } 74 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()); 82 return matrix; 83 } 84 85 SkColor getBGColor() const { return fBGColor; } 86 void setBGColor(SkColor); 87 88 // helper: fill a rect in the specified color based on the 89 // GM's getISize bounds. 90 void drawSizeBounds(SkCanvas*, SkColor); 91 92 bool isCanvasDeferred() const { return fCanvasIsDeferred; } 93 void setCanvasIsDeferred(bool isDeferred) { 94 fCanvasIsDeferred = isDeferred; 95 } 96 97 const SkMatrix& getStarterMatrix() { return fStarterMatrix; } 98 void setStarterMatrix(const SkMatrix& matrix) { 99 fStarterMatrix = matrix; 100 } 101 102 bool animate(const SkAnimTimer&); 103 104 virtual void modifyGrContextOptions(GrContextOptions* options) {} 105 106 /** draws a standard message that the GM is only intended to be used with the GPU.*/ 107 static void DrawGpuOnlyMessage(SkCanvas*); 108 109 protected: 110 virtual void onOnceBeforeDraw() {} 111 virtual void onDraw(SkCanvas*) = 0; 112 virtual void onDrawBackground(SkCanvas*); 113 virtual SkISize onISize() = 0; 114 virtual SkString onShortName() = 0; 115 116 virtual bool onAnimate(const SkAnimTimer&) { return false; } 117 virtual SkMatrix onGetInitialTransform() const { return SkMatrix::I(); } 118 119 private: 120 Mode fMode; 121 SkString fShortName; 122 SkColor fBGColor; 123 bool fCanvasIsDeferred; // work-around problem in srcmode.cpp 124 bool fHaveCalledOnceBeforeDraw; 125 SkMatrix fStarterMatrix; 126 }; 127 128 typedef SkTRegistry<GM*(*)(void*)> GMRegistry; 129 130 class SimpleGM : public skiagm::GM { 131 public: 132 SimpleGM(const SkString& name, 133 void (*drawProc)(SkCanvas*), 134 const SkISize& size, 135 SkColor backgroundColor) 136 : fName(name), fDrawProc(drawProc), fSize(size) { 137 if (backgroundColor != SK_ColorWHITE) { 138 this->setBGColor(backgroundColor); 139 } 140 } 141 protected: 142 void onDraw(SkCanvas* canvas) override; 143 SkISize onISize() override; 144 SkString onShortName() override; 145 private: 146 SkString fName; 147 void (*fDrawProc)(SkCanvas*); 148 SkISize fSize; 149 }; 150 } 151 152 #endif 153