BlurImageFilterBench.cpp revision 8a6697af95b340aad6dee7e6228048fa305c1e59
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 "SkCanvas.h"
11#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
20#define BLUR_SIGMA_MINI     0.5f
21#define BLUR_SIGMA_SMALL    1.0f
22#define BLUR_SIGMA_LARGE    10.0f
23#define BLUR_SIGMA_HUGE     80.0f
24
25class BlurImageFilterBench : public Benchmark {
26public:
27    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));
31    }
32
33protected:
34    const char* onGetName() override {
35        return fName.c_str();
36    }
37
38    void onDelayedSetup() override {
39        if (!fInitialized) {
40            make_checkerboard();
41            fInitialized = true;
42        }
43    }
44
45    void onDraw(const int loops, SkCanvas* canvas) override {
46        SkPaint paint;
47        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));
54        const SkImageFilter::CropRect* crop = fIsCropped ? &cropRect : nullptr;
55
56        paint.setImageFilter(SkBlurImageFilter::Create(fSigmaX, fSigmaY, nullptr, crop))->unref();
57
58        for (int i = 0; i < loops; i++) {
59            canvas->drawBitmap(fCheckerboard, kX, kY, &paint);
60        }
61    }
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;
67        fCheckerboard.allocN32Pixels(w, h);
68        SkCanvas canvas(fCheckerboard);
69        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;
89    bool fIsCropped;
90    bool fInitialized;
91    SkBitmap fCheckerboard;
92    SkScalar fSigmaX, fSigmaY;
93    typedef Benchmark INHERITED;
94};
95
96DEF_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);)
121