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