blob: 122996a31229cdb28d9120903a4b080a0684f0e0 [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,
scroggo@google.com61d6c9e2013-05-21 20:38:40 +0000870 int32_t* x, int32_t* y);
871bool get_upper_left_from_offset(SkBitmap::Config config, size_t offset, size_t rowBytes,
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000872 int32_t* x, int32_t* y) {
scroggo@google.coma2a31922012-12-07 19:14:45 +0000873 SkASSERT(x != NULL && y != NULL);
scroggo@google.coma2a31922012-12-07 19:14:45 +0000874 if (0 == offset) {
875 *x = *y = 0;
876 return true;
877 }
878 // Use integer division to find the correct y position.
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000879 *y = SkToS32(offset / rowBytes);
880 // The remainder will be the x position, after we reverse get_sub_offset.
881 *x = SkToS32(offset % rowBytes);
882 switch (config) {
scroggo@google.coma2a31922012-12-07 19:14:45 +0000883 case SkBitmap::kA8_Config:
884 // Fall through.
885 case SkBitmap::kIndex8_Config:
886 // x is unmodified
887 break;
888
889 case SkBitmap::kRGB_565_Config:
890 // Fall through.
891 case SkBitmap::kARGB_4444_Config:
892 *x >>= 1;
893 break;
894
895 case SkBitmap::kARGB_8888_Config:
896 *x >>= 2;
897 break;
898
899 case SkBitmap::kNo_Config:
900 // Fall through.
901 case SkBitmap::kA1_Config:
902 // Fall through.
903 default:
904 return false;
905 }
906 return true;
907}
908
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000909static bool get_upper_left_from_offset(const SkBitmap& bm, int32_t* x, int32_t* y) {
910 return get_upper_left_from_offset(bm.config(), bm.pixelRefOffset(), bm.rowBytes(), x, y);
911}
912
reed@android.com8a1c16f2008-12-17 15:59:43 +0000913bool SkBitmap::extractSubset(SkBitmap* result, const SkIRect& subset) const {
914 SkDEBUGCODE(this->validate();)
915
djsollen@google.comc84b8332012-07-27 13:41:44 +0000916 if (NULL == result || NULL == fPixelRef) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000917 return false; // no src pixels
918 }
919
920 SkIRect srcRect, r;
921 srcRect.set(0, 0, this->width(), this->height());
922 if (!r.intersect(srcRect, subset)) {
923 return false; // r is empty (i.e. no intersection)
924 }
925
scroggo@google.coma2a31922012-12-07 19:14:45 +0000926 if (fPixelRef->getTexture() != NULL) {
927 // Do a deep copy
928 SkPixelRef* pixelRef = fPixelRef->deepCopy(this->config(), &subset);
929 if (pixelRef != NULL) {
930 SkBitmap dst;
931 dst.setConfig(this->config(), subset.width(), subset.height());
932 dst.setIsVolatile(this->isVolatile());
933 dst.setIsOpaque(this->isOpaque());
934 dst.setPixelRef(pixelRef)->unref();
935 SkDEBUGCODE(dst.validate());
936 result->swap(dst);
937 return true;
938 }
939 }
940
reed@android.com8a1c16f2008-12-17 15:59:43 +0000941 if (kRLE_Index8_Config == fConfig) {
942 SkAutoLockPixels alp(*this);
943 // don't call readyToDraw(), since we can operate w/o a colortable
944 // at this stage
945 if (this->getPixels() == NULL) {
946 return false;
947 }
948 SkBitmap bm;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000949
reed@android.com8a1c16f2008-12-17 15:59:43 +0000950 bm.setConfig(kIndex8_Config, r.width(), r.height());
951 bm.allocPixels(this->getColorTable());
952 if (NULL == bm.getPixels()) {
953 return false;
954 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000955
reed@android.com8a1c16f2008-12-17 15:59:43 +0000956 const RLEPixels* rle = (const RLEPixels*)this->getPixels();
957 uint8_t* dst = bm.getAddr8(0, 0);
958 const int width = bm.width();
scroggo@google.come5f48242013-02-25 21:47:41 +0000959 const size_t rowBytes = bm.rowBytes();
weita@google.comf9ab99a2009-05-03 18:23:30 +0000960
reed@android.com8a1c16f2008-12-17 15:59:43 +0000961 for (int y = r.fTop; y < r.fBottom; y++) {
962 SkPackBits::Unpack8(dst, r.fLeft, width, rle->packedAtY(y));
963 dst += rowBytes;
964 }
965 result->swap(bm);
966 return true;
967 }
968
scroggo@google.coma2a31922012-12-07 19:14:45 +0000969 // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have
970 // exited above.
971 SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width()));
972 SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height()));
973
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000974 size_t offset = get_sub_offset(*this, r.fLeft, r.fTop);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000975 if (SUB_OFFSET_FAILURE == offset) {
976 return false; // config not supported
977 }
978
979 SkBitmap dst;
980 dst.setConfig(this->config(), r.width(), r.height(), this->rowBytes());
skyostil@google.com0eb75762012-01-16 10:45:53 +0000981 dst.setIsVolatile(this->isVolatile());
reed@google.com04685d22012-10-15 13:45:40 +0000982 dst.setIsOpaque(this->isOpaque());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000983
984 if (fPixelRef) {
985 // share the pixelref with a custom offset
986 dst.setPixelRef(fPixelRef, fPixelRefOffset + offset);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000987 }
988 SkDEBUGCODE(dst.validate();)
989
990 // we know we're good, so commit to result
991 result->swap(dst);
992 return true;
993}
994
995///////////////////////////////////////////////////////////////////////////////
996
997#include "SkCanvas.h"
998#include "SkPaint.h"
999
reed@android.comfbaa88d2009-05-06 17:44:34 +00001000bool SkBitmap::canCopyTo(Config dstConfig) const {
1001 if (this->getConfig() == kNo_Config) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001002 return false;
1003 }
1004
reed@android.comfbaa88d2009-05-06 17:44:34 +00001005 bool sameConfigs = (this->config() == dstConfig);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001006 switch (dstConfig) {
1007 case kA8_Config:
1008 case kARGB_4444_Config:
1009 case kRGB_565_Config:
1010 case kARGB_8888_Config:
1011 break;
weita@google.comf9ab99a2009-05-03 18:23:30 +00001012 case kA1_Config:
1013 case kIndex8_Config:
1014 if (!sameConfigs) {
1015 return false;
1016 }
1017 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001018 default:
1019 return false;
1020 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001021
1022 // do not copy src if srcConfig == kA1_Config while dstConfig != kA1_Config
1023 if (this->getConfig() == kA1_Config && !sameConfigs) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001024 return false;
1025 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001026
reed@android.comfbaa88d2009-05-06 17:44:34 +00001027 return true;
1028}
1029
1030bool SkBitmap::copyTo(SkBitmap* dst, Config dstConfig, Allocator* alloc) const {
1031 if (!this->canCopyTo(dstConfig)) {
1032 return false;
1033 }
1034
reed@google.com50dfa012011-04-01 19:05:36 +00001035 // if we have a texture, first get those pixels
1036 SkBitmap tmpSrc;
1037 const SkBitmap* src = this;
1038
scroggo@google.coma2a31922012-12-07 19:14:45 +00001039 if (fPixelRef) {
1040 SkIRect subset;
scroggo@google.com1b1bcc32013-05-21 20:31:23 +00001041 if (get_upper_left_from_offset(*this, &subset.fLeft, &subset.fTop)) {
scroggo@google.coma2a31922012-12-07 19:14:45 +00001042 subset.fRight = subset.fLeft + fWidth;
1043 subset.fBottom = subset.fTop + fHeight;
1044 if (fPixelRef->readPixels(&tmpSrc, &subset)) {
1045 SkASSERT(tmpSrc.width() == this->width());
1046 SkASSERT(tmpSrc.height() == this->height());
reed@google.com50dfa012011-04-01 19:05:36 +00001047
scroggo@google.coma2a31922012-12-07 19:14:45 +00001048 // did we get lucky and we can just return tmpSrc?
1049 if (tmpSrc.config() == dstConfig && NULL == alloc) {
1050 dst->swap(tmpSrc);
1051 if (dst->pixelRef() && this->config() == dstConfig) {
1052 dst->pixelRef()->fGenerationID = fPixelRef->getGenerationID();
1053 }
1054 return true;
1055 }
1056
1057 // fall through to the raster case
1058 src = &tmpSrc;
scroggo@google.comd5764e82012-08-22 15:00:05 +00001059 }
reed@google.com50dfa012011-04-01 19:05:36 +00001060 }
reed@android.comfbaa88d2009-05-06 17:44:34 +00001061 }
reed@android.com311c82d2009-05-05 23:13:23 +00001062
reed@google.com50dfa012011-04-01 19:05:36 +00001063 // we lock this now, since we may need its colortable
1064 SkAutoLockPixels srclock(*src);
1065 if (!src->readyToDraw()) {
1066 return false;
1067 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001068
reed@google.com50dfa012011-04-01 19:05:36 +00001069 SkBitmap tmpDst;
1070 tmpDst.setConfig(dstConfig, src->width(), src->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001071
weita@google.comf9ab99a2009-05-03 18:23:30 +00001072 // allocate colortable if srcConfig == kIndex8_Config
1073 SkColorTable* ctable = (dstConfig == kIndex8_Config) ?
reed@google.com50dfa012011-04-01 19:05:36 +00001074 new SkColorTable(*src->getColorTable()) : NULL;
weita@google.comf9ab99a2009-05-03 18:23:30 +00001075 SkAutoUnref au(ctable);
reed@google.com50dfa012011-04-01 19:05:36 +00001076 if (!tmpDst.allocPixels(alloc, ctable)) {
weita@google.comf9ab99a2009-05-03 18:23:30 +00001077 return false;
1078 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001079
reed@google.com50dfa012011-04-01 19:05:36 +00001080 if (!tmpDst.readyToDraw()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001081 // allocator/lock failed
1082 return false;
1083 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001084
reed@android.comfbaa88d2009-05-06 17:44:34 +00001085 /* do memcpy for the same configs cases, else use drawing
weita@google.comf9ab99a2009-05-03 18:23:30 +00001086 */
reed@google.com50dfa012011-04-01 19:05:36 +00001087 if (src->config() == dstConfig) {
1088 if (tmpDst.getSize() == src->getSize()) {
1089 memcpy(tmpDst.getPixels(), src->getPixels(), src->getSafeSize());
scroggo@google.comd5764e82012-08-22 15:00:05 +00001090 SkPixelRef* pixelRef = tmpDst.pixelRef();
1091 if (pixelRef != NULL) {
1092 pixelRef->fGenerationID = this->getGenerationID();
1093 }
reed@android.com311c82d2009-05-05 23:13:23 +00001094 } else {
reed@google.com50dfa012011-04-01 19:05:36 +00001095 const char* srcP = reinterpret_cast<const char*>(src->getPixels());
1096 char* dstP = reinterpret_cast<char*>(tmpDst.getPixels());
reed@android.com311c82d2009-05-05 23:13:23 +00001097 // to be sure we don't read too much, only copy our logical pixels
reed@google.com50dfa012011-04-01 19:05:36 +00001098 size_t bytesToCopy = tmpDst.width() * tmpDst.bytesPerPixel();
1099 for (int y = 0; y < tmpDst.height(); y++) {
reed@android.com311c82d2009-05-05 23:13:23 +00001100 memcpy(dstP, srcP, bytesToCopy);
reed@google.com50dfa012011-04-01 19:05:36 +00001101 srcP += src->rowBytes();
1102 dstP += tmpDst.rowBytes();
reed@android.com311c82d2009-05-05 23:13:23 +00001103 }
1104 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001105 } else {
1106 // if the src has alpha, we have to clear the dst first
reed@google.com50dfa012011-04-01 19:05:36 +00001107 if (!src->isOpaque()) {
junov@google.comdbfac8a2012-12-06 21:47:40 +00001108 tmpDst.eraseColor(SK_ColorTRANSPARENT);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001109 }
1110
reed@google.com50dfa012011-04-01 19:05:36 +00001111 SkCanvas canvas(tmpDst);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001112 SkPaint paint;
1113
1114 paint.setDither(true);
reed@google.com50dfa012011-04-01 19:05:36 +00001115 canvas.drawBitmap(*src, 0, 0, &paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001116 }
1117
reed@google.com50dfa012011-04-01 19:05:36 +00001118 tmpDst.setIsOpaque(src->isOpaque());
reed@android.comcafc9f92009-08-22 03:44:57 +00001119
reed@google.com50dfa012011-04-01 19:05:36 +00001120 dst->swap(tmpDst);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001121 return true;
1122}
1123
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001124bool SkBitmap::deepCopyTo(SkBitmap* dst, Config dstConfig) const {
1125 if (!this->canCopyTo(dstConfig)) {
1126 return false;
1127 }
1128
1129 // If we have a PixelRef, and it supports deep copy, use it.
1130 // Currently supported only by texture-backed bitmaps.
1131 if (fPixelRef) {
1132 SkPixelRef* pixelRef = fPixelRef->deepCopy(dstConfig);
1133 if (pixelRef) {
scroggo@google.coma2a31922012-12-07 19:14:45 +00001134 uint32_t rowBytes;
scroggo@google.comd5764e82012-08-22 15:00:05 +00001135 if (dstConfig == fConfig) {
1136 pixelRef->fGenerationID = fPixelRef->getGenerationID();
scroggo@google.coma2a31922012-12-07 19:14:45 +00001137 // Use the same rowBytes as the original.
1138 rowBytes = fRowBytes;
1139 } else {
1140 // With the new config, an appropriate fRowBytes will be computed by setConfig.
1141 rowBytes = 0;
scroggo@google.comd5764e82012-08-22 15:00:05 +00001142 }
scroggo@google.coma2a31922012-12-07 19:14:45 +00001143 dst->setConfig(dstConfig, fWidth, fHeight, rowBytes);
1144
1145 size_t pixelRefOffset;
1146 if (0 == fPixelRefOffset || dstConfig == fConfig) {
1147 // Use the same offset as the original.
1148 pixelRefOffset = fPixelRefOffset;
1149 } else {
1150 // Find the correct offset in the new config. This needs to be done after calling
1151 // setConfig so dst's fConfig and fRowBytes have been set properly.
scroggo@google.come5f48242013-02-25 21:47:41 +00001152 int32_t x, y;
scroggo@google.com1b1bcc32013-05-21 20:31:23 +00001153 if (!get_upper_left_from_offset(*this, &x, &y)) {
scroggo@google.coma2a31922012-12-07 19:14:45 +00001154 return false;
1155 }
scroggo@google.com1b1bcc32013-05-21 20:31:23 +00001156 pixelRefOffset = get_sub_offset(*dst, x, y);
scroggo@google.coma2a31922012-12-07 19:14:45 +00001157 if (SUB_OFFSET_FAILURE == pixelRefOffset) {
1158 return false;
1159 }
1160 }
1161 dst->setPixelRef(pixelRef, pixelRefOffset)->unref();
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001162 return true;
1163 }
1164 }
1165
1166 if (this->getTexture()) {
1167 return false;
1168 } else {
1169 return this->copyTo(dst, dstConfig, NULL);
1170 }
1171}
1172
reed@android.com8a1c16f2008-12-17 15:59:43 +00001173///////////////////////////////////////////////////////////////////////////////
1174///////////////////////////////////////////////////////////////////////////////
1175
1176static void downsampleby2_proc32(SkBitmap* dst, int x, int y,
1177 const SkBitmap& src) {
1178 x <<= 1;
1179 y <<= 1;
1180 const SkPMColor* p = src.getAddr32(x, y);
reed@android.com829c83c2009-06-08 12:05:31 +00001181 const SkPMColor* baseP = p;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001182 SkPMColor c, ag, rb;
1183
1184 c = *p; ag = (c >> 8) & 0xFF00FF; rb = c & 0xFF00FF;
1185 if (x < src.width() - 1) {
1186 p += 1;
1187 }
1188 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
1189
reed@android.com829c83c2009-06-08 12:05:31 +00001190 p = baseP;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001191 if (y < src.height() - 1) {
reed@android.com829c83c2009-06-08 12:05:31 +00001192 p += src.rowBytes() >> 2;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001193 }
1194 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
1195 if (x < src.width() - 1) {
1196 p += 1;
1197 }
1198 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
1199
1200 *dst->getAddr32(x >> 1, y >> 1) =
1201 ((rb >> 2) & 0xFF00FF) | ((ag << 6) & 0xFF00FF00);
1202}
1203
1204static inline uint32_t expand16(U16CPU c) {
1205 return (c & ~SK_G16_MASK_IN_PLACE) | ((c & SK_G16_MASK_IN_PLACE) << 16);
1206}
1207
1208// returns dirt in the top 16bits, but we don't care, since we only
1209// store the low 16bits.
1210static inline U16CPU pack16(uint32_t c) {
1211 return (c & ~SK_G16_MASK_IN_PLACE) | ((c >> 16) & SK_G16_MASK_IN_PLACE);
1212}
1213
1214static void downsampleby2_proc16(SkBitmap* dst, int x, int y,
1215 const SkBitmap& src) {
1216 x <<= 1;
1217 y <<= 1;
1218 const uint16_t* p = src.getAddr16(x, y);
reed@android.com829c83c2009-06-08 12:05:31 +00001219 const uint16_t* baseP = p;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001220 SkPMColor c;
weita@google.comf9ab99a2009-05-03 18:23:30 +00001221
reed@android.com8a1c16f2008-12-17 15:59:43 +00001222 c = expand16(*p);
reed@android.com829c83c2009-06-08 12:05:31 +00001223 if (x < src.width() - 1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001224 p += 1;
1225 }
1226 c += expand16(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001227
reed@android.com829c83c2009-06-08 12:05:31 +00001228 p = baseP;
1229 if (y < src.height() - 1) {
1230 p += src.rowBytes() >> 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001231 }
1232 c += expand16(*p);
reed@android.com829c83c2009-06-08 12:05:31 +00001233 if (x < src.width() - 1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001234 p += 1;
1235 }
1236 c += expand16(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001237
reed@android.com8a1c16f2008-12-17 15:59:43 +00001238 *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)pack16(c >> 2);
1239}
1240
1241static uint32_t expand4444(U16CPU c) {
1242 return (c & 0xF0F) | ((c & ~0xF0F) << 12);
1243}
1244
1245static U16CPU collaps4444(uint32_t c) {
1246 return (c & 0xF0F) | ((c >> 12) & ~0xF0F);
1247}
1248
1249static void downsampleby2_proc4444(SkBitmap* dst, int x, int y,
1250 const SkBitmap& src) {
1251 x <<= 1;
1252 y <<= 1;
1253 const uint16_t* p = src.getAddr16(x, y);
reed@android.com829c83c2009-06-08 12:05:31 +00001254 const uint16_t* baseP = p;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001255 uint32_t c;
weita@google.comf9ab99a2009-05-03 18:23:30 +00001256
reed@android.com8a1c16f2008-12-17 15:59:43 +00001257 c = expand4444(*p);
1258 if (x < src.width() - 1) {
1259 p += 1;
1260 }
1261 c += expand4444(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001262
reed@android.com829c83c2009-06-08 12:05:31 +00001263 p = baseP;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001264 if (y < src.height() - 1) {
reed@android.com829c83c2009-06-08 12:05:31 +00001265 p += src.rowBytes() >> 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001266 }
1267 c += expand4444(*p);
1268 if (x < src.width() - 1) {
1269 p += 1;
1270 }
1271 c += expand4444(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001272
reed@android.com8a1c16f2008-12-17 15:59:43 +00001273 *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)collaps4444(c >> 2);
1274}
1275
1276void SkBitmap::buildMipMap(bool forceRebuild) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001277 if (forceRebuild)
1278 this->freeMipMap();
1279 else if (fMipMap)
1280 return; // we're already built
1281
1282 SkASSERT(NULL == fMipMap);
1283
1284 void (*proc)(SkBitmap* dst, int x, int y, const SkBitmap& src);
1285
1286 const SkBitmap::Config config = this->getConfig();
1287
1288 switch (config) {
1289 case kARGB_8888_Config:
1290 proc = downsampleby2_proc32;
1291 break;
1292 case kRGB_565_Config:
1293 proc = downsampleby2_proc16;
1294 break;
1295 case kARGB_4444_Config:
1296 proc = downsampleby2_proc4444;
1297 break;
1298 case kIndex8_Config:
1299 case kA8_Config:
1300 default:
1301 return; // don't build mipmaps for these configs
1302 }
reed@android.com89bb83a2009-05-29 21:30:42 +00001303
reed@android.com149e2f62009-05-22 14:39:03 +00001304 SkAutoLockPixels alp(*this);
1305 if (!this->readyToDraw()) {
1306 return;
1307 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001308
1309 // whip through our loop to compute the exact size needed
1310 size_t size = 0;
1311 int maxLevels = 0;
1312 {
reed@android.com149e2f62009-05-22 14:39:03 +00001313 int width = this->width();
1314 int height = this->height();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001315 for (;;) {
1316 width >>= 1;
1317 height >>= 1;
1318 if (0 == width || 0 == height) {
1319 break;
1320 }
1321 size += ComputeRowBytes(config, width) * height;
1322 maxLevels += 1;
1323 }
1324 }
reed@android.com89bb83a2009-05-29 21:30:42 +00001325
reed@android.com149e2f62009-05-22 14:39:03 +00001326 // nothing to build
reed@android.com8a1c16f2008-12-17 15:59:43 +00001327 if (0 == maxLevels) {
1328 return;
1329 }
1330
reed@android.com149e2f62009-05-22 14:39:03 +00001331 SkBitmap srcBM(*this);
1332 srcBM.lockPixels();
1333 if (!srcBM.readyToDraw()) {
1334 return;
1335 }
1336
1337 MipMap* mm = MipMap::Alloc(maxLevels, size);
1338 if (NULL == mm) {
1339 return;
1340 }
1341
reed@android.com8a1c16f2008-12-17 15:59:43 +00001342 MipLevel* level = mm->levels();
1343 uint8_t* addr = (uint8_t*)mm->pixels();
reed@android.com149e2f62009-05-22 14:39:03 +00001344 int width = this->width();
1345 int height = this->height();
scroggo@google.come5f48242013-02-25 21:47:41 +00001346 uint32_t rowBytes;
reed@android.com149e2f62009-05-22 14:39:03 +00001347 SkBitmap dstBM;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001348
1349 for (int i = 0; i < maxLevels; i++) {
1350 width >>= 1;
1351 height >>= 1;
scroggo@google.come5f48242013-02-25 21:47:41 +00001352 rowBytes = SkToU32(ComputeRowBytes(config, width));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001353
1354 level[i].fPixels = addr;
reed@android.comf459a492009-03-27 12:33:50 +00001355 level[i].fWidth = width;
1356 level[i].fHeight = height;
reed@android.com49f0ff22009-03-19 21:52:42 +00001357 level[i].fRowBytes = rowBytes;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001358
1359 dstBM.setConfig(config, width, height, rowBytes);
1360 dstBM.setPixels(addr);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001361
bungeman@google.com7cf0e9e2012-07-25 16:09:10 +00001362 srcBM.lockPixels();
reed@android.com149e2f62009-05-22 14:39:03 +00001363 for (int y = 0; y < height; y++) {
1364 for (int x = 0; x < width; x++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001365 proc(&dstBM, x, y, srcBM);
1366 }
1367 }
bungeman@google.com7cf0e9e2012-07-25 16:09:10 +00001368 srcBM.unlockPixels();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001369
1370 srcBM = dstBM;
1371 addr += height * rowBytes;
1372 }
1373 SkASSERT(addr == (uint8_t*)mm->pixels() + size);
1374 fMipMap = mm;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001375}
1376
1377bool SkBitmap::hasMipMap() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001378 return fMipMap != NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001379}
1380
1381int SkBitmap::extractMipLevel(SkBitmap* dst, SkFixed sx, SkFixed sy) {
reed@android.com83f7bc32009-07-17 02:42:41 +00001382 if (NULL == fMipMap) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001383 return 0;
reed@android.com83f7bc32009-07-17 02:42:41 +00001384 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001385
reed@android.com8a1c16f2008-12-17 15:59:43 +00001386 int level = ComputeMipLevel(sx, sy) >> 16;
1387 SkASSERT(level >= 0);
1388 if (level <= 0) {
1389 return 0;
1390 }
1391
1392 if (level >= fMipMap->fLevelCount) {
1393 level = fMipMap->fLevelCount - 1;
1394 }
1395 if (dst) {
1396 const MipLevel& mip = fMipMap->levels()[level - 1];
1397 dst->setConfig((SkBitmap::Config)this->config(),
1398 mip.fWidth, mip.fHeight, mip.fRowBytes);
1399 dst->setPixels(mip.fPixels);
1400 }
1401 return level;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001402}
1403
1404SkFixed SkBitmap::ComputeMipLevel(SkFixed sx, SkFixed sy) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001405 sx = SkAbs32(sx);
1406 sy = SkAbs32(sy);
1407 if (sx < sy) {
1408 sx = sy;
1409 }
1410 if (sx < SK_Fixed1) {
1411 return 0;
1412 }
1413 int clz = SkCLZ(sx);
1414 SkASSERT(clz >= 1 && clz <= 15);
1415 return SkIntToFixed(15 - clz) + ((unsigned)(sx << (clz + 1)) >> 16);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001416}
1417
1418///////////////////////////////////////////////////////////////////////////////
1419
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +00001420static bool GetBitmapAlpha(const SkBitmap& src, uint8_t* SK_RESTRICT alpha,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001421 int alphaRowBytes) {
1422 SkASSERT(alpha != NULL);
1423 SkASSERT(alphaRowBytes >= src.width());
1424
1425 SkBitmap::Config config = src.getConfig();
1426 int w = src.width();
1427 int h = src.height();
scroggo@google.come5f48242013-02-25 21:47:41 +00001428 size_t rb = src.rowBytes();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001429
reed@android.com1cdcb512009-08-24 19:11:00 +00001430 SkAutoLockPixels alp(src);
1431 if (!src.readyToDraw()) {
1432 // zero out the alpha buffer and return
1433 while (--h >= 0) {
1434 memset(alpha, 0, w);
1435 alpha += alphaRowBytes;
1436 }
1437 return false;
1438 }
reed@google.com82065d62011-02-07 15:30:46 +00001439
reed@android.com8a1c16f2008-12-17 15:59:43 +00001440 if (SkBitmap::kA8_Config == config && !src.isOpaque()) {
1441 const uint8_t* s = src.getAddr8(0, 0);
1442 while (--h >= 0) {
1443 memcpy(alpha, s, w);
1444 s += rb;
1445 alpha += alphaRowBytes;
1446 }
1447 } else if (SkBitmap::kARGB_8888_Config == config && !src.isOpaque()) {
1448 const SkPMColor* SK_RESTRICT s = src.getAddr32(0, 0);
1449 while (--h >= 0) {
1450 for (int x = 0; x < w; x++) {
1451 alpha[x] = SkGetPackedA32(s[x]);
1452 }
1453 s = (const SkPMColor*)((const char*)s + rb);
1454 alpha += alphaRowBytes;
1455 }
1456 } else if (SkBitmap::kARGB_4444_Config == config && !src.isOpaque()) {
1457 const SkPMColor16* SK_RESTRICT s = src.getAddr16(0, 0);
1458 while (--h >= 0) {
1459 for (int x = 0; x < w; x++) {
1460 alpha[x] = SkPacked4444ToA32(s[x]);
1461 }
1462 s = (const SkPMColor16*)((const char*)s + rb);
1463 alpha += alphaRowBytes;
1464 }
1465 } else if (SkBitmap::kIndex8_Config == config && !src.isOpaque()) {
1466 SkColorTable* ct = src.getColorTable();
1467 if (ct) {
1468 const SkPMColor* SK_RESTRICT table = ct->lockColors();
1469 const uint8_t* SK_RESTRICT s = src.getAddr8(0, 0);
1470 while (--h >= 0) {
1471 for (int x = 0; x < w; x++) {
1472 alpha[x] = SkGetPackedA32(table[s[x]]);
1473 }
1474 s += rb;
1475 alpha += alphaRowBytes;
1476 }
1477 ct->unlockColors(false);
1478 }
1479 } else { // src is opaque, so just fill alpha[] with 0xFF
1480 memset(alpha, 0xFF, h * alphaRowBytes);
1481 }
reed@android.com1cdcb512009-08-24 19:11:00 +00001482 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001483}
1484
1485#include "SkPaint.h"
1486#include "SkMaskFilter.h"
1487#include "SkMatrix.h"
1488
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001489bool SkBitmap::extractAlpha(SkBitmap* dst, const SkPaint* paint,
djsollen@google.com57f49692011-02-23 20:46:31 +00001490 Allocator *allocator, SkIPoint* offset) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001491 SkDEBUGCODE(this->validate();)
1492
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001493 SkBitmap tmpBitmap;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001494 SkMatrix identity;
1495 SkMask srcM, dstM;
1496
1497 srcM.fBounds.set(0, 0, this->width(), this->height());
1498 srcM.fRowBytes = SkAlign4(this->width());
1499 srcM.fFormat = SkMask::kA8_Format;
1500
1501 SkMaskFilter* filter = paint ? paint->getMaskFilter() : NULL;
1502
1503 // compute our (larger?) dst bounds if we have a filter
1504 if (NULL != filter) {
1505 identity.reset();
1506 srcM.fImage = NULL;
1507 if (!filter->filterMask(&dstM, srcM, identity, NULL)) {
1508 goto NO_FILTER_CASE;
1509 }
1510 dstM.fRowBytes = SkAlign4(dstM.fBounds.width());
1511 } else {
1512 NO_FILTER_CASE:
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001513 tmpBitmap.setConfig(SkBitmap::kA8_Config, this->width(), this->height(),
reed@android.com8a1c16f2008-12-17 15:59:43 +00001514 srcM.fRowBytes);
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001515 if (!tmpBitmap.allocPixels(allocator, NULL)) {
1516 // Allocation of pixels for alpha bitmap failed.
1517 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
1518 tmpBitmap.width(), tmpBitmap.height());
1519 return false;
1520 }
1521 GetBitmapAlpha(*this, tmpBitmap.getAddr8(0, 0), srcM.fRowBytes);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001522 if (offset) {
1523 offset->set(0, 0);
1524 }
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001525 tmpBitmap.swap(*dst);
1526 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001527 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001528 srcM.fImage = SkMask::AllocImage(srcM.computeImageSize());
1529 SkAutoMaskFreeImage srcCleanup(srcM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001530
1531 GetBitmapAlpha(*this, srcM.fImage, srcM.fRowBytes);
1532 if (!filter->filterMask(&dstM, srcM, identity, NULL)) {
1533 goto NO_FILTER_CASE;
1534 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001535 SkAutoMaskFreeImage dstCleanup(dstM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001536
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001537 tmpBitmap.setConfig(SkBitmap::kA8_Config, dstM.fBounds.width(),
reed@android.com8a1c16f2008-12-17 15:59:43 +00001538 dstM.fBounds.height(), dstM.fRowBytes);
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001539 if (!tmpBitmap.allocPixels(allocator, NULL)) {
1540 // Allocation of pixels for alpha bitmap failed.
1541 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
1542 tmpBitmap.width(), tmpBitmap.height());
1543 return false;
1544 }
1545 memcpy(tmpBitmap.getPixels(), dstM.fImage, dstM.computeImageSize());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001546 if (offset) {
1547 offset->set(dstM.fBounds.fLeft, dstM.fBounds.fTop);
1548 }
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001549 SkDEBUGCODE(tmpBitmap.validate();)
1550
1551 tmpBitmap.swap(*dst);
1552 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001553}
1554
1555///////////////////////////////////////////////////////////////////////////////
1556
1557enum {
1558 SERIALIZE_PIXELTYPE_NONE,
djsollen@google.com21830d92012-08-07 19:49:41 +00001559 SERIALIZE_PIXELTYPE_REF_DATA
reed@android.com8a1c16f2008-12-17 15:59:43 +00001560};
1561
reed@android.com8a1c16f2008-12-17 15:59:43 +00001562void SkBitmap::flatten(SkFlattenableWriteBuffer& buffer) const {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001563 buffer.writeInt(fWidth);
1564 buffer.writeInt(fHeight);
1565 buffer.writeInt(fRowBytes);
1566 buffer.writeInt(fConfig);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001567 buffer.writeBool(this->isOpaque());
weita@google.comf9ab99a2009-05-03 18:23:30 +00001568
reed@android.com8a1c16f2008-12-17 15:59:43 +00001569 if (fPixelRef) {
djsollen@google.com5370cd92012-03-28 20:47:01 +00001570 if (fPixelRef->getFactory()) {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001571 buffer.writeInt(SERIALIZE_PIXELTYPE_REF_DATA);
scroggo@google.come5f48242013-02-25 21:47:41 +00001572 buffer.writeUInt(SkToU32(fPixelRefOffset));
djsollen@google.com5370cd92012-03-28 20:47:01 +00001573 buffer.writeFlattenable(fPixelRef);
1574 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001575 }
1576 // if we get here, we can't record the pixels
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001577 buffer.writeInt(SERIALIZE_PIXELTYPE_NONE);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001578 } else {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001579 buffer.writeInt(SERIALIZE_PIXELTYPE_NONE);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001580 }
1581}
1582
1583void SkBitmap::unflatten(SkFlattenableReadBuffer& buffer) {
1584 this->reset();
weita@google.comf9ab99a2009-05-03 18:23:30 +00001585
reed@android.com8a1c16f2008-12-17 15:59:43 +00001586 int width = buffer.readInt();
1587 int height = buffer.readInt();
1588 int rowBytes = buffer.readInt();
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001589 int config = buffer.readInt();
weita@google.comf9ab99a2009-05-03 18:23:30 +00001590
reed@android.com8a1c16f2008-12-17 15:59:43 +00001591 this->setConfig((Config)config, width, height, rowBytes);
1592 this->setIsOpaque(buffer.readBool());
weita@google.comf9ab99a2009-05-03 18:23:30 +00001593
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001594 int reftype = buffer.readInt();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001595 switch (reftype) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001596 case SERIALIZE_PIXELTYPE_REF_DATA: {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001597 size_t offset = buffer.readUInt();
1598 SkPixelRef* pr = buffer.readFlattenableT<SkPixelRef>();
reed@google.com82065d62011-02-07 15:30:46 +00001599 SkSafeUnref(this->setPixelRef(pr, offset));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001600 break;
1601 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001602 case SERIALIZE_PIXELTYPE_NONE:
1603 break;
1604 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +00001605 SkDEBUGFAIL("unrecognized pixeltype in serialized data");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001606 sk_throw();
1607 }
1608}
1609
1610///////////////////////////////////////////////////////////////////////////////
1611
1612SkBitmap::RLEPixels::RLEPixels(int width, int height) {
1613 fHeight = height;
1614 fYPtrs = (uint8_t**)sk_malloc_throw(height * sizeof(uint8_t*));
reed@android.com4516f472009-06-29 16:25:36 +00001615 sk_bzero(fYPtrs, height * sizeof(uint8_t*));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001616}
1617
1618SkBitmap::RLEPixels::~RLEPixels() {
1619 sk_free(fYPtrs);
1620}
1621
1622///////////////////////////////////////////////////////////////////////////////
1623
1624#ifdef SK_DEBUG
1625void SkBitmap::validate() const {
1626 SkASSERT(fConfig < kConfigCount);
1627 SkASSERT(fRowBytes >= (unsigned)ComputeRowBytes((Config)fConfig, fWidth));
djsollen@google.com21830d92012-08-07 19:49:41 +00001628 SkASSERT(fFlags <= (kImageIsOpaque_Flag | kImageIsVolatile_Flag | kImageIsImmutable_Flag));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001629 SkASSERT(fPixelLockCount >= 0);
1630 SkASSERT(NULL == fColorTable || (unsigned)fColorTable->getRefCnt() < 10000);
1631 SkASSERT((uint8_t)ComputeBytesPerPixel((Config)fConfig) == fBytesPerPixel);
1632
1633#if 0 // these asserts are not thread-correct, so disable for now
1634 if (fPixelRef) {
1635 if (fPixelLockCount > 0) {
reed@google.comff0da4f2012-05-17 13:14:52 +00001636 SkASSERT(fPixelRef->isLocked());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001637 } else {
1638 SkASSERT(NULL == fPixels);
1639 SkASSERT(NULL == fColorTable);
1640 }
1641 }
1642#endif
1643}
1644#endif
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001645
1646#ifdef SK_DEVELOPER
1647void SkBitmap::toString(SkString* str) const {
1648
1649 static const char* gConfigNames[kConfigCount] = {
1650 "NONE", "A1", "A8", "INDEX8", "565", "4444", "8888", "RLE"
1651 };
1652
1653 str->appendf("bitmap: ((%d, %d) %s", this->width(), this->height(),
1654 gConfigNames[this->config()]);
1655
1656 str->append(" (");
1657 if (this->isOpaque()) {
1658 str->append("opaque");
1659 } else {
1660 str->append("transparent");
1661 }
1662 if (this->isImmutable()) {
1663 str->append(", immutable");
1664 } else {
1665 str->append(", not-immutable");
1666 }
1667 str->append(")");
1668
1669 SkPixelRef* pr = this->pixelRef();
1670 if (NULL == pr) {
1671 // show null or the explicit pixel address (rare)
1672 str->appendf(" pixels:%p", this->getPixels());
1673 } else {
1674 const char* uri = pr->getURI();
1675 if (NULL != uri) {
1676 str->appendf(" uri:\"%s\"", uri);
1677 } else {
1678 str->appendf(" pixelref:%p", pr);
1679 }
1680 }
1681
1682 str->append(")");
1683}
1684#endif