Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(299)

Side by Side Diff: tests/ColorFilterTest.cpp

Issue 1827433002: Reland of [2] of "switch colorfilters to sk_sp (patchset #11 id:200001 of https://codereview.chromium.o… (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/utils/SkRGBAToYUV.cpp ('k') | tests/ColorMatrixTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2011 Google Inc. 2 * Copyright 2011 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 "SkColor.h" 8 #include "SkColor.h"
9 #include "SkColorFilter.h" 9 #include "SkColorFilter.h"
10 #include "SkColorPriv.h" 10 #include "SkColorPriv.h"
11 #include "SkLumaColorFilter.h" 11 #include "SkLumaColorFilter.h"
12 #include "SkReadBuffer.h" 12 #include "SkReadBuffer.h"
13 #include "SkWriteBuffer.h" 13 #include "SkWriteBuffer.h"
14 #include "SkRandom.h" 14 #include "SkRandom.h"
15 #include "SkXfermode.h" 15 #include "SkXfermode.h"
16 #include "Test.h" 16 #include "Test.h"
17 17
18 static SkColorFilter* reincarnate_colorfilter(SkFlattenable* obj) { 18 static sk_sp<SkColorFilter> reincarnate_colorfilter(SkFlattenable* obj) {
19 SkWriteBuffer wb; 19 SkWriteBuffer wb;
20 wb.writeFlattenable(obj); 20 wb.writeFlattenable(obj);
21 21
22 size_t size = wb.bytesWritten(); 22 size_t size = wb.bytesWritten();
23 SkAutoSMalloc<1024> storage(size); 23 SkAutoSMalloc<1024> storage(size);
24 // make a copy into storage 24 // make a copy into storage
25 wb.writeToMemory(storage.get()); 25 wb.writeToMemory(storage.get());
26 26
27 SkReadBuffer rb(storage.get(), size); 27 SkReadBuffer rb(storage.get(), size);
28 return rb.readColorFilter(); 28 return rb.readColorFilter();
29 } 29 }
30 30
31 /////////////////////////////////////////////////////////////////////////////// 31 ///////////////////////////////////////////////////////////////////////////////
32 32
33 static SkColorFilter* make_filter() { 33 static sk_sp<SkColorFilter> make_filter() {
34 // pick a filter that cannot compose with itself via newComposed() 34 // pick a filter that cannot compose with itself via newComposed()
35 return SkColorFilter::CreateModeFilter(SK_ColorRED, SkXfermode::kColorBurn_M ode); 35 return SkColorFilter::MakeModeFilter(SK_ColorRED, SkXfermode::kColorBurn_Mod e);
36 } 36 }
37 37
38 static void test_composecolorfilter_limit(skiatest::Reporter* reporter) { 38 static void test_composecolorfilter_limit(skiatest::Reporter* reporter) {
39 // Test that CreateComposeFilter() has some finite limit (i.e. that the fact ory can return null) 39 // Test that CreateComposeFilter() has some finite limit (i.e. that the fact ory can return null)
40 const int way_too_many = 100; 40 const int way_too_many = 100;
41 SkAutoTUnref<SkColorFilter> parent(make_filter()); 41 auto parent(make_filter());
42 for (int i = 2; i < way_too_many; ++i) { 42 for (int i = 2; i < way_too_many; ++i) {
43 SkAutoTUnref<SkColorFilter> filter(make_filter()); 43 auto filter(make_filter());
44 parent.reset(SkColorFilter::CreateComposeFilter(parent, filter)); 44 parent = SkColorFilter::MakeComposeFilter(parent, filter);
45 if (nullptr == parent) { 45 if (nullptr == parent) {
46 REPORTER_ASSERT(reporter, i > 2); // we need to have succeeded at le ast once! 46 REPORTER_ASSERT(reporter, i > 2); // we need to have succeeded at le ast once!
47 return; 47 return;
48 } 48 }
49 } 49 }
50 REPORTER_ASSERT(reporter, false); // we never saw a nullptr :( 50 REPORTER_ASSERT(reporter, false); // we never saw a nullptr :(
51 } 51 }
52 52
53 #define ILLEGAL_MODE ((SkXfermode::Mode)-1) 53 #define ILLEGAL_MODE ((SkXfermode::Mode)-1)
54 54
55 DEF_TEST(ColorFilter, reporter) { 55 DEF_TEST(ColorFilter, reporter) {
56 SkRandom rand; 56 SkRandom rand;
57 57
58 for (int mode = 0; mode <= SkXfermode::kLastMode; mode++) { 58 for (int mode = 0; mode <= SkXfermode::kLastMode; mode++) {
59 SkColor color = rand.nextU(); 59 SkColor color = rand.nextU();
60 60
61 // ensure we always get a filter, by avoiding the possibility of a 61 // ensure we always get a filter, by avoiding the possibility of a
62 // special case that would return nullptr (if color's alpha is 0 or 0xFF ) 62 // special case that would return nullptr (if color's alpha is 0 or 0xFF )
63 color = SkColorSetA(color, 0x7F); 63 color = SkColorSetA(color, 0x7F);
64 64
65 SkColorFilter* cf = SkColorFilter::CreateModeFilter(color, 65 auto cf = SkColorFilter::MakeModeFilter(color, (SkXfermode::Mode)mode);
66 (SkXfermode::Mode)mode);
67 66
68 // allow for no filter if we're in Dst mode (its a no op) 67 // allow for no filter if we're in Dst mode (its a no op)
69 if (SkXfermode::kDst_Mode == mode && nullptr == cf) { 68 if (SkXfermode::kDst_Mode == mode && nullptr == cf) {
70 continue; 69 continue;
71 } 70 }
72 71
73 SkAutoUnref aur(cf);
74 REPORTER_ASSERT(reporter, cf); 72 REPORTER_ASSERT(reporter, cf);
75 73
76 SkColor c = ~color; 74 SkColor c = ~color;
77 SkXfermode::Mode m = ILLEGAL_MODE; 75 SkXfermode::Mode m = ILLEGAL_MODE;
78 76
79 SkColor expectedColor = color; 77 SkColor expectedColor = color;
80 SkXfermode::Mode expectedMode = (SkXfermode::Mode)mode; 78 SkXfermode::Mode expectedMode = (SkXfermode::Mode)mode;
81 79
82 // SkDebugf("--- mc [%d %x] ", mode, color); 80 // SkDebugf("--- mc [%d %x] ", mode, color);
83 81
84 REPORTER_ASSERT(reporter, cf->asColorMode(&c, &m)); 82 REPORTER_ASSERT(reporter, cf->asColorMode(&c, &m));
85 // handle special-case folding by the factory 83 // handle special-case folding by the factory
86 if (SkXfermode::kClear_Mode == mode) { 84 if (SkXfermode::kClear_Mode == mode) {
87 if (c != expectedColor) { 85 if (c != expectedColor) {
88 expectedColor = 0; 86 expectedColor = 0;
89 } 87 }
90 if (m != expectedMode) { 88 if (m != expectedMode) {
91 expectedMode = SkXfermode::kSrc_Mode; 89 expectedMode = SkXfermode::kSrc_Mode;
92 } 90 }
93 } 91 }
94 92
95 // SkDebugf("--- got [%d %x] expected [%d %x]\n", m, c, expectedMode, exp ectedColor); 93 // SkDebugf("--- got [%d %x] expected [%d %x]\n", m, c, expectedMode, exp ectedColor);
96 94
97 REPORTER_ASSERT(reporter, c == expectedColor); 95 REPORTER_ASSERT(reporter, c == expectedColor);
98 REPORTER_ASSERT(reporter, m == expectedMode); 96 REPORTER_ASSERT(reporter, m == expectedMode);
99 97
100 { 98 {
101 SkColorFilter* cf2 = reincarnate_colorfilter(cf); 99 auto cf2 = reincarnate_colorfilter(cf.get());
102 SkAutoUnref aur2(cf2);
103 REPORTER_ASSERT(reporter, cf2); 100 REPORTER_ASSERT(reporter, cf2);
104 101
105 SkColor c2 = ~color; 102 SkColor c2 = ~color;
106 SkXfermode::Mode m2 = ILLEGAL_MODE; 103 SkXfermode::Mode m2 = ILLEGAL_MODE;
107 REPORTER_ASSERT(reporter, cf2->asColorMode(&c2, &m2)); 104 REPORTER_ASSERT(reporter, cf2->asColorMode(&c2, &m2));
108 REPORTER_ASSERT(reporter, c2 == expectedColor); 105 REPORTER_ASSERT(reporter, c2 == expectedColor);
109 REPORTER_ASSERT(reporter, m2 == expectedMode); 106 REPORTER_ASSERT(reporter, m2 == expectedMode);
110 } 107 }
111 } 108 }
112 109
113 test_composecolorfilter_limit(reporter); 110 test_composecolorfilter_limit(reporter);
114 } 111 }
115 112
116 /////////////////////////////////////////////////////////////////////////////// 113 ///////////////////////////////////////////////////////////////////////////////
117 114
118 DEF_TEST(LumaColorFilter, reporter) { 115 DEF_TEST(LumaColorFilter, reporter) {
119 SkPMColor in, out; 116 SkPMColor in, out;
120 SkAutoTUnref<SkColorFilter> lf(SkLumaColorFilter::Create()); 117 auto lf(SkLumaColorFilter::Make());
121 118
122 // Applying luma to white produces black with the same transparency. 119 // Applying luma to white produces black with the same transparency.
123 for (unsigned i = 0; i < 256; ++i) { 120 for (unsigned i = 0; i < 256; ++i) {
124 in = SkPackARGB32(i, i, i, i); 121 in = SkPackARGB32(i, i, i, i);
125 lf->filterSpan(&in, 1, &out); 122 lf->filterSpan(&in, 1, &out);
126 REPORTER_ASSERT(reporter, SkGetPackedA32(out) == i); 123 REPORTER_ASSERT(reporter, SkGetPackedA32(out) == i);
127 REPORTER_ASSERT(reporter, SkGetPackedR32(out) == 0); 124 REPORTER_ASSERT(reporter, SkGetPackedR32(out) == 0);
128 REPORTER_ASSERT(reporter, SkGetPackedG32(out) == 0); 125 REPORTER_ASSERT(reporter, SkGetPackedG32(out) == 0);
129 REPORTER_ASSERT(reporter, SkGetPackedB32(out) == 0); 126 REPORTER_ASSERT(reporter, SkGetPackedB32(out) == 0);
130 } 127 }
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 173
177 matrix[10] = 0.2126f - 0.2126f * amount; 174 matrix[10] = 0.2126f - 0.2126f * amount;
178 matrix[11] = 0.7152f - 0.7152f * amount; 175 matrix[11] = 0.7152f - 0.7152f * amount;
179 matrix[12] = 1.f - (matrix[10] + matrix[11]); 176 matrix[12] = 1.f - (matrix[10] + matrix[11]);
180 matrix[13] = matrix[14] = 0.f; 177 matrix[13] = matrix[14] = 0.f;
181 178
182 matrix[15] = matrix[16] = matrix[17] = matrix[19] = 0.f; 179 matrix[15] = matrix[16] = matrix[17] = matrix[19] = 0.f;
183 matrix[18] = 1.f; 180 matrix[18] = 1.f;
184 } 181 }
185 182
186 static SkColorFilter* make_cf0() { 183 static sk_sp<SkColorFilter> make_cf0() {
187 SkScalar matrix[20]; 184 SkScalar matrix[20];
188 get_brightness_matrix(0.5f, matrix); 185 get_brightness_matrix(0.5f, matrix);
189 return SkColorMatrixFilter::Create(matrix); 186 return SkColorFilter::MakeMatrixFilterRowMajor255(matrix);
190 } 187 }
191 static SkColorFilter* make_cf1() { 188 static sk_sp<SkColorFilter> make_cf1() {
192 SkScalar matrix[20]; 189 SkScalar matrix[20];
193 get_grayscale_matrix(1, matrix); 190 get_grayscale_matrix(1, matrix);
194 return SkColorMatrixFilter::Create(matrix); 191 return SkColorFilter::MakeMatrixFilterRowMajor255(matrix);
195 } 192 }
196 static SkColorFilter* make_cf2() { 193 static sk_sp<SkColorFilter> make_cf2() {
197 SkColorMatrix m0, m1; 194 SkColorMatrix m0, m1;
198 get_brightness_matrix(0.5f, m0.fMat); 195 get_brightness_matrix(0.5f, m0.fMat);
199 get_grayscale_matrix(1, m1.fMat); 196 get_grayscale_matrix(1, m1.fMat);
200 m0.preConcat(m1); 197 m0.preConcat(m1);
201 return SkColorMatrixFilter::Create(m0); 198 return SkColorFilter::MakeMatrixFilterRowMajor255(m0.fMat);
202 } 199 }
203 static SkColorFilter* make_cf3() { 200 static sk_sp<SkColorFilter> make_cf3() {
204 SkColorMatrix m0, m1; 201 SkColorMatrix m0, m1;
205 get_brightness_matrix(0.5f, m0.fMat); 202 get_brightness_matrix(0.5f, m0.fMat);
206 get_grayscale_matrix(1, m1.fMat); 203 get_grayscale_matrix(1, m1.fMat);
207 m0.postConcat(m1); 204 m0.postConcat(m1);
208 return SkColorMatrixFilter::Create(m0); 205 return SkColorFilter::MakeMatrixFilterRowMajor255(m0.fMat);
209 } 206 }
210 typedef SkColorFilter* (*CFProc)(); 207 typedef sk_sp<SkColorFilter> (*CFProc)();
211 208
212 // Test that a colormatrix that "should" preserve opaquness actually does. 209 // Test that a colormatrix that "should" preserve opaquness actually does.
213 DEF_TEST(ColorMatrixFilter, reporter) { 210 DEF_TEST(ColorMatrixFilter, reporter) {
214 const CFProc procs[] = { 211 const CFProc procs[] = {
215 make_cf0, make_cf1, make_cf2, make_cf3, 212 make_cf0, make_cf1, make_cf2, make_cf3,
216 }; 213 };
217 214
218 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) { 215 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) {
219 SkAutoTUnref<SkColorFilter> cf(procs[i]()); 216 auto cf(procs[i]());
220 217
221 // generate all possible r,g,b triples 218 // generate all possible r,g,b triples
222 for (int r = 0; r < 256; ++r) { 219 for (int r = 0; r < 256; ++r) {
223 for (int g = 0; g < 256; ++g) { 220 for (int g = 0; g < 256; ++g) {
224 SkPMColor storage[256]; 221 SkPMColor storage[256];
225 for (int b = 0; b < 256; ++b) { 222 for (int b = 0; b < 256; ++b) {
226 storage[b] = SkPackARGB32(0xFF, r, g, b); 223 storage[b] = SkPackARGB32(0xFF, r, g, b);
227 } 224 }
228 cf->filterSpan(storage, 256, storage); 225 cf->filterSpan(storage, 256, storage);
229 for (int b = 0; b < 256; ++b) { 226 for (int b = 0; b < 256; ++b) {
230 REPORTER_ASSERT(reporter, 0xFF == SkGetPackedA32(storage[b]) ); 227 REPORTER_ASSERT(reporter, 0xFF == SkGetPackedA32(storage[b]) );
231 } 228 }
232 } 229 }
233 } 230 }
234 } 231 }
235 } 232 }
OLDNEW
« no previous file with comments | « src/utils/SkRGBAToYUV.cpp ('k') | tests/ColorMatrixTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698