OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "Benchmark.h" | 8 #include "Benchmark.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 // the passes are collapsed in one. | 21 // the passes are collapsed in one. |
22 | 22 |
23 class BaseImageFilterCollapseBench : public Benchmark { | 23 class BaseImageFilterCollapseBench : public Benchmark { |
24 public: | 24 public: |
25 BaseImageFilterCollapseBench(): fImageFilter(nullptr) {} | 25 BaseImageFilterCollapseBench(): fImageFilter(nullptr) {} |
26 ~BaseImageFilterCollapseBench() { | 26 ~BaseImageFilterCollapseBench() { |
27 SkSafeUnref(fImageFilter); | 27 SkSafeUnref(fImageFilter); |
28 } | 28 } |
29 | 29 |
30 protected: | 30 protected: |
31 void doPreDraw(sk_sp<SkColorFilter> colorFilters[], int nFilters) { | 31 void doPreDraw(SkColorFilter* colorFilters[], int nFilters) { |
32 // Create a chain of ImageFilters from colorFilters | 32 // Create a chain of ImageFilters from colorFilters |
33 fImageFilter = nullptr; | 33 fImageFilter = nullptr; |
34 for(int i = nFilters; i --> 0;) { | 34 for(int i = nFilters; i --> 0;) { |
35 SkAutoTUnref<SkImageFilter> filter( | 35 SkAutoTUnref<SkImageFilter> filter( |
36 SkColorFilterImageFilter::Create(colorFilters[i].get(), fIma
geFilter, nullptr) | 36 SkColorFilterImageFilter::Create(colorFilters[i], fImage
Filter, nullptr) |
37 ); | 37 ); |
38 SkRefCnt_SafeAssign(fImageFilter, filter.get()); | 38 SkRefCnt_SafeAssign(fImageFilter, filter.get()); |
39 } | 39 } |
40 } | 40 } |
41 | 41 |
42 void onDraw(int loops, SkCanvas* canvas) override { | 42 void onDraw(int loops, SkCanvas* canvas) override { |
43 makeBitmap(); | 43 makeBitmap(); |
44 | 44 |
45 for(int i = 0; i < loops; i++) { | 45 for(int i = 0; i < loops; i++) { |
46 SkPaint paint; | 46 SkPaint paint; |
(...skipping 19 matching lines...) Expand all Loading... |
66 SK_ColorBLACK, SK_ColorGREEN, SK_ColorCYAN, | 66 SK_ColorBLACK, SK_ColorGREEN, SK_ColorCYAN, |
67 SK_ColorRED, 0, SK_ColorBLUE, SK_ColorWHITE | 67 SK_ColorRED, 0, SK_ColorBLUE, SK_ColorWHITE |
68 }; | 68 }; |
69 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_AR
RAY_COUNT(colors), | 69 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_AR
RAY_COUNT(colors), |
70 SkShader::kClamp_TileMode))
; | 70 SkShader::kClamp_TileMode))
; |
71 canvas.drawPaint(paint); | 71 canvas.drawPaint(paint); |
72 } | 72 } |
73 }; | 73 }; |
74 | 74 |
75 class TableCollapseBench: public BaseImageFilterCollapseBench { | 75 class TableCollapseBench: public BaseImageFilterCollapseBench { |
| 76 public: |
| 77 virtual ~TableCollapseBench() {} |
| 78 |
76 protected: | 79 protected: |
77 const char* onGetName() override { | 80 virtual const char* onGetName() override { |
78 return "image_filter_collapse_table"; | 81 return "image_filter_collapse_table"; |
79 } | 82 } |
80 | 83 |
81 void onDelayedSetup() override { | 84 virtual void onDelayedSetup() override { |
82 for (int i = 0; i < 256; ++i) { | 85 for (int i = 0; i < 256; ++i) { |
83 int n = i >> 5; | 86 int n = i >> 5; |
84 table1[i] = (n << 5) | (n << 2) | (n >> 1); | 87 table1[i] = (n << 5) | (n << 2) | (n >> 1); |
85 | 88 |
86 table2[i] = i * i / 255; | 89 table2[i] = i * i / 255; |
87 | 90 |
88 float fi = i / 255.0f; | 91 float fi = i / 255.0f; |
89 table3[i] = static_cast<uint8_t>(sqrtf(fi) * 255); | 92 table3[i] = static_cast<uint8_t>(sqrtf(fi) * 255); |
90 } | 93 } |
91 | 94 |
92 sk_sp<SkColorFilter> colorFilters[] = { | 95 SkColorFilter* colorFilters[] = { |
93 SkTableColorFilter::Make(table1), | 96 SkTableColorFilter::Create(table1), |
94 SkTableColorFilter::Make(table2), | 97 SkTableColorFilter::Create(table2), |
95 SkTableColorFilter::Make(table3), | 98 SkTableColorFilter::Create(table3), |
96 }; | 99 }; |
97 | 100 |
98 doPreDraw(colorFilters, SK_ARRAY_COUNT(colorFilters)); | 101 doPreDraw(colorFilters, SK_ARRAY_COUNT(colorFilters)); |
| 102 |
| 103 for(unsigned i = 0; i < SK_ARRAY_COUNT(colorFilters); i++) { |
| 104 colorFilters[i]->unref(); |
| 105 } |
99 } | 106 } |
100 | 107 |
101 private: | 108 private: |
102 uint8_t table1[256], table2[256], table3[256]; | 109 uint8_t table1[256], table2[256], table3[256]; |
103 }; | 110 }; |
104 | 111 |
105 static sk_sp<SkColorFilter> make_brightness(float amount) { | 112 static SkColorFilter* make_brightness(float amount) { |
106 SkScalar amount255 = SkScalarMul(amount, SkIntToScalar(255)); | 113 SkScalar amount255 = SkScalarMul(amount, SkIntToScalar(255)); |
107 SkScalar matrix[20] = { 1, 0, 0, 0, amount255, | 114 SkScalar matrix[20] = { 1, 0, 0, 0, amount255, |
108 0, 1, 0, 0, amount255, | 115 0, 1, 0, 0, amount255, |
109 0, 0, 1, 0, amount255, | 116 0, 0, 1, 0, amount255, |
110 0, 0, 0, 1, 0 }; | 117 0, 0, 0, 1, 0 }; |
111 return SkColorFilter::MakeMatrixFilterRowMajor255(matrix); | 118 return SkColorMatrixFilter::Create(matrix); |
112 } | 119 } |
113 | 120 |
114 static sk_sp<SkColorFilter> make_grayscale() { | 121 static SkColorFilter* make_grayscale() { |
115 SkScalar matrix[20]; | 122 SkScalar matrix[20]; |
116 memset(matrix, 0, 20 * sizeof(SkScalar)); | 123 memset(matrix, 0, 20 * sizeof(SkScalar)); |
117 matrix[0] = matrix[5] = matrix[10] = 0.2126f; | 124 matrix[0] = matrix[5] = matrix[10] = 0.2126f; |
118 matrix[1] = matrix[6] = matrix[11] = 0.7152f; | 125 matrix[1] = matrix[6] = matrix[11] = 0.7152f; |
119 matrix[2] = matrix[7] = matrix[12] = 0.0722f; | 126 matrix[2] = matrix[7] = matrix[12] = 0.0722f; |
120 matrix[18] = 1.0f; | 127 matrix[18] = 1.0f; |
121 return SkColorFilter::MakeMatrixFilterRowMajor255(matrix); | 128 return SkColorMatrixFilter::Create(matrix); |
122 } | 129 } |
123 | 130 |
124 class MatrixCollapseBench: public BaseImageFilterCollapseBench { | 131 class MatrixCollapseBench: public BaseImageFilterCollapseBench { |
| 132 public: |
| 133 virtual ~MatrixCollapseBench() {} |
| 134 |
125 protected: | 135 protected: |
126 const char* onGetName() override { | 136 virtual const char* onGetName() override { |
127 return "image_filter_collapse_matrix"; | 137 return "image_filter_collapse_matrix"; |
128 } | 138 } |
129 | 139 |
130 void onDelayedSetup() override { | 140 virtual void onDelayedSetup() override { |
131 sk_sp<SkColorFilter> colorFilters[] = { | 141 SkColorFilter* colorFilters[] = { |
132 make_brightness(0.1f), | 142 make_brightness(0.1f), |
133 make_grayscale(), | 143 make_grayscale(), |
134 make_brightness(-0.1f), | 144 make_brightness(-0.1f), |
135 }; | 145 }; |
136 | 146 |
137 doPreDraw(colorFilters, SK_ARRAY_COUNT(colorFilters)); | 147 doPreDraw(colorFilters, SK_ARRAY_COUNT(colorFilters)); |
| 148 |
| 149 for(unsigned i = 0; i < SK_ARRAY_COUNT(colorFilters); i++) { |
| 150 colorFilters[i]->unref(); |
| 151 } |
138 } | 152 } |
139 }; | 153 }; |
140 | 154 |
141 DEF_BENCH(return new TableCollapseBench;) | 155 DEF_BENCH(return new TableCollapseBench;) |
142 DEF_BENCH(return new MatrixCollapseBench;) | 156 DEF_BENCH(return new MatrixCollapseBench;) |
OLD | NEW |