blob: 337b4e4f0e041e5ffd2becc282b4b6dda7018063 [file] [log] [blame]
sugoi@google.com5d71adf2013-04-24 19:36:44 +00001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#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"
Michael Ludwig23003182019-08-05 11:25:23 -040014#include "include/effects/SkImageFilters.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/utils/SkRandom.h"
sugoi@google.com5d71adf2013-04-24 19:36:44 +000016
17#define FILTER_WIDTH_SMALL 32
18#define FILTER_HEIGHT_SMALL 32
19#define FILTER_WIDTH_LARGE 256
20#define FILTER_HEIGHT_LARGE 256
joshualitt5acfea72014-08-11 13:55:34 -070021#define BLUR_SIGMA_MINI 0.5f
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000022#define BLUR_SIGMA_SMALL 1.0f
23#define BLUR_SIGMA_LARGE 10.0f
commit-bot@chromium.orgc45ece52014-01-13 08:16:45 +000024#define BLUR_SIGMA_HUGE 80.0f
sugoi@google.com5d71adf2013-04-24 19:36:44 +000025
halcanary9d524f22016-03-29 09:03:52 -070026
senorblancoa8bd38e2015-10-30 13:17:20 -070027// 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
Mike Reed069e4842021-01-24 11:39:58 -050037static sk_sp<SkImage> make_checkerboard(int width, int height) {
robertphillips6e7025a2016-04-04 04:31:25 -070038 SkBitmap bm;
39 bm.allocN32Pixels(width, height);
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();
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);
54 canvas.restore();
55 }
56 }
57
Mike Reed069e4842021-01-24 11:39:58 -050058 return bm.asImage();
robertphillips6e7025a2016-04-04 04:31:25 -070059}
60
tfarinaf168b862014-06-19 12:32:29 -070061class BlurImageFilterBench : public Benchmark {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000062public:
senorblancoa8bd38e2015-10-30 13:17:20 -070063 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 SkScalarToFloat(sigmaX), SkScalarToFloat(sigmaY));
76 SkASSERT(!fIsExpanded || fIsCropped); // never want expansion w/o cropping
sugoi@google.com5d71adf2013-04-24 19:36:44 +000077 }
78
79protected:
mtklein36352bf2015-03-25 18:17:31 -070080 const char* onGetName() override {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000081 return fName.c_str();
82 }
83
joshualitt8a6697a2015-09-30 12:11:07 -070084 void onDelayedSetup() override {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000085 if (!fInitialized) {
robertphillips6e7025a2016-04-04 04:31:25 -070086 fCheckerboard = make_checkerboard(fIsSmall ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE,
87 fIsSmall ? FILTER_HEIGHT_SMALL : FILTER_HEIGHT_LARGE);
sugoi@google.com5d71adf2013-04-24 19:36:44 +000088 fInitialized = true;
89 }
90 }
91
mtkleina1ebeb22015-10-01 09:43:39 -070092 void onDraw(int loops, SkCanvas* canvas) override {
Michael Ludwig23003182019-08-05 11:25:23 -040093 static const int kX = 0;
94 static const int kY = 0;
Mike Reed069e4842021-01-24 11:39:58 -050095 const SkIRect bmpRect = SkIRect::MakeXYWH(kX, kY, fCheckerboard->width(),
96 fCheckerboard->height());
Michael Ludwig23003182019-08-05 11:25:23 -040097 const SkIRect bmpRectInset = bmpRect.makeInset(10, 10);
bsalomon2757e3f2015-06-30 07:42:42 -070098
Ben Wagner63fd7602017-10-09 15:45:33 -040099 sk_sp<SkImageFilter> input = fIsExpanded
Michael Ludwig23003182019-08-05 11:25:23 -0400100 ? SkImageFilters::Offset(0, 0, nullptr, &bmpRectInset)
robertphillips6e7025a2016-04-04 04:31:25 -0700101 : nullptr;
senorblancoa8bd38e2015-10-30 13:17:20 -0700102
Michael Ludwig23003182019-08-05 11:25:23 -0400103 const SkIRect* crop =
104 fIsExpanded ? &bmpRect : fIsCropped ? &bmpRectInset : nullptr;
robertphillips6e7025a2016-04-04 04:31:25 -0700105 SkPaint paint;
Michael Ludwig23003182019-08-05 11:25:23 -0400106 paint.setImageFilter(SkImageFilters::Blur(fSigmaX, fSigmaY, std::move(input), crop));
Mike Reed069e4842021-01-24 11:39:58 -0500107 SkSamplingOptions sampling;
mtklein@google.comc2897432013-09-10 19:23:38 +0000108
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000109 for (int i = 0; i < loops; i++) {
Mike Reed069e4842021-01-24 11:39:58 -0500110 canvas->drawImage(fCheckerboard, kX, kY, sampling, &paint);
mtklein@google.comc2897432013-09-10 19:23:38 +0000111 }
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000112 }
113
114private:
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000115
116 SkString fName;
117 bool fIsSmall;
bsalomon2757e3f2015-06-30 07:42:42 -0700118 bool fIsCropped;
senorblancoa8bd38e2015-10-30 13:17:20 -0700119 bool fIsExpanded;
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000120 bool fInitialized;
Mike Reed069e4842021-01-24 11:39:58 -0500121 sk_sp<SkImage> fCheckerboard;
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000122 SkScalar fSigmaX, fSigmaY;
John Stiles7571f9e2020-09-02 22:42:33 -0400123 using INHERITED = Benchmark;
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000124};
125
senorblancoa8bd38e2015-10-30 13:17:20 -0700126DEF_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);)
bsalomon2757e3f2015-06-30 07:42:42 -0700138
senorblancoa8bd38e2015-10-30 13:17:20 -0700139DEF_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);)
158DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, true, true, true);)
159DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, false, true, true);)
160DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, true, 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);)