aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/SkXfermode4f.cpp
blob: 1f6c6747bb825863f26f2d29287c3b36eba5c321 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/*
 * Copyright 2016 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkPM4fPriv.h"
#include "SkUtils.h"
#include "SkXfermode.h"

struct XferProcPair {
    SkXfermode::PM4fProc1 fP1;
    SkXfermode::PM4fProcN fPN;
};

enum DstType {
    kLinear_Dst,
    kSRGB_Dst,
};

static Sk4f scale_by_coverage(const Sk4f& x4, uint8_t coverage) {
    return x4 * Sk4f(coverage * (1/255.0f));
}

static Sk4f lerp(const Sk4f& src, const Sk4f& dst, uint8_t srcCoverage) {
    return dst + (src - dst) * Sk4f(srcCoverage * (1/255.0f));
}

template <DstType D> Sk4f load_dst(SkPMColor dstC) {
    return (D == kSRGB_Dst) ? Sk4f_fromS32(dstC) : Sk4f_fromL32(dstC);
}

static Sk4f srgb_4b_to_linear_unit(SkPMColor dstC) {
    return Sk4f_fromS32(dstC);
}

template <DstType D> uint32_t store_dst(const Sk4f& x4) {
    return (D == kSRGB_Dst) ? Sk4f_toS32(x4) : Sk4f_toL32(x4);
}

static Sk4f linear_unit_to_srgb_255f(const Sk4f& l4) {
    return linear_to_srgb(l4) * Sk4f(255) + Sk4f(0.5f);
}

///////////////////////////////////////////////////////////////////////////////////////////////////

static Sk4f scale_255_round(const SkPM4f& pm4) {
    return Sk4f::Load(pm4.fVec) * Sk4f(255) + Sk4f(0.5f);
}

static void pm4f_to_linear_32(SkPMColor dst[], const SkPM4f src[], int count) {
    while (count >= 4) {
        src[0].assertIsUnit();
        src[1].assertIsUnit();
        src[2].assertIsUnit();
        src[3].assertIsUnit();
        Sk4f_ToBytes((uint8_t*)dst,
                     scale_255_round(src[0]), scale_255_round(src[1]),
                     scale_255_round(src[2]), scale_255_round(src[3]));
        src += 4;
        dst += 4;
        count -= 4;
    }
    for (int i = 0; i < count; ++i) {
        src[i].assertIsUnit();
        SkNx_cast<uint8_t>(scale_255_round(src[i])).store((uint8_t*)&dst[i]);
    }
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// These are our fallback impl for the SkPM4f procs...
//
// They just convert the src color(s) into a linear SkPMColor value(s), and then
// call the existing virtual xfer32. This clear throws away data (converting floats to bytes)
// in the src, and ignores the sRGB flag, but should draw about the same as if the caller
// had passed in SkPMColor values directly.
//

void xfer_pm4_proc_1(const SkXfermode::PM4fState& state, uint32_t dst[], const SkPM4f& src,
                     int count, const SkAlpha aa[]) {
    uint32_t pm;
    pm4f_to_linear_32(&pm, &src, 1);

    const int N = 128;
    SkPMColor tmp[N];
    sk_memset32(tmp, pm, SkMin32(count, N));
    while (count > 0) {
        const int n = SkMin32(count, N);
        state.fXfer->xfer32(dst, tmp, n, aa);

        dst += n;
        if (aa) {
            aa += n;
        }
        count -= n;
    }
}

void xfer_pm4_proc_n(const SkXfermode::PM4fState& state, uint32_t dst[], const SkPM4f src[],
                     int count, const SkAlpha aa[]) {
    const int N = 128;
    SkPMColor tmp[N];
    while (count > 0) {
        const int n = SkMin32(count, N);
        pm4f_to_linear_32(tmp, src, n);
        state.fXfer->xfer32(dst, tmp, n, aa);

        src += n;
        dst += n;
        if (aa) {
            aa += n;
        }
        count -= n;
    }
}

///////////////////////////////////////////////////////////////////////////////////////////////////

static void clear_linear_n(const SkXfermode::PM4fState& state, uint32_t dst[], const SkPM4f[],
                           int count, const SkAlpha aa[]) {
    if (aa) {
        for (int i = 0; i < count; ++i) {
            unsigned a = aa[i];
            if (a) {
                SkPMColor dstC = dst[i];
                SkPMColor C = 0;
                if (0xFF != a) {
                    C = SkFourByteInterp(C, dstC, a);
                }
                dst[i] = C;
            }
        }
    } else {
        sk_bzero(dst, count * sizeof(SkPMColor));
    }
}

static void clear_linear_1(const SkXfermode::PM4fState& state, uint32_t dst[], const SkPM4f&,
                           int count, const SkAlpha coverage[]) {
    clear_linear_n(state, dst, nullptr, count, coverage);
}

static void clear_srgb_n(const SkXfermode::PM4fState& state, uint32_t dst[], const SkPM4f[],
                           int count, const SkAlpha aa[]) {
    if (aa) {
        for (int i = 0; i < count; ++i) {
            unsigned a = aa[i];
            if (a) {
                Sk4f d = Sk4f_fromS32(dst[i]) * Sk4f((255 - a) * (1/255.0f));
                dst[i] = Sk4f_toS32(d);
            }
        }
    } else {
        sk_bzero(dst, count * sizeof(SkPMColor));
    }
}

static void clear_srgb_1(const SkXfermode::PM4fState& state, uint32_t dst[], const SkPM4f&,
                           int count, const SkAlpha coverage[]) {
    clear_srgb_n(state, dst, nullptr, count, coverage);
}

const XferProcPair gProcs_Clear[] = {
    { clear_linear_1, clear_linear_n },       // linear   [alpha]
    { clear_linear_1, clear_linear_n },       // linear   [opaque]
    { clear_srgb_1,   clear_srgb_n   },       // srgb     [alpha]
    { clear_srgb_1,   clear_srgb_n   },       // srgb     [opaque]
};

///////////////////////////////////////////////////////////////////////////////////////////////////

template <DstType D> void src_n(const SkXfermode::PM4fState& state, uint32_t dst[],
                                const SkPM4f src[], int count, const SkAlpha aa[]) {
    for (int i = 0; i < count; ++i) {
        unsigned a = 0xFF;
        if (aa) {
            a = aa[i];
            if (0 == a) {
                continue;
            }
        }
        Sk4f r4 = Sk4f::Load(src[i].fVec);   // src always overrides dst
        if (a != 0xFF) {
            Sk4f d4 = load_dst<D>(dst[i]);
            r4 = lerp(r4, d4, a);
        }
        dst[i] = store_dst<D>(r4);
    }
}

static Sk4f lerp(const Sk4f& src, const Sk4f& dst, const Sk4f& src_scale) {
    return dst + (src - dst) * src_scale;
}

template <DstType D> void src_1(const SkXfermode::PM4fState& state, uint32_t dst[],
                                const SkPM4f& src, int count, const SkAlpha aa[]) {
    const Sk4f s4 = Sk4f::Load(src.fVec);

    if (aa) {
        if (D == kLinear_Dst) {
            // operate in bias-255 space for src and dst
            const Sk4f& s4_255 = s4 * Sk4f(255);
            while (count >= 4) {
                Sk4f aa4 = SkNx_cast<float>(Sk4b::Load(aa)) * Sk4f(1/255.f);
                Sk4f r0 = lerp(s4_255, to_4f(dst[0]), Sk4f(aa4[0])) + Sk4f(0.5f);
                Sk4f r1 = lerp(s4_255, to_4f(dst[1]), Sk4f(aa4[1])) + Sk4f(0.5f);
                Sk4f r2 = lerp(s4_255, to_4f(dst[2]), Sk4f(aa4[2])) + Sk4f(0.5f);
                Sk4f r3 = lerp(s4_255, to_4f(dst[3]), Sk4f(aa4[3])) + Sk4f(0.5f);
                Sk4f_ToBytes((uint8_t*)dst, r0, r1, r2, r3);
                
                dst += 4;
                aa += 4;
                count -= 4;
            }
        } else {    // kSRGB
            while (count >= 4) {
                Sk4f aa4 = SkNx_cast<float>(Sk4b::Load(aa)) * Sk4f(1/255.0f);

                /*  If we ever natively support convert 255_linear -> 255_srgb, then perhaps
                 *  it would be faster (and possibly allow more code sharing with kLinear) to
                 *  stay in that space.
                 */
                Sk4f r0 = lerp(s4, load_dst<D>(dst[0]), Sk4f(aa4[0]));
                Sk4f r1 = lerp(s4, load_dst<D>(dst[1]), Sk4f(aa4[1]));
                Sk4f r2 = lerp(s4, load_dst<D>(dst[2]), Sk4f(aa4[2]));
                Sk4f r3 = lerp(s4, load_dst<D>(dst[3]), Sk4f(aa4[3]));
                Sk4f_ToBytes((uint8_t*)dst,
                             linear_unit_to_srgb_255f(r0),
                             linear_unit_to_srgb_255f(r1),
                             linear_unit_to_srgb_255f(r2),
                             linear_unit_to_srgb_255f(r3));
                
                dst += 4;
                aa += 4;
                count -= 4;
            }
        }
        for (int i = 0; i < count; ++i) {
            unsigned a = aa[i];
            Sk4f d4 = load_dst<D>(dst[i]);
            dst[i] = store_dst<D>(lerp(s4, d4, a));
        }
    } else {
        sk_memset32(dst, store_dst<D>(s4), count);
    }
}

const XferProcPair gProcs_Src[] = {
    { src_1<kLinear_Dst>, src_n<kLinear_Dst> },       // linear   [alpha]
    { src_1<kLinear_Dst>, src_n<kLinear_Dst> },       // linear   [opaque]
    { src_1<kSRGB_Dst>,   src_n<kSRGB_Dst>   },       // srgb     [alpha]
    { src_1<kSRGB_Dst>,   src_n<kSRGB_Dst>   },       // srgb     [opaque]
};

///////////////////////////////////////////////////////////////////////////////////////////////////

static void dst_n(const SkXfermode::PM4fState& state, uint32_t dst[], const SkPM4f[],
                         int count, const SkAlpha aa[]) {}

static void dst_1(const SkXfermode::PM4fState& state, uint32_t dst[], const SkPM4f&,
                  int count, const SkAlpha coverage[]) {}

const XferProcPair gProcs_Dst[] = {
    { dst_1, dst_n },
    { dst_1, dst_n },
    { dst_1, dst_n },
    { dst_1, dst_n },
};

///////////////////////////////////////////////////////////////////////////////////////////////////

template <DstType D> void srcover_n(const SkXfermode::PM4fState& state, uint32_t dst[],
                                    const SkPM4f src[], int count, const SkAlpha aa[]) {
    if (aa) {
        for (int i = 0; i < count; ++i) {
            unsigned a = aa[i];
            if (0 == a) {
                continue;
            }
            Sk4f s4 = Sk4f::Load(src[i].fVec);
            Sk4f d4 = load_dst<D>(dst[i]);
            if (a != 0xFF) {
                s4 = scale_by_coverage(s4, a);
            }
            Sk4f r4 = s4 + d4 * Sk4f(1 - get_alpha(s4));
            dst[i] = store_dst<D>(r4);
        }
    } else {
        for (int i = 0; i < count; ++i) {
            Sk4f s4 = Sk4f::Load(src[i].fVec);
            Sk4f d4 = load_dst<D>(dst[i]);
            Sk4f r4 = s4 + d4 * Sk4f(1 - get_alpha(s4));
            dst[i] = store_dst<D>(r4);
        }
    }
}

static void srcover_linear_dst_1(const SkXfermode::PM4fState& state, uint32_t dst[],
                                 const SkPM4f& src, int count, const SkAlpha aa[]) {
    const Sk4f s4 = Sk4f::Load(src.fVec);
    const Sk4f dst_scale = Sk4f(1 - get_alpha(s4));
    
    if (aa) {
        for (int i = 0; i < count; ++i) {
            unsigned a = aa[i];
            if (0 == a) {
                continue;
            }
            Sk4f d4 = Sk4f_fromL32(dst[i]);
            Sk4f r4;
            if (a != 0xFF) {
                Sk4f s4_aa = scale_by_coverage(s4, a);
                r4 = s4_aa + d4 * Sk4f(1 - get_alpha(s4_aa));
            } else {
                r4 = s4 + d4 * dst_scale;
            }
            dst[i] = Sk4f_toL32(r4);
        }
    } else {
        const Sk4f s4_255 = s4 * Sk4f(255) + Sk4f(0.5f);   // +0.5 to pre-bias for rounding
        while (count >= 4) {
            Sk4f d0 = to_4f(dst[0]);
            Sk4f d1 = to_4f(dst[1]);
            Sk4f d2 = to_4f(dst[2]);
            Sk4f d3 = to_4f(dst[3]);
            Sk4f_ToBytes((uint8_t*)dst,
                         s4_255 + d0 * dst_scale,
                         s4_255 + d1 * dst_scale,
                         s4_255 + d2 * dst_scale,
                         s4_255 + d3 * dst_scale);
            dst += 4;
            count -= 4;
        }
        for (int i = 0; i < count; ++i) {
            Sk4f d4 = to_4f(dst[i]);
            dst[i] = to_4b(s4_255 + d4 * dst_scale);
        }
    }
}

static void srcover_srgb_dst_1(const SkXfermode::PM4fState& state, uint32_t dst[],
                               const SkPM4f& src, int count, const SkAlpha aa[]) {
    Sk4f s4 = Sk4f::Load(src.fVec);
    Sk4f dst_scale = Sk4f(1 - get_alpha(s4));

    if (aa) {
        for (int i = 0; i < count; ++i) {
            unsigned a = aa[i];
            if (0 == a) {
                continue;
            }
            Sk4f d4 = srgb_4b_to_linear_unit(dst[i]);
            Sk4f r4;
            if (a != 0xFF) {
                const Sk4f s4_aa = scale_by_coverage(s4, a);
                r4 = s4_aa + d4 * Sk4f(1 - get_alpha(s4_aa));
            } else {
                r4 = s4 + d4 * dst_scale;
            }
            dst[i] = to_4b(linear_unit_to_srgb_255f(r4));
        }
    } else {
        while (count >= 4) {
            Sk4f d0 = srgb_4b_to_linear_unit(dst[0]);
            Sk4f d1 = srgb_4b_to_linear_unit(dst[1]);
            Sk4f d2 = srgb_4b_to_linear_unit(dst[2]);
            Sk4f d3 = srgb_4b_to_linear_unit(dst[3]);
            Sk4f_ToBytes((uint8_t*)dst,
                         linear_unit_to_srgb_255f(s4 + d0 * dst_scale),
                         linear_unit_to_srgb_255f(s4 + d1 * dst_scale),
                         linear_unit_to_srgb_255f(s4 + d2 * dst_scale),
                         linear_unit_to_srgb_255f(s4 + d3 * dst_scale));
            dst += 4;
            count -= 4;
        }
        for (int i = 0; i < count; ++i) {
            Sk4f d4 = srgb_4b_to_linear_unit(dst[i]);
            dst[i] = to_4b(linear_unit_to_srgb_255f(s4 + d4 * dst_scale));
        }
    }
}

const XferProcPair gProcs_SrcOver[] = {
    { srcover_linear_dst_1, srcover_n<kLinear_Dst> },   // linear   alpha
    { src_1<kLinear_Dst>,   src_n<kLinear_Dst>     },   // linear   opaque [ we are src-mode ]
    { srcover_srgb_dst_1,   srcover_n<kSRGB_Dst>   },   // srgb     alpha
    { src_1<kSRGB_Dst>,     src_n<kSRGB_Dst>       },   // srgb     opaque [ we are src-mode ]
};

///////////////////////////////////////////////////////////////////////////////////////////////////

static XferProcPair find_procs(SkXfermode::Mode mode, uint32_t flags) {
    SkASSERT(0 == (flags & ~3));
    flags &= 3;

    switch (mode) {
        case SkXfermode::kClear_Mode:   return gProcs_Clear[flags];
        case SkXfermode::kSrc_Mode:     return gProcs_Src[flags];
        case SkXfermode::kDst_Mode:     return gProcs_Dst[flags];
        case SkXfermode::kSrcOver_Mode: return gProcs_SrcOver[flags];
        default:
            break;
    }
    return { xfer_pm4_proc_1, xfer_pm4_proc_n };
}

SkXfermode::PM4fProc1 SkXfermode::GetPM4fProc1(Mode mode, uint32_t flags) {
    return find_procs(mode, flags).fP1;
}

SkXfermode::PM4fProcN SkXfermode::GetPM4fProcN(Mode mode, uint32_t flags) {
    return find_procs(mode, flags).fPN;
}

SkXfermode::PM4fProc1 SkXfermode::getPM4fProc1(uint32_t flags) const {
    Mode mode;
    return this->asMode(&mode) ? GetPM4fProc1(mode, flags) : xfer_pm4_proc_1;
}

SkXfermode::PM4fProcN SkXfermode::getPM4fProcN(uint32_t flags) const {
    Mode mode;
    return this->asMode(&mode) ? GetPM4fProcN(mode, flags) : xfer_pm4_proc_n;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
#include "SkColorPriv.h"

static Sk4f lcd16_to_unit_4f(uint16_t rgb) {
#ifdef SK_PMCOLOR_IS_RGBA
    Sk4i rgbi = Sk4i(SkGetPackedR16(rgb), SkGetPackedG16(rgb), SkGetPackedB16(rgb), 0);
#else
    Sk4i rgbi = Sk4i(SkGetPackedB16(rgb), SkGetPackedG16(rgb), SkGetPackedR16(rgb), 0);
#endif
    return SkNx_cast<float>(rgbi) * Sk4f(1.0f/31, 1.0f/63, 1.0f/31, 0);
}

template <DstType D>
void src_1_lcd(uint32_t dst[], const SkPM4f* src, int count, const uint16_t lcd[]) {
    const Sk4f s4 = Sk4f::Load(src->fVec);
    
    if (D == kLinear_Dst) {
        // operate in bias-255 space for src and dst
        const Sk4f s4bias = s4 * Sk4f(255);
        for (int i = 0; i < count; ++i) {
            uint16_t rgb = lcd[i];
            if (0 == rgb) {
                continue;
            }
            Sk4f d4bias = to_4f(dst[i]);
            dst[i] = to_4b(lerp(s4bias, d4bias, lcd16_to_unit_4f(rgb))) | (SK_A32_MASK << SK_A32_SHIFT);
        }
    } else {    // kSRGB
        for (int i = 0; i < count; ++i) {
            uint16_t rgb = lcd[i];
            if (0 == rgb) {
                continue;
            }
            Sk4f d4 = load_dst<D>(dst[i]);
            dst[i] = store_dst<D>(lerp(s4, d4, lcd16_to_unit_4f(rgb))) | (SK_A32_MASK << SK_A32_SHIFT);
        }
    }
}

template <DstType D>
void src_n_lcd(uint32_t dst[], const SkPM4f src[], int count, const uint16_t lcd[]) {
    for (int i = 0; i < count; ++i) {
        uint16_t rgb = lcd[i];
        if (0 == rgb) {
            continue;
        }
        Sk4f s4 = Sk4f::Load(src[i].fVec);
        Sk4f d4 = load_dst<D>(dst[i]);
        dst[i] = store_dst<D>(lerp(s4, d4, lcd16_to_unit_4f(rgb))) | (SK_A32_MASK << SK_A32_SHIFT);
    }
}

template <DstType D>
void srcover_1_lcd(uint32_t dst[], const SkPM4f* src, int count, const uint16_t lcd[]) {
    const Sk4f s4 = Sk4f::Load(src->fVec);
    Sk4f dst_scale = Sk4f(1 - get_alpha(s4));

    for (int i = 0; i < count; ++i) {
        uint16_t rgb = lcd[i];
        if (0 == rgb) {
            continue;
        }
        Sk4f d4 = load_dst<D>(dst[i]);
        Sk4f r4 = s4 + d4 * dst_scale;
        r4 = lerp(r4, d4, lcd16_to_unit_4f(rgb));
        dst[i] = store_dst<D>(r4) | (SK_A32_MASK << SK_A32_SHIFT);
    }
}

template <DstType D>
void srcover_n_lcd(uint32_t dst[], const SkPM4f src[], int count, const uint16_t lcd[]) {
    for (int i = 0; i < count; ++i) {
        uint16_t rgb = lcd[i];
        if (0 == rgb) {
            continue;
        }
        Sk4f s4 = Sk4f::Load(src[i].fVec);
        Sk4f dst_scale = Sk4f(1 - get_alpha(s4));
        Sk4f d4 = load_dst<D>(dst[i]);
        Sk4f r4 = s4 + d4 * dst_scale;
        r4 = lerp(r4, d4, lcd16_to_unit_4f(rgb));
        dst[i] = store_dst<D>(r4) | (SK_A32_MASK << SK_A32_SHIFT);
    }
}

SkXfermode::LCD32Proc SkXfermode::GetLCD32Proc(uint32_t flags) {
    SkASSERT((flags & ~7) == 0);
    flags &= 7;

    const LCD32Proc procs[] = {
        srcover_n_lcd<kSRGB_Dst>,   src_n_lcd<kSRGB_Dst>,
        srcover_1_lcd<kSRGB_Dst>,   src_1_lcd<kSRGB_Dst>,

        srcover_n_lcd<kLinear_Dst>, src_n_lcd<kLinear_Dst>,
        srcover_1_lcd<kLinear_Dst>, src_1_lcd<kLinear_Dst>,
    };
    return procs[flags];
}