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 "../tools/Registry.h" 12 #include "SkBitmap.h" 13 #include "SkCanvas.h" 14 #include "SkClipOpPriv.h" 15 #include "SkMacros.h" 16 #include "SkMetaData.h" 17 #include "SkPaint.h" 18 #include "SkSize.h" 19 #include "SkString.h" 20 21 class SkAnimTimer; 22 struct GrContextOptions; 23 24 #define DEF_GM(code) \ 25 static skiagm::GM* SK_MACRO_APPEND_LINE(F_)(void*) { code; } \ 26 static skiagm::GMRegistry SK_MACRO_APPEND_LINE(R_)(SK_MACRO_APPEND_LINE(F_)); 27 28 // a Simple GM is a rendering test that does not store state between 29 // rendering calls or make use of the onOnceBeforeDraw() virtual; it 30 // consists of: 31 // * A single void(*)(SkCanvas*) function. 32 // * A name. 33 // * Prefered width and height. 34 // * Optionally, a background color (default is white). 35 #define DEF_SIMPLE_GM(NAME, CANVAS, W, H) \ 36 DEF_SIMPLE_GM_BG_NAME(NAME, CANVAS, W, H, SK_ColorWHITE, SkString(#NAME)) 37 #define DEF_SIMPLE_GM_BG(NAME, CANVAS, W, H, BGCOLOR)\ 38 DEF_SIMPLE_GM_BG_NAME(NAME, CANVAS, W, H, BGCOLOR, SkString(#NAME)) 39 #define DEF_SIMPLE_GM_BG_NAME(NAME, CANVAS, W, H, BGCOLOR, NAME_STR) \ 40 static void SK_MACRO_CONCAT(NAME, _GM)(SkCanvas * CANVAS); \ 41 DEF_GM(return new skiagm::SimpleGM(NAME_STR, SK_MACRO_CONCAT(NAME, _GM), \ 42 SkISize::Make(W, H), BGCOLOR);) \ 43 void SK_MACRO_CONCAT(NAME, _GM)(SkCanvas * CANVAS) 44 45 namespace skiagm { 46 47 class GM { 48 public: 49 GM(); 50 virtual ~GM(); 51 52 enum Mode { 53 kGM_Mode, 54 kSample_Mode, 55 kBench_Mode, 56 }; 57 58 void setMode(Mode mode) { fMode = mode; } 59 Mode getMode() const { return fMode; } 60 61 void draw(SkCanvas*); 62 void drawBackground(SkCanvas*); 63 void drawContent(SkCanvas*); 64 65 SkISize getISize() { return this->onISize(); } 66 const char* getName(); 67 68 virtual bool runAsBench() const { return false; } 69 70 SkScalar width() { 71 return SkIntToScalar(this->getISize().width()); 72 } 73 SkScalar height() { 74 return SkIntToScalar(this->getISize().height()); 75 } 76 77 SkColor getBGColor() const { return fBGColor; } 78 void setBGColor(SkColor); 79 80 // helper: fill a rect in the specified color based on the 81 // GM's getISize bounds. 82 void drawSizeBounds(SkCanvas*, SkColor); 83 84 bool isCanvasDeferred() const { return fCanvasIsDeferred; } 85 void setCanvasIsDeferred(bool isDeferred) { 86 fCanvasIsDeferred = isDeferred; 87 } 88 89 bool animate(const SkAnimTimer&); 90 bool handleKey(SkUnichar uni) { 91 return this->onHandleKey(uni); 92 } 93 94 bool getControls(SkMetaData* controls) { return this->onGetControls(controls); } 95 void setControls(const SkMetaData& controls) { this->onSetControls(controls); } 96 97 virtual void modifyGrContextOptions(GrContextOptions* options) {} 98 99 /** draws a standard message that the GM is only intended to be used with the GPU.*/ 100 static void DrawGpuOnlyMessage(SkCanvas*); 101 102 protected: 103 virtual void onOnceBeforeDraw() {} 104 virtual void onDraw(SkCanvas*) = 0; 105 virtual SkISize onISize() = 0; 106 virtual SkString onShortName() = 0; 107 108 virtual bool onAnimate(const SkAnimTimer&) { return false; } 109 virtual bool onHandleKey(SkUnichar uni) { return false; } 110 virtual bool onGetControls(SkMetaData*) { return false; } 111 virtual void onSetControls(const SkMetaData&) {} 112 113 private: 114 Mode fMode; 115 SkString fShortName; 116 SkColor fBGColor; 117 bool fCanvasIsDeferred; // work-around problem in srcmode.cpp 118 bool fHaveCalledOnceBeforeDraw; 119 }; 120 121 typedef GM*(*GMFactory)(void*) ; 122 typedef sk_tools::Registry<GMFactory> GMRegistry; 123 124 class SimpleGM : public skiagm::GM { 125 public: 126 SimpleGM(const SkString& name, 127 void (*drawProc)(SkCanvas*), 128 const SkISize& size, 129 SkColor backgroundColor) 130 : fName(name), fDrawProc(drawProc), fSize(size) { 131 if (backgroundColor != SK_ColorWHITE) { 132 this->setBGColor(backgroundColor); 133 } 134 } 135 protected: 136 void onDraw(SkCanvas* canvas) override; 137 SkISize onISize() override; 138 SkString onShortName() override; 139 private: 140 SkString fName; 141 void (*fDrawProc)(SkCanvas*); 142 SkISize fSize; 143 }; 144 } 145 146 void MarkGMGood(SkCanvas*, SkScalar x, SkScalar y); 147 void MarkGMBad (SkCanvas*, SkScalar x, SkScalar y); 148 149 #endif 150