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

Side by Side Diff: src/opts/SkColorXform_opts.h

Issue 2147763002: Add capability for SkColorXform to output half floats (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Call swizzle fn 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
« no previous file with comments | « src/core/SkOpts.cpp ('k') | src/opts/SkOpts_sse41.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #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 "SkSRGB.h" 14 #include "SkSRGB.h"
15 #include "SkTemplates.h"
14 16
15 namespace SK_OPTS_NS { 17 namespace SK_OPTS_NS {
16 18
17 static Sk4f linear_to_2dot2(const Sk4f& x) { 19 static Sk4f linear_to_2dot2(const Sk4f& x) {
18 // x^(29/64) is a very good approximation of the true value, x^(1/2.2). 20 // x^(29/64) is a very good approximation of the true value, x^(1/2.2).
19 auto x2 = x.rsqrt(), // x^(-1/2) 21 auto x2 = x.rsqrt(), // x^(-1/2)
20 x32 = x2.rsqrt().rsqrt().rsqrt().rsqrt(), // x^(-1/32) 22 x32 = x2.rsqrt().rsqrt().rsqrt().rsqrt(), // x^(-1/32)
21 x64 = x32.rsqrt(); // x^(+1/64) 23 x64 = x32.rsqrt(); // x^(+1/64)
22 24
23 // 29 = 32 - 2 - 1 25 // 29 = 32 - 2 - 1
24 return 255.0f * x2.invert() * x32 * x64.invert(); 26 return 255.0f * x2.invert() * x32 * x64.invert();
25 } 27 }
26 28
27 static Sk4f clamp_0_to_255(const Sk4f& x) { 29 static Sk4f clamp_0_to_255(const Sk4f& x) {
28 // The order of the arguments is important here. We want to make sure that NaN 30 // The order of the arguments is important here. We want to make sure that NaN
29 // clamps to zero. Note that max(NaN, 0) = 0, while max(0, NaN) = NaN. 31 // clamps to zero. Note that max(NaN, 0) = 0, while max(0, NaN) = NaN.
30 return Sk4f::Min(Sk4f::Max(x, 0.0f), 255.0f); 32 return Sk4f::Min(Sk4f::Max(x, 0.0f), 255.0f);
31 } 33 }
32 34
33 enum DstGamma { 35 enum DstGamma {
36 // 8888
34 kSRGB_DstGamma, 37 kSRGB_DstGamma,
35 k2Dot2_DstGamma, 38 k2Dot2_DstGamma,
36 kTable_DstGamma, 39 kTable_DstGamma,
40
41 // F16
42 kLinear_DstGamma,
37 }; 43 };
38 44
39 template <DstGamma kDstGamma> 45 template <DstGamma kDstGamma>
40 static void color_xform_RGB1(uint32_t* dst, const uint32_t* src, int len, 46 static void color_xform_RGB1(void* dst, const uint32_t* src, int len,
41 const float* const srcTables[3], const float matrix [16], 47 const float* const srcTables[3], const float matrix [16],
42 const uint8_t* const dstTables[3]) { 48 const uint8_t* const dstTables[3]) {
43 Sk4f rXgXbX = Sk4f::Load(matrix + 0), 49 Sk4f rXgXbX = Sk4f::Load(matrix + 0),
44 rYgYbY = Sk4f::Load(matrix + 4), 50 rYgYbY = Sk4f::Load(matrix + 4),
45 rZgZbZ = Sk4f::Load(matrix + 8); 51 rZgZbZ = Sk4f::Load(matrix + 8);
46 52
47 if (len >= 4) { 53 if (len >= 4) {
48 Sk4f reds, greens, blues; 54 Sk4f reds, greens, blues;
49 auto load_next_4 = [&reds, &greens, &blues, &src, &len, &srcTables] { 55 auto load_next_4 = [&reds, &greens, &blues, &src, &len, &srcTables] {
50 reds = Sk4f{srcTables[0][(src[0] >> 0) & 0xFF], 56 reds = Sk4f{srcTables[0][(src[0] >> 0) & 0xFF],
(...skipping 26 matching lines...) Expand all
77 (kSRGB_DstGamma == kDstGamma) ? sk_linear_to_srgb : line ar_to_2dot2; 83 (kSRGB_DstGamma == kDstGamma) ? sk_linear_to_srgb : line ar_to_2dot2;
78 84
79 dstReds = linear_to_curve(dstReds); 85 dstReds = linear_to_curve(dstReds);
80 dstGreens = linear_to_curve(dstGreens); 86 dstGreens = linear_to_curve(dstGreens);
81 dstBlues = linear_to_curve(dstBlues); 87 dstBlues = linear_to_curve(dstBlues);
82 88
83 dstReds = clamp_0_to_255(dstReds); 89 dstReds = clamp_0_to_255(dstReds);
84 dstGreens = clamp_0_to_255(dstGreens); 90 dstGreens = clamp_0_to_255(dstGreens);
85 dstBlues = clamp_0_to_255(dstBlues); 91 dstBlues = clamp_0_to_255(dstBlues);
86 92
87 auto rgba = (Sk4f_round(dstReds) ) 93 auto rgba = (Sk4f_round(dstReds) << SK_R32_SHIFT)
88 | (Sk4f_round(dstGreens) << 8) 94 | (Sk4f_round(dstGreens) << SK_G32_SHIFT)
89 | (Sk4f_round(dstBlues) << 16) 95 | (Sk4f_round(dstBlues) << SK_B32_SHIFT)
90 | (Sk4i{ 0xFF << 24}); 96 | (Sk4i{ 0xFF << SK_A32_SHIFT});
91 rgba.store(dst); 97 rgba.store((uint32_t*) dst);
92 } else { 98
99 dst = SkTAddOffset<void>(dst, 4 * sizeof(uint32_t));
100 } else if (kTable_DstGamma == kDstGamma) {
93 Sk4f scaledReds = Sk4f::Min(Sk4f::Max(1023.0f * dstReds, 0.0 f), 1023.0f); 101 Sk4f scaledReds = Sk4f::Min(Sk4f::Max(1023.0f * dstReds, 0.0 f), 1023.0f);
94 Sk4f scaledGreens = Sk4f::Min(Sk4f::Max(1023.0f * dstGreens, 0.0 f), 1023.0f); 102 Sk4f scaledGreens = Sk4f::Min(Sk4f::Max(1023.0f * dstGreens, 0.0 f), 1023.0f);
95 Sk4f scaledBlues = Sk4f::Min(Sk4f::Max(1023.0f * dstBlues, 0.0 f), 1023.0f); 103 Sk4f scaledBlues = Sk4f::Min(Sk4f::Max(1023.0f * dstBlues, 0.0 f), 1023.0f);
96 104
97 Sk4i indicesReds = Sk4f_round(scaledReds); 105 Sk4i indicesReds = Sk4f_round(scaledReds);
98 Sk4i indicesGreens = Sk4f_round(scaledGreens); 106 Sk4i indicesGreens = Sk4f_round(scaledGreens);
99 Sk4i indicesBlues = Sk4f_round(scaledBlues); 107 Sk4i indicesBlues = Sk4f_round(scaledBlues);
100 108
101 dst[0] = dstTables[0][indicesReds [0]] 109 uint32_t* dst32 = (uint32_t*) dst;
102 | dstTables[1][indicesGreens[0]] << 8 110 dst32[0] = dstTables[0][indicesReds [0]] << SK_R32_SHIFT
103 | dstTables[2][indicesBlues [0]] << 16 111 | dstTables[1][indicesGreens[0]] << SK_G32_SHIFT
104 | 0xFF << 24; 112 | dstTables[2][indicesBlues [0]] << SK_B32_SHIFT
105 dst[1] = dstTables[0][indicesReds [1]] 113 | 0xFF << SK_A32_SHIFT;
106 | dstTables[1][indicesGreens[1]] << 8 114 dst32[1] = dstTables[0][indicesReds [1]] << SK_R32_SHIFT
107 | dstTables[2][indicesBlues [1]] << 16 115 | dstTables[1][indicesGreens[1]] << SK_G32_SHIFT
108 | 0xFF << 24; 116 | dstTables[2][indicesBlues [1]] << SK_B32_SHIFT
109 dst[2] = dstTables[0][indicesReds [2]] 117 | 0xFF << SK_A32_SHIFT;
110 | dstTables[1][indicesGreens[2]] << 8 118 dst32[2] = dstTables[0][indicesReds [2]] << SK_R32_SHIFT
111 | dstTables[2][indicesBlues [2]] << 16 119 | dstTables[1][indicesGreens[2]] << SK_G32_SHIFT
112 | 0xFF << 24; 120 | dstTables[2][indicesBlues [2]] << SK_B32_SHIFT
113 dst[3] = dstTables[0][indicesReds [3]] 121 | 0xFF << SK_A32_SHIFT;
114 | dstTables[1][indicesGreens[3]] << 8 122 dst32[3] = dstTables[0][indicesReds [3]] << SK_R32_SHIFT
115 | dstTables[2][indicesBlues [3]] << 16 123 | dstTables[1][indicesGreens[3]] << SK_G32_SHIFT
116 | 0xFF << 24; 124 | dstTables[2][indicesBlues [3]] << SK_B32_SHIFT
125 | 0xFF << SK_A32_SHIFT;
126
127 dst = SkTAddOffset<void>(dst, 4 * sizeof(uint32_t));
128 } else {
129 // FIXME (msarett):
130 // Can we do better here? Should we store half floats as planar ?
131 // Should we write Intel/Arm specific code? Should we add a tra nspose
132 // function to SkNx? Should we rewrite the algorithm to be inte rleaved?
133 uint64_t* dst64 = (uint64_t*) dst;
134 dst64[0] = SkFloatToHalf_finite(Sk4f(dstReds[0], dstGreens[0], d stBlues[0], 1.0f));
135 dst64[1] = SkFloatToHalf_finite(Sk4f(dstReds[1], dstGreens[1], d stBlues[1], 1.0f));
136 dst64[2] = SkFloatToHalf_finite(Sk4f(dstReds[2], dstGreens[2], d stBlues[2], 1.0f));
137 dst64[3] = SkFloatToHalf_finite(Sk4f(dstReds[3], dstGreens[3], d stBlues[3], 1.0f));
138
139 dst = SkTAddOffset<void>(dst, 4 * sizeof(uint64_t));
117 } 140 }
118
119 dst += 4;
120 }; 141 };
121 142
122 load_next_4(); 143 load_next_4();
123 144
124 while (len >= 4) { 145 while (len >= 4) {
125 transform_4(); 146 transform_4();
126 load_next_4(); 147 load_next_4();
127 store_4(); 148 store_4();
128 } 149 }
129 150
130 transform_4(); 151 transform_4();
131 store_4(); 152 store_4();
132 } 153 }
133 154
134 while (len > 0) { 155 while (len > 0) {
135 // Splat r,g,b across a register each. 156 // Splat r,g,b across a register each.
136 auto r = Sk4f{srcTables[0][(*src >> 0) & 0xFF]}, 157 auto r = Sk4f{srcTables[0][(*src >> 0) & 0xFF]},
137 g = Sk4f{srcTables[1][(*src >> 8) & 0xFF]}, 158 g = Sk4f{srcTables[1][(*src >> 8) & 0xFF]},
138 b = Sk4f{srcTables[2][(*src >> 16) & 0xFF]}; 159 b = Sk4f{srcTables[2][(*src >> 16) & 0xFF]};
139 160
140 // Apply transformation matrix to dst gamut.
141 auto dstPixel = rXgXbX*r + rYgYbY*g + rZgZbZ*b; 161 auto dstPixel = rXgXbX*r + rYgYbY*g + rZgZbZ*b;
142 162
143 if (kSRGB_DstGamma == kDstGamma || k2Dot2_DstGamma == kDstGamma) { 163 if (kSRGB_DstGamma == kDstGamma || k2Dot2_DstGamma == kDstGamma) {
144 Sk4f (*linear_to_curve)(const Sk4f&) = 164 Sk4f (*linear_to_curve)(const Sk4f&) =
145 (kSRGB_DstGamma == kDstGamma) ? sk_linear_to_srgb : linear_t o_2dot2; 165 (kSRGB_DstGamma == kDstGamma) ? sk_linear_to_srgb : linear_t o_2dot2;
146 166
147 dstPixel = linear_to_curve(dstPixel); 167 dstPixel = linear_to_curve(dstPixel);
148 168
149 dstPixel = clamp_0_to_255(dstPixel); 169 dstPixel = clamp_0_to_255(dstPixel);
150 170
151 uint32_t rgba; 171 uint32_t rgba;
152 SkNx_cast<uint8_t>(Sk4f_round(dstPixel)).store(&rgba); 172 SkNx_cast<uint8_t>(Sk4f_round(dstPixel)).store(&rgba);
153 rgba |= 0xFF000000; 173 rgba |= 0xFF000000;
154 *dst = rgba; 174 *((uint32_t*) dst) = SkSwizzle_RGBA_to_PMColor(rgba);
155 } else { 175 dst = SkTAddOffset<void>(dst, sizeof(uint32_t));
176 } else if (kTable_DstGamma == kDstGamma) {
156 Sk4f scaledPixel = Sk4f::Min(Sk4f::Max(1023.0f * dstPixel, 0.0f), 10 23.0f); 177 Sk4f scaledPixel = Sk4f::Min(Sk4f::Max(1023.0f * dstPixel, 0.0f), 10 23.0f);
157 178
158 Sk4i indices = Sk4f_round(scaledPixel); 179 Sk4i indices = Sk4f_round(scaledPixel);
159 180
160 *dst = dstTables[0][indices[0]] 181 *((uint32_t*) dst) = dstTables[0][indices[0]] << SK_R32_SHIFT
161 | dstTables[1][indices[1]] << 8 182 | dstTables[1][indices[1]] << SK_G32_SHIFT
162 | dstTables[2][indices[2]] << 16 183 | dstTables[2][indices[2]] << SK_B32_SHIFT
163 | 0xFF << 24; 184 | 0xFF << SK_A32_SHIFT;
185
186 dst = SkTAddOffset<void>(dst, sizeof(uint32_t));
187 } else {
188 uint64_t rgba = SkFloatToHalf_finite(dstPixel);
189
190 // Set alpha to 1.0
191 rgba |= 0x3C00000000000000;
192 *((uint64_t*) dst) = rgba;
193 dst = SkTAddOffset<void>(dst, sizeof(uint64_t));
164 } 194 }
165 195
166 dst += 1;
167 src += 1; 196 src += 1;
168 len -= 1; 197 len -= 1;
169 } 198 }
170 } 199 }
171 200
172 static void color_xform_RGB1_to_2dot2(uint32_t* dst, const uint32_t* src, int le n, 201 static void color_xform_RGB1_to_2dot2(uint32_t* dst, const uint32_t* src, int le n,
173 const float* const srcTables[3], const flo at matrix[16]) { 202 const float* const srcTables[3], const flo at matrix[12]) {
174 color_xform_RGB1<k2Dot2_DstGamma>(dst, src, len, srcTables, matrix, nullptr) ; 203 color_xform_RGB1<k2Dot2_DstGamma>(dst, src, len, srcTables, matrix, nullptr) ;
175 } 204 }
176 205
177 static void color_xform_RGB1_to_srgb(uint32_t* dst, const uint32_t* src, int len , 206 static void color_xform_RGB1_to_srgb(uint32_t* dst, const uint32_t* src, int len ,
178 const float* const srcTables[3], const floa t matrix[16]) { 207 const float* const srcTables[3], const floa t matrix[12]) {
179 color_xform_RGB1<kSRGB_DstGamma>(dst, src, len, srcTables, matrix, nullptr); 208 color_xform_RGB1<kSRGB_DstGamma>(dst, src, len, srcTables, matrix, nullptr);
180 } 209 }
181 210
182 static void color_xform_RGB1_to_table(uint32_t* dst, const uint32_t* src, int le n, 211 static void color_xform_RGB1_to_table(uint32_t* dst, const uint32_t* src, int le n,
183 const float* const srcTables[3], const flo at matrix[16], 212 const float* const srcTables[3], const flo at matrix[12],
184 const uint8_t* const dstTables[3]) { 213 const uint8_t* const dstTables[3]) {
185 color_xform_RGB1<kTable_DstGamma>(dst, src, len, srcTables, matrix, dstTable s); 214 color_xform_RGB1<kTable_DstGamma>(dst, src, len, srcTables, matrix, dstTable s);
186 } 215 }
187 216
217 static void color_xform_RGB1_to_linear(uint64_t* dst, const uint32_t* src, int l en,
218 const float* const srcTables[3], const fl oat matrix[12]) {
219 color_xform_RGB1<kLinear_DstGamma>(dst, src, len, srcTables, matrix, nullptr );
220 }
221
188 } // namespace SK_OPTS_NS 222 } // namespace SK_OPTS_NS
189 223
190 #endif // SkColorXform_opts_DEFINED 224 #endif // SkColorXform_opts_DEFINED
OLDNEW
« no previous file with comments | « src/core/SkOpts.cpp ('k') | src/opts/SkOpts_sse41.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698