blob: fc09c371f4c74af5ebf8df35f07fe8d4e9e62ef3 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "bench/Benchmark.h"
9#include "include/core/SkCanvas.h"
10#include "include/effects/SkColorFilterImageFilter.h"
11#include "include/effects/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 Reede869a1e2019-04-30 12:18:54 -040019 float matrix[20] = { 1, 0, 0, 0, amount,
20 0, 1, 0, 0, amount,
21 0, 0, 1, 0, amount,
22 0, 0, 0, 1, 0 };
23 sk_sp<SkColorFilter> filter(SkColorFilters::Matrix(matrix));
robertphillips5605b562016-04-05 11:50:42 -070024 return SkColorFilterImageFilter::Make(std::move(filter), std::move(input));
25}
26
27static sk_sp<SkImageFilter> make_grayscale(sk_sp<SkImageFilter> input) {
Mike Reede869a1e2019-04-30 12:18:54 -040028 float matrix[20];
29 memset(matrix, 0, 20 * sizeof(float));
robertphillips5605b562016-04-05 11:50:42 -070030 matrix[0] = matrix[5] = matrix[10] = 0.2126f;
31 matrix[1] = matrix[6] = matrix[11] = 0.7152f;
32 matrix[2] = matrix[7] = matrix[12] = 0.0722f;
33 matrix[18] = 1.0f;
Mike Reede869a1e2019-04-30 12:18:54 -040034 sk_sp<SkColorFilter> filter(SkColorFilters::Matrix(matrix));
robertphillips5605b562016-04-05 11:50:42 -070035 return SkColorFilterImageFilter::Make(std::move(filter), std::move(input));
36}
37
38static sk_sp<SkImageFilter> make_mode_blue(sk_sp<SkImageFilter> input) {
Mike Reedb286bc22019-04-08 16:23:20 -040039 sk_sp<SkColorFilter> filter(SkColorFilters::Blend(SK_ColorBLUE, SkBlendMode::kSrcIn));
robertphillips5605b562016-04-05 11:50:42 -070040 return SkColorFilterImageFilter::Make(std::move(filter), std::move(input));
41}
42
tfarinaf168b862014-06-19 12:32:29 -070043class ColorFilterBaseBench : public Benchmark {
sugoi@google.com580a1722013-04-23 14:20:45 +000044
45public:
robertphillips8c0326d2016-04-05 12:48:34 -070046 ColorFilterBaseBench(bool small) : fIsSmall(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +000047
48protected:
49 SkRect getFilterRect() const {
robertphillips5605b562016-04-05 11:50:42 -070050 return this->isSmall() ? SkRect::MakeWH(FILTER_WIDTH_SMALL, FILTER_HEIGHT_SMALL) :
51 SkRect::MakeWH(FILTER_WIDTH_LARGE, FILTER_HEIGHT_LARGE);
sugoi@google.com580a1722013-04-23 14:20:45 +000052 }
53
54 inline bool isSmall() const { return fIsSmall; }
reedaa4c7a72015-04-02 20:31:17 -070055
sugoi@google.com580a1722013-04-23 14:20:45 +000056private:
57 bool fIsSmall;
58
tfarinaf168b862014-06-19 12:32:29 -070059 typedef Benchmark INHERITED;
sugoi@google.com580a1722013-04-23 14:20:45 +000060};
61
62class ColorFilterDimBrightBench : public ColorFilterBaseBench {
63
64public:
robertphillips5605b562016-04-05 11:50:42 -070065 ColorFilterDimBrightBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +000066
67protected:
mtklein36352bf2015-03-25 18:17:31 -070068 const char* onGetName() override {
robertphillips5605b562016-04-05 11:50:42 -070069 return this->isSmall() ? "colorfilter_dim_bright_small" : "colorfilter_dim_bright_large";
sugoi@google.com580a1722013-04-23 14:20:45 +000070 }
71
mtkleina1ebeb22015-10-01 09:43:39 -070072 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips5605b562016-04-05 11:50:42 -070073 SkRect r = this->getFilterRect();
sugoi@google.com580a1722013-04-23 14:20:45 +000074 SkPaint paint;
75 paint.setColor(SK_ColorRED);
mtklein@google.comc2897432013-09-10 19:23:38 +000076
commit-bot@chromium.org33614712013-12-03 18:17:16 +000077 for (int i = 0; i < loops; i++) {
mtklein@google.comc2897432013-09-10 19:23:38 +000078 for (float brightness = -1.0f; brightness <= 1.0f; brightness += 0.4f) {
robertphillips5605b562016-04-05 11:50:42 -070079 sk_sp<SkImageFilter> dim(make_brightness(-brightness, nullptr));
80 paint.setImageFilter(make_brightness(brightness, std::move(dim)));
mtklein@google.comc2897432013-09-10 19:23:38 +000081 canvas->drawRect(r, paint);
82 }
sugoi@google.com580a1722013-04-23 14:20:45 +000083 }
84 }
85
86private:
87 typedef ColorFilterBaseBench INHERITED;
88};
89
90class ColorFilterBrightGrayBench : public ColorFilterBaseBench {
91
92public:
robertphillips5605b562016-04-05 11:50:42 -070093 ColorFilterBrightGrayBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +000094
95protected:
mtklein36352bf2015-03-25 18:17:31 -070096 const char* onGetName() override {
robertphillips5605b562016-04-05 11:50:42 -070097 return this->isSmall() ? "colorfilter_bright_gray_small" : "colorfilter_bright_gray_large";
sugoi@google.com580a1722013-04-23 14:20:45 +000098 }
99
mtkleina1ebeb22015-10-01 09:43:39 -0700100 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips5605b562016-04-05 11:50:42 -0700101 SkRect r = this->getFilterRect();
sugoi@google.com580a1722013-04-23 14:20:45 +0000102 SkPaint paint;
103 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000104 for (int i = 0; i < loops; i++) {
robertphillips5605b562016-04-05 11:50:42 -0700105 sk_sp<SkImageFilter> brightness(make_brightness(0.9f, nullptr));
106 paint.setImageFilter(make_grayscale(std::move(brightness)));
sugoi@google.com580a1722013-04-23 14:20:45 +0000107 canvas->drawRect(r, paint);
108 }
109 }
110
111private:
112 typedef ColorFilterBaseBench INHERITED;
113};
114
115class ColorFilterGrayBrightBench : public ColorFilterBaseBench {
116
117public:
robertphillips5605b562016-04-05 11:50:42 -0700118 ColorFilterGrayBrightBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +0000119
120protected:
mtklein36352bf2015-03-25 18:17:31 -0700121 const char* onGetName() override {
robertphillips5605b562016-04-05 11:50:42 -0700122 return this->isSmall() ? "colorfilter_gray_bright_small" : "colorfilter_gray_bright_large";
sugoi@google.com580a1722013-04-23 14:20:45 +0000123 }
124
mtkleina1ebeb22015-10-01 09:43:39 -0700125 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips5605b562016-04-05 11:50:42 -0700126 SkRect r = this->getFilterRect();
sugoi@google.com580a1722013-04-23 14:20:45 +0000127 SkPaint paint;
128 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000129 for (int i = 0; i < loops; i++) {
robertphillips5605b562016-04-05 11:50:42 -0700130 sk_sp<SkImageFilter> grayscale(make_grayscale(nullptr));
131 paint.setImageFilter(make_brightness(0.9f, std::move(grayscale)));
sugoi@google.com580a1722013-04-23 14:20:45 +0000132 canvas->drawRect(r, paint);
133 }
134 }
135
136private:
137 typedef ColorFilterBaseBench INHERITED;
138};
139
140class ColorFilterBlueBrightBench : public ColorFilterBaseBench {
141
142public:
robertphillips5605b562016-04-05 11:50:42 -0700143 ColorFilterBlueBrightBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +0000144
145protected:
mtklein36352bf2015-03-25 18:17:31 -0700146 const char* onGetName() override {
robertphillips5605b562016-04-05 11:50:42 -0700147 return this->isSmall() ? "colorfilter_blue_bright_small" : "colorfilter_blue_bright_large";
sugoi@google.com580a1722013-04-23 14:20:45 +0000148 }
149
mtkleina1ebeb22015-10-01 09:43:39 -0700150 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips5605b562016-04-05 11:50:42 -0700151 SkRect r = this->getFilterRect();
sugoi@google.com580a1722013-04-23 14:20:45 +0000152 SkPaint paint;
153 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000154 for (int i = 0; i < loops; i++) {
robertphillips5605b562016-04-05 11:50:42 -0700155 sk_sp<SkImageFilter> blue(make_mode_blue(nullptr));
156 paint.setImageFilter(make_brightness(1.0f, std::move(blue)));
sugoi@google.com580a1722013-04-23 14:20:45 +0000157 canvas->drawRect(r, paint);
158 }
159 }
160
161private:
162 typedef ColorFilterBaseBench INHERITED;
163};
164
165class ColorFilterBrightBlueBench : public ColorFilterBaseBench {
166
167public:
robertphillips5605b562016-04-05 11:50:42 -0700168 ColorFilterBrightBlueBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +0000169
170protected:
mtklein36352bf2015-03-25 18:17:31 -0700171 const char* onGetName() override {
robertphillips5605b562016-04-05 11:50:42 -0700172 return this->isSmall() ? "colorfilter_bright_blue_small" : "colorfilter_bright_blue_large";
sugoi@google.com580a1722013-04-23 14:20:45 +0000173 }
174
mtkleina1ebeb22015-10-01 09:43:39 -0700175 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips5605b562016-04-05 11:50:42 -0700176 SkRect r = this->getFilterRect();
sugoi@google.com580a1722013-04-23 14:20:45 +0000177 SkPaint paint;
178 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000179 for (int i = 0; i < loops; i++) {
robertphillips5605b562016-04-05 11:50:42 -0700180 sk_sp<SkImageFilter> brightness(make_brightness(1.0f, nullptr));
181 paint.setImageFilter(make_mode_blue(std::move(brightness)));
sugoi@google.com580a1722013-04-23 14:20:45 +0000182 canvas->drawRect(r, paint);
183 }
184 }
185
186private:
187 typedef ColorFilterBaseBench INHERITED;
188};
189
190class ColorFilterBrightBench : public ColorFilterBaseBench {
191
192public:
robertphillips5605b562016-04-05 11:50:42 -0700193 ColorFilterBrightBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +0000194
195protected:
mtklein36352bf2015-03-25 18:17:31 -0700196 const char* onGetName() override {
robertphillips5605b562016-04-05 11:50:42 -0700197 return this->isSmall() ? "colorfilter_bright_small" : "colorfilter_bright_large";
sugoi@google.com580a1722013-04-23 14:20:45 +0000198 }
199
mtkleina1ebeb22015-10-01 09:43:39 -0700200 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips5605b562016-04-05 11:50:42 -0700201 SkRect r = this->getFilterRect();
sugoi@google.com580a1722013-04-23 14:20:45 +0000202 SkPaint paint;
203 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000204 for (int i = 0; i < loops; i++) {
robertphillips5605b562016-04-05 11:50:42 -0700205 paint.setImageFilter(make_brightness(1.0f, nullptr));
sugoi@google.com580a1722013-04-23 14:20:45 +0000206 canvas->drawRect(r, paint);
207 }
208 }
209
210private:
211 typedef ColorFilterBaseBench INHERITED;
212};
213
214class ColorFilterBlueBench : public ColorFilterBaseBench {
215
216public:
robertphillips5605b562016-04-05 11:50:42 -0700217 ColorFilterBlueBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +0000218
219protected:
mtklein36352bf2015-03-25 18:17:31 -0700220 const char* onGetName() override {
robertphillips5605b562016-04-05 11:50:42 -0700221 return this->isSmall() ? "colorfilter_blue_small" : "colorfilter_blue_large";
sugoi@google.com580a1722013-04-23 14:20:45 +0000222 }
223
mtkleina1ebeb22015-10-01 09:43:39 -0700224 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips5605b562016-04-05 11:50:42 -0700225 SkRect r = this->getFilterRect();
sugoi@google.com580a1722013-04-23 14:20:45 +0000226 SkPaint paint;
227 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000228 for (int i = 0; i < loops; i++) {
robertphillips5605b562016-04-05 11:50:42 -0700229 paint.setImageFilter(make_mode_blue(nullptr));
sugoi@google.com580a1722013-04-23 14:20:45 +0000230 canvas->drawRect(r, paint);
231 }
232 }
233
234private:
235 typedef ColorFilterBaseBench INHERITED;
236};
237
238class ColorFilterGrayBench : public ColorFilterBaseBench {
239
240public:
robertphillips5605b562016-04-05 11:50:42 -0700241 ColorFilterGrayBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +0000242
243protected:
mtklein36352bf2015-03-25 18:17:31 -0700244 const char* onGetName() override {
robertphillips5605b562016-04-05 11:50:42 -0700245 return this->isSmall() ? "colorfilter_gray_small" : "colorfilter_gray_large";
sugoi@google.com580a1722013-04-23 14:20:45 +0000246 }
247
mtkleina1ebeb22015-10-01 09:43:39 -0700248 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips5605b562016-04-05 11:50:42 -0700249 SkRect r = this->getFilterRect();
sugoi@google.com580a1722013-04-23 14:20:45 +0000250 SkPaint paint;
251 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000252 for (int i = 0; i < loops; i++) {
robertphillips5605b562016-04-05 11:50:42 -0700253 paint.setImageFilter(make_grayscale(nullptr));
sugoi@google.com580a1722013-04-23 14:20:45 +0000254 canvas->drawRect(r, paint);
255 }
256 }
257
258private:
259 typedef ColorFilterBaseBench INHERITED;
260};
261
sugoi@google.com580a1722013-04-23 14:20:45 +0000262///////////////////////////////////////////////////////////////////////////////
263
mtklein@google.com410e6e82013-09-13 19:52:27 +0000264DEF_BENCH( return new ColorFilterDimBrightBench(true); )
265DEF_BENCH( return new ColorFilterBrightGrayBench(true); )
266DEF_BENCH( return new ColorFilterGrayBrightBench(true); )
267DEF_BENCH( return new ColorFilterBlueBrightBench(true); )
268DEF_BENCH( return new ColorFilterBrightBlueBench(true); )
269DEF_BENCH( return new ColorFilterBrightBench(true); )
270DEF_BENCH( return new ColorFilterBlueBench(true); )
271DEF_BENCH( return new ColorFilterGrayBench(true); )
sugoi@google.com580a1722013-04-23 14:20:45 +0000272
mtklein@google.com410e6e82013-09-13 19:52:27 +0000273DEF_BENCH( return new ColorFilterDimBrightBench(false); )
274DEF_BENCH( return new ColorFilterBrightGrayBench(false); )
275DEF_BENCH( return new ColorFilterGrayBrightBench(false); )
276DEF_BENCH( return new ColorFilterBlueBrightBench(false); )
277DEF_BENCH( return new ColorFilterBrightBlueBench(false); )
278DEF_BENCH( return new ColorFilterBrightBench(false); )
279DEF_BENCH( return new ColorFilterBlueBench(false); )
280DEF_BENCH( return new ColorFilterGrayBench(false); )