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
name(SkMatrixConvolutionImageFilter::TileMode mode)14 static const char* name(SkMatrixConvolutionImageFilter::TileMode mode) {
15 switch (mode) {
16 case SkMatrixConvolutionImageFilter::kClamp_TileMode: return "clamp";
17 case SkMatrixConvolutionImageFilter::kRepeat_TileMode: return "repeat";
18 case SkMatrixConvolutionImageFilter::kClampToBlack_TileMode: return "clampToBlack";
19 }
20 return "oops";
21 }
22
23 class MatrixConvolutionBench : public Benchmark {
24 public:
MatrixConvolutionBench(SkMatrixConvolutionImageFilter::TileMode tileMode,bool convolveAlpha)25 MatrixConvolutionBench(SkMatrixConvolutionImageFilter::TileMode tileMode, bool convolveAlpha)
26 : fName(SkStringPrintf("matrixconvolution_%s%s",
27 name(tileMode),
28 convolveAlpha ? "" : "_noConvolveAlpha")) {
29 SkISize kernelSize = SkISize::Make(3, 3);
30 SkScalar kernel[9] = {
31 SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1),
32 SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1),
33 SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1),
34 };
35 SkScalar gain = 0.3f, bias = SkIntToScalar(100);
36 SkIPoint kernelOffset = SkIPoint::Make(1, 1);
37 fFilter = SkMatrixConvolutionImageFilter::Make(kernelSize, kernel, gain, bias,
38 kernelOffset, tileMode, convolveAlpha,
39 nullptr);
40 }
41
42 protected:
onGetName()43 virtual const char* onGetName() {
44 return fName.c_str();
45 }
46
onDraw(int loops,SkCanvas * canvas)47 virtual void onDraw(int loops, SkCanvas* canvas) {
48 SkPaint paint;
49 this->setupPaint(&paint);
50 paint.setAntiAlias(true);
51 SkRandom rand;
52 for (int i = 0; i < loops; i++) {
53 SkRect r = SkRect::MakeWH(rand.nextUScalar1() * 400,
54 rand.nextUScalar1() * 400);
55 paint.setImageFilter(fFilter);
56 canvas->drawOval(r, paint);
57 }
58 }
59
60 private:
61 sk_sp<SkImageFilter> fFilter;
62 SkString fName;
63
64 typedef Benchmark INHERITED;
65 };
66
67 DEF_BENCH( return new MatrixConvolutionBench(SkMatrixConvolutionImageFilter::kClamp_TileMode, true); )
68 DEF_BENCH( return new MatrixConvolutionBench(SkMatrixConvolutionImageFilter::kRepeat_TileMode, true); )
69 DEF_BENCH( return new MatrixConvolutionBench(SkMatrixConvolutionImageFilter::kClampToBlack_TileMode, true); )
70 DEF_BENCH( return new MatrixConvolutionBench(SkMatrixConvolutionImageFilter::kClampToBlack_TileMode, false); )
71