OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 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 "SkBitmapProcShader.h" | 8 #include "SkBitmapProcShader.h" |
9 #include "SkColor.h" | 9 #include "SkColor.h" |
10 #include "SkColorMatrixFilter.h" | 10 #include "SkColorMatrixFilter.h" |
(...skipping 25 matching lines...) Expand all Loading... |
36 const struct { | 36 const struct { |
37 SkColor fC; | 37 SkColor fC; |
38 SkColor4f fC4; | 38 SkColor4f fC4; |
39 } recs[] = { | 39 } recs[] = { |
40 { SK_ColorBLACK, { 0, 0, 0, 1 } }, | 40 { SK_ColorBLACK, { 0, 0, 0, 1 } }, |
41 { SK_ColorWHITE, { 1, 1, 1, 1 } }, | 41 { SK_ColorWHITE, { 1, 1, 1, 1 } }, |
42 { SK_ColorRED, { 1, 0, 0, 1 } }, | 42 { SK_ColorRED, { 1, 0, 0, 1 } }, |
43 { SK_ColorGREEN, { 0, 1, 0, 1 } }, | 43 { SK_ColorGREEN, { 0, 1, 0, 1 } }, |
44 { SK_ColorBLUE, { 0, 0, 1, 1 } }, | 44 { SK_ColorBLUE, { 0, 0, 1, 1 } }, |
45 { 0, { 0, 0, 0, 0 } }, | 45 { 0, { 0, 0, 0, 0 } }, |
46 { 0x55AAFF00, { 2/3.0f, 1, 0, 1 / 3.0f } }, | |
47 }; | 46 }; |
48 | 47 |
49 for (const auto& r : recs) { | 48 for (const auto& r : recs) { |
50 SkColor4f c4 = SkColor4f::FromColor(r.fC); | 49 SkColor4f c4 = SkColor4f::FromColor(r.fC); |
51 REPORTER_ASSERT(reporter, c4 == r.fC4); | 50 REPORTER_ASSERT(reporter, c4 == r.fC4); |
52 } | 51 } |
53 } | 52 } |
54 | 53 |
55 DEF_TEST(Color4f_premul, reporter) { | 54 DEF_TEST(Color4f_premul, reporter) { |
56 SkRandom rand; | 55 SkRandom rand; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 0, 0, 1, 0, -0.1f, | 89 0, 0, 1, 0, -0.1f, |
91 0, 0, 0, 1, 0, | 90 0, 0, 0, 1, 0, |
92 }; | 91 }; |
93 return SkColorFilter::MakeMatrixFilterRowMajor255(mx); | 92 return SkColorFilter::MakeMatrixFilterRowMajor255(mx); |
94 } | 93 } |
95 | 94 |
96 static sk_sp<SkColorFilter> make_compose_cf() { | 95 static sk_sp<SkColorFilter> make_compose_cf() { |
97 return SkColorFilter::MakeComposeFilter(make_mode_cf(), make_mx_cf()); | 96 return SkColorFilter::MakeComposeFilter(make_mode_cf(), make_mx_cf()); |
98 } | 97 } |
99 | 98 |
100 static sk_sp<SkShader> make_color_sh() { return SkShader::MakeColorShader(0xFFBB
8855); } | |
101 | |
102 static sk_sp<SkShader> make_image_sh() { | |
103 const SkImageInfo info = SkImageInfo::MakeN32Premul(2, 2); | |
104 const SkPMColor pixels[] { | |
105 SkPackARGB32(0xFF, 0xBB, 0x88, 0x55), | |
106 SkPackARGB32(0xFF, 0xBB, 0x88, 0x55), | |
107 SkPackARGB32(0xFF, 0xBB, 0x88, 0x55), | |
108 SkPackARGB32(0xFF, 0xBB, 0x88, 0x55), | |
109 }; | |
110 sk_sp<SkImage> image(SkImage::MakeRasterCopy(SkPixmap(info, pixels, sizeof(S
kPMColor) * 2))); | |
111 return image->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMod
e); | |
112 } | |
113 | |
114 static sk_sp<SkShader> make_grad_sh() { | |
115 #if 0 | |
116 const SkPoint pts[] {{ 0, 0 }, { 100, 100 }}; | |
117 const SkColor colors[] { SK_ColorRED, SK_ColorBLUE }; | |
118 return SkGradientShader::CreateLinear(pts, colors, nullptr, 2, SkShader::kCl
amp_TileMode); | |
119 #else | |
120 // TODO: need to convert new gradient code to enforce PM4f --> RGBA order | |
121 return make_color_sh(); | |
122 #endif | |
123 } | |
124 | |
125 static sk_sp<SkShader> make_cf_sh() { | |
126 return make_color_sh()->makeWithColorFilter(make_mx_cf()); | |
127 } | |
128 | |
129 static bool compare_spans(const SkPM4f span4f[], const SkPMColor span4b[], int c
ount, | 99 static bool compare_spans(const SkPM4f span4f[], const SkPMColor span4b[], int c
ount, |
130 float tolerance = 1.0f/255) { | 100 float tolerance = 1.0f/255) { |
131 for (int i = 0; i < count; ++i) { | 101 for (int i = 0; i < count; ++i) { |
132 SkPM4f c0 = SkPM4f::FromPMColor(span4b[i]); | 102 SkPM4f c0 = SkPM4f::FromPMColor(span4b[i]); |
133 SkPM4f c1 = span4f[i]; | 103 SkPM4f c1 = span4f[i]; |
134 if (!nearly_equal(c0, c1, tolerance)) { | 104 if (!nearly_equal(c0, c1, tolerance)) { |
135 return false; | 105 return false; |
136 } | 106 } |
137 } | 107 } |
138 return true; | 108 return true; |
139 } | 109 } |
140 | 110 |
141 DEF_TEST(Color4f_shader, reporter) { | |
142 struct { | |
143 sk_sp<SkShader> (*fFact)(); | |
144 bool fSupports4f; | |
145 float fTolerance; | |
146 } recs[] = { | |
147 { make_color_sh, true, 1.0f/255 }, | |
148 // PMColor 4f gradients are interpolated in 255-multiplied values, so we
need a | |
149 // slightly relaxed tolerance to accommodate the cumulative precision de
viation. | |
150 { make_grad_sh, true, 1.001f/255 }, | |
151 { make_image_sh, false, 1.0f/255 }, | |
152 { make_cf_sh, true, 1.0f/255 }, | |
153 }; | |
154 | |
155 SkPaint paint; | |
156 for (const auto& rec : recs) { | |
157 uint32_t storage[kSkBlitterContextSize]; | |
158 paint.setShader(rec.fFact()); | |
159 // Encourage 4f context selection. At some point we may need | |
160 // to instantiate two separate contexts for optimal 4b/4f selection. | |
161 const SkShader::ContextRec contextRec(paint, SkMatrix::I(), nullptr, | |
162 SkShader::ContextRec::kPM4f_DstTyp
e); | |
163 SkASSERT(paint.getShader()->contextSize(contextRec) <= sizeof(storage)); | |
164 SkShader::Context* ctx = paint.getShader()->createContext(contextRec, st
orage); | |
165 if (rec.fSupports4f) { | |
166 const int N = 100; | |
167 SkPM4f buffer4f[N]; | |
168 ctx->shadeSpan4f(0, 0, buffer4f, N); | |
169 SkPMColor buffer4b[N]; | |
170 ctx->shadeSpan(0, 0, buffer4b, N); | |
171 REPORTER_ASSERT(reporter, compare_spans(buffer4f, buffer4b, N, rec.f
Tolerance)); | |
172 } | |
173 ctx->~Context(); | |
174 } | |
175 } | |
176 | |
177 DEF_TEST(Color4f_colorfilter, reporter) { | 111 DEF_TEST(Color4f_colorfilter, reporter) { |
178 struct { | 112 struct { |
179 sk_sp<SkColorFilter> (*fFact)(); | 113 sk_sp<SkColorFilter> (*fFact)(); |
180 bool fSupports4f; | 114 bool fSupports4f; |
181 const char* fName; | 115 const char* fName; |
182 } recs[] = { | 116 } recs[] = { |
183 { make_mode_cf, true, "mode" }, | 117 { make_mode_cf, true, "mode" }, |
184 { make_mx_cf, true, "matrix" }, | 118 { make_mx_cf, true, "matrix" }, |
185 { make_compose_cf, true, "compose" }, | 119 { make_compose_cf, true, "compose" }, |
186 }; | 120 }; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 // | 175 // |
242 DEF_TEST(Color4f_xfermode_proc4f, reporter) { | 176 DEF_TEST(Color4f_xfermode_proc4f, reporter) { |
243 // TODO: extend xfermodes so that all cases can be tested. | 177 // TODO: extend xfermodes so that all cases can be tested. |
244 // | 178 // |
245 for (int mode = SkXfermode::kClear_Mode; mode <= SkXfermode::kScreen_Mode; +
+mode) { | 179 for (int mode = SkXfermode::kClear_Mode; mode <= SkXfermode::kScreen_Mode; +
+mode) { |
246 SkXfermodeProc proc32 = SkXfermode::GetProc((SkXfermode::Mode)mode); | 180 SkXfermodeProc proc32 = SkXfermode::GetProc((SkXfermode::Mode)mode); |
247 SkXfermodeProc4f proc4f = SkXfermode::GetProc4f((SkXfermode::Mode)mode); | 181 SkXfermodeProc4f proc4f = SkXfermode::GetProc4f((SkXfermode::Mode)mode); |
248 REPORTER_ASSERT(reporter, compare_procs(proc32, proc4f)); | 182 REPORTER_ASSERT(reporter, compare_procs(proc32, proc4f)); |
249 } | 183 } |
250 } | 184 } |
OLD | NEW |