blob: a258087a7eaba7e65444ae68746e0bd6e59ae0a4 [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));
scroggo@google.come5f48242013-02-25 21:47:41 +000050 size.add(SkToS32(pixelSize));
reed@android.com149e2f62009-05-22 14:39:03 +000051 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
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000184size_t 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;
scroggo@google.come5f48242013-02-25 21:47:41 +0000223 size.setMul(SkToS32(SkBitmap::ComputeRowBytes(c, width)), height);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000224 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,
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000235 size_t rowBytes) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000236 Sk64 safeSize;
237 safeSize.setZero();
238 if (height > 0) {
scroggo@google.come5f48242013-02-25 21:47:41 +0000239 // TODO: Handle the case where the return value from
240 // ComputeRowBytes is more than 31 bits.
241 safeSize.set(SkToS32(ComputeRowBytes(config, width)));
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000242 Sk64 sizeAllButLastRow;
scroggo@google.come5f48242013-02-25 21:47:41 +0000243 sizeAllButLastRow.setMul(height - 1, SkToS32(rowBytes));
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000244 safeSize.add(sizeAllButLastRow);
245 }
246 SkASSERT(!safeSize.isNeg());
247 return safeSize;
248}
249
250size_t SkBitmap::ComputeSafeSize(Config config,
251 uint32_t width,
252 uint32_t height,
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000253 size_t rowBytes) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000254 Sk64 safeSize = ComputeSafeSize64(config, width, height, rowBytes);
255 return (safeSize.is32() ? safeSize.get32() : 0);
256}
257
reed@google.com86b2e432012-03-15 21:17:03 +0000258void SkBitmap::getBounds(SkRect* bounds) const {
259 SkASSERT(bounds);
260 bounds->set(0, 0,
261 SkIntToScalar(fWidth), SkIntToScalar(fHeight));
262}
263
reed@google.com80e14592012-03-16 14:58:07 +0000264void SkBitmap::getBounds(SkIRect* bounds) const {
265 SkASSERT(bounds);
266 bounds->set(0, 0, fWidth, fHeight);
267}
268
reed@google.com86b2e432012-03-15 21:17:03 +0000269///////////////////////////////////////////////////////////////////////////////
270
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000271void SkBitmap::setConfig(Config c, int width, int height, size_t rowBytes) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000272 this->freePixels();
273
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000274 if ((width | height) < 0) {
agl@chromium.org6b8cb252009-06-01 23:52:37 +0000275 goto err;
reed@android.com149e2f62009-05-22 14:39:03 +0000276 }
277
reed@android.com8a1c16f2008-12-17 15:59:43 +0000278 if (rowBytes == 0) {
279 rowBytes = SkBitmap::ComputeRowBytes(c, width);
reed@android.com149e2f62009-05-22 14:39:03 +0000280 if (0 == rowBytes && kNo_Config != c) {
agl@chromium.org6b8cb252009-06-01 23:52:37 +0000281 goto err;
reed@android.com149e2f62009-05-22 14:39:03 +0000282 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000283 }
reed@android.com89bb83a2009-05-29 21:30:42 +0000284
reed@android.com8a1c16f2008-12-17 15:59:43 +0000285 fConfig = SkToU8(c);
reed@android.comf459a492009-03-27 12:33:50 +0000286 fWidth = width;
287 fHeight = height;
scroggo@google.come5f48242013-02-25 21:47:41 +0000288 fRowBytes = SkToU32(rowBytes);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000289
290 fBytesPerPixel = (uint8_t)ComputeBytesPerPixel(c);
291
292 SkDEBUGCODE(this->validate();)
reed@android.com9e0c2fc2009-06-02 18:54:28 +0000293 return;
agl@chromium.org6b8cb252009-06-01 23:52:37 +0000294
reed@android.com9e0c2fc2009-06-02 18:54:28 +0000295 // if we got here, we had an error, so we reset the bitmap to empty
agl@chromium.org6b8cb252009-06-01 23:52:37 +0000296err:
297 this->reset();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000298}
299
300void SkBitmap::updatePixelsFromRef() const {
301 if (NULL != fPixelRef) {
302 if (fPixelLockCount > 0) {
reed@google.comff0da4f2012-05-17 13:14:52 +0000303 SkASSERT(fPixelRef->isLocked());
weita@google.comf9ab99a2009-05-03 18:23:30 +0000304
reed@android.com8a1c16f2008-12-17 15:59:43 +0000305 void* p = fPixelRef->pixels();
306 if (NULL != p) {
307 p = (char*)p + fPixelRefOffset;
308 }
309 fPixels = p;
310 SkRefCnt_SafeAssign(fColorTable, fPixelRef->colorTable());
311 } else {
312 SkASSERT(0 == fPixelLockCount);
313 fPixels = NULL;
reed@android.com149e2f62009-05-22 14:39:03 +0000314 if (fColorTable) {
315 fColorTable->unref();
316 fColorTable = NULL;
317 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000318 }
319 }
320}
321
322SkPixelRef* SkBitmap::setPixelRef(SkPixelRef* pr, size_t offset) {
323 // do this first, we that we never have a non-zero offset with a null ref
324 if (NULL == pr) {
325 offset = 0;
326 }
327
328 if (fPixelRef != pr || fPixelRefOffset != offset) {
329 if (fPixelRef != pr) {
330 this->freePixels();
331 SkASSERT(NULL == fPixelRef);
weita@google.comf9ab99a2009-05-03 18:23:30 +0000332
reed@google.com82065d62011-02-07 15:30:46 +0000333 SkSafeRef(pr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000334 fPixelRef = pr;
335 }
336 fPixelRefOffset = offset;
337 this->updatePixelsFromRef();
338 }
339
340 SkDEBUGCODE(this->validate();)
341 return pr;
342}
343
344void SkBitmap::lockPixels() const {
345 if (NULL != fPixelRef && 1 == ++fPixelLockCount) {
346 fPixelRef->lockPixels();
347 this->updatePixelsFromRef();
348 }
349 SkDEBUGCODE(this->validate();)
350}
351
352void SkBitmap::unlockPixels() const {
353 SkASSERT(NULL == fPixelRef || fPixelLockCount > 0);
354
355 if (NULL != fPixelRef && 0 == --fPixelLockCount) {
356 fPixelRef->unlockPixels();
357 this->updatePixelsFromRef();
358 }
359 SkDEBUGCODE(this->validate();)
360}
361
reed@google.com9c49bc32011-07-07 13:42:37 +0000362bool SkBitmap::lockPixelsAreWritable() const {
djsollen@google.comc84b8332012-07-27 13:41:44 +0000363 return (fPixelRef) ? fPixelRef->lockPixelsAreWritable() : false;
reed@google.com9c49bc32011-07-07 13:42:37 +0000364}
365
reed@android.com8a1c16f2008-12-17 15:59:43 +0000366void SkBitmap::setPixels(void* p, SkColorTable* ctable) {
reed@google.com8e1034e2012-07-30 13:16:35 +0000367 if (NULL == p) {
368 this->setPixelRef(NULL, 0);
369 return;
370 }
371
djsollen@google.comc84b8332012-07-27 13:41:44 +0000372 Sk64 size = this->getSize64();
373 SkASSERT(!size.isNeg() && size.is32());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000374
djsollen@google.comc84b8332012-07-27 13:41:44 +0000375 this->setPixelRef(new SkMallocPixelRef(p, size.get32(), ctable, false))->unref();
376 // since we're already allocated, we lockPixels right away
377 this->lockPixels();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000378 SkDEBUGCODE(this->validate();)
379}
380
381bool SkBitmap::allocPixels(Allocator* allocator, SkColorTable* ctable) {
382 HeapAllocator stdalloc;
383
384 if (NULL == allocator) {
385 allocator = &stdalloc;
386 }
387 return allocator->allocPixelRef(this, ctable);
388}
389
390void SkBitmap::freePixels() {
391 // if we're gonna free the pixels, we certainly need to free the mipmap
392 this->freeMipMap();
393
reed@android.com149e2f62009-05-22 14:39:03 +0000394 if (fColorTable) {
395 fColorTable->unref();
396 fColorTable = NULL;
397 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000398
399 if (NULL != fPixelRef) {
400 if (fPixelLockCount > 0) {
401 fPixelRef->unlockPixels();
402 }
403 fPixelRef->unref();
404 fPixelRef = NULL;
405 fPixelRefOffset = 0;
406 }
407 fPixelLockCount = 0;
408 fPixels = NULL;
409}
410
411void SkBitmap::freeMipMap() {
reed@android.com149e2f62009-05-22 14:39:03 +0000412 if (fMipMap) {
413 fMipMap->unref();
414 fMipMap = NULL;
415 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000416}
417
418uint32_t SkBitmap::getGenerationID() const {
djsollen@google.comc84b8332012-07-27 13:41:44 +0000419 return (fPixelRef) ? fPixelRef->getGenerationID() : 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000420}
421
422void SkBitmap::notifyPixelsChanged() const {
junov@chromium.orgb0521292011-12-15 20:14:06 +0000423 SkASSERT(!this->isImmutable());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000424 if (fPixelRef) {
425 fPixelRef->notifyPixelsChanged();
426 }
427}
428
reed@android.comce4e53a2010-09-09 16:01:26 +0000429SkGpuTexture* SkBitmap::getTexture() const {
430 return fPixelRef ? fPixelRef->getTexture() : NULL;
431}
432
reed@android.com8a1c16f2008-12-17 15:59:43 +0000433///////////////////////////////////////////////////////////////////////////////
434
reed@android.com8a1c16f2008-12-17 15:59:43 +0000435/** We explicitly use the same allocator for our pixels that SkMask does,
436 so that we can freely assign memory allocated by one class to the other.
437 */
438bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst,
439 SkColorTable* ctable) {
440 Sk64 size = dst->getSize64();
441 if (size.isNeg() || !size.is32()) {
442 return false;
443 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000444
reed@android.com8a1c16f2008-12-17 15:59:43 +0000445 void* addr = sk_malloc_flags(size.get32(), 0); // returns NULL on failure
446 if (NULL == addr) {
447 return false;
448 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000449
reed@android.com8a1c16f2008-12-17 15:59:43 +0000450 dst->setPixelRef(new SkMallocPixelRef(addr, size.get32(), ctable))->unref();
451 // since we're already allocated, we lockPixels right away
452 dst->lockPixels();
453 return true;
454}
455
456///////////////////////////////////////////////////////////////////////////////
457
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000458size_t SkBitmap::getSafeSize() const {
459 // This is intended to be a size_t version of ComputeSafeSize64(), just
460 // faster. The computation is meant to be identical.
461 return (fHeight ? ((fHeight - 1) * fRowBytes) +
462 ComputeRowBytes(getConfig(), fWidth): 0);
463}
464
465Sk64 SkBitmap::getSafeSize64() const {
466 return ComputeSafeSize64(getConfig(), fWidth, fHeight, fRowBytes);
467}
468
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000469bool SkBitmap::copyPixelsTo(void* const dst, size_t dstSize,
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000470 size_t dstRowBytes, bool preserveDstPad) const {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000471
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000472 if (0 == dstRowBytes) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000473 dstRowBytes = fRowBytes;
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000474 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000475
476 if (getConfig() == kRLE_Index8_Config ||
477 dstRowBytes < ComputeRowBytes(getConfig(), fWidth) ||
478 dst == NULL || (getPixels() == NULL && pixelRef() == NULL))
479 return false;
480
bsalomon@google.comc6980972011-11-02 19:57:21 +0000481 if (!preserveDstPad && static_cast<uint32_t>(dstRowBytes) == fRowBytes) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000482 size_t safeSize = getSafeSize();
483 if (safeSize > dstSize || safeSize == 0)
484 return false;
485 else {
486 SkAutoLockPixels lock(*this);
487 // This implementation will write bytes beyond the end of each row,
488 // excluding the last row, if the bitmap's stride is greater than
489 // strictly required by the current config.
490 memcpy(dst, getPixels(), safeSize);
491
492 return true;
493 }
494 } else {
495 // If destination has different stride than us, then copy line by line.
496 if (ComputeSafeSize(getConfig(), fWidth, fHeight, dstRowBytes) >
497 dstSize)
498 return false;
499 else {
500 // Just copy what we need on each line.
scroggo@google.come5f48242013-02-25 21:47:41 +0000501 size_t rowBytes = ComputeRowBytes(getConfig(), fWidth);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000502 SkAutoLockPixels lock(*this);
503 const uint8_t* srcP = reinterpret_cast<const uint8_t*>(getPixels());
504 uint8_t* dstP = reinterpret_cast<uint8_t*>(dst);
505 for (uint32_t row = 0; row < fHeight;
506 row++, srcP += fRowBytes, dstP += dstRowBytes) {
507 memcpy(dstP, srcP, rowBytes);
508 }
509
510 return true;
511 }
512 }
513}
514
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000515///////////////////////////////////////////////////////////////////////////////
516
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000517bool SkBitmap::isImmutable() const {
junov@chromium.orgb0521292011-12-15 20:14:06 +0000518 return fPixelRef ? fPixelRef->isImmutable() :
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000519 fFlags & kImageIsImmutable_Flag;
junov@chromium.orgb0521292011-12-15 20:14:06 +0000520}
521
522void SkBitmap::setImmutable() {
523 if (fPixelRef) {
524 fPixelRef->setImmutable();
525 } else {
526 fFlags |= kImageIsImmutable_Flag;
527 }
528}
529
reed@android.com8a1c16f2008-12-17 15:59:43 +0000530bool SkBitmap::isOpaque() const {
531 switch (fConfig) {
532 case kNo_Config:
533 return true;
534
535 case kA1_Config:
536 case kA8_Config:
537 case kARGB_4444_Config:
538 case kARGB_8888_Config:
539 return (fFlags & kImageIsOpaque_Flag) != 0;
540
541 case kIndex8_Config:
542 case kRLE_Index8_Config: {
543 uint32_t flags = 0;
544
545 this->lockPixels();
546 // if lockPixels failed, we may not have a ctable ptr
547 if (fColorTable) {
548 flags = fColorTable->getFlags();
549 }
550 this->unlockPixels();
551
552 return (flags & SkColorTable::kColorsAreOpaque_Flag) != 0;
553 }
554
555 case kRGB_565_Config:
556 return true;
557
558 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000559 SkDEBUGFAIL("unknown bitmap config pased to isOpaque");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000560 return false;
561 }
562}
563
564void SkBitmap::setIsOpaque(bool isOpaque) {
565 /* we record this regardless of fConfig, though it is ignored in
566 isOpaque() for configs that can't support per-pixel alpha.
567 */
568 if (isOpaque) {
569 fFlags |= kImageIsOpaque_Flag;
570 } else {
571 fFlags &= ~kImageIsOpaque_Flag;
572 }
573}
574
junov@google.com4ee7ae52011-06-30 17:30:49 +0000575bool SkBitmap::isVolatile() const {
576 return (fFlags & kImageIsVolatile_Flag) != 0;
577}
578
579void SkBitmap::setIsVolatile(bool isVolatile) {
580 if (isVolatile) {
581 fFlags |= kImageIsVolatile_Flag;
582 } else {
583 fFlags &= ~kImageIsVolatile_Flag;
584 }
585}
586
reed@android.com8a1c16f2008-12-17 15:59:43 +0000587void* SkBitmap::getAddr(int x, int y) const {
588 SkASSERT((unsigned)x < (unsigned)this->width());
589 SkASSERT((unsigned)y < (unsigned)this->height());
590
591 char* base = (char*)this->getPixels();
592 if (base) {
593 base += y * this->rowBytes();
594 switch (this->config()) {
595 case SkBitmap::kARGB_8888_Config:
596 base += x << 2;
597 break;
598 case SkBitmap::kARGB_4444_Config:
599 case SkBitmap::kRGB_565_Config:
600 base += x << 1;
601 break;
602 case SkBitmap::kA8_Config:
603 case SkBitmap::kIndex8_Config:
604 base += x;
605 break;
606 case SkBitmap::kA1_Config:
607 base += x >> 3;
608 break;
609 case kRLE_Index8_Config:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000610 SkDEBUGFAIL("Can't return addr for kRLE_Index8_Config");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000611 base = NULL;
612 break;
613 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000614 SkDEBUGFAIL("Can't return addr for config");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000615 base = NULL;
616 break;
617 }
618 }
619 return base;
620}
621
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000622SkColor SkBitmap::getColor(int x, int y) const {
623 SkASSERT((unsigned)x < (unsigned)this->width());
624 SkASSERT((unsigned)y < (unsigned)this->height());
625
626 switch (this->config()) {
627 case SkBitmap::kA1_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000628 uint8_t* addr = this->getAddr1(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000629 uint8_t mask = 1 << (7 - (x % 8));
630 if (addr[0] & mask) {
631 return SK_ColorBLACK;
632 } else {
633 return 0;
634 }
635 }
636 case SkBitmap::kA8_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000637 uint8_t* addr = this->getAddr8(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000638 return SkColorSetA(0, addr[0]);
639 }
640 case SkBitmap::kIndex8_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000641 SkPMColor c = this->getIndex8Color(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000642 return SkUnPreMultiply::PMColorToColor(c);
643 }
644 case SkBitmap::kRGB_565_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000645 uint16_t* addr = this->getAddr16(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000646 return SkPixel16ToColor(addr[0]);
647 }
648 case SkBitmap::kARGB_4444_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000649 uint16_t* addr = this->getAddr16(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000650 SkPMColor c = SkPixel4444ToPixel32(addr[0]);
651 return SkUnPreMultiply::PMColorToColor(c);
652 }
653 case SkBitmap::kARGB_8888_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000654 uint32_t* addr = this->getAddr32(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000655 return SkUnPreMultiply::PMColorToColor(addr[0]);
656 }
657 case kRLE_Index8_Config: {
658 uint8_t dst;
659 const SkBitmap::RLEPixels* rle =
reed@google.com3b521d02011-04-29 11:53:41 +0000660 (const SkBitmap::RLEPixels*)this->getPixels();
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000661 SkPackBits::Unpack8(&dst, x, 1, rle->packedAtY(y));
662 return SkUnPreMultiply::PMColorToColor((*fColorTable)[dst]);
663 }
664 case kNo_Config:
665 case kConfigCount:
666 SkASSERT(false);
667 return 0;
668 }
669 SkASSERT(false); // Not reached.
670 return 0;
671}
672
reed@google.com2a7579d2012-11-07 18:30:18 +0000673bool SkBitmap::ComputeIsOpaque(const SkBitmap& bm) {
674 SkAutoLockPixels alp(bm);
675 if (!bm.getPixels()) {
676 return false;
677 }
678
679 const int height = bm.height();
680 const int width = bm.width();
681
682 switch (bm.config()) {
683 case SkBitmap::kA1_Config: {
684 // TODO
685 } break;
686 case SkBitmap::kA8_Config: {
687 unsigned a = 0xFF;
688 for (int y = 0; y < height; ++y) {
689 const uint8_t* row = bm.getAddr8(0, y);
690 for (int x = 0; x < width; ++x) {
691 a &= row[x];
692 }
693 if (0xFF != a) {
694 return false;
695 }
696 }
697 return true;
698 } break;
699 case kRLE_Index8_Config:
700 case SkBitmap::kIndex8_Config: {
701 SkAutoLockColors alc(bm);
702 const SkPMColor* table = alc.colors();
703 if (!table) {
704 return false;
705 }
reed@google.com140d7282013-01-07 20:25:04 +0000706 SkPMColor c = (SkPMColor)~0;
reed@google.com2a7579d2012-11-07 18:30:18 +0000707 for (int i = bm.getColorTable()->count() - 1; i >= 0; --i) {
708 c &= table[i];
709 }
710 return 0xFF == SkGetPackedA32(c);
711 } break;
712 case SkBitmap::kRGB_565_Config:
713 return true;
714 break;
715 case SkBitmap::kARGB_4444_Config: {
716 unsigned c = 0xFFFF;
717 for (int y = 0; y < height; ++y) {
718 const SkPMColor16* row = bm.getAddr16(0, y);
719 for (int x = 0; x < width; ++x) {
720 c &= row[x];
721 }
722 if (0xF != SkGetPackedA4444(c)) {
723 return false;
724 }
725 }
726 return true;
727 } break;
728 case SkBitmap::kARGB_8888_Config: {
reed@google.com140d7282013-01-07 20:25:04 +0000729 SkPMColor c = (SkPMColor)~0;
reed@google.com2a7579d2012-11-07 18:30:18 +0000730 for (int y = 0; y < height; ++y) {
731 const SkPMColor* row = bm.getAddr32(0, y);
732 for (int x = 0; x < width; ++x) {
733 c &= row[x];
734 }
735 if (0xFF != SkGetPackedA32(c)) {
736 return false;
737 }
738 }
739 return true;
740 }
741 default:
742 break;
743 }
744 return false;
745}
746
747
reed@android.com8a1c16f2008-12-17 15:59:43 +0000748///////////////////////////////////////////////////////////////////////////////
749///////////////////////////////////////////////////////////////////////////////
750
751void SkBitmap::eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const {
752 SkDEBUGCODE(this->validate();)
753
754 if (0 == fWidth || 0 == fHeight ||
755 kNo_Config == fConfig || kIndex8_Config == fConfig) {
756 return;
757 }
758
759 SkAutoLockPixels alp(*this);
760 // perform this check after the lock call
761 if (!this->readyToDraw()) {
762 return;
763 }
764
765 int height = fHeight;
766 const int width = fWidth;
767 const int rowBytes = fRowBytes;
768
769 // make rgb premultiplied
770 if (255 != a) {
771 r = SkAlphaMul(r, a);
772 g = SkAlphaMul(g, a);
773 b = SkAlphaMul(b, a);
774 }
775
776 switch (fConfig) {
777 case kA1_Config: {
778 uint8_t* p = (uint8_t*)fPixels;
779 const int count = (width + 7) >> 3;
780 a = (a >> 7) ? 0xFF : 0;
781 SkASSERT(count <= rowBytes);
782 while (--height >= 0) {
783 memset(p, a, count);
784 p += rowBytes;
785 }
786 break;
787 }
788 case kA8_Config: {
789 uint8_t* p = (uint8_t*)fPixels;
790 while (--height >= 0) {
791 memset(p, a, width);
792 p += rowBytes;
793 }
794 break;
795 }
796 case kARGB_4444_Config:
797 case kRGB_565_Config: {
798 uint16_t* p = (uint16_t*)fPixels;
799 uint16_t v;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000800
reed@android.com8a1c16f2008-12-17 15:59:43 +0000801 if (kARGB_4444_Config == fConfig) {
802 v = SkPackARGB4444(a >> 4, r >> 4, g >> 4, b >> 4);
803 } else { // kRGB_565_Config
804 v = SkPackRGB16(r >> (8 - SK_R16_BITS), g >> (8 - SK_G16_BITS),
805 b >> (8 - SK_B16_BITS));
806 }
807 while (--height >= 0) {
808 sk_memset16(p, v, width);
809 p = (uint16_t*)((char*)p + rowBytes);
810 }
811 break;
812 }
813 case kARGB_8888_Config: {
814 uint32_t* p = (uint32_t*)fPixels;
815 uint32_t v = SkPackARGB32(a, r, g, b);
816
817 while (--height >= 0) {
818 sk_memset32(p, v, width);
819 p = (uint32_t*)((char*)p + rowBytes);
820 }
821 break;
822 }
823 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000824
reed@android.com8a1c16f2008-12-17 15:59:43 +0000825 this->notifyPixelsChanged();
826}
827
828//////////////////////////////////////////////////////////////////////////////////////
829//////////////////////////////////////////////////////////////////////////////////////
830
831#define SUB_OFFSET_FAILURE ((size_t)-1)
832
scroggo@google.coma2a31922012-12-07 19:14:45 +0000833/**
834 * Based on the Config and rowBytes() of bm, return the offset into an SkPixelRef of the pixel at
835 * (x, y).
836 * Note that the SkPixelRef does not need to be set yet. deepCopyTo takes advantage of this fact.
837 * Also note that (x, y) may be outside the range of (0 - width(), 0 - height()), so long as it is
838 * within the bounds of the SkPixelRef being used.
839 */
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000840static size_t get_sub_offset(const SkBitmap& bm, int x, int y) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000841 switch (bm.getConfig()) {
842 case SkBitmap::kA8_Config:
843 case SkBitmap:: kIndex8_Config:
844 // x is fine as is for the calculation
845 break;
846
847 case SkBitmap::kRGB_565_Config:
848 case SkBitmap::kARGB_4444_Config:
849 x <<= 1;
850 break;
851
852 case SkBitmap::kARGB_8888_Config:
853 x <<= 2;
854 break;
855
856 case SkBitmap::kNo_Config:
857 case SkBitmap::kA1_Config:
858 default:
859 return SUB_OFFSET_FAILURE;
860 }
861 return y * bm.rowBytes() + x;
862}
863
scroggo@google.coma2a31922012-12-07 19:14:45 +0000864/**
865 * Using the pixelRefOffset(), rowBytes(), and Config of bm, determine the (x, y) coordinate of the
866 * upper left corner of bm relative to its SkPixelRef.
867 * x and y must be non-NULL.
868 */
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000869bool get_upper_left_from_offset(SkBitmap::Config config, size_t offset, size_t rowBytes,
870 int32_t* x, int32_t* y) {
scroggo@google.coma2a31922012-12-07 19:14:45 +0000871 SkASSERT(x != NULL && y != NULL);
scroggo@google.coma2a31922012-12-07 19:14:45 +0000872 if (0 == offset) {
873 *x = *y = 0;
874 return true;
875 }
876 // Use integer division to find the correct y position.
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000877 *y = SkToS32(offset / rowBytes);
878 // The remainder will be the x position, after we reverse get_sub_offset.
879 *x = SkToS32(offset % rowBytes);
880 switch (config) {
scroggo@google.coma2a31922012-12-07 19:14:45 +0000881 case SkBitmap::kA8_Config:
882 // Fall through.
883 case SkBitmap::kIndex8_Config:
884 // x is unmodified
885 break;
886
887 case SkBitmap::kRGB_565_Config:
888 // Fall through.
889 case SkBitmap::kARGB_4444_Config:
890 *x >>= 1;
891 break;
892
893 case SkBitmap::kARGB_8888_Config:
894 *x >>= 2;
895 break;
896
897 case SkBitmap::kNo_Config:
898 // Fall through.
899 case SkBitmap::kA1_Config:
900 // Fall through.
901 default:
902 return false;
903 }
904 return true;
905}
906
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000907static bool get_upper_left_from_offset(const SkBitmap& bm, int32_t* x, int32_t* y) {
908 return get_upper_left_from_offset(bm.config(), bm.pixelRefOffset(), bm.rowBytes(), x, y);
909}
910
reed@android.com8a1c16f2008-12-17 15:59:43 +0000911bool SkBitmap::extractSubset(SkBitmap* result, const SkIRect& subset) const {
912 SkDEBUGCODE(this->validate();)
913
djsollen@google.comc84b8332012-07-27 13:41:44 +0000914 if (NULL == result || NULL == fPixelRef) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000915 return false; // no src pixels
916 }
917
918 SkIRect srcRect, r;
919 srcRect.set(0, 0, this->width(), this->height());
920 if (!r.intersect(srcRect, subset)) {
921 return false; // r is empty (i.e. no intersection)
922 }
923
scroggo@google.coma2a31922012-12-07 19:14:45 +0000924 if (fPixelRef->getTexture() != NULL) {
925 // Do a deep copy
926 SkPixelRef* pixelRef = fPixelRef->deepCopy(this->config(), &subset);
927 if (pixelRef != NULL) {
928 SkBitmap dst;
929 dst.setConfig(this->config(), subset.width(), subset.height());
930 dst.setIsVolatile(this->isVolatile());
931 dst.setIsOpaque(this->isOpaque());
932 dst.setPixelRef(pixelRef)->unref();
933 SkDEBUGCODE(dst.validate());
934 result->swap(dst);
935 return true;
936 }
937 }
938
reed@android.com8a1c16f2008-12-17 15:59:43 +0000939 if (kRLE_Index8_Config == fConfig) {
940 SkAutoLockPixels alp(*this);
941 // don't call readyToDraw(), since we can operate w/o a colortable
942 // at this stage
943 if (this->getPixels() == NULL) {
944 return false;
945 }
946 SkBitmap bm;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000947
reed@android.com8a1c16f2008-12-17 15:59:43 +0000948 bm.setConfig(kIndex8_Config, r.width(), r.height());
949 bm.allocPixels(this->getColorTable());
950 if (NULL == bm.getPixels()) {
951 return false;
952 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000953
reed@android.com8a1c16f2008-12-17 15:59:43 +0000954 const RLEPixels* rle = (const RLEPixels*)this->getPixels();
955 uint8_t* dst = bm.getAddr8(0, 0);
956 const int width = bm.width();
scroggo@google.come5f48242013-02-25 21:47:41 +0000957 const size_t rowBytes = bm.rowBytes();
weita@google.comf9ab99a2009-05-03 18:23:30 +0000958
reed@android.com8a1c16f2008-12-17 15:59:43 +0000959 for (int y = r.fTop; y < r.fBottom; y++) {
960 SkPackBits::Unpack8(dst, r.fLeft, width, rle->packedAtY(y));
961 dst += rowBytes;
962 }
963 result->swap(bm);
964 return true;
965 }
966
scroggo@google.coma2a31922012-12-07 19:14:45 +0000967 // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have
968 // exited above.
969 SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width()));
970 SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height()));
971
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000972 size_t offset = get_sub_offset(*this, r.fLeft, r.fTop);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000973 if (SUB_OFFSET_FAILURE == offset) {
974 return false; // config not supported
975 }
976
977 SkBitmap dst;
978 dst.setConfig(this->config(), r.width(), r.height(), this->rowBytes());
skyostil@google.com0eb75762012-01-16 10:45:53 +0000979 dst.setIsVolatile(this->isVolatile());
reed@google.com04685d22012-10-15 13:45:40 +0000980 dst.setIsOpaque(this->isOpaque());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000981
982 if (fPixelRef) {
983 // share the pixelref with a custom offset
984 dst.setPixelRef(fPixelRef, fPixelRefOffset + offset);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000985 }
986 SkDEBUGCODE(dst.validate();)
987
988 // we know we're good, so commit to result
989 result->swap(dst);
990 return true;
991}
992
993///////////////////////////////////////////////////////////////////////////////
994
995#include "SkCanvas.h"
996#include "SkPaint.h"
997
reed@android.comfbaa88d2009-05-06 17:44:34 +0000998bool SkBitmap::canCopyTo(Config dstConfig) const {
999 if (this->getConfig() == kNo_Config) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001000 return false;
1001 }
1002
reed@android.comfbaa88d2009-05-06 17:44:34 +00001003 bool sameConfigs = (this->config() == dstConfig);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001004 switch (dstConfig) {
1005 case kA8_Config:
1006 case kARGB_4444_Config:
1007 case kRGB_565_Config:
1008 case kARGB_8888_Config:
1009 break;
weita@google.comf9ab99a2009-05-03 18:23:30 +00001010 case kA1_Config:
1011 case kIndex8_Config:
1012 if (!sameConfigs) {
1013 return false;
1014 }
1015 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001016 default:
1017 return false;
1018 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001019
1020 // do not copy src if srcConfig == kA1_Config while dstConfig != kA1_Config
1021 if (this->getConfig() == kA1_Config && !sameConfigs) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001022 return false;
1023 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001024
reed@android.comfbaa88d2009-05-06 17:44:34 +00001025 return true;
1026}
1027
1028bool SkBitmap::copyTo(SkBitmap* dst, Config dstConfig, Allocator* alloc) const {
1029 if (!this->canCopyTo(dstConfig)) {
1030 return false;
1031 }
1032
reed@google.com50dfa012011-04-01 19:05:36 +00001033 // if we have a texture, first get those pixels
1034 SkBitmap tmpSrc;
1035 const SkBitmap* src = this;
1036
scroggo@google.coma2a31922012-12-07 19:14:45 +00001037 if (fPixelRef) {
1038 SkIRect subset;
scroggo@google.com1b1bcc32013-05-21 20:31:23 +00001039 if (get_upper_left_from_offset(*this, &subset.fLeft, &subset.fTop)) {
scroggo@google.coma2a31922012-12-07 19:14:45 +00001040 subset.fRight = subset.fLeft + fWidth;
1041 subset.fBottom = subset.fTop + fHeight;
1042 if (fPixelRef->readPixels(&tmpSrc, &subset)) {
1043 SkASSERT(tmpSrc.width() == this->width());
1044 SkASSERT(tmpSrc.height() == this->height());
reed@google.com50dfa012011-04-01 19:05:36 +00001045
scroggo@google.coma2a31922012-12-07 19:14:45 +00001046 // did we get lucky and we can just return tmpSrc?
1047 if (tmpSrc.config() == dstConfig && NULL == alloc) {
1048 dst->swap(tmpSrc);
1049 if (dst->pixelRef() && this->config() == dstConfig) {
1050 dst->pixelRef()->fGenerationID = fPixelRef->getGenerationID();
1051 }
1052 return true;
1053 }
1054
1055 // fall through to the raster case
1056 src = &tmpSrc;
scroggo@google.comd5764e82012-08-22 15:00:05 +00001057 }
reed@google.com50dfa012011-04-01 19:05:36 +00001058 }
reed@android.comfbaa88d2009-05-06 17:44:34 +00001059 }
reed@android.com311c82d2009-05-05 23:13:23 +00001060
reed@google.com50dfa012011-04-01 19:05:36 +00001061 // we lock this now, since we may need its colortable
1062 SkAutoLockPixels srclock(*src);
1063 if (!src->readyToDraw()) {
1064 return false;
1065 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001066
reed@google.com50dfa012011-04-01 19:05:36 +00001067 SkBitmap tmpDst;
1068 tmpDst.setConfig(dstConfig, src->width(), src->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001069
weita@google.comf9ab99a2009-05-03 18:23:30 +00001070 // allocate colortable if srcConfig == kIndex8_Config
1071 SkColorTable* ctable = (dstConfig == kIndex8_Config) ?
reed@google.com50dfa012011-04-01 19:05:36 +00001072 new SkColorTable(*src->getColorTable()) : NULL;
weita@google.comf9ab99a2009-05-03 18:23:30 +00001073 SkAutoUnref au(ctable);
reed@google.com50dfa012011-04-01 19:05:36 +00001074 if (!tmpDst.allocPixels(alloc, ctable)) {
weita@google.comf9ab99a2009-05-03 18:23:30 +00001075 return false;
1076 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001077
reed@google.com50dfa012011-04-01 19:05:36 +00001078 if (!tmpDst.readyToDraw()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001079 // allocator/lock failed
1080 return false;
1081 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001082
reed@android.comfbaa88d2009-05-06 17:44:34 +00001083 /* do memcpy for the same configs cases, else use drawing
weita@google.comf9ab99a2009-05-03 18:23:30 +00001084 */
reed@google.com50dfa012011-04-01 19:05:36 +00001085 if (src->config() == dstConfig) {
1086 if (tmpDst.getSize() == src->getSize()) {
1087 memcpy(tmpDst.getPixels(), src->getPixels(), src->getSafeSize());
scroggo@google.comd5764e82012-08-22 15:00:05 +00001088 SkPixelRef* pixelRef = tmpDst.pixelRef();
1089 if (pixelRef != NULL) {
1090 pixelRef->fGenerationID = this->getGenerationID();
1091 }
reed@android.com311c82d2009-05-05 23:13:23 +00001092 } else {
reed@google.com50dfa012011-04-01 19:05:36 +00001093 const char* srcP = reinterpret_cast<const char*>(src->getPixels());
1094 char* dstP = reinterpret_cast<char*>(tmpDst.getPixels());
reed@android.com311c82d2009-05-05 23:13:23 +00001095 // to be sure we don't read too much, only copy our logical pixels
reed@google.com50dfa012011-04-01 19:05:36 +00001096 size_t bytesToCopy = tmpDst.width() * tmpDst.bytesPerPixel();
1097 for (int y = 0; y < tmpDst.height(); y++) {
reed@android.com311c82d2009-05-05 23:13:23 +00001098 memcpy(dstP, srcP, bytesToCopy);
reed@google.com50dfa012011-04-01 19:05:36 +00001099 srcP += src->rowBytes();
1100 dstP += tmpDst.rowBytes();
reed@android.com311c82d2009-05-05 23:13:23 +00001101 }
1102 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001103 } else {
1104 // if the src has alpha, we have to clear the dst first
reed@google.com50dfa012011-04-01 19:05:36 +00001105 if (!src->isOpaque()) {
junov@google.comdbfac8a2012-12-06 21:47:40 +00001106 tmpDst.eraseColor(SK_ColorTRANSPARENT);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001107 }
1108
reed@google.com50dfa012011-04-01 19:05:36 +00001109 SkCanvas canvas(tmpDst);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001110 SkPaint paint;
1111
1112 paint.setDither(true);
reed@google.com50dfa012011-04-01 19:05:36 +00001113 canvas.drawBitmap(*src, 0, 0, &paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001114 }
1115
reed@google.com50dfa012011-04-01 19:05:36 +00001116 tmpDst.setIsOpaque(src->isOpaque());
reed@android.comcafc9f92009-08-22 03:44:57 +00001117
reed@google.com50dfa012011-04-01 19:05:36 +00001118 dst->swap(tmpDst);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001119 return true;
1120}
1121
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001122bool SkBitmap::deepCopyTo(SkBitmap* dst, Config dstConfig) const {
1123 if (!this->canCopyTo(dstConfig)) {
1124 return false;
1125 }
1126
1127 // If we have a PixelRef, and it supports deep copy, use it.
1128 // Currently supported only by texture-backed bitmaps.
1129 if (fPixelRef) {
1130 SkPixelRef* pixelRef = fPixelRef->deepCopy(dstConfig);
1131 if (pixelRef) {
scroggo@google.coma2a31922012-12-07 19:14:45 +00001132 uint32_t rowBytes;
scroggo@google.comd5764e82012-08-22 15:00:05 +00001133 if (dstConfig == fConfig) {
1134 pixelRef->fGenerationID = fPixelRef->getGenerationID();
scroggo@google.coma2a31922012-12-07 19:14:45 +00001135 // Use the same rowBytes as the original.
1136 rowBytes = fRowBytes;
1137 } else {
1138 // With the new config, an appropriate fRowBytes will be computed by setConfig.
1139 rowBytes = 0;
scroggo@google.comd5764e82012-08-22 15:00:05 +00001140 }
scroggo@google.coma2a31922012-12-07 19:14:45 +00001141 dst->setConfig(dstConfig, fWidth, fHeight, rowBytes);
1142
1143 size_t pixelRefOffset;
1144 if (0 == fPixelRefOffset || dstConfig == fConfig) {
1145 // Use the same offset as the original.
1146 pixelRefOffset = fPixelRefOffset;
1147 } else {
1148 // Find the correct offset in the new config. This needs to be done after calling
1149 // setConfig so dst's fConfig and fRowBytes have been set properly.
scroggo@google.come5f48242013-02-25 21:47:41 +00001150 int32_t x, y;
scroggo@google.com1b1bcc32013-05-21 20:31:23 +00001151 if (!get_upper_left_from_offset(*this, &x, &y)) {
scroggo@google.coma2a31922012-12-07 19:14:45 +00001152 return false;
1153 }
scroggo@google.com1b1bcc32013-05-21 20:31:23 +00001154 pixelRefOffset = get_sub_offset(*dst, x, y);
scroggo@google.coma2a31922012-12-07 19:14:45 +00001155 if (SUB_OFFSET_FAILURE == pixelRefOffset) {
1156 return false;
1157 }
1158 }
1159 dst->setPixelRef(pixelRef, pixelRefOffset)->unref();
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001160 return true;
1161 }
1162 }
1163
1164 if (this->getTexture()) {
1165 return false;
1166 } else {
1167 return this->copyTo(dst, dstConfig, NULL);
1168 }
1169}
1170
reed@android.com8a1c16f2008-12-17 15:59:43 +00001171///////////////////////////////////////////////////////////////////////////////
1172///////////////////////////////////////////////////////////////////////////////
1173
1174static void downsampleby2_proc32(SkBitmap* dst, int x, int y,
1175 const SkBitmap& src) {
1176 x <<= 1;
1177 y <<= 1;
1178 const SkPMColor* p = src.getAddr32(x, y);
reed@android.com829c83c2009-06-08 12:05:31 +00001179 const SkPMColor* baseP = p;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001180 SkPMColor c, ag, rb;
1181
1182 c = *p; ag = (c >> 8) & 0xFF00FF; rb = c & 0xFF00FF;
1183 if (x < src.width() - 1) {
1184 p += 1;
1185 }
1186 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
1187
reed@android.com829c83c2009-06-08 12:05:31 +00001188 p = baseP;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001189 if (y < src.height() - 1) {
reed@android.com829c83c2009-06-08 12:05:31 +00001190 p += src.rowBytes() >> 2;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001191 }
1192 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
1193 if (x < src.width() - 1) {
1194 p += 1;
1195 }
1196 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
1197
1198 *dst->getAddr32(x >> 1, y >> 1) =
1199 ((rb >> 2) & 0xFF00FF) | ((ag << 6) & 0xFF00FF00);
1200}
1201
1202static inline uint32_t expand16(U16CPU c) {
1203 return (c & ~SK_G16_MASK_IN_PLACE) | ((c & SK_G16_MASK_IN_PLACE) << 16);
1204}
1205
1206// returns dirt in the top 16bits, but we don't care, since we only
1207// store the low 16bits.
1208static inline U16CPU pack16(uint32_t c) {
1209 return (c & ~SK_G16_MASK_IN_PLACE) | ((c >> 16) & SK_G16_MASK_IN_PLACE);
1210}
1211
1212static void downsampleby2_proc16(SkBitmap* dst, int x, int y,
1213 const SkBitmap& src) {
1214 x <<= 1;
1215 y <<= 1;
1216 const uint16_t* p = src.getAddr16(x, y);
reed@android.com829c83c2009-06-08 12:05:31 +00001217 const uint16_t* baseP = p;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001218 SkPMColor c;
weita@google.comf9ab99a2009-05-03 18:23:30 +00001219
reed@android.com8a1c16f2008-12-17 15:59:43 +00001220 c = expand16(*p);
reed@android.com829c83c2009-06-08 12:05:31 +00001221 if (x < src.width() - 1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001222 p += 1;
1223 }
1224 c += expand16(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001225
reed@android.com829c83c2009-06-08 12:05:31 +00001226 p = baseP;
1227 if (y < src.height() - 1) {
1228 p += src.rowBytes() >> 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001229 }
1230 c += expand16(*p);
reed@android.com829c83c2009-06-08 12:05:31 +00001231 if (x < src.width() - 1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001232 p += 1;
1233 }
1234 c += expand16(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001235
reed@android.com8a1c16f2008-12-17 15:59:43 +00001236 *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)pack16(c >> 2);
1237}
1238
1239static uint32_t expand4444(U16CPU c) {
1240 return (c & 0xF0F) | ((c & ~0xF0F) << 12);
1241}
1242
1243static U16CPU collaps4444(uint32_t c) {
1244 return (c & 0xF0F) | ((c >> 12) & ~0xF0F);
1245}
1246
1247static void downsampleby2_proc4444(SkBitmap* dst, int x, int y,
1248 const SkBitmap& src) {
1249 x <<= 1;
1250 y <<= 1;
1251 const uint16_t* p = src.getAddr16(x, y);
reed@android.com829c83c2009-06-08 12:05:31 +00001252 const uint16_t* baseP = p;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001253 uint32_t c;
weita@google.comf9ab99a2009-05-03 18:23:30 +00001254
reed@android.com8a1c16f2008-12-17 15:59:43 +00001255 c = expand4444(*p);
1256 if (x < src.width() - 1) {
1257 p += 1;
1258 }
1259 c += expand4444(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001260
reed@android.com829c83c2009-06-08 12:05:31 +00001261 p = baseP;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001262 if (y < src.height() - 1) {
reed@android.com829c83c2009-06-08 12:05:31 +00001263 p += src.rowBytes() >> 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001264 }
1265 c += expand4444(*p);
1266 if (x < src.width() - 1) {
1267 p += 1;
1268 }
1269 c += expand4444(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001270
reed@android.com8a1c16f2008-12-17 15:59:43 +00001271 *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)collaps4444(c >> 2);
1272}
1273
1274void SkBitmap::buildMipMap(bool forceRebuild) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001275 if (forceRebuild)
1276 this->freeMipMap();
1277 else if (fMipMap)
1278 return; // we're already built
1279
1280 SkASSERT(NULL == fMipMap);
1281
1282 void (*proc)(SkBitmap* dst, int x, int y, const SkBitmap& src);
1283
1284 const SkBitmap::Config config = this->getConfig();
1285
1286 switch (config) {
1287 case kARGB_8888_Config:
1288 proc = downsampleby2_proc32;
1289 break;
1290 case kRGB_565_Config:
1291 proc = downsampleby2_proc16;
1292 break;
1293 case kARGB_4444_Config:
1294 proc = downsampleby2_proc4444;
1295 break;
1296 case kIndex8_Config:
1297 case kA8_Config:
1298 default:
1299 return; // don't build mipmaps for these configs
1300 }
reed@android.com89bb83a2009-05-29 21:30:42 +00001301
reed@android.com149e2f62009-05-22 14:39:03 +00001302 SkAutoLockPixels alp(*this);
1303 if (!this->readyToDraw()) {
1304 return;
1305 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001306
1307 // whip through our loop to compute the exact size needed
1308 size_t size = 0;
1309 int maxLevels = 0;
1310 {
reed@android.com149e2f62009-05-22 14:39:03 +00001311 int width = this->width();
1312 int height = this->height();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001313 for (;;) {
1314 width >>= 1;
1315 height >>= 1;
1316 if (0 == width || 0 == height) {
1317 break;
1318 }
1319 size += ComputeRowBytes(config, width) * height;
1320 maxLevels += 1;
1321 }
1322 }
reed@android.com89bb83a2009-05-29 21:30:42 +00001323
reed@android.com149e2f62009-05-22 14:39:03 +00001324 // nothing to build
reed@android.com8a1c16f2008-12-17 15:59:43 +00001325 if (0 == maxLevels) {
1326 return;
1327 }
1328
reed@android.com149e2f62009-05-22 14:39:03 +00001329 SkBitmap srcBM(*this);
1330 srcBM.lockPixels();
1331 if (!srcBM.readyToDraw()) {
1332 return;
1333 }
1334
1335 MipMap* mm = MipMap::Alloc(maxLevels, size);
1336 if (NULL == mm) {
1337 return;
1338 }
1339
reed@android.com8a1c16f2008-12-17 15:59:43 +00001340 MipLevel* level = mm->levels();
1341 uint8_t* addr = (uint8_t*)mm->pixels();
reed@android.com149e2f62009-05-22 14:39:03 +00001342 int width = this->width();
1343 int height = this->height();
scroggo@google.come5f48242013-02-25 21:47:41 +00001344 uint32_t rowBytes;
reed@android.com149e2f62009-05-22 14:39:03 +00001345 SkBitmap dstBM;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001346
1347 for (int i = 0; i < maxLevels; i++) {
1348 width >>= 1;
1349 height >>= 1;
scroggo@google.come5f48242013-02-25 21:47:41 +00001350 rowBytes = SkToU32(ComputeRowBytes(config, width));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001351
1352 level[i].fPixels = addr;
reed@android.comf459a492009-03-27 12:33:50 +00001353 level[i].fWidth = width;
1354 level[i].fHeight = height;
reed@android.com49f0ff22009-03-19 21:52:42 +00001355 level[i].fRowBytes = rowBytes;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001356
1357 dstBM.setConfig(config, width, height, rowBytes);
1358 dstBM.setPixels(addr);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001359
bungeman@google.com7cf0e9e2012-07-25 16:09:10 +00001360 srcBM.lockPixels();
reed@android.com149e2f62009-05-22 14:39:03 +00001361 for (int y = 0; y < height; y++) {
1362 for (int x = 0; x < width; x++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001363 proc(&dstBM, x, y, srcBM);
1364 }
1365 }
bungeman@google.com7cf0e9e2012-07-25 16:09:10 +00001366 srcBM.unlockPixels();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001367
1368 srcBM = dstBM;
1369 addr += height * rowBytes;
1370 }
1371 SkASSERT(addr == (uint8_t*)mm->pixels() + size);
1372 fMipMap = mm;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001373}
1374
1375bool SkBitmap::hasMipMap() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001376 return fMipMap != NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001377}
1378
1379int SkBitmap::extractMipLevel(SkBitmap* dst, SkFixed sx, SkFixed sy) {
reed@android.com83f7bc32009-07-17 02:42:41 +00001380 if (NULL == fMipMap) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001381 return 0;
reed@android.com83f7bc32009-07-17 02:42:41 +00001382 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001383
reed@android.com8a1c16f2008-12-17 15:59:43 +00001384 int level = ComputeMipLevel(sx, sy) >> 16;
1385 SkASSERT(level >= 0);
1386 if (level <= 0) {
1387 return 0;
1388 }
1389
1390 if (level >= fMipMap->fLevelCount) {
1391 level = fMipMap->fLevelCount - 1;
1392 }
1393 if (dst) {
1394 const MipLevel& mip = fMipMap->levels()[level - 1];
1395 dst->setConfig((SkBitmap::Config)this->config(),
1396 mip.fWidth, mip.fHeight, mip.fRowBytes);
1397 dst->setPixels(mip.fPixels);
1398 }
1399 return level;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001400}
1401
1402SkFixed SkBitmap::ComputeMipLevel(SkFixed sx, SkFixed sy) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001403 sx = SkAbs32(sx);
1404 sy = SkAbs32(sy);
1405 if (sx < sy) {
1406 sx = sy;
1407 }
1408 if (sx < SK_Fixed1) {
1409 return 0;
1410 }
1411 int clz = SkCLZ(sx);
1412 SkASSERT(clz >= 1 && clz <= 15);
1413 return SkIntToFixed(15 - clz) + ((unsigned)(sx << (clz + 1)) >> 16);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001414}
1415
1416///////////////////////////////////////////////////////////////////////////////
1417
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +00001418static bool GetBitmapAlpha(const SkBitmap& src, uint8_t* SK_RESTRICT alpha,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001419 int alphaRowBytes) {
1420 SkASSERT(alpha != NULL);
1421 SkASSERT(alphaRowBytes >= src.width());
1422
1423 SkBitmap::Config config = src.getConfig();
1424 int w = src.width();
1425 int h = src.height();
scroggo@google.come5f48242013-02-25 21:47:41 +00001426 size_t rb = src.rowBytes();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001427
reed@android.com1cdcb512009-08-24 19:11:00 +00001428 SkAutoLockPixels alp(src);
1429 if (!src.readyToDraw()) {
1430 // zero out the alpha buffer and return
1431 while (--h >= 0) {
1432 memset(alpha, 0, w);
1433 alpha += alphaRowBytes;
1434 }
1435 return false;
1436 }
reed@google.com82065d62011-02-07 15:30:46 +00001437
reed@android.com8a1c16f2008-12-17 15:59:43 +00001438 if (SkBitmap::kA8_Config == config && !src.isOpaque()) {
1439 const uint8_t* s = src.getAddr8(0, 0);
1440 while (--h >= 0) {
1441 memcpy(alpha, s, w);
1442 s += rb;
1443 alpha += alphaRowBytes;
1444 }
1445 } else if (SkBitmap::kARGB_8888_Config == config && !src.isOpaque()) {
1446 const SkPMColor* SK_RESTRICT s = src.getAddr32(0, 0);
1447 while (--h >= 0) {
1448 for (int x = 0; x < w; x++) {
1449 alpha[x] = SkGetPackedA32(s[x]);
1450 }
1451 s = (const SkPMColor*)((const char*)s + rb);
1452 alpha += alphaRowBytes;
1453 }
1454 } else if (SkBitmap::kARGB_4444_Config == config && !src.isOpaque()) {
1455 const SkPMColor16* SK_RESTRICT s = src.getAddr16(0, 0);
1456 while (--h >= 0) {
1457 for (int x = 0; x < w; x++) {
1458 alpha[x] = SkPacked4444ToA32(s[x]);
1459 }
1460 s = (const SkPMColor16*)((const char*)s + rb);
1461 alpha += alphaRowBytes;
1462 }
1463 } else if (SkBitmap::kIndex8_Config == config && !src.isOpaque()) {
1464 SkColorTable* ct = src.getColorTable();
1465 if (ct) {
1466 const SkPMColor* SK_RESTRICT table = ct->lockColors();
1467 const uint8_t* SK_RESTRICT s = src.getAddr8(0, 0);
1468 while (--h >= 0) {
1469 for (int x = 0; x < w; x++) {
1470 alpha[x] = SkGetPackedA32(table[s[x]]);
1471 }
1472 s += rb;
1473 alpha += alphaRowBytes;
1474 }
1475 ct->unlockColors(false);
1476 }
1477 } else { // src is opaque, so just fill alpha[] with 0xFF
1478 memset(alpha, 0xFF, h * alphaRowBytes);
1479 }
reed@android.com1cdcb512009-08-24 19:11:00 +00001480 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001481}
1482
1483#include "SkPaint.h"
1484#include "SkMaskFilter.h"
1485#include "SkMatrix.h"
1486
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001487bool SkBitmap::extractAlpha(SkBitmap* dst, const SkPaint* paint,
djsollen@google.com57f49692011-02-23 20:46:31 +00001488 Allocator *allocator, SkIPoint* offset) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001489 SkDEBUGCODE(this->validate();)
1490
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001491 SkBitmap tmpBitmap;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001492 SkMatrix identity;
1493 SkMask srcM, dstM;
1494
1495 srcM.fBounds.set(0, 0, this->width(), this->height());
1496 srcM.fRowBytes = SkAlign4(this->width());
1497 srcM.fFormat = SkMask::kA8_Format;
1498
1499 SkMaskFilter* filter = paint ? paint->getMaskFilter() : NULL;
1500
1501 // compute our (larger?) dst bounds if we have a filter
1502 if (NULL != filter) {
1503 identity.reset();
1504 srcM.fImage = NULL;
1505 if (!filter->filterMask(&dstM, srcM, identity, NULL)) {
1506 goto NO_FILTER_CASE;
1507 }
1508 dstM.fRowBytes = SkAlign4(dstM.fBounds.width());
1509 } else {
1510 NO_FILTER_CASE:
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001511 tmpBitmap.setConfig(SkBitmap::kA8_Config, this->width(), this->height(),
reed@android.com8a1c16f2008-12-17 15:59:43 +00001512 srcM.fRowBytes);
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001513 if (!tmpBitmap.allocPixels(allocator, NULL)) {
1514 // Allocation of pixels for alpha bitmap failed.
1515 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
1516 tmpBitmap.width(), tmpBitmap.height());
1517 return false;
1518 }
1519 GetBitmapAlpha(*this, tmpBitmap.getAddr8(0, 0), srcM.fRowBytes);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001520 if (offset) {
1521 offset->set(0, 0);
1522 }
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001523 tmpBitmap.swap(*dst);
1524 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001525 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001526 srcM.fImage = SkMask::AllocImage(srcM.computeImageSize());
1527 SkAutoMaskFreeImage srcCleanup(srcM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001528
1529 GetBitmapAlpha(*this, srcM.fImage, srcM.fRowBytes);
1530 if (!filter->filterMask(&dstM, srcM, identity, NULL)) {
1531 goto NO_FILTER_CASE;
1532 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001533 SkAutoMaskFreeImage dstCleanup(dstM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001534
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001535 tmpBitmap.setConfig(SkBitmap::kA8_Config, dstM.fBounds.width(),
reed@android.com8a1c16f2008-12-17 15:59:43 +00001536 dstM.fBounds.height(), dstM.fRowBytes);
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001537 if (!tmpBitmap.allocPixels(allocator, NULL)) {
1538 // Allocation of pixels for alpha bitmap failed.
1539 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
1540 tmpBitmap.width(), tmpBitmap.height());
1541 return false;
1542 }
1543 memcpy(tmpBitmap.getPixels(), dstM.fImage, dstM.computeImageSize());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001544 if (offset) {
1545 offset->set(dstM.fBounds.fLeft, dstM.fBounds.fTop);
1546 }
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001547 SkDEBUGCODE(tmpBitmap.validate();)
1548
1549 tmpBitmap.swap(*dst);
1550 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001551}
1552
1553///////////////////////////////////////////////////////////////////////////////
1554
1555enum {
1556 SERIALIZE_PIXELTYPE_NONE,
djsollen@google.com21830d92012-08-07 19:49:41 +00001557 SERIALIZE_PIXELTYPE_REF_DATA
reed@android.com8a1c16f2008-12-17 15:59:43 +00001558};
1559
reed@android.com8a1c16f2008-12-17 15:59:43 +00001560void SkBitmap::flatten(SkFlattenableWriteBuffer& buffer) const {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001561 buffer.writeInt(fWidth);
1562 buffer.writeInt(fHeight);
1563 buffer.writeInt(fRowBytes);
1564 buffer.writeInt(fConfig);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001565 buffer.writeBool(this->isOpaque());
weita@google.comf9ab99a2009-05-03 18:23:30 +00001566
reed@android.com8a1c16f2008-12-17 15:59:43 +00001567 if (fPixelRef) {
djsollen@google.com5370cd92012-03-28 20:47:01 +00001568 if (fPixelRef->getFactory()) {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001569 buffer.writeInt(SERIALIZE_PIXELTYPE_REF_DATA);
scroggo@google.come5f48242013-02-25 21:47:41 +00001570 buffer.writeUInt(SkToU32(fPixelRefOffset));
djsollen@google.com5370cd92012-03-28 20:47:01 +00001571 buffer.writeFlattenable(fPixelRef);
1572 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001573 }
1574 // if we get here, we can't record the pixels
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001575 buffer.writeInt(SERIALIZE_PIXELTYPE_NONE);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001576 } else {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001577 buffer.writeInt(SERIALIZE_PIXELTYPE_NONE);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001578 }
1579}
1580
1581void SkBitmap::unflatten(SkFlattenableReadBuffer& buffer) {
1582 this->reset();
weita@google.comf9ab99a2009-05-03 18:23:30 +00001583
reed@android.com8a1c16f2008-12-17 15:59:43 +00001584 int width = buffer.readInt();
1585 int height = buffer.readInt();
1586 int rowBytes = buffer.readInt();
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001587 int config = buffer.readInt();
weita@google.comf9ab99a2009-05-03 18:23:30 +00001588
reed@android.com8a1c16f2008-12-17 15:59:43 +00001589 this->setConfig((Config)config, width, height, rowBytes);
1590 this->setIsOpaque(buffer.readBool());
weita@google.comf9ab99a2009-05-03 18:23:30 +00001591
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001592 int reftype = buffer.readInt();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001593 switch (reftype) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001594 case SERIALIZE_PIXELTYPE_REF_DATA: {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001595 size_t offset = buffer.readUInt();
1596 SkPixelRef* pr = buffer.readFlattenableT<SkPixelRef>();
reed@google.com82065d62011-02-07 15:30:46 +00001597 SkSafeUnref(this->setPixelRef(pr, offset));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001598 break;
1599 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001600 case SERIALIZE_PIXELTYPE_NONE:
1601 break;
1602 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +00001603 SkDEBUGFAIL("unrecognized pixeltype in serialized data");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001604 sk_throw();
1605 }
1606}
1607
1608///////////////////////////////////////////////////////////////////////////////
1609
1610SkBitmap::RLEPixels::RLEPixels(int width, int height) {
1611 fHeight = height;
1612 fYPtrs = (uint8_t**)sk_malloc_throw(height * sizeof(uint8_t*));
reed@android.com4516f472009-06-29 16:25:36 +00001613 sk_bzero(fYPtrs, height * sizeof(uint8_t*));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001614}
1615
1616SkBitmap::RLEPixels::~RLEPixels() {
1617 sk_free(fYPtrs);
1618}
1619
1620///////////////////////////////////////////////////////////////////////////////
1621
1622#ifdef SK_DEBUG
1623void SkBitmap::validate() const {
1624 SkASSERT(fConfig < kConfigCount);
1625 SkASSERT(fRowBytes >= (unsigned)ComputeRowBytes((Config)fConfig, fWidth));
djsollen@google.com21830d92012-08-07 19:49:41 +00001626 SkASSERT(fFlags <= (kImageIsOpaque_Flag | kImageIsVolatile_Flag | kImageIsImmutable_Flag));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001627 SkASSERT(fPixelLockCount >= 0);
1628 SkASSERT(NULL == fColorTable || (unsigned)fColorTable->getRefCnt() < 10000);
1629 SkASSERT((uint8_t)ComputeBytesPerPixel((Config)fConfig) == fBytesPerPixel);
1630
1631#if 0 // these asserts are not thread-correct, so disable for now
1632 if (fPixelRef) {
1633 if (fPixelLockCount > 0) {
reed@google.comff0da4f2012-05-17 13:14:52 +00001634 SkASSERT(fPixelRef->isLocked());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001635 } else {
1636 SkASSERT(NULL == fPixels);
1637 SkASSERT(NULL == fColorTable);
1638 }
1639 }
1640#endif
1641}
1642#endif
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001643
1644#ifdef SK_DEVELOPER
1645void SkBitmap::toString(SkString* str) const {
1646
1647 static const char* gConfigNames[kConfigCount] = {
1648 "NONE", "A1", "A8", "INDEX8", "565", "4444", "8888", "RLE"
1649 };
1650
1651 str->appendf("bitmap: ((%d, %d) %s", this->width(), this->height(),
1652 gConfigNames[this->config()]);
1653
1654 str->append(" (");
1655 if (this->isOpaque()) {
1656 str->append("opaque");
1657 } else {
1658 str->append("transparent");
1659 }
1660 if (this->isImmutable()) {
1661 str->append(", immutable");
1662 } else {
1663 str->append(", not-immutable");
1664 }
1665 str->append(")");
1666
1667 SkPixelRef* pr = this->pixelRef();
1668 if (NULL == pr) {
1669 // show null or the explicit pixel address (rare)
1670 str->appendf(" pixels:%p", this->getPixels());
1671 } else {
1672 const char* uri = pr->getURI();
1673 if (NULL != uri) {
1674 str->appendf(" uri:\"%s\"", uri);
1675 } else {
1676 str->appendf(" pixelref:%p", pr);
1677 }
1678 }
1679
1680 str->append(")");
1681}
1682#endif