Debian Codegraph @

src/core/SkConvertPixels.cpp

raw
1 /*
2 * Copyright 2014 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 #include "src/core/SkConvertPixels.h"
8
9 #include "include/core/SkColorType.h"
10 #include "include/core/SkImageInfo.h"
11 #include "include/core/SkSize.h"
12 #include "include/private/base/SkAssert.h"
13 #include "include/private/base/SkTPin.h"
14 #include "include/private/base/SkTemplates.h"
15 #include "src/base/SkHalf.h"
16 #include "src/base/SkRectMemcpy.h"
17 #include "src/core/SkColorData.h"
18 #include "src/core/SkColorSpaceXformSteps.h"
19 #include "src/core/SkImageInfoPriv.h"
20 #include "src/core/SkRasterPipeline.h"
21 #include "src/core/SkRasterPipelineOpContexts.h"
22 #include "src/core/SkSwizzlePriv.h"
23
24 #include <cstdint>
25 #include <cstring>
26 #include <initializer_list>
27
28 static bool rect_memcpy(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
29 const SkImageInfo& srcInfo, const void* srcPixels, size_t srcRB,
30 const SkColorSpaceXformSteps& steps) {
31 // We can copy the pixels when no color type, alpha type, or color space changes.
32 if (dstInfo.colorType() != srcInfo.colorType()) {
33 return false;
34 }
35 if (dstInfo.colorType() != kAlpha_8_SkColorType
36 && steps.fFlags.mask() != 0b00000) {
37 return false;
38 }
39
40 SkRectMemcpy(dstPixels, dstRB,
41 srcPixels, srcRB, dstInfo.minRowBytes(), dstInfo.height());
42 return true;
43 }
44
45 static bool swizzle_or_premul(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
46 const SkImageInfo& srcInfo, const void* srcPixels, size_t srcRB,
47 const SkColorSpaceXformSteps& steps) {
48 auto is_8888 = [](SkColorType ct) {
49 return ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType;
50 };
51 if (!is_8888(dstInfo.colorType()) ||
52 !is_8888(srcInfo.colorType()) ||
53 steps.fFlags.linearize ||
54 steps.fFlags.gamut_transform ||
55 #if !defined(SK_ARM_HAS_NEON)
56 steps.fFlags.unpremul ||
57 #endif
58 steps.fFlags.encode) {
59 return false;
60 }
61
62 const bool swapRB = dstInfo.colorType() != srcInfo.colorType();
63
64 void (*fn)(uint32_t*, const uint32_t*, int) = nullptr;
65
66 if (steps.fFlags.premul) {
67 fn = swapRB ? SkOpts::RGBA_to_bgrA
68 : SkOpts::RGBA_to_rgbA;
69 } else if (steps.fFlags.unpremul) {
70 fn = swapRB ? SkOpts::rgbA_to_BGRA
71 : SkOpts::rgbA_to_RGBA;
72 } else {
73 // If we're not swizzling, we ought to have used rect_memcpy().
74 SkASSERT(swapRB);
75 fn = SkOpts::RGBA_to_BGRA;
76 }
77
78 for (int y = 0; y < dstInfo.height(); y++) {
79 fn((uint32_t*)dstPixels, (const uint32_t*)srcPixels, dstInfo.width());
80 dstPixels = SkTAddOffset<void>(dstPixels, dstRB);
81 srcPixels = SkTAddOffset<const void>(srcPixels, srcRB);
82 }
83 return true;
84 }
85
86 static bool convert_to_alpha8(const SkImageInfo& dstInfo, void* vdst, size_t dstRB,
87 const SkImageInfo& srcInfo, const void* src, size_t srcRB,
88 const SkColorSpaceXformSteps&) {
89 if (dstInfo.colorType() != kAlpha_8_SkColorType) {
90 return false;
91 }
92 auto dst = (uint8_t*)vdst;
93
94 switch (srcInfo.colorType()) {
95 case kUnknown_SkColorType:
96 case kAlpha_8_SkColorType: {
97 // Unknown should never happen.
98 // Alpha8 should have been handled by rect_memcpy().
99 SkASSERT(false);
100 return false;
101 }
102
103 case kA16_unorm_SkColorType: {
104 auto src16 = (const uint16_t*) src;
105 for (int y = 0; y < srcInfo.height(); y++) {
106 for (int x = 0; x < srcInfo.width(); x++) {
107 dst[x] = src16[x] >> 8;
108 }
109 dst = SkTAddOffset<uint8_t>(dst, dstRB);
110 src16 = SkTAddOffset<const uint16_t>(src16, srcRB);
111 }
112 return true;
113 }
114
115 case kGray_8_SkColorType:
116 case kRGB_565_SkColorType:
117 case kR8G8_unorm_SkColorType:
118 case kR16_unorm_SkColorType:
119 case kR16G16_unorm_SkColorType:
120 case kR16G16_float_SkColorType:
121 case kRGB_888x_SkColorType:
122 case kRGB_101010x_SkColorType:
123 case kBGR_101010x_SkColorType:
124 case kBGR_101010x_XR_SkColorType:
125 case kRGB_F16F16F16x_SkColorType:
126 case kR8_unorm_SkColorType: {
127 for (int y = 0; y < srcInfo.height(); ++y) {
128 memset(dst, 0xFF, srcInfo.width());
129 dst = SkTAddOffset<uint8_t>(dst, dstRB);
130 }
131 return true;
132 }
133
134 case kARGB_4444_SkColorType: {
135 auto src16 = (const uint16_t*) src;
136 for (int y = 0; y < srcInfo.height(); y++) {
137 for (int x = 0; x < srcInfo.width(); x++) {
138 dst[x] = SkPacked4444ToA32(src16[x]);
139 }
140 dst = SkTAddOffset<uint8_t>(dst, dstRB);
141 src16 = SkTAddOffset<const uint16_t>(src16, srcRB);
142 }
143 return true;
144 }
145
146 case kBGRA_8888_SkColorType:
147 case kRGBA_8888_SkColorType:
148 case kSRGBA_8888_SkColorType: {
149 auto src32 = (const uint32_t*) src;
150 for (int y = 0; y < srcInfo.height(); y++) {
151 for (int x = 0; x < srcInfo.width(); x++) {
152 dst[x] = src32[x] >> 24;
153 }
154 dst = SkTAddOffset<uint8_t>(dst, dstRB);
155 src32 = SkTAddOffset<const uint32_t>(src32, srcRB);
156 }
157 return true;
158 }
159
160 case kRGBA_1010102_SkColorType:
161 case kBGRA_1010102_SkColorType: {
162 auto src32 = (const uint32_t*) src;
163 for (int y = 0; y < srcInfo.height(); y++) {
164 for (int x = 0; x < srcInfo.width(); x++) {
165 dst[x] = (src32[x] >> 30) * 0x55;
166 }
167 dst = SkTAddOffset<uint8_t>(dst, dstRB);
168 src32 = SkTAddOffset<const uint32_t>(src32, srcRB);
169 }
170 return true;
171 }
172
173 case kRGBA_F16Norm_SkColorType:
174 case kRGBA_F16_SkColorType: {
175 auto src64 = (const uint64_t*) src;
176 for (int y = 0; y < srcInfo.height(); y++) {
177 for (int x = 0; x < srcInfo.width(); x++) {
178 dst[x] = (uint8_t) (255.0f * SkHalfToFloat(src64[x] >> 48));
179 }
180 dst = SkTAddOffset<uint8_t>(dst, dstRB);
181 src64 = SkTAddOffset<const uint64_t>(src64, srcRB);
182 }
183 return true;
184 }
185
186 case kRGBA_F32_SkColorType: {
187 auto rgba = (const float*)src;
188 for (int y = 0; y < srcInfo.height(); y++) {
189 for (int x = 0; x < srcInfo.width(); x++) {
190 dst[x] = (uint8_t)(255.0f * rgba[4*x+3]);
191 }
192 dst = SkTAddOffset<uint8_t>(dst, dstRB);
193 rgba = SkTAddOffset<const float>(rgba, srcRB);
194 }
195 return true;
196 }
197
198 case kA16_float_SkColorType: {
199 auto srcF16 = (const uint16_t*) src;
200 for (int y = 0; y < srcInfo.height(); y++) {
201 for (int x = 0; x < srcInfo.width(); x++) {
202 dst[x] = (uint8_t) (255.0f * SkHalfToFloat(srcF16[x]));
203 }
204 dst = SkTAddOffset<uint8_t>(dst, dstRB);
205 srcF16 = SkTAddOffset<const uint16_t>(srcF16, srcRB);
206 }
207 return true;
208 }
209
210 case kBGRA_10101010_XR_SkColorType: {
211 auto src64 = (const uint64_t*) src;
212 for (int y = 0; y < srcInfo.height(); y++) {
213 for (int x = 0; x < srcInfo.width(); x++) {
214 static constexpr int64_t kZero = 384;
215 static constexpr int64_t kRange = 510;
216 static constexpr int64_t kMaxU8 = 0xff;
217 static constexpr int64_t kMinU8 = 0x00;
218 static constexpr int64_t kDivisor = kRange / kMaxU8;
219 int64_t raw_alpha = src64[x] >> 54;
220 // f(384) = 0
221 // f(894) = 255
222 int64_t alpha =
223 SkTPin((raw_alpha - kZero) / kDivisor, kMinU8, kMaxU8);
224 dst[x] = static_cast<uint8_t>(alpha);
225 }
226 dst = SkTAddOffset<uint8_t>(dst, dstRB);
227 src64 = SkTAddOffset<const uint64_t>(src64, srcRB);
228 }
229 return true;
230 }
231 case kRGBA_10x6_SkColorType:
232 case kR16G16B16A16_unorm_SkColorType: {
233 auto src64 = (const uint64_t*) src;
234 for (int y = 0; y < srcInfo.height(); y++) {
235 for (int x = 0; x < srcInfo.width(); x++) {
236 dst[x] = (src64[x] >> 48) >> 8;
237 }
238 dst = SkTAddOffset<uint8_t>(dst, dstRB);
239 src64 = SkTAddOffset<const uint64_t>(src64, srcRB);
240 }
241 return true;
242 }
243 }
244 return false;
245 }
246
247 // Default: Use the pipeline.
248 static void convert_with_pipeline(const SkImageInfo& dstInfo, void* dstRow, int dstStride,
249 const SkImageInfo& srcInfo, const void* srcRow, int srcStride,
250 const SkColorSpaceXformSteps& steps) {
251 SkRasterPipelineContexts::MemoryCtx src = {const_cast<void*>(srcRow), srcStride},
252 dst = {dstRow, dstStride};
253
254 SkRasterPipeline_<256> pipeline;
255 pipeline.appendLoad(srcInfo.colorType(), &src);
256 steps.apply(&pipeline);
257 pipeline.appendStore(dstInfo.colorType(), &dst);
258 pipeline.run(0,0, srcInfo.width(), srcInfo.height());
259 }
260
261 bool SkConvertPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
262 const SkImageInfo& srcInfo, const void* srcPixels, size_t srcRB) {
263 SkASSERT(dstInfo.dimensions() == srcInfo.dimensions());
264 SkASSERT(SkImageInfoValidConversion(dstInfo, srcInfo));
265
266 int srcStride = (int)(srcRB / srcInfo.bytesPerPixel());
267 int dstStride = (int)(dstRB / dstInfo.bytesPerPixel());
268 if ((size_t)srcStride * srcInfo.bytesPerPixel() != srcRB ||
269 (size_t)dstStride * dstInfo.bytesPerPixel() != dstRB) {
270 return false;
271 }
272
273 SkColorSpaceXformSteps steps{srcInfo.colorSpace(), srcInfo.alphaType(),
274 dstInfo.colorSpace(), dstInfo.alphaType()};
275
276 for (auto fn : {rect_memcpy, swizzle_or_premul, convert_to_alpha8}) {
277 if (fn(dstInfo, dstPixels, dstRB, srcInfo, srcPixels, srcRB, steps)) {
278 return true;
279 }
280 }
281 convert_with_pipeline(dstInfo, dstPixels, dstStride, srcInfo, srcPixels, srcStride, steps);
282 return true;
283 }