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