blob: 26842230b1bfd229da08553b32f42652ab78d0fb [file] [log] [blame]
humper@google.com7c7292c2013-01-04 20:29:03 +00001/*
2 * Copyright 2013 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 */
reed@google.comcb88d322013-01-07 21:54:25 +00007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "bench/Benchmark.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkPaint.h"
11#include "include/core/SkShader.h"
12#include "include/core/SkString.h"
13#include "include/utils/SkRandom.h"
14#include "src/core/SkBlurMask.h"
humper@google.com7c7292c2013-01-04 20:29:03 +000015
16#define SMALL SkIntToScalar(2)
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000017#define REAL 1.5f
robertphillips@google.comb7061172013-09-06 14:16:12 +000018static const SkScalar kMedium = SkIntToScalar(5);
humper@google.com7c7292c2013-01-04 20:29:03 +000019#define BIG SkIntToScalar(10)
robertphillips@google.comb7061172013-09-06 14:16:12 +000020static const SkScalar kMedBig = SkIntToScalar(20);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000021#define REALBIG 30.5f
humper@google.com7c7292c2013-01-04 20:29:03 +000022
tfarinaf168b862014-06-19 12:32:29 -070023class BlurRectBench: public Benchmark {
djsollen@google.comefbe8e92013-02-07 18:58:35 +000024 int fLoopCount;
humper@google.com7c7292c2013-01-04 20:29:03 +000025 SkScalar fRadius;
26 SkString fName;
27
28public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000029 BlurRectBench(SkScalar rad) {
humper@google.com7c7292c2013-01-04 20:29:03 +000030 fRadius = rad;
djsollen@google.comefbe8e92013-02-07 18:58:35 +000031
32 if (fRadius > SkIntToScalar(25)) {
33 fLoopCount = 100;
34 } else if (fRadius > SkIntToScalar(5)) {
35 fLoopCount = 1000;
36 } else {
37 fLoopCount = 10000;
38 }
humper@google.com7c7292c2013-01-04 20:29:03 +000039 }
40
41protected:
Brian Salomond0072812020-07-21 17:03:56 -040042 const char* onGetName() override {
humper@google.com7c7292c2013-01-04 20:29:03 +000043 return fName.c_str();
44 }
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +000045
humper@google.com7c7292c2013-01-04 20:29:03 +000046 SkScalar radius() const {
47 return fRadius;
48 }
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +000049
reed@google.comcb88d322013-01-07 21:54:25 +000050 void setName(const SkString& name) {
humper@google.com7c7292c2013-01-04 20:29:03 +000051 fName = name;
52 }
53
Brian Salomond0072812020-07-21 17:03:56 -040054 void onDraw(int loops, SkCanvas*) override {
humper@google.com7c7292c2013-01-04 20:29:03 +000055 SkPaint paint;
56 this->setupPaint(&paint);
57
58 paint.setAntiAlias(true);
59
reed@google.comcb88d322013-01-07 21:54:25 +000060 SkScalar pad = fRadius*3/2 + SK_Scalar1;
61 SkRect r = SkRect::MakeWH(2 * pad + SK_Scalar1, 2 * pad + SK_Scalar1);
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +000062
reed@google.comcb88d322013-01-07 21:54:25 +000063 preBenchSetup(r);
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +000064
commit-bot@chromium.org33614712013-12-03 18:17:16 +000065 for (int i = 0; i < loops; i++) {
robertphillipse80eb922015-12-17 11:33:12 -080066 this->makeBlurryRect(r);
humper@google.com7c7292c2013-01-04 20:29:03 +000067 }
68 }
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +000069
reed@google.comcb88d322013-01-07 21:54:25 +000070 virtual void makeBlurryRect(const SkRect&) = 0;
71 virtual void preBenchSetup(const SkRect&) {}
humper@google.com7c7292c2013-01-04 20:29:03 +000072private:
John Stiles7571f9e2020-09-02 22:42:33 -040073 using INHERITED = Benchmark;
humper@google.com7c7292c2013-01-04 20:29:03 +000074};
75
76
77class BlurRectDirectBench: public BlurRectBench {
78 public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000079 BlurRectDirectBench(SkScalar rad) : INHERITED(rad) {
humper@google.com7c7292c2013-01-04 20:29:03 +000080 SkString name;
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +000081
humper@google.com7c7292c2013-01-04 20:29:03 +000082 if (SkScalarFraction(rad) != 0) {
83 name.printf("blurrect_direct_%.2f", SkScalarToFloat(rad));
84 } else {
humper@google.coma99a92c2013-02-20 16:42:06 +000085 name.printf("blurrect_direct_%d", SkScalarRoundToInt(rad));
humper@google.com7c7292c2013-01-04 20:29:03 +000086 }
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +000087
robertphillips@google.comb7061172013-09-06 14:16:12 +000088 this->setName(name);
humper@google.com7c7292c2013-01-04 20:29:03 +000089 }
90protected:
mtklein36352bf2015-03-25 18:17:31 -070091 void makeBlurryRect(const SkRect& r) override {
humper@google.com7c7292c2013-01-04 20:29:03 +000092 SkMask mask;
robertphillipse80eb922015-12-17 11:33:12 -080093 if (!SkBlurMask::BlurRect(SkBlurMask::ConvertRadiusToSigma(this->radius()),
94 &mask, r, kNormal_SkBlurStyle)) {
halcanary9d524f22016-03-29 09:03:52 -070095 return;
robertphillipse80eb922015-12-17 11:33:12 -080096 }
bsalomon@google.com33cdbde2013-01-11 20:54:44 +000097 SkMask::FreeImage(mask.fImage);
humper@google.com7c7292c2013-01-04 20:29:03 +000098 }
humper@google.coma99a92c2013-02-20 16:42:06 +000099private:
John Stiles7571f9e2020-09-02 22:42:33 -0400100 using INHERITED = BlurRectBench;
humper@google.com7c7292c2013-01-04 20:29:03 +0000101};
102
103class BlurRectSeparableBench: public BlurRectBench {
humper@google.coma99a92c2013-02-20 16:42:06 +0000104
humper@google.com7c7292c2013-01-04 20:29:03 +0000105public:
robertphillipse80eb922015-12-17 11:33:12 -0800106 BlurRectSeparableBench(SkScalar rad) : INHERITED(rad) { }
bsalomon@google.com33cdbde2013-01-11 20:54:44 +0000107
Brian Salomond3b65972017-03-22 12:05:03 -0400108 ~BlurRectSeparableBench() override {
bsalomon@google.com33cdbde2013-01-11 20:54:44 +0000109 SkMask::FreeImage(fSrcMask.fImage);
humper@google.com7c7292c2013-01-04 20:29:03 +0000110 }
111
112protected:
mtklein36352bf2015-03-25 18:17:31 -0700113 void preBenchSetup(const SkRect& r) override {
bsalomon@google.com33cdbde2013-01-11 20:54:44 +0000114 SkMask::FreeImage(fSrcMask.fImage);
115
reed@google.comcb88d322013-01-07 21:54:25 +0000116 r.roundOut(&fSrcMask.fBounds);
humper@google.com7c7292c2013-01-04 20:29:03 +0000117 fSrcMask.fFormat = SkMask::kA8_Format;
reed@google.comcb88d322013-01-07 21:54:25 +0000118 fSrcMask.fRowBytes = fSrcMask.fBounds.width();
119 fSrcMask.fImage = SkMask::AllocImage(fSrcMask.computeTotalImageSize());
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +0000120
reed@google.comcb88d322013-01-07 21:54:25 +0000121 memset(fSrcMask.fImage, 0xff, fSrcMask.computeTotalImageSize());
humper@google.com7c7292c2013-01-04 20:29:03 +0000122 }
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000123
humper@google.coma99a92c2013-02-20 16:42:06 +0000124 SkMask fSrcMask;
125private:
John Stiles7571f9e2020-09-02 22:42:33 -0400126 using INHERITED = BlurRectBench;
humper@google.coma99a92c2013-02-20 16:42:06 +0000127};
128
129class BlurRectBoxFilterBench: public BlurRectSeparableBench {
130public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000131 BlurRectBoxFilterBench(SkScalar rad) : INHERITED(rad) {
humper@google.coma99a92c2013-02-20 16:42:06 +0000132 SkString name;
robertphillips@google.comb7061172013-09-06 14:16:12 +0000133
humper@google.coma99a92c2013-02-20 16:42:06 +0000134 if (SkScalarFraction(rad) != 0) {
135 name.printf("blurrect_boxfilter_%.2f", SkScalarToFloat(rad));
136 } else {
137 name.printf("blurrect_boxfilter_%d", SkScalarRoundToInt(rad));
138 }
robertphillips@google.comb7061172013-09-06 14:16:12 +0000139
140 this->setName(name);
humper@google.coma99a92c2013-02-20 16:42:06 +0000141 }
142
143protected:
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +0000144
mtklein36352bf2015-03-25 18:17:31 -0700145 void makeBlurryRect(const SkRect&) override {
humper@google.com7c7292c2013-01-04 20:29:03 +0000146 SkMask mask;
robertphillipse80eb922015-12-17 11:33:12 -0800147 if (!SkBlurMask::BoxBlur(&mask, fSrcMask, SkBlurMask::ConvertRadiusToSigma(this->radius()),
Mike Reed1be1f8d2018-03-14 13:01:17 -0400148 kNormal_SkBlurStyle)) {
robertphillipse80eb922015-12-17 11:33:12 -0800149 return;
150 }
bsalomon@google.com33cdbde2013-01-11 20:54:44 +0000151 SkMask::FreeImage(mask.fImage);
humper@google.com7c7292c2013-01-04 20:29:03 +0000152 }
humper@google.coma99a92c2013-02-20 16:42:06 +0000153private:
John Stiles7571f9e2020-09-02 22:42:33 -0400154 using INHERITED = BlurRectSeparableBench;
humper@google.com7c7292c2013-01-04 20:29:03 +0000155};
156
humper@google.coma99a92c2013-02-20 16:42:06 +0000157class BlurRectGaussianBench: public BlurRectSeparableBench {
158public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000159 BlurRectGaussianBench(SkScalar rad) : INHERITED(rad) {
humper@google.coma99a92c2013-02-20 16:42:06 +0000160 SkString name;
robertphillips@google.comb7061172013-09-06 14:16:12 +0000161
humper@google.coma99a92c2013-02-20 16:42:06 +0000162 if (SkScalarFraction(rad) != 0) {
163 name.printf("blurrect_gaussian_%.2f", SkScalarToFloat(rad));
164 } else {
165 name.printf("blurrect_gaussian_%d", SkScalarRoundToInt(rad));
166 }
skia.committer@gmail.comb3ec29d2013-09-07 07:01:16 +0000167
robertphillips@google.comb7061172013-09-06 14:16:12 +0000168 this->setName(name);
humper@google.coma99a92c2013-02-20 16:42:06 +0000169 }
170
171protected:
172
mtklein36352bf2015-03-25 18:17:31 -0700173 void makeBlurryRect(const SkRect&) override {
humper@google.coma99a92c2013-02-20 16:42:06 +0000174 SkMask mask;
robertphillipse80eb922015-12-17 11:33:12 -0800175 if (!SkBlurMask::BlurGroundTruth(SkBlurMask::ConvertRadiusToSigma(this->radius()),
176 &mask, fSrcMask, kNormal_SkBlurStyle)) {
177 return;
178 }
humper@google.coma99a92c2013-02-20 16:42:06 +0000179 SkMask::FreeImage(mask.fImage);
180 }
181private:
John Stiles7571f9e2020-09-02 22:42:33 -0400182 using INHERITED = BlurRectSeparableBench;
humper@google.coma99a92c2013-02-20 16:42:06 +0000183};
184
mtklein@google.com410e6e82013-09-13 19:52:27 +0000185DEF_BENCH(return new BlurRectBoxFilterBench(SMALL);)
186DEF_BENCH(return new BlurRectBoxFilterBench(BIG);)
187DEF_BENCH(return new BlurRectBoxFilterBench(REALBIG);)
188DEF_BENCH(return new BlurRectBoxFilterBench(REAL);)
189DEF_BENCH(return new BlurRectGaussianBench(SMALL);)
190DEF_BENCH(return new BlurRectGaussianBench(BIG);)
191DEF_BENCH(return new BlurRectGaussianBench(REALBIG);)
192DEF_BENCH(return new BlurRectGaussianBench(REAL);)
193DEF_BENCH(return new BlurRectDirectBench(SMALL);)
194DEF_BENCH(return new BlurRectDirectBench(BIG);)
195DEF_BENCH(return new BlurRectDirectBench(REALBIG);)
196DEF_BENCH(return new BlurRectDirectBench(REAL);)
humper@google.coma99a92c2013-02-20 16:42:06 +0000197
mtklein@google.com410e6e82013-09-13 19:52:27 +0000198DEF_BENCH(return new BlurRectDirectBench(kMedium);)
199DEF_BENCH(return new BlurRectDirectBench(kMedBig);)
humper@google.coma99a92c2013-02-20 16:42:06 +0000200
mtklein@google.com410e6e82013-09-13 19:52:27 +0000201DEF_BENCH(return new BlurRectBoxFilterBench(kMedium);)
202DEF_BENCH(return new BlurRectBoxFilterBench(kMedBig);)
humper@google.coma99a92c2013-02-20 16:42:06 +0000203
204#if 0
205// disable Gaussian benchmarks; the algorithm works well enough
206// and serves as a baseline for ground truth, but it's too slow
207// to use in production for non-trivial radii, so no real point
208// in having the bots benchmark it all the time.
209
mtklein@google.com410e6e82013-09-13 19:52:27 +0000210DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(1));)
211DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(2));)
212DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(3));)
213DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(4));)
214DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(5));)
215DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(6));)
216DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(7));)
217DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(8));)
218DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(9));)
219DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(10));)
220DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(11));)
221DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(12));)
222DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(13));)
223DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(14));)
224DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(15));)
225DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(16));)
226DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(17));)
227DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(18));)
228DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(19));)
229DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(20));)
humper@google.coma99a92c2013-02-20 16:42:06 +0000230#endif