OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "Benchmark.h" | 8 #include "Benchmark.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkPaint.h" | 10 #include "SkPaint.h" |
11 | 11 |
| 12 #include <ctype.h> |
| 13 |
12 /** This benchmark tests rendering rotated rectangles. It can optionally apply A
A and/or change the | 14 /** This benchmark tests rendering rotated rectangles. It can optionally apply A
A and/or change the |
13 paint color between each rect. */ | 15 paint color between each rect in different ways using the ColorType enum. Th
e xfermode used can |
| 16 be specified as well. |
| 17 */ |
| 18 |
| 19 enum ColorType { |
| 20 kConstantOpaque_ColorType, |
| 21 kConstantTransparent_ColorType, |
| 22 kChangingOpaque_ColorType, |
| 23 kChangingTransparent_ColorType, |
| 24 kAlternatingOpaqueAndTransparent_ColorType, |
| 25 }; |
| 26 |
| 27 static inline SkColor start_color(ColorType ct) { |
| 28 switch (ct) { |
| 29 case kConstantOpaque_ColorType: |
| 30 case kChangingOpaque_ColorType: |
| 31 case kAlternatingOpaqueAndTransparent_ColorType: |
| 32 return 0xFFA07040; |
| 33 case kConstantTransparent_ColorType: |
| 34 case kChangingTransparent_ColorType: |
| 35 return 0x80A07040; |
| 36 } |
| 37 SkFAIL("Shouldn't reach here."); |
| 38 return 0; |
| 39 } |
| 40 |
| 41 static inline SkColor advance_color(SkColor old, ColorType ct, int step) { |
| 42 if (kAlternatingOpaqueAndTransparent_ColorType == ct) { |
| 43 ct = (step & 0x1) ? kChangingOpaque_ColorType : kChangingTransparent_Col
orType ; |
| 44 } |
| 45 switch (ct) { |
| 46 case kConstantOpaque_ColorType: |
| 47 case kConstantTransparent_ColorType: |
| 48 return old; |
| 49 case kChangingOpaque_ColorType: |
| 50 return 0xFF000000 | (old + 0x00010307); |
| 51 case kChangingTransparent_ColorType: |
| 52 return (0x00FFFFFF & (old + 0x00010307)) | 0x80000000; |
| 53 case kAlternatingOpaqueAndTransparent_ColorType: |
| 54 SkFAIL("Can't get here"); |
| 55 } |
| 56 SkFAIL("Shouldn't reach here."); |
| 57 return 0; |
| 58 } |
| 59 |
| 60 static SkString to_lower(const char* str) { |
| 61 SkString lower(str); |
| 62 for (size_t i = 0; i < lower.size(); i++) { |
| 63 lower[i] = tolower(lower[i]); |
| 64 } |
| 65 return lower; |
| 66 } |
| 67 |
14 class RotRectBench: public Benchmark { | 68 class RotRectBench: public Benchmark { |
15 public: | 69 public: |
16 RotRectBench(bool aa, bool changeColor) | 70 RotRectBench(bool aa, ColorType ct, SkXfermode::Mode mode) |
17 : fAA(aa) | 71 : fAA(aa) |
18 , fChangeColor(changeColor) { | 72 , fColorType(ct) |
| 73 , fMode(mode) { |
19 this->makeName(); | 74 this->makeName(); |
20 } | 75 } |
21 | 76 |
22 protected: | 77 protected: |
23 virtual const char* onGetName() SK_OVERRIDE { return fName.c_str(); } | 78 virtual const char* onGetName() SK_OVERRIDE { return fName.c_str(); } |
24 | 79 |
25 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { | 80 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
26 SkPaint paint; | 81 SkPaint paint; |
27 paint.setAntiAlias(fAA); | 82 paint.setAntiAlias(fAA); |
28 SkColor color = 0xFF000000; | 83 paint.setXfermodeMode(fMode); |
| 84 SkColor color = start_color(fColorType); |
29 | 85 |
30 int w = canvas->getBaseLayerSize().width(); | 86 int w = canvas->getBaseLayerSize().width(); |
31 int h = canvas->getBaseLayerSize().height(); | 87 int h = canvas->getBaseLayerSize().height(); |
32 | 88 |
33 static const SkScalar kRectW = 25.1f; | 89 static const SkScalar kRectW = 25.1f; |
34 static const SkScalar kRectH = 25.9f; | 90 static const SkScalar kRectH = 25.9f; |
35 | 91 |
36 SkMatrix rotate; | 92 SkMatrix rotate; |
37 // This value was chosen so that we frequently hit the axis-aligned case
. | 93 // This value was chosen so that we frequently hit the axis-aligned case
. |
38 rotate.setRotate(30.f, kRectW / 2, kRectH / 2); | 94 rotate.setRotate(30.f, kRectW / 2, kRectH / 2); |
39 SkMatrix m = rotate; | 95 SkMatrix m = rotate; |
40 | 96 |
41 SkScalar tx = 0, ty = 0; | 97 SkScalar tx = 0, ty = 0; |
42 | 98 |
43 for (int i = 0; i < loops; ++i) { | 99 for (int i = 0; i < loops; ++i) { |
44 canvas->save(); | 100 canvas->save(); |
45 canvas->translate(tx, ty); | 101 canvas->translate(tx, ty); |
46 canvas->concat(m); | 102 canvas->concat(m); |
47 paint.setColor(color); | 103 paint.setColor(color); |
48 if (fChangeColor) { | 104 color = advance_color(color, fColorType, i); |
49 color += 0x010203; | 105 |
50 color |= 0xFF000000; | |
51 } | |
52 canvas->drawRect(SkRect::MakeWH(kRectW, kRectH), paint); | 106 canvas->drawRect(SkRect::MakeWH(kRectW, kRectH), paint); |
53 canvas->restore(); | 107 canvas->restore(); |
54 | 108 |
55 tx += kRectW + 2; | 109 tx += kRectW + 2; |
56 if (tx > w) { | 110 if (tx > w) { |
57 tx = 0; | 111 tx = 0; |
58 ty += kRectH + 2; | 112 ty += kRectH + 2; |
59 if (ty > h) { | 113 if (ty > h) { |
60 ty = 0; | 114 ty = 0; |
61 } | 115 } |
62 } | 116 } |
63 | 117 |
64 m.postConcat(rotate); | 118 m.postConcat(rotate); |
65 } | 119 } |
66 } | 120 } |
67 | 121 |
68 private: | 122 private: |
69 void makeName() { | 123 void makeName() { |
70 fName = "rotated_rects"; | 124 fName = "rotated_rects"; |
71 if (fAA) { | 125 if (fAA) { |
72 fName.append("_aa"); | 126 fName.append("_aa"); |
73 } else { | 127 } else { |
74 fName.append("_bw"); | 128 fName.append("_bw"); |
75 } | 129 } |
76 if (fChangeColor) { | 130 switch (fColorType) { |
77 fName.append("_change_color"); | 131 case kConstantOpaque_ColorType: |
78 } else { | 132 fName.append("_same_opaque"); |
79 fName.append("_same_color"); | 133 break; |
| 134 case kConstantTransparent_ColorType: |
| 135 fName.append("_same_transparent"); |
| 136 break; |
| 137 case kChangingOpaque_ColorType: |
| 138 fName.append("_changing_opaque"); |
| 139 break; |
| 140 case kChangingTransparent_ColorType: |
| 141 fName.append("_changing_transparent"); |
| 142 break; |
| 143 case kAlternatingOpaqueAndTransparent_ColorType: |
| 144 fName.append("_alternating_transparent_and_opaque"); |
| 145 break; |
80 } | 146 } |
| 147 fName.appendf("_%s", to_lower(SkXfermode::ModeName(fMode)).c_str()); |
81 } | 148 } |
82 | 149 |
83 bool fAA; | 150 bool fAA; |
84 bool fChangeColor; | 151 ColorType fColorType; |
85 SkString fName; | 152 SkXfermode::Mode fMode; |
| 153 SkString fName; |
86 | 154 |
87 typedef Benchmark INHERITED; | 155 typedef Benchmark INHERITED; |
88 }; | 156 }; |
89 | 157 |
90 DEF_BENCH(return new RotRectBench(true, true);) | 158 // Choose kSrcOver because it always allows coverage and alpha to be conflated.
kSrc only allows |
91 DEF_BENCH(return new RotRectBench(true, false);) | 159 // conflation when opaque, and kDarken because it isn't possilbe with standard G
L blending. |
92 DEF_BENCH(return new RotRectBench(false, true);) | 160 DEF_BENCH(return new RotRectBench(true, kConstantOpaque_ColorType,
SkXfermode::kSrcOver_Mode);) |
93 DEF_BENCH(return new RotRectBench(false, false);) | 161 DEF_BENCH(return new RotRectBench(true, kConstantTransparent_ColorType,
SkXfermode::kSrcOver_Mode);) |
| 162 DEF_BENCH(return new RotRectBench(true, kChangingOpaque_ColorType,
SkXfermode::kSrcOver_Mode);) |
| 163 DEF_BENCH(return new RotRectBench(true, kChangingTransparent_ColorType,
SkXfermode::kSrcOver_Mode);) |
| 164 DEF_BENCH(return new RotRectBench(true, kAlternatingOpaqueAndTransparent_ColorT
ype, SkXfermode::kSrcOver_Mode);) |
| 165 |
| 166 DEF_BENCH(return new RotRectBench(false, kConstantOpaque_ColorType,
SkXfermode::kSrcOver_Mode);) |
| 167 DEF_BENCH(return new RotRectBench(false, kConstantTransparent_ColorType,
SkXfermode::kSrcOver_Mode);) |
| 168 DEF_BENCH(return new RotRectBench(false, kChangingOpaque_ColorType,
SkXfermode::kSrcOver_Mode);) |
| 169 DEF_BENCH(return new RotRectBench(false, kChangingTransparent_ColorType,
SkXfermode::kSrcOver_Mode);) |
| 170 DEF_BENCH(return new RotRectBench(false, kAlternatingOpaqueAndTransparent_ColorT
ype, SkXfermode::kSrcOver_Mode);) |
| 171 |
| 172 DEF_BENCH(return new RotRectBench(true, kConstantOpaque_ColorType,
SkXfermode::kSrc_Mode);) |
| 173 DEF_BENCH(return new RotRectBench(true, kConstantTransparent_ColorType,
SkXfermode::kSrc_Mode);) |
| 174 DEF_BENCH(return new RotRectBench(true, kChangingOpaque_ColorType,
SkXfermode::kSrc_Mode);) |
| 175 DEF_BENCH(return new RotRectBench(true, kChangingTransparent_ColorType,
SkXfermode::kSrc_Mode);) |
| 176 DEF_BENCH(return new RotRectBench(true, kAlternatingOpaqueAndTransparent_ColorT
ype, SkXfermode::kSrc_Mode);) |
| 177 |
| 178 DEF_BENCH(return new RotRectBench(false, kConstantOpaque_ColorType,
SkXfermode::kSrc_Mode);) |
| 179 DEF_BENCH(return new RotRectBench(false, kConstantTransparent_ColorType,
SkXfermode::kSrc_Mode);) |
| 180 DEF_BENCH(return new RotRectBench(false, kChangingOpaque_ColorType,
SkXfermode::kSrc_Mode);) |
| 181 DEF_BENCH(return new RotRectBench(false, kChangingTransparent_ColorType,
SkXfermode::kSrc_Mode);) |
| 182 DEF_BENCH(return new RotRectBench(false, kAlternatingOpaqueAndTransparent_ColorT
ype, SkXfermode::kSrc_Mode);) |
| 183 |
| 184 DEF_BENCH(return new RotRectBench(true, kConstantOpaque_ColorType,
SkXfermode::kDarken_Mode);) |
| 185 DEF_BENCH(return new RotRectBench(true, kConstantTransparent_ColorType,
SkXfermode::kDarken_Mode);) |
| 186 DEF_BENCH(return new RotRectBench(true, kChangingOpaque_ColorType,
SkXfermode::kDarken_Mode);) |
| 187 DEF_BENCH(return new RotRectBench(true, kChangingTransparent_ColorType,
SkXfermode::kDarken_Mode);) |
| 188 DEF_BENCH(return new RotRectBench(true, kAlternatingOpaqueAndTransparent_ColorT
ype, SkXfermode::kDarken_Mode);) |
| 189 |
| 190 DEF_BENCH(return new RotRectBench(false, kConstantOpaque_ColorType,
SkXfermode::kDarken_Mode);) |
| 191 DEF_BENCH(return new RotRectBench(false, kConstantTransparent_ColorType,
SkXfermode::kDarken_Mode);) |
| 192 DEF_BENCH(return new RotRectBench(false, kChangingOpaque_ColorType,
SkXfermode::kDarken_Mode);) |
| 193 DEF_BENCH(return new RotRectBench(false, kChangingTransparent_ColorType,
SkXfermode::kDarken_Mode);) |
| 194 DEF_BENCH(return new RotRectBench(false, kAlternatingOpaqueAndTransparent_ColorT
ype, SkXfermode::kDarken_Mode);) |
OLD | NEW |