Debian Codegraph @

src/core/SkPixmap.cpp

raw
1 /*
2 * Copyright 2015 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 "include/core/SkPixmap.h"
9
10 #include "include/core/SkAlphaType.h"
11 #include "include/core/SkColorSpace.h"
12 #include "include/core/SkColorType.h"
13 #include "include/core/SkUnPreMultiply.h"
14 #include "include/private/base/SkFloatingPoint.h"
15 #include "include/private/base/SkTPin.h"
16 #include "src/base/SkHalf.h"
17 #include "src/base/SkVx.h"
18 #include "src/core/SkColorData.h"
19 #include "src/core/SkColorPriv.h"
20 #include "src/core/SkConvertPixels.h"
21 #include "src/core/SkImageInfoPriv.h"
22 #include "src/core/SkMask.h"
23 #include "src/core/SkReadPixelsRec.h"
24 #include "src/core/SkSwizzlePriv.h"
25 #include "src/opts/SkMemset_opts.h"
26
27 #include <cstdint>
28 #include <cstring>
29 #include <iterator>
30 #include <utility>
31
32 void SkPixmap::reset() {
33 fPixels = nullptr;
34 fRowBytes = 0;
35 fInfo = SkImageInfo::MakeUnknown();
36 }
37
38 void SkPixmap::reset(const SkImageInfo& info, const void* addr, size_t rowBytes) {
39 if (addr) {
40 SkDEBUGCODE(info.validate());
41 SkASSERT(info.validRowBytes(rowBytes));
42 }
43 fPixels = addr;
44 fRowBytes = rowBytes;
45 fInfo = info;
46 }
47
48 bool SkPixmap::reset(const SkMask& src) {
49 if (SkMask::kA8_Format == src.fFormat) {
50 this->reset(SkImageInfo::MakeA8(src.fBounds.width(), src.fBounds.height()),
51 src.fImage, src.fRowBytes);
52 return true;
53 }
54 this->reset();
55 return false;
56 }
57
58 void SkPixmap::setColorSpace(sk_sp<SkColorSpace> cs) {
59 fInfo = fInfo.makeColorSpace(std::move(cs));
60 }
61
62 SkColorSpace* SkPixmap::colorSpace() const { return fInfo.colorSpace(); }
63
64 sk_sp<SkColorSpace> SkPixmap::refColorSpace() const { return fInfo.refColorSpace(); }
65
66 bool SkPixmap::extractSubset(SkPixmap* result, const SkIRect& subset) const {
67 SkIRect srcRect, r;
68 srcRect.setWH(this->width(), this->height());
69 if (!r.intersect(srcRect, subset)) {
70 return false; // r is empty (i.e. no intersection)
71 }
72
73 // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have
74 // exited above.
75 SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width()));
76 SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height()));
77
78 const void* pixels = nullptr;
79 if (fPixels) {
80 const size_t bpp = fInfo.bytesPerPixel();
81 pixels = (const uint8_t*)fPixels + r.fTop * fRowBytes + r.fLeft * bpp;
82 }
83 result->reset(fInfo.makeDimensions(r.size()), pixels, fRowBytes);
84 return true;
85 }
86
87 // This is the same as SkPixmap::addr(x,y), but this version gets inlined, while the public
88 // method does not. Perhaps we could bloat it so it can be inlined, but that would grow code-size
89 // everywhere, instead of just here (on behalf of getAlphaf()).
90 static const void* fast_getaddr(const SkPixmap& pm, int x, int y) {
91 x <<= SkColorTypeShiftPerPixel(pm.colorType());
92 return static_cast<const char*>(pm.addr()) + y * pm.rowBytes() + x;
93 }
94
95 float SkPixmap::getAlphaf(int x, int y) const {
96 SkASSERT(this->addr());
97 SkASSERT((unsigned)x < (unsigned)this->width());
98 SkASSERT((unsigned)y < (unsigned)this->height());
99
100 float value = 0;
101 const void* srcPtr = fast_getaddr(*this, x, y);
102
103 switch (this->colorType()) {
104 case kUnknown_SkColorType:
105 return 0;
106 case kGray_8_SkColorType:
107 case kR8G8_unorm_SkColorType:
108 case kR16_unorm_SkColorType:
109 case kR16G16_unorm_SkColorType:
110 case kR16G16_float_SkColorType:
111 case kRGB_565_SkColorType:
112 case kRGB_888x_SkColorType:
113 case kRGB_101010x_SkColorType:
114 case kBGR_101010x_SkColorType:
115 case kBGR_101010x_XR_SkColorType:
116 case kRGB_F16F16F16x_SkColorType:
117 case kR8_unorm_SkColorType:
118 return 1;
119 case kAlpha_8_SkColorType:
120 value = static_cast<const uint8_t*>(srcPtr)[0] * (1.0f/255);
121 break;
122 case kA16_unorm_SkColorType:
123 value = static_cast<const uint16_t*>(srcPtr)[0] * (1.0f/65535);
124 break;
125 case kA16_float_SkColorType: {
126 SkHalf half = static_cast<const SkHalf*>(srcPtr)[0];
127 value = SkHalfToFloat(half);
128 break;
129 }
130 case kARGB_4444_SkColorType: {
131 uint16_t u16 = static_cast<const uint16_t*>(srcPtr)[0];
132 value = SkGetPackedA4444(u16) * (1.0f/15);
133 break;
134 }
135 case kRGBA_8888_SkColorType:
136 case kBGRA_8888_SkColorType:
137 case kSRGBA_8888_SkColorType:
138 value = static_cast<const uint8_t*>(srcPtr)[3] * (1.0f/255);
139 break;
140 case kRGBA_1010102_SkColorType:
141 case kBGRA_1010102_SkColorType: {
142 uint32_t u32 = static_cast<const uint32_t*>(srcPtr)[0];
143 value = (u32 >> 30) * (1.0f/3);
144 break;
145 }
146 case kBGRA_10101010_XR_SkColorType: {
147 uint64_t u64 = static_cast<const uint64_t*>(srcPtr)[0];
148 value = ((u64 >> 54) - 384) / 510.f;
149 break;
150 }
151 case kRGBA_10x6_SkColorType: {
152 uint64_t u64 = static_cast<const uint64_t*>(srcPtr)[0];
153 value = (u64 >> 54) * (1.0f/1023);
154 break;
155 }
156 case kR16G16B16A16_unorm_SkColorType: {
157 uint64_t u64 = static_cast<const uint64_t*>(srcPtr)[0];
158 value = (u64 >> 48) * (1.0f/65535);
159 break;
160 }
161 case kRGBA_F16Norm_SkColorType:
162 case kRGBA_F16_SkColorType: {
163 value = from_half(skvx::half4::Load(srcPtr))[3];
164 break;
165 }
166 case kRGBA_F32_SkColorType:
167 value = static_cast<const float*>(srcPtr)[3];
168 break;
169 }
170 return value;
171 }
172
173 bool SkPixmap::readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
174 int x, int y) const {
175 if (!SkImageInfoValidConversion(dstInfo, fInfo)) {
176 return false;
177 }
178
179 SkReadPixelsRec rec(dstInfo, dstPixels, dstRB, x, y);
180 if (!rec.trim(fInfo.width(), fInfo.height())) {
181 return false;
182 }
183
184 const void* srcPixels = this->addr(rec.fX, rec.fY);
185 const SkImageInfo srcInfo = fInfo.makeDimensions(rec.fInfo.dimensions());
186 return SkConvertPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, srcInfo, srcPixels,
187 this->rowBytes());
188 }
189
190 SkColor SkPixmap::getColor(int x, int y) const {
191 SkASSERT(this->addr());
192 SkASSERT((unsigned)x < (unsigned)this->width());
193 SkASSERT((unsigned)y < (unsigned)this->height());
194
195 const bool needsUnpremul = (kPremul_SkAlphaType == fInfo.alphaType());
196 auto toColor = [needsUnpremul](uint32_t maybePremulColor) {
197 return needsUnpremul ? SkUnPreMultiply::PMColorToColor(maybePremulColor)
198 : SkSwizzle_BGRA_to_PMColor(maybePremulColor);
199 };
200
201 switch (this->colorType()) {
202 case kGray_8_SkColorType: {
203 uint8_t value = *this->addr8(x, y);
204 return SkColorSetRGB(value, value, value);
205 }
206 case kR8_unorm_SkColorType: {
207 return SkColorSetRGB(*this->addr8(x, y), 0, 0);
208 }
209 case kAlpha_8_SkColorType: {
210 return SkColorSetA(0, *this->addr8(x, y));
211 }
212 case kA16_unorm_SkColorType: {
213 return SkColorSetA(0, (*this->addr16(x, y)) * (255 / 65535.0f));
214 }
215 case kA16_float_SkColorType: {
216 return SkColorSetA(0, 255 * SkHalfToFloat(*this->addr16(x, y)));
217 }
218 case kRGB_565_SkColorType: {
219 return SkPixel16ToColor(*this->addr16(x, y));
220 }
221 case kARGB_4444_SkColorType: {
222 SkPMColor c = SkPixel4444ToPixel32(*this->addr16(x, y));
223 return toColor(c);
224 }
225 case kR8G8_unorm_SkColorType: {
226 uint16_t value = *this->addr16(x, y);
227 return SkColorSetRGB((uint8_t)(value & 0xffff), (uint8_t)((value >> 8) & 0xffff), 0);
228 }
229 case kR16_unorm_SkColorType: {
230 uint16_t value = *this->addr16(x, y);
231 return SkColorSetRGB(value * (255 / 65535.0f), 0, 0);
232 }
233 case kR16G16_unorm_SkColorType: {
234 uint32_t value = *this->addr32(x, y);
235 uint8_t r = ((value >> 0) & 0xffff) * (255.0f / 65535.0f),
236 g = ((value >> 16) & 0xffff) * (255.0f / 65535.0f);
237 return SkColorSetRGB(r, g, 0);
238 }
239 case kR16G16_float_SkColorType: {
240 uint32_t value = *this->addr32(x, y);
241 float r = SkHalfToFloat((uint16_t)(value >> 0) & 0xffff),
242 g = SkHalfToFloat((uint16_t)(value >> 16) & 0xffff);
243 return SkColorSetRGB((uint8_t)(255 * r), (uint8_t)(255 * g), 0);
244 }
245 case kRGB_888x_SkColorType: {
246 uint32_t value = *this->addr32(x, y);
247 return SkSwizzle_RB(value | 0xff000000);
248 }
249 case kBGRA_8888_SkColorType: {
250 uint32_t value = *this->addr32(x, y);
251 return toColor(SkSwizzle_BGRA_to_PMColor(value));
252 }
253 case kRGBA_8888_SkColorType: {
254 uint32_t value = *this->addr32(x, y);
255 return toColor(SkSwizzle_RGBA_to_PMColor(value));
256 }
257 case kSRGBA_8888_SkColorType: {
258 uint32_t value = *this->addr32(x, y);
259 float r = ((value >> 0) & 0xff) * (1/255.0f),
260 g = ((value >> 8) & 0xff) * (1/255.0f),
261 b = ((value >> 16) & 0xff) * (1/255.0f),
262 a = ((value >> 24) & 0xff) * (1/255.0f);
263
264 auto srgb_to_linear = [](float x) {
265 return (x <= 0.04045f) ? x * (1 / 12.92f)
266 : std::pow(x * (1 / 1.055f) + (0.055f / 1.055f), 2.4f);
267 };
268 r = srgb_to_linear(r);
269 g = srgb_to_linear(g);
270 b = srgb_to_linear(b);
271 if (a != 0 && needsUnpremul) {
272 r = SkTPin(r/a, 0.0f, 1.0f);
273 g = SkTPin(g/a, 0.0f, 1.0f);
274 b = SkTPin(b/a, 0.0f, 1.0f);
275 }
276 r *= 255.0f;
277 g *= 255.0f;
278 b *= 255.0f;
279 a *= 255.0f;
280 return SkColorSetARGB(r, g, b, a);
281 }
282 case kRGB_101010x_SkColorType: {
283 uint32_t value = *this->addr32(x, y);
284 // Convert 10-bit rgb to 8-bit rgb
285 return SkColorSetRGB(((value >> 0) & 0x3ff) * (255/1023.0f),
286 ((value >> 10) & 0x3ff) * (255/1023.0f),
287 ((value >> 20) & 0x3ff) * (255/1023.0f));
288 }
289 case kBGR_101010x_XR_SkColorType: {
290 SkASSERT(false);
291 return 0;
292 }
293 case kBGR_101010x_SkColorType: {
294 uint32_t value = *this->addr32(x, y);
295 // Convert 10-bit bgr to 8-bit rgb
296 return SkColorSetRGB(((value >> 20) & 0x3ff) * (255/1023.0f),
297 ((value >> 10) & 0x3ff) * (255/1023.0f),
298 ((value >> 0) & 0x3ff) * (255/1023.0f));
299 }
300 case kRGBA_1010102_SkColorType:
301 case kBGRA_1010102_SkColorType: {
302 uint32_t value = *this->addr32(x, y);
303 float b = ((value >> 0) & 0x3ff) * (1/1023.0f),
304 g = ((value >> 10) & 0x3ff) * (1/1023.0f),
305 r = ((value >> 20) & 0x3ff) * (1/1023.0f),
306 a = ((value >> 30) & 0x3 ) * (1/ 3.0f);
307 if (a != 0 && needsUnpremul) {
308 r = SkTPin(r/a, 0.0f, 1.0f);
309 g = SkTPin(g/a, 0.0f, 1.0f);
310 b = SkTPin(b/a, 0.0f, 1.0f);
311 }
312 b *= 255.0f;
313 g *= 255.0f;
314 r *= 255.0f;
315 a *= 255.0f;
316 return SkColorSetARGB(a, r, g, b);
317 }
318 case kBGRA_10101010_XR_SkColorType: {
319 SkASSERT(false);
320 return 0;
321 }
322 case kRGBA_10x6_SkColorType: {
323 uint64_t value = *this->addr64(x, y);
324 return SkColorSetARGB(((value >> 54) & 0x3ff) * (1/1023.0f),
325 ((value >> 6) & 0x3ff) * (1/1023.0f),
326 ((value >> 22) & 0x3ff) * (1/1023.0f),
327 ((value >> 38) & 0x3ff) * (1/1023.0f));
328 }
329 case kR16G16B16A16_unorm_SkColorType: {
330 uint64_t value = *this->addr64(x, y);
331 float r = ((value >> 0) & 0xffff) * (1/65535.0f),
332 g = ((value >> 16) & 0xffff) * (1/65535.0f),
333 b = ((value >> 32) & 0xffff) * (1/65535.0f),
334 a = ((value >> 48) & 0xffff) * (1/65535.0f);
335 if (a != 0 && needsUnpremul) {
336 r *= (1.0f/a);
337 g *= (1.0f/a);
338 b *= (1.0f/a);
339 }
340 r *= 255.0f;
341 g *= 255.0f;
342 b *= 255.0f;
343 a *= 255.0f;
344 return SkColorSetARGB(r, g, b, a);
345 }
346 case kRGB_F16F16F16x_SkColorType: {
347 const uint64_t* addr =
348 (const uint64_t*)fPixels + y * (fRowBytes >> 3) + x;
349 skvx::float4 p4 = from_half(skvx::half4::Load(addr));
350 p4[3] = 1.0f;
351 // p4 is RGBA, but we want BGRA, so we need to swap next
352 return Sk4f_toL32(swizzle_rb(p4));
353 }
354 case kRGBA_F16Norm_SkColorType:
355 case kRGBA_F16_SkColorType: {
356 const uint64_t* addr =
357 (const uint64_t*)fPixels + y * (fRowBytes >> 3) + x;
358 skvx::float4 p4 = from_half(skvx::half4::Load(addr));
359 if (p4[3] && needsUnpremul) {
360 float inva = 1 / p4[3];
361 p4 = p4 * skvx::float4(inva, inva, inva, 1);
362 }
363 // p4 is RGBA, but we want BGRA, so we need to swap next
364 return Sk4f_toL32(swizzle_rb(p4));
365 }
366 case kRGBA_F32_SkColorType: {
367 const float* rgba =
368 (const float*)fPixels + 4*y*(fRowBytes >> 4) + 4*x;
369 skvx::float4 p4 = skvx::float4::Load(rgba);
370 // From here on, just like F16:
371 if (p4[3] && needsUnpremul) {
372 float inva = 1 / p4[3];
373 p4 = p4 * skvx::float4(inva, inva, inva, 1);
374 }
375 // p4 is RGBA, but we want BGRA, so we need to swap next
376 return Sk4f_toL32(swizzle_rb(p4));
377 }
378 case kUnknown_SkColorType:
379 break;
380 }
381 SkDEBUGFAIL("");
382 return SkColorSetARGB(0, 0, 0, 0);
383 }
384
385 //////////////////////////////////////////////////////////////////////////////////////////////////
386
387 SkColor4f SkPixmap::getColor4f(int x, int y) const {
388 SkASSERT(this->addr());
389 SkASSERT((unsigned)x < (unsigned)this->width());
390 SkASSERT((unsigned)y < (unsigned)this->height());
391
392 const bool needsUnpremul = (kPremul_SkAlphaType == fInfo.alphaType());
393 auto toColor = [needsUnpremul](uint32_t maybePremulColor) {
394 return needsUnpremul ? SkUnPreMultiply::PMColorToColor(maybePremulColor)
395 : SkSwizzle_BGRA_to_PMColor(maybePremulColor);
396 };
397
398 switch (this->colorType()) {
399 case kGray_8_SkColorType: {
400 float value = *this->addr8(x, y) / 255.0f;
401 return SkColor4f{value, value, value, 1.0f};
402 }
403 case kR8_unorm_SkColorType: {
404 return SkColor4f{(*this->addr8(x, y) / 255.0f), 0.0f, 0.0f, 1.0f};
405 }
406 case kAlpha_8_SkColorType: {
407 return SkColor4f{0.0f, 0.0f, 0.0f, (*this->addr8(x, y) / 255.0f)};
408 }
409 case kA16_unorm_SkColorType: {
410 return SkColor4f{0.0f, 0.0f, 0.0f, (*this->addr16(x, y) / 65535.0f)};
411 }
412 case kA16_float_SkColorType: {
413 return SkColor4f{0.0f, 0.0f, 0.0f, SkHalfToFloat(*this->addr16(x, y))};
414 }
415 case kRGB_565_SkColorType: {
416 return SkColor4f::FromColor(SkPixel16ToColor(*this->addr16(x, y)));
417 }
418 case kARGB_4444_SkColorType: {
419 SkPMColor c = SkPixel4444ToPixel32(*this->addr16(x, y));
420 return SkColor4f::FromColor(toColor(c));
421 }
422 case kR8G8_unorm_SkColorType: {
423 uint16_t value = *this->addr16(x, y);
424 return SkColor4f::FromColor(SkColorSetRGB((uint8_t)(value), (uint8_t)(value >> 8), 0));
425 }
426 case kR16_unorm_SkColorType: {
427 float value = *this->addr16(x, y) / 65535.0f;
428 return SkColor4f{value, 0.0f, 0.0f, 1.0f};
429 }
430 case kR16G16_unorm_SkColorType: {
431 uint32_t value = *this->addr32(x, y);
432 float r = ((value >> 0) & 0xffff) * (1.0f / 65535.0f),
433 g = ((value >> 16) & 0xffff) * (1.0f / 65535.0f);
434 return SkColor4f{r, g, 0.0, 1.0};
435 }
436 case kR16G16_float_SkColorType: {
437 uint32_t value = *this->addr32(x, y);
438 float r = SkHalfToFloat((value >> 0 ) & 0xffff);
439 float g = SkHalfToFloat((value >> 16) & 0xffff);
440 return SkColor4f{r, g, 0.0, 1.0};
441 }
442 case kRGB_888x_SkColorType: {
443 SkColor c = SkSwizzle_RB(*this->addr32(x, y) | 0xff000000);
444 return SkColor4f::FromColor(c);
445 }
446 case kBGRA_8888_SkColorType: {
447 SkPMColor c = SkSwizzle_BGRA_to_PMColor(*this->addr32(x, y));
448 return SkColor4f::FromColor(toColor(c));
449 }
450 case kRGBA_8888_SkColorType: {
451 SkPMColor c = SkSwizzle_RGBA_to_PMColor(*this->addr32(x, y));
452 return SkColor4f::FromColor(toColor(c));
453 }
454 case kSRGBA_8888_SkColorType: {
455 auto srgb_to_linear = [](float x) {
456 return (x <= 0.04045f) ? x * (1 / 12.92f)
457 : std::pow(x * (1 / 1.055f) + (0.055f / 1.055f), 2.4f);
458 };
459
460 uint32_t value = *this->addr32(x, y);
461 float r = ((value >> 0) & 0xff) * (1 / 255.0f),
462 g = ((value >> 8) & 0xff) * (1 / 255.0f),
463 b = ((value >> 16) & 0xff) * (1 / 255.0f),
464 a = ((value >> 24) & 0xff) * (1 / 255.0f);
465 r = srgb_to_linear(r);
466 g = srgb_to_linear(g);
467 b = srgb_to_linear(b);
468 if (a != 0 && needsUnpremul) {
469 r = SkTPin(r / a, 0.0f, 1.0f);
470 g = SkTPin(g / a, 0.0f, 1.0f);
471 b = SkTPin(b / a, 0.0f, 1.0f);
472 }
473 return SkColor4f{r, g, b, a};
474 }
475 case kBGR_101010x_XR_SkColorType: {
476 SkASSERT(false);
477 return {};
478 }
479 case kRGB_101010x_SkColorType: {
480 uint32_t value = *this->addr32(x, y);
481 // Convert 10-bit RGB to floats
482 return SkColor4f{((value >> 0) & 0x3ff) / (1023.0f),
483 ((value >> 10) & 0x3ff) / (1023.0f),
484 ((value >> 20) & 0x3ff) / (1023.0f),
485 1.0f};
486 }
487 case kBGR_101010x_SkColorType: {
488 uint32_t value = *this->addr32(x, y);
489 // Convert 10-bit BGR color values to RGBA floats
490 return SkColor4f{((value >> 20) & 0x3ff) / (1023.0f),
491 ((value >> 10) & 0x3ff) / (1023.0f),
492 ((value >> 0) & 0x3ff) / (1023.0f),
493 1.0f};
494 }
495 case kRGBA_1010102_SkColorType:
496 case kBGRA_1010102_SkColorType: {
497 uint32_t value = *this->addr32(x, y);
498 // Convert 10-bit color values to floats
499 float b = ((value >> 0) & 0x3ff) * (1 / 1023.0f),
500 g = ((value >> 10) & 0x3ff) * (1 / 1023.0f),
501 r = ((value >> 20) & 0x3ff) * (1 / 1023.0f),
502 a = ((value >> 30) & 0x3 ) * (1 / 3.0f );
503 if (a != 0 && needsUnpremul) {
504 r = SkTPin(r / a, 0.0f, 1.0f);
505 g = SkTPin(g / a, 0.0f, 1.0f);
506 b = SkTPin(b / a, 0.0f, 1.0f);
507 }
508 return SkColor4f{r, g, b, a};
509 }
510 case kBGRA_10101010_XR_SkColorType: {
511 SkASSERT(false);
512 return {};
513 }
514 case kRGBA_10x6_SkColorType: {
515 uint64_t value = *this->addr64(x, y);
516 return SkColor4f{((value >> 6) & 0x3ff) * (1/1023.0f),
517 ((value >> 22) & 0x3ff) * (1/1023.0f),
518 ((value >> 38) & 0x3ff) * (1/1023.0f),
519 ((value >> 54) & 0x3ff) * (1/1023.0f)};
520 }
521 case kR16G16B16A16_unorm_SkColorType: {
522 uint64_t value = *this->addr64(x, y);
523
524 float r = ((value >> 0) & 0xffff) * (1 / 65535.0f),
525 g = ((value >> 16) & 0xffff) * (1 / 65535.0f),
526 b = ((value >> 32) & 0xffff) * (1 / 65535.0f),
527 a = ((value >> 48) & 0xffff) * (1 / 65535.0f);
528 if (a != 0 && needsUnpremul) {
529 r *= (1.0f / a);
530 g *= (1.0f / a);
531 b *= (1.0f / a);
532 }
533 return SkColor4f{r, g, b, a};
534 }
535 case kRGBA_F16Norm_SkColorType:
536 case kRGBA_F16_SkColorType: {
537 const uint64_t* addr = (const uint64_t*)fPixels + y * (fRowBytes >> 3) + x;
538 skvx::float4 p4 = from_half(skvx::half4::Load(addr));
539 if (p4[3] && needsUnpremul) {
540 float inva = 1 / p4[3];
541 p4 = p4 * skvx::float4(inva, inva, inva, 1);
542 }
543 return SkColor4f{p4[0], p4[1], p4[2], p4[3]};
544 }
545 case kRGB_F16F16F16x_SkColorType: {
546 const uint64_t* addr = (const uint64_t*)fPixels + y * (fRowBytes >> 3) + x;
547 skvx::float4 p4 = from_half(skvx::half4::Load(addr));
548 p4[3] = 1.0f;
549 return SkColor4f{p4[0], p4[1], p4[2], p4[3]};
550 }
551 case kRGBA_F32_SkColorType: {
552 const float* rgba = (const float*)fPixels + 4 * y * (fRowBytes >> 4) + 4 * x;
553 skvx::float4 p4 = skvx::float4::Load(rgba);
554 // From here on, just like F16:
555 if (p4[3] && needsUnpremul) {
556 float inva = 1 / p4[3];
557 p4 = p4 * skvx::float4(inva, inva, inva, 1);
558 }
559 return SkColor4f{p4[0], p4[1], p4[2], p4[3]};
560 }
561 case kUnknown_SkColorType:
562 break;
563 }
564 SkDEBUGFAIL("");
565 return SkColors::kTransparent;
566 }
567
568 bool SkPixmap::computeIsOpaque() const {
569 const int height = this->height();
570 const int width = this->width();
571
572 switch (this->colorType()) {
573 case kAlpha_8_SkColorType: {
574 unsigned a = 0xFF;
575 for (int y = 0; y < height; ++y) {
576 const uint8_t* row = this->addr8(0, y);
577 for (int x = 0; x < width; ++x) {
578 a &= row[x];
579 }
580 if (0xFF != a) {
581 return false;
582 }
583 }
584 return true;
585 }
586 case kA16_unorm_SkColorType: {
587 unsigned a = 0xFFFF;
588 for (int y = 0; y < height; ++y) {
589 const uint16_t* row = this->addr16(0, y);
590 for (int x = 0; x < width; ++x) {
591 a &= row[x];
592 }
593 if (0xFFFF != a) {
594 return false;
595 }
596 }
597 return true;
598 }
599 case kA16_float_SkColorType: {
600 for (int y = 0; y < height; ++y) {
601 const SkHalf* row = this->addr16(0, y);
602 for (int x = 0; x < width; ++x) {
603 if (row[x] < SK_Half1) {
604 return false;
605 }
606 }
607 }
608 return true;
609 }
610 case kRGB_565_SkColorType:
611 case kGray_8_SkColorType:
612 case kR8G8_unorm_SkColorType:
613 case kR16_unorm_SkColorType:
614 case kR16G16_unorm_SkColorType:
615 case kR16G16_float_SkColorType:
616 case kRGB_888x_SkColorType:
617 case kRGB_101010x_SkColorType:
618 case kBGR_101010x_SkColorType:
619 case kRGB_F16F16F16x_SkColorType:
620 case kBGR_101010x_XR_SkColorType:
621 case kR8_unorm_SkColorType:
622 return true;
623 case kARGB_4444_SkColorType: {
624 unsigned c = 0xFFFF;
625 for (int y = 0; y < height; ++y) {
626 const SkPMColor16* row = this->addr16(0, y);
627 for (int x = 0; x < width; ++x) {
628 c &= row[x];
629 }
630 if (0xF != SkGetPackedA4444(c)) {
631 return false;
632 }
633 }
634 return true;
635 }
636 case kBGRA_8888_SkColorType:
637 case kRGBA_8888_SkColorType:
638 case kSRGBA_8888_SkColorType: {
639 SkPMColor c = (SkPMColor)~0;
640 for (int y = 0; y < height; ++y) {
641 const SkPMColor* row = this->addr32(0, y);
642 for (int x = 0; x < width; ++x) {
643 c &= row[x];
644 }
645 if (0xFF != SkGetPackedA32(c)) {
646 return false;
647 }
648 }
649 return true;
650 }
651 case kRGBA_F16Norm_SkColorType:
652 case kRGBA_F16_SkColorType: {
653 const SkHalf* row = (const SkHalf*)this->addr();
654 for (int y = 0; y < height; ++y) {
655 for (int x = 0; x < width; ++x) {
656 if (row[4 * x + 3] < SK_Half1) {
657 return false;
658 }
659 }
660 row += this->rowBytes() >> 1;
661 }
662 return true;
663 }
664 case kRGBA_F32_SkColorType: {
665 const float* row = (const float*)this->addr();
666 for (int y = 0; y < height; ++y) {
667 for (int x = 0; x < width; ++x) {
668 if (row[4 * x + 3] < 1.0f) {
669 return false;
670 }
671 }
672 row += this->rowBytes() >> 2;
673 }
674 return true;
675 }
676 case kRGBA_1010102_SkColorType:
677 case kBGRA_1010102_SkColorType: {
678 uint32_t c = ~0;
679 for (int y = 0; y < height; ++y) {
680 const uint32_t* row = this->addr32(0, y);
681 for (int x = 0; x < width; ++x) {
682 c &= row[x];
683 }
684 if (0b11 != c >> 30) {
685 return false;
686 }
687 }
688 return true;
689 }
690 case kBGRA_10101010_XR_SkColorType:{
691 static constexpr uint64_t kOne = 510 + 384;
692 for (int y = 0; y < height; ++y) {
693 const uint64_t* row = this->addr64(0, y);
694 for (int x = 0; x < width; ++x) {
695 if ((row[x] >> 54) < kOne) {
696 return false;
697 }
698 }
699 }
700 return true;
701 }
702 case kRGBA_10x6_SkColorType: {
703 uint16_t acc = 0xFFC0; // Ignore bottom six bits
704 for (int y = 0; y < height; ++y) {
705 const uint64_t* row = this->addr64(0, y);
706 for (int x = 0; x < width; ++x) {
707 acc &= (row[x] >> 48);
708 }
709 if (0xFFC0 != acc) {
710 return false;
711 }
712 }
713 return true;
714 }
715 case kR16G16B16A16_unorm_SkColorType: {
716 uint16_t acc = 0xFFFF;
717 for (int y = 0; y < height; ++y) {
718 const uint64_t* row = this->addr64(0, y);
719 for (int x = 0; x < width; ++x) {
720 acc &= (row[x] >> 48);
721 }
722 if (0xFFFF != acc) {
723 return false;
724 }
725 }
726 return true;
727 }
728 case kUnknown_SkColorType:
729 SkDEBUGFAIL("");
730 break;
731 }
732 return false;
733 }
734
735 bool SkPixmap::erase(SkColor color, const SkIRect& subset) const {
736 return this->erase(SkColor4f::FromColor(color), &subset);
737 }
738
739 bool SkPixmap::erase(const SkColor4f& color, const SkIRect* subset) const {
740 if (this->colorType() == kUnknown_SkColorType) {
741 return false;
742 }
743
744 SkIRect clip = this->bounds();
745 if (subset && !clip.intersect(*subset)) {
746 return false; // is this check really needed (i.e. to return false in this case?)
747 }
748
749 // Erase is meant to simulate drawing in kSRC mode -- which means we have to convert out
750 // unpremul input into premul (which we always do when we draw).
751 const auto c = color.premul();
752
753 const auto dst = SkImageInfo::Make(1, 1, this->colorType(), this->alphaType(),
754 sk_ref_sp(this->colorSpace()));
755 const auto src = SkImageInfo::Make(1, 1, kRGBA_F32_SkColorType, kPremul_SkAlphaType, nullptr);
756
757 uint64_t dstPixel[2] = {}; // be large enough for our widest config (F32 x 4)
758 SkASSERT((size_t)dst.bytesPerPixel() <= sizeof(dstPixel));
759
760 if (!SkConvertPixels(dst, dstPixel, sizeof(dstPixel), src, &c, sizeof(c))) {
761 return false;
762 }
763
764 if (this->colorType() == kRGBA_F32_SkColorType) {
765 SkColor4f dstColor;
766 memcpy(&dstColor, dstPixel, sizeof(dstColor));
767 for (int y = clip.fTop; y < clip.fBottom; ++y) {
768 SkColor4f* addr = (SkColor4f*)this->writable_addr(clip.fLeft, y);
769 SK_OPTS_NS::memsetT(addr, dstColor, clip.width());
770 }
771 } else {
772 using MemSet = void(*)(void*, uint64_t c, int count);
773 const MemSet procs[] = {
774 [](void* addr, uint64_t c, int count) {
775 SkASSERT(c == (uint8_t)c);
776 SK_OPTS_NS::memsetT((uint8_t*)addr, (uint8_t)c, count);
777 },
778 [](void* addr, uint64_t c, int count) {
779 SkASSERT(c == (uint16_t)c);
780 SK_OPTS_NS::memsetT((uint16_t*)addr, (uint16_t)c, count);
781 },
782 [](void* addr, uint64_t c, int count) {
783 SkASSERT(c == (uint32_t)c);
784 SK_OPTS_NS::memsetT((uint32_t*)addr, (uint32_t)c, count);
785 },
786 [](void* addr, uint64_t c, int count) {
787 SK_OPTS_NS::memsetT((uint64_t*)addr, c, count);
788 },
789 };
790
791 unsigned shift = SkColorTypeShiftPerPixel(this->colorType());
792 SkASSERT(shift < std::size(procs));
793 auto proc = procs[shift];
794
795 for (int y = clip.fTop; y < clip.fBottom; ++y) {
796 proc(this->writable_addr(clip.fLeft, y), dstPixel[0], clip.width());
797 }
798 }
799 return true;
800 }