Flutter Engine
The Flutter Engine
DisplacementBench.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2013 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"
11#include "include/core/SkFont.h"
15
16#define FILTER_WIDTH_SMALL 32
17#define FILTER_HEIGHT_SMALL 32
18#define FILTER_WIDTH_LARGE 256
19#define FILTER_HEIGHT_LARGE 256
20
22public:
23 DisplacementBaseBench(bool small) : fInitialized(false), fIsSmall(small) { }
24
25protected:
26 void onDelayedSetup() override {
27 if (!fInitialized) {
28 this->makeBitmap();
29 this->makeCheckerboard();
30 fInitialized = true;
31 }
32 }
33
34 void makeBitmap() {
35 const int w = this->isSmall() ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE;
36 const int h = this->isSmall() ? FILTER_HEIGHT_SMALL : FILTER_HEIGHT_LARGE;
39 paint.setColor(0xFF884422);
40
42 font.setSize(SkIntToScalar(96));
43 surf->getCanvas()->drawSimpleText("g", 1, SkTextEncoding::kUTF8, 15, 55, font, paint);
44 fImage = surf->makeImageSnapshot();
45 }
46
48 const int w = this->isSmall() ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE;
49 const int h = this->isSmall() ? FILTER_HEIGHT_SMALL : FILTER_HEIGHT_LARGE;
51 SkCanvas* canvas = surface->getCanvas();
52 canvas->clear(0x00000000);
53 SkPaint darkPaint;
54 darkPaint.setColor(0xFF804020);
55 SkPaint lightPaint;
56 lightPaint.setColor(0xFF244484);
57 for (int y = 0; y < h; y += 16) {
58 for (int x = 0; x < w; x += 16) {
59 canvas->save();
61 canvas->drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
62 canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
63 canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
64 canvas->drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
65 canvas->restore();
66 }
67 }
68
69 fCheckerboard = surface->makeImageSnapshot();
70 }
71
72 void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) {
73 canvas->save();
74 canvas->clipIRect(fImage->bounds().makeOffset(x, y));
76 canvas->restore();
77 }
78
79 inline bool isSmall() const { return fIsSmall; }
80
82
83private:
84 bool fInitialized;
85 bool fIsSmall;
86 using INHERITED = Benchmark;
87};
88
90public:
91 DisplacementZeroBench(bool small) : INHERITED(small) { }
92
93protected:
94 const char* onGetName() override {
95 return this->isSmall() ? "displacement_zero_small" : "displacement_zero_large";
96 }
97
98 void onDraw(int loops, SkCanvas* canvas) override {
101 // No displacement effect
103 0.0f, std::move(displ), nullptr));
104
105 for (int i = 0; i < loops; i++) {
106 this->drawClippedBitmap(canvas, 0, 0, paint);
107 }
108 }
109
110private:
111 using INHERITED = DisplacementBaseBench;
112};
113
115public:
116 DisplacementAlphaBench(bool small) : INHERITED(small) { }
117
118protected:
119 const char* onGetName() override {
120 return isSmall() ? "displacement_alpha_small" : "displacement_alpha_large";
121 }
122
123 void onDraw(int loops, SkCanvas* canvas) override {
126 // Displacement, with 1 alpha component (which isn't pre-multiplied)
128 16.0f, std::move(displ), nullptr));
129 for (int i = 0; i < loops; i++) {
130 this->drawClippedBitmap(canvas, 100, 0, paint);
131 }
132 }
133
134private:
135 using INHERITED = DisplacementBaseBench;
136};
137
139public:
140 DisplacementFullBench(bool small) : INHERITED(small) { }
141
142protected:
143 const char* onGetName() override {
144 return isSmall() ? "displacement_full_small" : "displacement_full_large";
145 }
146
147 void onDraw(int loops, SkCanvas* canvas) override {
150 // Displacement, with 2 non-alpha components
152 32.0f, std::move(displ), nullptr));
153 for (int i = 0; i < loops; ++i) {
154 this->drawClippedBitmap(canvas, 200, 0, paint);
155 }
156 }
157
158private:
159 using INHERITED = DisplacementBaseBench;
160};
161
162///////////////////////////////////////////////////////////////////////////////
163
164DEF_BENCH( return new DisplacementZeroBench(true); )
165DEF_BENCH( return new DisplacementAlphaBench(true); )
166DEF_BENCH( return new DisplacementFullBench(true); )
167DEF_BENCH( return new DisplacementZeroBench(false); )
168DEF_BENCH( return new DisplacementAlphaBench(false); )
169DEF_BENCH( return new DisplacementFullBench(false); )
#define DEF_BENCH(code)
Definition: Benchmark.h:20
#define FILTER_WIDTH_SMALL
#define FILTER_WIDTH_LARGE
#define FILTER_HEIGHT_SMALL
#define FILTER_HEIGHT_LARGE
@ kUTF8
uses bytes to represent UTF-8 or ASCII
#define SkIntToScalar(x)
Definition: SkScalar.h:57
void onDraw(int loops, SkCanvas *canvas) override
const char * onGetName() override
sk_sp< SkImage > fCheckerboard
void drawClippedBitmap(SkCanvas *canvas, int x, int y, const SkPaint &paint)
void onDelayedSetup() override
const char * onGetName() override
void onDraw(int loops, SkCanvas *canvas) override
void onDraw(int loops, SkCanvas *canvas) override
const char * onGetName() override
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void clipIRect(const SkIRect &irect, SkClipOp op=SkClipOp::kIntersect)
Definition: SkCanvas.h:991
void clear(SkColor color)
Definition: SkCanvas.h:1199
int save()
Definition: SkCanvas.cpp:447
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
Definition: SkFont.h:35
static sk_sp< SkImageFilter > DisplacementMap(SkColorChannel xChannelSelector, SkColorChannel yChannelSelector, SkScalar scale, sk_sp< SkImageFilter > displacement, sk_sp< SkImageFilter > color, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Image(sk_sp< SkImage > image, const SkRect &srcRect, const SkRect &dstRect, const SkSamplingOptions &sampling)
SkIRect bounds() const
Definition: SkImage.h:303
void setColor(SkColor color)
Definition: SkPaint.cpp:119
const Paint & paint
Definition: color_source.cc:38
VkSurfaceKHR surface
Definition: main.cc:49
double y
double x
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
SkFont DefaultFont()
font
Font Metadata and Metrics.
SkSamplingOptions(SkFilterMode::kLinear))
SkScalar w
SkScalar h
constexpr SkIRect makeOffset(int32_t dx, int32_t dy) const
Definition: SkRect.h:300
static SkImageInfo MakeN32Premul(int width, int height)
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659