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/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"
28 
29 #include <utility>
30 
31 /////////////////////////////////////////////////////////////////////////////////////////////////
32 
reset()33 void SkPixmap::reset() {
34     fPixels = nullptr;
35     fRowBytes = 0;
36     fInfo = SkImageInfo::MakeUnknown();
37 }
38 
reset(const SkImageInfo & info,const void * addr,size_t rowBytes)39 void SkPixmap::reset(const SkImageInfo& info, const void* addr, size_t rowBytes) {
40     if (addr) {
41         SkASSERT(info.validRowBytes(rowBytes));
42     }
43     fPixels = addr;
44     fRowBytes = rowBytes;
45     fInfo = info;
46 }
47 
reset(const SkMask & src)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 
setColorSpace(sk_sp<SkColorSpace> cs)58 void SkPixmap::setColorSpace(sk_sp<SkColorSpace> cs) {
59     fInfo = fInfo.makeColorSpace(std::move(cs));
60 }
61 
extractSubset(SkPixmap * result,const SkIRect & subset) const62 bool SkPixmap::extractSubset(SkPixmap* result, const SkIRect& subset) const {
63     SkIRect srcRect, r;
64     srcRect.setWH(this->width(), this->height());
65     if (!r.intersect(srcRect, subset)) {
66         return false;   // r is empty (i.e. no intersection)
67     }
68 
69     // 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 
74     const void* pixels = nullptr;
75     if (fPixels) {
76         const size_t bpp = fInfo.bytesPerPixel();
77         pixels = (const uint8_t*)fPixels + r.fTop * fRowBytes + r.fLeft * bpp;
78     }
79     result->reset(fInfo.makeDimensions(r.size()), pixels, fRowBytes);
80     return true;
81 }
82 
83 // 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()).
fast_getaddr(const SkPixmap & pm,int x,int y)86 static 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 
getAlphaf(int x,int y) const91 float 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:
103         case kR8G8_unorm_SkColorType:
104         case kR16G16_unorm_SkColorType:
105         case kR16G16_float_SkColorType:
106         case kRGB_565_SkColorType:
107         case kRGB_888x_SkColorType:
108         case kRGB_101010x_SkColorType:
109         case kBGR_101010x_SkColorType:
110             return 1;
111         case kAlpha_8_SkColorType:
112             value = static_cast<const uint8_t*>(srcPtr)[0] * (1.0f/255);
113             break;
114         case kA16_unorm_SkColorType:
115             value = static_cast<const uint16_t*>(srcPtr)[0] * (1.0f/65535);
116             break;
117         case kA16_float_SkColorType: {
118             SkHalf half = static_cast<const SkHalf*>(srcPtr)[0];
119             value = SkHalfToFloat(half);
120             break;
121         }
122         case kARGB_4444_SkColorType: {
123             uint16_t u16 = static_cast<const uint16_t*>(srcPtr)[0];
124             value = SkGetPackedA4444(u16) * (1.0f/15);
125             break;
126         }
127         case kRGBA_8888_SkColorType:
128         case kBGRA_8888_SkColorType:
129             value = static_cast<const uint8_t*>(srcPtr)[3] * (1.0f/255);
130             break;
131         case kRGBA_1010102_SkColorType:
132         case kBGRA_1010102_SkColorType: {
133             uint32_t u32 = static_cast<const uint32_t*>(srcPtr)[0];
134             value = (u32 >> 30) * (1.0f/3);
135             break;
136         }
137         case kR16G16B16A16_unorm_SkColorType: {
138             uint64_t u64 = static_cast<const uint64_t*>(srcPtr)[0];
139             value = (u64 >> 48) * (1.0f/65535);
140             break;
141         }
142         case kRGBA_F16Norm_SkColorType:
143         case kRGBA_F16_SkColorType: {
144             uint64_t px;
145             memcpy(&px, srcPtr, sizeof(px));
146             value = SkHalfToFloat_finite_ftz(px)[3];
147             break;
148         }
149         case kRGBA_F32_SkColorType:
150             value = static_cast<const float*>(srcPtr)[3];
151             break;
152     }
153     return value;
154 }
155 
readPixels(const SkImageInfo & dstInfo,void * dstPixels,size_t dstRB,int x,int y) const156 bool SkPixmap::readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
157                           int x, int y) const {
158     if (!SkImageInfoValidConversion(dstInfo, fInfo)) {
159         return false;
160     }
161 
162     SkReadPixelsRec rec(dstInfo, dstPixels, dstRB, x, y);
163     if (!rec.trim(fInfo.width(), fInfo.height())) {
164         return false;
165     }
166 
167     const void* srcPixels = this->addr(rec.fX, rec.fY);
168     const SkImageInfo srcInfo = fInfo.makeDimensions(rec.fInfo.dimensions());
169     SkConvertPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, srcInfo, srcPixels, this->rowBytes());
170     return true;
171 }
172 
erase(SkColor color,const SkIRect & subset) const173 bool SkPixmap::erase(SkColor color, const SkIRect& subset) const {
174     return this->erase(SkColor4f::FromColor(color), &subset);
175 }
176 
erase(const SkColor4f & color,const SkIRect * subset) const177 bool SkPixmap::erase(const SkColor4f& color, const SkIRect* subset) const {
178     SkPaint paint;
179     paint.setBlendMode(SkBlendMode::kSrc);
180     paint.setColor4f(color, this->colorSpace());
181 
182     SkIRect clip = this->bounds();
183     if (subset && !clip.intersect(*subset)) {
184         return false;
185     }
186     SkRasterClip rc{clip};
187 
188     SkDraw draw;
189     draw.fDst    = *this;
190     draw.fMatrix = &SkMatrix::I();
191     draw.fRC     = &rc;
192 
193     draw.drawPaint(paint);
194     return true;
195 }
196 
scalePixels(const SkPixmap & actualDst,SkFilterQuality quality) const197 bool SkPixmap::scalePixels(const SkPixmap& actualDst, SkFilterQuality quality) const {
198     // We may need to tweak how we interpret these just a little below, so we make copies.
199     SkPixmap src = *this,
200              dst = actualDst;
201 
202     // Can't do anthing with empty src or dst
203     if (src.width() <= 0 || src.height() <= 0 ||
204         dst.width() <= 0 || dst.height() <= 0) {
205         return false;
206     }
207 
208     // no scaling involved?
209     if (src.width() == dst.width() && src.height() == dst.height()) {
210         return src.readPixels(dst);
211     }
212 
213     // If src and dst are both unpremul, we'll fake the source out to appear as if premul,
214     // and mark the destination as opaque.  This odd combination allows us to scale unpremul
215     // pixels without ever premultiplying them (perhaps losing information in the color channels).
216     // This is an idiosyncratic feature of scalePixels(), and is tested by scalepixels_unpremul GM.
217     bool clampAsIfUnpremul = false;
218     if (src.alphaType() == kUnpremul_SkAlphaType &&
219         dst.alphaType() == kUnpremul_SkAlphaType) {
220         src.reset(src.info().makeAlphaType(kPremul_SkAlphaType), src.addr(), src.rowBytes());
221         dst.reset(dst.info().makeAlphaType(kOpaque_SkAlphaType), dst.addr(), dst.rowBytes());
222 
223         // We'll need to tell the image shader to clamp to [0,1] instead of the
224         // usual [0,a] when using a bicubic scaling (kHigh_SkFilterQuality).
225         clampAsIfUnpremul = true;
226     }
227 
228     SkBitmap bitmap;
229     if (!bitmap.installPixels(src)) {
230         return false;
231     }
232     bitmap.setImmutable();        // Don't copy when we create an image.
233     bitmap.setIsVolatile(true);   // Disable any caching.
234 
235     SkMatrix scale = SkMatrix::MakeRectToRect(SkRect::Make(src.bounds()),
236                                               SkRect::Make(dst.bounds()),
237                                               SkMatrix::kFill_ScaleToFit);
238 
239     // We'll create a shader to do this draw so we have control over the bicubic clamp.
240     sk_sp<SkShader> shader = SkImageShader::Make(SkImage::MakeFromBitmap(bitmap),
241                                                  SkTileMode::kClamp,
242                                                  SkTileMode::kClamp,
243                                                  &scale,
244                                                  clampAsIfUnpremul);
245 
246     sk_sp<SkSurface> surface = SkSurface::MakeRasterDirect(dst.info(),
247                                                            dst.writable_addr(),
248                                                            dst.rowBytes());
249     if (!shader || !surface) {
250         return false;
251     }
252 
253     SkPaint paint;
254     paint.setBlendMode(SkBlendMode::kSrc);
255     paint.setFilterQuality(quality);
256     paint.setShader(std::move(shader));
257     surface->getCanvas()->drawPaint(paint);
258     return true;
259 }
260 
261 //////////////////////////////////////////////////////////////////////////////////////////////////
262 
getColor(int x,int y) const263 SkColor SkPixmap::getColor(int x, int y) const {
264     SkASSERT(this->addr());
265     SkASSERT((unsigned)x < (unsigned)this->width());
266     SkASSERT((unsigned)y < (unsigned)this->height());
267 
268     const bool needsUnpremul = (kPremul_SkAlphaType == fInfo.alphaType());
269     auto toColor = [needsUnpremul](uint32_t maybePremulColor) {
270         return needsUnpremul ? SkUnPreMultiply::PMColorToColor(maybePremulColor)
271                              : SkColorSetARGB(SkGetPackedA32(maybePremulColor), SkGetPackedR32(maybePremulColor),
272 					      SkGetPackedG32(maybePremulColor), SkGetPackedB32(maybePremulColor));
273     };
274 
275     switch (this->colorType()) {
276         case kGray_8_SkColorType: {
277             uint8_t value = *this->addr8(x, y);
278             return SkColorSetRGB(value, value, value);
279         }
280         case kAlpha_8_SkColorType: {
281             return SkColorSetA(0, *this->addr8(x, y));
282         }
283         case kA16_unorm_SkColorType: {
284             uint16_t value = *this->addr16(x, y);
285             return SkColorSetA(0, value * (255 / 65535.0f));
286         }
287         case kA16_float_SkColorType: {
288             SkHalf value = *this->addr16(x, y);
289             return SkColorSetA(0, 255 * SkHalfToFloat(value));
290         }
291         case kRGB_565_SkColorType: {
292             return SkPixel16ToColor(*this->addr16(x, y));
293         }
294         case kARGB_4444_SkColorType: {
295             uint16_t value = *this->addr16(x, y);
296             SkPMColor c = SkPixel4444ToPixel32(value);
297             return toColor(c);
298         }
299         case kR8G8_unorm_SkColorType: {
300             uint16_t value = *this->addr16(x, y);
301             return (uint32_t)( ((value >>  0) & 0xff) ) << 16
302                  | (uint32_t)( ((value >>  8) & 0xff) ) <<  8
303                  | 0xff000000;
304         }
305         case kR16G16_unorm_SkColorType: {
306             uint32_t value = *this->addr32(x, y);
307             return (uint32_t)( ((value >>  0) & 0xffff) * (255/65535.0f) ) << 16
308                  | (uint32_t)( ((value >> 16) & 0xffff) * (255/65535.0f) ) <<  8
309                  | 0xff000000;
310         }
311         case kR16G16_float_SkColorType: {
312             uint32_t value = *this->addr32(x, y);
313             uint32_t r = 255 * SkHalfToFloat((value >>  0) & 0xffff);
314             uint32_t g = 255 * SkHalfToFloat((value >> 16) & 0xffff);
315             return (r << 16) | (g << 8) | 0xff000000;
316         }
317         case kRGB_888x_SkColorType: {
318             uint32_t value = *this->addr32(x, y);
319             return SkSwizzle_RB(value | 0xff000000);
320         }
321         case kBGRA_8888_SkColorType: {
322             uint32_t value = *this->addr32(x, y);
323             SkPMColor c = SkSwizzle_BGRA_to_PMColor(value);
324             return toColor(c);
325         }
326         case kRGBA_8888_SkColorType: {
327             uint32_t value = *this->addr32(x, y);
328             SkPMColor c = SkSwizzle_RGBA_to_PMColor(value);
329             return toColor(c);
330         }
331         case kRGB_101010x_SkColorType: {
332             uint32_t value = *this->addr32(x, y);
333             // Convert 10-bit rgb to 8-bit bgr, and mask in 0xff alpha at the top.
334             return (uint32_t)( ((value >>  0) & 0x3ff) * (255/1023.0f) ) << 16
335                  | (uint32_t)( ((value >> 10) & 0x3ff) * (255/1023.0f) ) <<  8
336                  | (uint32_t)( ((value >> 20) & 0x3ff) * (255/1023.0f) ) <<  0
337                  | 0xff000000;
338         }
339         case kBGR_101010x_SkColorType: {
340             uint32_t value = *this->addr32(x, y);
341             // Convert 10-bit bgr to 8-bit bgr, and mask in 0xff alpha at the top.
342             return (uint32_t)( ((value >>  0) & 0x3ff) * (255/1023.0f) ) <<  0
343                  | (uint32_t)( ((value >> 10) & 0x3ff) * (255/1023.0f) ) <<  8
344                  | (uint32_t)( ((value >> 20) & 0x3ff) * (255/1023.0f) ) << 16
345                  | 0xff000000;
346         }
347         case kRGBA_1010102_SkColorType:
348         case kBGRA_1010102_SkColorType: {
349             uint32_t value = *this->addr32(x, y);
350 
351             float r = ((value >>  0) & 0x3ff) * (1/1023.0f),
352                   g = ((value >> 10) & 0x3ff) * (1/1023.0f),
353                   b = ((value >> 20) & 0x3ff) * (1/1023.0f),
354                   a = ((value >> 30) & 0x3  ) * (1/   3.0f);
355             if (this->colorType() == kBGRA_1010102_SkColorType) {
356                 std::swap(r,b);
357             }
358             if (a != 0 && needsUnpremul) {
359                 r = SkTPin(r/a, 0.0f, 1.0f);
360                 g = SkTPin(g/a, 0.0f, 1.0f);
361                 b = SkTPin(b/a, 0.0f, 1.0f);
362             }
363             return (uint32_t)( r * 255.0f ) << 16
364                  | (uint32_t)( g * 255.0f ) <<  8
365                  | (uint32_t)( b * 255.0f ) <<  0
366                  | (uint32_t)( a * 255.0f ) << 24;
367         }
368         case kR16G16B16A16_unorm_SkColorType: {
369             uint64_t value = *this->addr64(x, y);
370 
371             float r = ((value      ) & 0xffff) * (1/65535.0f),
372                   g = ((value >> 16) & 0xffff) * (1/65535.0f),
373                   b = ((value >> 32) & 0xffff) * (1/65535.0f),
374                   a = ((value >> 48) & 0xffff) * (1/65535.0f);
375             if (a != 0 && needsUnpremul) {
376                 r *= (1.0f/a);
377                 g *= (1.0f/a);
378                 b *= (1.0f/a);
379             }
380             return (uint32_t)( r * 255.0f ) << 16
381                  | (uint32_t)( g * 255.0f ) <<  8
382                  | (uint32_t)( b * 255.0f ) <<  0
383                  | (uint32_t)( a * 255.0f ) << 24;
384         }
385         case kRGBA_F16Norm_SkColorType:
386         case kRGBA_F16_SkColorType: {
387             const uint64_t* addr =
388                 (const uint64_t*)fPixels + y * (fRowBytes >> 3) + x;
389             Sk4f p4 = SkHalfToFloat_finite_ftz(*addr);
390             if (p4[3] && needsUnpremul) {
391                 float inva = 1 / p4[3];
392                 p4 = p4 * Sk4f(inva, inva, inva, 1);
393             }
394             SkColor c;
395             SkNx_cast<uint8_t>(p4 * Sk4f(255) + Sk4f(0.5f)).store(&c);
396             // p4 is RGBA, but we want BGRA, so we need to swap next
397             return SkSwizzle_RB(c);
398         }
399         case kRGBA_F32_SkColorType: {
400             const float* rgba =
401                 (const float*)fPixels + 4*y*(fRowBytes >> 4) + 4*x;
402             Sk4f p4 = Sk4f::Load(rgba);
403             // From here on, just like F16:
404             if (p4[3] && needsUnpremul) {
405                 float inva = 1 / p4[3];
406                 p4 = p4 * Sk4f(inva, inva, inva, 1);
407             }
408             SkColor c;
409             SkNx_cast<uint8_t>(p4 * Sk4f(255) + Sk4f(0.5f)).store(&c);
410             // p4 is RGBA, but we want BGRA, so we need to swap next
411             return SkSwizzle_RB(c);
412         }
413         case kUnknown_SkColorType:
414             break;
415     }
416     SkDEBUGFAIL("");
417     return SkColorSetARGB(0, 0, 0, 0);
418 }
419 
computeIsOpaque() const420 bool SkPixmap::computeIsOpaque() const {
421     const int height = this->height();
422     const int width = this->width();
423 
424     switch (this->colorType()) {
425         case kAlpha_8_SkColorType: {
426             unsigned a = 0xFF;
427             for (int y = 0; y < height; ++y) {
428                 const uint8_t* row = this->addr8(0, y);
429                 for (int x = 0; x < width; ++x) {
430                     a &= row[x];
431                 }
432                 if (0xFF != a) {
433                     return false;
434                 }
435             }
436             return true;
437         }
438         case kA16_unorm_SkColorType: {
439             unsigned a = 0xFFFF;
440             for (int y = 0; y < height; ++y) {
441                 const uint16_t* row = this->addr16(0, y);
442                 for (int x = 0; x < width; ++x) {
443                     a &= row[x];
444                 }
445                 if (0xFFFF != a) {
446                     return false;
447                 }
448             }
449             return true;
450         }
451         case kA16_float_SkColorType: {
452             for (int y = 0; y < height; ++y) {
453                 const SkHalf* row = this->addr16(0, y);
454                 for (int x = 0; x < width; ++x) {
455                     if (row[x] < SK_Half1) {
456                         return false;
457                     }
458                 }
459             }
460             return true;
461         }
462         case kRGB_565_SkColorType:
463         case kGray_8_SkColorType:
464         case kR8G8_unorm_SkColorType:
465         case kR16G16_unorm_SkColorType:
466         case kR16G16_float_SkColorType:
467         case kRGB_888x_SkColorType:
468         case kRGB_101010x_SkColorType:
469         case kBGR_101010x_SkColorType:
470             return true;
471             break;
472         case kARGB_4444_SkColorType: {
473             unsigned c = 0xFFFF;
474             for (int y = 0; y < height; ++y) {
475                 const SkPMColor16* row = this->addr16(0, y);
476                 for (int x = 0; x < width; ++x) {
477                     c &= row[x];
478                 }
479                 if (0xF != SkGetPackedA4444(c)) {
480                     return false;
481                 }
482             }
483             return true;
484         }
485         case kBGRA_8888_SkColorType:
486         case kRGBA_8888_SkColorType: {
487             SkPMColor c = (SkPMColor)~0;
488             for (int y = 0; y < height; ++y) {
489                 const SkPMColor* row = this->addr32(0, y);
490                 for (int x = 0; x < width; ++x) {
491                     c &= row[x];
492                 }
493                 if (0xFF != SkGetPackedA32(c)) {
494                     return false;
495                 }
496             }
497             return true;
498         }
499         case kRGBA_F16Norm_SkColorType:
500         case kRGBA_F16_SkColorType: {
501             const SkHalf* row = (const SkHalf*)this->addr();
502             for (int y = 0; y < height; ++y) {
503                 for (int x = 0; x < width; ++x) {
504                     if (row[4 * x + 3] < SK_Half1) {
505                         return false;
506                     }
507                 }
508                 row += this->rowBytes() >> 1;
509             }
510             return true;
511         }
512         case kRGBA_F32_SkColorType: {
513             const float* row = (const float*)this->addr();
514             for (int y = 0; y < height; ++y) {
515                 for (int x = 0; x < width; ++x) {
516                     if (row[4 * x + 3] < 1.0f) {
517                         return false;
518                     }
519                 }
520                 row += this->rowBytes() >> 2;
521             }
522             return true;
523         }
524         case kRGBA_1010102_SkColorType:
525         case kBGRA_1010102_SkColorType: {
526             uint32_t c = ~0;
527             for (int y = 0; y < height; ++y) {
528                 const uint32_t* row = this->addr32(0, y);
529                 for (int x = 0; x < width; ++x) {
530                     c &= row[x];
531                 }
532                 if (0b11 != c >> 30) {
533                     return false;
534                 }
535             }
536             return true;
537         }
538         case kR16G16B16A16_unorm_SkColorType: {
539             uint16_t acc = 0xFFFF;
540             for (int y = 0; y < height; ++y) {
541                 const uint64_t* row = this->addr64(0, y);
542                 for (int x = 0; x < width; ++x) {
543                     acc &= (row[x] >> 48);
544                 }
545                 if (0xFFFF != acc) {
546                     return false;
547                 }
548             }
549             return true;
550         }
551         case kUnknown_SkColorType:
552             SkDEBUGFAIL("");
553             break;
554     }
555     return false;
556 }
557 
558 //////////////////////////////////////////////////////////////////////////////////////////////////
559 
draw_orientation(const SkPixmap & dst,const SkPixmap & src,SkEncodedOrigin origin)560 static bool draw_orientation(const SkPixmap& dst, const SkPixmap& src, SkEncodedOrigin origin) {
561     auto surf = SkSurface::MakeRasterDirect(dst.info(), dst.writable_addr(), dst.rowBytes());
562     if (!surf) {
563         return false;
564     }
565 
566     SkBitmap bm;
567     bm.installPixels(src);
568 
569     SkMatrix m = SkEncodedOriginToMatrix(origin, src.width(), src.height());
570 
571     SkPaint p;
572     p.setBlendMode(SkBlendMode::kSrc);
573     surf->getCanvas()->concat(m);
574     surf->getCanvas()->drawBitmap(bm, 0, 0, &p);
575     return true;
576 }
577 
Orient(const SkPixmap & dst,const SkPixmap & src,SkEncodedOrigin origin)578 bool SkPixmapPriv::Orient(const SkPixmap& dst, const SkPixmap& src, SkEncodedOrigin origin) {
579     if (src.colorType() != dst.colorType()) {
580         return false;
581     }
582     // note: we just ignore alphaType and colorSpace for this transformation
583 
584     int w = src.width();
585     int h = src.height();
586     if (ShouldSwapWidthHeight(origin)) {
587         using std::swap;
588         swap(w, h);
589     }
590     if (dst.width() != w || dst.height() != h) {
591         return false;
592     }
593     if (w == 0 || h == 0) {
594         return true;
595     }
596 
597     // check for aliasing to self
598     if (src.addr() == dst.addr()) {
599         return kTopLeft_SkEncodedOrigin == origin;
600     }
601     return draw_orientation(dst, src, origin);
602 }
603 
ShouldSwapWidthHeight(SkEncodedOrigin origin)604 bool SkPixmapPriv::ShouldSwapWidthHeight(SkEncodedOrigin origin) {
605     // The last four SkEncodedOrigin values involve 90 degree rotations
606     return origin >= kLeftTop_SkEncodedOrigin;
607 }
608 
SwapWidthHeight(const SkImageInfo & info)609 SkImageInfo SkPixmapPriv::SwapWidthHeight(const SkImageInfo& info) {
610     return info.makeWH(info.height(), info.width());
611 }
612 
613