blob: d644ecb12595400777b1479c59b68c88277614d3 [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
reed@google.com86b2e432012-03-15 21:17:03 +000094void SkBitmap::getBounds(SkRect* bounds) const {
95 SkASSERT(bounds);
96 bounds->set(0, 0,
reede5ea5002014-09-03 11:54:58 -070097 SkIntToScalar(fInfo.width()), SkIntToScalar(fInfo.height()));
reed@google.com86b2e432012-03-15 21:17:03 +000098}
99
reed@google.com80e14592012-03-16 14:58:07 +0000100void SkBitmap::getBounds(SkIRect* bounds) const {
101 SkASSERT(bounds);
reede5ea5002014-09-03 11:54:58 -0700102 bounds->set(0, 0, fInfo.width(), fInfo.height());
reed@google.com80e14592012-03-16 14:58:07 +0000103}
104
reed@google.com86b2e432012-03-15 21:17:03 +0000105///////////////////////////////////////////////////////////////////////////////
106
reede5ea5002014-09-03 11:54:58 -0700107bool SkBitmap::setInfo(const SkImageInfo& info, size_t rowBytes) {
108 SkAlphaType newAT = info.alphaType();
109 if (!SkColorTypeValidateAlphaType(info.colorType(), info.alphaType(), &newAT)) {
commit-bot@chromium.orgd5414e52014-02-13 22:30:38 +0000110 return reset_return_false(this);
111 }
reede5ea5002014-09-03 11:54:58 -0700112 // don't look at info.alphaType(), since newAT is the real value...
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000113
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000114 // require that rowBytes fit in 31bits
115 int64_t mrb = info.minRowBytes64();
116 if ((int32_t)mrb != mrb) {
117 return reset_return_false(this);
reed@google.com383a6972013-10-21 14:00:07 +0000118 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000119 if ((int64_t)rowBytes != (int32_t)rowBytes) {
120 return reset_return_false(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 }
reed@android.com89bb83a2009-05-29 21:30:42 +0000122
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000123 if (info.width() < 0 || info.height() < 0) {
124 return reset_return_false(this);
125 }
126
127 if (kUnknown_SkColorType == info.colorType()) {
128 rowBytes = 0;
129 } else if (0 == rowBytes) {
130 rowBytes = (size_t)mrb;
reedf0aed972014-07-01 12:48:11 -0700131 } else if (!info.validRowBytes(rowBytes)) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000132 return reset_return_false(this);
reed@google.com383a6972013-10-21 14:00:07 +0000133 }
134
135 this->freePixels();
136
reede5ea5002014-09-03 11:54:58 -0700137 fInfo = info.makeAlphaType(newAT);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000138 fRowBytes = SkToU32(rowBytes);
reed@google.com383a6972013-10-21 14:00:07 +0000139 return true;
reed@google.com383a6972013-10-21 14:00:07 +0000140}
141
reede5ea5002014-09-03 11:54:58 -0700142bool SkBitmap::setAlphaType(SkAlphaType newAlphaType) {
143 if (!SkColorTypeValidateAlphaType(fInfo.colorType(), newAlphaType, &newAlphaType)) {
reed@google.com383a6972013-10-21 14:00:07 +0000144 return false;
145 }
reede5ea5002014-09-03 11:54:58 -0700146 if (fInfo.alphaType() != newAlphaType) {
147 fInfo = fInfo.makeAlphaType(newAlphaType);
commit-bot@chromium.org0e8d0d62014-01-27 15:41:07 +0000148 if (fPixelRef) {
reede5ea5002014-09-03 11:54:58 -0700149 fPixelRef->changeAlphaType(newAlphaType);
commit-bot@chromium.org0e8d0d62014-01-27 15:41:07 +0000150 }
151 }
reed@google.com383a6972013-10-21 14:00:07 +0000152 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153}
154
155void SkBitmap::updatePixelsFromRef() const {
bsalomon49f085d2014-09-05 13:34:00 -0700156 if (fPixelRef) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000157 if (fPixelLockCount > 0) {
reed@google.comff0da4f2012-05-17 13:14:52 +0000158 SkASSERT(fPixelRef->isLocked());
weita@google.comf9ab99a2009-05-03 18:23:30 +0000159
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160 void* p = fPixelRef->pixels();
bsalomon49f085d2014-09-05 13:34:00 -0700161 if (p) {
reed@google.com672588b2014-01-08 15:42:01 +0000162 p = (char*)p
reed@google.com303c4752014-01-09 20:00:14 +0000163 + fPixelRefOrigin.fY * fRowBytes
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000164 + fPixelRefOrigin.fX * fInfo.bytesPerPixel();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165 }
166 fPixels = p;
reed@google.com5f62ed72014-01-15 19:59:45 +0000167 fColorTable = fPixelRef->colorTable();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000168 } else {
169 SkASSERT(0 == fPixelLockCount);
170 fPixels = NULL;
reed@google.com5f62ed72014-01-15 19:59:45 +0000171 fColorTable = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172 }
173 }
174}
175
reed@google.com672588b2014-01-08 15:42:01 +0000176SkPixelRef* SkBitmap::setPixelRef(SkPixelRef* pr, int dx, int dy) {
reed@google.comdcea5302014-01-03 13:43:01 +0000177#ifdef SK_DEBUG
reed@google.com672588b2014-01-08 15:42:01 +0000178 if (pr) {
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000179 if (kUnknown_SkColorType != fInfo.colorType()) {
reed@google.comdcea5302014-01-03 13:43:01 +0000180 const SkImageInfo& prInfo = pr->info();
reede5ea5002014-09-03 11:54:58 -0700181 SkASSERT(fInfo.width() <= prInfo.width());
182 SkASSERT(fInfo.height() <= prInfo.height());
183 SkASSERT(fInfo.colorType() == prInfo.colorType());
184 switch (prInfo.alphaType()) {
reed@google.comdcea5302014-01-03 13:43:01 +0000185 case kIgnore_SkAlphaType:
reede5ea5002014-09-03 11:54:58 -0700186 SkASSERT(fInfo.alphaType() == kIgnore_SkAlphaType);
reed@google.comdcea5302014-01-03 13:43:01 +0000187 break;
188 case kOpaque_SkAlphaType:
189 case kPremul_SkAlphaType:
reede5ea5002014-09-03 11:54:58 -0700190 SkASSERT(fInfo.alphaType() == kOpaque_SkAlphaType ||
191 fInfo.alphaType() == kPremul_SkAlphaType);
reed@google.comdcea5302014-01-03 13:43:01 +0000192 break;
193 case kUnpremul_SkAlphaType:
reede5ea5002014-09-03 11:54:58 -0700194 SkASSERT(fInfo.alphaType() == kOpaque_SkAlphaType ||
195 fInfo.alphaType() == kUnpremul_SkAlphaType);
reed@google.comdcea5302014-01-03 13:43:01 +0000196 break;
197 }
198 }
199 }
200#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000201
reed@google.com672588b2014-01-08 15:42:01 +0000202 if (pr) {
203 const SkImageInfo& info = pr->info();
reede5ea5002014-09-03 11:54:58 -0700204 fPixelRefOrigin.set(SkPin32(dx, 0, info.width()), SkPin32(dy, 0, info.height()));
reed@google.com672588b2014-01-08 15:42:01 +0000205 } else {
206 // ignore dx,dy if there is no pixelref
207 fPixelRefOrigin.setZero();
208 }
209
210 if (fPixelRef != pr) {
piotaixr0eb02a62014-06-16 11:50:49 -0700211 this->freePixels();
212 SkASSERT(NULL == fPixelRef);
weita@google.comf9ab99a2009-05-03 18:23:30 +0000213
piotaixr0eb02a62014-06-16 11:50:49 -0700214 SkSafeRef(pr);
215 fPixelRef = pr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000216 this->updatePixelsFromRef();
217 }
218
219 SkDEBUGCODE(this->validate();)
220 return pr;
221}
222
223void SkBitmap::lockPixels() const {
bsalomon49f085d2014-09-05 13:34:00 -0700224 if (fPixelRef && 0 == sk_atomic_inc(&fPixelLockCount)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000225 fPixelRef->lockPixels();
226 this->updatePixelsFromRef();
227 }
228 SkDEBUGCODE(this->validate();)
229}
230
231void SkBitmap::unlockPixels() const {
232 SkASSERT(NULL == fPixelRef || fPixelLockCount > 0);
233
bsalomon49f085d2014-09-05 13:34:00 -0700234 if (fPixelRef && 1 == sk_atomic_dec(&fPixelLockCount)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000235 fPixelRef->unlockPixels();
236 this->updatePixelsFromRef();
237 }
238 SkDEBUGCODE(this->validate();)
239}
240
reed@google.com9c49bc32011-07-07 13:42:37 +0000241bool SkBitmap::lockPixelsAreWritable() const {
djsollen@google.comc84b8332012-07-27 13:41:44 +0000242 return (fPixelRef) ? fPixelRef->lockPixelsAreWritable() : false;
reed@google.com9c49bc32011-07-07 13:42:37 +0000243}
244
reed@android.com8a1c16f2008-12-17 15:59:43 +0000245void SkBitmap::setPixels(void* p, SkColorTable* ctable) {
reed@google.com8e1034e2012-07-30 13:16:35 +0000246 if (NULL == p) {
reed@google.com672588b2014-01-08 15:42:01 +0000247 this->setPixelRef(NULL);
reed@google.com8e1034e2012-07-30 13:16:35 +0000248 return;
249 }
250
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000251 if (kUnknown_SkColorType == fInfo.colorType()) {
reed@google.com672588b2014-01-08 15:42:01 +0000252 this->setPixelRef(NULL);
reed@google.combf790232013-12-13 19:45:58 +0000253 return;
254 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000255
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000256 SkPixelRef* pr = SkMallocPixelRef::NewDirect(fInfo, p, fRowBytes, ctable);
reed@google.combf790232013-12-13 19:45:58 +0000257 if (NULL == pr) {
reed@google.com672588b2014-01-08 15:42:01 +0000258 this->setPixelRef(NULL);
reed@google.combf790232013-12-13 19:45:58 +0000259 return;
260 }
261
262 this->setPixelRef(pr)->unref();
263
djsollen@google.comc84b8332012-07-27 13:41:44 +0000264 // since we're already allocated, we lockPixels right away
265 this->lockPixels();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000266 SkDEBUGCODE(this->validate();)
267}
268
reed84825042014-09-02 12:50:45 -0700269bool SkBitmap::tryAllocPixels(Allocator* allocator, SkColorTable* ctable) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000270 HeapAllocator stdalloc;
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000271
reed@android.com8a1c16f2008-12-17 15:59:43 +0000272 if (NULL == allocator) {
273 allocator = &stdalloc;
274 }
275 return allocator->allocPixelRef(this, ctable);
276}
277
reed@google.com9ebcac52014-01-24 18:53:42 +0000278///////////////////////////////////////////////////////////////////////////////
279
reed84825042014-09-02 12:50:45 -0700280bool SkBitmap::tryAllocPixels(const SkImageInfo& requestedInfo, size_t rowBytes) {
reedbae704b2014-06-28 14:26:35 -0700281 if (kIndex_8_SkColorType == requestedInfo.colorType()) {
282 return reset_return_false(this);
283 }
reedf0aed972014-07-01 12:48:11 -0700284 if (!this->setInfo(requestedInfo, rowBytes)) {
reedbae704b2014-06-28 14:26:35 -0700285 return reset_return_false(this);
286 }
mtklein775b8192014-12-02 09:11:25 -0800287
reedbae704b2014-06-28 14:26:35 -0700288 // setInfo may have corrected info (e.g. 565 is always opaque).
289 const SkImageInfo& correctedInfo = this->info();
reedf0aed972014-07-01 12:48:11 -0700290 // setInfo may have computed a valid rowbytes if 0 were passed in
291 rowBytes = this->rowBytes();
292
reedbae704b2014-06-28 14:26:35 -0700293 SkMallocPixelRef::PRFactory defaultFactory;
mtklein775b8192014-12-02 09:11:25 -0800294
reedf0aed972014-07-01 12:48:11 -0700295 SkPixelRef* pr = defaultFactory.create(correctedInfo, rowBytes, NULL);
reedbae704b2014-06-28 14:26:35 -0700296 if (NULL == pr) {
297 return reset_return_false(this);
298 }
299 this->setPixelRef(pr)->unref();
mtklein775b8192014-12-02 09:11:25 -0800300
reedbae704b2014-06-28 14:26:35 -0700301 // TODO: lockPixels could/should return bool or void*/NULL
302 this->lockPixels();
303 if (NULL == this->getPixels()) {
304 return reset_return_false(this);
305 }
306 return true;
307}
308
reed84825042014-09-02 12:50:45 -0700309bool SkBitmap::tryAllocPixels(const SkImageInfo& requestedInfo, SkPixelRefFactory* factory,
310 SkColorTable* ctable) {
reede5ea5002014-09-03 11:54:58 -0700311 if (kIndex_8_SkColorType == requestedInfo.colorType() && NULL == ctable) {
reed@google.com9ebcac52014-01-24 18:53:42 +0000312 return reset_return_false(this);
313 }
scroggo0187dc22014-06-05 11:18:04 -0700314 if (!this->setInfo(requestedInfo)) {
reed@google.com9ebcac52014-01-24 18:53:42 +0000315 return reset_return_false(this);
316 }
317
scroggo0187dc22014-06-05 11:18:04 -0700318 // setInfo may have corrected info (e.g. 565 is always opaque).
319 const SkImageInfo& correctedInfo = this->info();
320
reed@google.com9ebcac52014-01-24 18:53:42 +0000321 SkMallocPixelRef::PRFactory defaultFactory;
322 if (NULL == factory) {
323 factory = &defaultFactory;
324 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000325
reedf0aed972014-07-01 12:48:11 -0700326 SkPixelRef* pr = factory->create(correctedInfo, correctedInfo.minRowBytes(), ctable);
reed@google.com9ebcac52014-01-24 18:53:42 +0000327 if (NULL == pr) {
328 return reset_return_false(this);
329 }
330 this->setPixelRef(pr)->unref();
331
332 // TODO: lockPixels could/should return bool or void*/NULL
333 this->lockPixels();
334 if (NULL == this->getPixels()) {
335 return reset_return_false(this);
336 }
337 return true;
338}
339
scroggo0187dc22014-06-05 11:18:04 -0700340bool SkBitmap::installPixels(const SkImageInfo& requestedInfo, void* pixels, size_t rb,
341 SkColorTable* ct, void (*releaseProc)(void* addr, void* context),
342 void* context) {
343 if (!this->setInfo(requestedInfo, rb)) {
reed@google.com9ebcac52014-01-24 18:53:42 +0000344 this->reset();
345 return false;
346 }
347
scroggo0187dc22014-06-05 11:18:04 -0700348 // setInfo may have corrected info (e.g. 565 is always opaque).
349 const SkImageInfo& correctedInfo = this->info();
350
351 SkPixelRef* pr = SkMallocPixelRef::NewWithProc(correctedInfo, rb, ct, pixels, releaseProc,
352 context);
reed@google.com9ebcac52014-01-24 18:53:42 +0000353 if (!pr) {
354 this->reset();
355 return false;
356 }
357
358 this->setPixelRef(pr)->unref();
mike@reedtribe.org6e58cf32014-02-16 20:54:21 +0000359
360 // since we're already allocated, we lockPixels right away
361 this->lockPixels();
mike@reedtribe.org6e58cf32014-02-16 20:54:21 +0000362 SkDEBUGCODE(this->validate();)
reed@google.com9ebcac52014-01-24 18:53:42 +0000363 return true;
364}
365
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000366bool SkBitmap::installMaskPixels(const SkMask& mask) {
367 if (SkMask::kA8_Format != mask.fFormat) {
368 this->reset();
369 return false;
370 }
371 return this->installPixels(SkImageInfo::MakeA8(mask.fBounds.width(),
372 mask.fBounds.height()),
373 mask.fImage, mask.fRowBytes);
374}
375
reed@google.comeb9a46c2014-01-25 16:46:20 +0000376///////////////////////////////////////////////////////////////////////////////
377
reed@android.com8a1c16f2008-12-17 15:59:43 +0000378void SkBitmap::freePixels() {
bsalomon49f085d2014-09-05 13:34:00 -0700379 if (fPixelRef) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000380 if (fPixelLockCount > 0) {
381 fPixelRef->unlockPixels();
382 }
383 fPixelRef->unref();
384 fPixelRef = NULL;
reed@google.com672588b2014-01-08 15:42:01 +0000385 fPixelRefOrigin.setZero();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000386 }
387 fPixelLockCount = 0;
388 fPixels = NULL;
reed@google.com5f62ed72014-01-15 19:59:45 +0000389 fColorTable = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000390}
391
reed@android.com8a1c16f2008-12-17 15:59:43 +0000392uint32_t SkBitmap::getGenerationID() const {
djsollen@google.comc84b8332012-07-27 13:41:44 +0000393 return (fPixelRef) ? fPixelRef->getGenerationID() : 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000394}
395
396void SkBitmap::notifyPixelsChanged() const {
junov@chromium.orgb0521292011-12-15 20:14:06 +0000397 SkASSERT(!this->isImmutable());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000398 if (fPixelRef) {
399 fPixelRef->notifyPixelsChanged();
400 }
401}
402
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000403GrTexture* SkBitmap::getTexture() const {
reed@android.comce4e53a2010-09-09 16:01:26 +0000404 return fPixelRef ? fPixelRef->getTexture() : NULL;
405}
406
reed@android.com8a1c16f2008-12-17 15:59:43 +0000407///////////////////////////////////////////////////////////////////////////////
408
reed@android.com8a1c16f2008-12-17 15:59:43 +0000409/** We explicitly use the same allocator for our pixels that SkMask does,
410 so that we can freely assign memory allocated by one class to the other.
411 */
412bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst,
413 SkColorTable* ctable) {
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000414 const SkImageInfo info = dst->info();
415 if (kUnknown_SkColorType == info.colorType()) {
reed@google.combf790232013-12-13 19:45:58 +0000416// SkDebugf("unsupported config for info %d\n", dst->config());
417 return false;
418 }
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000419
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000420 SkPixelRef* pr = SkMallocPixelRef::NewAllocate(info, dst->rowBytes(), ctable);
reed@google.combf790232013-12-13 19:45:58 +0000421 if (NULL == pr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000422 return false;
423 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000424
reed@google.com672588b2014-01-08 15:42:01 +0000425 dst->setPixelRef(pr)->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000426 // since we're already allocated, we lockPixels right away
427 dst->lockPixels();
428 return true;
429}
430
431///////////////////////////////////////////////////////////////////////////////
432
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000433bool SkBitmap::copyPixelsTo(void* const dst, size_t dstSize,
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000434 size_t dstRowBytes, bool preserveDstPad) const {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000435
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000436 if (0 == dstRowBytes) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000437 dstRowBytes = fRowBytes;
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000438 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000439
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000440 if (dstRowBytes < fInfo.minRowBytes() ||
441 dst == NULL || (getPixels() == NULL && pixelRef() == NULL)) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000442 return false;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000443 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000444
bsalomon@google.comc6980972011-11-02 19:57:21 +0000445 if (!preserveDstPad && static_cast<uint32_t>(dstRowBytes) == fRowBytes) {
reed@google.com44699382013-10-31 17:28:30 +0000446 size_t safeSize = this->getSafeSize();
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000447 if (safeSize > dstSize || safeSize == 0)
448 return false;
449 else {
450 SkAutoLockPixels lock(*this);
451 // This implementation will write bytes beyond the end of each row,
452 // excluding the last row, if the bitmap's stride is greater than
453 // strictly required by the current config.
454 memcpy(dst, getPixels(), safeSize);
455
456 return true;
457 }
458 } else {
459 // If destination has different stride than us, then copy line by line.
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000460 if (fInfo.getSafeSize(dstRowBytes) > dstSize) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000461 return false;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000462 } else {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000463 // Just copy what we need on each line.
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000464 size_t rowBytes = fInfo.minRowBytes();
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000465 SkAutoLockPixels lock(*this);
466 const uint8_t* srcP = reinterpret_cast<const uint8_t*>(getPixels());
467 uint8_t* dstP = reinterpret_cast<uint8_t*>(dst);
reede5ea5002014-09-03 11:54:58 -0700468 for (int row = 0; row < fInfo.height(); row++, srcP += fRowBytes, dstP += dstRowBytes) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000469 memcpy(dstP, srcP, rowBytes);
470 }
471
472 return true;
473 }
474 }
475}
476
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000477///////////////////////////////////////////////////////////////////////////////
478
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000479bool SkBitmap::isImmutable() const {
scroggo08470592014-07-15 19:56:48 -0700480 return fPixelRef ? fPixelRef->isImmutable() : false;
junov@chromium.orgb0521292011-12-15 20:14:06 +0000481}
482
483void SkBitmap::setImmutable() {
484 if (fPixelRef) {
485 fPixelRef->setImmutable();
junov@chromium.orgb0521292011-12-15 20:14:06 +0000486 }
487}
488
junov@google.com4ee7ae52011-06-30 17:30:49 +0000489bool SkBitmap::isVolatile() const {
490 return (fFlags & kImageIsVolatile_Flag) != 0;
491}
492
493void SkBitmap::setIsVolatile(bool isVolatile) {
494 if (isVolatile) {
495 fFlags |= kImageIsVolatile_Flag;
496 } else {
497 fFlags &= ~kImageIsVolatile_Flag;
498 }
499}
500
reed@android.com8a1c16f2008-12-17 15:59:43 +0000501void* SkBitmap::getAddr(int x, int y) const {
502 SkASSERT((unsigned)x < (unsigned)this->width());
503 SkASSERT((unsigned)y < (unsigned)this->height());
504
505 char* base = (char*)this->getPixels();
506 if (base) {
507 base += y * this->rowBytes();
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000508 switch (this->colorType()) {
509 case kRGBA_8888_SkColorType:
510 case kBGRA_8888_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000511 base += x << 2;
512 break;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000513 case kARGB_4444_SkColorType:
514 case kRGB_565_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000515 base += x << 1;
516 break;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000517 case kAlpha_8_SkColorType:
518 case kIndex_8_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000519 base += x;
520 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000521 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000522 SkDEBUGFAIL("Can't return addr for config");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000523 base = NULL;
524 break;
525 }
526 }
527 return base;
528}
529
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000530SkColor SkBitmap::getColor(int x, int y) const {
531 SkASSERT((unsigned)x < (unsigned)this->width());
532 SkASSERT((unsigned)y < (unsigned)this->height());
533
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000534 switch (this->colorType()) {
535 case kAlpha_8_SkColorType: {
reed@google.com3b521d02011-04-29 11:53:41 +0000536 uint8_t* addr = this->getAddr8(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000537 return SkColorSetA(0, addr[0]);
538 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000539 case kIndex_8_SkColorType: {
reed@google.com3b521d02011-04-29 11:53:41 +0000540 SkPMColor c = this->getIndex8Color(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000541 return SkUnPreMultiply::PMColorToColor(c);
542 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000543 case kRGB_565_SkColorType: {
reed@google.com3b521d02011-04-29 11:53:41 +0000544 uint16_t* addr = this->getAddr16(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000545 return SkPixel16ToColor(addr[0]);
546 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000547 case kARGB_4444_SkColorType: {
reed@google.com3b521d02011-04-29 11:53:41 +0000548 uint16_t* addr = this->getAddr16(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000549 SkPMColor c = SkPixel4444ToPixel32(addr[0]);
550 return SkUnPreMultiply::PMColorToColor(c);
551 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000552 case kBGRA_8888_SkColorType:
553 case kRGBA_8888_SkColorType: {
reed@google.com3b521d02011-04-29 11:53:41 +0000554 uint32_t* addr = this->getAddr32(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000555 return SkUnPreMultiply::PMColorToColor(addr[0]);
556 }
rmistry@google.comd6bab022013-12-02 13:50:38 +0000557 default:
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000558 SkASSERT(false);
559 return 0;
560 }
561 SkASSERT(false); // Not reached.
562 return 0;
563}
564
reed@google.com2a7579d2012-11-07 18:30:18 +0000565bool SkBitmap::ComputeIsOpaque(const SkBitmap& bm) {
566 SkAutoLockPixels alp(bm);
567 if (!bm.getPixels()) {
568 return false;
569 }
570
571 const int height = bm.height();
572 const int width = bm.width();
573
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000574 switch (bm.colorType()) {
575 case kAlpha_8_SkColorType: {
reed@google.com2a7579d2012-11-07 18:30:18 +0000576 unsigned a = 0xFF;
577 for (int y = 0; y < height; ++y) {
578 const uint8_t* row = bm.getAddr8(0, y);
579 for (int x = 0; x < width; ++x) {
580 a &= row[x];
581 }
582 if (0xFF != a) {
583 return false;
584 }
585 }
586 return true;
587 } break;
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000588 case kIndex_8_SkColorType: {
mtklein775b8192014-12-02 09:11:25 -0800589 if (!bm.getColorTable()) {
reed@google.com2a7579d2012-11-07 18:30:18 +0000590 return false;
591 }
mtklein775b8192014-12-02 09:11:25 -0800592 const SkPMColor* table = bm.getColorTable()->readColors();
reed@google.com140d7282013-01-07 20:25:04 +0000593 SkPMColor c = (SkPMColor)~0;
reed@google.com2a7579d2012-11-07 18:30:18 +0000594 for (int i = bm.getColorTable()->count() - 1; i >= 0; --i) {
595 c &= table[i];
596 }
597 return 0xFF == SkGetPackedA32(c);
598 } break;
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000599 case kRGB_565_SkColorType:
reed@google.com2a7579d2012-11-07 18:30:18 +0000600 return true;
601 break;
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000602 case kARGB_4444_SkColorType: {
reed@google.com2a7579d2012-11-07 18:30:18 +0000603 unsigned c = 0xFFFF;
604 for (int y = 0; y < height; ++y) {
605 const SkPMColor16* row = bm.getAddr16(0, y);
606 for (int x = 0; x < width; ++x) {
607 c &= row[x];
608 }
609 if (0xF != SkGetPackedA4444(c)) {
610 return false;
611 }
612 }
613 return true;
614 } break;
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000615 case kBGRA_8888_SkColorType:
616 case kRGBA_8888_SkColorType: {
reed@google.com140d7282013-01-07 20:25:04 +0000617 SkPMColor c = (SkPMColor)~0;
reed@google.com2a7579d2012-11-07 18:30:18 +0000618 for (int y = 0; y < height; ++y) {
619 const SkPMColor* row = bm.getAddr32(0, y);
620 for (int x = 0; x < width; ++x) {
621 c &= row[x];
622 }
623 if (0xFF != SkGetPackedA32(c)) {
624 return false;
625 }
626 }
627 return true;
628 }
629 default:
630 break;
631 }
632 return false;
633}
634
635
reed@android.com8a1c16f2008-12-17 15:59:43 +0000636///////////////////////////////////////////////////////////////////////////////
637///////////////////////////////////////////////////////////////////////////////
638
reed@google.com45f746f2013-06-21 19:51:31 +0000639static uint16_t pack_8888_to_4444(unsigned a, unsigned r, unsigned g, unsigned b) {
640 unsigned pixel = (SkA32To4444(a) << SK_A4444_SHIFT) |
641 (SkR32To4444(r) << SK_R4444_SHIFT) |
642 (SkG32To4444(g) << SK_G4444_SHIFT) |
643 (SkB32To4444(b) << SK_B4444_SHIFT);
644 return SkToU16(pixel);
645}
646
reed@google.com60d32352013-06-28 19:40:50 +0000647void SkBitmap::internalErase(const SkIRect& area,
648 U8CPU a, U8CPU r, U8CPU g, U8CPU b) const {
649#ifdef SK_DEBUG
reed@android.com8a1c16f2008-12-17 15:59:43 +0000650 SkDEBUGCODE(this->validate();)
reed@google.com60d32352013-06-28 19:40:50 +0000651 SkASSERT(!area.isEmpty());
652 {
reed@google.com92833f92013-06-28 19:47:43 +0000653 SkIRect total = { 0, 0, this->width(), this->height() };
reed@google.com60d32352013-06-28 19:40:50 +0000654 SkASSERT(total.contains(area));
655 }
656#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000657
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000658 switch (fInfo.colorType()) {
659 case kUnknown_SkColorType:
660 case kIndex_8_SkColorType:
commit-bot@chromium.org7669a772014-03-27 15:30:35 +0000661 return; // can't erase. Should we bzero so the memory is not uninitialized?
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000662 default:
663 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000664 }
665
666 SkAutoLockPixels alp(*this);
667 // perform this check after the lock call
668 if (!this->readyToDraw()) {
669 return;
670 }
671
reed@google.com60d32352013-06-28 19:40:50 +0000672 int height = area.height();
673 const int width = area.width();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000674 const int rowBytes = fRowBytes;
675
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000676 switch (this->colorType()) {
677 case kAlpha_8_SkColorType: {
reed@google.com60d32352013-06-28 19:40:50 +0000678 uint8_t* p = this->getAddr8(area.fLeft, area.fTop);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000679 while (--height >= 0) {
680 memset(p, a, width);
681 p += rowBytes;
682 }
683 break;
684 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000685 case kARGB_4444_SkColorType:
686 case kRGB_565_SkColorType: {
reed@google.com60d32352013-06-28 19:40:50 +0000687 uint16_t* p = this->getAddr16(area.fLeft, area.fTop);;
reed@google.com45f746f2013-06-21 19:51:31 +0000688 uint16_t v;
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000689
commit-bot@chromium.org7669a772014-03-27 15:30:35 +0000690 // make rgb premultiplied
691 if (255 != a) {
692 r = SkAlphaMul(r, a);
693 g = SkAlphaMul(g, a);
694 b = SkAlphaMul(b, a);
695 }
696
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000697 if (kARGB_4444_SkColorType == this->colorType()) {
reed@google.com45f746f2013-06-21 19:51:31 +0000698 v = pack_8888_to_4444(a, r, g, b);
699 } else {
700 v = SkPackRGB16(r >> (8 - SK_R16_BITS),
701 g >> (8 - SK_G16_BITS),
702 b >> (8 - SK_B16_BITS));
703 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000704 while (--height >= 0) {
705 sk_memset16(p, v, width);
706 p = (uint16_t*)((char*)p + rowBytes);
707 }
708 break;
709 }
commit-bot@chromium.org7669a772014-03-27 15:30:35 +0000710 case kBGRA_8888_SkColorType:
711 case kRGBA_8888_SkColorType: {
reed@google.com60d32352013-06-28 19:40:50 +0000712 uint32_t* p = this->getAddr32(area.fLeft, area.fTop);
commit-bot@chromium.org7669a772014-03-27 15:30:35 +0000713
714 if (255 != a && kPremul_SkAlphaType == this->alphaType()) {
715 r = SkAlphaMul(r, a);
716 g = SkAlphaMul(g, a);
717 b = SkAlphaMul(b, a);
718 }
719 uint32_t v = kRGBA_8888_SkColorType == this->colorType() ?
720 SkPackARGB_as_RGBA(a, r, g, b) : SkPackARGB_as_BGRA(a, r, g, b);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000721
722 while (--height >= 0) {
723 sk_memset32(p, v, width);
724 p = (uint32_t*)((char*)p + rowBytes);
725 }
726 break;
727 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000728 default:
729 return; // no change, so don't call notifyPixelsChanged()
reed@android.com8a1c16f2008-12-17 15:59:43 +0000730 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000731
reed@android.com8a1c16f2008-12-17 15:59:43 +0000732 this->notifyPixelsChanged();
733}
734
reed@google.com60d32352013-06-28 19:40:50 +0000735void SkBitmap::eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const {
reed@google.com92833f92013-06-28 19:47:43 +0000736 SkIRect area = { 0, 0, this->width(), this->height() };
reed@google.com60d32352013-06-28 19:40:50 +0000737 if (!area.isEmpty()) {
738 this->internalErase(area, a, r, g, b);
739 }
740}
741
742void SkBitmap::eraseArea(const SkIRect& rect, SkColor c) const {
reed@google.com92833f92013-06-28 19:47:43 +0000743 SkIRect area = { 0, 0, this->width(), this->height() };
reed@google.com60d32352013-06-28 19:40:50 +0000744 if (area.intersect(rect)) {
745 this->internalErase(area, SkColorGetA(c), SkColorGetR(c),
746 SkColorGetG(c), SkColorGetB(c));
747 }
748}
749
reed@android.com8a1c16f2008-12-17 15:59:43 +0000750//////////////////////////////////////////////////////////////////////////////////////
751//////////////////////////////////////////////////////////////////////////////////////
752
reed@android.com8a1c16f2008-12-17 15:59:43 +0000753bool SkBitmap::extractSubset(SkBitmap* result, const SkIRect& subset) const {
754 SkDEBUGCODE(this->validate();)
755
djsollen@google.comc84b8332012-07-27 13:41:44 +0000756 if (NULL == result || NULL == fPixelRef) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000757 return false; // no src pixels
758 }
759
760 SkIRect srcRect, r;
761 srcRect.set(0, 0, this->width(), this->height());
762 if (!r.intersect(srcRect, subset)) {
763 return false; // r is empty (i.e. no intersection)
764 }
765
scroggo@google.coma2a31922012-12-07 19:14:45 +0000766 if (fPixelRef->getTexture() != NULL) {
767 // Do a deep copy
reede4538f52014-06-11 06:09:50 -0700768 SkPixelRef* pixelRef = fPixelRef->deepCopy(this->colorType(), &subset);
scroggo@google.coma2a31922012-12-07 19:14:45 +0000769 if (pixelRef != NULL) {
770 SkBitmap dst;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000771 dst.setInfo(SkImageInfo::Make(subset.width(), subset.height(),
772 this->colorType(), this->alphaType()));
scroggo@google.coma2a31922012-12-07 19:14:45 +0000773 dst.setIsVolatile(this->isVolatile());
scroggo@google.coma2a31922012-12-07 19:14:45 +0000774 dst.setPixelRef(pixelRef)->unref();
775 SkDEBUGCODE(dst.validate());
776 result->swap(dst);
777 return true;
778 }
779 }
780
scroggo@google.coma2a31922012-12-07 19:14:45 +0000781 // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have
782 // exited above.
783 SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width()));
784 SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height()));
785
reed@android.com8a1c16f2008-12-17 15:59:43 +0000786 SkBitmap dst;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000787 dst.setInfo(SkImageInfo::Make(r.width(), r.height(), this->colorType(), this->alphaType()),
788 this->rowBytes());
skyostil@google.com0eb75762012-01-16 10:45:53 +0000789 dst.setIsVolatile(this->isVolatile());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000790
791 if (fPixelRef) {
reed@google.com672588b2014-01-08 15:42:01 +0000792 SkIPoint origin = fPixelRefOrigin;
793 origin.fX += r.fLeft;
794 origin.fY += r.fTop;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000795 // share the pixelref with a custom offset
reed@google.com672588b2014-01-08 15:42:01 +0000796 dst.setPixelRef(fPixelRef, origin);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000797 }
798 SkDEBUGCODE(dst.validate();)
799
800 // we know we're good, so commit to result
801 result->swap(dst);
802 return true;
803}
804
805///////////////////////////////////////////////////////////////////////////////
806
807#include "SkCanvas.h"
808#include "SkPaint.h"
809
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000810bool SkBitmap::canCopyTo(SkColorType dstColorType) const {
reedb184f7f2014-07-13 04:32:32 -0700811 const SkColorType srcCT = this->colorType();
812
813 if (srcCT == kUnknown_SkColorType) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000814 return false;
815 }
816
reedb184f7f2014-07-13 04:32:32 -0700817 bool sameConfigs = (srcCT == dstColorType);
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000818 switch (dstColorType) {
819 case kAlpha_8_SkColorType:
820 case kRGB_565_SkColorType:
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000821 case kRGBA_8888_SkColorType:
822 case kBGRA_8888_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000823 break;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000824 case kIndex_8_SkColorType:
weita@google.comf9ab99a2009-05-03 18:23:30 +0000825 if (!sameConfigs) {
826 return false;
827 }
828 break;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000829 case kARGB_4444_SkColorType:
reedb184f7f2014-07-13 04:32:32 -0700830 return sameConfigs || kN32_SkColorType == srcCT || kIndex_8_SkColorType == srcCT;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000831 default:
832 return false;
833 }
reed@android.comfbaa88d2009-05-06 17:44:34 +0000834 return true;
835}
836
reedb184f7f2014-07-13 04:32:32 -0700837#include "SkConfig8888.h"
838
839bool SkBitmap::readPixels(const SkImageInfo& requestedDstInfo, void* dstPixels, size_t dstRB,
840 int x, int y) const {
841 if (kUnknown_SkColorType == requestedDstInfo.colorType()) {
842 return false;
843 }
844 if (NULL == dstPixels || dstRB < requestedDstInfo.minRowBytes()) {
845 return false;
846 }
847 if (0 == requestedDstInfo.width() || 0 == requestedDstInfo.height()) {
848 return false;
849 }
mtklein775b8192014-12-02 09:11:25 -0800850
reedb184f7f2014-07-13 04:32:32 -0700851 SkIRect srcR = SkIRect::MakeXYWH(x, y, requestedDstInfo.width(), requestedDstInfo.height());
852 if (!srcR.intersect(0, 0, this->width(), this->height())) {
853 return false;
854 }
mtklein775b8192014-12-02 09:11:25 -0800855
reedb184f7f2014-07-13 04:32:32 -0700856 // the intersect may have shrunk info's logical size
reede5ea5002014-09-03 11:54:58 -0700857 const SkImageInfo dstInfo = requestedDstInfo.makeWH(srcR.width(), srcR.height());
mtklein775b8192014-12-02 09:11:25 -0800858
reedb184f7f2014-07-13 04:32:32 -0700859 // if x or y are negative, then we have to adjust pixels
860 if (x > 0) {
861 x = 0;
862 }
863 if (y > 0) {
864 y = 0;
865 }
866 // here x,y are either 0 or negative
867 dstPixels = ((char*)dstPixels - y * dstRB - x * dstInfo.bytesPerPixel());
868
869 //////////////
mtklein775b8192014-12-02 09:11:25 -0800870
reedb184f7f2014-07-13 04:32:32 -0700871 SkAutoLockPixels alp(*this);
mtklein775b8192014-12-02 09:11:25 -0800872
reedb184f7f2014-07-13 04:32:32 -0700873 // since we don't stop creating un-pixeled devices yet, check for no pixels here
874 if (NULL == this->getPixels()) {
875 return false;
876 }
mtklein775b8192014-12-02 09:11:25 -0800877
reede5ea5002014-09-03 11:54:58 -0700878 const SkImageInfo srcInfo = this->info().makeWH(dstInfo.width(), dstInfo.height());
mtklein775b8192014-12-02 09:11:25 -0800879
reedb184f7f2014-07-13 04:32:32 -0700880 const void* srcPixels = this->getAddr(srcR.x(), srcR.y());
881 return SkPixelInfo::CopyPixels(dstInfo, dstPixels, dstRB, srcInfo, srcPixels, this->rowBytes(),
882 this->getColorTable());
883}
884
885bool SkBitmap::copyTo(SkBitmap* dst, SkColorType dstColorType, Allocator* alloc) const {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000886 if (!this->canCopyTo(dstColorType)) {
reed@android.comfbaa88d2009-05-06 17:44:34 +0000887 return false;
888 }
889
reed@google.com50dfa012011-04-01 19:05:36 +0000890 // if we have a texture, first get those pixels
891 SkBitmap tmpSrc;
892 const SkBitmap* src = this;
893
scroggo@google.coma2a31922012-12-07 19:14:45 +0000894 if (fPixelRef) {
895 SkIRect subset;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000896 subset.setXYWH(fPixelRefOrigin.fX, fPixelRefOrigin.fY,
897 fInfo.width(), fInfo.height());
reed@google.com672588b2014-01-08 15:42:01 +0000898 if (fPixelRef->readPixels(&tmpSrc, &subset)) {
scroggo0187dc22014-06-05 11:18:04 -0700899 if (fPixelRef->info().alphaType() == kUnpremul_SkAlphaType) {
900 // FIXME: The only meaningful implementation of readPixels
901 // (GrPixelRef) assumes premultiplied pixels.
902 return false;
903 }
reed@google.com672588b2014-01-08 15:42:01 +0000904 SkASSERT(tmpSrc.width() == this->width());
905 SkASSERT(tmpSrc.height() == this->height());
reed@google.com50dfa012011-04-01 19:05:36 +0000906
reed@google.com672588b2014-01-08 15:42:01 +0000907 // did we get lucky and we can just return tmpSrc?
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000908 if (tmpSrc.colorType() == dstColorType && NULL == alloc) {
reed@google.com672588b2014-01-08 15:42:01 +0000909 dst->swap(tmpSrc);
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000910 // If the result is an exact copy, clone the gen ID.
911 if (dst->pixelRef() && dst->pixelRef()->info() == fPixelRef->info()) {
reed@google.com672588b2014-01-08 15:42:01 +0000912 dst->pixelRef()->cloneGenID(*fPixelRef);
scroggo@google.coma2a31922012-12-07 19:14:45 +0000913 }
reed@google.com672588b2014-01-08 15:42:01 +0000914 return true;
scroggo@google.comd5764e82012-08-22 15:00:05 +0000915 }
reed@google.com672588b2014-01-08 15:42:01 +0000916
917 // fall through to the raster case
918 src = &tmpSrc;
reed@google.com50dfa012011-04-01 19:05:36 +0000919 }
reed@android.comfbaa88d2009-05-06 17:44:34 +0000920 }
reed@android.com311c82d2009-05-05 23:13:23 +0000921
reed@google.com50dfa012011-04-01 19:05:36 +0000922 // we lock this now, since we may need its colortable
923 SkAutoLockPixels srclock(*src);
924 if (!src->readyToDraw()) {
925 return false;
926 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000927
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000928 // The only way to be readyToDraw is if fPixelRef is non NULL.
929 SkASSERT(fPixelRef != NULL);
930
reede5ea5002014-09-03 11:54:58 -0700931 const SkImageInfo dstInfo = src->info().makeColorType(dstColorType);
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000932
reed@google.com50dfa012011-04-01 19:05:36 +0000933 SkBitmap tmpDst;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000934 if (!tmpDst.setInfo(dstInfo)) {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000935 return false;
936 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000937
weita@google.comf9ab99a2009-05-03 18:23:30 +0000938 // allocate colortable if srcConfig == kIndex8_Config
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000939 SkAutoTUnref<SkColorTable> ctable;
940 if (dstColorType == kIndex_8_SkColorType) {
941 // TODO: can we just ref() the src colortable? Is it reentrant-safe?
942 ctable.reset(SkNEW_ARGS(SkColorTable, (*src->getColorTable())));
943 }
reed84825042014-09-02 12:50:45 -0700944 if (!tmpDst.tryAllocPixels(alloc, ctable)) {
weita@google.comf9ab99a2009-05-03 18:23:30 +0000945 return false;
946 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000947
reed@google.com50dfa012011-04-01 19:05:36 +0000948 if (!tmpDst.readyToDraw()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000949 // allocator/lock failed
950 return false;
951 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000952
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000953 // pixelRef must be non NULL or tmpDst.readyToDraw() would have
954 // returned false.
955 SkASSERT(tmpDst.pixelRef() != NULL);
956
reedb184f7f2014-07-13 04:32:32 -0700957 if (!src->readPixels(tmpDst.info(), tmpDst.getPixels(), tmpDst.rowBytes(), 0, 0)) {
958 return false;
959 }
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000960
reedb184f7f2014-07-13 04:32:32 -0700961 // (for BitmapHeap) Clone the pixelref genID even though we have a new pixelref.
962 // The old copyTo impl did this, so we continue it for now.
963 //
964 // TODO: should we ignore rowbytes (i.e. getSize)? Then it could just be
965 // if (src_pixelref->info == dst_pixelref->info)
966 //
967 if (src->colorType() == dstColorType && tmpDst.getSize() == src->getSize()) {
968 SkPixelRef* dstPixelRef = tmpDst.pixelRef();
969 if (dstPixelRef->info() == fPixelRef->info()) {
970 dstPixelRef->cloneGenID(*fPixelRef);
reed@android.com311c82d2009-05-05 23:13:23 +0000971 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000972 }
973
reed@google.com50dfa012011-04-01 19:05:36 +0000974 dst->swap(tmpDst);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000975 return true;
976}
977
commit-bot@chromium.orgfab349c2014-03-05 02:34:58 +0000978bool SkBitmap::deepCopyTo(SkBitmap* dst) const {
reede4538f52014-06-11 06:09:50 -0700979 const SkColorType dstCT = this->colorType();
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000980
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000981 if (!this->canCopyTo(dstCT)) {
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000982 return false;
983 }
984
985 // If we have a PixelRef, and it supports deep copy, use it.
986 // Currently supported only by texture-backed bitmaps.
987 if (fPixelRef) {
reede4538f52014-06-11 06:09:50 -0700988 SkPixelRef* pixelRef = fPixelRef->deepCopy(dstCT, NULL);
commit-bot@chromium.orgeeef0cc2014-05-21 15:58:00 +0000989 if (pixelRef) {
990 uint32_t rowBytes;
991 if (this->colorType() == dstCT) {
992 // Since there is no subset to pass to deepCopy, and deepCopy
993 // succeeded, the new pixel ref must be identical.
994 SkASSERT(fPixelRef->info() == pixelRef->info());
995 pixelRef->cloneGenID(*fPixelRef);
996 // Use the same rowBytes as the original.
997 rowBytes = fRowBytes;
998 } else {
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000999 // With the new config, an appropriate fRowBytes will be computed by setInfo.
commit-bot@chromium.orgeeef0cc2014-05-21 15:58:00 +00001000 rowBytes = 0;
1001 }
1002
reede5ea5002014-09-03 11:54:58 -07001003 const SkImageInfo info = fInfo.makeColorType(dstCT);
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001004 if (!dst->setInfo(info, rowBytes)) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001005 return false;
1006 }
commit-bot@chromium.orgeeef0cc2014-05-21 15:58:00 +00001007 dst->setPixelRef(pixelRef, fPixelRefOrigin)->unref();
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001008 return true;
1009 }
1010 }
1011
1012 if (this->getTexture()) {
1013 return false;
1014 } else {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +00001015 return this->copyTo(dst, dstCT, NULL);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001016 }
1017}
1018
reed@android.com8a1c16f2008-12-17 15:59:43 +00001019///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +00001020
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +00001021static bool GetBitmapAlpha(const SkBitmap& src, uint8_t* SK_RESTRICT alpha,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001022 int alphaRowBytes) {
1023 SkASSERT(alpha != NULL);
1024 SkASSERT(alphaRowBytes >= src.width());
1025
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001026 SkColorType colorType = src.colorType();
1027 int w = src.width();
1028 int h = src.height();
1029 size_t rb = src.rowBytes();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001030
reed@android.com1cdcb512009-08-24 19:11:00 +00001031 SkAutoLockPixels alp(src);
1032 if (!src.readyToDraw()) {
1033 // zero out the alpha buffer and return
1034 while (--h >= 0) {
1035 memset(alpha, 0, w);
1036 alpha += alphaRowBytes;
1037 }
1038 return false;
1039 }
reed@google.com82065d62011-02-07 15:30:46 +00001040
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001041 if (kAlpha_8_SkColorType == colorType && !src.isOpaque()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001042 const uint8_t* s = src.getAddr8(0, 0);
1043 while (--h >= 0) {
1044 memcpy(alpha, s, w);
1045 s += rb;
1046 alpha += alphaRowBytes;
1047 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001048 } else if (kN32_SkColorType == colorType && !src.isOpaque()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001049 const SkPMColor* SK_RESTRICT s = src.getAddr32(0, 0);
1050 while (--h >= 0) {
1051 for (int x = 0; x < w; x++) {
1052 alpha[x] = SkGetPackedA32(s[x]);
1053 }
1054 s = (const SkPMColor*)((const char*)s + rb);
1055 alpha += alphaRowBytes;
1056 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001057 } else if (kARGB_4444_SkColorType == colorType && !src.isOpaque()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001058 const SkPMColor16* SK_RESTRICT s = src.getAddr16(0, 0);
1059 while (--h >= 0) {
1060 for (int x = 0; x < w; x++) {
1061 alpha[x] = SkPacked4444ToA32(s[x]);
1062 }
1063 s = (const SkPMColor16*)((const char*)s + rb);
1064 alpha += alphaRowBytes;
1065 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001066 } else if (kIndex_8_SkColorType == colorType && !src.isOpaque()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001067 SkColorTable* ct = src.getColorTable();
1068 if (ct) {
mtklein775b8192014-12-02 09:11:25 -08001069 const SkPMColor* SK_RESTRICT table = ct->readColors();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001070 const uint8_t* SK_RESTRICT s = src.getAddr8(0, 0);
1071 while (--h >= 0) {
1072 for (int x = 0; x < w; x++) {
1073 alpha[x] = SkGetPackedA32(table[s[x]]);
1074 }
1075 s += rb;
1076 alpha += alphaRowBytes;
1077 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001078 }
1079 } else { // src is opaque, so just fill alpha[] with 0xFF
1080 memset(alpha, 0xFF, h * alphaRowBytes);
1081 }
reed@android.com1cdcb512009-08-24 19:11:00 +00001082 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001083}
1084
1085#include "SkPaint.h"
1086#include "SkMaskFilter.h"
1087#include "SkMatrix.h"
1088
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001089bool SkBitmap::extractAlpha(SkBitmap* dst, const SkPaint* paint,
djsollen@google.com57f49692011-02-23 20:46:31 +00001090 Allocator *allocator, SkIPoint* offset) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001091 SkDEBUGCODE(this->validate();)
1092
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001093 SkBitmap tmpBitmap;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001094 SkMatrix identity;
1095 SkMask srcM, dstM;
1096
1097 srcM.fBounds.set(0, 0, this->width(), this->height());
1098 srcM.fRowBytes = SkAlign4(this->width());
1099 srcM.fFormat = SkMask::kA8_Format;
1100
1101 SkMaskFilter* filter = paint ? paint->getMaskFilter() : NULL;
1102
1103 // compute our (larger?) dst bounds if we have a filter
bsalomon49f085d2014-09-05 13:34:00 -07001104 if (filter) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001105 identity.reset();
1106 srcM.fImage = NULL;
1107 if (!filter->filterMask(&dstM, srcM, identity, NULL)) {
1108 goto NO_FILTER_CASE;
1109 }
1110 dstM.fRowBytes = SkAlign4(dstM.fBounds.width());
1111 } else {
1112 NO_FILTER_CASE:
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001113 tmpBitmap.setInfo(SkImageInfo::MakeA8(this->width(), this->height()), srcM.fRowBytes);
reed84825042014-09-02 12:50:45 -07001114 if (!tmpBitmap.tryAllocPixels(allocator, NULL)) {
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001115 // Allocation of pixels for alpha bitmap failed.
1116 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
1117 tmpBitmap.width(), tmpBitmap.height());
1118 return false;
1119 }
1120 GetBitmapAlpha(*this, tmpBitmap.getAddr8(0, 0), srcM.fRowBytes);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001121 if (offset) {
1122 offset->set(0, 0);
1123 }
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001124 tmpBitmap.swap(*dst);
1125 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001126 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001127 srcM.fImage = SkMask::AllocImage(srcM.computeImageSize());
1128 SkAutoMaskFreeImage srcCleanup(srcM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001129
1130 GetBitmapAlpha(*this, srcM.fImage, srcM.fRowBytes);
1131 if (!filter->filterMask(&dstM, srcM, identity, NULL)) {
1132 goto NO_FILTER_CASE;
1133 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001134 SkAutoMaskFreeImage dstCleanup(dstM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001135
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001136 tmpBitmap.setInfo(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.height()),
1137 dstM.fRowBytes);
reed84825042014-09-02 12:50:45 -07001138 if (!tmpBitmap.tryAllocPixels(allocator, NULL)) {
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001139 // Allocation of pixels for alpha bitmap failed.
1140 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
1141 tmpBitmap.width(), tmpBitmap.height());
1142 return false;
1143 }
1144 memcpy(tmpBitmap.getPixels(), dstM.fImage, dstM.computeImageSize());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001145 if (offset) {
1146 offset->set(dstM.fBounds.fLeft, dstM.fBounds.fTop);
1147 }
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001148 SkDEBUGCODE(tmpBitmap.validate();)
1149
1150 tmpBitmap.swap(*dst);
1151 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001152}
1153
1154///////////////////////////////////////////////////////////////////////////////
1155
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001156void SkBitmap::WriteRawPixels(SkWriteBuffer* buffer, const SkBitmap& bitmap) {
1157 const SkImageInfo info = bitmap.info();
1158 SkAutoLockPixels alp(bitmap);
1159 if (0 == info.width() || 0 == info.height() || NULL == bitmap.getPixels()) {
1160 buffer->writeUInt(0); // instead of snugRB, signaling no pixels
1161 return;
1162 }
1163
1164 const size_t snugRB = info.width() * info.bytesPerPixel();
1165 const char* src = (const char*)bitmap.getPixels();
1166 const size_t ramRB = bitmap.rowBytes();
skia.committer@gmail.com3c134a92014-05-24 03:05:26 +00001167
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001168 buffer->write32(SkToU32(snugRB));
1169 info.flatten(*buffer);
1170
1171 const size_t size = snugRB * info.height();
1172 SkAutoMalloc storage(size);
1173 char* dst = (char*)storage.get();
1174 for (int y = 0; y < info.height(); ++y) {
1175 memcpy(dst, src, snugRB);
1176 dst += snugRB;
1177 src += ramRB;
1178 }
1179 buffer->writeByteArray(storage.get(), size);
1180
1181 SkColorTable* ct = bitmap.getColorTable();
1182 if (kIndex_8_SkColorType == info.colorType() && ct) {
1183 buffer->writeBool(true);
1184 ct->writeToBuffer(*buffer);
1185 } else {
1186 buffer->writeBool(false);
1187 }
1188}
1189
1190bool SkBitmap::ReadRawPixels(SkReadBuffer* buffer, SkBitmap* bitmap) {
1191 const size_t snugRB = buffer->readUInt();
1192 if (0 == snugRB) { // no pixels
1193 return false;
1194 }
1195
1196 SkImageInfo info;
1197 info.unflatten(*buffer);
1198
sugoica95c192014-07-08 09:18:48 -07001199 // If there was an error reading "info", don't use it to compute minRowBytes()
1200 if (!buffer->validate(true)) {
1201 return false;
1202 }
1203
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001204 const size_t ramRB = info.minRowBytes();
1205 const int height = info.height();
1206 const size_t snugSize = snugRB * height;
1207 const size_t ramSize = ramRB * height;
commit-bot@chromium.org05858432014-05-30 01:06:44 +00001208 if (!buffer->validate(snugSize <= ramSize)) {
1209 return false;
1210 }
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001211
reed33a30502014-09-11 08:42:36 -07001212 SkAutoDataUnref data(SkData::NewUninitialized(ramSize));
1213 char* dst = (char*)data->writable_data();
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001214 buffer->readByteArray(dst, snugSize);
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001215
1216 if (snugSize != ramSize) {
1217 const char* srcRow = dst + snugRB * (height - 1);
1218 char* dstRow = dst + ramRB * (height - 1);
1219 for (int y = height - 1; y >= 1; --y) {
1220 memmove(dstRow, srcRow, snugRB);
1221 srcRow -= snugRB;
1222 dstRow -= ramRB;
1223 }
1224 SkASSERT(srcRow == dstRow); // first row does not need to be moved
1225 }
skia.committer@gmail.com3c134a92014-05-24 03:05:26 +00001226
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001227 SkAutoTUnref<SkColorTable> ctable;
1228 if (buffer->readBool()) {
1229 ctable.reset(SkNEW_ARGS(SkColorTable, (*buffer)));
1230 }
skia.committer@gmail.com3c134a92014-05-24 03:05:26 +00001231
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001232 SkAutoTUnref<SkPixelRef> pr(SkMallocPixelRef::NewWithData(info, info.minRowBytes(),
1233 ctable.get(), data.get()));
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001234 bitmap->setInfo(pr->info());
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001235 bitmap->setPixelRef(pr, 0, 0);
1236 return true;
1237}
1238
reed@android.com8a1c16f2008-12-17 15:59:43 +00001239enum {
1240 SERIALIZE_PIXELTYPE_NONE,
djsollen@google.com21830d92012-08-07 19:49:41 +00001241 SERIALIZE_PIXELTYPE_REF_DATA
reed@android.com8a1c16f2008-12-17 15:59:43 +00001242};
1243
commit-bot@chromium.org851155c2014-05-27 14:03:51 +00001244void SkBitmap::legacyUnflatten(SkReadBuffer& buffer) {
reed9a9eae22014-07-07 14:32:06 -07001245#ifdef SK_SUPPORT_LEGACY_PIXELREF_UNFLATTENABLE
reed@android.com8a1c16f2008-12-17 15:59:43 +00001246 this->reset();
weita@google.comf9ab99a2009-05-03 18:23:30 +00001247
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001248 SkImageInfo info;
1249 info.unflatten(buffer);
1250 size_t rowBytes = buffer.readInt();
commit-bot@chromium.org33fed142014-02-13 18:46:13 +00001251 if (!buffer.validate((info.width() >= 0) && (info.height() >= 0) &&
1252 SkColorTypeIsValid(info.fColorType) &&
1253 SkAlphaTypeIsValid(info.fAlphaType) &&
scroggo2fd0d142014-07-01 07:08:19 -07001254 SkColorTypeValidateAlphaType(info.fColorType, info.fAlphaType) &&
commit-bot@chromium.org33fed142014-02-13 18:46:13 +00001255 info.validRowBytes(rowBytes))) {
1256 return;
1257 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001258
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001259 bool configIsValid = this->setInfo(info, rowBytes);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001260 buffer.validate(configIsValid);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001261
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001262 int reftype = buffer.readInt();
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +00001263 if (buffer.validate((SERIALIZE_PIXELTYPE_REF_DATA == reftype) ||
1264 (SERIALIZE_PIXELTYPE_NONE == reftype))) {
1265 switch (reftype) {
1266 case SERIALIZE_PIXELTYPE_REF_DATA: {
reed@google.com672588b2014-01-08 15:42:01 +00001267 SkIPoint origin;
1268 origin.fX = buffer.readInt();
1269 origin.fY = buffer.readInt();
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001270 size_t offset = origin.fY * rowBytes + origin.fX * info.bytesPerPixel();
reed9a9eae22014-07-07 14:32:06 -07001271 SkPixelRef* pr = buffer.readFlattenable<SkPixelRef>();
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +00001272 if (!buffer.validate((NULL == pr) ||
1273 (pr->getAllocatedSizeInBytes() >= (offset + this->getSafeSize())))) {
reed@google.com672588b2014-01-08 15:42:01 +00001274 origin.setZero();
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +00001275 }
reed@google.com672588b2014-01-08 15:42:01 +00001276 SkSafeUnref(this->setPixelRef(pr, origin));
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +00001277 break;
1278 }
1279 case SERIALIZE_PIXELTYPE_NONE:
1280 break;
1281 default:
1282 SkDEBUGFAIL("unrecognized pixeltype in serialized data");
1283 sk_throw();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001284 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001285 }
reed9a9eae22014-07-07 14:32:06 -07001286#else
1287 sk_throw();
1288#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001289}
1290
1291///////////////////////////////////////////////////////////////////////////////
1292
1293SkBitmap::RLEPixels::RLEPixels(int width, int height) {
1294 fHeight = height;
commit-bot@chromium.org235002f2013-10-09 18:39:59 +00001295 fYPtrs = (uint8_t**)sk_calloc_throw(height * sizeof(uint8_t*));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001296}
1297
1298SkBitmap::RLEPixels::~RLEPixels() {
1299 sk_free(fYPtrs);
1300}
1301
1302///////////////////////////////////////////////////////////////////////////////
1303
1304#ifdef SK_DEBUG
1305void SkBitmap::validate() const {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001306 fInfo.validate();
commit-bot@chromium.orgd5414e52014-02-13 22:30:38 +00001307
1308 // ImageInfo may not require this, but Bitmap ensures that opaque-only
1309 // colorTypes report opaque for their alphatype
1310 if (kRGB_565_SkColorType == fInfo.colorType()) {
1311 SkASSERT(kOpaque_SkAlphaType == fInfo.alphaType());
1312 }
1313
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001314 SkASSERT(fInfo.validRowBytes(fRowBytes));
scroggo08470592014-07-15 19:56:48 -07001315 uint8_t allFlags = kImageIsVolatile_Flag;
scroggo@google.com8e990eb2013-06-14 15:55:56 +00001316#ifdef SK_BUILD_FOR_ANDROID
1317 allFlags |= kHasHardwareMipMap_Flag;
1318#endif
scroggo08470592014-07-15 19:56:48 -07001319 SkASSERT((~allFlags & fFlags) == 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001320 SkASSERT(fPixelLockCount >= 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001321
reed@google.com615316c2014-01-15 19:15:23 +00001322 if (fPixels) {
1323 SkASSERT(fPixelRef);
robertphillipse77dadd2014-11-21 05:50:21 -08001324 SkASSERT(fPixelLockCount > 0);
reed@google.com615316c2014-01-15 19:15:23 +00001325 SkASSERT(fPixelRef->isLocked());
1326 SkASSERT(fPixelRef->rowBytes() == fRowBytes);
1327 SkASSERT(fPixelRefOrigin.fX >= 0);
1328 SkASSERT(fPixelRefOrigin.fY >= 0);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001329 SkASSERT(fPixelRef->info().width() >= (int)this->width() + fPixelRefOrigin.fX);
reede5ea5002014-09-03 11:54:58 -07001330 SkASSERT(fPixelRef->info().height() >= (int)this->height() + fPixelRefOrigin.fY);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001331 SkASSERT(fPixelRef->rowBytes() >= fInfo.minRowBytes());
reed@google.com615316c2014-01-15 19:15:23 +00001332 } else {
1333 SkASSERT(NULL == fColorTable);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001334 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001335}
1336#endif
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001337
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +00001338#ifndef SK_IGNORE_TO_STRING
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001339void SkBitmap::toString(SkString* str) const {
1340
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001341 static const char* gColorTypeNames[kLastEnum_SkColorType + 1] = {
1342 "UNKNOWN", "A8", "565", "4444", "RGBA", "BGRA", "INDEX8",
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001343 };
1344
1345 str->appendf("bitmap: ((%d, %d) %s", this->width(), this->height(),
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001346 gColorTypeNames[this->colorType()]);
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001347
1348 str->append(" (");
1349 if (this->isOpaque()) {
1350 str->append("opaque");
1351 } else {
1352 str->append("transparent");
1353 }
1354 if (this->isImmutable()) {
1355 str->append(", immutable");
1356 } else {
1357 str->append(", not-immutable");
1358 }
1359 str->append(")");
1360
1361 SkPixelRef* pr = this->pixelRef();
1362 if (NULL == pr) {
1363 // show null or the explicit pixel address (rare)
1364 str->appendf(" pixels:%p", this->getPixels());
1365 } else {
1366 const char* uri = pr->getURI();
bsalomon49f085d2014-09-05 13:34:00 -07001367 if (uri) {
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001368 str->appendf(" uri:\"%s\"", uri);
1369 } else {
1370 str->appendf(" pixelref:%p", pr);
1371 }
1372 }
1373
1374 str->append(")");
1375}
1376#endif
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001377
1378///////////////////////////////////////////////////////////////////////////////
1379
1380#ifdef SK_DEBUG
1381void SkImageInfo::validate() const {
1382 SkASSERT(fWidth >= 0);
1383 SkASSERT(fHeight >= 0);
1384 SkASSERT(SkColorTypeIsValid(fColorType));
1385 SkASSERT(SkAlphaTypeIsValid(fAlphaType));
1386}
1387#endif