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) :
28        fIsSmall(small), fInitialized(false), fSigmaX(sigmaX), fSigmaY(sigmaY) {
29        fName.printf("blur_image_filter_%s_%.2f_%.2f", fIsSmall ? "small" : "large",
30            SkScalarToFloat(sigmaX), SkScalarToFloat(sigmaY));
31    }
32
33protected:
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
45    virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
46        SkPaint paint;
47        paint.setImageFilter(SkBlurImageFilter::Create(fSigmaX, fSigmaY))->unref();
48
49        for (int i = 0; i < loops; i++) {
50            canvas->drawBitmap(fCheckerboard, 0, 0, &paint);
51        }
52    }
53
54private:
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.allocN32Pixels(w, h);
59        SkCanvas canvas(fCheckerboard);
60        canvas.clear(0x00000000);
61        SkPaint darkPaint;
62        darkPaint.setColor(0xFF804020);
63        SkPaint lightPaint;
64        lightPaint.setColor(0xFF244484);
65        for (int y = 0; y < h; y += 16) {
66            for (int x = 0; x < w; x += 16) {
67                canvas.save();
68                canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
69                canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
70                canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
71                canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
72                canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
73                canvas.restore();
74            }
75        }
76    }
77
78    SkString fName;
79    bool fIsSmall;
80    bool fInitialized;
81    SkBitmap fCheckerboard;
82    SkScalar fSigmaX, fSigmaY;
83    typedef Benchmark INHERITED;
84};
85
86DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, 0, false);)
87DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, 0, false);)
88DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_LARGE, false);)
89DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_SMALL, false);)
90DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, true);)
91DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, false);)
92DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, true);)
93DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, false);)
94DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, true);)
95DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, false);)
96DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, true);)
97DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, false);)
98