bsalomon | 3342ed5 | 2014-07-30 08:58:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | |
| 8 | #include "Benchmark.h" |
Mike Reed | d470673 | 2016-11-15 16:44:34 -0500 | [diff] [blame] | 9 | #include "SkBlendModePriv.h" |
bsalomon | 3342ed5 | 2014-07-30 08:58:20 -0700 | [diff] [blame] | 10 | #include "SkCanvas.h" |
Michael Ludwig | dde8b55 | 2018-11-20 14:58:23 -0500 | [diff] [blame] | 11 | #include "SkGradientShader.h" |
bsalomon | 3342ed5 | 2014-07-30 08:58:20 -0700 | [diff] [blame] | 12 | #include "SkPaint.h" |
| 13 | |
bsalomon | 6563562 | 2014-08-08 07:43:29 -0700 | [diff] [blame] | 14 | #include <ctype.h> |
| 15 | |
bsalomon | 3342ed5 | 2014-07-30 08:58:20 -0700 | [diff] [blame] | 16 | /** This benchmark tests rendering rotated rectangles. It can optionally apply AA and/or change the |
bsalomon | 6563562 | 2014-08-08 07:43:29 -0700 | [diff] [blame] | 17 | paint color between each rect in different ways using the ColorType enum. The xfermode used can |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 18 | be specified as well. |
bsalomon | 6563562 | 2014-08-08 07:43:29 -0700 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | enum ColorType { |
| 22 | kConstantOpaque_ColorType, |
| 23 | kConstantTransparent_ColorType, |
| 24 | kChangingOpaque_ColorType, |
| 25 | kChangingTransparent_ColorType, |
| 26 | kAlternatingOpaqueAndTransparent_ColorType, |
Michael Ludwig | dde8b55 | 2018-11-20 14:58:23 -0500 | [diff] [blame] | 27 | kShaderOpaque_ColorType |
bsalomon | 6563562 | 2014-08-08 07:43:29 -0700 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | static inline SkColor start_color(ColorType ct) { |
| 31 | switch (ct) { |
| 32 | case kConstantOpaque_ColorType: |
| 33 | case kChangingOpaque_ColorType: |
| 34 | case kAlternatingOpaqueAndTransparent_ColorType: |
| 35 | return 0xFFA07040; |
| 36 | case kConstantTransparent_ColorType: |
| 37 | case kChangingTransparent_ColorType: |
| 38 | return 0x80A07040; |
Michael Ludwig | dde8b55 | 2018-11-20 14:58:23 -0500 | [diff] [blame] | 39 | case kShaderOpaque_ColorType: |
| 40 | return SK_ColorWHITE; |
bsalomon | 6563562 | 2014-08-08 07:43:29 -0700 | [diff] [blame] | 41 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 42 | SK_ABORT("Shouldn't reach here."); |
bsalomon | 6563562 | 2014-08-08 07:43:29 -0700 | [diff] [blame] | 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | static inline SkColor advance_color(SkColor old, ColorType ct, int step) { |
| 47 | if (kAlternatingOpaqueAndTransparent_ColorType == ct) { |
| 48 | ct = (step & 0x1) ? kChangingOpaque_ColorType : kChangingTransparent_ColorType ; |
| 49 | } |
| 50 | switch (ct) { |
| 51 | case kConstantOpaque_ColorType: |
| 52 | case kConstantTransparent_ColorType: |
Michael Ludwig | dde8b55 | 2018-11-20 14:58:23 -0500 | [diff] [blame] | 53 | case kShaderOpaque_ColorType: |
bsalomon | 6563562 | 2014-08-08 07:43:29 -0700 | [diff] [blame] | 54 | return old; |
| 55 | case kChangingOpaque_ColorType: |
| 56 | return 0xFF000000 | (old + 0x00010307); |
| 57 | case kChangingTransparent_ColorType: |
| 58 | return (0x00FFFFFF & (old + 0x00010307)) | 0x80000000; |
| 59 | case kAlternatingOpaqueAndTransparent_ColorType: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 60 | SK_ABORT("Can't get here"); |
bsalomon | 6563562 | 2014-08-08 07:43:29 -0700 | [diff] [blame] | 61 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 62 | SK_ABORT("Shouldn't reach here."); |
bsalomon | 6563562 | 2014-08-08 07:43:29 -0700 | [diff] [blame] | 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | static SkString to_lower(const char* str) { |
| 67 | SkString lower(str); |
| 68 | for (size_t i = 0; i < lower.size(); i++) { |
| 69 | lower[i] = tolower(lower[i]); |
| 70 | } |
| 71 | return lower; |
| 72 | } |
| 73 | |
bsalomon | 3342ed5 | 2014-07-30 08:58:20 -0700 | [diff] [blame] | 74 | class RotRectBench: public Benchmark { |
| 75 | public: |
Michael Ludwig | dde8b55 | 2018-11-20 14:58:23 -0500 | [diff] [blame] | 76 | RotRectBench(bool aa, ColorType ct, SkBlendMode mode, bool perspective = false) |
bsalomon | 3342ed5 | 2014-07-30 08:58:20 -0700 | [diff] [blame] | 77 | : fAA(aa) |
Michael Ludwig | dde8b55 | 2018-11-20 14:58:23 -0500 | [diff] [blame] | 78 | , fPerspective(perspective) |
bsalomon | 6563562 | 2014-08-08 07:43:29 -0700 | [diff] [blame] | 79 | , fColorType(ct) |
| 80 | , fMode(mode) { |
bsalomon | 3342ed5 | 2014-07-30 08:58:20 -0700 | [diff] [blame] | 81 | this->makeName(); |
| 82 | } |
| 83 | |
| 84 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 85 | const char* onGetName() override { return fName.c_str(); } |
bsalomon | 3342ed5 | 2014-07-30 08:58:20 -0700 | [diff] [blame] | 86 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 87 | void onDraw(int loops, SkCanvas* canvas) override { |
bsalomon | 3342ed5 | 2014-07-30 08:58:20 -0700 | [diff] [blame] | 88 | SkPaint paint; |
| 89 | paint.setAntiAlias(fAA); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 90 | paint.setBlendMode(fMode); |
bsalomon | 6563562 | 2014-08-08 07:43:29 -0700 | [diff] [blame] | 91 | SkColor color = start_color(fColorType); |
bsalomon | 3342ed5 | 2014-07-30 08:58:20 -0700 | [diff] [blame] | 92 | |
tomhudson | c589f6c | 2015-03-23 07:46:13 -0700 | [diff] [blame] | 93 | int w = this->getSize().x(); |
| 94 | int h = this->getSize().y(); |
bsalomon | 3342ed5 | 2014-07-30 08:58:20 -0700 | [diff] [blame] | 95 | |
| 96 | static const SkScalar kRectW = 25.1f; |
| 97 | static const SkScalar kRectH = 25.9f; |
| 98 | |
Michael Ludwig | dde8b55 | 2018-11-20 14:58:23 -0500 | [diff] [blame] | 99 | if (fColorType == kShaderOpaque_ColorType) { |
| 100 | // The only requirement for the shader is that it requires local coordinates |
| 101 | SkPoint pts[2] = { {0.0f, 0.0f}, {kRectW, kRectH} }; |
| 102 | SkColor colors[] = { color, SK_ColorBLUE }; |
| 103 | paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, |
| 104 | SkShader::kClamp_TileMode)); |
| 105 | } |
| 106 | |
bsalomon | 3342ed5 | 2014-07-30 08:58:20 -0700 | [diff] [blame] | 107 | SkMatrix rotate; |
| 108 | // This value was chosen so that we frequently hit the axis-aligned case. |
| 109 | rotate.setRotate(30.f, kRectW / 2, kRectH / 2); |
| 110 | SkMatrix m = rotate; |
| 111 | |
| 112 | SkScalar tx = 0, ty = 0; |
| 113 | |
Michael Ludwig | dde8b55 | 2018-11-20 14:58:23 -0500 | [diff] [blame] | 114 | if (fPerspective) { |
| 115 | // Apply some fixed perspective to change how ops may draw the rects |
| 116 | SkMatrix perspective; |
| 117 | perspective.setIdentity(); |
| 118 | perspective.setPerspX(1e-4f); |
| 119 | perspective.setPerspY(1e-3f); |
| 120 | perspective.setSkewX(0.1f); |
| 121 | canvas->concat(perspective); |
| 122 | } |
| 123 | |
bsalomon | 3342ed5 | 2014-07-30 08:58:20 -0700 | [diff] [blame] | 124 | for (int i = 0; i < loops; ++i) { |
| 125 | canvas->save(); |
| 126 | canvas->translate(tx, ty); |
| 127 | canvas->concat(m); |
| 128 | paint.setColor(color); |
bsalomon | 6563562 | 2014-08-08 07:43:29 -0700 | [diff] [blame] | 129 | color = advance_color(color, fColorType, i); |
| 130 | |
bsalomon | 3342ed5 | 2014-07-30 08:58:20 -0700 | [diff] [blame] | 131 | canvas->drawRect(SkRect::MakeWH(kRectW, kRectH), paint); |
| 132 | canvas->restore(); |
| 133 | |
| 134 | tx += kRectW + 2; |
| 135 | if (tx > w) { |
| 136 | tx = 0; |
| 137 | ty += kRectH + 2; |
| 138 | if (ty > h) { |
| 139 | ty = 0; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | m.postConcat(rotate); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | private: |
| 148 | void makeName() { |
| 149 | fName = "rotated_rects"; |
| 150 | if (fAA) { |
| 151 | fName.append("_aa"); |
| 152 | } else { |
| 153 | fName.append("_bw"); |
| 154 | } |
Michael Ludwig | dde8b55 | 2018-11-20 14:58:23 -0500 | [diff] [blame] | 155 | if (fPerspective) { |
| 156 | fName.append("_persp"); |
| 157 | } |
bsalomon | 6563562 | 2014-08-08 07:43:29 -0700 | [diff] [blame] | 158 | switch (fColorType) { |
| 159 | case kConstantOpaque_ColorType: |
| 160 | fName.append("_same_opaque"); |
| 161 | break; |
| 162 | case kConstantTransparent_ColorType: |
| 163 | fName.append("_same_transparent"); |
| 164 | break; |
| 165 | case kChangingOpaque_ColorType: |
| 166 | fName.append("_changing_opaque"); |
| 167 | break; |
| 168 | case kChangingTransparent_ColorType: |
| 169 | fName.append("_changing_transparent"); |
| 170 | break; |
| 171 | case kAlternatingOpaqueAndTransparent_ColorType: |
| 172 | fName.append("_alternating_transparent_and_opaque"); |
| 173 | break; |
Michael Ludwig | dde8b55 | 2018-11-20 14:58:23 -0500 | [diff] [blame] | 174 | case kShaderOpaque_ColorType: |
| 175 | fName.append("_shader_opaque"); |
| 176 | break; |
bsalomon | 3342ed5 | 2014-07-30 08:58:20 -0700 | [diff] [blame] | 177 | } |
Mike Reed | d470673 | 2016-11-15 16:44:34 -0500 | [diff] [blame] | 178 | fName.appendf("_%s", to_lower(SkBlendMode_Name(fMode)).c_str()); |
bsalomon | 3342ed5 | 2014-07-30 08:58:20 -0700 | [diff] [blame] | 179 | } |
| 180 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 181 | bool fAA; |
Michael Ludwig | dde8b55 | 2018-11-20 14:58:23 -0500 | [diff] [blame] | 182 | bool fPerspective; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 183 | ColorType fColorType; |
| 184 | SkBlendMode fMode; |
| 185 | SkString fName; |
bsalomon | 3342ed5 | 2014-07-30 08:58:20 -0700 | [diff] [blame] | 186 | |
| 187 | typedef Benchmark INHERITED; |
| 188 | }; |
| 189 | |
Michael Ludwig | dde8b55 | 2018-11-20 14:58:23 -0500 | [diff] [blame] | 190 | #define DEF_FOR_COLOR_TYPES(aa, blend) \ |
| 191 | DEF_BENCH(return new RotRectBench(aa, kConstantOpaque_ColorType, blend);) \ |
| 192 | DEF_BENCH(return new RotRectBench(aa, kConstantTransparent_ColorType, blend);) \ |
| 193 | DEF_BENCH(return new RotRectBench(aa, kChangingOpaque_ColorType, blend);) \ |
| 194 | DEF_BENCH(return new RotRectBench(aa, kChangingTransparent_ColorType, blend);) \ |
| 195 | DEF_BENCH(return new RotRectBench(aa, kAlternatingOpaqueAndTransparent_ColorType, blend);) \ |
| 196 | DEF_BENCH(return new RotRectBench(aa, kShaderOpaque_ColorType, blend);) |
| 197 | #define DEF_FOR_AA_MODES(blend) \ |
| 198 | DEF_FOR_COLOR_TYPES(true, blend) \ |
| 199 | DEF_FOR_COLOR_TYPES(false, blend) |
| 200 | |
bsalomon | 6563562 | 2014-08-08 07:43:29 -0700 | [diff] [blame] | 201 | // Choose kSrcOver because it always allows coverage and alpha to be conflated. kSrc only allows |
| 202 | // conflation when opaque, and kDarken because it isn't possilbe with standard GL blending. |
Michael Ludwig | dde8b55 | 2018-11-20 14:58:23 -0500 | [diff] [blame] | 203 | DEF_FOR_AA_MODES(SkBlendMode::kSrcOver) |
| 204 | DEF_FOR_AA_MODES(SkBlendMode::kSrc) |
| 205 | DEF_FOR_AA_MODES(SkBlendMode::kDarken) |
bsalomon | 6563562 | 2014-08-08 07:43:29 -0700 | [diff] [blame] | 206 | |
Michael Ludwig | dde8b55 | 2018-11-20 14:58:23 -0500 | [diff] [blame] | 207 | // Only do a limited run of perspective tests |
| 208 | #define DEF_FOR_PERSP_MODES(aa) \ |
| 209 | DEF_BENCH(return new RotRectBench(aa, kConstantOpaque_ColorType, SkBlendMode::kSrcOver, true);)\ |
| 210 | DEF_BENCH(return new RotRectBench(aa, kShaderOpaque_ColorType, SkBlendMode::kSrcOver, true);) |
| 211 | DEF_FOR_PERSP_MODES(true) |
| 212 | DEF_FOR_PERSP_MODES(false) |