OLD | NEW |
---|---|
(Empty) | |
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" | |
9 #include "SkCanvas.h" | |
10 #include "SkGradientShader.h" | |
11 #include "SkPaint.h" | |
12 #include "SkString.h" | |
13 | |
14 enum ColorPattern { | |
15 kWhite_ColorPattern, | |
16 kBlue_ColorPattern, | |
17 kOpaqueBitmap_ColorPattern, | |
18 kAlphaBitmap_ColorPattern, | |
19 }; | |
20 | |
21 static const struct ColorPatternData{ | |
22 SkColor fColor; | |
bsalomon
2014/07/11 14:29:52
Calling setAlpha on SkPaint actually changes SkPai
egdaniel
2014/07/11 15:01:25
Alpha field removed.
On 2014/07/11 14:29:52, bsalo
| |
23 U8CPU fAlpha; | |
24 bool fIsBitmap; | |
25 const char* fName; | |
26 } gColorPatterns[] = { | |
27 // Keep this in same order as ColorPattern enum | |
28 { SK_ColorWHITE, 0xFF, false, "white" }, // kWhite_ColorPattern | |
29 { SK_ColorBLUE, 0xFF, false, "blue" }, // kBlue_ColorPattern | |
30 { SK_ColorWHITE, 0xFF, true, "obaqueBitMap" }, // kOpaqueBitmap_ColorPatte rn | |
31 { SK_ColorWHITE, 0x10, true, "alphaBitmap" }, // kAlphaBitmap_ColorPatter n | |
32 }; | |
33 | |
34 enum DrawType { | |
35 kRect_DrawType, | |
36 kPath_DrawType, | |
37 }; | |
38 | |
39 static void makebm(SkBitmap* bm, int w, int h) { | |
40 bm->allocN32Pixels(w, h); | |
41 bm->eraseColor(SK_ColorTRANSPARENT); | |
42 | |
43 SkCanvas canvas(*bm); | |
44 SkScalar s = SkIntToScalar(SkMin32(w, h)); | |
45 static const SkPoint kPts0[] = { { 0, 0 }, { s, s } }; | |
46 static const SkPoint kPts1[] = { { s/2, 0 }, { s/2, s } }; | |
47 static const SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 }; | |
48 static const SkColor kColors0[] = {0x80F00080, 0xF0F08000, 0x800080F0 }; | |
49 static const SkColor kColors1[] = {0xF08000F0, 0x8080F000, 0xF000F080 }; | |
50 | |
51 | |
52 SkPaint paint; | |
53 | |
54 paint.setShader(SkGradientShader::CreateLinear(kPts0, kColors0, kPos, | |
55 SK_ARRAY_COUNT(kColors0), SkShader::kClamp_TileMode))->unref (); | |
56 canvas.drawPaint(paint); | |
57 paint.setShader(SkGradientShader::CreateLinear(kPts1, kColors1, kPos, | |
58 SK_ARRAY_COUNT(kColors1), SkShader::kClamp_TileMode))->unref (); | |
59 canvas.drawPaint(paint); | |
60 } | |
61 | |
62 /** | |
63 * This bench draws a grid of either rects or filled paths, with two alternating color patterns. | |
64 * This color patterns are passed in as enums to the class. The options are: | |
65 * 1) solid white color | |
66 * 2) solid blue color | |
67 * 3) opaque bitmap | |
68 * 4) partial alpha bitmap | |
69 * The same color pattern can be set for both arguments to create a uniform patt ern on all draws. | |
70 * | |
71 * The bench is used to test a few things. First it can test any optimizations m ade for a specific | |
72 * color pattern (for example drawing an opaque bitmap versus one with partial a lpha). Also it can | |
73 * be used to test the cost of program switching and/or batching when alternatin g between different | |
74 * patterns when on the gpu. | |
75 */ | |
76 class AlternatingColorPatternBench : public Benchmark { | |
77 public: | |
78 enum { | |
79 NX = 5, | |
80 NY = 5, | |
81 NUM_DRAWS = NX * NY, | |
82 }; | |
83 SkPath fPaths[NUM_DRAWS]; | |
84 SkRect fRects[NUM_DRAWS]; | |
85 U8CPU fAlphas[NUM_DRAWS]; | |
86 SkColor fColors[NUM_DRAWS]; | |
87 SkShader* fShaders[NUM_DRAWS]; | |
88 | |
89 SkString fName; | |
90 ColorPatternData fPattern1; | |
91 ColorPatternData fPattern2; | |
92 DrawType fDrawType; | |
93 SkBitmap fBmp; | |
94 | |
95 SkShader* fBmShader; | |
96 | |
97 AlternatingColorPatternBench(ColorPattern pattern1, ColorPattern pattern2, D rawType drawType) { | |
98 fPattern1 = gColorPatterns[pattern1]; | |
99 fPattern2 = gColorPatterns[pattern2]; | |
100 fName.printf("colorbench_%s_%s_%s", | |
101 fPattern1.fName, fPattern2.fName, | |
102 kRect_DrawType == drawType ? "rect" : "path"); | |
103 fDrawType = drawType; | |
104 } | |
105 | |
106 virtual ~AlternatingColorPatternBench() { | |
107 fBmShader->unref(); | |
108 } | |
109 | |
110 protected: | |
111 virtual const char* onGetName() SK_OVERRIDE { | |
112 return fName.c_str(); | |
113 } | |
114 | |
115 virtual void onPreDraw() { | |
116 int w = 40; | |
117 int h = 40; | |
118 makebm(&fBmp, w, h); | |
119 fBmShader = SkShader::CreateBitmapShader(fBmp, | |
120 SkShader::kRepeat_TileMode, | |
121 SkShader::kRepeat_TileMode); | |
122 int offset = 2; | |
123 int count = 0; | |
124 for (int j = 0; j < NY; ++j) { | |
125 for (int i = 0; i < NX; ++i) { | |
126 int x = (w + offset) * i; | |
127 int y = (h * offset) * j; | |
128 if (kRect_DrawType == fDrawType) { | |
129 fRects[count].set(x, y, x + w, y + h); | |
130 } else { | |
131 fPaths[count].moveTo(x, y); | |
132 fPaths[count].rLineTo(w, 0); | |
133 fPaths[count].rLineTo(0, h); | |
134 fPaths[count].rLineTo(-w + 1, 0); | |
135 } | |
136 if (0 == count % 2) { | |
137 fColors[count] = fPattern1.fColor; | |
138 fAlphas[count] = fPattern1.fAlpha; | |
139 fShaders[count] = fPattern1.fIsBitmap ? fBmShader : NULL; | |
140 } else { | |
141 fColors[count] = fPattern2.fColor; | |
142 fAlphas[count] = fPattern2.fAlpha; | |
143 fShaders[count] = fPattern2.fIsBitmap ? fBmShader : NULL; | |
144 } | |
145 ++count; | |
146 } | |
147 } | |
148 } | |
149 | |
150 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { | |
151 SkPaint paint; | |
152 paint.setAntiAlias(false); | |
153 paint.setFilterLevel(SkPaint::kLow_FilterLevel); | |
154 | |
155 for (int i = 0; i < loops; ++i) { | |
156 for (int j = 0; j < NUM_DRAWS; ++j) { | |
157 paint.setColor(fColors[j]); | |
158 paint.setAlpha(fAlphas[j]); | |
159 paint.setShader(fShaders[j]); | |
160 if (kRect_DrawType == fDrawType) { | |
161 canvas->drawRect(fRects[j], paint); | |
162 } else { | |
163 canvas->drawPath(fPaths[j], paint); | |
164 } | |
165 } | |
166 } | |
167 } | |
168 | |
169 private: | |
170 typedef Benchmark INHERITED; | |
171 }; | |
172 | |
173 DEF_BENCH( return SkNEW_ARGS(AlternatingColorPatternBench, | |
174 (kWhite_ColorPattern, kWhite_ColorPattern, | |
175 kPath_DrawType)); ) | |
176 DEF_BENCH( return SkNEW_ARGS(AlternatingColorPatternBench, | |
177 (kBlue_ColorPattern, kBlue_ColorPattern, | |
178 kPath_DrawType)); ) | |
179 DEF_BENCH( return SkNEW_ARGS(AlternatingColorPatternBench, | |
180 (kWhite_ColorPattern, kBlue_ColorPattern, | |
181 kPath_DrawType)); ) | |
182 | |
183 DEF_BENCH( return SkNEW_ARGS(AlternatingColorPatternBench, | |
184 (kOpaqueBitmap_ColorPattern, kOpaqueBitmap_ColorPat tern, | |
185 kPath_DrawType)); ) | |
186 DEF_BENCH( return SkNEW_ARGS(AlternatingColorPatternBench, | |
187 (kAlphaBitmap_ColorPattern, kAlphaBitmap_ColorPatte rn, | |
188 kPath_DrawType)); ) | |
189 DEF_BENCH( return SkNEW_ARGS(AlternatingColorPatternBench, | |
190 (kOpaqueBitmap_ColorPattern, kAlphaBitmap_ColorPatt ern, | |
191 kPath_DrawType)); ) | |
192 | |
193 DEF_BENCH( return SkNEW_ARGS(AlternatingColorPatternBench, | |
194 (kOpaqueBitmap_ColorPattern, kOpaqueBitmap_ColorPat tern, | |
195 kRect_DrawType)); ) | |
196 DEF_BENCH( return SkNEW_ARGS(AlternatingColorPatternBench, | |
197 (kAlphaBitmap_ColorPattern, kAlphaBitmap_ColorPatte rn, | |
198 kRect_DrawType)); ) | |
199 DEF_BENCH( return SkNEW_ARGS(AlternatingColorPatternBench, | |
200 (kOpaqueBitmap_ColorPattern, kAlphaBitmap_ColorPatt ern, | |
201 kRect_DrawType)); ) | |
202 | |
OLD | NEW |