blob: 27ea35e974b8c4423ed58671e4b22b936cf00c75 [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.com8a1c16f2008-12-17 15:59:43 +000027struct MipLevel {
28 void* fPixels;
29 uint32_t fRowBytes;
reed@android.comf459a492009-03-27 12:33:50 +000030 uint32_t fWidth, fHeight;
reed@android.com8a1c16f2008-12-17 15:59:43 +000031};
32
33struct SkBitmap::MipMap : SkNoncopyable {
34 int32_t fRefCnt;
35 int fLevelCount;
36// MipLevel fLevel[fLevelCount];
37// Pixels[]
weita@google.comf9ab99a2009-05-03 18:23:30 +000038
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 static MipMap* Alloc(int levelCount, size_t pixelSize) {
reed@android.com149e2f62009-05-22 14:39:03 +000040 if (levelCount < 0) {
41 return NULL;
42 }
reed@google.com57212f92013-12-30 14:40:38 +000043 int64_t size = (levelCount + 1) * sizeof(MipLevel);
44 size += sizeof(MipMap) + pixelSize;
45 if (!sk_64_isS32(size)) {
reed@android.com149e2f62009-05-22 14:39:03 +000046 return NULL;
47 }
reed@google.com57212f92013-12-30 14:40:38 +000048 MipMap* mm = (MipMap*)sk_malloc_throw(sk_64_asS32(size));
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 mm->fRefCnt = 1;
50 mm->fLevelCount = levelCount;
51 return mm;
52 }
53
54 const MipLevel* levels() const { return (const MipLevel*)(this + 1); }
55 MipLevel* levels() { return (MipLevel*)(this + 1); }
56
57 const void* pixels() const { return levels() + fLevelCount; }
58 void* pixels() { return levels() + fLevelCount; }
weita@google.comf9ab99a2009-05-03 18:23:30 +000059
reed@android.com149e2f62009-05-22 14:39:03 +000060 void ref() {
61 if (SK_MaxS32 == sk_atomic_inc(&fRefCnt)) {
62 sk_throw();
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 }
64 }
reed@android.com149e2f62009-05-22 14:39:03 +000065 void unref() {
66 SkASSERT(fRefCnt > 0);
67 if (sk_atomic_dec(&fRefCnt) == 1) {
68 sk_free(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 }
70 }
71};
reed@android.com8a1c16f2008-12-17 15:59:43 +000072
73///////////////////////////////////////////////////////////////////////////////
74///////////////////////////////////////////////////////////////////////////////
75
76SkBitmap::SkBitmap() {
reed@android.com4516f472009-06-29 16:25:36 +000077 sk_bzero(this, sizeof(*this));
reed@android.com8a1c16f2008-12-17 15:59:43 +000078}
79
80SkBitmap::SkBitmap(const SkBitmap& src) {
81 SkDEBUGCODE(src.validate();)
reed@android.com4516f472009-06-29 16:25:36 +000082 sk_bzero(this, sizeof(*this));
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 *this = src;
84 SkDEBUGCODE(this->validate();)
85}
86
87SkBitmap::~SkBitmap() {
88 SkDEBUGCODE(this->validate();)
89 this->freePixels();
90}
91
92SkBitmap& SkBitmap::operator=(const SkBitmap& src) {
93 if (this != &src) {
94 this->freePixels();
95 memcpy(this, &src, sizeof(src));
96
97 // inc src reference counts
reed@android.com83f7bc32009-07-17 02:42:41 +000098 SkSafeRef(src.fPixelRef);
reed@android.com149e2f62009-05-22 14:39:03 +000099 SkSafeRef(src.fMipMap);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100
101 // we reset our locks if we get blown away
102 fPixelLockCount = 0;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000103
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 /* The src could be in 3 states
105 1. no pixelref, in which case we just copy/ref the pixels/ctable
106 2. unlocked pixelref, pixels/ctable should be null
107 3. locked pixelref, we should lock the ref again ourselves
108 */
109 if (NULL == fPixelRef) {
110 // leave fPixels as it is
reed@google.com82065d62011-02-07 15:30:46 +0000111 SkSafeRef(fColorTable); // ref the user's ctable if present
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112 } else { // we have a pixelref, so pixels/ctable reflect it
113 // ignore the values from the memcpy
114 fPixels = NULL;
115 fColorTable = NULL;
bsalomon@google.com586f48c2011-04-14 15:07:22 +0000116 // Note that what to for genID is somewhat arbitrary. We have no
117 // way to track changes to raw pixels across multiple SkBitmaps.
118 // Would benefit from an SkRawPixelRef type created by
119 // setPixels.
120 // Just leave the memcpy'ed one but they'll get out of sync
121 // as soon either is modified.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122 }
123 }
124
125 SkDEBUGCODE(this->validate();)
126 return *this;
127}
128
129void SkBitmap::swap(SkBitmap& other) {
bsalomon@google.com586f48c2011-04-14 15:07:22 +0000130 SkTSwap(fColorTable, other.fColorTable);
131 SkTSwap(fPixelRef, other.fPixelRef);
reed@google.com672588b2014-01-08 15:42:01 +0000132 SkTSwap(fPixelRefOrigin, other.fPixelRefOrigin);
bsalomon@google.com586f48c2011-04-14 15:07:22 +0000133 SkTSwap(fPixelLockCount, other.fPixelLockCount);
134 SkTSwap(fMipMap, other.fMipMap);
135 SkTSwap(fPixels, other.fPixels);
bsalomon@google.com586f48c2011-04-14 15:07:22 +0000136 SkTSwap(fRowBytes, other.fRowBytes);
137 SkTSwap(fWidth, other.fWidth);
138 SkTSwap(fHeight, other.fHeight);
139 SkTSwap(fConfig, other.fConfig);
reed@google.com383a6972013-10-21 14:00:07 +0000140 SkTSwap(fAlphaType, other.fAlphaType);
bsalomon@google.com586f48c2011-04-14 15:07:22 +0000141 SkTSwap(fFlags, other.fFlags);
142 SkTSwap(fBytesPerPixel, other.fBytesPerPixel);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143
144 SkDEBUGCODE(this->validate();)
145}
146
147void SkBitmap::reset() {
148 this->freePixels();
reed@android.com4516f472009-06-29 16:25:36 +0000149 sk_bzero(this, sizeof(*this));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150}
151
152int SkBitmap::ComputeBytesPerPixel(SkBitmap::Config config) {
153 int bpp;
154 switch (config) {
155 case kNo_Config:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 bpp = 0; // not applicable
157 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158 case kA8_Config:
159 case kIndex8_Config:
160 bpp = 1;
161 break;
162 case kRGB_565_Config:
163 case kARGB_4444_Config:
164 bpp = 2;
165 break;
166 case kARGB_8888_Config:
167 bpp = 4;
168 break;
169 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000170 SkDEBUGFAIL("unknown config");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000171 bpp = 0; // error
172 break;
173 }
174 return bpp;
175}
176
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000177size_t SkBitmap::ComputeRowBytes(Config c, int width) {
reed@android.com149e2f62009-05-22 14:39:03 +0000178 if (width < 0) {
179 return 0;
180 }
181
reed@google.com57212f92013-12-30 14:40:38 +0000182 int64_t rowBytes = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000183
184 switch (c) {
185 case kNo_Config:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000186 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000187 case kA8_Config:
188 case kIndex8_Config:
reed@google.com57212f92013-12-30 14:40:38 +0000189 rowBytes = width;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190 break;
191 case kRGB_565_Config:
192 case kARGB_4444_Config:
reed@google.com48569642013-12-30 19:21:22 +0000193 // assign and then shift, so we don't overflow int
194 rowBytes = width;
195 rowBytes <<= 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196 break;
197 case kARGB_8888_Config:
reed@google.com48569642013-12-30 19:21:22 +0000198 // assign and then shift, so we don't overflow int
199 rowBytes = width;
200 rowBytes <<= 2;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000201 break;
202 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000203 SkDEBUGFAIL("unknown config");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000204 break;
205 }
reed@google.com57212f92013-12-30 14:40:38 +0000206 return sk_64_isS32(rowBytes) ? sk_64_asS32(rowBytes) : 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000207}
208
reed@google.com57212f92013-12-30 14:40:38 +0000209int64_t SkBitmap::ComputeSize64(Config config, int width, int height) {
210 int64_t rowBytes = sk_64_mul(ComputeBytesPerPixel(config), width);
211 return rowBytes * height;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000212}
213
214size_t SkBitmap::ComputeSize(Config c, int width, int height) {
reed@google.com57212f92013-12-30 14:40:38 +0000215 int64_t size = SkBitmap::ComputeSize64(c, width, height);
216 return sk_64_isS32(size) ? sk_64_asS32(size) : 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000217}
218
reed@google.com57212f92013-12-30 14:40:38 +0000219int64_t SkBitmap::ComputeSafeSize64(Config config,
220 uint32_t width,
221 uint32_t height,
222 size_t rowBytes) {
223 int64_t safeSize = 0;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000224 if (height > 0) {
reed@google.com57212f92013-12-30 14:40:38 +0000225 int64_t lastRow = sk_64_mul(ComputeBytesPerPixel(config), width);
226 safeSize = sk_64_mul(height - 1, rowBytes) + lastRow;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000227 }
reed@google.com57212f92013-12-30 14:40:38 +0000228 SkASSERT(safeSize >= 0);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000229 return safeSize;
230}
231
232size_t SkBitmap::ComputeSafeSize(Config config,
233 uint32_t width,
234 uint32_t height,
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000235 size_t rowBytes) {
reed@google.com57212f92013-12-30 14:40:38 +0000236 int64_t safeSize = ComputeSafeSize64(config, width, height, rowBytes);
237 int32_t safeSize32 = (int32_t)safeSize;
skia.committer@gmail.comf5e1f632013-12-31 07:01:36 +0000238
reed@google.com57212f92013-12-30 14:40:38 +0000239 if (safeSize32 != safeSize) {
240 safeSize32 = 0;
241 }
242 return safeSize32;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000243}
244
reed@google.com86b2e432012-03-15 21:17:03 +0000245void SkBitmap::getBounds(SkRect* bounds) const {
246 SkASSERT(bounds);
247 bounds->set(0, 0,
248 SkIntToScalar(fWidth), SkIntToScalar(fHeight));
249}
250
reed@google.com80e14592012-03-16 14:58:07 +0000251void SkBitmap::getBounds(SkIRect* bounds) const {
252 SkASSERT(bounds);
253 bounds->set(0, 0, fWidth, fHeight);
254}
255
reed@google.com86b2e432012-03-15 21:17:03 +0000256///////////////////////////////////////////////////////////////////////////////
257
reed@google.com383a6972013-10-21 14:00:07 +0000258static bool validate_alphaType(SkBitmap::Config config, SkAlphaType alphaType,
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000259 SkAlphaType* canonical = NULL) {
reed@google.com383a6972013-10-21 14:00:07 +0000260 switch (config) {
261 case SkBitmap::kNo_Config:
262 alphaType = kIgnore_SkAlphaType;
263 break;
reed@google.com383a6972013-10-21 14:00:07 +0000264 case SkBitmap::kA8_Config:
265 if (kUnpremul_SkAlphaType == alphaType) {
266 alphaType = kPremul_SkAlphaType;
267 }
268 // fall-through
269 case SkBitmap::kIndex8_Config:
270 case SkBitmap::kARGB_4444_Config:
271 case SkBitmap::kARGB_8888_Config:
272 if (kIgnore_SkAlphaType == alphaType) {
273 return false;
274 }
275 break;
276 case SkBitmap::kRGB_565_Config:
277 alphaType = kOpaque_SkAlphaType;
278 break;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000279 default:
280 return false;
reed@android.com149e2f62009-05-22 14:39:03 +0000281 }
reed@google.com383a6972013-10-21 14:00:07 +0000282 if (canonical) {
283 *canonical = alphaType;
284 }
285 return true;
286}
reed@android.com149e2f62009-05-22 14:39:03 +0000287
reed@google.com383a6972013-10-21 14:00:07 +0000288bool SkBitmap::setConfig(Config config, int width, int height, size_t rowBytes,
289 SkAlphaType alphaType) {
290 if ((width | height) < 0) {
reed@google.com9cd697c2013-10-21 14:12:13 +0000291 goto BAD_CONFIG;
reed@google.com383a6972013-10-21 14:00:07 +0000292 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000293 if (rowBytes == 0) {
reed@google.com383a6972013-10-21 14:00:07 +0000294 rowBytes = SkBitmap::ComputeRowBytes(config, width);
halcanary@google.com44287342013-12-13 18:29:51 +0000295 if (0 == rowBytes && kNo_Config != config && width > 0) {
reed@google.com9cd697c2013-10-21 14:12:13 +0000296 goto BAD_CONFIG;
reed@android.com149e2f62009-05-22 14:39:03 +0000297 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000298 }
reed@android.com89bb83a2009-05-29 21:30:42 +0000299
reed@google.com383a6972013-10-21 14:00:07 +0000300 if (!validate_alphaType(config, alphaType, &alphaType)) {
reed@google.com9cd697c2013-10-21 14:12:13 +0000301 goto BAD_CONFIG;
reed@google.com383a6972013-10-21 14:00:07 +0000302 }
303
304 this->freePixels();
305
306 fConfig = SkToU8(config);
307 fAlphaType = SkToU8(alphaType);
reed@android.comf459a492009-03-27 12:33:50 +0000308 fWidth = width;
309 fHeight = height;
scroggo@google.come5f48242013-02-25 21:47:41 +0000310 fRowBytes = SkToU32(rowBytes);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000311
reed@google.com383a6972013-10-21 14:00:07 +0000312 fBytesPerPixel = (uint8_t)ComputeBytesPerPixel(config);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000313
314 SkDEBUGCODE(this->validate();)
reed@google.com383a6972013-10-21 14:00:07 +0000315 return true;
agl@chromium.org6b8cb252009-06-01 23:52:37 +0000316
reed@android.com9e0c2fc2009-06-02 18:54:28 +0000317 // if we got here, we had an error, so we reset the bitmap to empty
reed@google.com9cd697c2013-10-21 14:12:13 +0000318BAD_CONFIG:
agl@chromium.org6b8cb252009-06-01 23:52:37 +0000319 this->reset();
reed@google.com383a6972013-10-21 14:00:07 +0000320 return false;
321}
322
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000323bool SkBitmap::setConfig(const SkImageInfo& info, size_t rowBytes) {
324 return this->setConfig(SkImageInfoToBitmapConfig(info), info.fWidth,
325 info.fHeight, rowBytes, info.fAlphaType);
326}
327
reed@google.com383a6972013-10-21 14:00:07 +0000328bool SkBitmap::setAlphaType(SkAlphaType alphaType) {
329 if (!validate_alphaType(this->config(), alphaType, &alphaType)) {
330 return false;
331 }
332 fAlphaType = SkToU8(alphaType);
333 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000334}
335
336void SkBitmap::updatePixelsFromRef() const {
337 if (NULL != fPixelRef) {
338 if (fPixelLockCount > 0) {
reed@google.comff0da4f2012-05-17 13:14:52 +0000339 SkASSERT(fPixelRef->isLocked());
weita@google.comf9ab99a2009-05-03 18:23:30 +0000340
reed@android.com8a1c16f2008-12-17 15:59:43 +0000341 void* p = fPixelRef->pixels();
342 if (NULL != p) {
reed@google.com672588b2014-01-08 15:42:01 +0000343 p = (char*)p
reed@google.com303c4752014-01-09 20:00:14 +0000344 + fPixelRefOrigin.fY * fRowBytes
reed@google.com672588b2014-01-08 15:42:01 +0000345 + fPixelRefOrigin.fX * fBytesPerPixel;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000346 }
347 fPixels = p;
348 SkRefCnt_SafeAssign(fColorTable, fPixelRef->colorTable());
349 } else {
350 SkASSERT(0 == fPixelLockCount);
351 fPixels = NULL;
reed@android.com149e2f62009-05-22 14:39:03 +0000352 if (fColorTable) {
353 fColorTable->unref();
354 fColorTable = NULL;
355 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000356 }
357 }
358}
359
reed@google.com9230ea22013-12-09 22:01:03 +0000360static bool config_to_colorType(SkBitmap::Config config, SkColorType* ctOut) {
361 SkColorType ct;
362 switch (config) {
363 case SkBitmap::kA8_Config:
364 ct = kAlpha_8_SkColorType;
365 break;
366 case SkBitmap::kIndex8_Config:
367 ct = kIndex_8_SkColorType;
368 break;
369 case SkBitmap::kRGB_565_Config:
370 ct = kRGB_565_SkColorType;
371 break;
372 case SkBitmap::kARGB_4444_Config:
373 ct = kARGB_4444_SkColorType;
374 break;
375 case SkBitmap::kARGB_8888_Config:
376 ct = kPMColor_SkColorType;
377 break;
378 case SkBitmap::kNo_Config:
379 default:
380 return false;
381 }
382 if (ctOut) {
383 *ctOut = ct;
384 }
385 return true;
386}
387
388bool SkBitmap::asImageInfo(SkImageInfo* info) const {
389 SkColorType ct;
390 if (!config_to_colorType(this->config(), &ct)) {
391 return false;
392 }
393 if (info) {
394 info->fWidth = fWidth;
395 info->fHeight = fHeight;
396 info->fAlphaType = this->alphaType();
397 info->fColorType = ct;
398 }
399 return true;
400}
401
reed@google.com672588b2014-01-08 15:42:01 +0000402SkPixelRef* SkBitmap::setPixelRef(SkPixelRef* pr, int dx, int dy) {
reed@google.comdcea5302014-01-03 13:43:01 +0000403#ifdef SK_DEBUG
reed@google.com672588b2014-01-08 15:42:01 +0000404 if (pr) {
reed@google.comdcea5302014-01-03 13:43:01 +0000405 SkImageInfo info;
406 if (this->asImageInfo(&info)) {
407 const SkImageInfo& prInfo = pr->info();
408 SkASSERT(info.fWidth <= prInfo.fWidth);
409 SkASSERT(info.fHeight <= prInfo.fHeight);
410 SkASSERT(info.fColorType == prInfo.fColorType);
411 switch (prInfo.fAlphaType) {
412 case kIgnore_SkAlphaType:
413 SkASSERT(fAlphaType == kIgnore_SkAlphaType);
414 break;
415 case kOpaque_SkAlphaType:
416 case kPremul_SkAlphaType:
417 SkASSERT(info.fAlphaType == kOpaque_SkAlphaType ||
418 info.fAlphaType == kPremul_SkAlphaType);
419 break;
420 case kUnpremul_SkAlphaType:
421 SkASSERT(info.fAlphaType == kOpaque_SkAlphaType ||
422 info.fAlphaType == kUnpremul_SkAlphaType);
423 break;
424 }
425 }
426 }
427#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000428
reed@google.com672588b2014-01-08 15:42:01 +0000429 if (pr) {
430 const SkImageInfo& info = pr->info();
431 fPixelRefOrigin.set(SkPin32(dx, 0, info.fWidth),
432 SkPin32(dy, 0, info.fHeight));
433 } else {
434 // ignore dx,dy if there is no pixelref
435 fPixelRefOrigin.setZero();
436 }
437
438 if (fPixelRef != pr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000439 if (fPixelRef != pr) {
440 this->freePixels();
441 SkASSERT(NULL == fPixelRef);
weita@google.comf9ab99a2009-05-03 18:23:30 +0000442
reed@google.com82065d62011-02-07 15:30:46 +0000443 SkSafeRef(pr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000444 fPixelRef = pr;
445 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000446 this->updatePixelsFromRef();
447 }
448
449 SkDEBUGCODE(this->validate();)
450 return pr;
451}
452
453void SkBitmap::lockPixels() const {
djsollen@google.com7c6d2642013-08-06 12:19:38 +0000454 if (NULL != fPixelRef && 0 == sk_atomic_inc(&fPixelLockCount)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000455 fPixelRef->lockPixels();
456 this->updatePixelsFromRef();
457 }
458 SkDEBUGCODE(this->validate();)
459}
460
461void SkBitmap::unlockPixels() const {
462 SkASSERT(NULL == fPixelRef || fPixelLockCount > 0);
463
djsollen@google.com7c6d2642013-08-06 12:19:38 +0000464 if (NULL != fPixelRef && 1 == sk_atomic_dec(&fPixelLockCount)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000465 fPixelRef->unlockPixels();
466 this->updatePixelsFromRef();
467 }
468 SkDEBUGCODE(this->validate();)
469}
470
reed@google.com9c49bc32011-07-07 13:42:37 +0000471bool SkBitmap::lockPixelsAreWritable() const {
djsollen@google.comc84b8332012-07-27 13:41:44 +0000472 return (fPixelRef) ? fPixelRef->lockPixelsAreWritable() : false;
reed@google.com9c49bc32011-07-07 13:42:37 +0000473}
474
reed@android.com8a1c16f2008-12-17 15:59:43 +0000475void SkBitmap::setPixels(void* p, SkColorTable* ctable) {
reed@google.com8e1034e2012-07-30 13:16:35 +0000476 if (NULL == p) {
reed@google.com672588b2014-01-08 15:42:01 +0000477 this->setPixelRef(NULL);
reed@google.com8e1034e2012-07-30 13:16:35 +0000478 return;
479 }
480
reed@google.combf790232013-12-13 19:45:58 +0000481 SkImageInfo info;
482 if (!this->asImageInfo(&info)) {
reed@google.com672588b2014-01-08 15:42:01 +0000483 this->setPixelRef(NULL);
reed@google.combf790232013-12-13 19:45:58 +0000484 return;
485 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000486
reed@google.combf790232013-12-13 19:45:58 +0000487 SkPixelRef* pr = SkMallocPixelRef::NewDirect(info, p, fRowBytes, ctable);
488 if (NULL == pr) {
reed@google.com672588b2014-01-08 15:42:01 +0000489 this->setPixelRef(NULL);
reed@google.combf790232013-12-13 19:45:58 +0000490 return;
491 }
492
493 this->setPixelRef(pr)->unref();
494
djsollen@google.comc84b8332012-07-27 13:41:44 +0000495 // since we're already allocated, we lockPixels right away
496 this->lockPixels();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000497 SkDEBUGCODE(this->validate();)
498}
499
500bool SkBitmap::allocPixels(Allocator* allocator, SkColorTable* ctable) {
501 HeapAllocator stdalloc;
502
503 if (NULL == allocator) {
504 allocator = &stdalloc;
505 }
506 return allocator->allocPixelRef(this, ctable);
507}
508
509void SkBitmap::freePixels() {
510 // if we're gonna free the pixels, we certainly need to free the mipmap
511 this->freeMipMap();
512
reed@android.com149e2f62009-05-22 14:39:03 +0000513 if (fColorTable) {
514 fColorTable->unref();
515 fColorTable = NULL;
516 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000517
518 if (NULL != fPixelRef) {
519 if (fPixelLockCount > 0) {
520 fPixelRef->unlockPixels();
521 }
522 fPixelRef->unref();
523 fPixelRef = NULL;
reed@google.com672588b2014-01-08 15:42:01 +0000524 fPixelRefOrigin.setZero();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000525 }
526 fPixelLockCount = 0;
527 fPixels = NULL;
528}
529
530void SkBitmap::freeMipMap() {
reed@android.com149e2f62009-05-22 14:39:03 +0000531 if (fMipMap) {
532 fMipMap->unref();
533 fMipMap = NULL;
534 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000535}
536
537uint32_t SkBitmap::getGenerationID() const {
djsollen@google.comc84b8332012-07-27 13:41:44 +0000538 return (fPixelRef) ? fPixelRef->getGenerationID() : 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000539}
540
541void SkBitmap::notifyPixelsChanged() const {
junov@chromium.orgb0521292011-12-15 20:14:06 +0000542 SkASSERT(!this->isImmutable());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000543 if (fPixelRef) {
544 fPixelRef->notifyPixelsChanged();
545 }
546}
547
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000548GrTexture* SkBitmap::getTexture() const {
reed@android.comce4e53a2010-09-09 16:01:26 +0000549 return fPixelRef ? fPixelRef->getTexture() : NULL;
550}
551
reed@android.com8a1c16f2008-12-17 15:59:43 +0000552///////////////////////////////////////////////////////////////////////////////
553
reed@android.com8a1c16f2008-12-17 15:59:43 +0000554/** We explicitly use the same allocator for our pixels that SkMask does,
555 so that we can freely assign memory allocated by one class to the other.
556 */
557bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst,
558 SkColorTable* ctable) {
reed@google.combf790232013-12-13 19:45:58 +0000559 SkImageInfo info;
560 if (!dst->asImageInfo(&info)) {
561// SkDebugf("unsupported config for info %d\n", dst->config());
562 return false;
563 }
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000564
reed@google.combf790232013-12-13 19:45:58 +0000565 SkPixelRef* pr = SkMallocPixelRef::NewAllocate(info, dst->rowBytes(),
566 ctable);
567 if (NULL == pr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000568 return false;
569 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000570
reed@google.com672588b2014-01-08 15:42:01 +0000571 dst->setPixelRef(pr)->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000572 // since we're already allocated, we lockPixels right away
573 dst->lockPixels();
574 return true;
575}
576
577///////////////////////////////////////////////////////////////////////////////
578
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000579size_t SkBitmap::getSafeSize() const {
580 // This is intended to be a size_t version of ComputeSafeSize64(), just
581 // faster. The computation is meant to be identical.
582 return (fHeight ? ((fHeight - 1) * fRowBytes) +
reed@google.com44699382013-10-31 17:28:30 +0000583 ComputeRowBytes(this->config(), fWidth): 0);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000584}
585
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000586bool SkBitmap::copyPixelsTo(void* const dst, size_t dstSize,
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000587 size_t dstRowBytes, bool preserveDstPad) const {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000588
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000589 if (0 == dstRowBytes) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000590 dstRowBytes = fRowBytes;
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000591 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000592
reed@google.com44699382013-10-31 17:28:30 +0000593 if (dstRowBytes < ComputeRowBytes(this->config(), fWidth) ||
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000594 dst == NULL || (getPixels() == NULL && pixelRef() == NULL))
595 return false;
596
bsalomon@google.comc6980972011-11-02 19:57:21 +0000597 if (!preserveDstPad && static_cast<uint32_t>(dstRowBytes) == fRowBytes) {
reed@google.com44699382013-10-31 17:28:30 +0000598 size_t safeSize = this->getSafeSize();
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000599 if (safeSize > dstSize || safeSize == 0)
600 return false;
601 else {
602 SkAutoLockPixels lock(*this);
603 // This implementation will write bytes beyond the end of each row,
604 // excluding the last row, if the bitmap's stride is greater than
605 // strictly required by the current config.
606 memcpy(dst, getPixels(), safeSize);
607
608 return true;
609 }
610 } else {
611 // If destination has different stride than us, then copy line by line.
reed@google.com44699382013-10-31 17:28:30 +0000612 if (ComputeSafeSize(this->config(), fWidth, fHeight, dstRowBytes) >
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000613 dstSize)
614 return false;
615 else {
616 // Just copy what we need on each line.
reed@google.com44699382013-10-31 17:28:30 +0000617 size_t rowBytes = ComputeRowBytes(this->config(), fWidth);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000618 SkAutoLockPixels lock(*this);
619 const uint8_t* srcP = reinterpret_cast<const uint8_t*>(getPixels());
620 uint8_t* dstP = reinterpret_cast<uint8_t*>(dst);
621 for (uint32_t row = 0; row < fHeight;
622 row++, srcP += fRowBytes, dstP += dstRowBytes) {
623 memcpy(dstP, srcP, rowBytes);
624 }
625
626 return true;
627 }
628 }
629}
630
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000631///////////////////////////////////////////////////////////////////////////////
632
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000633bool SkBitmap::isImmutable() const {
junov@chromium.orgb0521292011-12-15 20:14:06 +0000634 return fPixelRef ? fPixelRef->isImmutable() :
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000635 fFlags & kImageIsImmutable_Flag;
junov@chromium.orgb0521292011-12-15 20:14:06 +0000636}
637
638void SkBitmap::setImmutable() {
639 if (fPixelRef) {
640 fPixelRef->setImmutable();
641 } else {
642 fFlags |= kImageIsImmutable_Flag;
643 }
644}
645
junov@google.com4ee7ae52011-06-30 17:30:49 +0000646bool SkBitmap::isVolatile() const {
647 return (fFlags & kImageIsVolatile_Flag) != 0;
648}
649
650void SkBitmap::setIsVolatile(bool isVolatile) {
651 if (isVolatile) {
652 fFlags |= kImageIsVolatile_Flag;
653 } else {
654 fFlags &= ~kImageIsVolatile_Flag;
655 }
656}
657
reed@android.com8a1c16f2008-12-17 15:59:43 +0000658void* SkBitmap::getAddr(int x, int y) const {
659 SkASSERT((unsigned)x < (unsigned)this->width());
660 SkASSERT((unsigned)y < (unsigned)this->height());
661
662 char* base = (char*)this->getPixels();
663 if (base) {
664 base += y * this->rowBytes();
665 switch (this->config()) {
666 case SkBitmap::kARGB_8888_Config:
667 base += x << 2;
668 break;
669 case SkBitmap::kARGB_4444_Config:
670 case SkBitmap::kRGB_565_Config:
671 base += x << 1;
672 break;
673 case SkBitmap::kA8_Config:
674 case SkBitmap::kIndex8_Config:
675 base += x;
676 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000677 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000678 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000679 SkDEBUGFAIL("Can't return addr for config");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000680 base = NULL;
681 break;
682 }
683 }
684 return base;
685}
686
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000687SkColor SkBitmap::getColor(int x, int y) const {
688 SkASSERT((unsigned)x < (unsigned)this->width());
689 SkASSERT((unsigned)y < (unsigned)this->height());
690
691 switch (this->config()) {
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000692 case SkBitmap::kA8_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000693 uint8_t* addr = this->getAddr8(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000694 return SkColorSetA(0, addr[0]);
695 }
696 case SkBitmap::kIndex8_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000697 SkPMColor c = this->getIndex8Color(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000698 return SkUnPreMultiply::PMColorToColor(c);
699 }
700 case SkBitmap::kRGB_565_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000701 uint16_t* addr = this->getAddr16(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000702 return SkPixel16ToColor(addr[0]);
703 }
704 case SkBitmap::kARGB_4444_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000705 uint16_t* addr = this->getAddr16(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000706 SkPMColor c = SkPixel4444ToPixel32(addr[0]);
707 return SkUnPreMultiply::PMColorToColor(c);
708 }
709 case SkBitmap::kARGB_8888_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000710 uint32_t* addr = this->getAddr32(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000711 return SkUnPreMultiply::PMColorToColor(addr[0]);
712 }
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000713 case kNo_Config:
rmistry@google.comd6bab022013-12-02 13:50:38 +0000714 default:
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000715 SkASSERT(false);
716 return 0;
717 }
718 SkASSERT(false); // Not reached.
719 return 0;
720}
721
reed@google.com2a7579d2012-11-07 18:30:18 +0000722bool SkBitmap::ComputeIsOpaque(const SkBitmap& bm) {
723 SkAutoLockPixels alp(bm);
724 if (!bm.getPixels()) {
725 return false;
726 }
727
728 const int height = bm.height();
729 const int width = bm.width();
730
731 switch (bm.config()) {
reed@google.com2a7579d2012-11-07 18:30:18 +0000732 case SkBitmap::kA8_Config: {
733 unsigned a = 0xFF;
734 for (int y = 0; y < height; ++y) {
735 const uint8_t* row = bm.getAddr8(0, y);
736 for (int x = 0; x < width; ++x) {
737 a &= row[x];
738 }
739 if (0xFF != a) {
740 return false;
741 }
742 }
743 return true;
744 } break;
reed@google.com2a7579d2012-11-07 18:30:18 +0000745 case SkBitmap::kIndex8_Config: {
746 SkAutoLockColors alc(bm);
747 const SkPMColor* table = alc.colors();
748 if (!table) {
749 return false;
750 }
reed@google.com140d7282013-01-07 20:25:04 +0000751 SkPMColor c = (SkPMColor)~0;
reed@google.com2a7579d2012-11-07 18:30:18 +0000752 for (int i = bm.getColorTable()->count() - 1; i >= 0; --i) {
753 c &= table[i];
754 }
755 return 0xFF == SkGetPackedA32(c);
756 } break;
757 case SkBitmap::kRGB_565_Config:
758 return true;
759 break;
760 case SkBitmap::kARGB_4444_Config: {
761 unsigned c = 0xFFFF;
762 for (int y = 0; y < height; ++y) {
763 const SkPMColor16* row = bm.getAddr16(0, y);
764 for (int x = 0; x < width; ++x) {
765 c &= row[x];
766 }
767 if (0xF != SkGetPackedA4444(c)) {
768 return false;
769 }
770 }
771 return true;
772 } break;
773 case SkBitmap::kARGB_8888_Config: {
reed@google.com140d7282013-01-07 20:25:04 +0000774 SkPMColor c = (SkPMColor)~0;
reed@google.com2a7579d2012-11-07 18:30:18 +0000775 for (int y = 0; y < height; ++y) {
776 const SkPMColor* row = bm.getAddr32(0, y);
777 for (int x = 0; x < width; ++x) {
778 c &= row[x];
779 }
780 if (0xFF != SkGetPackedA32(c)) {
781 return false;
782 }
783 }
784 return true;
785 }
786 default:
787 break;
788 }
789 return false;
790}
791
792
reed@android.com8a1c16f2008-12-17 15:59:43 +0000793///////////////////////////////////////////////////////////////////////////////
794///////////////////////////////////////////////////////////////////////////////
795
reed@google.com45f746f2013-06-21 19:51:31 +0000796static uint16_t pack_8888_to_4444(unsigned a, unsigned r, unsigned g, unsigned b) {
797 unsigned pixel = (SkA32To4444(a) << SK_A4444_SHIFT) |
798 (SkR32To4444(r) << SK_R4444_SHIFT) |
799 (SkG32To4444(g) << SK_G4444_SHIFT) |
800 (SkB32To4444(b) << SK_B4444_SHIFT);
801 return SkToU16(pixel);
802}
803
reed@google.com60d32352013-06-28 19:40:50 +0000804void SkBitmap::internalErase(const SkIRect& area,
805 U8CPU a, U8CPU r, U8CPU g, U8CPU b) const {
806#ifdef SK_DEBUG
reed@android.com8a1c16f2008-12-17 15:59:43 +0000807 SkDEBUGCODE(this->validate();)
reed@google.com60d32352013-06-28 19:40:50 +0000808 SkASSERT(!area.isEmpty());
809 {
reed@google.com92833f92013-06-28 19:47:43 +0000810 SkIRect total = { 0, 0, this->width(), this->height() };
reed@google.com60d32352013-06-28 19:40:50 +0000811 SkASSERT(total.contains(area));
812 }
813#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000814
reed@google.com60d32352013-06-28 19:40:50 +0000815 if (kNo_Config == fConfig || kIndex8_Config == fConfig) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000816 return;
817 }
818
819 SkAutoLockPixels alp(*this);
820 // perform this check after the lock call
821 if (!this->readyToDraw()) {
822 return;
823 }
824
reed@google.com60d32352013-06-28 19:40:50 +0000825 int height = area.height();
826 const int width = area.width();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000827 const int rowBytes = fRowBytes;
828
829 // make rgb premultiplied
830 if (255 != a) {
831 r = SkAlphaMul(r, a);
832 g = SkAlphaMul(g, a);
833 b = SkAlphaMul(b, a);
834 }
835
836 switch (fConfig) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000837 case kA8_Config: {
reed@google.com60d32352013-06-28 19:40:50 +0000838 uint8_t* p = this->getAddr8(area.fLeft, area.fTop);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000839 while (--height >= 0) {
840 memset(p, a, width);
841 p += rowBytes;
842 }
843 break;
844 }
reed@google.com45f746f2013-06-21 19:51:31 +0000845 case kARGB_4444_Config:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000846 case kRGB_565_Config: {
reed@google.com60d32352013-06-28 19:40:50 +0000847 uint16_t* p = this->getAddr16(area.fLeft, area.fTop);;
reed@google.com45f746f2013-06-21 19:51:31 +0000848 uint16_t v;
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000849
reed@google.com45f746f2013-06-21 19:51:31 +0000850 if (kARGB_4444_Config == fConfig) {
851 v = pack_8888_to_4444(a, r, g, b);
852 } else {
853 v = SkPackRGB16(r >> (8 - SK_R16_BITS),
854 g >> (8 - SK_G16_BITS),
855 b >> (8 - SK_B16_BITS));
856 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000857 while (--height >= 0) {
858 sk_memset16(p, v, width);
859 p = (uint16_t*)((char*)p + rowBytes);
860 }
861 break;
862 }
863 case kARGB_8888_Config: {
reed@google.com60d32352013-06-28 19:40:50 +0000864 uint32_t* p = this->getAddr32(area.fLeft, area.fTop);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000865 uint32_t v = SkPackARGB32(a, r, g, b);
866
867 while (--height >= 0) {
868 sk_memset32(p, v, width);
869 p = (uint32_t*)((char*)p + rowBytes);
870 }
871 break;
872 }
873 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000874
reed@android.com8a1c16f2008-12-17 15:59:43 +0000875 this->notifyPixelsChanged();
876}
877
reed@google.com60d32352013-06-28 19:40:50 +0000878void SkBitmap::eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const {
reed@google.com92833f92013-06-28 19:47:43 +0000879 SkIRect area = { 0, 0, this->width(), this->height() };
reed@google.com60d32352013-06-28 19:40:50 +0000880 if (!area.isEmpty()) {
881 this->internalErase(area, a, r, g, b);
882 }
883}
884
885void SkBitmap::eraseArea(const SkIRect& rect, SkColor c) const {
reed@google.com92833f92013-06-28 19:47:43 +0000886 SkIRect area = { 0, 0, this->width(), this->height() };
reed@google.com60d32352013-06-28 19:40:50 +0000887 if (area.intersect(rect)) {
888 this->internalErase(area, SkColorGetA(c), SkColorGetR(c),
889 SkColorGetG(c), SkColorGetB(c));
890 }
891}
892
reed@android.com8a1c16f2008-12-17 15:59:43 +0000893//////////////////////////////////////////////////////////////////////////////////////
894//////////////////////////////////////////////////////////////////////////////////////
895
reed@android.com8a1c16f2008-12-17 15:59:43 +0000896bool SkBitmap::extractSubset(SkBitmap* result, const SkIRect& subset) const {
897 SkDEBUGCODE(this->validate();)
898
djsollen@google.comc84b8332012-07-27 13:41:44 +0000899 if (NULL == result || NULL == fPixelRef) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000900 return false; // no src pixels
901 }
902
903 SkIRect srcRect, r;
904 srcRect.set(0, 0, this->width(), this->height());
905 if (!r.intersect(srcRect, subset)) {
906 return false; // r is empty (i.e. no intersection)
907 }
908
scroggo@google.coma2a31922012-12-07 19:14:45 +0000909 if (fPixelRef->getTexture() != NULL) {
910 // Do a deep copy
911 SkPixelRef* pixelRef = fPixelRef->deepCopy(this->config(), &subset);
912 if (pixelRef != NULL) {
913 SkBitmap dst;
reed@google.com383a6972013-10-21 14:00:07 +0000914 dst.setConfig(this->config(), subset.width(), subset.height(), 0,
915 this->alphaType());
scroggo@google.coma2a31922012-12-07 19:14:45 +0000916 dst.setIsVolatile(this->isVolatile());
scroggo@google.coma2a31922012-12-07 19:14:45 +0000917 dst.setPixelRef(pixelRef)->unref();
918 SkDEBUGCODE(dst.validate());
919 result->swap(dst);
920 return true;
921 }
922 }
923
scroggo@google.coma2a31922012-12-07 19:14:45 +0000924 // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have
925 // exited above.
926 SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width()));
927 SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height()));
928
reed@android.com8a1c16f2008-12-17 15:59:43 +0000929 SkBitmap dst;
reed@google.com383a6972013-10-21 14:00:07 +0000930 dst.setConfig(this->config(), r.width(), r.height(), this->rowBytes(),
931 this->alphaType());
skyostil@google.com0eb75762012-01-16 10:45:53 +0000932 dst.setIsVolatile(this->isVolatile());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000933
934 if (fPixelRef) {
reed@google.com672588b2014-01-08 15:42:01 +0000935 SkIPoint origin = fPixelRefOrigin;
936 origin.fX += r.fLeft;
937 origin.fY += r.fTop;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000938 // share the pixelref with a custom offset
reed@google.com672588b2014-01-08 15:42:01 +0000939 dst.setPixelRef(fPixelRef, origin);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000940 }
941 SkDEBUGCODE(dst.validate();)
942
943 // we know we're good, so commit to result
944 result->swap(dst);
945 return true;
946}
947
948///////////////////////////////////////////////////////////////////////////////
949
950#include "SkCanvas.h"
951#include "SkPaint.h"
952
reed@android.comfbaa88d2009-05-06 17:44:34 +0000953bool SkBitmap::canCopyTo(Config dstConfig) const {
reed@google.com44699382013-10-31 17:28:30 +0000954 if (this->config() == kNo_Config) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000955 return false;
956 }
957
reed@android.comfbaa88d2009-05-06 17:44:34 +0000958 bool sameConfigs = (this->config() == dstConfig);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000959 switch (dstConfig) {
960 case kA8_Config:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000961 case kRGB_565_Config:
962 case kARGB_8888_Config:
963 break;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000964 case kIndex8_Config:
965 if (!sameConfigs) {
966 return false;
967 }
968 break;
scroggo@google.com8dc8bc52013-08-07 19:16:05 +0000969 case kARGB_4444_Config:
970 return sameConfigs || kARGB_8888_Config == this->config();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000971 default:
972 return false;
973 }
reed@android.comfbaa88d2009-05-06 17:44:34 +0000974 return true;
975}
976
977bool SkBitmap::copyTo(SkBitmap* dst, Config dstConfig, Allocator* alloc) const {
978 if (!this->canCopyTo(dstConfig)) {
979 return false;
980 }
981
reed@google.com50dfa012011-04-01 19:05:36 +0000982 // if we have a texture, first get those pixels
983 SkBitmap tmpSrc;
984 const SkBitmap* src = this;
985
scroggo@google.coma2a31922012-12-07 19:14:45 +0000986 if (fPixelRef) {
987 SkIRect subset;
reed@google.com672588b2014-01-08 15:42:01 +0000988 subset.setXYWH(fPixelRefOrigin.fX, fPixelRefOrigin.fY, fWidth, fHeight);
989 if (fPixelRef->readPixels(&tmpSrc, &subset)) {
990 SkASSERT(tmpSrc.width() == this->width());
991 SkASSERT(tmpSrc.height() == this->height());
reed@google.com50dfa012011-04-01 19:05:36 +0000992
reed@google.com672588b2014-01-08 15:42:01 +0000993 // did we get lucky and we can just return tmpSrc?
994 if (tmpSrc.config() == dstConfig && NULL == alloc) {
995 dst->swap(tmpSrc);
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000996 // If the result is an exact copy, clone the gen ID.
997 if (dst->pixelRef() && dst->pixelRef()->info() == fPixelRef->info()) {
reed@google.com672588b2014-01-08 15:42:01 +0000998 dst->pixelRef()->cloneGenID(*fPixelRef);
scroggo@google.coma2a31922012-12-07 19:14:45 +0000999 }
reed@google.com672588b2014-01-08 15:42:01 +00001000 return true;
scroggo@google.comd5764e82012-08-22 15:00:05 +00001001 }
reed@google.com672588b2014-01-08 15:42:01 +00001002
1003 // fall through to the raster case
1004 src = &tmpSrc;
reed@google.com50dfa012011-04-01 19:05:36 +00001005 }
reed@android.comfbaa88d2009-05-06 17:44:34 +00001006 }
reed@android.com311c82d2009-05-05 23:13:23 +00001007
reed@google.com50dfa012011-04-01 19:05:36 +00001008 // we lock this now, since we may need its colortable
1009 SkAutoLockPixels srclock(*src);
1010 if (!src->readyToDraw()) {
1011 return false;
1012 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001013
scroggo@google.com5ccae2c2014-01-15 16:56:52 +00001014 // The only way to be readyToDraw is if fPixelRef is non NULL.
1015 SkASSERT(fPixelRef != NULL);
1016
reed@google.com50dfa012011-04-01 19:05:36 +00001017 SkBitmap tmpDst;
reed@google.com383a6972013-10-21 14:00:07 +00001018 tmpDst.setConfig(dstConfig, src->width(), src->height(), 0,
1019 src->alphaType());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001020
weita@google.comf9ab99a2009-05-03 18:23:30 +00001021 // allocate colortable if srcConfig == kIndex8_Config
1022 SkColorTable* ctable = (dstConfig == kIndex8_Config) ?
reed@google.com50dfa012011-04-01 19:05:36 +00001023 new SkColorTable(*src->getColorTable()) : NULL;
weita@google.comf9ab99a2009-05-03 18:23:30 +00001024 SkAutoUnref au(ctable);
reed@google.com50dfa012011-04-01 19:05:36 +00001025 if (!tmpDst.allocPixels(alloc, ctable)) {
weita@google.comf9ab99a2009-05-03 18:23:30 +00001026 return false;
1027 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001028
reed@google.com50dfa012011-04-01 19:05:36 +00001029 if (!tmpDst.readyToDraw()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001030 // allocator/lock failed
1031 return false;
1032 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001033
scroggo@google.com5ccae2c2014-01-15 16:56:52 +00001034 // pixelRef must be non NULL or tmpDst.readyToDraw() would have
1035 // returned false.
1036 SkASSERT(tmpDst.pixelRef() != NULL);
1037
reed@android.comfbaa88d2009-05-06 17:44:34 +00001038 /* do memcpy for the same configs cases, else use drawing
weita@google.comf9ab99a2009-05-03 18:23:30 +00001039 */
reed@google.com50dfa012011-04-01 19:05:36 +00001040 if (src->config() == dstConfig) {
1041 if (tmpDst.getSize() == src->getSize()) {
1042 memcpy(tmpDst.getPixels(), src->getPixels(), src->getSafeSize());
scroggo@google.comd5764e82012-08-22 15:00:05 +00001043 SkPixelRef* pixelRef = tmpDst.pixelRef();
scroggo@google.com5ccae2c2014-01-15 16:56:52 +00001044
1045 // In order to reach this point, we know that the width, config and
1046 // rowbytes of the SkPixelRefs are the same, but it is possible for
1047 // the heights to differ, if this SkBitmap's height is a subset of
1048 // fPixelRef. Only if the SkPixelRefs' heights match are we
1049 // guaranteed that this is an exact copy, meaning we should clone
1050 // the genID.
1051 if (pixelRef->info().fHeight == fPixelRef->info().fHeight) {
1052 // TODO: what to do if the two infos match, BUT
1053 // fPixelRef is premul and pixelRef is opaque?
1054 // skipping assert for now
1055 // https://code.google.com/p/skia/issues/detail?id=2012
1056// SkASSERT(pixelRef->info() == fPixelRef->info());
1057 SkASSERT(pixelRef->info().fWidth == fPixelRef->info().fWidth);
1058 SkASSERT(pixelRef->info().fColorType == fPixelRef->info().fColorType);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +00001059 pixelRef->cloneGenID(*fPixelRef);
scroggo@google.comd5764e82012-08-22 15:00:05 +00001060 }
reed@android.com311c82d2009-05-05 23:13:23 +00001061 } else {
reed@google.com50dfa012011-04-01 19:05:36 +00001062 const char* srcP = reinterpret_cast<const char*>(src->getPixels());
1063 char* dstP = reinterpret_cast<char*>(tmpDst.getPixels());
reed@android.com311c82d2009-05-05 23:13:23 +00001064 // to be sure we don't read too much, only copy our logical pixels
reed@google.com50dfa012011-04-01 19:05:36 +00001065 size_t bytesToCopy = tmpDst.width() * tmpDst.bytesPerPixel();
1066 for (int y = 0; y < tmpDst.height(); y++) {
reed@android.com311c82d2009-05-05 23:13:23 +00001067 memcpy(dstP, srcP, bytesToCopy);
reed@google.com50dfa012011-04-01 19:05:36 +00001068 srcP += src->rowBytes();
1069 dstP += tmpDst.rowBytes();
reed@android.com311c82d2009-05-05 23:13:23 +00001070 }
1071 }
scroggo@google.com8dc8bc52013-08-07 19:16:05 +00001072 } else if (SkBitmap::kARGB_4444_Config == dstConfig
1073 && SkBitmap::kARGB_8888_Config == src->config()) {
1074 SkASSERT(src->height() == tmpDst.height());
1075 SkASSERT(src->width() == tmpDst.width());
1076 for (int y = 0; y < src->height(); ++y) {
1077 SkPMColor16* SK_RESTRICT dstRow = (SkPMColor16*) tmpDst.getAddr16(0, y);
1078 SkPMColor* SK_RESTRICT srcRow = (SkPMColor*) src->getAddr32(0, y);
1079 DITHER_4444_SCAN(y);
1080 for (int x = 0; x < src->width(); ++x) {
1081 dstRow[x] = SkDitherARGB32To4444(srcRow[x],
1082 DITHER_VALUE(x));
1083 }
1084 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001085 } else {
robertphillips@google.com0197b322013-10-10 15:48:16 +00001086 // Always clear the dest in case one of the blitters accesses it
1087 // TODO: switch the allocation of tmpDst to call sk_calloc_throw
1088 tmpDst.eraseColor(SK_ColorTRANSPARENT);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001089
reed@google.com50dfa012011-04-01 19:05:36 +00001090 SkCanvas canvas(tmpDst);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001091 SkPaint paint;
1092
1093 paint.setDither(true);
reed@google.com50dfa012011-04-01 19:05:36 +00001094 canvas.drawBitmap(*src, 0, 0, &paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001095 }
1096
reed@google.com50dfa012011-04-01 19:05:36 +00001097 dst->swap(tmpDst);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001098 return true;
1099}
1100
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001101bool SkBitmap::deepCopyTo(SkBitmap* dst, Config dstConfig) const {
1102 if (!this->canCopyTo(dstConfig)) {
1103 return false;
1104 }
1105
1106 // If we have a PixelRef, and it supports deep copy, use it.
1107 // Currently supported only by texture-backed bitmaps.
1108 if (fPixelRef) {
1109 SkPixelRef* pixelRef = fPixelRef->deepCopy(dstConfig);
1110 if (pixelRef) {
scroggo@google.coma2a31922012-12-07 19:14:45 +00001111 uint32_t rowBytes;
scroggo@google.comd5764e82012-08-22 15:00:05 +00001112 if (dstConfig == fConfig) {
scroggo@google.com5ccae2c2014-01-15 16:56:52 +00001113 // Since there is no subset to pass to deepCopy, and deepCopy
1114 // succeeded, the new pixel ref must be identical.
1115 SkASSERT(fPixelRef->info() == pixelRef->info());
commit-bot@chromium.org50a30432013-10-24 17:44:27 +00001116 pixelRef->cloneGenID(*fPixelRef);
scroggo@google.coma2a31922012-12-07 19:14:45 +00001117 // Use the same rowBytes as the original.
1118 rowBytes = fRowBytes;
1119 } else {
1120 // With the new config, an appropriate fRowBytes will be computed by setConfig.
1121 rowBytes = 0;
scroggo@google.comd5764e82012-08-22 15:00:05 +00001122 }
scroggo@google.coma2a31922012-12-07 19:14:45 +00001123 dst->setConfig(dstConfig, fWidth, fHeight, rowBytes);
reed@google.com672588b2014-01-08 15:42:01 +00001124 dst->setPixelRef(pixelRef, fPixelRefOrigin)->unref();
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001125 return true;
1126 }
1127 }
1128
1129 if (this->getTexture()) {
1130 return false;
1131 } else {
1132 return this->copyTo(dst, dstConfig, NULL);
1133 }
1134}
1135
reed@android.com8a1c16f2008-12-17 15:59:43 +00001136///////////////////////////////////////////////////////////////////////////////
1137///////////////////////////////////////////////////////////////////////////////
1138
1139static void downsampleby2_proc32(SkBitmap* dst, int x, int y,
1140 const SkBitmap& src) {
1141 x <<= 1;
1142 y <<= 1;
1143 const SkPMColor* p = src.getAddr32(x, y);
reed@android.com829c83c2009-06-08 12:05:31 +00001144 const SkPMColor* baseP = p;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001145 SkPMColor c, ag, rb;
1146
1147 c = *p; ag = (c >> 8) & 0xFF00FF; rb = c & 0xFF00FF;
1148 if (x < src.width() - 1) {
1149 p += 1;
1150 }
1151 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
1152
reed@android.com829c83c2009-06-08 12:05:31 +00001153 p = baseP;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001154 if (y < src.height() - 1) {
reed@android.com829c83c2009-06-08 12:05:31 +00001155 p += src.rowBytes() >> 2;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001156 }
1157 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
1158 if (x < src.width() - 1) {
1159 p += 1;
1160 }
1161 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
1162
1163 *dst->getAddr32(x >> 1, y >> 1) =
1164 ((rb >> 2) & 0xFF00FF) | ((ag << 6) & 0xFF00FF00);
1165}
1166
1167static inline uint32_t expand16(U16CPU c) {
1168 return (c & ~SK_G16_MASK_IN_PLACE) | ((c & SK_G16_MASK_IN_PLACE) << 16);
1169}
1170
1171// returns dirt in the top 16bits, but we don't care, since we only
1172// store the low 16bits.
1173static inline U16CPU pack16(uint32_t c) {
1174 return (c & ~SK_G16_MASK_IN_PLACE) | ((c >> 16) & SK_G16_MASK_IN_PLACE);
1175}
1176
1177static void downsampleby2_proc16(SkBitmap* dst, int x, int y,
1178 const SkBitmap& src) {
1179 x <<= 1;
1180 y <<= 1;
1181 const uint16_t* p = src.getAddr16(x, y);
reed@android.com829c83c2009-06-08 12:05:31 +00001182 const uint16_t* baseP = p;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001183 SkPMColor c;
weita@google.comf9ab99a2009-05-03 18:23:30 +00001184
reed@android.com8a1c16f2008-12-17 15:59:43 +00001185 c = expand16(*p);
reed@android.com829c83c2009-06-08 12:05:31 +00001186 if (x < src.width() - 1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001187 p += 1;
1188 }
1189 c += expand16(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001190
reed@android.com829c83c2009-06-08 12:05:31 +00001191 p = baseP;
1192 if (y < src.height() - 1) {
1193 p += src.rowBytes() >> 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001194 }
1195 c += expand16(*p);
reed@android.com829c83c2009-06-08 12:05:31 +00001196 if (x < src.width() - 1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001197 p += 1;
1198 }
1199 c += expand16(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001200
reed@android.com8a1c16f2008-12-17 15:59:43 +00001201 *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)pack16(c >> 2);
1202}
1203
1204static uint32_t expand4444(U16CPU c) {
1205 return (c & 0xF0F) | ((c & ~0xF0F) << 12);
1206}
1207
1208static U16CPU collaps4444(uint32_t c) {
1209 return (c & 0xF0F) | ((c >> 12) & ~0xF0F);
1210}
1211
1212static void downsampleby2_proc4444(SkBitmap* dst, int x, int y,
1213 const SkBitmap& src) {
1214 x <<= 1;
1215 y <<= 1;
1216 const uint16_t* p = src.getAddr16(x, y);
reed@android.com829c83c2009-06-08 12:05:31 +00001217 const uint16_t* baseP = p;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001218 uint32_t c;
weita@google.comf9ab99a2009-05-03 18:23:30 +00001219
reed@android.com8a1c16f2008-12-17 15:59:43 +00001220 c = expand4444(*p);
1221 if (x < src.width() - 1) {
1222 p += 1;
1223 }
1224 c += expand4444(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001225
reed@android.com829c83c2009-06-08 12:05:31 +00001226 p = baseP;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001227 if (y < src.height() - 1) {
reed@android.com829c83c2009-06-08 12:05:31 +00001228 p += src.rowBytes() >> 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001229 }
1230 c += expand4444(*p);
1231 if (x < src.width() - 1) {
1232 p += 1;
1233 }
1234 c += expand4444(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001235
reed@android.com8a1c16f2008-12-17 15:59:43 +00001236 *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)collaps4444(c >> 2);
1237}
1238
1239void SkBitmap::buildMipMap(bool forceRebuild) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001240 if (forceRebuild)
1241 this->freeMipMap();
1242 else if (fMipMap)
1243 return; // we're already built
1244
1245 SkASSERT(NULL == fMipMap);
1246
1247 void (*proc)(SkBitmap* dst, int x, int y, const SkBitmap& src);
1248
reed@google.com44699382013-10-31 17:28:30 +00001249 const SkBitmap::Config config = this->config();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001250
1251 switch (config) {
1252 case kARGB_8888_Config:
1253 proc = downsampleby2_proc32;
1254 break;
1255 case kRGB_565_Config:
1256 proc = downsampleby2_proc16;
1257 break;
1258 case kARGB_4444_Config:
1259 proc = downsampleby2_proc4444;
1260 break;
1261 case kIndex8_Config:
1262 case kA8_Config:
1263 default:
1264 return; // don't build mipmaps for these configs
1265 }
reed@android.com89bb83a2009-05-29 21:30:42 +00001266
reed@android.com149e2f62009-05-22 14:39:03 +00001267 SkAutoLockPixels alp(*this);
1268 if (!this->readyToDraw()) {
1269 return;
1270 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001271
1272 // whip through our loop to compute the exact size needed
1273 size_t size = 0;
1274 int maxLevels = 0;
1275 {
reed@android.com149e2f62009-05-22 14:39:03 +00001276 int width = this->width();
1277 int height = this->height();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001278 for (;;) {
1279 width >>= 1;
1280 height >>= 1;
1281 if (0 == width || 0 == height) {
1282 break;
1283 }
1284 size += ComputeRowBytes(config, width) * height;
1285 maxLevels += 1;
1286 }
1287 }
reed@android.com89bb83a2009-05-29 21:30:42 +00001288
reed@android.com149e2f62009-05-22 14:39:03 +00001289 // nothing to build
reed@android.com8a1c16f2008-12-17 15:59:43 +00001290 if (0 == maxLevels) {
1291 return;
1292 }
1293
reed@android.com149e2f62009-05-22 14:39:03 +00001294 SkBitmap srcBM(*this);
1295 srcBM.lockPixels();
1296 if (!srcBM.readyToDraw()) {
1297 return;
1298 }
1299
1300 MipMap* mm = MipMap::Alloc(maxLevels, size);
1301 if (NULL == mm) {
1302 return;
1303 }
1304
reed@android.com8a1c16f2008-12-17 15:59:43 +00001305 MipLevel* level = mm->levels();
1306 uint8_t* addr = (uint8_t*)mm->pixels();
reed@android.com149e2f62009-05-22 14:39:03 +00001307 int width = this->width();
1308 int height = this->height();
scroggo@google.come5f48242013-02-25 21:47:41 +00001309 uint32_t rowBytes;
reed@android.com149e2f62009-05-22 14:39:03 +00001310 SkBitmap dstBM;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001311
1312 for (int i = 0; i < maxLevels; i++) {
1313 width >>= 1;
1314 height >>= 1;
scroggo@google.come5f48242013-02-25 21:47:41 +00001315 rowBytes = SkToU32(ComputeRowBytes(config, width));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001316
1317 level[i].fPixels = addr;
reed@android.comf459a492009-03-27 12:33:50 +00001318 level[i].fWidth = width;
1319 level[i].fHeight = height;
reed@android.com49f0ff22009-03-19 21:52:42 +00001320 level[i].fRowBytes = rowBytes;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001321
1322 dstBM.setConfig(config, width, height, rowBytes);
1323 dstBM.setPixels(addr);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001324
bungeman@google.com7cf0e9e2012-07-25 16:09:10 +00001325 srcBM.lockPixels();
reed@android.com149e2f62009-05-22 14:39:03 +00001326 for (int y = 0; y < height; y++) {
1327 for (int x = 0; x < width; x++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001328 proc(&dstBM, x, y, srcBM);
1329 }
1330 }
bungeman@google.com7cf0e9e2012-07-25 16:09:10 +00001331 srcBM.unlockPixels();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001332
1333 srcBM = dstBM;
1334 addr += height * rowBytes;
1335 }
1336 SkASSERT(addr == (uint8_t*)mm->pixels() + size);
1337 fMipMap = mm;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001338}
1339
1340bool SkBitmap::hasMipMap() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001341 return fMipMap != NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001342}
1343
1344int SkBitmap::extractMipLevel(SkBitmap* dst, SkFixed sx, SkFixed sy) {
reed@android.com83f7bc32009-07-17 02:42:41 +00001345 if (NULL == fMipMap) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001346 return 0;
reed@android.com83f7bc32009-07-17 02:42:41 +00001347 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001348
reed@android.com8a1c16f2008-12-17 15:59:43 +00001349 int level = ComputeMipLevel(sx, sy) >> 16;
1350 SkASSERT(level >= 0);
1351 if (level <= 0) {
1352 return 0;
1353 }
1354
1355 if (level >= fMipMap->fLevelCount) {
1356 level = fMipMap->fLevelCount - 1;
1357 }
1358 if (dst) {
1359 const MipLevel& mip = fMipMap->levels()[level - 1];
1360 dst->setConfig((SkBitmap::Config)this->config(),
1361 mip.fWidth, mip.fHeight, mip.fRowBytes);
1362 dst->setPixels(mip.fPixels);
1363 }
1364 return level;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001365}
1366
1367SkFixed SkBitmap::ComputeMipLevel(SkFixed sx, SkFixed sy) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001368 sx = SkAbs32(sx);
1369 sy = SkAbs32(sy);
1370 if (sx < sy) {
1371 sx = sy;
1372 }
1373 if (sx < SK_Fixed1) {
1374 return 0;
1375 }
1376 int clz = SkCLZ(sx);
1377 SkASSERT(clz >= 1 && clz <= 15);
1378 return SkIntToFixed(15 - clz) + ((unsigned)(sx << (clz + 1)) >> 16);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001379}
1380
1381///////////////////////////////////////////////////////////////////////////////
1382
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +00001383static bool GetBitmapAlpha(const SkBitmap& src, uint8_t* SK_RESTRICT alpha,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001384 int alphaRowBytes) {
1385 SkASSERT(alpha != NULL);
1386 SkASSERT(alphaRowBytes >= src.width());
1387
reed@google.com44699382013-10-31 17:28:30 +00001388 SkBitmap::Config config = src.config();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001389 int w = src.width();
1390 int h = src.height();
scroggo@google.come5f48242013-02-25 21:47:41 +00001391 size_t rb = src.rowBytes();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001392
reed@android.com1cdcb512009-08-24 19:11:00 +00001393 SkAutoLockPixels alp(src);
1394 if (!src.readyToDraw()) {
1395 // zero out the alpha buffer and return
1396 while (--h >= 0) {
1397 memset(alpha, 0, w);
1398 alpha += alphaRowBytes;
1399 }
1400 return false;
1401 }
reed@google.com82065d62011-02-07 15:30:46 +00001402
reed@android.com8a1c16f2008-12-17 15:59:43 +00001403 if (SkBitmap::kA8_Config == config && !src.isOpaque()) {
1404 const uint8_t* s = src.getAddr8(0, 0);
1405 while (--h >= 0) {
1406 memcpy(alpha, s, w);
1407 s += rb;
1408 alpha += alphaRowBytes;
1409 }
1410 } else if (SkBitmap::kARGB_8888_Config == config && !src.isOpaque()) {
1411 const SkPMColor* SK_RESTRICT s = src.getAddr32(0, 0);
1412 while (--h >= 0) {
1413 for (int x = 0; x < w; x++) {
1414 alpha[x] = SkGetPackedA32(s[x]);
1415 }
1416 s = (const SkPMColor*)((const char*)s + rb);
1417 alpha += alphaRowBytes;
1418 }
1419 } else if (SkBitmap::kARGB_4444_Config == config && !src.isOpaque()) {
1420 const SkPMColor16* SK_RESTRICT s = src.getAddr16(0, 0);
1421 while (--h >= 0) {
1422 for (int x = 0; x < w; x++) {
1423 alpha[x] = SkPacked4444ToA32(s[x]);
1424 }
1425 s = (const SkPMColor16*)((const char*)s + rb);
1426 alpha += alphaRowBytes;
1427 }
1428 } else if (SkBitmap::kIndex8_Config == config && !src.isOpaque()) {
1429 SkColorTable* ct = src.getColorTable();
1430 if (ct) {
1431 const SkPMColor* SK_RESTRICT table = ct->lockColors();
1432 const uint8_t* SK_RESTRICT s = src.getAddr8(0, 0);
1433 while (--h >= 0) {
1434 for (int x = 0; x < w; x++) {
1435 alpha[x] = SkGetPackedA32(table[s[x]]);
1436 }
1437 s += rb;
1438 alpha += alphaRowBytes;
1439 }
reed@google.com0a6151d2013-10-10 14:44:56 +00001440 ct->unlockColors();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001441 }
1442 } else { // src is opaque, so just fill alpha[] with 0xFF
1443 memset(alpha, 0xFF, h * alphaRowBytes);
1444 }
reed@android.com1cdcb512009-08-24 19:11:00 +00001445 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001446}
1447
1448#include "SkPaint.h"
1449#include "SkMaskFilter.h"
1450#include "SkMatrix.h"
1451
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001452bool SkBitmap::extractAlpha(SkBitmap* dst, const SkPaint* paint,
djsollen@google.com57f49692011-02-23 20:46:31 +00001453 Allocator *allocator, SkIPoint* offset) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001454 SkDEBUGCODE(this->validate();)
1455
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001456 SkBitmap tmpBitmap;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001457 SkMatrix identity;
1458 SkMask srcM, dstM;
1459
1460 srcM.fBounds.set(0, 0, this->width(), this->height());
1461 srcM.fRowBytes = SkAlign4(this->width());
1462 srcM.fFormat = SkMask::kA8_Format;
1463
1464 SkMaskFilter* filter = paint ? paint->getMaskFilter() : NULL;
1465
1466 // compute our (larger?) dst bounds if we have a filter
1467 if (NULL != filter) {
1468 identity.reset();
1469 srcM.fImage = NULL;
1470 if (!filter->filterMask(&dstM, srcM, identity, NULL)) {
1471 goto NO_FILTER_CASE;
1472 }
1473 dstM.fRowBytes = SkAlign4(dstM.fBounds.width());
1474 } else {
1475 NO_FILTER_CASE:
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001476 tmpBitmap.setConfig(SkBitmap::kA8_Config, this->width(), this->height(),
reed@android.com8a1c16f2008-12-17 15:59:43 +00001477 srcM.fRowBytes);
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001478 if (!tmpBitmap.allocPixels(allocator, NULL)) {
1479 // Allocation of pixels for alpha bitmap failed.
1480 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
1481 tmpBitmap.width(), tmpBitmap.height());
1482 return false;
1483 }
1484 GetBitmapAlpha(*this, tmpBitmap.getAddr8(0, 0), srcM.fRowBytes);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001485 if (offset) {
1486 offset->set(0, 0);
1487 }
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001488 tmpBitmap.swap(*dst);
1489 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001490 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001491 srcM.fImage = SkMask::AllocImage(srcM.computeImageSize());
1492 SkAutoMaskFreeImage srcCleanup(srcM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001493
1494 GetBitmapAlpha(*this, srcM.fImage, srcM.fRowBytes);
1495 if (!filter->filterMask(&dstM, srcM, identity, NULL)) {
1496 goto NO_FILTER_CASE;
1497 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001498 SkAutoMaskFreeImage dstCleanup(dstM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001499
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001500 tmpBitmap.setConfig(SkBitmap::kA8_Config, dstM.fBounds.width(),
reed@android.com8a1c16f2008-12-17 15:59:43 +00001501 dstM.fBounds.height(), dstM.fRowBytes);
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001502 if (!tmpBitmap.allocPixels(allocator, NULL)) {
1503 // Allocation of pixels for alpha bitmap failed.
1504 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
1505 tmpBitmap.width(), tmpBitmap.height());
1506 return false;
1507 }
1508 memcpy(tmpBitmap.getPixels(), dstM.fImage, dstM.computeImageSize());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001509 if (offset) {
1510 offset->set(dstM.fBounds.fLeft, dstM.fBounds.fTop);
1511 }
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001512 SkDEBUGCODE(tmpBitmap.validate();)
1513
1514 tmpBitmap.swap(*dst);
1515 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001516}
1517
1518///////////////////////////////////////////////////////////////////////////////
1519
1520enum {
1521 SERIALIZE_PIXELTYPE_NONE,
djsollen@google.com21830d92012-08-07 19:49:41 +00001522 SERIALIZE_PIXELTYPE_REF_DATA
reed@android.com8a1c16f2008-12-17 15:59:43 +00001523};
1524
reed@android.com8a1c16f2008-12-17 15:59:43 +00001525void SkBitmap::flatten(SkFlattenableWriteBuffer& buffer) const {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001526 buffer.writeInt(fWidth);
1527 buffer.writeInt(fHeight);
1528 buffer.writeInt(fRowBytes);
1529 buffer.writeInt(fConfig);
reed@google.com383a6972013-10-21 14:00:07 +00001530 buffer.writeInt(fAlphaType);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001531
reed@android.com8a1c16f2008-12-17 15:59:43 +00001532 if (fPixelRef) {
djsollen@google.com5370cd92012-03-28 20:47:01 +00001533 if (fPixelRef->getFactory()) {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001534 buffer.writeInt(SERIALIZE_PIXELTYPE_REF_DATA);
reed@google.com672588b2014-01-08 15:42:01 +00001535 buffer.writeInt(fPixelRefOrigin.fX);
1536 buffer.writeInt(fPixelRefOrigin.fY);
djsollen@google.com5370cd92012-03-28 20:47:01 +00001537 buffer.writeFlattenable(fPixelRef);
1538 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001539 }
1540 // if we get here, we can't record the pixels
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001541 buffer.writeInt(SERIALIZE_PIXELTYPE_NONE);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001542 } else {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001543 buffer.writeInt(SERIALIZE_PIXELTYPE_NONE);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001544 }
1545}
1546
1547void SkBitmap::unflatten(SkFlattenableReadBuffer& buffer) {
1548 this->reset();
weita@google.comf9ab99a2009-05-03 18:23:30 +00001549
reed@android.com8a1c16f2008-12-17 15:59:43 +00001550 int width = buffer.readInt();
1551 int height = buffer.readInt();
1552 int rowBytes = buffer.readInt();
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +00001553 Config config = (Config)buffer.readInt();
1554 SkAlphaType alphaType = (SkAlphaType)buffer.readInt();
1555 buffer.validate((width >= 0) && (height >= 0) && (rowBytes >= 0) &&
1556 SkIsValidConfig(config) && validate_alphaType(config, alphaType));
weita@google.comf9ab99a2009-05-03 18:23:30 +00001557
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +00001558 bool configIsValid = this->setConfig(config, width, height, rowBytes, alphaType);
1559 // Note : Using (fRowBytes >= (fWidth * fBytesPerPixel)) in the following test can create false
1560 // positives if the multiplication causes an integer overflow. Use the division instead.
1561 buffer.validate(configIsValid && (fBytesPerPixel > 0) &&
1562 ((fRowBytes / fBytesPerPixel) >= fWidth));
weita@google.comf9ab99a2009-05-03 18:23:30 +00001563
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001564 int reftype = buffer.readInt();
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +00001565 if (buffer.validate((SERIALIZE_PIXELTYPE_REF_DATA == reftype) ||
1566 (SERIALIZE_PIXELTYPE_NONE == reftype))) {
1567 switch (reftype) {
1568 case SERIALIZE_PIXELTYPE_REF_DATA: {
reed@google.com672588b2014-01-08 15:42:01 +00001569 SkIPoint origin;
1570 origin.fX = buffer.readInt();
1571 origin.fY = buffer.readInt();
1572 size_t offset = origin.fY * rowBytes + origin.fX * fBytesPerPixel;
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +00001573 SkPixelRef* pr = buffer.readPixelRef();
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +00001574 if (!buffer.validate((NULL == pr) ||
1575 (pr->getAllocatedSizeInBytes() >= (offset + this->getSafeSize())))) {
reed@google.com672588b2014-01-08 15:42:01 +00001576 origin.setZero();
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +00001577 }
reed@google.com672588b2014-01-08 15:42:01 +00001578 SkSafeUnref(this->setPixelRef(pr, origin));
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +00001579 break;
1580 }
1581 case SERIALIZE_PIXELTYPE_NONE:
1582 break;
1583 default:
1584 SkDEBUGFAIL("unrecognized pixeltype in serialized data");
1585 sk_throw();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001586 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001587 }
1588}
1589
1590///////////////////////////////////////////////////////////////////////////////
1591
1592SkBitmap::RLEPixels::RLEPixels(int width, int height) {
1593 fHeight = height;
commit-bot@chromium.org235002f2013-10-09 18:39:59 +00001594 fYPtrs = (uint8_t**)sk_calloc_throw(height * sizeof(uint8_t*));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001595}
1596
1597SkBitmap::RLEPixels::~RLEPixels() {
1598 sk_free(fYPtrs);
1599}
1600
1601///////////////////////////////////////////////////////////////////////////////
1602
1603#ifdef SK_DEBUG
1604void SkBitmap::validate() const {
1605 SkASSERT(fConfig < kConfigCount);
1606 SkASSERT(fRowBytes >= (unsigned)ComputeRowBytes((Config)fConfig, fWidth));
scroggo@google.com8e990eb2013-06-14 15:55:56 +00001607 uint8_t allFlags = kImageIsOpaque_Flag | kImageIsVolatile_Flag | kImageIsImmutable_Flag;
1608#ifdef SK_BUILD_FOR_ANDROID
1609 allFlags |= kHasHardwareMipMap_Flag;
1610#endif
1611 SkASSERT(fFlags <= allFlags);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001612 SkASSERT(fPixelLockCount >= 0);
1613 SkASSERT(NULL == fColorTable || (unsigned)fColorTable->getRefCnt() < 10000);
1614 SkASSERT((uint8_t)ComputeBytesPerPixel((Config)fConfig) == fBytesPerPixel);
1615
reed@google.com615316c2014-01-15 19:15:23 +00001616 if (fPixels) {
1617 SkASSERT(fPixelRef);
1618 SkASSERT(fPixelLockCount > 0);
1619 SkASSERT(fPixelRef->isLocked());
1620 SkASSERT(fPixelRef->rowBytes() == fRowBytes);
1621 SkASSERT(fPixelRefOrigin.fX >= 0);
1622 SkASSERT(fPixelRefOrigin.fY >= 0);
1623 SkASSERT(fPixelRef->info().fWidth >= (int)fWidth + fPixelRefOrigin.fX);
1624 SkASSERT(fPixelRef->info().fHeight >= (int)fHeight + fPixelRefOrigin.fY);
1625 SkASSERT(fPixelRef->rowBytes() >= fWidth * fBytesPerPixel);
1626 } else {
1627 SkASSERT(NULL == fColorTable);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001628 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001629}
1630#endif
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001631
1632#ifdef SK_DEVELOPER
1633void SkBitmap::toString(SkString* str) const {
1634
1635 static const char* gConfigNames[kConfigCount] = {
rmistry@google.comd6bab022013-12-02 13:50:38 +00001636 "NONE", "A8", "INDEX8", "565", "4444", "8888"
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001637 };
1638
1639 str->appendf("bitmap: ((%d, %d) %s", this->width(), this->height(),
1640 gConfigNames[this->config()]);
1641
1642 str->append(" (");
1643 if (this->isOpaque()) {
1644 str->append("opaque");
1645 } else {
1646 str->append("transparent");
1647 }
1648 if (this->isImmutable()) {
1649 str->append(", immutable");
1650 } else {
1651 str->append(", not-immutable");
1652 }
1653 str->append(")");
1654
1655 SkPixelRef* pr = this->pixelRef();
1656 if (NULL == pr) {
1657 // show null or the explicit pixel address (rare)
1658 str->appendf(" pixels:%p", this->getPixels());
1659 } else {
1660 const char* uri = pr->getURI();
1661 if (NULL != uri) {
1662 str->appendf(" uri:\"%s\"", uri);
1663 } else {
1664 str->appendf(" pixelref:%p", pr);
1665 }
1666 }
1667
1668 str->append(")");
1669}
1670#endif