blob: 6fa964f3574f725cb55e8eba39e79c5efce9ba15 [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 */
Mike Kleinc0bd9f92019-04-23 12:05:21 -05007#include "bench/Benchmark.h"
8#include "include/core/SkCanvas.h"
9#include "include/core/SkPaint.h"
10#include "include/core/SkString.h"
Michael Ludwig23003182019-08-05 11:25:23 -040011#include "include/core/SkTileMode.h"
12#include "include/effects/SkImageFilters.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/utils/SkRandom.h"
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000014
Michael Ludwig23003182019-08-05 11:25:23 -040015#include "tools/ToolUtils.h"
mtklein20efb412016-04-12 15:52:52 -070016
tfarinaf168b862014-06-19 12:32:29 -070017class MatrixConvolutionBench : public Benchmark {
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000018public:
Adlai Holler6f994a82020-05-01 15:41:55 -040019 MatrixConvolutionBench(bool bigKernel, SkTileMode tileMode, bool convolveAlpha)
20 : fName(SkStringPrintf("matrixconvolution_%s%s%s",
21 bigKernel ? "bigKernel_" : "",
Michael Ludwig23003182019-08-05 11:25:23 -040022 ToolUtils::tilemode_name(tileMode),
mtklein20efb412016-04-12 15:52:52 -070023 convolveAlpha ? "" : "_noConvolveAlpha")) {
Adlai Holler6f994a82020-05-01 15:41:55 -040024 if (bigKernel) {
25 SkISize kernelSize = SkISize::Make(9, 9);
26 SkScalar kernel[81];
27 for (int i = 0; i < 81; i++) {
28 kernel[i] = SkIntToScalar(1);
29 }
30 kernel[40] = SkIntToScalar(-79);
31 SkScalar gain = 0.3f, bias = SkIntToScalar(100);
32 SkIPoint kernelOffset = SkIPoint::Make(4, 4);
33 fFilter = SkImageFilters::MatrixConvolution(kernelSize, kernel, gain, bias,
34 kernelOffset, tileMode, convolveAlpha,
35 nullptr);
36 } else {
37 SkISize kernelSize = SkISize::Make(3, 3);
38 SkScalar kernel[9] = {
39 SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1),
40 SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1),
41 SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1),
42 };
43 SkScalar gain = 0.3f, bias = SkIntToScalar(100);
44 SkIPoint kernelOffset = SkIPoint::Make(1, 1);
45 fFilter = SkImageFilters::MatrixConvolution(kernelSize, kernel, gain, bias,
46 kernelOffset, tileMode, convolveAlpha,
47 nullptr);
48 }
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000049 }
50
51protected:
Michael Ludwig23003182019-08-05 11:25:23 -040052 const char* onGetName() override {
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000053 return fName.c_str();
54 }
55
Michael Ludwig23003182019-08-05 11:25:23 -040056 void onDraw(int loops, SkCanvas* canvas) override {
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000057 SkPaint paint;
58 this->setupPaint(&paint);
Michael Ludwig23003182019-08-05 11:25:23 -040059 paint.setImageFilter(fFilter);
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000060 paint.setAntiAlias(true);
Michael Ludwig23003182019-08-05 11:25:23 -040061
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000062 SkRandom rand;
commit-bot@chromium.org33614712013-12-03 18:17:16 +000063 for (int i = 0; i < loops; i++) {
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000064 SkRect r = SkRect::MakeWH(rand.nextUScalar1() * 400,
65 rand.nextUScalar1() * 400);
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000066 canvas->drawOval(r, paint);
67 }
68 }
69
70private:
robertphillipsef6a47b2016-04-08 08:01:20 -070071 sk_sp<SkImageFilter> fFilter;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000072 SkString fName;
robertphillipsef6a47b2016-04-08 08:01:20 -070073
John Stiles7571f9e2020-09-02 22:42:33 -040074 using INHERITED = Benchmark;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000075};
76
Adlai Holler6f994a82020-05-01 15:41:55 -040077DEF_BENCH( return new MatrixConvolutionBench(false, SkTileMode::kClamp, true); )
78DEF_BENCH( return new MatrixConvolutionBench(false, SkTileMode::kRepeat, true); )
79DEF_BENCH( return new MatrixConvolutionBench(false, SkTileMode::kMirror, true); )
80DEF_BENCH( return new MatrixConvolutionBench(false, SkTileMode::kDecal, true); )
81DEF_BENCH( return new MatrixConvolutionBench(false, SkTileMode::kDecal, false); )
82
83DEF_BENCH( return new MatrixConvolutionBench(true, SkTileMode::kClamp, true); )
84DEF_BENCH( return new MatrixConvolutionBench(true, SkTileMode::kRepeat, true); )
85DEF_BENCH( return new MatrixConvolutionBench(true, SkTileMode::kMirror, true); )
86DEF_BENCH( return new MatrixConvolutionBench(true, SkTileMode::kDecal, true); )
87DEF_BENCH( return new MatrixConvolutionBench(true, SkTileMode::kDecal, false); )