epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 7 | |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 8 | #ifndef skiagm_DEFINED |
| 9 | #define skiagm_DEFINED |
| 10 | |
epoger@google.com | d4af56c | 2011-07-25 16:27:59 +0000 | [diff] [blame] | 11 | #include "SkBitmap.h" |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 12 | #include "SkCanvas.h" |
| 13 | #include "SkPaint.h" |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 14 | #include "SkSize.h" |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 15 | #include "SkString.h" |
Mike Reed | ab273fa | 2017-01-11 13:58:55 -0500 | [diff] [blame] | 16 | #include "../tools/Registry.h" |
Mike Reed | ebfce6d | 2016-12-12 10:02:12 -0500 | [diff] [blame] | 17 | #include "SkClipOpPriv.h" |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 18 | |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 19 | class SkAnimTimer; |
bsalomon | 4ee6bd8 | 2015-05-27 13:23:23 -0700 | [diff] [blame] | 20 | struct GrContextOptions; |
mtklein@google.com | 62b50b7 | 2013-09-16 20:42:15 +0000 | [diff] [blame] | 21 | |
reed@google.com | 4117a24 | 2012-10-30 20:26:58 +0000 | [diff] [blame] | 22 | #define DEF_GM(code) \ |
sugoi@google.com | 0f0d9b7 | 2013-02-27 15:41:12 +0000 | [diff] [blame] | 23 | static skiagm::GM* SK_MACRO_APPEND_LINE(F_)(void*) { code; } \ |
reed@google.com | 4117a24 | 2012-10-30 20:26:58 +0000 | [diff] [blame] | 24 | static skiagm::GMRegistry SK_MACRO_APPEND_LINE(R_)(SK_MACRO_APPEND_LINE(F_)); |
| 25 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 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)) |
mtklein | 2843c42 | 2016-06-09 12:20:25 -0700 | [diff] [blame] | 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);) \ |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 41 | void SK_MACRO_CONCAT(NAME, _GM)(SkCanvas * CANVAS) |
halcanary | 30b83d4 | 2014-10-26 05:23:53 -0700 | [diff] [blame] | 42 | |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 43 | namespace skiagm { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 44 | |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 45 | class GM { |
| 46 | public: |
| 47 | GM(); |
| 48 | virtual ~GM(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 49 | |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 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 | |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 59 | void draw(SkCanvas*); |
| 60 | void drawBackground(SkCanvas*); |
| 61 | void drawContent(SkCanvas*); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 62 | |
mtklein | f4ba321 | 2015-01-28 15:32:24 -0800 | [diff] [blame] | 63 | SkISize getISize() { return this->onISize(); } |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 64 | const char* getName(); |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 65 | |
mtklein | cf5d9c9 | 2015-01-23 10:31:45 -0800 | [diff] [blame] | 66 | virtual bool runAsBench() const { return false; } |
| 67 | |
reed@google.com | 487b560 | 2013-02-01 15:01:24 +0000 | [diff] [blame] | 68 | SkScalar width() { |
| 69 | return SkIntToScalar(this->getISize().width()); |
| 70 | } |
| 71 | SkScalar height() { |
scroggo@google.com | ab02627 | 2013-04-12 22:14:03 +0000 | [diff] [blame] | 72 | return SkIntToScalar(this->getISize().height()); |
reed@google.com | 487b560 | 2013-02-01 15:01:24 +0000 | [diff] [blame] | 73 | } |
| 74 | |
reed@google.com | b4b49cc | 2011-12-06 16:15:42 +0000 | [diff] [blame] | 75 | SkColor getBGColor() const { return fBGColor; } |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 76 | void setBGColor(SkColor); |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 77 | |
reed@google.com | 2d6ef52 | 2012-01-03 17:20:38 +0000 | [diff] [blame] | 78 | // helper: fill a rect in the specified color based on the |
| 79 | // GM's getISize bounds. |
| 80 | void drawSizeBounds(SkCanvas*, SkColor); |
| 81 | |
reed@google.com | aef7361 | 2012-11-16 13:41:45 +0000 | [diff] [blame] | 82 | bool isCanvasDeferred() const { return fCanvasIsDeferred; } |
| 83 | void setCanvasIsDeferred(bool isDeferred) { |
| 84 | fCanvasIsDeferred = isDeferred; |
| 85 | } |
| 86 | |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 87 | bool animate(const SkAnimTimer&); |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 88 | bool handleKey(SkUnichar uni) { |
| 89 | return this->onHandleKey(uni); |
| 90 | } |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 91 | |
bsalomon | 4ee6bd8 | 2015-05-27 13:23:23 -0700 | [diff] [blame] | 92 | virtual void modifyGrContextOptions(GrContextOptions* options) {} |
| 93 | |
bsalomon | b62da80 | 2015-01-31 07:51:14 -0800 | [diff] [blame] | 94 | /** draws a standard message that the GM is only intended to be used with the GPU.*/ |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 95 | static void DrawGpuOnlyMessage(SkCanvas*); |
| 96 | |
| 97 | protected: |
reed@google.com | 7775d66 | 2012-11-27 15:15:58 +0000 | [diff] [blame] | 98 | virtual void onOnceBeforeDraw() {} |
robertphillips@google.com | 8570b5c | 2012-03-20 17:40:58 +0000 | [diff] [blame] | 99 | virtual void onDraw(SkCanvas*) = 0; |
| 100 | virtual void onDrawBackground(SkCanvas*); |
| 101 | virtual SkISize onISize() = 0; |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 102 | virtual SkString onShortName() = 0; |
mtklein | 1c40292 | 2015-01-23 11:07:07 -0800 | [diff] [blame] | 103 | |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 104 | virtual bool onAnimate(const SkAnimTimer&) { return false; } |
robertphillips | 05a4cf5 | 2016-09-08 09:02:43 -0700 | [diff] [blame] | 105 | virtual bool onHandleKey(SkUnichar uni) { return false; } |
vandebo@chromium.org | 79d3cb4 | 2012-03-21 17:34:30 +0000 | [diff] [blame] | 106 | |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 107 | private: |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 108 | Mode fMode; |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 109 | SkString fShortName; |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 110 | SkColor fBGColor; |
reed@google.com | aef7361 | 2012-11-16 13:41:45 +0000 | [diff] [blame] | 111 | bool fCanvasIsDeferred; // work-around problem in srcmode.cpp |
reed@google.com | 7775d66 | 2012-11-27 15:15:58 +0000 | [diff] [blame] | 112 | bool fHaveCalledOnceBeforeDraw; |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 113 | }; |
| 114 | |
Mike Reed | ab273fa | 2017-01-11 13:58:55 -0500 | [diff] [blame] | 115 | typedef sk_tools::Registry<GM*(*)(void*)> GMRegistry; |
halcanary | f62c634 | 2015-01-12 15:27:46 -0800 | [diff] [blame] | 116 | |
| 117 | class SimpleGM : public skiagm::GM { |
| 118 | public: |
| 119 | SimpleGM(const SkString& name, |
| 120 | void (*drawProc)(SkCanvas*), |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 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 | } |
halcanary | f62c634 | 2015-01-12 15:27:46 -0800 | [diff] [blame] | 128 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 129 | void onDraw(SkCanvas* canvas) override; |
| 130 | SkISize onISize() override; |
| 131 | SkString onShortName() override; |
halcanary | f62c634 | 2015-01-12 15:27:46 -0800 | [diff] [blame] | 132 | private: |
| 133 | SkString fName; |
| 134 | void (*fDrawProc)(SkCanvas*); |
| 135 | SkISize fSize; |
| 136 | }; |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | #endif |