epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 7 | #include "Benchmark.h" |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 8 | #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.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 15 | |
| 16 | struct GradData { |
| 17 | int fCount; |
| 18 | const SkColor* fColors; |
| 19 | const SkScalar* fPos; |
commit-bot@chromium.org | 5396a04 | 2013-07-10 16:13:14 +0000 | [diff] [blame] | 20 | const char* fName; |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 21 | }; |
| 22 | |
| 23 | static const SkColor gColors[] = { |
commit-bot@chromium.org | b686539 | 2013-07-09 19:43:29 +0000 | [diff] [blame] | 24 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK, |
commit-bot@chromium.org | 5396a04 | 2013-07-10 16:13:14 +0000 | [diff] [blame] | 25 | 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.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 34 | }; |
| 35 | |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 36 | static const SkColor gShallowColors[] = { 0xFF555555, 0xFF444444 }; |
Herb Derby | 4de1304 | 2017-05-15 10:49:39 -0400 | [diff] [blame] | 37 | static const SkScalar gPos[] = {0.25f, 0.75f}; |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 38 | |
commit-bot@chromium.org | 3400f4b | 2013-09-30 19:41:09 +0000 | [diff] [blame] | 39 | // We have several special-cases depending on the number (and spacing) of colors, so |
| 40 | // try to exercise those here. |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 41 | static const GradData gGradData[] = { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 42 | { 2, gColors, nullptr, "" }, |
| 43 | { 50, gColors, nullptr, "_hicolor" }, // many color gradient |
| 44 | { 3, gColors, nullptr, "_3color" }, |
| 45 | { 2, gShallowColors, nullptr, "_shallow" }, |
Herb Derby | 4de1304 | 2017-05-15 10:49:39 -0400 | [diff] [blame] | 46 | { 2, gColors, gPos, "_pos" }, |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 49 | /// Ignores scale |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 50 | static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data, |
Florin Malita | d1aedde | 2017-06-07 15:03:38 -0400 | [diff] [blame] | 51 | SkShader::TileMode tm, float scale) { |
| 52 | return SkGradientShader::MakeLinear(pts, data.fColors, data.fPos, data.fCount, tm); |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 53 | } |
| 54 | |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 55 | static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data, |
Florin Malita | d1aedde | 2017-06-07 15:03:38 -0400 | [diff] [blame] | 56 | SkShader::TileMode tm, float scale) { |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 57 | SkPoint center; |
| 58 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 59 | SkScalarAve(pts[0].fY, pts[1].fY)); |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 60 | return SkGradientShader::MakeRadial(center, center.fX * scale, data.fColors, |
| 61 | data.fPos, data.fCount, tm); |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 62 | } |
| 63 | |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 64 | /// Ignores scale |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 65 | static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data, |
Florin Malita | d1aedde | 2017-06-07 15:03:38 -0400 | [diff] [blame] | 66 | SkShader::TileMode tm, float scale) { |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 67 | SkPoint center; |
| 68 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 69 | SkScalarAve(pts[0].fY, pts[1].fY)); |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 70 | return SkGradientShader::MakeSweep(center.fX, center.fY, data.fColors, data.fPos, data.fCount); |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 71 | } |
| 72 | |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 73 | /// Ignores scale |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 74 | static sk_sp<SkShader> MakeConical(const SkPoint pts[2], const GradData& data, |
Florin Malita | d1aedde | 2017-06-07 15:03:38 -0400 | [diff] [blame] | 75 | SkShader::TileMode tm, float scale) { |
reed@google.com | 258e0bf | 2012-06-07 14:18:34 +0000 | [diff] [blame] | 76 | 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)); |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 81 | 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.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 84 | } |
| 85 | |
commit-bot@chromium.org | 95f7926 | 2014-04-14 16:02:31 +0000 | [diff] [blame] | 86 | /// Ignores scale |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 87 | static sk_sp<SkShader> MakeConicalZeroRad(const SkPoint pts[2], const GradData& data, |
Florin Malita | d1aedde | 2017-06-07 15:03:38 -0400 | [diff] [blame] | 88 | SkShader::TileMode tm, float scale) { |
commit-bot@chromium.org | 95f7926 | 2014-04-14 16:02:31 +0000 | [diff] [blame] | 89 | 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)); |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 94 | 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.org | 95f7926 | 2014-04-14 16:02:31 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /// Ignores scale |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 100 | static sk_sp<SkShader> MakeConicalOutside(const SkPoint pts[2], const GradData& data, |
Florin Malita | d1aedde | 2017-06-07 15:03:38 -0400 | [diff] [blame] | 101 | SkShader::TileMode tm, float scale) { |
commit-bot@chromium.org | 95f7926 | 2014-04-14 16:02:31 +0000 | [diff] [blame] | 102 | SkPoint center0, center1; |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 103 | SkScalar radius0 = (pts[1].fX - pts[0].fX) / 10; |
| 104 | SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3; |
commit-bot@chromium.org | 95f7926 | 2014-04-14 16:02:31 +0000 | [diff] [blame] | 105 | center0.set(pts[0].fX + radius0, pts[0].fY + radius0); |
| 106 | center1.set(pts[1].fX - radius1, pts[1].fY - radius1); |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 107 | return SkGradientShader::MakeTwoPointConical(center0, radius0, |
| 108 | center1, radius1, |
| 109 | data.fColors, data.fPos, |
| 110 | data.fCount, tm); |
commit-bot@chromium.org | 95f7926 | 2014-04-14 16:02:31 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /// Ignores scale |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 114 | static sk_sp<SkShader> MakeConicalOutsideZeroRad(const SkPoint pts[2], const GradData& data, |
Florin Malita | d1aedde | 2017-06-07 15:03:38 -0400 | [diff] [blame] | 115 | SkShader::TileMode tm, float scale) { |
commit-bot@chromium.org | 95f7926 | 2014-04-14 16:02:31 +0000 | [diff] [blame] | 116 | SkPoint center0, center1; |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 117 | SkScalar radius0 = (pts[1].fX - pts[0].fX) / 10; |
| 118 | SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3; |
commit-bot@chromium.org | 95f7926 | 2014-04-14 16:02:31 +0000 | [diff] [blame] | 119 | center0.set(pts[0].fX + radius0, pts[0].fY + radius0); |
| 120 | center1.set(pts[1].fX - radius1, pts[1].fY - radius1); |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 121 | return SkGradientShader::MakeTwoPointConical(center0, 0.0, |
| 122 | center1, radius1, |
| 123 | data.fColors, data.fPos, |
| 124 | data.fCount, tm); |
commit-bot@chromium.org | 95f7926 | 2014-04-14 16:02:31 +0000 | [diff] [blame] | 125 | } |
| 126 | |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 127 | typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData& data, |
Florin Malita | d1aedde | 2017-06-07 15:03:38 -0400 | [diff] [blame] | 128 | SkShader::TileMode tm, float scale); |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 129 | |
| 130 | static const struct { |
| 131 | GradMaker fMaker; |
| 132 | const char* fName; |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 133 | } gGrads[] = { |
commit-bot@chromium.org | 95f7926 | 2014-04-14 16:02:31 +0000 | [diff] [blame] | 134 | { MakeLinear, "linear" }, |
| 135 | { MakeRadial, "radial1" }, |
| 136 | { MakeSweep, "sweep" }, |
commit-bot@chromium.org | 95f7926 | 2014-04-14 16:02:31 +0000 | [diff] [blame] | 137 | { MakeConical, "conical" }, |
| 138 | { MakeConicalZeroRad, "conicalZero" }, |
| 139 | { MakeConicalOutside, "conicalOut" }, |
| 140 | { MakeConicalOutsideZeroRad, "conicalOutZero" }, |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | enum GradType { // these must match the order in gGrads |
| 144 | kLinear_GradType, |
| 145 | kRadial_GradType, |
| 146 | kSweep_GradType, |
commit-bot@chromium.org | 95f7926 | 2014-04-14 16:02:31 +0000 | [diff] [blame] | 147 | kConical_GradType, |
| 148 | kConicalZero_GradType, |
| 149 | kConicalOut_GradType, |
| 150 | kConicalOutZero_GradType |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 151 | }; |
| 152 | |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 153 | enum GeomType { |
| 154 | kRect_GeomType, |
| 155 | kOval_GeomType |
| 156 | }; |
| 157 | |
reed@google.com | 7241516 | 2011-06-15 21:17:37 +0000 | [diff] [blame] | 158 | static 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.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 167 | SkDEBUGFAIL("unknown tilemode"); |
reed@google.com | 7241516 | 2011-06-15 21:17:37 +0000 | [diff] [blame] | 168 | return "error"; |
| 169 | } |
| 170 | } |
| 171 | |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 172 | static 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.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 179 | SkDEBUGFAIL("unknown geometry type"); |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 180 | return "error"; |
| 181 | } |
| 182 | } |
| 183 | |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 184 | /////////////////////////////////////////////////////////////////////////////// |
| 185 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 186 | class GradientBench : public Benchmark { |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 187 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 188 | GradientBench(GradType gradType, |
commit-bot@chromium.org | 5396a04 | 2013-07-10 16:13:14 +0000 | [diff] [blame] | 189 | GradData data = gGradData[0], |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 190 | SkShader::TileMode tm = SkShader::kClamp_TileMode, |
| 191 | GeomType geomType = kRect_GeomType, |
Florin Malita | d1aedde | 2017-06-07 15:03:38 -0400 | [diff] [blame] | 192 | float scale = 1.0f) |
fmalita | 36693b5 | 2016-02-24 15:32:57 -0800 | [diff] [blame] | 193 | : fGeomType(geomType) { |
| 194 | |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 195 | fName.printf("gradient_%s_%s", gGrads[gradType].fName, |
| 196 | tilemodename(tm)); |
| 197 | if (geomType != kRect_GeomType) { |
fmalita | 36693b5 | 2016-02-24 15:32:57 -0800 | [diff] [blame] | 198 | fName.appendf("_%s", geomtypename(geomType)); |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 199 | } |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 200 | |
commit-bot@chromium.org | 3400f4b | 2013-09-30 19:41:09 +0000 | [diff] [blame] | 201 | if (scale != 1.f) { |
| 202 | fName.appendf("_scale_%g", scale); |
| 203 | } |
| 204 | |
commit-bot@chromium.org | 5396a04 | 2013-07-10 16:13:14 +0000 | [diff] [blame] | 205 | fName.append(data.fName); |
| 206 | |
fmalita | 36693b5 | 2016-02-24 15:32:57 -0800 | [diff] [blame] | 207 | this->setupPaint(&fPaint); |
Florin Malita | d1aedde | 2017-06-07 15:03:38 -0400 | [diff] [blame] | 208 | fPaint.setShader(MakeShader(gradType, data, tm, scale)); |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Florin Malita | d1aedde | 2017-06-07 15:03:38 -0400 | [diff] [blame] | 211 | GradientBench(GradType gradType, GradData data, bool dither) |
fmalita | 36693b5 | 2016-02-24 15:32:57 -0800 | [diff] [blame] | 212 | : fGeomType(kRect_GeomType) { |
| 213 | |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 214 | const char *tmname = tilemodename(SkShader::kClamp_TileMode); |
| 215 | fName.printf("gradient_%s_%s", gGrads[gradType].fName, tmname); |
| 216 | fName.append(data.fName); |
| 217 | |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 218 | if (dither) { |
| 219 | fName.appendf("_dither"); |
| 220 | } |
| 221 | |
fmalita | 36693b5 | 2016-02-24 15:32:57 -0800 | [diff] [blame] | 222 | this->setupPaint(&fPaint); |
Florin Malita | d1aedde | 2017-06-07 15:03:38 -0400 | [diff] [blame] | 223 | fPaint.setShader(MakeShader(gradType, data, SkShader::kClamp_TileMode, 1.0f)); |
fmalita | 36693b5 | 2016-02-24 15:32:57 -0800 | [diff] [blame] | 224 | fPaint.setDither(dither); |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | protected: |
fmalita | 36693b5 | 2016-02-24 15:32:57 -0800 | [diff] [blame] | 228 | const char* onGetName() override { |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 229 | return fName.c_str(); |
| 230 | } |
| 231 | |
fmalita | 36693b5 | 2016-02-24 15:32:57 -0800 | [diff] [blame] | 232 | SkIPoint onGetSize() override { |
| 233 | return SkIPoint::Make(kSize, kSize); |
| 234 | } |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 235 | |
fmalita | 36693b5 | 2016-02-24 15:32:57 -0800 | [diff] [blame] | 236 | void onDraw(int loops, SkCanvas* canvas) override { |
| 237 | const SkRect r = SkRect::MakeIWH(kSize, kSize); |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 238 | |
bsalomon | a7f79b4 | 2014-08-14 12:24:46 -0700 | [diff] [blame] | 239 | for (int i = 0; i < loops; i++) { |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 240 | switch (fGeomType) { |
| 241 | case kRect_GeomType: |
fmalita | 36693b5 | 2016-02-24 15:32:57 -0800 | [diff] [blame] | 242 | canvas->drawRect(r, fPaint); |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 243 | break; |
| 244 | case kOval_GeomType: |
fmalita | 36693b5 | 2016-02-24 15:32:57 -0800 | [diff] [blame] | 245 | canvas->drawOval(r, fPaint); |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 246 | break; |
| 247 | } |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
| 251 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 252 | typedef Benchmark INHERITED; |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 253 | |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 254 | sk_sp<SkShader> MakeShader(GradType gradType, GradData data, |
Florin Malita | d1aedde | 2017-06-07 15:03:38 -0400 | [diff] [blame] | 255 | SkShader::TileMode tm, float scale) { |
fmalita | 36693b5 | 2016-02-24 15:32:57 -0800 | [diff] [blame] | 256 | const SkPoint pts[2] = { |
| 257 | { 0, 0 }, |
| 258 | { SkIntToScalar(kSize), SkIntToScalar(kSize) } |
| 259 | }; |
| 260 | |
Florin Malita | d1aedde | 2017-06-07 15:03:38 -0400 | [diff] [blame] | 261 | return gGrads[gradType].fMaker(pts, data, tm, scale); |
fmalita | 36693b5 | 2016-02-24 15:32:57 -0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | static const int kSize = 400; |
| 265 | |
| 266 | SkString fName; |
| 267 | SkPaint fPaint; |
| 268 | const GeomType fGeomType; |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 269 | }; |
| 270 | |
fmalita | 36693b5 | 2016-02-24 15:32:57 -0800 | [diff] [blame] | 271 | DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[0]); ) |
commit-bot@chromium.org | b120bd9 | 2013-10-07 17:18:21 +0000 | [diff] [blame] | 272 | DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[1]); ) |
| 273 | DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[2]); ) |
Herb Derby | 4de1304 | 2017-05-15 10:49:39 -0400 | [diff] [blame] | 274 | DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[4]); ) |
fmalita | 36693b5 | 2016-02-24 15:32:57 -0800 | [diff] [blame] | 275 | DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[0], SkShader::kRepeat_TileMode); ) |
| 276 | DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[1], SkShader::kRepeat_TileMode); ) |
| 277 | DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[2], SkShader::kRepeat_TileMode); ) |
commit-bot@chromium.org | b120bd9 | 2013-10-07 17:18:21 +0000 | [diff] [blame] | 278 | DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[0], SkShader::kMirror_TileMode); ) |
fmalita | 36693b5 | 2016-02-24 15:32:57 -0800 | [diff] [blame] | 279 | DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[1], SkShader::kMirror_TileMode); ) |
| 280 | DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[2], SkShader::kMirror_TileMode); ) |
commit-bot@chromium.org | b120bd9 | 2013-10-07 17:18:21 +0000 | [diff] [blame] | 281 | |
| 282 | DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[0]); ) |
| 283 | DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[1]); ) |
| 284 | DEF_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 |
| 287 | DEF_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) |
| 291 | DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[0], SkShader::kClamp_TileMode, kOval_GeomType); ) |
| 292 | |
| 293 | DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[0], SkShader::kMirror_TileMode); ) |
commit-bot@chromium.org | 34150b4 | 2013-10-16 18:59:44 +0000 | [diff] [blame] | 294 | DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[0], SkShader::kRepeat_TileMode); ) |
commit-bot@chromium.org | b120bd9 | 2013-10-07 17:18:21 +0000 | [diff] [blame] | 295 | DEF_BENCH( return new GradientBench(kSweep_GradType); ) |
| 296 | DEF_BENCH( return new GradientBench(kSweep_GradType, gGradData[1]); ) |
| 297 | DEF_BENCH( return new GradientBench(kSweep_GradType, gGradData[2]); ) |
commit-bot@chromium.org | b120bd9 | 2013-10-07 17:18:21 +0000 | [diff] [blame] | 298 | DEF_BENCH( return new GradientBench(kConical_GradType); ) |
| 299 | DEF_BENCH( return new GradientBench(kConical_GradType, gGradData[1]); ) |
| 300 | DEF_BENCH( return new GradientBench(kConical_GradType, gGradData[2]); ) |
commit-bot@chromium.org | 95f7926 | 2014-04-14 16:02:31 +0000 | [diff] [blame] | 301 | DEF_BENCH( return new GradientBench(kConicalZero_GradType); ) |
| 302 | DEF_BENCH( return new GradientBench(kConicalZero_GradType, gGradData[1]); ) |
| 303 | DEF_BENCH( return new GradientBench(kConicalZero_GradType, gGradData[2]); ) |
| 304 | DEF_BENCH( return new GradientBench(kConicalOut_GradType); ) |
| 305 | DEF_BENCH( return new GradientBench(kConicalOut_GradType, gGradData[1]); ) |
| 306 | DEF_BENCH( return new GradientBench(kConicalOut_GradType, gGradData[2]); ) |
| 307 | DEF_BENCH( return new GradientBench(kConicalOutZero_GradType); ) |
| 308 | DEF_BENCH( return new GradientBench(kConicalOutZero_GradType, gGradData[1]); ) |
| 309 | DEF_BENCH( return new GradientBench(kConicalOutZero_GradType, gGradData[2]); ) |
commit-bot@chromium.org | b120bd9 | 2013-10-07 17:18:21 +0000 | [diff] [blame] | 310 | |
krajcevski | f461a8f | 2014-06-19 14:14:06 -0700 | [diff] [blame] | 311 | // Dithering |
| 312 | DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[3], true); ) |
| 313 | DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[3], false); ) |
| 314 | DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[3], true); ) |
| 315 | DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[3], false); ) |
| 316 | DEF_BENCH( return new GradientBench(kSweep_GradType, gGradData[3], true); ) |
| 317 | DEF_BENCH( return new GradientBench(kSweep_GradType, gGradData[3], false); ) |
| 318 | DEF_BENCH( return new GradientBench(kConical_GradType, gGradData[3], true); ) |
| 319 | DEF_BENCH( return new GradientBench(kConical_GradType, gGradData[3], false); ) |
| 320 | |
commit-bot@chromium.org | b120bd9 | 2013-10-07 17:18:21 +0000 | [diff] [blame] | 321 | /////////////////////////////////////////////////////////////////////////////// |
| 322 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 323 | class Gradient2Bench : public Benchmark { |
reed@google.com | 73349aa | 2013-02-04 18:09:58 +0000 | [diff] [blame] | 324 | SkString fName; |
| 325 | bool fHasAlpha; |
| 326 | |
reed@google.com | 8ac7a0f | 2011-06-16 13:14:19 +0000 | [diff] [blame] | 327 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 328 | Gradient2Bench(bool hasAlpha) { |
reed@google.com | 73349aa | 2013-02-04 18:09:58 +0000 | [diff] [blame] | 329 | fName.printf("gradient_create_%s", hasAlpha ? "alpha" : "opaque"); |
| 330 | fHasAlpha = hasAlpha; |
| 331 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 332 | |
reed@google.com | 8ac7a0f | 2011-06-16 13:14:19 +0000 | [diff] [blame] | 333 | protected: |
| 334 | virtual const char* onGetName() { |
reed@google.com | 73349aa | 2013-02-04 18:09:58 +0000 | [diff] [blame] | 335 | return fName.c_str(); |
reed@google.com | 8ac7a0f | 2011-06-16 13:14:19 +0000 | [diff] [blame] | 336 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 337 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 338 | virtual void onDraw(int loops, SkCanvas* canvas) { |
reed@google.com | 8ac7a0f | 2011-06-16 13:14:19 +0000 | [diff] [blame] | 339 | SkPaint paint; |
| 340 | this->setupPaint(&paint); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 341 | |
reed@google.com | 8ac7a0f | 2011-06-16 13:14:19 +0000 | [diff] [blame] | 342 | const SkRect r = { 0, 0, SkIntToScalar(4), SkIntToScalar(4) }; |
reed@google.com | 1ca4f26 | 2011-06-24 19:17:37 +0000 | [diff] [blame] | 343 | const SkPoint pts[] = { |
| 344 | { 0, 0 }, |
| 345 | { SkIntToScalar(100), SkIntToScalar(100) }, |
| 346 | }; |
reed@google.com | 8ac7a0f | 2011-06-16 13:14:19 +0000 | [diff] [blame] | 347 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 348 | for (int i = 0; i < loops; i++) { |
reed@google.com | 73349aa | 2013-02-04 18:09:58 +0000 | [diff] [blame] | 349 | const int gray = i % 256; |
| 350 | const int alpha = fHasAlpha ? gray : 0xFF; |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 351 | SkColor colors[] = { |
| 352 | SK_ColorBLACK, |
reed@google.com | 73349aa | 2013-02-04 18:09:58 +0000 | [diff] [blame] | 353 | SkColorSetARGB(alpha, gray, gray, gray), |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 354 | SK_ColorWHITE }; |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 355 | paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, |
reed@google.com | 8ac7a0f | 2011-06-16 13:14:19 +0000 | [diff] [blame] | 356 | SK_ARRAY_COUNT(colors), |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 357 | SkShader::kClamp_TileMode)); |
reed@google.com | 8ac7a0f | 2011-06-16 13:14:19 +0000 | [diff] [blame] | 358 | canvas->drawRect(r, paint); |
| 359 | } |
| 360 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 361 | |
reed@google.com | 8ac7a0f | 2011-06-16 13:14:19 +0000 | [diff] [blame] | 362 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 363 | typedef Benchmark INHERITED; |
reed@google.com | 8ac7a0f | 2011-06-16 13:14:19 +0000 | [diff] [blame] | 364 | }; |
| 365 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 366 | DEF_BENCH( return new Gradient2Bench(false); ) |
| 367 | DEF_BENCH( return new Gradient2Bench(true); ) |