OLD | NEW |
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 #ifndef SkColorXform_opts_DEFINED | 8 #ifndef SkColorXform_opts_DEFINED |
9 #define SkColorXform_opts_DEFINED | 9 #define SkColorXform_opts_DEFINED |
10 | 10 |
11 #include "SkNx.h" | 11 #include "SkNx.h" |
12 #include "SkColorPriv.h" | 12 #include "SkColorPriv.h" |
13 #include "SkHalf.h" | 13 #include "SkHalf.h" |
14 #include "SkSRGB.h" | 14 #include "SkSRGB.h" |
15 #include "SkTemplates.h" | 15 #include "SkTemplates.h" |
16 | 16 |
17 namespace SK_OPTS_NS { | 17 namespace SK_OPTS_NS { |
18 | 18 |
19 // Strange that we need a wrapper on SkNx_cast to use as a function ptr. | |
20 static Sk4i Sk4f_trunc(const Sk4f& x) { | |
21 return SkNx_cast<int>(x); | |
22 } | |
23 | |
24 static Sk4f linear_to_2dot2(const Sk4f& x) { | |
25 // x^(29/64) is a very good approximation of the true value, x^(1/2.2). | |
26 auto x2 = x.rsqrt(), // x^(-1/2) | |
27 x32 = x2.rsqrt().rsqrt().rsqrt().rsqrt(), // x^(-1/32) | |
28 x64 = x32.rsqrt(); // x^(+1/64) | |
29 | |
30 // 29 = 32 - 2 - 1 | |
31 return 255.0f * x2.invert() * x32 * x64.invert(); | |
32 } | |
33 | |
34 enum DstGamma { | 19 enum DstGamma { |
35 // 8888 | 20 // 8888 |
36 kSRGB_DstGamma, | 21 kSRGB_DstGamma, |
37 k2Dot2_DstGamma, | |
38 kTable_DstGamma, | 22 kTable_DstGamma, |
39 | 23 |
40 // F16 | 24 // F16 |
41 kLinear_DstGamma, | 25 kLinear_DstGamma, |
42 }; | 26 }; |
43 | 27 |
44 template <DstGamma kDstGamma, bool kSwapRB> | 28 template <DstGamma kDstGamma, bool kSwapRB> |
45 static void color_xform_RGB1(void* dst, const uint32_t* src, int len, | 29 static void color_xform_RGB1(void* dst, const uint32_t* src, int len, |
46 const float* const srcTables[3], const float matrix
[16], | 30 const float* const srcTables[3], const float matrix
[16], |
47 const uint8_t* const dstTables[3]) { | 31 const uint8_t* const dstTables[3]) { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 Sk4f dstReds, dstGreens, dstBlues; | 65 Sk4f dstReds, dstGreens, dstBlues; |
82 auto transform_4 = [&reds, &greens, &blues, &dstReds, &dstGreens, &dstBl
ues, &rXgXbX, | 66 auto transform_4 = [&reds, &greens, &blues, &dstReds, &dstGreens, &dstBl
ues, &rXgXbX, |
83 &rYgYbY, &rZgZbZ, &rTgTbT] { | 67 &rYgYbY, &rZgZbZ, &rTgTbT] { |
84 dstReds = rXgXbX[0]*reds + rYgYbY[0]*greens + rZgZbZ[0]*blues + rT
gTbT[0]; | 68 dstReds = rXgXbX[0]*reds + rYgYbY[0]*greens + rZgZbZ[0]*blues + rT
gTbT[0]; |
85 dstGreens = rXgXbX[1]*reds + rYgYbY[1]*greens + rZgZbZ[1]*blues + rT
gTbT[1]; | 69 dstGreens = rXgXbX[1]*reds + rYgYbY[1]*greens + rZgZbZ[1]*blues + rT
gTbT[1]; |
86 dstBlues = rXgXbX[2]*reds + rYgYbY[2]*greens + rZgZbZ[2]*blues + rT
gTbT[2]; | 70 dstBlues = rXgXbX[2]*reds + rYgYbY[2]*greens + rZgZbZ[2]*blues + rT
gTbT[2]; |
87 }; | 71 }; |
88 | 72 |
89 auto store_4 = [&dstReds, &dstGreens, &dstBlues, &dst, &dstTables, kRShi
ft, kGShift, | 73 auto store_4 = [&dstReds, &dstGreens, &dstBlues, &dst, &dstTables, kRShi
ft, kGShift, |
90 kBShift, kAShift] { | 74 kBShift, kAShift] { |
91 if (kSRGB_DstGamma == kDstGamma || k2Dot2_DstGamma == kDstGamma) { | 75 if (kSRGB_DstGamma == kDstGamma) { |
92 Sk4f (*linear_to_curve)(const Sk4f&) = (kSRGB_DstGamma == kDstGa
mma) ? | 76 dstReds = sk_linear_to_srgb_needs_trunc(dstReds); |
93 sk_linear_to_srgb_needs_trunc : linear_to_2dot2; | 77 dstGreens = sk_linear_to_srgb_needs_trunc(dstGreens); |
94 Sk4i (*float_to_int)(const Sk4f&) = (kSRGB_DstGamma == kDstGamma
) ? | 78 dstBlues = sk_linear_to_srgb_needs_trunc(dstBlues); |
95 Sk4f_trunc : Sk4f_round; | |
96 | |
97 dstReds = linear_to_curve(dstReds); | |
98 dstGreens = linear_to_curve(dstGreens); | |
99 dstBlues = linear_to_curve(dstBlues); | |
100 | 79 |
101 dstReds = sk_clamp_0_255(dstReds); | 80 dstReds = sk_clamp_0_255(dstReds); |
102 dstGreens = sk_clamp_0_255(dstGreens); | 81 dstGreens = sk_clamp_0_255(dstGreens); |
103 dstBlues = sk_clamp_0_255(dstBlues); | 82 dstBlues = sk_clamp_0_255(dstBlues); |
104 | 83 |
105 auto rgba = (float_to_int(dstReds) << kRShift) | 84 auto rgba = (SkNx_cast<int>(dstReds) << kRShift) |
106 | (float_to_int(dstGreens) << kGShift) | 85 | (SkNx_cast<int>(dstGreens) << kGShift) |
107 | (float_to_int(dstBlues) << kBShift) | 86 | (SkNx_cast<int>(dstBlues) << kBShift) |
108 | (Sk4i{0xFF} << kAShift); | 87 | (Sk4i{0xFF} << kAShift); |
109 rgba.store((uint32_t*) dst); | 88 rgba.store((uint32_t*) dst); |
110 | 89 |
111 dst = SkTAddOffset<void>(dst, 4 * sizeof(uint32_t)); | 90 dst = SkTAddOffset<void>(dst, 4 * sizeof(uint32_t)); |
112 } else if (kTable_DstGamma == kDstGamma) { | 91 } else if (kTable_DstGamma == kDstGamma) { |
113 Sk4f scaledReds = Sk4f::Min(Sk4f::Max(1023.0f * dstReds, 0.0
f), 1023.0f); | 92 Sk4f scaledReds = Sk4f::Min(Sk4f::Max(1023.0f * dstReds, 0.0
f), 1023.0f); |
114 Sk4f scaledGreens = Sk4f::Min(Sk4f::Max(1023.0f * dstGreens, 0.0
f), 1023.0f); | 93 Sk4f scaledGreens = Sk4f::Min(Sk4f::Max(1023.0f * dstGreens, 0.0
f), 1023.0f); |
115 Sk4f scaledBlues = Sk4f::Min(Sk4f::Max(1023.0f * dstBlues, 0.0
f), 1023.0f); | 94 Sk4f scaledBlues = Sk4f::Min(Sk4f::Max(1023.0f * dstBlues, 0.0
f), 1023.0f); |
116 | 95 |
117 Sk4i indicesReds = Sk4f_round(scaledReds); | 96 Sk4i indicesReds = Sk4f_round(scaledReds); |
118 Sk4i indicesGreens = Sk4f_round(scaledGreens); | 97 Sk4i indicesGreens = Sk4f_round(scaledGreens); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 } | 138 } |
160 | 139 |
161 while (len > 0) { | 140 while (len > 0) { |
162 // Splat r,g,b across a register each. | 141 // Splat r,g,b across a register each. |
163 auto r = Sk4f{srcTables[0][(*src >> 0) & 0xFF]}, | 142 auto r = Sk4f{srcTables[0][(*src >> 0) & 0xFF]}, |
164 g = Sk4f{srcTables[1][(*src >> 8) & 0xFF]}, | 143 g = Sk4f{srcTables[1][(*src >> 8) & 0xFF]}, |
165 b = Sk4f{srcTables[2][(*src >> 16) & 0xFF]}; | 144 b = Sk4f{srcTables[2][(*src >> 16) & 0xFF]}; |
166 | 145 |
167 auto dstPixel = rXgXbX*r + rYgYbY*g + rZgZbZ*b + rTgTbT; | 146 auto dstPixel = rXgXbX*r + rYgYbY*g + rZgZbZ*b + rTgTbT; |
168 | 147 |
169 if (kSRGB_DstGamma == kDstGamma || k2Dot2_DstGamma == kDstGamma) { | 148 if (kSRGB_DstGamma == kDstGamma) { |
170 Sk4f (*linear_to_curve)(const Sk4f&) = (kSRGB_DstGamma == kDstGamma)
? | 149 dstPixel = sk_clamp_0_255(sk_linear_to_srgb_needs_trunc(dstPixel)); |
171 sk_linear_to_srgb_needs_trunc : linear_to_2dot2; | |
172 Sk4i (*float_to_int)(const Sk4f&) = (kSRGB_DstGamma == kDstGamma) ? | |
173 Sk4f_trunc : Sk4f_round; | |
174 | |
175 dstPixel = sk_clamp_0_255(linear_to_curve(dstPixel)); | |
176 | 150 |
177 uint32_t rgba; | 151 uint32_t rgba; |
178 SkNx_cast<uint8_t>(float_to_int(dstPixel)).store(&rgba); | 152 SkNx_cast<uint8_t>(SkNx_cast<int>(dstPixel)).store(&rgba); |
179 rgba |= 0xFF000000; | 153 rgba |= 0xFF000000; |
180 if (kSwapRB) { | 154 if (kSwapRB) { |
181 *((uint32_t*) dst) = SkSwizzle_RB(rgba); | 155 *((uint32_t*) dst) = SkSwizzle_RB(rgba); |
182 } else { | 156 } else { |
183 *((uint32_t*) dst) = rgba; | 157 *((uint32_t*) dst) = rgba; |
184 } | 158 } |
185 dst = SkTAddOffset<void>(dst, sizeof(uint32_t)); | 159 dst = SkTAddOffset<void>(dst, sizeof(uint32_t)); |
186 } else if (kTable_DstGamma == kDstGamma) { | 160 } else if (kTable_DstGamma == kDstGamma) { |
187 Sk4f scaledPixel = Sk4f::Min(Sk4f::Max(1023.0f * dstPixel, 0.0f), 10
23.0f); | 161 Sk4f scaledPixel = Sk4f::Min(Sk4f::Max(1023.0f * dstPixel, 0.0f), 10
23.0f); |
188 | 162 |
(...skipping 11 matching lines...) Expand all Loading... |
200 rgba |= static_cast<uint64_t>(SK_Half1) << 48; | 174 rgba |= static_cast<uint64_t>(SK_Half1) << 48; |
201 *((uint64_t*) dst) = rgba; | 175 *((uint64_t*) dst) = rgba; |
202 dst = SkTAddOffset<void>(dst, sizeof(uint64_t)); | 176 dst = SkTAddOffset<void>(dst, sizeof(uint64_t)); |
203 } | 177 } |
204 | 178 |
205 src += 1; | 179 src += 1; |
206 len -= 1; | 180 len -= 1; |
207 } | 181 } |
208 } | 182 } |
209 | 183 |
210 static void color_xform_RGB1_to_2dot2(uint32_t* dst, const uint32_t* src, int le
n, | |
211 const float* const srcTables[3], const flo
at matrix[16]) { | |
212 color_xform_RGB1<k2Dot2_DstGamma, false>(dst, src, len, srcTables, matrix, n
ullptr); | |
213 } | |
214 | |
215 static void color_xform_RGB1_to_srgb(uint32_t* dst, const uint32_t* src, int len
, | 184 static void color_xform_RGB1_to_srgb(uint32_t* dst, const uint32_t* src, int len
, |
216 const float* const srcTables[3], const floa
t matrix[16]) { | 185 const float* const srcTables[3], const floa
t matrix[16]) { |
217 color_xform_RGB1<kSRGB_DstGamma, false>(dst, src, len, srcTables, matrix, nu
llptr); | 186 color_xform_RGB1<kSRGB_DstGamma, false>(dst, src, len, srcTables, matrix, nu
llptr); |
218 } | 187 } |
219 | 188 |
220 static void color_xform_RGB1_to_table(uint32_t* dst, const uint32_t* src, int le
n, | 189 static void color_xform_RGB1_to_table(uint32_t* dst, const uint32_t* src, int le
n, |
221 const float* const srcTables[3], const flo
at matrix[16], | 190 const float* const srcTables[3], const flo
at matrix[16], |
222 const uint8_t* const dstTables[3]) { | 191 const uint8_t* const dstTables[3]) { |
223 color_xform_RGB1<kTable_DstGamma, false>(dst, src, len, srcTables, matrix, d
stTables); | 192 color_xform_RGB1<kTable_DstGamma, false>(dst, src, len, srcTables, matrix, d
stTables); |
224 } | 193 } |
225 | 194 |
226 static void color_xform_RGB1_to_linear(uint64_t* dst, const uint32_t* src, int l
en, | 195 static void color_xform_RGB1_to_linear(uint64_t* dst, const uint32_t* src, int l
en, |
227 const float* const srcTables[3], const fl
oat matrix[16]) { | 196 const float* const srcTables[3], const fl
oat matrix[16]) { |
228 color_xform_RGB1<kLinear_DstGamma, false>(dst, src, len, srcTables, matrix,
nullptr); | 197 color_xform_RGB1<kLinear_DstGamma, false>(dst, src, len, srcTables, matrix,
nullptr); |
229 } | 198 } |
230 | 199 |
231 static void color_xform_RGB1_to_2dot2_swaprb(uint32_t* dst, const uint32_t* src,
int len, | |
232 const float* const srcTables[3], | |
233 const float matrix[16]) { | |
234 color_xform_RGB1<k2Dot2_DstGamma, true>(dst, src, len, srcTables, matrix, nu
llptr); | |
235 } | |
236 | |
237 static void color_xform_RGB1_to_srgb_swaprb(uint32_t* dst, const uint32_t* src,
int len, | 200 static void color_xform_RGB1_to_srgb_swaprb(uint32_t* dst, const uint32_t* src,
int len, |
238 const float* const srcTables[3], | 201 const float* const srcTables[3], |
239 const float matrix[16]) { | 202 const float matrix[16]) { |
240 color_xform_RGB1<kSRGB_DstGamma, true>(dst, src, len, srcTables, matrix, nul
lptr); | 203 color_xform_RGB1<kSRGB_DstGamma, true>(dst, src, len, srcTables, matrix, nul
lptr); |
241 } | 204 } |
242 | 205 |
243 static void color_xform_RGB1_to_table_swaprb(uint32_t* dst, const uint32_t* src,
int len, | 206 static void color_xform_RGB1_to_table_swaprb(uint32_t* dst, const uint32_t* src,
int len, |
244 const float* const srcTables[3], | 207 const float* const srcTables[3], |
245 const float matrix[16], | 208 const float matrix[16], |
246 const uint8_t* const dstTables[3])
{ | 209 const uint8_t* const dstTables[3])
{ |
247 color_xform_RGB1<kTable_DstGamma, true>(dst, src, len, srcTables, matrix, ds
tTables); | 210 color_xform_RGB1<kTable_DstGamma, true>(dst, src, len, srcTables, matrix, ds
tTables); |
248 } | 211 } |
249 | 212 |
250 } // namespace SK_OPTS_NS | 213 } // namespace SK_OPTS_NS |
251 | 214 |
252 #endif // SkColorXform_opts_DEFINED | 215 #endif // SkColorXform_opts_DEFINED |
OLD | NEW |