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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
|
/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkRasterPipeline_opts_DEFINED
#define SkRasterPipeline_opts_DEFINED
#include "SkColorPriv.h"
#include "SkColorLookUpTable.h"
#include "SkColorSpaceXform_A2B.h"
#include "SkColorSpaceXformPriv.h"
#include "SkHalf.h"
#include "SkImageShaderContext.h"
#include "SkMSAN.h"
#include "SkPM4f.h"
#include "SkPM4fPriv.h"
#include "SkRasterPipeline.h"
#include "SkSRGB.h"
#include "SkUtils.h"
#include <utility>
namespace {
#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_AVX2
static constexpr int N = 8;
#else
static constexpr int N = 4;
#endif
using SkNf = SkNx<N, float>;
using SkNi = SkNx<N, int32_t>;
using SkNu = SkNx<N, uint32_t>;
using SkNh = SkNx<N, uint16_t>;
using SkNb = SkNx<N, uint8_t>;
struct Stage;
using Fn = void(SK_VECTORCALL *)(Stage*, size_t x_tail, SkNf,SkNf,SkNf,SkNf,
SkNf,SkNf,SkNf,SkNf);
struct Stage { Fn next; void* ctx; };
// x_tail encodes two values x and tail as x*N+tail, where 0 <= tail < N.
// x is the induction variable we're walking along, incrementing by N each step.
// tail == 0 means work with a full N pixels; otherwise use only the low tail pixels.
} // namespace
#define SI static inline
// Stages are logically a pipeline, and physically are contiguous in an array.
// To get to the next stage, we just increment our pointer to the next array element.
SI void SK_VECTORCALL next(Stage* st, size_t x_tail, SkNf r, SkNf g, SkNf b, SkNf a,
SkNf dr, SkNf dg, SkNf db, SkNf da) {
st->next(st+1, x_tail, r,g,b,a, dr,dg,db,da);
}
// Stages defined below always call next.
// This is always the last stage, a backstop that actually returns to the caller when done.
SI void SK_VECTORCALL just_return(Stage*, size_t, SkNf, SkNf, SkNf, SkNf,
SkNf, SkNf, SkNf, SkNf) {}
#define STAGE(name) \
static SK_ALWAYS_INLINE void name##_kernel(void* ctx, size_t x, size_t tail, \
SkNf& r, SkNf& g, SkNf& b, SkNf& a, \
SkNf& dr, SkNf& dg, SkNf& db, SkNf& da); \
SI void SK_VECTORCALL name(Stage* st, size_t x_tail, \
SkNf r, SkNf g, SkNf b, SkNf a, \
SkNf dr, SkNf dg, SkNf db, SkNf da) { \
name##_kernel(st->ctx, x_tail/N, x_tail%N, r,g,b,a, dr,dg,db,da); \
next(st, x_tail, r,g,b,a, dr,dg,db,da); \
} \
static SK_ALWAYS_INLINE void name##_kernel(void* ctx, size_t x, size_t tail, \
SkNf& r, SkNf& g, SkNf& b, SkNf& a, \
SkNf& dr, SkNf& dg, SkNf& db, SkNf& da)
// Many xfermodes apply the same logic to each channel.
#define RGBA_XFERMODE(name) \
static SK_ALWAYS_INLINE SkNf name##_kernel(const SkNf& s, const SkNf& sa, \
const SkNf& d, const SkNf& da); \
SI void SK_VECTORCALL name(Stage* st, size_t x_tail, \
SkNf r, SkNf g, SkNf b, SkNf a, \
SkNf dr, SkNf dg, SkNf db, SkNf da) { \
r = name##_kernel(r,a,dr,da); \
g = name##_kernel(g,a,dg,da); \
b = name##_kernel(b,a,db,da); \
a = name##_kernel(a,a,da,da); \
next(st, x_tail, r,g,b,a, dr,dg,db,da); \
} \
static SK_ALWAYS_INLINE SkNf name##_kernel(const SkNf& s, const SkNf& sa, \
const SkNf& d, const SkNf& da)
// Most of the rest apply the same logic to color channels and use srcover's alpha logic.
#define RGB_XFERMODE(name) \
static SK_ALWAYS_INLINE SkNf name##_kernel(const SkNf& s, const SkNf& sa, \
const SkNf& d, const SkNf& da); \
SI void SK_VECTORCALL name(Stage* st, size_t x_tail, \
SkNf r, SkNf g, SkNf b, SkNf a, \
SkNf dr, SkNf dg, SkNf db, SkNf da) { \
r = name##_kernel(r,a,dr,da); \
g = name##_kernel(g,a,dg,da); \
b = name##_kernel(b,a,db,da); \
a = a + (da * (1.0f-a)); \
next(st, x_tail, r,g,b,a, dr,dg,db,da); \
} \
static SK_ALWAYS_INLINE SkNf name##_kernel(const SkNf& s, const SkNf& sa, \
const SkNf& d, const SkNf& da)
template <typename T>
SI SkNx<N,T> load(size_t tail, const T* src) {
if (tail) {
T buf[8] = {0};
switch (tail & (N-1)) {
case 7: buf[6] = src[6];
case 6: buf[5] = src[5];
case 5: buf[4] = src[4];
case 4: buf[3] = src[3];
case 3: buf[2] = src[2];
case 2: buf[1] = src[1];
}
buf[0] = src[0];
return SkNx<N,T>::Load(buf);
}
return SkNx<N,T>::Load(src);
}
template <typename T>
SI SkNx<N,T> gather(size_t tail, const T* src, const SkNi& offset) {
if (tail) {
T buf[8] = {0};
switch (tail & (N-1)) {
case 7: buf[6] = src[offset[6]];
case 6: buf[5] = src[offset[5]];
case 5: buf[4] = src[offset[4]];
case 4: buf[3] = src[offset[3]];
case 3: buf[2] = src[offset[2]];
case 2: buf[1] = src[offset[1]];
}
buf[0] = src[offset[0]];
return SkNx<N,T>::Load(buf);
}
T buf[8];
for (size_t i = 0; i < N; i++) {
buf[i] = src[offset[i]];
}
return SkNx<N,T>::Load(buf);
}
template <typename T>
SI void store(size_t tail, const SkNx<N,T>& v, T* dst) {
if (tail) {
switch (tail & (N-1)) {
case 7: dst[6] = v[6];
case 6: dst[5] = v[5];
case 5: dst[4] = v[4];
case 4: dst[3] = v[3];
case 3: dst[2] = v[2];
case 2: dst[1] = v[1];
}
dst[0] = v[0];
return;
}
v.store(dst);
}
#if !defined(SKNX_NO_SIMD) && SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_AVX2
SI __m256i mask(size_t tail) {
static const int masks[][8] = {
{~0,~0,~0,~0, ~0,~0,~0,~0 }, // remember, tail == 0 ~~> load all N
{~0, 0, 0, 0, 0, 0, 0, 0 },
{~0,~0, 0, 0, 0, 0, 0, 0 },
{~0,~0,~0, 0, 0, 0, 0, 0 },
{~0,~0,~0,~0, 0, 0, 0, 0 },
{~0,~0,~0,~0, ~0, 0, 0, 0 },
{~0,~0,~0,~0, ~0,~0, 0, 0 },
{~0,~0,~0,~0, ~0,~0,~0, 0 },
};
return SkNi::Load(masks + tail).fVec;
}
SI SkNi load(size_t tail, const int32_t* src) {
return tail ? _mm256_maskload_epi32((const int*)src, mask(tail))
: SkNi::Load(src);
}
SI SkNu load(size_t tail, const uint32_t* src) {
return tail ? _mm256_maskload_epi32((const int*)src, mask(tail))
: SkNu::Load(src);
}
SI SkNi gather(size_t tail, const int32_t* src, const SkNi& offset) {
return _mm256_mask_i32gather_epi32(SkNi(0).fVec,
(const int*)src, offset.fVec, mask(tail), 4);
}
SI SkNu gather(size_t tail, const uint32_t* src, const SkNi& offset) {
return _mm256_mask_i32gather_epi32(SkNi(0).fVec,
(const int*)src, offset.fVec, mask(tail), 4);
}
static const char* bug = "I don't think MSAN understands maskstore.";
SI void store(size_t tail, const SkNi& v, int32_t* dst) {
if (tail) {
_mm256_maskstore_epi32((int*)dst, mask(tail), v.fVec);
return sk_msan_mark_initialized(dst, dst+tail, bug);
}
v.store(dst);
}
SI void store(size_t tail, const SkNu& v, uint32_t* dst) {
if (tail) {
_mm256_maskstore_epi32((int*)dst, mask(tail), v.fVec);
return sk_msan_mark_initialized(dst, dst+tail, bug);
}
v.store(dst);
}
#endif
SI void from_8888(const SkNu& _8888, SkNf* r, SkNf* g, SkNf* b, SkNf* a) {
auto to_float = [](const SkNu& v) { return SkNx_cast<float>(SkNi::Load(&v)); };
*r = (1/255.0f)*to_float((_8888 >> 0) & 0xff);
*g = (1/255.0f)*to_float((_8888 >> 8) & 0xff);
*b = (1/255.0f)*to_float((_8888 >> 16) & 0xff);
*a = (1/255.0f)*to_float( _8888 >> 24 );
}
SI void from_4444(const SkNh& _4444, SkNf* r, SkNf* g, SkNf* b, SkNf* a) {
auto _32_bit = SkNx_cast<int>(_4444);
*r = SkNx_cast<float>(_32_bit & (0xF << SK_R4444_SHIFT)) * (1.0f / (0xF << SK_R4444_SHIFT));
*g = SkNx_cast<float>(_32_bit & (0xF << SK_G4444_SHIFT)) * (1.0f / (0xF << SK_G4444_SHIFT));
*b = SkNx_cast<float>(_32_bit & (0xF << SK_B4444_SHIFT)) * (1.0f / (0xF << SK_B4444_SHIFT));
*a = SkNx_cast<float>(_32_bit & (0xF << SK_A4444_SHIFT)) * (1.0f / (0xF << SK_A4444_SHIFT));
}
SI void from_565(const SkNh& _565, SkNf* r, SkNf* g, SkNf* b) {
auto _32_bit = SkNx_cast<int>(_565);
*r = SkNx_cast<float>(_32_bit & SK_R16_MASK_IN_PLACE) * (1.0f / SK_R16_MASK_IN_PLACE);
*g = SkNx_cast<float>(_32_bit & SK_G16_MASK_IN_PLACE) * (1.0f / SK_G16_MASK_IN_PLACE);
*b = SkNx_cast<float>(_32_bit & SK_B16_MASK_IN_PLACE) * (1.0f / SK_B16_MASK_IN_PLACE);
}
STAGE(trace) {
SkDebugf("%s\n", (const char*)ctx);
}
STAGE(registers) {
auto print = [](const char* name, const SkNf& v) {
SkDebugf("%s:", name);
for (int i = 0; i < N; i++) {
SkDebugf(" %g", v[i]);
}
SkDebugf("\n");
};
print(" r", r);
print(" g", g);
print(" b", b);
print(" a", a);
print("dr", dr);
print("dg", dg);
print("db", db);
print("da", da);
}
STAGE(clamp_0) {
a = SkNf::Max(a, 0.0f);
r = SkNf::Max(r, 0.0f);
g = SkNf::Max(g, 0.0f);
b = SkNf::Max(b, 0.0f);
}
STAGE(clamp_a) {
a = SkNf::Min(a, 1.0f);
r = SkNf::Min(r, a);
g = SkNf::Min(g, a);
b = SkNf::Min(b, a);
}
STAGE(clamp_1) {
a = SkNf::Min(a, 1.0f);
r = SkNf::Min(r, 1.0f);
g = SkNf::Min(g, 1.0f);
b = SkNf::Min(b, 1.0f);
}
STAGE(unpremul) {
auto scale = (a == 0.0f).thenElse(0.0f, 1.0f/a);
r *= scale;
g *= scale;
b *= scale;
}
STAGE(premul) {
r *= a;
g *= a;
b *= a;
}
STAGE(set_rgb) {
auto rgb = (const float*)ctx;
r = rgb[0];
g = rgb[1];
b = rgb[2];
}
STAGE(move_src_dst) {
dr = r;
dg = g;
db = b;
da = a;
}
STAGE(move_dst_src) {
r = dr;
g = dg;
b = db;
a = da;
}
STAGE(swap_rb) { SkTSwap( r, b); }
STAGE(swap_rb_d) { SkTSwap(dr, db); }
STAGE(from_srgb) {
r = sk_linear_from_srgb_math(r);
g = sk_linear_from_srgb_math(g);
b = sk_linear_from_srgb_math(b);
}
STAGE(from_srgb_d) {
dr = sk_linear_from_srgb_math(dr);
dg = sk_linear_from_srgb_math(dg);
db = sk_linear_from_srgb_math(db);
}
STAGE(to_srgb) {
r = sk_linear_to_srgb_needs_round(r);
g = sk_linear_to_srgb_needs_round(g);
b = sk_linear_to_srgb_needs_round(b);
}
// The default shader produces a constant color (from the SkPaint).
STAGE(constant_color) {
auto color = (const SkPM4f*)ctx;
r = color->r();
g = color->g();
b = color->b();
a = color->a();
}
// s' = sc for a constant c.
STAGE(scale_constant_float) {
SkNf c = *(const float*)ctx;
r *= c;
g *= c;
b *= c;
a *= c;
}
// s' = sc for 8-bit c.
STAGE(scale_u8) {
auto ptr = *(const uint8_t**)ctx + x;
SkNf c = SkNx_cast<float>(load(tail, ptr)) * (1/255.0f);
r = r*c;
g = g*c;
b = b*c;
a = a*c;
}
SI SkNf lerp(const SkNf& from, const SkNf& to, const SkNf& cov) {
return SkNx_fma(to-from, cov, from);
}
// s' = d(1-c) + sc, for a constant c.
STAGE(lerp_constant_float) {
SkNf c = *(const float*)ctx;
r = lerp(dr, r, c);
g = lerp(dg, g, c);
b = lerp(db, b, c);
a = lerp(da, a, c);
}
// s' = d(1-c) + sc for 8-bit c.
STAGE(lerp_u8) {
auto ptr = *(const uint8_t**)ctx + x;
SkNf c = SkNx_cast<float>(load(tail, ptr)) * (1/255.0f);
r = lerp(dr, r, c);
g = lerp(dg, g, c);
b = lerp(db, b, c);
a = lerp(da, a, c);
}
// s' = d(1-c) + sc for 565 c.
STAGE(lerp_565) {
auto ptr = *(const uint16_t**)ctx + x;
SkNf cr, cg, cb;
from_565(load(tail, ptr), &cr, &cg, &cb);
r = lerp(dr, r, cr);
g = lerp(dg, g, cg);
b = lerp(db, b, cb);
a = 1.0f;
}
STAGE(load_565) {
auto ptr = *(const uint16_t**)ctx + x;
from_565(load(tail, ptr), &r,&g,&b);
a = 1.0f;
}
STAGE(load_565_d) {
auto ptr = *(const uint16_t**)ctx + x;
from_565(load(tail, ptr), &dr,&dg,&db);
da = 1.0f;
}
STAGE(store_565) {
auto ptr = *(uint16_t**)ctx + x;
store(tail, SkNx_cast<uint16_t>( SkNx_cast<int>(r*SK_R16_MASK + 0.5f) << SK_R16_SHIFT
| SkNx_cast<int>(g*SK_G16_MASK + 0.5f) << SK_G16_SHIFT
| SkNx_cast<int>(b*SK_B16_MASK + 0.5f) << SK_B16_SHIFT), ptr);
}
STAGE(load_f16) {
auto ptr = *(const uint64_t**)ctx + x;
SkNh rh, gh, bh, ah;
if (tail) {
uint64_t buf[8] = {0};
switch (tail & (N-1)) {
case 7: buf[6] = ptr[6];
case 6: buf[5] = ptr[5];
case 5: buf[4] = ptr[4];
case 4: buf[3] = ptr[3];
case 3: buf[2] = ptr[2];
case 2: buf[1] = ptr[1];
}
buf[0] = ptr[0];
SkNh::Load4(buf, &rh, &gh, &bh, &ah);
} else {
SkNh::Load4(ptr, &rh, &gh, &bh, &ah);
}
r = SkHalfToFloat_finite_ftz(rh);
g = SkHalfToFloat_finite_ftz(gh);
b = SkHalfToFloat_finite_ftz(bh);
a = SkHalfToFloat_finite_ftz(ah);
}
STAGE(load_f16_d) {
auto ptr = *(const uint64_t**)ctx + x;
SkNh rh, gh, bh, ah;
if (tail) {
uint64_t buf[8] = {0};
switch (tail & (N-1)) {
case 7: buf[6] = ptr[6];
case 6: buf[5] = ptr[5];
case 5: buf[4] = ptr[4];
case 4: buf[3] = ptr[3];
case 3: buf[2] = ptr[2];
case 2: buf[1] = ptr[1];
}
buf[0] = ptr[0];
SkNh::Load4(buf, &rh, &gh, &bh, &ah);
} else {
SkNh::Load4(ptr, &rh, &gh, &bh, &ah);
}
dr = SkHalfToFloat_finite_ftz(rh);
dg = SkHalfToFloat_finite_ftz(gh);
db = SkHalfToFloat_finite_ftz(bh);
da = SkHalfToFloat_finite_ftz(ah);
}
STAGE(store_f16) {
auto ptr = *(uint64_t**)ctx + x;
uint64_t buf[8];
SkNh::Store4(tail ? buf : ptr, SkFloatToHalf_finite_ftz(r),
SkFloatToHalf_finite_ftz(g),
SkFloatToHalf_finite_ftz(b),
SkFloatToHalf_finite_ftz(a));
if (tail) {
switch (tail & (N-1)) {
case 7: ptr[6] = buf[6];
case 6: ptr[5] = buf[5];
case 5: ptr[4] = buf[4];
case 4: ptr[3] = buf[3];
case 3: ptr[2] = buf[2];
case 2: ptr[1] = buf[1];
}
ptr[0] = buf[0];
}
}
STAGE(store_f32) {
auto ptr = *(SkPM4f**)ctx + x;
SkPM4f buf[8];
SkNf::Store4(tail ? buf : ptr, r,g,b,a);
if (tail) {
switch (tail & (N-1)) {
case 7: ptr[6] = buf[6];
case 6: ptr[5] = buf[5];
case 5: ptr[4] = buf[4];
case 4: ptr[3] = buf[3];
case 3: ptr[2] = buf[2];
case 2: ptr[1] = buf[1];
}
ptr[0] = buf[0];
}
}
STAGE(load_8888) {
auto ptr = *(const uint32_t**)ctx + x;
from_8888(load(tail, ptr), &r, &g, &b, &a);
}
STAGE(load_8888_d) {
auto ptr = *(const uint32_t**)ctx + x;
from_8888(load(tail, ptr), &dr, &dg, &db, &da);
}
STAGE(store_8888) {
auto ptr = *(uint32_t**)ctx + x;
store(tail, ( SkNx_cast<int>(255.0f * r + 0.5f) << 0
| SkNx_cast<int>(255.0f * g + 0.5f) << 8
| SkNx_cast<int>(255.0f * b + 0.5f) << 16
| SkNx_cast<int>(255.0f * a + 0.5f) << 24 ), (int*)ptr);
}
SI SkNf inv(const SkNf& x) { return 1.0f - x; }
RGBA_XFERMODE(clear) { return 0.0f; }
RGBA_XFERMODE(srcatop) { return s*da + d*inv(sa); }
RGBA_XFERMODE(srcin) { return s * da; }
RGBA_XFERMODE(srcout) { return s * inv(da); }
RGBA_XFERMODE(srcover) { return SkNx_fma(d, inv(sa), s); }
RGBA_XFERMODE(dstatop) { return srcatop_kernel(d,da,s,sa); }
RGBA_XFERMODE(dstin) { return srcin_kernel (d,da,s,sa); }
RGBA_XFERMODE(dstout) { return srcout_kernel (d,da,s,sa); }
RGBA_XFERMODE(dstover) { return srcover_kernel(d,da,s,sa); }
RGBA_XFERMODE(modulate) { return s*d; }
RGBA_XFERMODE(multiply) { return s*inv(da) + d*inv(sa) + s*d; }
RGBA_XFERMODE(plus_) { return s + d; }
RGBA_XFERMODE(screen) { return s + d - s*d; }
RGBA_XFERMODE(xor_) { return s*inv(da) + d*inv(sa); }
RGB_XFERMODE(colorburn) {
return (d == da ).thenElse(d + s*inv(da),
(s == 0.0f).thenElse(s + d*inv(sa),
sa*(da - SkNf::Min(da, (da-d)*sa/s)) + s*inv(da) + d*inv(sa)));
}
RGB_XFERMODE(colordodge) {
return (d == 0.0f).thenElse(d + s*inv(da),
(s == sa ).thenElse(s + d*inv(sa),
sa*SkNf::Min(da, (d*sa)/(sa - s)) + s*inv(da) + d*inv(sa)));
}
RGB_XFERMODE(darken) { return s + d - SkNf::Max(s*da, d*sa); }
RGB_XFERMODE(difference) { return s + d - 2.0f*SkNf::Min(s*da,d*sa); }
RGB_XFERMODE(exclusion) { return s + d - 2.0f*s*d; }
RGB_XFERMODE(hardlight) {
return s*inv(da) + d*inv(sa)
+ (2.0f*s <= sa).thenElse(2.0f*s*d, sa*da - 2.0f*(da-d)*(sa-s));
}
RGB_XFERMODE(lighten) { return s + d - SkNf::Min(s*da, d*sa); }
RGB_XFERMODE(overlay) { return hardlight_kernel(d,da,s,sa); }
RGB_XFERMODE(softlight) {
SkNf m = (da > 0.0f).thenElse(d / da, 0.0f),
s2 = 2.0f*s,
m4 = 4.0f*m;
// The logic forks three ways:
// 1. dark src?
// 2. light src, dark dst?
// 3. light src, light dst?
SkNf darkSrc = d*(sa + (s2 - sa)*(1.0f - m)), // Used in case 1.
darkDst = (m4*m4 + m4)*(m - 1.0f) + 7.0f*m, // Used in case 2.
liteDst = m.rsqrt().invert() - m, // Used in case 3.
liteSrc = d*sa + da*(s2 - sa) * (4.0f*d <= da).thenElse(darkDst, liteDst); // 2 or 3?
return s*inv(da) + d*inv(sa) + (s2 <= sa).thenElse(darkSrc, liteSrc); // 1 or (2 or 3)?
}
STAGE(luminance_to_alpha) {
a = SK_LUM_COEFF_R*r + SK_LUM_COEFF_G*g + SK_LUM_COEFF_B*b;
r = g = b = 0;
}
STAGE(matrix_2x3) {
auto m = (const float*)ctx;
auto fma = [](const SkNf& f, const SkNf& m, const SkNf& a) { return SkNx_fma(f,m,a); };
auto R = fma(r,m[0], fma(g,m[2], m[4])),
G = fma(r,m[1], fma(g,m[3], m[5]));
r = R;
g = G;
}
STAGE(matrix_3x4) {
auto m = (const float*)ctx;
auto fma = [](const SkNf& f, const SkNf& m, const SkNf& a) { return SkNx_fma(f,m,a); };
auto R = fma(r,m[0], fma(g,m[3], fma(b,m[6], m[ 9]))),
G = fma(r,m[1], fma(g,m[4], fma(b,m[7], m[10]))),
B = fma(r,m[2], fma(g,m[5], fma(b,m[8], m[11])));
r = R;
g = G;
b = B;
}
STAGE(matrix_4x5) {
auto m = (const float*)ctx;
auto fma = [](const SkNf& f, const SkNf& m, const SkNf& a) { return SkNx_fma(f,m,a); };
auto R = fma(r,m[0], fma(g,m[4], fma(b,m[ 8], fma(a,m[12], m[16])))),
G = fma(r,m[1], fma(g,m[5], fma(b,m[ 9], fma(a,m[13], m[17])))),
B = fma(r,m[2], fma(g,m[6], fma(b,m[10], fma(a,m[14], m[18])))),
A = fma(r,m[3], fma(g,m[7], fma(b,m[11], fma(a,m[15], m[19]))));
r = R;
g = G;
b = B;
a = A;
}
STAGE(matrix_perspective) {
// N.B. unlike the matrix_NxM stages, this takes a row-major matrix.
auto m = (const float*)ctx;
auto fma = [](const SkNf& f, const SkNf& m, const SkNf& a) { return SkNx_fma(f,m,a); };
auto R = fma(r,m[0], fma(g,m[1], m[2])),
G = fma(r,m[3], fma(g,m[4], m[5])),
Z = fma(r,m[6], fma(g,m[7], m[8]));
r = R * Z.invert();
g = G * Z.invert();
}
SI SkNf parametric(const SkNf& v, const SkColorSpaceTransferFn& p) {
float result[N]; // Unconstrained powf() doesn't vectorize well...
for (int i = 0; i < N; i++) {
float s = v[i];
result[i] = (s <= p.fD) ? p.fE * s + p.fF
: powf(s * p.fA + p.fB, p.fG) + p.fC;
}
return SkNf::Load(result);
}
STAGE(parametric_r) { r = parametric(r, *(const SkColorSpaceTransferFn*)ctx); }
STAGE(parametric_g) { g = parametric(g, *(const SkColorSpaceTransferFn*)ctx); }
STAGE(parametric_b) { b = parametric(b, *(const SkColorSpaceTransferFn*)ctx); }
SI SkNf table(const SkNf& v, const SkTableTransferFn& table) {
float result[N];
for (int i = 0; i < N; i++) {
result[i] = interp_lut(v[i], table.fData, table.fSize);
}
return SkNf::Load(result);
}
STAGE(table_r) { r = table(r, *(const SkTableTransferFn*)ctx); }
STAGE(table_g) { g = table(g, *(const SkTableTransferFn*)ctx); }
STAGE(table_b) { b = table(b, *(const SkTableTransferFn*)ctx); }
STAGE(color_lookup_table) {
const SkColorLookUpTable* colorLUT = (const SkColorLookUpTable*)ctx;
float rgb[3];
float result[3][N];
for (int i = 0; i < N; ++i) {
rgb[0] = r[i];
rgb[1] = g[i];
rgb[2] = b[i];
colorLUT->interp3D(rgb, rgb);
result[0][i] = rgb[0];
result[1][i] = rgb[1];
result[2][i] = rgb[2];
}
r = SkNf::Load(result[0]);
g = SkNf::Load(result[1]);
b = SkNf::Load(result[2]);
}
STAGE(lab_to_xyz) {
const auto lab_l = r * 100.0f;
const auto lab_a = g * 255.0f - 128.0f;
const auto lab_b = b * 255.0f - 128.0f;
auto Y = (lab_l + 16.0f) * (1/116.0f);
auto X = lab_a * (1/500.0f) + Y;
auto Z = Y - (lab_b * (1/200.0f));
const auto X3 = X*X*X;
X = (X3 > 0.008856f).thenElse(X3, (X - (16/116.0f)) * (1/7.787f));
const auto Y3 = Y*Y*Y;
Y = (Y3 > 0.008856f).thenElse(Y3, (Y - (16/116.0f)) * (1/7.787f));
const auto Z3 = Z*Z*Z;
Z = (Z3 > 0.008856f).thenElse(Z3, (Z - (16/116.0f)) * (1/7.787f));
// adjust to D50 illuminant
X *= 0.96422f;
Y *= 1.00000f;
Z *= 0.82521f;
r = X;
g = Y;
b = Z;
}
SI SkNf assert_in_tile(const SkNf& v, float limit) {
for (int i = 0; i < N; i++) {
SkASSERT(0 <= v[i] && v[i] < limit);
}
return v;
}
SI SkNf clamp(const SkNf& v, float limit) {
SkNf result = SkNf::Max(0, SkNf::Min(v, limit - 0.5f));
return assert_in_tile(result, limit);
}
SI SkNf repeat(const SkNf& v, float limit) {
SkNf result = v - (v/limit).floor()*limit;
// For small negative v, (v/limit).floor()*limit can dominate v in the subtraction,
// which leaves result == limit. We want result < limit, so clamp it one ULP.
result = SkNf::Min(result, nextafterf(limit, 0));
return assert_in_tile(result, limit);
}
SI SkNf mirror(const SkNf& v, float l/*imit*/) {
SkNf result = ((v - l) - ((v - l) / (2*l)).floor()*(2*l) - l).abs();
// Same deal as repeat.
result = SkNf::Min(result, nextafterf(l, 0));
return assert_in_tile(result, l);
}
STAGE( clamp_x) { r = clamp (r, *(const int*)ctx); }
STAGE(repeat_x) { r = repeat(r, *(const int*)ctx); }
STAGE(mirror_x) { r = mirror(r, *(const int*)ctx); }
STAGE( clamp_y) { g = clamp (g, *(const int*)ctx); }
STAGE(repeat_y) { g = repeat(g, *(const int*)ctx); }
STAGE(mirror_y) { g = mirror(g, *(const int*)ctx); }
STAGE(top_left) {
auto sc = (SkImageShaderContext*)ctx;
r.store(sc->x);
g.store(sc->y);
r -= 0.5f;
g -= 0.5f;
auto fx = r - r.floor(),
fy = g - g.floor();
((1.0f - fx) * (1.0f - fy)).store(sc->scale);
};
STAGE(top_right) {
auto sc = (SkImageShaderContext*)ctx;
r = SkNf::Load(sc->x) + 0.5f;
g = SkNf::Load(sc->y) - 0.5f;
auto fx = r - r.floor(),
fy = g - g.floor();
(fx * (1.0f - fy)).store(sc->scale);
};
STAGE(bottom_left) {
auto sc = (SkImageShaderContext*)ctx;
r = SkNf::Load(sc->x) - 0.5f;
g = SkNf::Load(sc->y) + 0.5f;
auto fx = r - r.floor(),
fy = g - g.floor();
((1.0f - fx) * fy).store(sc->scale);
};
STAGE(bottom_right) {
auto sc = (SkImageShaderContext*)ctx;
r = SkNf::Load(sc->x) + 0.5f;
g = SkNf::Load(sc->y) + 0.5f;
auto fx = r - r.floor(),
fy = g - g.floor();
(fx * fy).store(sc->scale);
};
STAGE(accumulate) {
auto sc = (const SkImageShaderContext*)ctx;
auto scale = SkNf::Load(sc->scale);
dr = SkNx_fma(scale, r, dr);
dg = SkNx_fma(scale, g, dg);
db = SkNx_fma(scale, b, db);
da = SkNx_fma(scale, a, da);
}
template <typename T>
SI SkNi offset_and_ptr(T** ptr, const void* ctx, const SkNf& x, const SkNf& y) {
auto sc = (const SkImageShaderContext*)ctx;
SkNi ix = SkNx_cast<int>(x),
iy = SkNx_cast<int>(y);
SkNi offset = iy*sc->stride + ix;
*ptr = (const T*)sc->pixels;
return offset;
}
STAGE(gather_a8) {
const uint8_t* p;
SkNi offset = offset_and_ptr(&p, ctx, r, g);
r = g = b = 0.0f;
a = SkNx_cast<float>(gather(tail, p, offset)) * (1/255.0f);
}
STAGE(gather_i8) {
auto sc = (const SkImageShaderContext*)ctx;
const uint8_t* p;
SkNi offset = offset_and_ptr(&p, sc, r, g);
SkNi ix = SkNx_cast<int>(gather(tail, p, offset));
from_8888(gather(tail, sc->ctable->readColors(), ix), &r, &g, &b, &a);
}
STAGE(gather_g8) {
const uint8_t* p;
SkNi offset = offset_and_ptr(&p, ctx, r, g);
r = g = b = SkNx_cast<float>(gather(tail, p, offset)) * (1/255.0f);
a = 1.0f;
}
STAGE(gather_565) {
const uint16_t* p;
SkNi offset = offset_and_ptr(&p, ctx, r, g);
from_565(gather(tail, p, offset), &r, &g, &b);
a = 1.0f;
}
STAGE(gather_4444) {
const uint16_t* p;
SkNi offset = offset_and_ptr(&p, ctx, r, g);
from_4444(gather(tail, p, offset), &r, &g, &b, &a);
}
STAGE(gather_8888) {
const uint32_t* p;
SkNi offset = offset_and_ptr(&p, ctx, r, g);
from_8888(gather(tail, p, offset), &r, &g, &b, &a);
}
STAGE(gather_f16) {
const uint64_t* p;
SkNi offset = offset_and_ptr(&p, ctx, r, g);
// f16 -> f32 conversion works best with tightly packed f16s,
// so we gather each component rather than using gather().
uint16_t R[N], G[N], B[N], A[N];
size_t n = tail ? tail : N;
for (size_t i = 0; i < n; i++) {
uint64_t rgba = p[offset[i]];
R[i] = rgba >> 0;
G[i] = rgba >> 16;
B[i] = rgba >> 32;
A[i] = rgba >> 48;
}
for (size_t i = n; i < N; i++) {
R[i] = G[i] = B[i] = A[i] = 0;
}
r = SkHalfToFloat_finite_ftz(SkNh::Load(R));
g = SkHalfToFloat_finite_ftz(SkNh::Load(G));
b = SkHalfToFloat_finite_ftz(SkNh::Load(B));
a = SkHalfToFloat_finite_ftz(SkNh::Load(A));
}
SI Fn enum_to_Fn(SkRasterPipeline::StockStage st) {
switch (st) {
#define M(stage) case SkRasterPipeline::stage: return stage;
SK_RASTER_PIPELINE_STAGES(M)
#undef M
}
SkASSERT(false);
return just_return;
}
namespace SK_OPTS_NS {
struct Memset16 {
uint16_t** dst;
uint16_t val;
void operator()(size_t x, size_t, size_t n) { sk_memset16(*dst + x, val, n); }
};
struct Memset32 {
uint32_t** dst;
uint32_t val;
void operator()(size_t x, size_t, size_t n) { sk_memset32(*dst + x, val, n); }
};
struct Memset64 {
uint64_t** dst;
uint64_t val;
void operator()(size_t x, size_t, size_t n) { sk_memset64(*dst + x, val, n); }
};
SI std::function<void(size_t, size_t, size_t)>
compile_pipeline(const SkRasterPipeline::Stage* stages, int nstages) {
if (nstages == 2 && stages[0].stage == SkRasterPipeline::constant_color) {
SkPM4f src = *(const SkPM4f*)stages[0].ctx;
void* dst = stages[1].ctx;
switch (stages[1].stage) {
case SkRasterPipeline::store_565:
return Memset16{(uint16_t**)dst, SkPackRGB16(src.r() * SK_R16_MASK + 0.5f,
src.g() * SK_G16_MASK + 0.5f,
src.b() * SK_B16_MASK + 0.5f)};
case SkRasterPipeline::store_8888:
return Memset32{(uint32_t**)dst, Sk4f_toL32(src.to4f())};
case SkRasterPipeline::store_f16:
return Memset64{(uint64_t**)dst, src.toF16()};
default: break;
}
}
struct Compiled {
Compiled(const SkRasterPipeline::Stage* stages, int nstages) {
if (nstages == 0) {
return;
}
fStart = enum_to_Fn(stages[0].stage);
for (int i = 0; i < nstages-1; i++) {
fStages[i].next = enum_to_Fn(stages[i+1].stage);
fStages[i].ctx = stages[i].ctx;
}
fStages[nstages-1].next = just_return;
fStages[nstages-1].ctx = stages[nstages-1].ctx;
}
void operator()(size_t x, size_t y, size_t n) {
float dx[] = { 0,1,2,3,4,5,6,7 };
SkNf X = SkNf(x) + SkNf::Load(dx) + 0.5f,
Y = SkNf(y) + 0.5f,
_0 = SkNf(0),
_1 = SkNf(1);
while (n >= N) {
fStart(fStages, x*N, X,Y,_1,_0, _0,_0,_0,_0);
X += (float)N;
x += N;
n -= N;
}
if (n) {
fStart(fStages, x*N+n, X,Y,_1,_0, _0,_0,_0,_0);
}
}
Fn fStart = just_return;
Stage fStages[SkRasterPipeline::kMaxStages];
} fn { stages, nstages };
return fn;
}
} // namespace SK_OPTS_NS
#undef SI
#undef STAGE
#undef RGBA_XFERMODE
#undef RGB_XFERMODE
#endif//SkRasterPipeline_opts_DEFINED
|