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

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: Fix! 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
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. 67 static void store_4_srgb(void* ptr, const Sk4x4f& p) {
51 static Sk4x4f load_4_srgb(const void* ptr) { 68 auto r = sk_linear_to_srgb(p.r),
52 auto p = Sk4x4f::Transpose((const uint8_t*)ptr); 69 g = sk_linear_to_srgb(p.g),
70 b = sk_linear_to_srgb(p.b),
71 a = 255.0f * p.a ;
53 72
54 // Scale to [0,1]. 73 ( Sk4f_round(r) << 0
55 p.r *= 1/255.0f; 74 | Sk4f_round(g) << 8
56 p.g *= 1/255.0f; 75 | Sk4f_round(b) << 16
57 p.b *= 1/255.0f; 76 | Sk4f_round(a) << 24).store(ptr);
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) {
70 // Convert back to sRGB and [0,255], again approximating sRGB as gamma == 2.
71 auto r = p.r.rsqrt().invert() * 255.0f + 0.5f,
72 g = p.g.rsqrt().invert() * 255.0f + 0.5f,
73 b = p.b.rsqrt().invert() * 255.0f + 0.5f,
74 a = p.a * 255.0f + 0.5f;
75 Sk4x4f{r,g,b,a}.transpose((uint8_t*)ptr);
76 } 77 }
77 78
78 //////////////////////////////////////////////////////////////////////////////// /////////////////// 79 //////////////////////////////////////////////////////////////////////////////// ///////////////////
79 80
80 template <DstType D> void general_1(const SkXfermode* xfer, uint32_t dst[], 81 template <DstType D> void general_1(const SkXfermode* xfer, uint32_t dst[],
81 const SkPM4f* src, int count, const SkAlpha aa[]) { 82 const SkPM4f* src, int count, const SkAlpha aa[]) {
82 const SkPM4f s = rgba_to_pmcolor_order(*src); 83 const SkPM4f s = rgba_to_pmcolor_order(*src);
83 SkXfermodeProc4f proc = xfer->getProc4f(); 84 SkXfermodeProc4f proc = xfer->getProc4f();
84 SkPM4f d; 85 SkPM4f d;
85 if (aa) { 86 if (aa) {
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 191
191 static Sk4f lerp(const Sk4f& src, const Sk4f& dst, const Sk4f& src_scale) { 192 static Sk4f lerp(const Sk4f& src, const Sk4f& dst, const Sk4f& src_scale) {
192 return dst + (src - dst) * src_scale; 193 return dst + (src - dst) * src_scale;
193 } 194 }
194 195
195 template <DstType D> void src_1(const SkXfermode*, uint32_t dst[], 196 template <DstType D> void src_1(const SkXfermode*, uint32_t dst[],
196 const SkPM4f* src, int count, const SkAlpha aa[] ) { 197 const SkPM4f* src, int count, const SkAlpha aa[] ) {
197 const Sk4f s4 = src->to4f_pmorder(); 198 const Sk4f s4 = src->to4f_pmorder();
198 199
199 if (aa) { 200 if (aa) {
200 if (D == kLinear_Dst) { 201 SkPMColor srcColor = store_dst<D>(s4);
201 // operate in bias-255 space for src and dst 202 while (count-- > 0) {
202 const Sk4f& s4_255 = s4 * Sk4f(255); 203 SkAlpha cover = *aa++;
203 while (count >= 4) { 204 switch (cover) {
204 Sk4f aa4 = SkNx_cast<float>(Sk4b::Load(aa)) * Sk4f(1/255.f); 205 case 0xFF: {
205 Sk4f r0 = lerp(s4_255, to_4f(dst[0]), Sk4f(aa4[0])) + Sk4f(0.5f) ; 206 *dst++ = srcColor;
206 Sk4f r1 = lerp(s4_255, to_4f(dst[1]), Sk4f(aa4[1])) + Sk4f(0.5f) ; 207 break;
207 Sk4f r2 = lerp(s4_255, to_4f(dst[2]), Sk4f(aa4[2])) + Sk4f(0.5f) ; 208 }
208 Sk4f r3 = lerp(s4_255, to_4f(dst[3]), Sk4f(aa4[3])) + Sk4f(0.5f) ; 209 case 0x00: {
209 Sk4f_ToBytes((uint8_t*)dst, r0, r1, r2, r3); 210 dst++;
210 211 break;
211 dst += 4; 212 }
212 aa += 4; 213 default: {
213 count -= 4; 214 Sk4f d4 = load_dst<D>(*dst);
214 } 215 *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 } 216 }
233 } 217 }
234 } // kSRGB 218 }
235 } else { 219 } else {
236 sk_memset32(dst, store_dst<D>(s4), count); 220 sk_memset32(dst, store_dst<D>(s4), count);
237 } 221 }
238 } 222 }
239 223
240 const SkXfermode::D32Proc gProcs_Src[] = { 224 const SkXfermode::D32Proc gProcs_Src[] = {
241 src_n<kLinear_Dst>, src_n<kLinear_Dst>, 225 src_n<kLinear_Dst>, src_n<kLinear_Dst>,
242 src_1<kLinear_Dst>, src_1<kLinear_Dst>, 226 src_1<kLinear_Dst>, src_1<kLinear_Dst>,
243 src_n<kSRGB_Dst>, src_n<kSRGB_Dst>, 227 src_n<kSRGB_Dst>, src_n<kSRGB_Dst>,
244 src_1<kSRGB_Dst>, src_1<kSRGB_Dst>, 228 src_1<kSRGB_Dst>, src_1<kSRGB_Dst>,
(...skipping 22 matching lines...) Expand all
267 Sk4f d4 = load_dst<D>(dst[i]); 251 Sk4f d4 = load_dst<D>(dst[i]);
268 if (a != 0xFF) { 252 if (a != 0xFF) {
269 s4 = scale_by_coverage(s4, a); 253 s4 = scale_by_coverage(s4, a);
270 } 254 }
271 Sk4f r4 = s4 + d4 * Sk4f(1 - get_alpha(s4)); 255 Sk4f r4 = s4 + d4 * Sk4f(1 - get_alpha(s4));
272 dst[i] = store_dst<D>(r4); 256 dst[i] = store_dst<D>(r4);
273 } 257 }
274 } else { 258 } else {
275 while (count >= 4 && D == kSRGB_Dst) { 259 while (count >= 4 && D == kSRGB_Dst) {
276 auto d = load_4_srgb(dst); 260 auto d = load_4_srgb(dst);
277
278 auto s = Sk4x4f::Transpose(src->fVec); 261 auto s = Sk4x4f::Transpose(src->fVec);
279 #if defined(SK_PMCOLOR_IS_BGRA) 262 #if defined(SK_PMCOLOR_IS_BGRA)
280 SkTSwap(s.r, s.b); 263 SkTSwap(s.r, s.b);
281 #endif 264 #endif
282
283 auto invSA = 1.0f - s.a; 265 auto invSA = 1.0f - s.a;
284 auto r = s.r + d.r * invSA, 266 auto r = s.r + d.r * invSA,
285 g = s.g + d.g * invSA, 267 g = s.g + d.g * invSA,
286 b = s.b + d.b * invSA, 268 b = s.b + d.b * invSA,
287 a = s.a + d.a * invSA; 269 a = s.a + d.a * invSA;
288
289 store_4_srgb(dst, Sk4x4f{r,g,b,a}); 270 store_4_srgb(dst, Sk4x4f{r,g,b,a});
290 count -= 4; 271 count -= 4;
291 dst += 4; 272 dst += 4;
292 src += 4; 273 src += 4;
293 } 274 }
294 for (int i = 0; i < count; ++i) { 275 for (int i = 0; i < count; ++i) {
295 Sk4f s4 = src[i].to4f_pmorder(); 276 Sk4f s4 = src[i].to4f_pmorder();
296 Sk4f d4 = load_dst<D>(dst[i]); 277 Sk4f d4 = load_dst<D>(dst[i]);
297 Sk4f r4 = s4 + d4 * Sk4f(1 - get_alpha(s4)); 278 Sk4f r4 = s4 + d4 * Sk4f(1 - get_alpha(s4));
298 dst[i] = store_dst<D>(r4); 279 dst[i] = store_dst<D>(r4);
(...skipping 16 matching lines...) Expand all
315 Sk4f r4; 296 Sk4f r4;
316 if (a != 0xFF) { 297 if (a != 0xFF) {
317 Sk4f s4_aa = scale_by_coverage(s4, a); 298 Sk4f s4_aa = scale_by_coverage(s4, a);
318 r4 = s4_aa + d4 * Sk4f(1 - get_alpha(s4_aa)); 299 r4 = s4_aa + d4 * Sk4f(1 - get_alpha(s4_aa));
319 } else { 300 } else {
320 r4 = s4 + d4 * dst_scale; 301 r4 = s4 + d4 * dst_scale;
321 } 302 }
322 dst[i] = Sk4f_toL32(r4); 303 dst[i] = Sk4f_toL32(r4);
323 } 304 }
324 } else { 305 } 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) { 306 for (int i = 0; i < count; ++i) {
340 Sk4f d4 = to_4f(dst[i]); 307 Sk4f d4 = Sk4f_fromL32(dst[i]);
341 dst[i] = to_4b(s4_255 + d4 * dst_scale); 308 dst[i] = Sk4f_toL32(s4 + d4 * dst_scale);
342 } 309 }
343 } 310 }
344 } 311 }
345 312
346 static void srcover_srgb_dst_1(const SkXfermode*, uint32_t dst[], 313 static void srcover_srgb_dst_1(const SkXfermode*, uint32_t dst[],
347 const SkPM4f* src, int count, const SkAlpha aa[]) { 314 const SkPM4f* src, int count, const SkAlpha aa[]) {
348 Sk4f s4 = src->to4f_pmorder(); 315 Sk4f s4 = src->to4f_pmorder();
349 Sk4f dst_scale = Sk4f(1 - get_alpha(s4)); 316 Sk4f dst_scale = Sk4f(1 - get_alpha(s4));
350 317
351 if (aa) { 318 if (aa) {
352 for (int i = 0; i < count; ++i) { 319 for (int i = 0; i < count; ++i) {
353 unsigned a = aa[i]; 320 unsigned a = aa[i];
354 if (0 == a) { 321 if (0 == a) {
355 continue; 322 continue;
356 } 323 }
357 Sk4f d4 = srgb_4b_to_linear_unit(dst[i]); 324
325 Sk4f d4 = Sk4f_fromS32(dst[i]);
358 Sk4f r4; 326 Sk4f r4;
359 if (a != 0xFF) { 327 if (a != 0xFF) {
360 const Sk4f s4_aa = scale_by_coverage(s4, a); 328 const Sk4f s4_aa = scale_by_coverage(s4, a);
361 r4 = s4_aa + d4 * Sk4f(1 - get_alpha(s4_aa)); 329 r4 = s4_aa + d4 * Sk4f(1 - get_alpha(s4_aa));
362 } else { 330 } else {
363 r4 = s4 + d4 * dst_scale; 331 r4 = s4 + d4 * dst_scale;
364 } 332 }
365 dst[i] = to_4b(linear_unit_to_srgb_255f(r4)); 333 dst[i] = Sk4f_toS32(r4);
366 } 334 }
367 } else { 335 } else {
368 while (count >= 4) { 336 while (count >= 4) {
369 auto d = load_4_srgb(dst); 337 auto d = load_4_srgb(dst);
370
371 auto s = Sk4x4f{{ src->r() }, { src->g() }, { src->b() }, { src->a() }}; 338 auto s = Sk4x4f{{ src->r() }, { src->g() }, { src->b() }, { src->a() }};
372 #if defined(SK_PMCOLOR_IS_BGRA) 339 #if defined(SK_PMCOLOR_IS_BGRA)
373 SkTSwap(s.r, s.b); 340 SkTSwap(s.r, s.b);
374 #endif 341 #endif
375
376 auto invSA = 1.0f - s.a; 342 auto invSA = 1.0f - s.a;
377 auto r = s.r + d.r * invSA, 343 auto r = s.r + d.r * invSA,
378 g = s.g + d.g * invSA, 344 g = s.g + d.g * invSA,
379 b = s.b + d.b * invSA, 345 b = s.b + d.b * invSA,
380 a = s.a + d.a * invSA; 346 a = s.a + d.a * invSA;
381
382 store_4_srgb(dst, Sk4x4f{r,g,b,a}); 347 store_4_srgb(dst, Sk4x4f{r,g,b,a});
383 count -= 4; 348 count -= 4;
384 dst += 4; 349 dst += 4;
385 } 350 }
386 for (int i = 0; i < count; ++i) { 351 for (int i = 0; i < count; ++i) {
387 Sk4f d4 = srgb_4b_to_linear_unit(dst[i]); 352 Sk4f d4 = Sk4f_fromS32(dst[i]);
388 dst[i] = to_4b(linear_unit_to_srgb_255f(s4 + d4 * dst_scale)); 353 dst[i] = Sk4f_toS32(s4 + d4 * dst_scale);
389 } 354 }
390 } 355 }
391 } 356 }
392 357
393 const SkXfermode::D32Proc gProcs_SrcOver[] = { 358 const SkXfermode::D32Proc gProcs_SrcOver[] = {
394 srcover_n<kLinear_Dst>, src_n<kLinear_Dst>, 359 srcover_n<kLinear_Dst>, src_n<kLinear_Dst>,
395 srcover_linear_dst_1, src_1<kLinear_Dst>, 360 srcover_linear_dst_1, src_1<kLinear_Dst>,
396 361
397 srcover_n<kSRGB_Dst>, src_n<kSRGB_Dst>, 362 srcover_n<kSRGB_Dst>, src_n<kSRGB_Dst>,
398 srcover_srgb_dst_1, src_1<kSRGB_Dst>, 363 srcover_srgb_dst_1, src_1<kSRGB_Dst>,
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
436 #else 401 #else
437 Sk4i rgbi = Sk4i(SkGetPackedB16(rgb), SkGetPackedG16(rgb), SkGetPackedR16(rg b), 0); 402 Sk4i rgbi = Sk4i(SkGetPackedB16(rgb), SkGetPackedG16(rgb), SkGetPackedR16(rg b), 0);
438 #endif 403 #endif
439 return SkNx_cast<float>(rgbi) * Sk4f(1.0f/31, 1.0f/63, 1.0f/31, 0); 404 return SkNx_cast<float>(rgbi) * Sk4f(1.0f/31, 1.0f/63, 1.0f/31, 0);
440 } 405 }
441 406
442 template <DstType D> 407 template <DstType D>
443 void src_1_lcd(uint32_t dst[], const SkPM4f* src, int count, const uint16_t lcd[ ]) { 408 void src_1_lcd(uint32_t dst[], const SkPM4f* src, int count, const uint16_t lcd[ ]) {
444 const Sk4f s4 = src->to4f_pmorder(); 409 const Sk4f s4 = src->to4f_pmorder();
445 410
446 if (D == kLinear_Dst) { 411 for (int i = 0; i < count; ++i) {
447 // operate in bias-255 space for src and dst 412 uint16_t rgb = lcd[i];
448 const Sk4f s4bias = s4 * Sk4f(255); 413 if (0 == rgb) {
449 for (int i = 0; i < count; ++i) { 414 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 } 415 }
457 } else { // kSRGB 416 Sk4f d4 = load_dst<D>(dst[i]);
458 for (int i = 0; i < count; ++i) { 417 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 } 418 }
467 } 419 }
468 420
469 template <DstType D> 421 template <DstType D>
470 void src_n_lcd(uint32_t dst[], const SkPM4f src[], int count, const uint16_t lcd []) { 422 void src_n_lcd(uint32_t dst[], const SkPM4f src[], int count, const uint16_t lcd []) {
471 for (int i = 0; i < count; ++i) { 423 for (int i = 0; i < count; ++i) {
472 uint16_t rgb = lcd[i]; 424 uint16_t rgb = lcd[i];
473 if (0 == rgb) { 425 if (0 == rgb) {
474 continue; 426 continue;
475 } 427 }
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
518 470
519 const LCD32Proc procs[] = { 471 const LCD32Proc procs[] = {
520 srcover_n_lcd<kSRGB_Dst>, src_n_lcd<kSRGB_Dst>, 472 srcover_n_lcd<kSRGB_Dst>, src_n_lcd<kSRGB_Dst>,
521 srcover_1_lcd<kSRGB_Dst>, src_1_lcd<kSRGB_Dst>, 473 srcover_1_lcd<kSRGB_Dst>, src_1_lcd<kSRGB_Dst>,
522 474
523 srcover_n_lcd<kLinear_Dst>, src_n_lcd<kLinear_Dst>, 475 srcover_n_lcd<kLinear_Dst>, src_n_lcd<kLinear_Dst>,
524 srcover_1_lcd<kLinear_Dst>, src_1_lcd<kLinear_Dst>, 476 srcover_1_lcd<kLinear_Dst>, src_1_lcd<kLinear_Dst>,
525 }; 477 };
526 return procs[flags]; 478 return procs[flags];
527 } 479 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698