Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
PatchBench.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2014 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#include "bench/Benchmark.h"
15
16/**
17 * This bench measures the rendering time of the call SkCanvas::drawPatch with different types of
18 * input patches (regular case, with loops, a square, with a big difference between "parallel"
19 * sides). This bench also tests the different combination of optional parameters for the function
20 * (passing texture coordinates and colors, only textures coordinates, only colors or none).
21 * Finally, it applies a scale to test if the size affects the rendering time.
22 */
23
24class PatchBench : public Benchmark {
25
26public:
27
34
36 : fScale(scale)
37 , fVertexMode(vertexMode) { }
38
39 // to add name of specific class override this method
40 virtual void appendName(SkString* name) {
41 name->append("normal");
42 }
43
44 // to make other type of patches override this method
45 virtual void setCubics() {
47 //top points
48 {100,100},{150,50},{250,150}, {300,100},
49 //right points
50 {350, 150},{250,200},
51 //bottom points
52 {300,300},{250,250},{150,350},{100,300},
53 //left points
54 {50,250},{150,50}
55 };
57 }
58
59 virtual void setColors() {
60 const SkColor colors[SkPatchUtils::kNumCorners] = {
62 };
63 memcpy(fColors, colors, SkPatchUtils::kNumCorners * sizeof(SkColor));
64 }
65
66 virtual void setTexCoords() {
67 const SkPoint texCoords[SkPatchUtils::kNumCorners] = {
68 {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f,1.0f}, {0.0f, 1.0f}
69 };
70 memcpy(fTexCoords, texCoords, SkPatchUtils::kNumCorners * sizeof(SkPoint));
71 }
72
73 // override this method to change the shader
75 const SkColor colors[] = {
78 };
79 const SkPoint pts[] = { { 200.f / 4.f, 0.f }, { 3.f * 200.f / 4, 200.f } };
80
81 return SkGradientShader::MakeLinear(pts, colors, nullptr, std::size(colors),
83 }
84
85protected:
86 const char* onGetName() override {
87 SkString vertexMode;
88 switch (fVertexMode) {
90 vertexMode.set("meshlines");
91 break;
93 vertexMode.set("colors");
94 break;
96 vertexMode.set("texs");
97 break;
99 vertexMode.set("colors_texs");
100 break;
101 default:
102 break;
103 }
105 this->appendName(&type);
106 fName.printf("patch_%s_%s_%fx%f", type.c_str(), vertexMode.c_str(),
107 fScale.x(), fScale.y());
108 return fName.c_str();
109 }
110
111 void onDelayedSetup() override {
112 this->setCubics();
113 this->setColors();
114 this->setTexCoords();
115 this->setupPaint(&fPaint);
116 switch (fVertexMode) {
118 case kBoth_VertexMode:
120 break;
121 default:
122 fPaint.setShader(nullptr);
123 break;
124 }
125 }
126
127 void onDraw(int loops, SkCanvas* canvas) override {
128 canvas->scale(fScale.x(), fScale.y());
129 for (int i = 0; i < loops; i++) {
130 switch (fVertexMode) {
131 case kNone_VertexMode:
132 canvas->drawPatch(fCubics, nullptr, nullptr, SkBlendMode::kModulate, fPaint);
133 break;
136 break;
139 break;
140 case kBoth_VertexMode:
142 break;
143 default:
144 break;
145 }
146 }
147 }
148
156
158};
159
161public:
163 : INHERITED(scale, vertexMode) { }
164
165 void appendName(SkString* name) override {
166 name->append("square");
167 }
168
169 void setCubics() override {
171 //top points
172 {100,100},{150,100},{250,100}, {300,100},
173 //right points
174 {300, 150},{300,250},
175 //bottom points
176 {300,300},{250,300},{150,300},{100,300},
177 //left points
178 {100,250},{100,150}
179 };
181 }
182private:
183 using INHERITED = PatchBench;
184};
185
187public:
189 : INHERITED(scale, vertexMode) { }
190
191 void appendName(SkString* name) override {
192 name->append("LOD_Diff");
193 }
194
195 void setCubics() override {
197 //top points
198 {100,175},{150,100},{250,100}, {300,0},
199 //right points
200 {300, 150},{300,250},
201 //bottom points
202 {300,400},{250,300},{150,300},{100,225},
203 //left points
204 {100,215},{100,185}
205 };
207 }
208private:
209 using INHERITED = PatchBench;
210};
211
213public:
215 : INHERITED(scale, vertexMode) { }
216
217 void appendName(SkString* name) override {
218 name->append("loop");
219 }
220
221 void setCubics() override {
223 //top points
224 {100,100},{300,200},{100,200}, {300,100},
225 //right points
226 {380, 400},{380,0},
227 //bottom points
228 {300,300},{250,250},{30,200},{100,300},
229 //left points
230 {140,325},{150,150}
231 };
233 }
234private:
235 using INHERITED = PatchBench;
236};
237
238///////////////////////////////////////////////////////////////////////////////
239
252
253DEF_BENCH( return new SquarePatchBench(SkVector::Make(0.1f, 0.1f),
255DEF_BENCH( return new SquarePatchBench(SkVector::Make(0.1f, 0.1f),
257DEF_BENCH( return new SquarePatchBench(SkVector::Make(0.1f, 0.1f),
259DEF_BENCH( return new SquarePatchBench(SkVector::Make(0.1f, 0.1f),
261DEF_BENCH( return new SquarePatchBench(SkVector::Make(1.f, 1.0f),
263DEF_BENCH( return new SquarePatchBench(SkVector::Make(1.0f, 1.0f),
265DEF_BENCH( return new SquarePatchBench(SkVector::Make(1.0f, 1.0f),
267DEF_BENCH( return new SquarePatchBench(SkVector::Make(1.0f, 1.0f),
269DEF_BENCH( return new SquarePatchBench(SkVector::Make(3.0f, 3.0f),
271DEF_BENCH( return new SquarePatchBench(SkVector::Make(3.0f, 3.0f),
273DEF_BENCH( return new SquarePatchBench(SkVector::Make(3.0f, 3.0f),
275DEF_BENCH( return new SquarePatchBench(SkVector::Make(3.0f, 3.0f),
277
278DEF_BENCH( return new LODDiffPatchBench(SkVector::Make(0.1f, 0.1f),
280DEF_BENCH( return new LODDiffPatchBench(SkVector::Make(0.1f, 0.1f),
282DEF_BENCH( return new LODDiffPatchBench(SkVector::Make(0.1f, 0.1f),
284DEF_BENCH( return new LODDiffPatchBench(SkVector::Make(0.1f, 0.1f),
286DEF_BENCH( return new LODDiffPatchBench(SkVector::Make(1.f, 1.0f),
288DEF_BENCH( return new LODDiffPatchBench(SkVector::Make(1.0f, 1.0f),
290DEF_BENCH( return new LODDiffPatchBench(SkVector::Make(1.0f, 1.0f),
292DEF_BENCH( return new LODDiffPatchBench(SkVector::Make(1.0f, 1.0f),
294DEF_BENCH( return new LODDiffPatchBench(SkVector::Make(3.0f, 3.0f),
296DEF_BENCH( return new LODDiffPatchBench(SkVector::Make(3.0f, 3.0f),
298DEF_BENCH( return new LODDiffPatchBench(SkVector::Make(3.0f, 3.0f),
300DEF_BENCH( return new LODDiffPatchBench(SkVector::Make(3.0f, 3.0f),
302
303DEF_BENCH( return new LoopPatchBench(SkVector::Make(0.1f, 0.1f),
305DEF_BENCH( return new LoopPatchBench(SkVector::Make(0.1f, 0.1f),
307DEF_BENCH( return new LoopPatchBench(SkVector::Make(0.1f, 0.1f),
309DEF_BENCH( return new LoopPatchBench(SkVector::Make(0.1f, 0.1f),
311DEF_BENCH( return new LoopPatchBench(SkVector::Make(1.f, 1.0f),
313DEF_BENCH( return new LoopPatchBench(SkVector::Make(1.0f, 1.0f),
315DEF_BENCH( return new LoopPatchBench(SkVector::Make(1.0f, 1.0f),
317DEF_BENCH( return new LoopPatchBench(SkVector::Make(1.0f, 1.0f),
319DEF_BENCH( return new LoopPatchBench(SkVector::Make(3.0f, 3.0f),
321DEF_BENCH( return new LoopPatchBench(SkVector::Make(3.0f, 3.0f),
323DEF_BENCH( return new LoopPatchBench(SkVector::Make(3.0f, 3.0f),
325DEF_BENCH( return new LoopPatchBench(SkVector::Make(3.0f, 3.0f),
327
328//////////////////////////////////////////////
329#include "src/utils/SkPatchUtils.h"
330
333 const bool fLinearInterp;
334public:
335 PatchUtilsBench(bool linearInterp) : fLinearInterp(linearInterp) {
336 fName.printf("patchutils_%s", linearInterp ? "linear" : "legacy");
337 }
338
339 const char* onGetName() override { return fName.c_str(); }
340
341 bool isSuitableFor(Backend backend) override {
342 return backend == Backend::kNonRendering;
343 }
344
345 void onDraw(int loops, SkCanvas*) override {
346 const SkColor colors[] = { 0xFF000000, 0xFF00FF00, 0xFF0000FF, 0xFFFF0000 };
347 const SkPoint pts[] = {
348 { 0, 0 }, { 10, 0 }, { 20, 0 }, { 30, 0 },
349 { 30,10}, { 30,20 }, { 30,30 }, { 20,30 },
350 { 10,30}, { 0, 30 }, { 0, 20 }, { 0, 10 },
351 };
352 const SkPoint tex[] = {
353 { 0, 0 }, { 10, 0 }, { 10, 10 }, { 0, 10 },
354 };
355
356 auto cs = fLinearInterp ? SkColorSpace::MakeSRGBLinear() : nullptr;
357 for (int i = 0; i < 100*loops; ++i) {
358 SkPatchUtils::MakeVertices(pts, colors, tex, 20, 20, cs.get());
359 }
360 }
361};
362DEF_BENCH( return new PatchUtilsBench(false); )
363DEF_BENCH( return new PatchUtilsBench(true); )
#define DEF_BENCH(code)
Definition Benchmark.h:20
const char * backend
const char * fName
static const int points[]
@ kModulate
r = s*d
constexpr SkColor SK_ColorYELLOW
Definition SkColor.h:139
constexpr SkColor SK_ColorMAGENTA
Definition SkColor.h:147
uint32_t SkColor
Definition SkColor.h:37
constexpr SkColor SK_ColorCYAN
Definition SkColor.h:143
constexpr SkColor SK_ColorBLUE
Definition SkColor.h:135
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
virtual void setupPaint(SkPaint *paint)
Definition Benchmark.cpp:55
void appendName(SkString *name) override
void setCubics() override
LODDiffPatchBench(SkPoint scale, VertexMode vertexMode)
void appendName(SkString *name) override
void setCubics() override
LoopPatchBench(SkPoint scale, VertexMode vertexMode)
virtual sk_sp< SkShader > createShader()
SkPoint fTexCoords[4]
const char * onGetName() override
virtual void setColors()
virtual void setTexCoords()
SkColor fColors[4]
@ kTexCoords_VertexMode
void onDelayedSetup() override
VertexMode fVertexMode
virtual void setCubics()
SkString fName
SkPaint fPaint
PatchBench(SkPoint scale, VertexMode vertexMode)
virtual void appendName(SkString *name)
SkVector fScale
void onDraw(int loops, SkCanvas *canvas) override
SkPoint fCubics[12]
void onDraw(int loops, SkCanvas *) override
const char * onGetName() override
PatchUtilsBench(bool linearInterp)
bool isSuitableFor(Backend backend) override
void drawPatch(const SkPoint cubics[12], const SkColor colors[4], const SkPoint texCoords[4], SkBlendMode mode, const SkPaint &paint)
void scale(SkScalar sx, SkScalar sy)
static sk_sp< SkColorSpace > MakeSRGBLinear()
static sk_sp< SkShader > MakeLinear(const SkPoint pts[2], const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
void setShader(sk_sp< SkShader > shader)
static sk_sp< SkVertices > MakeVertices(const SkPoint cubics[12], const SkColor colors[4], const SkPoint texCoords[4], int lodX, int lodY, SkColorSpace *colorSpace=nullptr)
void printf(const char format[],...) SK_PRINTF_LIKE(2
Definition SkString.cpp:534
void set(const SkString &src)
Definition SkString.h:186
const char * c_str() const
Definition SkString.h:133
SquarePatchBench(SkPoint scale, VertexMode vertexMode)
void appendName(SkString *name) override
void setCubics() override
const char * name
Definition fuchsia.cc:50
const Scalar scale
static constexpr SkPoint Make(float x, float y)
constexpr float y() const
constexpr float x() const