2011-10-28 18:57:32 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 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/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"
|
2019-08-21 14:31:02 +00:00
|
|
|
#include "tools/Resources.h"
|
2011-10-28 18:57:32 +00:00
|
|
|
|
|
|
|
enum VertFlags {
|
2019-08-21 14:31:02 +00:00
|
|
|
kColors_VertFlag = 1 << 0,
|
|
|
|
kTexture_VertFlag = 1 << 1,
|
2011-10-28 18:57:32 +00:00
|
|
|
};
|
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
class VertBench : public Benchmark {
|
2011-10-28 18:57:32 +00:00
|
|
|
SkString fName;
|
|
|
|
enum {
|
2019-08-21 14:31:02 +00:00
|
|
|
W = 64*2,
|
|
|
|
H = 48*2,
|
2011-10-28 18:57:32 +00:00
|
|
|
ROW = 20,
|
|
|
|
COL = 20,
|
|
|
|
PTS = (ROW + 1) * (COL + 1),
|
|
|
|
IDX = ROW * COL * 6,
|
|
|
|
};
|
|
|
|
|
2019-08-21 14:31:02 +00:00
|
|
|
sk_sp<SkShader> fShader;
|
2011-10-28 18:57:32 +00:00
|
|
|
SkPoint fPts[PTS];
|
|
|
|
SkColor fColors[PTS];
|
|
|
|
uint16_t fIdx[IDX];
|
2019-08-21 14:31:02 +00:00
|
|
|
unsigned fFlags;
|
2011-10-28 18:57:32 +00:00
|
|
|
|
|
|
|
static void load_2_tris(uint16_t idx[], int x, int y, int rb) {
|
|
|
|
int n = y * rb + x;
|
|
|
|
idx[0] = n; idx[1] = n + 1; idx[2] = rb + n + 1;
|
|
|
|
idx[3] = n; idx[4] = rb + n + 1; idx[5] = n + rb;
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2019-08-21 14:31:02 +00:00
|
|
|
void onDelayedSetup() override {
|
|
|
|
auto img = GetResourceAsImage("images/mandrill_256.png");
|
|
|
|
if (img) {
|
|
|
|
fShader = img->makeShader();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-28 18:57:32 +00:00
|
|
|
public:
|
2019-08-21 14:31:02 +00:00
|
|
|
VertBench(unsigned flags) : fFlags(flags) {
|
2011-10-28 18:57:32 +00:00
|
|
|
const SkScalar dx = SkIntToScalar(W) / COL;
|
|
|
|
const SkScalar dy = SkIntToScalar(H) / COL;
|
|
|
|
|
|
|
|
SkPoint* pts = fPts;
|
|
|
|
uint16_t* idx = fIdx;
|
|
|
|
|
|
|
|
SkScalar yy = 0;
|
|
|
|
for (int y = 0; y <= ROW; y++) {
|
|
|
|
SkScalar xx = 0;
|
|
|
|
for (int x = 0; x <= COL; ++x) {
|
|
|
|
pts->set(xx, yy);
|
|
|
|
pts += 1;
|
|
|
|
xx += dx;
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2011-10-28 18:57:32 +00:00
|
|
|
if (x < COL && y < ROW) {
|
|
|
|
load_2_tris(idx, x, y, COL + 1);
|
|
|
|
for (int i = 0; i < 6; i++) {
|
|
|
|
SkASSERT(idx[i] < PTS);
|
|
|
|
}
|
|
|
|
idx += 6;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
yy += dy;
|
|
|
|
}
|
|
|
|
SkASSERT(PTS == pts - fPts);
|
|
|
|
SkASSERT(IDX == idx - fIdx);
|
|
|
|
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2011-10-28 18:57:32 +00:00
|
|
|
for (int i = 0; i < PTS; ++i) {
|
|
|
|
fColors[i] = rand.nextU() | (0xFF << 24);
|
|
|
|
}
|
|
|
|
|
|
|
|
fName.set("verts");
|
2019-08-21 14:31:02 +00:00
|
|
|
if (fFlags & kTexture_VertFlag) {
|
|
|
|
fName.append("_textures");
|
|
|
|
}
|
|
|
|
if (fFlags & kColors_VertFlag) {
|
|
|
|
fName.append("_colors");
|
|
|
|
}
|
2011-10-28 18:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2017-04-03 15:11:09 +00:00
|
|
|
const char* onGetName() override { return fName.c_str(); }
|
|
|
|
void onDraw(int loops, SkCanvas* canvas) override {
|
2011-10-28 18:57:32 +00:00
|
|
|
SkPaint paint;
|
|
|
|
this->setupPaint(&paint);
|
2019-08-21 14:31:02 +00:00
|
|
|
paint.setShader(fShader);
|
2011-10-28 19:50:21 +00:00
|
|
|
|
2019-08-21 14:31:02 +00:00
|
|
|
const SkPoint* texs = (fFlags & kTexture_VertFlag) ? fPts : nullptr;
|
|
|
|
const SkColor* cols = (fFlags & kColors_VertFlag) ? fColors : nullptr;
|
2017-04-03 15:11:09 +00:00
|
|
|
auto verts = SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode, PTS,
|
2019-08-21 14:31:02 +00:00
|
|
|
fPts, texs, cols, IDX, fIdx);
|
2013-12-03 18:17:16 +00:00
|
|
|
for (int i = 0; i < loops; i++) {
|
2017-04-03 15:11:09 +00:00
|
|
|
canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
|
2011-10-28 18:57:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
private:
|
2014-06-19 19:32:29 +00:00
|
|
|
typedef Benchmark INHERITED;
|
2011-10-28 18:57:32 +00:00
|
|
|
};
|
2019-08-21 14:31:02 +00:00
|
|
|
DEF_BENCH(return new VertBench(kTexture_VertFlag);)
|
|
|
|
DEF_BENCH(return new VertBench(kColors_VertFlag);)
|
|
|
|
DEF_BENCH(return new VertBench(kColors_VertFlag | kTexture_VertFlag);)
|
2011-10-28 18:57:32 +00:00
|
|
|
|
2019-03-22 19:57:45 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkRSXform.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
#include "tools/Resources.h"
|
2019-03-22 19:57:45 +00:00
|
|
|
|
|
|
|
enum AtlasFlags {
|
|
|
|
kColors_Flag = 1 << 0,
|
2019-08-12 12:49:36 +00:00
|
|
|
kRotate_Flag = 1 << 1,
|
2019-03-22 19:57:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class AtlasBench : public Benchmark {
|
|
|
|
unsigned fFlags;
|
|
|
|
SkString fName;
|
|
|
|
enum {
|
|
|
|
W = 640,
|
|
|
|
H = 480,
|
|
|
|
N = 10*1000,
|
|
|
|
};
|
|
|
|
|
|
|
|
sk_sp<SkImage> fAtlas;
|
|
|
|
SkRSXform fXforms[N];
|
|
|
|
SkRect fRects[N];
|
|
|
|
SkColor fColors[N];
|
|
|
|
|
|
|
|
public:
|
|
|
|
AtlasBench(unsigned flags) : fFlags(flags) {
|
2019-08-12 12:49:36 +00:00
|
|
|
fName.printf("drawAtlas");
|
|
|
|
if (flags & kColors_Flag) {
|
|
|
|
fName.append("_colors");
|
|
|
|
}
|
|
|
|
if (flags & kRotate_Flag) {
|
|
|
|
fName.append("_rotated");
|
|
|
|
}
|
2019-03-22 19:57:45 +00:00
|
|
|
}
|
|
|
|
~AtlasBench() override {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const char* onGetName() override { return fName.c_str(); }
|
|
|
|
void onDelayedSetup() override {
|
|
|
|
fAtlas = GetResourceAsImage("images/mandrill_256.png");
|
|
|
|
if (fAtlas) {
|
|
|
|
fAtlas = fAtlas->makeRasterImage();
|
|
|
|
}
|
|
|
|
|
|
|
|
const SkScalar imageW = fAtlas->width();
|
|
|
|
const SkScalar imageH = fAtlas->height();
|
2019-08-12 12:49:36 +00:00
|
|
|
SkScalar scos = 1;
|
|
|
|
SkScalar ssin = 0;
|
|
|
|
if (fFlags & kRotate_Flag) {
|
|
|
|
scos = 0.866025403784439f; // sqrt(3)/2
|
|
|
|
ssin = 0.5f;
|
|
|
|
}
|
2019-03-22 19:57:45 +00:00
|
|
|
|
|
|
|
SkRandom rand;
|
|
|
|
for (int i = 0; i < N; ++i) {
|
|
|
|
fRects[i] = SkRect::MakeXYWH(rand.nextF() * (imageW - 8),
|
|
|
|
rand.nextF() * (imageH - 8), 8, 8);
|
2019-08-05 17:04:18 +00:00
|
|
|
fColors[i] = rand.nextU() | 0xFF000000;
|
2019-08-12 12:49:36 +00:00
|
|
|
fXforms[i] = SkRSXform::Make(scos, ssin, rand.nextF() * W, rand.nextF() * H);
|
2019-03-22 19:57:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
void onDraw(int loops, SkCanvas* canvas) override {
|
|
|
|
const SkRect* cullRect = nullptr;
|
|
|
|
const SkPaint* paintPtr = nullptr;
|
|
|
|
const SkColor* colors = nullptr;
|
|
|
|
if (fFlags & kColors_Flag) {
|
|
|
|
colors = fColors;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < loops; i++) {
|
2019-08-12 12:49:36 +00:00
|
|
|
canvas->drawAtlas(fAtlas, fXforms, fRects, colors, N, SkBlendMode::kModulate,
|
2019-03-22 19:57:45 +00:00
|
|
|
cullRect, paintPtr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
typedef Benchmark INHERITED;
|
|
|
|
};
|
|
|
|
//DEF_BENCH(return new AtlasBench(0);)
|
|
|
|
//DEF_BENCH(return new AtlasBench(kColors_Flag);)
|
2019-08-12 12:49:36 +00:00
|
|
|
DEF_BENCH(return new AtlasBench(0);)
|
|
|
|
DEF_BENCH(return new AtlasBench(kRotate_Flag);)
|
|
|
|
DEF_BENCH(return new AtlasBench(kColors_Flag);)
|
|
|
|
DEF_BENCH(return new AtlasBench(kColors_Flag | kRotate_Flag);)
|
2011-10-28 18:57:32 +00:00
|
|
|
|