blob: 519c9df771cc3fbb2be219456f55332e45c415ae [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
Hal Canary22be4c42018-06-12 12:37:31 -040011#include "../tools/Registry.h"
epoger@google.comd4af56c2011-07-25 16:27:59 +000012#include "SkBitmap.h"
reed@android.comdd0ac282009-06-20 02:38:16 +000013#include "SkCanvas.h"
Hal Canary22be4c42018-06-12 12:37:31 -040014#include "SkClipOpPriv.h"
15#include "SkMacros.h"
Mike Reed81f60ec2018-05-15 10:09:52 -040016#include "SkMetaData.h"
reed@android.comdd0ac282009-06-20 02:38:16 +000017#include "SkPaint.h"
reed@android.comdd0ac282009-06-20 02:38:16 +000018#include "SkSize.h"
reed@android.com00dae862009-06-10 15:38:48 +000019#include "SkString.h"
reed@android.com00dae862009-06-10 15:38:48 +000020
reed76113a92015-02-02 12:55:02 -080021class SkAnimTimer;
bsalomon4ee6bd82015-05-27 13:23:23 -070022struct GrContextOptions;
mtklein@google.com62b50b72013-09-16 20:42:15 +000023
reed@google.com4117a242012-10-30 20:26:58 +000024#define DEF_GM(code) \
sugoi@google.com0f0d9b72013-02-27 15:41:12 +000025 static skiagm::GM* SK_MACRO_APPEND_LINE(F_)(void*) { code; } \
reed@google.com4117a242012-10-30 20:26:58 +000026 static skiagm::GMRegistry SK_MACRO_APPEND_LINE(R_)(SK_MACRO_APPEND_LINE(F_));
27
halcanary2a243382015-09-09 08:16:41 -070028// a Simple GM is a rendering test that does not store state between
29// rendering calls or make use of the onOnceBeforeDraw() virtual; it
30// consists of:
31// * A single void(*)(SkCanvas*) function.
32// * A name.
33// * Prefered width and height.
34// * Optionally, a background color (default is white).
35#define DEF_SIMPLE_GM(NAME, CANVAS, W, H) \
36 DEF_SIMPLE_GM_BG_NAME(NAME, CANVAS, W, H, SK_ColorWHITE, SkString(#NAME))
37#define DEF_SIMPLE_GM_BG(NAME, CANVAS, W, H, BGCOLOR)\
38 DEF_SIMPLE_GM_BG_NAME(NAME, CANVAS, W, H, BGCOLOR, SkString(#NAME))
mtklein2843c422016-06-09 12:20:25 -070039#define DEF_SIMPLE_GM_BG_NAME(NAME, CANVAS, W, H, BGCOLOR, NAME_STR) \
40 static void SK_MACRO_CONCAT(NAME, _GM)(SkCanvas * CANVAS); \
41 DEF_GM(return new skiagm::SimpleGM(NAME_STR, SK_MACRO_CONCAT(NAME, _GM), \
42 SkISize::Make(W, H), BGCOLOR);) \
halcanary385fe4d2015-08-26 13:07:48 -070043 void SK_MACRO_CONCAT(NAME, _GM)(SkCanvas * CANVAS)
halcanary30b83d42014-10-26 05:23:53 -070044
reed@android.com00dae862009-06-10 15:38:48 +000045namespace skiagm {
rmistry@google.comd6176b02012-08-23 18:14:13 +000046
reed@android.com00dae862009-06-10 15:38:48 +000047 class GM {
48 public:
49 GM();
50 virtual ~GM();
rmistry@google.comd6176b02012-08-23 18:14:13 +000051
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +000052 enum Mode {
53 kGM_Mode,
54 kSample_Mode,
55 kBench_Mode,
56 };
57
58 void setMode(Mode mode) { fMode = mode; }
59 Mode getMode() const { return fMode; }
60
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000061 void draw(SkCanvas*);
62 void drawBackground(SkCanvas*);
63 void drawContent(SkCanvas*);
rmistry@google.comd6176b02012-08-23 18:14:13 +000064
mtkleinf4ba3212015-01-28 15:32:24 -080065 SkISize getISize() { return this->onISize(); }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000066 const char* getName();
reed@android.com00dae862009-06-10 15:38:48 +000067
mtkleincf5d9c92015-01-23 10:31:45 -080068 virtual bool runAsBench() const { return false; }
69
reed@google.com487b5602013-02-01 15:01:24 +000070 SkScalar width() {
71 return SkIntToScalar(this->getISize().width());
72 }
73 SkScalar height() {
scroggo@google.comab026272013-04-12 22:14:03 +000074 return SkIntToScalar(this->getISize().height());
reed@google.com487b5602013-02-01 15:01:24 +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
reed76113a92015-02-02 12:55:02 -080089 bool animate(const SkAnimTimer&);
robertphillips05a4cf52016-09-08 09:02:43 -070090 bool handleKey(SkUnichar uni) {
91 return this->onHandleKey(uni);
92 }
reedd9adfe62015-02-01 19:01:04 -080093
Mike Reed81f60ec2018-05-15 10:09:52 -040094 bool getControls(SkMetaData* controls) { return this->onGetControls(controls); }
95 void setControls(const SkMetaData& controls) { this->onSetControls(controls); }
96
bsalomon4ee6bd82015-05-27 13:23:23 -070097 virtual void modifyGrContextOptions(GrContextOptions* options) {}
98
bsalomonb62da802015-01-31 07:51:14 -080099 /** draws a standard message that the GM is only intended to be used with the GPU.*/
halcanary2a243382015-09-09 08:16:41 -0700100 static void DrawGpuOnlyMessage(SkCanvas*);
101
102 protected:
reed@google.com7775d662012-11-27 15:15:58 +0000103 virtual void onOnceBeforeDraw() {}
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000104 virtual void onDraw(SkCanvas*) = 0;
105 virtual void onDrawBackground(SkCanvas*);
106 virtual SkISize onISize() = 0;
reed@android.com8015dd82009-06-21 00:49:18 +0000107 virtual SkString onShortName() = 0;
mtklein1c402922015-01-23 11:07:07 -0800108
reed76113a92015-02-02 12:55:02 -0800109 virtual bool onAnimate(const SkAnimTimer&) { return false; }
robertphillips05a4cf52016-09-08 09:02:43 -0700110 virtual bool onHandleKey(SkUnichar uni) { return false; }
Mike Reed81f60ec2018-05-15 10:09:52 -0400111 virtual bool onGetControls(SkMetaData*) { return false; }
112 virtual void onSetControls(const SkMetaData&) {}
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000113
reed@android.com8015dd82009-06-21 00:49:18 +0000114 private:
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000115 Mode fMode;
reed@android.com8015dd82009-06-21 00:49:18 +0000116 SkString fShortName;
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000117 SkColor fBGColor;
reed@google.comaef73612012-11-16 13:41:45 +0000118 bool fCanvasIsDeferred; // work-around problem in srcmode.cpp
reed@google.com7775d662012-11-27 15:15:58 +0000119 bool fHaveCalledOnceBeforeDraw;
reed@android.com00dae862009-06-10 15:38:48 +0000120 };
121
Hal Canary972eba32018-07-30 17:07:07 -0400122 typedef GM*(*GMFactory)(void*) ;
123 typedef sk_tools::Registry<GMFactory> GMRegistry;
halcanaryf62c6342015-01-12 15:27:46 -0800124
125 class SimpleGM : public skiagm::GM {
126 public:
127 SimpleGM(const SkString& name,
128 void (*drawProc)(SkCanvas*),
halcanary2a243382015-09-09 08:16:41 -0700129 const SkISize& size,
130 SkColor backgroundColor)
131 : fName(name), fDrawProc(drawProc), fSize(size) {
132 if (backgroundColor != SK_ColorWHITE) {
133 this->setBGColor(backgroundColor);
134 }
135 }
halcanaryf62c6342015-01-12 15:27:46 -0800136 protected:
mtklein36352bf2015-03-25 18:17:31 -0700137 void onDraw(SkCanvas* canvas) override;
138 SkISize onISize() override;
139 SkString onShortName() override;
halcanaryf62c6342015-01-12 15:27:46 -0800140 private:
141 SkString fName;
142 void (*fDrawProc)(SkCanvas*);
143 SkISize fSize;
144 };
reed@android.com00dae862009-06-10 15:38:48 +0000145}
146
Mike Kleinc9eace82018-10-31 10:49:38 -0400147void MarkGMGood(SkCanvas*, SkScalar x, SkScalar y);
148void MarkGMBad (SkCanvas*, SkScalar x, SkScalar y);
149
reed@android.com00dae862009-06-10 15:38:48 +0000150#endif