blob: 24c610041caee12758b65e1991c11fe7d7dae83c [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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.orgc3bd8af2014-02-13 17:14:46 +00007
reed@android.com00dae862009-06-10 15:38:48 +00008#ifndef skiagm_DEFINED
9#define skiagm_DEFINED
10
epoger@google.comd4af56c2011-07-25 16:27:59 +000011#include "SkBitmap.h"
reed@android.comdd0ac282009-06-20 02:38:16 +000012#include "SkCanvas.h"
13#include "SkPaint.h"
reed@android.comdd0ac282009-06-20 02:38:16 +000014#include "SkSize.h"
reed@android.com00dae862009-06-10 15:38:48 +000015#include "SkString.h"
16#include "SkTRegistry.h"
caryclark5fb6bd42014-06-23 11:25:00 -070017#include "sk_tool_utils.h"
reed@android.com00dae862009-06-10 15:38:48 +000018
reed76113a92015-02-02 12:55:02 -080019class SkAnimTimer;
bsalomon4ee6bd82015-05-27 13:23:23 -070020struct GrContextOptions;
mtklein@google.com62b50b72013-09-16 20:42:15 +000021
reed@google.com4117a242012-10-30 20:26:58 +000022#define DEF_GM(code) \
sugoi@google.com0f0d9b72013-02-27 15:41:12 +000023 static skiagm::GM* SK_MACRO_APPEND_LINE(F_)(void*) { code; } \
reed@google.com4117a242012-10-30 20:26:58 +000024 static skiagm::GMRegistry SK_MACRO_APPEND_LINE(R_)(SK_MACRO_APPEND_LINE(F_));
25
halcanary30b83d42014-10-26 05:23:53 -070026// See colorwheel.cpp for example usage.
halcanaryf62c6342015-01-12 15:27:46 -080027#define DEF_SIMPLE_GM(NAME, CANVAS, W, H) \
28 static void SK_MACRO_CONCAT(NAME, _GM)(SkCanvas* CANVAS); \
29 DEF_GM( return SkNEW_ARGS(skiagm::SimpleGM, \
30 (SkString(#NAME), \
31 SK_MACRO_CONCAT(NAME, _GM), \
32 SkISize::Make(W, H))); ) \
33 void SK_MACRO_CONCAT(NAME, _GM)(SkCanvas* CANVAS)
halcanary30b83d42014-10-26 05:23:53 -070034
reed@android.com00dae862009-06-10 15:38:48 +000035namespace skiagm {
rmistry@google.comd6176b02012-08-23 18:14:13 +000036
reed@android.com00dae862009-06-10 15:38:48 +000037 class GM {
38 public:
39 GM();
40 virtual ~GM();
rmistry@google.comd6176b02012-08-23 18:14:13 +000041
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +000042 enum Mode {
43 kGM_Mode,
44 kSample_Mode,
45 kBench_Mode,
46 };
47
48 void setMode(Mode mode) { fMode = mode; }
49 Mode getMode() const { return fMode; }
50
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000051 void draw(SkCanvas*);
52 void drawBackground(SkCanvas*);
53 void drawContent(SkCanvas*);
rmistry@google.comd6176b02012-08-23 18:14:13 +000054
mtkleinf4ba3212015-01-28 15:32:24 -080055 SkISize getISize() { return this->onISize(); }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000056 const char* getName();
reed@android.com00dae862009-06-10 15:38:48 +000057
mtkleincf5d9c92015-01-23 10:31:45 -080058 virtual bool runAsBench() const { return false; }
59
reed@google.com487b5602013-02-01 15:01:24 +000060 SkScalar width() {
61 return SkIntToScalar(this->getISize().width());
62 }
63 SkScalar height() {
scroggo@google.comab026272013-04-12 22:14:03 +000064 return SkIntToScalar(this->getISize().height());
reed@google.com487b5602013-02-01 15:01:24 +000065 }
66
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000067 // TODO(vandebo) Instead of exposing this, we should run all the GMs
68 // with and without an initial transform.
69 // Most GMs will return the identity matrix, but some PDFs tests
70 // require setting the initial transform.
71 SkMatrix getInitialTransform() const {
commit-bot@chromium.orgf4f9df42013-09-26 20:44:24 +000072 SkMatrix matrix = fStarterMatrix;
73 matrix.preConcat(this->onGetInitialTransform());
74 return matrix;
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000075 }
76
reed@google.comb4b49cc2011-12-06 16:15:42 +000077 SkColor getBGColor() const { return fBGColor; }
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000078 void setBGColor(SkColor);
reed@google.com30db5992011-08-29 17:41:02 +000079
reed@google.com2d6ef522012-01-03 17:20:38 +000080 // helper: fill a rect in the specified color based on the
81 // GM's getISize bounds.
82 void drawSizeBounds(SkCanvas*, SkColor);
83
reed@google.comaef73612012-11-16 13:41:45 +000084 bool isCanvasDeferred() const { return fCanvasIsDeferred; }
85 void setCanvasIsDeferred(bool isDeferred) {
86 fCanvasIsDeferred = isDeferred;
87 }
88
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +000089 const SkMatrix& getStarterMatrix() { return fStarterMatrix; }
90 void setStarterMatrix(const SkMatrix& matrix) {
91 fStarterMatrix = matrix;
92 }
commit-bot@chromium.orgf4f9df42013-09-26 20:44:24 +000093
reed76113a92015-02-02 12:55:02 -080094 bool animate(const SkAnimTimer&);
reedd9adfe62015-02-01 19:01:04 -080095
bsalomon4ee6bd82015-05-27 13:23:23 -070096 virtual void modifyGrContextOptions(GrContextOptions* options) {}
97
robertphillips@google.com8570b5c2012-03-20 17:40:58 +000098 protected:
bsalomonb62da802015-01-31 07:51:14 -080099 /** draws a standard message that the GM is only intended to be used with the GPU.*/
Brian Salomon0b737c52015-01-31 14:29:16 -0500100 void drawGpuOnlyMessage(SkCanvas*);
reed@google.com7775d662012-11-27 15:15:58 +0000101 virtual void onOnceBeforeDraw() {}
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000102 virtual void onDraw(SkCanvas*) = 0;
103 virtual void onDrawBackground(SkCanvas*);
104 virtual SkISize onISize() = 0;
reed@android.com8015dd82009-06-21 00:49:18 +0000105 virtual SkString onShortName() = 0;
mtklein1c402922015-01-23 11:07:07 -0800106
reed76113a92015-02-02 12:55:02 -0800107 virtual bool onAnimate(const SkAnimTimer&) { return false; }
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000108 virtual SkMatrix onGetInitialTransform() const { return SkMatrix::I(); }
109
reed@android.com8015dd82009-06-21 00:49:18 +0000110 private:
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000111 Mode fMode;
reed@android.com8015dd82009-06-21 00:49:18 +0000112 SkString fShortName;
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000113 SkColor fBGColor;
reed@google.comaef73612012-11-16 13:41:45 +0000114 bool fCanvasIsDeferred; // work-around problem in srcmode.cpp
reed@google.com7775d662012-11-27 15:15:58 +0000115 bool fHaveCalledOnceBeforeDraw;
commit-bot@chromium.orgf4f9df42013-09-26 20:44:24 +0000116 SkMatrix fStarterMatrix;
reed@android.com00dae862009-06-10 15:38:48 +0000117 };
118
mtklein@google.combd6343b2013-09-04 17:20:18 +0000119 typedef SkTRegistry<GM*(*)(void*)> GMRegistry;
halcanaryf62c6342015-01-12 15:27:46 -0800120
121 class SimpleGM : public skiagm::GM {
122 public:
123 SimpleGM(const SkString& name,
124 void (*drawProc)(SkCanvas*),
125 const SkISize& size)
126 : fName(name), fDrawProc(drawProc), fSize(size) {}
127 protected:
mtklein36352bf2015-03-25 18:17:31 -0700128 void onDraw(SkCanvas* canvas) override;
129 SkISize onISize() override;
130 SkString onShortName() override;
halcanaryf62c6342015-01-12 15:27:46 -0800131 private:
132 SkString fName;
133 void (*fDrawProc)(SkCanvas*);
134 SkISize fSize;
135 };
reed@android.com00dae862009-06-10 15:38:48 +0000136}
137
138#endif