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