blob: 3418fad7f197e1014fe63243a1571ee083178363 [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"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000017#include "SkReadBuffer.h"
18#include "SkWriteBuffer.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
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +000027static bool reset_return_false(SkBitmap* bm) {
28 bm->reset();
29 return false;
30}
31
reed@android.com8a1c16f2008-12-17 15:59:43 +000032struct MipLevel {
33 void* fPixels;
34 uint32_t fRowBytes;
reed@android.comf459a492009-03-27 12:33:50 +000035 uint32_t fWidth, fHeight;
reed@android.com8a1c16f2008-12-17 15:59:43 +000036};
37
38struct SkBitmap::MipMap : SkNoncopyable {
39 int32_t fRefCnt;
40 int fLevelCount;
41// MipLevel fLevel[fLevelCount];
42// Pixels[]
weita@google.comf9ab99a2009-05-03 18:23:30 +000043
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 static MipMap* Alloc(int levelCount, size_t pixelSize) {
reed@android.com149e2f62009-05-22 14:39:03 +000045 if (levelCount < 0) {
46 return NULL;
47 }
reed@google.com57212f92013-12-30 14:40:38 +000048 int64_t size = (levelCount + 1) * sizeof(MipLevel);
49 size += sizeof(MipMap) + pixelSize;
50 if (!sk_64_isS32(size)) {
reed@android.com149e2f62009-05-22 14:39:03 +000051 return NULL;
52 }
reed@google.com57212f92013-12-30 14:40:38 +000053 MipMap* mm = (MipMap*)sk_malloc_throw(sk_64_asS32(size));
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 mm->fRefCnt = 1;
55 mm->fLevelCount = levelCount;
56 return mm;
57 }
58
59 const MipLevel* levels() const { return (const MipLevel*)(this + 1); }
60 MipLevel* levels() { return (MipLevel*)(this + 1); }
61
62 const void* pixels() const { return levels() + fLevelCount; }
63 void* pixels() { return levels() + fLevelCount; }
weita@google.comf9ab99a2009-05-03 18:23:30 +000064
reed@android.com149e2f62009-05-22 14:39:03 +000065 void ref() {
66 if (SK_MaxS32 == sk_atomic_inc(&fRefCnt)) {
67 sk_throw();
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 }
69 }
reed@android.com149e2f62009-05-22 14:39:03 +000070 void unref() {
71 SkASSERT(fRefCnt > 0);
72 if (sk_atomic_dec(&fRefCnt) == 1) {
73 sk_free(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 }
75 }
76};
reed@android.com8a1c16f2008-12-17 15:59:43 +000077
78///////////////////////////////////////////////////////////////////////////////
79///////////////////////////////////////////////////////////////////////////////
80
81SkBitmap::SkBitmap() {
reed@android.com4516f472009-06-29 16:25:36 +000082 sk_bzero(this, sizeof(*this));
reed@android.com8a1c16f2008-12-17 15:59:43 +000083}
84
85SkBitmap::SkBitmap(const SkBitmap& src) {
86 SkDEBUGCODE(src.validate();)
reed@android.com4516f472009-06-29 16:25:36 +000087 sk_bzero(this, sizeof(*this));
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 *this = src;
89 SkDEBUGCODE(this->validate();)
90}
91
92SkBitmap::~SkBitmap() {
93 SkDEBUGCODE(this->validate();)
94 this->freePixels();
95}
96
97SkBitmap& SkBitmap::operator=(const SkBitmap& src) {
98 if (this != &src) {
99 this->freePixels();
100 memcpy(this, &src, sizeof(src));
101
102 // inc src reference counts
reed@android.com83f7bc32009-07-17 02:42:41 +0000103 SkSafeRef(src.fPixelRef);
reed@android.com149e2f62009-05-22 14:39:03 +0000104 SkSafeRef(src.fMipMap);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105
106 // we reset our locks if we get blown away
107 fPixelLockCount = 0;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000108
reed@google.com5f62ed72014-01-15 19:59:45 +0000109 if (fPixelRef) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110 // ignore the values from the memcpy
111 fPixels = NULL;
112 fColorTable = NULL;
bsalomon@google.com586f48c2011-04-14 15:07:22 +0000113 // Note that what to for genID is somewhat arbitrary. We have no
114 // way to track changes to raw pixels across multiple SkBitmaps.
115 // Would benefit from an SkRawPixelRef type created by
116 // setPixels.
117 // Just leave the memcpy'ed one but they'll get out of sync
118 // as soon either is modified.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119 }
120 }
121
122 SkDEBUGCODE(this->validate();)
123 return *this;
124}
125
126void SkBitmap::swap(SkBitmap& other) {
bsalomon@google.com586f48c2011-04-14 15:07:22 +0000127 SkTSwap(fColorTable, other.fColorTable);
128 SkTSwap(fPixelRef, other.fPixelRef);
reed@google.com672588b2014-01-08 15:42:01 +0000129 SkTSwap(fPixelRefOrigin, other.fPixelRefOrigin);
bsalomon@google.com586f48c2011-04-14 15:07:22 +0000130 SkTSwap(fPixelLockCount, other.fPixelLockCount);
131 SkTSwap(fMipMap, other.fMipMap);
132 SkTSwap(fPixels, other.fPixels);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000133 SkTSwap(fInfo, other.fInfo);
bsalomon@google.com586f48c2011-04-14 15:07:22 +0000134 SkTSwap(fRowBytes, other.fRowBytes);
bsalomon@google.com586f48c2011-04-14 15:07:22 +0000135 SkTSwap(fFlags, other.fFlags);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136
137 SkDEBUGCODE(this->validate();)
138}
139
140void SkBitmap::reset() {
141 this->freePixels();
reed@android.com4516f472009-06-29 16:25:36 +0000142 sk_bzero(this, sizeof(*this));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143}
144
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000145SkBitmap::Config SkBitmap::config() const {
146 return SkColorTypeToBitmapConfig(fInfo.colorType());
147}
148
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149int SkBitmap::ComputeBytesPerPixel(SkBitmap::Config config) {
150 int bpp;
151 switch (config) {
152 case kNo_Config:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153 bpp = 0; // not applicable
154 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 case kA8_Config:
156 case kIndex8_Config:
157 bpp = 1;
158 break;
159 case kRGB_565_Config:
160 case kARGB_4444_Config:
161 bpp = 2;
162 break;
163 case kARGB_8888_Config:
164 bpp = 4;
165 break;
166 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000167 SkDEBUGFAIL("unknown config");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000168 bpp = 0; // error
169 break;
170 }
171 return bpp;
172}
173
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000174size_t SkBitmap::ComputeRowBytes(Config c, int width) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000175 return SkColorTypeMinRowBytes(SkBitmapConfigToColorType(c), width);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176}
177
reed@google.com57212f92013-12-30 14:40:38 +0000178int64_t SkBitmap::ComputeSize64(Config config, int width, int height) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000179 SkColorType ct = SkBitmapConfigToColorType(config);
180 int64_t rowBytes = sk_64_mul(SkColorTypeBytesPerPixel(ct), width);
reed@google.com57212f92013-12-30 14:40:38 +0000181 return rowBytes * height;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000182}
183
184size_t SkBitmap::ComputeSize(Config c, int width, int height) {
reed@google.com57212f92013-12-30 14:40:38 +0000185 int64_t size = SkBitmap::ComputeSize64(c, width, height);
186 return sk_64_isS32(size) ? sk_64_asS32(size) : 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000187}
188
reed@google.com57212f92013-12-30 14:40:38 +0000189int64_t SkBitmap::ComputeSafeSize64(Config config,
190 uint32_t width,
191 uint32_t height,
192 size_t rowBytes) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000193 SkImageInfo info = SkImageInfo::Make(width, height,
194 SkBitmapConfigToColorType(config),
195 kPremul_SkAlphaType);
196 return info.getSafeSize64(rowBytes);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000197}
198
199size_t SkBitmap::ComputeSafeSize(Config config,
200 uint32_t width,
201 uint32_t height,
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000202 size_t rowBytes) {
reed@google.com57212f92013-12-30 14:40:38 +0000203 int64_t safeSize = ComputeSafeSize64(config, width, height, rowBytes);
204 int32_t safeSize32 = (int32_t)safeSize;
skia.committer@gmail.comf5e1f632013-12-31 07:01:36 +0000205
reed@google.com57212f92013-12-30 14:40:38 +0000206 if (safeSize32 != safeSize) {
207 safeSize32 = 0;
208 }
209 return safeSize32;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000210}
211
reed@google.com86b2e432012-03-15 21:17:03 +0000212void SkBitmap::getBounds(SkRect* bounds) const {
213 SkASSERT(bounds);
214 bounds->set(0, 0,
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000215 SkIntToScalar(fInfo.fWidth), SkIntToScalar(fInfo.fHeight));
reed@google.com86b2e432012-03-15 21:17:03 +0000216}
217
reed@google.com80e14592012-03-16 14:58:07 +0000218void SkBitmap::getBounds(SkIRect* bounds) const {
219 SkASSERT(bounds);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000220 bounds->set(0, 0, fInfo.fWidth, fInfo.fHeight);
reed@google.com80e14592012-03-16 14:58:07 +0000221}
222
reed@google.com86b2e432012-03-15 21:17:03 +0000223///////////////////////////////////////////////////////////////////////////////
224
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000225static bool validate_alphaType(SkColorType colorType, SkAlphaType alphaType,
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000226 SkAlphaType* canonical = NULL) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000227 switch (colorType) {
228 case kUnknown_SkColorType:
reed@google.com383a6972013-10-21 14:00:07 +0000229 alphaType = kIgnore_SkAlphaType;
230 break;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000231 case kAlpha_8_SkColorType:
reed@google.com383a6972013-10-21 14:00:07 +0000232 if (kUnpremul_SkAlphaType == alphaType) {
233 alphaType = kPremul_SkAlphaType;
234 }
235 // fall-through
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000236 case kIndex_8_SkColorType:
237 case kARGB_4444_SkColorType:
238 case kRGBA_8888_SkColorType:
239 case kBGRA_8888_SkColorType:
reed@google.com383a6972013-10-21 14:00:07 +0000240 if (kIgnore_SkAlphaType == alphaType) {
241 return false;
242 }
243 break;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000244 case kRGB_565_SkColorType:
reed@google.com383a6972013-10-21 14:00:07 +0000245 alphaType = kOpaque_SkAlphaType;
246 break;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000247 default:
248 return false;
reed@android.com149e2f62009-05-22 14:39:03 +0000249 }
reed@google.com383a6972013-10-21 14:00:07 +0000250 if (canonical) {
251 *canonical = alphaType;
252 }
253 return true;
254}
reed@android.com149e2f62009-05-22 14:39:03 +0000255
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000256bool SkBitmap::setConfig(const SkImageInfo& info, size_t rowBytes) {
257 // require that rowBytes fit in 31bits
258 int64_t mrb = info.minRowBytes64();
259 if ((int32_t)mrb != mrb) {
260 return reset_return_false(this);
reed@google.com383a6972013-10-21 14:00:07 +0000261 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000262 if ((int64_t)rowBytes != (int32_t)rowBytes) {
263 return reset_return_false(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000264 }
reed@android.com89bb83a2009-05-29 21:30:42 +0000265
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000266 if (info.width() < 0 || info.height() < 0) {
267 return reset_return_false(this);
268 }
269
270 if (kUnknown_SkColorType == info.colorType()) {
271 rowBytes = 0;
272 } else if (0 == rowBytes) {
273 rowBytes = (size_t)mrb;
274 } else if (rowBytes < info.minRowBytes()) {
275 return reset_return_false(this);
reed@google.com383a6972013-10-21 14:00:07 +0000276 }
277
278 this->freePixels();
279
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000280 fInfo = info;
281 fRowBytes = SkToU32(rowBytes);
reed@google.com383a6972013-10-21 14:00:07 +0000282 return true;
reed@google.com383a6972013-10-21 14:00:07 +0000283}
284
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000285bool SkBitmap::setConfig(Config config, int width, int height, size_t rowBytes,
286 SkAlphaType alphaType) {
287 SkColorType ct = SkBitmapConfigToColorType(config);
288 return this->setConfig(SkImageInfo::Make(width, height, ct, alphaType),
289 rowBytes);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000290}
291
reed@google.com383a6972013-10-21 14:00:07 +0000292bool SkBitmap::setAlphaType(SkAlphaType alphaType) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000293 if (!validate_alphaType(fInfo.fColorType, alphaType, &alphaType)) {
reed@google.com383a6972013-10-21 14:00:07 +0000294 return false;
295 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000296 if (fInfo.fAlphaType != alphaType) {
297 fInfo.fAlphaType = alphaType;
commit-bot@chromium.org0e8d0d62014-01-27 15:41:07 +0000298 if (fPixelRef) {
reed@google.comc1587f92014-01-28 16:05:39 +0000299 fPixelRef->changeAlphaType(alphaType);
commit-bot@chromium.org0e8d0d62014-01-27 15:41:07 +0000300 }
301 }
reed@google.com383a6972013-10-21 14:00:07 +0000302 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000303}
304
305void SkBitmap::updatePixelsFromRef() const {
306 if (NULL != fPixelRef) {
307 if (fPixelLockCount > 0) {
reed@google.comff0da4f2012-05-17 13:14:52 +0000308 SkASSERT(fPixelRef->isLocked());
weita@google.comf9ab99a2009-05-03 18:23:30 +0000309
reed@android.com8a1c16f2008-12-17 15:59:43 +0000310 void* p = fPixelRef->pixels();
311 if (NULL != p) {
reed@google.com672588b2014-01-08 15:42:01 +0000312 p = (char*)p
reed@google.com303c4752014-01-09 20:00:14 +0000313 + fPixelRefOrigin.fY * fRowBytes
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000314 + fPixelRefOrigin.fX * fInfo.bytesPerPixel();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000315 }
316 fPixels = p;
reed@google.com5f62ed72014-01-15 19:59:45 +0000317 fColorTable = fPixelRef->colorTable();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000318 } else {
319 SkASSERT(0 == fPixelLockCount);
320 fPixels = NULL;
reed@google.com5f62ed72014-01-15 19:59:45 +0000321 fColorTable = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000322 }
323 }
324}
325
reed@google.com9230ea22013-12-09 22:01:03 +0000326static bool config_to_colorType(SkBitmap::Config config, SkColorType* ctOut) {
327 SkColorType ct;
328 switch (config) {
329 case SkBitmap::kA8_Config:
330 ct = kAlpha_8_SkColorType;
331 break;
332 case SkBitmap::kIndex8_Config:
333 ct = kIndex_8_SkColorType;
334 break;
335 case SkBitmap::kRGB_565_Config:
336 ct = kRGB_565_SkColorType;
337 break;
338 case SkBitmap::kARGB_4444_Config:
339 ct = kARGB_4444_SkColorType;
340 break;
341 case SkBitmap::kARGB_8888_Config:
342 ct = kPMColor_SkColorType;
343 break;
344 case SkBitmap::kNo_Config:
345 default:
346 return false;
347 }
348 if (ctOut) {
349 *ctOut = ct;
350 }
351 return true;
352}
353
reed@google.com672588b2014-01-08 15:42:01 +0000354SkPixelRef* SkBitmap::setPixelRef(SkPixelRef* pr, int dx, int dy) {
reed@google.comdcea5302014-01-03 13:43:01 +0000355#ifdef SK_DEBUG
reed@google.com672588b2014-01-08 15:42:01 +0000356 if (pr) {
reed@google.comdcea5302014-01-03 13:43:01 +0000357 SkImageInfo info;
358 if (this->asImageInfo(&info)) {
359 const SkImageInfo& prInfo = pr->info();
360 SkASSERT(info.fWidth <= prInfo.fWidth);
361 SkASSERT(info.fHeight <= prInfo.fHeight);
362 SkASSERT(info.fColorType == prInfo.fColorType);
363 switch (prInfo.fAlphaType) {
364 case kIgnore_SkAlphaType:
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000365 SkASSERT(fInfo.fAlphaType == kIgnore_SkAlphaType);
reed@google.comdcea5302014-01-03 13:43:01 +0000366 break;
367 case kOpaque_SkAlphaType:
368 case kPremul_SkAlphaType:
369 SkASSERT(info.fAlphaType == kOpaque_SkAlphaType ||
370 info.fAlphaType == kPremul_SkAlphaType);
371 break;
372 case kUnpremul_SkAlphaType:
373 SkASSERT(info.fAlphaType == kOpaque_SkAlphaType ||
374 info.fAlphaType == kUnpremul_SkAlphaType);
375 break;
376 }
377 }
378 }
379#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000380
reed@google.com672588b2014-01-08 15:42:01 +0000381 if (pr) {
382 const SkImageInfo& info = pr->info();
383 fPixelRefOrigin.set(SkPin32(dx, 0, info.fWidth),
384 SkPin32(dy, 0, info.fHeight));
385 } else {
386 // ignore dx,dy if there is no pixelref
387 fPixelRefOrigin.setZero();
388 }
389
390 if (fPixelRef != pr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000391 if (fPixelRef != pr) {
392 this->freePixels();
393 SkASSERT(NULL == fPixelRef);
weita@google.comf9ab99a2009-05-03 18:23:30 +0000394
reed@google.com82065d62011-02-07 15:30:46 +0000395 SkSafeRef(pr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000396 fPixelRef = pr;
397 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000398 this->updatePixelsFromRef();
399 }
400
401 SkDEBUGCODE(this->validate();)
402 return pr;
403}
404
405void SkBitmap::lockPixels() const {
djsollen@google.com7c6d2642013-08-06 12:19:38 +0000406 if (NULL != fPixelRef && 0 == sk_atomic_inc(&fPixelLockCount)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000407 fPixelRef->lockPixels();
408 this->updatePixelsFromRef();
409 }
410 SkDEBUGCODE(this->validate();)
411}
412
413void SkBitmap::unlockPixels() const {
414 SkASSERT(NULL == fPixelRef || fPixelLockCount > 0);
415
djsollen@google.com7c6d2642013-08-06 12:19:38 +0000416 if (NULL != fPixelRef && 1 == sk_atomic_dec(&fPixelLockCount)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000417 fPixelRef->unlockPixels();
418 this->updatePixelsFromRef();
419 }
420 SkDEBUGCODE(this->validate();)
421}
422
reed@google.com9c49bc32011-07-07 13:42:37 +0000423bool SkBitmap::lockPixelsAreWritable() const {
djsollen@google.comc84b8332012-07-27 13:41:44 +0000424 return (fPixelRef) ? fPixelRef->lockPixelsAreWritable() : false;
reed@google.com9c49bc32011-07-07 13:42:37 +0000425}
426
reed@android.com8a1c16f2008-12-17 15:59:43 +0000427void SkBitmap::setPixels(void* p, SkColorTable* ctable) {
reed@google.com8e1034e2012-07-30 13:16:35 +0000428 if (NULL == p) {
reed@google.com672588b2014-01-08 15:42:01 +0000429 this->setPixelRef(NULL);
reed@google.com8e1034e2012-07-30 13:16:35 +0000430 return;
431 }
432
reed@google.combf790232013-12-13 19:45:58 +0000433 SkImageInfo info;
434 if (!this->asImageInfo(&info)) {
reed@google.com672588b2014-01-08 15:42:01 +0000435 this->setPixelRef(NULL);
reed@google.combf790232013-12-13 19:45:58 +0000436 return;
437 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000438
reed@google.combf790232013-12-13 19:45:58 +0000439 SkPixelRef* pr = SkMallocPixelRef::NewDirect(info, p, fRowBytes, ctable);
440 if (NULL == pr) {
reed@google.com672588b2014-01-08 15:42:01 +0000441 this->setPixelRef(NULL);
reed@google.combf790232013-12-13 19:45:58 +0000442 return;
443 }
444
445 this->setPixelRef(pr)->unref();
446
djsollen@google.comc84b8332012-07-27 13:41:44 +0000447 // since we're already allocated, we lockPixels right away
448 this->lockPixels();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000449 SkDEBUGCODE(this->validate();)
450}
451
452bool SkBitmap::allocPixels(Allocator* allocator, SkColorTable* ctable) {
453 HeapAllocator stdalloc;
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000454
reed@android.com8a1c16f2008-12-17 15:59:43 +0000455 if (NULL == allocator) {
456 allocator = &stdalloc;
457 }
458 return allocator->allocPixelRef(this, ctable);
459}
460
reed@google.com9ebcac52014-01-24 18:53:42 +0000461///////////////////////////////////////////////////////////////////////////////
462
reed@google.com9ebcac52014-01-24 18:53:42 +0000463bool SkBitmap::allocPixels(const SkImageInfo& info, SkPixelRefFactory* factory,
464 SkColorTable* ctable) {
465 if (kIndex_8_SkColorType == info.fColorType && NULL == ctable) {
466 return reset_return_false(this);
467 }
468 if (!this->setConfig(info)) {
469 return reset_return_false(this);
470 }
471
472 SkMallocPixelRef::PRFactory defaultFactory;
473 if (NULL == factory) {
474 factory = &defaultFactory;
475 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000476
reed@google.com9ebcac52014-01-24 18:53:42 +0000477 SkPixelRef* pr = factory->create(info, ctable);
478 if (NULL == pr) {
479 return reset_return_false(this);
480 }
481 this->setPixelRef(pr)->unref();
482
483 // TODO: lockPixels could/should return bool or void*/NULL
484 this->lockPixels();
485 if (NULL == this->getPixels()) {
486 return reset_return_false(this);
487 }
488 return true;
489}
490
491bool SkBitmap::installPixels(const SkImageInfo& info, void* pixels, size_t rb,
492 void (*releaseProc)(void* addr, void* context),
493 void* context) {
reed@google.comc2d768c2014-02-05 21:02:35 +0000494 if (!this->setConfig(info, rb)) {
reed@google.com9ebcac52014-01-24 18:53:42 +0000495 this->reset();
496 return false;
497 }
498
499 SkPixelRef* pr = SkMallocPixelRef::NewWithProc(info, rb, NULL, pixels,
500 releaseProc, context);
501 if (!pr) {
502 this->reset();
503 return false;
504 }
505
506 this->setPixelRef(pr)->unref();
507 return true;
508}
509
reed@google.comeb9a46c2014-01-25 16:46:20 +0000510bool SkBitmap::allocConfigPixels(Config config, int width, int height,
511 bool isOpaque) {
512 SkColorType ct;
513 if (!config_to_colorType(config, &ct)) {
514 return false;
515 }
skia.committer@gmail.com33c749a2014-01-26 07:01:45 +0000516
reed@google.comeb9a46c2014-01-25 16:46:20 +0000517 SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
reed@google.comeb9a46c2014-01-25 16:46:20 +0000518 return this->allocPixels(SkImageInfo::Make(width, height, ct, at));
519}
520
521///////////////////////////////////////////////////////////////////////////////
522
reed@android.com8a1c16f2008-12-17 15:59:43 +0000523void SkBitmap::freePixels() {
524 // if we're gonna free the pixels, we certainly need to free the mipmap
525 this->freeMipMap();
526
reed@android.com8a1c16f2008-12-17 15:59:43 +0000527 if (NULL != fPixelRef) {
528 if (fPixelLockCount > 0) {
529 fPixelRef->unlockPixels();
530 }
531 fPixelRef->unref();
532 fPixelRef = NULL;
reed@google.com672588b2014-01-08 15:42:01 +0000533 fPixelRefOrigin.setZero();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000534 }
535 fPixelLockCount = 0;
536 fPixels = NULL;
reed@google.com5f62ed72014-01-15 19:59:45 +0000537 fColorTable = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000538}
539
540void SkBitmap::freeMipMap() {
reed@android.com149e2f62009-05-22 14:39:03 +0000541 if (fMipMap) {
542 fMipMap->unref();
543 fMipMap = NULL;
544 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000545}
546
547uint32_t SkBitmap::getGenerationID() const {
djsollen@google.comc84b8332012-07-27 13:41:44 +0000548 return (fPixelRef) ? fPixelRef->getGenerationID() : 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000549}
550
551void SkBitmap::notifyPixelsChanged() const {
junov@chromium.orgb0521292011-12-15 20:14:06 +0000552 SkASSERT(!this->isImmutable());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000553 if (fPixelRef) {
554 fPixelRef->notifyPixelsChanged();
555 }
556}
557
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000558GrTexture* SkBitmap::getTexture() const {
reed@android.comce4e53a2010-09-09 16:01:26 +0000559 return fPixelRef ? fPixelRef->getTexture() : NULL;
560}
561
reed@android.com8a1c16f2008-12-17 15:59:43 +0000562///////////////////////////////////////////////////////////////////////////////
563
reed@android.com8a1c16f2008-12-17 15:59:43 +0000564/** We explicitly use the same allocator for our pixels that SkMask does,
565 so that we can freely assign memory allocated by one class to the other.
566 */
567bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst,
568 SkColorTable* ctable) {
reed@google.combf790232013-12-13 19:45:58 +0000569 SkImageInfo info;
570 if (!dst->asImageInfo(&info)) {
571// SkDebugf("unsupported config for info %d\n", dst->config());
572 return false;
573 }
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000574
reed@google.combf790232013-12-13 19:45:58 +0000575 SkPixelRef* pr = SkMallocPixelRef::NewAllocate(info, dst->rowBytes(),
576 ctable);
577 if (NULL == pr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000578 return false;
579 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000580
reed@google.com672588b2014-01-08 15:42:01 +0000581 dst->setPixelRef(pr)->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000582 // since we're already allocated, we lockPixels right away
583 dst->lockPixels();
584 return true;
585}
586
587///////////////////////////////////////////////////////////////////////////////
588
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000589bool SkBitmap::copyPixelsTo(void* const dst, size_t dstSize,
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000590 size_t dstRowBytes, bool preserveDstPad) const {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000591
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000592 if (0 == dstRowBytes) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000593 dstRowBytes = fRowBytes;
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000594 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000595
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000596 if (dstRowBytes < fInfo.minRowBytes() ||
597 dst == NULL || (getPixels() == NULL && pixelRef() == NULL)) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000598 return false;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000599 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000600
bsalomon@google.comc6980972011-11-02 19:57:21 +0000601 if (!preserveDstPad && static_cast<uint32_t>(dstRowBytes) == fRowBytes) {
reed@google.com44699382013-10-31 17:28:30 +0000602 size_t safeSize = this->getSafeSize();
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000603 if (safeSize > dstSize || safeSize == 0)
604 return false;
605 else {
606 SkAutoLockPixels lock(*this);
607 // This implementation will write bytes beyond the end of each row,
608 // excluding the last row, if the bitmap's stride is greater than
609 // strictly required by the current config.
610 memcpy(dst, getPixels(), safeSize);
611
612 return true;
613 }
614 } else {
615 // If destination has different stride than us, then copy line by line.
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000616 if (fInfo.getSafeSize(dstRowBytes) > dstSize) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000617 return false;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000618 } else {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000619 // Just copy what we need on each line.
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000620 size_t rowBytes = fInfo.minRowBytes();
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000621 SkAutoLockPixels lock(*this);
622 const uint8_t* srcP = reinterpret_cast<const uint8_t*>(getPixels());
623 uint8_t* dstP = reinterpret_cast<uint8_t*>(dst);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000624 for (int row = 0; row < fInfo.fHeight;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000625 row++, srcP += fRowBytes, dstP += dstRowBytes) {
626 memcpy(dstP, srcP, rowBytes);
627 }
628
629 return true;
630 }
631 }
632}
633
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000634///////////////////////////////////////////////////////////////////////////////
635
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000636bool SkBitmap::isImmutable() const {
junov@chromium.orgb0521292011-12-15 20:14:06 +0000637 return fPixelRef ? fPixelRef->isImmutable() :
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000638 fFlags & kImageIsImmutable_Flag;
junov@chromium.orgb0521292011-12-15 20:14:06 +0000639}
640
641void SkBitmap::setImmutable() {
642 if (fPixelRef) {
643 fPixelRef->setImmutable();
644 } else {
645 fFlags |= kImageIsImmutable_Flag;
646 }
647}
648
junov@google.com4ee7ae52011-06-30 17:30:49 +0000649bool SkBitmap::isVolatile() const {
650 return (fFlags & kImageIsVolatile_Flag) != 0;
651}
652
653void SkBitmap::setIsVolatile(bool isVolatile) {
654 if (isVolatile) {
655 fFlags |= kImageIsVolatile_Flag;
656 } else {
657 fFlags &= ~kImageIsVolatile_Flag;
658 }
659}
660
reed@android.com8a1c16f2008-12-17 15:59:43 +0000661void* SkBitmap::getAddr(int x, int y) const {
662 SkASSERT((unsigned)x < (unsigned)this->width());
663 SkASSERT((unsigned)y < (unsigned)this->height());
664
665 char* base = (char*)this->getPixels();
666 if (base) {
667 base += y * this->rowBytes();
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000668 switch (this->colorType()) {
669 case kRGBA_8888_SkColorType:
670 case kBGRA_8888_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000671 base += x << 2;
672 break;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000673 case kARGB_4444_SkColorType:
674 case kRGB_565_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000675 base += x << 1;
676 break;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000677 case kAlpha_8_SkColorType:
678 case kIndex_8_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000679 base += x;
680 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000681 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000682 SkDEBUGFAIL("Can't return addr for config");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000683 base = NULL;
684 break;
685 }
686 }
687 return base;
688}
689
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000690SkColor SkBitmap::getColor(int x, int y) const {
691 SkASSERT((unsigned)x < (unsigned)this->width());
692 SkASSERT((unsigned)y < (unsigned)this->height());
693
694 switch (this->config()) {
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000695 case SkBitmap::kA8_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000696 uint8_t* addr = this->getAddr8(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000697 return SkColorSetA(0, addr[0]);
698 }
699 case SkBitmap::kIndex8_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000700 SkPMColor c = this->getIndex8Color(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000701 return SkUnPreMultiply::PMColorToColor(c);
702 }
703 case SkBitmap::kRGB_565_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000704 uint16_t* addr = this->getAddr16(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000705 return SkPixel16ToColor(addr[0]);
706 }
707 case SkBitmap::kARGB_4444_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000708 uint16_t* addr = this->getAddr16(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000709 SkPMColor c = SkPixel4444ToPixel32(addr[0]);
710 return SkUnPreMultiply::PMColorToColor(c);
711 }
712 case SkBitmap::kARGB_8888_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000713 uint32_t* addr = this->getAddr32(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000714 return SkUnPreMultiply::PMColorToColor(addr[0]);
715 }
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000716 case kNo_Config:
rmistry@google.comd6bab022013-12-02 13:50:38 +0000717 default:
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000718 SkASSERT(false);
719 return 0;
720 }
721 SkASSERT(false); // Not reached.
722 return 0;
723}
724
reed@google.com2a7579d2012-11-07 18:30:18 +0000725bool SkBitmap::ComputeIsOpaque(const SkBitmap& bm) {
726 SkAutoLockPixels alp(bm);
727 if (!bm.getPixels()) {
728 return false;
729 }
730
731 const int height = bm.height();
732 const int width = bm.width();
733
734 switch (bm.config()) {
reed@google.com2a7579d2012-11-07 18:30:18 +0000735 case SkBitmap::kA8_Config: {
736 unsigned a = 0xFF;
737 for (int y = 0; y < height; ++y) {
738 const uint8_t* row = bm.getAddr8(0, y);
739 for (int x = 0; x < width; ++x) {
740 a &= row[x];
741 }
742 if (0xFF != a) {
743 return false;
744 }
745 }
746 return true;
747 } break;
reed@google.com2a7579d2012-11-07 18:30:18 +0000748 case SkBitmap::kIndex8_Config: {
749 SkAutoLockColors alc(bm);
750 const SkPMColor* table = alc.colors();
751 if (!table) {
752 return false;
753 }
reed@google.com140d7282013-01-07 20:25:04 +0000754 SkPMColor c = (SkPMColor)~0;
reed@google.com2a7579d2012-11-07 18:30:18 +0000755 for (int i = bm.getColorTable()->count() - 1; i >= 0; --i) {
756 c &= table[i];
757 }
758 return 0xFF == SkGetPackedA32(c);
759 } break;
760 case SkBitmap::kRGB_565_Config:
761 return true;
762 break;
763 case SkBitmap::kARGB_4444_Config: {
764 unsigned c = 0xFFFF;
765 for (int y = 0; y < height; ++y) {
766 const SkPMColor16* row = bm.getAddr16(0, y);
767 for (int x = 0; x < width; ++x) {
768 c &= row[x];
769 }
770 if (0xF != SkGetPackedA4444(c)) {
771 return false;
772 }
773 }
774 return true;
775 } break;
776 case SkBitmap::kARGB_8888_Config: {
reed@google.com140d7282013-01-07 20:25:04 +0000777 SkPMColor c = (SkPMColor)~0;
reed@google.com2a7579d2012-11-07 18:30:18 +0000778 for (int y = 0; y < height; ++y) {
779 const SkPMColor* row = bm.getAddr32(0, y);
780 for (int x = 0; x < width; ++x) {
781 c &= row[x];
782 }
783 if (0xFF != SkGetPackedA32(c)) {
784 return false;
785 }
786 }
787 return true;
788 }
789 default:
790 break;
791 }
792 return false;
793}
794
795
reed@android.com8a1c16f2008-12-17 15:59:43 +0000796///////////////////////////////////////////////////////////////////////////////
797///////////////////////////////////////////////////////////////////////////////
798
reed@google.com45f746f2013-06-21 19:51:31 +0000799static uint16_t pack_8888_to_4444(unsigned a, unsigned r, unsigned g, unsigned b) {
800 unsigned pixel = (SkA32To4444(a) << SK_A4444_SHIFT) |
801 (SkR32To4444(r) << SK_R4444_SHIFT) |
802 (SkG32To4444(g) << SK_G4444_SHIFT) |
803 (SkB32To4444(b) << SK_B4444_SHIFT);
804 return SkToU16(pixel);
805}
806
reed@google.com60d32352013-06-28 19:40:50 +0000807void SkBitmap::internalErase(const SkIRect& area,
808 U8CPU a, U8CPU r, U8CPU g, U8CPU b) const {
809#ifdef SK_DEBUG
reed@android.com8a1c16f2008-12-17 15:59:43 +0000810 SkDEBUGCODE(this->validate();)
reed@google.com60d32352013-06-28 19:40:50 +0000811 SkASSERT(!area.isEmpty());
812 {
reed@google.com92833f92013-06-28 19:47:43 +0000813 SkIRect total = { 0, 0, this->width(), this->height() };
reed@google.com60d32352013-06-28 19:40:50 +0000814 SkASSERT(total.contains(area));
815 }
816#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000817
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000818 switch (fInfo.colorType()) {
819 case kUnknown_SkColorType:
820 case kIndex_8_SkColorType:
821 return; // can't erase
822 default:
823 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000824 }
825
826 SkAutoLockPixels alp(*this);
827 // perform this check after the lock call
828 if (!this->readyToDraw()) {
829 return;
830 }
831
reed@google.com60d32352013-06-28 19:40:50 +0000832 int height = area.height();
833 const int width = area.width();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000834 const int rowBytes = fRowBytes;
835
836 // make rgb premultiplied
837 if (255 != a) {
838 r = SkAlphaMul(r, a);
839 g = SkAlphaMul(g, a);
840 b = SkAlphaMul(b, a);
841 }
842
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000843 switch (this->colorType()) {
844 case kAlpha_8_SkColorType: {
reed@google.com60d32352013-06-28 19:40:50 +0000845 uint8_t* p = this->getAddr8(area.fLeft, area.fTop);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000846 while (--height >= 0) {
847 memset(p, a, width);
848 p += rowBytes;
849 }
850 break;
851 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000852 case kARGB_4444_SkColorType:
853 case kRGB_565_SkColorType: {
reed@google.com60d32352013-06-28 19:40:50 +0000854 uint16_t* p = this->getAddr16(area.fLeft, area.fTop);;
reed@google.com45f746f2013-06-21 19:51:31 +0000855 uint16_t v;
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000856
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000857 if (kARGB_4444_SkColorType == this->colorType()) {
reed@google.com45f746f2013-06-21 19:51:31 +0000858 v = pack_8888_to_4444(a, r, g, b);
859 } else {
860 v = SkPackRGB16(r >> (8 - SK_R16_BITS),
861 g >> (8 - SK_G16_BITS),
862 b >> (8 - SK_B16_BITS));
863 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000864 while (--height >= 0) {
865 sk_memset16(p, v, width);
866 p = (uint16_t*)((char*)p + rowBytes);
867 }
868 break;
869 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000870 case kPMColor_SkColorType: {
871 // what to do about BGRA or RGBA (which ever is != PMColor ?
872 // for now we don't support them.
reed@google.com60d32352013-06-28 19:40:50 +0000873 uint32_t* p = this->getAddr32(area.fLeft, area.fTop);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000874 uint32_t v = SkPackARGB32(a, r, g, b);
875
876 while (--height >= 0) {
877 sk_memset32(p, v, width);
878 p = (uint32_t*)((char*)p + rowBytes);
879 }
880 break;
881 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000882 default:
883 return; // no change, so don't call notifyPixelsChanged()
reed@android.com8a1c16f2008-12-17 15:59:43 +0000884 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000885
reed@android.com8a1c16f2008-12-17 15:59:43 +0000886 this->notifyPixelsChanged();
887}
888
reed@google.com60d32352013-06-28 19:40:50 +0000889void SkBitmap::eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const {
reed@google.com92833f92013-06-28 19:47:43 +0000890 SkIRect area = { 0, 0, this->width(), this->height() };
reed@google.com60d32352013-06-28 19:40:50 +0000891 if (!area.isEmpty()) {
892 this->internalErase(area, a, r, g, b);
893 }
894}
895
896void SkBitmap::eraseArea(const SkIRect& rect, SkColor c) const {
reed@google.com92833f92013-06-28 19:47:43 +0000897 SkIRect area = { 0, 0, this->width(), this->height() };
reed@google.com60d32352013-06-28 19:40:50 +0000898 if (area.intersect(rect)) {
899 this->internalErase(area, SkColorGetA(c), SkColorGetR(c),
900 SkColorGetG(c), SkColorGetB(c));
901 }
902}
903
reed@android.com8a1c16f2008-12-17 15:59:43 +0000904//////////////////////////////////////////////////////////////////////////////////////
905//////////////////////////////////////////////////////////////////////////////////////
906
reed@android.com8a1c16f2008-12-17 15:59:43 +0000907bool SkBitmap::extractSubset(SkBitmap* result, const SkIRect& subset) const {
908 SkDEBUGCODE(this->validate();)
909
djsollen@google.comc84b8332012-07-27 13:41:44 +0000910 if (NULL == result || NULL == fPixelRef) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000911 return false; // no src pixels
912 }
913
914 SkIRect srcRect, r;
915 srcRect.set(0, 0, this->width(), this->height());
916 if (!r.intersect(srcRect, subset)) {
917 return false; // r is empty (i.e. no intersection)
918 }
919
scroggo@google.coma2a31922012-12-07 19:14:45 +0000920 if (fPixelRef->getTexture() != NULL) {
921 // Do a deep copy
922 SkPixelRef* pixelRef = fPixelRef->deepCopy(this->config(), &subset);
923 if (pixelRef != NULL) {
924 SkBitmap dst;
reed@google.com383a6972013-10-21 14:00:07 +0000925 dst.setConfig(this->config(), subset.width(), subset.height(), 0,
926 this->alphaType());
scroggo@google.coma2a31922012-12-07 19:14:45 +0000927 dst.setIsVolatile(this->isVolatile());
scroggo@google.coma2a31922012-12-07 19:14:45 +0000928 dst.setPixelRef(pixelRef)->unref();
929 SkDEBUGCODE(dst.validate());
930 result->swap(dst);
931 return true;
932 }
933 }
934
scroggo@google.coma2a31922012-12-07 19:14:45 +0000935 // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have
936 // exited above.
937 SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width()));
938 SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height()));
939
reed@android.com8a1c16f2008-12-17 15:59:43 +0000940 SkBitmap dst;
reed@google.com383a6972013-10-21 14:00:07 +0000941 dst.setConfig(this->config(), r.width(), r.height(), this->rowBytes(),
942 this->alphaType());
skyostil@google.com0eb75762012-01-16 10:45:53 +0000943 dst.setIsVolatile(this->isVolatile());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000944
945 if (fPixelRef) {
reed@google.com672588b2014-01-08 15:42:01 +0000946 SkIPoint origin = fPixelRefOrigin;
947 origin.fX += r.fLeft;
948 origin.fY += r.fTop;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000949 // share the pixelref with a custom offset
reed@google.com672588b2014-01-08 15:42:01 +0000950 dst.setPixelRef(fPixelRef, origin);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000951 }
952 SkDEBUGCODE(dst.validate();)
953
954 // we know we're good, so commit to result
955 result->swap(dst);
956 return true;
957}
958
959///////////////////////////////////////////////////////////////////////////////
960
961#include "SkCanvas.h"
962#include "SkPaint.h"
963
reed@android.comfbaa88d2009-05-06 17:44:34 +0000964bool SkBitmap::canCopyTo(Config dstConfig) const {
reed@google.com44699382013-10-31 17:28:30 +0000965 if (this->config() == kNo_Config) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000966 return false;
967 }
968
reed@android.comfbaa88d2009-05-06 17:44:34 +0000969 bool sameConfigs = (this->config() == dstConfig);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000970 switch (dstConfig) {
971 case kA8_Config:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000972 case kRGB_565_Config:
973 case kARGB_8888_Config:
974 break;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000975 case kIndex8_Config:
976 if (!sameConfigs) {
977 return false;
978 }
979 break;
scroggo@google.com8dc8bc52013-08-07 19:16:05 +0000980 case kARGB_4444_Config:
981 return sameConfigs || kARGB_8888_Config == this->config();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000982 default:
983 return false;
984 }
reed@android.comfbaa88d2009-05-06 17:44:34 +0000985 return true;
986}
987
988bool SkBitmap::copyTo(SkBitmap* dst, Config dstConfig, Allocator* alloc) const {
989 if (!this->canCopyTo(dstConfig)) {
990 return false;
991 }
992
reed@google.com50dfa012011-04-01 19:05:36 +0000993 // if we have a texture, first get those pixels
994 SkBitmap tmpSrc;
995 const SkBitmap* src = this;
996
scroggo@google.coma2a31922012-12-07 19:14:45 +0000997 if (fPixelRef) {
998 SkIRect subset;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000999 subset.setXYWH(fPixelRefOrigin.fX, fPixelRefOrigin.fY,
1000 fInfo.width(), fInfo.height());
reed@google.com672588b2014-01-08 15:42:01 +00001001 if (fPixelRef->readPixels(&tmpSrc, &subset)) {
1002 SkASSERT(tmpSrc.width() == this->width());
1003 SkASSERT(tmpSrc.height() == this->height());
reed@google.com50dfa012011-04-01 19:05:36 +00001004
reed@google.com672588b2014-01-08 15:42:01 +00001005 // did we get lucky and we can just return tmpSrc?
1006 if (tmpSrc.config() == dstConfig && NULL == alloc) {
1007 dst->swap(tmpSrc);
scroggo@google.com5ccae2c2014-01-15 16:56:52 +00001008 // If the result is an exact copy, clone the gen ID.
1009 if (dst->pixelRef() && dst->pixelRef()->info() == fPixelRef->info()) {
reed@google.com672588b2014-01-08 15:42:01 +00001010 dst->pixelRef()->cloneGenID(*fPixelRef);
scroggo@google.coma2a31922012-12-07 19:14:45 +00001011 }
reed@google.com672588b2014-01-08 15:42:01 +00001012 return true;
scroggo@google.comd5764e82012-08-22 15:00:05 +00001013 }
reed@google.com672588b2014-01-08 15:42:01 +00001014
1015 // fall through to the raster case
1016 src = &tmpSrc;
reed@google.com50dfa012011-04-01 19:05:36 +00001017 }
reed@android.comfbaa88d2009-05-06 17:44:34 +00001018 }
reed@android.com311c82d2009-05-05 23:13:23 +00001019
reed@google.com50dfa012011-04-01 19:05:36 +00001020 // we lock this now, since we may need its colortable
1021 SkAutoLockPixels srclock(*src);
1022 if (!src->readyToDraw()) {
1023 return false;
1024 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001025
scroggo@google.com5ccae2c2014-01-15 16:56:52 +00001026 // The only way to be readyToDraw is if fPixelRef is non NULL.
1027 SkASSERT(fPixelRef != NULL);
1028
reed@google.com50dfa012011-04-01 19:05:36 +00001029 SkBitmap tmpDst;
reed@google.com383a6972013-10-21 14:00:07 +00001030 tmpDst.setConfig(dstConfig, src->width(), src->height(), 0,
1031 src->alphaType());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001032
weita@google.comf9ab99a2009-05-03 18:23:30 +00001033 // allocate colortable if srcConfig == kIndex8_Config
1034 SkColorTable* ctable = (dstConfig == kIndex8_Config) ?
reed@google.com50dfa012011-04-01 19:05:36 +00001035 new SkColorTable(*src->getColorTable()) : NULL;
weita@google.comf9ab99a2009-05-03 18:23:30 +00001036 SkAutoUnref au(ctable);
reed@google.com50dfa012011-04-01 19:05:36 +00001037 if (!tmpDst.allocPixels(alloc, ctable)) {
weita@google.comf9ab99a2009-05-03 18:23:30 +00001038 return false;
1039 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001040
reed@google.com50dfa012011-04-01 19:05:36 +00001041 if (!tmpDst.readyToDraw()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001042 // allocator/lock failed
1043 return false;
1044 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001045
scroggo@google.com5ccae2c2014-01-15 16:56:52 +00001046 // pixelRef must be non NULL or tmpDst.readyToDraw() would have
1047 // returned false.
1048 SkASSERT(tmpDst.pixelRef() != NULL);
1049
reed@android.comfbaa88d2009-05-06 17:44:34 +00001050 /* do memcpy for the same configs cases, else use drawing
weita@google.comf9ab99a2009-05-03 18:23:30 +00001051 */
reed@google.com50dfa012011-04-01 19:05:36 +00001052 if (src->config() == dstConfig) {
1053 if (tmpDst.getSize() == src->getSize()) {
1054 memcpy(tmpDst.getPixels(), src->getPixels(), src->getSafeSize());
scroggo@google.comd5764e82012-08-22 15:00:05 +00001055 SkPixelRef* pixelRef = tmpDst.pixelRef();
scroggo@google.com5ccae2c2014-01-15 16:56:52 +00001056
1057 // In order to reach this point, we know that the width, config and
1058 // rowbytes of the SkPixelRefs are the same, but it is possible for
1059 // the heights to differ, if this SkBitmap's height is a subset of
1060 // fPixelRef. Only if the SkPixelRefs' heights match are we
1061 // guaranteed that this is an exact copy, meaning we should clone
1062 // the genID.
1063 if (pixelRef->info().fHeight == fPixelRef->info().fHeight) {
1064 // TODO: what to do if the two infos match, BUT
1065 // fPixelRef is premul and pixelRef is opaque?
1066 // skipping assert for now
1067 // https://code.google.com/p/skia/issues/detail?id=2012
1068// SkASSERT(pixelRef->info() == fPixelRef->info());
1069 SkASSERT(pixelRef->info().fWidth == fPixelRef->info().fWidth);
1070 SkASSERT(pixelRef->info().fColorType == fPixelRef->info().fColorType);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +00001071 pixelRef->cloneGenID(*fPixelRef);
scroggo@google.comd5764e82012-08-22 15:00:05 +00001072 }
reed@android.com311c82d2009-05-05 23:13:23 +00001073 } else {
reed@google.com50dfa012011-04-01 19:05:36 +00001074 const char* srcP = reinterpret_cast<const char*>(src->getPixels());
1075 char* dstP = reinterpret_cast<char*>(tmpDst.getPixels());
reed@android.com311c82d2009-05-05 23:13:23 +00001076 // to be sure we don't read too much, only copy our logical pixels
reed@google.com50dfa012011-04-01 19:05:36 +00001077 size_t bytesToCopy = tmpDst.width() * tmpDst.bytesPerPixel();
1078 for (int y = 0; y < tmpDst.height(); y++) {
reed@android.com311c82d2009-05-05 23:13:23 +00001079 memcpy(dstP, srcP, bytesToCopy);
reed@google.com50dfa012011-04-01 19:05:36 +00001080 srcP += src->rowBytes();
1081 dstP += tmpDst.rowBytes();
reed@android.com311c82d2009-05-05 23:13:23 +00001082 }
1083 }
scroggo@google.com8dc8bc52013-08-07 19:16:05 +00001084 } else if (SkBitmap::kARGB_4444_Config == dstConfig
1085 && SkBitmap::kARGB_8888_Config == src->config()) {
1086 SkASSERT(src->height() == tmpDst.height());
1087 SkASSERT(src->width() == tmpDst.width());
1088 for (int y = 0; y < src->height(); ++y) {
1089 SkPMColor16* SK_RESTRICT dstRow = (SkPMColor16*) tmpDst.getAddr16(0, y);
1090 SkPMColor* SK_RESTRICT srcRow = (SkPMColor*) src->getAddr32(0, y);
1091 DITHER_4444_SCAN(y);
1092 for (int x = 0; x < src->width(); ++x) {
1093 dstRow[x] = SkDitherARGB32To4444(srcRow[x],
1094 DITHER_VALUE(x));
1095 }
1096 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001097 } else {
robertphillips@google.com0197b322013-10-10 15:48:16 +00001098 // Always clear the dest in case one of the blitters accesses it
1099 // TODO: switch the allocation of tmpDst to call sk_calloc_throw
1100 tmpDst.eraseColor(SK_ColorTRANSPARENT);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001101
reed@google.com50dfa012011-04-01 19:05:36 +00001102 SkCanvas canvas(tmpDst);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001103 SkPaint paint;
1104
1105 paint.setDither(true);
reed@google.com50dfa012011-04-01 19:05:36 +00001106 canvas.drawBitmap(*src, 0, 0, &paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001107 }
1108
reed@google.com50dfa012011-04-01 19:05:36 +00001109 dst->swap(tmpDst);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001110 return true;
1111}
1112
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001113bool SkBitmap::deepCopyTo(SkBitmap* dst, Config dstConfig) const {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001114 const SkColorType dstCT = SkBitmapConfigToColorType(dstConfig);
1115
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001116 if (!this->canCopyTo(dstConfig)) {
1117 return false;
1118 }
1119
1120 // If we have a PixelRef, and it supports deep copy, use it.
1121 // Currently supported only by texture-backed bitmaps.
1122 if (fPixelRef) {
1123 SkPixelRef* pixelRef = fPixelRef->deepCopy(dstConfig);
1124 if (pixelRef) {
scroggo@google.coma2a31922012-12-07 19:14:45 +00001125 uint32_t rowBytes;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001126 if (this->colorType() == dstCT) {
scroggo@google.com5ccae2c2014-01-15 16:56:52 +00001127 // Since there is no subset to pass to deepCopy, and deepCopy
1128 // succeeded, the new pixel ref must be identical.
1129 SkASSERT(fPixelRef->info() == pixelRef->info());
commit-bot@chromium.org50a30432013-10-24 17:44:27 +00001130 pixelRef->cloneGenID(*fPixelRef);
scroggo@google.coma2a31922012-12-07 19:14:45 +00001131 // Use the same rowBytes as the original.
1132 rowBytes = fRowBytes;
1133 } else {
1134 // With the new config, an appropriate fRowBytes will be computed by setConfig.
1135 rowBytes = 0;
scroggo@google.comd5764e82012-08-22 15:00:05 +00001136 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001137
1138 SkImageInfo info = fInfo;
1139 info.fColorType = dstCT;
1140 if (!dst->setConfig(info, rowBytes)) {
1141 return false;
1142 }
reed@google.com672588b2014-01-08 15:42:01 +00001143 dst->setPixelRef(pixelRef, fPixelRefOrigin)->unref();
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001144 return true;
1145 }
1146 }
1147
1148 if (this->getTexture()) {
1149 return false;
1150 } else {
1151 return this->copyTo(dst, dstConfig, NULL);
1152 }
1153}
1154
reed@android.com8a1c16f2008-12-17 15:59:43 +00001155///////////////////////////////////////////////////////////////////////////////
1156///////////////////////////////////////////////////////////////////////////////
1157
1158static void downsampleby2_proc32(SkBitmap* dst, int x, int y,
1159 const SkBitmap& src) {
1160 x <<= 1;
1161 y <<= 1;
1162 const SkPMColor* p = src.getAddr32(x, y);
reed@android.com829c83c2009-06-08 12:05:31 +00001163 const SkPMColor* baseP = p;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001164 SkPMColor c, ag, rb;
1165
1166 c = *p; ag = (c >> 8) & 0xFF00FF; rb = c & 0xFF00FF;
1167 if (x < src.width() - 1) {
1168 p += 1;
1169 }
1170 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
1171
reed@android.com829c83c2009-06-08 12:05:31 +00001172 p = baseP;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001173 if (y < src.height() - 1) {
reed@android.com829c83c2009-06-08 12:05:31 +00001174 p += src.rowBytes() >> 2;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001175 }
1176 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
1177 if (x < src.width() - 1) {
1178 p += 1;
1179 }
1180 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
1181
1182 *dst->getAddr32(x >> 1, y >> 1) =
1183 ((rb >> 2) & 0xFF00FF) | ((ag << 6) & 0xFF00FF00);
1184}
1185
1186static inline uint32_t expand16(U16CPU c) {
1187 return (c & ~SK_G16_MASK_IN_PLACE) | ((c & SK_G16_MASK_IN_PLACE) << 16);
1188}
1189
1190// returns dirt in the top 16bits, but we don't care, since we only
1191// store the low 16bits.
1192static inline U16CPU pack16(uint32_t c) {
1193 return (c & ~SK_G16_MASK_IN_PLACE) | ((c >> 16) & SK_G16_MASK_IN_PLACE);
1194}
1195
1196static void downsampleby2_proc16(SkBitmap* dst, int x, int y,
1197 const SkBitmap& src) {
1198 x <<= 1;
1199 y <<= 1;
1200 const uint16_t* p = src.getAddr16(x, y);
reed@android.com829c83c2009-06-08 12:05:31 +00001201 const uint16_t* baseP = p;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001202 SkPMColor c;
weita@google.comf9ab99a2009-05-03 18:23:30 +00001203
reed@android.com8a1c16f2008-12-17 15:59:43 +00001204 c = expand16(*p);
reed@android.com829c83c2009-06-08 12:05:31 +00001205 if (x < src.width() - 1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001206 p += 1;
1207 }
1208 c += expand16(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001209
reed@android.com829c83c2009-06-08 12:05:31 +00001210 p = baseP;
1211 if (y < src.height() - 1) {
1212 p += src.rowBytes() >> 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001213 }
1214 c += expand16(*p);
reed@android.com829c83c2009-06-08 12:05:31 +00001215 if (x < src.width() - 1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001216 p += 1;
1217 }
1218 c += expand16(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001219
reed@android.com8a1c16f2008-12-17 15:59:43 +00001220 *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)pack16(c >> 2);
1221}
1222
1223static uint32_t expand4444(U16CPU c) {
1224 return (c & 0xF0F) | ((c & ~0xF0F) << 12);
1225}
1226
1227static U16CPU collaps4444(uint32_t c) {
1228 return (c & 0xF0F) | ((c >> 12) & ~0xF0F);
1229}
1230
1231static void downsampleby2_proc4444(SkBitmap* dst, int x, int y,
1232 const SkBitmap& src) {
1233 x <<= 1;
1234 y <<= 1;
1235 const uint16_t* p = src.getAddr16(x, y);
reed@android.com829c83c2009-06-08 12:05:31 +00001236 const uint16_t* baseP = p;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001237 uint32_t c;
weita@google.comf9ab99a2009-05-03 18:23:30 +00001238
reed@android.com8a1c16f2008-12-17 15:59:43 +00001239 c = expand4444(*p);
1240 if (x < src.width() - 1) {
1241 p += 1;
1242 }
1243 c += expand4444(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001244
reed@android.com829c83c2009-06-08 12:05:31 +00001245 p = baseP;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001246 if (y < src.height() - 1) {
reed@android.com829c83c2009-06-08 12:05:31 +00001247 p += src.rowBytes() >> 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001248 }
1249 c += expand4444(*p);
1250 if (x < src.width() - 1) {
1251 p += 1;
1252 }
1253 c += expand4444(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001254
reed@android.com8a1c16f2008-12-17 15:59:43 +00001255 *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)collaps4444(c >> 2);
1256}
1257
1258void SkBitmap::buildMipMap(bool forceRebuild) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001259 if (forceRebuild)
1260 this->freeMipMap();
1261 else if (fMipMap)
1262 return; // we're already built
1263
1264 SkASSERT(NULL == fMipMap);
1265
1266 void (*proc)(SkBitmap* dst, int x, int y, const SkBitmap& src);
1267
reed@google.com44699382013-10-31 17:28:30 +00001268 const SkBitmap::Config config = this->config();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001269
1270 switch (config) {
1271 case kARGB_8888_Config:
1272 proc = downsampleby2_proc32;
1273 break;
1274 case kRGB_565_Config:
1275 proc = downsampleby2_proc16;
1276 break;
1277 case kARGB_4444_Config:
1278 proc = downsampleby2_proc4444;
1279 break;
1280 case kIndex8_Config:
1281 case kA8_Config:
1282 default:
1283 return; // don't build mipmaps for these configs
1284 }
reed@android.com89bb83a2009-05-29 21:30:42 +00001285
reed@android.com149e2f62009-05-22 14:39:03 +00001286 SkAutoLockPixels alp(*this);
1287 if (!this->readyToDraw()) {
1288 return;
1289 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001290
1291 // whip through our loop to compute the exact size needed
1292 size_t size = 0;
1293 int maxLevels = 0;
1294 {
reed@android.com149e2f62009-05-22 14:39:03 +00001295 int width = this->width();
1296 int height = this->height();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001297 for (;;) {
1298 width >>= 1;
1299 height >>= 1;
1300 if (0 == width || 0 == height) {
1301 break;
1302 }
1303 size += ComputeRowBytes(config, width) * height;
1304 maxLevels += 1;
1305 }
1306 }
reed@android.com89bb83a2009-05-29 21:30:42 +00001307
reed@android.com149e2f62009-05-22 14:39:03 +00001308 // nothing to build
reed@android.com8a1c16f2008-12-17 15:59:43 +00001309 if (0 == maxLevels) {
1310 return;
1311 }
1312
reed@android.com149e2f62009-05-22 14:39:03 +00001313 SkBitmap srcBM(*this);
1314 srcBM.lockPixels();
1315 if (!srcBM.readyToDraw()) {
1316 return;
1317 }
1318
1319 MipMap* mm = MipMap::Alloc(maxLevels, size);
1320 if (NULL == mm) {
1321 return;
1322 }
1323
reed@android.com8a1c16f2008-12-17 15:59:43 +00001324 MipLevel* level = mm->levels();
1325 uint8_t* addr = (uint8_t*)mm->pixels();
reed@android.com149e2f62009-05-22 14:39:03 +00001326 int width = this->width();
1327 int height = this->height();
scroggo@google.come5f48242013-02-25 21:47:41 +00001328 uint32_t rowBytes;
reed@android.com149e2f62009-05-22 14:39:03 +00001329 SkBitmap dstBM;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001330
1331 for (int i = 0; i < maxLevels; i++) {
1332 width >>= 1;
1333 height >>= 1;
scroggo@google.come5f48242013-02-25 21:47:41 +00001334 rowBytes = SkToU32(ComputeRowBytes(config, width));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001335
1336 level[i].fPixels = addr;
reed@android.comf459a492009-03-27 12:33:50 +00001337 level[i].fWidth = width;
1338 level[i].fHeight = height;
reed@android.com49f0ff22009-03-19 21:52:42 +00001339 level[i].fRowBytes = rowBytes;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001340
1341 dstBM.setConfig(config, width, height, rowBytes);
1342 dstBM.setPixels(addr);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001343
bungeman@google.com7cf0e9e2012-07-25 16:09:10 +00001344 srcBM.lockPixels();
reed@android.com149e2f62009-05-22 14:39:03 +00001345 for (int y = 0; y < height; y++) {
1346 for (int x = 0; x < width; x++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001347 proc(&dstBM, x, y, srcBM);
1348 }
1349 }
bungeman@google.com7cf0e9e2012-07-25 16:09:10 +00001350 srcBM.unlockPixels();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001351
1352 srcBM = dstBM;
1353 addr += height * rowBytes;
1354 }
1355 SkASSERT(addr == (uint8_t*)mm->pixels() + size);
1356 fMipMap = mm;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001357}
1358
1359bool SkBitmap::hasMipMap() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001360 return fMipMap != NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001361}
1362
1363int SkBitmap::extractMipLevel(SkBitmap* dst, SkFixed sx, SkFixed sy) {
reed@android.com83f7bc32009-07-17 02:42:41 +00001364 if (NULL == fMipMap) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001365 return 0;
reed@android.com83f7bc32009-07-17 02:42:41 +00001366 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001367
reed@android.com8a1c16f2008-12-17 15:59:43 +00001368 int level = ComputeMipLevel(sx, sy) >> 16;
1369 SkASSERT(level >= 0);
1370 if (level <= 0) {
1371 return 0;
1372 }
1373
1374 if (level >= fMipMap->fLevelCount) {
1375 level = fMipMap->fLevelCount - 1;
1376 }
1377 if (dst) {
1378 const MipLevel& mip = fMipMap->levels()[level - 1];
1379 dst->setConfig((SkBitmap::Config)this->config(),
1380 mip.fWidth, mip.fHeight, mip.fRowBytes);
1381 dst->setPixels(mip.fPixels);
1382 }
1383 return level;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001384}
1385
1386SkFixed SkBitmap::ComputeMipLevel(SkFixed sx, SkFixed sy) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001387 sx = SkAbs32(sx);
1388 sy = SkAbs32(sy);
1389 if (sx < sy) {
1390 sx = sy;
1391 }
1392 if (sx < SK_Fixed1) {
1393 return 0;
1394 }
1395 int clz = SkCLZ(sx);
1396 SkASSERT(clz >= 1 && clz <= 15);
1397 return SkIntToFixed(15 - clz) + ((unsigned)(sx << (clz + 1)) >> 16);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001398}
1399
1400///////////////////////////////////////////////////////////////////////////////
1401
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +00001402static bool GetBitmapAlpha(const SkBitmap& src, uint8_t* SK_RESTRICT alpha,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001403 int alphaRowBytes) {
1404 SkASSERT(alpha != NULL);
1405 SkASSERT(alphaRowBytes >= src.width());
1406
reed@google.com44699382013-10-31 17:28:30 +00001407 SkBitmap::Config config = src.config();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001408 int w = src.width();
1409 int h = src.height();
scroggo@google.come5f48242013-02-25 21:47:41 +00001410 size_t rb = src.rowBytes();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001411
reed@android.com1cdcb512009-08-24 19:11:00 +00001412 SkAutoLockPixels alp(src);
1413 if (!src.readyToDraw()) {
1414 // zero out the alpha buffer and return
1415 while (--h >= 0) {
1416 memset(alpha, 0, w);
1417 alpha += alphaRowBytes;
1418 }
1419 return false;
1420 }
reed@google.com82065d62011-02-07 15:30:46 +00001421
reed@android.com8a1c16f2008-12-17 15:59:43 +00001422 if (SkBitmap::kA8_Config == config && !src.isOpaque()) {
1423 const uint8_t* s = src.getAddr8(0, 0);
1424 while (--h >= 0) {
1425 memcpy(alpha, s, w);
1426 s += rb;
1427 alpha += alphaRowBytes;
1428 }
1429 } else if (SkBitmap::kARGB_8888_Config == config && !src.isOpaque()) {
1430 const SkPMColor* SK_RESTRICT s = src.getAddr32(0, 0);
1431 while (--h >= 0) {
1432 for (int x = 0; x < w; x++) {
1433 alpha[x] = SkGetPackedA32(s[x]);
1434 }
1435 s = (const SkPMColor*)((const char*)s + rb);
1436 alpha += alphaRowBytes;
1437 }
1438 } else if (SkBitmap::kARGB_4444_Config == config && !src.isOpaque()) {
1439 const SkPMColor16* SK_RESTRICT s = src.getAddr16(0, 0);
1440 while (--h >= 0) {
1441 for (int x = 0; x < w; x++) {
1442 alpha[x] = SkPacked4444ToA32(s[x]);
1443 }
1444 s = (const SkPMColor16*)((const char*)s + rb);
1445 alpha += alphaRowBytes;
1446 }
1447 } else if (SkBitmap::kIndex8_Config == config && !src.isOpaque()) {
1448 SkColorTable* ct = src.getColorTable();
1449 if (ct) {
1450 const SkPMColor* SK_RESTRICT table = ct->lockColors();
1451 const uint8_t* SK_RESTRICT s = src.getAddr8(0, 0);
1452 while (--h >= 0) {
1453 for (int x = 0; x < w; x++) {
1454 alpha[x] = SkGetPackedA32(table[s[x]]);
1455 }
1456 s += rb;
1457 alpha += alphaRowBytes;
1458 }
reed@google.com0a6151d2013-10-10 14:44:56 +00001459 ct->unlockColors();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001460 }
1461 } else { // src is opaque, so just fill alpha[] with 0xFF
1462 memset(alpha, 0xFF, h * alphaRowBytes);
1463 }
reed@android.com1cdcb512009-08-24 19:11:00 +00001464 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001465}
1466
1467#include "SkPaint.h"
1468#include "SkMaskFilter.h"
1469#include "SkMatrix.h"
1470
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001471bool SkBitmap::extractAlpha(SkBitmap* dst, const SkPaint* paint,
djsollen@google.com57f49692011-02-23 20:46:31 +00001472 Allocator *allocator, SkIPoint* offset) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001473 SkDEBUGCODE(this->validate();)
1474
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001475 SkBitmap tmpBitmap;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001476 SkMatrix identity;
1477 SkMask srcM, dstM;
1478
1479 srcM.fBounds.set(0, 0, this->width(), this->height());
1480 srcM.fRowBytes = SkAlign4(this->width());
1481 srcM.fFormat = SkMask::kA8_Format;
1482
1483 SkMaskFilter* filter = paint ? paint->getMaskFilter() : NULL;
1484
1485 // compute our (larger?) dst bounds if we have a filter
1486 if (NULL != filter) {
1487 identity.reset();
1488 srcM.fImage = NULL;
1489 if (!filter->filterMask(&dstM, srcM, identity, NULL)) {
1490 goto NO_FILTER_CASE;
1491 }
1492 dstM.fRowBytes = SkAlign4(dstM.fBounds.width());
1493 } else {
1494 NO_FILTER_CASE:
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001495 tmpBitmap.setConfig(SkBitmap::kA8_Config, this->width(), this->height(),
reed@android.com8a1c16f2008-12-17 15:59:43 +00001496 srcM.fRowBytes);
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001497 if (!tmpBitmap.allocPixels(allocator, NULL)) {
1498 // Allocation of pixels for alpha bitmap failed.
1499 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
1500 tmpBitmap.width(), tmpBitmap.height());
1501 return false;
1502 }
1503 GetBitmapAlpha(*this, tmpBitmap.getAddr8(0, 0), srcM.fRowBytes);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001504 if (offset) {
1505 offset->set(0, 0);
1506 }
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001507 tmpBitmap.swap(*dst);
1508 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001509 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001510 srcM.fImage = SkMask::AllocImage(srcM.computeImageSize());
1511 SkAutoMaskFreeImage srcCleanup(srcM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001512
1513 GetBitmapAlpha(*this, srcM.fImage, srcM.fRowBytes);
1514 if (!filter->filterMask(&dstM, srcM, identity, NULL)) {
1515 goto NO_FILTER_CASE;
1516 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001517 SkAutoMaskFreeImage dstCleanup(dstM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001518
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001519 tmpBitmap.setConfig(SkBitmap::kA8_Config, dstM.fBounds.width(),
reed@android.com8a1c16f2008-12-17 15:59:43 +00001520 dstM.fBounds.height(), dstM.fRowBytes);
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001521 if (!tmpBitmap.allocPixels(allocator, NULL)) {
1522 // Allocation of pixels for alpha bitmap failed.
1523 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
1524 tmpBitmap.width(), tmpBitmap.height());
1525 return false;
1526 }
1527 memcpy(tmpBitmap.getPixels(), dstM.fImage, dstM.computeImageSize());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001528 if (offset) {
1529 offset->set(dstM.fBounds.fLeft, dstM.fBounds.fTop);
1530 }
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001531 SkDEBUGCODE(tmpBitmap.validate();)
1532
1533 tmpBitmap.swap(*dst);
1534 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001535}
1536
1537///////////////////////////////////////////////////////////////////////////////
1538
1539enum {
1540 SERIALIZE_PIXELTYPE_NONE,
djsollen@google.com21830d92012-08-07 19:49:41 +00001541 SERIALIZE_PIXELTYPE_REF_DATA
reed@android.com8a1c16f2008-12-17 15:59:43 +00001542};
1543
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +00001544void SkBitmap::flatten(SkWriteBuffer& buffer) const {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001545 fInfo.flatten(buffer);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001546 buffer.writeInt(fRowBytes);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001547
reed@android.com8a1c16f2008-12-17 15:59:43 +00001548 if (fPixelRef) {
djsollen@google.com5370cd92012-03-28 20:47:01 +00001549 if (fPixelRef->getFactory()) {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001550 buffer.writeInt(SERIALIZE_PIXELTYPE_REF_DATA);
reed@google.com672588b2014-01-08 15:42:01 +00001551 buffer.writeInt(fPixelRefOrigin.fX);
1552 buffer.writeInt(fPixelRefOrigin.fY);
djsollen@google.com5370cd92012-03-28 20:47:01 +00001553 buffer.writeFlattenable(fPixelRef);
1554 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001555 }
1556 // if we get here, we can't record the pixels
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001557 buffer.writeInt(SERIALIZE_PIXELTYPE_NONE);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001558 } else {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001559 buffer.writeInt(SERIALIZE_PIXELTYPE_NONE);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001560 }
1561}
1562
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +00001563void SkBitmap::unflatten(SkReadBuffer& buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001564 this->reset();
weita@google.comf9ab99a2009-05-03 18:23:30 +00001565
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001566 SkImageInfo info;
1567 info.unflatten(buffer);
1568 size_t rowBytes = buffer.readInt();
commit-bot@chromium.org33fed142014-02-13 18:46:13 +00001569 if (!buffer.validate((info.width() >= 0) && (info.height() >= 0) &&
1570 SkColorTypeIsValid(info.fColorType) &&
1571 SkAlphaTypeIsValid(info.fAlphaType) &&
1572 validate_alphaType(info.fColorType, info.fAlphaType) &&
1573 info.validRowBytes(rowBytes))) {
1574 return;
1575 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001576
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001577 bool configIsValid = this->setConfig(info, rowBytes);
1578 buffer.validate(configIsValid);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001579
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001580 int reftype = buffer.readInt();
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +00001581 if (buffer.validate((SERIALIZE_PIXELTYPE_REF_DATA == reftype) ||
1582 (SERIALIZE_PIXELTYPE_NONE == reftype))) {
1583 switch (reftype) {
1584 case SERIALIZE_PIXELTYPE_REF_DATA: {
reed@google.com672588b2014-01-08 15:42:01 +00001585 SkIPoint origin;
1586 origin.fX = buffer.readInt();
1587 origin.fY = buffer.readInt();
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001588 size_t offset = origin.fY * rowBytes + origin.fX * info.bytesPerPixel();
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +00001589 SkPixelRef* pr = buffer.readPixelRef();
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +00001590 if (!buffer.validate((NULL == pr) ||
1591 (pr->getAllocatedSizeInBytes() >= (offset + this->getSafeSize())))) {
reed@google.com672588b2014-01-08 15:42:01 +00001592 origin.setZero();
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +00001593 }
reed@google.com672588b2014-01-08 15:42:01 +00001594 SkSafeUnref(this->setPixelRef(pr, origin));
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +00001595 break;
1596 }
1597 case SERIALIZE_PIXELTYPE_NONE:
1598 break;
1599 default:
1600 SkDEBUGFAIL("unrecognized pixeltype in serialized data");
1601 sk_throw();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001602 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001603 }
1604}
1605
1606///////////////////////////////////////////////////////////////////////////////
1607
1608SkBitmap::RLEPixels::RLEPixels(int width, int height) {
1609 fHeight = height;
commit-bot@chromium.org235002f2013-10-09 18:39:59 +00001610 fYPtrs = (uint8_t**)sk_calloc_throw(height * sizeof(uint8_t*));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001611}
1612
1613SkBitmap::RLEPixels::~RLEPixels() {
1614 sk_free(fYPtrs);
1615}
1616
1617///////////////////////////////////////////////////////////////////////////////
1618
1619#ifdef SK_DEBUG
1620void SkBitmap::validate() const {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001621 fInfo.validate();
1622 SkASSERT(fInfo.validRowBytes(fRowBytes));
scroggo@google.com8e990eb2013-06-14 15:55:56 +00001623 uint8_t allFlags = kImageIsOpaque_Flag | kImageIsVolatile_Flag | kImageIsImmutable_Flag;
1624#ifdef SK_BUILD_FOR_ANDROID
1625 allFlags |= kHasHardwareMipMap_Flag;
1626#endif
1627 SkASSERT(fFlags <= allFlags);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001628 SkASSERT(fPixelLockCount >= 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001629
reed@google.com615316c2014-01-15 19:15:23 +00001630 if (fPixels) {
1631 SkASSERT(fPixelRef);
1632 SkASSERT(fPixelLockCount > 0);
1633 SkASSERT(fPixelRef->isLocked());
1634 SkASSERT(fPixelRef->rowBytes() == fRowBytes);
1635 SkASSERT(fPixelRefOrigin.fX >= 0);
1636 SkASSERT(fPixelRefOrigin.fY >= 0);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001637 SkASSERT(fPixelRef->info().width() >= (int)this->width() + fPixelRefOrigin.fX);
1638 SkASSERT(fPixelRef->info().fHeight >= (int)this->height() + fPixelRefOrigin.fY);
1639 SkASSERT(fPixelRef->rowBytes() >= fInfo.minRowBytes());
reed@google.com615316c2014-01-15 19:15:23 +00001640 } else {
1641 SkASSERT(NULL == fColorTable);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001642 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001643}
1644#endif
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001645
1646#ifdef SK_DEVELOPER
1647void SkBitmap::toString(SkString* str) const {
1648
1649 static const char* gConfigNames[kConfigCount] = {
rmistry@google.comd6bab022013-12-02 13:50:38 +00001650 "NONE", "A8", "INDEX8", "565", "4444", "8888"
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001651 };
1652
1653 str->appendf("bitmap: ((%d, %d) %s", this->width(), this->height(),
1654 gConfigNames[this->config()]);
1655
1656 str->append(" (");
1657 if (this->isOpaque()) {
1658 str->append("opaque");
1659 } else {
1660 str->append("transparent");
1661 }
1662 if (this->isImmutable()) {
1663 str->append(", immutable");
1664 } else {
1665 str->append(", not-immutable");
1666 }
1667 str->append(")");
1668
1669 SkPixelRef* pr = this->pixelRef();
1670 if (NULL == pr) {
1671 // show null or the explicit pixel address (rare)
1672 str->appendf(" pixels:%p", this->getPixels());
1673 } else {
1674 const char* uri = pr->getURI();
1675 if (NULL != uri) {
1676 str->appendf(" uri:\"%s\"", uri);
1677 } else {
1678 str->appendf(" pixelref:%p", pr);
1679 }
1680 }
1681
1682 str->append(")");
1683}
1684#endif
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001685
1686///////////////////////////////////////////////////////////////////////////////
1687
1688#ifdef SK_DEBUG
1689void SkImageInfo::validate() const {
1690 SkASSERT(fWidth >= 0);
1691 SkASSERT(fHeight >= 0);
1692 SkASSERT(SkColorTypeIsValid(fColorType));
1693 SkASSERT(SkAlphaTypeIsValid(fAlphaType));
1694}
1695#endif