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