blob: 2464d5d5967409f77d87ce7cbf26cc5e4b82ab4d [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@google.com5f62ed72014-01-15 19:59:45 +0000104 if (fPixelRef) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 // ignore the values from the memcpy
106 fPixels = NULL;
107 fColorTable = NULL;
bsalomon@google.com586f48c2011-04-14 15:07:22 +0000108 // Note that what to for genID is somewhat arbitrary. We have no
109 // way to track changes to raw pixels across multiple SkBitmaps.
110 // Would benefit from an SkRawPixelRef type created by
111 // setPixels.
112 // Just leave the memcpy'ed one but they'll get out of sync
113 // as soon either is modified.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114 }
115 }
116
117 SkDEBUGCODE(this->validate();)
118 return *this;
119}
120
121void SkBitmap::swap(SkBitmap& other) {
bsalomon@google.com586f48c2011-04-14 15:07:22 +0000122 SkTSwap(fColorTable, other.fColorTable);
123 SkTSwap(fPixelRef, other.fPixelRef);
reed@google.com672588b2014-01-08 15:42:01 +0000124 SkTSwap(fPixelRefOrigin, other.fPixelRefOrigin);
bsalomon@google.com586f48c2011-04-14 15:07:22 +0000125 SkTSwap(fPixelLockCount, other.fPixelLockCount);
126 SkTSwap(fMipMap, other.fMipMap);
127 SkTSwap(fPixels, other.fPixels);
bsalomon@google.com586f48c2011-04-14 15:07:22 +0000128 SkTSwap(fRowBytes, other.fRowBytes);
129 SkTSwap(fWidth, other.fWidth);
130 SkTSwap(fHeight, other.fHeight);
131 SkTSwap(fConfig, other.fConfig);
reed@google.com383a6972013-10-21 14:00:07 +0000132 SkTSwap(fAlphaType, other.fAlphaType);
bsalomon@google.com586f48c2011-04-14 15:07:22 +0000133 SkTSwap(fFlags, other.fFlags);
134 SkTSwap(fBytesPerPixel, other.fBytesPerPixel);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135
136 SkDEBUGCODE(this->validate();)
137}
138
139void SkBitmap::reset() {
140 this->freePixels();
reed@android.com4516f472009-06-29 16:25:36 +0000141 sk_bzero(this, sizeof(*this));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142}
143
144int SkBitmap::ComputeBytesPerPixel(SkBitmap::Config config) {
145 int bpp;
146 switch (config) {
147 case kNo_Config:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148 bpp = 0; // not applicable
149 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 case kA8_Config:
151 case kIndex8_Config:
152 bpp = 1;
153 break;
154 case kRGB_565_Config:
155 case kARGB_4444_Config:
156 bpp = 2;
157 break;
158 case kARGB_8888_Config:
159 bpp = 4;
160 break;
161 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000162 SkDEBUGFAIL("unknown config");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163 bpp = 0; // error
164 break;
165 }
166 return bpp;
167}
168
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000169size_t SkBitmap::ComputeRowBytes(Config c, int width) {
reed@android.com149e2f62009-05-22 14:39:03 +0000170 if (width < 0) {
171 return 0;
172 }
173
reed@google.com57212f92013-12-30 14:40:38 +0000174 int64_t rowBytes = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000175
176 switch (c) {
177 case kNo_Config:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179 case kA8_Config:
180 case kIndex8_Config:
reed@google.com57212f92013-12-30 14:40:38 +0000181 rowBytes = width;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000182 break;
183 case kRGB_565_Config:
184 case kARGB_4444_Config:
reed@google.com48569642013-12-30 19:21:22 +0000185 // assign and then shift, so we don't overflow int
186 rowBytes = width;
187 rowBytes <<= 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000188 break;
189 case kARGB_8888_Config:
reed@google.com48569642013-12-30 19:21:22 +0000190 // assign and then shift, so we don't overflow int
191 rowBytes = width;
192 rowBytes <<= 2;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000193 break;
194 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000195 SkDEBUGFAIL("unknown config");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196 break;
197 }
reed@google.com57212f92013-12-30 14:40:38 +0000198 return sk_64_isS32(rowBytes) ? sk_64_asS32(rowBytes) : 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000199}
200
reed@google.com57212f92013-12-30 14:40:38 +0000201int64_t SkBitmap::ComputeSize64(Config config, int width, int height) {
202 int64_t rowBytes = sk_64_mul(ComputeBytesPerPixel(config), width);
203 return rowBytes * height;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000204}
205
206size_t SkBitmap::ComputeSize(Config c, int width, int height) {
reed@google.com57212f92013-12-30 14:40:38 +0000207 int64_t size = SkBitmap::ComputeSize64(c, width, height);
208 return sk_64_isS32(size) ? sk_64_asS32(size) : 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000209}
210
reed@google.com57212f92013-12-30 14:40:38 +0000211int64_t SkBitmap::ComputeSafeSize64(Config config,
212 uint32_t width,
213 uint32_t height,
214 size_t rowBytes) {
215 int64_t safeSize = 0;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000216 if (height > 0) {
reed@google.com57212f92013-12-30 14:40:38 +0000217 int64_t lastRow = sk_64_mul(ComputeBytesPerPixel(config), width);
218 safeSize = sk_64_mul(height - 1, rowBytes) + lastRow;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000219 }
reed@google.com57212f92013-12-30 14:40:38 +0000220 SkASSERT(safeSize >= 0);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000221 return safeSize;
222}
223
224size_t SkBitmap::ComputeSafeSize(Config config,
225 uint32_t width,
226 uint32_t height,
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000227 size_t rowBytes) {
reed@google.com57212f92013-12-30 14:40:38 +0000228 int64_t safeSize = ComputeSafeSize64(config, width, height, rowBytes);
229 int32_t safeSize32 = (int32_t)safeSize;
skia.committer@gmail.comf5e1f632013-12-31 07:01:36 +0000230
reed@google.com57212f92013-12-30 14:40:38 +0000231 if (safeSize32 != safeSize) {
232 safeSize32 = 0;
233 }
234 return safeSize32;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000235}
236
reed@google.com86b2e432012-03-15 21:17:03 +0000237void SkBitmap::getBounds(SkRect* bounds) const {
238 SkASSERT(bounds);
239 bounds->set(0, 0,
240 SkIntToScalar(fWidth), SkIntToScalar(fHeight));
241}
242
reed@google.com80e14592012-03-16 14:58:07 +0000243void SkBitmap::getBounds(SkIRect* bounds) const {
244 SkASSERT(bounds);
245 bounds->set(0, 0, fWidth, fHeight);
246}
247
reed@google.com86b2e432012-03-15 21:17:03 +0000248///////////////////////////////////////////////////////////////////////////////
249
reed@google.com383a6972013-10-21 14:00:07 +0000250static bool validate_alphaType(SkBitmap::Config config, SkAlphaType alphaType,
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000251 SkAlphaType* canonical = NULL) {
reed@google.com383a6972013-10-21 14:00:07 +0000252 switch (config) {
253 case SkBitmap::kNo_Config:
254 alphaType = kIgnore_SkAlphaType;
255 break;
reed@google.com383a6972013-10-21 14:00:07 +0000256 case SkBitmap::kA8_Config:
257 if (kUnpremul_SkAlphaType == alphaType) {
258 alphaType = kPremul_SkAlphaType;
259 }
260 // fall-through
261 case SkBitmap::kIndex8_Config:
262 case SkBitmap::kARGB_4444_Config:
263 case SkBitmap::kARGB_8888_Config:
264 if (kIgnore_SkAlphaType == alphaType) {
265 return false;
266 }
267 break;
268 case SkBitmap::kRGB_565_Config:
269 alphaType = kOpaque_SkAlphaType;
270 break;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000271 default:
272 return false;
reed@android.com149e2f62009-05-22 14:39:03 +0000273 }
reed@google.com383a6972013-10-21 14:00:07 +0000274 if (canonical) {
275 *canonical = alphaType;
276 }
277 return true;
278}
reed@android.com149e2f62009-05-22 14:39:03 +0000279
reed@google.com383a6972013-10-21 14:00:07 +0000280bool SkBitmap::setConfig(Config config, int width, int height, size_t rowBytes,
281 SkAlphaType alphaType) {
282 if ((width | height) < 0) {
reed@google.com9cd697c2013-10-21 14:12:13 +0000283 goto BAD_CONFIG;
reed@google.com383a6972013-10-21 14:00:07 +0000284 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000285 if (rowBytes == 0) {
reed@google.com383a6972013-10-21 14:00:07 +0000286 rowBytes = SkBitmap::ComputeRowBytes(config, width);
halcanary@google.com44287342013-12-13 18:29:51 +0000287 if (0 == rowBytes && kNo_Config != config && width > 0) {
reed@google.com9cd697c2013-10-21 14:12:13 +0000288 goto BAD_CONFIG;
reed@android.com149e2f62009-05-22 14:39:03 +0000289 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000290 }
reed@android.com89bb83a2009-05-29 21:30:42 +0000291
reed@google.com383a6972013-10-21 14:00:07 +0000292 if (!validate_alphaType(config, alphaType, &alphaType)) {
reed@google.com9cd697c2013-10-21 14:12:13 +0000293 goto BAD_CONFIG;
reed@google.com383a6972013-10-21 14:00:07 +0000294 }
295
296 this->freePixels();
297
298 fConfig = SkToU8(config);
299 fAlphaType = SkToU8(alphaType);
reed@android.comf459a492009-03-27 12:33:50 +0000300 fWidth = width;
301 fHeight = height;
scroggo@google.come5f48242013-02-25 21:47:41 +0000302 fRowBytes = SkToU32(rowBytes);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000303
reed@google.com383a6972013-10-21 14:00:07 +0000304 fBytesPerPixel = (uint8_t)ComputeBytesPerPixel(config);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000305
306 SkDEBUGCODE(this->validate();)
reed@google.com383a6972013-10-21 14:00:07 +0000307 return true;
agl@chromium.org6b8cb252009-06-01 23:52:37 +0000308
reed@android.com9e0c2fc2009-06-02 18:54:28 +0000309 // if we got here, we had an error, so we reset the bitmap to empty
reed@google.com9cd697c2013-10-21 14:12:13 +0000310BAD_CONFIG:
agl@chromium.org6b8cb252009-06-01 23:52:37 +0000311 this->reset();
reed@google.com383a6972013-10-21 14:00:07 +0000312 return false;
313}
314
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000315bool SkBitmap::setConfig(const SkImageInfo& info, size_t rowBytes) {
316 return this->setConfig(SkImageInfoToBitmapConfig(info), info.fWidth,
317 info.fHeight, rowBytes, info.fAlphaType);
318}
319
reed@google.com383a6972013-10-21 14:00:07 +0000320bool SkBitmap::setAlphaType(SkAlphaType alphaType) {
321 if (!validate_alphaType(this->config(), alphaType, &alphaType)) {
322 return false;
323 }
324 fAlphaType = SkToU8(alphaType);
325 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000326}
327
328void SkBitmap::updatePixelsFromRef() const {
329 if (NULL != fPixelRef) {
330 if (fPixelLockCount > 0) {
reed@google.comff0da4f2012-05-17 13:14:52 +0000331 SkASSERT(fPixelRef->isLocked());
weita@google.comf9ab99a2009-05-03 18:23:30 +0000332
reed@android.com8a1c16f2008-12-17 15:59:43 +0000333 void* p = fPixelRef->pixels();
334 if (NULL != p) {
reed@google.com672588b2014-01-08 15:42:01 +0000335 p = (char*)p
reed@google.com303c4752014-01-09 20:00:14 +0000336 + fPixelRefOrigin.fY * fRowBytes
reed@google.com672588b2014-01-08 15:42:01 +0000337 + fPixelRefOrigin.fX * fBytesPerPixel;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000338 }
339 fPixels = p;
reed@google.com5f62ed72014-01-15 19:59:45 +0000340 fColorTable = fPixelRef->colorTable();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000341 } else {
342 SkASSERT(0 == fPixelLockCount);
343 fPixels = NULL;
reed@google.com5f62ed72014-01-15 19:59:45 +0000344 fColorTable = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000345 }
346 }
347}
348
reed@google.com9230ea22013-12-09 22:01:03 +0000349static bool config_to_colorType(SkBitmap::Config config, SkColorType* ctOut) {
350 SkColorType ct;
351 switch (config) {
352 case SkBitmap::kA8_Config:
353 ct = kAlpha_8_SkColorType;
354 break;
355 case SkBitmap::kIndex8_Config:
356 ct = kIndex_8_SkColorType;
357 break;
358 case SkBitmap::kRGB_565_Config:
359 ct = kRGB_565_SkColorType;
360 break;
361 case SkBitmap::kARGB_4444_Config:
362 ct = kARGB_4444_SkColorType;
363 break;
364 case SkBitmap::kARGB_8888_Config:
365 ct = kPMColor_SkColorType;
366 break;
367 case SkBitmap::kNo_Config:
368 default:
369 return false;
370 }
371 if (ctOut) {
372 *ctOut = ct;
373 }
374 return true;
375}
376
377bool SkBitmap::asImageInfo(SkImageInfo* info) const {
378 SkColorType ct;
379 if (!config_to_colorType(this->config(), &ct)) {
380 return false;
381 }
382 if (info) {
383 info->fWidth = fWidth;
384 info->fHeight = fHeight;
385 info->fAlphaType = this->alphaType();
386 info->fColorType = ct;
387 }
388 return true;
389}
390
reed@google.com672588b2014-01-08 15:42:01 +0000391SkPixelRef* SkBitmap::setPixelRef(SkPixelRef* pr, int dx, int dy) {
reed@google.comdcea5302014-01-03 13:43:01 +0000392#ifdef SK_DEBUG
reed@google.com672588b2014-01-08 15:42:01 +0000393 if (pr) {
reed@google.comdcea5302014-01-03 13:43:01 +0000394 SkImageInfo info;
395 if (this->asImageInfo(&info)) {
396 const SkImageInfo& prInfo = pr->info();
397 SkASSERT(info.fWidth <= prInfo.fWidth);
398 SkASSERT(info.fHeight <= prInfo.fHeight);
399 SkASSERT(info.fColorType == prInfo.fColorType);
400 switch (prInfo.fAlphaType) {
401 case kIgnore_SkAlphaType:
402 SkASSERT(fAlphaType == kIgnore_SkAlphaType);
403 break;
404 case kOpaque_SkAlphaType:
405 case kPremul_SkAlphaType:
406 SkASSERT(info.fAlphaType == kOpaque_SkAlphaType ||
407 info.fAlphaType == kPremul_SkAlphaType);
408 break;
409 case kUnpremul_SkAlphaType:
410 SkASSERT(info.fAlphaType == kOpaque_SkAlphaType ||
411 info.fAlphaType == kUnpremul_SkAlphaType);
412 break;
413 }
414 }
415 }
416#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000417
reed@google.com672588b2014-01-08 15:42:01 +0000418 if (pr) {
419 const SkImageInfo& info = pr->info();
420 fPixelRefOrigin.set(SkPin32(dx, 0, info.fWidth),
421 SkPin32(dy, 0, info.fHeight));
422 } else {
423 // ignore dx,dy if there is no pixelref
424 fPixelRefOrigin.setZero();
425 }
426
427 if (fPixelRef != pr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000428 if (fPixelRef != pr) {
429 this->freePixels();
430 SkASSERT(NULL == fPixelRef);
weita@google.comf9ab99a2009-05-03 18:23:30 +0000431
reed@google.com82065d62011-02-07 15:30:46 +0000432 SkSafeRef(pr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000433 fPixelRef = pr;
434 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000435 this->updatePixelsFromRef();
436 }
437
438 SkDEBUGCODE(this->validate();)
439 return pr;
440}
441
442void SkBitmap::lockPixels() const {
djsollen@google.com7c6d2642013-08-06 12:19:38 +0000443 if (NULL != fPixelRef && 0 == sk_atomic_inc(&fPixelLockCount)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000444 fPixelRef->lockPixels();
445 this->updatePixelsFromRef();
446 }
447 SkDEBUGCODE(this->validate();)
448}
449
450void SkBitmap::unlockPixels() const {
451 SkASSERT(NULL == fPixelRef || fPixelLockCount > 0);
452
djsollen@google.com7c6d2642013-08-06 12:19:38 +0000453 if (NULL != fPixelRef && 1 == sk_atomic_dec(&fPixelLockCount)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000454 fPixelRef->unlockPixels();
455 this->updatePixelsFromRef();
456 }
457 SkDEBUGCODE(this->validate();)
458}
459
reed@google.com9c49bc32011-07-07 13:42:37 +0000460bool SkBitmap::lockPixelsAreWritable() const {
djsollen@google.comc84b8332012-07-27 13:41:44 +0000461 return (fPixelRef) ? fPixelRef->lockPixelsAreWritable() : false;
reed@google.com9c49bc32011-07-07 13:42:37 +0000462}
463
reed@android.com8a1c16f2008-12-17 15:59:43 +0000464void SkBitmap::setPixels(void* p, SkColorTable* ctable) {
reed@google.com8e1034e2012-07-30 13:16:35 +0000465 if (NULL == p) {
reed@google.com672588b2014-01-08 15:42:01 +0000466 this->setPixelRef(NULL);
reed@google.com8e1034e2012-07-30 13:16:35 +0000467 return;
468 }
469
reed@google.combf790232013-12-13 19:45:58 +0000470 SkImageInfo info;
471 if (!this->asImageInfo(&info)) {
reed@google.com672588b2014-01-08 15:42:01 +0000472 this->setPixelRef(NULL);
reed@google.combf790232013-12-13 19:45:58 +0000473 return;
474 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000475
reed@google.combf790232013-12-13 19:45:58 +0000476 SkPixelRef* pr = SkMallocPixelRef::NewDirect(info, p, fRowBytes, ctable);
477 if (NULL == pr) {
reed@google.com672588b2014-01-08 15:42:01 +0000478 this->setPixelRef(NULL);
reed@google.combf790232013-12-13 19:45:58 +0000479 return;
480 }
481
482 this->setPixelRef(pr)->unref();
483
djsollen@google.comc84b8332012-07-27 13:41:44 +0000484 // since we're already allocated, we lockPixels right away
485 this->lockPixels();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000486 SkDEBUGCODE(this->validate();)
487}
488
489bool SkBitmap::allocPixels(Allocator* allocator, SkColorTable* ctable) {
490 HeapAllocator stdalloc;
491
492 if (NULL == allocator) {
493 allocator = &stdalloc;
494 }
495 return allocator->allocPixelRef(this, ctable);
496}
497
498void SkBitmap::freePixels() {
499 // if we're gonna free the pixels, we certainly need to free the mipmap
500 this->freeMipMap();
501
reed@android.com8a1c16f2008-12-17 15:59:43 +0000502 if (NULL != fPixelRef) {
503 if (fPixelLockCount > 0) {
504 fPixelRef->unlockPixels();
505 }
506 fPixelRef->unref();
507 fPixelRef = NULL;
reed@google.com672588b2014-01-08 15:42:01 +0000508 fPixelRefOrigin.setZero();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000509 }
510 fPixelLockCount = 0;
511 fPixels = NULL;
reed@google.com5f62ed72014-01-15 19:59:45 +0000512 fColorTable = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000513}
514
515void SkBitmap::freeMipMap() {
reed@android.com149e2f62009-05-22 14:39:03 +0000516 if (fMipMap) {
517 fMipMap->unref();
518 fMipMap = NULL;
519 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000520}
521
522uint32_t SkBitmap::getGenerationID() const {
djsollen@google.comc84b8332012-07-27 13:41:44 +0000523 return (fPixelRef) ? fPixelRef->getGenerationID() : 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000524}
525
526void SkBitmap::notifyPixelsChanged() const {
junov@chromium.orgb0521292011-12-15 20:14:06 +0000527 SkASSERT(!this->isImmutable());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000528 if (fPixelRef) {
529 fPixelRef->notifyPixelsChanged();
530 }
531}
532
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000533GrTexture* SkBitmap::getTexture() const {
reed@android.comce4e53a2010-09-09 16:01:26 +0000534 return fPixelRef ? fPixelRef->getTexture() : NULL;
535}
536
reed@android.com8a1c16f2008-12-17 15:59:43 +0000537///////////////////////////////////////////////////////////////////////////////
538
reed@android.com8a1c16f2008-12-17 15:59:43 +0000539/** We explicitly use the same allocator for our pixels that SkMask does,
540 so that we can freely assign memory allocated by one class to the other.
541 */
542bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst,
543 SkColorTable* ctable) {
reed@google.combf790232013-12-13 19:45:58 +0000544 SkImageInfo info;
545 if (!dst->asImageInfo(&info)) {
546// SkDebugf("unsupported config for info %d\n", dst->config());
547 return false;
548 }
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000549
reed@google.combf790232013-12-13 19:45:58 +0000550 SkPixelRef* pr = SkMallocPixelRef::NewAllocate(info, dst->rowBytes(),
551 ctable);
552 if (NULL == pr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000553 return false;
554 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000555
reed@google.com672588b2014-01-08 15:42:01 +0000556 dst->setPixelRef(pr)->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000557 // since we're already allocated, we lockPixels right away
558 dst->lockPixels();
559 return true;
560}
561
562///////////////////////////////////////////////////////////////////////////////
563
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000564size_t SkBitmap::getSafeSize() const {
565 // This is intended to be a size_t version of ComputeSafeSize64(), just
566 // faster. The computation is meant to be identical.
567 return (fHeight ? ((fHeight - 1) * fRowBytes) +
reed@google.com44699382013-10-31 17:28:30 +0000568 ComputeRowBytes(this->config(), fWidth): 0);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000569}
570
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000571bool SkBitmap::copyPixelsTo(void* const dst, size_t dstSize,
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000572 size_t dstRowBytes, bool preserveDstPad) const {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000573
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000574 if (0 == dstRowBytes) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000575 dstRowBytes = fRowBytes;
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000576 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000577
reed@google.com44699382013-10-31 17:28:30 +0000578 if (dstRowBytes < ComputeRowBytes(this->config(), fWidth) ||
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000579 dst == NULL || (getPixels() == NULL && pixelRef() == NULL))
580 return false;
581
bsalomon@google.comc6980972011-11-02 19:57:21 +0000582 if (!preserveDstPad && static_cast<uint32_t>(dstRowBytes) == fRowBytes) {
reed@google.com44699382013-10-31 17:28:30 +0000583 size_t safeSize = this->getSafeSize();
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000584 if (safeSize > dstSize || safeSize == 0)
585 return false;
586 else {
587 SkAutoLockPixels lock(*this);
588 // This implementation will write bytes beyond the end of each row,
589 // excluding the last row, if the bitmap's stride is greater than
590 // strictly required by the current config.
591 memcpy(dst, getPixels(), safeSize);
592
593 return true;
594 }
595 } else {
596 // If destination has different stride than us, then copy line by line.
reed@google.com44699382013-10-31 17:28:30 +0000597 if (ComputeSafeSize(this->config(), fWidth, fHeight, dstRowBytes) >
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000598 dstSize)
599 return false;
600 else {
601 // Just copy what we need on each line.
reed@google.com44699382013-10-31 17:28:30 +0000602 size_t rowBytes = ComputeRowBytes(this->config(), fWidth);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000603 SkAutoLockPixels lock(*this);
604 const uint8_t* srcP = reinterpret_cast<const uint8_t*>(getPixels());
605 uint8_t* dstP = reinterpret_cast<uint8_t*>(dst);
606 for (uint32_t row = 0; row < fHeight;
607 row++, srcP += fRowBytes, dstP += dstRowBytes) {
608 memcpy(dstP, srcP, rowBytes);
609 }
610
611 return true;
612 }
613 }
614}
615
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000616///////////////////////////////////////////////////////////////////////////////
617
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000618bool SkBitmap::isImmutable() const {
junov@chromium.orgb0521292011-12-15 20:14:06 +0000619 return fPixelRef ? fPixelRef->isImmutable() :
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000620 fFlags & kImageIsImmutable_Flag;
junov@chromium.orgb0521292011-12-15 20:14:06 +0000621}
622
623void SkBitmap::setImmutable() {
624 if (fPixelRef) {
625 fPixelRef->setImmutable();
626 } else {
627 fFlags |= kImageIsImmutable_Flag;
628 }
629}
630
junov@google.com4ee7ae52011-06-30 17:30:49 +0000631bool SkBitmap::isVolatile() const {
632 return (fFlags & kImageIsVolatile_Flag) != 0;
633}
634
635void SkBitmap::setIsVolatile(bool isVolatile) {
636 if (isVolatile) {
637 fFlags |= kImageIsVolatile_Flag;
638 } else {
639 fFlags &= ~kImageIsVolatile_Flag;
640 }
641}
642
reed@android.com8a1c16f2008-12-17 15:59:43 +0000643void* SkBitmap::getAddr(int x, int y) const {
644 SkASSERT((unsigned)x < (unsigned)this->width());
645 SkASSERT((unsigned)y < (unsigned)this->height());
646
647 char* base = (char*)this->getPixels();
648 if (base) {
649 base += y * this->rowBytes();
650 switch (this->config()) {
651 case SkBitmap::kARGB_8888_Config:
652 base += x << 2;
653 break;
654 case SkBitmap::kARGB_4444_Config:
655 case SkBitmap::kRGB_565_Config:
656 base += x << 1;
657 break;
658 case SkBitmap::kA8_Config:
659 case SkBitmap::kIndex8_Config:
660 base += x;
661 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000662 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000663 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000664 SkDEBUGFAIL("Can't return addr for config");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000665 base = NULL;
666 break;
667 }
668 }
669 return base;
670}
671
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000672SkColor SkBitmap::getColor(int x, int y) const {
673 SkASSERT((unsigned)x < (unsigned)this->width());
674 SkASSERT((unsigned)y < (unsigned)this->height());
675
676 switch (this->config()) {
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000677 case SkBitmap::kA8_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000678 uint8_t* addr = this->getAddr8(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000679 return SkColorSetA(0, addr[0]);
680 }
681 case SkBitmap::kIndex8_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000682 SkPMColor c = this->getIndex8Color(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000683 return SkUnPreMultiply::PMColorToColor(c);
684 }
685 case SkBitmap::kRGB_565_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000686 uint16_t* addr = this->getAddr16(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000687 return SkPixel16ToColor(addr[0]);
688 }
689 case SkBitmap::kARGB_4444_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000690 uint16_t* addr = this->getAddr16(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000691 SkPMColor c = SkPixel4444ToPixel32(addr[0]);
692 return SkUnPreMultiply::PMColorToColor(c);
693 }
694 case SkBitmap::kARGB_8888_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000695 uint32_t* addr = this->getAddr32(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000696 return SkUnPreMultiply::PMColorToColor(addr[0]);
697 }
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000698 case kNo_Config:
rmistry@google.comd6bab022013-12-02 13:50:38 +0000699 default:
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000700 SkASSERT(false);
701 return 0;
702 }
703 SkASSERT(false); // Not reached.
704 return 0;
705}
706
reed@google.com2a7579d2012-11-07 18:30:18 +0000707bool SkBitmap::ComputeIsOpaque(const SkBitmap& bm) {
708 SkAutoLockPixels alp(bm);
709 if (!bm.getPixels()) {
710 return false;
711 }
712
713 const int height = bm.height();
714 const int width = bm.width();
715
716 switch (bm.config()) {
reed@google.com2a7579d2012-11-07 18:30:18 +0000717 case SkBitmap::kA8_Config: {
718 unsigned a = 0xFF;
719 for (int y = 0; y < height; ++y) {
720 const uint8_t* row = bm.getAddr8(0, y);
721 for (int x = 0; x < width; ++x) {
722 a &= row[x];
723 }
724 if (0xFF != a) {
725 return false;
726 }
727 }
728 return true;
729 } break;
reed@google.com2a7579d2012-11-07 18:30:18 +0000730 case SkBitmap::kIndex8_Config: {
731 SkAutoLockColors alc(bm);
732 const SkPMColor* table = alc.colors();
733 if (!table) {
734 return false;
735 }
reed@google.com140d7282013-01-07 20:25:04 +0000736 SkPMColor c = (SkPMColor)~0;
reed@google.com2a7579d2012-11-07 18:30:18 +0000737 for (int i = bm.getColorTable()->count() - 1; i >= 0; --i) {
738 c &= table[i];
739 }
740 return 0xFF == SkGetPackedA32(c);
741 } break;
742 case SkBitmap::kRGB_565_Config:
743 return true;
744 break;
745 case SkBitmap::kARGB_4444_Config: {
746 unsigned c = 0xFFFF;
747 for (int y = 0; y < height; ++y) {
748 const SkPMColor16* row = bm.getAddr16(0, y);
749 for (int x = 0; x < width; ++x) {
750 c &= row[x];
751 }
752 if (0xF != SkGetPackedA4444(c)) {
753 return false;
754 }
755 }
756 return true;
757 } break;
758 case SkBitmap::kARGB_8888_Config: {
reed@google.com140d7282013-01-07 20:25:04 +0000759 SkPMColor c = (SkPMColor)~0;
reed@google.com2a7579d2012-11-07 18:30:18 +0000760 for (int y = 0; y < height; ++y) {
761 const SkPMColor* row = bm.getAddr32(0, y);
762 for (int x = 0; x < width; ++x) {
763 c &= row[x];
764 }
765 if (0xFF != SkGetPackedA32(c)) {
766 return false;
767 }
768 }
769 return true;
770 }
771 default:
772 break;
773 }
774 return false;
775}
776
777
reed@android.com8a1c16f2008-12-17 15:59:43 +0000778///////////////////////////////////////////////////////////////////////////////
779///////////////////////////////////////////////////////////////////////////////
780
reed@google.com45f746f2013-06-21 19:51:31 +0000781static uint16_t pack_8888_to_4444(unsigned a, unsigned r, unsigned g, unsigned b) {
782 unsigned pixel = (SkA32To4444(a) << SK_A4444_SHIFT) |
783 (SkR32To4444(r) << SK_R4444_SHIFT) |
784 (SkG32To4444(g) << SK_G4444_SHIFT) |
785 (SkB32To4444(b) << SK_B4444_SHIFT);
786 return SkToU16(pixel);
787}
788
reed@google.com60d32352013-06-28 19:40:50 +0000789void SkBitmap::internalErase(const SkIRect& area,
790 U8CPU a, U8CPU r, U8CPU g, U8CPU b) const {
791#ifdef SK_DEBUG
reed@android.com8a1c16f2008-12-17 15:59:43 +0000792 SkDEBUGCODE(this->validate();)
reed@google.com60d32352013-06-28 19:40:50 +0000793 SkASSERT(!area.isEmpty());
794 {
reed@google.com92833f92013-06-28 19:47:43 +0000795 SkIRect total = { 0, 0, this->width(), this->height() };
reed@google.com60d32352013-06-28 19:40:50 +0000796 SkASSERT(total.contains(area));
797 }
798#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000799
reed@google.com60d32352013-06-28 19:40:50 +0000800 if (kNo_Config == fConfig || kIndex8_Config == fConfig) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000801 return;
802 }
803
804 SkAutoLockPixels alp(*this);
805 // perform this check after the lock call
806 if (!this->readyToDraw()) {
807 return;
808 }
809
reed@google.com60d32352013-06-28 19:40:50 +0000810 int height = area.height();
811 const int width = area.width();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000812 const int rowBytes = fRowBytes;
813
814 // make rgb premultiplied
815 if (255 != a) {
816 r = SkAlphaMul(r, a);
817 g = SkAlphaMul(g, a);
818 b = SkAlphaMul(b, a);
819 }
820
821 switch (fConfig) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000822 case kA8_Config: {
reed@google.com60d32352013-06-28 19:40:50 +0000823 uint8_t* p = this->getAddr8(area.fLeft, area.fTop);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000824 while (--height >= 0) {
825 memset(p, a, width);
826 p += rowBytes;
827 }
828 break;
829 }
reed@google.com45f746f2013-06-21 19:51:31 +0000830 case kARGB_4444_Config:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000831 case kRGB_565_Config: {
reed@google.com60d32352013-06-28 19:40:50 +0000832 uint16_t* p = this->getAddr16(area.fLeft, area.fTop);;
reed@google.com45f746f2013-06-21 19:51:31 +0000833 uint16_t v;
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000834
reed@google.com45f746f2013-06-21 19:51:31 +0000835 if (kARGB_4444_Config == fConfig) {
836 v = pack_8888_to_4444(a, r, g, b);
837 } else {
838 v = SkPackRGB16(r >> (8 - SK_R16_BITS),
839 g >> (8 - SK_G16_BITS),
840 b >> (8 - SK_B16_BITS));
841 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000842 while (--height >= 0) {
843 sk_memset16(p, v, width);
844 p = (uint16_t*)((char*)p + rowBytes);
845 }
846 break;
847 }
848 case kARGB_8888_Config: {
reed@google.com60d32352013-06-28 19:40:50 +0000849 uint32_t* p = this->getAddr32(area.fLeft, area.fTop);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000850 uint32_t v = SkPackARGB32(a, r, g, b);
851
852 while (--height >= 0) {
853 sk_memset32(p, v, width);
854 p = (uint32_t*)((char*)p + rowBytes);
855 }
856 break;
857 }
858 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000859
reed@android.com8a1c16f2008-12-17 15:59:43 +0000860 this->notifyPixelsChanged();
861}
862
reed@google.com60d32352013-06-28 19:40:50 +0000863void SkBitmap::eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const {
reed@google.com92833f92013-06-28 19:47:43 +0000864 SkIRect area = { 0, 0, this->width(), this->height() };
reed@google.com60d32352013-06-28 19:40:50 +0000865 if (!area.isEmpty()) {
866 this->internalErase(area, a, r, g, b);
867 }
868}
869
870void SkBitmap::eraseArea(const SkIRect& rect, SkColor c) const {
reed@google.com92833f92013-06-28 19:47:43 +0000871 SkIRect area = { 0, 0, this->width(), this->height() };
reed@google.com60d32352013-06-28 19:40:50 +0000872 if (area.intersect(rect)) {
873 this->internalErase(area, SkColorGetA(c), SkColorGetR(c),
874 SkColorGetG(c), SkColorGetB(c));
875 }
876}
877
reed@android.com8a1c16f2008-12-17 15:59:43 +0000878//////////////////////////////////////////////////////////////////////////////////////
879//////////////////////////////////////////////////////////////////////////////////////
880
reed@android.com8a1c16f2008-12-17 15:59:43 +0000881bool SkBitmap::extractSubset(SkBitmap* result, const SkIRect& subset) const {
882 SkDEBUGCODE(this->validate();)
883
djsollen@google.comc84b8332012-07-27 13:41:44 +0000884 if (NULL == result || NULL == fPixelRef) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000885 return false; // no src pixels
886 }
887
888 SkIRect srcRect, r;
889 srcRect.set(0, 0, this->width(), this->height());
890 if (!r.intersect(srcRect, subset)) {
891 return false; // r is empty (i.e. no intersection)
892 }
893
scroggo@google.coma2a31922012-12-07 19:14:45 +0000894 if (fPixelRef->getTexture() != NULL) {
895 // Do a deep copy
896 SkPixelRef* pixelRef = fPixelRef->deepCopy(this->config(), &subset);
897 if (pixelRef != NULL) {
898 SkBitmap dst;
reed@google.com383a6972013-10-21 14:00:07 +0000899 dst.setConfig(this->config(), subset.width(), subset.height(), 0,
900 this->alphaType());
scroggo@google.coma2a31922012-12-07 19:14:45 +0000901 dst.setIsVolatile(this->isVolatile());
scroggo@google.coma2a31922012-12-07 19:14:45 +0000902 dst.setPixelRef(pixelRef)->unref();
903 SkDEBUGCODE(dst.validate());
904 result->swap(dst);
905 return true;
906 }
907 }
908
scroggo@google.coma2a31922012-12-07 19:14:45 +0000909 // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have
910 // exited above.
911 SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width()));
912 SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height()));
913
reed@android.com8a1c16f2008-12-17 15:59:43 +0000914 SkBitmap dst;
reed@google.com383a6972013-10-21 14:00:07 +0000915 dst.setConfig(this->config(), r.width(), r.height(), this->rowBytes(),
916 this->alphaType());
skyostil@google.com0eb75762012-01-16 10:45:53 +0000917 dst.setIsVolatile(this->isVolatile());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000918
919 if (fPixelRef) {
reed@google.com672588b2014-01-08 15:42:01 +0000920 SkIPoint origin = fPixelRefOrigin;
921 origin.fX += r.fLeft;
922 origin.fY += r.fTop;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000923 // share the pixelref with a custom offset
reed@google.com672588b2014-01-08 15:42:01 +0000924 dst.setPixelRef(fPixelRef, origin);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000925 }
926 SkDEBUGCODE(dst.validate();)
927
928 // we know we're good, so commit to result
929 result->swap(dst);
930 return true;
931}
932
933///////////////////////////////////////////////////////////////////////////////
934
935#include "SkCanvas.h"
936#include "SkPaint.h"
937
reed@android.comfbaa88d2009-05-06 17:44:34 +0000938bool SkBitmap::canCopyTo(Config dstConfig) const {
reed@google.com44699382013-10-31 17:28:30 +0000939 if (this->config() == kNo_Config) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000940 return false;
941 }
942
reed@android.comfbaa88d2009-05-06 17:44:34 +0000943 bool sameConfigs = (this->config() == dstConfig);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000944 switch (dstConfig) {
945 case kA8_Config:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000946 case kRGB_565_Config:
947 case kARGB_8888_Config:
948 break;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000949 case kIndex8_Config:
950 if (!sameConfigs) {
951 return false;
952 }
953 break;
scroggo@google.com8dc8bc52013-08-07 19:16:05 +0000954 case kARGB_4444_Config:
955 return sameConfigs || kARGB_8888_Config == this->config();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000956 default:
957 return false;
958 }
reed@android.comfbaa88d2009-05-06 17:44:34 +0000959 return true;
960}
961
962bool SkBitmap::copyTo(SkBitmap* dst, Config dstConfig, Allocator* alloc) const {
963 if (!this->canCopyTo(dstConfig)) {
964 return false;
965 }
966
reed@google.com50dfa012011-04-01 19:05:36 +0000967 // if we have a texture, first get those pixels
968 SkBitmap tmpSrc;
969 const SkBitmap* src = this;
970
scroggo@google.coma2a31922012-12-07 19:14:45 +0000971 if (fPixelRef) {
972 SkIRect subset;
reed@google.com672588b2014-01-08 15:42:01 +0000973 subset.setXYWH(fPixelRefOrigin.fX, fPixelRefOrigin.fY, fWidth, fHeight);
974 if (fPixelRef->readPixels(&tmpSrc, &subset)) {
975 SkASSERT(tmpSrc.width() == this->width());
976 SkASSERT(tmpSrc.height() == this->height());
reed@google.com50dfa012011-04-01 19:05:36 +0000977
reed@google.com672588b2014-01-08 15:42:01 +0000978 // did we get lucky and we can just return tmpSrc?
979 if (tmpSrc.config() == dstConfig && NULL == alloc) {
980 dst->swap(tmpSrc);
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000981 // If the result is an exact copy, clone the gen ID.
982 if (dst->pixelRef() && dst->pixelRef()->info() == fPixelRef->info()) {
reed@google.com672588b2014-01-08 15:42:01 +0000983 dst->pixelRef()->cloneGenID(*fPixelRef);
scroggo@google.coma2a31922012-12-07 19:14:45 +0000984 }
reed@google.com672588b2014-01-08 15:42:01 +0000985 return true;
scroggo@google.comd5764e82012-08-22 15:00:05 +0000986 }
reed@google.com672588b2014-01-08 15:42:01 +0000987
988 // fall through to the raster case
989 src = &tmpSrc;
reed@google.com50dfa012011-04-01 19:05:36 +0000990 }
reed@android.comfbaa88d2009-05-06 17:44:34 +0000991 }
reed@android.com311c82d2009-05-05 23:13:23 +0000992
reed@google.com50dfa012011-04-01 19:05:36 +0000993 // we lock this now, since we may need its colortable
994 SkAutoLockPixels srclock(*src);
995 if (!src->readyToDraw()) {
996 return false;
997 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000998
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000999 // The only way to be readyToDraw is if fPixelRef is non NULL.
1000 SkASSERT(fPixelRef != NULL);
1001
reed@google.com50dfa012011-04-01 19:05:36 +00001002 SkBitmap tmpDst;
reed@google.com383a6972013-10-21 14:00:07 +00001003 tmpDst.setConfig(dstConfig, src->width(), src->height(), 0,
1004 src->alphaType());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001005
weita@google.comf9ab99a2009-05-03 18:23:30 +00001006 // allocate colortable if srcConfig == kIndex8_Config
1007 SkColorTable* ctable = (dstConfig == kIndex8_Config) ?
reed@google.com50dfa012011-04-01 19:05:36 +00001008 new SkColorTable(*src->getColorTable()) : NULL;
weita@google.comf9ab99a2009-05-03 18:23:30 +00001009 SkAutoUnref au(ctable);
reed@google.com50dfa012011-04-01 19:05:36 +00001010 if (!tmpDst.allocPixels(alloc, ctable)) {
weita@google.comf9ab99a2009-05-03 18:23:30 +00001011 return false;
1012 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001013
reed@google.com50dfa012011-04-01 19:05:36 +00001014 if (!tmpDst.readyToDraw()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001015 // allocator/lock failed
1016 return false;
1017 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001018
scroggo@google.com5ccae2c2014-01-15 16:56:52 +00001019 // pixelRef must be non NULL or tmpDst.readyToDraw() would have
1020 // returned false.
1021 SkASSERT(tmpDst.pixelRef() != NULL);
1022
reed@android.comfbaa88d2009-05-06 17:44:34 +00001023 /* do memcpy for the same configs cases, else use drawing
weita@google.comf9ab99a2009-05-03 18:23:30 +00001024 */
reed@google.com50dfa012011-04-01 19:05:36 +00001025 if (src->config() == dstConfig) {
1026 if (tmpDst.getSize() == src->getSize()) {
1027 memcpy(tmpDst.getPixels(), src->getPixels(), src->getSafeSize());
scroggo@google.comd5764e82012-08-22 15:00:05 +00001028 SkPixelRef* pixelRef = tmpDst.pixelRef();
scroggo@google.com5ccae2c2014-01-15 16:56:52 +00001029
1030 // In order to reach this point, we know that the width, config and
1031 // rowbytes of the SkPixelRefs are the same, but it is possible for
1032 // the heights to differ, if this SkBitmap's height is a subset of
1033 // fPixelRef. Only if the SkPixelRefs' heights match are we
1034 // guaranteed that this is an exact copy, meaning we should clone
1035 // the genID.
1036 if (pixelRef->info().fHeight == fPixelRef->info().fHeight) {
1037 // TODO: what to do if the two infos match, BUT
1038 // fPixelRef is premul and pixelRef is opaque?
1039 // skipping assert for now
1040 // https://code.google.com/p/skia/issues/detail?id=2012
1041// SkASSERT(pixelRef->info() == fPixelRef->info());
1042 SkASSERT(pixelRef->info().fWidth == fPixelRef->info().fWidth);
1043 SkASSERT(pixelRef->info().fColorType == fPixelRef->info().fColorType);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +00001044 pixelRef->cloneGenID(*fPixelRef);
scroggo@google.comd5764e82012-08-22 15:00:05 +00001045 }
reed@android.com311c82d2009-05-05 23:13:23 +00001046 } else {
reed@google.com50dfa012011-04-01 19:05:36 +00001047 const char* srcP = reinterpret_cast<const char*>(src->getPixels());
1048 char* dstP = reinterpret_cast<char*>(tmpDst.getPixels());
reed@android.com311c82d2009-05-05 23:13:23 +00001049 // to be sure we don't read too much, only copy our logical pixels
reed@google.com50dfa012011-04-01 19:05:36 +00001050 size_t bytesToCopy = tmpDst.width() * tmpDst.bytesPerPixel();
1051 for (int y = 0; y < tmpDst.height(); y++) {
reed@android.com311c82d2009-05-05 23:13:23 +00001052 memcpy(dstP, srcP, bytesToCopy);
reed@google.com50dfa012011-04-01 19:05:36 +00001053 srcP += src->rowBytes();
1054 dstP += tmpDst.rowBytes();
reed@android.com311c82d2009-05-05 23:13:23 +00001055 }
1056 }
scroggo@google.com8dc8bc52013-08-07 19:16:05 +00001057 } else if (SkBitmap::kARGB_4444_Config == dstConfig
1058 && SkBitmap::kARGB_8888_Config == src->config()) {
1059 SkASSERT(src->height() == tmpDst.height());
1060 SkASSERT(src->width() == tmpDst.width());
1061 for (int y = 0; y < src->height(); ++y) {
1062 SkPMColor16* SK_RESTRICT dstRow = (SkPMColor16*) tmpDst.getAddr16(0, y);
1063 SkPMColor* SK_RESTRICT srcRow = (SkPMColor*) src->getAddr32(0, y);
1064 DITHER_4444_SCAN(y);
1065 for (int x = 0; x < src->width(); ++x) {
1066 dstRow[x] = SkDitherARGB32To4444(srcRow[x],
1067 DITHER_VALUE(x));
1068 }
1069 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001070 } else {
robertphillips@google.com0197b322013-10-10 15:48:16 +00001071 // Always clear the dest in case one of the blitters accesses it
1072 // TODO: switch the allocation of tmpDst to call sk_calloc_throw
1073 tmpDst.eraseColor(SK_ColorTRANSPARENT);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001074
reed@google.com50dfa012011-04-01 19:05:36 +00001075 SkCanvas canvas(tmpDst);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001076 SkPaint paint;
1077
1078 paint.setDither(true);
reed@google.com50dfa012011-04-01 19:05:36 +00001079 canvas.drawBitmap(*src, 0, 0, &paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001080 }
1081
reed@google.com50dfa012011-04-01 19:05:36 +00001082 dst->swap(tmpDst);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001083 return true;
1084}
1085
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001086bool SkBitmap::deepCopyTo(SkBitmap* dst, Config dstConfig) const {
1087 if (!this->canCopyTo(dstConfig)) {
1088 return false;
1089 }
1090
1091 // If we have a PixelRef, and it supports deep copy, use it.
1092 // Currently supported only by texture-backed bitmaps.
1093 if (fPixelRef) {
1094 SkPixelRef* pixelRef = fPixelRef->deepCopy(dstConfig);
1095 if (pixelRef) {
scroggo@google.coma2a31922012-12-07 19:14:45 +00001096 uint32_t rowBytes;
scroggo@google.comd5764e82012-08-22 15:00:05 +00001097 if (dstConfig == fConfig) {
scroggo@google.com5ccae2c2014-01-15 16:56:52 +00001098 // Since there is no subset to pass to deepCopy, and deepCopy
1099 // succeeded, the new pixel ref must be identical.
1100 SkASSERT(fPixelRef->info() == pixelRef->info());
commit-bot@chromium.org50a30432013-10-24 17:44:27 +00001101 pixelRef->cloneGenID(*fPixelRef);
scroggo@google.coma2a31922012-12-07 19:14:45 +00001102 // Use the same rowBytes as the original.
1103 rowBytes = fRowBytes;
1104 } else {
1105 // With the new config, an appropriate fRowBytes will be computed by setConfig.
1106 rowBytes = 0;
scroggo@google.comd5764e82012-08-22 15:00:05 +00001107 }
scroggo@google.coma2a31922012-12-07 19:14:45 +00001108 dst->setConfig(dstConfig, fWidth, fHeight, rowBytes);
reed@google.com672588b2014-01-08 15:42:01 +00001109 dst->setPixelRef(pixelRef, fPixelRefOrigin)->unref();
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001110 return true;
1111 }
1112 }
1113
1114 if (this->getTexture()) {
1115 return false;
1116 } else {
1117 return this->copyTo(dst, dstConfig, NULL);
1118 }
1119}
1120
reed@android.com8a1c16f2008-12-17 15:59:43 +00001121///////////////////////////////////////////////////////////////////////////////
1122///////////////////////////////////////////////////////////////////////////////
1123
1124static void downsampleby2_proc32(SkBitmap* dst, int x, int y,
1125 const SkBitmap& src) {
1126 x <<= 1;
1127 y <<= 1;
1128 const SkPMColor* p = src.getAddr32(x, y);
reed@android.com829c83c2009-06-08 12:05:31 +00001129 const SkPMColor* baseP = p;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001130 SkPMColor c, ag, rb;
1131
1132 c = *p; ag = (c >> 8) & 0xFF00FF; rb = c & 0xFF00FF;
1133 if (x < src.width() - 1) {
1134 p += 1;
1135 }
1136 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
1137
reed@android.com829c83c2009-06-08 12:05:31 +00001138 p = baseP;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001139 if (y < src.height() - 1) {
reed@android.com829c83c2009-06-08 12:05:31 +00001140 p += src.rowBytes() >> 2;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001141 }
1142 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
1143 if (x < src.width() - 1) {
1144 p += 1;
1145 }
1146 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
1147
1148 *dst->getAddr32(x >> 1, y >> 1) =
1149 ((rb >> 2) & 0xFF00FF) | ((ag << 6) & 0xFF00FF00);
1150}
1151
1152static inline uint32_t expand16(U16CPU c) {
1153 return (c & ~SK_G16_MASK_IN_PLACE) | ((c & SK_G16_MASK_IN_PLACE) << 16);
1154}
1155
1156// returns dirt in the top 16bits, but we don't care, since we only
1157// store the low 16bits.
1158static inline U16CPU pack16(uint32_t c) {
1159 return (c & ~SK_G16_MASK_IN_PLACE) | ((c >> 16) & SK_G16_MASK_IN_PLACE);
1160}
1161
1162static void downsampleby2_proc16(SkBitmap* dst, int x, int y,
1163 const SkBitmap& src) {
1164 x <<= 1;
1165 y <<= 1;
1166 const uint16_t* p = src.getAddr16(x, y);
reed@android.com829c83c2009-06-08 12:05:31 +00001167 const uint16_t* baseP = p;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001168 SkPMColor c;
weita@google.comf9ab99a2009-05-03 18:23:30 +00001169
reed@android.com8a1c16f2008-12-17 15:59:43 +00001170 c = expand16(*p);
reed@android.com829c83c2009-06-08 12:05:31 +00001171 if (x < src.width() - 1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001172 p += 1;
1173 }
1174 c += expand16(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001175
reed@android.com829c83c2009-06-08 12:05:31 +00001176 p = baseP;
1177 if (y < src.height() - 1) {
1178 p += src.rowBytes() >> 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001179 }
1180 c += expand16(*p);
reed@android.com829c83c2009-06-08 12:05:31 +00001181 if (x < src.width() - 1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001182 p += 1;
1183 }
1184 c += expand16(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001185
reed@android.com8a1c16f2008-12-17 15:59:43 +00001186 *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)pack16(c >> 2);
1187}
1188
1189static uint32_t expand4444(U16CPU c) {
1190 return (c & 0xF0F) | ((c & ~0xF0F) << 12);
1191}
1192
1193static U16CPU collaps4444(uint32_t c) {
1194 return (c & 0xF0F) | ((c >> 12) & ~0xF0F);
1195}
1196
1197static void downsampleby2_proc4444(SkBitmap* dst, int x, int y,
1198 const SkBitmap& src) {
1199 x <<= 1;
1200 y <<= 1;
1201 const uint16_t* p = src.getAddr16(x, y);
reed@android.com829c83c2009-06-08 12:05:31 +00001202 const uint16_t* baseP = p;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001203 uint32_t c;
weita@google.comf9ab99a2009-05-03 18:23:30 +00001204
reed@android.com8a1c16f2008-12-17 15:59:43 +00001205 c = expand4444(*p);
1206 if (x < src.width() - 1) {
1207 p += 1;
1208 }
1209 c += expand4444(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001210
reed@android.com829c83c2009-06-08 12:05:31 +00001211 p = baseP;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001212 if (y < src.height() - 1) {
reed@android.com829c83c2009-06-08 12:05:31 +00001213 p += src.rowBytes() >> 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001214 }
1215 c += expand4444(*p);
1216 if (x < src.width() - 1) {
1217 p += 1;
1218 }
1219 c += expand4444(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001220
reed@android.com8a1c16f2008-12-17 15:59:43 +00001221 *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)collaps4444(c >> 2);
1222}
1223
1224void SkBitmap::buildMipMap(bool forceRebuild) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001225 if (forceRebuild)
1226 this->freeMipMap();
1227 else if (fMipMap)
1228 return; // we're already built
1229
1230 SkASSERT(NULL == fMipMap);
1231
1232 void (*proc)(SkBitmap* dst, int x, int y, const SkBitmap& src);
1233
reed@google.com44699382013-10-31 17:28:30 +00001234 const SkBitmap::Config config = this->config();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001235
1236 switch (config) {
1237 case kARGB_8888_Config:
1238 proc = downsampleby2_proc32;
1239 break;
1240 case kRGB_565_Config:
1241 proc = downsampleby2_proc16;
1242 break;
1243 case kARGB_4444_Config:
1244 proc = downsampleby2_proc4444;
1245 break;
1246 case kIndex8_Config:
1247 case kA8_Config:
1248 default:
1249 return; // don't build mipmaps for these configs
1250 }
reed@android.com89bb83a2009-05-29 21:30:42 +00001251
reed@android.com149e2f62009-05-22 14:39:03 +00001252 SkAutoLockPixels alp(*this);
1253 if (!this->readyToDraw()) {
1254 return;
1255 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001256
1257 // whip through our loop to compute the exact size needed
1258 size_t size = 0;
1259 int maxLevels = 0;
1260 {
reed@android.com149e2f62009-05-22 14:39:03 +00001261 int width = this->width();
1262 int height = this->height();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001263 for (;;) {
1264 width >>= 1;
1265 height >>= 1;
1266 if (0 == width || 0 == height) {
1267 break;
1268 }
1269 size += ComputeRowBytes(config, width) * height;
1270 maxLevels += 1;
1271 }
1272 }
reed@android.com89bb83a2009-05-29 21:30:42 +00001273
reed@android.com149e2f62009-05-22 14:39:03 +00001274 // nothing to build
reed@android.com8a1c16f2008-12-17 15:59:43 +00001275 if (0 == maxLevels) {
1276 return;
1277 }
1278
reed@android.com149e2f62009-05-22 14:39:03 +00001279 SkBitmap srcBM(*this);
1280 srcBM.lockPixels();
1281 if (!srcBM.readyToDraw()) {
1282 return;
1283 }
1284
1285 MipMap* mm = MipMap::Alloc(maxLevels, size);
1286 if (NULL == mm) {
1287 return;
1288 }
1289
reed@android.com8a1c16f2008-12-17 15:59:43 +00001290 MipLevel* level = mm->levels();
1291 uint8_t* addr = (uint8_t*)mm->pixels();
reed@android.com149e2f62009-05-22 14:39:03 +00001292 int width = this->width();
1293 int height = this->height();
scroggo@google.come5f48242013-02-25 21:47:41 +00001294 uint32_t rowBytes;
reed@android.com149e2f62009-05-22 14:39:03 +00001295 SkBitmap dstBM;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001296
1297 for (int i = 0; i < maxLevels; i++) {
1298 width >>= 1;
1299 height >>= 1;
scroggo@google.come5f48242013-02-25 21:47:41 +00001300 rowBytes = SkToU32(ComputeRowBytes(config, width));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001301
1302 level[i].fPixels = addr;
reed@android.comf459a492009-03-27 12:33:50 +00001303 level[i].fWidth = width;
1304 level[i].fHeight = height;
reed@android.com49f0ff22009-03-19 21:52:42 +00001305 level[i].fRowBytes = rowBytes;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001306
1307 dstBM.setConfig(config, width, height, rowBytes);
1308 dstBM.setPixels(addr);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001309
bungeman@google.com7cf0e9e2012-07-25 16:09:10 +00001310 srcBM.lockPixels();
reed@android.com149e2f62009-05-22 14:39:03 +00001311 for (int y = 0; y < height; y++) {
1312 for (int x = 0; x < width; x++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001313 proc(&dstBM, x, y, srcBM);
1314 }
1315 }
bungeman@google.com7cf0e9e2012-07-25 16:09:10 +00001316 srcBM.unlockPixels();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001317
1318 srcBM = dstBM;
1319 addr += height * rowBytes;
1320 }
1321 SkASSERT(addr == (uint8_t*)mm->pixels() + size);
1322 fMipMap = mm;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001323}
1324
1325bool SkBitmap::hasMipMap() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001326 return fMipMap != NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001327}
1328
1329int SkBitmap::extractMipLevel(SkBitmap* dst, SkFixed sx, SkFixed sy) {
reed@android.com83f7bc32009-07-17 02:42:41 +00001330 if (NULL == fMipMap) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001331 return 0;
reed@android.com83f7bc32009-07-17 02:42:41 +00001332 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001333
reed@android.com8a1c16f2008-12-17 15:59:43 +00001334 int level = ComputeMipLevel(sx, sy) >> 16;
1335 SkASSERT(level >= 0);
1336 if (level <= 0) {
1337 return 0;
1338 }
1339
1340 if (level >= fMipMap->fLevelCount) {
1341 level = fMipMap->fLevelCount - 1;
1342 }
1343 if (dst) {
1344 const MipLevel& mip = fMipMap->levels()[level - 1];
1345 dst->setConfig((SkBitmap::Config)this->config(),
1346 mip.fWidth, mip.fHeight, mip.fRowBytes);
1347 dst->setPixels(mip.fPixels);
1348 }
1349 return level;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001350}
1351
1352SkFixed SkBitmap::ComputeMipLevel(SkFixed sx, SkFixed sy) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001353 sx = SkAbs32(sx);
1354 sy = SkAbs32(sy);
1355 if (sx < sy) {
1356 sx = sy;
1357 }
1358 if (sx < SK_Fixed1) {
1359 return 0;
1360 }
1361 int clz = SkCLZ(sx);
1362 SkASSERT(clz >= 1 && clz <= 15);
1363 return SkIntToFixed(15 - clz) + ((unsigned)(sx << (clz + 1)) >> 16);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001364}
1365
1366///////////////////////////////////////////////////////////////////////////////
1367
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +00001368static bool GetBitmapAlpha(const SkBitmap& src, uint8_t* SK_RESTRICT alpha,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001369 int alphaRowBytes) {
1370 SkASSERT(alpha != NULL);
1371 SkASSERT(alphaRowBytes >= src.width());
1372
reed@google.com44699382013-10-31 17:28:30 +00001373 SkBitmap::Config config = src.config();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001374 int w = src.width();
1375 int h = src.height();
scroggo@google.come5f48242013-02-25 21:47:41 +00001376 size_t rb = src.rowBytes();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001377
reed@android.com1cdcb512009-08-24 19:11:00 +00001378 SkAutoLockPixels alp(src);
1379 if (!src.readyToDraw()) {
1380 // zero out the alpha buffer and return
1381 while (--h >= 0) {
1382 memset(alpha, 0, w);
1383 alpha += alphaRowBytes;
1384 }
1385 return false;
1386 }
reed@google.com82065d62011-02-07 15:30:46 +00001387
reed@android.com8a1c16f2008-12-17 15:59:43 +00001388 if (SkBitmap::kA8_Config == config && !src.isOpaque()) {
1389 const uint8_t* s = src.getAddr8(0, 0);
1390 while (--h >= 0) {
1391 memcpy(alpha, s, w);
1392 s += rb;
1393 alpha += alphaRowBytes;
1394 }
1395 } else if (SkBitmap::kARGB_8888_Config == config && !src.isOpaque()) {
1396 const SkPMColor* SK_RESTRICT s = src.getAddr32(0, 0);
1397 while (--h >= 0) {
1398 for (int x = 0; x < w; x++) {
1399 alpha[x] = SkGetPackedA32(s[x]);
1400 }
1401 s = (const SkPMColor*)((const char*)s + rb);
1402 alpha += alphaRowBytes;
1403 }
1404 } else if (SkBitmap::kARGB_4444_Config == config && !src.isOpaque()) {
1405 const SkPMColor16* SK_RESTRICT s = src.getAddr16(0, 0);
1406 while (--h >= 0) {
1407 for (int x = 0; x < w; x++) {
1408 alpha[x] = SkPacked4444ToA32(s[x]);
1409 }
1410 s = (const SkPMColor16*)((const char*)s + rb);
1411 alpha += alphaRowBytes;
1412 }
1413 } else if (SkBitmap::kIndex8_Config == config && !src.isOpaque()) {
1414 SkColorTable* ct = src.getColorTable();
1415 if (ct) {
1416 const SkPMColor* SK_RESTRICT table = ct->lockColors();
1417 const uint8_t* SK_RESTRICT s = src.getAddr8(0, 0);
1418 while (--h >= 0) {
1419 for (int x = 0; x < w; x++) {
1420 alpha[x] = SkGetPackedA32(table[s[x]]);
1421 }
1422 s += rb;
1423 alpha += alphaRowBytes;
1424 }
reed@google.com0a6151d2013-10-10 14:44:56 +00001425 ct->unlockColors();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001426 }
1427 } else { // src is opaque, so just fill alpha[] with 0xFF
1428 memset(alpha, 0xFF, h * alphaRowBytes);
1429 }
reed@android.com1cdcb512009-08-24 19:11:00 +00001430 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001431}
1432
1433#include "SkPaint.h"
1434#include "SkMaskFilter.h"
1435#include "SkMatrix.h"
1436
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001437bool SkBitmap::extractAlpha(SkBitmap* dst, const SkPaint* paint,
djsollen@google.com57f49692011-02-23 20:46:31 +00001438 Allocator *allocator, SkIPoint* offset) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001439 SkDEBUGCODE(this->validate();)
1440
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001441 SkBitmap tmpBitmap;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001442 SkMatrix identity;
1443 SkMask srcM, dstM;
1444
1445 srcM.fBounds.set(0, 0, this->width(), this->height());
1446 srcM.fRowBytes = SkAlign4(this->width());
1447 srcM.fFormat = SkMask::kA8_Format;
1448
1449 SkMaskFilter* filter = paint ? paint->getMaskFilter() : NULL;
1450
1451 // compute our (larger?) dst bounds if we have a filter
1452 if (NULL != filter) {
1453 identity.reset();
1454 srcM.fImage = NULL;
1455 if (!filter->filterMask(&dstM, srcM, identity, NULL)) {
1456 goto NO_FILTER_CASE;
1457 }
1458 dstM.fRowBytes = SkAlign4(dstM.fBounds.width());
1459 } else {
1460 NO_FILTER_CASE:
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001461 tmpBitmap.setConfig(SkBitmap::kA8_Config, this->width(), this->height(),
reed@android.com8a1c16f2008-12-17 15:59:43 +00001462 srcM.fRowBytes);
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001463 if (!tmpBitmap.allocPixels(allocator, NULL)) {
1464 // Allocation of pixels for alpha bitmap failed.
1465 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
1466 tmpBitmap.width(), tmpBitmap.height());
1467 return false;
1468 }
1469 GetBitmapAlpha(*this, tmpBitmap.getAddr8(0, 0), srcM.fRowBytes);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001470 if (offset) {
1471 offset->set(0, 0);
1472 }
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001473 tmpBitmap.swap(*dst);
1474 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001475 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001476 srcM.fImage = SkMask::AllocImage(srcM.computeImageSize());
1477 SkAutoMaskFreeImage srcCleanup(srcM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001478
1479 GetBitmapAlpha(*this, srcM.fImage, srcM.fRowBytes);
1480 if (!filter->filterMask(&dstM, srcM, identity, NULL)) {
1481 goto NO_FILTER_CASE;
1482 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001483 SkAutoMaskFreeImage dstCleanup(dstM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001484
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001485 tmpBitmap.setConfig(SkBitmap::kA8_Config, dstM.fBounds.width(),
reed@android.com8a1c16f2008-12-17 15:59:43 +00001486 dstM.fBounds.height(), dstM.fRowBytes);
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001487 if (!tmpBitmap.allocPixels(allocator, NULL)) {
1488 // Allocation of pixels for alpha bitmap failed.
1489 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
1490 tmpBitmap.width(), tmpBitmap.height());
1491 return false;
1492 }
1493 memcpy(tmpBitmap.getPixels(), dstM.fImage, dstM.computeImageSize());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001494 if (offset) {
1495 offset->set(dstM.fBounds.fLeft, dstM.fBounds.fTop);
1496 }
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001497 SkDEBUGCODE(tmpBitmap.validate();)
1498
1499 tmpBitmap.swap(*dst);
1500 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001501}
1502
1503///////////////////////////////////////////////////////////////////////////////
1504
1505enum {
1506 SERIALIZE_PIXELTYPE_NONE,
djsollen@google.com21830d92012-08-07 19:49:41 +00001507 SERIALIZE_PIXELTYPE_REF_DATA
reed@android.com8a1c16f2008-12-17 15:59:43 +00001508};
1509
reed@android.com8a1c16f2008-12-17 15:59:43 +00001510void SkBitmap::flatten(SkFlattenableWriteBuffer& buffer) const {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001511 buffer.writeInt(fWidth);
1512 buffer.writeInt(fHeight);
1513 buffer.writeInt(fRowBytes);
1514 buffer.writeInt(fConfig);
reed@google.com383a6972013-10-21 14:00:07 +00001515 buffer.writeInt(fAlphaType);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001516
reed@android.com8a1c16f2008-12-17 15:59:43 +00001517 if (fPixelRef) {
djsollen@google.com5370cd92012-03-28 20:47:01 +00001518 if (fPixelRef->getFactory()) {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001519 buffer.writeInt(SERIALIZE_PIXELTYPE_REF_DATA);
reed@google.com672588b2014-01-08 15:42:01 +00001520 buffer.writeInt(fPixelRefOrigin.fX);
1521 buffer.writeInt(fPixelRefOrigin.fY);
djsollen@google.com5370cd92012-03-28 20:47:01 +00001522 buffer.writeFlattenable(fPixelRef);
1523 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001524 }
1525 // if we get here, we can't record the pixels
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001526 buffer.writeInt(SERIALIZE_PIXELTYPE_NONE);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001527 } else {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001528 buffer.writeInt(SERIALIZE_PIXELTYPE_NONE);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001529 }
1530}
1531
1532void SkBitmap::unflatten(SkFlattenableReadBuffer& buffer) {
1533 this->reset();
weita@google.comf9ab99a2009-05-03 18:23:30 +00001534
reed@android.com8a1c16f2008-12-17 15:59:43 +00001535 int width = buffer.readInt();
1536 int height = buffer.readInt();
1537 int rowBytes = buffer.readInt();
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +00001538 Config config = (Config)buffer.readInt();
1539 SkAlphaType alphaType = (SkAlphaType)buffer.readInt();
1540 buffer.validate((width >= 0) && (height >= 0) && (rowBytes >= 0) &&
1541 SkIsValidConfig(config) && validate_alphaType(config, alphaType));
weita@google.comf9ab99a2009-05-03 18:23:30 +00001542
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +00001543 bool configIsValid = this->setConfig(config, width, height, rowBytes, alphaType);
1544 // Note : Using (fRowBytes >= (fWidth * fBytesPerPixel)) in the following test can create false
1545 // positives if the multiplication causes an integer overflow. Use the division instead.
1546 buffer.validate(configIsValid && (fBytesPerPixel > 0) &&
1547 ((fRowBytes / fBytesPerPixel) >= fWidth));
weita@google.comf9ab99a2009-05-03 18:23:30 +00001548
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001549 int reftype = buffer.readInt();
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +00001550 if (buffer.validate((SERIALIZE_PIXELTYPE_REF_DATA == reftype) ||
1551 (SERIALIZE_PIXELTYPE_NONE == reftype))) {
1552 switch (reftype) {
1553 case SERIALIZE_PIXELTYPE_REF_DATA: {
reed@google.com672588b2014-01-08 15:42:01 +00001554 SkIPoint origin;
1555 origin.fX = buffer.readInt();
1556 origin.fY = buffer.readInt();
1557 size_t offset = origin.fY * rowBytes + origin.fX * fBytesPerPixel;
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +00001558 SkPixelRef* pr = buffer.readPixelRef();
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +00001559 if (!buffer.validate((NULL == pr) ||
1560 (pr->getAllocatedSizeInBytes() >= (offset + this->getSafeSize())))) {
reed@google.com672588b2014-01-08 15:42:01 +00001561 origin.setZero();
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +00001562 }
reed@google.com672588b2014-01-08 15:42:01 +00001563 SkSafeUnref(this->setPixelRef(pr, origin));
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +00001564 break;
1565 }
1566 case SERIALIZE_PIXELTYPE_NONE:
1567 break;
1568 default:
1569 SkDEBUGFAIL("unrecognized pixeltype in serialized data");
1570 sk_throw();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001571 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001572 }
1573}
1574
1575///////////////////////////////////////////////////////////////////////////////
1576
1577SkBitmap::RLEPixels::RLEPixels(int width, int height) {
1578 fHeight = height;
commit-bot@chromium.org235002f2013-10-09 18:39:59 +00001579 fYPtrs = (uint8_t**)sk_calloc_throw(height * sizeof(uint8_t*));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001580}
1581
1582SkBitmap::RLEPixels::~RLEPixels() {
1583 sk_free(fYPtrs);
1584}
1585
1586///////////////////////////////////////////////////////////////////////////////
1587
1588#ifdef SK_DEBUG
1589void SkBitmap::validate() const {
1590 SkASSERT(fConfig < kConfigCount);
1591 SkASSERT(fRowBytes >= (unsigned)ComputeRowBytes((Config)fConfig, fWidth));
scroggo@google.com8e990eb2013-06-14 15:55:56 +00001592 uint8_t allFlags = kImageIsOpaque_Flag | kImageIsVolatile_Flag | kImageIsImmutable_Flag;
1593#ifdef SK_BUILD_FOR_ANDROID
1594 allFlags |= kHasHardwareMipMap_Flag;
1595#endif
1596 SkASSERT(fFlags <= allFlags);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001597 SkASSERT(fPixelLockCount >= 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001598 SkASSERT((uint8_t)ComputeBytesPerPixel((Config)fConfig) == fBytesPerPixel);
1599
reed@google.com615316c2014-01-15 19:15:23 +00001600 if (fPixels) {
1601 SkASSERT(fPixelRef);
1602 SkASSERT(fPixelLockCount > 0);
1603 SkASSERT(fPixelRef->isLocked());
commit-bot@chromium.orgf7ba4132014-01-16 15:47:06 +00001604#if !defined(SK_SUPPORT_LEGACY_ONLOCKPIXELS)
reed@google.com615316c2014-01-15 19:15:23 +00001605 SkASSERT(fPixelRef->rowBytes() == fRowBytes);
commit-bot@chromium.orgf7ba4132014-01-16 15:47:06 +00001606#endif
reed@google.com615316c2014-01-15 19:15:23 +00001607 SkASSERT(fPixelRefOrigin.fX >= 0);
1608 SkASSERT(fPixelRefOrigin.fY >= 0);
1609 SkASSERT(fPixelRef->info().fWidth >= (int)fWidth + fPixelRefOrigin.fX);
1610 SkASSERT(fPixelRef->info().fHeight >= (int)fHeight + fPixelRefOrigin.fY);
commit-bot@chromium.orgf7ba4132014-01-16 15:47:06 +00001611#if !defined(SK_SUPPORT_LEGACY_ONLOCKPIXELS)
reed@google.com615316c2014-01-15 19:15:23 +00001612 SkASSERT(fPixelRef->rowBytes() >= fWidth * fBytesPerPixel);
commit-bot@chromium.orgf7ba4132014-01-16 15:47:06 +00001613#endif
reed@google.com615316c2014-01-15 19:15:23 +00001614 } else {
1615 SkASSERT(NULL == fColorTable);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001616 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001617}
1618#endif
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001619
1620#ifdef SK_DEVELOPER
1621void SkBitmap::toString(SkString* str) const {
1622
1623 static const char* gConfigNames[kConfigCount] = {
rmistry@google.comd6bab022013-12-02 13:50:38 +00001624 "NONE", "A8", "INDEX8", "565", "4444", "8888"
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001625 };
1626
1627 str->appendf("bitmap: ((%d, %d) %s", this->width(), this->height(),
1628 gConfigNames[this->config()]);
1629
1630 str->append(" (");
1631 if (this->isOpaque()) {
1632 str->append("opaque");
1633 } else {
1634 str->append("transparent");
1635 }
1636 if (this->isImmutable()) {
1637 str->append(", immutable");
1638 } else {
1639 str->append(", not-immutable");
1640 }
1641 str->append(")");
1642
1643 SkPixelRef* pr = this->pixelRef();
1644 if (NULL == pr) {
1645 // show null or the explicit pixel address (rare)
1646 str->appendf(" pixels:%p", this->getPixels());
1647 } else {
1648 const char* uri = pr->getURI();
1649 if (NULL != uri) {
1650 str->appendf(" uri:\"%s\"", uri);
1651 } else {
1652 str->appendf(" pixelref:%p", pr);
1653 }
1654 }
1655
1656 str->append(")");
1657}
1658#endif