blob: f0a7a8eea0315dc54e2dd7e4fdb14cd4dd61ef32 [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
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
sugoi@google.com5d71adf2013-04-24 19:36:44 +00009#include "SkBlurImageFilter.h"
senorblancoa8bd38e2015-10-30 13:17:20 -070010#include "SkOffsetImageFilter.h"
sugoi@google.com5d71adf2013-04-24 19:36:44 +000011#include "SkCanvas.h"
sugoi@google.com5d71adf2013-04-24 19:36:44 +000012#include "SkPaint.h"
13#include "SkRandom.h"
14#include "SkShader.h"
15#include "SkString.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
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
senorblancoa8bd38e2015-10-30 13:17:20 -070026
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
tfarinaf168b862014-06-19 12:32:29 -070037class BlurImageFilterBench : public Benchmark {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000038public:
senorblancoa8bd38e2015-10-30 13:17:20 -070039 BlurImageFilterBench(SkScalar sigmaX, SkScalar sigmaY, bool small, bool cropped,
40 bool expanded)
41 : fIsSmall(small)
42 , fIsCropped(cropped)
43 , fIsExpanded(expanded)
44 , fInitialized(false)
45 , fSigmaX(sigmaX)
46 , fSigmaY(sigmaY) {
47 fName.printf("blur_image_filter_%s%s%s_%.2f_%.2f",
48 fIsSmall ? "small" : "large",
49 fIsCropped ? "_cropped" : "",
50 fIsExpanded ? "_expanded" : "",
51 SkScalarToFloat(sigmaX), SkScalarToFloat(sigmaY));
52 SkASSERT(!fIsExpanded || fIsCropped); // never want expansion w/o cropping
sugoi@google.com5d71adf2013-04-24 19:36:44 +000053 }
54
55protected:
mtklein36352bf2015-03-25 18:17:31 -070056 const char* onGetName() override {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000057 return fName.c_str();
58 }
59
joshualitt8a6697a2015-09-30 12:11:07 -070060 void onDelayedSetup() override {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000061 if (!fInitialized) {
62 make_checkerboard();
63 fInitialized = true;
64 }
65 }
66
mtkleina1ebeb22015-10-01 09:43:39 -070067 void onDraw(int loops, SkCanvas* canvas) override {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000068 SkPaint paint;
bsalomon2757e3f2015-06-30 07:42:42 -070069 static const SkScalar kX = 0;
70 static const SkScalar kY = 0;
71 const SkRect bmpRect = SkRect::MakeXYWH(kX, kY,
72 SkIntToScalar(fCheckerboard.width()),
73 SkIntToScalar(fCheckerboard.height()));
senorblancoa8bd38e2015-10-30 13:17:20 -070074 const SkImageFilter::CropRect cropRect(bmpRect.makeInset(10.f, 10.f));
75 const SkImageFilter::CropRect cropRectLarge(bmpRect);
76 SkAutoTUnref<SkImageFilter> noOpCropped(SkOffsetImageFilter::Create(0, 0, nullptr,
77 &cropRect));
bsalomon2757e3f2015-06-30 07:42:42 -070078
senorblancoa8bd38e2015-10-30 13:17:20 -070079 SkImageFilter* input = fIsExpanded ? noOpCropped.get() : nullptr;
80
81 const SkImageFilter::CropRect* crop =
82 fIsExpanded ? &cropRectLarge : fIsCropped ? &cropRect : nullptr;
xidachen467ddc02015-12-10 12:08:44 -080083 SkAutoTUnref<SkImageFilter> blur(SkBlurImageFilter::Create(fSigmaX, fSigmaY, input, crop));
84 paint.setImageFilter(blur);
mtklein@google.comc2897432013-09-10 19:23:38 +000085
commit-bot@chromium.org33614712013-12-03 18:17:16 +000086 for (int i = 0; i < loops; i++) {
bsalomon2757e3f2015-06-30 07:42:42 -070087 canvas->drawBitmap(fCheckerboard, kX, kY, &paint);
mtklein@google.comc2897432013-09-10 19:23:38 +000088 }
sugoi@google.com5d71adf2013-04-24 19:36:44 +000089 }
90
91private:
92 void make_checkerboard() {
93 const int w = fIsSmall ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE;
94 const int h = fIsSmall ? FILTER_HEIGHT_LARGE : FILTER_HEIGHT_LARGE;
reed6c225732014-06-09 19:52:07 -070095 fCheckerboard.allocN32Pixels(w, h);
96 SkCanvas canvas(fCheckerboard);
sugoi@google.com5d71adf2013-04-24 19:36:44 +000097 canvas.clear(0x00000000);
98 SkPaint darkPaint;
99 darkPaint.setColor(0xFF804020);
100 SkPaint lightPaint;
101 lightPaint.setColor(0xFF244484);
102 for (int y = 0; y < h; y += 16) {
103 for (int x = 0; x < w; x += 16) {
104 canvas.save();
105 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
106 canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
107 canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
108 canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
109 canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
110 canvas.restore();
111 }
112 }
113 }
114
115 SkString fName;
116 bool fIsSmall;
bsalomon2757e3f2015-06-30 07:42:42 -0700117 bool fIsCropped;
senorblancoa8bd38e2015-10-30 13:17:20 -0700118 bool fIsExpanded;
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000119 bool fInitialized;
120 SkBitmap fCheckerboard;
121 SkScalar fSigmaX, fSigmaY;
tfarinaf168b862014-06-19 12:32:29 -0700122 typedef Benchmark INHERITED;
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000123};
124
senorblancoa8bd38e2015-10-30 13:17:20 -0700125DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, 0, false, false, false);)
126DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, 0, false, false, false);)
127DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_LARGE, false, false, false);)
128DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_SMALL, false, false, false);)
129DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, true, false, false);)
130DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, false, false, false);)
131DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, true, false, false);)
132DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, false, false, false);)
133DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, true, false, false);)
134DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, false, false, false);)
135DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, true, false, false);)
136DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, false, false, false);)
bsalomon2757e3f2015-06-30 07:42:42 -0700137
senorblancoa8bd38e2015-10-30 13:17:20 -0700138DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, 0, false, true, false);)
139DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, 0, false, true, false);)
140DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_LARGE, false, true, false);)
141DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_SMALL, false, true, false);)
142DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, true, true, false);)
143DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, false, true, false);)
144DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, true, true, false);)
145DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, false, true, false);)
146DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, true, true, false);)
147DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, false, true, false);)
148DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, true, true, false);)
149DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, false, true, false);)
150
151DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, 0, false, true, true);)
152DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, 0, false, true, true);)
153DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_LARGE, false, true, true);)
154DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_SMALL, false, true, true);)
155DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, true, true, true);)
156DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, false, true, true);)
157DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, true, true, true);)
158DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, false, true, true);)
159DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, true, true, true);)
160DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, false, true, true);)
161DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, true, true, true);)
162DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, false, true, true);)