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

Side by Side Diff: src/core/SkXfermode4f.cpp

Issue 2163683002: Correct sRGB <-> linear everywhere. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: back to brute Created 4 years, 5 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/core/SkSpanProcs.cpp ('k') | src/effects/gradients/Sk4fGradientPriv.h » ('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 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 "SkPM4fPriv.h" 8 #include "SkPM4fPriv.h"
9 #include "SkUtils.h" 9 #include "SkUtils.h"
10 #include "SkXfermode.h" 10 #include "SkXfermode.h"
(...skipping 17 matching lines...) Expand all
28 } 28 }
29 29
30 static Sk4f lerp(const Sk4f& src, const Sk4f& dst, uint8_t srcCoverage) { 30 static Sk4f lerp(const Sk4f& src, const Sk4f& dst, uint8_t srcCoverage) {
31 return dst + (src - dst) * Sk4f(srcCoverage * (1/255.0f)); 31 return dst + (src - dst) * Sk4f(srcCoverage * (1/255.0f));
32 } 32 }
33 33
34 template <DstType D> Sk4f load_dst(SkPMColor dstC) { 34 template <DstType D> Sk4f load_dst(SkPMColor dstC) {
35 return (D == kSRGB_Dst) ? Sk4f_fromS32(dstC) : Sk4f_fromL32(dstC); 35 return (D == kSRGB_Dst) ? Sk4f_fromS32(dstC) : Sk4f_fromL32(dstC);
36 } 36 }
37 37
38 static Sk4f srgb_4b_to_linear_unit(SkPMColor dstC) {
39 return Sk4f_fromS32(dstC);
40 }
41
42 template <DstType D> uint32_t store_dst(const Sk4f& x4) { 38 template <DstType D> uint32_t store_dst(const Sk4f& x4) {
43 return (D == kSRGB_Dst) ? Sk4f_toS32(x4) : Sk4f_toL32(x4); 39 return (D == kSRGB_Dst) ? Sk4f_toS32(x4) : Sk4f_toL32(x4);
44 } 40 }
45 41
46 static Sk4f linear_unit_to_srgb_255f(const Sk4f& l4) { 42 static Sk4x4f load_4_srgb(const void* vptr) {
47 return linear_to_srgb(l4) * Sk4f(255) + Sk4f(0.5f); 43 auto ptr = (const uint32_t*)vptr;
44
45 Sk4x4f rgba;
46
47 rgba.r = { sk_linear_from_srgb[(ptr[0] >> 0) & 0xff],
48 sk_linear_from_srgb[(ptr[1] >> 0) & 0xff],
49 sk_linear_from_srgb[(ptr[2] >> 0) & 0xff],
50 sk_linear_from_srgb[(ptr[3] >> 0) & 0xff] };
51
52 rgba.g = { sk_linear_from_srgb[(ptr[0] >> 8) & 0xff],
53 sk_linear_from_srgb[(ptr[1] >> 8) & 0xff],
54 sk_linear_from_srgb[(ptr[2] >> 8) & 0xff],
55 sk_linear_from_srgb[(ptr[3] >> 8) & 0xff] };
56
57 rgba.b = { sk_linear_from_srgb[(ptr[0] >> 16) & 0xff],
58 sk_linear_from_srgb[(ptr[1] >> 16) & 0xff],
59 sk_linear_from_srgb[(ptr[2] >> 16) & 0xff],
60 sk_linear_from_srgb[(ptr[3] >> 16) & 0xff] };
61
62 rgba.a = SkNx_cast<float>((Sk4i::Load(ptr) >> 24) & 0xff) * (1/255.0f);
63
64 return rgba;
48 } 65 }
49 66
50 // Load 4 interlaced 8888 sRGB pixels as an Sk4x4f, transposed and converted to float.
51 static Sk4x4f load_4_srgb(const void* ptr) {
52 auto p = Sk4x4f::Transpose((const uint8_t*)ptr);
53
54 // Scale to [0,1].
55 p.r *= 1/255.0f;
56 p.g *= 1/255.0f;
57 p.b *= 1/255.0f;
58 p.a *= 1/255.0f;
59
60 // Apply approximate sRGB gamma correction to convert to linear (as if gamma were 2).
61 p.r *= p.r;
62 p.g *= p.g;
63 p.b *= p.b;
64
65 return p;
66 }
67
68 // Store an Sk4x4f back to 4 interlaced 8888 sRGB pixels.
69 static void store_4_srgb(void* ptr, const Sk4x4f& p) { 67 static void store_4_srgb(void* ptr, const Sk4x4f& p) {
70 // Convert back to sRGB and [0,255], again approximating sRGB as gamma == 2. 68 ( sk_linear_to_srgb(p.r) << 0
71 auto r = p.r.rsqrt().invert() * 255.0f + 0.5f, 69 | sk_linear_to_srgb(p.g) << 8
72 g = p.g.rsqrt().invert() * 255.0f + 0.5f, 70 | sk_linear_to_srgb(p.b) << 16
73 b = p.b.rsqrt().invert() * 255.0f + 0.5f, 71 | Sk4f_round(255.0f*p.a) << 24).store(ptr);
74 a = p.a * 255.0f + 0.5f;
75 Sk4x4f{r,g,b,a}.transpose((uint8_t*)ptr);
76 } 72 }
77 73
78 //////////////////////////////////////////////////////////////////////////////// /////////////////// 74 //////////////////////////////////////////////////////////////////////////////// ///////////////////
79 75
80 template <DstType D> void general_1(const SkXfermode* xfer, uint32_t dst[], 76 template <DstType D> void general_1(const SkXfermode* xfer, uint32_t dst[],
81 const SkPM4f* src, int count, const SkAlpha aa[]) { 77 const SkPM4f* src, int count, const SkAlpha aa[]) {
82 const SkPM4f s = rgba_to_pmcolor_order(*src); 78 const SkPM4f s = rgba_to_pmcolor_order(*src);
83 SkXfermodeProc4f proc = xfer->getProc4f(); 79 SkXfermodeProc4f proc = xfer->getProc4f();
84 SkPM4f d; 80 SkPM4f d;
85 if (aa) { 81 if (aa) {
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 186
191 static Sk4f lerp(const Sk4f& src, const Sk4f& dst, const Sk4f& src_scale) { 187 static Sk4f lerp(const Sk4f& src, const Sk4f& dst, const Sk4f& src_scale) {
192 return dst + (src - dst) * src_scale; 188 return dst + (src - dst) * src_scale;
193 } 189 }
194 190
195 template <DstType D> void src_1(const SkXfermode*, uint32_t dst[], 191 template <DstType D> void src_1(const SkXfermode*, uint32_t dst[],
196 const SkPM4f* src, int count, const SkAlpha aa[] ) { 192 const SkPM4f* src, int count, const SkAlpha aa[] ) {
197 const Sk4f s4 = src->to4f_pmorder(); 193 const Sk4f s4 = src->to4f_pmorder();
198 194
199 if (aa) { 195 if (aa) {
200 if (D == kLinear_Dst) { 196 SkPMColor srcColor = store_dst<D>(s4);
201 // operate in bias-255 space for src and dst 197 while (count-- > 0) {
202 const Sk4f& s4_255 = s4 * Sk4f(255); 198 SkAlpha cover = *aa++;
203 while (count >= 4) { 199 switch (cover) {
204 Sk4f aa4 = SkNx_cast<float>(Sk4b::Load(aa)) * Sk4f(1/255.f); 200 case 0xFF: {
205 Sk4f r0 = lerp(s4_255, to_4f(dst[0]), Sk4f(aa4[0])) + Sk4f(0.5f) ; 201 *dst++ = srcColor;
206 Sk4f r1 = lerp(s4_255, to_4f(dst[1]), Sk4f(aa4[1])) + Sk4f(0.5f) ; 202 break;
207 Sk4f r2 = lerp(s4_255, to_4f(dst[2]), Sk4f(aa4[2])) + Sk4f(0.5f) ; 203 }
208 Sk4f r3 = lerp(s4_255, to_4f(dst[3]), Sk4f(aa4[3])) + Sk4f(0.5f) ; 204 case 0x00: {
209 Sk4f_ToBytes((uint8_t*)dst, r0, r1, r2, r3); 205 dst++;
210 206 break;
211 dst += 4; 207 }
212 aa += 4; 208 default: {
213 count -= 4; 209 Sk4f d4 = load_dst<D>(*dst);
214 } 210 *dst++ = store_dst<D>(lerp(s4, d4, cover));
215 } else { // kSRGB
216 SkPMColor srcColor = store_dst<D>(s4);
217 while (count-- > 0) {
218 SkAlpha cover = *aa++;
219 switch (cover) {
220 case 0xFF: {
221 *dst++ = srcColor;
222 break;
223 }
224 case 0x00: {
225 dst++;
226 break;
227 }
228 default: {
229 Sk4f d4 = load_dst<D>(*dst);
230 *dst++ = store_dst<D>(lerp(s4, d4, cover));
231 }
232 } 211 }
233 } 212 }
234 } // kSRGB 213 }
235 } else { 214 } else {
236 sk_memset32(dst, store_dst<D>(s4), count); 215 sk_memset32(dst, store_dst<D>(s4), count);
237 } 216 }
238 } 217 }
239 218
240 const SkXfermode::D32Proc gProcs_Src[] = { 219 const SkXfermode::D32Proc gProcs_Src[] = {
241 src_n<kLinear_Dst>, src_n<kLinear_Dst>, 220 src_n<kLinear_Dst>, src_n<kLinear_Dst>,
242 src_1<kLinear_Dst>, src_1<kLinear_Dst>, 221 src_1<kLinear_Dst>, src_1<kLinear_Dst>,
243 src_n<kSRGB_Dst>, src_n<kSRGB_Dst>, 222 src_n<kSRGB_Dst>, src_n<kSRGB_Dst>,
244 src_1<kSRGB_Dst>, src_1<kSRGB_Dst>, 223 src_1<kSRGB_Dst>, src_1<kSRGB_Dst>,
(...skipping 22 matching lines...) Expand all
267 Sk4f d4 = load_dst<D>(dst[i]); 246 Sk4f d4 = load_dst<D>(dst[i]);
268 if (a != 0xFF) { 247 if (a != 0xFF) {
269 s4 = scale_by_coverage(s4, a); 248 s4 = scale_by_coverage(s4, a);
270 } 249 }
271 Sk4f r4 = s4 + d4 * Sk4f(1 - get_alpha(s4)); 250 Sk4f r4 = s4 + d4 * Sk4f(1 - get_alpha(s4));
272 dst[i] = store_dst<D>(r4); 251 dst[i] = store_dst<D>(r4);
273 } 252 }
274 } else { 253 } else {
275 while (count >= 4 && D == kSRGB_Dst) { 254 while (count >= 4 && D == kSRGB_Dst) {
276 auto d = load_4_srgb(dst); 255 auto d = load_4_srgb(dst);
277
278 auto s = Sk4x4f::Transpose(src->fVec); 256 auto s = Sk4x4f::Transpose(src->fVec);
279 #if defined(SK_PMCOLOR_IS_BGRA) 257 #if defined(SK_PMCOLOR_IS_BGRA)
280 SkTSwap(s.r, s.b); 258 SkTSwap(s.r, s.b);
281 #endif 259 #endif
282
283 auto invSA = 1.0f - s.a; 260 auto invSA = 1.0f - s.a;
284 auto r = s.r + d.r * invSA, 261 auto r = s.r + d.r * invSA,
285 g = s.g + d.g * invSA, 262 g = s.g + d.g * invSA,
286 b = s.b + d.b * invSA, 263 b = s.b + d.b * invSA,
287 a = s.a + d.a * invSA; 264 a = s.a + d.a * invSA;
288
289 store_4_srgb(dst, Sk4x4f{r,g,b,a}); 265 store_4_srgb(dst, Sk4x4f{r,g,b,a});
290 count -= 4; 266 count -= 4;
291 dst += 4; 267 dst += 4;
292 src += 4; 268 src += 4;
293 } 269 }
294 for (int i = 0; i < count; ++i) { 270 for (int i = 0; i < count; ++i) {
295 Sk4f s4 = src[i].to4f_pmorder(); 271 Sk4f s4 = src[i].to4f_pmorder();
296 Sk4f d4 = load_dst<D>(dst[i]); 272 Sk4f d4 = load_dst<D>(dst[i]);
297 Sk4f r4 = s4 + d4 * Sk4f(1 - get_alpha(s4)); 273 Sk4f r4 = s4 + d4 * Sk4f(1 - get_alpha(s4));
298 dst[i] = store_dst<D>(r4); 274 dst[i] = store_dst<D>(r4);
(...skipping 16 matching lines...) Expand all
315 Sk4f r4; 291 Sk4f r4;
316 if (a != 0xFF) { 292 if (a != 0xFF) {
317 Sk4f s4_aa = scale_by_coverage(s4, a); 293 Sk4f s4_aa = scale_by_coverage(s4, a);
318 r4 = s4_aa + d4 * Sk4f(1 - get_alpha(s4_aa)); 294 r4 = s4_aa + d4 * Sk4f(1 - get_alpha(s4_aa));
319 } else { 295 } else {
320 r4 = s4 + d4 * dst_scale; 296 r4 = s4 + d4 * dst_scale;
321 } 297 }
322 dst[i] = Sk4f_toL32(r4); 298 dst[i] = Sk4f_toL32(r4);
323 } 299 }
324 } else { 300 } else {
325 const Sk4f s4_255 = s4 * Sk4f(255) + Sk4f(0.5f); // +0.5 to pre-bias f or rounding
326 while (count >= 4) {
327 Sk4f d0 = to_4f(dst[0]);
328 Sk4f d1 = to_4f(dst[1]);
329 Sk4f d2 = to_4f(dst[2]);
330 Sk4f d3 = to_4f(dst[3]);
331 Sk4f_ToBytes((uint8_t*)dst,
332 s4_255 + d0 * dst_scale,
333 s4_255 + d1 * dst_scale,
334 s4_255 + d2 * dst_scale,
335 s4_255 + d3 * dst_scale);
336 dst += 4;
337 count -= 4;
338 }
339 for (int i = 0; i < count; ++i) { 301 for (int i = 0; i < count; ++i) {
340 Sk4f d4 = to_4f(dst[i]); 302 Sk4f d4 = Sk4f_fromL32(dst[i]);
341 dst[i] = to_4b(s4_255 + d4 * dst_scale); 303 dst[i] = Sk4f_toL32(s4 + d4 * dst_scale);
342 } 304 }
343 } 305 }
344 } 306 }
345 307
346 static void srcover_srgb_dst_1(const SkXfermode*, uint32_t dst[], 308 static void srcover_srgb_dst_1(const SkXfermode*, uint32_t dst[],
347 const SkPM4f* src, int count, const SkAlpha aa[]) { 309 const SkPM4f* src, int count, const SkAlpha aa[]) {
348 Sk4f s4 = src->to4f_pmorder(); 310 Sk4f s4 = src->to4f_pmorder();
349 Sk4f dst_scale = Sk4f(1 - get_alpha(s4)); 311 Sk4f dst_scale = Sk4f(1 - get_alpha(s4));
350 312
351 if (aa) { 313 if (aa) {
352 for (int i = 0; i < count; ++i) { 314 for (int i = 0; i < count; ++i) {
353 unsigned a = aa[i]; 315 unsigned a = aa[i];
354 if (0 == a) { 316 if (0 == a) {
355 continue; 317 continue;
356 } 318 }
357 Sk4f d4 = srgb_4b_to_linear_unit(dst[i]); 319
320 Sk4f d4 = Sk4f_fromS32(dst[i]);
358 Sk4f r4; 321 Sk4f r4;
359 if (a != 0xFF) { 322 if (a != 0xFF) {
360 const Sk4f s4_aa = scale_by_coverage(s4, a); 323 const Sk4f s4_aa = scale_by_coverage(s4, a);
361 r4 = s4_aa + d4 * Sk4f(1 - get_alpha(s4_aa)); 324 r4 = s4_aa + d4 * Sk4f(1 - get_alpha(s4_aa));
362 } else { 325 } else {
363 r4 = s4 + d4 * dst_scale; 326 r4 = s4 + d4 * dst_scale;
364 } 327 }
365 dst[i] = to_4b(linear_unit_to_srgb_255f(r4)); 328 dst[i] = Sk4f_toS32(r4);
366 } 329 }
367 } else { 330 } else {
368 while (count >= 4) { 331 while (count >= 4) {
369 auto d = load_4_srgb(dst); 332 auto d = load_4_srgb(dst);
370
371 auto s = Sk4x4f{{ src->r() }, { src->g() }, { src->b() }, { src->a() }}; 333 auto s = Sk4x4f{{ src->r() }, { src->g() }, { src->b() }, { src->a() }};
372 #if defined(SK_PMCOLOR_IS_BGRA) 334 #if defined(SK_PMCOLOR_IS_BGRA)
373 SkTSwap(s.r, s.b); 335 SkTSwap(s.r, s.b);
374 #endif 336 #endif
375
376 auto invSA = 1.0f - s.a; 337 auto invSA = 1.0f - s.a;
377 auto r = s.r + d.r * invSA, 338 auto r = s.r + d.r * invSA,
378 g = s.g + d.g * invSA, 339 g = s.g + d.g * invSA,
379 b = s.b + d.b * invSA, 340 b = s.b + d.b * invSA,
380 a = s.a + d.a * invSA; 341 a = s.a + d.a * invSA;
381
382 store_4_srgb(dst, Sk4x4f{r,g,b,a}); 342 store_4_srgb(dst, Sk4x4f{r,g,b,a});
383 count -= 4; 343 count -= 4;
384 dst += 4; 344 dst += 4;
385 } 345 }
386 for (int i = 0; i < count; ++i) { 346 for (int i = 0; i < count; ++i) {
387 Sk4f d4 = srgb_4b_to_linear_unit(dst[i]); 347 Sk4f d4 = Sk4f_fromS32(dst[i]);
388 dst[i] = to_4b(linear_unit_to_srgb_255f(s4 + d4 * dst_scale)); 348 dst[i] = Sk4f_toS32(s4 + d4 * dst_scale);
389 } 349 }
390 } 350 }
391 } 351 }
392 352
393 const SkXfermode::D32Proc gProcs_SrcOver[] = { 353 const SkXfermode::D32Proc gProcs_SrcOver[] = {
394 srcover_n<kLinear_Dst>, src_n<kLinear_Dst>, 354 srcover_n<kLinear_Dst>, src_n<kLinear_Dst>,
395 srcover_linear_dst_1, src_1<kLinear_Dst>, 355 srcover_linear_dst_1, src_1<kLinear_Dst>,
396 356
397 srcover_n<kSRGB_Dst>, src_n<kSRGB_Dst>, 357 srcover_n<kSRGB_Dst>, src_n<kSRGB_Dst>,
398 srcover_srgb_dst_1, src_1<kSRGB_Dst>, 358 srcover_srgb_dst_1, src_1<kSRGB_Dst>,
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
436 #else 396 #else
437 Sk4i rgbi = Sk4i(SkGetPackedB16(rgb), SkGetPackedG16(rgb), SkGetPackedR16(rg b), 0); 397 Sk4i rgbi = Sk4i(SkGetPackedB16(rgb), SkGetPackedG16(rgb), SkGetPackedR16(rg b), 0);
438 #endif 398 #endif
439 return SkNx_cast<float>(rgbi) * Sk4f(1.0f/31, 1.0f/63, 1.0f/31, 0); 399 return SkNx_cast<float>(rgbi) * Sk4f(1.0f/31, 1.0f/63, 1.0f/31, 0);
440 } 400 }
441 401
442 template <DstType D> 402 template <DstType D>
443 void src_1_lcd(uint32_t dst[], const SkPM4f* src, int count, const uint16_t lcd[ ]) { 403 void src_1_lcd(uint32_t dst[], const SkPM4f* src, int count, const uint16_t lcd[ ]) {
444 const Sk4f s4 = src->to4f_pmorder(); 404 const Sk4f s4 = src->to4f_pmorder();
445 405
446 if (D == kLinear_Dst) { 406 for (int i = 0; i < count; ++i) {
447 // operate in bias-255 space for src and dst 407 uint16_t rgb = lcd[i];
448 const Sk4f s4bias = s4 * Sk4f(255); 408 if (0 == rgb) {
449 for (int i = 0; i < count; ++i) { 409 continue;
450 uint16_t rgb = lcd[i];
451 if (0 == rgb) {
452 continue;
453 }
454 Sk4f d4bias = to_4f(dst[i]);
455 dst[i] = to_4b(lerp(s4bias, d4bias, lcd16_to_unit_4f(rgb))) | (SK_A3 2_MASK << SK_A32_SHIFT);
456 } 410 }
457 } else { // kSRGB 411 Sk4f d4 = load_dst<D>(dst[i]);
458 for (int i = 0; i < count; ++i) { 412 dst[i] = store_dst<D>(lerp(s4, d4, lcd16_to_unit_4f(rgb))) | (SK_A32_MAS K << SK_A32_SHIFT);
459 uint16_t rgb = lcd[i];
460 if (0 == rgb) {
461 continue;
462 }
463 Sk4f d4 = load_dst<D>(dst[i]);
464 dst[i] = store_dst<D>(lerp(s4, d4, lcd16_to_unit_4f(rgb))) | (SK_A32 _MASK << SK_A32_SHIFT);
465 }
466 } 413 }
467 } 414 }
468 415
469 template <DstType D> 416 template <DstType D>
470 void src_n_lcd(uint32_t dst[], const SkPM4f src[], int count, const uint16_t lcd []) { 417 void src_n_lcd(uint32_t dst[], const SkPM4f src[], int count, const uint16_t lcd []) {
471 for (int i = 0; i < count; ++i) { 418 for (int i = 0; i < count; ++i) {
472 uint16_t rgb = lcd[i]; 419 uint16_t rgb = lcd[i];
473 if (0 == rgb) { 420 if (0 == rgb) {
474 continue; 421 continue;
475 } 422 }
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
518 465
519 const LCD32Proc procs[] = { 466 const LCD32Proc procs[] = {
520 srcover_n_lcd<kSRGB_Dst>, src_n_lcd<kSRGB_Dst>, 467 srcover_n_lcd<kSRGB_Dst>, src_n_lcd<kSRGB_Dst>,
521 srcover_1_lcd<kSRGB_Dst>, src_1_lcd<kSRGB_Dst>, 468 srcover_1_lcd<kSRGB_Dst>, src_1_lcd<kSRGB_Dst>,
522 469
523 srcover_n_lcd<kLinear_Dst>, src_n_lcd<kLinear_Dst>, 470 srcover_n_lcd<kLinear_Dst>, src_n_lcd<kLinear_Dst>,
524 srcover_1_lcd<kLinear_Dst>, src_1_lcd<kLinear_Dst>, 471 srcover_1_lcd<kLinear_Dst>, src_1_lcd<kLinear_Dst>,
525 }; 472 };
526 return procs[flags]; 473 return procs[flags];
527 } 474 }
OLDNEW
« no previous file with comments | « src/core/SkSpanProcs.cpp ('k') | src/effects/gradients/Sk4fGradientPriv.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698