blob: d293d40e4c83b896d4608c8f8ec15e01375c8ffd [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"
Mike Reedab273fa2017-01-11 13:58:55 -050016#include "../tools/Registry.h"
Mike Reedebfce6d2016-12-12 10:02:12 -050017#include "SkClipOpPriv.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
halcanary2a243382015-09-09 08:16:41 -070026// 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))
mtklein2843c422016-06-09 12:20:25 -070037#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);) \
halcanary385fe4d2015-08-26 13:07:48 -070041 void SK_MACRO_CONCAT(NAME, _GM)(SkCanvas * CANVAS)
halcanary30b83d42014-10-26 05:23:53 -070042
reed@android.com00dae862009-06-10 15:38:48 +000043namespace skiagm {
rmistry@google.comd6176b02012-08-23 18:14:13 +000044
reed@android.com00dae862009-06-10 15:38:48 +000045 class GM {
46 public:
47 GM();
48 virtual ~GM();
rmistry@google.comd6176b02012-08-23 18:14:13 +000049
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +000050 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.com48dd1a22011-10-31 14:18:20 +000059 void draw(SkCanvas*);
60 void drawBackground(SkCanvas*);
61 void drawContent(SkCanvas*);
rmistry@google.comd6176b02012-08-23 18:14:13 +000062
mtkleinf4ba3212015-01-28 15:32:24 -080063 SkISize getISize() { return this->onISize(); }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000064 const char* getName();
reed@android.com00dae862009-06-10 15:38:48 +000065
mtkleincf5d9c92015-01-23 10:31:45 -080066 virtual bool runAsBench() const { return false; }
67
reed@google.com487b5602013-02-01 15:01:24 +000068 SkScalar width() {
69 return SkIntToScalar(this->getISize().width());
70 }
71 SkScalar height() {
scroggo@google.comab026272013-04-12 22:14:03 +000072 return SkIntToScalar(this->getISize().height());
reed@google.com487b5602013-02-01 15:01:24 +000073 }
74
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000075 // TODO(vandebo) Instead of exposing this, we should run all the GMs
76 // with and without an initial transform.
77 // Most GMs will return the identity matrix, but some PDFs tests
78 // require setting the initial transform.
79 SkMatrix getInitialTransform() const {
commit-bot@chromium.orgf4f9df42013-09-26 20:44:24 +000080 SkMatrix matrix = fStarterMatrix;
81 matrix.preConcat(this->onGetInitialTransform());
82 return matrix;
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000083 }
84
reed@google.comb4b49cc2011-12-06 16:15:42 +000085 SkColor getBGColor() const { return fBGColor; }
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000086 void setBGColor(SkColor);
reed@google.com30db5992011-08-29 17:41:02 +000087
reed@google.com2d6ef522012-01-03 17:20:38 +000088 // helper: fill a rect in the specified color based on the
89 // GM's getISize bounds.
90 void drawSizeBounds(SkCanvas*, SkColor);
91
reed@google.comaef73612012-11-16 13:41:45 +000092 bool isCanvasDeferred() const { return fCanvasIsDeferred; }
93 void setCanvasIsDeferred(bool isDeferred) {
94 fCanvasIsDeferred = isDeferred;
95 }
96
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +000097 const SkMatrix& getStarterMatrix() { return fStarterMatrix; }
98 void setStarterMatrix(const SkMatrix& matrix) {
99 fStarterMatrix = matrix;
100 }
commit-bot@chromium.orgf4f9df42013-09-26 20:44:24 +0000101
reed76113a92015-02-02 12:55:02 -0800102 bool animate(const SkAnimTimer&);
robertphillips05a4cf52016-09-08 09:02:43 -0700103 bool handleKey(SkUnichar uni) {
104 return this->onHandleKey(uni);
105 }
reedd9adfe62015-02-01 19:01:04 -0800106
bsalomon4ee6bd82015-05-27 13:23:23 -0700107 virtual void modifyGrContextOptions(GrContextOptions* options) {}
108
bsalomonb62da802015-01-31 07:51:14 -0800109 /** draws a standard message that the GM is only intended to be used with the GPU.*/
halcanary2a243382015-09-09 08:16:41 -0700110 static void DrawGpuOnlyMessage(SkCanvas*);
111
112 protected:
reed@google.com7775d662012-11-27 15:15:58 +0000113 virtual void onOnceBeforeDraw() {}
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000114 virtual void onDraw(SkCanvas*) = 0;
115 virtual void onDrawBackground(SkCanvas*);
116 virtual SkISize onISize() = 0;
reed@android.com8015dd82009-06-21 00:49:18 +0000117 virtual SkString onShortName() = 0;
mtklein1c402922015-01-23 11:07:07 -0800118
reed76113a92015-02-02 12:55:02 -0800119 virtual bool onAnimate(const SkAnimTimer&) { return false; }
robertphillips05a4cf52016-09-08 09:02:43 -0700120 virtual bool onHandleKey(SkUnichar uni) { return false; }
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000121 virtual SkMatrix onGetInitialTransform() const { return SkMatrix::I(); }
122
reed@android.com8015dd82009-06-21 00:49:18 +0000123 private:
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000124 Mode fMode;
reed@android.com8015dd82009-06-21 00:49:18 +0000125 SkString fShortName;
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000126 SkColor fBGColor;
reed@google.comaef73612012-11-16 13:41:45 +0000127 bool fCanvasIsDeferred; // work-around problem in srcmode.cpp
reed@google.com7775d662012-11-27 15:15:58 +0000128 bool fHaveCalledOnceBeforeDraw;
commit-bot@chromium.orgf4f9df42013-09-26 20:44:24 +0000129 SkMatrix fStarterMatrix;
reed@android.com00dae862009-06-10 15:38:48 +0000130 };
131
Mike Reedab273fa2017-01-11 13:58:55 -0500132 typedef sk_tools::Registry<GM*(*)(void*)> GMRegistry;
halcanaryf62c6342015-01-12 15:27:46 -0800133
134 class SimpleGM : public skiagm::GM {
135 public:
136 SimpleGM(const SkString& name,
137 void (*drawProc)(SkCanvas*),
halcanary2a243382015-09-09 08:16:41 -0700138 const SkISize& size,
139 SkColor backgroundColor)
140 : fName(name), fDrawProc(drawProc), fSize(size) {
141 if (backgroundColor != SK_ColorWHITE) {
142 this->setBGColor(backgroundColor);
143 }
144 }
halcanaryf62c6342015-01-12 15:27:46 -0800145 protected:
mtklein36352bf2015-03-25 18:17:31 -0700146 void onDraw(SkCanvas* canvas) override;
147 SkISize onISize() override;
148 SkString onShortName() override;
halcanaryf62c6342015-01-12 15:27:46 -0800149 private:
150 SkString fName;
151 void (*fDrawProc)(SkCanvas*);
152 SkISize fSize;
153 };
reed@android.com00dae862009-06-10 15:38:48 +0000154}
155
156#endif