2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "SkGradientShader.h"
11 #include "SkPatchUtils.h"
15 * This bench measures the rendering time of the call SkCanvas::drawPatch with different types of
16 * input patches (regular case, with loops, a square, with a big difference between "parallel"
17 * sides). This bench also tests the different combination of optional parameters for the function
18 * (passing texture coordinates and colors, only textures coordinates, only colors or none).
19 * Finally, it applies a scale to test if the size affects the rendering time.
22 class PatchBench : public Benchmark {
29 kTexCoords_VertexMode,
33 PatchBench(SkPoint scale, VertexMode vertexMode)
35 , fVertexMode(vertexMode) { }
37 // to add name of specific class override this method
38 virtual void appendName(SkString* name) {
39 name->append("normal");
42 // to make other type of patches override this method
43 virtual void setCubics() {
44 const SkPoint points[SkPatchUtils::kNumCtrlPts] = {
46 {100,100},{150,50},{250,150}, {300,100},
50 {300,300},{250,250},{150,350},{100,300},
54 memcpy(fCubics, points, SkPatchUtils::kNumCtrlPts * sizeof(SkPoint));
57 virtual void setColors() {
58 const SkColor colors[SkPatchUtils::kNumCorners] = {
59 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorCYAN
61 memcpy(fColors, colors, SkPatchUtils::kNumCorners * sizeof(SkColor));
64 virtual void setTexCoords() {
65 const SkPoint texCoords[SkPatchUtils::kNumCorners] = {
66 {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f,1.0f}, {0.0f, 1.0f}
68 memcpy(fTexCoords, texCoords, SkPatchUtils::kNumCorners * sizeof(SkPoint));
71 // override this method to change the shader
72 virtual SkShader* createShader() {
73 const SkColor colors[] = {
74 SK_ColorRED, SK_ColorCYAN, SK_ColorGREEN, SK_ColorWHITE,
75 SK_ColorMAGENTA, SK_ColorBLUE, SK_ColorYELLOW,
77 const SkPoint pts[] = { { 200.f / 4.f, 0.f }, { 3.f * 200.f / 4, 200.f } };
79 return SkGradientShader::CreateLinear(pts, colors, NULL,
80 SK_ARRAY_COUNT(colors),
81 SkShader::kMirror_TileMode);
85 const char* onGetName() SK_OVERRIDE {
87 switch (fVertexMode) {
88 case kNone_VertexMode:
89 vertexMode.set("meshlines");
91 case kColors_VertexMode:
92 vertexMode.set("colors");
94 case kTexCoords_VertexMode:
95 vertexMode.set("texs");
97 case kBoth_VertexMode:
98 vertexMode.set("colors_texs");
104 this->appendName(&type);
105 fName.printf("patch_%s_%s_[%f,%f]", type.c_str(), vertexMode.c_str(),
106 fScale.x(), fScale.y());
107 return fName.c_str();
110 void onPreDraw() SK_OVERRIDE {
113 this->setTexCoords();
114 this->setupPaint(&fPaint);
115 switch (fVertexMode) {
116 case kTexCoords_VertexMode:
117 case kBoth_VertexMode:
118 fPaint.setShader(this->createShader())->unref();
121 fPaint.setShader(NULL);
126 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
127 canvas->scale(fScale.x(), fScale.y());
128 for (int i = 0; i < loops; i++) {
129 switch (fVertexMode) {
130 case kNone_VertexMode:
131 canvas->drawPatch(fCubics, NULL, NULL, NULL, fPaint);
133 case kColors_VertexMode:
134 canvas->drawPatch(fCubics, fColors, NULL, NULL, fPaint);
136 case kTexCoords_VertexMode:
137 canvas->drawPatch(fCubics, NULL, fTexCoords, NULL, fPaint);
139 case kBoth_VertexMode:
140 canvas->drawPatch(fCubics, fColors, fTexCoords, NULL, fPaint);
152 SkPoint fTexCoords[4];
154 VertexMode fVertexMode;
156 typedef Benchmark INHERITED;
159 class SquarePatchBench : public PatchBench {
161 SquarePatchBench(SkPoint scale, VertexMode vertexMode)
162 : INHERITED(scale, vertexMode) { }
164 void appendName(SkString* name) SK_OVERRIDE {
165 name->append("square");
168 void setCubics() SK_OVERRIDE {
169 const SkPoint points[SkPatchUtils::kNumCtrlPts] = {
171 {100,100},{150,100},{250,100}, {300,100},
173 {300, 150},{300,250},
175 {300,300},{250,300},{150,300},{100,300},
179 memcpy(fCubics, points, SkPatchUtils::kNumCtrlPts * sizeof(SkPoint));
182 typedef PatchBench INHERITED;
185 class LODDiffPatchBench : public PatchBench {
187 LODDiffPatchBench(SkPoint scale, VertexMode vertexMode)
188 : INHERITED(scale, vertexMode) { }
190 void appendName(SkString* name) SK_OVERRIDE {
191 name->append("LOD_Diff");
194 void setCubics() SK_OVERRIDE {
195 const SkPoint points[SkPatchUtils::kNumCtrlPts] = {
197 {100,175},{150,100},{250,100}, {300,0},
199 {300, 150},{300,250},
201 {300,400},{250,300},{150,300},{100,225},
205 memcpy(fCubics, points, SkPatchUtils::kNumCtrlPts * sizeof(SkPoint));
208 typedef PatchBench INHERITED;
211 class LoopPatchBench : public PatchBench {
213 LoopPatchBench(SkPoint scale, VertexMode vertexMode)
214 : INHERITED(scale, vertexMode) { }
216 void appendName(SkString* name) SK_OVERRIDE {
217 name->append("loop");
220 void setCubics() SK_OVERRIDE {
221 const SkPoint points[SkPatchUtils::kNumCtrlPts] = {
223 {100,100},{300,200},{100,200}, {300,100},
227 {300,300},{250,250},{30,200},{100,300},
231 memcpy(fCubics, points, SkPatchUtils::kNumCtrlPts * sizeof(SkPoint));
234 typedef PatchBench INHERITED;
237 ///////////////////////////////////////////////////////////////////////////////
239 DEF_BENCH( return new PatchBench(SkVector::Make(0.1f, 0.1f), PatchBench::kNone_VertexMode); )
240 DEF_BENCH( return new PatchBench(SkVector::Make(0.1f, 0.1f), PatchBench::kColors_VertexMode); )
241 DEF_BENCH( return new PatchBench(SkVector::Make(0.1f, 0.1f), PatchBench::kTexCoords_VertexMode); )
242 DEF_BENCH( return new PatchBench(SkVector::Make(0.1f, 0.1f), PatchBench::kBoth_VertexMode); )
243 DEF_BENCH( return new PatchBench(SkVector::Make(1.f, 1.0f), PatchBench::kNone_VertexMode); )
244 DEF_BENCH( return new PatchBench(SkVector::Make(1.0f, 1.0f), PatchBench::kColors_VertexMode); )
245 DEF_BENCH( return new PatchBench(SkVector::Make(1.0f, 1.0f), PatchBench::kTexCoords_VertexMode); )
246 DEF_BENCH( return new PatchBench(SkVector::Make(1.0f, 1.0f), PatchBench::kBoth_VertexMode); )
247 DEF_BENCH( return new PatchBench(SkVector::Make(3.0f, 3.0f), PatchBench::kNone_VertexMode); )
248 DEF_BENCH( return new PatchBench(SkVector::Make(3.0f, 3.0f), PatchBench::kColors_VertexMode); )
249 DEF_BENCH( return new PatchBench(SkVector::Make(3.0f, 3.0f), PatchBench::kTexCoords_VertexMode); )
250 DEF_BENCH( return new PatchBench(SkVector::Make(3.0f, 3.0f), PatchBench::kBoth_VertexMode); )
252 DEF_BENCH( return new SquarePatchBench(SkVector::Make(0.1f, 0.1f),
253 PatchBench::kNone_VertexMode); )
254 DEF_BENCH( return new SquarePatchBench(SkVector::Make(0.1f, 0.1f),
255 PatchBench::kColors_VertexMode); )
256 DEF_BENCH( return new SquarePatchBench(SkVector::Make(0.1f, 0.1f),
257 PatchBench::kTexCoords_VertexMode); )
258 DEF_BENCH( return new SquarePatchBench(SkVector::Make(0.1f, 0.1f),
259 PatchBench::kBoth_VertexMode); )
260 DEF_BENCH( return new SquarePatchBench(SkVector::Make(1.f, 1.0f),
261 PatchBench::kNone_VertexMode); )
262 DEF_BENCH( return new SquarePatchBench(SkVector::Make(1.0f, 1.0f),
263 PatchBench::kColors_VertexMode); )
264 DEF_BENCH( return new SquarePatchBench(SkVector::Make(1.0f, 1.0f),
265 PatchBench::kTexCoords_VertexMode); )
266 DEF_BENCH( return new SquarePatchBench(SkVector::Make(1.0f, 1.0f),
267 PatchBench::kBoth_VertexMode); )
268 DEF_BENCH( return new SquarePatchBench(SkVector::Make(3.0f, 3.0f),
269 PatchBench::kNone_VertexMode); )
270 DEF_BENCH( return new SquarePatchBench(SkVector::Make(3.0f, 3.0f),
271 PatchBench::kColors_VertexMode); )
272 DEF_BENCH( return new SquarePatchBench(SkVector::Make(3.0f, 3.0f),
273 PatchBench::kTexCoords_VertexMode); )
274 DEF_BENCH( return new SquarePatchBench(SkVector::Make(3.0f, 3.0f),
275 PatchBench::kBoth_VertexMode); )
277 DEF_BENCH( return new LODDiffPatchBench(SkVector::Make(0.1f, 0.1f),
278 PatchBench::kNone_VertexMode); )
279 DEF_BENCH( return new LODDiffPatchBench(SkVector::Make(0.1f, 0.1f),
280 PatchBench::kColors_VertexMode); )
281 DEF_BENCH( return new LODDiffPatchBench(SkVector::Make(0.1f, 0.1f),
282 PatchBench::kTexCoords_VertexMode); )
283 DEF_BENCH( return new LODDiffPatchBench(SkVector::Make(0.1f, 0.1f),
284 PatchBench::kBoth_VertexMode); )
285 DEF_BENCH( return new LODDiffPatchBench(SkVector::Make(1.f, 1.0f),
286 PatchBench::kNone_VertexMode); )
287 DEF_BENCH( return new LODDiffPatchBench(SkVector::Make(1.0f, 1.0f),
288 PatchBench::kColors_VertexMode); )
289 DEF_BENCH( return new LODDiffPatchBench(SkVector::Make(1.0f, 1.0f),
290 PatchBench::kTexCoords_VertexMode); )
291 DEF_BENCH( return new LODDiffPatchBench(SkVector::Make(1.0f, 1.0f),
292 PatchBench::kBoth_VertexMode); )
293 DEF_BENCH( return new LODDiffPatchBench(SkVector::Make(3.0f, 3.0f),
294 PatchBench::kNone_VertexMode); )
295 DEF_BENCH( return new LODDiffPatchBench(SkVector::Make(3.0f, 3.0f),
296 PatchBench::kColors_VertexMode); )
297 DEF_BENCH( return new LODDiffPatchBench(SkVector::Make(3.0f, 3.0f),
298 PatchBench::kTexCoords_VertexMode); )
299 DEF_BENCH( return new LODDiffPatchBench(SkVector::Make(3.0f, 3.0f),
300 PatchBench::kBoth_VertexMode); )
302 DEF_BENCH( return new LoopPatchBench(SkVector::Make(0.1f, 0.1f),
303 PatchBench::kNone_VertexMode); )
304 DEF_BENCH( return new LoopPatchBench(SkVector::Make(0.1f, 0.1f),
305 PatchBench::kColors_VertexMode); )
306 DEF_BENCH( return new LoopPatchBench(SkVector::Make(0.1f, 0.1f),
307 PatchBench::kTexCoords_VertexMode); )
308 DEF_BENCH( return new LoopPatchBench(SkVector::Make(0.1f, 0.1f),
309 PatchBench::kBoth_VertexMode); )
310 DEF_BENCH( return new LoopPatchBench(SkVector::Make(1.f, 1.0f),
311 PatchBench::kNone_VertexMode); )
312 DEF_BENCH( return new LoopPatchBench(SkVector::Make(1.0f, 1.0f),
313 PatchBench::kColors_VertexMode); )
314 DEF_BENCH( return new LoopPatchBench(SkVector::Make(1.0f, 1.0f),
315 PatchBench::kTexCoords_VertexMode); )
316 DEF_BENCH( return new LoopPatchBench(SkVector::Make(1.0f, 1.0f),
317 PatchBench::kBoth_VertexMode); )
318 DEF_BENCH( return new LoopPatchBench(SkVector::Make(3.0f, 3.0f),
319 PatchBench::kNone_VertexMode); )
320 DEF_BENCH( return new LoopPatchBench(SkVector::Make(3.0f, 3.0f),
321 PatchBench::kColors_VertexMode); )
322 DEF_BENCH( return new LoopPatchBench(SkVector::Make(3.0f, 3.0f),
323 PatchBench::kTexCoords_VertexMode); )
324 DEF_BENCH( return new LoopPatchBench(SkVector::Make(3.0f, 3.0f),
325 PatchBench::kBoth_VertexMode); )