2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "bench/Benchmark.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkShader.h"
13 #include "include/core/SkString.h"
14 #include "include/effects/SkImageFilters.h"
15 #include "include/utils/SkRandom.h"
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
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.
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.
37 static sk_sp<SkImage> make_checkerboard(int width, int height) {
39 bm.allocN32Pixels(width, height);
41 canvas.clear(0x00000000);
43 darkPaint.setColor(0xFF804020);
45 lightPaint.setColor(0xFF244484);
46 for (int y = 0; y < height; y += 16) {
47 for (int x = 0; x < width; x += 16) {
49 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
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);
61 class BlurImageFilterBench : public Benchmark {
63 BlurImageFilterBench(SkScalar sigmaX, SkScalar sigmaY, bool small, bool cropped,
67 , fIsExpanded(expanded)
71 fName.printf("blur_image_filter_%s%s%s_%.2f_%.2f",
72 fIsSmall ? "small" : "large",
73 fIsCropped ? "_cropped" : "",
74 fIsExpanded ? "_expanded" : "",
75 SkScalarToFloat(sigmaX), SkScalarToFloat(sigmaY));
76 SkASSERT(!fIsExpanded || fIsCropped); // never want expansion w/o cropping
80 const char* onGetName() override {
84 void onDelayedSetup() override {
86 fCheckerboard = make_checkerboard(fIsSmall ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE,
87 fIsSmall ? FILTER_HEIGHT_SMALL : FILTER_HEIGHT_LARGE);
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);
99 sk_sp<SkImageFilter> input = fIsExpanded
100 ? SkImageFilters::Offset(0, 0, nullptr, &bmpRectInset)
103 const SkIRect* crop =
104 fIsExpanded ? &bmpRect : fIsCropped ? &bmpRectInset : nullptr;
106 paint.setImageFilter(SkImageFilters::Blur(fSigmaX, fSigmaY, std::move(input), crop));
107 SkSamplingOptions sampling;
109 for (int i = 0; i < loops; i++) {
110 canvas->drawImage(fCheckerboard, kX, kY, sampling, &paint);
121 sk_sp<SkImage> fCheckerboard;
122 SkScalar fSigmaX, fSigmaY;
123 using INHERITED = Benchmark;
126 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, 0, false, false, false);)
127 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, 0, false, false, false);)
128 DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_LARGE, false, false, false);)
129 DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_SMALL, false, false, false);)
130 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, true, false, false);)
131 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, false, false, false);)
132 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, true, false, false);)
133 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, false, false, false);)
134 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, true, false, false);)
135 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, false, false, false);)
136 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, true, false, false);)
137 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, false, false, false);)
139 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, 0, false, true, false);)
140 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, 0, false, true, false);)
141 DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_LARGE, false, true, false);)
142 DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_SMALL, false, true, false);)
143 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, true, true, false);)
144 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, false, true, false);)
145 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, true, true, false);)
146 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, false, true, false);)
147 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, true, true, false);)
148 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, false, true, false);)
149 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, true, true, false);)
150 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, false, true, false);)
152 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, 0, false, true, true);)
153 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, 0, false, true, true);)
154 DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_LARGE, false, true, true);)
155 DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_SMALL, false, true, true);)
156 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, true, true, true);)
157 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, false, true, true);)
158 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, true, true, true);)
159 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, false, true, true);)
160 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, true, true, true);)
161 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, false, true, true);)
162 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, true, true, true);)
163 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, false, true, true);)