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