blob: e6c02d3335e767839df880bf3f79e8b36ea833e7 [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 */
tfarinaf168b862014-06-19 12:32:29 -07007#include "Benchmark.h"
reed@google.com84e9c082011-04-13 17:44:24 +00008#include "SkBitmap.h"
9#include "SkCanvas.h"
10#include "SkColorPriv.h"
11#include "SkGradientShader.h"
12#include "SkPaint.h"
13#include "SkShader.h"
14#include "SkString.h"
reed@google.com84e9c082011-04-13 17:44:24 +000015
16struct GradData {
17 int fCount;
18 const SkColor* fColors;
19 const SkScalar* fPos;
commit-bot@chromium.org5396a042013-07-10 16:13:14 +000020 const char* fName;
reed@google.com84e9c082011-04-13 17:44:24 +000021};
22
23static const SkColor gColors[] = {
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000024 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
commit-bot@chromium.org5396a042013-07-10 16:13:14 +000025 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
26 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
27 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
28 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
29 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
30 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
31 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
32 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
33 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK, // 10 lines, 50 colors
reed@google.com84e9c082011-04-13 17:44:24 +000034};
35
krajcevskif461a8f2014-06-19 14:14:06 -070036static const SkColor gShallowColors[] = { 0xFF555555, 0xFF444444 };
Herb Derby4de13042017-05-15 10:49:39 -040037static const SkScalar gPos[] = {0.25f, 0.75f};
krajcevskif461a8f2014-06-19 14:14:06 -070038
commit-bot@chromium.org3400f4b2013-09-30 19:41:09 +000039// We have several special-cases depending on the number (and spacing) of colors, so
40// try to exercise those here.
reed@google.com84e9c082011-04-13 17:44:24 +000041static const GradData gGradData[] = {
halcanary96fcdcc2015-08-27 07:41:13 -070042 { 2, gColors, nullptr, "" },
43 { 50, gColors, nullptr, "_hicolor" }, // many color gradient
44 { 3, gColors, nullptr, "_3color" },
45 { 2, gShallowColors, nullptr, "_shallow" },
Herb Derby4de13042017-05-15 10:49:39 -040046 { 2, gColors, gPos, "_pos" },
reed@google.com84e9c082011-04-13 17:44:24 +000047};
48
tomhudson@google.com5ea050f2011-09-26 15:03:55 +000049/// Ignores scale
reedc6f28f72016-03-14 12:22:10 -070050static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data,
Florin Malitad1aedde2017-06-07 15:03:38 -040051 SkShader::TileMode tm, float scale) {
52 return SkGradientShader::MakeLinear(pts, data.fColors, data.fPos, data.fCount, tm);
reed@google.com84e9c082011-04-13 17:44:24 +000053}
54
reedc6f28f72016-03-14 12:22:10 -070055static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data,
Florin Malitad1aedde2017-06-07 15:03:38 -040056 SkShader::TileMode tm, float scale) {
reed@google.com84e9c082011-04-13 17:44:24 +000057 SkPoint center;
58 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
59 SkScalarAve(pts[0].fY, pts[1].fY));
reedc6f28f72016-03-14 12:22:10 -070060 return SkGradientShader::MakeRadial(center, center.fX * scale, data.fColors,
61 data.fPos, data.fCount, tm);
reed@google.com84e9c082011-04-13 17:44:24 +000062}
63
tomhudson@google.com5ea050f2011-09-26 15:03:55 +000064/// Ignores scale
reedc6f28f72016-03-14 12:22:10 -070065static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data,
Florin Malitad1aedde2017-06-07 15:03:38 -040066 SkShader::TileMode tm, float scale) {
reed@google.com84e9c082011-04-13 17:44:24 +000067 SkPoint center;
68 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
69 SkScalarAve(pts[0].fY, pts[1].fY));
reedc6f28f72016-03-14 12:22:10 -070070 return SkGradientShader::MakeSweep(center.fX, center.fY, data.fColors, data.fPos, data.fCount);
reed@google.com84e9c082011-04-13 17:44:24 +000071}
72
tomhudson@google.com5ea050f2011-09-26 15:03:55 +000073/// Ignores scale
reedc6f28f72016-03-14 12:22:10 -070074static sk_sp<SkShader> MakeConical(const SkPoint pts[2], const GradData& data,
Florin Malitad1aedde2017-06-07 15:03:38 -040075 SkShader::TileMode tm, float scale) {
reed@google.com258e0bf2012-06-07 14:18:34 +000076 SkPoint center0, center1;
77 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
78 SkScalarAve(pts[0].fY, pts[1].fY));
79 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
80 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
reedc6f28f72016-03-14 12:22:10 -070081 return SkGradientShader::MakeTwoPointConical(center1, (pts[1].fX - pts[0].fX) / 7,
82 center0, (pts[1].fX - pts[0].fX) / 2,
83 data.fColors, data.fPos, data.fCount, tm);
reed@google.com84e9c082011-04-13 17:44:24 +000084}
85
commit-bot@chromium.org95f79262014-04-14 16:02:31 +000086/// Ignores scale
reedc6f28f72016-03-14 12:22:10 -070087static sk_sp<SkShader> MakeConicalZeroRad(const SkPoint pts[2], const GradData& data,
Florin Malitad1aedde2017-06-07 15:03:38 -040088 SkShader::TileMode tm, float scale) {
commit-bot@chromium.org95f79262014-04-14 16:02:31 +000089 SkPoint center0, center1;
90 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
91 SkScalarAve(pts[0].fY, pts[1].fY));
92 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
93 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
reedc6f28f72016-03-14 12:22:10 -070094 return SkGradientShader::MakeTwoPointConical(center1, 0.0,
95 center0, (pts[1].fX - pts[0].fX) / 2,
96 data.fColors, data.fPos, data.fCount, tm);
commit-bot@chromium.org95f79262014-04-14 16:02:31 +000097}
98
99/// Ignores scale
reedc6f28f72016-03-14 12:22:10 -0700100static sk_sp<SkShader> MakeConicalOutside(const SkPoint pts[2], const GradData& data,
Florin Malitad1aedde2017-06-07 15:03:38 -0400101 SkShader::TileMode tm, float scale) {
commit-bot@chromium.org95f79262014-04-14 16:02:31 +0000102 SkPoint center0, center1;
reed80ea19c2015-05-12 10:37:34 -0700103 SkScalar radius0 = (pts[1].fX - pts[0].fX) / 10;
104 SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3;
commit-bot@chromium.org95f79262014-04-14 16:02:31 +0000105 center0.set(pts[0].fX + radius0, pts[0].fY + radius0);
106 center1.set(pts[1].fX - radius1, pts[1].fY - radius1);
reedc6f28f72016-03-14 12:22:10 -0700107 return SkGradientShader::MakeTwoPointConical(center0, radius0,
108 center1, radius1,
109 data.fColors, data.fPos,
110 data.fCount, tm);
commit-bot@chromium.org95f79262014-04-14 16:02:31 +0000111}
112
113/// Ignores scale
reedc6f28f72016-03-14 12:22:10 -0700114static sk_sp<SkShader> MakeConicalOutsideZeroRad(const SkPoint pts[2], const GradData& data,
Florin Malitad1aedde2017-06-07 15:03:38 -0400115 SkShader::TileMode tm, float scale) {
commit-bot@chromium.org95f79262014-04-14 16:02:31 +0000116 SkPoint center0, center1;
reed80ea19c2015-05-12 10:37:34 -0700117 SkScalar radius0 = (pts[1].fX - pts[0].fX) / 10;
118 SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3;
commit-bot@chromium.org95f79262014-04-14 16:02:31 +0000119 center0.set(pts[0].fX + radius0, pts[0].fY + radius0);
120 center1.set(pts[1].fX - radius1, pts[1].fY - radius1);
reedc6f28f72016-03-14 12:22:10 -0700121 return SkGradientShader::MakeTwoPointConical(center0, 0.0,
122 center1, radius1,
123 data.fColors, data.fPos,
124 data.fCount, tm);
commit-bot@chromium.org95f79262014-04-14 16:02:31 +0000125}
126
reedc6f28f72016-03-14 12:22:10 -0700127typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData& data,
Florin Malitad1aedde2017-06-07 15:03:38 -0400128 SkShader::TileMode tm, float scale);
reed@google.com84e9c082011-04-13 17:44:24 +0000129
130static const struct {
131 GradMaker fMaker;
132 const char* fName;
reed@google.com84e9c082011-04-13 17:44:24 +0000133} gGrads[] = {
commit-bot@chromium.org95f79262014-04-14 16:02:31 +0000134 { MakeLinear, "linear" },
135 { MakeRadial, "radial1" },
136 { MakeSweep, "sweep" },
commit-bot@chromium.org95f79262014-04-14 16:02:31 +0000137 { MakeConical, "conical" },
138 { MakeConicalZeroRad, "conicalZero" },
139 { MakeConicalOutside, "conicalOut" },
140 { MakeConicalOutsideZeroRad, "conicalOutZero" },
reed@google.com84e9c082011-04-13 17:44:24 +0000141};
142
143enum GradType { // these must match the order in gGrads
144 kLinear_GradType,
145 kRadial_GradType,
146 kSweep_GradType,
commit-bot@chromium.org95f79262014-04-14 16:02:31 +0000147 kConical_GradType,
148 kConicalZero_GradType,
149 kConicalOut_GradType,
150 kConicalOutZero_GradType
reed@google.com84e9c082011-04-13 17:44:24 +0000151};
152
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000153enum GeomType {
154 kRect_GeomType,
155 kOval_GeomType
156};
157
reed@google.com72415162011-06-15 21:17:37 +0000158static const char* tilemodename(SkShader::TileMode tm) {
159 switch (tm) {
160 case SkShader::kClamp_TileMode:
161 return "clamp";
162 case SkShader::kRepeat_TileMode:
163 return "repeat";
164 case SkShader::kMirror_TileMode:
165 return "mirror";
166 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000167 SkDEBUGFAIL("unknown tilemode");
reed@google.com72415162011-06-15 21:17:37 +0000168 return "error";
169 }
170}
171
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000172static const char* geomtypename(GeomType gt) {
173 switch (gt) {
174 case kRect_GeomType:
175 return "rectangle";
176 case kOval_GeomType:
177 return "oval";
178 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000179 SkDEBUGFAIL("unknown geometry type");
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000180 return "error";
181 }
182}
183
reed@google.com84e9c082011-04-13 17:44:24 +0000184///////////////////////////////////////////////////////////////////////////////
185
tfarinaf168b862014-06-19 12:32:29 -0700186class GradientBench : public Benchmark {
reed@google.com84e9c082011-04-13 17:44:24 +0000187public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000188 GradientBench(GradType gradType,
commit-bot@chromium.org5396a042013-07-10 16:13:14 +0000189 GradData data = gGradData[0],
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000190 SkShader::TileMode tm = SkShader::kClamp_TileMode,
191 GeomType geomType = kRect_GeomType,
Florin Malitad1aedde2017-06-07 15:03:38 -0400192 float scale = 1.0f)
fmalita36693b52016-02-24 15:32:57 -0800193 : fGeomType(geomType) {
194
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000195 fName.printf("gradient_%s_%s", gGrads[gradType].fName,
196 tilemodename(tm));
197 if (geomType != kRect_GeomType) {
fmalita36693b52016-02-24 15:32:57 -0800198 fName.appendf("_%s", geomtypename(geomType));
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000199 }
reed@google.com84e9c082011-04-13 17:44:24 +0000200
commit-bot@chromium.org3400f4b2013-09-30 19:41:09 +0000201 if (scale != 1.f) {
202 fName.appendf("_scale_%g", scale);
203 }
204
commit-bot@chromium.org5396a042013-07-10 16:13:14 +0000205 fName.append(data.fName);
206
fmalita36693b52016-02-24 15:32:57 -0800207 this->setupPaint(&fPaint);
Florin Malitad1aedde2017-06-07 15:03:38 -0400208 fPaint.setShader(MakeShader(gradType, data, tm, scale));
reed@google.com84e9c082011-04-13 17:44:24 +0000209 }
210
Florin Malitad1aedde2017-06-07 15:03:38 -0400211 GradientBench(GradType gradType, GradData data, bool dither)
fmalita36693b52016-02-24 15:32:57 -0800212 : fGeomType(kRect_GeomType) {
213
krajcevskif461a8f2014-06-19 14:14:06 -0700214 const char *tmname = tilemodename(SkShader::kClamp_TileMode);
215 fName.printf("gradient_%s_%s", gGrads[gradType].fName, tmname);
216 fName.append(data.fName);
217
krajcevskif461a8f2014-06-19 14:14:06 -0700218 if (dither) {
219 fName.appendf("_dither");
220 }
221
fmalita36693b52016-02-24 15:32:57 -0800222 this->setupPaint(&fPaint);
Florin Malitad1aedde2017-06-07 15:03:38 -0400223 fPaint.setShader(MakeShader(gradType, data, SkShader::kClamp_TileMode, 1.0f));
fmalita36693b52016-02-24 15:32:57 -0800224 fPaint.setDither(dither);
reed@google.com84e9c082011-04-13 17:44:24 +0000225 }
226
227protected:
fmalita36693b52016-02-24 15:32:57 -0800228 const char* onGetName() override {
reed@google.com84e9c082011-04-13 17:44:24 +0000229 return fName.c_str();
230 }
231
fmalita36693b52016-02-24 15:32:57 -0800232 SkIPoint onGetSize() override {
233 return SkIPoint::Make(kSize, kSize);
234 }
reed@google.com84e9c082011-04-13 17:44:24 +0000235
fmalita36693b52016-02-24 15:32:57 -0800236 void onDraw(int loops, SkCanvas* canvas) override {
237 const SkRect r = SkRect::MakeIWH(kSize, kSize);
reed@google.com84e9c082011-04-13 17:44:24 +0000238
bsalomona7f79b42014-08-14 12:24:46 -0700239 for (int i = 0; i < loops; i++) {
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000240 switch (fGeomType) {
241 case kRect_GeomType:
fmalita36693b52016-02-24 15:32:57 -0800242 canvas->drawRect(r, fPaint);
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000243 break;
244 case kOval_GeomType:
fmalita36693b52016-02-24 15:32:57 -0800245 canvas->drawOval(r, fPaint);
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000246 break;
247 }
reed@google.com84e9c082011-04-13 17:44:24 +0000248 }
249 }
250
251private:
tfarinaf168b862014-06-19 12:32:29 -0700252 typedef Benchmark INHERITED;
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000253
reedc6f28f72016-03-14 12:22:10 -0700254 sk_sp<SkShader> MakeShader(GradType gradType, GradData data,
Florin Malitad1aedde2017-06-07 15:03:38 -0400255 SkShader::TileMode tm, float scale) {
fmalita36693b52016-02-24 15:32:57 -0800256 const SkPoint pts[2] = {
257 { 0, 0 },
258 { SkIntToScalar(kSize), SkIntToScalar(kSize) }
259 };
260
Florin Malitad1aedde2017-06-07 15:03:38 -0400261 return gGrads[gradType].fMaker(pts, data, tm, scale);
fmalita36693b52016-02-24 15:32:57 -0800262 }
263
264 static const int kSize = 400;
265
266 SkString fName;
267 SkPaint fPaint;
268 const GeomType fGeomType;
reed@google.com84e9c082011-04-13 17:44:24 +0000269};
270
fmalita36693b52016-02-24 15:32:57 -0800271DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[0]); )
commit-bot@chromium.orgb120bd92013-10-07 17:18:21 +0000272DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[1]); )
273DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[2]); )
Herb Derby4de13042017-05-15 10:49:39 -0400274DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[4]); )
fmalita36693b52016-02-24 15:32:57 -0800275DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[0], SkShader::kRepeat_TileMode); )
276DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[1], SkShader::kRepeat_TileMode); )
277DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[2], SkShader::kRepeat_TileMode); )
commit-bot@chromium.orgb120bd92013-10-07 17:18:21 +0000278DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[0], SkShader::kMirror_TileMode); )
fmalita36693b52016-02-24 15:32:57 -0800279DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[1], SkShader::kMirror_TileMode); )
280DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[2], SkShader::kMirror_TileMode); )
commit-bot@chromium.orgb120bd92013-10-07 17:18:21 +0000281
282DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[0]); )
283DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[1]); )
284DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[2]); )
285// Draw a radial gradient of radius 1/2 on a rectangle; half the lines should
286// be completely pinned, the other half should pe partially pinned
287DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[0], SkShader::kClamp_TileMode, kRect_GeomType, 0.5f); )
288
289// Draw a radial gradient on a circle of equal size; all the lines should
290// hit the unpinned fast path (so long as GradientBench.W == H)
291DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[0], SkShader::kClamp_TileMode, kOval_GeomType); )
292
293DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[0], SkShader::kMirror_TileMode); )
commit-bot@chromium.org34150b42013-10-16 18:59:44 +0000294DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[0], SkShader::kRepeat_TileMode); )
commit-bot@chromium.orgb120bd92013-10-07 17:18:21 +0000295DEF_BENCH( return new GradientBench(kSweep_GradType); )
296DEF_BENCH( return new GradientBench(kSweep_GradType, gGradData[1]); )
297DEF_BENCH( return new GradientBench(kSweep_GradType, gGradData[2]); )
commit-bot@chromium.orgb120bd92013-10-07 17:18:21 +0000298DEF_BENCH( return new GradientBench(kConical_GradType); )
299DEF_BENCH( return new GradientBench(kConical_GradType, gGradData[1]); )
300DEF_BENCH( return new GradientBench(kConical_GradType, gGradData[2]); )
commit-bot@chromium.org95f79262014-04-14 16:02:31 +0000301DEF_BENCH( return new GradientBench(kConicalZero_GradType); )
302DEF_BENCH( return new GradientBench(kConicalZero_GradType, gGradData[1]); )
303DEF_BENCH( return new GradientBench(kConicalZero_GradType, gGradData[2]); )
304DEF_BENCH( return new GradientBench(kConicalOut_GradType); )
305DEF_BENCH( return new GradientBench(kConicalOut_GradType, gGradData[1]); )
306DEF_BENCH( return new GradientBench(kConicalOut_GradType, gGradData[2]); )
307DEF_BENCH( return new GradientBench(kConicalOutZero_GradType); )
308DEF_BENCH( return new GradientBench(kConicalOutZero_GradType, gGradData[1]); )
309DEF_BENCH( return new GradientBench(kConicalOutZero_GradType, gGradData[2]); )
commit-bot@chromium.orgb120bd92013-10-07 17:18:21 +0000310
krajcevskif461a8f2014-06-19 14:14:06 -0700311// Dithering
312DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[3], true); )
313DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[3], false); )
314DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[3], true); )
315DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[3], false); )
316DEF_BENCH( return new GradientBench(kSweep_GradType, gGradData[3], true); )
317DEF_BENCH( return new GradientBench(kSweep_GradType, gGradData[3], false); )
318DEF_BENCH( return new GradientBench(kConical_GradType, gGradData[3], true); )
319DEF_BENCH( return new GradientBench(kConical_GradType, gGradData[3], false); )
320
commit-bot@chromium.orgb120bd92013-10-07 17:18:21 +0000321///////////////////////////////////////////////////////////////////////////////
322
tfarinaf168b862014-06-19 12:32:29 -0700323class Gradient2Bench : public Benchmark {
reed@google.com73349aa2013-02-04 18:09:58 +0000324 SkString fName;
325 bool fHasAlpha;
326
reed@google.com8ac7a0f2011-06-16 13:14:19 +0000327public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000328 Gradient2Bench(bool hasAlpha) {
reed@google.com73349aa2013-02-04 18:09:58 +0000329 fName.printf("gradient_create_%s", hasAlpha ? "alpha" : "opaque");
330 fHasAlpha = hasAlpha;
331 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000332
reed@google.com8ac7a0f2011-06-16 13:14:19 +0000333protected:
334 virtual const char* onGetName() {
reed@google.com73349aa2013-02-04 18:09:58 +0000335 return fName.c_str();
reed@google.com8ac7a0f2011-06-16 13:14:19 +0000336 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000337
mtkleina1ebeb22015-10-01 09:43:39 -0700338 virtual void onDraw(int loops, SkCanvas* canvas) {
reed@google.com8ac7a0f2011-06-16 13:14:19 +0000339 SkPaint paint;
340 this->setupPaint(&paint);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000341
reed@google.com8ac7a0f2011-06-16 13:14:19 +0000342 const SkRect r = { 0, 0, SkIntToScalar(4), SkIntToScalar(4) };
reed@google.com1ca4f262011-06-24 19:17:37 +0000343 const SkPoint pts[] = {
344 { 0, 0 },
345 { SkIntToScalar(100), SkIntToScalar(100) },
346 };
reed@google.com8ac7a0f2011-06-16 13:14:19 +0000347
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000348 for (int i = 0; i < loops; i++) {
reed@google.com73349aa2013-02-04 18:09:58 +0000349 const int gray = i % 256;
350 const int alpha = fHasAlpha ? gray : 0xFF;
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000351 SkColor colors[] = {
352 SK_ColorBLACK,
reed@google.com73349aa2013-02-04 18:09:58 +0000353 SkColorSetARGB(alpha, gray, gray, gray),
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000354 SK_ColorWHITE };
reedc6f28f72016-03-14 12:22:10 -0700355 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr,
reed@google.com8ac7a0f2011-06-16 13:14:19 +0000356 SK_ARRAY_COUNT(colors),
reedc6f28f72016-03-14 12:22:10 -0700357 SkShader::kClamp_TileMode));
reed@google.com8ac7a0f2011-06-16 13:14:19 +0000358 canvas->drawRect(r, paint);
359 }
360 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000361
reed@google.com8ac7a0f2011-06-16 13:14:19 +0000362private:
tfarinaf168b862014-06-19 12:32:29 -0700363 typedef Benchmark INHERITED;
reed@google.com8ac7a0f2011-06-16 13:14:19 +0000364};
365
mtklein@google.com410e6e82013-09-13 19:52:27 +0000366DEF_BENCH( return new Gradient2Bench(false); )
367DEF_BENCH( return new Gradient2Bench(true); )