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 | */ |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 8 | #include "SkBenchmark.h" |
| 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" |
| 16 | #include "SkUnitMapper.h" |
| 17 | |
| 18 | struct GradData { |
| 19 | int fCount; |
| 20 | const SkColor* fColors; |
| 21 | const SkScalar* fPos; |
| 22 | }; |
| 23 | |
| 24 | static const SkColor gColors[] = { |
| 25 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK |
| 26 | }; |
| 27 | static const SkScalar gPos0[] = { 0, SK_Scalar1 }; |
| 28 | static const SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 }; |
| 29 | static const SkScalar gPos2[] = { |
| 30 | 0, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1 |
| 31 | }; |
| 32 | |
| 33 | static const GradData gGradData[] = { |
| 34 | { 2, gColors, NULL }, |
| 35 | { 2, gColors, gPos0 }, |
| 36 | { 2, gColors, gPos1 }, |
| 37 | { 5, gColors, NULL }, |
| 38 | { 5, gColors, gPos2 } |
| 39 | }; |
| 40 | |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 41 | /// Ignores scale |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 42 | static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 43 | SkShader::TileMode tm, SkUnitMapper* mapper, |
| 44 | float scale) { |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 45 | return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, |
| 46 | data.fCount, tm, mapper); |
| 47 | } |
| 48 | |
| 49 | static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 50 | SkShader::TileMode tm, SkUnitMapper* mapper, |
| 51 | float scale) { |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 52 | SkPoint center; |
| 53 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 54 | SkScalarAve(pts[0].fY, pts[1].fY)); |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 55 | return SkGradientShader::CreateRadial(center, center.fX * scale, |
| 56 | data.fColors, |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 57 | data.fPos, data.fCount, tm, mapper); |
| 58 | } |
| 59 | |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 60 | /// Ignores scale |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 61 | static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 62 | SkShader::TileMode tm, SkUnitMapper* mapper, |
| 63 | float scale) { |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 64 | SkPoint center; |
| 65 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 66 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 67 | return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, |
| 68 | data.fPos, data.fCount, mapper); |
| 69 | } |
| 70 | |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 71 | /// Ignores scale |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 72 | static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data, |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 73 | SkShader::TileMode tm, SkUnitMapper* mapper, |
| 74 | float scale) { |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 75 | SkPoint center0, center1; |
| 76 | center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 77 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 78 | center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
| 79 | SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
| 80 | return SkGradientShader::CreateTwoPointRadial( |
reed@google.com | 258e0bf | 2012-06-07 14:18:34 +0000 | [diff] [blame] | 81 | 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, mapper); |
| 84 | } |
| 85 | |
| 86 | /// Ignores scale |
| 87 | static SkShader* MakeConical(const SkPoint pts[2], const GradData& data, |
| 88 | SkShader::TileMode tm, SkUnitMapper* mapper, |
| 89 | float scale) { |
| 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)); |
| 95 | return SkGradientShader::CreateTwoPointConical(center1, (pts[1].fX - pts[0].fX) / 7, |
| 96 | center0, (pts[1].fX - pts[0].fX) / 2, |
| 97 | data.fColors, data.fPos, data.fCount, tm, mapper); |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 101 | SkShader::TileMode tm, SkUnitMapper* mapper, |
| 102 | float scale); |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 103 | |
| 104 | static const struct { |
| 105 | GradMaker fMaker; |
| 106 | const char* fName; |
| 107 | int fRepeat; |
| 108 | } gGrads[] = { |
| 109 | { MakeLinear, "linear", 15 }, |
reed@google.com | 80adceb | 2011-04-13 18:32:19 +0000 | [diff] [blame] | 110 | { MakeRadial, "radial1", 10 }, |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 111 | { MakeSweep, "sweep", 1 }, |
| 112 | { Make2Radial, "radial2", 5 }, |
reed@google.com | 258e0bf | 2012-06-07 14:18:34 +0000 | [diff] [blame] | 113 | { MakeConical, "conical", 5 }, |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | enum GradType { // these must match the order in gGrads |
| 117 | kLinear_GradType, |
| 118 | kRadial_GradType, |
| 119 | kSweep_GradType, |
reed@google.com | 258e0bf | 2012-06-07 14:18:34 +0000 | [diff] [blame] | 120 | kRadial2_GradType, |
| 121 | kConical_GradType |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 124 | enum GeomType { |
| 125 | kRect_GeomType, |
| 126 | kOval_GeomType |
| 127 | }; |
| 128 | |
reed@google.com | 7241516 | 2011-06-15 21:17:37 +0000 | [diff] [blame] | 129 | static const char* tilemodename(SkShader::TileMode tm) { |
| 130 | switch (tm) { |
| 131 | case SkShader::kClamp_TileMode: |
| 132 | return "clamp"; |
| 133 | case SkShader::kRepeat_TileMode: |
| 134 | return "repeat"; |
| 135 | case SkShader::kMirror_TileMode: |
| 136 | return "mirror"; |
| 137 | default: |
| 138 | SkASSERT(!"unknown tilemode"); |
| 139 | return "error"; |
| 140 | } |
| 141 | } |
| 142 | |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 143 | static const char* geomtypename(GeomType gt) { |
| 144 | switch (gt) { |
| 145 | case kRect_GeomType: |
| 146 | return "rectangle"; |
| 147 | case kOval_GeomType: |
| 148 | return "oval"; |
| 149 | default: |
| 150 | SkASSERT(!"unknown geometry type"); |
| 151 | return "error"; |
| 152 | } |
| 153 | } |
| 154 | |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 155 | /////////////////////////////////////////////////////////////////////////////// |
| 156 | |
| 157 | class GradientBench : public SkBenchmark { |
| 158 | SkString fName; |
| 159 | SkShader* fShader; |
| 160 | int fCount; |
| 161 | enum { |
| 162 | W = 400, |
| 163 | H = 400, |
| 164 | N = 1 |
| 165 | }; |
| 166 | public: |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 167 | GradientBench(void* param, GradType gradType, |
| 168 | SkShader::TileMode tm = SkShader::kClamp_TileMode, |
| 169 | GeomType geomType = kRect_GeomType, |
| 170 | float scale = 1.0f) |
| 171 | : INHERITED(param) { |
| 172 | fName.printf("gradient_%s_%s", gGrads[gradType].fName, |
| 173 | tilemodename(tm)); |
| 174 | if (geomType != kRect_GeomType) { |
| 175 | fName.append("_"); |
| 176 | fName.append(geomtypename(geomType)); |
| 177 | } |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 178 | |
| 179 | const SkPoint pts[2] = { |
| 180 | { 0, 0 }, |
| 181 | { SkIntToScalar(W), SkIntToScalar(H) } |
| 182 | }; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 183 | |
tomhudson@google.com | ca529d3 | 2011-10-28 15:34:49 +0000 | [diff] [blame] | 184 | fCount = SkBENCHLOOP(N * gGrads[gradType].fRepeat); |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 185 | fShader = gGrads[gradType].fMaker(pts, gGradData[0], tm, NULL, scale); |
| 186 | fGeomType = geomType; |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | virtual ~GradientBench() { |
| 190 | fShader->unref(); |
| 191 | } |
| 192 | |
| 193 | protected: |
| 194 | virtual const char* onGetName() { |
| 195 | return fName.c_str(); |
| 196 | } |
| 197 | |
| 198 | virtual void onDraw(SkCanvas* canvas) { |
| 199 | SkPaint paint; |
| 200 | this->setupPaint(&paint); |
| 201 | |
| 202 | paint.setShader(fShader); |
| 203 | |
| 204 | SkRect r = { 0, 0, SkIntToScalar(W), SkIntToScalar(H) }; |
| 205 | for (int i = 0; i < fCount; i++) { |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 206 | switch (fGeomType) { |
| 207 | case kRect_GeomType: |
| 208 | canvas->drawRect(r, paint); |
| 209 | break; |
| 210 | case kOval_GeomType: |
| 211 | canvas->drawOval(r, paint); |
| 212 | break; |
| 213 | } |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
| 217 | private: |
| 218 | typedef SkBenchmark INHERITED; |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 219 | |
| 220 | GeomType fGeomType; |
reed@google.com | 84e9c08 | 2011-04-13 17:44:24 +0000 | [diff] [blame] | 221 | }; |
| 222 | |
reed@google.com | 8ac7a0f | 2011-06-16 13:14:19 +0000 | [diff] [blame] | 223 | class Gradient2Bench : public SkBenchmark { |
reed@google.com | 73349aa | 2013-02-04 18:09:58 +0000 | [diff] [blame] | 224 | SkString fName; |
| 225 | bool fHasAlpha; |
| 226 | |
reed@google.com | 8ac7a0f | 2011-06-16 13:14:19 +0000 | [diff] [blame] | 227 | public: |
reed@google.com | 73349aa | 2013-02-04 18:09:58 +0000 | [diff] [blame] | 228 | Gradient2Bench(void* param, bool hasAlpha) : INHERITED(param) { |
| 229 | fName.printf("gradient_create_%s", hasAlpha ? "alpha" : "opaque"); |
| 230 | fHasAlpha = hasAlpha; |
| 231 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 232 | |
reed@google.com | 8ac7a0f | 2011-06-16 13:14:19 +0000 | [diff] [blame] | 233 | protected: |
| 234 | virtual const char* onGetName() { |
reed@google.com | 73349aa | 2013-02-04 18:09:58 +0000 | [diff] [blame] | 235 | return fName.c_str(); |
reed@google.com | 8ac7a0f | 2011-06-16 13:14:19 +0000 | [diff] [blame] | 236 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 237 | |
reed@google.com | 8ac7a0f | 2011-06-16 13:14:19 +0000 | [diff] [blame] | 238 | virtual void onDraw(SkCanvas* canvas) { |
| 239 | SkPaint paint; |
| 240 | this->setupPaint(&paint); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 241 | |
reed@google.com | 8ac7a0f | 2011-06-16 13:14:19 +0000 | [diff] [blame] | 242 | const SkRect r = { 0, 0, SkIntToScalar(4), SkIntToScalar(4) }; |
reed@google.com | 1ca4f26 | 2011-06-24 19:17:37 +0000 | [diff] [blame] | 243 | const SkPoint pts[] = { |
| 244 | { 0, 0 }, |
| 245 | { SkIntToScalar(100), SkIntToScalar(100) }, |
| 246 | }; |
reed@google.com | 8ac7a0f | 2011-06-16 13:14:19 +0000 | [diff] [blame] | 247 | |
tomhudson@google.com | ca529d3 | 2011-10-28 15:34:49 +0000 | [diff] [blame] | 248 | for (int i = 0; i < SkBENCHLOOP(1000); i++) { |
reed@google.com | 73349aa | 2013-02-04 18:09:58 +0000 | [diff] [blame] | 249 | const int gray = i % 256; |
| 250 | const int alpha = fHasAlpha ? gray : 0xFF; |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 251 | SkColor colors[] = { |
| 252 | SK_ColorBLACK, |
reed@google.com | 73349aa | 2013-02-04 18:09:58 +0000 | [diff] [blame] | 253 | SkColorSetARGB(alpha, gray, gray, gray), |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 254 | SK_ColorWHITE }; |
reed@google.com | 8ac7a0f | 2011-06-16 13:14:19 +0000 | [diff] [blame] | 255 | SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, |
| 256 | SK_ARRAY_COUNT(colors), |
| 257 | SkShader::kClamp_TileMode); |
| 258 | paint.setShader(s)->unref(); |
| 259 | canvas->drawRect(r, paint); |
| 260 | } |
| 261 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 262 | |
reed@google.com | 8ac7a0f | 2011-06-16 13:14:19 +0000 | [diff] [blame] | 263 | private: |
| 264 | typedef SkBenchmark INHERITED; |
| 265 | }; |
| 266 | |
reed@google.com | 58ba410 | 2013-02-04 18:00:54 +0000 | [diff] [blame] | 267 | DEF_BENCH( return new GradientBench(p, kLinear_GradType); ) |
| 268 | DEF_BENCH( return new GradientBench(p, kLinear_GradType, SkShader::kMirror_TileMode); ) |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 269 | |
| 270 | // Draw a radial gradient of radius 1/2 on a rectangle; half the lines should |
| 271 | // be completely pinned, the other half should pe partially pinned |
reed@google.com | 58ba410 | 2013-02-04 18:00:54 +0000 | [diff] [blame] | 272 | DEF_BENCH( return new GradientBench(p, kRadial_GradType, SkShader::kClamp_TileMode, kRect_GeomType, 0.5f); ) |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 273 | |
| 274 | // Draw a radial gradient on a circle of equal size; all the lines should |
| 275 | // hit the unpinned fast path (so long as GradientBench.W == H) |
reed@google.com | 58ba410 | 2013-02-04 18:00:54 +0000 | [diff] [blame] | 276 | DEF_BENCH( return new GradientBench(p, kRadial_GradType, SkShader::kClamp_TileMode, kOval_GeomType); ) |
tomhudson@google.com | 5ea050f | 2011-09-26 15:03:55 +0000 | [diff] [blame] | 277 | |
reed@google.com | 58ba410 | 2013-02-04 18:00:54 +0000 | [diff] [blame] | 278 | DEF_BENCH( return new GradientBench(p, kRadial_GradType, SkShader::kMirror_TileMode); ) |
| 279 | DEF_BENCH( return new GradientBench(p, kSweep_GradType); ) |
| 280 | DEF_BENCH( return new GradientBench(p, kRadial2_GradType); ) |
| 281 | DEF_BENCH( return new GradientBench(p, kRadial2_GradType, SkShader::kMirror_TileMode); ) |
| 282 | DEF_BENCH( return new GradientBench(p, kConical_GradType); ) |
reed@google.com | 8ac7a0f | 2011-06-16 13:14:19 +0000 | [diff] [blame] | 283 | |
reed@google.com | 73349aa | 2013-02-04 18:09:58 +0000 | [diff] [blame] | 284 | DEF_BENCH( return new Gradient2Bench(p, false); ) |
| 285 | DEF_BENCH( return new Gradient2Bench(p, true); ) |