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" |
| 16 | #include "SkTRegistry.h" |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 17 | #include "sk_tool_utils.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; |
| 20 | |
mtklein@google.com | 62b50b7 | 2013-09-16 20:42:15 +0000 | [diff] [blame] | 21 | #if SK_SUPPORT_GPU |
| 22 | #include "GrContext.h" |
| 23 | #endif |
| 24 | |
reed@google.com | 4117a24 | 2012-10-30 20:26:58 +0000 | [diff] [blame] | 25 | #define DEF_GM(code) \ |
sugoi@google.com | 0f0d9b7 | 2013-02-27 15:41:12 +0000 | [diff] [blame] | 26 | static skiagm::GM* SK_MACRO_APPEND_LINE(F_)(void*) { code; } \ |
reed@google.com | 4117a24 | 2012-10-30 20:26:58 +0000 | [diff] [blame] | 27 | static skiagm::GMRegistry SK_MACRO_APPEND_LINE(R_)(SK_MACRO_APPEND_LINE(F_)); |
| 28 | |
halcanary | 30b83d4 | 2014-10-26 05:23:53 -0700 | [diff] [blame] | 29 | // See colorwheel.cpp for example usage. |
halcanary | f62c634 | 2015-01-12 15:27:46 -0800 | [diff] [blame] | 30 | #define DEF_SIMPLE_GM(NAME, CANVAS, W, H) \ |
| 31 | static void SK_MACRO_CONCAT(NAME, _GM)(SkCanvas* CANVAS); \ |
| 32 | DEF_GM( return SkNEW_ARGS(skiagm::SimpleGM, \ |
| 33 | (SkString(#NAME), \ |
| 34 | SK_MACRO_CONCAT(NAME, _GM), \ |
| 35 | SkISize::Make(W, H))); ) \ |
| 36 | void SK_MACRO_CONCAT(NAME, _GM)(SkCanvas* CANVAS) |
halcanary | 30b83d4 | 2014-10-26 05:23:53 -0700 | [diff] [blame] | 37 | |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 38 | namespace skiagm { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 39 | |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 40 | class GM { |
| 41 | public: |
| 42 | GM(); |
| 43 | virtual ~GM(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 44 | |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 45 | enum Mode { |
| 46 | kGM_Mode, |
| 47 | kSample_Mode, |
| 48 | kBench_Mode, |
| 49 | }; |
| 50 | |
| 51 | void setMode(Mode mode) { fMode = mode; } |
| 52 | Mode getMode() const { return fMode; } |
| 53 | |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 54 | void draw(SkCanvas*); |
| 55 | void drawBackground(SkCanvas*); |
| 56 | void drawContent(SkCanvas*); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 57 | |
mtklein | f4ba321 | 2015-01-28 15:32:24 -0800 | [diff] [blame] | 58 | SkISize getISize() { return this->onISize(); } |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 59 | const char* getName(); |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 60 | |
mtklein | cf5d9c9 | 2015-01-23 10:31:45 -0800 | [diff] [blame] | 61 | virtual bool runAsBench() const { return false; } |
| 62 | |
reed@google.com | 487b560 | 2013-02-01 15:01:24 +0000 | [diff] [blame] | 63 | SkScalar width() { |
| 64 | return SkIntToScalar(this->getISize().width()); |
| 65 | } |
| 66 | SkScalar height() { |
scroggo@google.com | ab02627 | 2013-04-12 22:14:03 +0000 | [diff] [blame] | 67 | return SkIntToScalar(this->getISize().height()); |
reed@google.com | 487b560 | 2013-02-01 15:01:24 +0000 | [diff] [blame] | 68 | } |
| 69 | |
vandebo@chromium.org | 79d3cb4 | 2012-03-21 17:34:30 +0000 | [diff] [blame] | 70 | // TODO(vandebo) Instead of exposing this, we should run all the GMs |
| 71 | // with and without an initial transform. |
| 72 | // Most GMs will return the identity matrix, but some PDFs tests |
| 73 | // require setting the initial transform. |
| 74 | SkMatrix getInitialTransform() const { |
commit-bot@chromium.org | f4f9df4 | 2013-09-26 20:44:24 +0000 | [diff] [blame] | 75 | SkMatrix matrix = fStarterMatrix; |
| 76 | matrix.preConcat(this->onGetInitialTransform()); |
| 77 | return matrix; |
vandebo@chromium.org | 79d3cb4 | 2012-03-21 17:34:30 +0000 | [diff] [blame] | 78 | } |
| 79 | |
reed@google.com | b4b49cc | 2011-12-06 16:15:42 +0000 | [diff] [blame] | 80 | SkColor getBGColor() const { return fBGColor; } |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 81 | void setBGColor(SkColor); |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 82 | |
reed@google.com | 2d6ef52 | 2012-01-03 17:20:38 +0000 | [diff] [blame] | 83 | // helper: fill a rect in the specified color based on the |
| 84 | // GM's getISize bounds. |
| 85 | void drawSizeBounds(SkCanvas*, SkColor); |
| 86 | |
reed@google.com | aef7361 | 2012-11-16 13:41:45 +0000 | [diff] [blame] | 87 | bool isCanvasDeferred() const { return fCanvasIsDeferred; } |
| 88 | void setCanvasIsDeferred(bool isDeferred) { |
| 89 | fCanvasIsDeferred = isDeferred; |
| 90 | } |
| 91 | |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 92 | const SkMatrix& getStarterMatrix() { return fStarterMatrix; } |
| 93 | void setStarterMatrix(const SkMatrix& matrix) { |
| 94 | fStarterMatrix = matrix; |
| 95 | } |
commit-bot@chromium.org | f4f9df4 | 2013-09-26 20:44:24 +0000 | [diff] [blame] | 96 | |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 97 | bool animate(const SkAnimTimer&); |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 98 | |
robertphillips@google.com | 8570b5c | 2012-03-20 17:40:58 +0000 | [diff] [blame] | 99 | protected: |
bsalomon | b62da80 | 2015-01-31 07:51:14 -0800 | [diff] [blame] | 100 | /** draws a standard message that the GM is only intended to be used with the GPU.*/ |
Brian Salomon | 0b737c5 | 2015-01-31 14:29:16 -0500 | [diff] [blame] | 101 | void drawGpuOnlyMessage(SkCanvas*); |
reed@google.com | 7775d66 | 2012-11-27 15:15:58 +0000 | [diff] [blame] | 102 | virtual void onOnceBeforeDraw() {} |
robertphillips@google.com | 8570b5c | 2012-03-20 17:40:58 +0000 | [diff] [blame] | 103 | virtual void onDraw(SkCanvas*) = 0; |
| 104 | virtual void onDrawBackground(SkCanvas*); |
| 105 | virtual SkISize onISize() = 0; |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 106 | virtual SkString onShortName() = 0; |
mtklein | 1c40292 | 2015-01-23 11:07:07 -0800 | [diff] [blame] | 107 | |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 108 | virtual bool onAnimate(const SkAnimTimer&) { return false; } |
vandebo@chromium.org | 79d3cb4 | 2012-03-21 17:34:30 +0000 | [diff] [blame] | 109 | virtual SkMatrix onGetInitialTransform() const { return SkMatrix::I(); } |
| 110 | |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 111 | private: |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 112 | Mode fMode; |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 113 | SkString fShortName; |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 114 | SkColor fBGColor; |
reed@google.com | aef7361 | 2012-11-16 13:41:45 +0000 | [diff] [blame] | 115 | bool fCanvasIsDeferred; // work-around problem in srcmode.cpp |
reed@google.com | 7775d66 | 2012-11-27 15:15:58 +0000 | [diff] [blame] | 116 | bool fHaveCalledOnceBeforeDraw; |
commit-bot@chromium.org | f4f9df4 | 2013-09-26 20:44:24 +0000 | [diff] [blame] | 117 | SkMatrix fStarterMatrix; |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
mtklein@google.com | bd6343b | 2013-09-04 17:20:18 +0000 | [diff] [blame] | 120 | typedef SkTRegistry<GM*(*)(void*)> GMRegistry; |
halcanary | f62c634 | 2015-01-12 15:27:46 -0800 | [diff] [blame] | 121 | |
| 122 | class SimpleGM : public skiagm::GM { |
| 123 | public: |
| 124 | SimpleGM(const SkString& name, |
| 125 | void (*drawProc)(SkCanvas*), |
| 126 | const SkISize& size) |
| 127 | : fName(name), fDrawProc(drawProc), fSize(size) {} |
| 128 | protected: |
| 129 | void onDraw(SkCanvas* canvas) SK_OVERRIDE; |
| 130 | SkISize onISize() SK_OVERRIDE; |
| 131 | SkString onShortName() SK_OVERRIDE; |
| 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 |