blob: 54a04d27712b1c3a7aeb046a422866afb4db3790 [file] [log] [blame]
sugoi@google.com580a1722013-04-23 14:20:45 +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 */
reedaa4c7a72015-04-02 20:31:17 -07007
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
sugoi@google.com580a1722013-04-23 14:20:45 +00009#include "SkCanvas.h"
10#include "SkColorFilterImageFilter.h"
11#include "SkColorMatrixFilter.h"
sugoi@google.com580a1722013-04-23 14:20:45 +000012
13#define FILTER_WIDTH_SMALL SkIntToScalar(32)
14#define FILTER_HEIGHT_SMALL SkIntToScalar(32)
15#define FILTER_WIDTH_LARGE SkIntToScalar(256)
16#define FILTER_HEIGHT_LARGE SkIntToScalar(256)
17
robertphillips5605b562016-04-05 11:50:42 -070018static sk_sp<SkImageFilter> make_brightness(float amount, sk_sp<SkImageFilter> input) {
Mike Reeddf85c382017-02-14 10:59:19 -050019 SkScalar amount255 = amount * 255;
robertphillips5605b562016-04-05 11:50:42 -070020 SkScalar matrix[20] = { 1, 0, 0, 0, amount255,
21 0, 1, 0, 0, amount255,
22 0, 0, 1, 0, amount255,
23 0, 0, 0, 1, 0 };
24 sk_sp<SkColorFilter> filter(SkColorFilter::MakeMatrixFilterRowMajor255(matrix));
25 return SkColorFilterImageFilter::Make(std::move(filter), std::move(input));
26}
27
28static sk_sp<SkImageFilter> make_grayscale(sk_sp<SkImageFilter> input) {
29 SkScalar matrix[20];
30 memset(matrix, 0, 20 * sizeof(SkScalar));
31 matrix[0] = matrix[5] = matrix[10] = 0.2126f;
32 matrix[1] = matrix[6] = matrix[11] = 0.7152f;
33 matrix[2] = matrix[7] = matrix[12] = 0.0722f;
34 matrix[18] = 1.0f;
35 sk_sp<SkColorFilter> filter(SkColorFilter::MakeMatrixFilterRowMajor255(matrix));
36 return SkColorFilterImageFilter::Make(std::move(filter), std::move(input));
37}
38
39static sk_sp<SkImageFilter> make_mode_blue(sk_sp<SkImageFilter> input) {
Mike Reed7d954ad2016-10-28 15:42:34 -040040 sk_sp<SkColorFilter> filter(SkColorFilter::MakeModeFilter(SK_ColorBLUE, SkBlendMode::kSrcIn));
robertphillips5605b562016-04-05 11:50:42 -070041 return SkColorFilterImageFilter::Make(std::move(filter), std::move(input));
42}
43
tfarinaf168b862014-06-19 12:32:29 -070044class ColorFilterBaseBench : public Benchmark {
sugoi@google.com580a1722013-04-23 14:20:45 +000045
46public:
robertphillips8c0326d2016-04-05 12:48:34 -070047 ColorFilterBaseBench(bool small) : fIsSmall(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +000048
49protected:
50 SkRect getFilterRect() const {
robertphillips5605b562016-04-05 11:50:42 -070051 return this->isSmall() ? SkRect::MakeWH(FILTER_WIDTH_SMALL, FILTER_HEIGHT_SMALL) :
52 SkRect::MakeWH(FILTER_WIDTH_LARGE, FILTER_HEIGHT_LARGE);
sugoi@google.com580a1722013-04-23 14:20:45 +000053 }
54
55 inline bool isSmall() const { return fIsSmall; }
reedaa4c7a72015-04-02 20:31:17 -070056
sugoi@google.com580a1722013-04-23 14:20:45 +000057private:
58 bool fIsSmall;
59
tfarinaf168b862014-06-19 12:32:29 -070060 typedef Benchmark INHERITED;
sugoi@google.com580a1722013-04-23 14:20:45 +000061};
62
63class ColorFilterDimBrightBench : public ColorFilterBaseBench {
64
65public:
robertphillips5605b562016-04-05 11:50:42 -070066 ColorFilterDimBrightBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +000067
68protected:
mtklein36352bf2015-03-25 18:17:31 -070069 const char* onGetName() override {
robertphillips5605b562016-04-05 11:50:42 -070070 return this->isSmall() ? "colorfilter_dim_bright_small" : "colorfilter_dim_bright_large";
sugoi@google.com580a1722013-04-23 14:20:45 +000071 }
72
mtkleina1ebeb22015-10-01 09:43:39 -070073 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips5605b562016-04-05 11:50:42 -070074 SkRect r = this->getFilterRect();
sugoi@google.com580a1722013-04-23 14:20:45 +000075 SkPaint paint;
76 paint.setColor(SK_ColorRED);
mtklein@google.comc2897432013-09-10 19:23:38 +000077
commit-bot@chromium.org33614712013-12-03 18:17:16 +000078 for (int i = 0; i < loops; i++) {
mtklein@google.comc2897432013-09-10 19:23:38 +000079 for (float brightness = -1.0f; brightness <= 1.0f; brightness += 0.4f) {
robertphillips5605b562016-04-05 11:50:42 -070080 sk_sp<SkImageFilter> dim(make_brightness(-brightness, nullptr));
81 paint.setImageFilter(make_brightness(brightness, std::move(dim)));
mtklein@google.comc2897432013-09-10 19:23:38 +000082 canvas->drawRect(r, paint);
83 }
sugoi@google.com580a1722013-04-23 14:20:45 +000084 }
85 }
86
87private:
88 typedef ColorFilterBaseBench INHERITED;
89};
90
91class ColorFilterBrightGrayBench : public ColorFilterBaseBench {
92
93public:
robertphillips5605b562016-04-05 11:50:42 -070094 ColorFilterBrightGrayBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +000095
96protected:
mtklein36352bf2015-03-25 18:17:31 -070097 const char* onGetName() override {
robertphillips5605b562016-04-05 11:50:42 -070098 return this->isSmall() ? "colorfilter_bright_gray_small" : "colorfilter_bright_gray_large";
sugoi@google.com580a1722013-04-23 14:20:45 +000099 }
100
mtkleina1ebeb22015-10-01 09:43:39 -0700101 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips5605b562016-04-05 11:50:42 -0700102 SkRect r = this->getFilterRect();
sugoi@google.com580a1722013-04-23 14:20:45 +0000103 SkPaint paint;
104 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000105 for (int i = 0; i < loops; i++) {
robertphillips5605b562016-04-05 11:50:42 -0700106 sk_sp<SkImageFilter> brightness(make_brightness(0.9f, nullptr));
107 paint.setImageFilter(make_grayscale(std::move(brightness)));
sugoi@google.com580a1722013-04-23 14:20:45 +0000108 canvas->drawRect(r, paint);
109 }
110 }
111
112private:
113 typedef ColorFilterBaseBench INHERITED;
114};
115
116class ColorFilterGrayBrightBench : public ColorFilterBaseBench {
117
118public:
robertphillips5605b562016-04-05 11:50:42 -0700119 ColorFilterGrayBrightBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +0000120
121protected:
mtklein36352bf2015-03-25 18:17:31 -0700122 const char* onGetName() override {
robertphillips5605b562016-04-05 11:50:42 -0700123 return this->isSmall() ? "colorfilter_gray_bright_small" : "colorfilter_gray_bright_large";
sugoi@google.com580a1722013-04-23 14:20:45 +0000124 }
125
mtkleina1ebeb22015-10-01 09:43:39 -0700126 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips5605b562016-04-05 11:50:42 -0700127 SkRect r = this->getFilterRect();
sugoi@google.com580a1722013-04-23 14:20:45 +0000128 SkPaint paint;
129 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000130 for (int i = 0; i < loops; i++) {
robertphillips5605b562016-04-05 11:50:42 -0700131 sk_sp<SkImageFilter> grayscale(make_grayscale(nullptr));
132 paint.setImageFilter(make_brightness(0.9f, std::move(grayscale)));
sugoi@google.com580a1722013-04-23 14:20:45 +0000133 canvas->drawRect(r, paint);
134 }
135 }
136
137private:
138 typedef ColorFilterBaseBench INHERITED;
139};
140
141class ColorFilterBlueBrightBench : public ColorFilterBaseBench {
142
143public:
robertphillips5605b562016-04-05 11:50:42 -0700144 ColorFilterBlueBrightBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +0000145
146protected:
mtklein36352bf2015-03-25 18:17:31 -0700147 const char* onGetName() override {
robertphillips5605b562016-04-05 11:50:42 -0700148 return this->isSmall() ? "colorfilter_blue_bright_small" : "colorfilter_blue_bright_large";
sugoi@google.com580a1722013-04-23 14:20:45 +0000149 }
150
mtkleina1ebeb22015-10-01 09:43:39 -0700151 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips5605b562016-04-05 11:50:42 -0700152 SkRect r = this->getFilterRect();
sugoi@google.com580a1722013-04-23 14:20:45 +0000153 SkPaint paint;
154 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000155 for (int i = 0; i < loops; i++) {
robertphillips5605b562016-04-05 11:50:42 -0700156 sk_sp<SkImageFilter> blue(make_mode_blue(nullptr));
157 paint.setImageFilter(make_brightness(1.0f, std::move(blue)));
sugoi@google.com580a1722013-04-23 14:20:45 +0000158 canvas->drawRect(r, paint);
159 }
160 }
161
162private:
163 typedef ColorFilterBaseBench INHERITED;
164};
165
166class ColorFilterBrightBlueBench : public ColorFilterBaseBench {
167
168public:
robertphillips5605b562016-04-05 11:50:42 -0700169 ColorFilterBrightBlueBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +0000170
171protected:
mtklein36352bf2015-03-25 18:17:31 -0700172 const char* onGetName() override {
robertphillips5605b562016-04-05 11:50:42 -0700173 return this->isSmall() ? "colorfilter_bright_blue_small" : "colorfilter_bright_blue_large";
sugoi@google.com580a1722013-04-23 14:20:45 +0000174 }
175
mtkleina1ebeb22015-10-01 09:43:39 -0700176 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips5605b562016-04-05 11:50:42 -0700177 SkRect r = this->getFilterRect();
sugoi@google.com580a1722013-04-23 14:20:45 +0000178 SkPaint paint;
179 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000180 for (int i = 0; i < loops; i++) {
robertphillips5605b562016-04-05 11:50:42 -0700181 sk_sp<SkImageFilter> brightness(make_brightness(1.0f, nullptr));
182 paint.setImageFilter(make_mode_blue(std::move(brightness)));
sugoi@google.com580a1722013-04-23 14:20:45 +0000183 canvas->drawRect(r, paint);
184 }
185 }
186
187private:
188 typedef ColorFilterBaseBench INHERITED;
189};
190
191class ColorFilterBrightBench : public ColorFilterBaseBench {
192
193public:
robertphillips5605b562016-04-05 11:50:42 -0700194 ColorFilterBrightBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +0000195
196protected:
mtklein36352bf2015-03-25 18:17:31 -0700197 const char* onGetName() override {
robertphillips5605b562016-04-05 11:50:42 -0700198 return this->isSmall() ? "colorfilter_bright_small" : "colorfilter_bright_large";
sugoi@google.com580a1722013-04-23 14:20:45 +0000199 }
200
mtkleina1ebeb22015-10-01 09:43:39 -0700201 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips5605b562016-04-05 11:50:42 -0700202 SkRect r = this->getFilterRect();
sugoi@google.com580a1722013-04-23 14:20:45 +0000203 SkPaint paint;
204 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000205 for (int i = 0; i < loops; i++) {
robertphillips5605b562016-04-05 11:50:42 -0700206 paint.setImageFilter(make_brightness(1.0f, nullptr));
sugoi@google.com580a1722013-04-23 14:20:45 +0000207 canvas->drawRect(r, paint);
208 }
209 }
210
211private:
212 typedef ColorFilterBaseBench INHERITED;
213};
214
215class ColorFilterBlueBench : public ColorFilterBaseBench {
216
217public:
robertphillips5605b562016-04-05 11:50:42 -0700218 ColorFilterBlueBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +0000219
220protected:
mtklein36352bf2015-03-25 18:17:31 -0700221 const char* onGetName() override {
robertphillips5605b562016-04-05 11:50:42 -0700222 return this->isSmall() ? "colorfilter_blue_small" : "colorfilter_blue_large";
sugoi@google.com580a1722013-04-23 14:20:45 +0000223 }
224
mtkleina1ebeb22015-10-01 09:43:39 -0700225 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips5605b562016-04-05 11:50:42 -0700226 SkRect r = this->getFilterRect();
sugoi@google.com580a1722013-04-23 14:20:45 +0000227 SkPaint paint;
228 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000229 for (int i = 0; i < loops; i++) {
robertphillips5605b562016-04-05 11:50:42 -0700230 paint.setImageFilter(make_mode_blue(nullptr));
sugoi@google.com580a1722013-04-23 14:20:45 +0000231 canvas->drawRect(r, paint);
232 }
233 }
234
235private:
236 typedef ColorFilterBaseBench INHERITED;
237};
238
239class ColorFilterGrayBench : public ColorFilterBaseBench {
240
241public:
robertphillips5605b562016-04-05 11:50:42 -0700242 ColorFilterGrayBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +0000243
244protected:
mtklein36352bf2015-03-25 18:17:31 -0700245 const char* onGetName() override {
robertphillips5605b562016-04-05 11:50:42 -0700246 return this->isSmall() ? "colorfilter_gray_small" : "colorfilter_gray_large";
sugoi@google.com580a1722013-04-23 14:20:45 +0000247 }
248
mtkleina1ebeb22015-10-01 09:43:39 -0700249 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips5605b562016-04-05 11:50:42 -0700250 SkRect r = this->getFilterRect();
sugoi@google.com580a1722013-04-23 14:20:45 +0000251 SkPaint paint;
252 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000253 for (int i = 0; i < loops; i++) {
robertphillips5605b562016-04-05 11:50:42 -0700254 paint.setImageFilter(make_grayscale(nullptr));
sugoi@google.com580a1722013-04-23 14:20:45 +0000255 canvas->drawRect(r, paint);
256 }
257 }
258
259private:
260 typedef ColorFilterBaseBench INHERITED;
261};
262
sugoi@google.com580a1722013-04-23 14:20:45 +0000263///////////////////////////////////////////////////////////////////////////////
264
mtklein@google.com410e6e82013-09-13 19:52:27 +0000265DEF_BENCH( return new ColorFilterDimBrightBench(true); )
266DEF_BENCH( return new ColorFilterBrightGrayBench(true); )
267DEF_BENCH( return new ColorFilterGrayBrightBench(true); )
268DEF_BENCH( return new ColorFilterBlueBrightBench(true); )
269DEF_BENCH( return new ColorFilterBrightBlueBench(true); )
270DEF_BENCH( return new ColorFilterBrightBench(true); )
271DEF_BENCH( return new ColorFilterBlueBench(true); )
272DEF_BENCH( return new ColorFilterGrayBench(true); )
sugoi@google.com580a1722013-04-23 14:20:45 +0000273
mtklein@google.com410e6e82013-09-13 19:52:27 +0000274DEF_BENCH( return new ColorFilterDimBrightBench(false); )
275DEF_BENCH( return new ColorFilterBrightGrayBench(false); )
276DEF_BENCH( return new ColorFilterGrayBrightBench(false); )
277DEF_BENCH( return new ColorFilterBlueBrightBench(false); )
278DEF_BENCH( return new ColorFilterBrightBlueBench(false); )
279DEF_BENCH( return new ColorFilterBrightBench(false); )
280DEF_BENCH( return new ColorFilterBlueBench(false); )
281DEF_BENCH( return new ColorFilterGrayBench(false); )