BlurImageFilterBench.cpp revision 51a315eff9b86bd60e7884240c4efc199129d37a
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 "Benchmark.h" 9#include "SkBlurImageFilter.h" 10#include "SkOffsetImageFilter.h" 11#include "SkCanvas.h" 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_MINI 0.5f 22#define BLUR_SIGMA_SMALL 1.0f 23#define BLUR_SIGMA_LARGE 10.0f 24#define BLUR_SIGMA_HUGE 80.0f 25 26 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 37class BlurImageFilterBench : public Benchmark { 38public: 39 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 53 } 54 55protected: 56 const char* onGetName() override { 57 return fName.c_str(); 58 } 59 60 void onDelayedSetup() override { 61 if (!fInitialized) { 62 make_checkerboard(); 63 fInitialized = true; 64 } 65 } 66 67 void onDraw(int loops, SkCanvas* canvas) override { 68 SkPaint paint; 69 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())); 74 const SkImageFilter::CropRect cropRect(bmpRect.makeInset(10.f, 10.f)); 75 const SkImageFilter::CropRect cropRectLarge(bmpRect); 76 sk_sp<SkImageFilter> noOpCropped(SkOffsetImageFilter::Make(0, 0, nullptr, &cropRect)); 77 78 SkImageFilter* input = fIsExpanded ? noOpCropped.get() : nullptr; 79 80 const SkImageFilter::CropRect* crop = 81 fIsExpanded ? &cropRectLarge : fIsCropped ? &cropRect : nullptr; 82 SkAutoTUnref<SkImageFilter> blur(SkBlurImageFilter::Create(fSigmaX, fSigmaY, input, crop)); 83 paint.setImageFilter(blur); 84 85 for (int i = 0; i < loops; i++) { 86 canvas->drawBitmap(fCheckerboard, kX, kY, &paint); 87 } 88 } 89 90private: 91 void make_checkerboard() { 92 const int w = fIsSmall ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE; 93 const int h = fIsSmall ? FILTER_HEIGHT_LARGE : FILTER_HEIGHT_LARGE; 94 fCheckerboard.allocN32Pixels(w, h); 95 SkCanvas canvas(fCheckerboard); 96 canvas.clear(0x00000000); 97 SkPaint darkPaint; 98 darkPaint.setColor(0xFF804020); 99 SkPaint lightPaint; 100 lightPaint.setColor(0xFF244484); 101 for (int y = 0; y < h; y += 16) { 102 for (int x = 0; x < w; x += 16) { 103 canvas.save(); 104 canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); 105 canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint); 106 canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint); 107 canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint); 108 canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint); 109 canvas.restore(); 110 } 111 } 112 } 113 114 SkString fName; 115 bool fIsSmall; 116 bool fIsCropped; 117 bool fIsExpanded; 118 bool fInitialized; 119 SkBitmap fCheckerboard; 120 SkScalar fSigmaX, fSigmaY; 121 typedef Benchmark INHERITED; 122}; 123 124DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, 0, false, false, false);) 125DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, 0, false, false, false);) 126DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_LARGE, false, false, false);) 127DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_SMALL, false, false, false);) 128DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, true, false, false);) 129DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, false, false, false);) 130DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, true, false, false);) 131DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, false, false, false);) 132DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, true, false, false);) 133DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, false, false, false);) 134DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, true, false, false);) 135DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, false, false, false);) 136 137DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, 0, false, true, false);) 138DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, 0, false, true, false);) 139DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_LARGE, false, true, false);) 140DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_SMALL, false, true, false);) 141DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, true, true, false);) 142DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, false, true, false);) 143DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, true, true, false);) 144DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, false, true, false);) 145DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, true, true, false);) 146DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, false, true, false);) 147DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, true, true, false);) 148DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, false, true, false);) 149 150DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, 0, false, true, true);) 151DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, 0, false, true, true);) 152DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_LARGE, false, true, true);) 153DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_SMALL, false, true, true);) 154DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, true, true, true);) 155DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, false, true, true);) 156DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, true, true, true);) 157DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, false, true, true);) 158DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, true, true, true);) 159DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, false, true, true);) 160DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, true, true, true);) 161DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, false, true, true);) 162