| 1 |
/* |
| 2 |
* Copyright 2011 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 "bench/Benchmark.h" |
| 9 |
#include "include/core/SkCanvas.h" |
| 10 |
#include "include/core/SkPaint.h" |
| 11 |
#include "include/core/SkShader.h" |
| 12 |
#include "include/core/SkString.h" |
| 13 |
#include "include/core/SkVertices.h" |
| 14 |
#include "src/base/SkRandom.h" |
| 15 |
#include "tools/DecodeUtils.h" |
| 16 |
#include "tools/Resources.h" |
| 17 |
|
| 18 |
// Just want to trigger perspective handling, not dramatically change size |
| 19 |
static void tiny_persp_effect(SkCanvas* canvas) { |
| 20 |
SkMatrix m; |
| 21 |
m.reset(); |
| 22 |
m[7] = 0.000001f; |
| 23 |
canvas->concat(m); |
| 24 |
} |
| 25 |
|
| 26 |
enum VertFlags { |
| 27 |
kColors_VertFlag = 1 << 0, |
| 28 |
kTexture_VertFlag = 1 << 1, |
| 29 |
kPersp_VertFlag = 1 << 2, |
| 30 |
kBilerp_VertFlag = 1 << 3, |
| 31 |
}; |
| 32 |
|
| 33 |
class VertBench : public Benchmark { |
| 34 |
SkString fName; |
| 35 |
|
| 36 |
static constexpr int W = 64*2; |
| 37 |
static constexpr int H = 48*2; |
| 38 |
static constexpr int ROW = 20; |
| 39 |
static constexpr int COL = 20; |
| 40 |
static constexpr int PTS = (ROW + 1) * (COL + 1); |
| 41 |
static constexpr int IDX = ROW * COL * 6; |
| 42 |
|
| 43 |
sk_sp<SkShader> fShader; |
| 44 |
SkPoint fPts[PTS], fTex[PTS]; |
| 45 |
SkColor fColors[PTS]; |
| 46 |
uint16_t fIdx[IDX]; |
| 47 |
unsigned fFlags; |
| 48 |
|
| 49 |
static void load_2_tris(uint16_t idx[], int x, int y, int rb) { |
| 50 |
int n = y * rb + x; |
| 51 |
idx[0] = n; idx[1] = n + 1; idx[2] = rb + n + 1; |
| 52 |
idx[3] = n; idx[4] = rb + n + 1; idx[5] = n + rb; |
| 53 |
} |
| 54 |
|
| 55 |
void onDelayedSetup() override { |
| 56 |
if (fFlags & kTexture_VertFlag) { |
| 57 |
auto img = ToolUtils::GetResourceAsImage("images/mandrill_256.png"); |
| 58 |
if (img) { |
| 59 |
SkFilterMode fm = (fFlags & kBilerp_VertFlag) ? SkFilterMode::kLinear |
| 60 |
: SkFilterMode::kNearest; |
| 61 |
fShader = img->makeShader(SkSamplingOptions(fm)); |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
public: |
| 67 |
VertBench(unsigned flags) : fFlags(flags) { |
| 68 |
const SkScalar dx = SkIntToScalar(W) / COL; |
| 69 |
const SkScalar dy = SkIntToScalar(H) / COL; |
| 70 |
|
| 71 |
SkPoint* pts = fPts; |
| 72 |
uint16_t* idx = fIdx; |
| 73 |
|
| 74 |
SkScalar yy = 0; |
| 75 |
for (int y = 0; y <= ROW; y++) { |
| 76 |
SkScalar xx = 0; |
| 77 |
for (int x = 0; x <= COL; ++x) { |
| 78 |
pts->set(xx, yy); |
| 79 |
pts += 1; |
| 80 |
xx += dx; |
| 81 |
|
| 82 |
if (x < COL && y < ROW) { |
| 83 |
load_2_tris(idx, x, y, COL + 1); |
| 84 |
for (int i = 0; i < 6; i++) { |
| 85 |
SkASSERT(idx[i] < PTS); |
| 86 |
} |
| 87 |
idx += 6; |
| 88 |
} |
| 89 |
} |
| 90 |
yy += dy; |
| 91 |
} |
| 92 |
SkASSERT(PTS == pts - fPts); |
| 93 |
SkASSERT(IDX == idx - fIdx); |
| 94 |
|
| 95 |
// We want to store texs in a separate array, so the blitters don't "cheat" and |
| 96 |
// skip the (normal) step of computing the new local-matrix. This is the common case |
| 97 |
// we think in the wild (where the texture coordinates are different from the positions. |
| 98 |
memcpy(fTex, fPts, sizeof(fPts)); |
| 99 |
|
| 100 |
SkRandom rand; |
| 101 |
for (int i = 0; i < PTS; ++i) { |
| 102 |
fColors[i] = rand.nextU() | (0xFF << 24); |
| 103 |
} |
| 104 |
|
| 105 |
fName.set("verts"); |
| 106 |
if (fFlags & kTexture_VertFlag) { |
| 107 |
fName.append("_textures"); |
| 108 |
} |
| 109 |
if (fFlags & kColors_VertFlag) { |
| 110 |
fName.append("_colors"); |
| 111 |
} |
| 112 |
if (fFlags & kPersp_VertFlag) { |
| 113 |
fName.append("_persp"); |
| 114 |
} |
| 115 |
if (fFlags & kBilerp_VertFlag) { |
| 116 |
fName.append("_bilerp"); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
protected: |
| 121 |
const char* onGetName() override { return fName.c_str(); } |
| 122 |
void onDraw(int loops, SkCanvas* canvas) override { |
| 123 |
SkPaint paint; |
| 124 |
this->setupPaint(&paint); |
| 125 |
paint.setShader(fShader); |
| 126 |
|
| 127 |
if (fFlags & kPersp_VertFlag) { |
| 128 |
tiny_persp_effect(canvas); |
| 129 |
} |
| 130 |
|
| 131 |
const SkPoint* texs = (fFlags & kTexture_VertFlag) ? fTex : nullptr; |
| 132 |
const SkColor* cols = (fFlags & kColors_VertFlag) ? fColors : nullptr; |
| 133 |
auto verts = SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode, PTS, |
| 134 |
fPts, texs, cols, IDX, fIdx); |
| 135 |
for (int i = 0; i < loops; i++) { |
| 136 |
canvas->drawVertices(verts, SkBlendMode::kModulate, paint); |
| 137 |
} |
| 138 |
} |
| 139 |
private: |
| 140 |
using INHERITED = Benchmark; |
| 141 |
}; |
| 142 |
DEF_BENCH(return new VertBench(kTexture_VertFlag | kPersp_VertFlag);) |
| 143 |
DEF_BENCH(return new VertBench(kTexture_VertFlag | kPersp_VertFlag | kBilerp_VertFlag);) |
| 144 |
DEF_BENCH(return new VertBench(kColors_VertFlag | kPersp_VertFlag);) |
| 145 |
DEF_BENCH(return new VertBench(kTexture_VertFlag);) |
| 146 |
DEF_BENCH(return new VertBench(kTexture_VertFlag | kBilerp_VertFlag);) |
| 147 |
DEF_BENCH(return new VertBench(kColors_VertFlag);) |
| 148 |
DEF_BENCH(return new VertBench(kColors_VertFlag | kTexture_VertFlag);) |
| 149 |
DEF_BENCH(return new VertBench(kColors_VertFlag | kTexture_VertFlag | kBilerp_VertFlag);) |
| 150 |
|
| 151 |
///////////////////////////////////////////////////////////////////////////////////////////////// |
| 152 |
|
| 153 |
#include "include/core/SkRSXform.h" |
| 154 |
#include "src/base/SkRandom.h" |
| 155 |
#include "tools/Resources.h" |
| 156 |
|
| 157 |
enum AtlasFlags { |
| 158 |
kColors_Flag = 1 << 0, |
| 159 |
kRotate_Flag = 1 << 1, |
| 160 |
kPersp_Flag = 1 << 2, |
| 161 |
}; |
| 162 |
|
| 163 |
class AtlasBench : public Benchmark { |
| 164 |
unsigned fFlags; |
| 165 |
SkString fName; |
| 166 |
|
| 167 |
static constexpr int W = 640; |
| 168 |
static constexpr int H = 480; |
| 169 |
static constexpr int N = 10*1000; |
| 170 |
|
| 171 |
sk_sp<SkImage> fAtlas; |
| 172 |
SkRSXform fXforms[N]; |
| 173 |
SkRect fRects[N]; |
| 174 |
SkColor fColors[N]; |
| 175 |
|
| 176 |
public: |
| 177 |
AtlasBench(unsigned flags) : fFlags(flags) { |
| 178 |
fName.printf("drawAtlas"); |
| 179 |
if (flags & kColors_Flag) { |
| 180 |
fName.append("_colors"); |
| 181 |
} |
| 182 |
if (flags & kRotate_Flag) { |
| 183 |
fName.append("_rotated"); |
| 184 |
} |
| 185 |
if (flags & kPersp_Flag) { |
| 186 |
fName.append("_persp"); |
| 187 |
} |
| 188 |
} |
| 189 |
~AtlasBench() override {} |
| 190 |
|
| 191 |
protected: |
| 192 |
const char* onGetName() override { return fName.c_str(); } |
| 193 |
void onDelayedSetup() override { |
| 194 |
fAtlas = ToolUtils::GetResourceAsImage("images/mandrill_256.png"); |
| 195 |
if (fAtlas) { |
| 196 |
fAtlas = fAtlas->makeRasterImage(nullptr); |
| 197 |
} |
| 198 |
|
| 199 |
const SkScalar imageW = fAtlas->width(); |
| 200 |
const SkScalar imageH = fAtlas->height(); |
| 201 |
SkScalar scos = 1; |
| 202 |
SkScalar ssin = 0; |
| 203 |
if (fFlags & kRotate_Flag) { |
| 204 |
scos = 0.866025403784439f; // sqrt(3)/2 |
| 205 |
ssin = 0.5f; |
| 206 |
} |
| 207 |
|
| 208 |
SkRandom rand; |
| 209 |
for (int i = 0; i < N; ++i) { |
| 210 |
fRects[i] = SkRect::MakeXYWH(rand.nextF() * (imageW - 8), |
| 211 |
rand.nextF() * (imageH - 8), 8, 8); |
| 212 |
fColors[i] = rand.nextU() | 0xFF000000; |
| 213 |
fXforms[i] = SkRSXform::Make(scos, ssin, rand.nextF() * W, rand.nextF() * H); |
| 214 |
} |
| 215 |
} |
| 216 |
void onDraw(int loops, SkCanvas* canvas) override { |
| 217 |
const SkRect* cullRect = nullptr; |
| 218 |
const SkPaint* paintPtr = nullptr; |
| 219 |
const SkColor* colors = nullptr; |
| 220 |
if (fFlags & kColors_Flag) { |
| 221 |
colors = fColors; |
| 222 |
} |
| 223 |
if (fFlags & kPersp_Flag) { |
| 224 |
tiny_persp_effect(canvas); |
| 225 |
} |
| 226 |
for (int i = 0; i < loops; i++) { |
| 227 |
canvas->drawAtlas(fAtlas.get(), fXforms, fRects, |
| 228 |
{colors, colors ? (size_t)N : 0}, SkBlendMode::kModulate, |
| 229 |
SkSamplingOptions(), cullRect, paintPtr); |
| 230 |
} |
| 231 |
} |
| 232 |
private: |
| 233 |
using INHERITED = Benchmark; |
| 234 |
}; |
| 235 |
//DEF_BENCH(return new AtlasBench(0);) |
| 236 |
//DEF_BENCH(return new AtlasBench(kColors_Flag);) |
| 237 |
DEF_BENCH(return new AtlasBench(0);) |
| 238 |
DEF_BENCH(return new AtlasBench(kRotate_Flag);) |
| 239 |
DEF_BENCH(return new AtlasBench(kPersp_Flag);) |
| 240 |
DEF_BENCH(return new AtlasBench(kColors_Flag);) |
| 241 |
DEF_BENCH(return new AtlasBench(kColors_Flag | kRotate_Flag);) |
| 242 |
|