blob: e308b88b4252ac13bcd09c0a7643d557bd4767a3 [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:
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000665 SkASSERT(false);
666 return 0;
667 }
668 SkASSERT(false); // Not reached.
669 return 0;
670}
671
reed@google.com2a7579d2012-11-07 18:30:18 +0000672bool SkBitmap::ComputeIsOpaque(const SkBitmap& bm) {
673 SkAutoLockPixels alp(bm);
674 if (!bm.getPixels()) {
675 return false;
676 }
677
678 const int height = bm.height();
679 const int width = bm.width();
680
681 switch (bm.config()) {
682 case SkBitmap::kA1_Config: {
683 // TODO
684 } break;
685 case SkBitmap::kA8_Config: {
686 unsigned a = 0xFF;
687 for (int y = 0; y < height; ++y) {
688 const uint8_t* row = bm.getAddr8(0, y);
689 for (int x = 0; x < width; ++x) {
690 a &= row[x];
691 }
692 if (0xFF != a) {
693 return false;
694 }
695 }
696 return true;
697 } break;
698 case kRLE_Index8_Config:
699 case SkBitmap::kIndex8_Config: {
700 SkAutoLockColors alc(bm);
701 const SkPMColor* table = alc.colors();
702 if (!table) {
703 return false;
704 }
reed@google.com140d7282013-01-07 20:25:04 +0000705 SkPMColor c = (SkPMColor)~0;
reed@google.com2a7579d2012-11-07 18:30:18 +0000706 for (int i = bm.getColorTable()->count() - 1; i >= 0; --i) {
707 c &= table[i];
708 }
709 return 0xFF == SkGetPackedA32(c);
710 } break;
711 case SkBitmap::kRGB_565_Config:
712 return true;
713 break;
714 case SkBitmap::kARGB_4444_Config: {
715 unsigned c = 0xFFFF;
716 for (int y = 0; y < height; ++y) {
717 const SkPMColor16* row = bm.getAddr16(0, y);
718 for (int x = 0; x < width; ++x) {
719 c &= row[x];
720 }
721 if (0xF != SkGetPackedA4444(c)) {
722 return false;
723 }
724 }
725 return true;
726 } break;
727 case SkBitmap::kARGB_8888_Config: {
reed@google.com140d7282013-01-07 20:25:04 +0000728 SkPMColor c = (SkPMColor)~0;
reed@google.com2a7579d2012-11-07 18:30:18 +0000729 for (int y = 0; y < height; ++y) {
730 const SkPMColor* row = bm.getAddr32(0, y);
731 for (int x = 0; x < width; ++x) {
732 c &= row[x];
733 }
734 if (0xFF != SkGetPackedA32(c)) {
735 return false;
736 }
737 }
738 return true;
739 }
740 default:
741 break;
742 }
743 return false;
744}
745
746
reed@android.com8a1c16f2008-12-17 15:59:43 +0000747///////////////////////////////////////////////////////////////////////////////
748///////////////////////////////////////////////////////////////////////////////
749
reed@google.com45f746f2013-06-21 19:51:31 +0000750static uint16_t pack_8888_to_4444(unsigned a, unsigned r, unsigned g, unsigned b) {
751 unsigned pixel = (SkA32To4444(a) << SK_A4444_SHIFT) |
752 (SkR32To4444(r) << SK_R4444_SHIFT) |
753 (SkG32To4444(g) << SK_G4444_SHIFT) |
754 (SkB32To4444(b) << SK_B4444_SHIFT);
755 return SkToU16(pixel);
756}
757
reed@android.com8a1c16f2008-12-17 15:59:43 +0000758void SkBitmap::eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const {
759 SkDEBUGCODE(this->validate();)
760
761 if (0 == fWidth || 0 == fHeight ||
762 kNo_Config == fConfig || kIndex8_Config == fConfig) {
763 return;
764 }
765
766 SkAutoLockPixels alp(*this);
767 // perform this check after the lock call
768 if (!this->readyToDraw()) {
769 return;
770 }
771
772 int height = fHeight;
773 const int width = fWidth;
774 const int rowBytes = fRowBytes;
775
776 // make rgb premultiplied
777 if (255 != a) {
778 r = SkAlphaMul(r, a);
779 g = SkAlphaMul(g, a);
780 b = SkAlphaMul(b, a);
781 }
782
783 switch (fConfig) {
784 case kA1_Config: {
785 uint8_t* p = (uint8_t*)fPixels;
786 const int count = (width + 7) >> 3;
787 a = (a >> 7) ? 0xFF : 0;
788 SkASSERT(count <= rowBytes);
789 while (--height >= 0) {
790 memset(p, a, count);
791 p += rowBytes;
792 }
793 break;
794 }
795 case kA8_Config: {
796 uint8_t* p = (uint8_t*)fPixels;
797 while (--height >= 0) {
798 memset(p, a, width);
799 p += rowBytes;
800 }
801 break;
802 }
reed@google.com45f746f2013-06-21 19:51:31 +0000803 case kARGB_4444_Config:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000804 case kRGB_565_Config: {
805 uint16_t* p = (uint16_t*)fPixels;
reed@google.com45f746f2013-06-21 19:51:31 +0000806 uint16_t v;
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000807
reed@google.com45f746f2013-06-21 19:51:31 +0000808 if (kARGB_4444_Config == fConfig) {
809 v = pack_8888_to_4444(a, r, g, b);
810 } else {
811 v = SkPackRGB16(r >> (8 - SK_R16_BITS),
812 g >> (8 - SK_G16_BITS),
813 b >> (8 - SK_B16_BITS));
814 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000815 while (--height >= 0) {
816 sk_memset16(p, v, width);
817 p = (uint16_t*)((char*)p + rowBytes);
818 }
819 break;
820 }
821 case kARGB_8888_Config: {
822 uint32_t* p = (uint32_t*)fPixels;
823 uint32_t v = SkPackARGB32(a, r, g, b);
824
825 while (--height >= 0) {
826 sk_memset32(p, v, width);
827 p = (uint32_t*)((char*)p + rowBytes);
828 }
829 break;
830 }
831 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000832
reed@android.com8a1c16f2008-12-17 15:59:43 +0000833 this->notifyPixelsChanged();
834}
835
836//////////////////////////////////////////////////////////////////////////////////////
837//////////////////////////////////////////////////////////////////////////////////////
838
839#define SUB_OFFSET_FAILURE ((size_t)-1)
840
scroggo@google.coma2a31922012-12-07 19:14:45 +0000841/**
842 * Based on the Config and rowBytes() of bm, return the offset into an SkPixelRef of the pixel at
843 * (x, y).
844 * Note that the SkPixelRef does not need to be set yet. deepCopyTo takes advantage of this fact.
845 * Also note that (x, y) may be outside the range of (0 - width(), 0 - height()), so long as it is
846 * within the bounds of the SkPixelRef being used.
847 */
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000848static size_t get_sub_offset(const SkBitmap& bm, int x, int y) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000849 switch (bm.getConfig()) {
850 case SkBitmap::kA8_Config:
851 case SkBitmap:: kIndex8_Config:
852 // x is fine as is for the calculation
853 break;
854
855 case SkBitmap::kRGB_565_Config:
856 case SkBitmap::kARGB_4444_Config:
857 x <<= 1;
858 break;
859
860 case SkBitmap::kARGB_8888_Config:
861 x <<= 2;
862 break;
863
864 case SkBitmap::kNo_Config:
865 case SkBitmap::kA1_Config:
866 default:
867 return SUB_OFFSET_FAILURE;
868 }
869 return y * bm.rowBytes() + x;
870}
871
scroggo@google.coma2a31922012-12-07 19:14:45 +0000872/**
873 * Using the pixelRefOffset(), rowBytes(), and Config of bm, determine the (x, y) coordinate of the
874 * upper left corner of bm relative to its SkPixelRef.
875 * x and y must be non-NULL.
876 */
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000877bool get_upper_left_from_offset(SkBitmap::Config config, size_t offset, size_t rowBytes,
scroggo@google.com61d6c9e2013-05-21 20:38:40 +0000878 int32_t* x, int32_t* y);
879bool get_upper_left_from_offset(SkBitmap::Config config, size_t offset, size_t rowBytes,
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000880 int32_t* x, int32_t* y) {
scroggo@google.coma2a31922012-12-07 19:14:45 +0000881 SkASSERT(x != NULL && y != NULL);
scroggo@google.coma2a31922012-12-07 19:14:45 +0000882 if (0 == offset) {
883 *x = *y = 0;
884 return true;
885 }
886 // Use integer division to find the correct y position.
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000887 *y = SkToS32(offset / rowBytes);
888 // The remainder will be the x position, after we reverse get_sub_offset.
889 *x = SkToS32(offset % rowBytes);
890 switch (config) {
scroggo@google.coma2a31922012-12-07 19:14:45 +0000891 case SkBitmap::kA8_Config:
892 // Fall through.
893 case SkBitmap::kIndex8_Config:
894 // x is unmodified
895 break;
896
897 case SkBitmap::kRGB_565_Config:
898 // Fall through.
899 case SkBitmap::kARGB_4444_Config:
900 *x >>= 1;
901 break;
902
903 case SkBitmap::kARGB_8888_Config:
904 *x >>= 2;
905 break;
906
907 case SkBitmap::kNo_Config:
908 // Fall through.
909 case SkBitmap::kA1_Config:
910 // Fall through.
911 default:
912 return false;
913 }
914 return true;
915}
916
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000917static bool get_upper_left_from_offset(const SkBitmap& bm, int32_t* x, int32_t* y) {
918 return get_upper_left_from_offset(bm.config(), bm.pixelRefOffset(), bm.rowBytes(), x, y);
919}
920
reed@android.com8a1c16f2008-12-17 15:59:43 +0000921bool SkBitmap::extractSubset(SkBitmap* result, const SkIRect& subset) const {
922 SkDEBUGCODE(this->validate();)
923
djsollen@google.comc84b8332012-07-27 13:41:44 +0000924 if (NULL == result || NULL == fPixelRef) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000925 return false; // no src pixels
926 }
927
928 SkIRect srcRect, r;
929 srcRect.set(0, 0, this->width(), this->height());
930 if (!r.intersect(srcRect, subset)) {
931 return false; // r is empty (i.e. no intersection)
932 }
933
scroggo@google.coma2a31922012-12-07 19:14:45 +0000934 if (fPixelRef->getTexture() != NULL) {
935 // Do a deep copy
936 SkPixelRef* pixelRef = fPixelRef->deepCopy(this->config(), &subset);
937 if (pixelRef != NULL) {
938 SkBitmap dst;
939 dst.setConfig(this->config(), subset.width(), subset.height());
940 dst.setIsVolatile(this->isVolatile());
941 dst.setIsOpaque(this->isOpaque());
942 dst.setPixelRef(pixelRef)->unref();
943 SkDEBUGCODE(dst.validate());
944 result->swap(dst);
945 return true;
946 }
947 }
948
reed@android.com8a1c16f2008-12-17 15:59:43 +0000949 if (kRLE_Index8_Config == fConfig) {
950 SkAutoLockPixels alp(*this);
951 // don't call readyToDraw(), since we can operate w/o a colortable
952 // at this stage
953 if (this->getPixels() == NULL) {
954 return false;
955 }
956 SkBitmap bm;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000957
reed@android.com8a1c16f2008-12-17 15:59:43 +0000958 bm.setConfig(kIndex8_Config, r.width(), r.height());
959 bm.allocPixels(this->getColorTable());
960 if (NULL == bm.getPixels()) {
961 return false;
962 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000963
reed@android.com8a1c16f2008-12-17 15:59:43 +0000964 const RLEPixels* rle = (const RLEPixels*)this->getPixels();
965 uint8_t* dst = bm.getAddr8(0, 0);
966 const int width = bm.width();
scroggo@google.come5f48242013-02-25 21:47:41 +0000967 const size_t rowBytes = bm.rowBytes();
weita@google.comf9ab99a2009-05-03 18:23:30 +0000968
reed@android.com8a1c16f2008-12-17 15:59:43 +0000969 for (int y = r.fTop; y < r.fBottom; y++) {
970 SkPackBits::Unpack8(dst, r.fLeft, width, rle->packedAtY(y));
971 dst += rowBytes;
972 }
973 result->swap(bm);
974 return true;
975 }
976
scroggo@google.coma2a31922012-12-07 19:14:45 +0000977 // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have
978 // exited above.
979 SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width()));
980 SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height()));
981
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000982 size_t offset = get_sub_offset(*this, r.fLeft, r.fTop);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000983 if (SUB_OFFSET_FAILURE == offset) {
984 return false; // config not supported
985 }
986
987 SkBitmap dst;
988 dst.setConfig(this->config(), r.width(), r.height(), this->rowBytes());
skyostil@google.com0eb75762012-01-16 10:45:53 +0000989 dst.setIsVolatile(this->isVolatile());
reed@google.com04685d22012-10-15 13:45:40 +0000990 dst.setIsOpaque(this->isOpaque());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000991
992 if (fPixelRef) {
993 // share the pixelref with a custom offset
994 dst.setPixelRef(fPixelRef, fPixelRefOffset + offset);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000995 }
996 SkDEBUGCODE(dst.validate();)
997
998 // we know we're good, so commit to result
999 result->swap(dst);
1000 return true;
1001}
1002
1003///////////////////////////////////////////////////////////////////////////////
1004
1005#include "SkCanvas.h"
1006#include "SkPaint.h"
1007
reed@android.comfbaa88d2009-05-06 17:44:34 +00001008bool SkBitmap::canCopyTo(Config dstConfig) const {
1009 if (this->getConfig() == kNo_Config) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001010 return false;
1011 }
1012
reed@android.comfbaa88d2009-05-06 17:44:34 +00001013 bool sameConfigs = (this->config() == dstConfig);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001014 switch (dstConfig) {
1015 case kA8_Config:
reed@android.com8a1c16f2008-12-17 15:59:43 +00001016 case kRGB_565_Config:
1017 case kARGB_8888_Config:
1018 break;
weita@google.comf9ab99a2009-05-03 18:23:30 +00001019 case kA1_Config:
1020 case kIndex8_Config:
reed@google.com6ba45722013-06-21 18:30:53 +00001021 case kARGB_4444_Config:
weita@google.comf9ab99a2009-05-03 18:23:30 +00001022 if (!sameConfigs) {
1023 return false;
1024 }
1025 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001026 default:
1027 return false;
1028 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001029
1030 // do not copy src if srcConfig == kA1_Config while dstConfig != kA1_Config
1031 if (this->getConfig() == kA1_Config && !sameConfigs) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001032 return false;
1033 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001034
reed@android.comfbaa88d2009-05-06 17:44:34 +00001035 return true;
1036}
1037
1038bool SkBitmap::copyTo(SkBitmap* dst, Config dstConfig, Allocator* alloc) const {
1039 if (!this->canCopyTo(dstConfig)) {
1040 return false;
1041 }
1042
reed@google.com50dfa012011-04-01 19:05:36 +00001043 // if we have a texture, first get those pixels
1044 SkBitmap tmpSrc;
1045 const SkBitmap* src = this;
1046
scroggo@google.coma2a31922012-12-07 19:14:45 +00001047 if (fPixelRef) {
1048 SkIRect subset;
scroggo@google.com1b1bcc32013-05-21 20:31:23 +00001049 if (get_upper_left_from_offset(*this, &subset.fLeft, &subset.fTop)) {
scroggo@google.coma2a31922012-12-07 19:14:45 +00001050 subset.fRight = subset.fLeft + fWidth;
1051 subset.fBottom = subset.fTop + fHeight;
1052 if (fPixelRef->readPixels(&tmpSrc, &subset)) {
1053 SkASSERT(tmpSrc.width() == this->width());
1054 SkASSERT(tmpSrc.height() == this->height());
reed@google.com50dfa012011-04-01 19:05:36 +00001055
scroggo@google.coma2a31922012-12-07 19:14:45 +00001056 // did we get lucky and we can just return tmpSrc?
1057 if (tmpSrc.config() == dstConfig && NULL == alloc) {
1058 dst->swap(tmpSrc);
1059 if (dst->pixelRef() && this->config() == dstConfig) {
1060 dst->pixelRef()->fGenerationID = fPixelRef->getGenerationID();
1061 }
1062 return true;
1063 }
1064
1065 // fall through to the raster case
1066 src = &tmpSrc;
scroggo@google.comd5764e82012-08-22 15:00:05 +00001067 }
reed@google.com50dfa012011-04-01 19:05:36 +00001068 }
reed@android.comfbaa88d2009-05-06 17:44:34 +00001069 }
reed@android.com311c82d2009-05-05 23:13:23 +00001070
reed@google.com50dfa012011-04-01 19:05:36 +00001071 // we lock this now, since we may need its colortable
1072 SkAutoLockPixels srclock(*src);
1073 if (!src->readyToDraw()) {
1074 return false;
1075 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001076
reed@google.com50dfa012011-04-01 19:05:36 +00001077 SkBitmap tmpDst;
1078 tmpDst.setConfig(dstConfig, src->width(), src->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001079
weita@google.comf9ab99a2009-05-03 18:23:30 +00001080 // allocate colortable if srcConfig == kIndex8_Config
1081 SkColorTable* ctable = (dstConfig == kIndex8_Config) ?
reed@google.com50dfa012011-04-01 19:05:36 +00001082 new SkColorTable(*src->getColorTable()) : NULL;
weita@google.comf9ab99a2009-05-03 18:23:30 +00001083 SkAutoUnref au(ctable);
reed@google.com50dfa012011-04-01 19:05:36 +00001084 if (!tmpDst.allocPixels(alloc, ctable)) {
weita@google.comf9ab99a2009-05-03 18:23:30 +00001085 return false;
1086 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001087
reed@google.com50dfa012011-04-01 19:05:36 +00001088 if (!tmpDst.readyToDraw()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001089 // allocator/lock failed
1090 return false;
1091 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001092
reed@android.comfbaa88d2009-05-06 17:44:34 +00001093 /* do memcpy for the same configs cases, else use drawing
weita@google.comf9ab99a2009-05-03 18:23:30 +00001094 */
reed@google.com50dfa012011-04-01 19:05:36 +00001095 if (src->config() == dstConfig) {
1096 if (tmpDst.getSize() == src->getSize()) {
1097 memcpy(tmpDst.getPixels(), src->getPixels(), src->getSafeSize());
scroggo@google.comd5764e82012-08-22 15:00:05 +00001098 SkPixelRef* pixelRef = tmpDst.pixelRef();
1099 if (pixelRef != NULL) {
1100 pixelRef->fGenerationID = this->getGenerationID();
1101 }
reed@android.com311c82d2009-05-05 23:13:23 +00001102 } else {
reed@google.com50dfa012011-04-01 19:05:36 +00001103 const char* srcP = reinterpret_cast<const char*>(src->getPixels());
1104 char* dstP = reinterpret_cast<char*>(tmpDst.getPixels());
reed@android.com311c82d2009-05-05 23:13:23 +00001105 // to be sure we don't read too much, only copy our logical pixels
reed@google.com50dfa012011-04-01 19:05:36 +00001106 size_t bytesToCopy = tmpDst.width() * tmpDst.bytesPerPixel();
1107 for (int y = 0; y < tmpDst.height(); y++) {
reed@android.com311c82d2009-05-05 23:13:23 +00001108 memcpy(dstP, srcP, bytesToCopy);
reed@google.com50dfa012011-04-01 19:05:36 +00001109 srcP += src->rowBytes();
1110 dstP += tmpDst.rowBytes();
reed@android.com311c82d2009-05-05 23:13:23 +00001111 }
1112 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001113 } else {
1114 // if the src has alpha, we have to clear the dst first
reed@google.com50dfa012011-04-01 19:05:36 +00001115 if (!src->isOpaque()) {
junov@google.comdbfac8a2012-12-06 21:47:40 +00001116 tmpDst.eraseColor(SK_ColorTRANSPARENT);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001117 }
1118
reed@google.com50dfa012011-04-01 19:05:36 +00001119 SkCanvas canvas(tmpDst);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001120 SkPaint paint;
1121
1122 paint.setDither(true);
reed@google.com50dfa012011-04-01 19:05:36 +00001123 canvas.drawBitmap(*src, 0, 0, &paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001124 }
1125
reed@google.com50dfa012011-04-01 19:05:36 +00001126 tmpDst.setIsOpaque(src->isOpaque());
reed@android.comcafc9f92009-08-22 03:44:57 +00001127
reed@google.com50dfa012011-04-01 19:05:36 +00001128 dst->swap(tmpDst);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001129 return true;
1130}
1131
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001132bool SkBitmap::deepCopyTo(SkBitmap* dst, Config dstConfig) const {
1133 if (!this->canCopyTo(dstConfig)) {
1134 return false;
1135 }
1136
1137 // If we have a PixelRef, and it supports deep copy, use it.
1138 // Currently supported only by texture-backed bitmaps.
1139 if (fPixelRef) {
1140 SkPixelRef* pixelRef = fPixelRef->deepCopy(dstConfig);
1141 if (pixelRef) {
scroggo@google.coma2a31922012-12-07 19:14:45 +00001142 uint32_t rowBytes;
scroggo@google.comd5764e82012-08-22 15:00:05 +00001143 if (dstConfig == fConfig) {
1144 pixelRef->fGenerationID = fPixelRef->getGenerationID();
scroggo@google.coma2a31922012-12-07 19:14:45 +00001145 // Use the same rowBytes as the original.
1146 rowBytes = fRowBytes;
1147 } else {
1148 // With the new config, an appropriate fRowBytes will be computed by setConfig.
1149 rowBytes = 0;
scroggo@google.comd5764e82012-08-22 15:00:05 +00001150 }
scroggo@google.coma2a31922012-12-07 19:14:45 +00001151 dst->setConfig(dstConfig, fWidth, fHeight, rowBytes);
1152
1153 size_t pixelRefOffset;
1154 if (0 == fPixelRefOffset || dstConfig == fConfig) {
1155 // Use the same offset as the original.
1156 pixelRefOffset = fPixelRefOffset;
1157 } else {
1158 // Find the correct offset in the new config. This needs to be done after calling
1159 // setConfig so dst's fConfig and fRowBytes have been set properly.
scroggo@google.come5f48242013-02-25 21:47:41 +00001160 int32_t x, y;
scroggo@google.com1b1bcc32013-05-21 20:31:23 +00001161 if (!get_upper_left_from_offset(*this, &x, &y)) {
scroggo@google.coma2a31922012-12-07 19:14:45 +00001162 return false;
1163 }
scroggo@google.com1b1bcc32013-05-21 20:31:23 +00001164 pixelRefOffset = get_sub_offset(*dst, x, y);
scroggo@google.coma2a31922012-12-07 19:14:45 +00001165 if (SUB_OFFSET_FAILURE == pixelRefOffset) {
1166 return false;
1167 }
1168 }
1169 dst->setPixelRef(pixelRef, pixelRefOffset)->unref();
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001170 return true;
1171 }
1172 }
1173
1174 if (this->getTexture()) {
1175 return false;
1176 } else {
1177 return this->copyTo(dst, dstConfig, NULL);
1178 }
1179}
1180
reed@android.com8a1c16f2008-12-17 15:59:43 +00001181///////////////////////////////////////////////////////////////////////////////
1182///////////////////////////////////////////////////////////////////////////////
1183
1184static void downsampleby2_proc32(SkBitmap* dst, int x, int y,
1185 const SkBitmap& src) {
1186 x <<= 1;
1187 y <<= 1;
1188 const SkPMColor* p = src.getAddr32(x, y);
reed@android.com829c83c2009-06-08 12:05:31 +00001189 const SkPMColor* baseP = p;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001190 SkPMColor c, ag, rb;
1191
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
reed@android.com829c83c2009-06-08 12:05:31 +00001198 p = baseP;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001199 if (y < src.height() - 1) {
reed@android.com829c83c2009-06-08 12:05:31 +00001200 p += src.rowBytes() >> 2;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001201 }
1202 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
1203 if (x < src.width() - 1) {
1204 p += 1;
1205 }
1206 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
1207
1208 *dst->getAddr32(x >> 1, y >> 1) =
1209 ((rb >> 2) & 0xFF00FF) | ((ag << 6) & 0xFF00FF00);
1210}
1211
1212static inline uint32_t expand16(U16CPU c) {
1213 return (c & ~SK_G16_MASK_IN_PLACE) | ((c & SK_G16_MASK_IN_PLACE) << 16);
1214}
1215
1216// returns dirt in the top 16bits, but we don't care, since we only
1217// store the low 16bits.
1218static inline U16CPU pack16(uint32_t c) {
1219 return (c & ~SK_G16_MASK_IN_PLACE) | ((c >> 16) & SK_G16_MASK_IN_PLACE);
1220}
1221
1222static void downsampleby2_proc16(SkBitmap* dst, int x, int y,
1223 const SkBitmap& src) {
1224 x <<= 1;
1225 y <<= 1;
1226 const uint16_t* p = src.getAddr16(x, y);
reed@android.com829c83c2009-06-08 12:05:31 +00001227 const uint16_t* baseP = p;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001228 SkPMColor c;
weita@google.comf9ab99a2009-05-03 18:23:30 +00001229
reed@android.com8a1c16f2008-12-17 15:59:43 +00001230 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.com829c83c2009-06-08 12:05:31 +00001236 p = baseP;
1237 if (y < src.height() - 1) {
1238 p += src.rowBytes() >> 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001239 }
1240 c += expand16(*p);
reed@android.com829c83c2009-06-08 12:05:31 +00001241 if (x < src.width() - 1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001242 p += 1;
1243 }
1244 c += expand16(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001245
reed@android.com8a1c16f2008-12-17 15:59:43 +00001246 *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)pack16(c >> 2);
1247}
1248
1249static uint32_t expand4444(U16CPU c) {
1250 return (c & 0xF0F) | ((c & ~0xF0F) << 12);
1251}
1252
1253static U16CPU collaps4444(uint32_t c) {
1254 return (c & 0xF0F) | ((c >> 12) & ~0xF0F);
1255}
1256
1257static void downsampleby2_proc4444(SkBitmap* dst, int x, int y,
1258 const SkBitmap& src) {
1259 x <<= 1;
1260 y <<= 1;
1261 const uint16_t* p = src.getAddr16(x, y);
reed@android.com829c83c2009-06-08 12:05:31 +00001262 const uint16_t* baseP = p;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001263 uint32_t c;
weita@google.comf9ab99a2009-05-03 18:23:30 +00001264
reed@android.com8a1c16f2008-12-17 15:59:43 +00001265 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.com829c83c2009-06-08 12:05:31 +00001271 p = baseP;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001272 if (y < src.height() - 1) {
reed@android.com829c83c2009-06-08 12:05:31 +00001273 p += src.rowBytes() >> 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001274 }
1275 c += expand4444(*p);
1276 if (x < src.width() - 1) {
1277 p += 1;
1278 }
1279 c += expand4444(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001280
reed@android.com8a1c16f2008-12-17 15:59:43 +00001281 *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)collaps4444(c >> 2);
1282}
1283
1284void SkBitmap::buildMipMap(bool forceRebuild) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001285 if (forceRebuild)
1286 this->freeMipMap();
1287 else if (fMipMap)
1288 return; // we're already built
1289
1290 SkASSERT(NULL == fMipMap);
1291
1292 void (*proc)(SkBitmap* dst, int x, int y, const SkBitmap& src);
1293
1294 const SkBitmap::Config config = this->getConfig();
1295
1296 switch (config) {
1297 case kARGB_8888_Config:
1298 proc = downsampleby2_proc32;
1299 break;
1300 case kRGB_565_Config:
1301 proc = downsampleby2_proc16;
1302 break;
1303 case kARGB_4444_Config:
1304 proc = downsampleby2_proc4444;
1305 break;
1306 case kIndex8_Config:
1307 case kA8_Config:
1308 default:
1309 return; // don't build mipmaps for these configs
1310 }
reed@android.com89bb83a2009-05-29 21:30:42 +00001311
reed@android.com149e2f62009-05-22 14:39:03 +00001312 SkAutoLockPixels alp(*this);
1313 if (!this->readyToDraw()) {
1314 return;
1315 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001316
1317 // whip through our loop to compute the exact size needed
1318 size_t size = 0;
1319 int maxLevels = 0;
1320 {
reed@android.com149e2f62009-05-22 14:39:03 +00001321 int width = this->width();
1322 int height = this->height();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001323 for (;;) {
1324 width >>= 1;
1325 height >>= 1;
1326 if (0 == width || 0 == height) {
1327 break;
1328 }
1329 size += ComputeRowBytes(config, width) * height;
1330 maxLevels += 1;
1331 }
1332 }
reed@android.com89bb83a2009-05-29 21:30:42 +00001333
reed@android.com149e2f62009-05-22 14:39:03 +00001334 // nothing to build
reed@android.com8a1c16f2008-12-17 15:59:43 +00001335 if (0 == maxLevels) {
1336 return;
1337 }
1338
reed@android.com149e2f62009-05-22 14:39:03 +00001339 SkBitmap srcBM(*this);
1340 srcBM.lockPixels();
1341 if (!srcBM.readyToDraw()) {
1342 return;
1343 }
1344
1345 MipMap* mm = MipMap::Alloc(maxLevels, size);
1346 if (NULL == mm) {
1347 return;
1348 }
1349
reed@android.com8a1c16f2008-12-17 15:59:43 +00001350 MipLevel* level = mm->levels();
1351 uint8_t* addr = (uint8_t*)mm->pixels();
reed@android.com149e2f62009-05-22 14:39:03 +00001352 int width = this->width();
1353 int height = this->height();
scroggo@google.come5f48242013-02-25 21:47:41 +00001354 uint32_t rowBytes;
reed@android.com149e2f62009-05-22 14:39:03 +00001355 SkBitmap dstBM;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001356
1357 for (int i = 0; i < maxLevels; i++) {
1358 width >>= 1;
1359 height >>= 1;
scroggo@google.come5f48242013-02-25 21:47:41 +00001360 rowBytes = SkToU32(ComputeRowBytes(config, width));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001361
1362 level[i].fPixels = addr;
reed@android.comf459a492009-03-27 12:33:50 +00001363 level[i].fWidth = width;
1364 level[i].fHeight = height;
reed@android.com49f0ff22009-03-19 21:52:42 +00001365 level[i].fRowBytes = rowBytes;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001366
1367 dstBM.setConfig(config, width, height, rowBytes);
1368 dstBM.setPixels(addr);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001369
bungeman@google.com7cf0e9e2012-07-25 16:09:10 +00001370 srcBM.lockPixels();
reed@android.com149e2f62009-05-22 14:39:03 +00001371 for (int y = 0; y < height; y++) {
1372 for (int x = 0; x < width; x++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001373 proc(&dstBM, x, y, srcBM);
1374 }
1375 }
bungeman@google.com7cf0e9e2012-07-25 16:09:10 +00001376 srcBM.unlockPixels();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001377
1378 srcBM = dstBM;
1379 addr += height * rowBytes;
1380 }
1381 SkASSERT(addr == (uint8_t*)mm->pixels() + size);
1382 fMipMap = mm;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001383}
1384
1385bool SkBitmap::hasMipMap() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001386 return fMipMap != NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001387}
1388
1389int SkBitmap::extractMipLevel(SkBitmap* dst, SkFixed sx, SkFixed sy) {
reed@android.com83f7bc32009-07-17 02:42:41 +00001390 if (NULL == fMipMap) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001391 return 0;
reed@android.com83f7bc32009-07-17 02:42:41 +00001392 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001393
reed@android.com8a1c16f2008-12-17 15:59:43 +00001394 int level = ComputeMipLevel(sx, sy) >> 16;
1395 SkASSERT(level >= 0);
1396 if (level <= 0) {
1397 return 0;
1398 }
1399
1400 if (level >= fMipMap->fLevelCount) {
1401 level = fMipMap->fLevelCount - 1;
1402 }
1403 if (dst) {
1404 const MipLevel& mip = fMipMap->levels()[level - 1];
1405 dst->setConfig((SkBitmap::Config)this->config(),
1406 mip.fWidth, mip.fHeight, mip.fRowBytes);
1407 dst->setPixels(mip.fPixels);
1408 }
1409 return level;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001410}
1411
1412SkFixed SkBitmap::ComputeMipLevel(SkFixed sx, SkFixed sy) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001413 sx = SkAbs32(sx);
1414 sy = SkAbs32(sy);
1415 if (sx < sy) {
1416 sx = sy;
1417 }
1418 if (sx < SK_Fixed1) {
1419 return 0;
1420 }
1421 int clz = SkCLZ(sx);
1422 SkASSERT(clz >= 1 && clz <= 15);
1423 return SkIntToFixed(15 - clz) + ((unsigned)(sx << (clz + 1)) >> 16);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001424}
1425
1426///////////////////////////////////////////////////////////////////////////////
1427
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +00001428static bool GetBitmapAlpha(const SkBitmap& src, uint8_t* SK_RESTRICT alpha,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001429 int alphaRowBytes) {
1430 SkASSERT(alpha != NULL);
1431 SkASSERT(alphaRowBytes >= src.width());
1432
1433 SkBitmap::Config config = src.getConfig();
1434 int w = src.width();
1435 int h = src.height();
scroggo@google.come5f48242013-02-25 21:47:41 +00001436 size_t rb = src.rowBytes();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001437
reed@android.com1cdcb512009-08-24 19:11:00 +00001438 SkAutoLockPixels alp(src);
1439 if (!src.readyToDraw()) {
1440 // zero out the alpha buffer and return
1441 while (--h >= 0) {
1442 memset(alpha, 0, w);
1443 alpha += alphaRowBytes;
1444 }
1445 return false;
1446 }
reed@google.com82065d62011-02-07 15:30:46 +00001447
reed@android.com8a1c16f2008-12-17 15:59:43 +00001448 if (SkBitmap::kA8_Config == config && !src.isOpaque()) {
1449 const uint8_t* s = src.getAddr8(0, 0);
1450 while (--h >= 0) {
1451 memcpy(alpha, s, w);
1452 s += rb;
1453 alpha += alphaRowBytes;
1454 }
1455 } else if (SkBitmap::kARGB_8888_Config == config && !src.isOpaque()) {
1456 const SkPMColor* SK_RESTRICT s = src.getAddr32(0, 0);
1457 while (--h >= 0) {
1458 for (int x = 0; x < w; x++) {
1459 alpha[x] = SkGetPackedA32(s[x]);
1460 }
1461 s = (const SkPMColor*)((const char*)s + rb);
1462 alpha += alphaRowBytes;
1463 }
1464 } else if (SkBitmap::kARGB_4444_Config == config && !src.isOpaque()) {
1465 const SkPMColor16* SK_RESTRICT s = src.getAddr16(0, 0);
1466 while (--h >= 0) {
1467 for (int x = 0; x < w; x++) {
1468 alpha[x] = SkPacked4444ToA32(s[x]);
1469 }
1470 s = (const SkPMColor16*)((const char*)s + rb);
1471 alpha += alphaRowBytes;
1472 }
1473 } else if (SkBitmap::kIndex8_Config == config && !src.isOpaque()) {
1474 SkColorTable* ct = src.getColorTable();
1475 if (ct) {
1476 const SkPMColor* SK_RESTRICT table = ct->lockColors();
1477 const uint8_t* SK_RESTRICT s = src.getAddr8(0, 0);
1478 while (--h >= 0) {
1479 for (int x = 0; x < w; x++) {
1480 alpha[x] = SkGetPackedA32(table[s[x]]);
1481 }
1482 s += rb;
1483 alpha += alphaRowBytes;
1484 }
1485 ct->unlockColors(false);
1486 }
1487 } else { // src is opaque, so just fill alpha[] with 0xFF
1488 memset(alpha, 0xFF, h * alphaRowBytes);
1489 }
reed@android.com1cdcb512009-08-24 19:11:00 +00001490 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001491}
1492
1493#include "SkPaint.h"
1494#include "SkMaskFilter.h"
1495#include "SkMatrix.h"
1496
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001497bool SkBitmap::extractAlpha(SkBitmap* dst, const SkPaint* paint,
djsollen@google.com57f49692011-02-23 20:46:31 +00001498 Allocator *allocator, SkIPoint* offset) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001499 SkDEBUGCODE(this->validate();)
1500
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001501 SkBitmap tmpBitmap;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001502 SkMatrix identity;
1503 SkMask srcM, dstM;
1504
1505 srcM.fBounds.set(0, 0, this->width(), this->height());
1506 srcM.fRowBytes = SkAlign4(this->width());
1507 srcM.fFormat = SkMask::kA8_Format;
1508
1509 SkMaskFilter* filter = paint ? paint->getMaskFilter() : NULL;
1510
1511 // compute our (larger?) dst bounds if we have a filter
1512 if (NULL != filter) {
1513 identity.reset();
1514 srcM.fImage = NULL;
1515 if (!filter->filterMask(&dstM, srcM, identity, NULL)) {
1516 goto NO_FILTER_CASE;
1517 }
1518 dstM.fRowBytes = SkAlign4(dstM.fBounds.width());
1519 } else {
1520 NO_FILTER_CASE:
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001521 tmpBitmap.setConfig(SkBitmap::kA8_Config, this->width(), this->height(),
reed@android.com8a1c16f2008-12-17 15:59:43 +00001522 srcM.fRowBytes);
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001523 if (!tmpBitmap.allocPixels(allocator, NULL)) {
1524 // Allocation of pixels for alpha bitmap failed.
1525 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
1526 tmpBitmap.width(), tmpBitmap.height());
1527 return false;
1528 }
1529 GetBitmapAlpha(*this, tmpBitmap.getAddr8(0, 0), srcM.fRowBytes);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001530 if (offset) {
1531 offset->set(0, 0);
1532 }
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001533 tmpBitmap.swap(*dst);
1534 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001535 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001536 srcM.fImage = SkMask::AllocImage(srcM.computeImageSize());
1537 SkAutoMaskFreeImage srcCleanup(srcM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001538
1539 GetBitmapAlpha(*this, srcM.fImage, srcM.fRowBytes);
1540 if (!filter->filterMask(&dstM, srcM, identity, NULL)) {
1541 goto NO_FILTER_CASE;
1542 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001543 SkAutoMaskFreeImage dstCleanup(dstM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001544
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001545 tmpBitmap.setConfig(SkBitmap::kA8_Config, dstM.fBounds.width(),
reed@android.com8a1c16f2008-12-17 15:59:43 +00001546 dstM.fBounds.height(), dstM.fRowBytes);
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001547 if (!tmpBitmap.allocPixels(allocator, NULL)) {
1548 // Allocation of pixels for alpha bitmap failed.
1549 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
1550 tmpBitmap.width(), tmpBitmap.height());
1551 return false;
1552 }
1553 memcpy(tmpBitmap.getPixels(), dstM.fImage, dstM.computeImageSize());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001554 if (offset) {
1555 offset->set(dstM.fBounds.fLeft, dstM.fBounds.fTop);
1556 }
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001557 SkDEBUGCODE(tmpBitmap.validate();)
1558
1559 tmpBitmap.swap(*dst);
1560 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001561}
1562
1563///////////////////////////////////////////////////////////////////////////////
1564
1565enum {
1566 SERIALIZE_PIXELTYPE_NONE,
djsollen@google.com21830d92012-08-07 19:49:41 +00001567 SERIALIZE_PIXELTYPE_REF_DATA
reed@android.com8a1c16f2008-12-17 15:59:43 +00001568};
1569
reed@android.com8a1c16f2008-12-17 15:59:43 +00001570void SkBitmap::flatten(SkFlattenableWriteBuffer& buffer) const {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001571 buffer.writeInt(fWidth);
1572 buffer.writeInt(fHeight);
1573 buffer.writeInt(fRowBytes);
1574 buffer.writeInt(fConfig);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001575 buffer.writeBool(this->isOpaque());
weita@google.comf9ab99a2009-05-03 18:23:30 +00001576
reed@android.com8a1c16f2008-12-17 15:59:43 +00001577 if (fPixelRef) {
djsollen@google.com5370cd92012-03-28 20:47:01 +00001578 if (fPixelRef->getFactory()) {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001579 buffer.writeInt(SERIALIZE_PIXELTYPE_REF_DATA);
scroggo@google.come5f48242013-02-25 21:47:41 +00001580 buffer.writeUInt(SkToU32(fPixelRefOffset));
djsollen@google.com5370cd92012-03-28 20:47:01 +00001581 buffer.writeFlattenable(fPixelRef);
1582 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001583 }
1584 // if we get here, we can't record the pixels
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001585 buffer.writeInt(SERIALIZE_PIXELTYPE_NONE);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001586 } else {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001587 buffer.writeInt(SERIALIZE_PIXELTYPE_NONE);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001588 }
1589}
1590
1591void SkBitmap::unflatten(SkFlattenableReadBuffer& buffer) {
1592 this->reset();
weita@google.comf9ab99a2009-05-03 18:23:30 +00001593
reed@android.com8a1c16f2008-12-17 15:59:43 +00001594 int width = buffer.readInt();
1595 int height = buffer.readInt();
1596 int rowBytes = buffer.readInt();
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001597 int config = buffer.readInt();
weita@google.comf9ab99a2009-05-03 18:23:30 +00001598
reed@android.com8a1c16f2008-12-17 15:59:43 +00001599 this->setConfig((Config)config, width, height, rowBytes);
1600 this->setIsOpaque(buffer.readBool());
weita@google.comf9ab99a2009-05-03 18:23:30 +00001601
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001602 int reftype = buffer.readInt();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001603 switch (reftype) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001604 case SERIALIZE_PIXELTYPE_REF_DATA: {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001605 size_t offset = buffer.readUInt();
1606 SkPixelRef* pr = buffer.readFlattenableT<SkPixelRef>();
reed@google.com82065d62011-02-07 15:30:46 +00001607 SkSafeUnref(this->setPixelRef(pr, offset));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001608 break;
1609 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001610 case SERIALIZE_PIXELTYPE_NONE:
1611 break;
1612 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +00001613 SkDEBUGFAIL("unrecognized pixeltype in serialized data");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001614 sk_throw();
1615 }
1616}
1617
1618///////////////////////////////////////////////////////////////////////////////
1619
1620SkBitmap::RLEPixels::RLEPixels(int width, int height) {
1621 fHeight = height;
1622 fYPtrs = (uint8_t**)sk_malloc_throw(height * sizeof(uint8_t*));
reed@android.com4516f472009-06-29 16:25:36 +00001623 sk_bzero(fYPtrs, height * sizeof(uint8_t*));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001624}
1625
1626SkBitmap::RLEPixels::~RLEPixels() {
1627 sk_free(fYPtrs);
1628}
1629
1630///////////////////////////////////////////////////////////////////////////////
1631
1632#ifdef SK_DEBUG
1633void SkBitmap::validate() const {
1634 SkASSERT(fConfig < kConfigCount);
1635 SkASSERT(fRowBytes >= (unsigned)ComputeRowBytes((Config)fConfig, fWidth));
scroggo@google.com8e990eb2013-06-14 15:55:56 +00001636 uint8_t allFlags = kImageIsOpaque_Flag | kImageIsVolatile_Flag | kImageIsImmutable_Flag;
1637#ifdef SK_BUILD_FOR_ANDROID
1638 allFlags |= kHasHardwareMipMap_Flag;
1639#endif
1640 SkASSERT(fFlags <= allFlags);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001641 SkASSERT(fPixelLockCount >= 0);
1642 SkASSERT(NULL == fColorTable || (unsigned)fColorTable->getRefCnt() < 10000);
1643 SkASSERT((uint8_t)ComputeBytesPerPixel((Config)fConfig) == fBytesPerPixel);
1644
1645#if 0 // these asserts are not thread-correct, so disable for now
1646 if (fPixelRef) {
1647 if (fPixelLockCount > 0) {
reed@google.comff0da4f2012-05-17 13:14:52 +00001648 SkASSERT(fPixelRef->isLocked());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001649 } else {
1650 SkASSERT(NULL == fPixels);
1651 SkASSERT(NULL == fColorTable);
1652 }
1653 }
1654#endif
1655}
1656#endif
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001657
1658#ifdef SK_DEVELOPER
1659void SkBitmap::toString(SkString* str) const {
1660
1661 static const char* gConfigNames[kConfigCount] = {
1662 "NONE", "A1", "A8", "INDEX8", "565", "4444", "8888", "RLE"
1663 };
1664
1665 str->appendf("bitmap: ((%d, %d) %s", this->width(), this->height(),
1666 gConfigNames[this->config()]);
1667
1668 str->append(" (");
1669 if (this->isOpaque()) {
1670 str->append("opaque");
1671 } else {
1672 str->append("transparent");
1673 }
1674 if (this->isImmutable()) {
1675 str->append(", immutable");
1676 } else {
1677 str->append(", not-immutable");
1678 }
1679 str->append(")");
1680
1681 SkPixelRef* pr = this->pixelRef();
1682 if (NULL == pr) {
1683 // show null or the explicit pixel address (rare)
1684 str->appendf(" pixels:%p", this->getPixels());
1685 } else {
1686 const char* uri = pr->getURI();
1687 if (NULL != uri) {
1688 str->appendf(" uri:\"%s\"", uri);
1689 } else {
1690 str->appendf(" pixelref:%p", pr);
1691 }
1692 }
1693
1694 str->append(")");
1695}
1696#endif