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