blob: 0364a32fb867bbe9c38196cc65cb14a755a65cce [file] [log] [blame]
reed92fc2ae2015-05-22 08:06:21 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkPixmap.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkBitmap.h"
11#include "include/core/SkData.h"
12#include "include/core/SkSurface.h"
13#include "include/core/SkUnPreMultiply.h"
14#include "include/private/SkColorData.h"
15#include "include/private/SkHalf.h"
16#include "include/private/SkImageInfoPriv.h"
17#include "include/private/SkNx.h"
18#include "include/private/SkTemplates.h"
19#include "include/private/SkTo.h"
20#include "src/core/SkConvertPixels.h"
21#include "src/core/SkDraw.h"
22#include "src/core/SkMask.h"
Brian Osman9aaec362020-05-08 14:54:37 -040023#include "src/core/SkMatrixProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/core/SkPixmapPriv.h"
25#include "src/core/SkRasterClip.h"
26#include "src/core/SkUtils.h"
27#include "src/image/SkReadPixelsRec.h"
28#include "src/shaders/SkImageShader.h"
reed92fc2ae2015-05-22 08:06:21 -070029
Ben Wagnerf08d1d02018-06-18 15:11:00 -040030#include <utility>
31
reed95d343f2015-05-23 13:21:06 -070032/////////////////////////////////////////////////////////////////////////////////////////////////
33
reed884e97c2015-05-26 11:31:54 -070034void SkPixmap::reset() {
halcanary96fcdcc2015-08-27 07:41:13 -070035 fPixels = nullptr;
reed884e97c2015-05-26 11:31:54 -070036 fRowBytes = 0;
37 fInfo = SkImageInfo::MakeUnknown();
38}
39
Mike Reed086a4272017-07-18 10:53:11 -040040void SkPixmap::reset(const SkImageInfo& info, const void* addr, size_t rowBytes) {
reed884e97c2015-05-26 11:31:54 -070041 if (addr) {
42 SkASSERT(info.validRowBytes(rowBytes));
43 }
44 fPixels = addr;
reed884e97c2015-05-26 11:31:54 -070045 fRowBytes = rowBytes;
46 fInfo = info;
47}
48
reed183b57f2015-06-05 14:33:17 -070049bool SkPixmap::reset(const SkMask& src) {
50 if (SkMask::kA8_Format == src.fFormat) {
51 this->reset(SkImageInfo::MakeA8(src.fBounds.width(), src.fBounds.height()),
Mike Reed086a4272017-07-18 10:53:11 -040052 src.fImage, src.fRowBytes);
reed183b57f2015-06-05 14:33:17 -070053 return true;
54 }
55 this->reset();
56 return false;
57}
58
msarett804b4612016-06-09 11:03:45 -070059void SkPixmap::setColorSpace(sk_sp<SkColorSpace> cs) {
60 fInfo = fInfo.makeColorSpace(std::move(cs));
61}
62
reed183b57f2015-06-05 14:33:17 -070063bool SkPixmap::extractSubset(SkPixmap* result, const SkIRect& subset) const {
64 SkIRect srcRect, r;
Mike Reed92b33352019-08-24 19:39:13 -040065 srcRect.setWH(this->width(), this->height());
reed183b57f2015-06-05 14:33:17 -070066 if (!r.intersect(srcRect, subset)) {
67 return false; // r is empty (i.e. no intersection)
68 }
halcanary9d524f22016-03-29 09:03:52 -070069
reed183b57f2015-06-05 14:33:17 -070070 // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have
71 // exited above.
72 SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width()));
73 SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height()));
74
halcanary96fcdcc2015-08-27 07:41:13 -070075 const void* pixels = nullptr;
reed183b57f2015-06-05 14:33:17 -070076 if (fPixels) {
77 const size_t bpp = fInfo.bytesPerPixel();
78 pixels = (const uint8_t*)fPixels + r.fTop * fRowBytes + r.fLeft * bpp;
79 }
Brian Salomon9241a6d2019-10-03 13:26:54 -040080 result->reset(fInfo.makeDimensions(r.size()), pixels, fRowBytes);
reed183b57f2015-06-05 14:33:17 -070081 return true;
82}
83
Mike Reedc25f4402018-09-20 15:00:56 -040084// This is the same as SkPixmap::addr(x,y), but this version gets inlined, while the public
85// method does not. Perhaps we could bloat it so it can be inlined, but that would grow code-size
86// everywhere, instead of just here (on behalf of getAlphaf()).
87static const void* fast_getaddr(const SkPixmap& pm, int x, int y) {
88 x <<= SkColorTypeShiftPerPixel(pm.colorType());
89 return static_cast<const char*>(pm.addr()) + y * pm.rowBytes() + x;
90}
91
92float SkPixmap::getAlphaf(int x, int y) const {
93 SkASSERT(this->addr());
94 SkASSERT((unsigned)x < (unsigned)this->width());
95 SkASSERT((unsigned)y < (unsigned)this->height());
96
97 float value = 0;
98 const void* srcPtr = fast_getaddr(*this, x, y);
99
100 switch (this->colorType()) {
101 case kUnknown_SkColorType:
102 return 0;
103 case kGray_8_SkColorType:
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400104 case kR8G8_unorm_SkColorType:
105 case kR16G16_unorm_SkColorType:
106 case kR16G16_float_SkColorType:
Mike Reedc25f4402018-09-20 15:00:56 -0400107 case kRGB_565_SkColorType:
108 case kRGB_888x_SkColorType:
109 case kRGB_101010x_SkColorType:
Mike Kleinf7eb0542020-02-11 12:19:08 -0600110 case kBGR_101010x_SkColorType:
Mike Reedc25f4402018-09-20 15:00:56 -0400111 return 1;
112 case kAlpha_8_SkColorType:
113 value = static_cast<const uint8_t*>(srcPtr)[0] * (1.0f/255);
114 break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400115 case kA16_unorm_SkColorType:
Robert Phillips429f0d32019-09-11 17:03:28 -0400116 value = static_cast<const uint16_t*>(srcPtr)[0] * (1.0f/65535);
117 break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400118 case kA16_float_SkColorType: {
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400119 SkHalf half = static_cast<const SkHalf*>(srcPtr)[0];
120 value = SkHalfToFloat(half);
121 break;
122 }
Mike Reedc25f4402018-09-20 15:00:56 -0400123 case kARGB_4444_SkColorType: {
124 uint16_t u16 = static_cast<const uint16_t*>(srcPtr)[0];
125 value = SkGetPackedA4444(u16) * (1.0f/15);
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400126 break;
127 }
Mike Reedc25f4402018-09-20 15:00:56 -0400128 case kRGBA_8888_SkColorType:
129 case kBGRA_8888_SkColorType:
130 value = static_cast<const uint8_t*>(srcPtr)[3] * (1.0f/255);
131 break;
Mike Kleinf7eb0542020-02-11 12:19:08 -0600132 case kRGBA_1010102_SkColorType:
133 case kBGRA_1010102_SkColorType: {
Mike Reedc25f4402018-09-20 15:00:56 -0400134 uint32_t u32 = static_cast<const uint32_t*>(srcPtr)[0];
135 value = (u32 >> 30) * (1.0f/3);
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400136 break;
137 }
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400138 case kR16G16B16A16_unorm_SkColorType: {
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400139 uint64_t u64 = static_cast<const uint64_t*>(srcPtr)[0];
140 value = (u64 >> 48) * (1.0f/65535);
141 break;
142 }
Mike Kleinb70990e2019-02-28 10:03:27 -0600143 case kRGBA_F16Norm_SkColorType:
Mike Reedc25f4402018-09-20 15:00:56 -0400144 case kRGBA_F16_SkColorType: {
145 uint64_t px;
146 memcpy(&px, srcPtr, sizeof(px));
147 value = SkHalfToFloat_finite_ftz(px)[3];
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400148 break;
149 }
Mike Reedc25f4402018-09-20 15:00:56 -0400150 case kRGBA_F32_SkColorType:
151 value = static_cast<const float*>(srcPtr)[3];
152 break;
153 }
154 return value;
155}
156
Brian Osmanf6db4952018-07-16 13:06:02 -0400157bool SkPixmap::readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
158 int x, int y) const {
Matt Sarett03dd6d52017-01-23 12:15:09 -0500159 if (!SkImageInfoValidConversion(dstInfo, fInfo)) {
reed95d343f2015-05-23 13:21:06 -0700160 return false;
161 }
Matt Sarettcb6266b2017-01-17 10:48:53 -0500162
Matt Sarett03dd6d52017-01-23 12:15:09 -0500163 SkReadPixelsRec rec(dstInfo, dstPixels, dstRB, x, y);
164 if (!rec.trim(fInfo.width(), fInfo.height())) {
reed95d343f2015-05-23 13:21:06 -0700165 return false;
166 }
halcanary9d524f22016-03-29 09:03:52 -0700167
Matt Sarett03dd6d52017-01-23 12:15:09 -0500168 const void* srcPixels = this->addr(rec.fX, rec.fY);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400169 const SkImageInfo srcInfo = fInfo.makeDimensions(rec.fInfo.dimensions());
Brian Osmanb62f50c2018-07-12 14:44:27 -0400170 SkConvertPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, srcInfo, srcPixels, this->rowBytes());
Matt Sarett8572d852017-02-14 11:21:02 -0500171 return true;
reed95d343f2015-05-23 13:21:06 -0700172}
reed183b57f2015-06-05 14:33:17 -0700173
Mike Klein29a85bb2018-10-30 16:14:53 -0400174bool SkPixmap::erase(SkColor color, const SkIRect& subset) const {
175 return this->erase(SkColor4f::FromColor(color), &subset);
reed7aefe032015-06-08 10:22:22 -0700176}
177
Mike Kleind5cba9d2020-04-24 07:44:50 -0500178bool SkPixmap::erase(const SkColor4f& color, SkColorSpace* cs, const SkIRect* subset) const {
Mike Klein29a85bb2018-10-30 16:14:53 -0400179 SkPaint paint;
180 paint.setBlendMode(SkBlendMode::kSrc);
Mike Kleind5cba9d2020-04-24 07:44:50 -0500181 paint.setColor4f(color, cs);
Mike Klein29a85bb2018-10-30 16:14:53 -0400182
183 SkIRect clip = this->bounds();
184 if (subset && !clip.intersect(*subset)) {
Mike Klein9ec77b52018-09-18 12:19:57 +0000185 return false;
186 }
Mike Klein29a85bb2018-10-30 16:14:53 -0400187 SkRasterClip rc{clip};
reed7aefe032015-06-08 10:22:22 -0700188
Mike Klein29a85bb2018-10-30 16:14:53 -0400189 SkDraw draw;
Brian Osman9aaec362020-05-08 14:54:37 -0400190 SkSimpleMatrixProvider matrixProvider(SkMatrix::I());
191 draw.fDst = *this;
192 draw.fMatrixProvider = &matrixProvider;
193 draw.fRC = &rc;
reed7aefe032015-06-08 10:22:22 -0700194
Mike Klein29a85bb2018-10-30 16:14:53 -0400195 draw.drawPaint(paint);
Mike Klein9ec77b52018-09-18 12:19:57 +0000196 return true;
197}
198
Mike Klein1f313092018-01-03 10:30:21 -0500199bool SkPixmap::scalePixels(const SkPixmap& actualDst, SkFilterQuality quality) const {
200 // We may need to tweak how we interpret these just a little below, so we make copies.
201 SkPixmap src = *this,
202 dst = actualDst;
Mike Klein47cf0482018-02-09 18:57:54 +0000203
reed09553032015-11-23 12:32:16 -0800204 // Can't do anthing with empty src or dst
Mike Klein1f313092018-01-03 10:30:21 -0500205 if (src.width() <= 0 || src.height() <= 0 ||
206 dst.width() <= 0 || dst.height() <= 0) {
reed09553032015-11-23 12:32:16 -0800207 return false;
208 }
209
210 // no scaling involved?
Mike Klein1f313092018-01-03 10:30:21 -0500211 if (src.width() == dst.width() && src.height() == dst.height()) {
212 return src.readPixels(dst);
reed09553032015-11-23 12:32:16 -0800213 }
214
Mike Kleinfc9624c2018-09-06 11:25:21 -0400215 // If src and dst are both unpremul, we'll fake the source out to appear as if premul,
216 // and mark the destination as opaque. This odd combination allows us to scale unpremul
217 // pixels without ever premultiplying them (perhaps losing information in the color channels).
218 // This is an idiosyncratic feature of scalePixels(), and is tested by scalepixels_unpremul GM.
Mike Klein1f313092018-01-03 10:30:21 -0500219 bool clampAsIfUnpremul = false;
220 if (src.alphaType() == kUnpremul_SkAlphaType &&
221 dst.alphaType() == kUnpremul_SkAlphaType) {
222 src.reset(src.info().makeAlphaType(kPremul_SkAlphaType), src.addr(), src.rowBytes());
Mike Kleinfc9624c2018-09-06 11:25:21 -0400223 dst.reset(dst.info().makeAlphaType(kOpaque_SkAlphaType), dst.addr(), dst.rowBytes());
Mike Klein47cf0482018-02-09 18:57:54 +0000224
Mike Kleinfc9624c2018-09-06 11:25:21 -0400225 // We'll need to tell the image shader to clamp to [0,1] instead of the
226 // usual [0,a] when using a bicubic scaling (kHigh_SkFilterQuality).
Mike Klein1f313092018-01-03 10:30:21 -0500227 clampAsIfUnpremul = true;
Mike Klein47cf0482018-02-09 18:57:54 +0000228 }
229
reed09553032015-11-23 12:32:16 -0800230 SkBitmap bitmap;
Mike Klein1f313092018-01-03 10:30:21 -0500231 if (!bitmap.installPixels(src)) {
reed09553032015-11-23 12:32:16 -0800232 return false;
233 }
Mike Klein1f313092018-01-03 10:30:21 -0500234 bitmap.setImmutable(); // Don't copy when we create an image.
235 bitmap.setIsVolatile(true); // Disable any caching.
reed09553032015-11-23 12:32:16 -0800236
Mike Klein1f313092018-01-03 10:30:21 -0500237 SkMatrix scale = SkMatrix::MakeRectToRect(SkRect::Make(src.bounds()),
238 SkRect::Make(dst.bounds()),
239 SkMatrix::kFill_ScaleToFit);
240
241 // We'll create a shader to do this draw so we have control over the bicubic clamp.
242 sk_sp<SkShader> shader = SkImageShader::Make(SkImage::MakeFromBitmap(bitmap),
Mike Reede25b4472019-04-02 17:49:12 -0400243 SkTileMode::kClamp,
244 SkTileMode::kClamp,
Mike Klein1f313092018-01-03 10:30:21 -0500245 &scale,
Mike Reed9290d012020-06-11 16:56:06 -0400246 (SkImageShader::FilterEnum)quality,
Mike Klein1f313092018-01-03 10:30:21 -0500247 clampAsIfUnpremul);
248
249 sk_sp<SkSurface> surface = SkSurface::MakeRasterDirect(dst.info(),
250 dst.writable_addr(),
251 dst.rowBytes());
252 if (!shader || !surface) {
reed09553032015-11-23 12:32:16 -0800253 return false;
254 }
255
256 SkPaint paint;
Mike Klein47cf0482018-02-09 18:57:54 +0000257 paint.setBlendMode(SkBlendMode::kSrc);
Mike Klein1f313092018-01-03 10:30:21 -0500258 paint.setFilterQuality(quality);
259 paint.setShader(std::move(shader));
260 surface->getCanvas()->drawPaint(paint);
reed09553032015-11-23 12:32:16 -0800261 return true;
262}
263
reed183b57f2015-06-05 14:33:17 -0700264//////////////////////////////////////////////////////////////////////////////////////////////////
Hal Canary94e1a2f2016-10-31 09:38:12 -0400265
266SkColor SkPixmap::getColor(int x, int y) const {
267 SkASSERT(this->addr());
268 SkASSERT((unsigned)x < (unsigned)this->width());
269 SkASSERT((unsigned)y < (unsigned)this->height());
Matt Sarett9466bf52017-06-09 09:54:55 -0400270
Mike Klein9e7f5042018-09-19 14:39:25 +0000271 const bool needsUnpremul = (kPremul_SkAlphaType == fInfo.alphaType());
272 auto toColor = [needsUnpremul](uint32_t maybePremulColor) {
273 return needsUnpremul ? SkUnPreMultiply::PMColorToColor(maybePremulColor)
274 : SkSwizzle_BGRA_to_PMColor(maybePremulColor);
275 };
276
277 switch (this->colorType()) {
278 case kGray_8_SkColorType: {
279 uint8_t value = *this->addr8(x, y);
280 return SkColorSetRGB(value, value, value);
281 }
282 case kAlpha_8_SkColorType: {
283 return SkColorSetA(0, *this->addr8(x, y));
284 }
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400285 case kA16_unorm_SkColorType: {
Robert Phillips429f0d32019-09-11 17:03:28 -0400286 uint16_t value = *this->addr16(x, y);
287 return SkColorSetA(0, value * (255 / 65535.0f));
288 }
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400289 case kA16_float_SkColorType: {
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400290 SkHalf value = *this->addr16(x, y);
291 return SkColorSetA(0, 255 * SkHalfToFloat(value));
292 }
Mike Klein9e7f5042018-09-19 14:39:25 +0000293 case kRGB_565_SkColorType: {
294 return SkPixel16ToColor(*this->addr16(x, y));
295 }
296 case kARGB_4444_SkColorType: {
297 uint16_t value = *this->addr16(x, y);
298 SkPMColor c = SkPixel4444ToPixel32(value);
299 return toColor(c);
300 }
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400301 case kR8G8_unorm_SkColorType: {
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400302 uint16_t value = *this->addr16(x, y);
303 return (uint32_t)( ((value >> 0) & 0xff) ) << 16
304 | (uint32_t)( ((value >> 8) & 0xff) ) << 8
305 | 0xff000000;
306 }
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400307 case kR16G16_unorm_SkColorType: {
Robert Phillips429f0d32019-09-11 17:03:28 -0400308 uint32_t value = *this->addr32(x, y);
309 return (uint32_t)( ((value >> 0) & 0xffff) * (255/65535.0f) ) << 16
310 | (uint32_t)( ((value >> 16) & 0xffff) * (255/65535.0f) ) << 8
311 | 0xff000000;
312 }
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400313 case kR16G16_float_SkColorType: {
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400314 uint32_t value = *this->addr32(x, y);
315 uint32_t r = 255 * SkHalfToFloat((value >> 0) & 0xffff);
316 uint32_t g = 255 * SkHalfToFloat((value >> 16) & 0xffff);
317 return (r << 16) | (g << 8) | 0xff000000;
318 }
Mike Klein9e7f5042018-09-19 14:39:25 +0000319 case kRGB_888x_SkColorType: {
320 uint32_t value = *this->addr32(x, y);
321 return SkSwizzle_RB(value | 0xff000000);
322 }
323 case kBGRA_8888_SkColorType: {
324 uint32_t value = *this->addr32(x, y);
325 SkPMColor c = SkSwizzle_BGRA_to_PMColor(value);
326 return toColor(c);
327 }
328 case kRGBA_8888_SkColorType: {
329 uint32_t value = *this->addr32(x, y);
330 SkPMColor c = SkSwizzle_RGBA_to_PMColor(value);
331 return toColor(c);
332 }
333 case kRGB_101010x_SkColorType: {
334 uint32_t value = *this->addr32(x, y);
335 // Convert 10-bit rgb to 8-bit bgr, and mask in 0xff alpha at the top.
336 return (uint32_t)( ((value >> 0) & 0x3ff) * (255/1023.0f) ) << 16
337 | (uint32_t)( ((value >> 10) & 0x3ff) * (255/1023.0f) ) << 8
338 | (uint32_t)( ((value >> 20) & 0x3ff) * (255/1023.0f) ) << 0
339 | 0xff000000;
340 }
Mike Kleinf7eb0542020-02-11 12:19:08 -0600341 case kBGR_101010x_SkColorType: {
342 uint32_t value = *this->addr32(x, y);
343 // Convert 10-bit bgr to 8-bit bgr, and mask in 0xff alpha at the top.
344 return (uint32_t)( ((value >> 0) & 0x3ff) * (255/1023.0f) ) << 0
345 | (uint32_t)( ((value >> 10) & 0x3ff) * (255/1023.0f) ) << 8
346 | (uint32_t)( ((value >> 20) & 0x3ff) * (255/1023.0f) ) << 16
347 | 0xff000000;
348 }
349 case kRGBA_1010102_SkColorType:
350 case kBGRA_1010102_SkColorType: {
Mike Klein9e7f5042018-09-19 14:39:25 +0000351 uint32_t value = *this->addr32(x, y);
352
353 float r = ((value >> 0) & 0x3ff) * (1/1023.0f),
354 g = ((value >> 10) & 0x3ff) * (1/1023.0f),
355 b = ((value >> 20) & 0x3ff) * (1/1023.0f),
356 a = ((value >> 30) & 0x3 ) * (1/ 3.0f);
Mike Kleinf7eb0542020-02-11 12:19:08 -0600357 if (this->colorType() == kBGRA_1010102_SkColorType) {
358 std::swap(r,b);
359 }
Mike Klein9e7f5042018-09-19 14:39:25 +0000360 if (a != 0 && needsUnpremul) {
Robert Phillips7f367982019-09-26 14:01:36 -0400361 r = SkTPin(r/a, 0.0f, 1.0f);
362 g = SkTPin(g/a, 0.0f, 1.0f);
363 b = SkTPin(b/a, 0.0f, 1.0f);
Mike Klein9e7f5042018-09-19 14:39:25 +0000364 }
365 return (uint32_t)( r * 255.0f ) << 16
366 | (uint32_t)( g * 255.0f ) << 8
367 | (uint32_t)( b * 255.0f ) << 0
368 | (uint32_t)( a * 255.0f ) << 24;
369 }
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400370 case kR16G16B16A16_unorm_SkColorType: {
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400371 uint64_t value = *this->addr64(x, y);
372
373 float r = ((value ) & 0xffff) * (1/65535.0f),
374 g = ((value >> 16) & 0xffff) * (1/65535.0f),
375 b = ((value >> 32) & 0xffff) * (1/65535.0f),
376 a = ((value >> 48) & 0xffff) * (1/65535.0f);
377 if (a != 0 && needsUnpremul) {
378 r *= (1.0f/a);
379 g *= (1.0f/a);
380 b *= (1.0f/a);
381 }
382 return (uint32_t)( r * 255.0f ) << 16
383 | (uint32_t)( g * 255.0f ) << 8
384 | (uint32_t)( b * 255.0f ) << 0
385 | (uint32_t)( a * 255.0f ) << 24;
386 }
Brian Osman1132f742019-03-07 14:03:58 -0500387 case kRGBA_F16Norm_SkColorType:
Mike Klein9e7f5042018-09-19 14:39:25 +0000388 case kRGBA_F16_SkColorType: {
389 const uint64_t* addr =
390 (const uint64_t*)fPixels + y * (fRowBytes >> 3) + x;
391 Sk4f p4 = SkHalfToFloat_finite_ftz(*addr);
392 if (p4[3] && needsUnpremul) {
393 float inva = 1 / p4[3];
394 p4 = p4 * Sk4f(inva, inva, inva, 1);
395 }
396 SkColor c;
397 SkNx_cast<uint8_t>(p4 * Sk4f(255) + Sk4f(0.5f)).store(&c);
398 // p4 is RGBA, but we want BGRA, so we need to swap next
399 return SkSwizzle_RB(c);
400 }
401 case kRGBA_F32_SkColorType: {
402 const float* rgba =
403 (const float*)fPixels + 4*y*(fRowBytes >> 4) + 4*x;
404 Sk4f p4 = Sk4f::Load(rgba);
405 // From here on, just like F16:
406 if (p4[3] && needsUnpremul) {
407 float inva = 1 / p4[3];
408 p4 = p4 * Sk4f(inva, inva, inva, 1);
409 }
410 SkColor c;
411 SkNx_cast<uint8_t>(p4 * Sk4f(255) + Sk4f(0.5f)).store(&c);
412 // p4 is RGBA, but we want BGRA, so we need to swap next
413 return SkSwizzle_RB(c);
414 }
Mike Kleinf7eb0542020-02-11 12:19:08 -0600415 case kUnknown_SkColorType:
416 break;
Mike Klein9e7f5042018-09-19 14:39:25 +0000417 }
Mike Kleinf7eb0542020-02-11 12:19:08 -0600418 SkDEBUGFAIL("");
419 return SkColorSetARGB(0, 0, 0, 0);
Hal Canary94e1a2f2016-10-31 09:38:12 -0400420}
Hal Canary58a76942016-12-07 15:24:59 -0500421
422bool SkPixmap::computeIsOpaque() const {
423 const int height = this->height();
424 const int width = this->width();
425
426 switch (this->colorType()) {
427 case kAlpha_8_SkColorType: {
428 unsigned a = 0xFF;
429 for (int y = 0; y < height; ++y) {
430 const uint8_t* row = this->addr8(0, y);
431 for (int x = 0; x < width; ++x) {
432 a &= row[x];
433 }
434 if (0xFF != a) {
435 return false;
436 }
437 }
438 return true;
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400439 }
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400440 case kA16_unorm_SkColorType: {
Robert Phillips429f0d32019-09-11 17:03:28 -0400441 unsigned a = 0xFFFF;
442 for (int y = 0; y < height; ++y) {
443 const uint16_t* row = this->addr16(0, y);
444 for (int x = 0; x < width; ++x) {
445 a &= row[x];
446 }
447 if (0xFFFF != a) {
448 return false;
449 }
450 }
451 return true;
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400452 }
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400453 case kA16_float_SkColorType: {
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400454 for (int y = 0; y < height; ++y) {
455 const SkHalf* row = this->addr16(0, y);
456 for (int x = 0; x < width; ++x) {
457 if (row[x] < SK_Half1) {
458 return false;
459 }
460 }
461 }
462 return true;
463 }
Hal Canary58a76942016-12-07 15:24:59 -0500464 case kRGB_565_SkColorType:
465 case kGray_8_SkColorType:
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400466 case kR8G8_unorm_SkColorType:
467 case kR16G16_unorm_SkColorType:
468 case kR16G16_float_SkColorType:
Brian Osman2091fbb2018-10-11 11:05:37 -0400469 case kRGB_888x_SkColorType:
470 case kRGB_101010x_SkColorType:
Mike Kleinf7eb0542020-02-11 12:19:08 -0600471 case kBGR_101010x_SkColorType:
Hal Canary58a76942016-12-07 15:24:59 -0500472 return true;
473 break;
474 case kARGB_4444_SkColorType: {
475 unsigned c = 0xFFFF;
476 for (int y = 0; y < height; ++y) {
477 const SkPMColor16* row = this->addr16(0, y);
478 for (int x = 0; x < width; ++x) {
479 c &= row[x];
480 }
481 if (0xF != SkGetPackedA4444(c)) {
482 return false;
483 }
484 }
485 return true;
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400486 }
Hal Canary58a76942016-12-07 15:24:59 -0500487 case kBGRA_8888_SkColorType:
488 case kRGBA_8888_SkColorType: {
489 SkPMColor c = (SkPMColor)~0;
490 for (int y = 0; y < height; ++y) {
491 const SkPMColor* row = this->addr32(0, y);
492 for (int x = 0; x < width; ++x) {
493 c &= row[x];
494 }
495 if (0xFF != SkGetPackedA32(c)) {
496 return false;
497 }
498 }
499 return true;
500 }
Mike Kleinb70990e2019-02-28 10:03:27 -0600501 case kRGBA_F16Norm_SkColorType:
Hal Canary58a76942016-12-07 15:24:59 -0500502 case kRGBA_F16_SkColorType: {
503 const SkHalf* row = (const SkHalf*)this->addr();
504 for (int y = 0; y < height; ++y) {
505 for (int x = 0; x < width; ++x) {
506 if (row[4 * x + 3] < SK_Half1) {
507 return false;
508 }
509 }
510 row += this->rowBytes() >> 1;
511 }
512 return true;
513 }
Brian Osman2091fbb2018-10-11 11:05:37 -0400514 case kRGBA_F32_SkColorType: {
515 const float* row = (const float*)this->addr();
516 for (int y = 0; y < height; ++y) {
517 for (int x = 0; x < width; ++x) {
518 if (row[4 * x + 3] < 1.0f) {
519 return false;
520 }
521 }
522 row += this->rowBytes() >> 2;
523 }
524 return true;
525 }
Mike Kleinf7eb0542020-02-11 12:19:08 -0600526 case kRGBA_1010102_SkColorType:
527 case kBGRA_1010102_SkColorType: {
Brian Osman2091fbb2018-10-11 11:05:37 -0400528 uint32_t c = ~0;
529 for (int y = 0; y < height; ++y) {
530 const uint32_t* row = this->addr32(0, y);
531 for (int x = 0; x < width; ++x) {
532 c &= row[x];
533 }
534 if (0b11 != c >> 30) {
535 return false;
536 }
537 }
538 return true;
539 }
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400540 case kR16G16B16A16_unorm_SkColorType: {
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400541 uint16_t acc = 0xFFFF;
542 for (int y = 0; y < height; ++y) {
543 const uint64_t* row = this->addr64(0, y);
544 for (int x = 0; x < width; ++x) {
545 acc &= (row[x] >> 48);
546 }
547 if (0xFFFF != acc) {
548 return false;
549 }
550 }
551 return true;
552 }
Brian Osman2091fbb2018-10-11 11:05:37 -0400553 case kUnknown_SkColorType:
554 SkDEBUGFAIL("");
Hal Canary58a76942016-12-07 15:24:59 -0500555 break;
556 }
557 return false;
558}
Mike Reed43798692017-10-17 18:04:32 +0000559
560//////////////////////////////////////////////////////////////////////////////////////////////////
561
Brian Osmanc337a632018-11-30 10:39:32 -0500562static bool draw_orientation(const SkPixmap& dst, const SkPixmap& src, SkEncodedOrigin origin) {
Mike Reed7306bcd2017-10-25 10:37:30 -0400563 auto surf = SkSurface::MakeRasterDirect(dst.info(), dst.writable_addr(), dst.rowBytes());
564 if (!surf) {
565 return false;
Mike Reed43798692017-10-17 18:04:32 +0000566 }
Mike Reed43798692017-10-17 18:04:32 +0000567
Mike Reed7306bcd2017-10-25 10:37:30 -0400568 SkBitmap bm;
569 bm.installPixels(src);
Mike Reed43798692017-10-17 18:04:32 +0000570
Brian Osmanc337a632018-11-30 10:39:32 -0500571 SkMatrix m = SkEncodedOriginToMatrix(origin, src.width(), src.height());
Mike Reed7306bcd2017-10-25 10:37:30 -0400572
Mike Reed7306bcd2017-10-25 10:37:30 -0400573 SkPaint p;
574 p.setBlendMode(SkBlendMode::kSrc);
575 surf->getCanvas()->concat(m);
576 surf->getCanvas()->drawBitmap(bm, 0, 0, &p);
Mike Reed43798692017-10-17 18:04:32 +0000577 return true;
578}
579
Brian Osmanc337a632018-11-30 10:39:32 -0500580bool SkPixmapPriv::Orient(const SkPixmap& dst, const SkPixmap& src, SkEncodedOrigin origin) {
Mike Reed43798692017-10-17 18:04:32 +0000581 if (src.colorType() != dst.colorType()) {
582 return false;
583 }
584 // note: we just ignore alphaType and colorSpace for this transformation
585
586 int w = src.width();
587 int h = src.height();
Brian Osmanc337a632018-11-30 10:39:32 -0500588 if (ShouldSwapWidthHeight(origin)) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400589 using std::swap;
590 swap(w, h);
Mike Reed43798692017-10-17 18:04:32 +0000591 }
592 if (dst.width() != w || dst.height() != h) {
593 return false;
594 }
595 if (w == 0 || h == 0) {
596 return true;
597 }
598
599 // check for aliasing to self
600 if (src.addr() == dst.addr()) {
Brian Osmanc337a632018-11-30 10:39:32 -0500601 return kTopLeft_SkEncodedOrigin == origin;
Mike Reed43798692017-10-17 18:04:32 +0000602 }
Brian Osmanc337a632018-11-30 10:39:32 -0500603 return draw_orientation(dst, src, origin);
Mike Reed43798692017-10-17 18:04:32 +0000604}
605
Brian Osmanc337a632018-11-30 10:39:32 -0500606bool SkPixmapPriv::ShouldSwapWidthHeight(SkEncodedOrigin origin) {
607 // The last four SkEncodedOrigin values involve 90 degree rotations
608 return origin >= kLeftTop_SkEncodedOrigin;
Leon Scroggins III0cbc10f2017-10-30 09:07:53 -0400609}
610
611SkImageInfo SkPixmapPriv::SwapWidthHeight(const SkImageInfo& info) {
612 return info.makeWH(info.height(), info.width());
613}
614