blob: 05f0193ceca7c0276f4a0b8a8fbf8bff1b2c0825 [file] [log] [blame]
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +00001/*
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 */
tfarinaf168b862014-06-19 12:32:29 -07007#include "Benchmark.h"
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +00008#include "SkCanvas.h"
tfarinaf168b862014-06-19 12:32:29 -07009#include "SkMatrixConvolutionImageFilter.h"
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000010#include "SkPaint.h"
11#include "SkRandom.h"
12#include "SkString.h"
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000013
tfarinaf168b862014-06-19 12:32:29 -070014class MatrixConvolutionBench : public Benchmark {
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000015public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000016 MatrixConvolutionBench(SkMatrixConvolutionImageFilter::TileMode tileMode, bool convolveAlpha)
17 : fName("matrixconvolution") {
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000018 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 };
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000024 SkScalar gain = 0.3f, bias = SkIntToScalar(100);
commit-bot@chromium.org84cd0992014-03-12 16:36:08 +000025 SkIPoint kernelOffset = SkIPoint::Make(1, 1);
26 fFilter = SkMatrixConvolutionImageFilter::Create(kernelSize, kernel, gain, bias, kernelOffset, tileMode, convolveAlpha);
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000027 }
28
29 ~MatrixConvolutionBench() {
30 fFilter->unref();
31 }
32
33protected:
34 virtual const char* onGetName() {
35 return fName.c_str();
36 }
37
mtkleina1ebeb22015-10-01 09:43:39 -070038 virtual void onDraw(int loops, SkCanvas* canvas) {
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000039 SkPaint paint;
40 this->setupPaint(&paint);
41 paint.setAntiAlias(true);
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000042 SkRandom rand;
commit-bot@chromium.org33614712013-12-03 18:17:16 +000043 for (int i = 0; i < loops; i++) {
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000044 SkRect r = SkRect::MakeWH(rand.nextUScalar1() * 400,
45 rand.nextUScalar1() * 400);
46 paint.setImageFilter(fFilter);
47 canvas->drawOval(r, paint);
48 }
49 }
50
51private:
tfarinaf168b862014-06-19 12:32:29 -070052 typedef Benchmark INHERITED;
reed5e1ddb12015-12-21 08:52:45 -080053 SkImageFilter* fFilter;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000054 SkString fName;
55};
56
mtklein@google.com410e6e82013-09-13 19:52:27 +000057DEF_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); )