blob: 036fed01e199f3dadad17f98c55db5ee11bf539c [file] [log] [blame]
robertphillips@google.com6670ab92013-05-09 19:03:48 +00001/*
2 * Copyright 2012 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "bench/Benchmark.h"
9#include "include/core/SkBitmap.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkPaint.h"
12#include "include/core/SkShader.h"
13#include "include/core/SkString.h"
14#include "include/core/SkVertices.h"
15#include "include/utils/SkRandom.h"
robertphillips@google.com6670ab92013-05-09 19:03:48 +000016
robertphillips@google.com6670ab92013-05-09 19:03:48 +000017// This bench simulates the calls Skia sees from various HTML5 canvas
18// game bench marks
tfarinaf168b862014-06-19 12:32:29 -070019class GameBench : public Benchmark {
robertphillips@google.com6670ab92013-05-09 19:03:48 +000020public:
21 enum Type {
skia.committer@gmail.com0f20a3f2013-05-10 07:01:04 +000022 kScale_Type,
23 kTranslate_Type,
robertphillips@google.com6670ab92013-05-09 19:03:48 +000024 kRotate_Type
25 };
26
robertphillips@google.comf865be32013-05-31 13:27:02 +000027 enum Clear {
28 kFull_Clear,
29 kPartial_Clear
30 };
31
mtklein@google.com410e6e82013-09-13 19:52:27 +000032 GameBench(Type type, Clear clear,
robertphillips@google.com631a59b2013-07-31 14:57:53 +000033 bool aligned = false, bool useAtlas = false,
34 bool useDrawVertices = false)
mtklein@google.com410e6e82013-09-13 19:52:27 +000035 : fType(type)
robertphillips@google.comf865be32013-05-31 13:27:02 +000036 , fClear(clear)
robertphillips@google.com347fd4e2013-05-15 14:18:32 +000037 , fAligned(aligned)
robertphillips@google.comf865be32013-05-31 13:27:02 +000038 , fUseAtlas(useAtlas)
robertphillips@google.com631a59b2013-07-31 14:57:53 +000039 , fUseDrawVertices(useDrawVertices)
robertphillips@google.com6670ab92013-05-09 19:03:48 +000040 , fName("game")
41 , fNumSaved(0)
42 , fInitialized(false) {
43
44 switch (fType) {
45 case kScale_Type:
46 fName.append("_scale");
47 break;
48 case kTranslate_Type:
49 fName.append("_trans");
50 break;
51 case kRotate_Type:
52 fName.append("_rot");
53 break;
Brian Salomon23356442018-11-30 15:33:19 -050054 }
robertphillips@google.com6670ab92013-05-09 19:03:48 +000055
robertphillips@google.com347fd4e2013-05-15 14:18:32 +000056 if (aligned) {
57 fName.append("_aligned");
58 }
59
robertphillips@google.comf865be32013-05-31 13:27:02 +000060 if (kPartial_Clear == clear) {
robertphillips@google.com6670ab92013-05-09 19:03:48 +000061 fName.append("_partial");
62 } else {
63 fName.append("_full");
64 }
65
robertphillips@google.comf865be32013-05-31 13:27:02 +000066 if (useAtlas) {
67 fName.append("_atlas");
68 }
69
robertphillips@google.com631a59b2013-07-31 14:57:53 +000070 if (useDrawVertices) {
71 fName.append("_drawVerts");
72 }
73
robertphillips@google.com6670ab92013-05-09 19:03:48 +000074 // It's HTML 5 canvas, so always AA
75 fName.append("_aa");
76 }
77
78protected:
mtklein36352bf2015-03-25 18:17:31 -070079 const char* onGetName() override {
robertphillips@google.comf865be32013-05-31 13:27:02 +000080 return fName.c_str();
robertphillips@google.com6670ab92013-05-09 19:03:48 +000081 }
82
joshualitt8a6697a2015-09-30 12:11:07 -070083 void onDelayedSetup() override {
robertphillips@google.com6670ab92013-05-09 19:03:48 +000084 if (!fInitialized) {
85 this->makeCheckerboard();
robertphillips@google.comf865be32013-05-31 13:27:02 +000086 this->makeAtlas();
robertphillips@google.com6670ab92013-05-09 19:03:48 +000087 fInitialized = true;
88 }
89 }
90
mtkleina1ebeb22015-10-01 09:43:39 -070091 void onDraw(int loops, SkCanvas* canvas) override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000092 SkRandom scaleRand;
93 SkRandom transRand;
94 SkRandom rotRand;
robertphillips@google.com6670ab92013-05-09 19:03:48 +000095
robertphillips@google.comf865be32013-05-31 13:27:02 +000096 int width, height;
97 if (fUseAtlas) {
98 width = kAtlasCellWidth;
99 height = kAtlasCellHeight;
100 } else {
101 width = kCheckerboardWidth;
102 height = kCheckerboardHeight;
103 }
104
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000105 SkPaint clearPaint;
106 clearPaint.setColor(0xFF000000);
107 clearPaint.setAntiAlias(true);
108
Mike Reed3661bc92017-02-22 13:21:42 -0500109 SkISize size = canvas->getBaseLayerSize();
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000110
111 SkScalar maxTransX, maxTransY;
112
113 if (kScale_Type == fType) {
robertphillips@google.comf865be32013-05-31 13:27:02 +0000114 maxTransX = size.fWidth - (1.5f * width);
115 maxTransY = size.fHeight - (1.5f * height);
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000116 } else if (kTranslate_Type == fType) {
robertphillips@google.comf865be32013-05-31 13:27:02 +0000117 maxTransX = SkIntToScalar(size.fWidth - width);
118 maxTransY = SkIntToScalar(size.fHeight - height);
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000119 } else {
120 SkASSERT(kRotate_Type == fType);
121 // Yes, some rotations will be off the top and left sides
robertphillips@google.comf865be32013-05-31 13:27:02 +0000122 maxTransX = size.fWidth - SK_ScalarSqrt2 * height;
123 maxTransY = size.fHeight - SK_ScalarSqrt2 * height;
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000124 }
125
126 SkMatrix mat;
robertphillips@google.comf865be32013-05-31 13:27:02 +0000127 SkRect dst = { 0, 0, SkIntToScalar(width), SkIntToScalar(height) };
128 SkRect clearRect = { -1.0f, -1.0f, width+1.0f, height+1.0f };
robertphillips@google.com631a59b2013-07-31 14:57:53 +0000129 SkPoint verts[4] = { // for drawVertices path
130 { 0, 0 },
131 { 0, SkIntToScalar(height) },
132 { SkIntToScalar(width), SkIntToScalar(height) },
133 { SkIntToScalar(width), 0 }
134 };
135 uint16_t indices[6] = { 0, 1, 2, 0, 2, 3 };
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000136
137 SkPaint p;
138 p.setColor(0xFF000000);
reed93a12152015-03-16 10:08:34 -0700139 p.setFilterQuality(kLow_SkFilterQuality);
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000140
robertphillips@google.com631a59b2013-07-31 14:57:53 +0000141 SkPaint p2; // for drawVertices path
142 p2.setColor(0xFF000000);
reed93a12152015-03-16 10:08:34 -0700143 p2.setFilterQuality(kLow_SkFilterQuality);
Mike Reed50acf8f2019-04-08 13:20:23 -0400144 p2.setShader(fAtlas.makeShader());
robertphillips@google.com631a59b2013-07-31 14:57:53 +0000145
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000146 for (int i = 0; i < loops; ++i, ++fNumSaved) {
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000147 if (0 == i % kNumBeforeClear) {
robertphillips@google.comf865be32013-05-31 13:27:02 +0000148 if (kPartial_Clear == fClear) {
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000149 for (int j = 0; j < fNumSaved; ++j) {
150 canvas->setMatrix(SkMatrix::I());
151 mat.setTranslate(fSaved[j][0], fSaved[j][1]);
152
153 if (kScale_Type == fType) {
154 mat.preScale(fSaved[j][2], fSaved[j][2]);
155 } else if (kRotate_Type == fType) {
156 mat.preRotate(fSaved[j][2]);
157 }
158
159 canvas->concat(mat);
160 canvas->drawRect(clearRect, clearPaint);
161 }
162 } else {
163 canvas->clear(0xFF000000);
164 }
165
166 fNumSaved = 0;
167 }
168
169 SkASSERT(fNumSaved < kNumBeforeClear);
170
171 canvas->setMatrix(SkMatrix::I());
172
173 fSaved[fNumSaved][0] = transRand.nextRangeScalar(0.0f, maxTransX);
174 fSaved[fNumSaved][1] = transRand.nextRangeScalar(0.0f, maxTransY);
robertphillips@google.com347fd4e2013-05-15 14:18:32 +0000175 if (fAligned) {
176 // make the translations integer aligned
177 fSaved[fNumSaved][0] = SkScalarFloorToScalar(fSaved[fNumSaved][0]);
178 fSaved[fNumSaved][1] = SkScalarFloorToScalar(fSaved[fNumSaved][1]);
179 }
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000180
181 mat.setTranslate(fSaved[fNumSaved][0], fSaved[fNumSaved][1]);
182
183 if (kScale_Type == fType) {
184 fSaved[fNumSaved][2] = scaleRand.nextRangeScalar(0.5f, 1.5f);
185 mat.preScale(fSaved[fNumSaved][2], fSaved[fNumSaved][2]);
186 } else if (kRotate_Type == fType) {
187 fSaved[fNumSaved][2] = rotRand.nextRangeScalar(0.0f, 360.0f);
188 mat.preRotate(fSaved[fNumSaved][2]);
189 }
190
191 canvas->concat(mat);
skia.committer@gmail.com26da7f02013-06-01 07:01:39 +0000192 if (fUseAtlas) {
commit-bot@chromium.org44e3f712014-05-07 17:12:55 +0000193 const int curCell = i % (kNumAtlasedX * kNumAtlasedY);
robertphillips@google.com631a59b2013-07-31 14:57:53 +0000194 SkIRect src = fAtlasRects[curCell % (kNumAtlasedX)][curCell / (kNumAtlasedX)];
robertphillips@google.com631a59b2013-07-31 14:57:53 +0000195
196 if (fUseDrawVertices) {
skia.committer@gmail.com5d4b7732013-08-01 07:01:05 +0000197 SkPoint uvs[4] = {
robertphillips@google.com631a59b2013-07-31 14:57:53 +0000198 { SkIntToScalar(src.fLeft), SkIntToScalar(src.fBottom) },
199 { SkIntToScalar(src.fLeft), SkIntToScalar(src.fTop) },
200 { SkIntToScalar(src.fRight), SkIntToScalar(src.fTop) },
201 { SkIntToScalar(src.fRight), SkIntToScalar(src.fBottom) },
202 };
Mike Reed887cdf12017-04-03 11:11:09 -0400203 canvas->drawVertices(SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode,
204 4, verts, uvs, nullptr, 6, indices),
205 SkBlendMode::kModulate, p2);
robertphillips@google.com631a59b2013-07-31 14:57:53 +0000206 } else {
reed84984ef2015-07-17 07:09:43 -0700207 canvas->drawBitmapRect(fAtlas, src, dst, &p,
208 SkCanvas::kFast_SrcRectConstraint);
robertphillips@google.com631a59b2013-07-31 14:57:53 +0000209 }
robertphillips@google.comf865be32013-05-31 13:27:02 +0000210 } else {
reed84984ef2015-07-17 07:09:43 -0700211 canvas->drawBitmapRect(fCheckerboard, dst, &p);
robertphillips@google.comf865be32013-05-31 13:27:02 +0000212 }
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000213 }
214 }
215
216private:
217 static const int kCheckerboardWidth = 64;
218 static const int kCheckerboardHeight = 128;
robertphillips@google.comf865be32013-05-31 13:27:02 +0000219
220 static const int kAtlasCellWidth = 48;
221 static const int kAtlasCellHeight = 36;
222 static const int kNumAtlasedX = 5;
223 static const int kNumAtlasedY = 5;
224 static const int kAtlasSpacer = 2;
225 static const int kTotAtlasWidth = kNumAtlasedX * kAtlasCellWidth +
226 (kNumAtlasedX+1) * kAtlasSpacer;
227 static const int kTotAtlasHeight = kNumAtlasedY * kAtlasCellHeight +
228 (kNumAtlasedY+1) * kAtlasSpacer;
mtklein@google.comc2897432013-09-10 19:23:38 +0000229 static const int kNumBeforeClear = 100;
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000230
231 Type fType;
robertphillips@google.comf865be32013-05-31 13:27:02 +0000232 Clear fClear;
robertphillips@google.com347fd4e2013-05-15 14:18:32 +0000233 bool fAligned;
robertphillips@google.comf865be32013-05-31 13:27:02 +0000234 bool fUseAtlas;
robertphillips@google.com631a59b2013-07-31 14:57:53 +0000235 bool fUseDrawVertices;
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000236 SkString fName;
237 int fNumSaved; // num draws stored in 'fSaved'
238 bool fInitialized;
239
240 // 0 & 1 are always x & y translate. 2 is either scale or rotate.
241 SkScalar fSaved[kNumBeforeClear][3];
robertphillips@google.comf865be32013-05-31 13:27:02 +0000242
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000243 SkBitmap fCheckerboard;
robertphillips@google.comf865be32013-05-31 13:27:02 +0000244 SkBitmap fAtlas;
245 SkIRect fAtlasRects[kNumAtlasedX][kNumAtlasedY];
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000246
247 // Note: the resulting checker board has transparency
248 void makeCheckerboard() {
robertphillips@google.com6d58ad32013-05-09 19:08:57 +0000249 static int kCheckSize = 16;
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000250
reed6c225732014-06-09 19:52:07 -0700251 fCheckerboard.allocN32Pixels(kCheckerboardWidth, kCheckerboardHeight);
robertphillips@google.com6d58ad32013-05-09 19:08:57 +0000252 for (int y = 0; y < kCheckerboardHeight; ++y) {
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000253 int even = (y / kCheckSize) % 2;
254
255 SkPMColor* scanline = fCheckerboard.getAddr32(0, y);
256
robertphillips@google.com6d58ad32013-05-09 19:08:57 +0000257 for (int x = 0; x < kCheckerboardWidth; ++x) {
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000258 if (even == (x / kCheckSize) % 2) {
259 *scanline++ = 0xFFFF0000;
260 } else {
261 *scanline++ = 0x00000000;
262 }
263 }
264 }
265 }
266
robertphillips@google.comf865be32013-05-31 13:27:02 +0000267 // Note: the resulting atlas has transparency
268 void makeAtlas() {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000269 SkRandom rand;
robertphillips@google.comf865be32013-05-31 13:27:02 +0000270
271 SkColor colors[kNumAtlasedX][kNumAtlasedY];
272
273 for (int y = 0; y < kNumAtlasedY; ++y) {
274 for (int x = 0; x < kNumAtlasedX; ++x) {
275 colors[x][y] = rand.nextU() | 0xff000000;
276 fAtlasRects[x][y] = SkIRect::MakeXYWH(kAtlasSpacer + x * (kAtlasCellWidth + kAtlasSpacer),
277 kAtlasSpacer + y * (kAtlasCellHeight + kAtlasSpacer),
278 kAtlasCellWidth,
279 kAtlasCellHeight);
280 }
281 }
282
reed6c225732014-06-09 19:52:07 -0700283 fAtlas.allocN32Pixels(kTotAtlasWidth, kTotAtlasHeight);
robertphillips@google.comf865be32013-05-31 13:27:02 +0000284
285 for (int y = 0; y < kTotAtlasHeight; ++y) {
286 int colorY = y / (kAtlasCellHeight + kAtlasSpacer);
287 bool inColorY = (y % (kAtlasCellHeight + kAtlasSpacer)) >= kAtlasSpacer;
288
289 SkPMColor* scanline = fAtlas.getAddr32(0, y);
290
291 for (int x = 0; x < kTotAtlasWidth; ++x, ++scanline) {
292 int colorX = x / (kAtlasCellWidth + kAtlasSpacer);
293 bool inColorX = (x % (kAtlasCellWidth + kAtlasSpacer)) >= kAtlasSpacer;
294
295 if (inColorX && inColorY) {
296 SkASSERT(colorX < kNumAtlasedX && colorY < kNumAtlasedY);
297 *scanline = colors[colorX][colorY];
298 } else {
299 *scanline = 0x00000000;
300 }
301 }
302 }
303 }
304
tfarinaf168b862014-06-19 12:32:29 -0700305 typedef Benchmark INHERITED;
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000306};
307
308// Partial clear
halcanary385fe4d2015-08-26 13:07:48 -0700309DEF_BENCH(return new GameBench(GameBench::kScale_Type, GameBench::kPartial_Clear);)
310DEF_BENCH(return new GameBench(GameBench::kTranslate_Type, GameBench::kPartial_Clear);)
311DEF_BENCH(return new GameBench(GameBench::kTranslate_Type, GameBench::kPartial_Clear, true);)
312DEF_BENCH(return new GameBench(GameBench::kRotate_Type, GameBench::kPartial_Clear);)
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000313
314// Full clear
halcanary385fe4d2015-08-26 13:07:48 -0700315DEF_BENCH(return new GameBench(GameBench::kScale_Type, GameBench::kFull_Clear);)
316DEF_BENCH(return new GameBench(GameBench::kTranslate_Type, GameBench::kFull_Clear);)
317DEF_BENCH(return new GameBench(GameBench::kTranslate_Type, GameBench::kFull_Clear, true);)
318DEF_BENCH(return new GameBench(GameBench::kRotate_Type, GameBench::kFull_Clear);)
robertphillips@google.comf865be32013-05-31 13:27:02 +0000319
320// Atlased
halcanary385fe4d2015-08-26 13:07:48 -0700321DEF_BENCH(return new GameBench(GameBench::kTranslate_Type, GameBench::kFull_Clear, false, true);)
322DEF_BENCH(return new GameBench(
323 GameBench::kTranslate_Type, GameBench::kFull_Clear, false, true, true);)
Mike Reed77cf4302019-12-30 10:21:29 -0500324
325
326class CanvasMatrixBench : public Benchmark {
327 SkString fName;
328public:
329 enum Type {
330 kTranslate_Type,
331 kScale_Type,
332 k2x3_Type,
333 k3x3_Type,
Mike Reed064c7f92020-01-08 17:33:04 -0500334 k4x4_Type,
Mike Reed77cf4302019-12-30 10:21:29 -0500335 };
336 Type fType;
337
338 CanvasMatrixBench(Type t) : fType(t) {
339 fName.set("canvas_matrix");
340 switch (fType) {
341 case kTranslate_Type: fName.append("_trans"); break;
342 case kScale_Type: fName.append("_scale"); break;
343 case k2x3_Type: fName.append("_2x3"); break;
344 case k3x3_Type: fName.append("_3x3"); break;
Mike Reed064c7f92020-01-08 17:33:04 -0500345 case k4x4_Type: fName.append("_4x4"); break;
Mike Reed77cf4302019-12-30 10:21:29 -0500346 }
347 }
348
349protected:
350 const char* onGetName() override {
351 return fName.c_str();
352 }
353
354 void onDraw(int loops, SkCanvas* canvas) override {
355 SkMatrix m;
356 m.setRotate(1);
357 if (fType == k3x3_Type) {
358 m[7] = 0.0001f;
359 }
Mike Reed064c7f92020-01-08 17:33:04 -0500360 SkMatrix44 m4(m);
Mike Reed77cf4302019-12-30 10:21:29 -0500361
362 for (int i = 0; i < loops; ++i) {
363 canvas->save();
364 for (int j = 0; j < 10000; ++j) {
365 switch (fType) {
366 case kTranslate_Type: canvas->translate(0.0001f, 0.0001f); break;
367 case kScale_Type: canvas->scale(1.0001f, 0.9999f); break;
368 case k2x3_Type: canvas->concat(m); break;
369 case k3x3_Type: canvas->concat(m); break;
Mike Reed064c7f92020-01-08 17:33:04 -0500370 case k4x4_Type: canvas->concat(m4); break;
Mike Reed77cf4302019-12-30 10:21:29 -0500371 }
372 }
373 canvas->restore();
374 }
375 }
376
377private:
378 typedef Benchmark INHERITED;
379};
380
381DEF_BENCH(return new CanvasMatrixBench(CanvasMatrixBench::kTranslate_Type));
382DEF_BENCH(return new CanvasMatrixBench(CanvasMatrixBench::kScale_Type));
383DEF_BENCH(return new CanvasMatrixBench(CanvasMatrixBench::k2x3_Type));
384DEF_BENCH(return new CanvasMatrixBench(CanvasMatrixBench::k3x3_Type));
Mike Reed064c7f92020-01-08 17:33:04 -0500385DEF_BENCH(return new CanvasMatrixBench(CanvasMatrixBench::k4x4_Type));