Flutter Engine
The Flutter Engine
BlurImageFilterBench.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"
15#include "src/base/SkRandom.h"
16
17#define FILTER_WIDTH_SMALL 32
18#define FILTER_HEIGHT_SMALL 32
19#define FILTER_WIDTH_LARGE 256
20#define FILTER_HEIGHT_LARGE 256
21#define BLUR_SIGMA_MINI 0.5f
22#define BLUR_SIGMA_SMALL 1.0f
23#define BLUR_SIGMA_LARGE 10.0f
24#define BLUR_SIGMA_HUGE 80.0f
25
26
27// When 'cropped' is set we apply a cropRect to the blurImageFilter. The crop rect is an inset of
28// the source's natural dimensions. This is intended to exercise blurring a larger source bitmap
29// to a smaller destination bitmap.
30
31// When 'expanded' is set we apply a cropRect to the input of the blurImageFilter (a noOp
32// offsetImageFilter). The crop rect in this case is an inset of the source's natural dimensions.
33// An additional crop rect is applied to the blurImageFilter that is just the natural dimensions
34// of the source (not inset). This is intended to exercise blurring a smaller source bitmap to a
35// larger destination.
36
38 SkBitmap bm;
40 SkCanvas canvas(bm);
41 canvas.clear(0x00000000);
42 SkPaint darkPaint;
43 darkPaint.setColor(0xFF804020);
44 SkPaint lightPaint;
45 lightPaint.setColor(0xFF244484);
46 for (int y = 0; y < height; y += 16) {
47 for (int x = 0; x < width; x += 16) {
48 canvas.save();
50 canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
51 canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
52 canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
53 canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
54 canvas.restore();
55 }
56 }
57
58 return bm.asImage();
59}
60
62public:
63 BlurImageFilterBench(SkScalar sigmaX, SkScalar sigmaY, bool small, bool cropped,
64 bool expanded)
65 : fIsSmall(small)
66 , fIsCropped(cropped)
67 , fIsExpanded(expanded)
68 , fInitialized(false)
69 , fSigmaX(sigmaX)
70 , fSigmaY(sigmaY) {
71 fName.printf("blur_image_filter_%s%s%s_%.2f_%.2f",
72 fIsSmall ? "small" : "large",
73 fIsCropped ? "_cropped" : "",
74 fIsExpanded ? "_expanded" : "",
75 sigmaX, sigmaY);
76 SkASSERT(!fIsExpanded || fIsCropped); // never want expansion w/o cropping
77 }
78
79protected:
80 const char* onGetName() override {
81 return fName.c_str();
82 }
83
84 void onDelayedSetup() override {
85 if (!fInitialized) {
86 fCheckerboard = make_checkerboard(fIsSmall ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE,
88 fInitialized = true;
89 }
90 }
91
92 void onDraw(int loops, SkCanvas* canvas) override {
93 static const int kX = 0;
94 static const int kY = 0;
95 const SkIRect bmpRect = SkIRect::MakeXYWH(kX, kY, fCheckerboard->width(),
96 fCheckerboard->height());
97 const SkIRect bmpRectInset = bmpRect.makeInset(10, 10);
98
99 sk_sp<SkImageFilter> input = fIsExpanded
100 ? SkImageFilters::Offset(0, 0, nullptr, &bmpRectInset)
101 : nullptr;
102
103 const SkIRect* crop =
104 fIsExpanded ? &bmpRect : fIsCropped ? &bmpRectInset : nullptr;
106 paint.setImageFilter(SkImageFilters::Blur(fSigmaX, fSigmaY, std::move(input), crop));
108
109 for (int i = 0; i < loops; i++) {
110 canvas->drawImage(fCheckerboard, kX, kY, sampling, &paint);
111 }
112 }
113
114private:
115
116 SkString fName;
117 bool fIsSmall;
118 bool fIsCropped;
119 bool fIsExpanded;
120 bool fInitialized;
121 sk_sp<SkImage> fCheckerboard;
122 SkScalar fSigmaX, fSigmaY;
123 using INHERITED = Benchmark;
124};
125
126DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, 0, false, false, false);)
127DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, 0, false, false, false);)
128DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_LARGE, false, false, false);)
129DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_SMALL, false, false, false);)
130DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, true, false, false);)
131DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, false, false, false);)
132DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, true, false, false);)
133DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, false, false, false);)
134DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, true, false, false);)
135DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, false, false, false);)
136DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, true, false, false);)
137DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, false, false, false);)
138
139DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, 0, false, true, false);)
140DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, 0, false, true, false);)
141DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_LARGE, false, true, false);)
142DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_SMALL, false, true, false);)
143DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, true, true, false);)
144DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, false, true, false);)
145DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, true, true, false);)
146DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, false, true, false);)
147DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, true, true, false);)
148DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, false, true, false);)
149DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, true, true, false);)
150DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, false, true, false);)
151
152DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, 0, false, true, true);)
153DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, 0, false, true, true);)
154DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_LARGE, false, true, true);)
155DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_SMALL, false, true, true);)
156DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, true, true, true);)
157DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, false, true, true);)
159DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, false, true, true);)
161DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, false, true, true);)
162DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, true, true, true);)
163DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, false, true, true);)
#define DEF_BENCH(code)
Definition: Benchmark.h:20
#define FILTER_WIDTH_SMALL
#define BLUR_SIGMA_MINI
static sk_sp< SkImage > make_checkerboard(int width, int height)
#define FILTER_WIDTH_LARGE
#define BLUR_SIGMA_LARGE
#define FILTER_HEIGHT_SMALL
#define BLUR_SIGMA_HUGE
#define BLUR_SIGMA_SMALL
#define FILTER_HEIGHT_LARGE
#define SkASSERT(cond)
Definition: SkAssert.h:116
#define SkIntToScalar(x)
Definition: SkScalar.h:57
void onDelayedSetup() override
void onDraw(int loops, SkCanvas *canvas) override
BlurImageFilterBench(SkScalar sigmaX, SkScalar sigmaY, bool small, bool cropped, bool expanded)
const char * onGetName() override
sk_sp< SkImage > asImage() const
Definition: SkBitmap.cpp:645
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition: SkBitmap.cpp:232
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 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
static sk_sp< SkImageFilter > Blur(SkScalar sigmaX, SkScalar sigmaY, SkTileMode tileMode, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Offset(SkScalar dx, SkScalar dy, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
int width() const
Definition: SkImage.h:285
int height() const
Definition: SkImage.h:291
void setColor(SkColor color)
Definition: SkPaint.cpp:119
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
float SkScalar
Definition: extension.cpp:12
double y
double x
SkSamplingOptions sampling
Definition: SkRecords.h:337
int32_t height
int32_t width
Definition: SkRect.h:32
static constexpr SkIRect MakeXYWH(int32_t x, int32_t y, int32_t w, int32_t h)
Definition: SkRect.h:104
SkIRect makeInset(int32_t dx, int32_t dy) const
Definition: SkRect.h:332
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659