Flutter Engine
The Flutter Engine
PictureNestingBench.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
8#include "bench/Benchmark.h"
16
17class PictureNesting : public Benchmark {
18public:
19 PictureNesting(const char* name, int maxLevel, int maxPictureLevel)
20 : fMaxLevel(maxLevel)
21 , fMaxPictureLevel(maxPictureLevel) {
22 fName.printf("picture_nesting_%s_%d", name, this->countPics());
23 fPaint.setColor(SK_ColorRED);
24 fPaint.setAntiAlias(true);
26 }
27
28protected:
29 const char* onGetName() override {
30 return fName.c_str();
31 }
32
33 void doDraw(SkCanvas* canvas) {
34 SkISize canvasSize = onGetSize();
35 canvas->save();
36 canvas->scale(SkIntToScalar(canvasSize.width()), SkIntToScalar(canvasSize.height()));
37
38 SkDEBUGCODE(int pics = ) this->sierpinsky(canvas, 0, fPaint);
39 SkASSERT(pics == this->countPics());
40
41 canvas->restore();
42 }
43
44 int sierpinsky(SkCanvas* canvas, int lvl, const SkPaint& paint) {
45 if (++lvl > fMaxLevel) {
46 return 0;
47 }
48
49 int pics = 0;
50 bool recordPicture = lvl <= fMaxPictureLevel;
51 SkPictureRecorder recorder;
52 SkCanvas* c = canvas;
53
54 if (recordPicture) {
55 c = recorder.beginRecording(1, 1);
56 pics++;
57 }
58
59 c->drawLine(0.5, 0, 0, 1, paint);
60 c->drawLine(0.5, 0, 1, 1, paint);
61 c->drawLine(0, 1, 1, 1, paint);
62
63 c->save();
64 c->scale(0.5, 0.5);
65
66 c->translate(0, 1);
67 pics += this->sierpinsky(c, lvl, paint);
68
69 c->translate(1, 0);
70 pics += this->sierpinsky(c, lvl, paint);
71
72 c->translate(-0.5, -1);
73 pics += this->sierpinsky(c, lvl, paint);
74 c->restore();
75
76 if (recordPicture) {
77 canvas->drawPicture(recorder.finishRecordingAsPicture());
78 }
79
80 return pics;
81 }
82
85
86private:
87 int countPics() const {
88 // Solve: pics from sierpinsky
89 // f(m) = 1 + 3*f(m - 1)
90 // f(0) = 0
91 // via "recursive function to closed form" tricks
92 // f(m) = 1/2 (3^m - 1)
93 int pics = 1;
94 for (int i = 0; i < fMaxPictureLevel; i++) {
95 pics *= 3;
96 }
97 pics--;
98 pics /= 2;
99 return pics;
100 }
101
102 SkString fName;
103 SkPaint fPaint;
104
105 using INHERITED = Benchmark;
106};
107
109public:
110 PictureNestingRecording(int maxLevel, int maxPictureLevel)
111 : INHERITED("recording", maxLevel, maxPictureLevel) {
112 }
113
114protected:
117 }
118
119 void onDraw(int loops, SkCanvas*) override {
120 SkISize canvasSize = onGetSize();
121 SkPictureRecorder recorder;
122
123 for (int i = 0; i < loops; i++) {
124 SkCanvas* c = recorder.beginRecording(SkIntToScalar(canvasSize.width()),
125 SkIntToScalar(canvasSize.height()));
126 this->doDraw(c);
127 (void)recorder.finishRecordingAsPicture();
128 }
129 }
130
131private:
132 using INHERITED = PictureNesting;
133};
134
136public:
137 PictureNestingPlayback(int maxLevel, int maxPictureLevel)
138 : INHERITED("playback", maxLevel, maxPictureLevel) {
139 }
140protected:
141 void onDelayedSetup() override {
143
144 SkISize canvasSize = onGetSize();
145 SkPictureRecorder recorder;
146 SkCanvas* c = recorder.beginRecording(SkIntToScalar(canvasSize.width()),
147 SkIntToScalar(canvasSize.height()));
148
149 this->doDraw(c);
150 fPicture = recorder.finishRecordingAsPicture();
151 }
152
153 void onDraw(int loops, SkCanvas* canvas) override {
154 for (int i = 0; i < loops; i++) {
155 canvas->drawPicture(fPicture);
156 }
157 }
158
159private:
160 sk_sp<SkPicture> fPicture;
161
162 using INHERITED = PictureNesting;
163};
164
165DEF_BENCH( return new PictureNestingRecording(8, 0); )
166DEF_BENCH( return new PictureNestingRecording(8, 1); )
167DEF_BENCH( return new PictureNestingRecording(8, 2); )
168DEF_BENCH( return new PictureNestingRecording(8, 3); )
169DEF_BENCH( return new PictureNestingRecording(8, 4); )
170DEF_BENCH( return new PictureNestingRecording(8, 5); )
171DEF_BENCH( return new PictureNestingRecording(8, 6); )
172DEF_BENCH( return new PictureNestingRecording(8, 7); )
173DEF_BENCH( return new PictureNestingRecording(8, 8); )
174
175DEF_BENCH( return new PictureNestingPlayback(8, 0); )
176DEF_BENCH( return new PictureNestingPlayback(8, 1); )
177DEF_BENCH( return new PictureNestingPlayback(8, 2); )
178DEF_BENCH( return new PictureNestingPlayback(8, 3); )
179DEF_BENCH( return new PictureNestingPlayback(8, 4); )
180DEF_BENCH( return new PictureNestingPlayback(8, 5); )
181DEF_BENCH( return new PictureNestingPlayback(8, 6); )
182DEF_BENCH( return new PictureNestingPlayback(8, 7); )
183DEF_BENCH( return new PictureNestingPlayback(8, 8); )
#define DEF_BENCH(code)
Definition: Benchmark.h:20
const char * backend
#define SkASSERT(cond)
Definition: SkAssert.h:116
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
#define SkIntToScalar(x)
Definition: SkScalar.h:57
SkDEBUGCODE(SK_SPI) SkThreadID SkGetThreadID()
virtual SkISize onGetSize()
Definition: Benchmark.cpp:59
virtual void onDelayedSetup()
Definition: Benchmark.h:101
void onDraw(int loops, SkCanvas *canvas) override
PictureNestingPlayback(int maxLevel, int maxPictureLevel)
bool isSuitableFor(Backend backend) override
void onDraw(int loops, SkCanvas *) override
PictureNestingRecording(int maxLevel, int maxPictureLevel)
const char * onGetName() override
int sierpinsky(SkCanvas *canvas, int lvl, const SkPaint &paint)
void doDraw(SkCanvas *canvas)
PictureNesting(const char *name, int maxLevel, int maxPictureLevel)
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, const SkPaint &paint)
Definition: SkCanvas.cpp:2700
int save()
Definition: SkCanvas.cpp:447
void scale(SkScalar sx, SkScalar sy)
Definition: SkCanvas.cpp:1289
void drawPicture(const SkPicture *picture)
Definition: SkCanvas.h:1961
void setStyle(Style style)
Definition: SkPaint.cpp:105
void setColor(SkColor color)
Definition: SkPaint.cpp:119
void setAntiAlias(bool aa)
Definition: SkPaint.h:170
@ kStroke_Style
set to stroke geometry
Definition: SkPaint.h:194
SkCanvas * beginRecording(const SkRect &bounds, sk_sp< SkBBoxHierarchy > bbh)
sk_sp< SkPicture > finishRecordingAsPicture()
void printf(const char format[],...) SK_PRINTF_LIKE(2
Definition: SkString.cpp:534
const char * c_str() const
Definition: SkString.h:133
const Paint & paint
Definition: color_source.cc:38
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
Definition: SkSize.h:16
constexpr int32_t width() const
Definition: SkSize.h:36
constexpr int32_t height() const
Definition: SkSize.h:37