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