OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2016 Google Inc. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license that can be | |
5 * found in the LICENSE file. | |
6 */ | |
7 | |
8 #include "SkHalf.h" | |
9 #include "SkPM4fPriv.h" | |
10 #include "SkUtils.h" | |
11 #include "SkXfermode.h" | |
12 | |
13 enum DstType { | |
14 kU16_Dst, | |
15 kF16_Dst, | |
16 }; | |
17 | |
18 static Sk4f lerp_by_coverage(const Sk4f& src, const Sk4f& dst, uint8_t srcCovera
ge) { | |
19 return dst + (src - dst) * Sk4f(srcCoverage * (1/255.0f)); | |
20 } | |
21 | |
22 template <DstType D> Sk4f unit_to_bias(const Sk4f& x4) { | |
23 return (D == kU16_Dst) ? x4 * Sk4f(65535) : x4; | |
24 } | |
25 | |
26 template <DstType D> Sk4f bias_to_unit(const Sk4f& x4) { | |
27 return (D == kU16_Dst) ? x4 * Sk4f(1.0f/65535) : x4; | |
28 } | |
29 | |
30 // returns value already biased by 65535 | |
31 static Sk4f load_from_u16(uint64_t value) { | |
32 return SkNx_cast<float>(Sk4h::Load(&value)); | |
33 } | |
34 | |
35 // takes floats already biased by 65535 | |
36 static uint64_t store_to_u16(const Sk4f& x4) { | |
37 uint64_t value; | |
38 SkNx_cast<uint16_t>(x4 + Sk4f(0.5f)).store(&value); | |
39 return value; | |
40 } | |
41 | |
42 // Returns dst in its "natural" bias (either unit-float or 16bit int) | |
43 // | |
44 template <DstType D> Sk4f load_from_dst(uint64_t dst) { | |
45 return (D == kU16_Dst) ? load_from_u16(dst) : SkHalfToFloat_01(dst); | |
46 } | |
47 | |
48 // Assumes x4 is already in the "natural" bias (either unit-float or 16bit int) | |
49 template <DstType D> uint64_t store_to_dst(const Sk4f& x4) { | |
50 return (D == kU16_Dst) ? store_to_u16(x4) : SkFloatToHalf_01(x4); | |
51 } | |
52 | |
53 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | |
54 | |
55 template <DstType D> void xfer_u64_1(const SkXfermode* xfer, uint64_t dst[], | |
56 const SkPM4f* src, int count, const SkAlpha
aa[]) { | |
57 SkXfermodeProc4f proc = xfer->getProc4f(); | |
58 SkPM4f d; | |
59 if (aa) { | |
60 for (int i = 0; i < count; ++i) { | |
61 Sk4f d4 = bias_to_unit<D>(load_from_dst<D>(dst[i])); | |
62 d4.store(d.fVec); | |
63 Sk4f r4 = unit_to_bias<D>(Sk4f::Load(proc(*src, d).fVec)); | |
64 dst[i] = store_to_dst<D>(lerp_by_coverage(r4, d4, aa[i])); | |
65 } | |
66 } else { | |
67 for (int i = 0; i < count; ++i) { | |
68 bias_to_unit<D>(load_from_dst<D>(dst[i])).store(d.fVec); | |
69 Sk4f r4 = unit_to_bias<D>(Sk4f::Load(proc(*src, d).fVec)); | |
70 dst[i] = store_to_dst<D>(r4); | |
71 } | |
72 } | |
73 } | |
74 | |
75 template <DstType D> void xfer_u64_n(const SkXfermode* xfer, uint64_t dst[], | |
76 const SkPM4f src[], int count, const SkAlph
a aa[]) { | |
77 SkXfermodeProc4f proc = xfer->getProc4f(); | |
78 SkPM4f d; | |
79 if (aa) { | |
80 for (int i = 0; i < count; ++i) { | |
81 Sk4f d4 = bias_to_unit<D>(load_from_dst<D>(dst[i])); | |
82 d4.store(d.fVec); | |
83 Sk4f r4 = unit_to_bias<D>(Sk4f::Load(proc(src[i], d).fVec)); | |
84 dst[i] = store_to_dst<D>(lerp_by_coverage(r4, d4, aa[i])); | |
85 } | |
86 } else { | |
87 for (int i = 0; i < count; ++i) { | |
88 bias_to_unit<D>(load_from_dst<D>(dst[i])).store(d.fVec); | |
89 Sk4f r4 = unit_to_bias<D>(Sk4f::Load(proc(src[i], d).fVec)); | |
90 dst[i] = store_to_dst<D>(r4); | |
91 } | |
92 } | |
93 } | |
94 | |
95 const SkXfermode::D64Proc gProcs_General[] = { | |
96 xfer_u64_n<kU16_Dst>, xfer_u64_n<kU16_Dst>, | |
97 xfer_u64_1<kU16_Dst>, xfer_u64_1<kU16_Dst>, | |
98 xfer_u64_n<kF16_Dst>, xfer_u64_n<kF16_Dst>, | |
99 xfer_u64_1<kF16_Dst>, xfer_u64_1<kF16_Dst>, | |
100 }; | |
101 | |
102 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | |
103 | |
104 template <DstType D> void clear(const SkXfermode*, uint64_t dst[], | |
105 const SkPM4f*, int count, const SkAlpha aa[]) { | |
106 if (aa) { | |
107 for (int i = 0; i < count; ++i) { | |
108 if (aa[i]) { | |
109 const Sk4f d4 = load_from_dst<D>(dst[i]); | |
110 dst[i] = store_to_dst<D>(d4 * Sk4f((255 - aa[i]) * 1.0f/255)); | |
111 } | |
112 } | |
113 } else { | |
114 sk_memset64(dst, 0, count); | |
115 } | |
116 } | |
117 | |
118 const SkXfermode::D64Proc gProcs_Clear[] = { | |
119 clear<kU16_Dst>, clear<kU16_Dst>, | |
120 clear<kU16_Dst>, clear<kU16_Dst>, | |
121 clear<kF16_Dst>, clear<kF16_Dst>, | |
122 clear<kF16_Dst>, clear<kF16_Dst>, | |
123 }; | |
124 | |
125 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | |
126 | |
127 template <DstType D> void src_1(const SkXfermode*, uint64_t dst[], | |
128 const SkPM4f* src, int count, const SkAlpha aa[]
) { | |
129 const Sk4f s4 = unit_to_bias<D>(Sk4f::Load(src->fVec)); | |
130 if (aa) { | |
131 for (int i = 0; i < count; ++i) { | |
132 const Sk4f d4 = load_from_dst<D>(dst[i]); | |
133 dst[i] = store_to_dst<D>(lerp_by_coverage(s4, d4, aa[i])); | |
134 } | |
135 } else { | |
136 sk_memset64(dst, store_to_dst<D>(s4), count); | |
137 } | |
138 } | |
139 | |
140 template <DstType D> void src_n(const SkXfermode*, uint64_t dst[], | |
141 const SkPM4f src[], int count, const SkAlpha aa[
]) { | |
142 if (aa) { | |
143 for (int i = 0; i < count; ++i) { | |
144 const Sk4f s4 = unit_to_bias<D>(Sk4f::Load(src[i].fVec)); | |
145 const Sk4f d4 = load_from_dst<D>(dst[i]); | |
146 dst[i] = store_to_dst<D>(lerp_by_coverage(s4, d4, aa[i])); | |
147 } | |
148 } else { | |
149 for (int i = 0; i < count; ++i) { | |
150 const Sk4f s4 = unit_to_bias<D>(Sk4f::Load(src[i].fVec)); | |
151 dst[i] = store_to_dst<D>(s4); | |
152 } | |
153 } | |
154 } | |
155 | |
156 const SkXfermode::D64Proc gProcs_Src[] = { | |
157 src_n<kU16_Dst>, src_n<kU16_Dst>, | |
158 src_1<kU16_Dst>, src_1<kU16_Dst>, | |
159 src_n<kF16_Dst>, src_n<kF16_Dst>, | |
160 src_1<kF16_Dst>, src_1<kF16_Dst>, | |
161 }; | |
162 | |
163 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | |
164 | |
165 static void dst(const SkXfermode*, uint64_t*, const SkPM4f*, int count, const Sk
Alpha[]) {} | |
166 | |
167 const SkXfermode::D64Proc gProcs_Dst[] = { | |
168 dst, dst, dst, dst, dst, dst, dst, dst, | |
169 }; | |
170 | |
171 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | |
172 | |
173 template <DstType D> void srcover_1(const SkXfermode*, uint64_t dst[], | |
174 const SkPM4f* src, int count, const SkAlpha
aa[]) { | |
175 const Sk4f s4 = Sk4f::Load(src->fVec); | |
176 const Sk4f dst_scale = Sk4f(1 - get_alpha(s4)); | |
177 const Sk4f s4bias = unit_to_bias<D>(s4); | |
178 for (int i = 0; i < count; ++i) { | |
179 const Sk4f d4bias = load_from_dst<D>(dst[i]); | |
180 const Sk4f r4bias = s4bias + d4bias * dst_scale; | |
181 if (aa) { | |
182 dst[i] = store_to_dst<D>(lerp_by_coverage(r4bias, d4bias, aa[i])); | |
183 } else { | |
184 dst[i] = store_to_dst<D>(r4bias); | |
185 } | |
186 } | |
187 } | |
188 | |
189 template <DstType D> void srcover_n(const SkXfermode*, uint64_t dst[], | |
190 const SkPM4f src[], int count, const SkAlpha
aa[]) { | |
191 for (int i = 0; i < count; ++i) { | |
192 const Sk4f s4 = Sk4f::Load(src[i].fVec); | |
193 const Sk4f dst_scale = Sk4f(1 - get_alpha(s4)); | |
194 const Sk4f s4bias = unit_to_bias<D>(s4); | |
195 const Sk4f d4bias = load_from_dst<D>(dst[i]); | |
196 const Sk4f r4bias = s4bias + d4bias * dst_scale; | |
197 if (aa) { | |
198 dst[i] = store_to_dst<D>(lerp_by_coverage(r4bias, d4bias, aa[i])); | |
199 } else { | |
200 dst[i] = store_to_dst<D>(r4bias); | |
201 } | |
202 } | |
203 } | |
204 | |
205 const SkXfermode::D64Proc gProcs_SrcOver[] = { | |
206 srcover_n<kU16_Dst>, src_n<kU16_Dst>, | |
207 srcover_1<kU16_Dst>, src_1<kU16_Dst>, | |
208 srcover_n<kF16_Dst>, src_n<kF16_Dst>, | |
209 srcover_1<kF16_Dst>, src_1<kF16_Dst>, | |
210 }; | |
211 | |
212 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | |
213 | |
214 static SkXfermode::D64Proc find_proc(SkXfermode::Mode mode, uint32_t flags) { | |
215 SkASSERT(0 == (flags & ~7)); | |
216 flags &= 7; | |
217 | |
218 switch (mode) { | |
219 case SkXfermode::kClear_Mode: return gProcs_Clear[flags]; | |
220 case SkXfermode::kSrc_Mode: return gProcs_Src[flags]; | |
221 case SkXfermode::kDst_Mode: return gProcs_Dst[flags]; | |
222 case SkXfermode::kSrcOver_Mode: return gProcs_SrcOver[flags]; | |
223 default: | |
224 break; | |
225 } | |
226 return gProcs_General[flags]; | |
227 } | |
228 | |
229 SkXfermode::D64Proc SkXfermode::onGetD64Proc(uint32_t flags) const { | |
230 SkASSERT(0 == (flags & ~7)); | |
231 flags &= 7; | |
232 | |
233 Mode mode; | |
234 return this->asMode(&mode) ? find_proc(mode, flags) : gProcs_General[flags]; | |
235 } | |
236 | |
237 SkXfermode::D64Proc SkXfermode::GetD64Proc(SkXfermode* xfer, uint32_t flags) { | |
238 return xfer ? xfer->onGetD64Proc(flags) : find_proc(SkXfermode::kSrcOver_Mod
e, flags); | |
239 } | |
OLD | NEW |