BlurImageFilterBench.cpp revision 8c0c7b0bcdf17ff672a972748f692a41c22aa0a1
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_SMALL    1.0f
21#define BLUR_SIGMA_LARGE    10.0f
22#define BLUR_SIGMA_HUGE     80.0f
23
24class BlurImageFilterBench : public Benchmark {
25public:
26    BlurImageFilterBench(SkScalar sigmaX, SkScalar sigmaY,  bool small) :
27        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
32protected:
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(const int loops, SkCanvas* canvas) SK_OVERRIDE {
45        SkPaint paint;
46        paint.setImageFilter(SkBlurImageFilter::Create(fSigmaX, fSigmaY))->unref();
47
48        for (int i = 0; i < loops; i++) {
49            canvas->drawBitmap(fCheckerboard, 0, 0, &paint);
50        }
51    }
52
53private:
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.allocN32Pixels(w, h);
58        SkCanvas canvas(fCheckerboard);
59        canvas.clear(0x00000000);
60        SkPaint darkPaint;
61        darkPaint.setColor(0xFF804020);
62        SkPaint lightPaint;
63        lightPaint.setColor(0xFF244484);
64        for (int y = 0; y < h; y += 16) {
65            for (int x = 0; x < w; x += 16) {
66                canvas.save();
67                canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
68                canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
69                canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
70                canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
71                canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
72                canvas.restore();
73            }
74        }
75    }
76
77    SkString fName;
78    bool fIsSmall;
79    bool fInitialized;
80    SkBitmap fCheckerboard;
81    SkScalar fSigmaX, fSigmaY;
82    typedef Benchmark INHERITED;
83};
84
85DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, 0, false);)
86DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, 0, false);)
87DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_LARGE, false);)
88DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_SMALL, false);)
89DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, true);)
90DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, false);)
91DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, true);)
92DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, false);)
93DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, true);)
94DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, false);)
95