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 "../tools/Registry.h" 17 #include "SkClipOpPriv.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 SkColor getBGColor() const { return fBGColor; } 76 void setBGColor(SkColor); 77 78 // helper: fill a rect in the specified color based on the 79 // GM's getISize bounds. 80 void drawSizeBounds(SkCanvas*, SkColor); 81 82 bool isCanvasDeferred() const { return fCanvasIsDeferred; } 83 void setCanvasIsDeferred(bool isDeferred) { 84 fCanvasIsDeferred = isDeferred; 85 } 86 87 bool animate(const SkAnimTimer&); 88 bool handleKey(SkUnichar uni) { 89 return this->onHandleKey(uni); 90 } 91 92 virtual void modifyGrContextOptions(GrContextOptions* options) {} 93 94 /** draws a standard message that the GM is only intended to be used with the GPU.*/ 95 static void DrawGpuOnlyMessage(SkCanvas*); 96 97 protected: 98 virtual void onOnceBeforeDraw() {} 99 virtual void onDraw(SkCanvas*) = 0; 100 virtual void onDrawBackground(SkCanvas*); 101 virtual SkISize onISize() = 0; 102 virtual SkString onShortName() = 0; 103 104 virtual bool onAnimate(const SkAnimTimer&) { return false; } 105 virtual bool onHandleKey(SkUnichar uni) { return false; } 106 107 private: 108 Mode fMode; 109 SkString fShortName; 110 SkColor fBGColor; 111 bool fCanvasIsDeferred; // work-around problem in srcmode.cpp 112 bool fHaveCalledOnceBeforeDraw; 113 }; 114 115 typedef sk_tools::Registry<GM*(*)(void*)> GMRegistry; 116 117 class SimpleGM : public skiagm::GM { 118 public: 119 SimpleGM(const SkString& name, 120 void (*drawProc)(SkCanvas*), 121 const SkISize& size, 122 SkColor backgroundColor) 123 : fName(name), fDrawProc(drawProc), fSize(size) { 124 if (backgroundColor != SK_ColorWHITE) { 125 this->setBGColor(backgroundColor); 126 } 127 } 128 protected: 129 void onDraw(SkCanvas* canvas) override; 130 SkISize onISize() override; 131 SkString onShortName() override; 132 private: 133 SkString fName; 134 void (*fDrawProc)(SkCanvas*); 135 SkISize fSize; 136 }; 137 } 138 139 #endif 140