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 | |
| 8 | #include "SkBenchmark.h" |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 9 | #include "SkBitmapDevice.h" |
sugoi@google.com | 5d71adf | 2013-04-24 19:36:44 +0000 | [diff] [blame] | 10 | #include "SkBlurImageFilter.h" |
| 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 |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 21 | #define BLUR_SIGMA_SMALL 1.0f |
| 22 | #define BLUR_SIGMA_LARGE 10.0f |
commit-bot@chromium.org | c45ece5 | 2014-01-13 08:16:45 +0000 | [diff] [blame] | 23 | #define BLUR_SIGMA_HUGE 80.0f |
sugoi@google.com | 5d71adf | 2013-04-24 19:36:44 +0000 | [diff] [blame] | 24 | |
| 25 | class BlurImageFilterBench : public SkBenchmark { |
| 26 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 27 | BlurImageFilterBench(SkScalar sigmaX, SkScalar sigmaY, bool small) : |
| 28 | fIsSmall(small), fInitialized(false), fSigmaX(sigmaX), fSigmaY(sigmaY) { |
sugoi@google.com | 5d71adf | 2013-04-24 19:36:44 +0000 | [diff] [blame] | 29 | fName.printf("blur_image_filter_%s_%.2f_%.2f", fIsSmall ? "small" : "large", |
| 30 | SkScalarToFloat(sigmaX), SkScalarToFloat(sigmaY)); |
| 31 | } |
| 32 | |
| 33 | protected: |
| 34 | virtual const char* onGetName() SK_OVERRIDE { |
| 35 | return fName.c_str(); |
| 36 | } |
| 37 | |
| 38 | virtual void onPreDraw() SK_OVERRIDE { |
| 39 | if (!fInitialized) { |
| 40 | make_checkerboard(); |
| 41 | fInitialized = true; |
| 42 | } |
| 43 | } |
| 44 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 45 | virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
sugoi@google.com | 5d71adf | 2013-04-24 19:36:44 +0000 | [diff] [blame] | 46 | SkPaint paint; |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 47 | paint.setImageFilter(SkBlurImageFilter::Create(fSigmaX, fSigmaY))->unref(); |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 48 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 49 | for (int i = 0; i < loops; i++) { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 50 | canvas->drawBitmap(fCheckerboard, 0, 0, &paint); |
| 51 | } |
sugoi@google.com | 5d71adf | 2013-04-24 19:36:44 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | private: |
| 55 | void make_checkerboard() { |
| 56 | const int w = fIsSmall ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE; |
| 57 | const int h = fIsSmall ? FILTER_HEIGHT_LARGE : FILTER_HEIGHT_LARGE; |
| 58 | fCheckerboard.setConfig(SkBitmap::kARGB_8888_Config, w, h); |
| 59 | fCheckerboard.allocPixels(); |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 60 | SkBitmapDevice device(fCheckerboard); |
sugoi@google.com | 5d71adf | 2013-04-24 19:36:44 +0000 | [diff] [blame] | 61 | SkCanvas canvas(&device); |
| 62 | canvas.clear(0x00000000); |
| 63 | SkPaint darkPaint; |
| 64 | darkPaint.setColor(0xFF804020); |
| 65 | SkPaint lightPaint; |
| 66 | lightPaint.setColor(0xFF244484); |
| 67 | for (int y = 0; y < h; y += 16) { |
| 68 | for (int x = 0; x < w; x += 16) { |
| 69 | canvas.save(); |
| 70 | canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 71 | canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint); |
| 72 | canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint); |
| 73 | canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint); |
| 74 | canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint); |
| 75 | canvas.restore(); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | SkString fName; |
| 81 | bool fIsSmall; |
| 82 | bool fInitialized; |
| 83 | SkBitmap fCheckerboard; |
| 84 | SkScalar fSigmaX, fSigmaY; |
| 85 | typedef SkBenchmark INHERITED; |
| 86 | }; |
| 87 | |
senorblanco@chromium.org | 27eec46 | 2013-11-08 20:49:04 +0000 | [diff] [blame] | 88 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, 0, false);) |
| 89 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, 0, false);) |
| 90 | DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_LARGE, false);) |
| 91 | DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_SMALL, false);) |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 92 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, true);) |
| 93 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, false);) |
| 94 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, true);) |
| 95 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, false);) |
commit-bot@chromium.org | c45ece5 | 2014-01-13 08:16:45 +0000 | [diff] [blame] | 96 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, true);) |
| 97 | DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, false);) |