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 "include/utils/SkRandom.h"
15 #include "tools/Resources.h"
16
17 // Just want to trigger perspective handling, not dramatically change size
tiny_persp_effect(SkCanvas * canvas)18 static void tiny_persp_effect(SkCanvas* canvas) {
19 SkMatrix m;
20 m.reset();
21 m[7] = 0.000001f;
22 canvas->concat(m);
23 }
24
25 enum VertFlags {
26 kColors_VertFlag = 1 << 0,
27 kTexture_VertFlag = 1 << 1,
28 kPersp_VertFlag = 1 << 2,
29 kBilerp_VertFlag = 1 << 3,
30 };
31
32 class VertBench : public Benchmark {
33 SkString fName;
34 enum {
35 W = 64*2,
36 H = 48*2,
37 ROW = 20,
38 COL = 20,
39 PTS = (ROW + 1) * (COL + 1),
40 IDX = ROW * COL * 6,
41 };
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
load_2_tris(uint16_t idx[],int x,int y,int rb)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
onDelayedSetup()55 void onDelayedSetup() override {
56 if (fFlags & kTexture_VertFlag) {
57 auto img = 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:
VertBench(unsigned flags)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:
onGetName()121 const char* onGetName() override { return fName.c_str(); }
onDraw(int loops,SkCanvas * canvas)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 "include/utils/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 enum {
167 W = 640,
168 H = 480,
169 N = 10*1000,
170 };
171
172 sk_sp<SkImage> fAtlas;
173 SkRSXform fXforms[N];
174 SkRect fRects[N];
175 SkColor fColors[N];
176
177 public:
AtlasBench(unsigned flags)178 AtlasBench(unsigned flags) : fFlags(flags) {
179 fName.printf("drawAtlas");
180 if (flags & kColors_Flag) {
181 fName.append("_colors");
182 }
183 if (flags & kRotate_Flag) {
184 fName.append("_rotated");
185 }
186 if (flags & kPersp_Flag) {
187 fName.append("_persp");
188 }
189 }
~AtlasBench()190 ~AtlasBench() override {}
191
192 protected:
onGetName()193 const char* onGetName() override { return fName.c_str(); }
onDelayedSetup()194 void onDelayedSetup() override {
195 fAtlas = GetResourceAsImage("images/mandrill_256.png");
196 if (fAtlas) {
197 fAtlas = fAtlas->makeRasterImage();
198 }
199
200 const SkScalar imageW = fAtlas->width();
201 const SkScalar imageH = fAtlas->height();
202 SkScalar scos = 1;
203 SkScalar ssin = 0;
204 if (fFlags & kRotate_Flag) {
205 scos = 0.866025403784439f; // sqrt(3)/2
206 ssin = 0.5f;
207 }
208
209 SkRandom rand;
210 for (int i = 0; i < N; ++i) {
211 fRects[i] = SkRect::MakeXYWH(rand.nextF() * (imageW - 8),
212 rand.nextF() * (imageH - 8), 8, 8);
213 fColors[i] = rand.nextU() | 0xFF000000;
214 fXforms[i] = SkRSXform::Make(scos, ssin, rand.nextF() * W, rand.nextF() * H);
215 }
216 }
onDraw(int loops,SkCanvas * canvas)217 void onDraw(int loops, SkCanvas* canvas) override {
218 const SkRect* cullRect = nullptr;
219 const SkPaint* paintPtr = nullptr;
220 const SkColor* colors = nullptr;
221 if (fFlags & kColors_Flag) {
222 colors = fColors;
223 }
224 if (fFlags & kPersp_Flag) {
225 tiny_persp_effect(canvas);
226 }
227 for (int i = 0; i < loops; i++) {
228 canvas->drawAtlas(fAtlas.get(), fXforms, fRects, colors, N, 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
243