blob: 3a43de863d9b74fb8ca09b2cc935fcf8768de181 [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
mtklein20efb412016-04-12 15:52:52 -070014static 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
tfarinaf168b862014-06-19 12:32:29 -070023class MatrixConvolutionBench : public Benchmark {
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000024public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000025 MatrixConvolutionBench(SkMatrixConvolutionImageFilter::TileMode tileMode, bool convolveAlpha)
mtklein20efb412016-04-12 15:52:52 -070026 : fName(SkStringPrintf("matrixconvolution_%s%s",
27 name(tileMode),
28 convolveAlpha ? "" : "_noConvolveAlpha")) {
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000029 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 };
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000035 SkScalar gain = 0.3f, bias = SkIntToScalar(100);
commit-bot@chromium.org84cd0992014-03-12 16:36:08 +000036 SkIPoint kernelOffset = SkIPoint::Make(1, 1);
mtklein20efb412016-04-12 15:52:52 -070037 fFilter = SkMatrixConvolutionImageFilter::Make(kernelSize, kernel, gain, bias,
robertphillipsef6a47b2016-04-08 08:01:20 -070038 kernelOffset, tileMode, convolveAlpha,
39 nullptr);
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000040 }
41
42protected:
43 virtual const char* onGetName() {
44 return fName.c_str();
45 }
46
mtkleina1ebeb22015-10-01 09:43:39 -070047 virtual void onDraw(int loops, SkCanvas* canvas) {
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000048 SkPaint paint;
49 this->setupPaint(&paint);
50 paint.setAntiAlias(true);
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000051 SkRandom rand;
commit-bot@chromium.org33614712013-12-03 18:17:16 +000052 for (int i = 0; i < loops; i++) {
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000053 SkRect r = SkRect::MakeWH(rand.nextUScalar1() * 400,
54 rand.nextUScalar1() * 400);
55 paint.setImageFilter(fFilter);
56 canvas->drawOval(r, paint);
57 }
58 }
59
60private:
robertphillipsef6a47b2016-04-08 08:01:20 -070061 sk_sp<SkImageFilter> fFilter;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000062 SkString fName;
robertphillipsef6a47b2016-04-08 08:01:20 -070063
64 typedef Benchmark INHERITED;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000065};
66
mtklein@google.com410e6e82013-09-13 19:52:27 +000067DEF_BENCH( return new MatrixConvolutionBench(SkMatrixConvolutionImageFilter::kClamp_TileMode, true); )
68DEF_BENCH( return new MatrixConvolutionBench(SkMatrixConvolutionImageFilter::kRepeat_TileMode, true); )
69DEF_BENCH( return new MatrixConvolutionBench(SkMatrixConvolutionImageFilter::kClampToBlack_TileMode, true); )
70DEF_BENCH( return new MatrixConvolutionBench(SkMatrixConvolutionImageFilter::kClampToBlack_TileMode, false); )