OLD | NEW |
---|---|
(Empty) | |
1 /* | |
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 filter imager filter or several | |
Stephen White
2015/01/21 16:50:07
Nit: imager filter -> image filters
| |
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 | |
23 class BaseImageFilterCollapseBench : public Benchmark { | |
24 public: | |
25 BaseImageFilterCollapseBench(): imageFilter(NULL) {} | |
26 ~BaseImageFilterCollapseBench() { | |
27 if (imageFilter) { | |
28 imageFilter->unref(); | |
Stephen White
2015/01/21 16:50:07
Nit: could also use SkSafeUnref(imageFilter).
| |
29 } | |
30 } | |
31 | |
32 protected: | |
33 void doPreDraw(SkColorFilter* colorFilters[], int nFilters) { | |
34 // Create a chain of ImageFilters from colorFilters | |
35 imageFilter = NULL; | |
36 for(int i = nFilters; i --> 0;) { | |
37 SkImageFilter* filter = SkColorFilterImageFilter::Create(colorFilter s[i], | |
38 imageFilter , NULL, 0); | |
39 | |
40 if (imageFilter) { | |
41 imageFilter->unref(); | |
42 } | |
43 imageFilter = filter; | |
Stephen White
2015/01/21 16:50:07
Could use SkRefCnt_SafeAssign. This will eliminate
| |
44 } | |
45 } | |
46 | |
47 void onDraw(int loops, SkCanvas* canvas) SK_OVERRIDE { | |
48 makeBitmap(); | |
49 | |
50 for(int i = 0; i < loops; i++) { | |
51 SkPaint paint; | |
52 paint.setImageFilter(imageFilter); | |
53 canvas->drawBitmap(bm, 0, 0, &paint); | |
54 } | |
55 } | |
56 | |
57 private: | |
58 SkImageFilter* imageFilter; | |
Stephen White
2015/01/21 16:50:07
Fields in Skia should be prefixed with 'f', so thi
| |
59 SkBitmap bm; | |
Stephen White
2015/01/21 16:50:07
Same here. fBM, or fbm, or (my preference) fBitmap
| |
60 | |
61 void makeBitmap() { | |
62 int W = 400; | |
63 int H = 400; | |
64 bm.allocN32Pixels(W, H); | |
65 bm.eraseColor(SK_ColorTRANSPARENT); | |
66 | |
67 SkCanvas canvas(bm); | |
68 SkPaint paint; | |
69 SkPoint pts[] = { {0, 0}, {SkIntToScalar(W), SkIntToScalar(H)} }; | |
70 SkColor colors[] = { | |
71 SK_ColorBLACK, SK_ColorGREEN, SK_ColorCYAN, | |
72 SK_ColorRED, 0, SK_ColorBLUE, SK_ColorWHITE | |
73 }; | |
74 SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY _COUNT(colors), | |
75 SkShader::kClamp_TileMode); | |
76 paint.setShader(s)->unref(); | |
Stephen White
2015/01/21 16:50:07
Could use SkAutoTUnref here to avoid the need for
| |
77 canvas.drawPaint(paint); | |
78 } | |
79 }; | |
80 | |
81 class TableCollapseBench: public BaseImageFilterCollapseBench { | |
82 public: | |
83 virtual ~TableCollapseBench() {} | |
84 | |
85 protected: | |
86 virtual const char* onGetName() SK_OVERRIDE { | |
87 return "image_filter_collapse_table"; | |
88 } | |
89 | |
90 virtual void onPreDraw() SK_OVERRIDE { | |
91 for (int i = 0; i < 256; ++i) { | |
92 int n = i >> 5; | |
93 table1[i] = (n << 5) | (n << 2) | (n >> 1); | |
94 | |
95 table2[i] = i * i / 255; | |
96 | |
97 float fi = i / 255.0f; | |
98 table3[i] = static_cast<uint8_t>(sqrtf(fi) * 255); | |
99 } | |
100 | |
101 SkColorFilter* colorFilters[] = { | |
102 SkTableColorFilter::Create(table1), | |
103 SkTableColorFilter::Create(table2), | |
104 SkTableColorFilter::Create(table3), | |
105 }; | |
106 | |
107 doPreDraw(colorFilters, SK_ARRAY_COUNT(colorFilters)); | |
108 | |
109 for(unsigned i = 0; i < SK_ARRAY_COUNT(colorFilters); i++) { | |
110 colorFilters[i]->unref(); | |
111 } | |
112 } | |
113 | |
114 private: | |
115 uint8_t table1[256], table2[256], table3[256]; | |
116 }; | |
117 | |
118 static SkColorFilter* make_brightness(float amount) { | |
119 SkScalar amount255 = SkScalarMul(amount, SkIntToScalar(255)); | |
120 SkScalar matrix[20] = { 1, 0, 0, 0, amount255, | |
121 0, 1, 0, 0, amount255, | |
122 0, 0, 1, 0, amount255, | |
123 0, 0, 0, 1, 0 }; | |
124 return SkColorMatrixFilter::Create(matrix); | |
125 } | |
126 | |
127 static SkColorFilter* make_grayscale() { | |
128 SkScalar matrix[20]; | |
129 memset(matrix, 0, 20 * sizeof(SkScalar)); | |
130 matrix[0] = matrix[5] = matrix[10] = 0.2126f; | |
131 matrix[1] = matrix[6] = matrix[11] = 0.7152f; | |
132 matrix[2] = matrix[7] = matrix[12] = 0.0722f; | |
133 matrix[18] = 1.0f; | |
134 return SkColorMatrixFilter::Create(matrix); | |
135 } | |
136 | |
137 class MatrixCollapseBench: public BaseImageFilterCollapseBench { | |
138 public: | |
139 virtual ~MatrixCollapseBench() {} | |
140 | |
141 protected: | |
142 virtual const char* onGetName() SK_OVERRIDE { | |
143 return "image_filter_collapse_matrix"; | |
144 } | |
145 | |
146 virtual void onPreDraw() SK_OVERRIDE { | |
147 SkColorFilter* colorFilters[] = { | |
148 make_brightness(0.1f), | |
149 make_grayscale(), | |
150 make_brightness(-0.1f), | |
151 }; | |
152 | |
153 doPreDraw(colorFilters, SK_ARRAY_COUNT(colorFilters)); | |
154 | |
155 for(unsigned i = 0; i < SK_ARRAY_COUNT(colorFilters); i++) { | |
156 colorFilters[i]->unref(); | |
157 } | |
158 } | |
159 }; | |
160 | |
161 DEF_BENCH(return new TableCollapseBench;) | |
162 DEF_BENCH(return new MatrixCollapseBench;) | |
OLD | NEW |