blob: 383f0efaef8415dbbba92db5157dde2092cf0d09 [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
reed@google.com45f746f2013-06-21 19:51:31 +0000751static uint16_t pack_8888_to_4444(unsigned a, unsigned r, unsigned g, unsigned b) {
752 unsigned pixel = (SkA32To4444(a) << SK_A4444_SHIFT) |
753 (SkR32To4444(r) << SK_R4444_SHIFT) |
754 (SkG32To4444(g) << SK_G4444_SHIFT) |
755 (SkB32To4444(b) << SK_B4444_SHIFT);
756 return SkToU16(pixel);
757}
758
reed@android.com8a1c16f2008-12-17 15:59:43 +0000759void SkBitmap::eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const {
760 SkDEBUGCODE(this->validate();)
761
762 if (0 == fWidth || 0 == fHeight ||
763 kNo_Config == fConfig || kIndex8_Config == fConfig) {
764 return;
765 }
766
767 SkAutoLockPixels alp(*this);
768 // perform this check after the lock call
769 if (!this->readyToDraw()) {
770 return;
771 }
772
773 int height = fHeight;
774 const int width = fWidth;
775 const int rowBytes = fRowBytes;
776
777 // make rgb premultiplied
778 if (255 != a) {
779 r = SkAlphaMul(r, a);
780 g = SkAlphaMul(g, a);
781 b = SkAlphaMul(b, a);
782 }
783
784 switch (fConfig) {
785 case kA1_Config: {
786 uint8_t* p = (uint8_t*)fPixels;
787 const int count = (width + 7) >> 3;
788 a = (a >> 7) ? 0xFF : 0;
789 SkASSERT(count <= rowBytes);
790 while (--height >= 0) {
791 memset(p, a, count);
792 p += rowBytes;
793 }
794 break;
795 }
796 case kA8_Config: {
797 uint8_t* p = (uint8_t*)fPixels;
798 while (--height >= 0) {
799 memset(p, a, width);
800 p += rowBytes;
801 }
802 break;
803 }
reed@google.com45f746f2013-06-21 19:51:31 +0000804 case kARGB_4444_Config:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000805 case kRGB_565_Config: {
806 uint16_t* p = (uint16_t*)fPixels;
reed@google.com45f746f2013-06-21 19:51:31 +0000807 uint16_t v;
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000808
reed@google.com45f746f2013-06-21 19:51:31 +0000809 if (kARGB_4444_Config == fConfig) {
810 v = pack_8888_to_4444(a, r, g, b);
811 } else {
812 v = SkPackRGB16(r >> (8 - SK_R16_BITS),
813 g >> (8 - SK_G16_BITS),
814 b >> (8 - SK_B16_BITS));
815 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000816 while (--height >= 0) {
817 sk_memset16(p, v, width);
818 p = (uint16_t*)((char*)p + rowBytes);
819 }
820 break;
821 }
822 case kARGB_8888_Config: {
823 uint32_t* p = (uint32_t*)fPixels;
824 uint32_t v = SkPackARGB32(a, r, g, b);
825
826 while (--height >= 0) {
827 sk_memset32(p, v, width);
828 p = (uint32_t*)((char*)p + rowBytes);
829 }
830 break;
831 }
832 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000833
reed@android.com8a1c16f2008-12-17 15:59:43 +0000834 this->notifyPixelsChanged();
835}
836
837//////////////////////////////////////////////////////////////////////////////////////
838//////////////////////////////////////////////////////////////////////////////////////
839
840#define SUB_OFFSET_FAILURE ((size_t)-1)
841
scroggo@google.coma2a31922012-12-07 19:14:45 +0000842/**
843 * Based on the Config and rowBytes() of bm, return the offset into an SkPixelRef of the pixel at
844 * (x, y).
845 * Note that the SkPixelRef does not need to be set yet. deepCopyTo takes advantage of this fact.
846 * Also note that (x, y) may be outside the range of (0 - width(), 0 - height()), so long as it is
847 * within the bounds of the SkPixelRef being used.
848 */
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000849static size_t get_sub_offset(const SkBitmap& bm, int x, int y) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000850 switch (bm.getConfig()) {
851 case SkBitmap::kA8_Config:
852 case SkBitmap:: kIndex8_Config:
853 // x is fine as is for the calculation
854 break;
855
856 case SkBitmap::kRGB_565_Config:
857 case SkBitmap::kARGB_4444_Config:
858 x <<= 1;
859 break;
860
861 case SkBitmap::kARGB_8888_Config:
862 x <<= 2;
863 break;
864
865 case SkBitmap::kNo_Config:
866 case SkBitmap::kA1_Config:
867 default:
868 return SUB_OFFSET_FAILURE;
869 }
870 return y * bm.rowBytes() + x;
871}
872
scroggo@google.coma2a31922012-12-07 19:14:45 +0000873/**
874 * Using the pixelRefOffset(), rowBytes(), and Config of bm, determine the (x, y) coordinate of the
875 * upper left corner of bm relative to its SkPixelRef.
876 * x and y must be non-NULL.
877 */
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000878bool get_upper_left_from_offset(SkBitmap::Config config, size_t offset, size_t rowBytes,
scroggo@google.com61d6c9e2013-05-21 20:38:40 +0000879 int32_t* x, int32_t* y);
880bool get_upper_left_from_offset(SkBitmap::Config config, size_t offset, size_t rowBytes,
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000881 int32_t* x, int32_t* y) {
scroggo@google.coma2a31922012-12-07 19:14:45 +0000882 SkASSERT(x != NULL && y != NULL);
scroggo@google.coma2a31922012-12-07 19:14:45 +0000883 if (0 == offset) {
884 *x = *y = 0;
885 return true;
886 }
887 // Use integer division to find the correct y position.
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000888 *y = SkToS32(offset / rowBytes);
889 // The remainder will be the x position, after we reverse get_sub_offset.
890 *x = SkToS32(offset % rowBytes);
891 switch (config) {
scroggo@google.coma2a31922012-12-07 19:14:45 +0000892 case SkBitmap::kA8_Config:
893 // Fall through.
894 case SkBitmap::kIndex8_Config:
895 // x is unmodified
896 break;
897
898 case SkBitmap::kRGB_565_Config:
899 // Fall through.
900 case SkBitmap::kARGB_4444_Config:
901 *x >>= 1;
902 break;
903
904 case SkBitmap::kARGB_8888_Config:
905 *x >>= 2;
906 break;
907
908 case SkBitmap::kNo_Config:
909 // Fall through.
910 case SkBitmap::kA1_Config:
911 // Fall through.
912 default:
913 return false;
914 }
915 return true;
916}
917
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000918static bool get_upper_left_from_offset(const SkBitmap& bm, int32_t* x, int32_t* y) {
919 return get_upper_left_from_offset(bm.config(), bm.pixelRefOffset(), bm.rowBytes(), x, y);
920}
921
reed@android.com8a1c16f2008-12-17 15:59:43 +0000922bool SkBitmap::extractSubset(SkBitmap* result, const SkIRect& subset) const {
923 SkDEBUGCODE(this->validate();)
924
djsollen@google.comc84b8332012-07-27 13:41:44 +0000925 if (NULL == result || NULL == fPixelRef) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000926 return false; // no src pixels
927 }
928
929 SkIRect srcRect, r;
930 srcRect.set(0, 0, this->width(), this->height());
931 if (!r.intersect(srcRect, subset)) {
932 return false; // r is empty (i.e. no intersection)
933 }
934
scroggo@google.coma2a31922012-12-07 19:14:45 +0000935 if (fPixelRef->getTexture() != NULL) {
936 // Do a deep copy
937 SkPixelRef* pixelRef = fPixelRef->deepCopy(this->config(), &subset);
938 if (pixelRef != NULL) {
939 SkBitmap dst;
940 dst.setConfig(this->config(), subset.width(), subset.height());
941 dst.setIsVolatile(this->isVolatile());
942 dst.setIsOpaque(this->isOpaque());
943 dst.setPixelRef(pixelRef)->unref();
944 SkDEBUGCODE(dst.validate());
945 result->swap(dst);
946 return true;
947 }
948 }
949
reed@android.com8a1c16f2008-12-17 15:59:43 +0000950 if (kRLE_Index8_Config == fConfig) {
951 SkAutoLockPixels alp(*this);
952 // don't call readyToDraw(), since we can operate w/o a colortable
953 // at this stage
954 if (this->getPixels() == NULL) {
955 return false;
956 }
957 SkBitmap bm;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000958
reed@android.com8a1c16f2008-12-17 15:59:43 +0000959 bm.setConfig(kIndex8_Config, r.width(), r.height());
960 bm.allocPixels(this->getColorTable());
961 if (NULL == bm.getPixels()) {
962 return false;
963 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000964
reed@android.com8a1c16f2008-12-17 15:59:43 +0000965 const RLEPixels* rle = (const RLEPixels*)this->getPixels();
966 uint8_t* dst = bm.getAddr8(0, 0);
967 const int width = bm.width();
scroggo@google.come5f48242013-02-25 21:47:41 +0000968 const size_t rowBytes = bm.rowBytes();
weita@google.comf9ab99a2009-05-03 18:23:30 +0000969
reed@android.com8a1c16f2008-12-17 15:59:43 +0000970 for (int y = r.fTop; y < r.fBottom; y++) {
971 SkPackBits::Unpack8(dst, r.fLeft, width, rle->packedAtY(y));
972 dst += rowBytes;
973 }
974 result->swap(bm);
975 return true;
976 }
977
scroggo@google.coma2a31922012-12-07 19:14:45 +0000978 // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have
979 // exited above.
980 SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width()));
981 SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height()));
982
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000983 size_t offset = get_sub_offset(*this, r.fLeft, r.fTop);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000984 if (SUB_OFFSET_FAILURE == offset) {
985 return false; // config not supported
986 }
987
988 SkBitmap dst;
989 dst.setConfig(this->config(), r.width(), r.height(), this->rowBytes());
skyostil@google.com0eb75762012-01-16 10:45:53 +0000990 dst.setIsVolatile(this->isVolatile());
reed@google.com04685d22012-10-15 13:45:40 +0000991 dst.setIsOpaque(this->isOpaque());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000992
993 if (fPixelRef) {
994 // share the pixelref with a custom offset
995 dst.setPixelRef(fPixelRef, fPixelRefOffset + offset);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000996 }
997 SkDEBUGCODE(dst.validate();)
998
999 // we know we're good, so commit to result
1000 result->swap(dst);
1001 return true;
1002}
1003
1004///////////////////////////////////////////////////////////////////////////////
1005
1006#include "SkCanvas.h"
1007#include "SkPaint.h"
1008
reed@android.comfbaa88d2009-05-06 17:44:34 +00001009bool SkBitmap::canCopyTo(Config dstConfig) const {
1010 if (this->getConfig() == kNo_Config) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001011 return false;
1012 }
1013
reed@android.comfbaa88d2009-05-06 17:44:34 +00001014 bool sameConfigs = (this->config() == dstConfig);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001015 switch (dstConfig) {
1016 case kA8_Config:
reed@android.com8a1c16f2008-12-17 15:59:43 +00001017 case kRGB_565_Config:
1018 case kARGB_8888_Config:
1019 break;
weita@google.comf9ab99a2009-05-03 18:23:30 +00001020 case kA1_Config:
1021 case kIndex8_Config:
reed@google.com6ba45722013-06-21 18:30:53 +00001022 case kARGB_4444_Config:
weita@google.comf9ab99a2009-05-03 18:23:30 +00001023 if (!sameConfigs) {
1024 return false;
1025 }
1026 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001027 default:
1028 return false;
1029 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001030
1031 // do not copy src if srcConfig == kA1_Config while dstConfig != kA1_Config
1032 if (this->getConfig() == kA1_Config && !sameConfigs) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001033 return false;
1034 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001035
reed@android.comfbaa88d2009-05-06 17:44:34 +00001036 return true;
1037}
1038
1039bool SkBitmap::copyTo(SkBitmap* dst, Config dstConfig, Allocator* alloc) const {
1040 if (!this->canCopyTo(dstConfig)) {
1041 return false;
1042 }
1043
reed@google.com50dfa012011-04-01 19:05:36 +00001044 // if we have a texture, first get those pixels
1045 SkBitmap tmpSrc;
1046 const SkBitmap* src = this;
1047
scroggo@google.coma2a31922012-12-07 19:14:45 +00001048 if (fPixelRef) {
1049 SkIRect subset;
scroggo@google.com1b1bcc32013-05-21 20:31:23 +00001050 if (get_upper_left_from_offset(*this, &subset.fLeft, &subset.fTop)) {
scroggo@google.coma2a31922012-12-07 19:14:45 +00001051 subset.fRight = subset.fLeft + fWidth;
1052 subset.fBottom = subset.fTop + fHeight;
1053 if (fPixelRef->readPixels(&tmpSrc, &subset)) {
1054 SkASSERT(tmpSrc.width() == this->width());
1055 SkASSERT(tmpSrc.height() == this->height());
reed@google.com50dfa012011-04-01 19:05:36 +00001056
scroggo@google.coma2a31922012-12-07 19:14:45 +00001057 // did we get lucky and we can just return tmpSrc?
1058 if (tmpSrc.config() == dstConfig && NULL == alloc) {
1059 dst->swap(tmpSrc);
1060 if (dst->pixelRef() && this->config() == dstConfig) {
1061 dst->pixelRef()->fGenerationID = fPixelRef->getGenerationID();
1062 }
1063 return true;
1064 }
1065
1066 // fall through to the raster case
1067 src = &tmpSrc;
scroggo@google.comd5764e82012-08-22 15:00:05 +00001068 }
reed@google.com50dfa012011-04-01 19:05:36 +00001069 }
reed@android.comfbaa88d2009-05-06 17:44:34 +00001070 }
reed@android.com311c82d2009-05-05 23:13:23 +00001071
reed@google.com50dfa012011-04-01 19:05:36 +00001072 // we lock this now, since we may need its colortable
1073 SkAutoLockPixels srclock(*src);
1074 if (!src->readyToDraw()) {
1075 return false;
1076 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001077
reed@google.com50dfa012011-04-01 19:05:36 +00001078 SkBitmap tmpDst;
1079 tmpDst.setConfig(dstConfig, src->width(), src->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001080
weita@google.comf9ab99a2009-05-03 18:23:30 +00001081 // allocate colortable if srcConfig == kIndex8_Config
1082 SkColorTable* ctable = (dstConfig == kIndex8_Config) ?
reed@google.com50dfa012011-04-01 19:05:36 +00001083 new SkColorTable(*src->getColorTable()) : NULL;
weita@google.comf9ab99a2009-05-03 18:23:30 +00001084 SkAutoUnref au(ctable);
reed@google.com50dfa012011-04-01 19:05:36 +00001085 if (!tmpDst.allocPixels(alloc, ctable)) {
weita@google.comf9ab99a2009-05-03 18:23:30 +00001086 return false;
1087 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001088
reed@google.com50dfa012011-04-01 19:05:36 +00001089 if (!tmpDst.readyToDraw()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001090 // allocator/lock failed
1091 return false;
1092 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001093
reed@android.comfbaa88d2009-05-06 17:44:34 +00001094 /* do memcpy for the same configs cases, else use drawing
weita@google.comf9ab99a2009-05-03 18:23:30 +00001095 */
reed@google.com50dfa012011-04-01 19:05:36 +00001096 if (src->config() == dstConfig) {
1097 if (tmpDst.getSize() == src->getSize()) {
1098 memcpy(tmpDst.getPixels(), src->getPixels(), src->getSafeSize());
scroggo@google.comd5764e82012-08-22 15:00:05 +00001099 SkPixelRef* pixelRef = tmpDst.pixelRef();
1100 if (pixelRef != NULL) {
1101 pixelRef->fGenerationID = this->getGenerationID();
1102 }
reed@android.com311c82d2009-05-05 23:13:23 +00001103 } else {
reed@google.com50dfa012011-04-01 19:05:36 +00001104 const char* srcP = reinterpret_cast<const char*>(src->getPixels());
1105 char* dstP = reinterpret_cast<char*>(tmpDst.getPixels());
reed@android.com311c82d2009-05-05 23:13:23 +00001106 // to be sure we don't read too much, only copy our logical pixels
reed@google.com50dfa012011-04-01 19:05:36 +00001107 size_t bytesToCopy = tmpDst.width() * tmpDst.bytesPerPixel();
1108 for (int y = 0; y < tmpDst.height(); y++) {
reed@android.com311c82d2009-05-05 23:13:23 +00001109 memcpy(dstP, srcP, bytesToCopy);
reed@google.com50dfa012011-04-01 19:05:36 +00001110 srcP += src->rowBytes();
1111 dstP += tmpDst.rowBytes();
reed@android.com311c82d2009-05-05 23:13:23 +00001112 }
1113 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001114 } else {
1115 // if the src has alpha, we have to clear the dst first
reed@google.com50dfa012011-04-01 19:05:36 +00001116 if (!src->isOpaque()) {
junov@google.comdbfac8a2012-12-06 21:47:40 +00001117 tmpDst.eraseColor(SK_ColorTRANSPARENT);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001118 }
1119
reed@google.com50dfa012011-04-01 19:05:36 +00001120 SkCanvas canvas(tmpDst);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001121 SkPaint paint;
1122
1123 paint.setDither(true);
reed@google.com50dfa012011-04-01 19:05:36 +00001124 canvas.drawBitmap(*src, 0, 0, &paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001125 }
1126
reed@google.com50dfa012011-04-01 19:05:36 +00001127 tmpDst.setIsOpaque(src->isOpaque());
reed@android.comcafc9f92009-08-22 03:44:57 +00001128
reed@google.com50dfa012011-04-01 19:05:36 +00001129 dst->swap(tmpDst);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001130 return true;
1131}
1132
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001133bool SkBitmap::deepCopyTo(SkBitmap* dst, Config dstConfig) const {
1134 if (!this->canCopyTo(dstConfig)) {
1135 return false;
1136 }
1137
1138 // If we have a PixelRef, and it supports deep copy, use it.
1139 // Currently supported only by texture-backed bitmaps.
1140 if (fPixelRef) {
1141 SkPixelRef* pixelRef = fPixelRef->deepCopy(dstConfig);
1142 if (pixelRef) {
scroggo@google.coma2a31922012-12-07 19:14:45 +00001143 uint32_t rowBytes;
scroggo@google.comd5764e82012-08-22 15:00:05 +00001144 if (dstConfig == fConfig) {
1145 pixelRef->fGenerationID = fPixelRef->getGenerationID();
scroggo@google.coma2a31922012-12-07 19:14:45 +00001146 // Use the same rowBytes as the original.
1147 rowBytes = fRowBytes;
1148 } else {
1149 // With the new config, an appropriate fRowBytes will be computed by setConfig.
1150 rowBytes = 0;
scroggo@google.comd5764e82012-08-22 15:00:05 +00001151 }
scroggo@google.coma2a31922012-12-07 19:14:45 +00001152 dst->setConfig(dstConfig, fWidth, fHeight, rowBytes);
1153
1154 size_t pixelRefOffset;
1155 if (0 == fPixelRefOffset || dstConfig == fConfig) {
1156 // Use the same offset as the original.
1157 pixelRefOffset = fPixelRefOffset;
1158 } else {
1159 // Find the correct offset in the new config. This needs to be done after calling
1160 // setConfig so dst's fConfig and fRowBytes have been set properly.
scroggo@google.come5f48242013-02-25 21:47:41 +00001161 int32_t x, y;
scroggo@google.com1b1bcc32013-05-21 20:31:23 +00001162 if (!get_upper_left_from_offset(*this, &x, &y)) {
scroggo@google.coma2a31922012-12-07 19:14:45 +00001163 return false;
1164 }
scroggo@google.com1b1bcc32013-05-21 20:31:23 +00001165 pixelRefOffset = get_sub_offset(*dst, x, y);
scroggo@google.coma2a31922012-12-07 19:14:45 +00001166 if (SUB_OFFSET_FAILURE == pixelRefOffset) {
1167 return false;
1168 }
1169 }
1170 dst->setPixelRef(pixelRef, pixelRefOffset)->unref();
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001171 return true;
1172 }
1173 }
1174
1175 if (this->getTexture()) {
1176 return false;
1177 } else {
1178 return this->copyTo(dst, dstConfig, NULL);
1179 }
1180}
1181
reed@android.com8a1c16f2008-12-17 15:59:43 +00001182///////////////////////////////////////////////////////////////////////////////
1183///////////////////////////////////////////////////////////////////////////////
1184
1185static void downsampleby2_proc32(SkBitmap* dst, int x, int y,
1186 const SkBitmap& src) {
1187 x <<= 1;
1188 y <<= 1;
1189 const SkPMColor* p = src.getAddr32(x, y);
reed@android.com829c83c2009-06-08 12:05:31 +00001190 const SkPMColor* baseP = p;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001191 SkPMColor c, ag, rb;
1192
1193 c = *p; ag = (c >> 8) & 0xFF00FF; rb = c & 0xFF00FF;
1194 if (x < src.width() - 1) {
1195 p += 1;
1196 }
1197 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
1198
reed@android.com829c83c2009-06-08 12:05:31 +00001199 p = baseP;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001200 if (y < src.height() - 1) {
reed@android.com829c83c2009-06-08 12:05:31 +00001201 p += src.rowBytes() >> 2;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001202 }
1203 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
1204 if (x < src.width() - 1) {
1205 p += 1;
1206 }
1207 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
1208
1209 *dst->getAddr32(x >> 1, y >> 1) =
1210 ((rb >> 2) & 0xFF00FF) | ((ag << 6) & 0xFF00FF00);
1211}
1212
1213static inline uint32_t expand16(U16CPU c) {
1214 return (c & ~SK_G16_MASK_IN_PLACE) | ((c & SK_G16_MASK_IN_PLACE) << 16);
1215}
1216
1217// returns dirt in the top 16bits, but we don't care, since we only
1218// store the low 16bits.
1219static inline U16CPU pack16(uint32_t c) {
1220 return (c & ~SK_G16_MASK_IN_PLACE) | ((c >> 16) & SK_G16_MASK_IN_PLACE);
1221}
1222
1223static void downsampleby2_proc16(SkBitmap* dst, int x, int y,
1224 const SkBitmap& src) {
1225 x <<= 1;
1226 y <<= 1;
1227 const uint16_t* p = src.getAddr16(x, y);
reed@android.com829c83c2009-06-08 12:05:31 +00001228 const uint16_t* baseP = p;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001229 SkPMColor c;
weita@google.comf9ab99a2009-05-03 18:23:30 +00001230
reed@android.com8a1c16f2008-12-17 15:59:43 +00001231 c = expand16(*p);
reed@android.com829c83c2009-06-08 12:05:31 +00001232 if (x < src.width() - 1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001233 p += 1;
1234 }
1235 c += expand16(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001236
reed@android.com829c83c2009-06-08 12:05:31 +00001237 p = baseP;
1238 if (y < src.height() - 1) {
1239 p += src.rowBytes() >> 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001240 }
1241 c += expand16(*p);
reed@android.com829c83c2009-06-08 12:05:31 +00001242 if (x < src.width() - 1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001243 p += 1;
1244 }
1245 c += expand16(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001246
reed@android.com8a1c16f2008-12-17 15:59:43 +00001247 *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)pack16(c >> 2);
1248}
1249
1250static uint32_t expand4444(U16CPU c) {
1251 return (c & 0xF0F) | ((c & ~0xF0F) << 12);
1252}
1253
1254static U16CPU collaps4444(uint32_t c) {
1255 return (c & 0xF0F) | ((c >> 12) & ~0xF0F);
1256}
1257
1258static void downsampleby2_proc4444(SkBitmap* dst, int x, int y,
1259 const SkBitmap& src) {
1260 x <<= 1;
1261 y <<= 1;
1262 const uint16_t* p = src.getAddr16(x, y);
reed@android.com829c83c2009-06-08 12:05:31 +00001263 const uint16_t* baseP = p;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001264 uint32_t c;
weita@google.comf9ab99a2009-05-03 18:23:30 +00001265
reed@android.com8a1c16f2008-12-17 15:59:43 +00001266 c = expand4444(*p);
1267 if (x < src.width() - 1) {
1268 p += 1;
1269 }
1270 c += expand4444(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001271
reed@android.com829c83c2009-06-08 12:05:31 +00001272 p = baseP;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001273 if (y < src.height() - 1) {
reed@android.com829c83c2009-06-08 12:05:31 +00001274 p += src.rowBytes() >> 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001275 }
1276 c += expand4444(*p);
1277 if (x < src.width() - 1) {
1278 p += 1;
1279 }
1280 c += expand4444(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001281
reed@android.com8a1c16f2008-12-17 15:59:43 +00001282 *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)collaps4444(c >> 2);
1283}
1284
1285void SkBitmap::buildMipMap(bool forceRebuild) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001286 if (forceRebuild)
1287 this->freeMipMap();
1288 else if (fMipMap)
1289 return; // we're already built
1290
1291 SkASSERT(NULL == fMipMap);
1292
1293 void (*proc)(SkBitmap* dst, int x, int y, const SkBitmap& src);
1294
1295 const SkBitmap::Config config = this->getConfig();
1296
1297 switch (config) {
1298 case kARGB_8888_Config:
1299 proc = downsampleby2_proc32;
1300 break;
1301 case kRGB_565_Config:
1302 proc = downsampleby2_proc16;
1303 break;
1304 case kARGB_4444_Config:
1305 proc = downsampleby2_proc4444;
1306 break;
1307 case kIndex8_Config:
1308 case kA8_Config:
1309 default:
1310 return; // don't build mipmaps for these configs
1311 }
reed@android.com89bb83a2009-05-29 21:30:42 +00001312
reed@android.com149e2f62009-05-22 14:39:03 +00001313 SkAutoLockPixels alp(*this);
1314 if (!this->readyToDraw()) {
1315 return;
1316 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001317
1318 // whip through our loop to compute the exact size needed
1319 size_t size = 0;
1320 int maxLevels = 0;
1321 {
reed@android.com149e2f62009-05-22 14:39:03 +00001322 int width = this->width();
1323 int height = this->height();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001324 for (;;) {
1325 width >>= 1;
1326 height >>= 1;
1327 if (0 == width || 0 == height) {
1328 break;
1329 }
1330 size += ComputeRowBytes(config, width) * height;
1331 maxLevels += 1;
1332 }
1333 }
reed@android.com89bb83a2009-05-29 21:30:42 +00001334
reed@android.com149e2f62009-05-22 14:39:03 +00001335 // nothing to build
reed@android.com8a1c16f2008-12-17 15:59:43 +00001336 if (0 == maxLevels) {
1337 return;
1338 }
1339
reed@android.com149e2f62009-05-22 14:39:03 +00001340 SkBitmap srcBM(*this);
1341 srcBM.lockPixels();
1342 if (!srcBM.readyToDraw()) {
1343 return;
1344 }
1345
1346 MipMap* mm = MipMap::Alloc(maxLevels, size);
1347 if (NULL == mm) {
1348 return;
1349 }
1350
reed@android.com8a1c16f2008-12-17 15:59:43 +00001351 MipLevel* level = mm->levels();
1352 uint8_t* addr = (uint8_t*)mm->pixels();
reed@android.com149e2f62009-05-22 14:39:03 +00001353 int width = this->width();
1354 int height = this->height();
scroggo@google.come5f48242013-02-25 21:47:41 +00001355 uint32_t rowBytes;
reed@android.com149e2f62009-05-22 14:39:03 +00001356 SkBitmap dstBM;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001357
1358 for (int i = 0; i < maxLevels; i++) {
1359 width >>= 1;
1360 height >>= 1;
scroggo@google.come5f48242013-02-25 21:47:41 +00001361 rowBytes = SkToU32(ComputeRowBytes(config, width));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001362
1363 level[i].fPixels = addr;
reed@android.comf459a492009-03-27 12:33:50 +00001364 level[i].fWidth = width;
1365 level[i].fHeight = height;
reed@android.com49f0ff22009-03-19 21:52:42 +00001366 level[i].fRowBytes = rowBytes;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001367
1368 dstBM.setConfig(config, width, height, rowBytes);
1369 dstBM.setPixels(addr);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001370
bungeman@google.com7cf0e9e2012-07-25 16:09:10 +00001371 srcBM.lockPixels();
reed@android.com149e2f62009-05-22 14:39:03 +00001372 for (int y = 0; y < height; y++) {
1373 for (int x = 0; x < width; x++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001374 proc(&dstBM, x, y, srcBM);
1375 }
1376 }
bungeman@google.com7cf0e9e2012-07-25 16:09:10 +00001377 srcBM.unlockPixels();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001378
1379 srcBM = dstBM;
1380 addr += height * rowBytes;
1381 }
1382 SkASSERT(addr == (uint8_t*)mm->pixels() + size);
1383 fMipMap = mm;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001384}
1385
1386bool SkBitmap::hasMipMap() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001387 return fMipMap != NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001388}
1389
1390int SkBitmap::extractMipLevel(SkBitmap* dst, SkFixed sx, SkFixed sy) {
reed@android.com83f7bc32009-07-17 02:42:41 +00001391 if (NULL == fMipMap) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001392 return 0;
reed@android.com83f7bc32009-07-17 02:42:41 +00001393 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001394
reed@android.com8a1c16f2008-12-17 15:59:43 +00001395 int level = ComputeMipLevel(sx, sy) >> 16;
1396 SkASSERT(level >= 0);
1397 if (level <= 0) {
1398 return 0;
1399 }
1400
1401 if (level >= fMipMap->fLevelCount) {
1402 level = fMipMap->fLevelCount - 1;
1403 }
1404 if (dst) {
1405 const MipLevel& mip = fMipMap->levels()[level - 1];
1406 dst->setConfig((SkBitmap::Config)this->config(),
1407 mip.fWidth, mip.fHeight, mip.fRowBytes);
1408 dst->setPixels(mip.fPixels);
1409 }
1410 return level;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001411}
1412
1413SkFixed SkBitmap::ComputeMipLevel(SkFixed sx, SkFixed sy) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001414 sx = SkAbs32(sx);
1415 sy = SkAbs32(sy);
1416 if (sx < sy) {
1417 sx = sy;
1418 }
1419 if (sx < SK_Fixed1) {
1420 return 0;
1421 }
1422 int clz = SkCLZ(sx);
1423 SkASSERT(clz >= 1 && clz <= 15);
1424 return SkIntToFixed(15 - clz) + ((unsigned)(sx << (clz + 1)) >> 16);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001425}
1426
1427///////////////////////////////////////////////////////////////////////////////
1428
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +00001429static bool GetBitmapAlpha(const SkBitmap& src, uint8_t* SK_RESTRICT alpha,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001430 int alphaRowBytes) {
1431 SkASSERT(alpha != NULL);
1432 SkASSERT(alphaRowBytes >= src.width());
1433
1434 SkBitmap::Config config = src.getConfig();
1435 int w = src.width();
1436 int h = src.height();
scroggo@google.come5f48242013-02-25 21:47:41 +00001437 size_t rb = src.rowBytes();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001438
reed@android.com1cdcb512009-08-24 19:11:00 +00001439 SkAutoLockPixels alp(src);
1440 if (!src.readyToDraw()) {
1441 // zero out the alpha buffer and return
1442 while (--h >= 0) {
1443 memset(alpha, 0, w);
1444 alpha += alphaRowBytes;
1445 }
1446 return false;
1447 }
reed@google.com82065d62011-02-07 15:30:46 +00001448
reed@android.com8a1c16f2008-12-17 15:59:43 +00001449 if (SkBitmap::kA8_Config == config && !src.isOpaque()) {
1450 const uint8_t* s = src.getAddr8(0, 0);
1451 while (--h >= 0) {
1452 memcpy(alpha, s, w);
1453 s += rb;
1454 alpha += alphaRowBytes;
1455 }
1456 } else if (SkBitmap::kARGB_8888_Config == config && !src.isOpaque()) {
1457 const SkPMColor* SK_RESTRICT s = src.getAddr32(0, 0);
1458 while (--h >= 0) {
1459 for (int x = 0; x < w; x++) {
1460 alpha[x] = SkGetPackedA32(s[x]);
1461 }
1462 s = (const SkPMColor*)((const char*)s + rb);
1463 alpha += alphaRowBytes;
1464 }
1465 } else if (SkBitmap::kARGB_4444_Config == config && !src.isOpaque()) {
1466 const SkPMColor16* SK_RESTRICT s = src.getAddr16(0, 0);
1467 while (--h >= 0) {
1468 for (int x = 0; x < w; x++) {
1469 alpha[x] = SkPacked4444ToA32(s[x]);
1470 }
1471 s = (const SkPMColor16*)((const char*)s + rb);
1472 alpha += alphaRowBytes;
1473 }
1474 } else if (SkBitmap::kIndex8_Config == config && !src.isOpaque()) {
1475 SkColorTable* ct = src.getColorTable();
1476 if (ct) {
1477 const SkPMColor* SK_RESTRICT table = ct->lockColors();
1478 const uint8_t* SK_RESTRICT s = src.getAddr8(0, 0);
1479 while (--h >= 0) {
1480 for (int x = 0; x < w; x++) {
1481 alpha[x] = SkGetPackedA32(table[s[x]]);
1482 }
1483 s += rb;
1484 alpha += alphaRowBytes;
1485 }
1486 ct->unlockColors(false);
1487 }
1488 } else { // src is opaque, so just fill alpha[] with 0xFF
1489 memset(alpha, 0xFF, h * alphaRowBytes);
1490 }
reed@android.com1cdcb512009-08-24 19:11:00 +00001491 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001492}
1493
1494#include "SkPaint.h"
1495#include "SkMaskFilter.h"
1496#include "SkMatrix.h"
1497
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001498bool SkBitmap::extractAlpha(SkBitmap* dst, const SkPaint* paint,
djsollen@google.com57f49692011-02-23 20:46:31 +00001499 Allocator *allocator, SkIPoint* offset) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001500 SkDEBUGCODE(this->validate();)
1501
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001502 SkBitmap tmpBitmap;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001503 SkMatrix identity;
1504 SkMask srcM, dstM;
1505
1506 srcM.fBounds.set(0, 0, this->width(), this->height());
1507 srcM.fRowBytes = SkAlign4(this->width());
1508 srcM.fFormat = SkMask::kA8_Format;
1509
1510 SkMaskFilter* filter = paint ? paint->getMaskFilter() : NULL;
1511
1512 // compute our (larger?) dst bounds if we have a filter
1513 if (NULL != filter) {
1514 identity.reset();
1515 srcM.fImage = NULL;
1516 if (!filter->filterMask(&dstM, srcM, identity, NULL)) {
1517 goto NO_FILTER_CASE;
1518 }
1519 dstM.fRowBytes = SkAlign4(dstM.fBounds.width());
1520 } else {
1521 NO_FILTER_CASE:
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001522 tmpBitmap.setConfig(SkBitmap::kA8_Config, this->width(), this->height(),
reed@android.com8a1c16f2008-12-17 15:59:43 +00001523 srcM.fRowBytes);
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001524 if (!tmpBitmap.allocPixels(allocator, NULL)) {
1525 // Allocation of pixels for alpha bitmap failed.
1526 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
1527 tmpBitmap.width(), tmpBitmap.height());
1528 return false;
1529 }
1530 GetBitmapAlpha(*this, tmpBitmap.getAddr8(0, 0), srcM.fRowBytes);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001531 if (offset) {
1532 offset->set(0, 0);
1533 }
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001534 tmpBitmap.swap(*dst);
1535 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001536 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001537 srcM.fImage = SkMask::AllocImage(srcM.computeImageSize());
1538 SkAutoMaskFreeImage srcCleanup(srcM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001539
1540 GetBitmapAlpha(*this, srcM.fImage, srcM.fRowBytes);
1541 if (!filter->filterMask(&dstM, srcM, identity, NULL)) {
1542 goto NO_FILTER_CASE;
1543 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001544 SkAutoMaskFreeImage dstCleanup(dstM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001545
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001546 tmpBitmap.setConfig(SkBitmap::kA8_Config, dstM.fBounds.width(),
reed@android.com8a1c16f2008-12-17 15:59:43 +00001547 dstM.fBounds.height(), dstM.fRowBytes);
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001548 if (!tmpBitmap.allocPixels(allocator, NULL)) {
1549 // Allocation of pixels for alpha bitmap failed.
1550 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
1551 tmpBitmap.width(), tmpBitmap.height());
1552 return false;
1553 }
1554 memcpy(tmpBitmap.getPixels(), dstM.fImage, dstM.computeImageSize());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001555 if (offset) {
1556 offset->set(dstM.fBounds.fLeft, dstM.fBounds.fTop);
1557 }
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001558 SkDEBUGCODE(tmpBitmap.validate();)
1559
1560 tmpBitmap.swap(*dst);
1561 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001562}
1563
1564///////////////////////////////////////////////////////////////////////////////
1565
1566enum {
1567 SERIALIZE_PIXELTYPE_NONE,
djsollen@google.com21830d92012-08-07 19:49:41 +00001568 SERIALIZE_PIXELTYPE_REF_DATA
reed@android.com8a1c16f2008-12-17 15:59:43 +00001569};
1570
reed@android.com8a1c16f2008-12-17 15:59:43 +00001571void SkBitmap::flatten(SkFlattenableWriteBuffer& buffer) const {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001572 buffer.writeInt(fWidth);
1573 buffer.writeInt(fHeight);
1574 buffer.writeInt(fRowBytes);
1575 buffer.writeInt(fConfig);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001576 buffer.writeBool(this->isOpaque());
weita@google.comf9ab99a2009-05-03 18:23:30 +00001577
reed@android.com8a1c16f2008-12-17 15:59:43 +00001578 if (fPixelRef) {
djsollen@google.com5370cd92012-03-28 20:47:01 +00001579 if (fPixelRef->getFactory()) {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001580 buffer.writeInt(SERIALIZE_PIXELTYPE_REF_DATA);
scroggo@google.come5f48242013-02-25 21:47:41 +00001581 buffer.writeUInt(SkToU32(fPixelRefOffset));
djsollen@google.com5370cd92012-03-28 20:47:01 +00001582 buffer.writeFlattenable(fPixelRef);
1583 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001584 }
1585 // if we get here, we can't record the pixels
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001586 buffer.writeInt(SERIALIZE_PIXELTYPE_NONE);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001587 } else {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001588 buffer.writeInt(SERIALIZE_PIXELTYPE_NONE);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001589 }
1590}
1591
1592void SkBitmap::unflatten(SkFlattenableReadBuffer& buffer) {
1593 this->reset();
weita@google.comf9ab99a2009-05-03 18:23:30 +00001594
reed@android.com8a1c16f2008-12-17 15:59:43 +00001595 int width = buffer.readInt();
1596 int height = buffer.readInt();
1597 int rowBytes = buffer.readInt();
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001598 int config = buffer.readInt();
weita@google.comf9ab99a2009-05-03 18:23:30 +00001599
reed@android.com8a1c16f2008-12-17 15:59:43 +00001600 this->setConfig((Config)config, width, height, rowBytes);
1601 this->setIsOpaque(buffer.readBool());
weita@google.comf9ab99a2009-05-03 18:23:30 +00001602
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001603 int reftype = buffer.readInt();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001604 switch (reftype) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001605 case SERIALIZE_PIXELTYPE_REF_DATA: {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001606 size_t offset = buffer.readUInt();
1607 SkPixelRef* pr = buffer.readFlattenableT<SkPixelRef>();
reed@google.com82065d62011-02-07 15:30:46 +00001608 SkSafeUnref(this->setPixelRef(pr, offset));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001609 break;
1610 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001611 case SERIALIZE_PIXELTYPE_NONE:
1612 break;
1613 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +00001614 SkDEBUGFAIL("unrecognized pixeltype in serialized data");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001615 sk_throw();
1616 }
1617}
1618
1619///////////////////////////////////////////////////////////////////////////////
1620
1621SkBitmap::RLEPixels::RLEPixels(int width, int height) {
1622 fHeight = height;
1623 fYPtrs = (uint8_t**)sk_malloc_throw(height * sizeof(uint8_t*));
reed@android.com4516f472009-06-29 16:25:36 +00001624 sk_bzero(fYPtrs, height * sizeof(uint8_t*));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001625}
1626
1627SkBitmap::RLEPixels::~RLEPixels() {
1628 sk_free(fYPtrs);
1629}
1630
1631///////////////////////////////////////////////////////////////////////////////
1632
1633#ifdef SK_DEBUG
1634void SkBitmap::validate() const {
1635 SkASSERT(fConfig < kConfigCount);
1636 SkASSERT(fRowBytes >= (unsigned)ComputeRowBytes((Config)fConfig, fWidth));
scroggo@google.com8e990eb2013-06-14 15:55:56 +00001637 uint8_t allFlags = kImageIsOpaque_Flag | kImageIsVolatile_Flag | kImageIsImmutable_Flag;
1638#ifdef SK_BUILD_FOR_ANDROID
1639 allFlags |= kHasHardwareMipMap_Flag;
1640#endif
1641 SkASSERT(fFlags <= allFlags);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001642 SkASSERT(fPixelLockCount >= 0);
1643 SkASSERT(NULL == fColorTable || (unsigned)fColorTable->getRefCnt() < 10000);
1644 SkASSERT((uint8_t)ComputeBytesPerPixel((Config)fConfig) == fBytesPerPixel);
1645
1646#if 0 // these asserts are not thread-correct, so disable for now
1647 if (fPixelRef) {
1648 if (fPixelLockCount > 0) {
reed@google.comff0da4f2012-05-17 13:14:52 +00001649 SkASSERT(fPixelRef->isLocked());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001650 } else {
1651 SkASSERT(NULL == fPixels);
1652 SkASSERT(NULL == fColorTable);
1653 }
1654 }
1655#endif
1656}
1657#endif
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001658
1659#ifdef SK_DEVELOPER
1660void SkBitmap::toString(SkString* str) const {
1661
1662 static const char* gConfigNames[kConfigCount] = {
1663 "NONE", "A1", "A8", "INDEX8", "565", "4444", "8888", "RLE"
1664 };
1665
1666 str->appendf("bitmap: ((%d, %d) %s", this->width(), this->height(),
1667 gConfigNames[this->config()]);
1668
1669 str->append(" (");
1670 if (this->isOpaque()) {
1671 str->append("opaque");
1672 } else {
1673 str->append("transparent");
1674 }
1675 if (this->isImmutable()) {
1676 str->append(", immutable");
1677 } else {
1678 str->append(", not-immutable");
1679 }
1680 str->append(")");
1681
1682 SkPixelRef* pr = this->pixelRef();
1683 if (NULL == pr) {
1684 // show null or the explicit pixel address (rare)
1685 str->appendf(" pixels:%p", this->getPixels());
1686 } else {
1687 const char* uri = pr->getURI();
1688 if (NULL != uri) {
1689 str->appendf(" uri:\"%s\"", uri);
1690 } else {
1691 str->appendf(" pixelref:%p", pr);
1692 }
1693 }
1694
1695 str->append(")");
1696}
1697#endif