blob: 7d942ced7ac60d924363ef0a3de16bf8adbcfef2 [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) {
19 SkScalar amount255 = SkScalarMul(amount, SkIntToScalar(255));
20 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) {
40 sk_sp<SkColorFilter> filter(SkColorFilter::MakeModeFilter(SK_ColorBLUE,
41 SkXfermode::kSrcIn_Mode));
42 return SkColorFilterImageFilter::Make(std::move(filter), std::move(input));
43}
44
tfarinaf168b862014-06-19 12:32:29 -070045class ColorFilterBaseBench : public Benchmark {
sugoi@google.com580a1722013-04-23 14:20:45 +000046
47public:
robertphillips8c0326d2016-04-05 12:48:34 -070048 ColorFilterBaseBench(bool small) : fIsSmall(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +000049
50protected:
51 SkRect getFilterRect() const {
robertphillips5605b562016-04-05 11:50:42 -070052 return this->isSmall() ? SkRect::MakeWH(FILTER_WIDTH_SMALL, FILTER_HEIGHT_SMALL) :
53 SkRect::MakeWH(FILTER_WIDTH_LARGE, FILTER_HEIGHT_LARGE);
sugoi@google.com580a1722013-04-23 14:20:45 +000054 }
55
56 inline bool isSmall() const { return fIsSmall; }
reedaa4c7a72015-04-02 20:31:17 -070057
sugoi@google.com580a1722013-04-23 14:20:45 +000058private:
59 bool fIsSmall;
60
tfarinaf168b862014-06-19 12:32:29 -070061 typedef Benchmark INHERITED;
sugoi@google.com580a1722013-04-23 14:20:45 +000062};
63
64class ColorFilterDimBrightBench : public ColorFilterBaseBench {
65
66public:
robertphillips5605b562016-04-05 11:50:42 -070067 ColorFilterDimBrightBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +000068
69protected:
mtklein36352bf2015-03-25 18:17:31 -070070 const char* onGetName() override {
robertphillips5605b562016-04-05 11:50:42 -070071 return this->isSmall() ? "colorfilter_dim_bright_small" : "colorfilter_dim_bright_large";
sugoi@google.com580a1722013-04-23 14:20:45 +000072 }
73
mtkleina1ebeb22015-10-01 09:43:39 -070074 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips5605b562016-04-05 11:50:42 -070075 SkRect r = this->getFilterRect();
sugoi@google.com580a1722013-04-23 14:20:45 +000076 SkPaint paint;
77 paint.setColor(SK_ColorRED);
mtklein@google.comc2897432013-09-10 19:23:38 +000078
commit-bot@chromium.org33614712013-12-03 18:17:16 +000079 for (int i = 0; i < loops; i++) {
mtklein@google.comc2897432013-09-10 19:23:38 +000080 for (float brightness = -1.0f; brightness <= 1.0f; brightness += 0.4f) {
robertphillips5605b562016-04-05 11:50:42 -070081 sk_sp<SkImageFilter> dim(make_brightness(-brightness, nullptr));
82 paint.setImageFilter(make_brightness(brightness, std::move(dim)));
mtklein@google.comc2897432013-09-10 19:23:38 +000083 canvas->drawRect(r, paint);
84 }
sugoi@google.com580a1722013-04-23 14:20:45 +000085 }
86 }
87
88private:
89 typedef ColorFilterBaseBench INHERITED;
90};
91
92class ColorFilterBrightGrayBench : public ColorFilterBaseBench {
93
94public:
robertphillips5605b562016-04-05 11:50:42 -070095 ColorFilterBrightGrayBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +000096
97protected:
mtklein36352bf2015-03-25 18:17:31 -070098 const char* onGetName() override {
robertphillips5605b562016-04-05 11:50:42 -070099 return this->isSmall() ? "colorfilter_bright_gray_small" : "colorfilter_bright_gray_large";
sugoi@google.com580a1722013-04-23 14:20:45 +0000100 }
101
mtkleina1ebeb22015-10-01 09:43:39 -0700102 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips5605b562016-04-05 11:50:42 -0700103 SkRect r = this->getFilterRect();
sugoi@google.com580a1722013-04-23 14:20:45 +0000104 SkPaint paint;
105 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000106 for (int i = 0; i < loops; i++) {
robertphillips5605b562016-04-05 11:50:42 -0700107 sk_sp<SkImageFilter> brightness(make_brightness(0.9f, nullptr));
108 paint.setImageFilter(make_grayscale(std::move(brightness)));
sugoi@google.com580a1722013-04-23 14:20:45 +0000109 canvas->drawRect(r, paint);
110 }
111 }
112
113private:
114 typedef ColorFilterBaseBench INHERITED;
115};
116
117class ColorFilterGrayBrightBench : public ColorFilterBaseBench {
118
119public:
robertphillips5605b562016-04-05 11:50:42 -0700120 ColorFilterGrayBrightBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +0000121
122protected:
mtklein36352bf2015-03-25 18:17:31 -0700123 const char* onGetName() override {
robertphillips5605b562016-04-05 11:50:42 -0700124 return this->isSmall() ? "colorfilter_gray_bright_small" : "colorfilter_gray_bright_large";
sugoi@google.com580a1722013-04-23 14:20:45 +0000125 }
126
mtkleina1ebeb22015-10-01 09:43:39 -0700127 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips5605b562016-04-05 11:50:42 -0700128 SkRect r = this->getFilterRect();
sugoi@google.com580a1722013-04-23 14:20:45 +0000129 SkPaint paint;
130 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000131 for (int i = 0; i < loops; i++) {
robertphillips5605b562016-04-05 11:50:42 -0700132 sk_sp<SkImageFilter> grayscale(make_grayscale(nullptr));
133 paint.setImageFilter(make_brightness(0.9f, std::move(grayscale)));
sugoi@google.com580a1722013-04-23 14:20:45 +0000134 canvas->drawRect(r, paint);
135 }
136 }
137
138private:
139 typedef ColorFilterBaseBench INHERITED;
140};
141
142class ColorFilterBlueBrightBench : public ColorFilterBaseBench {
143
144public:
robertphillips5605b562016-04-05 11:50:42 -0700145 ColorFilterBlueBrightBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +0000146
147protected:
mtklein36352bf2015-03-25 18:17:31 -0700148 const char* onGetName() override {
robertphillips5605b562016-04-05 11:50:42 -0700149 return this->isSmall() ? "colorfilter_blue_bright_small" : "colorfilter_blue_bright_large";
sugoi@google.com580a1722013-04-23 14:20:45 +0000150 }
151
mtkleina1ebeb22015-10-01 09:43:39 -0700152 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips5605b562016-04-05 11:50:42 -0700153 SkRect r = this->getFilterRect();
sugoi@google.com580a1722013-04-23 14:20:45 +0000154 SkPaint paint;
155 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000156 for (int i = 0; i < loops; i++) {
robertphillips5605b562016-04-05 11:50:42 -0700157 sk_sp<SkImageFilter> blue(make_mode_blue(nullptr));
158 paint.setImageFilter(make_brightness(1.0f, std::move(blue)));
sugoi@google.com580a1722013-04-23 14:20:45 +0000159 canvas->drawRect(r, paint);
160 }
161 }
162
163private:
164 typedef ColorFilterBaseBench INHERITED;
165};
166
167class ColorFilterBrightBlueBench : public ColorFilterBaseBench {
168
169public:
robertphillips5605b562016-04-05 11:50:42 -0700170 ColorFilterBrightBlueBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +0000171
172protected:
mtklein36352bf2015-03-25 18:17:31 -0700173 const char* onGetName() override {
robertphillips5605b562016-04-05 11:50:42 -0700174 return this->isSmall() ? "colorfilter_bright_blue_small" : "colorfilter_bright_blue_large";
sugoi@google.com580a1722013-04-23 14:20:45 +0000175 }
176
mtkleina1ebeb22015-10-01 09:43:39 -0700177 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips5605b562016-04-05 11:50:42 -0700178 SkRect r = this->getFilterRect();
sugoi@google.com580a1722013-04-23 14:20:45 +0000179 SkPaint paint;
180 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000181 for (int i = 0; i < loops; i++) {
robertphillips5605b562016-04-05 11:50:42 -0700182 sk_sp<SkImageFilter> brightness(make_brightness(1.0f, nullptr));
183 paint.setImageFilter(make_mode_blue(std::move(brightness)));
sugoi@google.com580a1722013-04-23 14:20:45 +0000184 canvas->drawRect(r, paint);
185 }
186 }
187
188private:
189 typedef ColorFilterBaseBench INHERITED;
190};
191
192class ColorFilterBrightBench : public ColorFilterBaseBench {
193
194public:
robertphillips5605b562016-04-05 11:50:42 -0700195 ColorFilterBrightBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +0000196
197protected:
mtklein36352bf2015-03-25 18:17:31 -0700198 const char* onGetName() override {
robertphillips5605b562016-04-05 11:50:42 -0700199 return this->isSmall() ? "colorfilter_bright_small" : "colorfilter_bright_large";
sugoi@google.com580a1722013-04-23 14:20:45 +0000200 }
201
mtkleina1ebeb22015-10-01 09:43:39 -0700202 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips5605b562016-04-05 11:50:42 -0700203 SkRect r = this->getFilterRect();
sugoi@google.com580a1722013-04-23 14:20:45 +0000204 SkPaint paint;
205 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000206 for (int i = 0; i < loops; i++) {
robertphillips5605b562016-04-05 11:50:42 -0700207 paint.setImageFilter(make_brightness(1.0f, nullptr));
sugoi@google.com580a1722013-04-23 14:20:45 +0000208 canvas->drawRect(r, paint);
209 }
210 }
211
212private:
213 typedef ColorFilterBaseBench INHERITED;
214};
215
216class ColorFilterBlueBench : public ColorFilterBaseBench {
217
218public:
robertphillips5605b562016-04-05 11:50:42 -0700219 ColorFilterBlueBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +0000220
221protected:
mtklein36352bf2015-03-25 18:17:31 -0700222 const char* onGetName() override {
robertphillips5605b562016-04-05 11:50:42 -0700223 return this->isSmall() ? "colorfilter_blue_small" : "colorfilter_blue_large";
sugoi@google.com580a1722013-04-23 14:20:45 +0000224 }
225
mtkleina1ebeb22015-10-01 09:43:39 -0700226 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips5605b562016-04-05 11:50:42 -0700227 SkRect r = this->getFilterRect();
sugoi@google.com580a1722013-04-23 14:20:45 +0000228 SkPaint paint;
229 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000230 for (int i = 0; i < loops; i++) {
robertphillips5605b562016-04-05 11:50:42 -0700231 paint.setImageFilter(make_mode_blue(nullptr));
sugoi@google.com580a1722013-04-23 14:20:45 +0000232 canvas->drawRect(r, paint);
233 }
234 }
235
236private:
237 typedef ColorFilterBaseBench INHERITED;
238};
239
240class ColorFilterGrayBench : public ColorFilterBaseBench {
241
242public:
robertphillips5605b562016-04-05 11:50:42 -0700243 ColorFilterGrayBench(bool small) : INHERITED(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +0000244
245protected:
mtklein36352bf2015-03-25 18:17:31 -0700246 const char* onGetName() override {
robertphillips5605b562016-04-05 11:50:42 -0700247 return this->isSmall() ? "colorfilter_gray_small" : "colorfilter_gray_large";
sugoi@google.com580a1722013-04-23 14:20:45 +0000248 }
249
mtkleina1ebeb22015-10-01 09:43:39 -0700250 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips5605b562016-04-05 11:50:42 -0700251 SkRect r = this->getFilterRect();
sugoi@google.com580a1722013-04-23 14:20:45 +0000252 SkPaint paint;
253 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000254 for (int i = 0; i < loops; i++) {
robertphillips5605b562016-04-05 11:50:42 -0700255 paint.setImageFilter(make_grayscale(nullptr));
sugoi@google.com580a1722013-04-23 14:20:45 +0000256 canvas->drawRect(r, paint);
257 }
258 }
259
260private:
261 typedef ColorFilterBaseBench INHERITED;
262};
263
sugoi@google.com580a1722013-04-23 14:20:45 +0000264///////////////////////////////////////////////////////////////////////////////
265
mtklein@google.com410e6e82013-09-13 19:52:27 +0000266DEF_BENCH( return new ColorFilterDimBrightBench(true); )
267DEF_BENCH( return new ColorFilterBrightGrayBench(true); )
268DEF_BENCH( return new ColorFilterGrayBrightBench(true); )
269DEF_BENCH( return new ColorFilterBlueBrightBench(true); )
270DEF_BENCH( return new ColorFilterBrightBlueBench(true); )
271DEF_BENCH( return new ColorFilterBrightBench(true); )
272DEF_BENCH( return new ColorFilterBlueBench(true); )
273DEF_BENCH( return new ColorFilterGrayBench(true); )
sugoi@google.com580a1722013-04-23 14:20:45 +0000274
mtklein@google.com410e6e82013-09-13 19:52:27 +0000275DEF_BENCH( return new ColorFilterDimBrightBench(false); )
276DEF_BENCH( return new ColorFilterBrightGrayBench(false); )
277DEF_BENCH( return new ColorFilterGrayBrightBench(false); )
278DEF_BENCH( return new ColorFilterBlueBrightBench(false); )
279DEF_BENCH( return new ColorFilterBrightBlueBench(false); )
280DEF_BENCH( return new ColorFilterBrightBench(false); )
281DEF_BENCH( return new ColorFilterBlueBench(false); )
282DEF_BENCH( return new ColorFilterGrayBench(false); )