blob: d2a308b03ab950fe536980ea0a84a8d6122f62f9 [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"
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000014#include "SkImagePriv.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkMallocPixelRef.h"
16#include "SkMask.h"
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000017#include "SkOrderedReadBuffer.h"
18#include "SkOrderedWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000019#include "SkPixelRef.h"
20#include "SkThread.h"
vandebo@chromium.org112706d2011-02-24 22:50:55 +000021#include "SkUnPreMultiply.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000022#include "SkUtils.h"
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +000023#include "SkValidationUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000024#include "SkPackBits.h"
25#include <new>
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);
reed@google.com383a6972013-10-21 14:00:07 +0000146 SkTSwap(fAlphaType, other.fAlphaType);
bsalomon@google.com586f48c2011-04-14 15:07:22 +0000147 SkTSwap(fFlags, other.fFlags);
148 SkTSwap(fBytesPerPixel, other.fBytesPerPixel);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149
150 SkDEBUGCODE(this->validate();)
151}
152
153void SkBitmap::reset() {
154 this->freePixels();
reed@android.com4516f472009-06-29 16:25:36 +0000155 sk_bzero(this, sizeof(*this));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156}
157
158int SkBitmap::ComputeBytesPerPixel(SkBitmap::Config config) {
159 int bpp;
160 switch (config) {
161 case kNo_Config:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162 bpp = 0; // not applicable
163 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164 case kA8_Config:
165 case kIndex8_Config:
166 bpp = 1;
167 break;
168 case kRGB_565_Config:
169 case kARGB_4444_Config:
170 bpp = 2;
171 break;
172 case kARGB_8888_Config:
173 bpp = 4;
174 break;
175 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000176 SkDEBUGFAIL("unknown config");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000177 bpp = 0; // error
178 break;
179 }
180 return bpp;
181}
182
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000183size_t SkBitmap::ComputeRowBytes(Config c, int width) {
reed@android.com149e2f62009-05-22 14:39:03 +0000184 if (width < 0) {
185 return 0;
186 }
187
188 Sk64 rowBytes;
189 rowBytes.setZero();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190
191 switch (c) {
192 case kNo_Config:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000193 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000194 case kA8_Config:
195 case kIndex8_Config:
reed@android.com149e2f62009-05-22 14:39:03 +0000196 rowBytes.set(width);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000197 break;
198 case kRGB_565_Config:
199 case kARGB_4444_Config:
reed@android.com149e2f62009-05-22 14:39:03 +0000200 rowBytes.set(width);
201 rowBytes.shiftLeft(1);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000202 break;
203 case kARGB_8888_Config:
reed@android.com149e2f62009-05-22 14:39:03 +0000204 rowBytes.set(width);
205 rowBytes.shiftLeft(2);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000206 break;
207 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000208 SkDEBUGFAIL("unknown config");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000209 break;
210 }
reed@android.com149e2f62009-05-22 14:39:03 +0000211 return isPos32Bits(rowBytes) ? rowBytes.get32() : 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000212}
213
214Sk64 SkBitmap::ComputeSize64(Config c, int width, int height) {
215 Sk64 size;
scroggo@google.come5f48242013-02-25 21:47:41 +0000216 size.setMul(SkToS32(SkBitmap::ComputeRowBytes(c, width)), height);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000217 return size;
218}
219
220size_t SkBitmap::ComputeSize(Config c, int width, int height) {
221 Sk64 size = SkBitmap::ComputeSize64(c, width, height);
reed@android.com149e2f62009-05-22 14:39:03 +0000222 return isPos32Bits(size) ? size.get32() : 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000223}
224
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000225Sk64 SkBitmap::ComputeSafeSize64(Config config,
226 uint32_t width,
227 uint32_t height,
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000228 size_t rowBytes) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000229 Sk64 safeSize;
230 safeSize.setZero();
231 if (height > 0) {
scroggo@google.come5f48242013-02-25 21:47:41 +0000232 // TODO: Handle the case where the return value from
233 // ComputeRowBytes is more than 31 bits.
234 safeSize.set(SkToS32(ComputeRowBytes(config, width)));
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000235 Sk64 sizeAllButLastRow;
scroggo@google.come5f48242013-02-25 21:47:41 +0000236 sizeAllButLastRow.setMul(height - 1, SkToS32(rowBytes));
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000237 safeSize.add(sizeAllButLastRow);
238 }
239 SkASSERT(!safeSize.isNeg());
240 return safeSize;
241}
242
243size_t SkBitmap::ComputeSafeSize(Config config,
244 uint32_t width,
245 uint32_t height,
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000246 size_t rowBytes) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000247 Sk64 safeSize = ComputeSafeSize64(config, width, height, rowBytes);
248 return (safeSize.is32() ? safeSize.get32() : 0);
249}
250
reed@google.com86b2e432012-03-15 21:17:03 +0000251void SkBitmap::getBounds(SkRect* bounds) const {
252 SkASSERT(bounds);
253 bounds->set(0, 0,
254 SkIntToScalar(fWidth), SkIntToScalar(fHeight));
255}
256
reed@google.com80e14592012-03-16 14:58:07 +0000257void SkBitmap::getBounds(SkIRect* bounds) const {
258 SkASSERT(bounds);
259 bounds->set(0, 0, fWidth, fHeight);
260}
261
reed@google.com86b2e432012-03-15 21:17:03 +0000262///////////////////////////////////////////////////////////////////////////////
263
reed@google.com383a6972013-10-21 14:00:07 +0000264static bool validate_alphaType(SkBitmap::Config config, SkAlphaType alphaType,
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000265 SkAlphaType* canonical = NULL) {
reed@google.com383a6972013-10-21 14:00:07 +0000266 switch (config) {
267 case SkBitmap::kNo_Config:
268 alphaType = kIgnore_SkAlphaType;
269 break;
reed@google.com383a6972013-10-21 14:00:07 +0000270 case SkBitmap::kA8_Config:
271 if (kUnpremul_SkAlphaType == alphaType) {
272 alphaType = kPremul_SkAlphaType;
273 }
274 // fall-through
275 case SkBitmap::kIndex8_Config:
276 case SkBitmap::kARGB_4444_Config:
277 case SkBitmap::kARGB_8888_Config:
278 if (kIgnore_SkAlphaType == alphaType) {
279 return false;
280 }
281 break;
282 case SkBitmap::kRGB_565_Config:
283 alphaType = kOpaque_SkAlphaType;
284 break;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000285 default:
286 return false;
reed@android.com149e2f62009-05-22 14:39:03 +0000287 }
reed@google.com383a6972013-10-21 14:00:07 +0000288 if (canonical) {
289 *canonical = alphaType;
290 }
291 return true;
292}
reed@android.com149e2f62009-05-22 14:39:03 +0000293
reed@google.com383a6972013-10-21 14:00:07 +0000294bool SkBitmap::setConfig(Config config, int width, int height, size_t rowBytes,
295 SkAlphaType alphaType) {
296 if ((width | height) < 0) {
reed@google.com9cd697c2013-10-21 14:12:13 +0000297 goto BAD_CONFIG;
reed@google.com383a6972013-10-21 14:00:07 +0000298 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000299 if (rowBytes == 0) {
reed@google.com383a6972013-10-21 14:00:07 +0000300 rowBytes = SkBitmap::ComputeRowBytes(config, width);
301 if (0 == rowBytes && kNo_Config != config) {
reed@google.com9cd697c2013-10-21 14:12:13 +0000302 goto BAD_CONFIG;
reed@android.com149e2f62009-05-22 14:39:03 +0000303 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000304 }
reed@android.com89bb83a2009-05-29 21:30:42 +0000305
reed@google.com383a6972013-10-21 14:00:07 +0000306 if (!validate_alphaType(config, alphaType, &alphaType)) {
reed@google.com9cd697c2013-10-21 14:12:13 +0000307 goto BAD_CONFIG;
reed@google.com383a6972013-10-21 14:00:07 +0000308 }
309
310 this->freePixels();
311
312 fConfig = SkToU8(config);
313 fAlphaType = SkToU8(alphaType);
reed@android.comf459a492009-03-27 12:33:50 +0000314 fWidth = width;
315 fHeight = height;
scroggo@google.come5f48242013-02-25 21:47:41 +0000316 fRowBytes = SkToU32(rowBytes);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000317
reed@google.com383a6972013-10-21 14:00:07 +0000318 fBytesPerPixel = (uint8_t)ComputeBytesPerPixel(config);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000319
320 SkDEBUGCODE(this->validate();)
reed@google.com383a6972013-10-21 14:00:07 +0000321 return true;
agl@chromium.org6b8cb252009-06-01 23:52:37 +0000322
reed@android.com9e0c2fc2009-06-02 18:54:28 +0000323 // if we got here, we had an error, so we reset the bitmap to empty
reed@google.com9cd697c2013-10-21 14:12:13 +0000324BAD_CONFIG:
agl@chromium.org6b8cb252009-06-01 23:52:37 +0000325 this->reset();
reed@google.com383a6972013-10-21 14:00:07 +0000326 return false;
327}
328
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000329bool SkBitmap::setConfig(const SkImageInfo& info, size_t rowBytes) {
330 return this->setConfig(SkImageInfoToBitmapConfig(info), info.fWidth,
331 info.fHeight, rowBytes, info.fAlphaType);
332}
333
reed@google.com383a6972013-10-21 14:00:07 +0000334bool SkBitmap::setAlphaType(SkAlphaType alphaType) {
335 if (!validate_alphaType(this->config(), alphaType, &alphaType)) {
336 return false;
337 }
338 fAlphaType = SkToU8(alphaType);
339 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000340}
341
342void SkBitmap::updatePixelsFromRef() const {
343 if (NULL != fPixelRef) {
344 if (fPixelLockCount > 0) {
reed@google.comff0da4f2012-05-17 13:14:52 +0000345 SkASSERT(fPixelRef->isLocked());
weita@google.comf9ab99a2009-05-03 18:23:30 +0000346
reed@android.com8a1c16f2008-12-17 15:59:43 +0000347 void* p = fPixelRef->pixels();
348 if (NULL != p) {
349 p = (char*)p + fPixelRefOffset;
350 }
351 fPixels = p;
352 SkRefCnt_SafeAssign(fColorTable, fPixelRef->colorTable());
353 } else {
354 SkASSERT(0 == fPixelLockCount);
355 fPixels = NULL;
reed@android.com149e2f62009-05-22 14:39:03 +0000356 if (fColorTable) {
357 fColorTable->unref();
358 fColorTable = NULL;
359 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000360 }
361 }
362}
363
364SkPixelRef* SkBitmap::setPixelRef(SkPixelRef* pr, size_t offset) {
365 // do this first, we that we never have a non-zero offset with a null ref
366 if (NULL == pr) {
367 offset = 0;
368 }
369
370 if (fPixelRef != pr || fPixelRefOffset != offset) {
371 if (fPixelRef != pr) {
372 this->freePixels();
373 SkASSERT(NULL == fPixelRef);
weita@google.comf9ab99a2009-05-03 18:23:30 +0000374
reed@google.com82065d62011-02-07 15:30:46 +0000375 SkSafeRef(pr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000376 fPixelRef = pr;
377 }
378 fPixelRefOffset = offset;
379 this->updatePixelsFromRef();
380 }
381
382 SkDEBUGCODE(this->validate();)
383 return pr;
384}
385
386void SkBitmap::lockPixels() const {
djsollen@google.com7c6d2642013-08-06 12:19:38 +0000387 if (NULL != fPixelRef && 0 == sk_atomic_inc(&fPixelLockCount)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000388 fPixelRef->lockPixels();
389 this->updatePixelsFromRef();
390 }
391 SkDEBUGCODE(this->validate();)
392}
393
394void SkBitmap::unlockPixels() const {
395 SkASSERT(NULL == fPixelRef || fPixelLockCount > 0);
396
djsollen@google.com7c6d2642013-08-06 12:19:38 +0000397 if (NULL != fPixelRef && 1 == sk_atomic_dec(&fPixelLockCount)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000398 fPixelRef->unlockPixels();
399 this->updatePixelsFromRef();
400 }
401 SkDEBUGCODE(this->validate();)
402}
403
reed@google.com9c49bc32011-07-07 13:42:37 +0000404bool SkBitmap::lockPixelsAreWritable() const {
djsollen@google.comc84b8332012-07-27 13:41:44 +0000405 return (fPixelRef) ? fPixelRef->lockPixelsAreWritable() : false;
reed@google.com9c49bc32011-07-07 13:42:37 +0000406}
407
reed@android.com8a1c16f2008-12-17 15:59:43 +0000408void SkBitmap::setPixels(void* p, SkColorTable* ctable) {
reed@google.com8e1034e2012-07-30 13:16:35 +0000409 if (NULL == p) {
410 this->setPixelRef(NULL, 0);
411 return;
412 }
413
reed@google.com5b132b22013-12-06 18:51:08 +0000414 Sk64 size = this->getSize64();
415 SkASSERT(!size.isNeg() && size.is32());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000416
reed@google.com5b132b22013-12-06 18:51:08 +0000417 this->setPixelRef(new SkMallocPixelRef(p, size.get32(), ctable, false))->unref();
djsollen@google.comc84b8332012-07-27 13:41:44 +0000418 // since we're already allocated, we lockPixels right away
419 this->lockPixels();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000420 SkDEBUGCODE(this->validate();)
421}
422
423bool SkBitmap::allocPixels(Allocator* allocator, SkColorTable* ctable) {
424 HeapAllocator stdalloc;
425
426 if (NULL == allocator) {
427 allocator = &stdalloc;
428 }
429 return allocator->allocPixelRef(this, ctable);
430}
431
432void SkBitmap::freePixels() {
433 // if we're gonna free the pixels, we certainly need to free the mipmap
434 this->freeMipMap();
435
reed@android.com149e2f62009-05-22 14:39:03 +0000436 if (fColorTable) {
437 fColorTable->unref();
438 fColorTable = NULL;
439 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000440
441 if (NULL != fPixelRef) {
442 if (fPixelLockCount > 0) {
443 fPixelRef->unlockPixels();
444 }
445 fPixelRef->unref();
446 fPixelRef = NULL;
447 fPixelRefOffset = 0;
448 }
449 fPixelLockCount = 0;
450 fPixels = NULL;
451}
452
453void SkBitmap::freeMipMap() {
reed@android.com149e2f62009-05-22 14:39:03 +0000454 if (fMipMap) {
455 fMipMap->unref();
456 fMipMap = NULL;
457 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000458}
459
460uint32_t SkBitmap::getGenerationID() const {
djsollen@google.comc84b8332012-07-27 13:41:44 +0000461 return (fPixelRef) ? fPixelRef->getGenerationID() : 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000462}
463
464void SkBitmap::notifyPixelsChanged() const {
junov@chromium.orgb0521292011-12-15 20:14:06 +0000465 SkASSERT(!this->isImmutable());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000466 if (fPixelRef) {
467 fPixelRef->notifyPixelsChanged();
468 }
469}
470
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000471GrTexture* SkBitmap::getTexture() const {
reed@android.comce4e53a2010-09-09 16:01:26 +0000472 return fPixelRef ? fPixelRef->getTexture() : NULL;
473}
474
reed@android.com8a1c16f2008-12-17 15:59:43 +0000475///////////////////////////////////////////////////////////////////////////////
476
reed@android.com8a1c16f2008-12-17 15:59:43 +0000477/** We explicitly use the same allocator for our pixels that SkMask does,
478 so that we can freely assign memory allocated by one class to the other.
479 */
480bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst,
481 SkColorTable* ctable) {
reed@google.com5b132b22013-12-06 18:51:08 +0000482 Sk64 size = dst->getSize64();
483 if (size.isNeg() || !size.is32()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000484 return false;
485 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000486
reed@google.com5b132b22013-12-06 18:51:08 +0000487 void* addr = sk_malloc_flags(size.get32(), 0); // returns NULL on failure
488 if (NULL == addr) {
489 return false;
490 }
491
492 dst->setPixelRef(new SkMallocPixelRef(addr, size.get32(), ctable))->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000493 // since we're already allocated, we lockPixels right away
494 dst->lockPixels();
495 return true;
496}
497
498///////////////////////////////////////////////////////////////////////////////
499
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000500size_t SkBitmap::getSafeSize() const {
501 // This is intended to be a size_t version of ComputeSafeSize64(), just
502 // faster. The computation is meant to be identical.
503 return (fHeight ? ((fHeight - 1) * fRowBytes) +
reed@google.com44699382013-10-31 17:28:30 +0000504 ComputeRowBytes(this->config(), fWidth): 0);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000505}
506
507Sk64 SkBitmap::getSafeSize64() const {
reed@google.com44699382013-10-31 17:28:30 +0000508 return ComputeSafeSize64(this->config(), fWidth, fHeight, fRowBytes);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000509}
510
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000511bool SkBitmap::copyPixelsTo(void* const dst, size_t dstSize,
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000512 size_t dstRowBytes, bool preserveDstPad) const {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000513
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000514 if (0 == dstRowBytes) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000515 dstRowBytes = fRowBytes;
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000516 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000517
reed@google.com44699382013-10-31 17:28:30 +0000518 if (dstRowBytes < ComputeRowBytes(this->config(), fWidth) ||
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000519 dst == NULL || (getPixels() == NULL && pixelRef() == NULL))
520 return false;
521
bsalomon@google.comc6980972011-11-02 19:57:21 +0000522 if (!preserveDstPad && static_cast<uint32_t>(dstRowBytes) == fRowBytes) {
reed@google.com44699382013-10-31 17:28:30 +0000523 size_t safeSize = this->getSafeSize();
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000524 if (safeSize > dstSize || safeSize == 0)
525 return false;
526 else {
527 SkAutoLockPixels lock(*this);
528 // This implementation will write bytes beyond the end of each row,
529 // excluding the last row, if the bitmap's stride is greater than
530 // strictly required by the current config.
531 memcpy(dst, getPixels(), safeSize);
532
533 return true;
534 }
535 } else {
536 // If destination has different stride than us, then copy line by line.
reed@google.com44699382013-10-31 17:28:30 +0000537 if (ComputeSafeSize(this->config(), fWidth, fHeight, dstRowBytes) >
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000538 dstSize)
539 return false;
540 else {
541 // Just copy what we need on each line.
reed@google.com44699382013-10-31 17:28:30 +0000542 size_t rowBytes = ComputeRowBytes(this->config(), fWidth);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000543 SkAutoLockPixels lock(*this);
544 const uint8_t* srcP = reinterpret_cast<const uint8_t*>(getPixels());
545 uint8_t* dstP = reinterpret_cast<uint8_t*>(dst);
546 for (uint32_t row = 0; row < fHeight;
547 row++, srcP += fRowBytes, dstP += dstRowBytes) {
548 memcpy(dstP, srcP, rowBytes);
549 }
550
551 return true;
552 }
553 }
554}
555
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000556///////////////////////////////////////////////////////////////////////////////
557
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000558bool SkBitmap::isImmutable() const {
junov@chromium.orgb0521292011-12-15 20:14:06 +0000559 return fPixelRef ? fPixelRef->isImmutable() :
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000560 fFlags & kImageIsImmutable_Flag;
junov@chromium.orgb0521292011-12-15 20:14:06 +0000561}
562
563void SkBitmap::setImmutable() {
564 if (fPixelRef) {
565 fPixelRef->setImmutable();
566 } else {
567 fFlags |= kImageIsImmutable_Flag;
568 }
569}
570
junov@google.com4ee7ae52011-06-30 17:30:49 +0000571bool SkBitmap::isVolatile() const {
572 return (fFlags & kImageIsVolatile_Flag) != 0;
573}
574
575void SkBitmap::setIsVolatile(bool isVolatile) {
576 if (isVolatile) {
577 fFlags |= kImageIsVolatile_Flag;
578 } else {
579 fFlags &= ~kImageIsVolatile_Flag;
580 }
581}
582
reed@android.com8a1c16f2008-12-17 15:59:43 +0000583void* SkBitmap::getAddr(int x, int y) const {
584 SkASSERT((unsigned)x < (unsigned)this->width());
585 SkASSERT((unsigned)y < (unsigned)this->height());
586
587 char* base = (char*)this->getPixels();
588 if (base) {
589 base += y * this->rowBytes();
590 switch (this->config()) {
591 case SkBitmap::kARGB_8888_Config:
592 base += x << 2;
593 break;
594 case SkBitmap::kARGB_4444_Config:
595 case SkBitmap::kRGB_565_Config:
596 base += x << 1;
597 break;
598 case SkBitmap::kA8_Config:
599 case SkBitmap::kIndex8_Config:
600 base += x;
601 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000602 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000603 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000604 SkDEBUGFAIL("Can't return addr for config");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000605 base = NULL;
606 break;
607 }
608 }
609 return base;
610}
611
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000612SkColor SkBitmap::getColor(int x, int y) const {
613 SkASSERT((unsigned)x < (unsigned)this->width());
614 SkASSERT((unsigned)y < (unsigned)this->height());
615
616 switch (this->config()) {
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000617 case SkBitmap::kA8_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000618 uint8_t* addr = this->getAddr8(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000619 return SkColorSetA(0, addr[0]);
620 }
621 case SkBitmap::kIndex8_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000622 SkPMColor c = this->getIndex8Color(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000623 return SkUnPreMultiply::PMColorToColor(c);
624 }
625 case SkBitmap::kRGB_565_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000626 uint16_t* addr = this->getAddr16(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000627 return SkPixel16ToColor(addr[0]);
628 }
629 case SkBitmap::kARGB_4444_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000630 uint16_t* addr = this->getAddr16(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000631 SkPMColor c = SkPixel4444ToPixel32(addr[0]);
632 return SkUnPreMultiply::PMColorToColor(c);
633 }
634 case SkBitmap::kARGB_8888_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000635 uint32_t* addr = this->getAddr32(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000636 return SkUnPreMultiply::PMColorToColor(addr[0]);
637 }
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000638 case kNo_Config:
rmistry@google.comd6bab022013-12-02 13:50:38 +0000639 default:
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000640 SkASSERT(false);
641 return 0;
642 }
643 SkASSERT(false); // Not reached.
644 return 0;
645}
646
reed@google.com2a7579d2012-11-07 18:30:18 +0000647bool SkBitmap::ComputeIsOpaque(const SkBitmap& bm) {
648 SkAutoLockPixels alp(bm);
649 if (!bm.getPixels()) {
650 return false;
651 }
652
653 const int height = bm.height();
654 const int width = bm.width();
655
656 switch (bm.config()) {
reed@google.com2a7579d2012-11-07 18:30:18 +0000657 case SkBitmap::kA8_Config: {
658 unsigned a = 0xFF;
659 for (int y = 0; y < height; ++y) {
660 const uint8_t* row = bm.getAddr8(0, y);
661 for (int x = 0; x < width; ++x) {
662 a &= row[x];
663 }
664 if (0xFF != a) {
665 return false;
666 }
667 }
668 return true;
669 } break;
reed@google.com2a7579d2012-11-07 18:30:18 +0000670 case SkBitmap::kIndex8_Config: {
671 SkAutoLockColors alc(bm);
672 const SkPMColor* table = alc.colors();
673 if (!table) {
674 return false;
675 }
reed@google.com140d7282013-01-07 20:25:04 +0000676 SkPMColor c = (SkPMColor)~0;
reed@google.com2a7579d2012-11-07 18:30:18 +0000677 for (int i = bm.getColorTable()->count() - 1; i >= 0; --i) {
678 c &= table[i];
679 }
680 return 0xFF == SkGetPackedA32(c);
681 } break;
682 case SkBitmap::kRGB_565_Config:
683 return true;
684 break;
685 case SkBitmap::kARGB_4444_Config: {
686 unsigned c = 0xFFFF;
687 for (int y = 0; y < height; ++y) {
688 const SkPMColor16* row = bm.getAddr16(0, y);
689 for (int x = 0; x < width; ++x) {
690 c &= row[x];
691 }
692 if (0xF != SkGetPackedA4444(c)) {
693 return false;
694 }
695 }
696 return true;
697 } break;
698 case SkBitmap::kARGB_8888_Config: {
reed@google.com140d7282013-01-07 20:25:04 +0000699 SkPMColor c = (SkPMColor)~0;
reed@google.com2a7579d2012-11-07 18:30:18 +0000700 for (int y = 0; y < height; ++y) {
701 const SkPMColor* row = bm.getAddr32(0, y);
702 for (int x = 0; x < width; ++x) {
703 c &= row[x];
704 }
705 if (0xFF != SkGetPackedA32(c)) {
706 return false;
707 }
708 }
709 return true;
710 }
711 default:
712 break;
713 }
714 return false;
715}
716
717
reed@android.com8a1c16f2008-12-17 15:59:43 +0000718///////////////////////////////////////////////////////////////////////////////
719///////////////////////////////////////////////////////////////////////////////
720
reed@google.com45f746f2013-06-21 19:51:31 +0000721static uint16_t pack_8888_to_4444(unsigned a, unsigned r, unsigned g, unsigned b) {
722 unsigned pixel = (SkA32To4444(a) << SK_A4444_SHIFT) |
723 (SkR32To4444(r) << SK_R4444_SHIFT) |
724 (SkG32To4444(g) << SK_G4444_SHIFT) |
725 (SkB32To4444(b) << SK_B4444_SHIFT);
726 return SkToU16(pixel);
727}
728
reed@google.com60d32352013-06-28 19:40:50 +0000729void SkBitmap::internalErase(const SkIRect& area,
730 U8CPU a, U8CPU r, U8CPU g, U8CPU b) const {
731#ifdef SK_DEBUG
reed@android.com8a1c16f2008-12-17 15:59:43 +0000732 SkDEBUGCODE(this->validate();)
reed@google.com60d32352013-06-28 19:40:50 +0000733 SkASSERT(!area.isEmpty());
734 {
reed@google.com92833f92013-06-28 19:47:43 +0000735 SkIRect total = { 0, 0, this->width(), this->height() };
reed@google.com60d32352013-06-28 19:40:50 +0000736 SkASSERT(total.contains(area));
737 }
738#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000739
reed@google.com60d32352013-06-28 19:40:50 +0000740 if (kNo_Config == fConfig || kIndex8_Config == fConfig) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000741 return;
742 }
743
744 SkAutoLockPixels alp(*this);
745 // perform this check after the lock call
746 if (!this->readyToDraw()) {
747 return;
748 }
749
reed@google.com60d32352013-06-28 19:40:50 +0000750 int height = area.height();
751 const int width = area.width();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000752 const int rowBytes = fRowBytes;
753
754 // make rgb premultiplied
755 if (255 != a) {
756 r = SkAlphaMul(r, a);
757 g = SkAlphaMul(g, a);
758 b = SkAlphaMul(b, a);
759 }
760
761 switch (fConfig) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000762 case kA8_Config: {
reed@google.com60d32352013-06-28 19:40:50 +0000763 uint8_t* p = this->getAddr8(area.fLeft, area.fTop);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000764 while (--height >= 0) {
765 memset(p, a, width);
766 p += rowBytes;
767 }
768 break;
769 }
reed@google.com45f746f2013-06-21 19:51:31 +0000770 case kARGB_4444_Config:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000771 case kRGB_565_Config: {
reed@google.com60d32352013-06-28 19:40:50 +0000772 uint16_t* p = this->getAddr16(area.fLeft, area.fTop);;
reed@google.com45f746f2013-06-21 19:51:31 +0000773 uint16_t v;
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000774
reed@google.com45f746f2013-06-21 19:51:31 +0000775 if (kARGB_4444_Config == fConfig) {
776 v = pack_8888_to_4444(a, r, g, b);
777 } else {
778 v = SkPackRGB16(r >> (8 - SK_R16_BITS),
779 g >> (8 - SK_G16_BITS),
780 b >> (8 - SK_B16_BITS));
781 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000782 while (--height >= 0) {
783 sk_memset16(p, v, width);
784 p = (uint16_t*)((char*)p + rowBytes);
785 }
786 break;
787 }
788 case kARGB_8888_Config: {
reed@google.com60d32352013-06-28 19:40:50 +0000789 uint32_t* p = this->getAddr32(area.fLeft, area.fTop);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000790 uint32_t v = SkPackARGB32(a, r, g, b);
791
792 while (--height >= 0) {
793 sk_memset32(p, v, width);
794 p = (uint32_t*)((char*)p + rowBytes);
795 }
796 break;
797 }
798 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000799
reed@android.com8a1c16f2008-12-17 15:59:43 +0000800 this->notifyPixelsChanged();
801}
802
reed@google.com60d32352013-06-28 19:40:50 +0000803void SkBitmap::eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const {
reed@google.com92833f92013-06-28 19:47:43 +0000804 SkIRect area = { 0, 0, this->width(), this->height() };
reed@google.com60d32352013-06-28 19:40:50 +0000805 if (!area.isEmpty()) {
806 this->internalErase(area, a, r, g, b);
807 }
808}
809
810void SkBitmap::eraseArea(const SkIRect& rect, SkColor c) const {
reed@google.com92833f92013-06-28 19:47:43 +0000811 SkIRect area = { 0, 0, this->width(), this->height() };
reed@google.com60d32352013-06-28 19:40:50 +0000812 if (area.intersect(rect)) {
813 this->internalErase(area, SkColorGetA(c), SkColorGetR(c),
814 SkColorGetG(c), SkColorGetB(c));
815 }
816}
817
reed@android.com8a1c16f2008-12-17 15:59:43 +0000818//////////////////////////////////////////////////////////////////////////////////////
819//////////////////////////////////////////////////////////////////////////////////////
820
821#define SUB_OFFSET_FAILURE ((size_t)-1)
822
scroggo@google.coma2a31922012-12-07 19:14:45 +0000823/**
824 * Based on the Config and rowBytes() of bm, return the offset into an SkPixelRef of the pixel at
825 * (x, y).
826 * Note that the SkPixelRef does not need to be set yet. deepCopyTo takes advantage of this fact.
827 * Also note that (x, y) may be outside the range of (0 - width(), 0 - height()), so long as it is
828 * within the bounds of the SkPixelRef being used.
829 */
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000830static size_t get_sub_offset(const SkBitmap& bm, int x, int y) {
reed@google.com44699382013-10-31 17:28:30 +0000831 switch (bm.config()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000832 case SkBitmap::kA8_Config:
833 case SkBitmap:: kIndex8_Config:
834 // x is fine as is for the calculation
835 break;
836
837 case SkBitmap::kRGB_565_Config:
838 case SkBitmap::kARGB_4444_Config:
839 x <<= 1;
840 break;
841
842 case SkBitmap::kARGB_8888_Config:
843 x <<= 2;
844 break;
845
846 case SkBitmap::kNo_Config:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000847 default:
848 return SUB_OFFSET_FAILURE;
849 }
850 return y * bm.rowBytes() + x;
851}
852
scroggo@google.coma2a31922012-12-07 19:14:45 +0000853/**
854 * Using the pixelRefOffset(), rowBytes(), and Config of bm, determine the (x, y) coordinate of the
855 * upper left corner of bm relative to its SkPixelRef.
856 * x and y must be non-NULL.
857 */
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000858bool get_upper_left_from_offset(SkBitmap::Config config, size_t offset, size_t rowBytes,
scroggo@google.com61d6c9e2013-05-21 20:38:40 +0000859 int32_t* x, int32_t* y);
860bool get_upper_left_from_offset(SkBitmap::Config config, size_t offset, size_t rowBytes,
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000861 int32_t* x, int32_t* y) {
scroggo@google.coma2a31922012-12-07 19:14:45 +0000862 SkASSERT(x != NULL && y != NULL);
scroggo@google.coma2a31922012-12-07 19:14:45 +0000863 if (0 == offset) {
864 *x = *y = 0;
865 return true;
866 }
867 // Use integer division to find the correct y position.
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000868 // The remainder will be the x position, after we reverse get_sub_offset.
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000869 SkTDivMod(offset, rowBytes, y, x);
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000870 switch (config) {
scroggo@google.coma2a31922012-12-07 19:14:45 +0000871 case SkBitmap::kA8_Config:
872 // Fall through.
873 case SkBitmap::kIndex8_Config:
874 // x is unmodified
875 break;
876
877 case SkBitmap::kRGB_565_Config:
878 // Fall through.
879 case SkBitmap::kARGB_4444_Config:
880 *x >>= 1;
881 break;
882
883 case SkBitmap::kARGB_8888_Config:
884 *x >>= 2;
885 break;
886
887 case SkBitmap::kNo_Config:
888 // Fall through.
scroggo@google.coma2a31922012-12-07 19:14:45 +0000889 default:
890 return false;
891 }
892 return true;
893}
894
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000895static bool get_upper_left_from_offset(const SkBitmap& bm, int32_t* x, int32_t* y) {
896 return get_upper_left_from_offset(bm.config(), bm.pixelRefOffset(), bm.rowBytes(), x, y);
897}
898
reed@android.com8a1c16f2008-12-17 15:59:43 +0000899bool SkBitmap::extractSubset(SkBitmap* result, const SkIRect& subset) const {
900 SkDEBUGCODE(this->validate();)
901
djsollen@google.comc84b8332012-07-27 13:41:44 +0000902 if (NULL == result || NULL == fPixelRef) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000903 return false; // no src pixels
904 }
905
906 SkIRect srcRect, r;
907 srcRect.set(0, 0, this->width(), this->height());
908 if (!r.intersect(srcRect, subset)) {
909 return false; // r is empty (i.e. no intersection)
910 }
911
scroggo@google.coma2a31922012-12-07 19:14:45 +0000912 if (fPixelRef->getTexture() != NULL) {
913 // Do a deep copy
914 SkPixelRef* pixelRef = fPixelRef->deepCopy(this->config(), &subset);
915 if (pixelRef != NULL) {
916 SkBitmap dst;
reed@google.com383a6972013-10-21 14:00:07 +0000917 dst.setConfig(this->config(), subset.width(), subset.height(), 0,
918 this->alphaType());
scroggo@google.coma2a31922012-12-07 19:14:45 +0000919 dst.setIsVolatile(this->isVolatile());
scroggo@google.coma2a31922012-12-07 19:14:45 +0000920 dst.setPixelRef(pixelRef)->unref();
921 SkDEBUGCODE(dst.validate());
922 result->swap(dst);
923 return true;
924 }
925 }
926
scroggo@google.coma2a31922012-12-07 19:14:45 +0000927 // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have
928 // exited above.
929 SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width()));
930 SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height()));
931
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000932 size_t offset = get_sub_offset(*this, r.fLeft, r.fTop);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000933 if (SUB_OFFSET_FAILURE == offset) {
934 return false; // config not supported
935 }
936
937 SkBitmap dst;
reed@google.com383a6972013-10-21 14:00:07 +0000938 dst.setConfig(this->config(), r.width(), r.height(), this->rowBytes(),
939 this->alphaType());
skyostil@google.com0eb75762012-01-16 10:45:53 +0000940 dst.setIsVolatile(this->isVolatile());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000941
942 if (fPixelRef) {
943 // share the pixelref with a custom offset
944 dst.setPixelRef(fPixelRef, fPixelRefOffset + offset);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000945 }
946 SkDEBUGCODE(dst.validate();)
947
948 // we know we're good, so commit to result
949 result->swap(dst);
950 return true;
951}
952
953///////////////////////////////////////////////////////////////////////////////
954
955#include "SkCanvas.h"
956#include "SkPaint.h"
957
reed@android.comfbaa88d2009-05-06 17:44:34 +0000958bool SkBitmap::canCopyTo(Config dstConfig) const {
reed@google.com44699382013-10-31 17:28:30 +0000959 if (this->config() == kNo_Config) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000960 return false;
961 }
962
reed@android.comfbaa88d2009-05-06 17:44:34 +0000963 bool sameConfigs = (this->config() == dstConfig);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000964 switch (dstConfig) {
965 case kA8_Config:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000966 case kRGB_565_Config:
967 case kARGB_8888_Config:
968 break;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000969 case kIndex8_Config:
970 if (!sameConfigs) {
971 return false;
972 }
973 break;
scroggo@google.com8dc8bc52013-08-07 19:16:05 +0000974 case kARGB_4444_Config:
975 return sameConfigs || kARGB_8888_Config == this->config();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000976 default:
977 return false;
978 }
reed@android.comfbaa88d2009-05-06 17:44:34 +0000979 return true;
980}
981
982bool SkBitmap::copyTo(SkBitmap* dst, Config dstConfig, Allocator* alloc) const {
983 if (!this->canCopyTo(dstConfig)) {
984 return false;
985 }
986
reed@google.com50dfa012011-04-01 19:05:36 +0000987 // if we have a texture, first get those pixels
988 SkBitmap tmpSrc;
989 const SkBitmap* src = this;
990
scroggo@google.coma2a31922012-12-07 19:14:45 +0000991 if (fPixelRef) {
992 SkIRect subset;
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000993 if (get_upper_left_from_offset(*this, &subset.fLeft, &subset.fTop)) {
scroggo@google.coma2a31922012-12-07 19:14:45 +0000994 subset.fRight = subset.fLeft + fWidth;
995 subset.fBottom = subset.fTop + fHeight;
996 if (fPixelRef->readPixels(&tmpSrc, &subset)) {
997 SkASSERT(tmpSrc.width() == this->width());
998 SkASSERT(tmpSrc.height() == this->height());
reed@google.com50dfa012011-04-01 19:05:36 +0000999
scroggo@google.coma2a31922012-12-07 19:14:45 +00001000 // did we get lucky and we can just return tmpSrc?
1001 if (tmpSrc.config() == dstConfig && NULL == alloc) {
1002 dst->swap(tmpSrc);
1003 if (dst->pixelRef() && this->config() == dstConfig) {
commit-bot@chromium.org50a30432013-10-24 17:44:27 +00001004 // TODO(scroggo): fix issue 1742
1005 dst->pixelRef()->cloneGenID(*fPixelRef);
scroggo@google.coma2a31922012-12-07 19:14:45 +00001006 }
1007 return true;
1008 }
1009
1010 // fall through to the raster case
1011 src = &tmpSrc;
scroggo@google.comd5764e82012-08-22 15:00:05 +00001012 }
reed@google.com50dfa012011-04-01 19:05:36 +00001013 }
reed@android.comfbaa88d2009-05-06 17:44:34 +00001014 }
reed@android.com311c82d2009-05-05 23:13:23 +00001015
reed@google.com50dfa012011-04-01 19:05:36 +00001016 // we lock this now, since we may need its colortable
1017 SkAutoLockPixels srclock(*src);
1018 if (!src->readyToDraw()) {
1019 return false;
1020 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001021
reed@google.com50dfa012011-04-01 19:05:36 +00001022 SkBitmap tmpDst;
reed@google.com383a6972013-10-21 14:00:07 +00001023 tmpDst.setConfig(dstConfig, src->width(), src->height(), 0,
1024 src->alphaType());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001025
weita@google.comf9ab99a2009-05-03 18:23:30 +00001026 // allocate colortable if srcConfig == kIndex8_Config
1027 SkColorTable* ctable = (dstConfig == kIndex8_Config) ?
reed@google.com50dfa012011-04-01 19:05:36 +00001028 new SkColorTable(*src->getColorTable()) : NULL;
weita@google.comf9ab99a2009-05-03 18:23:30 +00001029 SkAutoUnref au(ctable);
reed@google.com50dfa012011-04-01 19:05:36 +00001030 if (!tmpDst.allocPixels(alloc, ctable)) {
weita@google.comf9ab99a2009-05-03 18:23:30 +00001031 return false;
1032 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001033
reed@google.com50dfa012011-04-01 19:05:36 +00001034 if (!tmpDst.readyToDraw()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001035 // allocator/lock failed
1036 return false;
1037 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001038
reed@android.comfbaa88d2009-05-06 17:44:34 +00001039 /* do memcpy for the same configs cases, else use drawing
weita@google.comf9ab99a2009-05-03 18:23:30 +00001040 */
reed@google.com50dfa012011-04-01 19:05:36 +00001041 if (src->config() == dstConfig) {
1042 if (tmpDst.getSize() == src->getSize()) {
1043 memcpy(tmpDst.getPixels(), src->getPixels(), src->getSafeSize());
scroggo@google.comd5764e82012-08-22 15:00:05 +00001044 SkPixelRef* pixelRef = tmpDst.pixelRef();
commit-bot@chromium.org50a30432013-10-24 17:44:27 +00001045 if (NULL != pixelRef && NULL != fPixelRef) {
1046 // TODO(scroggo): fix issue 1742
1047 pixelRef->cloneGenID(*fPixelRef);
scroggo@google.comd5764e82012-08-22 15:00:05 +00001048 }
reed@android.com311c82d2009-05-05 23:13:23 +00001049 } else {
reed@google.com50dfa012011-04-01 19:05:36 +00001050 const char* srcP = reinterpret_cast<const char*>(src->getPixels());
1051 char* dstP = reinterpret_cast<char*>(tmpDst.getPixels());
reed@android.com311c82d2009-05-05 23:13:23 +00001052 // to be sure we don't read too much, only copy our logical pixels
reed@google.com50dfa012011-04-01 19:05:36 +00001053 size_t bytesToCopy = tmpDst.width() * tmpDst.bytesPerPixel();
1054 for (int y = 0; y < tmpDst.height(); y++) {
reed@android.com311c82d2009-05-05 23:13:23 +00001055 memcpy(dstP, srcP, bytesToCopy);
reed@google.com50dfa012011-04-01 19:05:36 +00001056 srcP += src->rowBytes();
1057 dstP += tmpDst.rowBytes();
reed@android.com311c82d2009-05-05 23:13:23 +00001058 }
1059 }
scroggo@google.com8dc8bc52013-08-07 19:16:05 +00001060 } else if (SkBitmap::kARGB_4444_Config == dstConfig
1061 && SkBitmap::kARGB_8888_Config == src->config()) {
1062 SkASSERT(src->height() == tmpDst.height());
1063 SkASSERT(src->width() == tmpDst.width());
1064 for (int y = 0; y < src->height(); ++y) {
1065 SkPMColor16* SK_RESTRICT dstRow = (SkPMColor16*) tmpDst.getAddr16(0, y);
1066 SkPMColor* SK_RESTRICT srcRow = (SkPMColor*) src->getAddr32(0, y);
1067 DITHER_4444_SCAN(y);
1068 for (int x = 0; x < src->width(); ++x) {
1069 dstRow[x] = SkDitherARGB32To4444(srcRow[x],
1070 DITHER_VALUE(x));
1071 }
1072 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001073 } else {
robertphillips@google.com0197b322013-10-10 15:48:16 +00001074 // Always clear the dest in case one of the blitters accesses it
1075 // TODO: switch the allocation of tmpDst to call sk_calloc_throw
1076 tmpDst.eraseColor(SK_ColorTRANSPARENT);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001077
reed@google.com50dfa012011-04-01 19:05:36 +00001078 SkCanvas canvas(tmpDst);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001079 SkPaint paint;
1080
1081 paint.setDither(true);
reed@google.com50dfa012011-04-01 19:05:36 +00001082 canvas.drawBitmap(*src, 0, 0, &paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001083 }
1084
reed@google.com50dfa012011-04-01 19:05:36 +00001085 dst->swap(tmpDst);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001086 return true;
1087}
1088
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001089bool SkBitmap::deepCopyTo(SkBitmap* dst, Config dstConfig) const {
1090 if (!this->canCopyTo(dstConfig)) {
1091 return false;
1092 }
1093
1094 // If we have a PixelRef, and it supports deep copy, use it.
1095 // Currently supported only by texture-backed bitmaps.
1096 if (fPixelRef) {
1097 SkPixelRef* pixelRef = fPixelRef->deepCopy(dstConfig);
1098 if (pixelRef) {
scroggo@google.coma2a31922012-12-07 19:14:45 +00001099 uint32_t rowBytes;
scroggo@google.comd5764e82012-08-22 15:00:05 +00001100 if (dstConfig == fConfig) {
commit-bot@chromium.org50a30432013-10-24 17:44:27 +00001101 // TODO(scroggo): fix issue 1742
1102 pixelRef->cloneGenID(*fPixelRef);
scroggo@google.coma2a31922012-12-07 19:14:45 +00001103 // Use the same rowBytes as the original.
1104 rowBytes = fRowBytes;
1105 } else {
1106 // With the new config, an appropriate fRowBytes will be computed by setConfig.
1107 rowBytes = 0;
scroggo@google.comd5764e82012-08-22 15:00:05 +00001108 }
scroggo@google.coma2a31922012-12-07 19:14:45 +00001109 dst->setConfig(dstConfig, fWidth, fHeight, rowBytes);
1110
1111 size_t pixelRefOffset;
1112 if (0 == fPixelRefOffset || dstConfig == fConfig) {
1113 // Use the same offset as the original.
1114 pixelRefOffset = fPixelRefOffset;
1115 } else {
1116 // Find the correct offset in the new config. This needs to be done after calling
1117 // setConfig so dst's fConfig and fRowBytes have been set properly.
scroggo@google.come5f48242013-02-25 21:47:41 +00001118 int32_t x, y;
scroggo@google.com1b1bcc32013-05-21 20:31:23 +00001119 if (!get_upper_left_from_offset(*this, &x, &y)) {
scroggo@google.coma2a31922012-12-07 19:14:45 +00001120 return false;
1121 }
scroggo@google.com1b1bcc32013-05-21 20:31:23 +00001122 pixelRefOffset = get_sub_offset(*dst, x, y);
scroggo@google.coma2a31922012-12-07 19:14:45 +00001123 if (SUB_OFFSET_FAILURE == pixelRefOffset) {
1124 return false;
1125 }
1126 }
1127 dst->setPixelRef(pixelRef, pixelRefOffset)->unref();
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001128 return true;
1129 }
1130 }
1131
1132 if (this->getTexture()) {
1133 return false;
1134 } else {
1135 return this->copyTo(dst, dstConfig, NULL);
1136 }
1137}
1138
reed@android.com8a1c16f2008-12-17 15:59:43 +00001139///////////////////////////////////////////////////////////////////////////////
1140///////////////////////////////////////////////////////////////////////////////
1141
1142static void downsampleby2_proc32(SkBitmap* dst, int x, int y,
1143 const SkBitmap& src) {
1144 x <<= 1;
1145 y <<= 1;
1146 const SkPMColor* p = src.getAddr32(x, y);
reed@android.com829c83c2009-06-08 12:05:31 +00001147 const SkPMColor* baseP = p;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001148 SkPMColor c, ag, rb;
1149
1150 c = *p; ag = (c >> 8) & 0xFF00FF; rb = c & 0xFF00FF;
1151 if (x < src.width() - 1) {
1152 p += 1;
1153 }
1154 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
1155
reed@android.com829c83c2009-06-08 12:05:31 +00001156 p = baseP;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001157 if (y < src.height() - 1) {
reed@android.com829c83c2009-06-08 12:05:31 +00001158 p += src.rowBytes() >> 2;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001159 }
1160 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
1161 if (x < src.width() - 1) {
1162 p += 1;
1163 }
1164 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
1165
1166 *dst->getAddr32(x >> 1, y >> 1) =
1167 ((rb >> 2) & 0xFF00FF) | ((ag << 6) & 0xFF00FF00);
1168}
1169
1170static inline uint32_t expand16(U16CPU c) {
1171 return (c & ~SK_G16_MASK_IN_PLACE) | ((c & SK_G16_MASK_IN_PLACE) << 16);
1172}
1173
1174// returns dirt in the top 16bits, but we don't care, since we only
1175// store the low 16bits.
1176static inline U16CPU pack16(uint32_t c) {
1177 return (c & ~SK_G16_MASK_IN_PLACE) | ((c >> 16) & SK_G16_MASK_IN_PLACE);
1178}
1179
1180static void downsampleby2_proc16(SkBitmap* dst, int x, int y,
1181 const SkBitmap& src) {
1182 x <<= 1;
1183 y <<= 1;
1184 const uint16_t* p = src.getAddr16(x, y);
reed@android.com829c83c2009-06-08 12:05:31 +00001185 const uint16_t* baseP = p;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001186 SkPMColor c;
weita@google.comf9ab99a2009-05-03 18:23:30 +00001187
reed@android.com8a1c16f2008-12-17 15:59:43 +00001188 c = expand16(*p);
reed@android.com829c83c2009-06-08 12:05:31 +00001189 if (x < src.width() - 1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001190 p += 1;
1191 }
1192 c += expand16(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001193
reed@android.com829c83c2009-06-08 12:05:31 +00001194 p = baseP;
1195 if (y < src.height() - 1) {
1196 p += src.rowBytes() >> 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001197 }
1198 c += expand16(*p);
reed@android.com829c83c2009-06-08 12:05:31 +00001199 if (x < src.width() - 1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001200 p += 1;
1201 }
1202 c += expand16(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001203
reed@android.com8a1c16f2008-12-17 15:59:43 +00001204 *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)pack16(c >> 2);
1205}
1206
1207static uint32_t expand4444(U16CPU c) {
1208 return (c & 0xF0F) | ((c & ~0xF0F) << 12);
1209}
1210
1211static U16CPU collaps4444(uint32_t c) {
1212 return (c & 0xF0F) | ((c >> 12) & ~0xF0F);
1213}
1214
1215static void downsampleby2_proc4444(SkBitmap* dst, int x, int y,
1216 const SkBitmap& src) {
1217 x <<= 1;
1218 y <<= 1;
1219 const uint16_t* p = src.getAddr16(x, y);
reed@android.com829c83c2009-06-08 12:05:31 +00001220 const uint16_t* baseP = p;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001221 uint32_t c;
weita@google.comf9ab99a2009-05-03 18:23:30 +00001222
reed@android.com8a1c16f2008-12-17 15:59:43 +00001223 c = expand4444(*p);
1224 if (x < src.width() - 1) {
1225 p += 1;
1226 }
1227 c += expand4444(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001228
reed@android.com829c83c2009-06-08 12:05:31 +00001229 p = baseP;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001230 if (y < src.height() - 1) {
reed@android.com829c83c2009-06-08 12:05:31 +00001231 p += src.rowBytes() >> 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001232 }
1233 c += expand4444(*p);
1234 if (x < src.width() - 1) {
1235 p += 1;
1236 }
1237 c += expand4444(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001238
reed@android.com8a1c16f2008-12-17 15:59:43 +00001239 *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)collaps4444(c >> 2);
1240}
1241
1242void SkBitmap::buildMipMap(bool forceRebuild) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001243 if (forceRebuild)
1244 this->freeMipMap();
1245 else if (fMipMap)
1246 return; // we're already built
1247
1248 SkASSERT(NULL == fMipMap);
1249
1250 void (*proc)(SkBitmap* dst, int x, int y, const SkBitmap& src);
1251
reed@google.com44699382013-10-31 17:28:30 +00001252 const SkBitmap::Config config = this->config();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001253
1254 switch (config) {
1255 case kARGB_8888_Config:
1256 proc = downsampleby2_proc32;
1257 break;
1258 case kRGB_565_Config:
1259 proc = downsampleby2_proc16;
1260 break;
1261 case kARGB_4444_Config:
1262 proc = downsampleby2_proc4444;
1263 break;
1264 case kIndex8_Config:
1265 case kA8_Config:
1266 default:
1267 return; // don't build mipmaps for these configs
1268 }
reed@android.com89bb83a2009-05-29 21:30:42 +00001269
reed@android.com149e2f62009-05-22 14:39:03 +00001270 SkAutoLockPixels alp(*this);
1271 if (!this->readyToDraw()) {
1272 return;
1273 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001274
1275 // whip through our loop to compute the exact size needed
1276 size_t size = 0;
1277 int maxLevels = 0;
1278 {
reed@android.com149e2f62009-05-22 14:39:03 +00001279 int width = this->width();
1280 int height = this->height();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001281 for (;;) {
1282 width >>= 1;
1283 height >>= 1;
1284 if (0 == width || 0 == height) {
1285 break;
1286 }
1287 size += ComputeRowBytes(config, width) * height;
1288 maxLevels += 1;
1289 }
1290 }
reed@android.com89bb83a2009-05-29 21:30:42 +00001291
reed@android.com149e2f62009-05-22 14:39:03 +00001292 // nothing to build
reed@android.com8a1c16f2008-12-17 15:59:43 +00001293 if (0 == maxLevels) {
1294 return;
1295 }
1296
reed@android.com149e2f62009-05-22 14:39:03 +00001297 SkBitmap srcBM(*this);
1298 srcBM.lockPixels();
1299 if (!srcBM.readyToDraw()) {
1300 return;
1301 }
1302
1303 MipMap* mm = MipMap::Alloc(maxLevels, size);
1304 if (NULL == mm) {
1305 return;
1306 }
1307
reed@android.com8a1c16f2008-12-17 15:59:43 +00001308 MipLevel* level = mm->levels();
1309 uint8_t* addr = (uint8_t*)mm->pixels();
reed@android.com149e2f62009-05-22 14:39:03 +00001310 int width = this->width();
1311 int height = this->height();
scroggo@google.come5f48242013-02-25 21:47:41 +00001312 uint32_t rowBytes;
reed@android.com149e2f62009-05-22 14:39:03 +00001313 SkBitmap dstBM;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001314
1315 for (int i = 0; i < maxLevels; i++) {
1316 width >>= 1;
1317 height >>= 1;
scroggo@google.come5f48242013-02-25 21:47:41 +00001318 rowBytes = SkToU32(ComputeRowBytes(config, width));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001319
1320 level[i].fPixels = addr;
reed@android.comf459a492009-03-27 12:33:50 +00001321 level[i].fWidth = width;
1322 level[i].fHeight = height;
reed@android.com49f0ff22009-03-19 21:52:42 +00001323 level[i].fRowBytes = rowBytes;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001324
1325 dstBM.setConfig(config, width, height, rowBytes);
1326 dstBM.setPixels(addr);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001327
bungeman@google.com7cf0e9e2012-07-25 16:09:10 +00001328 srcBM.lockPixels();
reed@android.com149e2f62009-05-22 14:39:03 +00001329 for (int y = 0; y < height; y++) {
1330 for (int x = 0; x < width; x++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001331 proc(&dstBM, x, y, srcBM);
1332 }
1333 }
bungeman@google.com7cf0e9e2012-07-25 16:09:10 +00001334 srcBM.unlockPixels();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001335
1336 srcBM = dstBM;
1337 addr += height * rowBytes;
1338 }
1339 SkASSERT(addr == (uint8_t*)mm->pixels() + size);
1340 fMipMap = mm;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001341}
1342
1343bool SkBitmap::hasMipMap() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001344 return fMipMap != NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001345}
1346
1347int SkBitmap::extractMipLevel(SkBitmap* dst, SkFixed sx, SkFixed sy) {
reed@android.com83f7bc32009-07-17 02:42:41 +00001348 if (NULL == fMipMap) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001349 return 0;
reed@android.com83f7bc32009-07-17 02:42:41 +00001350 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001351
reed@android.com8a1c16f2008-12-17 15:59:43 +00001352 int level = ComputeMipLevel(sx, sy) >> 16;
1353 SkASSERT(level >= 0);
1354 if (level <= 0) {
1355 return 0;
1356 }
1357
1358 if (level >= fMipMap->fLevelCount) {
1359 level = fMipMap->fLevelCount - 1;
1360 }
1361 if (dst) {
1362 const MipLevel& mip = fMipMap->levels()[level - 1];
1363 dst->setConfig((SkBitmap::Config)this->config(),
1364 mip.fWidth, mip.fHeight, mip.fRowBytes);
1365 dst->setPixels(mip.fPixels);
1366 }
1367 return level;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001368}
1369
1370SkFixed SkBitmap::ComputeMipLevel(SkFixed sx, SkFixed sy) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001371 sx = SkAbs32(sx);
1372 sy = SkAbs32(sy);
1373 if (sx < sy) {
1374 sx = sy;
1375 }
1376 if (sx < SK_Fixed1) {
1377 return 0;
1378 }
1379 int clz = SkCLZ(sx);
1380 SkASSERT(clz >= 1 && clz <= 15);
1381 return SkIntToFixed(15 - clz) + ((unsigned)(sx << (clz + 1)) >> 16);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001382}
1383
1384///////////////////////////////////////////////////////////////////////////////
1385
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +00001386static bool GetBitmapAlpha(const SkBitmap& src, uint8_t* SK_RESTRICT alpha,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001387 int alphaRowBytes) {
1388 SkASSERT(alpha != NULL);
1389 SkASSERT(alphaRowBytes >= src.width());
1390
reed@google.com44699382013-10-31 17:28:30 +00001391 SkBitmap::Config config = src.config();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001392 int w = src.width();
1393 int h = src.height();
scroggo@google.come5f48242013-02-25 21:47:41 +00001394 size_t rb = src.rowBytes();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001395
reed@android.com1cdcb512009-08-24 19:11:00 +00001396 SkAutoLockPixels alp(src);
1397 if (!src.readyToDraw()) {
1398 // zero out the alpha buffer and return
1399 while (--h >= 0) {
1400 memset(alpha, 0, w);
1401 alpha += alphaRowBytes;
1402 }
1403 return false;
1404 }
reed@google.com82065d62011-02-07 15:30:46 +00001405
reed@android.com8a1c16f2008-12-17 15:59:43 +00001406 if (SkBitmap::kA8_Config == config && !src.isOpaque()) {
1407 const uint8_t* s = src.getAddr8(0, 0);
1408 while (--h >= 0) {
1409 memcpy(alpha, s, w);
1410 s += rb;
1411 alpha += alphaRowBytes;
1412 }
1413 } else if (SkBitmap::kARGB_8888_Config == config && !src.isOpaque()) {
1414 const SkPMColor* SK_RESTRICT s = src.getAddr32(0, 0);
1415 while (--h >= 0) {
1416 for (int x = 0; x < w; x++) {
1417 alpha[x] = SkGetPackedA32(s[x]);
1418 }
1419 s = (const SkPMColor*)((const char*)s + rb);
1420 alpha += alphaRowBytes;
1421 }
1422 } else if (SkBitmap::kARGB_4444_Config == config && !src.isOpaque()) {
1423 const SkPMColor16* SK_RESTRICT s = src.getAddr16(0, 0);
1424 while (--h >= 0) {
1425 for (int x = 0; x < w; x++) {
1426 alpha[x] = SkPacked4444ToA32(s[x]);
1427 }
1428 s = (const SkPMColor16*)((const char*)s + rb);
1429 alpha += alphaRowBytes;
1430 }
1431 } else if (SkBitmap::kIndex8_Config == config && !src.isOpaque()) {
1432 SkColorTable* ct = src.getColorTable();
1433 if (ct) {
1434 const SkPMColor* SK_RESTRICT table = ct->lockColors();
1435 const uint8_t* SK_RESTRICT s = src.getAddr8(0, 0);
1436 while (--h >= 0) {
1437 for (int x = 0; x < w; x++) {
1438 alpha[x] = SkGetPackedA32(table[s[x]]);
1439 }
1440 s += rb;
1441 alpha += alphaRowBytes;
1442 }
reed@google.com0a6151d2013-10-10 14:44:56 +00001443 ct->unlockColors();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001444 }
1445 } else { // src is opaque, so just fill alpha[] with 0xFF
1446 memset(alpha, 0xFF, h * alphaRowBytes);
1447 }
reed@android.com1cdcb512009-08-24 19:11:00 +00001448 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001449}
1450
1451#include "SkPaint.h"
1452#include "SkMaskFilter.h"
1453#include "SkMatrix.h"
1454
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001455bool SkBitmap::extractAlpha(SkBitmap* dst, const SkPaint* paint,
djsollen@google.com57f49692011-02-23 20:46:31 +00001456 Allocator *allocator, SkIPoint* offset) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001457 SkDEBUGCODE(this->validate();)
1458
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001459 SkBitmap tmpBitmap;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001460 SkMatrix identity;
1461 SkMask srcM, dstM;
1462
1463 srcM.fBounds.set(0, 0, this->width(), this->height());
1464 srcM.fRowBytes = SkAlign4(this->width());
1465 srcM.fFormat = SkMask::kA8_Format;
1466
1467 SkMaskFilter* filter = paint ? paint->getMaskFilter() : NULL;
1468
1469 // compute our (larger?) dst bounds if we have a filter
1470 if (NULL != filter) {
1471 identity.reset();
1472 srcM.fImage = NULL;
1473 if (!filter->filterMask(&dstM, srcM, identity, NULL)) {
1474 goto NO_FILTER_CASE;
1475 }
1476 dstM.fRowBytes = SkAlign4(dstM.fBounds.width());
1477 } else {
1478 NO_FILTER_CASE:
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001479 tmpBitmap.setConfig(SkBitmap::kA8_Config, this->width(), this->height(),
reed@android.com8a1c16f2008-12-17 15:59:43 +00001480 srcM.fRowBytes);
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001481 if (!tmpBitmap.allocPixels(allocator, NULL)) {
1482 // Allocation of pixels for alpha bitmap failed.
1483 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
1484 tmpBitmap.width(), tmpBitmap.height());
1485 return false;
1486 }
1487 GetBitmapAlpha(*this, tmpBitmap.getAddr8(0, 0), srcM.fRowBytes);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001488 if (offset) {
1489 offset->set(0, 0);
1490 }
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001491 tmpBitmap.swap(*dst);
1492 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001493 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001494 srcM.fImage = SkMask::AllocImage(srcM.computeImageSize());
1495 SkAutoMaskFreeImage srcCleanup(srcM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001496
1497 GetBitmapAlpha(*this, srcM.fImage, srcM.fRowBytes);
1498 if (!filter->filterMask(&dstM, srcM, identity, NULL)) {
1499 goto NO_FILTER_CASE;
1500 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001501 SkAutoMaskFreeImage dstCleanup(dstM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001502
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001503 tmpBitmap.setConfig(SkBitmap::kA8_Config, dstM.fBounds.width(),
reed@android.com8a1c16f2008-12-17 15:59:43 +00001504 dstM.fBounds.height(), dstM.fRowBytes);
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001505 if (!tmpBitmap.allocPixels(allocator, NULL)) {
1506 // Allocation of pixels for alpha bitmap failed.
1507 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
1508 tmpBitmap.width(), tmpBitmap.height());
1509 return false;
1510 }
1511 memcpy(tmpBitmap.getPixels(), dstM.fImage, dstM.computeImageSize());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001512 if (offset) {
1513 offset->set(dstM.fBounds.fLeft, dstM.fBounds.fTop);
1514 }
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001515 SkDEBUGCODE(tmpBitmap.validate();)
1516
1517 tmpBitmap.swap(*dst);
1518 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001519}
1520
1521///////////////////////////////////////////////////////////////////////////////
1522
1523enum {
1524 SERIALIZE_PIXELTYPE_NONE,
djsollen@google.com21830d92012-08-07 19:49:41 +00001525 SERIALIZE_PIXELTYPE_REF_DATA
reed@android.com8a1c16f2008-12-17 15:59:43 +00001526};
1527
reed@android.com8a1c16f2008-12-17 15:59:43 +00001528void SkBitmap::flatten(SkFlattenableWriteBuffer& buffer) const {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001529 buffer.writeInt(fWidth);
1530 buffer.writeInt(fHeight);
1531 buffer.writeInt(fRowBytes);
1532 buffer.writeInt(fConfig);
reed@google.com383a6972013-10-21 14:00:07 +00001533 buffer.writeInt(fAlphaType);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001534
reed@android.com8a1c16f2008-12-17 15:59:43 +00001535 if (fPixelRef) {
djsollen@google.com5370cd92012-03-28 20:47:01 +00001536 if (fPixelRef->getFactory()) {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001537 buffer.writeInt(SERIALIZE_PIXELTYPE_REF_DATA);
scroggo@google.come5f48242013-02-25 21:47:41 +00001538 buffer.writeUInt(SkToU32(fPixelRefOffset));
djsollen@google.com5370cd92012-03-28 20:47:01 +00001539 buffer.writeFlattenable(fPixelRef);
1540 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001541 }
1542 // if we get here, we can't record the pixels
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001543 buffer.writeInt(SERIALIZE_PIXELTYPE_NONE);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001544 } else {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001545 buffer.writeInt(SERIALIZE_PIXELTYPE_NONE);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001546 }
1547}
1548
1549void SkBitmap::unflatten(SkFlattenableReadBuffer& buffer) {
1550 this->reset();
weita@google.comf9ab99a2009-05-03 18:23:30 +00001551
reed@android.com8a1c16f2008-12-17 15:59:43 +00001552 int width = buffer.readInt();
1553 int height = buffer.readInt();
1554 int rowBytes = buffer.readInt();
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +00001555 Config config = (Config)buffer.readInt();
1556 SkAlphaType alphaType = (SkAlphaType)buffer.readInt();
1557 buffer.validate((width >= 0) && (height >= 0) && (rowBytes >= 0) &&
1558 SkIsValidConfig(config) && validate_alphaType(config, alphaType));
weita@google.comf9ab99a2009-05-03 18:23:30 +00001559
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +00001560 this->setConfig(config, width, height, rowBytes, alphaType);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +00001561 buffer.validate(fRowBytes >= (fWidth * fBytesPerPixel));
weita@google.comf9ab99a2009-05-03 18:23:30 +00001562
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001563 int reftype = buffer.readInt();
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +00001564 if (buffer.validate((SERIALIZE_PIXELTYPE_REF_DATA == reftype) ||
1565 (SERIALIZE_PIXELTYPE_NONE == reftype))) {
1566 switch (reftype) {
1567 case SERIALIZE_PIXELTYPE_REF_DATA: {
1568 size_t offset = buffer.readUInt();
1569 SkPixelRef* pr = buffer.readPixelRef();
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +00001570 if (!buffer.validate((NULL == pr) ||
1571 (pr->getAllocatedSizeInBytes() >= (offset + this->getSafeSize())))) {
1572 offset = 0;
1573 }
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +00001574 SkSafeUnref(this->setPixelRef(pr, offset));
1575 break;
1576 }
1577 case SERIALIZE_PIXELTYPE_NONE:
1578 break;
1579 default:
1580 SkDEBUGFAIL("unrecognized pixeltype in serialized data");
1581 sk_throw();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001582 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001583 }
1584}
1585
1586///////////////////////////////////////////////////////////////////////////////
1587
1588SkBitmap::RLEPixels::RLEPixels(int width, int height) {
1589 fHeight = height;
commit-bot@chromium.org235002f2013-10-09 18:39:59 +00001590 fYPtrs = (uint8_t**)sk_calloc_throw(height * sizeof(uint8_t*));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001591}
1592
1593SkBitmap::RLEPixels::~RLEPixels() {
1594 sk_free(fYPtrs);
1595}
1596
1597///////////////////////////////////////////////////////////////////////////////
1598
1599#ifdef SK_DEBUG
1600void SkBitmap::validate() const {
1601 SkASSERT(fConfig < kConfigCount);
1602 SkASSERT(fRowBytes >= (unsigned)ComputeRowBytes((Config)fConfig, fWidth));
scroggo@google.com8e990eb2013-06-14 15:55:56 +00001603 uint8_t allFlags = kImageIsOpaque_Flag | kImageIsVolatile_Flag | kImageIsImmutable_Flag;
1604#ifdef SK_BUILD_FOR_ANDROID
1605 allFlags |= kHasHardwareMipMap_Flag;
1606#endif
1607 SkASSERT(fFlags <= allFlags);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001608 SkASSERT(fPixelLockCount >= 0);
1609 SkASSERT(NULL == fColorTable || (unsigned)fColorTable->getRefCnt() < 10000);
1610 SkASSERT((uint8_t)ComputeBytesPerPixel((Config)fConfig) == fBytesPerPixel);
1611
1612#if 0 // these asserts are not thread-correct, so disable for now
1613 if (fPixelRef) {
1614 if (fPixelLockCount > 0) {
reed@google.comff0da4f2012-05-17 13:14:52 +00001615 SkASSERT(fPixelRef->isLocked());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001616 } else {
1617 SkASSERT(NULL == fPixels);
1618 SkASSERT(NULL == fColorTable);
1619 }
1620 }
1621#endif
1622}
1623#endif
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001624
1625#ifdef SK_DEVELOPER
1626void SkBitmap::toString(SkString* str) const {
1627
1628 static const char* gConfigNames[kConfigCount] = {
rmistry@google.comd6bab022013-12-02 13:50:38 +00001629 "NONE", "A8", "INDEX8", "565", "4444", "8888"
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001630 };
1631
1632 str->appendf("bitmap: ((%d, %d) %s", this->width(), this->height(),
1633 gConfigNames[this->config()]);
1634
1635 str->append(" (");
1636 if (this->isOpaque()) {
1637 str->append("opaque");
1638 } else {
1639 str->append("transparent");
1640 }
1641 if (this->isImmutable()) {
1642 str->append(", immutable");
1643 } else {
1644 str->append(", not-immutable");
1645 }
1646 str->append(")");
1647
1648 SkPixelRef* pr = this->pixelRef();
1649 if (NULL == pr) {
1650 // show null or the explicit pixel address (rare)
1651 str->appendf(" pixels:%p", this->getPixels());
1652 } else {
1653 const char* uri = pr->getURI();
1654 if (NULL != uri) {
1655 str->appendf(" uri:\"%s\"", uri);
1656 } else {
1657 str->appendf(" pixelref:%p", pr);
1658 }
1659 }
1660
1661 str->append(")");
1662}
1663#endif