blob: db7865bb3b068bea18e11ec9bbcba83a81060852 [file] [log] [blame]
dandov7e5598a2014-08-15 13:30:47 -07001/*
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 "SkPatchGrid.h"
13#include "SkString.h"
14
15 /**
halcanary9d524f22016-03-29 09:03:52 -070016 * This bench measures the rendering time of a gridof patches.
17 * This bench also tests the different combination of optional parameters for the function
dandov7e5598a2014-08-15 13:30:47 -070018 * (passing texture coordinates and colors, only textures coordinates, only colors or none).
19 * Finally, it also has 3 possible sizes small, medium and big to test if the size of the patches
halcanary9d524f22016-03-29 09:03:52 -070020 * in the grid affects time.
dandov7e5598a2014-08-15 13:30:47 -070021 */
22
23class PatchGridBench : public Benchmark {
halcanary9d524f22016-03-29 09:03:52 -070024
dandov7e5598a2014-08-15 13:30:47 -070025public:
halcanary9d524f22016-03-29 09:03:52 -070026
dandov7e5598a2014-08-15 13:30:47 -070027 enum Size {
28 kSmall_Size,
29 kMedium_Size,
30 kBig_Size
31 };
halcanary9d524f22016-03-29 09:03:52 -070032
dandov7e5598a2014-08-15 13:30:47 -070033 enum VertexMode {
34 kNone_VertexMode,
35 kColors_VertexMode,
36 kTexCoords_VertexMode,
37 kBoth_VertexMode
38 };
halcanary9d524f22016-03-29 09:03:52 -070039
dandov7e5598a2014-08-15 13:30:47 -070040 PatchGridBench(Size size, VertexMode vertexMode)
41 : fVertexMode(vertexMode)
42 , fSize(size) { }
halcanary9d524f22016-03-29 09:03:52 -070043
dandov7e5598a2014-08-15 13:30:47 -070044 void setScale(SkCanvas* canvas){
45 switch (fSize) {
46 case kSmall_Size:
47 canvas->scale(0.1f, 0.1f);
48 break;
49 case kMedium_Size:
50 canvas->scale(1.0f, 1.0f);
51 break;
52 case kBig_Size:
53 canvas->scale(3.0f, 3.0f);
54 break;
55 }
56 }
halcanary9d524f22016-03-29 09:03:52 -070057
dandov7e5598a2014-08-15 13:30:47 -070058 void setGrid() {
59 SkPoint vertices[4][5] = {
60 {{50,50}, {150,50}, {250,50},{350,50},{450,50}},
61 {{50,150}, {120,120}, {250,150},{350,150},{450,150}},
62 {{50,250}, {150,250}, {250,250},{350,250},{450,250}},
63 {{100,300}, {150,350}, {250,350},{350,350},{450,350}}
64 };
halcanary9d524f22016-03-29 09:03:52 -070065
dandov7e5598a2014-08-15 13:30:47 -070066 SkColor cornerColors[4][5] = {
67 {SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE},
68 {SK_ColorRED, SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE, SK_ColorRED},
69 {SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE},
70 {SK_ColorRED, SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE, SK_ColorRED},
71 };
halcanary9d524f22016-03-29 09:03:52 -070072
dandov7e5598a2014-08-15 13:30:47 -070073 SkPoint texCoords[4][5] = {
74 {{0.0f,0.0f}, {1.0f,0.0f}, {2.0f,0.0f}, {3.0f,0.0f}, {4.0f,0.0f}},
75 {{0.0f,1.0f}, {1.0f,1.0f}, {2.0f,1.0f}, {3.0f,1.0f}, {4.0f,1.0f}},
76 {{0.0f,2.0f}, {1.0f,2.0f}, {2.0f,2.0f}, {3.0f,2.0f}, {4.0f,2.0f}},
77 {{0.0f,3.0f}, {1.0f,3.0f}, {2.0f,3.0f}, {3.0f,3.0f}, {4.0f,3.0f}},
78 };
halcanary9d524f22016-03-29 09:03:52 -070079
dandov7e5598a2014-08-15 13:30:47 -070080 SkPoint hrzCtrl[4][8] = {
81 {{75,30},{125,45},{175,70},{225,20},{275,50},{325,50},{375,5},{425,90}},
82 {{75,150},{125,150},{175,150},{225,150},{275,150},{325,150},{375,150},{425,150}},
83 {{75,250},{125,250},{175,250},{225,250},{275,200},{325,150},{375,250},{425,250}},
84 {{75,350},{125,350},{175,350},{225,350},{275,350},{325,350},{375,350},{425,350}}
85 };
halcanary9d524f22016-03-29 09:03:52 -070086
dandov7e5598a2014-08-15 13:30:47 -070087 SkPoint vrtCtrl[6][5] = {
88 {{50,75},{150,75},{250,75},{350,75},{450,75}},
89 {{50,125},{150,125},{250,125},{350,125},{450,125}},
90 {{50,175},{150,175},{220,225},{350,175},{470,225}},
91 {{50,225},{150,225},{220,175},{350,225},{470,155}},
92 {{50,275},{150,275},{250,275},{350,275},{400,305}},
93 {{50,325},{150,325},{250,325},{350,325},{450,325}}
94 };
halcanary9d524f22016-03-29 09:03:52 -070095
dandov7e5598a2014-08-15 13:30:47 -070096 static const int kRows = 3;
97 static const int kCols = 4;
halcanary9d524f22016-03-29 09:03:52 -070098
halcanary96fcdcc2015-08-27 07:41:13 -070099 fGrid.reset(kRows, kCols, SkPatchGrid::kColors_VertexType, nullptr);
dandov7e5598a2014-08-15 13:30:47 -0700100 for (int i = 0; i < kRows; i++) {
101 for (int j = 0; j < kCols; j++) {
102 SkPoint points[12];
halcanary9d524f22016-03-29 09:03:52 -0700103
dandov7e5598a2014-08-15 13:30:47 -0700104 //set corners
105 points[SkPatchUtils::kTopP0_CubicCtrlPts] = vertices[i][j];
106 points[SkPatchUtils::kTopP3_CubicCtrlPts] = vertices[i][j + 1];
107 points[SkPatchUtils::kBottomP0_CubicCtrlPts] = vertices[i + 1][j];
108 points[SkPatchUtils::kBottomP3_CubicCtrlPts] = vertices[i + 1][j + 1];
halcanary9d524f22016-03-29 09:03:52 -0700109
dandov7e5598a2014-08-15 13:30:47 -0700110 points[SkPatchUtils::kTopP1_CubicCtrlPts] = hrzCtrl[i][j * 2];
111 points[SkPatchUtils::kTopP2_CubicCtrlPts] = hrzCtrl[i][j * 2 + 1];
112 points[SkPatchUtils::kBottomP1_CubicCtrlPts] = hrzCtrl[i + 1][j * 2];
113 points[SkPatchUtils::kBottomP2_CubicCtrlPts] = hrzCtrl[i + 1][j * 2 + 1];
halcanary9d524f22016-03-29 09:03:52 -0700114
dandov7e5598a2014-08-15 13:30:47 -0700115 points[SkPatchUtils::kLeftP1_CubicCtrlPts] = vrtCtrl[i * 2][j];
116 points[SkPatchUtils::kLeftP2_CubicCtrlPts] = vrtCtrl[i * 2 + 1][j];
117 points[SkPatchUtils::kRightP1_CubicCtrlPts] = vrtCtrl[i * 2][j + 1];
118 points[SkPatchUtils::kRightP2_CubicCtrlPts] = vrtCtrl[i * 2 + 1][j + 1];
halcanary9d524f22016-03-29 09:03:52 -0700119
dandov7e5598a2014-08-15 13:30:47 -0700120 SkColor colors[4];
121 colors[0] = cornerColors[i][j];
122 colors[1] = cornerColors[i][j + 1];
123 colors[3] = cornerColors[i + 1][j];
124 colors[2] = cornerColors[i + 1][j + 1];
halcanary9d524f22016-03-29 09:03:52 -0700125
dandov7e5598a2014-08-15 13:30:47 -0700126 SkPoint texs[4];
127 texs[0] = texCoords[i][j];
128 texs[1] = texCoords[i][j + 1];
129 texs[3] = texCoords[i + 1][j];
130 texs[2] = texCoords[i + 1][j + 1];
halcanary9d524f22016-03-29 09:03:52 -0700131
dandov7e5598a2014-08-15 13:30:47 -0700132 switch (fVertexMode) {
133 case kNone_VertexMode:
halcanary96fcdcc2015-08-27 07:41:13 -0700134 fGrid.setPatch(j, i, points, nullptr, nullptr);
dandov7e5598a2014-08-15 13:30:47 -0700135 break;
136 case kColors_VertexMode:
halcanary96fcdcc2015-08-27 07:41:13 -0700137 fGrid.setPatch(j, i, points, colors, nullptr);
dandov7e5598a2014-08-15 13:30:47 -0700138 break;
139 case kTexCoords_VertexMode:
halcanary96fcdcc2015-08-27 07:41:13 -0700140 fGrid.setPatch(j, i, points, nullptr, texs);
dandov7e5598a2014-08-15 13:30:47 -0700141 break;
142 case kBoth_VertexMode:
143 fGrid.setPatch(j, i, points, colors, texs);
144 break;
145 default:
146 break;
147 }
148 }
149 }
150 }
halcanary9d524f22016-03-29 09:03:52 -0700151
dandov7e5598a2014-08-15 13:30:47 -0700152 // override this method to change the shader
reedc6f28f72016-03-14 12:22:10 -0700153 sk_sp<SkShader> createShader() {
dandov7e5598a2014-08-15 13:30:47 -0700154 const SkColor colors[] = {
155 SK_ColorRED, SK_ColorCYAN, SK_ColorGREEN, SK_ColorWHITE,
156 SK_ColorMAGENTA, SK_ColorBLUE, SK_ColorYELLOW,
157 };
158 const SkPoint pts[] = { { 200.f / 4.f, 0.f }, { 3.f * 200.f / 4, 200.f } };
halcanary9d524f22016-03-29 09:03:52 -0700159
reedc6f28f72016-03-14 12:22:10 -0700160 return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
161 SkShader::kMirror_TileMode);
dandov7e5598a2014-08-15 13:30:47 -0700162 }
163
164protected:
mtklein36352bf2015-03-25 18:17:31 -0700165 const char* onGetName() override {
dandov7e5598a2014-08-15 13:30:47 -0700166 SkString vertexMode;
167 switch (fVertexMode) {
168 case kNone_VertexMode:
169 vertexMode.set("meshlines");
170 break;
171 case kColors_VertexMode:
172 vertexMode.set("colors");
173 break;
174 case kTexCoords_VertexMode:
175 vertexMode.set("texs");
176 break;
177 case kBoth_VertexMode:
178 vertexMode.set("colors_texs");
179 break;
180 default:
181 break;
182 }
halcanary9d524f22016-03-29 09:03:52 -0700183
dandov7e5598a2014-08-15 13:30:47 -0700184 SkString size;
185 switch (fSize) {
186 case kSmall_Size:
187 size.set("small");
188 break;
189 case kMedium_Size:
190 size.set("medium");
191 break;
192 case kBig_Size:
193 size.set("big");
194 break;
195 default:
196 break;
197 }
198 fName.printf("patch_grid_%s_%s", vertexMode.c_str(), size.c_str());
199 return fName.c_str();
200 }
halcanary9d524f22016-03-29 09:03:52 -0700201
joshualitt8a6697a2015-09-30 12:11:07 -0700202 void onDelayedSetup() override {
dandov7e5598a2014-08-15 13:30:47 -0700203 this->setGrid();
204 switch (fVertexMode) {
205 case kTexCoords_VertexMode:
206 case kBoth_VertexMode:
reedc6f28f72016-03-14 12:22:10 -0700207 fPaint.setShader(createShader());
dandov7e5598a2014-08-15 13:30:47 -0700208 break;
209 default:
halcanary96fcdcc2015-08-27 07:41:13 -0700210 fPaint.setShader(nullptr);
dandov7e5598a2014-08-15 13:30:47 -0700211 break;
212 }
213 this->setupPaint(&fPaint);
214 }
215
mtkleina1ebeb22015-10-01 09:43:39 -0700216 void onDraw(int loops, SkCanvas* canvas) override {
dandov7e5598a2014-08-15 13:30:47 -0700217 this->setScale(canvas);
218 for (int i = 0; i < loops; i++) {
219 fGrid.draw(canvas, fPaint);
220 }
221 }
222
223 SkPaint fPaint;
224 SkString fName;
225 SkPatchGrid fGrid;
226 VertexMode fVertexMode;
227 Size fSize;
halcanary9d524f22016-03-29 09:03:52 -0700228
dandov7e5598a2014-08-15 13:30:47 -0700229 typedef Benchmark INHERITED;
230};
231
232
233///////////////////////////////////////////////////////////////////////////////
234
235DEF_BENCH( return new PatchGridBench(PatchGridBench::kSmall_Size,
236 PatchGridBench::kNone_VertexMode); )
237DEF_BENCH( return new PatchGridBench(PatchGridBench::kSmall_Size,
238 PatchGridBench::kColors_VertexMode); )
239DEF_BENCH( return new PatchGridBench(PatchGridBench::kSmall_Size,
240 PatchGridBench::kTexCoords_VertexMode); )
241DEF_BENCH( return new PatchGridBench(PatchGridBench::kSmall_Size,
242 PatchGridBench::kBoth_VertexMode); )
243DEF_BENCH( return new PatchGridBench(PatchGridBench::kMedium_Size,
244 PatchGridBench::kNone_VertexMode); )
245DEF_BENCH( return new PatchGridBench(PatchGridBench::kMedium_Size,
246 PatchGridBench::kColors_VertexMode); )
247DEF_BENCH( return new PatchGridBench(PatchGridBench::kMedium_Size,
248 PatchGridBench::kTexCoords_VertexMode); )
249DEF_BENCH( return new PatchGridBench(PatchGridBench::kMedium_Size,
250 PatchGridBench::kBoth_VertexMode); )
251DEF_BENCH( return new PatchGridBench(PatchGridBench::kBig_Size,
252 PatchGridBench::kNone_VertexMode); )
253DEF_BENCH( return new PatchGridBench(PatchGridBench::kBig_Size,
254 PatchGridBench::kColors_VertexMode); )
255DEF_BENCH( return new PatchGridBench(PatchGridBench::kBig_Size,
256 PatchGridBench::kTexCoords_VertexMode); )
257DEF_BENCH( return new PatchGridBench(PatchGridBench::kBig_Size,
258 PatchGridBench::kBoth_VertexMode); )