| 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 |
*/ |
| 7 |
#include "bench/Benchmark.h" |
| 8 |
#include "include/core/SkBitmap.h" |
| 9 |
#include "include/core/SkCanvas.h" |
| 10 |
#include "include/core/SkPaint.h" |
| 11 |
#include "include/core/SkShader.h" |
| 12 |
#include "include/core/SkString.h" |
| 13 |
#include "include/effects/SkGradient.h" |
| 14 |
#include "include/private/base/SkFloatingPoint.h" |
| 15 |
#include "tools/ToolUtils.h" |
| 16 |
|
| 17 |
struct GradData { |
| 18 |
size_t fCount; |
| 19 |
const SkColor4f* fColors; |
| 20 |
const float* fPos; |
| 21 |
const char* fName; |
| 22 |
|
| 23 |
SkGradient grad(SkTileMode tm) const { |
| 24 |
SkSpan<const float> pos; |
| 25 |
if (fPos) { |
| 26 |
pos = {fPos, fCount}; |
| 27 |
} |
| 28 |
return {{{fColors, fCount}, pos, tm}, {}}; |
| 29 |
} |
| 30 |
}; |
| 31 |
|
| 32 |
static const SkColor4f gColors[] = { |
| 33 |
SkColors::kRed, SkColors::kGreen, SkColors::kBlue, SkColors::kWhite, SkColors::kBlack, |
| 34 |
SkColors::kRed, SkColors::kGreen, SkColors::kBlue, SkColors::kWhite, SkColors::kBlack, |
| 35 |
SkColors::kRed, SkColors::kGreen, SkColors::kBlue, SkColors::kWhite, SkColors::kBlack, |
| 36 |
SkColors::kRed, SkColors::kGreen, SkColors::kBlue, SkColors::kWhite, SkColors::kBlack, |
| 37 |
SkColors::kRed, SkColors::kGreen, SkColors::kBlue, SkColors::kWhite, SkColors::kBlack, |
| 38 |
SkColors::kRed, SkColors::kGreen, SkColors::kBlue, SkColors::kWhite, SkColors::kBlack, |
| 39 |
SkColors::kRed, SkColors::kGreen, SkColors::kBlue, SkColors::kWhite, SkColors::kBlack, |
| 40 |
SkColors::kRed, SkColors::kGreen, SkColors::kBlue, SkColors::kWhite, SkColors::kBlack, |
| 41 |
SkColors::kRed, SkColors::kGreen, SkColors::kBlue, SkColors::kWhite, SkColors::kBlack, |
| 42 |
SkColors::kRed, SkColors::kGreen, SkColors::kBlue, SkColors::kWhite, SkColors::kBlack, |
| 43 |
}; |
| 44 |
|
| 45 |
const SkColor4f gShallowColors[] = { |
| 46 |
SkColor4f::FromColor(0xFF555555), SkColor4f::FromColor(0xFF444444) }; |
| 47 |
static const float gPos[] = {0.25f, 0.75f}; |
| 48 |
|
| 49 |
// We have several special-cases depending on the number (and spacing) of colors, so |
| 50 |
// try to exercise those here. |
| 51 |
static const GradData gGradData[] = { |
| 52 |
{ 2, gColors, nullptr, "" }, |
| 53 |
{ 50, gColors, nullptr, "_hicolor" }, // many color gradient |
| 54 |
{ 3, gColors, nullptr, "_3color" }, |
| 55 |
{ 2, gShallowColors, nullptr, "_shallow" }, |
| 56 |
{ 2, gColors, gPos, "_pos" }, |
| 57 |
}; |
| 58 |
|
| 59 |
/// Ignores scale |
| 60 |
static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data, |
| 61 |
SkTileMode tm, float scale) { |
| 62 |
return SkShaders::LinearGradient(pts, data.grad(tm)); |
| 63 |
} |
| 64 |
|
| 65 |
static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data, |
| 66 |
SkTileMode tm, float scale) { |
| 67 |
SkPoint center; |
| 68 |
center.set(sk_float_midpoint(pts[0].fX, pts[1].fX), |
| 69 |
sk_float_midpoint(pts[0].fY, pts[1].fY)); |
| 70 |
return SkShaders::RadialGradient(center, center.fX * scale, data.grad(tm)); |
| 71 |
} |
| 72 |
|
| 73 |
/// Ignores scale |
| 74 |
static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data, |
| 75 |
SkTileMode tm, float scale) { |
| 76 |
SkPoint center; |
| 77 |
center.set(sk_float_midpoint(pts[0].fX, pts[1].fX), |
| 78 |
sk_float_midpoint(pts[0].fY, pts[1].fY)); |
| 79 |
return SkShaders::SweepGradient(center, data.grad(tm)); |
| 80 |
} |
| 81 |
|
| 82 |
/// Ignores scale |
| 83 |
static sk_sp<SkShader> MakeConical(const SkPoint pts[2], const GradData& data, |
| 84 |
SkTileMode tm, float scale) { |
| 85 |
SkPoint center0, center1; |
| 86 |
center0.set(sk_float_midpoint(pts[0].fX, pts[1].fX), |
| 87 |
sk_float_midpoint(pts[0].fY, pts[1].fY)); |
| 88 |
center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
| 89 |
SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
| 90 |
return SkShaders::TwoPointConicalGradient(center1, (pts[1].fX - pts[0].fX) / 7, |
| 91 |
center0, (pts[1].fX - pts[0].fX) / 2, |
| 92 |
data.grad(tm)); |
| 93 |
} |
| 94 |
|
| 95 |
/// Ignores scale |
| 96 |
static sk_sp<SkShader> MakeConicalZeroRad(const SkPoint pts[2], const GradData& data, |
| 97 |
SkTileMode tm, float scale) { |
| 98 |
SkPoint center0, center1; |
| 99 |
center0.set(sk_float_midpoint(pts[0].fX, pts[1].fX), |
| 100 |
sk_float_midpoint(pts[0].fY, pts[1].fY)); |
| 101 |
center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
| 102 |
SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
| 103 |
return SkShaders::TwoPointConicalGradient(center1, 0.0, |
| 104 |
center0, (pts[1].fX - pts[0].fX) / 2, |
| 105 |
data.grad(tm)); |
| 106 |
} |
| 107 |
|
| 108 |
/// Ignores scale |
| 109 |
static sk_sp<SkShader> MakeConicalOutside(const SkPoint pts[2], const GradData& data, |
| 110 |
SkTileMode tm, float scale) { |
| 111 |
SkPoint center0, center1; |
| 112 |
SkScalar radius0 = (pts[1].fX - pts[0].fX) / 10; |
| 113 |
SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3; |
| 114 |
center0.set(pts[0].fX + radius0, pts[0].fY + radius0); |
| 115 |
center1.set(pts[1].fX - radius1, pts[1].fY - radius1); |
| 116 |
return SkShaders::TwoPointConicalGradient(center0, radius0, center1, radius1, data.grad(tm)); |
| 117 |
} |
| 118 |
|
| 119 |
/// Ignores scale |
| 120 |
static sk_sp<SkShader> MakeConicalOutsideZeroRad(const SkPoint pts[2], const GradData& data, |
| 121 |
SkTileMode tm, float scale) { |
| 122 |
SkPoint center0, center1; |
| 123 |
SkScalar radius0 = (pts[1].fX - pts[0].fX) / 10; |
| 124 |
SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3; |
| 125 |
center0.set(pts[0].fX + radius0, pts[0].fY + radius0); |
| 126 |
center1.set(pts[1].fX - radius1, pts[1].fY - radius1); |
| 127 |
return SkShaders::TwoPointConicalGradient(center0, 0, center1, radius1, data.grad(tm)); |
| 128 |
} |
| 129 |
|
| 130 |
typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData& data, |
| 131 |
SkTileMode tm, float scale); |
| 132 |
|
| 133 |
static const struct { |
| 134 |
GradMaker fMaker; |
| 135 |
const char* fName; |
| 136 |
} gGrads[] = { |
| 137 |
{ MakeLinear, "linear" }, |
| 138 |
{ MakeRadial, "radial1" }, |
| 139 |
{ MakeSweep, "sweep" }, |
| 140 |
{ MakeConical, "conical" }, |
| 141 |
{ MakeConicalZeroRad, "conicalZero" }, |
| 142 |
{ MakeConicalOutside, "conicalOut" }, |
| 143 |
{ MakeConicalOutsideZeroRad, "conicalOutZero" }, |
| 144 |
}; |
| 145 |
|
| 146 |
enum GradType { // these must match the order in gGrads |
| 147 |
kLinear_GradType, |
| 148 |
kRadial_GradType, |
| 149 |
kSweep_GradType, |
| 150 |
kConical_GradType, |
| 151 |
kConicalZero_GradType, |
| 152 |
kConicalOut_GradType, |
| 153 |
kConicalOutZero_GradType |
| 154 |
}; |
| 155 |
|
| 156 |
enum GeomType { |
| 157 |
kRect_GeomType, |
| 158 |
kOval_GeomType |
| 159 |
}; |
| 160 |
|
| 161 |
static const char* geomtypename(GeomType gt) { |
| 162 |
switch (gt) { |
| 163 |
case kRect_GeomType: |
| 164 |
return "rectangle"; |
| 165 |
case kOval_GeomType: |
| 166 |
return "oval"; |
| 167 |
default: |
| 168 |
SkDEBUGFAIL("unknown geometry type"); |
| 169 |
return "error"; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
/////////////////////////////////////////////////////////////////////////////// |
| 174 |
|
| 175 |
class GradientBench : public Benchmark { |
| 176 |
public: |
| 177 |
GradientBench(GradType gradType, |
| 178 |
GradData data = gGradData[0], |
| 179 |
SkTileMode tm = SkTileMode::kClamp, |
| 180 |
GeomType geomType = kRect_GeomType, |
| 181 |
float scale = 1.0f) |
| 182 |
: fGeomType(geomType) { |
| 183 |
|
| 184 |
fName.printf("gradient_%s_%s", gGrads[gradType].fName, |
| 185 |
ToolUtils::tilemode_name(tm)); |
| 186 |
if (geomType != kRect_GeomType) { |
| 187 |
fName.appendf("_%s", geomtypename(geomType)); |
| 188 |
} |
| 189 |
|
| 190 |
if (scale != 1.f) { |
| 191 |
fName.appendf("_scale_%g", scale); |
| 192 |
} |
| 193 |
|
| 194 |
fName.append(data.fName); |
| 195 |
|
| 196 |
this->setupPaint(&fPaint); |
| 197 |
fPaint.setShader(MakeShader(gradType, data, tm, scale)); |
| 198 |
} |
| 199 |
|
| 200 |
GradientBench(GradType gradType, GradData data, bool dither) |
| 201 |
: fGeomType(kRect_GeomType) { |
| 202 |
|
| 203 |
const char *tmname = ToolUtils::tilemode_name(SkTileMode::kClamp); |
| 204 |
fName.printf("gradient_%s_%s", gGrads[gradType].fName, tmname); |
| 205 |
fName.append(data.fName); |
| 206 |
|
| 207 |
if (dither) { |
| 208 |
fName.appendf("_dither"); |
| 209 |
} |
| 210 |
|
| 211 |
this->setupPaint(&fPaint); |
| 212 |
fPaint.setShader(MakeShader(gradType, data, SkTileMode::kClamp, 1.0f)); |
| 213 |
fPaint.setDither(dither); |
| 214 |
} |
| 215 |
|
| 216 |
protected: |
| 217 |
const char* onGetName() override { |
| 218 |
return fName.c_str(); |
| 219 |
} |
| 220 |
|
| 221 |
SkISize onGetSize() override { |
| 222 |
return SkISize::Make(kSize, kSize); |
| 223 |
} |
| 224 |
|
| 225 |
void onDraw(int loops, SkCanvas* canvas) override { |
| 226 |
const SkRect r = SkRect::MakeIWH(kSize, kSize); |
| 227 |
|
| 228 |
for (int i = 0; i < loops; i++) { |
| 229 |
switch (fGeomType) { |
| 230 |
case kRect_GeomType: |
| 231 |
canvas->drawRect(r, fPaint); |
| 232 |
break; |
| 233 |
case kOval_GeomType: |
| 234 |
canvas->drawOval(r, fPaint); |
| 235 |
break; |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
private: |
| 241 |
using INHERITED = Benchmark; |
| 242 |
|
| 243 |
sk_sp<SkShader> MakeShader(GradType gradType, GradData data, |
| 244 |
SkTileMode tm, float scale) { |
| 245 |
const SkPoint pts[2] = { |
| 246 |
{ 0, 0 }, |
| 247 |
{ SkIntToScalar(kSize), SkIntToScalar(kSize) } |
| 248 |
}; |
| 249 |
|
| 250 |
return gGrads[gradType].fMaker(pts, data, tm, scale); |
| 251 |
} |
| 252 |
|
| 253 |
static const int kSize = 400; |
| 254 |
|
| 255 |
SkString fName; |
| 256 |
SkPaint fPaint; |
| 257 |
const GeomType fGeomType; |
| 258 |
}; |
| 259 |
|
| 260 |
DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[0]); ) |
| 261 |
DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[1]); ) |
| 262 |
DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[2]); ) |
| 263 |
DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[4]); ) |
| 264 |
DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[0], SkTileMode::kRepeat); ) |
| 265 |
DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[1], SkTileMode::kRepeat); ) |
| 266 |
DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[2], SkTileMode::kRepeat); ) |
| 267 |
DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[0], SkTileMode::kMirror); ) |
| 268 |
DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[1], SkTileMode::kMirror); ) |
| 269 |
DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[2], SkTileMode::kMirror); ) |
| 270 |
|
| 271 |
DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[0]); ) |
| 272 |
DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[1]); ) |
| 273 |
DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[2]); ) |
| 274 |
// Draw a radial gradient of radius 1/2 on a rectangle; half the lines should |
| 275 |
// be completely pinned, the other half should pe partially pinned |
| 276 |
DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[0], SkTileMode::kClamp, kRect_GeomType, 0.5f); ) |
| 277 |
|
| 278 |
// Draw a radial gradient on a circle of equal size; all the lines should |
| 279 |
// hit the unpinned fast path (so long as GradientBench.W == H) |
| 280 |
DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[0], SkTileMode::kClamp, kOval_GeomType); ) |
| 281 |
|
| 282 |
DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[0], SkTileMode::kMirror); ) |
| 283 |
DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[0], SkTileMode::kRepeat); ) |
| 284 |
DEF_BENCH( return new GradientBench(kSweep_GradType); ) |
| 285 |
DEF_BENCH( return new GradientBench(kSweep_GradType, gGradData[1]); ) |
| 286 |
DEF_BENCH( return new GradientBench(kSweep_GradType, gGradData[2]); ) |
| 287 |
DEF_BENCH( return new GradientBench(kConical_GradType); ) |
| 288 |
DEF_BENCH( return new GradientBench(kConical_GradType, gGradData[1]); ) |
| 289 |
DEF_BENCH( return new GradientBench(kConical_GradType, gGradData[2]); ) |
| 290 |
DEF_BENCH( return new GradientBench(kConicalZero_GradType); ) |
| 291 |
DEF_BENCH( return new GradientBench(kConicalZero_GradType, gGradData[1]); ) |
| 292 |
DEF_BENCH( return new GradientBench(kConicalZero_GradType, gGradData[2]); ) |
| 293 |
DEF_BENCH( return new GradientBench(kConicalOut_GradType); ) |
| 294 |
DEF_BENCH( return new GradientBench(kConicalOut_GradType, gGradData[1]); ) |
| 295 |
DEF_BENCH( return new GradientBench(kConicalOut_GradType, gGradData[2]); ) |
| 296 |
DEF_BENCH( return new GradientBench(kConicalOutZero_GradType); ) |
| 297 |
DEF_BENCH( return new GradientBench(kConicalOutZero_GradType, gGradData[1]); ) |
| 298 |
DEF_BENCH( return new GradientBench(kConicalOutZero_GradType, gGradData[2]); ) |
| 299 |
|
| 300 |
// Dithering |
| 301 |
DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[3], true); ) |
| 302 |
DEF_BENCH( return new GradientBench(kLinear_GradType, gGradData[3], false); ) |
| 303 |
DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[3], true); ) |
| 304 |
DEF_BENCH( return new GradientBench(kRadial_GradType, gGradData[3], false); ) |
| 305 |
DEF_BENCH( return new GradientBench(kSweep_GradType, gGradData[3], true); ) |
| 306 |
DEF_BENCH( return new GradientBench(kSweep_GradType, gGradData[3], false); ) |
| 307 |
DEF_BENCH( return new GradientBench(kConical_GradType, gGradData[3], true); ) |
| 308 |
DEF_BENCH( return new GradientBench(kConical_GradType, gGradData[3], false); ) |
| 309 |
|
| 310 |
/////////////////////////////////////////////////////////////////////////////// |
| 311 |
|
| 312 |
class Gradient2Bench : public Benchmark { |
| 313 |
SkString fName; |
| 314 |
bool fHasAlpha; |
| 315 |
|
| 316 |
public: |
| 317 |
Gradient2Bench(bool hasAlpha) { |
| 318 |
fName.printf("gradient_create_%s", hasAlpha ? "alpha" : "opaque"); |
| 319 |
fHasAlpha = hasAlpha; |
| 320 |
} |
| 321 |
|
| 322 |
protected: |
| 323 |
const char* onGetName() override { |
| 324 |
return fName.c_str(); |
| 325 |
} |
| 326 |
|
| 327 |
void onDraw(int loops, SkCanvas* canvas) override { |
| 328 |
SkPaint paint; |
| 329 |
this->setupPaint(&paint); |
| 330 |
|
| 331 |
const SkRect r = { 0, 0, SkIntToScalar(4), SkIntToScalar(4) }; |
| 332 |
const SkPoint pts[] = { |
| 333 |
{ 0, 0 }, |
| 334 |
{ SkIntToScalar(100), SkIntToScalar(100) }, |
| 335 |
}; |
| 336 |
|
| 337 |
for (int i = 0; i < loops; i++) { |
| 338 |
const int gray = i % 256; |
| 339 |
const int alpha = fHasAlpha ? gray : 0xFF; |
| 340 |
const SkColor4f colors[] = { |
| 341 |
SkColors::kBlack, |
| 342 |
SkColor4f::FromColor(SkColorSetARGB(alpha, gray, gray, gray)), |
| 343 |
SkColors::kWhite }; |
| 344 |
paint.setShader(SkShaders::LinearGradient(pts, {{colors, {}, SkTileMode::kClamp}, {}})); |
| 345 |
canvas->drawRect(r, paint); |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
private: |
| 350 |
using INHERITED = Benchmark; |
| 351 |
}; |
| 352 |
|
| 353 |
DEF_BENCH( return new Gradient2Bench(false); ) |
| 354 |
DEF_BENCH( return new Gradient2Bench(true); ) |