blob: d7d3eb2bc324f3e18ccc985887347607268edbc9 [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"
10#include "SkCanvas.h"
sugoi@google.com5d71adf2013-04-24 19:36:44 +000011#include "SkPaint.h"
12#include "SkRandom.h"
13#include "SkShader.h"
14#include "SkString.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
joshualitt5acfea72014-08-11 13:55:34 -070020#define BLUR_SIGMA_MINI 0.5f
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000021#define BLUR_SIGMA_SMALL 1.0f
22#define BLUR_SIGMA_LARGE 10.0f
commit-bot@chromium.orgc45ece52014-01-13 08:16:45 +000023#define BLUR_SIGMA_HUGE 80.0f
sugoi@google.com5d71adf2013-04-24 19:36:44 +000024
tfarinaf168b862014-06-19 12:32:29 -070025class BlurImageFilterBench : public Benchmark {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000026public:
bsalomon2757e3f2015-06-30 07:42:42 -070027 BlurImageFilterBench(SkScalar sigmaX, SkScalar sigmaY, bool small, bool cropped) :
28 fIsSmall(small), fIsCropped(cropped), fInitialized(false), fSigmaX(sigmaX), fSigmaY(sigmaY) {
29 fName.printf("blur_image_filter_%s%s_%.2f_%.2f", fIsSmall ? "small" : "large",
30 fIsCropped ? "_cropped" : "", SkScalarToFloat(sigmaX), SkScalarToFloat(sigmaY));
sugoi@google.com5d71adf2013-04-24 19:36:44 +000031 }
32
33protected:
mtklein36352bf2015-03-25 18:17:31 -070034 const char* onGetName() override {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000035 return fName.c_str();
36 }
37
joshualitt8a6697a2015-09-30 12:11:07 -070038 void onDelayedSetup() override {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000039 if (!fInitialized) {
40 make_checkerboard();
41 fInitialized = true;
42 }
43 }
44
mtklein36352bf2015-03-25 18:17:31 -070045 void onDraw(const int loops, SkCanvas* canvas) override {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000046 SkPaint paint;
bsalomon2757e3f2015-06-30 07:42:42 -070047 static const SkScalar kX = 0;
48 static const SkScalar kY = 0;
49 const SkRect bmpRect = SkRect::MakeXYWH(kX, kY,
50 SkIntToScalar(fCheckerboard.width()),
51 SkIntToScalar(fCheckerboard.height()));
52 const SkImageFilter::CropRect cropRect =
53 SkImageFilter::CropRect(bmpRect.makeInset(10.f, 10.f));
halcanary96fcdcc2015-08-27 07:41:13 -070054 const SkImageFilter::CropRect* crop = fIsCropped ? &cropRect : nullptr;
bsalomon2757e3f2015-06-30 07:42:42 -070055
halcanary96fcdcc2015-08-27 07:41:13 -070056 paint.setImageFilter(SkBlurImageFilter::Create(fSigmaX, fSigmaY, nullptr, crop))->unref();
mtklein@google.comc2897432013-09-10 19:23:38 +000057
commit-bot@chromium.org33614712013-12-03 18:17:16 +000058 for (int i = 0; i < loops; i++) {
bsalomon2757e3f2015-06-30 07:42:42 -070059 canvas->drawBitmap(fCheckerboard, kX, kY, &paint);
mtklein@google.comc2897432013-09-10 19:23:38 +000060 }
sugoi@google.com5d71adf2013-04-24 19:36:44 +000061 }
62
63private:
64 void make_checkerboard() {
65 const int w = fIsSmall ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE;
66 const int h = fIsSmall ? FILTER_HEIGHT_LARGE : FILTER_HEIGHT_LARGE;
reed6c225732014-06-09 19:52:07 -070067 fCheckerboard.allocN32Pixels(w, h);
68 SkCanvas canvas(fCheckerboard);
sugoi@google.com5d71adf2013-04-24 19:36:44 +000069 canvas.clear(0x00000000);
70 SkPaint darkPaint;
71 darkPaint.setColor(0xFF804020);
72 SkPaint lightPaint;
73 lightPaint.setColor(0xFF244484);
74 for (int y = 0; y < h; y += 16) {
75 for (int x = 0; x < w; x += 16) {
76 canvas.save();
77 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
78 canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
79 canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
80 canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
81 canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
82 canvas.restore();
83 }
84 }
85 }
86
87 SkString fName;
88 bool fIsSmall;
bsalomon2757e3f2015-06-30 07:42:42 -070089 bool fIsCropped;
sugoi@google.com5d71adf2013-04-24 19:36:44 +000090 bool fInitialized;
91 SkBitmap fCheckerboard;
92 SkScalar fSigmaX, fSigmaY;
tfarinaf168b862014-06-19 12:32:29 -070093 typedef Benchmark INHERITED;
sugoi@google.com5d71adf2013-04-24 19:36:44 +000094};
95
bsalomon2757e3f2015-06-30 07:42:42 -070096DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, 0, false, false);)
97DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, 0, false, false);)
98DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_LARGE, false, false);)
99DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_SMALL, false, false);)
100DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, true, false);)
101DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, false, false);)
102DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, true, false);)
103DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, false, false);)
104DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, true, false);)
105DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, false, false);)
106DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, true, false);)
107DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, false, false);)
108
109DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, 0, false, true);)
110DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, 0, false, true);)
111DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_LARGE, false, true);)
112DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_SMALL, false, true);)
113DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, true, true);)
114DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, false, true);)
115DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, true, true);)
116DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, false, true);)
117DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, true, true);)
118DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, false, true);)
119DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, true, true);)
120DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, false, true);)