2013-05-09 19:03:48 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "bench/Benchmark.h"
|
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkVertices.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
2013-05-09 19:03:48 +00:00
|
|
|
|
|
|
|
// This bench simulates the calls Skia sees from various HTML5 canvas
|
|
|
|
// game bench marks
|
2014-06-19 19:32:29 +00:00
|
|
|
class GameBench : public Benchmark {
|
2013-05-09 19:03:48 +00:00
|
|
|
public:
|
|
|
|
enum Type {
|
2013-05-10 07:01:04 +00:00
|
|
|
kScale_Type,
|
|
|
|
kTranslate_Type,
|
2013-05-09 19:03:48 +00:00
|
|
|
kRotate_Type
|
|
|
|
};
|
|
|
|
|
2013-05-31 13:27:02 +00:00
|
|
|
enum Clear {
|
|
|
|
kFull_Clear,
|
|
|
|
kPartial_Clear
|
|
|
|
};
|
|
|
|
|
2013-09-13 19:52:27 +00:00
|
|
|
GameBench(Type type, Clear clear,
|
2013-07-31 14:57:53 +00:00
|
|
|
bool aligned = false, bool useAtlas = false,
|
|
|
|
bool useDrawVertices = false)
|
2013-09-13 19:52:27 +00:00
|
|
|
: fType(type)
|
2013-05-31 13:27:02 +00:00
|
|
|
, fClear(clear)
|
2013-05-15 14:18:32 +00:00
|
|
|
, fAligned(aligned)
|
2013-05-31 13:27:02 +00:00
|
|
|
, fUseAtlas(useAtlas)
|
2013-07-31 14:57:53 +00:00
|
|
|
, fUseDrawVertices(useDrawVertices)
|
2013-05-09 19:03:48 +00:00
|
|
|
, fName("game")
|
|
|
|
, fNumSaved(0)
|
|
|
|
, fInitialized(false) {
|
|
|
|
|
|
|
|
switch (fType) {
|
|
|
|
case kScale_Type:
|
|
|
|
fName.append("_scale");
|
|
|
|
break;
|
|
|
|
case kTranslate_Type:
|
|
|
|
fName.append("_trans");
|
|
|
|
break;
|
|
|
|
case kRotate_Type:
|
|
|
|
fName.append("_rot");
|
|
|
|
break;
|
2018-11-30 20:33:19 +00:00
|
|
|
}
|
2013-05-09 19:03:48 +00:00
|
|
|
|
2013-05-15 14:18:32 +00:00
|
|
|
if (aligned) {
|
|
|
|
fName.append("_aligned");
|
|
|
|
}
|
|
|
|
|
2013-05-31 13:27:02 +00:00
|
|
|
if (kPartial_Clear == clear) {
|
2013-05-09 19:03:48 +00:00
|
|
|
fName.append("_partial");
|
|
|
|
} else {
|
|
|
|
fName.append("_full");
|
|
|
|
}
|
|
|
|
|
2013-05-31 13:27:02 +00:00
|
|
|
if (useAtlas) {
|
|
|
|
fName.append("_atlas");
|
|
|
|
}
|
|
|
|
|
2013-07-31 14:57:53 +00:00
|
|
|
if (useDrawVertices) {
|
|
|
|
fName.append("_drawVerts");
|
|
|
|
}
|
|
|
|
|
2013-05-09 19:03:48 +00:00
|
|
|
// It's HTML 5 canvas, so always AA
|
|
|
|
fName.append("_aa");
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
const char* onGetName() override {
|
2013-05-31 13:27:02 +00:00
|
|
|
return fName.c_str();
|
2013-05-09 19:03:48 +00:00
|
|
|
}
|
|
|
|
|
2015-09-30 19:11:07 +00:00
|
|
|
void onDelayedSetup() override {
|
2013-05-09 19:03:48 +00:00
|
|
|
if (!fInitialized) {
|
|
|
|
this->makeCheckerboard();
|
2013-05-31 13:27:02 +00:00
|
|
|
this->makeAtlas();
|
2013-05-09 19:03:48 +00:00
|
|
|
fInitialized = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-01 16:43:39 +00:00
|
|
|
void onDraw(int loops, SkCanvas* canvas) override {
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom scaleRand;
|
|
|
|
SkRandom transRand;
|
|
|
|
SkRandom rotRand;
|
2013-05-09 19:03:48 +00:00
|
|
|
|
2013-05-31 13:27:02 +00:00
|
|
|
int width, height;
|
|
|
|
if (fUseAtlas) {
|
|
|
|
width = kAtlasCellWidth;
|
|
|
|
height = kAtlasCellHeight;
|
|
|
|
} else {
|
|
|
|
width = kCheckerboardWidth;
|
|
|
|
height = kCheckerboardHeight;
|
|
|
|
}
|
|
|
|
|
2013-05-09 19:03:48 +00:00
|
|
|
SkPaint clearPaint;
|
|
|
|
clearPaint.setColor(0xFF000000);
|
|
|
|
clearPaint.setAntiAlias(true);
|
|
|
|
|
2017-02-22 18:21:42 +00:00
|
|
|
SkISize size = canvas->getBaseLayerSize();
|
2013-05-09 19:03:48 +00:00
|
|
|
|
|
|
|
SkScalar maxTransX, maxTransY;
|
|
|
|
|
|
|
|
if (kScale_Type == fType) {
|
2013-05-31 13:27:02 +00:00
|
|
|
maxTransX = size.fWidth - (1.5f * width);
|
|
|
|
maxTransY = size.fHeight - (1.5f * height);
|
2013-05-09 19:03:48 +00:00
|
|
|
} else if (kTranslate_Type == fType) {
|
2013-05-31 13:27:02 +00:00
|
|
|
maxTransX = SkIntToScalar(size.fWidth - width);
|
|
|
|
maxTransY = SkIntToScalar(size.fHeight - height);
|
2013-05-09 19:03:48 +00:00
|
|
|
} else {
|
|
|
|
SkASSERT(kRotate_Type == fType);
|
|
|
|
// Yes, some rotations will be off the top and left sides
|
2013-05-31 13:27:02 +00:00
|
|
|
maxTransX = size.fWidth - SK_ScalarSqrt2 * height;
|
|
|
|
maxTransY = size.fHeight - SK_ScalarSqrt2 * height;
|
2013-05-09 19:03:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkMatrix mat;
|
2013-05-31 13:27:02 +00:00
|
|
|
SkRect dst = { 0, 0, SkIntToScalar(width), SkIntToScalar(height) };
|
|
|
|
SkRect clearRect = { -1.0f, -1.0f, width+1.0f, height+1.0f };
|
2013-07-31 14:57:53 +00:00
|
|
|
SkPoint verts[4] = { // for drawVertices path
|
|
|
|
{ 0, 0 },
|
|
|
|
{ 0, SkIntToScalar(height) },
|
|
|
|
{ SkIntToScalar(width), SkIntToScalar(height) },
|
|
|
|
{ SkIntToScalar(width), 0 }
|
|
|
|
};
|
|
|
|
uint16_t indices[6] = { 0, 1, 2, 0, 2, 3 };
|
2013-05-09 19:03:48 +00:00
|
|
|
|
|
|
|
SkPaint p;
|
|
|
|
p.setColor(0xFF000000);
|
2015-03-16 17:08:34 +00:00
|
|
|
p.setFilterQuality(kLow_SkFilterQuality);
|
2013-05-09 19:03:48 +00:00
|
|
|
|
2013-07-31 14:57:53 +00:00
|
|
|
SkPaint p2; // for drawVertices path
|
|
|
|
p2.setColor(0xFF000000);
|
2015-03-16 17:08:34 +00:00
|
|
|
p2.setFilterQuality(kLow_SkFilterQuality);
|
2019-04-08 17:20:23 +00:00
|
|
|
p2.setShader(fAtlas.makeShader());
|
2013-07-31 14:57:53 +00:00
|
|
|
|
2013-12-03 18:17:16 +00:00
|
|
|
for (int i = 0; i < loops; ++i, ++fNumSaved) {
|
2013-05-09 19:03:48 +00:00
|
|
|
if (0 == i % kNumBeforeClear) {
|
2013-05-31 13:27:02 +00:00
|
|
|
if (kPartial_Clear == fClear) {
|
2013-05-09 19:03:48 +00:00
|
|
|
for (int j = 0; j < fNumSaved; ++j) {
|
|
|
|
canvas->setMatrix(SkMatrix::I());
|
|
|
|
mat.setTranslate(fSaved[j][0], fSaved[j][1]);
|
|
|
|
|
|
|
|
if (kScale_Type == fType) {
|
|
|
|
mat.preScale(fSaved[j][2], fSaved[j][2]);
|
|
|
|
} else if (kRotate_Type == fType) {
|
|
|
|
mat.preRotate(fSaved[j][2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas->concat(mat);
|
|
|
|
canvas->drawRect(clearRect, clearPaint);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
canvas->clear(0xFF000000);
|
|
|
|
}
|
|
|
|
|
|
|
|
fNumSaved = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkASSERT(fNumSaved < kNumBeforeClear);
|
|
|
|
|
|
|
|
canvas->setMatrix(SkMatrix::I());
|
|
|
|
|
|
|
|
fSaved[fNumSaved][0] = transRand.nextRangeScalar(0.0f, maxTransX);
|
|
|
|
fSaved[fNumSaved][1] = transRand.nextRangeScalar(0.0f, maxTransY);
|
2013-05-15 14:18:32 +00:00
|
|
|
if (fAligned) {
|
|
|
|
// make the translations integer aligned
|
|
|
|
fSaved[fNumSaved][0] = SkScalarFloorToScalar(fSaved[fNumSaved][0]);
|
|
|
|
fSaved[fNumSaved][1] = SkScalarFloorToScalar(fSaved[fNumSaved][1]);
|
|
|
|
}
|
2013-05-09 19:03:48 +00:00
|
|
|
|
|
|
|
mat.setTranslate(fSaved[fNumSaved][0], fSaved[fNumSaved][1]);
|
|
|
|
|
|
|
|
if (kScale_Type == fType) {
|
|
|
|
fSaved[fNumSaved][2] = scaleRand.nextRangeScalar(0.5f, 1.5f);
|
|
|
|
mat.preScale(fSaved[fNumSaved][2], fSaved[fNumSaved][2]);
|
|
|
|
} else if (kRotate_Type == fType) {
|
|
|
|
fSaved[fNumSaved][2] = rotRand.nextRangeScalar(0.0f, 360.0f);
|
|
|
|
mat.preRotate(fSaved[fNumSaved][2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas->concat(mat);
|
2013-06-01 07:01:39 +00:00
|
|
|
if (fUseAtlas) {
|
2014-05-07 17:12:55 +00:00
|
|
|
const int curCell = i % (kNumAtlasedX * kNumAtlasedY);
|
2013-07-31 14:57:53 +00:00
|
|
|
SkIRect src = fAtlasRects[curCell % (kNumAtlasedX)][curCell / (kNumAtlasedX)];
|
|
|
|
|
|
|
|
if (fUseDrawVertices) {
|
2013-08-01 07:01:05 +00:00
|
|
|
SkPoint uvs[4] = {
|
2013-07-31 14:57:53 +00:00
|
|
|
{ SkIntToScalar(src.fLeft), SkIntToScalar(src.fBottom) },
|
|
|
|
{ SkIntToScalar(src.fLeft), SkIntToScalar(src.fTop) },
|
|
|
|
{ SkIntToScalar(src.fRight), SkIntToScalar(src.fTop) },
|
|
|
|
{ SkIntToScalar(src.fRight), SkIntToScalar(src.fBottom) },
|
|
|
|
};
|
2017-04-03 15:11:09 +00:00
|
|
|
canvas->drawVertices(SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode,
|
|
|
|
4, verts, uvs, nullptr, 6, indices),
|
|
|
|
SkBlendMode::kModulate, p2);
|
2013-07-31 14:57:53 +00:00
|
|
|
} else {
|
2015-07-17 14:09:43 +00:00
|
|
|
canvas->drawBitmapRect(fAtlas, src, dst, &p,
|
|
|
|
SkCanvas::kFast_SrcRectConstraint);
|
2013-07-31 14:57:53 +00:00
|
|
|
}
|
2013-05-31 13:27:02 +00:00
|
|
|
} else {
|
2015-07-17 14:09:43 +00:00
|
|
|
canvas->drawBitmapRect(fCheckerboard, dst, &p);
|
2013-05-31 13:27:02 +00:00
|
|
|
}
|
2013-05-09 19:03:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static const int kCheckerboardWidth = 64;
|
|
|
|
static const int kCheckerboardHeight = 128;
|
2013-05-31 13:27:02 +00:00
|
|
|
|
|
|
|
static const int kAtlasCellWidth = 48;
|
|
|
|
static const int kAtlasCellHeight = 36;
|
|
|
|
static const int kNumAtlasedX = 5;
|
|
|
|
static const int kNumAtlasedY = 5;
|
|
|
|
static const int kAtlasSpacer = 2;
|
|
|
|
static const int kTotAtlasWidth = kNumAtlasedX * kAtlasCellWidth +
|
|
|
|
(kNumAtlasedX+1) * kAtlasSpacer;
|
|
|
|
static const int kTotAtlasHeight = kNumAtlasedY * kAtlasCellHeight +
|
|
|
|
(kNumAtlasedY+1) * kAtlasSpacer;
|
2013-09-10 19:23:38 +00:00
|
|
|
static const int kNumBeforeClear = 100;
|
2013-05-09 19:03:48 +00:00
|
|
|
|
|
|
|
Type fType;
|
2013-05-31 13:27:02 +00:00
|
|
|
Clear fClear;
|
2013-05-15 14:18:32 +00:00
|
|
|
bool fAligned;
|
2013-05-31 13:27:02 +00:00
|
|
|
bool fUseAtlas;
|
2013-07-31 14:57:53 +00:00
|
|
|
bool fUseDrawVertices;
|
2013-05-09 19:03:48 +00:00
|
|
|
SkString fName;
|
|
|
|
int fNumSaved; // num draws stored in 'fSaved'
|
|
|
|
bool fInitialized;
|
|
|
|
|
|
|
|
// 0 & 1 are always x & y translate. 2 is either scale or rotate.
|
|
|
|
SkScalar fSaved[kNumBeforeClear][3];
|
2013-05-31 13:27:02 +00:00
|
|
|
|
2013-05-09 19:03:48 +00:00
|
|
|
SkBitmap fCheckerboard;
|
2013-05-31 13:27:02 +00:00
|
|
|
SkBitmap fAtlas;
|
|
|
|
SkIRect fAtlasRects[kNumAtlasedX][kNumAtlasedY];
|
2013-05-09 19:03:48 +00:00
|
|
|
|
|
|
|
// Note: the resulting checker board has transparency
|
|
|
|
void makeCheckerboard() {
|
2013-05-09 19:08:57 +00:00
|
|
|
static int kCheckSize = 16;
|
2013-05-09 19:03:48 +00:00
|
|
|
|
2014-06-10 02:52:07 +00:00
|
|
|
fCheckerboard.allocN32Pixels(kCheckerboardWidth, kCheckerboardHeight);
|
2013-05-09 19:08:57 +00:00
|
|
|
for (int y = 0; y < kCheckerboardHeight; ++y) {
|
2013-05-09 19:03:48 +00:00
|
|
|
int even = (y / kCheckSize) % 2;
|
|
|
|
|
|
|
|
SkPMColor* scanline = fCheckerboard.getAddr32(0, y);
|
|
|
|
|
2013-05-09 19:08:57 +00:00
|
|
|
for (int x = 0; x < kCheckerboardWidth; ++x) {
|
2013-05-09 19:03:48 +00:00
|
|
|
if (even == (x / kCheckSize) % 2) {
|
|
|
|
*scanline++ = 0xFFFF0000;
|
|
|
|
} else {
|
|
|
|
*scanline++ = 0x00000000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 13:27:02 +00:00
|
|
|
// Note: the resulting atlas has transparency
|
|
|
|
void makeAtlas() {
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2013-05-31 13:27:02 +00:00
|
|
|
|
|
|
|
SkColor colors[kNumAtlasedX][kNumAtlasedY];
|
|
|
|
|
|
|
|
for (int y = 0; y < kNumAtlasedY; ++y) {
|
|
|
|
for (int x = 0; x < kNumAtlasedX; ++x) {
|
|
|
|
colors[x][y] = rand.nextU() | 0xff000000;
|
|
|
|
fAtlasRects[x][y] = SkIRect::MakeXYWH(kAtlasSpacer + x * (kAtlasCellWidth + kAtlasSpacer),
|
|
|
|
kAtlasSpacer + y * (kAtlasCellHeight + kAtlasSpacer),
|
|
|
|
kAtlasCellWidth,
|
|
|
|
kAtlasCellHeight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:52:07 +00:00
|
|
|
fAtlas.allocN32Pixels(kTotAtlasWidth, kTotAtlasHeight);
|
2013-05-31 13:27:02 +00:00
|
|
|
|
|
|
|
for (int y = 0; y < kTotAtlasHeight; ++y) {
|
|
|
|
int colorY = y / (kAtlasCellHeight + kAtlasSpacer);
|
|
|
|
bool inColorY = (y % (kAtlasCellHeight + kAtlasSpacer)) >= kAtlasSpacer;
|
|
|
|
|
|
|
|
SkPMColor* scanline = fAtlas.getAddr32(0, y);
|
|
|
|
|
|
|
|
for (int x = 0; x < kTotAtlasWidth; ++x, ++scanline) {
|
|
|
|
int colorX = x / (kAtlasCellWidth + kAtlasSpacer);
|
|
|
|
bool inColorX = (x % (kAtlasCellWidth + kAtlasSpacer)) >= kAtlasSpacer;
|
|
|
|
|
|
|
|
if (inColorX && inColorY) {
|
|
|
|
SkASSERT(colorX < kNumAtlasedX && colorY < kNumAtlasedY);
|
|
|
|
*scanline = colors[colorX][colorY];
|
|
|
|
} else {
|
|
|
|
*scanline = 0x00000000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
typedef Benchmark INHERITED;
|
2013-05-09 19:03:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Partial clear
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_BENCH(return new GameBench(GameBench::kScale_Type, GameBench::kPartial_Clear);)
|
|
|
|
DEF_BENCH(return new GameBench(GameBench::kTranslate_Type, GameBench::kPartial_Clear);)
|
|
|
|
DEF_BENCH(return new GameBench(GameBench::kTranslate_Type, GameBench::kPartial_Clear, true);)
|
|
|
|
DEF_BENCH(return new GameBench(GameBench::kRotate_Type, GameBench::kPartial_Clear);)
|
2013-05-09 19:03:48 +00:00
|
|
|
|
|
|
|
// Full clear
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_BENCH(return new GameBench(GameBench::kScale_Type, GameBench::kFull_Clear);)
|
|
|
|
DEF_BENCH(return new GameBench(GameBench::kTranslate_Type, GameBench::kFull_Clear);)
|
|
|
|
DEF_BENCH(return new GameBench(GameBench::kTranslate_Type, GameBench::kFull_Clear, true);)
|
|
|
|
DEF_BENCH(return new GameBench(GameBench::kRotate_Type, GameBench::kFull_Clear);)
|
2013-05-31 13:27:02 +00:00
|
|
|
|
|
|
|
// Atlased
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_BENCH(return new GameBench(GameBench::kTranslate_Type, GameBench::kFull_Clear, false, true);)
|
|
|
|
DEF_BENCH(return new GameBench(
|
|
|
|
GameBench::kTranslate_Type, GameBench::kFull_Clear, false, true, true);)
|
2019-12-30 15:21:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CanvasMatrixBench : public Benchmark {
|
|
|
|
SkString fName;
|
|
|
|
public:
|
|
|
|
enum Type {
|
|
|
|
kTranslate_Type,
|
|
|
|
kScale_Type,
|
|
|
|
k2x3_Type,
|
|
|
|
k3x3_Type,
|
|
|
|
};
|
|
|
|
Type fType;
|
|
|
|
|
|
|
|
CanvasMatrixBench(Type t) : fType(t) {
|
|
|
|
fName.set("canvas_matrix");
|
|
|
|
switch (fType) {
|
|
|
|
case kTranslate_Type: fName.append("_trans"); break;
|
|
|
|
case kScale_Type: fName.append("_scale"); break;
|
|
|
|
case k2x3_Type: fName.append("_2x3"); break;
|
|
|
|
case k3x3_Type: fName.append("_3x3"); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const char* onGetName() override {
|
|
|
|
return fName.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(int loops, SkCanvas* canvas) override {
|
|
|
|
SkMatrix m;
|
|
|
|
m.setRotate(1);
|
|
|
|
if (fType == k3x3_Type) {
|
|
|
|
m[7] = 0.0001f;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < loops; ++i) {
|
|
|
|
canvas->save();
|
|
|
|
for (int j = 0; j < 10000; ++j) {
|
|
|
|
switch (fType) {
|
|
|
|
case kTranslate_Type: canvas->translate(0.0001f, 0.0001f); break;
|
|
|
|
case kScale_Type: canvas->scale(1.0001f, 0.9999f); break;
|
|
|
|
case k2x3_Type: canvas->concat(m); break;
|
|
|
|
case k3x3_Type: canvas->concat(m); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef Benchmark INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
DEF_BENCH(return new CanvasMatrixBench(CanvasMatrixBench::kTranslate_Type));
|
|
|
|
DEF_BENCH(return new CanvasMatrixBench(CanvasMatrixBench::kScale_Type));
|
|
|
|
DEF_BENCH(return new CanvasMatrixBench(CanvasMatrixBench::k2x3_Type));
|
|
|
|
DEF_BENCH(return new CanvasMatrixBench(CanvasMatrixBench::k3x3_Type));
|