blob: 3da7c1f39983cc4d3bd34865bba2e71b597ae85f [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"
23#include "src/core/SkPixmapPriv.h"
24#include "src/core/SkRasterClip.h"
25#include "src/core/SkUtils.h"
26#include "src/image/SkReadPixelsRec.h"
27#include "src/shaders/SkImageShader.h"
reed92fc2ae2015-05-22 08:06:21 -070028
Ben Wagnerf08d1d02018-06-18 15:11:00 -040029#include <utility>
30
reed95d343f2015-05-23 13:21:06 -070031/////////////////////////////////////////////////////////////////////////////////////////////////
32
reed884e97c2015-05-26 11:31:54 -070033void SkPixmap::reset() {
halcanary96fcdcc2015-08-27 07:41:13 -070034 fPixels = nullptr;
reed884e97c2015-05-26 11:31:54 -070035 fRowBytes = 0;
36 fInfo = SkImageInfo::MakeUnknown();
37}
38
Mike Reed086a4272017-07-18 10:53:11 -040039void SkPixmap::reset(const SkImageInfo& info, const void* addr, size_t rowBytes) {
reed884e97c2015-05-26 11:31:54 -070040 if (addr) {
41 SkASSERT(info.validRowBytes(rowBytes));
42 }
43 fPixels = addr;
reed884e97c2015-05-26 11:31:54 -070044 fRowBytes = rowBytes;
45 fInfo = info;
46}
47
reed183b57f2015-06-05 14:33:17 -070048bool SkPixmap::reset(const SkMask& src) {
49 if (SkMask::kA8_Format == src.fFormat) {
50 this->reset(SkImageInfo::MakeA8(src.fBounds.width(), src.fBounds.height()),
Mike Reed086a4272017-07-18 10:53:11 -040051 src.fImage, src.fRowBytes);
reed183b57f2015-06-05 14:33:17 -070052 return true;
53 }
54 this->reset();
55 return false;
56}
57
msarett804b4612016-06-09 11:03:45 -070058void SkPixmap::setColorSpace(sk_sp<SkColorSpace> cs) {
59 fInfo = fInfo.makeColorSpace(std::move(cs));
60}
61
reed183b57f2015-06-05 14:33:17 -070062bool SkPixmap::extractSubset(SkPixmap* result, const SkIRect& subset) const {
63 SkIRect srcRect, r;
Mike Reed92b33352019-08-24 19:39:13 -040064 srcRect.setWH(this->width(), this->height());
reed183b57f2015-06-05 14:33:17 -070065 if (!r.intersect(srcRect, subset)) {
66 return false; // r is empty (i.e. no intersection)
67 }
halcanary9d524f22016-03-29 09:03:52 -070068
reed183b57f2015-06-05 14:33:17 -070069 // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have
70 // exited above.
71 SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width()));
72 SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height()));
73
halcanary96fcdcc2015-08-27 07:41:13 -070074 const void* pixels = nullptr;
reed183b57f2015-06-05 14:33:17 -070075 if (fPixels) {
76 const size_t bpp = fInfo.bytesPerPixel();
77 pixels = (const uint8_t*)fPixels + r.fTop * fRowBytes + r.fLeft * bpp;
78 }
Mike Reed26e9ddd2017-07-17 17:33:53 -040079 result->reset(fInfo.makeWH(r.width(), r.height()), pixels, fRowBytes);
reed183b57f2015-06-05 14:33:17 -070080 return true;
81}
82
Mike Reedc25f4402018-09-20 15:00:56 -040083// This is the same as SkPixmap::addr(x,y), but this version gets inlined, while the public
84// method does not. Perhaps we could bloat it so it can be inlined, but that would grow code-size
85// everywhere, instead of just here (on behalf of getAlphaf()).
86static const void* fast_getaddr(const SkPixmap& pm, int x, int y) {
87 x <<= SkColorTypeShiftPerPixel(pm.colorType());
88 return static_cast<const char*>(pm.addr()) + y * pm.rowBytes() + x;
89}
90
91float SkPixmap::getAlphaf(int x, int y) const {
92 SkASSERT(this->addr());
93 SkASSERT((unsigned)x < (unsigned)this->width());
94 SkASSERT((unsigned)y < (unsigned)this->height());
95
96 float value = 0;
97 const void* srcPtr = fast_getaddr(*this, x, y);
98
99 switch (this->colorType()) {
100 case kUnknown_SkColorType:
101 return 0;
102 case kGray_8_SkColorType:
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400103 case kR8G8_unorm_SkColorType:
104 case kR16G16_unorm_SkColorType:
105 case kR16G16_float_SkColorType:
Mike Reedc25f4402018-09-20 15:00:56 -0400106 case kRGB_565_SkColorType:
107 case kRGB_888x_SkColorType:
108 case kRGB_101010x_SkColorType:
109 return 1;
110 case kAlpha_8_SkColorType:
111 value = static_cast<const uint8_t*>(srcPtr)[0] * (1.0f/255);
112 break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400113 case kA16_unorm_SkColorType:
Robert Phillips429f0d32019-09-11 17:03:28 -0400114 value = static_cast<const uint16_t*>(srcPtr)[0] * (1.0f/65535);
115 break;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400116 case kA16_float_SkColorType: {
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400117 SkHalf half = static_cast<const SkHalf*>(srcPtr)[0];
118 value = SkHalfToFloat(half);
119 break;
120 }
Mike Reedc25f4402018-09-20 15:00:56 -0400121 case kARGB_4444_SkColorType: {
122 uint16_t u16 = static_cast<const uint16_t*>(srcPtr)[0];
123 value = SkGetPackedA4444(u16) * (1.0f/15);
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400124 break;
125 }
Mike Reedc25f4402018-09-20 15:00:56 -0400126 case kRGBA_8888_SkColorType:
127 case kBGRA_8888_SkColorType:
128 value = static_cast<const uint8_t*>(srcPtr)[3] * (1.0f/255);
129 break;
130 case kRGBA_1010102_SkColorType: {
131 uint32_t u32 = static_cast<const uint32_t*>(srcPtr)[0];
132 value = (u32 >> 30) * (1.0f/3);
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400133 break;
134 }
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400135 case kR16G16B16A16_unorm_SkColorType: {
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400136 uint64_t u64 = static_cast<const uint64_t*>(srcPtr)[0];
137 value = (u64 >> 48) * (1.0f/65535);
138 break;
139 }
Mike Kleinb70990e2019-02-28 10:03:27 -0600140 case kRGBA_F16Norm_SkColorType:
Mike Reedc25f4402018-09-20 15:00:56 -0400141 case kRGBA_F16_SkColorType: {
142 uint64_t px;
143 memcpy(&px, srcPtr, sizeof(px));
144 value = SkHalfToFloat_finite_ftz(px)[3];
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400145 break;
146 }
Mike Reedc25f4402018-09-20 15:00:56 -0400147 case kRGBA_F32_SkColorType:
148 value = static_cast<const float*>(srcPtr)[3];
149 break;
150 }
151 return value;
152}
153
Brian Osmanf6db4952018-07-16 13:06:02 -0400154bool SkPixmap::readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
155 int x, int y) const {
Matt Sarett03dd6d52017-01-23 12:15:09 -0500156 if (!SkImageInfoValidConversion(dstInfo, fInfo)) {
reed95d343f2015-05-23 13:21:06 -0700157 return false;
158 }
Matt Sarettcb6266b2017-01-17 10:48:53 -0500159
Matt Sarett03dd6d52017-01-23 12:15:09 -0500160 SkReadPixelsRec rec(dstInfo, dstPixels, dstRB, x, y);
161 if (!rec.trim(fInfo.width(), fInfo.height())) {
reed95d343f2015-05-23 13:21:06 -0700162 return false;
163 }
halcanary9d524f22016-03-29 09:03:52 -0700164
Matt Sarett03dd6d52017-01-23 12:15:09 -0500165 const void* srcPixels = this->addr(rec.fX, rec.fY);
166 const SkImageInfo srcInfo = fInfo.makeWH(rec.fInfo.width(), rec.fInfo.height());
Brian Osmanb62f50c2018-07-12 14:44:27 -0400167 SkConvertPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, srcInfo, srcPixels, this->rowBytes());
Matt Sarett8572d852017-02-14 11:21:02 -0500168 return true;
reed95d343f2015-05-23 13:21:06 -0700169}
reed183b57f2015-06-05 14:33:17 -0700170
Mike Klein29a85bb2018-10-30 16:14:53 -0400171bool SkPixmap::erase(SkColor color, const SkIRect& subset) const {
172 return this->erase(SkColor4f::FromColor(color), &subset);
reed7aefe032015-06-08 10:22:22 -0700173}
174
Mike Klein29a85bb2018-10-30 16:14:53 -0400175bool SkPixmap::erase(const SkColor4f& color, const SkIRect* subset) const {
176 SkPaint paint;
177 paint.setBlendMode(SkBlendMode::kSrc);
Robert Phillips832c9312019-01-08 13:04:10 +0000178 paint.setColor4f(color, this->colorSpace());
Mike Klein29a85bb2018-10-30 16:14:53 -0400179
180 SkIRect clip = this->bounds();
181 if (subset && !clip.intersect(*subset)) {
Mike Klein9ec77b52018-09-18 12:19:57 +0000182 return false;
183 }
Mike Klein29a85bb2018-10-30 16:14:53 -0400184 SkRasterClip rc{clip};
reed7aefe032015-06-08 10:22:22 -0700185
Mike Klein29a85bb2018-10-30 16:14:53 -0400186 SkDraw draw;
187 draw.fDst = *this;
188 draw.fMatrix = &SkMatrix::I();
189 draw.fRC = &rc;
reed7aefe032015-06-08 10:22:22 -0700190
Mike Klein29a85bb2018-10-30 16:14:53 -0400191 draw.drawPaint(paint);
Mike Klein9ec77b52018-09-18 12:19:57 +0000192 return true;
193}
194
Mike Klein1f313092018-01-03 10:30:21 -0500195bool SkPixmap::scalePixels(const SkPixmap& actualDst, SkFilterQuality quality) const {
196 // We may need to tweak how we interpret these just a little below, so we make copies.
197 SkPixmap src = *this,
198 dst = actualDst;
Mike Klein47cf0482018-02-09 18:57:54 +0000199
reed09553032015-11-23 12:32:16 -0800200 // Can't do anthing with empty src or dst
Mike Klein1f313092018-01-03 10:30:21 -0500201 if (src.width() <= 0 || src.height() <= 0 ||
202 dst.width() <= 0 || dst.height() <= 0) {
reed09553032015-11-23 12:32:16 -0800203 return false;
204 }
205
206 // no scaling involved?
Mike Klein1f313092018-01-03 10:30:21 -0500207 if (src.width() == dst.width() && src.height() == dst.height()) {
208 return src.readPixels(dst);
reed09553032015-11-23 12:32:16 -0800209 }
210
Mike Kleinfc9624c2018-09-06 11:25:21 -0400211 // If src and dst are both unpremul, we'll fake the source out to appear as if premul,
212 // and mark the destination as opaque. This odd combination allows us to scale unpremul
213 // pixels without ever premultiplying them (perhaps losing information in the color channels).
214 // This is an idiosyncratic feature of scalePixels(), and is tested by scalepixels_unpremul GM.
Mike Klein1f313092018-01-03 10:30:21 -0500215 bool clampAsIfUnpremul = false;
216 if (src.alphaType() == kUnpremul_SkAlphaType &&
217 dst.alphaType() == kUnpremul_SkAlphaType) {
218 src.reset(src.info().makeAlphaType(kPremul_SkAlphaType), src.addr(), src.rowBytes());
Mike Kleinfc9624c2018-09-06 11:25:21 -0400219 dst.reset(dst.info().makeAlphaType(kOpaque_SkAlphaType), dst.addr(), dst.rowBytes());
Mike Klein47cf0482018-02-09 18:57:54 +0000220
Mike Kleinfc9624c2018-09-06 11:25:21 -0400221 // We'll need to tell the image shader to clamp to [0,1] instead of the
222 // usual [0,a] when using a bicubic scaling (kHigh_SkFilterQuality).
Mike Klein1f313092018-01-03 10:30:21 -0500223 clampAsIfUnpremul = true;
Mike Klein47cf0482018-02-09 18:57:54 +0000224 }
225
reed09553032015-11-23 12:32:16 -0800226 SkBitmap bitmap;
Mike Klein1f313092018-01-03 10:30:21 -0500227 if (!bitmap.installPixels(src)) {
reed09553032015-11-23 12:32:16 -0800228 return false;
229 }
Mike Klein1f313092018-01-03 10:30:21 -0500230 bitmap.setImmutable(); // Don't copy when we create an image.
231 bitmap.setIsVolatile(true); // Disable any caching.
reed09553032015-11-23 12:32:16 -0800232
Mike Klein1f313092018-01-03 10:30:21 -0500233 SkMatrix scale = SkMatrix::MakeRectToRect(SkRect::Make(src.bounds()),
234 SkRect::Make(dst.bounds()),
235 SkMatrix::kFill_ScaleToFit);
236
237 // We'll create a shader to do this draw so we have control over the bicubic clamp.
238 sk_sp<SkShader> shader = SkImageShader::Make(SkImage::MakeFromBitmap(bitmap),
Mike Reede25b4472019-04-02 17:49:12 -0400239 SkTileMode::kClamp,
240 SkTileMode::kClamp,
Mike Klein1f313092018-01-03 10:30:21 -0500241 &scale,
242 clampAsIfUnpremul);
243
244 sk_sp<SkSurface> surface = SkSurface::MakeRasterDirect(dst.info(),
245 dst.writable_addr(),
246 dst.rowBytes());
247 if (!shader || !surface) {
reed09553032015-11-23 12:32:16 -0800248 return false;
249 }
250
251 SkPaint paint;
Mike Klein47cf0482018-02-09 18:57:54 +0000252 paint.setBlendMode(SkBlendMode::kSrc);
Mike Klein1f313092018-01-03 10:30:21 -0500253 paint.setFilterQuality(quality);
254 paint.setShader(std::move(shader));
255 surface->getCanvas()->drawPaint(paint);
reed09553032015-11-23 12:32:16 -0800256 return true;
257}
258
reed183b57f2015-06-05 14:33:17 -0700259//////////////////////////////////////////////////////////////////////////////////////////////////
Hal Canary94e1a2f2016-10-31 09:38:12 -0400260
261SkColor SkPixmap::getColor(int x, int y) const {
262 SkASSERT(this->addr());
263 SkASSERT((unsigned)x < (unsigned)this->width());
264 SkASSERT((unsigned)y < (unsigned)this->height());
Matt Sarett9466bf52017-06-09 09:54:55 -0400265
Mike Klein9e7f5042018-09-19 14:39:25 +0000266 const bool needsUnpremul = (kPremul_SkAlphaType == fInfo.alphaType());
267 auto toColor = [needsUnpremul](uint32_t maybePremulColor) {
268 return needsUnpremul ? SkUnPreMultiply::PMColorToColor(maybePremulColor)
269 : SkSwizzle_BGRA_to_PMColor(maybePremulColor);
270 };
271
272 switch (this->colorType()) {
273 case kGray_8_SkColorType: {
274 uint8_t value = *this->addr8(x, y);
275 return SkColorSetRGB(value, value, value);
276 }
277 case kAlpha_8_SkColorType: {
278 return SkColorSetA(0, *this->addr8(x, y));
279 }
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400280 case kA16_unorm_SkColorType: {
Robert Phillips429f0d32019-09-11 17:03:28 -0400281 uint16_t value = *this->addr16(x, y);
282 return SkColorSetA(0, value * (255 / 65535.0f));
283 }
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400284 case kA16_float_SkColorType: {
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400285 SkHalf value = *this->addr16(x, y);
286 return SkColorSetA(0, 255 * SkHalfToFloat(value));
287 }
Mike Klein9e7f5042018-09-19 14:39:25 +0000288 case kRGB_565_SkColorType: {
289 return SkPixel16ToColor(*this->addr16(x, y));
290 }
291 case kARGB_4444_SkColorType: {
292 uint16_t value = *this->addr16(x, y);
293 SkPMColor c = SkPixel4444ToPixel32(value);
294 return toColor(c);
295 }
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400296 case kR8G8_unorm_SkColorType: {
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400297 uint16_t value = *this->addr16(x, y);
298 return (uint32_t)( ((value >> 0) & 0xff) ) << 16
299 | (uint32_t)( ((value >> 8) & 0xff) ) << 8
300 | 0xff000000;
301 }
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400302 case kR16G16_unorm_SkColorType: {
Robert Phillips429f0d32019-09-11 17:03:28 -0400303 uint32_t value = *this->addr32(x, y);
304 return (uint32_t)( ((value >> 0) & 0xffff) * (255/65535.0f) ) << 16
305 | (uint32_t)( ((value >> 16) & 0xffff) * (255/65535.0f) ) << 8
306 | 0xff000000;
307 }
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400308 case kR16G16_float_SkColorType: {
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400309 uint32_t value = *this->addr32(x, y);
310 uint32_t r = 255 * SkHalfToFloat((value >> 0) & 0xffff);
311 uint32_t g = 255 * SkHalfToFloat((value >> 16) & 0xffff);
312 return (r << 16) | (g << 8) | 0xff000000;
313 }
Mike Klein9e7f5042018-09-19 14:39:25 +0000314 case kRGB_888x_SkColorType: {
315 uint32_t value = *this->addr32(x, y);
316 return SkSwizzle_RB(value | 0xff000000);
317 }
318 case kBGRA_8888_SkColorType: {
319 uint32_t value = *this->addr32(x, y);
320 SkPMColor c = SkSwizzle_BGRA_to_PMColor(value);
321 return toColor(c);
322 }
323 case kRGBA_8888_SkColorType: {
324 uint32_t value = *this->addr32(x, y);
325 SkPMColor c = SkSwizzle_RGBA_to_PMColor(value);
326 return toColor(c);
327 }
328 case kRGB_101010x_SkColorType: {
329 uint32_t value = *this->addr32(x, y);
330 // Convert 10-bit rgb to 8-bit bgr, and mask in 0xff alpha at the top.
331 return (uint32_t)( ((value >> 0) & 0x3ff) * (255/1023.0f) ) << 16
332 | (uint32_t)( ((value >> 10) & 0x3ff) * (255/1023.0f) ) << 8
333 | (uint32_t)( ((value >> 20) & 0x3ff) * (255/1023.0f) ) << 0
334 | 0xff000000;
335 }
336 case kRGBA_1010102_SkColorType: {
337 uint32_t value = *this->addr32(x, y);
338
339 float r = ((value >> 0) & 0x3ff) * (1/1023.0f),
340 g = ((value >> 10) & 0x3ff) * (1/1023.0f),
341 b = ((value >> 20) & 0x3ff) * (1/1023.0f),
342 a = ((value >> 30) & 0x3 ) * (1/ 3.0f);
343 if (a != 0 && needsUnpremul) {
Robert Phillips7f367982019-09-26 14:01:36 -0400344 r = SkTPin(r/a, 0.0f, 1.0f);
345 g = SkTPin(g/a, 0.0f, 1.0f);
346 b = SkTPin(b/a, 0.0f, 1.0f);
Mike Klein9e7f5042018-09-19 14:39:25 +0000347 }
348 return (uint32_t)( r * 255.0f ) << 16
349 | (uint32_t)( g * 255.0f ) << 8
350 | (uint32_t)( b * 255.0f ) << 0
351 | (uint32_t)( a * 255.0f ) << 24;
352 }
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400353 case kR16G16B16A16_unorm_SkColorType: {
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400354 uint64_t value = *this->addr64(x, y);
355
356 float r = ((value ) & 0xffff) * (1/65535.0f),
357 g = ((value >> 16) & 0xffff) * (1/65535.0f),
358 b = ((value >> 32) & 0xffff) * (1/65535.0f),
359 a = ((value >> 48) & 0xffff) * (1/65535.0f);
360 if (a != 0 && needsUnpremul) {
361 r *= (1.0f/a);
362 g *= (1.0f/a);
363 b *= (1.0f/a);
364 }
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 }
Brian Osman1132f742019-03-07 14:03:58 -0500370 case kRGBA_F16Norm_SkColorType:
Mike Klein9e7f5042018-09-19 14:39:25 +0000371 case kRGBA_F16_SkColorType: {
372 const uint64_t* addr =
373 (const uint64_t*)fPixels + y * (fRowBytes >> 3) + x;
374 Sk4f p4 = SkHalfToFloat_finite_ftz(*addr);
375 if (p4[3] && needsUnpremul) {
376 float inva = 1 / p4[3];
377 p4 = p4 * Sk4f(inva, inva, inva, 1);
378 }
379 SkColor c;
380 SkNx_cast<uint8_t>(p4 * Sk4f(255) + Sk4f(0.5f)).store(&c);
381 // p4 is RGBA, but we want BGRA, so we need to swap next
382 return SkSwizzle_RB(c);
383 }
384 case kRGBA_F32_SkColorType: {
385 const float* rgba =
386 (const float*)fPixels + 4*y*(fRowBytes >> 4) + 4*x;
387 Sk4f p4 = Sk4f::Load(rgba);
388 // From here on, just like F16:
389 if (p4[3] && needsUnpremul) {
390 float inva = 1 / p4[3];
391 p4 = p4 * Sk4f(inva, inva, inva, 1);
392 }
393 SkColor c;
394 SkNx_cast<uint8_t>(p4 * Sk4f(255) + Sk4f(0.5f)).store(&c);
395 // p4 is RGBA, but we want BGRA, so we need to swap next
396 return SkSwizzle_RB(c);
397 }
398 default:
399 SkDEBUGFAIL("");
400 return SkColorSetARGB(0, 0, 0, 0);
401 }
Hal Canary94e1a2f2016-10-31 09:38:12 -0400402}
Hal Canary58a76942016-12-07 15:24:59 -0500403
404bool SkPixmap::computeIsOpaque() const {
405 const int height = this->height();
406 const int width = this->width();
407
408 switch (this->colorType()) {
409 case kAlpha_8_SkColorType: {
410 unsigned a = 0xFF;
411 for (int y = 0; y < height; ++y) {
412 const uint8_t* row = this->addr8(0, y);
413 for (int x = 0; x < width; ++x) {
414 a &= row[x];
415 }
416 if (0xFF != a) {
417 return false;
418 }
419 }
420 return true;
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400421 }
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400422 case kA16_unorm_SkColorType: {
Robert Phillips429f0d32019-09-11 17:03:28 -0400423 unsigned a = 0xFFFF;
424 for (int y = 0; y < height; ++y) {
425 const uint16_t* row = this->addr16(0, y);
426 for (int x = 0; x < width; ++x) {
427 a &= row[x];
428 }
429 if (0xFFFF != a) {
430 return false;
431 }
432 }
433 return true;
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400434 }
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400435 case kA16_float_SkColorType: {
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400436 for (int y = 0; y < height; ++y) {
437 const SkHalf* row = this->addr16(0, y);
438 for (int x = 0; x < width; ++x) {
439 if (row[x] < SK_Half1) {
440 return false;
441 }
442 }
443 }
444 return true;
445 }
Hal Canary58a76942016-12-07 15:24:59 -0500446 case kRGB_565_SkColorType:
447 case kGray_8_SkColorType:
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400448 case kR8G8_unorm_SkColorType:
449 case kR16G16_unorm_SkColorType:
450 case kR16G16_float_SkColorType:
Brian Osman2091fbb2018-10-11 11:05:37 -0400451 case kRGB_888x_SkColorType:
452 case kRGB_101010x_SkColorType:
Hal Canary58a76942016-12-07 15:24:59 -0500453 return true;
454 break;
455 case kARGB_4444_SkColorType: {
456 unsigned c = 0xFFFF;
457 for (int y = 0; y < height; ++y) {
458 const SkPMColor16* row = this->addr16(0, y);
459 for (int x = 0; x < width; ++x) {
460 c &= row[x];
461 }
462 if (0xF != SkGetPackedA4444(c)) {
463 return false;
464 }
465 }
466 return true;
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400467 }
Hal Canary58a76942016-12-07 15:24:59 -0500468 case kBGRA_8888_SkColorType:
469 case kRGBA_8888_SkColorType: {
470 SkPMColor c = (SkPMColor)~0;
471 for (int y = 0; y < height; ++y) {
472 const SkPMColor* row = this->addr32(0, y);
473 for (int x = 0; x < width; ++x) {
474 c &= row[x];
475 }
476 if (0xFF != SkGetPackedA32(c)) {
477 return false;
478 }
479 }
480 return true;
481 }
Mike Kleinb70990e2019-02-28 10:03:27 -0600482 case kRGBA_F16Norm_SkColorType:
Hal Canary58a76942016-12-07 15:24:59 -0500483 case kRGBA_F16_SkColorType: {
484 const SkHalf* row = (const SkHalf*)this->addr();
485 for (int y = 0; y < height; ++y) {
486 for (int x = 0; x < width; ++x) {
487 if (row[4 * x + 3] < SK_Half1) {
488 return false;
489 }
490 }
491 row += this->rowBytes() >> 1;
492 }
493 return true;
494 }
Brian Osman2091fbb2018-10-11 11:05:37 -0400495 case kRGBA_F32_SkColorType: {
496 const float* row = (const float*)this->addr();
497 for (int y = 0; y < height; ++y) {
498 for (int x = 0; x < width; ++x) {
499 if (row[4 * x + 3] < 1.0f) {
500 return false;
501 }
502 }
503 row += this->rowBytes() >> 2;
504 }
505 return true;
506 }
507 case kRGBA_1010102_SkColorType: {
508 uint32_t c = ~0;
509 for (int y = 0; y < height; ++y) {
510 const uint32_t* row = this->addr32(0, y);
511 for (int x = 0; x < width; ++x) {
512 c &= row[x];
513 }
514 if (0b11 != c >> 30) {
515 return false;
516 }
517 }
518 return true;
519 }
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400520 case kR16G16B16A16_unorm_SkColorType: {
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400521 uint16_t acc = 0xFFFF;
522 for (int y = 0; y < height; ++y) {
523 const uint64_t* row = this->addr64(0, y);
524 for (int x = 0; x < width; ++x) {
525 acc &= (row[x] >> 48);
526 }
527 if (0xFFFF != acc) {
528 return false;
529 }
530 }
531 return true;
532 }
Brian Osman2091fbb2018-10-11 11:05:37 -0400533 case kUnknown_SkColorType:
534 SkDEBUGFAIL("");
Hal Canary58a76942016-12-07 15:24:59 -0500535 break;
536 }
537 return false;
538}
Mike Reed43798692017-10-17 18:04:32 +0000539
540//////////////////////////////////////////////////////////////////////////////////////////////////
541
Brian Osmanc337a632018-11-30 10:39:32 -0500542static bool draw_orientation(const SkPixmap& dst, const SkPixmap& src, SkEncodedOrigin origin) {
Mike Reed7306bcd2017-10-25 10:37:30 -0400543 auto surf = SkSurface::MakeRasterDirect(dst.info(), dst.writable_addr(), dst.rowBytes());
544 if (!surf) {
545 return false;
Mike Reed43798692017-10-17 18:04:32 +0000546 }
Mike Reed43798692017-10-17 18:04:32 +0000547
Mike Reed7306bcd2017-10-25 10:37:30 -0400548 SkBitmap bm;
549 bm.installPixels(src);
Mike Reed43798692017-10-17 18:04:32 +0000550
Brian Osmanc337a632018-11-30 10:39:32 -0500551 SkMatrix m = SkEncodedOriginToMatrix(origin, src.width(), src.height());
Mike Reed7306bcd2017-10-25 10:37:30 -0400552
Mike Reed7306bcd2017-10-25 10:37:30 -0400553 SkPaint p;
554 p.setBlendMode(SkBlendMode::kSrc);
555 surf->getCanvas()->concat(m);
556 surf->getCanvas()->drawBitmap(bm, 0, 0, &p);
Mike Reed43798692017-10-17 18:04:32 +0000557 return true;
558}
559
Brian Osmanc337a632018-11-30 10:39:32 -0500560bool SkPixmapPriv::Orient(const SkPixmap& dst, const SkPixmap& src, SkEncodedOrigin origin) {
Mike Reed43798692017-10-17 18:04:32 +0000561 if (src.colorType() != dst.colorType()) {
562 return false;
563 }
564 // note: we just ignore alphaType and colorSpace for this transformation
565
566 int w = src.width();
567 int h = src.height();
Brian Osmanc337a632018-11-30 10:39:32 -0500568 if (ShouldSwapWidthHeight(origin)) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400569 using std::swap;
570 swap(w, h);
Mike Reed43798692017-10-17 18:04:32 +0000571 }
572 if (dst.width() != w || dst.height() != h) {
573 return false;
574 }
575 if (w == 0 || h == 0) {
576 return true;
577 }
578
579 // check for aliasing to self
580 if (src.addr() == dst.addr()) {
Brian Osmanc337a632018-11-30 10:39:32 -0500581 return kTopLeft_SkEncodedOrigin == origin;
Mike Reed43798692017-10-17 18:04:32 +0000582 }
Brian Osmanc337a632018-11-30 10:39:32 -0500583 return draw_orientation(dst, src, origin);
Mike Reed43798692017-10-17 18:04:32 +0000584}
585
Brian Osmanc337a632018-11-30 10:39:32 -0500586bool SkPixmapPriv::ShouldSwapWidthHeight(SkEncodedOrigin origin) {
587 // The last four SkEncodedOrigin values involve 90 degree rotations
588 return origin >= kLeftTop_SkEncodedOrigin;
Leon Scroggins III0cbc10f2017-10-30 09:07:53 -0400589}
590
591SkImageInfo SkPixmapPriv::SwapWidthHeight(const SkImageInfo& info) {
592 return info.makeWH(info.height(), info.width());
593}
594