blob: bd3f80fefb59a2b9f89b1f46c34ec8d54b0736f0 [file] [log] [blame]
cwallezc12b74d2015-01-26 07:45:53 -08001/*
2 * Copyright 2014 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 */
7
8#include "Benchmark.h"
9#include "SkBitmap.h"
10#include "SkCanvas.h"
11#include "SkColorFilterImageFilter.h"
12#include "SkColorMatrixFilter.h"
13#include "SkGradientShader.h"
14#include "SkImageFilter.h"
15#include "SkTableColorFilter.h"
16
17// Chains several matrix color filters image filter or several
18// table filter image filters and draws a bitmap.
19// This bench shows an improvement in performance and memory
20// when collapsing matrices or tables is implemented since all
21// the passes are collapsed in one.
22
23class BaseImageFilterCollapseBench : public Benchmark {
24public:
halcanary96fcdcc2015-08-27 07:41:13 -070025 BaseImageFilterCollapseBench(): fImageFilter(nullptr) {}
cwallezc12b74d2015-01-26 07:45:53 -080026 ~BaseImageFilterCollapseBench() {
27 SkSafeUnref(fImageFilter);
28 }
29
30protected:
31 void doPreDraw(SkColorFilter* colorFilters[], int nFilters) {
32 // Create a chain of ImageFilters from colorFilters
halcanary96fcdcc2015-08-27 07:41:13 -070033 fImageFilter = nullptr;
cwallezc12b74d2015-01-26 07:45:53 -080034 for(int i = nFilters; i --> 0;) {
35 SkAutoTUnref<SkImageFilter> filter(
halcanary96fcdcc2015-08-27 07:41:13 -070036 SkColorFilterImageFilter::Create(colorFilters[i], fImageFilter, nullptr)
cwallezc12b74d2015-01-26 07:45:53 -080037 );
38 SkRefCnt_SafeAssign(fImageFilter, filter.get());
39 }
40 }
41
mtkleina1ebeb22015-10-01 09:43:39 -070042 void onDraw(int loops, SkCanvas* canvas) override {
cwallezc12b74d2015-01-26 07:45:53 -080043 makeBitmap();
44
45 for(int i = 0; i < loops; i++) {
46 SkPaint paint;
47 paint.setImageFilter(fImageFilter);
48 canvas->drawBitmap(fBitmap, 0, 0, &paint);
49 }
50 }
51
52private:
53 SkImageFilter* fImageFilter;
54 SkBitmap fBitmap;
55
56 void makeBitmap() {
57 int W = 400;
58 int H = 400;
59 fBitmap.allocN32Pixels(W, H);
60 fBitmap.eraseColor(SK_ColorTRANSPARENT);
61
62 SkCanvas canvas(fBitmap);
63 SkPaint paint;
64 SkPoint pts[] = { {0, 0}, {SkIntToScalar(W), SkIntToScalar(H)} };
65 SkColor colors[] = {
66 SK_ColorBLACK, SK_ColorGREEN, SK_ColorCYAN,
67 SK_ColorRED, 0, SK_ColorBLUE, SK_ColorWHITE
68 };
reedc6f28f72016-03-14 12:22:10 -070069 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
70 SkShader::kClamp_TileMode));
cwallezc12b74d2015-01-26 07:45:53 -080071 canvas.drawPaint(paint);
72 }
73};
74
75class TableCollapseBench: public BaseImageFilterCollapseBench {
76public:
77 virtual ~TableCollapseBench() {}
78
79protected:
mtklein36352bf2015-03-25 18:17:31 -070080 virtual const char* onGetName() override {
cwallezc12b74d2015-01-26 07:45:53 -080081 return "image_filter_collapse_table";
82 }
83
joshualitt8a6697a2015-09-30 12:11:07 -070084 virtual void onDelayedSetup() override {
cwallezc12b74d2015-01-26 07:45:53 -080085 for (int i = 0; i < 256; ++i) {
86 int n = i >> 5;
87 table1[i] = (n << 5) | (n << 2) | (n >> 1);
88
89 table2[i] = i * i / 255;
90
91 float fi = i / 255.0f;
92 table3[i] = static_cast<uint8_t>(sqrtf(fi) * 255);
93 }
94
95 SkColorFilter* colorFilters[] = {
96 SkTableColorFilter::Create(table1),
97 SkTableColorFilter::Create(table2),
98 SkTableColorFilter::Create(table3),
99 };
100
101 doPreDraw(colorFilters, SK_ARRAY_COUNT(colorFilters));
102
103 for(unsigned i = 0; i < SK_ARRAY_COUNT(colorFilters); i++) {
104 colorFilters[i]->unref();
105 }
106 }
107
108private:
109 uint8_t table1[256], table2[256], table3[256];
110};
111
112static SkColorFilter* make_brightness(float amount) {
113 SkScalar amount255 = SkScalarMul(amount, SkIntToScalar(255));
114 SkScalar matrix[20] = { 1, 0, 0, 0, amount255,
115 0, 1, 0, 0, amount255,
116 0, 0, 1, 0, amount255,
117 0, 0, 0, 1, 0 };
118 return SkColorMatrixFilter::Create(matrix);
119}
120
121static SkColorFilter* make_grayscale() {
122 SkScalar matrix[20];
123 memset(matrix, 0, 20 * sizeof(SkScalar));
124 matrix[0] = matrix[5] = matrix[10] = 0.2126f;
125 matrix[1] = matrix[6] = matrix[11] = 0.7152f;
126 matrix[2] = matrix[7] = matrix[12] = 0.0722f;
127 matrix[18] = 1.0f;
128 return SkColorMatrixFilter::Create(matrix);
129}
130
131class MatrixCollapseBench: public BaseImageFilterCollapseBench {
132public:
133 virtual ~MatrixCollapseBench() {}
134
135protected:
mtklein36352bf2015-03-25 18:17:31 -0700136 virtual const char* onGetName() override {
cwallezc12b74d2015-01-26 07:45:53 -0800137 return "image_filter_collapse_matrix";
138 }
139
joshualitt8a6697a2015-09-30 12:11:07 -0700140 virtual void onDelayedSetup() override {
cwallezc12b74d2015-01-26 07:45:53 -0800141 SkColorFilter* colorFilters[] = {
142 make_brightness(0.1f),
143 make_grayscale(),
144 make_brightness(-0.1f),
145 };
146
147 doPreDraw(colorFilters, SK_ARRAY_COUNT(colorFilters));
148
149 for(unsigned i = 0; i < SK_ARRAY_COUNT(colorFilters); i++) {
150 colorFilters[i]->unref();
151 }
152 }
153};
154
155DEF_BENCH(return new TableCollapseBench;)
156DEF_BENCH(return new MatrixCollapseBench;)