sugoi@google.com | 5d71adf | 2013-04-24 19:36:44 +0000 | [diff] [blame] | 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 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 8 | #include "Benchmark.h" |
sugoi@google.com | 5d71adf | 2013-04-24 19:36:44 +0000 | [diff] [blame] | 9 | #include "SkBlurImageFilter.h" |
senorblanco | a8bd38e | 2015-10-30 13:17:20 -0700 | [diff] [blame] | 10 | #include "SkOffsetImageFilter.h" |
sugoi@google.com | 5d71adf | 2013-04-24 19:36:44 +0000 | [diff] [blame] | 11 | #include "SkCanvas.h" |
sugoi@google.com | 5d71adf | 2013-04-24 19:36:44 +0000 | [diff] [blame] | 12 | #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 |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 21 | #define BLUR_SIGMA_MINI 0.5f |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 22 | #define BLUR_SIGMA_SMALL 1.0f |
| 23 | #define BLUR_SIGMA_LARGE 10.0f |
commit-bot@chromium.org | c45ece5 | 2014-01-13 08:16:45 +0000 | [diff] [blame] | 24 | #define BLUR_SIGMA_HUGE 80.0f |
sugoi@google.com | 5d71adf | 2013-04-24 19:36:44 +0000 | [diff] [blame] | 25 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 26 | |
senorblanco | a8bd38e | 2015-10-30 13:17:20 -0700 | [diff] [blame] | 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 | |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 37 | static SkBitmap make_checkerboard(int width, int height) { |
| 38 | 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 | |
| 58 | return bm; |
| 59 | } |
| 60 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 61 | class BlurImageFilterBench : public Benchmark { |
sugoi@google.com | 5d71adf | 2013-04-24 19:36:44 +0000 | [diff] [blame] | 62 | public: |
senorblanco | a8bd38e | 2015-10-30 13:17:20 -0700 | [diff] [blame] | 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 | SkScalarToFloat(sigmaX), SkScalarToFloat(sigmaY)); |
| 76 | SkASSERT(!fIsExpanded || fIsCropped); // never want expansion w/o cropping |
sugoi@google.com | 5d71adf | 2013-04-24 19:36:44 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 80 | const char* onGetName() override { |
sugoi@google.com | 5d71adf | 2013-04-24 19:36:44 +0000 | [diff] [blame] | 81 | return fName.c_str(); |
| 82 | } |
| 83 | |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 84 | void onDelayedSetup() override { |
sugoi@google.com | 5d71adf | 2013-04-24 19:36:44 +0000 | [diff] [blame] | 85 | if (!fInitialized) { |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 86 | fCheckerboard = make_checkerboard(fIsSmall ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE, |
| 87 | fIsSmall ? FILTER_HEIGHT_SMALL : FILTER_HEIGHT_LARGE); |
sugoi@google.com | 5d71adf | 2013-04-24 19:36:44 +0000 | [diff] [blame] | 88 | fInitialized = true; |
| 89 | } |
| 90 | } |
| 91 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 92 | void onDraw(int loops, SkCanvas* canvas) override { |
bsalomon | 2757e3f | 2015-06-30 07:42:42 -0700 | [diff] [blame] | 93 | static const SkScalar kX = 0; |
| 94 | static const SkScalar kY = 0; |
| 95 | const SkRect bmpRect = SkRect::MakeXYWH(kX, kY, |
| 96 | SkIntToScalar(fCheckerboard.width()), |
| 97 | SkIntToScalar(fCheckerboard.height())); |
senorblanco | a8bd38e | 2015-10-30 13:17:20 -0700 | [diff] [blame] | 98 | const SkImageFilter::CropRect cropRect(bmpRect.makeInset(10.f, 10.f)); |
| 99 | const SkImageFilter::CropRect cropRectLarge(bmpRect); |
bsalomon | 2757e3f | 2015-06-30 07:42:42 -0700 | [diff] [blame] | 100 | |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 101 | sk_sp<SkImageFilter> input = fIsExpanded |
| 102 | ? SkOffsetImageFilter::Make(0, 0, nullptr, &cropRect) |
| 103 | : nullptr; |
senorblanco | a8bd38e | 2015-10-30 13:17:20 -0700 | [diff] [blame] | 104 | |
| 105 | const SkImageFilter::CropRect* crop = |
| 106 | fIsExpanded ? &cropRectLarge : fIsCropped ? &cropRect : nullptr; |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 107 | SkPaint paint; |
| 108 | paint.setImageFilter(SkBlurImageFilter::Make(fSigmaX, fSigmaY, std::move(input), crop)); |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 109 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 110 | for (int i = 0; i < loops; i++) { |
bsalomon | 2757e3f | 2015-06-30 07:42:42 -0700 | [diff] [blame] | 111 | canvas->drawBitmap(fCheckerboard, kX, kY, &paint); |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 112 | } |
sugoi@google.com | 5d71adf | 2013-04-24 19:36:44 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | private: |
sugoi@google.com | 5d71adf | 2013-04-24 19:36:44 +0000 | [diff] [blame] | 116 | |
| 117 | SkString fName; |
| 118 | bool fIsSmall; |
bsalomon | 2757e3f | 2015-06-30 07:42:42 -0700 | [diff] [blame] | 119 | bool fIsCropped; |
senorblanco | a8bd38e | 2015-10-30 13:17:20 -0700 | [diff] [blame] | 120 | bool fIsExpanded; |
sugoi@google.com | 5d71adf | 2013-04-24 19:36:44 +0000 | [diff] [blame] | 121 | bool fInitialized; |
| 122 | SkBitmap fCheckerboard; |
| 123 | SkScalar fSigmaX, fSigmaY; |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 124 | typedef Benchmark INHERITED; |
sugoi@google.com | 5d71adf | 2013-04-24 19:36:44 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
senorblanco | a8bd38e | 2015-10-30 13:17:20 -0700 | [diff] [blame] | 127 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, 0, false, false, false);) |
| 128 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, 0, false, false, false);) |
| 129 | DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_LARGE, false, false, false);) |
| 130 | DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_SMALL, false, false, false);) |
| 131 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, true, false, false);) |
| 132 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, false, false, false);) |
| 133 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, true, false, false);) |
| 134 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, false, false, false);) |
| 135 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, true, false, false);) |
| 136 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, false, false, false);) |
| 137 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, true, false, false);) |
| 138 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, false, false, false);) |
bsalomon | 2757e3f | 2015-06-30 07:42:42 -0700 | [diff] [blame] | 139 | |
senorblanco | a8bd38e | 2015-10-30 13:17:20 -0700 | [diff] [blame] | 140 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, 0, false, true, false);) |
| 141 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, 0, false, true, false);) |
| 142 | DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_LARGE, false, true, false);) |
| 143 | DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_SMALL, false, true, false);) |
| 144 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, true, true, false);) |
| 145 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, false, true, false);) |
| 146 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, true, true, false);) |
| 147 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, false, true, false);) |
| 148 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, true, true, false);) |
| 149 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, false, true, false);) |
| 150 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, true, true, false);) |
| 151 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, false, true, false);) |
| 152 | |
| 153 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, 0, false, true, true);) |
| 154 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, 0, false, true, true);) |
| 155 | DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_LARGE, false, true, true);) |
| 156 | DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_SMALL, false, true, true);) |
| 157 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, true, true, true);) |
| 158 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, false, true, true);) |
| 159 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, true, true, true);) |
| 160 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, false, true, true);) |
| 161 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, true, true, true);) |
| 162 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, false, true, true);) |
| 163 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, true, true, true);) |
| 164 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, false, true, true);) |