blob: 5365a9c2ad52082c79de59f2a4ce07490694c3c5 [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"
commit-bot@chromium.org6c1ee2d2013-10-07 18:00:17 +000012#include "SkLumaColorFilter.h"
sugoi@google.com580a1722013-04-23 14:20:45 +000013#include "SkTableColorFilter.h"
14
15#define FILTER_WIDTH_SMALL SkIntToScalar(32)
16#define FILTER_HEIGHT_SMALL SkIntToScalar(32)
17#define FILTER_WIDTH_LARGE SkIntToScalar(256)
18#define FILTER_HEIGHT_LARGE SkIntToScalar(256)
19
tfarinaf168b862014-06-19 12:32:29 -070020class ColorFilterBaseBench : public Benchmark {
sugoi@google.com580a1722013-04-23 14:20:45 +000021
22public:
reedaa4c7a72015-04-02 20:31:17 -070023 ColorFilterBaseBench(bool small) : fIsSmall(small) {}
sugoi@google.com580a1722013-04-23 14:20:45 +000024
25protected:
26 SkRect getFilterRect() const {
27 return isSmall() ? SkRect::MakeWH(FILTER_WIDTH_SMALL, FILTER_HEIGHT_SMALL) :
28 SkRect::MakeWH(FILTER_WIDTH_LARGE, FILTER_HEIGHT_LARGE);
29 }
30
halcanary96fcdcc2015-08-27 07:41:13 -070031 static SkImageFilter* make_brightness(float amount, SkImageFilter* input = nullptr) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000032 SkScalar amount255 = SkScalarMul(amount, SkIntToScalar(255));
sugoi@google.com580a1722013-04-23 14:20:45 +000033 SkScalar matrix[20] = { 1, 0, 0, 0, amount255,
34 0, 1, 0, 0, amount255,
35 0, 0, 1, 0, amount255,
36 0, 0, 0, 1, 0 };
commit-bot@chromium.org727a3522014-02-21 18:46:30 +000037 SkAutoTUnref<SkColorFilter> filter(SkColorMatrixFilter::Create(matrix));
sugoi@google.com580a1722013-04-23 14:20:45 +000038 return SkColorFilterImageFilter::Create(filter, input);
39 }
40
halcanary96fcdcc2015-08-27 07:41:13 -070041 static SkImageFilter* make_grayscale(SkImageFilter* input = nullptr) {
sugoi@google.com580a1722013-04-23 14:20:45 +000042 SkScalar matrix[20];
43 memset(matrix, 0, 20 * sizeof(SkScalar));
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000044 matrix[0] = matrix[5] = matrix[10] = 0.2126f;
45 matrix[1] = matrix[6] = matrix[11] = 0.7152f;
46 matrix[2] = matrix[7] = matrix[12] = 0.0722f;
47 matrix[18] = 1.0f;
commit-bot@chromium.org727a3522014-02-21 18:46:30 +000048 SkAutoTUnref<SkColorFilter> filter(SkColorMatrixFilter::Create(matrix));
sugoi@google.com580a1722013-04-23 14:20:45 +000049 return SkColorFilterImageFilter::Create(filter, input);
50 }
51
halcanary96fcdcc2015-08-27 07:41:13 -070052 static SkImageFilter* make_mode_blue(SkImageFilter* input = nullptr) {
sugoi@google.com580a1722013-04-23 14:20:45 +000053 SkAutoTUnref<SkColorFilter> filter(
54 SkColorFilter::CreateModeFilter(SK_ColorBLUE, SkXfermode::kSrcIn_Mode));
55 return SkColorFilterImageFilter::Create(filter, input);
56 }
57
58 inline bool isSmall() const { return fIsSmall; }
reedaa4c7a72015-04-02 20:31:17 -070059
sugoi@google.com580a1722013-04-23 14:20:45 +000060private:
61 bool fIsSmall;
62
tfarinaf168b862014-06-19 12:32:29 -070063 typedef Benchmark INHERITED;
sugoi@google.com580a1722013-04-23 14:20:45 +000064};
65
66class ColorFilterDimBrightBench : public ColorFilterBaseBench {
67
68public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000069 ColorFilterDimBrightBench(bool small) : INHERITED(small) {
sugoi@google.com580a1722013-04-23 14:20:45 +000070 }
71
72protected:
mtklein36352bf2015-03-25 18:17:31 -070073 const char* onGetName() override {
sugoi@google.com580a1722013-04-23 14:20:45 +000074 return isSmall() ? "colorfilter_dim_bright_small" : "colorfilter_dim_bright_large";
75 }
76
mtkleina1ebeb22015-10-01 09:43:39 -070077 void onDraw(int loops, SkCanvas* canvas) override {
sugoi@google.com580a1722013-04-23 14:20:45 +000078 SkRect r = getFilterRect();
79 SkPaint paint;
80 paint.setColor(SK_ColorRED);
mtklein@google.comc2897432013-09-10 19:23:38 +000081
commit-bot@chromium.org33614712013-12-03 18:17:16 +000082 for (int i = 0; i < loops; i++) {
mtklein@google.comc2897432013-09-10 19:23:38 +000083 for (float brightness = -1.0f; brightness <= 1.0f; brightness += 0.4f) {
84 SkAutoTUnref<SkImageFilter> dim(make_brightness(-brightness));
85 SkAutoTUnref<SkImageFilter> bright(make_brightness(brightness, dim));
86 paint.setImageFilter(bright);
87 canvas->drawRect(r, paint);
88 }
sugoi@google.com580a1722013-04-23 14:20:45 +000089 }
90 }
91
92private:
93 typedef ColorFilterBaseBench INHERITED;
94};
95
96class ColorFilterBrightGrayBench : public ColorFilterBaseBench {
97
98public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000099 ColorFilterBrightGrayBench(bool small) : INHERITED(small) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000100 }
101
102protected:
mtklein36352bf2015-03-25 18:17:31 -0700103 const char* onGetName() override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000104 return isSmall() ? "colorfilter_bright_gray_small" : "colorfilter_bright_gray_large";
105 }
106
mtkleina1ebeb22015-10-01 09:43:39 -0700107 void onDraw(int loops, SkCanvas* canvas) override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000108 SkRect r = getFilterRect();
109 SkPaint paint;
110 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000111 for (int i = 0; i < loops; i++) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000112 SkAutoTUnref<SkImageFilter> brightness(make_brightness(0.9f));
113 SkAutoTUnref<SkImageFilter> grayscale(make_grayscale(brightness));
114 paint.setImageFilter(grayscale);
115 canvas->drawRect(r, paint);
116 }
117 }
118
119private:
120 typedef ColorFilterBaseBench INHERITED;
121};
122
123class ColorFilterGrayBrightBench : public ColorFilterBaseBench {
124
125public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000126 ColorFilterGrayBrightBench(bool small) : INHERITED(small) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000127 }
128
129protected:
mtklein36352bf2015-03-25 18:17:31 -0700130 const char* onGetName() override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000131 return isSmall() ? "colorfilter_gray_bright_small" : "colorfilter_gray_bright_large";
132 }
133
mtkleina1ebeb22015-10-01 09:43:39 -0700134 void onDraw(int loops, SkCanvas* canvas) override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000135 SkRect r = getFilterRect();
136 SkPaint paint;
137 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000138 for (int i = 0; i < loops; i++) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000139 SkAutoTUnref<SkImageFilter> grayscale(make_grayscale());
140 SkAutoTUnref<SkImageFilter> brightness(make_brightness(0.9f, grayscale));
141 paint.setImageFilter(brightness);
142 canvas->drawRect(r, paint);
143 }
144 }
145
146private:
147 typedef ColorFilterBaseBench INHERITED;
148};
149
150class ColorFilterBlueBrightBench : public ColorFilterBaseBench {
151
152public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000153 ColorFilterBlueBrightBench(bool small) : INHERITED(small) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000154 }
155
156protected:
mtklein36352bf2015-03-25 18:17:31 -0700157 const char* onGetName() override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000158 return isSmall() ? "colorfilter_blue_bright_small" : "colorfilter_blue_bright_large";
159 }
160
mtkleina1ebeb22015-10-01 09:43:39 -0700161 void onDraw(int loops, SkCanvas* canvas) override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000162 SkRect r = getFilterRect();
163 SkPaint paint;
164 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000165 for (int i = 0; i < loops; i++) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000166 SkAutoTUnref<SkImageFilter> blue(make_mode_blue());
167 SkAutoTUnref<SkImageFilter> brightness(make_brightness(1.0f, blue));
168 paint.setImageFilter(brightness);
169 canvas->drawRect(r, paint);
170 }
171 }
172
173private:
174 typedef ColorFilterBaseBench INHERITED;
175};
176
177class ColorFilterBrightBlueBench : public ColorFilterBaseBench {
178
179public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000180 ColorFilterBrightBlueBench(bool small) : INHERITED(small) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000181 }
182
183protected:
mtklein36352bf2015-03-25 18:17:31 -0700184 const char* onGetName() override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000185 return isSmall() ? "colorfilter_bright_blue_small" : "colorfilter_bright_blue_large";
186 }
187
mtkleina1ebeb22015-10-01 09:43:39 -0700188 void onDraw(int loops, SkCanvas* canvas) override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000189 SkRect r = getFilterRect();
190 SkPaint paint;
191 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000192 for (int i = 0; i < loops; i++) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000193 SkAutoTUnref<SkImageFilter> brightness(make_brightness(1.0f));
194 SkAutoTUnref<SkImageFilter> blue(make_mode_blue(brightness));
195 paint.setImageFilter(blue);
196 canvas->drawRect(r, paint);
197 }
198 }
199
200private:
201 typedef ColorFilterBaseBench INHERITED;
202};
203
204class ColorFilterBrightBench : public ColorFilterBaseBench {
205
206public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000207 ColorFilterBrightBench(bool small) : INHERITED(small) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000208 }
209
210protected:
mtklein36352bf2015-03-25 18:17:31 -0700211 const char* onGetName() override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000212 return isSmall() ? "colorfilter_bright_small" : "colorfilter_bright_large";
213 }
214
mtkleina1ebeb22015-10-01 09:43:39 -0700215 void onDraw(int loops, SkCanvas* canvas) override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000216 SkRect r = getFilterRect();
217 SkPaint paint;
218 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000219 for (int i = 0; i < loops; i++) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000220 SkAutoTUnref<SkImageFilter> brightness(make_brightness(1.0f));
221 paint.setImageFilter(brightness);
222 canvas->drawRect(r, paint);
223 }
224 }
225
226private:
227 typedef ColorFilterBaseBench INHERITED;
228};
229
230class ColorFilterBlueBench : public ColorFilterBaseBench {
231
232public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000233 ColorFilterBlueBench(bool small) : INHERITED(small) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000234 }
235
236protected:
mtklein36352bf2015-03-25 18:17:31 -0700237 const char* onGetName() override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000238 return isSmall() ? "colorfilter_blue_small" : "colorfilter_blue_large";
239 }
240
mtkleina1ebeb22015-10-01 09:43:39 -0700241 void onDraw(int loops, SkCanvas* canvas) override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000242 SkRect r = getFilterRect();
243 SkPaint paint;
244 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000245 for (int i = 0; i < loops; i++) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000246 SkAutoTUnref<SkImageFilter> blue(make_mode_blue());
247 paint.setImageFilter(blue);
248 canvas->drawRect(r, paint);
249 }
250 }
251
252private:
253 typedef ColorFilterBaseBench INHERITED;
254};
255
256class ColorFilterGrayBench : public ColorFilterBaseBench {
257
258public:
reedaa4c7a72015-04-02 20:31:17 -0700259 ColorFilterGrayBench(bool small) : INHERITED(small) {}
sugoi@google.com580a1722013-04-23 14:20:45 +0000260
261protected:
mtklein36352bf2015-03-25 18:17:31 -0700262 const char* onGetName() override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000263 return isSmall() ? "colorfilter_gray_small" : "colorfilter_gray_large";
264 }
265
mtkleina1ebeb22015-10-01 09:43:39 -0700266 void onDraw(int loops, SkCanvas* canvas) override {
sugoi@google.com580a1722013-04-23 14:20:45 +0000267 SkRect r = getFilterRect();
268 SkPaint paint;
269 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000270 for (int i = 0; i < loops; i++) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000271 SkAutoTUnref<SkImageFilter> grayscale(make_grayscale());
272 paint.setImageFilter(grayscale);
273 canvas->drawRect(r, paint);
274 }
275 }
276
277private:
278 typedef ColorFilterBaseBench INHERITED;
279};
280
sugoi@google.com580a1722013-04-23 14:20:45 +0000281///////////////////////////////////////////////////////////////////////////////
282
mtklein@google.com410e6e82013-09-13 19:52:27 +0000283DEF_BENCH( return new ColorFilterDimBrightBench(true); )
284DEF_BENCH( return new ColorFilterBrightGrayBench(true); )
285DEF_BENCH( return new ColorFilterGrayBrightBench(true); )
286DEF_BENCH( return new ColorFilterBlueBrightBench(true); )
287DEF_BENCH( return new ColorFilterBrightBlueBench(true); )
288DEF_BENCH( return new ColorFilterBrightBench(true); )
289DEF_BENCH( return new ColorFilterBlueBench(true); )
290DEF_BENCH( return new ColorFilterGrayBench(true); )
sugoi@google.com580a1722013-04-23 14:20:45 +0000291
mtklein@google.com410e6e82013-09-13 19:52:27 +0000292DEF_BENCH( return new ColorFilterDimBrightBench(false); )
293DEF_BENCH( return new ColorFilterBrightGrayBench(false); )
294DEF_BENCH( return new ColorFilterGrayBrightBench(false); )
295DEF_BENCH( return new ColorFilterBlueBrightBench(false); )
296DEF_BENCH( return new ColorFilterBrightBlueBench(false); )
297DEF_BENCH( return new ColorFilterBrightBench(false); )
298DEF_BENCH( return new ColorFilterBlueBench(false); )
299DEF_BENCH( return new ColorFilterGrayBench(false); )