blob: e28c6fdc3488ff486d98a338828389d5d2e4f131 [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 }
287
288 // 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;
294
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();
300
301 // 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: {
reed@google.com2a7579d2012-11-07 18:30:18 +0000589 SkAutoLockColors alc(bm);
590 const SkPMColor* table = alc.colors();
591 if (!table) {
592 return false;
593 }
reed@google.com140d7282013-01-07 20:25:04 +0000594 SkPMColor c = (SkPMColor)~0;
reed@google.com2a7579d2012-11-07 18:30:18 +0000595 for (int i = bm.getColorTable()->count() - 1; i >= 0; --i) {
596 c &= table[i];
597 }
598 return 0xFF == SkGetPackedA32(c);
599 } break;
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000600 case kRGB_565_SkColorType:
reed@google.com2a7579d2012-11-07 18:30:18 +0000601 return true;
602 break;
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000603 case kARGB_4444_SkColorType: {
reed@google.com2a7579d2012-11-07 18:30:18 +0000604 unsigned c = 0xFFFF;
605 for (int y = 0; y < height; ++y) {
606 const SkPMColor16* row = bm.getAddr16(0, y);
607 for (int x = 0; x < width; ++x) {
608 c &= row[x];
609 }
610 if (0xF != SkGetPackedA4444(c)) {
611 return false;
612 }
613 }
614 return true;
615 } break;
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000616 case kBGRA_8888_SkColorType:
617 case kRGBA_8888_SkColorType: {
reed@google.com140d7282013-01-07 20:25:04 +0000618 SkPMColor c = (SkPMColor)~0;
reed@google.com2a7579d2012-11-07 18:30:18 +0000619 for (int y = 0; y < height; ++y) {
620 const SkPMColor* row = bm.getAddr32(0, y);
621 for (int x = 0; x < width; ++x) {
622 c &= row[x];
623 }
624 if (0xFF != SkGetPackedA32(c)) {
625 return false;
626 }
627 }
628 return true;
629 }
630 default:
631 break;
632 }
633 return false;
634}
635
636
reed@android.com8a1c16f2008-12-17 15:59:43 +0000637///////////////////////////////////////////////////////////////////////////////
638///////////////////////////////////////////////////////////////////////////////
639
reed@google.com45f746f2013-06-21 19:51:31 +0000640static uint16_t pack_8888_to_4444(unsigned a, unsigned r, unsigned g, unsigned b) {
641 unsigned pixel = (SkA32To4444(a) << SK_A4444_SHIFT) |
642 (SkR32To4444(r) << SK_R4444_SHIFT) |
643 (SkG32To4444(g) << SK_G4444_SHIFT) |
644 (SkB32To4444(b) << SK_B4444_SHIFT);
645 return SkToU16(pixel);
646}
647
reed@google.com60d32352013-06-28 19:40:50 +0000648void SkBitmap::internalErase(const SkIRect& area,
649 U8CPU a, U8CPU r, U8CPU g, U8CPU b) const {
650#ifdef SK_DEBUG
reed@android.com8a1c16f2008-12-17 15:59:43 +0000651 SkDEBUGCODE(this->validate();)
reed@google.com60d32352013-06-28 19:40:50 +0000652 SkASSERT(!area.isEmpty());
653 {
reed@google.com92833f92013-06-28 19:47:43 +0000654 SkIRect total = { 0, 0, this->width(), this->height() };
reed@google.com60d32352013-06-28 19:40:50 +0000655 SkASSERT(total.contains(area));
656 }
657#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000658
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000659 switch (fInfo.colorType()) {
660 case kUnknown_SkColorType:
661 case kIndex_8_SkColorType:
commit-bot@chromium.org7669a772014-03-27 15:30:35 +0000662 return; // can't erase. Should we bzero so the memory is not uninitialized?
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000663 default:
664 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000665 }
666
667 SkAutoLockPixels alp(*this);
668 // perform this check after the lock call
669 if (!this->readyToDraw()) {
670 return;
671 }
672
reed@google.com60d32352013-06-28 19:40:50 +0000673 int height = area.height();
674 const int width = area.width();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000675 const int rowBytes = fRowBytes;
676
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000677 switch (this->colorType()) {
678 case kAlpha_8_SkColorType: {
reed@google.com60d32352013-06-28 19:40:50 +0000679 uint8_t* p = this->getAddr8(area.fLeft, area.fTop);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000680 while (--height >= 0) {
681 memset(p, a, width);
682 p += rowBytes;
683 }
684 break;
685 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000686 case kARGB_4444_SkColorType:
687 case kRGB_565_SkColorType: {
reed@google.com60d32352013-06-28 19:40:50 +0000688 uint16_t* p = this->getAddr16(area.fLeft, area.fTop);;
reed@google.com45f746f2013-06-21 19:51:31 +0000689 uint16_t v;
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000690
commit-bot@chromium.org7669a772014-03-27 15:30:35 +0000691 // make rgb premultiplied
692 if (255 != a) {
693 r = SkAlphaMul(r, a);
694 g = SkAlphaMul(g, a);
695 b = SkAlphaMul(b, a);
696 }
697
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000698 if (kARGB_4444_SkColorType == this->colorType()) {
reed@google.com45f746f2013-06-21 19:51:31 +0000699 v = pack_8888_to_4444(a, r, g, b);
700 } else {
701 v = SkPackRGB16(r >> (8 - SK_R16_BITS),
702 g >> (8 - SK_G16_BITS),
703 b >> (8 - SK_B16_BITS));
704 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000705 while (--height >= 0) {
706 sk_memset16(p, v, width);
707 p = (uint16_t*)((char*)p + rowBytes);
708 }
709 break;
710 }
commit-bot@chromium.org7669a772014-03-27 15:30:35 +0000711 case kBGRA_8888_SkColorType:
712 case kRGBA_8888_SkColorType: {
reed@google.com60d32352013-06-28 19:40:50 +0000713 uint32_t* p = this->getAddr32(area.fLeft, area.fTop);
commit-bot@chromium.org7669a772014-03-27 15:30:35 +0000714
715 if (255 != a && kPremul_SkAlphaType == this->alphaType()) {
716 r = SkAlphaMul(r, a);
717 g = SkAlphaMul(g, a);
718 b = SkAlphaMul(b, a);
719 }
720 uint32_t v = kRGBA_8888_SkColorType == this->colorType() ?
721 SkPackARGB_as_RGBA(a, r, g, b) : SkPackARGB_as_BGRA(a, r, g, b);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000722
723 while (--height >= 0) {
724 sk_memset32(p, v, width);
725 p = (uint32_t*)((char*)p + rowBytes);
726 }
727 break;
728 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000729 default:
730 return; // no change, so don't call notifyPixelsChanged()
reed@android.com8a1c16f2008-12-17 15:59:43 +0000731 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000732
reed@android.com8a1c16f2008-12-17 15:59:43 +0000733 this->notifyPixelsChanged();
734}
735
reed@google.com60d32352013-06-28 19:40:50 +0000736void SkBitmap::eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const {
reed@google.com92833f92013-06-28 19:47:43 +0000737 SkIRect area = { 0, 0, this->width(), this->height() };
reed@google.com60d32352013-06-28 19:40:50 +0000738 if (!area.isEmpty()) {
739 this->internalErase(area, a, r, g, b);
740 }
741}
742
743void SkBitmap::eraseArea(const SkIRect& rect, SkColor c) const {
reed@google.com92833f92013-06-28 19:47:43 +0000744 SkIRect area = { 0, 0, this->width(), this->height() };
reed@google.com60d32352013-06-28 19:40:50 +0000745 if (area.intersect(rect)) {
746 this->internalErase(area, SkColorGetA(c), SkColorGetR(c),
747 SkColorGetG(c), SkColorGetB(c));
748 }
749}
750
reed@android.com8a1c16f2008-12-17 15:59:43 +0000751//////////////////////////////////////////////////////////////////////////////////////
752//////////////////////////////////////////////////////////////////////////////////////
753
reed@android.com8a1c16f2008-12-17 15:59:43 +0000754bool SkBitmap::extractSubset(SkBitmap* result, const SkIRect& subset) const {
755 SkDEBUGCODE(this->validate();)
756
djsollen@google.comc84b8332012-07-27 13:41:44 +0000757 if (NULL == result || NULL == fPixelRef) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000758 return false; // no src pixels
759 }
760
761 SkIRect srcRect, r;
762 srcRect.set(0, 0, this->width(), this->height());
763 if (!r.intersect(srcRect, subset)) {
764 return false; // r is empty (i.e. no intersection)
765 }
766
scroggo@google.coma2a31922012-12-07 19:14:45 +0000767 if (fPixelRef->getTexture() != NULL) {
768 // Do a deep copy
reede4538f52014-06-11 06:09:50 -0700769 SkPixelRef* pixelRef = fPixelRef->deepCopy(this->colorType(), &subset);
scroggo@google.coma2a31922012-12-07 19:14:45 +0000770 if (pixelRef != NULL) {
771 SkBitmap dst;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000772 dst.setInfo(SkImageInfo::Make(subset.width(), subset.height(),
773 this->colorType(), this->alphaType()));
scroggo@google.coma2a31922012-12-07 19:14:45 +0000774 dst.setIsVolatile(this->isVolatile());
scroggo@google.coma2a31922012-12-07 19:14:45 +0000775 dst.setPixelRef(pixelRef)->unref();
776 SkDEBUGCODE(dst.validate());
777 result->swap(dst);
778 return true;
779 }
780 }
781
scroggo@google.coma2a31922012-12-07 19:14:45 +0000782 // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have
783 // exited above.
784 SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width()));
785 SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height()));
786
reed@android.com8a1c16f2008-12-17 15:59:43 +0000787 SkBitmap dst;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000788 dst.setInfo(SkImageInfo::Make(r.width(), r.height(), this->colorType(), this->alphaType()),
789 this->rowBytes());
skyostil@google.com0eb75762012-01-16 10:45:53 +0000790 dst.setIsVolatile(this->isVolatile());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000791
792 if (fPixelRef) {
reed@google.com672588b2014-01-08 15:42:01 +0000793 SkIPoint origin = fPixelRefOrigin;
794 origin.fX += r.fLeft;
795 origin.fY += r.fTop;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000796 // share the pixelref with a custom offset
reed@google.com672588b2014-01-08 15:42:01 +0000797 dst.setPixelRef(fPixelRef, origin);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000798 }
799 SkDEBUGCODE(dst.validate();)
800
801 // we know we're good, so commit to result
802 result->swap(dst);
803 return true;
804}
805
806///////////////////////////////////////////////////////////////////////////////
807
808#include "SkCanvas.h"
809#include "SkPaint.h"
810
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000811bool SkBitmap::canCopyTo(SkColorType dstColorType) const {
reedb184f7f2014-07-13 04:32:32 -0700812 const SkColorType srcCT = this->colorType();
813
814 if (srcCT == kUnknown_SkColorType) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000815 return false;
816 }
817
reedb184f7f2014-07-13 04:32:32 -0700818 bool sameConfigs = (srcCT == dstColorType);
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000819 switch (dstColorType) {
820 case kAlpha_8_SkColorType:
821 case kRGB_565_SkColorType:
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000822 case kRGBA_8888_SkColorType:
823 case kBGRA_8888_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000824 break;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000825 case kIndex_8_SkColorType:
weita@google.comf9ab99a2009-05-03 18:23:30 +0000826 if (!sameConfigs) {
827 return false;
828 }
829 break;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000830 case kARGB_4444_SkColorType:
reedb184f7f2014-07-13 04:32:32 -0700831 return sameConfigs || kN32_SkColorType == srcCT || kIndex_8_SkColorType == srcCT;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000832 default:
833 return false;
834 }
reed@android.comfbaa88d2009-05-06 17:44:34 +0000835 return true;
836}
837
reedb184f7f2014-07-13 04:32:32 -0700838#include "SkConfig8888.h"
839
840bool SkBitmap::readPixels(const SkImageInfo& requestedDstInfo, void* dstPixels, size_t dstRB,
841 int x, int y) const {
842 if (kUnknown_SkColorType == requestedDstInfo.colorType()) {
843 return false;
844 }
845 if (NULL == dstPixels || dstRB < requestedDstInfo.minRowBytes()) {
846 return false;
847 }
848 if (0 == requestedDstInfo.width() || 0 == requestedDstInfo.height()) {
849 return false;
850 }
851
852 SkIRect srcR = SkIRect::MakeXYWH(x, y, requestedDstInfo.width(), requestedDstInfo.height());
853 if (!srcR.intersect(0, 0, this->width(), this->height())) {
854 return false;
855 }
856
reedb184f7f2014-07-13 04:32:32 -0700857 // the intersect may have shrunk info's logical size
reede5ea5002014-09-03 11:54:58 -0700858 const SkImageInfo dstInfo = requestedDstInfo.makeWH(srcR.width(), srcR.height());
reedb184f7f2014-07-13 04:32:32 -0700859
860 // if x or y are negative, then we have to adjust pixels
861 if (x > 0) {
862 x = 0;
863 }
864 if (y > 0) {
865 y = 0;
866 }
867 // here x,y are either 0 or negative
868 dstPixels = ((char*)dstPixels - y * dstRB - x * dstInfo.bytesPerPixel());
869
870 //////////////
871
872 SkAutoLockPixels alp(*this);
873
874 // since we don't stop creating un-pixeled devices yet, check for no pixels here
875 if (NULL == this->getPixels()) {
876 return false;
877 }
878
reede5ea5002014-09-03 11:54:58 -0700879 const SkImageInfo srcInfo = this->info().makeWH(dstInfo.width(), dstInfo.height());
reedb184f7f2014-07-13 04:32:32 -0700880
881 const void* srcPixels = this->getAddr(srcR.x(), srcR.y());
882 return SkPixelInfo::CopyPixels(dstInfo, dstPixels, dstRB, srcInfo, srcPixels, this->rowBytes(),
883 this->getColorTable());
884}
885
886bool SkBitmap::copyTo(SkBitmap* dst, SkColorType dstColorType, Allocator* alloc) const {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000887 if (!this->canCopyTo(dstColorType)) {
reed@android.comfbaa88d2009-05-06 17:44:34 +0000888 return false;
889 }
890
reed@google.com50dfa012011-04-01 19:05:36 +0000891 // if we have a texture, first get those pixels
892 SkBitmap tmpSrc;
893 const SkBitmap* src = this;
894
scroggo@google.coma2a31922012-12-07 19:14:45 +0000895 if (fPixelRef) {
896 SkIRect subset;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000897 subset.setXYWH(fPixelRefOrigin.fX, fPixelRefOrigin.fY,
898 fInfo.width(), fInfo.height());
reed@google.com672588b2014-01-08 15:42:01 +0000899 if (fPixelRef->readPixels(&tmpSrc, &subset)) {
scroggo0187dc22014-06-05 11:18:04 -0700900 if (fPixelRef->info().alphaType() == kUnpremul_SkAlphaType) {
901 // FIXME: The only meaningful implementation of readPixels
902 // (GrPixelRef) assumes premultiplied pixels.
903 return false;
904 }
reed@google.com672588b2014-01-08 15:42:01 +0000905 SkASSERT(tmpSrc.width() == this->width());
906 SkASSERT(tmpSrc.height() == this->height());
reed@google.com50dfa012011-04-01 19:05:36 +0000907
reed@google.com672588b2014-01-08 15:42:01 +0000908 // did we get lucky and we can just return tmpSrc?
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000909 if (tmpSrc.colorType() == dstColorType && NULL == alloc) {
reed@google.com672588b2014-01-08 15:42:01 +0000910 dst->swap(tmpSrc);
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000911 // If the result is an exact copy, clone the gen ID.
912 if (dst->pixelRef() && dst->pixelRef()->info() == fPixelRef->info()) {
reed@google.com672588b2014-01-08 15:42:01 +0000913 dst->pixelRef()->cloneGenID(*fPixelRef);
scroggo@google.coma2a31922012-12-07 19:14:45 +0000914 }
reed@google.com672588b2014-01-08 15:42:01 +0000915 return true;
scroggo@google.comd5764e82012-08-22 15:00:05 +0000916 }
reed@google.com672588b2014-01-08 15:42:01 +0000917
918 // fall through to the raster case
919 src = &tmpSrc;
reed@google.com50dfa012011-04-01 19:05:36 +0000920 }
reed@android.comfbaa88d2009-05-06 17:44:34 +0000921 }
reed@android.com311c82d2009-05-05 23:13:23 +0000922
reed@google.com50dfa012011-04-01 19:05:36 +0000923 // we lock this now, since we may need its colortable
924 SkAutoLockPixels srclock(*src);
925 if (!src->readyToDraw()) {
926 return false;
927 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000928
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000929 // The only way to be readyToDraw is if fPixelRef is non NULL.
930 SkASSERT(fPixelRef != NULL);
931
reede5ea5002014-09-03 11:54:58 -0700932 const SkImageInfo dstInfo = src->info().makeColorType(dstColorType);
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000933
reed@google.com50dfa012011-04-01 19:05:36 +0000934 SkBitmap tmpDst;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000935 if (!tmpDst.setInfo(dstInfo)) {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000936 return false;
937 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000938
weita@google.comf9ab99a2009-05-03 18:23:30 +0000939 // allocate colortable if srcConfig == kIndex8_Config
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000940 SkAutoTUnref<SkColorTable> ctable;
941 if (dstColorType == kIndex_8_SkColorType) {
942 // TODO: can we just ref() the src colortable? Is it reentrant-safe?
943 ctable.reset(SkNEW_ARGS(SkColorTable, (*src->getColorTable())));
944 }
reed84825042014-09-02 12:50:45 -0700945 if (!tmpDst.tryAllocPixels(alloc, ctable)) {
weita@google.comf9ab99a2009-05-03 18:23:30 +0000946 return false;
947 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000948
reed@google.com50dfa012011-04-01 19:05:36 +0000949 if (!tmpDst.readyToDraw()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000950 // allocator/lock failed
951 return false;
952 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000953
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000954 // pixelRef must be non NULL or tmpDst.readyToDraw() would have
955 // returned false.
956 SkASSERT(tmpDst.pixelRef() != NULL);
957
reedb184f7f2014-07-13 04:32:32 -0700958 if (!src->readPixels(tmpDst.info(), tmpDst.getPixels(), tmpDst.rowBytes(), 0, 0)) {
959 return false;
960 }
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000961
reedb184f7f2014-07-13 04:32:32 -0700962 // (for BitmapHeap) Clone the pixelref genID even though we have a new pixelref.
963 // The old copyTo impl did this, so we continue it for now.
964 //
965 // TODO: should we ignore rowbytes (i.e. getSize)? Then it could just be
966 // if (src_pixelref->info == dst_pixelref->info)
967 //
968 if (src->colorType() == dstColorType && tmpDst.getSize() == src->getSize()) {
969 SkPixelRef* dstPixelRef = tmpDst.pixelRef();
970 if (dstPixelRef->info() == fPixelRef->info()) {
971 dstPixelRef->cloneGenID(*fPixelRef);
reed@android.com311c82d2009-05-05 23:13:23 +0000972 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000973 }
974
reed@google.com50dfa012011-04-01 19:05:36 +0000975 dst->swap(tmpDst);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000976 return true;
977}
978
commit-bot@chromium.orgfab349c2014-03-05 02:34:58 +0000979bool SkBitmap::deepCopyTo(SkBitmap* dst) const {
reede4538f52014-06-11 06:09:50 -0700980 const SkColorType dstCT = this->colorType();
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000981
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000982 if (!this->canCopyTo(dstCT)) {
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000983 return false;
984 }
985
986 // If we have a PixelRef, and it supports deep copy, use it.
987 // Currently supported only by texture-backed bitmaps.
988 if (fPixelRef) {
reede4538f52014-06-11 06:09:50 -0700989 SkPixelRef* pixelRef = fPixelRef->deepCopy(dstCT, NULL);
commit-bot@chromium.orgeeef0cc2014-05-21 15:58:00 +0000990 if (pixelRef) {
991 uint32_t rowBytes;
992 if (this->colorType() == dstCT) {
993 // Since there is no subset to pass to deepCopy, and deepCopy
994 // succeeded, the new pixel ref must be identical.
995 SkASSERT(fPixelRef->info() == pixelRef->info());
996 pixelRef->cloneGenID(*fPixelRef);
997 // Use the same rowBytes as the original.
998 rowBytes = fRowBytes;
999 } else {
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001000 // With the new config, an appropriate fRowBytes will be computed by setInfo.
commit-bot@chromium.orgeeef0cc2014-05-21 15:58:00 +00001001 rowBytes = 0;
1002 }
1003
reede5ea5002014-09-03 11:54:58 -07001004 const SkImageInfo info = fInfo.makeColorType(dstCT);
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001005 if (!dst->setInfo(info, rowBytes)) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001006 return false;
1007 }
commit-bot@chromium.orgeeef0cc2014-05-21 15:58:00 +00001008 dst->setPixelRef(pixelRef, fPixelRefOrigin)->unref();
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001009 return true;
1010 }
1011 }
1012
1013 if (this->getTexture()) {
1014 return false;
1015 } else {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +00001016 return this->copyTo(dst, dstCT, NULL);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001017 }
1018}
1019
reed@android.com8a1c16f2008-12-17 15:59:43 +00001020///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +00001021
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +00001022static bool GetBitmapAlpha(const SkBitmap& src, uint8_t* SK_RESTRICT alpha,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001023 int alphaRowBytes) {
1024 SkASSERT(alpha != NULL);
1025 SkASSERT(alphaRowBytes >= src.width());
1026
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001027 SkColorType colorType = src.colorType();
1028 int w = src.width();
1029 int h = src.height();
1030 size_t rb = src.rowBytes();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001031
reed@android.com1cdcb512009-08-24 19:11:00 +00001032 SkAutoLockPixels alp(src);
1033 if (!src.readyToDraw()) {
1034 // zero out the alpha buffer and return
1035 while (--h >= 0) {
1036 memset(alpha, 0, w);
1037 alpha += alphaRowBytes;
1038 }
1039 return false;
1040 }
reed@google.com82065d62011-02-07 15:30:46 +00001041
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001042 if (kAlpha_8_SkColorType == colorType && !src.isOpaque()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001043 const uint8_t* s = src.getAddr8(0, 0);
1044 while (--h >= 0) {
1045 memcpy(alpha, s, w);
1046 s += rb;
1047 alpha += alphaRowBytes;
1048 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001049 } else if (kN32_SkColorType == colorType && !src.isOpaque()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001050 const SkPMColor* SK_RESTRICT s = src.getAddr32(0, 0);
1051 while (--h >= 0) {
1052 for (int x = 0; x < w; x++) {
1053 alpha[x] = SkGetPackedA32(s[x]);
1054 }
1055 s = (const SkPMColor*)((const char*)s + rb);
1056 alpha += alphaRowBytes;
1057 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001058 } else if (kARGB_4444_SkColorType == colorType && !src.isOpaque()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001059 const SkPMColor16* SK_RESTRICT s = src.getAddr16(0, 0);
1060 while (--h >= 0) {
1061 for (int x = 0; x < w; x++) {
1062 alpha[x] = SkPacked4444ToA32(s[x]);
1063 }
1064 s = (const SkPMColor16*)((const char*)s + rb);
1065 alpha += alphaRowBytes;
1066 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001067 } else if (kIndex_8_SkColorType == colorType && !src.isOpaque()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001068 SkColorTable* ct = src.getColorTable();
1069 if (ct) {
1070 const SkPMColor* SK_RESTRICT table = ct->lockColors();
1071 const uint8_t* SK_RESTRICT s = src.getAddr8(0, 0);
1072 while (--h >= 0) {
1073 for (int x = 0; x < w; x++) {
1074 alpha[x] = SkGetPackedA32(table[s[x]]);
1075 }
1076 s += rb;
1077 alpha += alphaRowBytes;
1078 }
reed@google.com0a6151d2013-10-10 14:44:56 +00001079 ct->unlockColors();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001080 }
1081 } else { // src is opaque, so just fill alpha[] with 0xFF
1082 memset(alpha, 0xFF, h * alphaRowBytes);
1083 }
reed@android.com1cdcb512009-08-24 19:11:00 +00001084 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001085}
1086
1087#include "SkPaint.h"
1088#include "SkMaskFilter.h"
1089#include "SkMatrix.h"
1090
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001091bool SkBitmap::extractAlpha(SkBitmap* dst, const SkPaint* paint,
djsollen@google.com57f49692011-02-23 20:46:31 +00001092 Allocator *allocator, SkIPoint* offset) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001093 SkDEBUGCODE(this->validate();)
1094
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001095 SkBitmap tmpBitmap;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001096 SkMatrix identity;
1097 SkMask srcM, dstM;
1098
1099 srcM.fBounds.set(0, 0, this->width(), this->height());
1100 srcM.fRowBytes = SkAlign4(this->width());
1101 srcM.fFormat = SkMask::kA8_Format;
1102
1103 SkMaskFilter* filter = paint ? paint->getMaskFilter() : NULL;
1104
1105 // compute our (larger?) dst bounds if we have a filter
bsalomon49f085d2014-09-05 13:34:00 -07001106 if (filter) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001107 identity.reset();
1108 srcM.fImage = NULL;
1109 if (!filter->filterMask(&dstM, srcM, identity, NULL)) {
1110 goto NO_FILTER_CASE;
1111 }
1112 dstM.fRowBytes = SkAlign4(dstM.fBounds.width());
1113 } else {
1114 NO_FILTER_CASE:
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001115 tmpBitmap.setInfo(SkImageInfo::MakeA8(this->width(), this->height()), srcM.fRowBytes);
reed84825042014-09-02 12:50:45 -07001116 if (!tmpBitmap.tryAllocPixels(allocator, NULL)) {
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001117 // Allocation of pixels for alpha bitmap failed.
1118 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
1119 tmpBitmap.width(), tmpBitmap.height());
1120 return false;
1121 }
1122 GetBitmapAlpha(*this, tmpBitmap.getAddr8(0, 0), srcM.fRowBytes);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001123 if (offset) {
1124 offset->set(0, 0);
1125 }
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001126 tmpBitmap.swap(*dst);
1127 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001128 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001129 srcM.fImage = SkMask::AllocImage(srcM.computeImageSize());
1130 SkAutoMaskFreeImage srcCleanup(srcM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001131
1132 GetBitmapAlpha(*this, srcM.fImage, srcM.fRowBytes);
1133 if (!filter->filterMask(&dstM, srcM, identity, NULL)) {
1134 goto NO_FILTER_CASE;
1135 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001136 SkAutoMaskFreeImage dstCleanup(dstM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001137
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001138 tmpBitmap.setInfo(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.height()),
1139 dstM.fRowBytes);
reed84825042014-09-02 12:50:45 -07001140 if (!tmpBitmap.tryAllocPixels(allocator, NULL)) {
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001141 // Allocation of pixels for alpha bitmap failed.
1142 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
1143 tmpBitmap.width(), tmpBitmap.height());
1144 return false;
1145 }
1146 memcpy(tmpBitmap.getPixels(), dstM.fImage, dstM.computeImageSize());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001147 if (offset) {
1148 offset->set(dstM.fBounds.fLeft, dstM.fBounds.fTop);
1149 }
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001150 SkDEBUGCODE(tmpBitmap.validate();)
1151
1152 tmpBitmap.swap(*dst);
1153 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001154}
1155
1156///////////////////////////////////////////////////////////////////////////////
1157
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001158void SkBitmap::WriteRawPixels(SkWriteBuffer* buffer, const SkBitmap& bitmap) {
1159 const SkImageInfo info = bitmap.info();
1160 SkAutoLockPixels alp(bitmap);
1161 if (0 == info.width() || 0 == info.height() || NULL == bitmap.getPixels()) {
1162 buffer->writeUInt(0); // instead of snugRB, signaling no pixels
1163 return;
1164 }
1165
1166 const size_t snugRB = info.width() * info.bytesPerPixel();
1167 const char* src = (const char*)bitmap.getPixels();
1168 const size_t ramRB = bitmap.rowBytes();
skia.committer@gmail.com3c134a92014-05-24 03:05:26 +00001169
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001170 buffer->write32(SkToU32(snugRB));
1171 info.flatten(*buffer);
1172
1173 const size_t size = snugRB * info.height();
1174 SkAutoMalloc storage(size);
1175 char* dst = (char*)storage.get();
1176 for (int y = 0; y < info.height(); ++y) {
1177 memcpy(dst, src, snugRB);
1178 dst += snugRB;
1179 src += ramRB;
1180 }
1181 buffer->writeByteArray(storage.get(), size);
1182
1183 SkColorTable* ct = bitmap.getColorTable();
1184 if (kIndex_8_SkColorType == info.colorType() && ct) {
1185 buffer->writeBool(true);
1186 ct->writeToBuffer(*buffer);
1187 } else {
1188 buffer->writeBool(false);
1189 }
1190}
1191
1192bool SkBitmap::ReadRawPixels(SkReadBuffer* buffer, SkBitmap* bitmap) {
1193 const size_t snugRB = buffer->readUInt();
1194 if (0 == snugRB) { // no pixels
1195 return false;
1196 }
1197
1198 SkImageInfo info;
1199 info.unflatten(*buffer);
1200
sugoica95c192014-07-08 09:18:48 -07001201 // If there was an error reading "info", don't use it to compute minRowBytes()
1202 if (!buffer->validate(true)) {
1203 return false;
1204 }
1205
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001206 const size_t ramRB = info.minRowBytes();
1207 const int height = info.height();
1208 const size_t snugSize = snugRB * height;
1209 const size_t ramSize = ramRB * height;
commit-bot@chromium.org05858432014-05-30 01:06:44 +00001210 if (!buffer->validate(snugSize <= ramSize)) {
1211 return false;
1212 }
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001213
1214 char* dst = (char*)sk_malloc_throw(ramSize);
1215 buffer->readByteArray(dst, snugSize);
1216 SkAutoDataUnref data(SkData::NewFromMalloc(dst, ramSize));
1217
1218 if (snugSize != ramSize) {
1219 const char* srcRow = dst + snugRB * (height - 1);
1220 char* dstRow = dst + ramRB * (height - 1);
1221 for (int y = height - 1; y >= 1; --y) {
1222 memmove(dstRow, srcRow, snugRB);
1223 srcRow -= snugRB;
1224 dstRow -= ramRB;
1225 }
1226 SkASSERT(srcRow == dstRow); // first row does not need to be moved
1227 }
skia.committer@gmail.com3c134a92014-05-24 03:05:26 +00001228
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001229 SkAutoTUnref<SkColorTable> ctable;
1230 if (buffer->readBool()) {
1231 ctable.reset(SkNEW_ARGS(SkColorTable, (*buffer)));
1232 }
skia.committer@gmail.com3c134a92014-05-24 03:05:26 +00001233
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001234 SkAutoTUnref<SkPixelRef> pr(SkMallocPixelRef::NewWithData(info, info.minRowBytes(),
1235 ctable.get(), data.get()));
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001236 bitmap->setInfo(pr->info());
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001237 bitmap->setPixelRef(pr, 0, 0);
1238 return true;
1239}
1240
reed@android.com8a1c16f2008-12-17 15:59:43 +00001241enum {
1242 SERIALIZE_PIXELTYPE_NONE,
djsollen@google.com21830d92012-08-07 19:49:41 +00001243 SERIALIZE_PIXELTYPE_REF_DATA
reed@android.com8a1c16f2008-12-17 15:59:43 +00001244};
1245
commit-bot@chromium.org851155c2014-05-27 14:03:51 +00001246void SkBitmap::legacyUnflatten(SkReadBuffer& buffer) {
reed9a9eae22014-07-07 14:32:06 -07001247#ifdef SK_SUPPORT_LEGACY_PIXELREF_UNFLATTENABLE
reed@android.com8a1c16f2008-12-17 15:59:43 +00001248 this->reset();
weita@google.comf9ab99a2009-05-03 18:23:30 +00001249
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001250 SkImageInfo info;
1251 info.unflatten(buffer);
1252 size_t rowBytes = buffer.readInt();
commit-bot@chromium.org33fed142014-02-13 18:46:13 +00001253 if (!buffer.validate((info.width() >= 0) && (info.height() >= 0) &&
1254 SkColorTypeIsValid(info.fColorType) &&
1255 SkAlphaTypeIsValid(info.fAlphaType) &&
scroggo2fd0d142014-07-01 07:08:19 -07001256 SkColorTypeValidateAlphaType(info.fColorType, info.fAlphaType) &&
commit-bot@chromium.org33fed142014-02-13 18:46:13 +00001257 info.validRowBytes(rowBytes))) {
1258 return;
1259 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001260
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001261 bool configIsValid = this->setInfo(info, rowBytes);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001262 buffer.validate(configIsValid);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001263
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001264 int reftype = buffer.readInt();
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +00001265 if (buffer.validate((SERIALIZE_PIXELTYPE_REF_DATA == reftype) ||
1266 (SERIALIZE_PIXELTYPE_NONE == reftype))) {
1267 switch (reftype) {
1268 case SERIALIZE_PIXELTYPE_REF_DATA: {
reed@google.com672588b2014-01-08 15:42:01 +00001269 SkIPoint origin;
1270 origin.fX = buffer.readInt();
1271 origin.fY = buffer.readInt();
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001272 size_t offset = origin.fY * rowBytes + origin.fX * info.bytesPerPixel();
reed9a9eae22014-07-07 14:32:06 -07001273 SkPixelRef* pr = buffer.readFlattenable<SkPixelRef>();
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +00001274 if (!buffer.validate((NULL == pr) ||
1275 (pr->getAllocatedSizeInBytes() >= (offset + this->getSafeSize())))) {
reed@google.com672588b2014-01-08 15:42:01 +00001276 origin.setZero();
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +00001277 }
reed@google.com672588b2014-01-08 15:42:01 +00001278 SkSafeUnref(this->setPixelRef(pr, origin));
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +00001279 break;
1280 }
1281 case SERIALIZE_PIXELTYPE_NONE:
1282 break;
1283 default:
1284 SkDEBUGFAIL("unrecognized pixeltype in serialized data");
1285 sk_throw();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001286 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001287 }
reed9a9eae22014-07-07 14:32:06 -07001288#else
1289 sk_throw();
1290#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001291}
1292
1293///////////////////////////////////////////////////////////////////////////////
1294
1295SkBitmap::RLEPixels::RLEPixels(int width, int height) {
1296 fHeight = height;
commit-bot@chromium.org235002f2013-10-09 18:39:59 +00001297 fYPtrs = (uint8_t**)sk_calloc_throw(height * sizeof(uint8_t*));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001298}
1299
1300SkBitmap::RLEPixels::~RLEPixels() {
1301 sk_free(fYPtrs);
1302}
1303
1304///////////////////////////////////////////////////////////////////////////////
1305
1306#ifdef SK_DEBUG
1307void SkBitmap::validate() const {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001308 fInfo.validate();
commit-bot@chromium.orgd5414e52014-02-13 22:30:38 +00001309
1310 // ImageInfo may not require this, but Bitmap ensures that opaque-only
1311 // colorTypes report opaque for their alphatype
1312 if (kRGB_565_SkColorType == fInfo.colorType()) {
1313 SkASSERT(kOpaque_SkAlphaType == fInfo.alphaType());
1314 }
1315
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001316 SkASSERT(fInfo.validRowBytes(fRowBytes));
scroggo08470592014-07-15 19:56:48 -07001317 uint8_t allFlags = kImageIsVolatile_Flag;
scroggo@google.com8e990eb2013-06-14 15:55:56 +00001318#ifdef SK_BUILD_FOR_ANDROID
1319 allFlags |= kHasHardwareMipMap_Flag;
1320#endif
scroggo08470592014-07-15 19:56:48 -07001321 SkASSERT((~allFlags & fFlags) == 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001322 SkASSERT(fPixelLockCount >= 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001323
reed@google.com615316c2014-01-15 19:15:23 +00001324 if (fPixels) {
1325 SkASSERT(fPixelRef);
1326 SkASSERT(fPixelLockCount > 0);
1327 SkASSERT(fPixelRef->isLocked());
1328 SkASSERT(fPixelRef->rowBytes() == fRowBytes);
1329 SkASSERT(fPixelRefOrigin.fX >= 0);
1330 SkASSERT(fPixelRefOrigin.fY >= 0);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001331 SkASSERT(fPixelRef->info().width() >= (int)this->width() + fPixelRefOrigin.fX);
reede5ea5002014-09-03 11:54:58 -07001332 SkASSERT(fPixelRef->info().height() >= (int)this->height() + fPixelRefOrigin.fY);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001333 SkASSERT(fPixelRef->rowBytes() >= fInfo.minRowBytes());
reed@google.com615316c2014-01-15 19:15:23 +00001334 } else {
1335 SkASSERT(NULL == fColorTable);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001336 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001337}
1338#endif
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001339
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +00001340#ifndef SK_IGNORE_TO_STRING
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001341void SkBitmap::toString(SkString* str) const {
1342
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001343 static const char* gColorTypeNames[kLastEnum_SkColorType + 1] = {
1344 "UNKNOWN", "A8", "565", "4444", "RGBA", "BGRA", "INDEX8",
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001345 };
1346
1347 str->appendf("bitmap: ((%d, %d) %s", this->width(), this->height(),
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001348 gColorTypeNames[this->colorType()]);
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001349
1350 str->append(" (");
1351 if (this->isOpaque()) {
1352 str->append("opaque");
1353 } else {
1354 str->append("transparent");
1355 }
1356 if (this->isImmutable()) {
1357 str->append(", immutable");
1358 } else {
1359 str->append(", not-immutable");
1360 }
1361 str->append(")");
1362
1363 SkPixelRef* pr = this->pixelRef();
1364 if (NULL == pr) {
1365 // show null or the explicit pixel address (rare)
1366 str->appendf(" pixels:%p", this->getPixels());
1367 } else {
1368 const char* uri = pr->getURI();
bsalomon49f085d2014-09-05 13:34:00 -07001369 if (uri) {
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001370 str->appendf(" uri:\"%s\"", uri);
1371 } else {
1372 str->appendf(" pixelref:%p", pr);
1373 }
1374 }
1375
1376 str->append(")");
1377}
1378#endif
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001379
1380///////////////////////////////////////////////////////////////////////////////
1381
1382#ifdef SK_DEBUG
1383void SkImageInfo::validate() const {
1384 SkASSERT(fWidth >= 0);
1385 SkASSERT(fHeight >= 0);
1386 SkASSERT(SkColorTypeIsValid(fColorType));
1387 SkASSERT(SkAlphaTypeIsValid(fAlphaType));
1388}
1389#endif