blob: 7eae21aa7572ac2555f6c308516ead07f944cda7 [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 */
tfarinaf168b862014-06-19 12:32:29 -07007#include "Benchmark.h"
sugoi@google.com580a1722013-04-23 14:20:45 +00008#include "SkCanvas.h"
9#include "SkColorFilterImageFilter.h"
10#include "SkColorMatrixFilter.h"
commit-bot@chromium.org6c1ee2d2013-10-07 18:00:17 +000011#include "SkLumaColorFilter.h"
sugoi@google.com580a1722013-04-23 14:20:45 +000012#include "SkTableColorFilter.h"
13
14#define FILTER_WIDTH_SMALL SkIntToScalar(32)
15#define FILTER_HEIGHT_SMALL SkIntToScalar(32)
16#define FILTER_WIDTH_LARGE SkIntToScalar(256)
17#define FILTER_HEIGHT_LARGE SkIntToScalar(256)
18
tfarinaf168b862014-06-19 12:32:29 -070019class ColorFilterBaseBench : public Benchmark {
sugoi@google.com580a1722013-04-23 14:20:45 +000020
21public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000022 ColorFilterBaseBench(bool small) : fIsSmall(small) { }
sugoi@google.com580a1722013-04-23 14:20:45 +000023
24protected:
25 SkRect getFilterRect() const {
26 return isSmall() ? SkRect::MakeWH(FILTER_WIDTH_SMALL, FILTER_HEIGHT_SMALL) :
27 SkRect::MakeWH(FILTER_WIDTH_LARGE, FILTER_HEIGHT_LARGE);
28 }
29
30 static SkImageFilter* make_brightness(float amount, SkImageFilter* input = NULL) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000031 SkScalar amount255 = SkScalarMul(amount, SkIntToScalar(255));
sugoi@google.com580a1722013-04-23 14:20:45 +000032 SkScalar matrix[20] = { 1, 0, 0, 0, amount255,
33 0, 1, 0, 0, amount255,
34 0, 0, 1, 0, amount255,
35 0, 0, 0, 1, 0 };
commit-bot@chromium.org727a3522014-02-21 18:46:30 +000036 SkAutoTUnref<SkColorFilter> filter(SkColorMatrixFilter::Create(matrix));
sugoi@google.com580a1722013-04-23 14:20:45 +000037 return SkColorFilterImageFilter::Create(filter, input);
38 }
39
40 static SkImageFilter* make_grayscale(SkImageFilter* input = NULL) {
41 SkScalar matrix[20];
42 memset(matrix, 0, 20 * sizeof(SkScalar));
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000043 matrix[0] = matrix[5] = matrix[10] = 0.2126f;
44 matrix[1] = matrix[6] = matrix[11] = 0.7152f;
45 matrix[2] = matrix[7] = matrix[12] = 0.0722f;
46 matrix[18] = 1.0f;
commit-bot@chromium.org727a3522014-02-21 18:46:30 +000047 SkAutoTUnref<SkColorFilter> filter(SkColorMatrixFilter::Create(matrix));
sugoi@google.com580a1722013-04-23 14:20:45 +000048 return SkColorFilterImageFilter::Create(filter, input);
49 }
50
51 static SkImageFilter* make_mode_blue(SkImageFilter* input = NULL) {
52 SkAutoTUnref<SkColorFilter> filter(
53 SkColorFilter::CreateModeFilter(SK_ColorBLUE, SkXfermode::kSrcIn_Mode));
54 return SkColorFilterImageFilter::Create(filter, input);
55 }
56
57 inline bool isSmall() const { return fIsSmall; }
58private:
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:
mtklein@google.com410e6e82013-09-13 19:52:27 +000067 ColorFilterDimBrightBench(bool small) : INHERITED(small) {
sugoi@google.com580a1722013-04-23 14:20:45 +000068 }
69
70protected:
71 virtual const char* onGetName() SK_OVERRIDE {
72 return isSmall() ? "colorfilter_dim_bright_small" : "colorfilter_dim_bright_large";
73 }
74
commit-bot@chromium.org33614712013-12-03 18:17:16 +000075 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
sugoi@google.com580a1722013-04-23 14:20:45 +000076 SkRect r = getFilterRect();
77 SkPaint paint;
78 paint.setColor(SK_ColorRED);
mtklein@google.comc2897432013-09-10 19:23:38 +000079
commit-bot@chromium.org33614712013-12-03 18:17:16 +000080 for (int i = 0; i < loops; i++) {
mtklein@google.comc2897432013-09-10 19:23:38 +000081 for (float brightness = -1.0f; brightness <= 1.0f; brightness += 0.4f) {
82 SkAutoTUnref<SkImageFilter> dim(make_brightness(-brightness));
83 SkAutoTUnref<SkImageFilter> bright(make_brightness(brightness, dim));
84 paint.setImageFilter(bright);
85 canvas->drawRect(r, paint);
86 }
sugoi@google.com580a1722013-04-23 14:20:45 +000087 }
88 }
89
90private:
91 typedef ColorFilterBaseBench INHERITED;
92};
93
94class ColorFilterBrightGrayBench : public ColorFilterBaseBench {
95
96public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000097 ColorFilterBrightGrayBench(bool small) : INHERITED(small) {
sugoi@google.com580a1722013-04-23 14:20:45 +000098 }
99
100protected:
101 virtual const char* onGetName() SK_OVERRIDE {
102 return isSmall() ? "colorfilter_bright_gray_small" : "colorfilter_bright_gray_large";
103 }
104
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000105 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
sugoi@google.com580a1722013-04-23 14:20:45 +0000106 SkRect r = getFilterRect();
107 SkPaint paint;
108 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000109 for (int i = 0; i < loops; i++) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000110 SkAutoTUnref<SkImageFilter> brightness(make_brightness(0.9f));
111 SkAutoTUnref<SkImageFilter> grayscale(make_grayscale(brightness));
112 paint.setImageFilter(grayscale);
113 canvas->drawRect(r, paint);
114 }
115 }
116
117private:
118 typedef ColorFilterBaseBench INHERITED;
119};
120
121class ColorFilterGrayBrightBench : public ColorFilterBaseBench {
122
123public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000124 ColorFilterGrayBrightBench(bool small) : INHERITED(small) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000125 }
126
127protected:
128 virtual const char* onGetName() SK_OVERRIDE {
129 return isSmall() ? "colorfilter_gray_bright_small" : "colorfilter_gray_bright_large";
130 }
131
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000132 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
sugoi@google.com580a1722013-04-23 14:20:45 +0000133 SkRect r = getFilterRect();
134 SkPaint paint;
135 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000136 for (int i = 0; i < loops; i++) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000137 SkAutoTUnref<SkImageFilter> grayscale(make_grayscale());
138 SkAutoTUnref<SkImageFilter> brightness(make_brightness(0.9f, grayscale));
139 paint.setImageFilter(brightness);
140 canvas->drawRect(r, paint);
141 }
142 }
143
144private:
145 typedef ColorFilterBaseBench INHERITED;
146};
147
148class ColorFilterBlueBrightBench : public ColorFilterBaseBench {
149
150public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000151 ColorFilterBlueBrightBench(bool small) : INHERITED(small) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000152 }
153
154protected:
155 virtual const char* onGetName() SK_OVERRIDE {
156 return isSmall() ? "colorfilter_blue_bright_small" : "colorfilter_blue_bright_large";
157 }
158
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000159 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
sugoi@google.com580a1722013-04-23 14:20:45 +0000160 SkRect r = getFilterRect();
161 SkPaint paint;
162 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000163 for (int i = 0; i < loops; i++) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000164 SkAutoTUnref<SkImageFilter> blue(make_mode_blue());
165 SkAutoTUnref<SkImageFilter> brightness(make_brightness(1.0f, blue));
166 paint.setImageFilter(brightness);
167 canvas->drawRect(r, paint);
168 }
169 }
170
171private:
172 typedef ColorFilterBaseBench INHERITED;
173};
174
175class ColorFilterBrightBlueBench : public ColorFilterBaseBench {
176
177public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000178 ColorFilterBrightBlueBench(bool small) : INHERITED(small) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000179 }
180
181protected:
182 virtual const char* onGetName() SK_OVERRIDE {
183 return isSmall() ? "colorfilter_bright_blue_small" : "colorfilter_bright_blue_large";
184 }
185
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000186 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
sugoi@google.com580a1722013-04-23 14:20:45 +0000187 SkRect r = getFilterRect();
188 SkPaint paint;
189 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000190 for (int i = 0; i < loops; i++) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000191 SkAutoTUnref<SkImageFilter> brightness(make_brightness(1.0f));
192 SkAutoTUnref<SkImageFilter> blue(make_mode_blue(brightness));
193 paint.setImageFilter(blue);
194 canvas->drawRect(r, paint);
195 }
196 }
197
198private:
199 typedef ColorFilterBaseBench INHERITED;
200};
201
202class ColorFilterBrightBench : public ColorFilterBaseBench {
203
204public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000205 ColorFilterBrightBench(bool small) : INHERITED(small) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000206 }
207
208protected:
209 virtual const char* onGetName() SK_OVERRIDE {
210 return isSmall() ? "colorfilter_bright_small" : "colorfilter_bright_large";
211 }
212
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000213 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
sugoi@google.com580a1722013-04-23 14:20:45 +0000214 SkRect r = getFilterRect();
215 SkPaint paint;
216 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000217 for (int i = 0; i < loops; i++) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000218 SkAutoTUnref<SkImageFilter> brightness(make_brightness(1.0f));
219 paint.setImageFilter(brightness);
220 canvas->drawRect(r, paint);
221 }
222 }
223
224private:
225 typedef ColorFilterBaseBench INHERITED;
226};
227
228class ColorFilterBlueBench : public ColorFilterBaseBench {
229
230public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000231 ColorFilterBlueBench(bool small) : INHERITED(small) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000232 }
233
234protected:
235 virtual const char* onGetName() SK_OVERRIDE {
236 return isSmall() ? "colorfilter_blue_small" : "colorfilter_blue_large";
237 }
238
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000239 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
sugoi@google.com580a1722013-04-23 14:20:45 +0000240 SkRect r = getFilterRect();
241 SkPaint paint;
242 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000243 for (int i = 0; i < loops; i++) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000244 SkAutoTUnref<SkImageFilter> blue(make_mode_blue());
245 paint.setImageFilter(blue);
246 canvas->drawRect(r, paint);
247 }
248 }
249
250private:
251 typedef ColorFilterBaseBench INHERITED;
252};
253
254class ColorFilterGrayBench : public ColorFilterBaseBench {
255
256public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000257 ColorFilterGrayBench(bool small) : INHERITED(small) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000258 }
259
260protected:
261 virtual const char* onGetName() SK_OVERRIDE {
262 return isSmall() ? "colorfilter_gray_small" : "colorfilter_gray_large";
263 }
264
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000265 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
sugoi@google.com580a1722013-04-23 14:20:45 +0000266 SkRect r = getFilterRect();
267 SkPaint paint;
268 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000269 for (int i = 0; i < loops; i++) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000270 SkAutoTUnref<SkImageFilter> grayscale(make_grayscale());
271 paint.setImageFilter(grayscale);
272 canvas->drawRect(r, paint);
273 }
274 }
275
276private:
277 typedef ColorFilterBaseBench INHERITED;
278};
279
280class TableColorFilterBench : public ColorFilterBaseBench {
281
282public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000283 TableColorFilterBench(bool small) : INHERITED(small) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000284 }
285
286protected:
287 virtual const char* onGetName() SK_OVERRIDE {
288 return isSmall() ? "table_colorfilter_small" : "table_colorfilter_large";
289 }
290
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000291 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
sugoi@google.com580a1722013-04-23 14:20:45 +0000292 SkRect r = getFilterRect();
293 SkPaint paint;
294 paint.setColor(SK_ColorRED);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000295 for (int i = 0; i < loops; i++) {
sugoi@google.com580a1722013-04-23 14:20:45 +0000296 SkAutoTUnref<SkColorFilter> table_filter(make_table_filter());
297 paint.setColorFilter(table_filter);
298 canvas->drawRect(r, paint);
299 }
300 }
301
302private:
303 static void fill_table_data(uint8_t table[]) {
304 for (int i = 0; i < 256; ++i) {
305 int n = i >> 5;
306 table[i] = (n << 5) | (n << 2) | (n >> 1);
307 }
308 }
309
310 static SkColorFilter* make_table_filter() {
311 uint8_t table[256]; fill_table_data(table);
312 return SkTableColorFilter::Create(table);
313 }
314
315 typedef ColorFilterBaseBench INHERITED;
316};
317
commit-bot@chromium.org6c1ee2d2013-10-07 18:00:17 +0000318class LumaColorFilterBench : public ColorFilterBaseBench {
319
320public:
321 LumaColorFilterBench(bool small) : INHERITED(small) {
322 }
323
324protected:
325 virtual const char* onGetName() SK_OVERRIDE {
326 return isSmall() ? "luma_colorfilter_small" : "luma_colorfilter_large";
327 }
328
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000329 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
commit-bot@chromium.org6c1ee2d2013-10-07 18:00:17 +0000330 SkRect r = getFilterRect();
331 SkPaint paint;
332 paint.setColor(SK_ColorRED);
333
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000334 for (int i = 0; i < loops; i++) {
commit-bot@chromium.org6c1ee2d2013-10-07 18:00:17 +0000335 SkAutoTUnref<SkColorFilter> luma_filter(SkLumaColorFilter::Create());
336 paint.setColorFilter(luma_filter);
337 canvas->drawRect(r, paint);
338 }
339 }
340
341private:
342 typedef ColorFilterBaseBench INHERITED;
343};
344
sugoi@google.com580a1722013-04-23 14:20:45 +0000345///////////////////////////////////////////////////////////////////////////////
346
mtklein@google.com410e6e82013-09-13 19:52:27 +0000347DEF_BENCH( return new ColorFilterDimBrightBench(true); )
348DEF_BENCH( return new ColorFilterBrightGrayBench(true); )
349DEF_BENCH( return new ColorFilterGrayBrightBench(true); )
350DEF_BENCH( return new ColorFilterBlueBrightBench(true); )
351DEF_BENCH( return new ColorFilterBrightBlueBench(true); )
352DEF_BENCH( return new ColorFilterBrightBench(true); )
353DEF_BENCH( return new ColorFilterBlueBench(true); )
354DEF_BENCH( return new ColorFilterGrayBench(true); )
355DEF_BENCH( return new TableColorFilterBench(true); )
commit-bot@chromium.org6c1ee2d2013-10-07 18:00:17 +0000356DEF_BENCH( return new LumaColorFilterBench(true); )
sugoi@google.com580a1722013-04-23 14:20:45 +0000357
mtklein@google.com410e6e82013-09-13 19:52:27 +0000358DEF_BENCH( return new ColorFilterDimBrightBench(false); )
359DEF_BENCH( return new ColorFilterBrightGrayBench(false); )
360DEF_BENCH( return new ColorFilterGrayBrightBench(false); )
361DEF_BENCH( return new ColorFilterBlueBrightBench(false); )
362DEF_BENCH( return new ColorFilterBrightBlueBench(false); )
363DEF_BENCH( return new ColorFilterBrightBench(false); )
364DEF_BENCH( return new ColorFilterBlueBench(false); )
365DEF_BENCH( return new ColorFilterGrayBench(false); )
366DEF_BENCH( return new TableColorFilterBench(false); )
commit-bot@chromium.org6c1ee2d2013-10-07 18:00:17 +0000367DEF_BENCH( return new LumaColorFilterBench(false); )