Flutter Engine
The Flutter Engine
VertBench.cpp
Go to the documentation of this file.
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"
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
19static void tiny_persp_effect(SkCanvas* canvas) {
20 SkMatrix m;
21 m.reset();
22 m[7] = 0.000001f;
23 canvas->concat(m);
24}
25
31};
32
33class 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 {
57 auto img = ToolUtils::GetResourceAsImage("images/mandrill_256.png");
58 if (img) {
61 fShader = img->makeShader(SkSamplingOptions(fm));
62 }
63 }
64 }
65
66public:
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");
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
120protected:
121 const char* onGetName() override { return fName.c_str(); }
122 void onDraw(int loops, SkCanvas* canvas) override {
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;
134 fPts, texs, cols, IDX, fIdx);
135 for (int i = 0; i < loops; i++) {
137 }
138 }
139private:
140 using INHERITED = Benchmark;
141};
150
151/////////////////////////////////////////////////////////////////////////////////////////////////
152
153#include "include/core/SkRSXform.h"
154#include "src/base/SkRandom.h"
155#include "tools/Resources.h"
156
158 kColors_Flag = 1 << 0,
159 kRotate_Flag = 1 << 1,
160 kPersp_Flag = 1 << 2,
161};
162
163class 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
176public:
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
191protected:
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();
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, colors, N, SkBlendMode::kModulate,
228 SkSamplingOptions(), cullRect, paintPtr);
229 }
230 }
231private:
232 using INHERITED = Benchmark;
233};
234//DEF_BENCH(return new AtlasBench(0);)
235//DEF_BENCH(return new AtlasBench(kColors_Flag);)
236DEF_BENCH(return new AtlasBench(0);)
238DEF_BENCH(return new AtlasBench(kPersp_Flag);)
241
SkPoint fPts[2]
#define DEF_BENCH(code)
Definition: Benchmark.h:20
const char * fName
uint16_t fFlags
Definition: ShapeLayer.cpp:106
#define SkASSERT(cond)
Definition: SkAssert.h:116
@ kModulate
r = s*d
uint32_t SkColor
Definition: SkColor.h:37
SkFilterMode
#define SkIntToScalar(x)
Definition: SkScalar.h:57
VertFlags
Definition: VertBench.cpp:26
@ kColors_VertFlag
Definition: VertBench.cpp:27
@ kTexture_VertFlag
Definition: VertBench.cpp:28
@ kBilerp_VertFlag
Definition: VertBench.cpp:30
@ kPersp_VertFlag
Definition: VertBench.cpp:29
AtlasFlags
Definition: VertBench.cpp:157
@ kColors_Flag
Definition: VertBench.cpp:158
@ kPersp_Flag
Definition: VertBench.cpp:160
@ kRotate_Flag
Definition: VertBench.cpp:159
static void tiny_persp_effect(SkCanvas *canvas)
Definition: VertBench.cpp:19
#define W
Definition: aaa.cpp:17
#define N
Definition: beziers.cpp:19
void onDelayedSetup() override
Definition: VertBench.cpp:193
const char * onGetName() override
Definition: VertBench.cpp:192
AtlasBench(unsigned flags)
Definition: VertBench.cpp:177
void onDraw(int loops, SkCanvas *canvas) override
Definition: VertBench.cpp:216
~AtlasBench() override
Definition: VertBench.cpp:189
virtual void setupPaint(SkPaint *paint)
Definition: Benchmark.cpp:55
void drawAtlas(const SkImage *atlas, const SkRSXform xform[], const SkRect tex[], const SkColor colors[], int count, SkBlendMode mode, const SkSamplingOptions &sampling, const SkRect *cullRect, const SkPaint *paint)
Definition: SkCanvas.cpp:1810
void concat(const SkMatrix &matrix)
Definition: SkCanvas.cpp:1318
void drawVertices(const SkVertices *vertices, SkBlendMode mode, const SkPaint &paint)
Definition: SkCanvas.cpp:1720
sk_sp< SkImage > makeRasterImage(GrDirectContext *, CachingHint cachingHint=kDisallow_CachingHint) const
Definition: SkImage.cpp:267
int width() const
Definition: SkImage.h:285
int height() const
Definition: SkImage.h:291
uint32_t nextU()
Definition: SkRandom.h:42
float nextF()
Definition: SkRandom.h:55
static sk_sp< SkVertices > MakeCopy(VertexMode mode, int vertexCount, const SkPoint positions[], const SkPoint texs[], const SkColor colors[], int indexCount, const uint16_t indices[])
Definition: SkVertices.cpp:200
@ kTriangles_VertexMode
Definition: SkVertices.h:31
const char * onGetName() override
Definition: VertBench.cpp:121
VertBench(unsigned flags)
Definition: VertBench.cpp:67
void onDraw(int loops, SkCanvas *canvas) override
Definition: VertBench.cpp:122
T * get() const
Definition: SkRefCnt.h:303
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
FlutterSemanticsFlag flags
double y
double x
PODArray< SkRect > texs
Definition: SkRecords.h:333
PODArray< SkColor > colors
Definition: SkRecords.h:276
skia_private::AutoTArray< sk_sp< SkImageFilter > > filters TypedMatrix matrix TypedMatrix matrix SkScalar dx
Definition: SkRecords.h:208
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition: DecodeUtils.h:25
SkSamplingOptions(SkFilterMode::kLinear))
Definition: SkMD5.cpp:130
void set(float x, float y)
Definition: SkPoint_impl.h:200
static SkRSXform Make(SkScalar scos, SkScalar ssin, SkScalar tx, SkScalar ty)
Definition: SkRSXform.h:24
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659