blob: bd7d11465d0a1566f311e1656b4d50387d47b3ee [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
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
robertphillips@google.com6670ab92013-05-09 19:03:48 +00009#include "SkCanvas.h"
10#include "SkPaint.h"
11#include "SkRandom.h"
robertphillips@google.com631a59b2013-07-31 14:57:53 +000012#include "SkShader.h"
robertphillips@google.com6670ab92013-05-09 19:03:48 +000013#include "SkString.h"
Mike Reed887cdf12017-04-03 11:11:09 -040014#include "SkVertices.h"
robertphillips@google.com6670ab92013-05-09 19:03:48 +000015
robertphillips@google.com6670ab92013-05-09 19:03:48 +000016// This bench simulates the calls Skia sees from various HTML5 canvas
17// game bench marks
tfarinaf168b862014-06-19 12:32:29 -070018class GameBench : public Benchmark {
robertphillips@google.com6670ab92013-05-09 19:03:48 +000019public:
20 enum Type {
skia.committer@gmail.com0f20a3f2013-05-10 07:01:04 +000021 kScale_Type,
22 kTranslate_Type,
robertphillips@google.com6670ab92013-05-09 19:03:48 +000023 kRotate_Type
24 };
25
robertphillips@google.comf865be32013-05-31 13:27:02 +000026 enum Clear {
27 kFull_Clear,
28 kPartial_Clear
29 };
30
mtklein@google.com410e6e82013-09-13 19:52:27 +000031 GameBench(Type type, Clear clear,
robertphillips@google.com631a59b2013-07-31 14:57:53 +000032 bool aligned = false, bool useAtlas = false,
33 bool useDrawVertices = false)
mtklein@google.com410e6e82013-09-13 19:52:27 +000034 : fType(type)
robertphillips@google.comf865be32013-05-31 13:27:02 +000035 , fClear(clear)
robertphillips@google.com347fd4e2013-05-15 14:18:32 +000036 , fAligned(aligned)
robertphillips@google.comf865be32013-05-31 13:27:02 +000037 , fUseAtlas(useAtlas)
robertphillips@google.com631a59b2013-07-31 14:57:53 +000038 , fUseDrawVertices(useDrawVertices)
robertphillips@google.com6670ab92013-05-09 19:03:48 +000039 , fName("game")
40 , fNumSaved(0)
41 , fInitialized(false) {
42
43 switch (fType) {
44 case kScale_Type:
45 fName.append("_scale");
46 break;
47 case kTranslate_Type:
48 fName.append("_trans");
49 break;
50 case kRotate_Type:
51 fName.append("_rot");
52 break;
53 };
54
robertphillips@google.com347fd4e2013-05-15 14:18:32 +000055 if (aligned) {
56 fName.append("_aligned");
57 }
58
robertphillips@google.comf865be32013-05-31 13:27:02 +000059 if (kPartial_Clear == clear) {
robertphillips@google.com6670ab92013-05-09 19:03:48 +000060 fName.append("_partial");
61 } else {
62 fName.append("_full");
63 }
64
robertphillips@google.comf865be32013-05-31 13:27:02 +000065 if (useAtlas) {
66 fName.append("_atlas");
67 }
68
robertphillips@google.com631a59b2013-07-31 14:57:53 +000069 if (useDrawVertices) {
70 fName.append("_drawVerts");
71 }
72
robertphillips@google.com6670ab92013-05-09 19:03:48 +000073 // It's HTML 5 canvas, so always AA
74 fName.append("_aa");
75 }
76
77protected:
mtklein36352bf2015-03-25 18:17:31 -070078 const char* onGetName() override {
robertphillips@google.comf865be32013-05-31 13:27:02 +000079 return fName.c_str();
robertphillips@google.com6670ab92013-05-09 19:03:48 +000080 }
81
joshualitt8a6697a2015-09-30 12:11:07 -070082 void onDelayedSetup() override {
robertphillips@google.com6670ab92013-05-09 19:03:48 +000083 if (!fInitialized) {
84 this->makeCheckerboard();
robertphillips@google.comf865be32013-05-31 13:27:02 +000085 this->makeAtlas();
robertphillips@google.com6670ab92013-05-09 19:03:48 +000086 fInitialized = true;
87 }
88 }
89
mtkleina1ebeb22015-10-01 09:43:39 -070090 void onDraw(int loops, SkCanvas* canvas) override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000091 SkRandom scaleRand;
92 SkRandom transRand;
93 SkRandom rotRand;
robertphillips@google.com6670ab92013-05-09 19:03:48 +000094
robertphillips@google.comf865be32013-05-31 13:27:02 +000095 int width, height;
96 if (fUseAtlas) {
97 width = kAtlasCellWidth;
98 height = kAtlasCellHeight;
99 } else {
100 width = kCheckerboardWidth;
101 height = kCheckerboardHeight;
102 }
103
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000104 SkPaint clearPaint;
105 clearPaint.setColor(0xFF000000);
106 clearPaint.setAntiAlias(true);
107
Mike Reed3661bc92017-02-22 13:21:42 -0500108 SkISize size = canvas->getBaseLayerSize();
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000109
110 SkScalar maxTransX, maxTransY;
111
112 if (kScale_Type == fType) {
robertphillips@google.comf865be32013-05-31 13:27:02 +0000113 maxTransX = size.fWidth - (1.5f * width);
114 maxTransY = size.fHeight - (1.5f * height);
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000115 } else if (kTranslate_Type == fType) {
robertphillips@google.comf865be32013-05-31 13:27:02 +0000116 maxTransX = SkIntToScalar(size.fWidth - width);
117 maxTransY = SkIntToScalar(size.fHeight - height);
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000118 } else {
119 SkASSERT(kRotate_Type == fType);
120 // Yes, some rotations will be off the top and left sides
robertphillips@google.comf865be32013-05-31 13:27:02 +0000121 maxTransX = size.fWidth - SK_ScalarSqrt2 * height;
122 maxTransY = size.fHeight - SK_ScalarSqrt2 * height;
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000123 }
124
125 SkMatrix mat;
robertphillips@google.comf865be32013-05-31 13:27:02 +0000126 SkRect dst = { 0, 0, SkIntToScalar(width), SkIntToScalar(height) };
127 SkRect clearRect = { -1.0f, -1.0f, width+1.0f, height+1.0f };
robertphillips@google.com631a59b2013-07-31 14:57:53 +0000128 SkPoint verts[4] = { // for drawVertices path
129 { 0, 0 },
130 { 0, SkIntToScalar(height) },
131 { SkIntToScalar(width), SkIntToScalar(height) },
132 { SkIntToScalar(width), 0 }
133 };
134 uint16_t indices[6] = { 0, 1, 2, 0, 2, 3 };
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000135
136 SkPaint p;
137 p.setColor(0xFF000000);
reed93a12152015-03-16 10:08:34 -0700138 p.setFilterQuality(kLow_SkFilterQuality);
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000139
robertphillips@google.com631a59b2013-07-31 14:57:53 +0000140 SkPaint p2; // for drawVertices path
141 p2.setColor(0xFF000000);
reed93a12152015-03-16 10:08:34 -0700142 p2.setFilterQuality(kLow_SkFilterQuality);
reedc6f28f72016-03-14 12:22:10 -0700143 p2.setShader(SkShader::MakeBitmapShader(fAtlas,
144 SkShader::kClamp_TileMode,
145 SkShader::kClamp_TileMode));
robertphillips@google.com631a59b2013-07-31 14:57:53 +0000146
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000147 for (int i = 0; i < loops; ++i, ++fNumSaved) {
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000148 if (0 == i % kNumBeforeClear) {
robertphillips@google.comf865be32013-05-31 13:27:02 +0000149 if (kPartial_Clear == fClear) {
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000150 for (int j = 0; j < fNumSaved; ++j) {
151 canvas->setMatrix(SkMatrix::I());
152 mat.setTranslate(fSaved[j][0], fSaved[j][1]);
153
154 if (kScale_Type == fType) {
155 mat.preScale(fSaved[j][2], fSaved[j][2]);
156 } else if (kRotate_Type == fType) {
157 mat.preRotate(fSaved[j][2]);
158 }
159
160 canvas->concat(mat);
161 canvas->drawRect(clearRect, clearPaint);
162 }
163 } else {
164 canvas->clear(0xFF000000);
165 }
166
167 fNumSaved = 0;
168 }
169
170 SkASSERT(fNumSaved < kNumBeforeClear);
171
172 canvas->setMatrix(SkMatrix::I());
173
174 fSaved[fNumSaved][0] = transRand.nextRangeScalar(0.0f, maxTransX);
175 fSaved[fNumSaved][1] = transRand.nextRangeScalar(0.0f, maxTransY);
robertphillips@google.com347fd4e2013-05-15 14:18:32 +0000176 if (fAligned) {
177 // make the translations integer aligned
178 fSaved[fNumSaved][0] = SkScalarFloorToScalar(fSaved[fNumSaved][0]);
179 fSaved[fNumSaved][1] = SkScalarFloorToScalar(fSaved[fNumSaved][1]);
180 }
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000181
182 mat.setTranslate(fSaved[fNumSaved][0], fSaved[fNumSaved][1]);
183
184 if (kScale_Type == fType) {
185 fSaved[fNumSaved][2] = scaleRand.nextRangeScalar(0.5f, 1.5f);
186 mat.preScale(fSaved[fNumSaved][2], fSaved[fNumSaved][2]);
187 } else if (kRotate_Type == fType) {
188 fSaved[fNumSaved][2] = rotRand.nextRangeScalar(0.0f, 360.0f);
189 mat.preRotate(fSaved[fNumSaved][2]);
190 }
191
192 canvas->concat(mat);
skia.committer@gmail.com26da7f02013-06-01 07:01:39 +0000193 if (fUseAtlas) {
commit-bot@chromium.org44e3f712014-05-07 17:12:55 +0000194 const int curCell = i % (kNumAtlasedX * kNumAtlasedY);
robertphillips@google.com631a59b2013-07-31 14:57:53 +0000195 SkIRect src = fAtlasRects[curCell % (kNumAtlasedX)][curCell / (kNumAtlasedX)];
robertphillips@google.com631a59b2013-07-31 14:57:53 +0000196
197 if (fUseDrawVertices) {
skia.committer@gmail.com5d4b7732013-08-01 07:01:05 +0000198 SkPoint uvs[4] = {
robertphillips@google.com631a59b2013-07-31 14:57:53 +0000199 { SkIntToScalar(src.fLeft), SkIntToScalar(src.fBottom) },
200 { SkIntToScalar(src.fLeft), SkIntToScalar(src.fTop) },
201 { SkIntToScalar(src.fRight), SkIntToScalar(src.fTop) },
202 { SkIntToScalar(src.fRight), SkIntToScalar(src.fBottom) },
203 };
Mike Reed887cdf12017-04-03 11:11:09 -0400204 canvas->drawVertices(SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode,
205 4, verts, uvs, nullptr, 6, indices),
206 SkBlendMode::kModulate, p2);
robertphillips@google.com631a59b2013-07-31 14:57:53 +0000207 } else {
reed84984ef2015-07-17 07:09:43 -0700208 canvas->drawBitmapRect(fAtlas, src, dst, &p,
209 SkCanvas::kFast_SrcRectConstraint);
robertphillips@google.com631a59b2013-07-31 14:57:53 +0000210 }
robertphillips@google.comf865be32013-05-31 13:27:02 +0000211 } else {
reed84984ef2015-07-17 07:09:43 -0700212 canvas->drawBitmapRect(fCheckerboard, dst, &p);
robertphillips@google.comf865be32013-05-31 13:27:02 +0000213 }
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000214 }
215 }
216
217private:
218 static const int kCheckerboardWidth = 64;
219 static const int kCheckerboardHeight = 128;
robertphillips@google.comf865be32013-05-31 13:27:02 +0000220
221 static const int kAtlasCellWidth = 48;
222 static const int kAtlasCellHeight = 36;
223 static const int kNumAtlasedX = 5;
224 static const int kNumAtlasedY = 5;
225 static const int kAtlasSpacer = 2;
226 static const int kTotAtlasWidth = kNumAtlasedX * kAtlasCellWidth +
227 (kNumAtlasedX+1) * kAtlasSpacer;
228 static const int kTotAtlasHeight = kNumAtlasedY * kAtlasCellHeight +
229 (kNumAtlasedY+1) * kAtlasSpacer;
mtklein@google.comc2897432013-09-10 19:23:38 +0000230 static const int kNumBeforeClear = 100;
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000231
232 Type fType;
robertphillips@google.comf865be32013-05-31 13:27:02 +0000233 Clear fClear;
robertphillips@google.com347fd4e2013-05-15 14:18:32 +0000234 bool fAligned;
robertphillips@google.comf865be32013-05-31 13:27:02 +0000235 bool fUseAtlas;
robertphillips@google.com631a59b2013-07-31 14:57:53 +0000236 bool fUseDrawVertices;
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000237 SkString fName;
238 int fNumSaved; // num draws stored in 'fSaved'
239 bool fInitialized;
240
241 // 0 & 1 are always x & y translate. 2 is either scale or rotate.
242 SkScalar fSaved[kNumBeforeClear][3];
robertphillips@google.comf865be32013-05-31 13:27:02 +0000243
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000244 SkBitmap fCheckerboard;
robertphillips@google.comf865be32013-05-31 13:27:02 +0000245 SkBitmap fAtlas;
246 SkIRect fAtlasRects[kNumAtlasedX][kNumAtlasedY];
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000247
248 // Note: the resulting checker board has transparency
249 void makeCheckerboard() {
robertphillips@google.com6d58ad32013-05-09 19:08:57 +0000250 static int kCheckSize = 16;
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000251
reed6c225732014-06-09 19:52:07 -0700252 fCheckerboard.allocN32Pixels(kCheckerboardWidth, kCheckerboardHeight);
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000253 SkAutoLockPixels lock(fCheckerboard);
robertphillips@google.com6d58ad32013-05-09 19:08:57 +0000254 for (int y = 0; y < kCheckerboardHeight; ++y) {
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000255 int even = (y / kCheckSize) % 2;
256
257 SkPMColor* scanline = fCheckerboard.getAddr32(0, y);
258
robertphillips@google.com6d58ad32013-05-09 19:08:57 +0000259 for (int x = 0; x < kCheckerboardWidth; ++x) {
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000260 if (even == (x / kCheckSize) % 2) {
261 *scanline++ = 0xFFFF0000;
262 } else {
263 *scanline++ = 0x00000000;
264 }
265 }
266 }
267 }
268
robertphillips@google.comf865be32013-05-31 13:27:02 +0000269 // Note: the resulting atlas has transparency
270 void makeAtlas() {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000271 SkRandom rand;
robertphillips@google.comf865be32013-05-31 13:27:02 +0000272
273 SkColor colors[kNumAtlasedX][kNumAtlasedY];
274
275 for (int y = 0; y < kNumAtlasedY; ++y) {
276 for (int x = 0; x < kNumAtlasedX; ++x) {
277 colors[x][y] = rand.nextU() | 0xff000000;
278 fAtlasRects[x][y] = SkIRect::MakeXYWH(kAtlasSpacer + x * (kAtlasCellWidth + kAtlasSpacer),
279 kAtlasSpacer + y * (kAtlasCellHeight + kAtlasSpacer),
280 kAtlasCellWidth,
281 kAtlasCellHeight);
282 }
283 }
284
reed6c225732014-06-09 19:52:07 -0700285 fAtlas.allocN32Pixels(kTotAtlasWidth, kTotAtlasHeight);
robertphillips@google.comf865be32013-05-31 13:27:02 +0000286 SkAutoLockPixels lock(fAtlas);
287
288 for (int y = 0; y < kTotAtlasHeight; ++y) {
289 int colorY = y / (kAtlasCellHeight + kAtlasSpacer);
290 bool inColorY = (y % (kAtlasCellHeight + kAtlasSpacer)) >= kAtlasSpacer;
291
292 SkPMColor* scanline = fAtlas.getAddr32(0, y);
293
294 for (int x = 0; x < kTotAtlasWidth; ++x, ++scanline) {
295 int colorX = x / (kAtlasCellWidth + kAtlasSpacer);
296 bool inColorX = (x % (kAtlasCellWidth + kAtlasSpacer)) >= kAtlasSpacer;
297
298 if (inColorX && inColorY) {
299 SkASSERT(colorX < kNumAtlasedX && colorY < kNumAtlasedY);
300 *scanline = colors[colorX][colorY];
301 } else {
302 *scanline = 0x00000000;
303 }
304 }
305 }
306 }
307
tfarinaf168b862014-06-19 12:32:29 -0700308 typedef Benchmark INHERITED;
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000309};
310
311// Partial clear
halcanary385fe4d2015-08-26 13:07:48 -0700312DEF_BENCH(return new GameBench(GameBench::kScale_Type, GameBench::kPartial_Clear);)
313DEF_BENCH(return new GameBench(GameBench::kTranslate_Type, GameBench::kPartial_Clear);)
314DEF_BENCH(return new GameBench(GameBench::kTranslate_Type, GameBench::kPartial_Clear, true);)
315DEF_BENCH(return new GameBench(GameBench::kRotate_Type, GameBench::kPartial_Clear);)
robertphillips@google.com6670ab92013-05-09 19:03:48 +0000316
317// Full clear
halcanary385fe4d2015-08-26 13:07:48 -0700318DEF_BENCH(return new GameBench(GameBench::kScale_Type, GameBench::kFull_Clear);)
319DEF_BENCH(return new GameBench(GameBench::kTranslate_Type, GameBench::kFull_Clear);)
320DEF_BENCH(return new GameBench(GameBench::kTranslate_Type, GameBench::kFull_Clear, true);)
321DEF_BENCH(return new GameBench(GameBench::kRotate_Type, GameBench::kFull_Clear);)
robertphillips@google.comf865be32013-05-31 13:27:02 +0000322
323// Atlased
halcanary385fe4d2015-08-26 13:07:48 -0700324DEF_BENCH(return new GameBench(GameBench::kTranslate_Type, GameBench::kFull_Clear, false, true);)
325DEF_BENCH(return new GameBench(
326 GameBench::kTranslate_Type, GameBench::kFull_Clear, false, true, true);)