1/*
2 * Copyright 2012 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#include "Benchmark.h"
8#include "SkCanvas.h"
9#include "SkMatrixConvolutionImageFilter.h"
10#include "SkPaint.h"
11#include "SkRandom.h"
12#include "SkString.h"
13
14class MatrixConvolutionBench : public Benchmark {
15public:
16    MatrixConvolutionBench(SkMatrixConvolutionImageFilter::TileMode tileMode, bool convolveAlpha)
17        : fName("matrixconvolution") {
18        SkISize kernelSize = SkISize::Make(3, 3);
19        SkScalar kernel[9] = {
20            SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1),
21            SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1),
22            SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1),
23        };
24        SkScalar gain = 0.3f, bias = SkIntToScalar(100);
25        SkIPoint kernelOffset = SkIPoint::Make(1, 1);
26        fFilter = SkMatrixConvolutionImageFilter::Create(kernelSize, kernel, gain, bias, kernelOffset, tileMode, convolveAlpha);
27    }
28
29    ~MatrixConvolutionBench() {
30        fFilter->unref();
31    }
32
33protected:
34    virtual const char* onGetName() {
35        return fName.c_str();
36    }
37
38    virtual void onDraw(const int loops, SkCanvas* canvas) {
39        SkPaint paint;
40        this->setupPaint(&paint);
41        paint.setAntiAlias(true);
42        SkRandom rand;
43        for (int i = 0; i < loops; i++) {
44            SkRect r = SkRect::MakeWH(rand.nextUScalar1() * 400,
45                                      rand.nextUScalar1() * 400);
46            paint.setImageFilter(fFilter);
47            canvas->drawOval(r, paint);
48        }
49    }
50
51private:
52    typedef Benchmark INHERITED;
53    SkMatrixConvolutionImageFilter* fFilter;
54    SkString fName;
55};
56
57DEF_BENCH( return new MatrixConvolutionBench(SkMatrixConvolutionImageFilter::kClamp_TileMode, true); )
58DEF_BENCH( return new MatrixConvolutionBench(SkMatrixConvolutionImageFilter::kRepeat_TileMode, true); )
59DEF_BENCH( return new MatrixConvolutionBench(SkMatrixConvolutionImageFilter::kClampToBlack_TileMode, true); )
60DEF_BENCH( return new MatrixConvolutionBench(SkMatrixConvolutionImageFilter::kClampToBlack_TileMode, false); )
61