blob: b8eb86ab79cf71c24b5e450674b670944b9d034e [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 */
Mike Kleinc0bd9f92019-04-23 12:05:21 -05007#include "bench/Benchmark.h"
8#include "include/core/SkBitmap.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkColorPriv.h"
11#include "include/core/SkPaint.h"
12#include "include/core/SkShader.h"
13#include "include/core/SkString.h"
14#include "include/effects/SkGradientShader.h"
reed@google.com84e9c082011-04-13 17:44:24 +000015
Michael Ludwig23003182019-08-05 11:25:23 -040016#include "tools/ToolUtils.h"
17
reed@google.com84e9c082011-04-13 17:44:24 +000018struct GradData {
19 int fCount;
20 const SkColor* fColors;
21 const SkScalar* fPos;
commit-bot@chromium.org5396a042013-07-10 16:13:14 +000022 const char* fName;
reed@google.com84e9c082011-04-13 17:44:24 +000023};
24
25static const SkColor gColors[] = {
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000026 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
commit-bot@chromium.org5396a042013-07-10 16:13:14 +000027 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,
34 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
35 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK, // 10 lines, 50 colors
reed@google.com84e9c082011-04-13 17:44:24 +000036};
37
krajcevskif461a8f2014-06-19 14:14:06 -070038static const SkColor gShallowColors[] = { 0xFF555555, 0xFF444444 };
Herb Derby4de13042017-05-15 10:49:39 -040039static const SkScalar gPos[] = {0.25f, 0.75f};
krajcevskif461a8f2014-06-19 14:14:06 -070040
commit-bot@chromium.org3400f4b2013-09-30 19:41:09 +000041// We have several special-cases depending on the number (and spacing) of colors, so
42// try to exercise those here.
reed@google.com84e9c082011-04-13 17:44:24 +000043static const GradData gGradData[] = {
halcanary96fcdcc2015-08-27 07:41:13 -070044 { 2, gColors, nullptr, "" },
45 { 50, gColors, nullptr, "_hicolor" }, // many color gradient
46 { 3, gColors, nullptr, "_3color" },
47 { 2, gShallowColors, nullptr, "_shallow" },
Herb Derby4de13042017-05-15 10:49:39 -040048 { 2, gColors, gPos, "_pos" },
reed@google.com84e9c082011-04-13 17:44:24 +000049};
50
tomhudson@google.com5ea050f2011-09-26 15:03:55 +000051/// Ignores scale
reedc6f28f72016-03-14 12:22:10 -070052static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data,
Mike Reedfae8fce2019-04-03 10:27:45 -040053 SkTileMode tm, float scale) {
Florin Malitad1aedde2017-06-07 15:03:38 -040054 return SkGradientShader::MakeLinear(pts, data.fColors, data.fPos, data.fCount, tm);
reed@google.com84e9c082011-04-13 17:44:24 +000055}
56
reedc6f28f72016-03-14 12:22:10 -070057static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data,
Mike Reedfae8fce2019-04-03 10:27:45 -040058 SkTileMode tm, float scale) {
reed@google.com84e9c082011-04-13 17:44:24 +000059 SkPoint center;
60 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
61 SkScalarAve(pts[0].fY, pts[1].fY));
reedc6f28f72016-03-14 12:22:10 -070062 return SkGradientShader::MakeRadial(center, center.fX * scale, data.fColors,
63 data.fPos, data.fCount, tm);
reed@google.com84e9c082011-04-13 17:44:24 +000064}
65
tomhudson@google.com5ea050f2011-09-26 15:03:55 +000066/// Ignores scale
reedc6f28f72016-03-14 12:22:10 -070067static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data,
Mike Reedfae8fce2019-04-03 10:27:45 -040068 SkTileMode tm, float scale) {
reed@google.com84e9c082011-04-13 17:44:24 +000069 SkPoint center;
70 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
71 SkScalarAve(pts[0].fY, pts[1].fY));
reedc6f28f72016-03-14 12:22:10 -070072 return SkGradientShader::MakeSweep(center.fX, center.fY, data.fColors, data.fPos, data.fCount);
reed@google.com84e9c082011-04-13 17:44:24 +000073}
74
tomhudson@google.com5ea050f2011-09-26 15:03:55 +000075/// Ignores scale
reedc6f28f72016-03-14 12:22:10 -070076static sk_sp<SkShader> MakeConical(const SkPoint pts[2], const GradData& data,
Mike Reedfae8fce2019-04-03 10:27:45 -040077 SkTileMode tm, float scale) {
reed@google.com258e0bf2012-06-07 14:18:34 +000078 SkPoint center0, center1;
79 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
80 SkScalarAve(pts[0].fY, pts[1].fY));
81 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
82 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
reedc6f28f72016-03-14 12:22:10 -070083 return SkGradientShader::MakeTwoPointConical(center1, (pts[1].fX - pts[0].fX) / 7,
84 center0, (pts[1].fX - pts[0].fX) / 2,
85 data.fColors, data.fPos, data.fCount, tm);
reed@google.com84e9c082011-04-13 17:44:24 +000086}
87
commit-bot@chromium.org95f79262014-04-14 16:02:31 +000088/// Ignores scale
reedc6f28f72016-03-14 12:22:10 -070089static sk_sp<SkShader> MakeConicalZeroRad(const SkPoint pts[2], const GradData& data,
Mike Reedfae8fce2019-04-03 10:27:45 -040090 SkTileMode tm, float scale) {
commit-bot@chromium.org95f79262014-04-14 16:02:31 +000091 SkPoint center0, center1;
92 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
93 SkScalarAve(pts[0].fY, pts[1].fY));
94 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
95 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
reedc6f28f72016-03-14 12:22:10 -070096 return SkGradientShader::MakeTwoPointConical(center1, 0.0,
97 center0, (pts[1].fX - pts[0].fX) / 2,
98 data.fColors, data.fPos, data.fCount, tm);
commit-bot@chromium.org95f79262014-04-14 16:02:31 +000099}
100
101/// Ignores scale
reedc6f28f72016-03-14 12:22:10 -0700102static sk_sp<SkShader> MakeConicalOutside(const SkPoint pts[2], const GradData& data,
Mike Reedfae8fce2019-04-03 10:27:45 -0400103 SkTileMode tm, float scale) {
commit-bot@chromium.org95f79262014-04-14 16:02:31 +0000104 SkPoint center0, center1;
reed80ea19c2015-05-12 10:37:34 -0700105 SkScalar radius0 = (pts[1].fX - pts[0].fX) / 10;
106 SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3;
commit-bot@chromium.org95f79262014-04-14 16:02:31 +0000107 center0.set(pts[0].fX + radius0, pts[0].fY + radius0);
108 center1.set(pts[1].fX - radius1, pts[1].fY - radius1);
reedc6f28f72016-03-14 12:22:10 -0700109 return SkGradientShader::MakeTwoPointConical(center0, radius0,
110 center1, radius1,
111 data.fColors, data.fPos,
112 data.fCount, tm);
commit-bot@chromium.org95f79262014-04-14 16:02:31 +0000113}
114
115/// Ignores scale
reedc6f28f72016-03-14 12:22:10 -0700116static sk_sp<SkShader> MakeConicalOutsideZeroRad(const SkPoint pts[2], const GradData& data,
Mike Reedfae8fce2019-04-03 10:27:45 -0400117 SkTileMode tm, float scale) {
commit-bot@chromium.org95f79262014-04-14 16:02:31 +0000118 SkPoint center0, center1;
reed80ea19c2015-05-12 10:37:34 -0700119 SkScalar radius0 = (pts[1].fX - pts[0].fX) / 10;
120 SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3;
commit-bot@chromium.org95f79262014-04-14 16:02:31 +0000121 center0.set(pts[0].fX + radius0, pts[0].fY + radius0);
122 center1.set(pts[1].fX - radius1, pts[1].fY - radius1);
reedc6f28f72016-03-14 12:22:10 -0700123 return SkGradientShader::MakeTwoPointConical(center0, 0.0,
124 center1, radius1,
125 data.fColors, data.fPos,
126 data.fCount, tm);
commit-bot@chromium.org95f79262014-04-14 16:02:31 +0000127}
128
reedc6f28f72016-03-14 12:22:10 -0700129typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData& data,
Mike Reedfae8fce2019-04-03 10:27:45 -0400130 SkTileMode tm, float scale);
reed@google.com84e9c082011-04-13 17:44:24 +0000131
132static const struct {
133 GradMaker fMaker;
134 const char* fName;
reed@google.com84e9c082011-04-13 17:44:24 +0000135} gGrads[] = {
commit-bot@chromium.org95f79262014-04-14 16:02:31 +0000136 { MakeLinear, "linear" },
137 { MakeRadial, "radial1" },
138 { MakeSweep, "sweep" },
commit-bot@chromium.org95f79262014-04-14 16:02:31 +0000139 { MakeConical, "conical" },
140 { MakeConicalZeroRad, "conicalZero" },
141 { MakeConicalOutside, "conicalOut" },
142 { MakeConicalOutsideZeroRad, "conicalOutZero" },
reed@google.com84e9c082011-04-13 17:44:24 +0000143};
144
145enum GradType { // these must match the order in gGrads
146 kLinear_GradType,
147 kRadial_GradType,
148 kSweep_GradType,
commit-bot@chromium.org95f79262014-04-14 16:02:31 +0000149 kConical_GradType,
150 kConicalZero_GradType,
151 kConicalOut_GradType,
152 kConicalOutZero_GradType
reed@google.com84e9c082011-04-13 17:44:24 +0000153};
154
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000155enum GeomType {
156 kRect_GeomType,
157 kOval_GeomType
158};
159
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000160static const char* geomtypename(GeomType gt) {
161 switch (gt) {
162 case kRect_GeomType:
163 return "rectangle";
164 case kOval_GeomType:
165 return "oval";
166 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000167 SkDEBUGFAIL("unknown geometry type");
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000168 return "error";
169 }
170}
171
reed@google.com84e9c082011-04-13 17:44:24 +0000172///////////////////////////////////////////////////////////////////////////////
173
tfarinaf168b862014-06-19 12:32:29 -0700174class GradientBench : public Benchmark {
reed@google.com84e9c082011-04-13 17:44:24 +0000175public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000176 GradientBench(GradType gradType,
commit-bot@chromium.org5396a042013-07-10 16:13:14 +0000177 GradData data = gGradData[0],
Mike Reedfae8fce2019-04-03 10:27:45 -0400178 SkTileMode tm = SkTileMode::kClamp,
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000179 GeomType geomType = kRect_GeomType,
Florin Malitad1aedde2017-06-07 15:03:38 -0400180 float scale = 1.0f)
fmalita36693b52016-02-24 15:32:57 -0800181 : fGeomType(geomType) {
182
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000183 fName.printf("gradient_%s_%s", gGrads[gradType].fName,
Michael Ludwig23003182019-08-05 11:25:23 -0400184 ToolUtils::tilemode_name(tm));
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000185 if (geomType != kRect_GeomType) {
fmalita36693b52016-02-24 15:32:57 -0800186 fName.appendf("_%s", geomtypename(geomType));
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000187 }
reed@google.com84e9c082011-04-13 17:44:24 +0000188
commit-bot@chromium.org3400f4b2013-09-30 19:41:09 +0000189 if (scale != 1.f) {
190 fName.appendf("_scale_%g", scale);
191 }
192
commit-bot@chromium.org5396a042013-07-10 16:13:14 +0000193 fName.append(data.fName);
194
fmalita36693b52016-02-24 15:32:57 -0800195 this->setupPaint(&fPaint);
Florin Malitad1aedde2017-06-07 15:03:38 -0400196 fPaint.setShader(MakeShader(gradType, data, tm, scale));
reed@google.com84e9c082011-04-13 17:44:24 +0000197 }
198
Florin Malitad1aedde2017-06-07 15:03:38 -0400199 GradientBench(GradType gradType, GradData data, bool dither)
fmalita36693b52016-02-24 15:32:57 -0800200 : fGeomType(kRect_GeomType) {
201
Michael Ludwig23003182019-08-05 11:25:23 -0400202 const char *tmname = ToolUtils::tilemode_name(SkTileMode::kClamp);
krajcevskif461a8f2014-06-19 14:14:06 -0700203 fName.printf("gradient_%s_%s", gGrads[gradType].fName, tmname);
204 fName.append(data.fName);
205
krajcevskif461a8f2014-06-19 14:14:06 -0700206 if (dither) {
207 fName.appendf("_dither");
208 }
209
fmalita36693b52016-02-24 15:32:57 -0800210 this->setupPaint(&fPaint);
Mike Reedfae8fce2019-04-03 10:27:45 -0400211 fPaint.setShader(MakeShader(gradType, data, SkTileMode::kClamp, 1.0f));
fmalita36693b52016-02-24 15:32:57 -0800212 fPaint.setDither(dither);
reed@google.com84e9c082011-04-13 17:44:24 +0000213 }
214
215protected:
fmalita36693b52016-02-24 15:32:57 -0800216 const char* onGetName() override {
reed@google.com84e9c082011-04-13 17:44:24 +0000217 return fName.c_str();
218 }
219
fmalita36693b52016-02-24 15:32:57 -0800220 SkIPoint onGetSize() override {
221 return SkIPoint::Make(kSize, kSize);
222 }
reed@google.com84e9c082011-04-13 17:44:24 +0000223
fmalita36693b52016-02-24 15:32:57 -0800224 void onDraw(int loops, SkCanvas* canvas) override {
225 const SkRect r = SkRect::MakeIWH(kSize, kSize);
reed@google.com84e9c082011-04-13 17:44:24 +0000226
bsalomona7f79b42014-08-14 12:24:46 -0700227 for (int i = 0; i < loops; i++) {
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000228 switch (fGeomType) {
229 case kRect_GeomType:
fmalita36693b52016-02-24 15:32:57 -0800230 canvas->drawRect(r, fPaint);
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000231 break;
232 case kOval_GeomType:
fmalita36693b52016-02-24 15:32:57 -0800233 canvas->drawOval(r, fPaint);
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000234 break;
235 }
reed@google.com84e9c082011-04-13 17:44:24 +0000236 }
237 }
238
239private:
John Stiles7571f9e2020-09-02 22:42:33 -0400240 using INHERITED = Benchmark;
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000241
reedc6f28f72016-03-14 12:22:10 -0700242 sk_sp<SkShader> MakeShader(GradType gradType, GradData data,
Mike Reedfae8fce2019-04-03 10:27:45 -0400243 SkTileMode tm, float scale) {
fmalita36693b52016-02-24 15:32:57 -0800244 const SkPoint pts[2] = {
245 { 0, 0 },
246 { SkIntToScalar(kSize), SkIntToScalar(kSize) }
247 };
248
Florin Malitad1aedde2017-06-07 15:03:38 -0400249 return gGrads[gradType].fMaker(pts, data, tm, scale);
fmalita36693b52016-02-24 15:32:57 -0800250 }
251
252 static const int kSize = 400;
253
254 SkString fName;
255 SkPaint fPaint;
256 const GeomType fGeomType;
reed@google.com84e9c082011-04-13 17:44:24 +0000257};
258
fmalita36693b52016-02-24 15:32:57 -0800259DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[0]); )
commit-bot@chromium.orgb120bd92013-10-07 17:18:21 +0000260DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[1]); )
261DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[2]); )
Herb Derby4de13042017-05-15 10:49:39 -0400262DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[4]); )
Mike Reedfae8fce2019-04-03 10:27:45 -0400263DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[0], SkTileMode::kRepeat); )
264DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[1], SkTileMode::kRepeat); )
265DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[2], SkTileMode::kRepeat); )
266DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[0], SkTileMode::kMirror); )
267DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[1], SkTileMode::kMirror); )
268DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[2], SkTileMode::kMirror); )
commit-bot@chromium.orgb120bd92013-10-07 17:18:21 +0000269
270DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[0]); )
271DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[1]); )
272DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[2]); )
273// Draw a radial gradient of radius 1/2 on a rectangle; half the lines should
274// be completely pinned, the other half should pe partially pinned
Mike Reedfae8fce2019-04-03 10:27:45 -0400275DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[0], SkTileMode::kClamp, kRect_GeomType, 0.5f); )
commit-bot@chromium.orgb120bd92013-10-07 17:18:21 +0000276
277// Draw a radial gradient on a circle of equal size; all the lines should
278// hit the unpinned fast path (so long as GradientBench.W == H)
Mike Reedfae8fce2019-04-03 10:27:45 -0400279DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[0], SkTileMode::kClamp, kOval_GeomType); )
commit-bot@chromium.orgb120bd92013-10-07 17:18:21 +0000280
Mike Reedfae8fce2019-04-03 10:27:45 -0400281DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[0], SkTileMode::kMirror); )
282DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[0], SkTileMode::kRepeat); )
commit-bot@chromium.orgb120bd92013-10-07 17:18:21 +0000283DEF_BENCH( return new GradientBench(kSweep_GradType); )
284DEF_BENCH( return new GradientBench(kSweep_GradType, gGradData[1]); )
285DEF_BENCH( return new GradientBench(kSweep_GradType, gGradData[2]); )
commit-bot@chromium.orgb120bd92013-10-07 17:18:21 +0000286DEF_BENCH( return new GradientBench(kConical_GradType); )
287DEF_BENCH( return new GradientBench(kConical_GradType, gGradData[1]); )
288DEF_BENCH( return new GradientBench(kConical_GradType, gGradData[2]); )
commit-bot@chromium.org95f79262014-04-14 16:02:31 +0000289DEF_BENCH( return new GradientBench(kConicalZero_GradType); )
290DEF_BENCH( return new GradientBench(kConicalZero_GradType, gGradData[1]); )
291DEF_BENCH( return new GradientBench(kConicalZero_GradType, gGradData[2]); )
292DEF_BENCH( return new GradientBench(kConicalOut_GradType); )
293DEF_BENCH( return new GradientBench(kConicalOut_GradType, gGradData[1]); )
294DEF_BENCH( return new GradientBench(kConicalOut_GradType, gGradData[2]); )
295DEF_BENCH( return new GradientBench(kConicalOutZero_GradType); )
296DEF_BENCH( return new GradientBench(kConicalOutZero_GradType, gGradData[1]); )
297DEF_BENCH( return new GradientBench(kConicalOutZero_GradType, gGradData[2]); )
commit-bot@chromium.orgb120bd92013-10-07 17:18:21 +0000298
krajcevskif461a8f2014-06-19 14:14:06 -0700299// Dithering
300DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[3], true); )
301DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[3], false); )
302DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[3], true); )
303DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[3], false); )
304DEF_BENCH( return new GradientBench(kSweep_GradType, gGradData[3], true); )
305DEF_BENCH( return new GradientBench(kSweep_GradType, gGradData[3], false); )
306DEF_BENCH( return new GradientBench(kConical_GradType, gGradData[3], true); )
307DEF_BENCH( return new GradientBench(kConical_GradType, gGradData[3], false); )
308
commit-bot@chromium.orgb120bd92013-10-07 17:18:21 +0000309///////////////////////////////////////////////////////////////////////////////
310
tfarinaf168b862014-06-19 12:32:29 -0700311class Gradient2Bench : public Benchmark {
reed@google.com73349aa2013-02-04 18:09:58 +0000312 SkString fName;
313 bool fHasAlpha;
314
reed@google.com8ac7a0f2011-06-16 13:14:19 +0000315public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000316 Gradient2Bench(bool hasAlpha) {
reed@google.com73349aa2013-02-04 18:09:58 +0000317 fName.printf("gradient_create_%s", hasAlpha ? "alpha" : "opaque");
318 fHasAlpha = hasAlpha;
319 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000320
reed@google.com8ac7a0f2011-06-16 13:14:19 +0000321protected:
Brian Salomond0072812020-07-21 17:03:56 -0400322 const char* onGetName() override {
reed@google.com73349aa2013-02-04 18:09:58 +0000323 return fName.c_str();
reed@google.com8ac7a0f2011-06-16 13:14:19 +0000324 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000325
Brian Salomond0072812020-07-21 17:03:56 -0400326 void onDraw(int loops, SkCanvas* canvas) override {
reed@google.com8ac7a0f2011-06-16 13:14:19 +0000327 SkPaint paint;
328 this->setupPaint(&paint);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000329
reed@google.com8ac7a0f2011-06-16 13:14:19 +0000330 const SkRect r = { 0, 0, SkIntToScalar(4), SkIntToScalar(4) };
reed@google.com1ca4f262011-06-24 19:17:37 +0000331 const SkPoint pts[] = {
332 { 0, 0 },
333 { SkIntToScalar(100), SkIntToScalar(100) },
334 };
reed@google.com8ac7a0f2011-06-16 13:14:19 +0000335
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000336 for (int i = 0; i < loops; i++) {
reed@google.com73349aa2013-02-04 18:09:58 +0000337 const int gray = i % 256;
338 const int alpha = fHasAlpha ? gray : 0xFF;
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000339 SkColor colors[] = {
340 SK_ColorBLACK,
reed@google.com73349aa2013-02-04 18:09:58 +0000341 SkColorSetARGB(alpha, gray, gray, gray),
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000342 SK_ColorWHITE };
reedc6f28f72016-03-14 12:22:10 -0700343 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr,
reed@google.com8ac7a0f2011-06-16 13:14:19 +0000344 SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -0400345 SkTileMode::kClamp));
reed@google.com8ac7a0f2011-06-16 13:14:19 +0000346 canvas->drawRect(r, paint);
347 }
348 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000349
reed@google.com8ac7a0f2011-06-16 13:14:19 +0000350private:
John Stiles7571f9e2020-09-02 22:42:33 -0400351 using INHERITED = Benchmark;
reed@google.com8ac7a0f2011-06-16 13:14:19 +0000352};
353
mtklein@google.com410e6e82013-09-13 19:52:27 +0000354DEF_BENCH( return new Gradient2Bench(false); )
355DEF_BENCH( return new Gradient2Bench(true); )