blob: 9d51f9b1cc981a09bee95df948dba506e9135b82 [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"
14#include "SkMallocPixelRef.h"
15#include "SkMask.h"
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000016#include "SkOrderedReadBuffer.h"
17#include "SkOrderedWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000018#include "SkPixelRef.h"
19#include "SkThread.h"
vandebo@chromium.org112706d2011-02-24 22:50:55 +000020#include "SkUnPreMultiply.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000021#include "SkUtils.h"
22#include "SkPackBits.h"
23#include <new>
24
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000025SK_DEFINE_INST_COUNT(SkBitmap::Allocator)
26
reed@android.com149e2f62009-05-22 14:39:03 +000027static bool isPos32Bits(const Sk64& value) {
28 return !value.isNeg() && value.is32();
29}
30
reed@android.com8a1c16f2008-12-17 15:59:43 +000031struct MipLevel {
32 void* fPixels;
33 uint32_t fRowBytes;
reed@android.comf459a492009-03-27 12:33:50 +000034 uint32_t fWidth, fHeight;
reed@android.com8a1c16f2008-12-17 15:59:43 +000035};
36
37struct SkBitmap::MipMap : SkNoncopyable {
38 int32_t fRefCnt;
39 int fLevelCount;
40// MipLevel fLevel[fLevelCount];
41// Pixels[]
weita@google.comf9ab99a2009-05-03 18:23:30 +000042
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 static MipMap* Alloc(int levelCount, size_t pixelSize) {
reed@android.com149e2f62009-05-22 14:39:03 +000044 if (levelCount < 0) {
45 return NULL;
46 }
47 Sk64 size;
48 size.setMul(levelCount + 1, sizeof(MipLevel));
49 size.add(sizeof(MipMap));
50 size.add(pixelSize);
51 if (!isPos32Bits(size)) {
52 return NULL;
53 }
54 MipMap* mm = (MipMap*)sk_malloc_throw(size.get32());
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 mm->fRefCnt = 1;
56 mm->fLevelCount = levelCount;
57 return mm;
58 }
59
60 const MipLevel* levels() const { return (const MipLevel*)(this + 1); }
61 MipLevel* levels() { return (MipLevel*)(this + 1); }
62
63 const void* pixels() const { return levels() + fLevelCount; }
64 void* pixels() { return levels() + fLevelCount; }
weita@google.comf9ab99a2009-05-03 18:23:30 +000065
reed@android.com149e2f62009-05-22 14:39:03 +000066 void ref() {
67 if (SK_MaxS32 == sk_atomic_inc(&fRefCnt)) {
68 sk_throw();
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 }
70 }
reed@android.com149e2f62009-05-22 14:39:03 +000071 void unref() {
72 SkASSERT(fRefCnt > 0);
73 if (sk_atomic_dec(&fRefCnt) == 1) {
74 sk_free(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 }
76 }
77};
reed@android.com8a1c16f2008-12-17 15:59:43 +000078
79///////////////////////////////////////////////////////////////////////////////
80///////////////////////////////////////////////////////////////////////////////
81
82SkBitmap::SkBitmap() {
reed@android.com4516f472009-06-29 16:25:36 +000083 sk_bzero(this, sizeof(*this));
reed@android.com8a1c16f2008-12-17 15:59:43 +000084}
85
86SkBitmap::SkBitmap(const SkBitmap& src) {
87 SkDEBUGCODE(src.validate();)
reed@android.com4516f472009-06-29 16:25:36 +000088 sk_bzero(this, sizeof(*this));
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 *this = src;
90 SkDEBUGCODE(this->validate();)
91}
92
93SkBitmap::~SkBitmap() {
94 SkDEBUGCODE(this->validate();)
95 this->freePixels();
96}
97
98SkBitmap& SkBitmap::operator=(const SkBitmap& src) {
99 if (this != &src) {
100 this->freePixels();
101 memcpy(this, &src, sizeof(src));
102
103 // inc src reference counts
reed@android.com83f7bc32009-07-17 02:42:41 +0000104 SkSafeRef(src.fPixelRef);
reed@android.com149e2f62009-05-22 14:39:03 +0000105 SkSafeRef(src.fMipMap);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106
107 // we reset our locks if we get blown away
108 fPixelLockCount = 0;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000109
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110 /* The src could be in 3 states
111 1. no pixelref, in which case we just copy/ref the pixels/ctable
112 2. unlocked pixelref, pixels/ctable should be null
113 3. locked pixelref, we should lock the ref again ourselves
114 */
115 if (NULL == fPixelRef) {
116 // leave fPixels as it is
reed@google.com82065d62011-02-07 15:30:46 +0000117 SkSafeRef(fColorTable); // ref the user's ctable if present
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 } else { // we have a pixelref, so pixels/ctable reflect it
119 // ignore the values from the memcpy
120 fPixels = NULL;
121 fColorTable = NULL;
bsalomon@google.com586f48c2011-04-14 15:07:22 +0000122 // Note that what to for genID is somewhat arbitrary. We have no
123 // way to track changes to raw pixels across multiple SkBitmaps.
124 // Would benefit from an SkRawPixelRef type created by
125 // setPixels.
126 // Just leave the memcpy'ed one but they'll get out of sync
127 // as soon either is modified.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128 }
129 }
130
131 SkDEBUGCODE(this->validate();)
132 return *this;
133}
134
135void SkBitmap::swap(SkBitmap& other) {
bsalomon@google.com586f48c2011-04-14 15:07:22 +0000136 SkTSwap(fColorTable, other.fColorTable);
137 SkTSwap(fPixelRef, other.fPixelRef);
138 SkTSwap(fPixelRefOffset, other.fPixelRefOffset);
139 SkTSwap(fPixelLockCount, other.fPixelLockCount);
140 SkTSwap(fMipMap, other.fMipMap);
141 SkTSwap(fPixels, other.fPixels);
bsalomon@google.com586f48c2011-04-14 15:07:22 +0000142 SkTSwap(fRowBytes, other.fRowBytes);
143 SkTSwap(fWidth, other.fWidth);
144 SkTSwap(fHeight, other.fHeight);
145 SkTSwap(fConfig, other.fConfig);
146 SkTSwap(fFlags, other.fFlags);
147 SkTSwap(fBytesPerPixel, other.fBytesPerPixel);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148
149 SkDEBUGCODE(this->validate();)
150}
151
152void SkBitmap::reset() {
153 this->freePixels();
reed@android.com4516f472009-06-29 16:25:36 +0000154 sk_bzero(this, sizeof(*this));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155}
156
157int SkBitmap::ComputeBytesPerPixel(SkBitmap::Config config) {
158 int bpp;
159 switch (config) {
160 case kNo_Config:
161 case kA1_Config:
162 bpp = 0; // not applicable
163 break;
164 case kRLE_Index8_Config:
165 case kA8_Config:
166 case kIndex8_Config:
167 bpp = 1;
168 break;
169 case kRGB_565_Config:
170 case kARGB_4444_Config:
171 bpp = 2;
172 break;
173 case kARGB_8888_Config:
174 bpp = 4;
175 break;
176 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000177 SkDEBUGFAIL("unknown config");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178 bpp = 0; // error
179 break;
180 }
181 return bpp;
182}
183
184int SkBitmap::ComputeRowBytes(Config c, int width) {
reed@android.com149e2f62009-05-22 14:39:03 +0000185 if (width < 0) {
186 return 0;
187 }
188
189 Sk64 rowBytes;
190 rowBytes.setZero();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000191
192 switch (c) {
193 case kNo_Config:
194 case kRLE_Index8_Config:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000195 break;
196 case kA1_Config:
reed@android.com149e2f62009-05-22 14:39:03 +0000197 rowBytes.set(width);
198 rowBytes.add(7);
199 rowBytes.shiftRight(3);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000200 break;
201 case kA8_Config:
202 case kIndex8_Config:
reed@android.com149e2f62009-05-22 14:39:03 +0000203 rowBytes.set(width);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000204 break;
205 case kRGB_565_Config:
206 case kARGB_4444_Config:
reed@android.com149e2f62009-05-22 14:39:03 +0000207 rowBytes.set(width);
208 rowBytes.shiftLeft(1);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000209 break;
210 case kARGB_8888_Config:
reed@android.com149e2f62009-05-22 14:39:03 +0000211 rowBytes.set(width);
212 rowBytes.shiftLeft(2);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000213 break;
214 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000215 SkDEBUGFAIL("unknown config");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000216 break;
217 }
reed@android.com149e2f62009-05-22 14:39:03 +0000218 return isPos32Bits(rowBytes) ? rowBytes.get32() : 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000219}
220
221Sk64 SkBitmap::ComputeSize64(Config c, int width, int height) {
222 Sk64 size;
223 size.setMul(SkBitmap::ComputeRowBytes(c, width), height);
224 return size;
225}
226
227size_t SkBitmap::ComputeSize(Config c, int width, int height) {
228 Sk64 size = SkBitmap::ComputeSize64(c, width, height);
reed@android.com149e2f62009-05-22 14:39:03 +0000229 return isPos32Bits(size) ? size.get32() : 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000230}
231
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000232Sk64 SkBitmap::ComputeSafeSize64(Config config,
233 uint32_t width,
234 uint32_t height,
235 uint32_t rowBytes) {
236 Sk64 safeSize;
237 safeSize.setZero();
238 if (height > 0) {
239 safeSize.set(ComputeRowBytes(config, width));
240 Sk64 sizeAllButLastRow;
241 sizeAllButLastRow.setMul(height - 1, rowBytes);
242 safeSize.add(sizeAllButLastRow);
243 }
244 SkASSERT(!safeSize.isNeg());
245 return safeSize;
246}
247
248size_t SkBitmap::ComputeSafeSize(Config config,
249 uint32_t width,
250 uint32_t height,
251 uint32_t rowBytes) {
252 Sk64 safeSize = ComputeSafeSize64(config, width, height, rowBytes);
253 return (safeSize.is32() ? safeSize.get32() : 0);
254}
255
reed@google.com86b2e432012-03-15 21:17:03 +0000256void SkBitmap::getBounds(SkRect* bounds) const {
257 SkASSERT(bounds);
258 bounds->set(0, 0,
259 SkIntToScalar(fWidth), SkIntToScalar(fHeight));
260}
261
reed@google.com80e14592012-03-16 14:58:07 +0000262void SkBitmap::getBounds(SkIRect* bounds) const {
263 SkASSERT(bounds);
264 bounds->set(0, 0, fWidth, fHeight);
265}
266
reed@google.com86b2e432012-03-15 21:17:03 +0000267///////////////////////////////////////////////////////////////////////////////
268
reed@android.com8a1c16f2008-12-17 15:59:43 +0000269void SkBitmap::setConfig(Config c, int width, int height, int rowBytes) {
270 this->freePixels();
271
reed@android.com149e2f62009-05-22 14:39:03 +0000272 if ((width | height | rowBytes) < 0) {
agl@chromium.org6b8cb252009-06-01 23:52:37 +0000273 goto err;
reed@android.com149e2f62009-05-22 14:39:03 +0000274 }
275
reed@android.com8a1c16f2008-12-17 15:59:43 +0000276 if (rowBytes == 0) {
277 rowBytes = SkBitmap::ComputeRowBytes(c, width);
reed@android.com149e2f62009-05-22 14:39:03 +0000278 if (0 == rowBytes && kNo_Config != c) {
agl@chromium.org6b8cb252009-06-01 23:52:37 +0000279 goto err;
reed@android.com149e2f62009-05-22 14:39:03 +0000280 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000281 }
reed@android.com89bb83a2009-05-29 21:30:42 +0000282
reed@android.com8a1c16f2008-12-17 15:59:43 +0000283 fConfig = SkToU8(c);
reed@android.comf459a492009-03-27 12:33:50 +0000284 fWidth = width;
285 fHeight = height;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000286 fRowBytes = rowBytes;
287
288 fBytesPerPixel = (uint8_t)ComputeBytesPerPixel(c);
289
290 SkDEBUGCODE(this->validate();)
reed@android.com9e0c2fc2009-06-02 18:54:28 +0000291 return;
agl@chromium.org6b8cb252009-06-01 23:52:37 +0000292
reed@android.com9e0c2fc2009-06-02 18:54:28 +0000293 // if we got here, we had an error, so we reset the bitmap to empty
agl@chromium.org6b8cb252009-06-01 23:52:37 +0000294err:
295 this->reset();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000296}
297
298void SkBitmap::updatePixelsFromRef() const {
299 if (NULL != fPixelRef) {
300 if (fPixelLockCount > 0) {
reed@google.comff0da4f2012-05-17 13:14:52 +0000301 SkASSERT(fPixelRef->isLocked());
weita@google.comf9ab99a2009-05-03 18:23:30 +0000302
reed@android.com8a1c16f2008-12-17 15:59:43 +0000303 void* p = fPixelRef->pixels();
304 if (NULL != p) {
305 p = (char*)p + fPixelRefOffset;
306 }
307 fPixels = p;
308 SkRefCnt_SafeAssign(fColorTable, fPixelRef->colorTable());
309 } else {
310 SkASSERT(0 == fPixelLockCount);
311 fPixels = NULL;
reed@android.com149e2f62009-05-22 14:39:03 +0000312 if (fColorTable) {
313 fColorTable->unref();
314 fColorTable = NULL;
315 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000316 }
317 }
318}
319
320SkPixelRef* SkBitmap::setPixelRef(SkPixelRef* pr, size_t offset) {
321 // do this first, we that we never have a non-zero offset with a null ref
322 if (NULL == pr) {
323 offset = 0;
324 }
325
326 if (fPixelRef != pr || fPixelRefOffset != offset) {
327 if (fPixelRef != pr) {
328 this->freePixels();
329 SkASSERT(NULL == fPixelRef);
weita@google.comf9ab99a2009-05-03 18:23:30 +0000330
reed@google.com82065d62011-02-07 15:30:46 +0000331 SkSafeRef(pr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000332 fPixelRef = pr;
333 }
334 fPixelRefOffset = offset;
335 this->updatePixelsFromRef();
336 }
337
338 SkDEBUGCODE(this->validate();)
339 return pr;
340}
341
342void SkBitmap::lockPixels() const {
343 if (NULL != fPixelRef && 1 == ++fPixelLockCount) {
344 fPixelRef->lockPixels();
345 this->updatePixelsFromRef();
346 }
347 SkDEBUGCODE(this->validate();)
348}
349
350void SkBitmap::unlockPixels() const {
351 SkASSERT(NULL == fPixelRef || fPixelLockCount > 0);
352
353 if (NULL != fPixelRef && 0 == --fPixelLockCount) {
354 fPixelRef->unlockPixels();
355 this->updatePixelsFromRef();
356 }
357 SkDEBUGCODE(this->validate();)
358}
359
reed@google.com9c49bc32011-07-07 13:42:37 +0000360bool SkBitmap::lockPixelsAreWritable() const {
djsollen@google.comc84b8332012-07-27 13:41:44 +0000361 return (fPixelRef) ? fPixelRef->lockPixelsAreWritable() : false;
reed@google.com9c49bc32011-07-07 13:42:37 +0000362}
363
reed@android.com8a1c16f2008-12-17 15:59:43 +0000364void SkBitmap::setPixels(void* p, SkColorTable* ctable) {
reed@google.com8e1034e2012-07-30 13:16:35 +0000365 if (NULL == p) {
366 this->setPixelRef(NULL, 0);
367 return;
368 }
369
djsollen@google.comc84b8332012-07-27 13:41:44 +0000370 Sk64 size = this->getSize64();
371 SkASSERT(!size.isNeg() && size.is32());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000372
djsollen@google.comc84b8332012-07-27 13:41:44 +0000373 this->setPixelRef(new SkMallocPixelRef(p, size.get32(), ctable, false))->unref();
374 // since we're already allocated, we lockPixels right away
375 this->lockPixels();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000376 SkDEBUGCODE(this->validate();)
377}
378
379bool SkBitmap::allocPixels(Allocator* allocator, SkColorTable* ctable) {
380 HeapAllocator stdalloc;
381
382 if (NULL == allocator) {
383 allocator = &stdalloc;
384 }
385 return allocator->allocPixelRef(this, ctable);
386}
387
388void SkBitmap::freePixels() {
389 // if we're gonna free the pixels, we certainly need to free the mipmap
390 this->freeMipMap();
391
reed@android.com149e2f62009-05-22 14:39:03 +0000392 if (fColorTable) {
393 fColorTable->unref();
394 fColorTable = NULL;
395 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000396
397 if (NULL != fPixelRef) {
398 if (fPixelLockCount > 0) {
399 fPixelRef->unlockPixels();
400 }
401 fPixelRef->unref();
402 fPixelRef = NULL;
403 fPixelRefOffset = 0;
404 }
405 fPixelLockCount = 0;
406 fPixels = NULL;
407}
408
409void SkBitmap::freeMipMap() {
reed@android.com149e2f62009-05-22 14:39:03 +0000410 if (fMipMap) {
411 fMipMap->unref();
412 fMipMap = NULL;
413 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000414}
415
416uint32_t SkBitmap::getGenerationID() const {
djsollen@google.comc84b8332012-07-27 13:41:44 +0000417 return (fPixelRef) ? fPixelRef->getGenerationID() : 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000418}
419
420void SkBitmap::notifyPixelsChanged() const {
junov@chromium.orgb0521292011-12-15 20:14:06 +0000421 SkASSERT(!this->isImmutable());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000422 if (fPixelRef) {
423 fPixelRef->notifyPixelsChanged();
424 }
425}
426
reed@android.comce4e53a2010-09-09 16:01:26 +0000427SkGpuTexture* SkBitmap::getTexture() const {
428 return fPixelRef ? fPixelRef->getTexture() : NULL;
429}
430
reed@android.com8a1c16f2008-12-17 15:59:43 +0000431///////////////////////////////////////////////////////////////////////////////
432
reed@android.com8a1c16f2008-12-17 15:59:43 +0000433/** We explicitly use the same allocator for our pixels that SkMask does,
434 so that we can freely assign memory allocated by one class to the other.
435 */
436bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst,
437 SkColorTable* ctable) {
438 Sk64 size = dst->getSize64();
439 if (size.isNeg() || !size.is32()) {
440 return false;
441 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000442
reed@android.com8a1c16f2008-12-17 15:59:43 +0000443 void* addr = sk_malloc_flags(size.get32(), 0); // returns NULL on failure
444 if (NULL == addr) {
445 return false;
446 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000447
reed@android.com8a1c16f2008-12-17 15:59:43 +0000448 dst->setPixelRef(new SkMallocPixelRef(addr, size.get32(), ctable))->unref();
449 // since we're already allocated, we lockPixels right away
450 dst->lockPixels();
451 return true;
452}
453
454///////////////////////////////////////////////////////////////////////////////
455
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000456size_t SkBitmap::getSafeSize() const {
457 // This is intended to be a size_t version of ComputeSafeSize64(), just
458 // faster. The computation is meant to be identical.
459 return (fHeight ? ((fHeight - 1) * fRowBytes) +
460 ComputeRowBytes(getConfig(), fWidth): 0);
461}
462
463Sk64 SkBitmap::getSafeSize64() const {
464 return ComputeSafeSize64(getConfig(), fWidth, fHeight, fRowBytes);
465}
466
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000467bool SkBitmap::copyPixelsTo(void* const dst, size_t dstSize,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000468 int dstRowBytes, bool preserveDstPad) const {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000469
470 if (dstRowBytes == -1)
471 dstRowBytes = fRowBytes;
472 SkASSERT(dstRowBytes >= 0);
473
474 if (getConfig() == kRLE_Index8_Config ||
475 dstRowBytes < ComputeRowBytes(getConfig(), fWidth) ||
476 dst == NULL || (getPixels() == NULL && pixelRef() == NULL))
477 return false;
478
bsalomon@google.comc6980972011-11-02 19:57:21 +0000479 if (!preserveDstPad && static_cast<uint32_t>(dstRowBytes) == fRowBytes) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000480 size_t safeSize = getSafeSize();
481 if (safeSize > dstSize || safeSize == 0)
482 return false;
483 else {
484 SkAutoLockPixels lock(*this);
485 // This implementation will write bytes beyond the end of each row,
486 // excluding the last row, if the bitmap's stride is greater than
487 // strictly required by the current config.
488 memcpy(dst, getPixels(), safeSize);
489
490 return true;
491 }
492 } else {
493 // If destination has different stride than us, then copy line by line.
494 if (ComputeSafeSize(getConfig(), fWidth, fHeight, dstRowBytes) >
495 dstSize)
496 return false;
497 else {
498 // Just copy what we need on each line.
499 uint32_t rowBytes = ComputeRowBytes(getConfig(), fWidth);
500 SkAutoLockPixels lock(*this);
501 const uint8_t* srcP = reinterpret_cast<const uint8_t*>(getPixels());
502 uint8_t* dstP = reinterpret_cast<uint8_t*>(dst);
503 for (uint32_t row = 0; row < fHeight;
504 row++, srcP += fRowBytes, dstP += dstRowBytes) {
505 memcpy(dstP, srcP, rowBytes);
506 }
507
508 return true;
509 }
510 }
511}
512
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000513///////////////////////////////////////////////////////////////////////////////
514
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000515bool SkBitmap::isImmutable() const {
junov@chromium.orgb0521292011-12-15 20:14:06 +0000516 return fPixelRef ? fPixelRef->isImmutable() :
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000517 fFlags & kImageIsImmutable_Flag;
junov@chromium.orgb0521292011-12-15 20:14:06 +0000518}
519
520void SkBitmap::setImmutable() {
521 if (fPixelRef) {
522 fPixelRef->setImmutable();
523 } else {
524 fFlags |= kImageIsImmutable_Flag;
525 }
526}
527
reed@android.com8a1c16f2008-12-17 15:59:43 +0000528bool SkBitmap::isOpaque() const {
529 switch (fConfig) {
530 case kNo_Config:
531 return true;
532
533 case kA1_Config:
534 case kA8_Config:
535 case kARGB_4444_Config:
536 case kARGB_8888_Config:
537 return (fFlags & kImageIsOpaque_Flag) != 0;
538
539 case kIndex8_Config:
540 case kRLE_Index8_Config: {
541 uint32_t flags = 0;
542
543 this->lockPixels();
544 // if lockPixels failed, we may not have a ctable ptr
545 if (fColorTable) {
546 flags = fColorTable->getFlags();
547 }
548 this->unlockPixels();
549
550 return (flags & SkColorTable::kColorsAreOpaque_Flag) != 0;
551 }
552
553 case kRGB_565_Config:
554 return true;
555
556 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000557 SkDEBUGFAIL("unknown bitmap config pased to isOpaque");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000558 return false;
559 }
560}
561
562void SkBitmap::setIsOpaque(bool isOpaque) {
563 /* we record this regardless of fConfig, though it is ignored in
564 isOpaque() for configs that can't support per-pixel alpha.
565 */
566 if (isOpaque) {
567 fFlags |= kImageIsOpaque_Flag;
568 } else {
569 fFlags &= ~kImageIsOpaque_Flag;
570 }
571}
572
junov@google.com4ee7ae52011-06-30 17:30:49 +0000573bool SkBitmap::isVolatile() const {
574 return (fFlags & kImageIsVolatile_Flag) != 0;
575}
576
577void SkBitmap::setIsVolatile(bool isVolatile) {
578 if (isVolatile) {
579 fFlags |= kImageIsVolatile_Flag;
580 } else {
581 fFlags &= ~kImageIsVolatile_Flag;
582 }
583}
584
reed@android.com8a1c16f2008-12-17 15:59:43 +0000585void* SkBitmap::getAddr(int x, int y) const {
586 SkASSERT((unsigned)x < (unsigned)this->width());
587 SkASSERT((unsigned)y < (unsigned)this->height());
588
589 char* base = (char*)this->getPixels();
590 if (base) {
591 base += y * this->rowBytes();
592 switch (this->config()) {
593 case SkBitmap::kARGB_8888_Config:
594 base += x << 2;
595 break;
596 case SkBitmap::kARGB_4444_Config:
597 case SkBitmap::kRGB_565_Config:
598 base += x << 1;
599 break;
600 case SkBitmap::kA8_Config:
601 case SkBitmap::kIndex8_Config:
602 base += x;
603 break;
604 case SkBitmap::kA1_Config:
605 base += x >> 3;
606 break;
607 case kRLE_Index8_Config:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000608 SkDEBUGFAIL("Can't return addr for kRLE_Index8_Config");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000609 base = NULL;
610 break;
611 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000612 SkDEBUGFAIL("Can't return addr for config");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000613 base = NULL;
614 break;
615 }
616 }
617 return base;
618}
619
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000620SkColor SkBitmap::getColor(int x, int y) const {
621 SkASSERT((unsigned)x < (unsigned)this->width());
622 SkASSERT((unsigned)y < (unsigned)this->height());
623
624 switch (this->config()) {
625 case SkBitmap::kA1_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000626 uint8_t* addr = this->getAddr1(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000627 uint8_t mask = 1 << (7 - (x % 8));
628 if (addr[0] & mask) {
629 return SK_ColorBLACK;
630 } else {
631 return 0;
632 }
633 }
634 case SkBitmap::kA8_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000635 uint8_t* addr = this->getAddr8(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000636 return SkColorSetA(0, addr[0]);
637 }
638 case SkBitmap::kIndex8_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000639 SkPMColor c = this->getIndex8Color(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000640 return SkUnPreMultiply::PMColorToColor(c);
641 }
642 case SkBitmap::kRGB_565_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000643 uint16_t* addr = this->getAddr16(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000644 return SkPixel16ToColor(addr[0]);
645 }
646 case SkBitmap::kARGB_4444_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000647 uint16_t* addr = this->getAddr16(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000648 SkPMColor c = SkPixel4444ToPixel32(addr[0]);
649 return SkUnPreMultiply::PMColorToColor(c);
650 }
651 case SkBitmap::kARGB_8888_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000652 uint32_t* addr = this->getAddr32(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000653 return SkUnPreMultiply::PMColorToColor(addr[0]);
654 }
655 case kRLE_Index8_Config: {
656 uint8_t dst;
657 const SkBitmap::RLEPixels* rle =
reed@google.com3b521d02011-04-29 11:53:41 +0000658 (const SkBitmap::RLEPixels*)this->getPixels();
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000659 SkPackBits::Unpack8(&dst, x, 1, rle->packedAtY(y));
660 return SkUnPreMultiply::PMColorToColor((*fColorTable)[dst]);
661 }
662 case kNo_Config:
663 case kConfigCount:
664 SkASSERT(false);
665 return 0;
666 }
667 SkASSERT(false); // Not reached.
668 return 0;
669}
670
reed@google.com2a7579d2012-11-07 18:30:18 +0000671bool SkBitmap::ComputeIsOpaque(const SkBitmap& bm) {
672 SkAutoLockPixels alp(bm);
673 if (!bm.getPixels()) {
674 return false;
675 }
676
677 const int height = bm.height();
678 const int width = bm.width();
679
680 switch (bm.config()) {
681 case SkBitmap::kA1_Config: {
682 // TODO
683 } break;
684 case SkBitmap::kA8_Config: {
685 unsigned a = 0xFF;
686 for (int y = 0; y < height; ++y) {
687 const uint8_t* row = bm.getAddr8(0, y);
688 for (int x = 0; x < width; ++x) {
689 a &= row[x];
690 }
691 if (0xFF != a) {
692 return false;
693 }
694 }
695 return true;
696 } break;
697 case kRLE_Index8_Config:
698 case SkBitmap::kIndex8_Config: {
699 SkAutoLockColors alc(bm);
700 const SkPMColor* table = alc.colors();
701 if (!table) {
702 return false;
703 }
704 SkPMColor c = ~0;
705 for (int i = bm.getColorTable()->count() - 1; i >= 0; --i) {
706 c &= table[i];
707 }
708 return 0xFF == SkGetPackedA32(c);
709 } break;
710 case SkBitmap::kRGB_565_Config:
711 return true;
712 break;
713 case SkBitmap::kARGB_4444_Config: {
714 unsigned c = 0xFFFF;
715 for (int y = 0; y < height; ++y) {
716 const SkPMColor16* row = bm.getAddr16(0, y);
717 for (int x = 0; x < width; ++x) {
718 c &= row[x];
719 }
720 if (0xF != SkGetPackedA4444(c)) {
721 return false;
722 }
723 }
724 return true;
725 } break;
726 case SkBitmap::kARGB_8888_Config: {
727 SkPMColor c = ~0;
728 for (int y = 0; y < height; ++y) {
729 const SkPMColor* row = bm.getAddr32(0, y);
730 for (int x = 0; x < width; ++x) {
731 c &= row[x];
732 }
733 if (0xFF != SkGetPackedA32(c)) {
734 return false;
735 }
736 }
737 return true;
738 }
739 default:
740 break;
741 }
742 return false;
743}
744
745
reed@android.com8a1c16f2008-12-17 15:59:43 +0000746///////////////////////////////////////////////////////////////////////////////
747///////////////////////////////////////////////////////////////////////////////
748
749void SkBitmap::eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const {
750 SkDEBUGCODE(this->validate();)
751
752 if (0 == fWidth || 0 == fHeight ||
753 kNo_Config == fConfig || kIndex8_Config == fConfig) {
754 return;
755 }
756
757 SkAutoLockPixels alp(*this);
758 // perform this check after the lock call
759 if (!this->readyToDraw()) {
760 return;
761 }
762
763 int height = fHeight;
764 const int width = fWidth;
765 const int rowBytes = fRowBytes;
766
767 // make rgb premultiplied
768 if (255 != a) {
769 r = SkAlphaMul(r, a);
770 g = SkAlphaMul(g, a);
771 b = SkAlphaMul(b, a);
772 }
773
774 switch (fConfig) {
775 case kA1_Config: {
776 uint8_t* p = (uint8_t*)fPixels;
777 const int count = (width + 7) >> 3;
778 a = (a >> 7) ? 0xFF : 0;
779 SkASSERT(count <= rowBytes);
780 while (--height >= 0) {
781 memset(p, a, count);
782 p += rowBytes;
783 }
784 break;
785 }
786 case kA8_Config: {
787 uint8_t* p = (uint8_t*)fPixels;
788 while (--height >= 0) {
789 memset(p, a, width);
790 p += rowBytes;
791 }
792 break;
793 }
794 case kARGB_4444_Config:
795 case kRGB_565_Config: {
796 uint16_t* p = (uint16_t*)fPixels;
797 uint16_t v;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000798
reed@android.com8a1c16f2008-12-17 15:59:43 +0000799 if (kARGB_4444_Config == fConfig) {
800 v = SkPackARGB4444(a >> 4, r >> 4, g >> 4, b >> 4);
801 } else { // kRGB_565_Config
802 v = SkPackRGB16(r >> (8 - SK_R16_BITS), g >> (8 - SK_G16_BITS),
803 b >> (8 - SK_B16_BITS));
804 }
805 while (--height >= 0) {
806 sk_memset16(p, v, width);
807 p = (uint16_t*)((char*)p + rowBytes);
808 }
809 break;
810 }
811 case kARGB_8888_Config: {
812 uint32_t* p = (uint32_t*)fPixels;
813 uint32_t v = SkPackARGB32(a, r, g, b);
814
815 while (--height >= 0) {
816 sk_memset32(p, v, width);
817 p = (uint32_t*)((char*)p + rowBytes);
818 }
819 break;
820 }
821 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000822
reed@android.com8a1c16f2008-12-17 15:59:43 +0000823 this->notifyPixelsChanged();
824}
825
826//////////////////////////////////////////////////////////////////////////////////////
827//////////////////////////////////////////////////////////////////////////////////////
828
829#define SUB_OFFSET_FAILURE ((size_t)-1)
830
831static size_t getSubOffset(const SkBitmap& bm, int x, int y) {
832 SkASSERT((unsigned)x < (unsigned)bm.width());
833 SkASSERT((unsigned)y < (unsigned)bm.height());
weita@google.comf9ab99a2009-05-03 18:23:30 +0000834
reed@android.com8a1c16f2008-12-17 15:59:43 +0000835 switch (bm.getConfig()) {
836 case SkBitmap::kA8_Config:
837 case SkBitmap:: kIndex8_Config:
838 // x is fine as is for the calculation
839 break;
840
841 case SkBitmap::kRGB_565_Config:
842 case SkBitmap::kARGB_4444_Config:
843 x <<= 1;
844 break;
845
846 case SkBitmap::kARGB_8888_Config:
847 x <<= 2;
848 break;
849
850 case SkBitmap::kNo_Config:
851 case SkBitmap::kA1_Config:
852 default:
853 return SUB_OFFSET_FAILURE;
854 }
855 return y * bm.rowBytes() + x;
856}
857
858bool SkBitmap::extractSubset(SkBitmap* result, const SkIRect& subset) const {
859 SkDEBUGCODE(this->validate();)
860
djsollen@google.comc84b8332012-07-27 13:41:44 +0000861 if (NULL == result || NULL == fPixelRef) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000862 return false; // no src pixels
863 }
864
865 SkIRect srcRect, r;
866 srcRect.set(0, 0, this->width(), this->height());
867 if (!r.intersect(srcRect, subset)) {
868 return false; // r is empty (i.e. no intersection)
869 }
870
871 if (kRLE_Index8_Config == fConfig) {
872 SkAutoLockPixels alp(*this);
873 // don't call readyToDraw(), since we can operate w/o a colortable
874 // at this stage
875 if (this->getPixels() == NULL) {
876 return false;
877 }
878 SkBitmap bm;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000879
reed@android.com8a1c16f2008-12-17 15:59:43 +0000880 bm.setConfig(kIndex8_Config, r.width(), r.height());
881 bm.allocPixels(this->getColorTable());
882 if (NULL == bm.getPixels()) {
883 return false;
884 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000885
reed@android.com8a1c16f2008-12-17 15:59:43 +0000886 const RLEPixels* rle = (const RLEPixels*)this->getPixels();
887 uint8_t* dst = bm.getAddr8(0, 0);
888 const int width = bm.width();
889 const int rowBytes = bm.rowBytes();
weita@google.comf9ab99a2009-05-03 18:23:30 +0000890
reed@android.com8a1c16f2008-12-17 15:59:43 +0000891 for (int y = r.fTop; y < r.fBottom; y++) {
892 SkPackBits::Unpack8(dst, r.fLeft, width, rle->packedAtY(y));
893 dst += rowBytes;
894 }
895 result->swap(bm);
896 return true;
897 }
898
899 size_t offset = getSubOffset(*this, r.fLeft, r.fTop);
900 if (SUB_OFFSET_FAILURE == offset) {
901 return false; // config not supported
902 }
903
904 SkBitmap dst;
905 dst.setConfig(this->config(), r.width(), r.height(), this->rowBytes());
skyostil@google.com0eb75762012-01-16 10:45:53 +0000906 dst.setIsVolatile(this->isVolatile());
reed@google.com04685d22012-10-15 13:45:40 +0000907 dst.setIsOpaque(this->isOpaque());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000908
909 if (fPixelRef) {
910 // share the pixelref with a custom offset
911 dst.setPixelRef(fPixelRef, fPixelRefOffset + offset);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000912 }
913 SkDEBUGCODE(dst.validate();)
914
915 // we know we're good, so commit to result
916 result->swap(dst);
917 return true;
918}
919
920///////////////////////////////////////////////////////////////////////////////
921
922#include "SkCanvas.h"
923#include "SkPaint.h"
924
reed@android.comfbaa88d2009-05-06 17:44:34 +0000925bool SkBitmap::canCopyTo(Config dstConfig) const {
926 if (this->getConfig() == kNo_Config) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000927 return false;
928 }
929
reed@android.comfbaa88d2009-05-06 17:44:34 +0000930 bool sameConfigs = (this->config() == dstConfig);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000931 switch (dstConfig) {
932 case kA8_Config:
933 case kARGB_4444_Config:
934 case kRGB_565_Config:
935 case kARGB_8888_Config:
936 break;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000937 case kA1_Config:
938 case kIndex8_Config:
939 if (!sameConfigs) {
940 return false;
941 }
942 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000943 default:
944 return false;
945 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000946
947 // do not copy src if srcConfig == kA1_Config while dstConfig != kA1_Config
948 if (this->getConfig() == kA1_Config && !sameConfigs) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000949 return false;
950 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000951
reed@android.comfbaa88d2009-05-06 17:44:34 +0000952 return true;
953}
954
955bool SkBitmap::copyTo(SkBitmap* dst, Config dstConfig, Allocator* alloc) const {
956 if (!this->canCopyTo(dstConfig)) {
957 return false;
958 }
959
reed@google.com50dfa012011-04-01 19:05:36 +0000960 // if we have a texture, first get those pixels
961 SkBitmap tmpSrc;
962 const SkBitmap* src = this;
963
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000964 if (fPixelRef && fPixelRef->readPixels(&tmpSrc)) {
reed@google.com50dfa012011-04-01 19:05:36 +0000965 SkASSERT(tmpSrc.width() == this->width());
966 SkASSERT(tmpSrc.height() == this->height());
967
968 // did we get lucky and we can just return tmpSrc?
969 if (tmpSrc.config() == dstConfig && NULL == alloc) {
970 dst->swap(tmpSrc);
scroggo@google.comd5764e82012-08-22 15:00:05 +0000971 if (dst->pixelRef()) {
972 dst->pixelRef()->fGenerationID = fPixelRef->getGenerationID();
973 }
reed@google.com50dfa012011-04-01 19:05:36 +0000974 return true;
975 }
976
977 // fall through to the raster case
978 src = &tmpSrc;
reed@android.comfbaa88d2009-05-06 17:44:34 +0000979 }
reed@android.com311c82d2009-05-05 23:13:23 +0000980
reed@google.com50dfa012011-04-01 19:05:36 +0000981 // we lock this now, since we may need its colortable
982 SkAutoLockPixels srclock(*src);
983 if (!src->readyToDraw()) {
984 return false;
985 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000986
reed@google.com50dfa012011-04-01 19:05:36 +0000987 SkBitmap tmpDst;
988 tmpDst.setConfig(dstConfig, src->width(), src->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000989
weita@google.comf9ab99a2009-05-03 18:23:30 +0000990 // allocate colortable if srcConfig == kIndex8_Config
991 SkColorTable* ctable = (dstConfig == kIndex8_Config) ?
reed@google.com50dfa012011-04-01 19:05:36 +0000992 new SkColorTable(*src->getColorTable()) : NULL;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000993 SkAutoUnref au(ctable);
reed@google.com50dfa012011-04-01 19:05:36 +0000994 if (!tmpDst.allocPixels(alloc, ctable)) {
weita@google.comf9ab99a2009-05-03 18:23:30 +0000995 return false;
996 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000997
reed@google.com50dfa012011-04-01 19:05:36 +0000998 if (!tmpDst.readyToDraw()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000999 // allocator/lock failed
1000 return false;
1001 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001002
reed@android.comfbaa88d2009-05-06 17:44:34 +00001003 /* do memcpy for the same configs cases, else use drawing
weita@google.comf9ab99a2009-05-03 18:23:30 +00001004 */
reed@google.com50dfa012011-04-01 19:05:36 +00001005 if (src->config() == dstConfig) {
1006 if (tmpDst.getSize() == src->getSize()) {
1007 memcpy(tmpDst.getPixels(), src->getPixels(), src->getSafeSize());
scroggo@google.comd5764e82012-08-22 15:00:05 +00001008 SkPixelRef* pixelRef = tmpDst.pixelRef();
1009 if (pixelRef != NULL) {
1010 pixelRef->fGenerationID = this->getGenerationID();
1011 }
reed@android.com311c82d2009-05-05 23:13:23 +00001012 } else {
reed@google.com50dfa012011-04-01 19:05:36 +00001013 const char* srcP = reinterpret_cast<const char*>(src->getPixels());
1014 char* dstP = reinterpret_cast<char*>(tmpDst.getPixels());
reed@android.com311c82d2009-05-05 23:13:23 +00001015 // to be sure we don't read too much, only copy our logical pixels
reed@google.com50dfa012011-04-01 19:05:36 +00001016 size_t bytesToCopy = tmpDst.width() * tmpDst.bytesPerPixel();
1017 for (int y = 0; y < tmpDst.height(); y++) {
reed@android.com311c82d2009-05-05 23:13:23 +00001018 memcpy(dstP, srcP, bytesToCopy);
reed@google.com50dfa012011-04-01 19:05:36 +00001019 srcP += src->rowBytes();
1020 dstP += tmpDst.rowBytes();
reed@android.com311c82d2009-05-05 23:13:23 +00001021 }
1022 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001023 } else {
1024 // if the src has alpha, we have to clear the dst first
reed@google.com50dfa012011-04-01 19:05:36 +00001025 if (!src->isOpaque()) {
junov@google.comdbfac8a2012-12-06 21:47:40 +00001026 tmpDst.eraseColor(SK_ColorTRANSPARENT);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001027 }
1028
reed@google.com50dfa012011-04-01 19:05:36 +00001029 SkCanvas canvas(tmpDst);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001030 SkPaint paint;
1031
1032 paint.setDither(true);
reed@google.com50dfa012011-04-01 19:05:36 +00001033 canvas.drawBitmap(*src, 0, 0, &paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001034 }
1035
reed@google.com50dfa012011-04-01 19:05:36 +00001036 tmpDst.setIsOpaque(src->isOpaque());
reed@android.comcafc9f92009-08-22 03:44:57 +00001037
reed@google.com50dfa012011-04-01 19:05:36 +00001038 dst->swap(tmpDst);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001039 return true;
1040}
1041
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001042bool SkBitmap::deepCopyTo(SkBitmap* dst, Config dstConfig) const {
1043 if (!this->canCopyTo(dstConfig)) {
1044 return false;
1045 }
1046
1047 // If we have a PixelRef, and it supports deep copy, use it.
1048 // Currently supported only by texture-backed bitmaps.
1049 if (fPixelRef) {
1050 SkPixelRef* pixelRef = fPixelRef->deepCopy(dstConfig);
1051 if (pixelRef) {
scroggo@google.comd5764e82012-08-22 15:00:05 +00001052 if (dstConfig == fConfig) {
1053 pixelRef->fGenerationID = fPixelRef->getGenerationID();
1054 }
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001055 dst->setConfig(dstConfig, fWidth, fHeight);
1056 dst->setPixelRef(pixelRef)->unref();
1057 return true;
1058 }
1059 }
1060
1061 if (this->getTexture()) {
1062 return false;
1063 } else {
1064 return this->copyTo(dst, dstConfig, NULL);
1065 }
1066}
1067
reed@android.com8a1c16f2008-12-17 15:59:43 +00001068///////////////////////////////////////////////////////////////////////////////
1069///////////////////////////////////////////////////////////////////////////////
1070
1071static void downsampleby2_proc32(SkBitmap* dst, int x, int y,
1072 const SkBitmap& src) {
1073 x <<= 1;
1074 y <<= 1;
1075 const SkPMColor* p = src.getAddr32(x, y);
reed@android.com829c83c2009-06-08 12:05:31 +00001076 const SkPMColor* baseP = p;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001077 SkPMColor c, ag, rb;
1078
1079 c = *p; ag = (c >> 8) & 0xFF00FF; rb = c & 0xFF00FF;
1080 if (x < src.width() - 1) {
1081 p += 1;
1082 }
1083 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
1084
reed@android.com829c83c2009-06-08 12:05:31 +00001085 p = baseP;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001086 if (y < src.height() - 1) {
reed@android.com829c83c2009-06-08 12:05:31 +00001087 p += src.rowBytes() >> 2;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001088 }
1089 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
1090 if (x < src.width() - 1) {
1091 p += 1;
1092 }
1093 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
1094
1095 *dst->getAddr32(x >> 1, y >> 1) =
1096 ((rb >> 2) & 0xFF00FF) | ((ag << 6) & 0xFF00FF00);
1097}
1098
1099static inline uint32_t expand16(U16CPU c) {
1100 return (c & ~SK_G16_MASK_IN_PLACE) | ((c & SK_G16_MASK_IN_PLACE) << 16);
1101}
1102
1103// returns dirt in the top 16bits, but we don't care, since we only
1104// store the low 16bits.
1105static inline U16CPU pack16(uint32_t c) {
1106 return (c & ~SK_G16_MASK_IN_PLACE) | ((c >> 16) & SK_G16_MASK_IN_PLACE);
1107}
1108
1109static void downsampleby2_proc16(SkBitmap* dst, int x, int y,
1110 const SkBitmap& src) {
1111 x <<= 1;
1112 y <<= 1;
1113 const uint16_t* p = src.getAddr16(x, y);
reed@android.com829c83c2009-06-08 12:05:31 +00001114 const uint16_t* baseP = p;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001115 SkPMColor c;
weita@google.comf9ab99a2009-05-03 18:23:30 +00001116
reed@android.com8a1c16f2008-12-17 15:59:43 +00001117 c = expand16(*p);
reed@android.com829c83c2009-06-08 12:05:31 +00001118 if (x < src.width() - 1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001119 p += 1;
1120 }
1121 c += expand16(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001122
reed@android.com829c83c2009-06-08 12:05:31 +00001123 p = baseP;
1124 if (y < src.height() - 1) {
1125 p += src.rowBytes() >> 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001126 }
1127 c += expand16(*p);
reed@android.com829c83c2009-06-08 12:05:31 +00001128 if (x < src.width() - 1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001129 p += 1;
1130 }
1131 c += expand16(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001132
reed@android.com8a1c16f2008-12-17 15:59:43 +00001133 *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)pack16(c >> 2);
1134}
1135
1136static uint32_t expand4444(U16CPU c) {
1137 return (c & 0xF0F) | ((c & ~0xF0F) << 12);
1138}
1139
1140static U16CPU collaps4444(uint32_t c) {
1141 return (c & 0xF0F) | ((c >> 12) & ~0xF0F);
1142}
1143
1144static void downsampleby2_proc4444(SkBitmap* dst, int x, int y,
1145 const SkBitmap& src) {
1146 x <<= 1;
1147 y <<= 1;
1148 const uint16_t* p = src.getAddr16(x, y);
reed@android.com829c83c2009-06-08 12:05:31 +00001149 const uint16_t* baseP = p;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001150 uint32_t c;
weita@google.comf9ab99a2009-05-03 18:23:30 +00001151
reed@android.com8a1c16f2008-12-17 15:59:43 +00001152 c = expand4444(*p);
1153 if (x < src.width() - 1) {
1154 p += 1;
1155 }
1156 c += expand4444(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001157
reed@android.com829c83c2009-06-08 12:05:31 +00001158 p = baseP;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001159 if (y < src.height() - 1) {
reed@android.com829c83c2009-06-08 12:05:31 +00001160 p += src.rowBytes() >> 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001161 }
1162 c += expand4444(*p);
1163 if (x < src.width() - 1) {
1164 p += 1;
1165 }
1166 c += expand4444(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001167
reed@android.com8a1c16f2008-12-17 15:59:43 +00001168 *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)collaps4444(c >> 2);
1169}
1170
1171void SkBitmap::buildMipMap(bool forceRebuild) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001172 if (forceRebuild)
1173 this->freeMipMap();
1174 else if (fMipMap)
1175 return; // we're already built
1176
1177 SkASSERT(NULL == fMipMap);
1178
1179 void (*proc)(SkBitmap* dst, int x, int y, const SkBitmap& src);
1180
1181 const SkBitmap::Config config = this->getConfig();
1182
1183 switch (config) {
1184 case kARGB_8888_Config:
1185 proc = downsampleby2_proc32;
1186 break;
1187 case kRGB_565_Config:
1188 proc = downsampleby2_proc16;
1189 break;
1190 case kARGB_4444_Config:
1191 proc = downsampleby2_proc4444;
1192 break;
1193 case kIndex8_Config:
1194 case kA8_Config:
1195 default:
1196 return; // don't build mipmaps for these configs
1197 }
reed@android.com89bb83a2009-05-29 21:30:42 +00001198
reed@android.com149e2f62009-05-22 14:39:03 +00001199 SkAutoLockPixels alp(*this);
1200 if (!this->readyToDraw()) {
1201 return;
1202 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001203
1204 // whip through our loop to compute the exact size needed
1205 size_t size = 0;
1206 int maxLevels = 0;
1207 {
reed@android.com149e2f62009-05-22 14:39:03 +00001208 int width = this->width();
1209 int height = this->height();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001210 for (;;) {
1211 width >>= 1;
1212 height >>= 1;
1213 if (0 == width || 0 == height) {
1214 break;
1215 }
1216 size += ComputeRowBytes(config, width) * height;
1217 maxLevels += 1;
1218 }
1219 }
reed@android.com89bb83a2009-05-29 21:30:42 +00001220
reed@android.com149e2f62009-05-22 14:39:03 +00001221 // nothing to build
reed@android.com8a1c16f2008-12-17 15:59:43 +00001222 if (0 == maxLevels) {
1223 return;
1224 }
1225
reed@android.com149e2f62009-05-22 14:39:03 +00001226 SkBitmap srcBM(*this);
1227 srcBM.lockPixels();
1228 if (!srcBM.readyToDraw()) {
1229 return;
1230 }
1231
1232 MipMap* mm = MipMap::Alloc(maxLevels, size);
1233 if (NULL == mm) {
1234 return;
1235 }
1236
reed@android.com8a1c16f2008-12-17 15:59:43 +00001237 MipLevel* level = mm->levels();
1238 uint8_t* addr = (uint8_t*)mm->pixels();
reed@android.com149e2f62009-05-22 14:39:03 +00001239 int width = this->width();
1240 int height = this->height();
robertphillips@google.com5b5bba32012-09-24 14:20:00 +00001241 unsigned rowBytes;
reed@android.com149e2f62009-05-22 14:39:03 +00001242 SkBitmap dstBM;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001243
1244 for (int i = 0; i < maxLevels; i++) {
1245 width >>= 1;
1246 height >>= 1;
1247 rowBytes = ComputeRowBytes(config, width);
1248
1249 level[i].fPixels = addr;
reed@android.comf459a492009-03-27 12:33:50 +00001250 level[i].fWidth = width;
1251 level[i].fHeight = height;
reed@android.com49f0ff22009-03-19 21:52:42 +00001252 level[i].fRowBytes = rowBytes;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001253
1254 dstBM.setConfig(config, width, height, rowBytes);
1255 dstBM.setPixels(addr);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001256
bungeman@google.com7cf0e9e2012-07-25 16:09:10 +00001257 srcBM.lockPixels();
reed@android.com149e2f62009-05-22 14:39:03 +00001258 for (int y = 0; y < height; y++) {
1259 for (int x = 0; x < width; x++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001260 proc(&dstBM, x, y, srcBM);
1261 }
1262 }
bungeman@google.com7cf0e9e2012-07-25 16:09:10 +00001263 srcBM.unlockPixels();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001264
1265 srcBM = dstBM;
1266 addr += height * rowBytes;
1267 }
1268 SkASSERT(addr == (uint8_t*)mm->pixels() + size);
1269 fMipMap = mm;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001270}
1271
1272bool SkBitmap::hasMipMap() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001273 return fMipMap != NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001274}
1275
1276int SkBitmap::extractMipLevel(SkBitmap* dst, SkFixed sx, SkFixed sy) {
reed@android.com83f7bc32009-07-17 02:42:41 +00001277 if (NULL == fMipMap) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001278 return 0;
reed@android.com83f7bc32009-07-17 02:42:41 +00001279 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001280
reed@android.com8a1c16f2008-12-17 15:59:43 +00001281 int level = ComputeMipLevel(sx, sy) >> 16;
1282 SkASSERT(level >= 0);
1283 if (level <= 0) {
1284 return 0;
1285 }
1286
1287 if (level >= fMipMap->fLevelCount) {
1288 level = fMipMap->fLevelCount - 1;
1289 }
1290 if (dst) {
1291 const MipLevel& mip = fMipMap->levels()[level - 1];
1292 dst->setConfig((SkBitmap::Config)this->config(),
1293 mip.fWidth, mip.fHeight, mip.fRowBytes);
1294 dst->setPixels(mip.fPixels);
1295 }
1296 return level;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001297}
1298
1299SkFixed SkBitmap::ComputeMipLevel(SkFixed sx, SkFixed sy) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001300 sx = SkAbs32(sx);
1301 sy = SkAbs32(sy);
1302 if (sx < sy) {
1303 sx = sy;
1304 }
1305 if (sx < SK_Fixed1) {
1306 return 0;
1307 }
1308 int clz = SkCLZ(sx);
1309 SkASSERT(clz >= 1 && clz <= 15);
1310 return SkIntToFixed(15 - clz) + ((unsigned)(sx << (clz + 1)) >> 16);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001311}
1312
1313///////////////////////////////////////////////////////////////////////////////
1314
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +00001315static bool GetBitmapAlpha(const SkBitmap& src, uint8_t* SK_RESTRICT alpha,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001316 int alphaRowBytes) {
1317 SkASSERT(alpha != NULL);
1318 SkASSERT(alphaRowBytes >= src.width());
1319
1320 SkBitmap::Config config = src.getConfig();
1321 int w = src.width();
1322 int h = src.height();
1323 int rb = src.rowBytes();
1324
reed@android.com1cdcb512009-08-24 19:11:00 +00001325 SkAutoLockPixels alp(src);
1326 if (!src.readyToDraw()) {
1327 // zero out the alpha buffer and return
1328 while (--h >= 0) {
1329 memset(alpha, 0, w);
1330 alpha += alphaRowBytes;
1331 }
1332 return false;
1333 }
reed@google.com82065d62011-02-07 15:30:46 +00001334
reed@android.com8a1c16f2008-12-17 15:59:43 +00001335 if (SkBitmap::kA8_Config == config && !src.isOpaque()) {
1336 const uint8_t* s = src.getAddr8(0, 0);
1337 while (--h >= 0) {
1338 memcpy(alpha, s, w);
1339 s += rb;
1340 alpha += alphaRowBytes;
1341 }
1342 } else if (SkBitmap::kARGB_8888_Config == config && !src.isOpaque()) {
1343 const SkPMColor* SK_RESTRICT s = src.getAddr32(0, 0);
1344 while (--h >= 0) {
1345 for (int x = 0; x < w; x++) {
1346 alpha[x] = SkGetPackedA32(s[x]);
1347 }
1348 s = (const SkPMColor*)((const char*)s + rb);
1349 alpha += alphaRowBytes;
1350 }
1351 } else if (SkBitmap::kARGB_4444_Config == config && !src.isOpaque()) {
1352 const SkPMColor16* SK_RESTRICT s = src.getAddr16(0, 0);
1353 while (--h >= 0) {
1354 for (int x = 0; x < w; x++) {
1355 alpha[x] = SkPacked4444ToA32(s[x]);
1356 }
1357 s = (const SkPMColor16*)((const char*)s + rb);
1358 alpha += alphaRowBytes;
1359 }
1360 } else if (SkBitmap::kIndex8_Config == config && !src.isOpaque()) {
1361 SkColorTable* ct = src.getColorTable();
1362 if (ct) {
1363 const SkPMColor* SK_RESTRICT table = ct->lockColors();
1364 const uint8_t* SK_RESTRICT s = src.getAddr8(0, 0);
1365 while (--h >= 0) {
1366 for (int x = 0; x < w; x++) {
1367 alpha[x] = SkGetPackedA32(table[s[x]]);
1368 }
1369 s += rb;
1370 alpha += alphaRowBytes;
1371 }
1372 ct->unlockColors(false);
1373 }
1374 } else { // src is opaque, so just fill alpha[] with 0xFF
1375 memset(alpha, 0xFF, h * alphaRowBytes);
1376 }
reed@android.com1cdcb512009-08-24 19:11:00 +00001377 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001378}
1379
1380#include "SkPaint.h"
1381#include "SkMaskFilter.h"
1382#include "SkMatrix.h"
1383
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001384bool SkBitmap::extractAlpha(SkBitmap* dst, const SkPaint* paint,
djsollen@google.com57f49692011-02-23 20:46:31 +00001385 Allocator *allocator, SkIPoint* offset) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001386 SkDEBUGCODE(this->validate();)
1387
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001388 SkBitmap tmpBitmap;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001389 SkMatrix identity;
1390 SkMask srcM, dstM;
1391
1392 srcM.fBounds.set(0, 0, this->width(), this->height());
1393 srcM.fRowBytes = SkAlign4(this->width());
1394 srcM.fFormat = SkMask::kA8_Format;
1395
1396 SkMaskFilter* filter = paint ? paint->getMaskFilter() : NULL;
1397
1398 // compute our (larger?) dst bounds if we have a filter
1399 if (NULL != filter) {
1400 identity.reset();
1401 srcM.fImage = NULL;
1402 if (!filter->filterMask(&dstM, srcM, identity, NULL)) {
1403 goto NO_FILTER_CASE;
1404 }
1405 dstM.fRowBytes = SkAlign4(dstM.fBounds.width());
1406 } else {
1407 NO_FILTER_CASE:
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001408 tmpBitmap.setConfig(SkBitmap::kA8_Config, this->width(), this->height(),
reed@android.com8a1c16f2008-12-17 15:59:43 +00001409 srcM.fRowBytes);
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001410 if (!tmpBitmap.allocPixels(allocator, NULL)) {
1411 // Allocation of pixels for alpha bitmap failed.
1412 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
1413 tmpBitmap.width(), tmpBitmap.height());
1414 return false;
1415 }
1416 GetBitmapAlpha(*this, tmpBitmap.getAddr8(0, 0), srcM.fRowBytes);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001417 if (offset) {
1418 offset->set(0, 0);
1419 }
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001420 tmpBitmap.swap(*dst);
1421 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001422 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001423 srcM.fImage = SkMask::AllocImage(srcM.computeImageSize());
1424 SkAutoMaskFreeImage srcCleanup(srcM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001425
1426 GetBitmapAlpha(*this, srcM.fImage, srcM.fRowBytes);
1427 if (!filter->filterMask(&dstM, srcM, identity, NULL)) {
1428 goto NO_FILTER_CASE;
1429 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001430 SkAutoMaskFreeImage dstCleanup(dstM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001431
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001432 tmpBitmap.setConfig(SkBitmap::kA8_Config, dstM.fBounds.width(),
reed@android.com8a1c16f2008-12-17 15:59:43 +00001433 dstM.fBounds.height(), dstM.fRowBytes);
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001434 if (!tmpBitmap.allocPixels(allocator, NULL)) {
1435 // Allocation of pixels for alpha bitmap failed.
1436 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
1437 tmpBitmap.width(), tmpBitmap.height());
1438 return false;
1439 }
1440 memcpy(tmpBitmap.getPixels(), dstM.fImage, dstM.computeImageSize());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001441 if (offset) {
1442 offset->set(dstM.fBounds.fLeft, dstM.fBounds.fTop);
1443 }
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001444 SkDEBUGCODE(tmpBitmap.validate();)
1445
1446 tmpBitmap.swap(*dst);
1447 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001448}
1449
1450///////////////////////////////////////////////////////////////////////////////
1451
1452enum {
1453 SERIALIZE_PIXELTYPE_NONE,
djsollen@google.com21830d92012-08-07 19:49:41 +00001454 SERIALIZE_PIXELTYPE_REF_DATA
reed@android.com8a1c16f2008-12-17 15:59:43 +00001455};
1456
reed@android.com8a1c16f2008-12-17 15:59:43 +00001457void SkBitmap::flatten(SkFlattenableWriteBuffer& buffer) const {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001458 buffer.writeInt(fWidth);
1459 buffer.writeInt(fHeight);
1460 buffer.writeInt(fRowBytes);
1461 buffer.writeInt(fConfig);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001462 buffer.writeBool(this->isOpaque());
weita@google.comf9ab99a2009-05-03 18:23:30 +00001463
reed@android.com8a1c16f2008-12-17 15:59:43 +00001464 if (fPixelRef) {
djsollen@google.com5370cd92012-03-28 20:47:01 +00001465 if (fPixelRef->getFactory()) {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001466 buffer.writeInt(SERIALIZE_PIXELTYPE_REF_DATA);
1467 buffer.writeUInt(fPixelRefOffset);
djsollen@google.com5370cd92012-03-28 20:47:01 +00001468 buffer.writeFlattenable(fPixelRef);
1469 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001470 }
1471 // if we get here, we can't record the pixels
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001472 buffer.writeInt(SERIALIZE_PIXELTYPE_NONE);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001473 } else {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001474 buffer.writeInt(SERIALIZE_PIXELTYPE_NONE);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001475 }
1476}
1477
1478void SkBitmap::unflatten(SkFlattenableReadBuffer& buffer) {
1479 this->reset();
weita@google.comf9ab99a2009-05-03 18:23:30 +00001480
reed@android.com8a1c16f2008-12-17 15:59:43 +00001481 int width = buffer.readInt();
1482 int height = buffer.readInt();
1483 int rowBytes = buffer.readInt();
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001484 int config = buffer.readInt();
weita@google.comf9ab99a2009-05-03 18:23:30 +00001485
reed@android.com8a1c16f2008-12-17 15:59:43 +00001486 this->setConfig((Config)config, width, height, rowBytes);
1487 this->setIsOpaque(buffer.readBool());
weita@google.comf9ab99a2009-05-03 18:23:30 +00001488
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001489 int reftype = buffer.readInt();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001490 switch (reftype) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001491 case SERIALIZE_PIXELTYPE_REF_DATA: {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001492 size_t offset = buffer.readUInt();
1493 SkPixelRef* pr = buffer.readFlattenableT<SkPixelRef>();
reed@google.com82065d62011-02-07 15:30:46 +00001494 SkSafeUnref(this->setPixelRef(pr, offset));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001495 break;
1496 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001497 case SERIALIZE_PIXELTYPE_NONE:
1498 break;
1499 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +00001500 SkDEBUGFAIL("unrecognized pixeltype in serialized data");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001501 sk_throw();
1502 }
1503}
1504
1505///////////////////////////////////////////////////////////////////////////////
1506
1507SkBitmap::RLEPixels::RLEPixels(int width, int height) {
1508 fHeight = height;
1509 fYPtrs = (uint8_t**)sk_malloc_throw(height * sizeof(uint8_t*));
reed@android.com4516f472009-06-29 16:25:36 +00001510 sk_bzero(fYPtrs, height * sizeof(uint8_t*));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001511}
1512
1513SkBitmap::RLEPixels::~RLEPixels() {
1514 sk_free(fYPtrs);
1515}
1516
1517///////////////////////////////////////////////////////////////////////////////
1518
1519#ifdef SK_DEBUG
1520void SkBitmap::validate() const {
1521 SkASSERT(fConfig < kConfigCount);
1522 SkASSERT(fRowBytes >= (unsigned)ComputeRowBytes((Config)fConfig, fWidth));
djsollen@google.com21830d92012-08-07 19:49:41 +00001523 SkASSERT(fFlags <= (kImageIsOpaque_Flag | kImageIsVolatile_Flag | kImageIsImmutable_Flag));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001524 SkASSERT(fPixelLockCount >= 0);
1525 SkASSERT(NULL == fColorTable || (unsigned)fColorTable->getRefCnt() < 10000);
1526 SkASSERT((uint8_t)ComputeBytesPerPixel((Config)fConfig) == fBytesPerPixel);
1527
1528#if 0 // these asserts are not thread-correct, so disable for now
1529 if (fPixelRef) {
1530 if (fPixelLockCount > 0) {
reed@google.comff0da4f2012-05-17 13:14:52 +00001531 SkASSERT(fPixelRef->isLocked());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001532 } else {
1533 SkASSERT(NULL == fPixels);
1534 SkASSERT(NULL == fColorTable);
1535 }
1536 }
1537#endif
1538}
1539#endif