blob: dcbba9afb080976c77162628bdd7d7bfc8f5a84f [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2008 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkBitmap.h"
11#include "SkColorPriv.h"
12#include "SkDither.h"
13#include "SkFlattenable.h"
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000014#include "SkImagePriv.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkMallocPixelRef.h"
16#include "SkMask.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000017#include "SkReadBuffer.h"
18#include "SkWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000019#include "SkPixelRef.h"
20#include "SkThread.h"
vandebo@chromium.org112706d2011-02-24 22:50:55 +000021#include "SkUnPreMultiply.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000022#include "SkUtils.h"
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +000023#include "SkValidationUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000024#include "SkPackBits.h"
25#include <new>
26
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +000027static bool reset_return_false(SkBitmap* bm) {
28 bm->reset();
29 return false;
30}
31
reed@android.com8a1c16f2008-12-17 15:59:43 +000032SkBitmap::SkBitmap() {
reed@android.com4516f472009-06-29 16:25:36 +000033 sk_bzero(this, sizeof(*this));
reed@android.com8a1c16f2008-12-17 15:59:43 +000034}
35
36SkBitmap::SkBitmap(const SkBitmap& src) {
37 SkDEBUGCODE(src.validate();)
reed@android.com4516f472009-06-29 16:25:36 +000038 sk_bzero(this, sizeof(*this));
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 *this = src;
40 SkDEBUGCODE(this->validate();)
41}
42
43SkBitmap::~SkBitmap() {
44 SkDEBUGCODE(this->validate();)
45 this->freePixels();
46}
47
48SkBitmap& SkBitmap::operator=(const SkBitmap& src) {
49 if (this != &src) {
50 this->freePixels();
51 memcpy(this, &src, sizeof(src));
52
53 // inc src reference counts
reed@android.com83f7bc32009-07-17 02:42:41 +000054 SkSafeRef(src.fPixelRef);
reed@android.com8a1c16f2008-12-17 15:59:43 +000055
56 // we reset our locks if we get blown away
57 fPixelLockCount = 0;
weita@google.comf9ab99a2009-05-03 18:23:30 +000058
reed@google.com5f62ed72014-01-15 19:59:45 +000059 if (fPixelRef) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000060 // ignore the values from the memcpy
61 fPixels = NULL;
62 fColorTable = NULL;
bsalomon@google.com586f48c2011-04-14 15:07:22 +000063 // Note that what to for genID is somewhat arbitrary. We have no
64 // way to track changes to raw pixels across multiple SkBitmaps.
65 // Would benefit from an SkRawPixelRef type created by
66 // setPixels.
67 // Just leave the memcpy'ed one but they'll get out of sync
68 // as soon either is modified.
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 }
70 }
71
72 SkDEBUGCODE(this->validate();)
73 return *this;
74}
75
76void SkBitmap::swap(SkBitmap& other) {
bsalomon@google.com586f48c2011-04-14 15:07:22 +000077 SkTSwap(fColorTable, other.fColorTable);
78 SkTSwap(fPixelRef, other.fPixelRef);
reed@google.com672588b2014-01-08 15:42:01 +000079 SkTSwap(fPixelRefOrigin, other.fPixelRefOrigin);
bsalomon@google.com586f48c2011-04-14 15:07:22 +000080 SkTSwap(fPixelLockCount, other.fPixelLockCount);
bsalomon@google.com586f48c2011-04-14 15:07:22 +000081 SkTSwap(fPixels, other.fPixels);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +000082 SkTSwap(fInfo, other.fInfo);
bsalomon@google.com586f48c2011-04-14 15:07:22 +000083 SkTSwap(fRowBytes, other.fRowBytes);
bsalomon@google.com586f48c2011-04-14 15:07:22 +000084 SkTSwap(fFlags, other.fFlags);
reed@android.com8a1c16f2008-12-17 15:59:43 +000085
86 SkDEBUGCODE(this->validate();)
87}
88
89void SkBitmap::reset() {
90 this->freePixels();
reed@android.com4516f472009-06-29 16:25:36 +000091 sk_bzero(this, sizeof(*this));
reed@android.com8a1c16f2008-12-17 15:59:43 +000092}
93
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +000094SkBitmap::Config SkBitmap::config() const {
95 return SkColorTypeToBitmapConfig(fInfo.colorType());
96}
97
reedddd014e2014-06-05 08:51:20 -070098#ifdef SK_SUPPORT_LEGACY_COMPUTE_CONFIG_SIZE
reed@android.com8a1c16f2008-12-17 15:59:43 +000099int SkBitmap::ComputeBytesPerPixel(SkBitmap::Config config) {
100 int bpp;
101 switch (config) {
102 case kNo_Config:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 bpp = 0; // not applicable
104 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 case kA8_Config:
106 case kIndex8_Config:
107 bpp = 1;
108 break;
109 case kRGB_565_Config:
110 case kARGB_4444_Config:
111 bpp = 2;
112 break;
113 case kARGB_8888_Config:
114 bpp = 4;
115 break;
116 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000117 SkDEBUGFAIL("unknown config");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 bpp = 0; // error
119 break;
120 }
121 return bpp;
122}
123
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000124size_t SkBitmap::ComputeRowBytes(Config c, int width) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000125 return SkColorTypeMinRowBytes(SkBitmapConfigToColorType(c), width);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126}
127
reed@google.com57212f92013-12-30 14:40:38 +0000128int64_t SkBitmap::ComputeSize64(Config config, int width, int height) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000129 SkColorType ct = SkBitmapConfigToColorType(config);
130 int64_t rowBytes = sk_64_mul(SkColorTypeBytesPerPixel(ct), width);
reed@google.com57212f92013-12-30 14:40:38 +0000131 return rowBytes * height;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132}
133
134size_t SkBitmap::ComputeSize(Config c, int width, int height) {
reed@google.com57212f92013-12-30 14:40:38 +0000135 int64_t size = SkBitmap::ComputeSize64(c, width, height);
136 return sk_64_isS32(size) ? sk_64_asS32(size) : 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137}
reedddd014e2014-06-05 08:51:20 -0700138#endif
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000139
reed@google.com86b2e432012-03-15 21:17:03 +0000140void SkBitmap::getBounds(SkRect* bounds) const {
141 SkASSERT(bounds);
142 bounds->set(0, 0,
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000143 SkIntToScalar(fInfo.fWidth), SkIntToScalar(fInfo.fHeight));
reed@google.com86b2e432012-03-15 21:17:03 +0000144}
145
reed@google.com80e14592012-03-16 14:58:07 +0000146void SkBitmap::getBounds(SkIRect* bounds) const {
147 SkASSERT(bounds);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000148 bounds->set(0, 0, fInfo.fWidth, fInfo.fHeight);
reed@google.com80e14592012-03-16 14:58:07 +0000149}
150
reed@google.com86b2e432012-03-15 21:17:03 +0000151///////////////////////////////////////////////////////////////////////////////
152
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000153static bool validate_alphaType(SkColorType colorType, SkAlphaType alphaType,
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000154 SkAlphaType* canonical = NULL) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000155 switch (colorType) {
156 case kUnknown_SkColorType:
reed@google.com383a6972013-10-21 14:00:07 +0000157 alphaType = kIgnore_SkAlphaType;
158 break;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000159 case kAlpha_8_SkColorType:
reed@google.com383a6972013-10-21 14:00:07 +0000160 if (kUnpremul_SkAlphaType == alphaType) {
161 alphaType = kPremul_SkAlphaType;
162 }
163 // fall-through
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000164 case kIndex_8_SkColorType:
165 case kARGB_4444_SkColorType:
166 case kRGBA_8888_SkColorType:
167 case kBGRA_8888_SkColorType:
reed@google.com383a6972013-10-21 14:00:07 +0000168 if (kIgnore_SkAlphaType == alphaType) {
169 return false;
170 }
171 break;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000172 case kRGB_565_SkColorType:
reed@google.com383a6972013-10-21 14:00:07 +0000173 alphaType = kOpaque_SkAlphaType;
174 break;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000175 default:
176 return false;
reed@android.com149e2f62009-05-22 14:39:03 +0000177 }
reed@google.com383a6972013-10-21 14:00:07 +0000178 if (canonical) {
179 *canonical = alphaType;
180 }
181 return true;
182}
reed@android.com149e2f62009-05-22 14:39:03 +0000183
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000184bool SkBitmap::setInfo(const SkImageInfo& origInfo, size_t rowBytes) {
commit-bot@chromium.orgd5414e52014-02-13 22:30:38 +0000185 SkImageInfo info = origInfo;
186
187 if (!validate_alphaType(info.fColorType, info.fAlphaType,
188 &info.fAlphaType)) {
189 return reset_return_false(this);
190 }
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000191
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000192 // require that rowBytes fit in 31bits
193 int64_t mrb = info.minRowBytes64();
194 if ((int32_t)mrb != mrb) {
195 return reset_return_false(this);
reed@google.com383a6972013-10-21 14:00:07 +0000196 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000197 if ((int64_t)rowBytes != (int32_t)rowBytes) {
198 return reset_return_false(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000199 }
reed@android.com89bb83a2009-05-29 21:30:42 +0000200
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000201 if (info.width() < 0 || info.height() < 0) {
202 return reset_return_false(this);
203 }
204
205 if (kUnknown_SkColorType == info.colorType()) {
206 rowBytes = 0;
207 } else if (0 == rowBytes) {
208 rowBytes = (size_t)mrb;
209 } else if (rowBytes < info.minRowBytes()) {
210 return reset_return_false(this);
reed@google.com383a6972013-10-21 14:00:07 +0000211 }
212
213 this->freePixels();
214
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000215 fInfo = info;
216 fRowBytes = SkToU32(rowBytes);
reed@google.com383a6972013-10-21 14:00:07 +0000217 return true;
reed@google.com383a6972013-10-21 14:00:07 +0000218}
219
reed6c225732014-06-09 19:52:07 -0700220#ifdef SK_SUPPORT_LEGACY_SETCONFIG
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000221bool SkBitmap::setConfig(Config config, int width, int height, size_t rowBytes,
222 SkAlphaType alphaType) {
223 SkColorType ct = SkBitmapConfigToColorType(config);
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000224 return this->setInfo(SkImageInfo::Make(width, height, ct, alphaType), rowBytes);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000225}
reed6c225732014-06-09 19:52:07 -0700226#endif
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000227
reed@google.com383a6972013-10-21 14:00:07 +0000228bool SkBitmap::setAlphaType(SkAlphaType alphaType) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000229 if (!validate_alphaType(fInfo.fColorType, alphaType, &alphaType)) {
reed@google.com383a6972013-10-21 14:00:07 +0000230 return false;
231 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000232 if (fInfo.fAlphaType != alphaType) {
233 fInfo.fAlphaType = alphaType;
commit-bot@chromium.org0e8d0d62014-01-27 15:41:07 +0000234 if (fPixelRef) {
reed@google.comc1587f92014-01-28 16:05:39 +0000235 fPixelRef->changeAlphaType(alphaType);
commit-bot@chromium.org0e8d0d62014-01-27 15:41:07 +0000236 }
237 }
reed@google.com383a6972013-10-21 14:00:07 +0000238 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000239}
240
241void SkBitmap::updatePixelsFromRef() const {
242 if (NULL != fPixelRef) {
243 if (fPixelLockCount > 0) {
reed@google.comff0da4f2012-05-17 13:14:52 +0000244 SkASSERT(fPixelRef->isLocked());
weita@google.comf9ab99a2009-05-03 18:23:30 +0000245
reed@android.com8a1c16f2008-12-17 15:59:43 +0000246 void* p = fPixelRef->pixels();
247 if (NULL != p) {
reed@google.com672588b2014-01-08 15:42:01 +0000248 p = (char*)p
reed@google.com303c4752014-01-09 20:00:14 +0000249 + fPixelRefOrigin.fY * fRowBytes
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000250 + fPixelRefOrigin.fX * fInfo.bytesPerPixel();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000251 }
252 fPixels = p;
reed@google.com5f62ed72014-01-15 19:59:45 +0000253 fColorTable = fPixelRef->colorTable();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000254 } else {
255 SkASSERT(0 == fPixelLockCount);
256 fPixels = NULL;
reed@google.com5f62ed72014-01-15 19:59:45 +0000257 fColorTable = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000258 }
259 }
260}
261
reed@google.com672588b2014-01-08 15:42:01 +0000262SkPixelRef* SkBitmap::setPixelRef(SkPixelRef* pr, int dx, int dy) {
reed@google.comdcea5302014-01-03 13:43:01 +0000263#ifdef SK_DEBUG
reed@google.com672588b2014-01-08 15:42:01 +0000264 if (pr) {
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000265 if (kUnknown_SkColorType != fInfo.colorType()) {
reed@google.comdcea5302014-01-03 13:43:01 +0000266 const SkImageInfo& prInfo = pr->info();
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000267 SkASSERT(fInfo.fWidth <= prInfo.fWidth);
268 SkASSERT(fInfo.fHeight <= prInfo.fHeight);
269 SkASSERT(fInfo.fColorType == prInfo.fColorType);
reed@google.comdcea5302014-01-03 13:43:01 +0000270 switch (prInfo.fAlphaType) {
271 case kIgnore_SkAlphaType:
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000272 SkASSERT(fInfo.fAlphaType == kIgnore_SkAlphaType);
reed@google.comdcea5302014-01-03 13:43:01 +0000273 break;
274 case kOpaque_SkAlphaType:
275 case kPremul_SkAlphaType:
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000276 SkASSERT(fInfo.fAlphaType == kOpaque_SkAlphaType ||
277 fInfo.fAlphaType == kPremul_SkAlphaType);
reed@google.comdcea5302014-01-03 13:43:01 +0000278 break;
279 case kUnpremul_SkAlphaType:
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000280 SkASSERT(fInfo.fAlphaType == kOpaque_SkAlphaType ||
281 fInfo.fAlphaType == kUnpremul_SkAlphaType);
reed@google.comdcea5302014-01-03 13:43:01 +0000282 break;
283 }
284 }
285 }
286#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000287
reed@google.com672588b2014-01-08 15:42:01 +0000288 if (pr) {
289 const SkImageInfo& info = pr->info();
290 fPixelRefOrigin.set(SkPin32(dx, 0, info.fWidth),
291 SkPin32(dy, 0, info.fHeight));
292 } else {
293 // ignore dx,dy if there is no pixelref
294 fPixelRefOrigin.setZero();
295 }
296
297 if (fPixelRef != pr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000298 if (fPixelRef != pr) {
299 this->freePixels();
300 SkASSERT(NULL == fPixelRef);
weita@google.comf9ab99a2009-05-03 18:23:30 +0000301
reed@google.com82065d62011-02-07 15:30:46 +0000302 SkSafeRef(pr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000303 fPixelRef = pr;
304 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000305 this->updatePixelsFromRef();
306 }
307
308 SkDEBUGCODE(this->validate();)
309 return pr;
310}
311
312void SkBitmap::lockPixels() const {
djsollen@google.com7c6d2642013-08-06 12:19:38 +0000313 if (NULL != fPixelRef && 0 == sk_atomic_inc(&fPixelLockCount)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000314 fPixelRef->lockPixels();
315 this->updatePixelsFromRef();
316 }
317 SkDEBUGCODE(this->validate();)
318}
319
320void SkBitmap::unlockPixels() const {
321 SkASSERT(NULL == fPixelRef || fPixelLockCount > 0);
322
djsollen@google.com7c6d2642013-08-06 12:19:38 +0000323 if (NULL != fPixelRef && 1 == sk_atomic_dec(&fPixelLockCount)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000324 fPixelRef->unlockPixels();
325 this->updatePixelsFromRef();
326 }
327 SkDEBUGCODE(this->validate();)
328}
329
reed@google.com9c49bc32011-07-07 13:42:37 +0000330bool SkBitmap::lockPixelsAreWritable() const {
djsollen@google.comc84b8332012-07-27 13:41:44 +0000331 return (fPixelRef) ? fPixelRef->lockPixelsAreWritable() : false;
reed@google.com9c49bc32011-07-07 13:42:37 +0000332}
333
reed@android.com8a1c16f2008-12-17 15:59:43 +0000334void SkBitmap::setPixels(void* p, SkColorTable* ctable) {
reed@google.com8e1034e2012-07-30 13:16:35 +0000335 if (NULL == p) {
reed@google.com672588b2014-01-08 15:42:01 +0000336 this->setPixelRef(NULL);
reed@google.com8e1034e2012-07-30 13:16:35 +0000337 return;
338 }
339
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000340 if (kUnknown_SkColorType == fInfo.colorType()) {
reed@google.com672588b2014-01-08 15:42:01 +0000341 this->setPixelRef(NULL);
reed@google.combf790232013-12-13 19:45:58 +0000342 return;
343 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000344
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000345 SkPixelRef* pr = SkMallocPixelRef::NewDirect(fInfo, p, fRowBytes, ctable);
reed@google.combf790232013-12-13 19:45:58 +0000346 if (NULL == pr) {
reed@google.com672588b2014-01-08 15:42:01 +0000347 this->setPixelRef(NULL);
reed@google.combf790232013-12-13 19:45:58 +0000348 return;
349 }
350
351 this->setPixelRef(pr)->unref();
352
djsollen@google.comc84b8332012-07-27 13:41:44 +0000353 // since we're already allocated, we lockPixels right away
354 this->lockPixels();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000355 SkDEBUGCODE(this->validate();)
356}
357
358bool SkBitmap::allocPixels(Allocator* allocator, SkColorTable* ctable) {
359 HeapAllocator stdalloc;
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000360
reed@android.com8a1c16f2008-12-17 15:59:43 +0000361 if (NULL == allocator) {
362 allocator = &stdalloc;
363 }
364 return allocator->allocPixelRef(this, ctable);
365}
366
reed@google.com9ebcac52014-01-24 18:53:42 +0000367///////////////////////////////////////////////////////////////////////////////
368
scroggo0187dc22014-06-05 11:18:04 -0700369bool SkBitmap::allocPixels(const SkImageInfo& requestedInfo, SkPixelRefFactory* factory,
reed@google.com9ebcac52014-01-24 18:53:42 +0000370 SkColorTable* ctable) {
scroggo0187dc22014-06-05 11:18:04 -0700371 if (kIndex_8_SkColorType == requestedInfo.fColorType && NULL == ctable) {
reed@google.com9ebcac52014-01-24 18:53:42 +0000372 return reset_return_false(this);
373 }
scroggo0187dc22014-06-05 11:18:04 -0700374 if (!this->setInfo(requestedInfo)) {
reed@google.com9ebcac52014-01-24 18:53:42 +0000375 return reset_return_false(this);
376 }
377
scroggo0187dc22014-06-05 11:18:04 -0700378 // setInfo may have corrected info (e.g. 565 is always opaque).
379 const SkImageInfo& correctedInfo = this->info();
380
reed@google.com9ebcac52014-01-24 18:53:42 +0000381 SkMallocPixelRef::PRFactory defaultFactory;
382 if (NULL == factory) {
383 factory = &defaultFactory;
384 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000385
scroggo0187dc22014-06-05 11:18:04 -0700386 SkPixelRef* pr = factory->create(correctedInfo, ctable);
reed@google.com9ebcac52014-01-24 18:53:42 +0000387 if (NULL == pr) {
388 return reset_return_false(this);
389 }
390 this->setPixelRef(pr)->unref();
391
392 // TODO: lockPixels could/should return bool or void*/NULL
393 this->lockPixels();
394 if (NULL == this->getPixels()) {
395 return reset_return_false(this);
396 }
397 return true;
398}
399
scroggo0187dc22014-06-05 11:18:04 -0700400bool SkBitmap::installPixels(const SkImageInfo& requestedInfo, void* pixels, size_t rb,
401 SkColorTable* ct, void (*releaseProc)(void* addr, void* context),
402 void* context) {
403 if (!this->setInfo(requestedInfo, rb)) {
reed@google.com9ebcac52014-01-24 18:53:42 +0000404 this->reset();
405 return false;
406 }
407
scroggo0187dc22014-06-05 11:18:04 -0700408 // setInfo may have corrected info (e.g. 565 is always opaque).
409 const SkImageInfo& correctedInfo = this->info();
410
411 SkPixelRef* pr = SkMallocPixelRef::NewWithProc(correctedInfo, rb, ct, pixels, releaseProc,
412 context);
reed@google.com9ebcac52014-01-24 18:53:42 +0000413 if (!pr) {
414 this->reset();
415 return false;
416 }
417
418 this->setPixelRef(pr)->unref();
mike@reedtribe.org6e58cf32014-02-16 20:54:21 +0000419
420 // since we're already allocated, we lockPixels right away
421 this->lockPixels();
mike@reedtribe.org6e58cf32014-02-16 20:54:21 +0000422 SkDEBUGCODE(this->validate();)
reed@google.com9ebcac52014-01-24 18:53:42 +0000423 return true;
424}
425
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000426bool SkBitmap::installMaskPixels(const SkMask& mask) {
427 if (SkMask::kA8_Format != mask.fFormat) {
428 this->reset();
429 return false;
430 }
431 return this->installPixels(SkImageInfo::MakeA8(mask.fBounds.width(),
432 mask.fBounds.height()),
433 mask.fImage, mask.fRowBytes);
434}
435
reed@google.comeb9a46c2014-01-25 16:46:20 +0000436///////////////////////////////////////////////////////////////////////////////
437
reed@android.com8a1c16f2008-12-17 15:59:43 +0000438void SkBitmap::freePixels() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000439 if (NULL != fPixelRef) {
440 if (fPixelLockCount > 0) {
441 fPixelRef->unlockPixels();
442 }
443 fPixelRef->unref();
444 fPixelRef = NULL;
reed@google.com672588b2014-01-08 15:42:01 +0000445 fPixelRefOrigin.setZero();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000446 }
447 fPixelLockCount = 0;
448 fPixels = NULL;
reed@google.com5f62ed72014-01-15 19:59:45 +0000449 fColorTable = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000450}
451
reed@android.com8a1c16f2008-12-17 15:59:43 +0000452uint32_t SkBitmap::getGenerationID() const {
djsollen@google.comc84b8332012-07-27 13:41:44 +0000453 return (fPixelRef) ? fPixelRef->getGenerationID() : 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000454}
455
456void SkBitmap::notifyPixelsChanged() const {
junov@chromium.orgb0521292011-12-15 20:14:06 +0000457 SkASSERT(!this->isImmutable());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000458 if (fPixelRef) {
459 fPixelRef->notifyPixelsChanged();
460 }
461}
462
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000463GrTexture* SkBitmap::getTexture() const {
reed@android.comce4e53a2010-09-09 16:01:26 +0000464 return fPixelRef ? fPixelRef->getTexture() : NULL;
465}
466
reed@android.com8a1c16f2008-12-17 15:59:43 +0000467///////////////////////////////////////////////////////////////////////////////
468
reed@android.com8a1c16f2008-12-17 15:59:43 +0000469/** We explicitly use the same allocator for our pixels that SkMask does,
470 so that we can freely assign memory allocated by one class to the other.
471 */
472bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst,
473 SkColorTable* ctable) {
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000474 const SkImageInfo info = dst->info();
475 if (kUnknown_SkColorType == info.colorType()) {
reed@google.combf790232013-12-13 19:45:58 +0000476// SkDebugf("unsupported config for info %d\n", dst->config());
477 return false;
478 }
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000479
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000480 SkPixelRef* pr = SkMallocPixelRef::NewAllocate(info, dst->rowBytes(), ctable);
reed@google.combf790232013-12-13 19:45:58 +0000481 if (NULL == pr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000482 return false;
483 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000484
reed@google.com672588b2014-01-08 15:42:01 +0000485 dst->setPixelRef(pr)->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000486 // since we're already allocated, we lockPixels right away
487 dst->lockPixels();
488 return true;
489}
490
491///////////////////////////////////////////////////////////////////////////////
492
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000493bool SkBitmap::copyPixelsTo(void* const dst, size_t dstSize,
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000494 size_t dstRowBytes, bool preserveDstPad) const {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000495
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000496 if (0 == dstRowBytes) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000497 dstRowBytes = fRowBytes;
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000498 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000499
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000500 if (dstRowBytes < fInfo.minRowBytes() ||
501 dst == NULL || (getPixels() == NULL && pixelRef() == NULL)) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000502 return false;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000503 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000504
bsalomon@google.comc6980972011-11-02 19:57:21 +0000505 if (!preserveDstPad && static_cast<uint32_t>(dstRowBytes) == fRowBytes) {
reed@google.com44699382013-10-31 17:28:30 +0000506 size_t safeSize = this->getSafeSize();
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000507 if (safeSize > dstSize || safeSize == 0)
508 return false;
509 else {
510 SkAutoLockPixels lock(*this);
511 // This implementation will write bytes beyond the end of each row,
512 // excluding the last row, if the bitmap's stride is greater than
513 // strictly required by the current config.
514 memcpy(dst, getPixels(), safeSize);
515
516 return true;
517 }
518 } else {
519 // If destination has different stride than us, then copy line by line.
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000520 if (fInfo.getSafeSize(dstRowBytes) > dstSize) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000521 return false;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000522 } else {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000523 // Just copy what we need on each line.
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000524 size_t rowBytes = fInfo.minRowBytes();
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000525 SkAutoLockPixels lock(*this);
526 const uint8_t* srcP = reinterpret_cast<const uint8_t*>(getPixels());
527 uint8_t* dstP = reinterpret_cast<uint8_t*>(dst);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000528 for (int row = 0; row < fInfo.fHeight;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000529 row++, srcP += fRowBytes, dstP += dstRowBytes) {
530 memcpy(dstP, srcP, rowBytes);
531 }
532
533 return true;
534 }
535 }
536}
537
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000538///////////////////////////////////////////////////////////////////////////////
539
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000540bool SkBitmap::isImmutable() const {
junov@chromium.orgb0521292011-12-15 20:14:06 +0000541 return fPixelRef ? fPixelRef->isImmutable() :
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000542 fFlags & kImageIsImmutable_Flag;
junov@chromium.orgb0521292011-12-15 20:14:06 +0000543}
544
545void SkBitmap::setImmutable() {
546 if (fPixelRef) {
547 fPixelRef->setImmutable();
548 } else {
549 fFlags |= kImageIsImmutable_Flag;
550 }
551}
552
junov@google.com4ee7ae52011-06-30 17:30:49 +0000553bool SkBitmap::isVolatile() const {
554 return (fFlags & kImageIsVolatile_Flag) != 0;
555}
556
557void SkBitmap::setIsVolatile(bool isVolatile) {
558 if (isVolatile) {
559 fFlags |= kImageIsVolatile_Flag;
560 } else {
561 fFlags &= ~kImageIsVolatile_Flag;
562 }
563}
564
reed@android.com8a1c16f2008-12-17 15:59:43 +0000565void* SkBitmap::getAddr(int x, int y) const {
566 SkASSERT((unsigned)x < (unsigned)this->width());
567 SkASSERT((unsigned)y < (unsigned)this->height());
568
569 char* base = (char*)this->getPixels();
570 if (base) {
571 base += y * this->rowBytes();
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000572 switch (this->colorType()) {
573 case kRGBA_8888_SkColorType:
574 case kBGRA_8888_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000575 base += x << 2;
576 break;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000577 case kARGB_4444_SkColorType:
578 case kRGB_565_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000579 base += x << 1;
580 break;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000581 case kAlpha_8_SkColorType:
582 case kIndex_8_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000583 base += x;
584 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000585 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000586 SkDEBUGFAIL("Can't return addr for config");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000587 base = NULL;
588 break;
589 }
590 }
591 return base;
592}
593
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000594SkColor SkBitmap::getColor(int x, int y) const {
595 SkASSERT((unsigned)x < (unsigned)this->width());
596 SkASSERT((unsigned)y < (unsigned)this->height());
597
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000598 switch (this->colorType()) {
599 case kAlpha_8_SkColorType: {
reed@google.com3b521d02011-04-29 11:53:41 +0000600 uint8_t* addr = this->getAddr8(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000601 return SkColorSetA(0, addr[0]);
602 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000603 case kIndex_8_SkColorType: {
reed@google.com3b521d02011-04-29 11:53:41 +0000604 SkPMColor c = this->getIndex8Color(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000605 return SkUnPreMultiply::PMColorToColor(c);
606 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000607 case kRGB_565_SkColorType: {
reed@google.com3b521d02011-04-29 11:53:41 +0000608 uint16_t* addr = this->getAddr16(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000609 return SkPixel16ToColor(addr[0]);
610 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000611 case kARGB_4444_SkColorType: {
reed@google.com3b521d02011-04-29 11:53:41 +0000612 uint16_t* addr = this->getAddr16(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000613 SkPMColor c = SkPixel4444ToPixel32(addr[0]);
614 return SkUnPreMultiply::PMColorToColor(c);
615 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000616 case kBGRA_8888_SkColorType:
617 case kRGBA_8888_SkColorType: {
reed@google.com3b521d02011-04-29 11:53:41 +0000618 uint32_t* addr = this->getAddr32(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000619 return SkUnPreMultiply::PMColorToColor(addr[0]);
620 }
rmistry@google.comd6bab022013-12-02 13:50:38 +0000621 default:
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000622 SkASSERT(false);
623 return 0;
624 }
625 SkASSERT(false); // Not reached.
626 return 0;
627}
628
reed@google.com2a7579d2012-11-07 18:30:18 +0000629bool SkBitmap::ComputeIsOpaque(const SkBitmap& bm) {
630 SkAutoLockPixels alp(bm);
631 if (!bm.getPixels()) {
632 return false;
633 }
634
635 const int height = bm.height();
636 const int width = bm.width();
637
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000638 switch (bm.colorType()) {
639 case kAlpha_8_SkColorType: {
reed@google.com2a7579d2012-11-07 18:30:18 +0000640 unsigned a = 0xFF;
641 for (int y = 0; y < height; ++y) {
642 const uint8_t* row = bm.getAddr8(0, y);
643 for (int x = 0; x < width; ++x) {
644 a &= row[x];
645 }
646 if (0xFF != a) {
647 return false;
648 }
649 }
650 return true;
651 } break;
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000652 case kIndex_8_SkColorType: {
reed@google.com2a7579d2012-11-07 18:30:18 +0000653 SkAutoLockColors alc(bm);
654 const SkPMColor* table = alc.colors();
655 if (!table) {
656 return false;
657 }
reed@google.com140d7282013-01-07 20:25:04 +0000658 SkPMColor c = (SkPMColor)~0;
reed@google.com2a7579d2012-11-07 18:30:18 +0000659 for (int i = bm.getColorTable()->count() - 1; i >= 0; --i) {
660 c &= table[i];
661 }
662 return 0xFF == SkGetPackedA32(c);
663 } break;
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000664 case kRGB_565_SkColorType:
reed@google.com2a7579d2012-11-07 18:30:18 +0000665 return true;
666 break;
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000667 case kARGB_4444_SkColorType: {
reed@google.com2a7579d2012-11-07 18:30:18 +0000668 unsigned c = 0xFFFF;
669 for (int y = 0; y < height; ++y) {
670 const SkPMColor16* row = bm.getAddr16(0, y);
671 for (int x = 0; x < width; ++x) {
672 c &= row[x];
673 }
674 if (0xF != SkGetPackedA4444(c)) {
675 return false;
676 }
677 }
678 return true;
679 } break;
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000680 case kBGRA_8888_SkColorType:
681 case kRGBA_8888_SkColorType: {
reed@google.com140d7282013-01-07 20:25:04 +0000682 SkPMColor c = (SkPMColor)~0;
reed@google.com2a7579d2012-11-07 18:30:18 +0000683 for (int y = 0; y < height; ++y) {
684 const SkPMColor* row = bm.getAddr32(0, y);
685 for (int x = 0; x < width; ++x) {
686 c &= row[x];
687 }
688 if (0xFF != SkGetPackedA32(c)) {
689 return false;
690 }
691 }
692 return true;
693 }
694 default:
695 break;
696 }
697 return false;
698}
699
700
reed@android.com8a1c16f2008-12-17 15:59:43 +0000701///////////////////////////////////////////////////////////////////////////////
702///////////////////////////////////////////////////////////////////////////////
703
reed@google.com45f746f2013-06-21 19:51:31 +0000704static uint16_t pack_8888_to_4444(unsigned a, unsigned r, unsigned g, unsigned b) {
705 unsigned pixel = (SkA32To4444(a) << SK_A4444_SHIFT) |
706 (SkR32To4444(r) << SK_R4444_SHIFT) |
707 (SkG32To4444(g) << SK_G4444_SHIFT) |
708 (SkB32To4444(b) << SK_B4444_SHIFT);
709 return SkToU16(pixel);
710}
711
reed@google.com60d32352013-06-28 19:40:50 +0000712void SkBitmap::internalErase(const SkIRect& area,
713 U8CPU a, U8CPU r, U8CPU g, U8CPU b) const {
714#ifdef SK_DEBUG
reed@android.com8a1c16f2008-12-17 15:59:43 +0000715 SkDEBUGCODE(this->validate();)
reed@google.com60d32352013-06-28 19:40:50 +0000716 SkASSERT(!area.isEmpty());
717 {
reed@google.com92833f92013-06-28 19:47:43 +0000718 SkIRect total = { 0, 0, this->width(), this->height() };
reed@google.com60d32352013-06-28 19:40:50 +0000719 SkASSERT(total.contains(area));
720 }
721#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000722
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000723 switch (fInfo.colorType()) {
724 case kUnknown_SkColorType:
725 case kIndex_8_SkColorType:
commit-bot@chromium.org7669a772014-03-27 15:30:35 +0000726 return; // can't erase. Should we bzero so the memory is not uninitialized?
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000727 default:
728 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000729 }
730
731 SkAutoLockPixels alp(*this);
732 // perform this check after the lock call
733 if (!this->readyToDraw()) {
734 return;
735 }
736
reed@google.com60d32352013-06-28 19:40:50 +0000737 int height = area.height();
738 const int width = area.width();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000739 const int rowBytes = fRowBytes;
740
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000741 switch (this->colorType()) {
742 case kAlpha_8_SkColorType: {
reed@google.com60d32352013-06-28 19:40:50 +0000743 uint8_t* p = this->getAddr8(area.fLeft, area.fTop);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000744 while (--height >= 0) {
745 memset(p, a, width);
746 p += rowBytes;
747 }
748 break;
749 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000750 case kARGB_4444_SkColorType:
751 case kRGB_565_SkColorType: {
reed@google.com60d32352013-06-28 19:40:50 +0000752 uint16_t* p = this->getAddr16(area.fLeft, area.fTop);;
reed@google.com45f746f2013-06-21 19:51:31 +0000753 uint16_t v;
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000754
commit-bot@chromium.org7669a772014-03-27 15:30:35 +0000755 // make rgb premultiplied
756 if (255 != a) {
757 r = SkAlphaMul(r, a);
758 g = SkAlphaMul(g, a);
759 b = SkAlphaMul(b, a);
760 }
761
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000762 if (kARGB_4444_SkColorType == this->colorType()) {
reed@google.com45f746f2013-06-21 19:51:31 +0000763 v = pack_8888_to_4444(a, r, g, b);
764 } else {
765 v = SkPackRGB16(r >> (8 - SK_R16_BITS),
766 g >> (8 - SK_G16_BITS),
767 b >> (8 - SK_B16_BITS));
768 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000769 while (--height >= 0) {
770 sk_memset16(p, v, width);
771 p = (uint16_t*)((char*)p + rowBytes);
772 }
773 break;
774 }
commit-bot@chromium.org7669a772014-03-27 15:30:35 +0000775 case kBGRA_8888_SkColorType:
776 case kRGBA_8888_SkColorType: {
reed@google.com60d32352013-06-28 19:40:50 +0000777 uint32_t* p = this->getAddr32(area.fLeft, area.fTop);
commit-bot@chromium.org7669a772014-03-27 15:30:35 +0000778
779 if (255 != a && kPremul_SkAlphaType == this->alphaType()) {
780 r = SkAlphaMul(r, a);
781 g = SkAlphaMul(g, a);
782 b = SkAlphaMul(b, a);
783 }
784 uint32_t v = kRGBA_8888_SkColorType == this->colorType() ?
785 SkPackARGB_as_RGBA(a, r, g, b) : SkPackARGB_as_BGRA(a, r, g, b);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000786
787 while (--height >= 0) {
788 sk_memset32(p, v, width);
789 p = (uint32_t*)((char*)p + rowBytes);
790 }
791 break;
792 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000793 default:
794 return; // no change, so don't call notifyPixelsChanged()
reed@android.com8a1c16f2008-12-17 15:59:43 +0000795 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000796
reed@android.com8a1c16f2008-12-17 15:59:43 +0000797 this->notifyPixelsChanged();
798}
799
reed@google.com60d32352013-06-28 19:40:50 +0000800void SkBitmap::eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const {
reed@google.com92833f92013-06-28 19:47:43 +0000801 SkIRect area = { 0, 0, this->width(), this->height() };
reed@google.com60d32352013-06-28 19:40:50 +0000802 if (!area.isEmpty()) {
803 this->internalErase(area, a, r, g, b);
804 }
805}
806
807void SkBitmap::eraseArea(const SkIRect& rect, SkColor c) const {
reed@google.com92833f92013-06-28 19:47:43 +0000808 SkIRect area = { 0, 0, this->width(), this->height() };
reed@google.com60d32352013-06-28 19:40:50 +0000809 if (area.intersect(rect)) {
810 this->internalErase(area, SkColorGetA(c), SkColorGetR(c),
811 SkColorGetG(c), SkColorGetB(c));
812 }
813}
814
reed@android.com8a1c16f2008-12-17 15:59:43 +0000815//////////////////////////////////////////////////////////////////////////////////////
816//////////////////////////////////////////////////////////////////////////////////////
817
reed@android.com8a1c16f2008-12-17 15:59:43 +0000818bool SkBitmap::extractSubset(SkBitmap* result, const SkIRect& subset) const {
819 SkDEBUGCODE(this->validate();)
820
djsollen@google.comc84b8332012-07-27 13:41:44 +0000821 if (NULL == result || NULL == fPixelRef) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000822 return false; // no src pixels
823 }
824
825 SkIRect srcRect, r;
826 srcRect.set(0, 0, this->width(), this->height());
827 if (!r.intersect(srcRect, subset)) {
828 return false; // r is empty (i.e. no intersection)
829 }
830
scroggo@google.coma2a31922012-12-07 19:14:45 +0000831 if (fPixelRef->getTexture() != NULL) {
832 // Do a deep copy
reede4538f52014-06-11 06:09:50 -0700833 SkPixelRef* pixelRef = fPixelRef->deepCopy(this->colorType(), &subset);
scroggo@google.coma2a31922012-12-07 19:14:45 +0000834 if (pixelRef != NULL) {
835 SkBitmap dst;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000836 dst.setInfo(SkImageInfo::Make(subset.width(), subset.height(),
837 this->colorType(), this->alphaType()));
scroggo@google.coma2a31922012-12-07 19:14:45 +0000838 dst.setIsVolatile(this->isVolatile());
scroggo@google.coma2a31922012-12-07 19:14:45 +0000839 dst.setPixelRef(pixelRef)->unref();
840 SkDEBUGCODE(dst.validate());
841 result->swap(dst);
842 return true;
843 }
844 }
845
scroggo@google.coma2a31922012-12-07 19:14:45 +0000846 // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have
847 // exited above.
848 SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width()));
849 SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height()));
850
reed@android.com8a1c16f2008-12-17 15:59:43 +0000851 SkBitmap dst;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000852 dst.setInfo(SkImageInfo::Make(r.width(), r.height(), this->colorType(), this->alphaType()),
853 this->rowBytes());
skyostil@google.com0eb75762012-01-16 10:45:53 +0000854 dst.setIsVolatile(this->isVolatile());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000855
856 if (fPixelRef) {
reed@google.com672588b2014-01-08 15:42:01 +0000857 SkIPoint origin = fPixelRefOrigin;
858 origin.fX += r.fLeft;
859 origin.fY += r.fTop;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000860 // share the pixelref with a custom offset
reed@google.com672588b2014-01-08 15:42:01 +0000861 dst.setPixelRef(fPixelRef, origin);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000862 }
863 SkDEBUGCODE(dst.validate();)
864
865 // we know we're good, so commit to result
866 result->swap(dst);
867 return true;
868}
869
870///////////////////////////////////////////////////////////////////////////////
871
872#include "SkCanvas.h"
873#include "SkPaint.h"
874
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000875bool SkBitmap::canCopyTo(SkColorType dstColorType) const {
876 if (this->colorType() == kUnknown_SkColorType) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000877 return false;
878 }
879
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000880 bool sameConfigs = (this->colorType() == dstColorType);
881 switch (dstColorType) {
882 case kAlpha_8_SkColorType:
883 case kRGB_565_SkColorType:
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000884 case kRGBA_8888_SkColorType:
885 case kBGRA_8888_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000886 break;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000887 case kIndex_8_SkColorType:
weita@google.comf9ab99a2009-05-03 18:23:30 +0000888 if (!sameConfigs) {
889 return false;
890 }
891 break;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000892 case kARGB_4444_SkColorType:
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000893 return sameConfigs || kN32_SkColorType == this->colorType();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000894 default:
895 return false;
896 }
reed@android.comfbaa88d2009-05-06 17:44:34 +0000897 return true;
898}
899
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000900bool SkBitmap::copyTo(SkBitmap* dst, SkColorType dstColorType,
901 Allocator* alloc) const {
902 if (!this->canCopyTo(dstColorType)) {
reed@android.comfbaa88d2009-05-06 17:44:34 +0000903 return false;
904 }
905
reed@google.com50dfa012011-04-01 19:05:36 +0000906 // if we have a texture, first get those pixels
907 SkBitmap tmpSrc;
908 const SkBitmap* src = this;
909
scroggo@google.coma2a31922012-12-07 19:14:45 +0000910 if (fPixelRef) {
911 SkIRect subset;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000912 subset.setXYWH(fPixelRefOrigin.fX, fPixelRefOrigin.fY,
913 fInfo.width(), fInfo.height());
reed@google.com672588b2014-01-08 15:42:01 +0000914 if (fPixelRef->readPixels(&tmpSrc, &subset)) {
scroggo0187dc22014-06-05 11:18:04 -0700915 if (fPixelRef->info().alphaType() == kUnpremul_SkAlphaType) {
916 // FIXME: The only meaningful implementation of readPixels
917 // (GrPixelRef) assumes premultiplied pixels.
918 return false;
919 }
reed@google.com672588b2014-01-08 15:42:01 +0000920 SkASSERT(tmpSrc.width() == this->width());
921 SkASSERT(tmpSrc.height() == this->height());
reed@google.com50dfa012011-04-01 19:05:36 +0000922
reed@google.com672588b2014-01-08 15:42:01 +0000923 // did we get lucky and we can just return tmpSrc?
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000924 if (tmpSrc.colorType() == dstColorType && NULL == alloc) {
reed@google.com672588b2014-01-08 15:42:01 +0000925 dst->swap(tmpSrc);
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000926 // If the result is an exact copy, clone the gen ID.
927 if (dst->pixelRef() && dst->pixelRef()->info() == fPixelRef->info()) {
reed@google.com672588b2014-01-08 15:42:01 +0000928 dst->pixelRef()->cloneGenID(*fPixelRef);
scroggo@google.coma2a31922012-12-07 19:14:45 +0000929 }
reed@google.com672588b2014-01-08 15:42:01 +0000930 return true;
scroggo@google.comd5764e82012-08-22 15:00:05 +0000931 }
reed@google.com672588b2014-01-08 15:42:01 +0000932
933 // fall through to the raster case
934 src = &tmpSrc;
reed@google.com50dfa012011-04-01 19:05:36 +0000935 }
reed@android.comfbaa88d2009-05-06 17:44:34 +0000936 }
reed@android.com311c82d2009-05-05 23:13:23 +0000937
reed@google.com50dfa012011-04-01 19:05:36 +0000938 // we lock this now, since we may need its colortable
939 SkAutoLockPixels srclock(*src);
940 if (!src->readyToDraw()) {
941 return false;
942 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000943
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000944 // The only way to be readyToDraw is if fPixelRef is non NULL.
945 SkASSERT(fPixelRef != NULL);
946
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000947 SkImageInfo dstInfo = src->info();
948 dstInfo.fColorType = dstColorType;
949
reed@google.com50dfa012011-04-01 19:05:36 +0000950 SkBitmap tmpDst;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000951 if (!tmpDst.setInfo(dstInfo)) {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000952 return false;
953 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000954
weita@google.comf9ab99a2009-05-03 18:23:30 +0000955 // allocate colortable if srcConfig == kIndex8_Config
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000956 SkAutoTUnref<SkColorTable> ctable;
957 if (dstColorType == kIndex_8_SkColorType) {
958 // TODO: can we just ref() the src colortable? Is it reentrant-safe?
959 ctable.reset(SkNEW_ARGS(SkColorTable, (*src->getColorTable())));
960 }
reed@google.com50dfa012011-04-01 19:05:36 +0000961 if (!tmpDst.allocPixels(alloc, ctable)) {
weita@google.comf9ab99a2009-05-03 18:23:30 +0000962 return false;
963 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000964
reed@google.com50dfa012011-04-01 19:05:36 +0000965 if (!tmpDst.readyToDraw()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000966 // allocator/lock failed
967 return false;
968 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000969
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000970 // pixelRef must be non NULL or tmpDst.readyToDraw() would have
971 // returned false.
972 SkASSERT(tmpDst.pixelRef() != NULL);
973
reed@android.comfbaa88d2009-05-06 17:44:34 +0000974 /* do memcpy for the same configs cases, else use drawing
weita@google.comf9ab99a2009-05-03 18:23:30 +0000975 */
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000976 if (src->colorType() == dstColorType) {
reed@google.com50dfa012011-04-01 19:05:36 +0000977 if (tmpDst.getSize() == src->getSize()) {
978 memcpy(tmpDst.getPixels(), src->getPixels(), src->getSafeSize());
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000979
scroggo0187dc22014-06-05 11:18:04 -0700980 SkPixelRef* dstPixelRef = tmpDst.pixelRef();
981 if (dstPixelRef->info() == fPixelRef->info()) {
982 dstPixelRef->cloneGenID(*fPixelRef);
scroggo@google.comd5764e82012-08-22 15:00:05 +0000983 }
reed@android.com311c82d2009-05-05 23:13:23 +0000984 } else {
reed@google.com50dfa012011-04-01 19:05:36 +0000985 const char* srcP = reinterpret_cast<const char*>(src->getPixels());
986 char* dstP = reinterpret_cast<char*>(tmpDst.getPixels());
reed@android.com311c82d2009-05-05 23:13:23 +0000987 // to be sure we don't read too much, only copy our logical pixels
reed@google.com50dfa012011-04-01 19:05:36 +0000988 size_t bytesToCopy = tmpDst.width() * tmpDst.bytesPerPixel();
989 for (int y = 0; y < tmpDst.height(); y++) {
reed@android.com311c82d2009-05-05 23:13:23 +0000990 memcpy(dstP, srcP, bytesToCopy);
reed@google.com50dfa012011-04-01 19:05:36 +0000991 srcP += src->rowBytes();
992 dstP += tmpDst.rowBytes();
reed@android.com311c82d2009-05-05 23:13:23 +0000993 }
994 }
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000995 } else if (kARGB_4444_SkColorType == dstColorType
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000996 && kN32_SkColorType == src->colorType()) {
scroggo0187dc22014-06-05 11:18:04 -0700997 if (src->alphaType() == kUnpremul_SkAlphaType) {
998 // Our method for converting to 4444 assumes premultiplied.
999 return false;
1000 }
scroggo@google.com8dc8bc52013-08-07 19:16:05 +00001001 SkASSERT(src->height() == tmpDst.height());
1002 SkASSERT(src->width() == tmpDst.width());
1003 for (int y = 0; y < src->height(); ++y) {
1004 SkPMColor16* SK_RESTRICT dstRow = (SkPMColor16*) tmpDst.getAddr16(0, y);
1005 SkPMColor* SK_RESTRICT srcRow = (SkPMColor*) src->getAddr32(0, y);
1006 DITHER_4444_SCAN(y);
1007 for (int x = 0; x < src->width(); ++x) {
1008 dstRow[x] = SkDitherARGB32To4444(srcRow[x],
1009 DITHER_VALUE(x));
1010 }
1011 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001012 } else {
scroggo0187dc22014-06-05 11:18:04 -07001013 if (tmpDst.alphaType() == kUnpremul_SkAlphaType) {
1014 // We do not support drawing to unpremultiplied bitmaps.
1015 return false;
1016 }
1017
robertphillips@google.com0197b322013-10-10 15:48:16 +00001018 // Always clear the dest in case one of the blitters accesses it
1019 // TODO: switch the allocation of tmpDst to call sk_calloc_throw
1020 tmpDst.eraseColor(SK_ColorTRANSPARENT);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001021
reed@google.com50dfa012011-04-01 19:05:36 +00001022 SkCanvas canvas(tmpDst);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001023 SkPaint paint;
1024
1025 paint.setDither(true);
reed@google.com50dfa012011-04-01 19:05:36 +00001026 canvas.drawBitmap(*src, 0, 0, &paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001027 }
1028
reed@google.com50dfa012011-04-01 19:05:36 +00001029 dst->swap(tmpDst);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001030 return true;
1031}
1032
commit-bot@chromium.orgfab349c2014-03-05 02:34:58 +00001033bool SkBitmap::deepCopyTo(SkBitmap* dst) const {
reede4538f52014-06-11 06:09:50 -07001034 const SkColorType dstCT = this->colorType();
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001035
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +00001036 if (!this->canCopyTo(dstCT)) {
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001037 return false;
1038 }
1039
1040 // If we have a PixelRef, and it supports deep copy, use it.
1041 // Currently supported only by texture-backed bitmaps.
1042 if (fPixelRef) {
reede4538f52014-06-11 06:09:50 -07001043 SkPixelRef* pixelRef = fPixelRef->deepCopy(dstCT, NULL);
commit-bot@chromium.orgeeef0cc2014-05-21 15:58:00 +00001044 if (pixelRef) {
1045 uint32_t rowBytes;
1046 if (this->colorType() == dstCT) {
1047 // Since there is no subset to pass to deepCopy, and deepCopy
1048 // succeeded, the new pixel ref must be identical.
1049 SkASSERT(fPixelRef->info() == pixelRef->info());
1050 pixelRef->cloneGenID(*fPixelRef);
1051 // Use the same rowBytes as the original.
1052 rowBytes = fRowBytes;
1053 } else {
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001054 // With the new config, an appropriate fRowBytes will be computed by setInfo.
commit-bot@chromium.orgeeef0cc2014-05-21 15:58:00 +00001055 rowBytes = 0;
1056 }
1057
1058 SkImageInfo info = fInfo;
1059 info.fColorType = dstCT;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001060 if (!dst->setInfo(info, rowBytes)) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001061 return false;
1062 }
commit-bot@chromium.orgeeef0cc2014-05-21 15:58:00 +00001063 dst->setPixelRef(pixelRef, fPixelRefOrigin)->unref();
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001064 return true;
1065 }
1066 }
1067
1068 if (this->getTexture()) {
1069 return false;
1070 } else {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +00001071 return this->copyTo(dst, dstCT, NULL);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001072 }
1073}
1074
reed@android.com8a1c16f2008-12-17 15:59:43 +00001075///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +00001076
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +00001077static bool GetBitmapAlpha(const SkBitmap& src, uint8_t* SK_RESTRICT alpha,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001078 int alphaRowBytes) {
1079 SkASSERT(alpha != NULL);
1080 SkASSERT(alphaRowBytes >= src.width());
1081
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001082 SkColorType colorType = src.colorType();
1083 int w = src.width();
1084 int h = src.height();
1085 size_t rb = src.rowBytes();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001086
reed@android.com1cdcb512009-08-24 19:11:00 +00001087 SkAutoLockPixels alp(src);
1088 if (!src.readyToDraw()) {
1089 // zero out the alpha buffer and return
1090 while (--h >= 0) {
1091 memset(alpha, 0, w);
1092 alpha += alphaRowBytes;
1093 }
1094 return false;
1095 }
reed@google.com82065d62011-02-07 15:30:46 +00001096
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001097 if (kAlpha_8_SkColorType == colorType && !src.isOpaque()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001098 const uint8_t* s = src.getAddr8(0, 0);
1099 while (--h >= 0) {
1100 memcpy(alpha, s, w);
1101 s += rb;
1102 alpha += alphaRowBytes;
1103 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001104 } else if (kN32_SkColorType == colorType && !src.isOpaque()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001105 const SkPMColor* SK_RESTRICT s = src.getAddr32(0, 0);
1106 while (--h >= 0) {
1107 for (int x = 0; x < w; x++) {
1108 alpha[x] = SkGetPackedA32(s[x]);
1109 }
1110 s = (const SkPMColor*)((const char*)s + rb);
1111 alpha += alphaRowBytes;
1112 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001113 } else if (kARGB_4444_SkColorType == colorType && !src.isOpaque()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001114 const SkPMColor16* SK_RESTRICT s = src.getAddr16(0, 0);
1115 while (--h >= 0) {
1116 for (int x = 0; x < w; x++) {
1117 alpha[x] = SkPacked4444ToA32(s[x]);
1118 }
1119 s = (const SkPMColor16*)((const char*)s + rb);
1120 alpha += alphaRowBytes;
1121 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001122 } else if (kIndex_8_SkColorType == colorType && !src.isOpaque()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001123 SkColorTable* ct = src.getColorTable();
1124 if (ct) {
1125 const SkPMColor* SK_RESTRICT table = ct->lockColors();
1126 const uint8_t* SK_RESTRICT s = src.getAddr8(0, 0);
1127 while (--h >= 0) {
1128 for (int x = 0; x < w; x++) {
1129 alpha[x] = SkGetPackedA32(table[s[x]]);
1130 }
1131 s += rb;
1132 alpha += alphaRowBytes;
1133 }
reed@google.com0a6151d2013-10-10 14:44:56 +00001134 ct->unlockColors();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001135 }
1136 } else { // src is opaque, so just fill alpha[] with 0xFF
1137 memset(alpha, 0xFF, h * alphaRowBytes);
1138 }
reed@android.com1cdcb512009-08-24 19:11:00 +00001139 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001140}
1141
1142#include "SkPaint.h"
1143#include "SkMaskFilter.h"
1144#include "SkMatrix.h"
1145
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001146bool SkBitmap::extractAlpha(SkBitmap* dst, const SkPaint* paint,
djsollen@google.com57f49692011-02-23 20:46:31 +00001147 Allocator *allocator, SkIPoint* offset) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001148 SkDEBUGCODE(this->validate();)
1149
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001150 SkBitmap tmpBitmap;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001151 SkMatrix identity;
1152 SkMask srcM, dstM;
1153
1154 srcM.fBounds.set(0, 0, this->width(), this->height());
1155 srcM.fRowBytes = SkAlign4(this->width());
1156 srcM.fFormat = SkMask::kA8_Format;
1157
1158 SkMaskFilter* filter = paint ? paint->getMaskFilter() : NULL;
1159
1160 // compute our (larger?) dst bounds if we have a filter
1161 if (NULL != filter) {
1162 identity.reset();
1163 srcM.fImage = NULL;
1164 if (!filter->filterMask(&dstM, srcM, identity, NULL)) {
1165 goto NO_FILTER_CASE;
1166 }
1167 dstM.fRowBytes = SkAlign4(dstM.fBounds.width());
1168 } else {
1169 NO_FILTER_CASE:
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001170 tmpBitmap.setInfo(SkImageInfo::MakeA8(this->width(), this->height()), srcM.fRowBytes);
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001171 if (!tmpBitmap.allocPixels(allocator, NULL)) {
1172 // Allocation of pixels for alpha bitmap failed.
1173 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
1174 tmpBitmap.width(), tmpBitmap.height());
1175 return false;
1176 }
1177 GetBitmapAlpha(*this, tmpBitmap.getAddr8(0, 0), srcM.fRowBytes);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001178 if (offset) {
1179 offset->set(0, 0);
1180 }
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001181 tmpBitmap.swap(*dst);
1182 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001183 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001184 srcM.fImage = SkMask::AllocImage(srcM.computeImageSize());
1185 SkAutoMaskFreeImage srcCleanup(srcM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001186
1187 GetBitmapAlpha(*this, srcM.fImage, srcM.fRowBytes);
1188 if (!filter->filterMask(&dstM, srcM, identity, NULL)) {
1189 goto NO_FILTER_CASE;
1190 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001191 SkAutoMaskFreeImage dstCleanup(dstM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001192
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001193 tmpBitmap.setInfo(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.height()),
1194 dstM.fRowBytes);
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001195 if (!tmpBitmap.allocPixels(allocator, NULL)) {
1196 // Allocation of pixels for alpha bitmap failed.
1197 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
1198 tmpBitmap.width(), tmpBitmap.height());
1199 return false;
1200 }
1201 memcpy(tmpBitmap.getPixels(), dstM.fImage, dstM.computeImageSize());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001202 if (offset) {
1203 offset->set(dstM.fBounds.fLeft, dstM.fBounds.fTop);
1204 }
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001205 SkDEBUGCODE(tmpBitmap.validate();)
1206
1207 tmpBitmap.swap(*dst);
1208 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001209}
1210
1211///////////////////////////////////////////////////////////////////////////////
1212
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001213void SkBitmap::WriteRawPixels(SkWriteBuffer* buffer, const SkBitmap& bitmap) {
1214 const SkImageInfo info = bitmap.info();
1215 SkAutoLockPixels alp(bitmap);
1216 if (0 == info.width() || 0 == info.height() || NULL == bitmap.getPixels()) {
1217 buffer->writeUInt(0); // instead of snugRB, signaling no pixels
1218 return;
1219 }
1220
1221 const size_t snugRB = info.width() * info.bytesPerPixel();
1222 const char* src = (const char*)bitmap.getPixels();
1223 const size_t ramRB = bitmap.rowBytes();
skia.committer@gmail.com3c134a92014-05-24 03:05:26 +00001224
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001225 buffer->write32(SkToU32(snugRB));
1226 info.flatten(*buffer);
1227
1228 const size_t size = snugRB * info.height();
1229 SkAutoMalloc storage(size);
1230 char* dst = (char*)storage.get();
1231 for (int y = 0; y < info.height(); ++y) {
1232 memcpy(dst, src, snugRB);
1233 dst += snugRB;
1234 src += ramRB;
1235 }
1236 buffer->writeByteArray(storage.get(), size);
1237
1238 SkColorTable* ct = bitmap.getColorTable();
1239 if (kIndex_8_SkColorType == info.colorType() && ct) {
1240 buffer->writeBool(true);
1241 ct->writeToBuffer(*buffer);
1242 } else {
1243 buffer->writeBool(false);
1244 }
1245}
1246
1247bool SkBitmap::ReadRawPixels(SkReadBuffer* buffer, SkBitmap* bitmap) {
1248 const size_t snugRB = buffer->readUInt();
1249 if (0 == snugRB) { // no pixels
1250 return false;
1251 }
1252
1253 SkImageInfo info;
1254 info.unflatten(*buffer);
1255
1256 const size_t ramRB = info.minRowBytes();
1257 const int height = info.height();
1258 const size_t snugSize = snugRB * height;
1259 const size_t ramSize = ramRB * height;
commit-bot@chromium.org05858432014-05-30 01:06:44 +00001260 if (!buffer->validate(snugSize <= ramSize)) {
1261 return false;
1262 }
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001263
1264 char* dst = (char*)sk_malloc_throw(ramSize);
1265 buffer->readByteArray(dst, snugSize);
1266 SkAutoDataUnref data(SkData::NewFromMalloc(dst, ramSize));
1267
1268 if (snugSize != ramSize) {
1269 const char* srcRow = dst + snugRB * (height - 1);
1270 char* dstRow = dst + ramRB * (height - 1);
1271 for (int y = height - 1; y >= 1; --y) {
1272 memmove(dstRow, srcRow, snugRB);
1273 srcRow -= snugRB;
1274 dstRow -= ramRB;
1275 }
1276 SkASSERT(srcRow == dstRow); // first row does not need to be moved
1277 }
skia.committer@gmail.com3c134a92014-05-24 03:05:26 +00001278
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001279 SkAutoTUnref<SkColorTable> ctable;
1280 if (buffer->readBool()) {
1281 ctable.reset(SkNEW_ARGS(SkColorTable, (*buffer)));
1282 }
skia.committer@gmail.com3c134a92014-05-24 03:05:26 +00001283
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001284 SkAutoTUnref<SkPixelRef> pr(SkMallocPixelRef::NewWithData(info, info.minRowBytes(),
1285 ctable.get(), data.get()));
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001286 bitmap->setInfo(pr->info());
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001287 bitmap->setPixelRef(pr, 0, 0);
1288 return true;
1289}
1290
reed@android.com8a1c16f2008-12-17 15:59:43 +00001291enum {
1292 SERIALIZE_PIXELTYPE_NONE,
djsollen@google.com21830d92012-08-07 19:49:41 +00001293 SERIALIZE_PIXELTYPE_REF_DATA
reed@android.com8a1c16f2008-12-17 15:59:43 +00001294};
1295
commit-bot@chromium.org851155c2014-05-27 14:03:51 +00001296void SkBitmap::legacyUnflatten(SkReadBuffer& buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001297 this->reset();
weita@google.comf9ab99a2009-05-03 18:23:30 +00001298
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001299 SkImageInfo info;
1300 info.unflatten(buffer);
1301 size_t rowBytes = buffer.readInt();
commit-bot@chromium.org33fed142014-02-13 18:46:13 +00001302 if (!buffer.validate((info.width() >= 0) && (info.height() >= 0) &&
1303 SkColorTypeIsValid(info.fColorType) &&
1304 SkAlphaTypeIsValid(info.fAlphaType) &&
1305 validate_alphaType(info.fColorType, info.fAlphaType) &&
1306 info.validRowBytes(rowBytes))) {
1307 return;
1308 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001309
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001310 bool configIsValid = this->setInfo(info, rowBytes);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001311 buffer.validate(configIsValid);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001312
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001313 int reftype = buffer.readInt();
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +00001314 if (buffer.validate((SERIALIZE_PIXELTYPE_REF_DATA == reftype) ||
1315 (SERIALIZE_PIXELTYPE_NONE == reftype))) {
1316 switch (reftype) {
1317 case SERIALIZE_PIXELTYPE_REF_DATA: {
reed@google.com672588b2014-01-08 15:42:01 +00001318 SkIPoint origin;
1319 origin.fX = buffer.readInt();
1320 origin.fY = buffer.readInt();
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001321 size_t offset = origin.fY * rowBytes + origin.fX * info.bytesPerPixel();
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +00001322 SkPixelRef* pr = buffer.readPixelRef();
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +00001323 if (!buffer.validate((NULL == pr) ||
1324 (pr->getAllocatedSizeInBytes() >= (offset + this->getSafeSize())))) {
reed@google.com672588b2014-01-08 15:42:01 +00001325 origin.setZero();
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +00001326 }
reed@google.com672588b2014-01-08 15:42:01 +00001327 SkSafeUnref(this->setPixelRef(pr, origin));
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +00001328 break;
1329 }
1330 case SERIALIZE_PIXELTYPE_NONE:
1331 break;
1332 default:
1333 SkDEBUGFAIL("unrecognized pixeltype in serialized data");
1334 sk_throw();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001335 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001336 }
1337}
1338
1339///////////////////////////////////////////////////////////////////////////////
1340
1341SkBitmap::RLEPixels::RLEPixels(int width, int height) {
1342 fHeight = height;
commit-bot@chromium.org235002f2013-10-09 18:39:59 +00001343 fYPtrs = (uint8_t**)sk_calloc_throw(height * sizeof(uint8_t*));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001344}
1345
1346SkBitmap::RLEPixels::~RLEPixels() {
1347 sk_free(fYPtrs);
1348}
1349
1350///////////////////////////////////////////////////////////////////////////////
1351
1352#ifdef SK_DEBUG
1353void SkBitmap::validate() const {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001354 fInfo.validate();
commit-bot@chromium.orgd5414e52014-02-13 22:30:38 +00001355
1356 // ImageInfo may not require this, but Bitmap ensures that opaque-only
1357 // colorTypes report opaque for their alphatype
1358 if (kRGB_565_SkColorType == fInfo.colorType()) {
1359 SkASSERT(kOpaque_SkAlphaType == fInfo.alphaType());
1360 }
1361
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001362 SkASSERT(fInfo.validRowBytes(fRowBytes));
scroggo@google.com8e990eb2013-06-14 15:55:56 +00001363 uint8_t allFlags = kImageIsOpaque_Flag | kImageIsVolatile_Flag | kImageIsImmutable_Flag;
1364#ifdef SK_BUILD_FOR_ANDROID
1365 allFlags |= kHasHardwareMipMap_Flag;
1366#endif
1367 SkASSERT(fFlags <= allFlags);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001368 SkASSERT(fPixelLockCount >= 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001369
reed@google.com615316c2014-01-15 19:15:23 +00001370 if (fPixels) {
1371 SkASSERT(fPixelRef);
1372 SkASSERT(fPixelLockCount > 0);
1373 SkASSERT(fPixelRef->isLocked());
1374 SkASSERT(fPixelRef->rowBytes() == fRowBytes);
1375 SkASSERT(fPixelRefOrigin.fX >= 0);
1376 SkASSERT(fPixelRefOrigin.fY >= 0);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001377 SkASSERT(fPixelRef->info().width() >= (int)this->width() + fPixelRefOrigin.fX);
1378 SkASSERT(fPixelRef->info().fHeight >= (int)this->height() + fPixelRefOrigin.fY);
1379 SkASSERT(fPixelRef->rowBytes() >= fInfo.minRowBytes());
reed@google.com615316c2014-01-15 19:15:23 +00001380 } else {
1381 SkASSERT(NULL == fColorTable);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001382 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001383}
1384#endif
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001385
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +00001386#ifndef SK_IGNORE_TO_STRING
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001387void SkBitmap::toString(SkString* str) const {
1388
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001389 static const char* gColorTypeNames[kLastEnum_SkColorType + 1] = {
1390 "UNKNOWN", "A8", "565", "4444", "RGBA", "BGRA", "INDEX8",
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001391 };
1392
1393 str->appendf("bitmap: ((%d, %d) %s", this->width(), this->height(),
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001394 gColorTypeNames[this->colorType()]);
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001395
1396 str->append(" (");
1397 if (this->isOpaque()) {
1398 str->append("opaque");
1399 } else {
1400 str->append("transparent");
1401 }
1402 if (this->isImmutable()) {
1403 str->append(", immutable");
1404 } else {
1405 str->append(", not-immutable");
1406 }
1407 str->append(")");
1408
1409 SkPixelRef* pr = this->pixelRef();
1410 if (NULL == pr) {
1411 // show null or the explicit pixel address (rare)
1412 str->appendf(" pixels:%p", this->getPixels());
1413 } else {
1414 const char* uri = pr->getURI();
1415 if (NULL != uri) {
1416 str->appendf(" uri:\"%s\"", uri);
1417 } else {
1418 str->appendf(" pixelref:%p", pr);
1419 }
1420 }
1421
1422 str->append(")");
1423}
1424#endif
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001425
1426///////////////////////////////////////////////////////////////////////////////
1427
1428#ifdef SK_DEBUG
1429void SkImageInfo::validate() const {
1430 SkASSERT(fWidth >= 0);
1431 SkASSERT(fHeight >= 0);
1432 SkASSERT(SkColorTypeIsValid(fColorType));
1433 SkASSERT(SkAlphaTypeIsValid(fAlphaType));
1434}
1435#endif