blob: 07f6f177deb16bc5df445381ae61d78e019fc404 [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 +000032SkBitmap::SkBitmap() {
reed@android.com4516f472009-06-29 16:25:36 +000033 sk_bzero(this, sizeof(*this));
reed@android.com8a1c16f2008-12-17 15:59:43 +000034}
35
36SkBitmap::SkBitmap(const SkBitmap& src) {
37 SkDEBUGCODE(src.validate();)
reed@android.com4516f472009-06-29 16:25:36 +000038 sk_bzero(this, sizeof(*this));
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 *this = src;
40 SkDEBUGCODE(this->validate();)
41}
42
43SkBitmap::~SkBitmap() {
44 SkDEBUGCODE(this->validate();)
45 this->freePixels();
46}
47
48SkBitmap& SkBitmap::operator=(const SkBitmap& src) {
49 if (this != &src) {
50 this->freePixels();
51 memcpy(this, &src, sizeof(src));
52
53 // inc src reference counts
reed@android.com83f7bc32009-07-17 02:42:41 +000054 SkSafeRef(src.fPixelRef);
reed@android.com8a1c16f2008-12-17 15:59:43 +000055
56 // we reset our locks if we get blown away
57 fPixelLockCount = 0;
weita@google.comf9ab99a2009-05-03 18:23:30 +000058
reed@google.com5f62ed72014-01-15 19:59:45 +000059 if (fPixelRef) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000060 // ignore the values from the memcpy
61 fPixels = NULL;
62 fColorTable = NULL;
bsalomon@google.com586f48c2011-04-14 15:07:22 +000063 // Note that what to for genID is somewhat arbitrary. We have no
64 // way to track changes to raw pixels across multiple SkBitmaps.
65 // Would benefit from an SkRawPixelRef type created by
66 // setPixels.
67 // Just leave the memcpy'ed one but they'll get out of sync
68 // as soon either is modified.
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 }
70 }
71
72 SkDEBUGCODE(this->validate();)
73 return *this;
74}
75
76void SkBitmap::swap(SkBitmap& other) {
bsalomon@google.com586f48c2011-04-14 15:07:22 +000077 SkTSwap(fColorTable, other.fColorTable);
78 SkTSwap(fPixelRef, other.fPixelRef);
reed@google.com672588b2014-01-08 15:42:01 +000079 SkTSwap(fPixelRefOrigin, other.fPixelRefOrigin);
bsalomon@google.com586f48c2011-04-14 15:07:22 +000080 SkTSwap(fPixelLockCount, other.fPixelLockCount);
bsalomon@google.com586f48c2011-04-14 15:07:22 +000081 SkTSwap(fPixels, other.fPixels);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +000082 SkTSwap(fInfo, other.fInfo);
bsalomon@google.com586f48c2011-04-14 15:07:22 +000083 SkTSwap(fRowBytes, other.fRowBytes);
bsalomon@google.com586f48c2011-04-14 15:07:22 +000084 SkTSwap(fFlags, other.fFlags);
reed@android.com8a1c16f2008-12-17 15:59:43 +000085
86 SkDEBUGCODE(this->validate();)
87}
88
89void SkBitmap::reset() {
90 this->freePixels();
reed@android.com4516f472009-06-29 16:25:36 +000091 sk_bzero(this, sizeof(*this));
reed@android.com8a1c16f2008-12-17 15:59:43 +000092}
93
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +000094SkBitmap::Config SkBitmap::config() const {
95 return SkColorTypeToBitmapConfig(fInfo.colorType());
96}
97
reed@android.com8a1c16f2008-12-17 15:59:43 +000098int SkBitmap::ComputeBytesPerPixel(SkBitmap::Config config) {
99 int bpp;
100 switch (config) {
101 case kNo_Config:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102 bpp = 0; // not applicable
103 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 case kA8_Config:
105 case kIndex8_Config:
106 bpp = 1;
107 break;
108 case kRGB_565_Config:
109 case kARGB_4444_Config:
110 bpp = 2;
111 break;
112 case kARGB_8888_Config:
113 bpp = 4;
114 break;
115 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000116 SkDEBUGFAIL("unknown config");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 bpp = 0; // error
118 break;
119 }
120 return bpp;
121}
122
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000123size_t SkBitmap::ComputeRowBytes(Config c, int width) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000124 return SkColorTypeMinRowBytes(SkBitmapConfigToColorType(c), width);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125}
126
reed@google.com57212f92013-12-30 14:40:38 +0000127int64_t SkBitmap::ComputeSize64(Config config, int width, int height) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000128 SkColorType ct = SkBitmapConfigToColorType(config);
129 int64_t rowBytes = sk_64_mul(SkColorTypeBytesPerPixel(ct), width);
reed@google.com57212f92013-12-30 14:40:38 +0000130 return rowBytes * height;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131}
132
133size_t SkBitmap::ComputeSize(Config c, int width, int height) {
reed@google.com57212f92013-12-30 14:40:38 +0000134 int64_t size = SkBitmap::ComputeSize64(c, width, height);
135 return sk_64_isS32(size) ? sk_64_asS32(size) : 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136}
137
reed@google.com57212f92013-12-30 14:40:38 +0000138int64_t SkBitmap::ComputeSafeSize64(Config config,
139 uint32_t width,
140 uint32_t height,
141 size_t rowBytes) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000142 SkImageInfo info = SkImageInfo::Make(width, height,
143 SkBitmapConfigToColorType(config),
144 kPremul_SkAlphaType);
145 return info.getSafeSize64(rowBytes);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000146}
147
148size_t SkBitmap::ComputeSafeSize(Config config,
149 uint32_t width,
150 uint32_t height,
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000151 size_t rowBytes) {
reed@google.com57212f92013-12-30 14:40:38 +0000152 int64_t safeSize = ComputeSafeSize64(config, width, height, rowBytes);
153 int32_t safeSize32 = (int32_t)safeSize;
skia.committer@gmail.comf5e1f632013-12-31 07:01:36 +0000154
reed@google.com57212f92013-12-30 14:40:38 +0000155 if (safeSize32 != safeSize) {
156 safeSize32 = 0;
157 }
158 return safeSize32;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000159}
160
reed@google.com86b2e432012-03-15 21:17:03 +0000161void SkBitmap::getBounds(SkRect* bounds) const {
162 SkASSERT(bounds);
163 bounds->set(0, 0,
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000164 SkIntToScalar(fInfo.fWidth), SkIntToScalar(fInfo.fHeight));
reed@google.com86b2e432012-03-15 21:17:03 +0000165}
166
reed@google.com80e14592012-03-16 14:58:07 +0000167void SkBitmap::getBounds(SkIRect* bounds) const {
168 SkASSERT(bounds);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000169 bounds->set(0, 0, fInfo.fWidth, fInfo.fHeight);
reed@google.com80e14592012-03-16 14:58:07 +0000170}
171
reed@google.com86b2e432012-03-15 21:17:03 +0000172///////////////////////////////////////////////////////////////////////////////
173
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000174static bool validate_alphaType(SkColorType colorType, SkAlphaType alphaType,
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000175 SkAlphaType* canonical = NULL) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000176 switch (colorType) {
177 case kUnknown_SkColorType:
reed@google.com383a6972013-10-21 14:00:07 +0000178 alphaType = kIgnore_SkAlphaType;
179 break;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000180 case kAlpha_8_SkColorType:
reed@google.com383a6972013-10-21 14:00:07 +0000181 if (kUnpremul_SkAlphaType == alphaType) {
182 alphaType = kPremul_SkAlphaType;
183 }
184 // fall-through
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000185 case kIndex_8_SkColorType:
186 case kARGB_4444_SkColorType:
187 case kRGBA_8888_SkColorType:
188 case kBGRA_8888_SkColorType:
reed@google.com383a6972013-10-21 14:00:07 +0000189 if (kIgnore_SkAlphaType == alphaType) {
190 return false;
191 }
192 break;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000193 case kRGB_565_SkColorType:
reed@google.com383a6972013-10-21 14:00:07 +0000194 alphaType = kOpaque_SkAlphaType;
195 break;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000196 default:
197 return false;
reed@android.com149e2f62009-05-22 14:39:03 +0000198 }
reed@google.com383a6972013-10-21 14:00:07 +0000199 if (canonical) {
200 *canonical = alphaType;
201 }
202 return true;
203}
reed@android.com149e2f62009-05-22 14:39:03 +0000204
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000205bool SkBitmap::setInfo(const SkImageInfo& origInfo, size_t rowBytes) {
commit-bot@chromium.orgd5414e52014-02-13 22:30:38 +0000206 SkImageInfo info = origInfo;
207
208 if (!validate_alphaType(info.fColorType, info.fAlphaType,
209 &info.fAlphaType)) {
210 return reset_return_false(this);
211 }
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000212
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000213 // require that rowBytes fit in 31bits
214 int64_t mrb = info.minRowBytes64();
215 if ((int32_t)mrb != mrb) {
216 return reset_return_false(this);
reed@google.com383a6972013-10-21 14:00:07 +0000217 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000218 if ((int64_t)rowBytes != (int32_t)rowBytes) {
219 return reset_return_false(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000220 }
reed@android.com89bb83a2009-05-29 21:30:42 +0000221
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000222 if (info.width() < 0 || info.height() < 0) {
223 return reset_return_false(this);
224 }
225
226 if (kUnknown_SkColorType == info.colorType()) {
227 rowBytes = 0;
228 } else if (0 == rowBytes) {
229 rowBytes = (size_t)mrb;
230 } else if (rowBytes < info.minRowBytes()) {
231 return reset_return_false(this);
reed@google.com383a6972013-10-21 14:00:07 +0000232 }
233
234 this->freePixels();
235
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000236 fInfo = info;
237 fRowBytes = SkToU32(rowBytes);
reed@google.com383a6972013-10-21 14:00:07 +0000238 return true;
reed@google.com383a6972013-10-21 14:00:07 +0000239}
240
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000241bool SkBitmap::setConfig(Config config, int width, int height, size_t rowBytes,
242 SkAlphaType alphaType) {
243 SkColorType ct = SkBitmapConfigToColorType(config);
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000244 return this->setInfo(SkImageInfo::Make(width, height, ct, alphaType), rowBytes);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000245}
246
reed@google.com383a6972013-10-21 14:00:07 +0000247bool SkBitmap::setAlphaType(SkAlphaType alphaType) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000248 if (!validate_alphaType(fInfo.fColorType, alphaType, &alphaType)) {
reed@google.com383a6972013-10-21 14:00:07 +0000249 return false;
250 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000251 if (fInfo.fAlphaType != alphaType) {
252 fInfo.fAlphaType = alphaType;
commit-bot@chromium.org0e8d0d62014-01-27 15:41:07 +0000253 if (fPixelRef) {
reed@google.comc1587f92014-01-28 16:05:39 +0000254 fPixelRef->changeAlphaType(alphaType);
commit-bot@chromium.org0e8d0d62014-01-27 15:41:07 +0000255 }
256 }
reed@google.com383a6972013-10-21 14:00:07 +0000257 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000258}
259
260void SkBitmap::updatePixelsFromRef() const {
261 if (NULL != fPixelRef) {
262 if (fPixelLockCount > 0) {
reed@google.comff0da4f2012-05-17 13:14:52 +0000263 SkASSERT(fPixelRef->isLocked());
weita@google.comf9ab99a2009-05-03 18:23:30 +0000264
reed@android.com8a1c16f2008-12-17 15:59:43 +0000265 void* p = fPixelRef->pixels();
266 if (NULL != p) {
reed@google.com672588b2014-01-08 15:42:01 +0000267 p = (char*)p
reed@google.com303c4752014-01-09 20:00:14 +0000268 + fPixelRefOrigin.fY * fRowBytes
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000269 + fPixelRefOrigin.fX * fInfo.bytesPerPixel();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000270 }
271 fPixels = p;
reed@google.com5f62ed72014-01-15 19:59:45 +0000272 fColorTable = fPixelRef->colorTable();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000273 } else {
274 SkASSERT(0 == fPixelLockCount);
275 fPixels = NULL;
reed@google.com5f62ed72014-01-15 19:59:45 +0000276 fColorTable = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000277 }
278 }
279}
280
reed@google.com9230ea22013-12-09 22:01:03 +0000281static bool config_to_colorType(SkBitmap::Config config, SkColorType* ctOut) {
282 SkColorType ct;
283 switch (config) {
284 case SkBitmap::kA8_Config:
285 ct = kAlpha_8_SkColorType;
286 break;
287 case SkBitmap::kIndex8_Config:
288 ct = kIndex_8_SkColorType;
289 break;
290 case SkBitmap::kRGB_565_Config:
291 ct = kRGB_565_SkColorType;
292 break;
293 case SkBitmap::kARGB_4444_Config:
294 ct = kARGB_4444_SkColorType;
295 break;
296 case SkBitmap::kARGB_8888_Config:
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000297 ct = kN32_SkColorType;
reed@google.com9230ea22013-12-09 22:01:03 +0000298 break;
299 case SkBitmap::kNo_Config:
300 default:
301 return false;
302 }
303 if (ctOut) {
304 *ctOut = ct;
305 }
306 return true;
307}
308
reed@google.com672588b2014-01-08 15:42:01 +0000309SkPixelRef* SkBitmap::setPixelRef(SkPixelRef* pr, int dx, int dy) {
reed@google.comdcea5302014-01-03 13:43:01 +0000310#ifdef SK_DEBUG
reed@google.com672588b2014-01-08 15:42:01 +0000311 if (pr) {
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000312 if (kUnknown_SkColorType != fInfo.colorType()) {
reed@google.comdcea5302014-01-03 13:43:01 +0000313 const SkImageInfo& prInfo = pr->info();
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000314 SkASSERT(fInfo.fWidth <= prInfo.fWidth);
315 SkASSERT(fInfo.fHeight <= prInfo.fHeight);
316 SkASSERT(fInfo.fColorType == prInfo.fColorType);
reed@google.comdcea5302014-01-03 13:43:01 +0000317 switch (prInfo.fAlphaType) {
318 case kIgnore_SkAlphaType:
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000319 SkASSERT(fInfo.fAlphaType == kIgnore_SkAlphaType);
reed@google.comdcea5302014-01-03 13:43:01 +0000320 break;
321 case kOpaque_SkAlphaType:
322 case kPremul_SkAlphaType:
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000323 SkASSERT(fInfo.fAlphaType == kOpaque_SkAlphaType ||
324 fInfo.fAlphaType == kPremul_SkAlphaType);
reed@google.comdcea5302014-01-03 13:43:01 +0000325 break;
326 case kUnpremul_SkAlphaType:
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000327 SkASSERT(fInfo.fAlphaType == kOpaque_SkAlphaType ||
328 fInfo.fAlphaType == kUnpremul_SkAlphaType);
reed@google.comdcea5302014-01-03 13:43:01 +0000329 break;
330 }
331 }
332 }
333#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000334
reed@google.com672588b2014-01-08 15:42:01 +0000335 if (pr) {
336 const SkImageInfo& info = pr->info();
337 fPixelRefOrigin.set(SkPin32(dx, 0, info.fWidth),
338 SkPin32(dy, 0, info.fHeight));
339 } else {
340 // ignore dx,dy if there is no pixelref
341 fPixelRefOrigin.setZero();
342 }
343
344 if (fPixelRef != pr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000345 if (fPixelRef != pr) {
346 this->freePixels();
347 SkASSERT(NULL == fPixelRef);
weita@google.comf9ab99a2009-05-03 18:23:30 +0000348
reed@google.com82065d62011-02-07 15:30:46 +0000349 SkSafeRef(pr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000350 fPixelRef = pr;
351 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000352 this->updatePixelsFromRef();
353 }
354
355 SkDEBUGCODE(this->validate();)
356 return pr;
357}
358
359void SkBitmap::lockPixels() const {
djsollen@google.com7c6d2642013-08-06 12:19:38 +0000360 if (NULL != fPixelRef && 0 == sk_atomic_inc(&fPixelLockCount)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000361 fPixelRef->lockPixels();
362 this->updatePixelsFromRef();
363 }
364 SkDEBUGCODE(this->validate();)
365}
366
367void SkBitmap::unlockPixels() const {
368 SkASSERT(NULL == fPixelRef || fPixelLockCount > 0);
369
djsollen@google.com7c6d2642013-08-06 12:19:38 +0000370 if (NULL != fPixelRef && 1 == sk_atomic_dec(&fPixelLockCount)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000371 fPixelRef->unlockPixels();
372 this->updatePixelsFromRef();
373 }
374 SkDEBUGCODE(this->validate();)
375}
376
reed@google.com9c49bc32011-07-07 13:42:37 +0000377bool SkBitmap::lockPixelsAreWritable() const {
djsollen@google.comc84b8332012-07-27 13:41:44 +0000378 return (fPixelRef) ? fPixelRef->lockPixelsAreWritable() : false;
reed@google.com9c49bc32011-07-07 13:42:37 +0000379}
380
reed@android.com8a1c16f2008-12-17 15:59:43 +0000381void SkBitmap::setPixels(void* p, SkColorTable* ctable) {
reed@google.com8e1034e2012-07-30 13:16:35 +0000382 if (NULL == p) {
reed@google.com672588b2014-01-08 15:42:01 +0000383 this->setPixelRef(NULL);
reed@google.com8e1034e2012-07-30 13:16:35 +0000384 return;
385 }
386
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000387 if (kUnknown_SkColorType == fInfo.colorType()) {
reed@google.com672588b2014-01-08 15:42:01 +0000388 this->setPixelRef(NULL);
reed@google.combf790232013-12-13 19:45:58 +0000389 return;
390 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000391
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000392 SkPixelRef* pr = SkMallocPixelRef::NewDirect(fInfo, p, fRowBytes, ctable);
reed@google.combf790232013-12-13 19:45:58 +0000393 if (NULL == pr) {
reed@google.com672588b2014-01-08 15:42:01 +0000394 this->setPixelRef(NULL);
reed@google.combf790232013-12-13 19:45:58 +0000395 return;
396 }
397
398 this->setPixelRef(pr)->unref();
399
djsollen@google.comc84b8332012-07-27 13:41:44 +0000400 // since we're already allocated, we lockPixels right away
401 this->lockPixels();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000402 SkDEBUGCODE(this->validate();)
403}
404
405bool SkBitmap::allocPixels(Allocator* allocator, SkColorTable* ctable) {
406 HeapAllocator stdalloc;
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000407
reed@android.com8a1c16f2008-12-17 15:59:43 +0000408 if (NULL == allocator) {
409 allocator = &stdalloc;
410 }
411 return allocator->allocPixelRef(this, ctable);
412}
413
reed@google.com9ebcac52014-01-24 18:53:42 +0000414///////////////////////////////////////////////////////////////////////////////
415
reed@google.com9ebcac52014-01-24 18:53:42 +0000416bool SkBitmap::allocPixels(const SkImageInfo& info, SkPixelRefFactory* factory,
417 SkColorTable* ctable) {
418 if (kIndex_8_SkColorType == info.fColorType && NULL == ctable) {
419 return reset_return_false(this);
420 }
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000421 if (!this->setInfo(info)) {
reed@google.com9ebcac52014-01-24 18:53:42 +0000422 return reset_return_false(this);
423 }
424
425 SkMallocPixelRef::PRFactory defaultFactory;
426 if (NULL == factory) {
427 factory = &defaultFactory;
428 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000429
reed@google.com9ebcac52014-01-24 18:53:42 +0000430 SkPixelRef* pr = factory->create(info, ctable);
431 if (NULL == pr) {
432 return reset_return_false(this);
433 }
434 this->setPixelRef(pr)->unref();
435
436 // TODO: lockPixels could/should return bool or void*/NULL
437 this->lockPixels();
438 if (NULL == this->getPixels()) {
439 return reset_return_false(this);
440 }
441 return true;
442}
443
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000444bool SkBitmap::installPixels(const SkImageInfo& info, void* pixels, size_t rb, SkColorTable* ct,
445 void (*releaseProc)(void* addr, void* context), void* context) {
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000446 if (!this->setInfo(info, rb)) {
reed@google.com9ebcac52014-01-24 18:53:42 +0000447 this->reset();
448 return false;
449 }
450
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000451 SkPixelRef* pr = SkMallocPixelRef::NewWithProc(info, rb, ct, pixels, releaseProc, context);
reed@google.com9ebcac52014-01-24 18:53:42 +0000452 if (!pr) {
453 this->reset();
454 return false;
455 }
456
457 this->setPixelRef(pr)->unref();
mike@reedtribe.org6e58cf32014-02-16 20:54:21 +0000458
459 // since we're already allocated, we lockPixels right away
460 this->lockPixels();
mike@reedtribe.org6e58cf32014-02-16 20:54:21 +0000461 SkDEBUGCODE(this->validate();)
reed@google.com9ebcac52014-01-24 18:53:42 +0000462 return true;
463}
464
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000465bool SkBitmap::installMaskPixels(const SkMask& mask) {
466 if (SkMask::kA8_Format != mask.fFormat) {
467 this->reset();
468 return false;
469 }
470 return this->installPixels(SkImageInfo::MakeA8(mask.fBounds.width(),
471 mask.fBounds.height()),
472 mask.fImage, mask.fRowBytes);
473}
474
reed@google.comeb9a46c2014-01-25 16:46:20 +0000475bool SkBitmap::allocConfigPixels(Config config, int width, int height,
476 bool isOpaque) {
477 SkColorType ct;
478 if (!config_to_colorType(config, &ct)) {
479 return false;
480 }
skia.committer@gmail.com33c749a2014-01-26 07:01:45 +0000481
reed@google.comeb9a46c2014-01-25 16:46:20 +0000482 SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
reed@google.comeb9a46c2014-01-25 16:46:20 +0000483 return this->allocPixels(SkImageInfo::Make(width, height, ct, at));
484}
485
486///////////////////////////////////////////////////////////////////////////////
487
reed@android.com8a1c16f2008-12-17 15:59:43 +0000488void SkBitmap::freePixels() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000489 if (NULL != fPixelRef) {
490 if (fPixelLockCount > 0) {
491 fPixelRef->unlockPixels();
492 }
493 fPixelRef->unref();
494 fPixelRef = NULL;
reed@google.com672588b2014-01-08 15:42:01 +0000495 fPixelRefOrigin.setZero();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000496 }
497 fPixelLockCount = 0;
498 fPixels = NULL;
reed@google.com5f62ed72014-01-15 19:59:45 +0000499 fColorTable = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000500}
501
reed@android.com8a1c16f2008-12-17 15:59:43 +0000502uint32_t SkBitmap::getGenerationID() const {
djsollen@google.comc84b8332012-07-27 13:41:44 +0000503 return (fPixelRef) ? fPixelRef->getGenerationID() : 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000504}
505
506void SkBitmap::notifyPixelsChanged() const {
junov@chromium.orgb0521292011-12-15 20:14:06 +0000507 SkASSERT(!this->isImmutable());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000508 if (fPixelRef) {
509 fPixelRef->notifyPixelsChanged();
510 }
511}
512
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000513GrTexture* SkBitmap::getTexture() const {
reed@android.comce4e53a2010-09-09 16:01:26 +0000514 return fPixelRef ? fPixelRef->getTexture() : NULL;
515}
516
reed@android.com8a1c16f2008-12-17 15:59:43 +0000517///////////////////////////////////////////////////////////////////////////////
518
reed@android.com8a1c16f2008-12-17 15:59:43 +0000519/** We explicitly use the same allocator for our pixels that SkMask does,
520 so that we can freely assign memory allocated by one class to the other.
521 */
522bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst,
523 SkColorTable* ctable) {
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000524 const SkImageInfo info = dst->info();
525 if (kUnknown_SkColorType == info.colorType()) {
reed@google.combf790232013-12-13 19:45:58 +0000526// SkDebugf("unsupported config for info %d\n", dst->config());
527 return false;
528 }
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000529
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000530 SkPixelRef* pr = SkMallocPixelRef::NewAllocate(info, dst->rowBytes(), ctable);
reed@google.combf790232013-12-13 19:45:58 +0000531 if (NULL == pr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000532 return false;
533 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000534
reed@google.com672588b2014-01-08 15:42:01 +0000535 dst->setPixelRef(pr)->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000536 // since we're already allocated, we lockPixels right away
537 dst->lockPixels();
538 return true;
539}
540
541///////////////////////////////////////////////////////////////////////////////
542
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000543bool SkBitmap::copyPixelsTo(void* const dst, size_t dstSize,
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000544 size_t dstRowBytes, bool preserveDstPad) const {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000545
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000546 if (0 == dstRowBytes) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000547 dstRowBytes = fRowBytes;
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000548 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000549
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000550 if (dstRowBytes < fInfo.minRowBytes() ||
551 dst == NULL || (getPixels() == NULL && pixelRef() == NULL)) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000552 return false;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000553 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000554
bsalomon@google.comc6980972011-11-02 19:57:21 +0000555 if (!preserveDstPad && static_cast<uint32_t>(dstRowBytes) == fRowBytes) {
reed@google.com44699382013-10-31 17:28:30 +0000556 size_t safeSize = this->getSafeSize();
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000557 if (safeSize > dstSize || safeSize == 0)
558 return false;
559 else {
560 SkAutoLockPixels lock(*this);
561 // This implementation will write bytes beyond the end of each row,
562 // excluding the last row, if the bitmap's stride is greater than
563 // strictly required by the current config.
564 memcpy(dst, getPixels(), safeSize);
565
566 return true;
567 }
568 } else {
569 // If destination has different stride than us, then copy line by line.
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000570 if (fInfo.getSafeSize(dstRowBytes) > dstSize) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000571 return false;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000572 } else {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000573 // Just copy what we need on each line.
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000574 size_t rowBytes = fInfo.minRowBytes();
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000575 SkAutoLockPixels lock(*this);
576 const uint8_t* srcP = reinterpret_cast<const uint8_t*>(getPixels());
577 uint8_t* dstP = reinterpret_cast<uint8_t*>(dst);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000578 for (int row = 0; row < fInfo.fHeight;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000579 row++, srcP += fRowBytes, dstP += dstRowBytes) {
580 memcpy(dstP, srcP, rowBytes);
581 }
582
583 return true;
584 }
585 }
586}
587
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000588///////////////////////////////////////////////////////////////////////////////
589
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000590bool SkBitmap::isImmutable() const {
junov@chromium.orgb0521292011-12-15 20:14:06 +0000591 return fPixelRef ? fPixelRef->isImmutable() :
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000592 fFlags & kImageIsImmutable_Flag;
junov@chromium.orgb0521292011-12-15 20:14:06 +0000593}
594
595void SkBitmap::setImmutable() {
596 if (fPixelRef) {
597 fPixelRef->setImmutable();
598 } else {
599 fFlags |= kImageIsImmutable_Flag;
600 }
601}
602
junov@google.com4ee7ae52011-06-30 17:30:49 +0000603bool SkBitmap::isVolatile() const {
604 return (fFlags & kImageIsVolatile_Flag) != 0;
605}
606
607void SkBitmap::setIsVolatile(bool isVolatile) {
608 if (isVolatile) {
609 fFlags |= kImageIsVolatile_Flag;
610 } else {
611 fFlags &= ~kImageIsVolatile_Flag;
612 }
613}
614
reed@android.com8a1c16f2008-12-17 15:59:43 +0000615void* SkBitmap::getAddr(int x, int y) const {
616 SkASSERT((unsigned)x < (unsigned)this->width());
617 SkASSERT((unsigned)y < (unsigned)this->height());
618
619 char* base = (char*)this->getPixels();
620 if (base) {
621 base += y * this->rowBytes();
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000622 switch (this->colorType()) {
623 case kRGBA_8888_SkColorType:
624 case kBGRA_8888_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000625 base += x << 2;
626 break;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000627 case kARGB_4444_SkColorType:
628 case kRGB_565_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000629 base += x << 1;
630 break;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000631 case kAlpha_8_SkColorType:
632 case kIndex_8_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000633 base += x;
634 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000635 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000636 SkDEBUGFAIL("Can't return addr for config");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000637 base = NULL;
638 break;
639 }
640 }
641 return base;
642}
643
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000644SkColor SkBitmap::getColor(int x, int y) const {
645 SkASSERT((unsigned)x < (unsigned)this->width());
646 SkASSERT((unsigned)y < (unsigned)this->height());
647
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000648 switch (this->colorType()) {
649 case kAlpha_8_SkColorType: {
reed@google.com3b521d02011-04-29 11:53:41 +0000650 uint8_t* addr = this->getAddr8(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000651 return SkColorSetA(0, addr[0]);
652 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000653 case kIndex_8_SkColorType: {
reed@google.com3b521d02011-04-29 11:53:41 +0000654 SkPMColor c = this->getIndex8Color(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000655 return SkUnPreMultiply::PMColorToColor(c);
656 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000657 case kRGB_565_SkColorType: {
reed@google.com3b521d02011-04-29 11:53:41 +0000658 uint16_t* addr = this->getAddr16(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000659 return SkPixel16ToColor(addr[0]);
660 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000661 case kARGB_4444_SkColorType: {
reed@google.com3b521d02011-04-29 11:53:41 +0000662 uint16_t* addr = this->getAddr16(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000663 SkPMColor c = SkPixel4444ToPixel32(addr[0]);
664 return SkUnPreMultiply::PMColorToColor(c);
665 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000666 case kBGRA_8888_SkColorType:
667 case kRGBA_8888_SkColorType: {
reed@google.com3b521d02011-04-29 11:53:41 +0000668 uint32_t* addr = this->getAddr32(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000669 return SkUnPreMultiply::PMColorToColor(addr[0]);
670 }
rmistry@google.comd6bab022013-12-02 13:50:38 +0000671 default:
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000672 SkASSERT(false);
673 return 0;
674 }
675 SkASSERT(false); // Not reached.
676 return 0;
677}
678
reed@google.com2a7579d2012-11-07 18:30:18 +0000679bool SkBitmap::ComputeIsOpaque(const SkBitmap& bm) {
680 SkAutoLockPixels alp(bm);
681 if (!bm.getPixels()) {
682 return false;
683 }
684
685 const int height = bm.height();
686 const int width = bm.width();
687
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000688 switch (bm.colorType()) {
689 case kAlpha_8_SkColorType: {
reed@google.com2a7579d2012-11-07 18:30:18 +0000690 unsigned a = 0xFF;
691 for (int y = 0; y < height; ++y) {
692 const uint8_t* row = bm.getAddr8(0, y);
693 for (int x = 0; x < width; ++x) {
694 a &= row[x];
695 }
696 if (0xFF != a) {
697 return false;
698 }
699 }
700 return true;
701 } break;
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000702 case kIndex_8_SkColorType: {
reed@google.com2a7579d2012-11-07 18:30:18 +0000703 SkAutoLockColors alc(bm);
704 const SkPMColor* table = alc.colors();
705 if (!table) {
706 return false;
707 }
reed@google.com140d7282013-01-07 20:25:04 +0000708 SkPMColor c = (SkPMColor)~0;
reed@google.com2a7579d2012-11-07 18:30:18 +0000709 for (int i = bm.getColorTable()->count() - 1; i >= 0; --i) {
710 c &= table[i];
711 }
712 return 0xFF == SkGetPackedA32(c);
713 } break;
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000714 case kRGB_565_SkColorType:
reed@google.com2a7579d2012-11-07 18:30:18 +0000715 return true;
716 break;
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000717 case kARGB_4444_SkColorType: {
reed@google.com2a7579d2012-11-07 18:30:18 +0000718 unsigned c = 0xFFFF;
719 for (int y = 0; y < height; ++y) {
720 const SkPMColor16* row = bm.getAddr16(0, y);
721 for (int x = 0; x < width; ++x) {
722 c &= row[x];
723 }
724 if (0xF != SkGetPackedA4444(c)) {
725 return false;
726 }
727 }
728 return true;
729 } break;
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000730 case kBGRA_8888_SkColorType:
731 case kRGBA_8888_SkColorType: {
reed@google.com140d7282013-01-07 20:25:04 +0000732 SkPMColor c = (SkPMColor)~0;
reed@google.com2a7579d2012-11-07 18:30:18 +0000733 for (int y = 0; y < height; ++y) {
734 const SkPMColor* row = bm.getAddr32(0, y);
735 for (int x = 0; x < width; ++x) {
736 c &= row[x];
737 }
738 if (0xFF != SkGetPackedA32(c)) {
739 return false;
740 }
741 }
742 return true;
743 }
744 default:
745 break;
746 }
747 return false;
748}
749
750
reed@android.com8a1c16f2008-12-17 15:59:43 +0000751///////////////////////////////////////////////////////////////////////////////
752///////////////////////////////////////////////////////////////////////////////
753
reed@google.com45f746f2013-06-21 19:51:31 +0000754static uint16_t pack_8888_to_4444(unsigned a, unsigned r, unsigned g, unsigned b) {
755 unsigned pixel = (SkA32To4444(a) << SK_A4444_SHIFT) |
756 (SkR32To4444(r) << SK_R4444_SHIFT) |
757 (SkG32To4444(g) << SK_G4444_SHIFT) |
758 (SkB32To4444(b) << SK_B4444_SHIFT);
759 return SkToU16(pixel);
760}
761
reed@google.com60d32352013-06-28 19:40:50 +0000762void SkBitmap::internalErase(const SkIRect& area,
763 U8CPU a, U8CPU r, U8CPU g, U8CPU b) const {
764#ifdef SK_DEBUG
reed@android.com8a1c16f2008-12-17 15:59:43 +0000765 SkDEBUGCODE(this->validate();)
reed@google.com60d32352013-06-28 19:40:50 +0000766 SkASSERT(!area.isEmpty());
767 {
reed@google.com92833f92013-06-28 19:47:43 +0000768 SkIRect total = { 0, 0, this->width(), this->height() };
reed@google.com60d32352013-06-28 19:40:50 +0000769 SkASSERT(total.contains(area));
770 }
771#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000772
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000773 switch (fInfo.colorType()) {
774 case kUnknown_SkColorType:
775 case kIndex_8_SkColorType:
commit-bot@chromium.org7669a772014-03-27 15:30:35 +0000776 return; // can't erase. Should we bzero so the memory is not uninitialized?
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000777 default:
778 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000779 }
780
781 SkAutoLockPixels alp(*this);
782 // perform this check after the lock call
783 if (!this->readyToDraw()) {
784 return;
785 }
786
reed@google.com60d32352013-06-28 19:40:50 +0000787 int height = area.height();
788 const int width = area.width();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000789 const int rowBytes = fRowBytes;
790
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000791 switch (this->colorType()) {
792 case kAlpha_8_SkColorType: {
reed@google.com60d32352013-06-28 19:40:50 +0000793 uint8_t* p = this->getAddr8(area.fLeft, area.fTop);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000794 while (--height >= 0) {
795 memset(p, a, width);
796 p += rowBytes;
797 }
798 break;
799 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000800 case kARGB_4444_SkColorType:
801 case kRGB_565_SkColorType: {
reed@google.com60d32352013-06-28 19:40:50 +0000802 uint16_t* p = this->getAddr16(area.fLeft, area.fTop);;
reed@google.com45f746f2013-06-21 19:51:31 +0000803 uint16_t v;
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000804
commit-bot@chromium.org7669a772014-03-27 15:30:35 +0000805 // make rgb premultiplied
806 if (255 != a) {
807 r = SkAlphaMul(r, a);
808 g = SkAlphaMul(g, a);
809 b = SkAlphaMul(b, a);
810 }
811
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000812 if (kARGB_4444_SkColorType == this->colorType()) {
reed@google.com45f746f2013-06-21 19:51:31 +0000813 v = pack_8888_to_4444(a, r, g, b);
814 } else {
815 v = SkPackRGB16(r >> (8 - SK_R16_BITS),
816 g >> (8 - SK_G16_BITS),
817 b >> (8 - SK_B16_BITS));
818 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000819 while (--height >= 0) {
820 sk_memset16(p, v, width);
821 p = (uint16_t*)((char*)p + rowBytes);
822 }
823 break;
824 }
commit-bot@chromium.org7669a772014-03-27 15:30:35 +0000825 case kBGRA_8888_SkColorType:
826 case kRGBA_8888_SkColorType: {
reed@google.com60d32352013-06-28 19:40:50 +0000827 uint32_t* p = this->getAddr32(area.fLeft, area.fTop);
commit-bot@chromium.org7669a772014-03-27 15:30:35 +0000828
829 if (255 != a && kPremul_SkAlphaType == this->alphaType()) {
830 r = SkAlphaMul(r, a);
831 g = SkAlphaMul(g, a);
832 b = SkAlphaMul(b, a);
833 }
834 uint32_t v = kRGBA_8888_SkColorType == this->colorType() ?
835 SkPackARGB_as_RGBA(a, r, g, b) : SkPackARGB_as_BGRA(a, r, g, b);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000836
837 while (--height >= 0) {
838 sk_memset32(p, v, width);
839 p = (uint32_t*)((char*)p + rowBytes);
840 }
841 break;
842 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000843 default:
844 return; // no change, so don't call notifyPixelsChanged()
reed@android.com8a1c16f2008-12-17 15:59:43 +0000845 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000846
reed@android.com8a1c16f2008-12-17 15:59:43 +0000847 this->notifyPixelsChanged();
848}
849
reed@google.com60d32352013-06-28 19:40:50 +0000850void SkBitmap::eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const {
reed@google.com92833f92013-06-28 19:47:43 +0000851 SkIRect area = { 0, 0, this->width(), this->height() };
reed@google.com60d32352013-06-28 19:40:50 +0000852 if (!area.isEmpty()) {
853 this->internalErase(area, a, r, g, b);
854 }
855}
856
857void SkBitmap::eraseArea(const SkIRect& rect, SkColor c) const {
reed@google.com92833f92013-06-28 19:47:43 +0000858 SkIRect area = { 0, 0, this->width(), this->height() };
reed@google.com60d32352013-06-28 19:40:50 +0000859 if (area.intersect(rect)) {
860 this->internalErase(area, SkColorGetA(c), SkColorGetR(c),
861 SkColorGetG(c), SkColorGetB(c));
862 }
863}
864
reed@android.com8a1c16f2008-12-17 15:59:43 +0000865//////////////////////////////////////////////////////////////////////////////////////
866//////////////////////////////////////////////////////////////////////////////////////
867
reed@android.com8a1c16f2008-12-17 15:59:43 +0000868bool SkBitmap::extractSubset(SkBitmap* result, const SkIRect& subset) const {
869 SkDEBUGCODE(this->validate();)
870
djsollen@google.comc84b8332012-07-27 13:41:44 +0000871 if (NULL == result || NULL == fPixelRef) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000872 return false; // no src pixels
873 }
874
875 SkIRect srcRect, r;
876 srcRect.set(0, 0, this->width(), this->height());
877 if (!r.intersect(srcRect, subset)) {
878 return false; // r is empty (i.e. no intersection)
879 }
880
scroggo@google.coma2a31922012-12-07 19:14:45 +0000881 if (fPixelRef->getTexture() != NULL) {
882 // Do a deep copy
commit-bot@chromium.orgeeef0cc2014-05-21 15:58:00 +0000883 SkPixelRef* pixelRef = fPixelRef->deepCopy(this->config(), &subset);
scroggo@google.coma2a31922012-12-07 19:14:45 +0000884 if (pixelRef != NULL) {
885 SkBitmap dst;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000886 dst.setInfo(SkImageInfo::Make(subset.width(), subset.height(),
887 this->colorType(), this->alphaType()));
scroggo@google.coma2a31922012-12-07 19:14:45 +0000888 dst.setIsVolatile(this->isVolatile());
scroggo@google.coma2a31922012-12-07 19:14:45 +0000889 dst.setPixelRef(pixelRef)->unref();
890 SkDEBUGCODE(dst.validate());
891 result->swap(dst);
892 return true;
893 }
894 }
895
scroggo@google.coma2a31922012-12-07 19:14:45 +0000896 // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have
897 // exited above.
898 SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width()));
899 SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height()));
900
reed@android.com8a1c16f2008-12-17 15:59:43 +0000901 SkBitmap dst;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000902 dst.setInfo(SkImageInfo::Make(r.width(), r.height(), this->colorType(), this->alphaType()),
903 this->rowBytes());
skyostil@google.com0eb75762012-01-16 10:45:53 +0000904 dst.setIsVolatile(this->isVolatile());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000905
906 if (fPixelRef) {
reed@google.com672588b2014-01-08 15:42:01 +0000907 SkIPoint origin = fPixelRefOrigin;
908 origin.fX += r.fLeft;
909 origin.fY += r.fTop;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000910 // share the pixelref with a custom offset
reed@google.com672588b2014-01-08 15:42:01 +0000911 dst.setPixelRef(fPixelRef, origin);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000912 }
913 SkDEBUGCODE(dst.validate();)
914
915 // we know we're good, so commit to result
916 result->swap(dst);
917 return true;
918}
919
920///////////////////////////////////////////////////////////////////////////////
921
922#include "SkCanvas.h"
923#include "SkPaint.h"
924
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000925bool SkBitmap::canCopyTo(SkColorType dstColorType) const {
926 if (this->colorType() == kUnknown_SkColorType) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000927 return false;
928 }
929
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000930 bool sameConfigs = (this->colorType() == dstColorType);
931 switch (dstColorType) {
932 case kAlpha_8_SkColorType:
933 case kRGB_565_SkColorType:
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000934 case kRGBA_8888_SkColorType:
935 case kBGRA_8888_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000936 break;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000937 case kIndex_8_SkColorType:
weita@google.comf9ab99a2009-05-03 18:23:30 +0000938 if (!sameConfigs) {
939 return false;
940 }
941 break;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000942 case kARGB_4444_SkColorType:
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000943 return sameConfigs || kN32_SkColorType == this->colorType();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000944 default:
945 return false;
946 }
reed@android.comfbaa88d2009-05-06 17:44:34 +0000947 return true;
948}
949
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000950bool SkBitmap::copyTo(SkBitmap* dst, SkColorType dstColorType,
951 Allocator* alloc) const {
952 if (!this->canCopyTo(dstColorType)) {
reed@android.comfbaa88d2009-05-06 17:44:34 +0000953 return false;
954 }
955
reed@google.com50dfa012011-04-01 19:05:36 +0000956 // if we have a texture, first get those pixels
957 SkBitmap tmpSrc;
958 const SkBitmap* src = this;
959
scroggo@google.coma2a31922012-12-07 19:14:45 +0000960 if (fPixelRef) {
961 SkIRect subset;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000962 subset.setXYWH(fPixelRefOrigin.fX, fPixelRefOrigin.fY,
963 fInfo.width(), fInfo.height());
reed@google.com672588b2014-01-08 15:42:01 +0000964 if (fPixelRef->readPixels(&tmpSrc, &subset)) {
965 SkASSERT(tmpSrc.width() == this->width());
966 SkASSERT(tmpSrc.height() == this->height());
reed@google.com50dfa012011-04-01 19:05:36 +0000967
reed@google.com672588b2014-01-08 15:42:01 +0000968 // did we get lucky and we can just return tmpSrc?
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000969 if (tmpSrc.colorType() == dstColorType && NULL == alloc) {
reed@google.com672588b2014-01-08 15:42:01 +0000970 dst->swap(tmpSrc);
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000971 // If the result is an exact copy, clone the gen ID.
972 if (dst->pixelRef() && dst->pixelRef()->info() == fPixelRef->info()) {
reed@google.com672588b2014-01-08 15:42:01 +0000973 dst->pixelRef()->cloneGenID(*fPixelRef);
scroggo@google.coma2a31922012-12-07 19:14:45 +0000974 }
reed@google.com672588b2014-01-08 15:42:01 +0000975 return true;
scroggo@google.comd5764e82012-08-22 15:00:05 +0000976 }
reed@google.com672588b2014-01-08 15:42:01 +0000977
978 // fall through to the raster case
979 src = &tmpSrc;
reed@google.com50dfa012011-04-01 19:05:36 +0000980 }
reed@android.comfbaa88d2009-05-06 17:44:34 +0000981 }
reed@android.com311c82d2009-05-05 23:13:23 +0000982
reed@google.com50dfa012011-04-01 19:05:36 +0000983 // we lock this now, since we may need its colortable
984 SkAutoLockPixels srclock(*src);
985 if (!src->readyToDraw()) {
986 return false;
987 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000988
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000989 // The only way to be readyToDraw is if fPixelRef is non NULL.
990 SkASSERT(fPixelRef != NULL);
991
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000992 SkImageInfo dstInfo = src->info();
993 dstInfo.fColorType = dstColorType;
994
reed@google.com50dfa012011-04-01 19:05:36 +0000995 SkBitmap tmpDst;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000996 if (!tmpDst.setInfo(dstInfo)) {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000997 return false;
998 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000999
weita@google.comf9ab99a2009-05-03 18:23:30 +00001000 // allocate colortable if srcConfig == kIndex8_Config
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +00001001 SkAutoTUnref<SkColorTable> ctable;
1002 if (dstColorType == kIndex_8_SkColorType) {
1003 // TODO: can we just ref() the src colortable? Is it reentrant-safe?
1004 ctable.reset(SkNEW_ARGS(SkColorTable, (*src->getColorTable())));
1005 }
reed@google.com50dfa012011-04-01 19:05:36 +00001006 if (!tmpDst.allocPixels(alloc, ctable)) {
weita@google.comf9ab99a2009-05-03 18:23:30 +00001007 return false;
1008 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001009
reed@google.com50dfa012011-04-01 19:05:36 +00001010 if (!tmpDst.readyToDraw()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001011 // allocator/lock failed
1012 return false;
1013 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001014
scroggo@google.com5ccae2c2014-01-15 16:56:52 +00001015 // pixelRef must be non NULL or tmpDst.readyToDraw() would have
1016 // returned false.
1017 SkASSERT(tmpDst.pixelRef() != NULL);
1018
reed@android.comfbaa88d2009-05-06 17:44:34 +00001019 /* do memcpy for the same configs cases, else use drawing
weita@google.comf9ab99a2009-05-03 18:23:30 +00001020 */
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +00001021 if (src->colorType() == dstColorType) {
reed@google.com50dfa012011-04-01 19:05:36 +00001022 if (tmpDst.getSize() == src->getSize()) {
1023 memcpy(tmpDst.getPixels(), src->getPixels(), src->getSafeSize());
scroggo@google.comd5764e82012-08-22 15:00:05 +00001024 SkPixelRef* pixelRef = tmpDst.pixelRef();
scroggo@google.com5ccae2c2014-01-15 16:56:52 +00001025
1026 // In order to reach this point, we know that the width, config and
1027 // rowbytes of the SkPixelRefs are the same, but it is possible for
1028 // the heights to differ, if this SkBitmap's height is a subset of
1029 // fPixelRef. Only if the SkPixelRefs' heights match are we
1030 // guaranteed that this is an exact copy, meaning we should clone
1031 // the genID.
1032 if (pixelRef->info().fHeight == fPixelRef->info().fHeight) {
1033 // TODO: what to do if the two infos match, BUT
1034 // fPixelRef is premul and pixelRef is opaque?
1035 // skipping assert for now
1036 // https://code.google.com/p/skia/issues/detail?id=2012
1037// SkASSERT(pixelRef->info() == fPixelRef->info());
1038 SkASSERT(pixelRef->info().fWidth == fPixelRef->info().fWidth);
1039 SkASSERT(pixelRef->info().fColorType == fPixelRef->info().fColorType);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +00001040 pixelRef->cloneGenID(*fPixelRef);
scroggo@google.comd5764e82012-08-22 15:00:05 +00001041 }
reed@android.com311c82d2009-05-05 23:13:23 +00001042 } else {
reed@google.com50dfa012011-04-01 19:05:36 +00001043 const char* srcP = reinterpret_cast<const char*>(src->getPixels());
1044 char* dstP = reinterpret_cast<char*>(tmpDst.getPixels());
reed@android.com311c82d2009-05-05 23:13:23 +00001045 // to be sure we don't read too much, only copy our logical pixels
reed@google.com50dfa012011-04-01 19:05:36 +00001046 size_t bytesToCopy = tmpDst.width() * tmpDst.bytesPerPixel();
1047 for (int y = 0; y < tmpDst.height(); y++) {
reed@android.com311c82d2009-05-05 23:13:23 +00001048 memcpy(dstP, srcP, bytesToCopy);
reed@google.com50dfa012011-04-01 19:05:36 +00001049 srcP += src->rowBytes();
1050 dstP += tmpDst.rowBytes();
reed@android.com311c82d2009-05-05 23:13:23 +00001051 }
1052 }
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +00001053 } else if (kARGB_4444_SkColorType == dstColorType
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +00001054 && kN32_SkColorType == src->colorType()) {
scroggo@google.com8dc8bc52013-08-07 19:16:05 +00001055 SkASSERT(src->height() == tmpDst.height());
1056 SkASSERT(src->width() == tmpDst.width());
1057 for (int y = 0; y < src->height(); ++y) {
1058 SkPMColor16* SK_RESTRICT dstRow = (SkPMColor16*) tmpDst.getAddr16(0, y);
1059 SkPMColor* SK_RESTRICT srcRow = (SkPMColor*) src->getAddr32(0, y);
1060 DITHER_4444_SCAN(y);
1061 for (int x = 0; x < src->width(); ++x) {
1062 dstRow[x] = SkDitherARGB32To4444(srcRow[x],
1063 DITHER_VALUE(x));
1064 }
1065 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001066 } else {
robertphillips@google.com0197b322013-10-10 15:48:16 +00001067 // Always clear the dest in case one of the blitters accesses it
1068 // TODO: switch the allocation of tmpDst to call sk_calloc_throw
1069 tmpDst.eraseColor(SK_ColorTRANSPARENT);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001070
reed@google.com50dfa012011-04-01 19:05:36 +00001071 SkCanvas canvas(tmpDst);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001072 SkPaint paint;
1073
1074 paint.setDither(true);
reed@google.com50dfa012011-04-01 19:05:36 +00001075 canvas.drawBitmap(*src, 0, 0, &paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001076 }
1077
reed@google.com50dfa012011-04-01 19:05:36 +00001078 dst->swap(tmpDst);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001079 return true;
1080}
1081
commit-bot@chromium.orgfab349c2014-03-05 02:34:58 +00001082bool SkBitmap::deepCopyTo(SkBitmap* dst) const {
commit-bot@chromium.orgeeef0cc2014-05-21 15:58:00 +00001083 const SkBitmap::Config dstConfig = this->config();
1084 const SkColorType dstCT = SkBitmapConfigToColorType(dstConfig);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001085
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +00001086 if (!this->canCopyTo(dstCT)) {
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001087 return false;
1088 }
1089
1090 // If we have a PixelRef, and it supports deep copy, use it.
1091 // Currently supported only by texture-backed bitmaps.
1092 if (fPixelRef) {
commit-bot@chromium.orgeeef0cc2014-05-21 15:58:00 +00001093 SkPixelRef* pixelRef = fPixelRef->deepCopy(dstConfig);
1094 if (pixelRef) {
1095 uint32_t rowBytes;
1096 if (this->colorType() == dstCT) {
1097 // Since there is no subset to pass to deepCopy, and deepCopy
1098 // succeeded, the new pixel ref must be identical.
1099 SkASSERT(fPixelRef->info() == pixelRef->info());
1100 pixelRef->cloneGenID(*fPixelRef);
1101 // Use the same rowBytes as the original.
1102 rowBytes = fRowBytes;
1103 } else {
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001104 // With the new config, an appropriate fRowBytes will be computed by setInfo.
commit-bot@chromium.orgeeef0cc2014-05-21 15:58:00 +00001105 rowBytes = 0;
1106 }
1107
1108 SkImageInfo info = fInfo;
1109 info.fColorType = dstCT;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001110 if (!dst->setInfo(info, rowBytes)) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001111 return false;
1112 }
commit-bot@chromium.orgeeef0cc2014-05-21 15:58:00 +00001113 dst->setPixelRef(pixelRef, fPixelRefOrigin)->unref();
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001114 return true;
1115 }
1116 }
1117
1118 if (this->getTexture()) {
1119 return false;
1120 } else {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +00001121 return this->copyTo(dst, dstCT, NULL);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001122 }
1123}
1124
reed@android.com8a1c16f2008-12-17 15:59:43 +00001125///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +00001126
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +00001127static bool GetBitmapAlpha(const SkBitmap& src, uint8_t* SK_RESTRICT alpha,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001128 int alphaRowBytes) {
1129 SkASSERT(alpha != NULL);
1130 SkASSERT(alphaRowBytes >= src.width());
1131
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001132 SkColorType colorType = src.colorType();
1133 int w = src.width();
1134 int h = src.height();
1135 size_t rb = src.rowBytes();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001136
reed@android.com1cdcb512009-08-24 19:11:00 +00001137 SkAutoLockPixels alp(src);
1138 if (!src.readyToDraw()) {
1139 // zero out the alpha buffer and return
1140 while (--h >= 0) {
1141 memset(alpha, 0, w);
1142 alpha += alphaRowBytes;
1143 }
1144 return false;
1145 }
reed@google.com82065d62011-02-07 15:30:46 +00001146
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001147 if (kAlpha_8_SkColorType == colorType && !src.isOpaque()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001148 const uint8_t* s = src.getAddr8(0, 0);
1149 while (--h >= 0) {
1150 memcpy(alpha, s, w);
1151 s += rb;
1152 alpha += alphaRowBytes;
1153 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001154 } else if (kN32_SkColorType == colorType && !src.isOpaque()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001155 const SkPMColor* SK_RESTRICT s = src.getAddr32(0, 0);
1156 while (--h >= 0) {
1157 for (int x = 0; x < w; x++) {
1158 alpha[x] = SkGetPackedA32(s[x]);
1159 }
1160 s = (const SkPMColor*)((const char*)s + rb);
1161 alpha += alphaRowBytes;
1162 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001163 } else if (kARGB_4444_SkColorType == colorType && !src.isOpaque()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001164 const SkPMColor16* SK_RESTRICT s = src.getAddr16(0, 0);
1165 while (--h >= 0) {
1166 for (int x = 0; x < w; x++) {
1167 alpha[x] = SkPacked4444ToA32(s[x]);
1168 }
1169 s = (const SkPMColor16*)((const char*)s + rb);
1170 alpha += alphaRowBytes;
1171 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001172 } else if (kIndex_8_SkColorType == colorType && !src.isOpaque()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001173 SkColorTable* ct = src.getColorTable();
1174 if (ct) {
1175 const SkPMColor* SK_RESTRICT table = ct->lockColors();
1176 const uint8_t* SK_RESTRICT s = src.getAddr8(0, 0);
1177 while (--h >= 0) {
1178 for (int x = 0; x < w; x++) {
1179 alpha[x] = SkGetPackedA32(table[s[x]]);
1180 }
1181 s += rb;
1182 alpha += alphaRowBytes;
1183 }
reed@google.com0a6151d2013-10-10 14:44:56 +00001184 ct->unlockColors();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001185 }
1186 } else { // src is opaque, so just fill alpha[] with 0xFF
1187 memset(alpha, 0xFF, h * alphaRowBytes);
1188 }
reed@android.com1cdcb512009-08-24 19:11:00 +00001189 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001190}
1191
1192#include "SkPaint.h"
1193#include "SkMaskFilter.h"
1194#include "SkMatrix.h"
1195
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001196bool SkBitmap::extractAlpha(SkBitmap* dst, const SkPaint* paint,
djsollen@google.com57f49692011-02-23 20:46:31 +00001197 Allocator *allocator, SkIPoint* offset) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001198 SkDEBUGCODE(this->validate();)
1199
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001200 SkBitmap tmpBitmap;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001201 SkMatrix identity;
1202 SkMask srcM, dstM;
1203
1204 srcM.fBounds.set(0, 0, this->width(), this->height());
1205 srcM.fRowBytes = SkAlign4(this->width());
1206 srcM.fFormat = SkMask::kA8_Format;
1207
1208 SkMaskFilter* filter = paint ? paint->getMaskFilter() : NULL;
1209
1210 // compute our (larger?) dst bounds if we have a filter
1211 if (NULL != filter) {
1212 identity.reset();
1213 srcM.fImage = NULL;
1214 if (!filter->filterMask(&dstM, srcM, identity, NULL)) {
1215 goto NO_FILTER_CASE;
1216 }
1217 dstM.fRowBytes = SkAlign4(dstM.fBounds.width());
1218 } else {
1219 NO_FILTER_CASE:
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001220 tmpBitmap.setInfo(SkImageInfo::MakeA8(this->width(), this->height()), srcM.fRowBytes);
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001221 if (!tmpBitmap.allocPixels(allocator, NULL)) {
1222 // Allocation of pixels for alpha bitmap failed.
1223 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
1224 tmpBitmap.width(), tmpBitmap.height());
1225 return false;
1226 }
1227 GetBitmapAlpha(*this, tmpBitmap.getAddr8(0, 0), srcM.fRowBytes);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001228 if (offset) {
1229 offset->set(0, 0);
1230 }
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001231 tmpBitmap.swap(*dst);
1232 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001233 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001234 srcM.fImage = SkMask::AllocImage(srcM.computeImageSize());
1235 SkAutoMaskFreeImage srcCleanup(srcM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001236
1237 GetBitmapAlpha(*this, srcM.fImage, srcM.fRowBytes);
1238 if (!filter->filterMask(&dstM, srcM, identity, NULL)) {
1239 goto NO_FILTER_CASE;
1240 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001241 SkAutoMaskFreeImage dstCleanup(dstM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001242
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001243 tmpBitmap.setInfo(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.height()),
1244 dstM.fRowBytes);
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001245 if (!tmpBitmap.allocPixels(allocator, NULL)) {
1246 // Allocation of pixels for alpha bitmap failed.
1247 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
1248 tmpBitmap.width(), tmpBitmap.height());
1249 return false;
1250 }
1251 memcpy(tmpBitmap.getPixels(), dstM.fImage, dstM.computeImageSize());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001252 if (offset) {
1253 offset->set(dstM.fBounds.fLeft, dstM.fBounds.fTop);
1254 }
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001255 SkDEBUGCODE(tmpBitmap.validate();)
1256
1257 tmpBitmap.swap(*dst);
1258 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001259}
1260
1261///////////////////////////////////////////////////////////////////////////////
1262
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001263void SkBitmap::WriteRawPixels(SkWriteBuffer* buffer, const SkBitmap& bitmap) {
1264 const SkImageInfo info = bitmap.info();
1265 SkAutoLockPixels alp(bitmap);
1266 if (0 == info.width() || 0 == info.height() || NULL == bitmap.getPixels()) {
1267 buffer->writeUInt(0); // instead of snugRB, signaling no pixels
1268 return;
1269 }
1270
1271 const size_t snugRB = info.width() * info.bytesPerPixel();
1272 const char* src = (const char*)bitmap.getPixels();
1273 const size_t ramRB = bitmap.rowBytes();
skia.committer@gmail.com3c134a92014-05-24 03:05:26 +00001274
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001275 buffer->write32(SkToU32(snugRB));
1276 info.flatten(*buffer);
1277
1278 const size_t size = snugRB * info.height();
1279 SkAutoMalloc storage(size);
1280 char* dst = (char*)storage.get();
1281 for (int y = 0; y < info.height(); ++y) {
1282 memcpy(dst, src, snugRB);
1283 dst += snugRB;
1284 src += ramRB;
1285 }
1286 buffer->writeByteArray(storage.get(), size);
1287
1288 SkColorTable* ct = bitmap.getColorTable();
1289 if (kIndex_8_SkColorType == info.colorType() && ct) {
1290 buffer->writeBool(true);
1291 ct->writeToBuffer(*buffer);
1292 } else {
1293 buffer->writeBool(false);
1294 }
1295}
1296
1297bool SkBitmap::ReadRawPixels(SkReadBuffer* buffer, SkBitmap* bitmap) {
1298 const size_t snugRB = buffer->readUInt();
1299 if (0 == snugRB) { // no pixels
1300 return false;
1301 }
1302
1303 SkImageInfo info;
1304 info.unflatten(*buffer);
1305
1306 const size_t ramRB = info.minRowBytes();
1307 const int height = info.height();
1308 const size_t snugSize = snugRB * height;
1309 const size_t ramSize = ramRB * height;
commit-bot@chromium.org05858432014-05-30 01:06:44 +00001310 if (!buffer->validate(snugSize <= ramSize)) {
1311 return false;
1312 }
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001313
1314 char* dst = (char*)sk_malloc_throw(ramSize);
1315 buffer->readByteArray(dst, snugSize);
1316 SkAutoDataUnref data(SkData::NewFromMalloc(dst, ramSize));
1317
1318 if (snugSize != ramSize) {
1319 const char* srcRow = dst + snugRB * (height - 1);
1320 char* dstRow = dst + ramRB * (height - 1);
1321 for (int y = height - 1; y >= 1; --y) {
1322 memmove(dstRow, srcRow, snugRB);
1323 srcRow -= snugRB;
1324 dstRow -= ramRB;
1325 }
1326 SkASSERT(srcRow == dstRow); // first row does not need to be moved
1327 }
skia.committer@gmail.com3c134a92014-05-24 03:05:26 +00001328
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001329 SkAutoTUnref<SkColorTable> ctable;
1330 if (buffer->readBool()) {
1331 ctable.reset(SkNEW_ARGS(SkColorTable, (*buffer)));
1332 }
skia.committer@gmail.com3c134a92014-05-24 03:05:26 +00001333
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001334 SkAutoTUnref<SkPixelRef> pr(SkMallocPixelRef::NewWithData(info, info.minRowBytes(),
1335 ctable.get(), data.get()));
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001336 bitmap->setInfo(pr->info());
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001337 bitmap->setPixelRef(pr, 0, 0);
1338 return true;
1339}
1340
reed@android.com8a1c16f2008-12-17 15:59:43 +00001341enum {
1342 SERIALIZE_PIXELTYPE_NONE,
djsollen@google.com21830d92012-08-07 19:49:41 +00001343 SERIALIZE_PIXELTYPE_REF_DATA
reed@android.com8a1c16f2008-12-17 15:59:43 +00001344};
1345
commit-bot@chromium.org851155c2014-05-27 14:03:51 +00001346void SkBitmap::legacyUnflatten(SkReadBuffer& buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001347 this->reset();
weita@google.comf9ab99a2009-05-03 18:23:30 +00001348
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001349 SkImageInfo info;
1350 info.unflatten(buffer);
1351 size_t rowBytes = buffer.readInt();
commit-bot@chromium.org33fed142014-02-13 18:46:13 +00001352 if (!buffer.validate((info.width() >= 0) && (info.height() >= 0) &&
1353 SkColorTypeIsValid(info.fColorType) &&
1354 SkAlphaTypeIsValid(info.fAlphaType) &&
1355 validate_alphaType(info.fColorType, info.fAlphaType) &&
1356 info.validRowBytes(rowBytes))) {
1357 return;
1358 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001359
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001360 bool configIsValid = this->setInfo(info, rowBytes);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001361 buffer.validate(configIsValid);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001362
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001363 int reftype = buffer.readInt();
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +00001364 if (buffer.validate((SERIALIZE_PIXELTYPE_REF_DATA == reftype) ||
1365 (SERIALIZE_PIXELTYPE_NONE == reftype))) {
1366 switch (reftype) {
1367 case SERIALIZE_PIXELTYPE_REF_DATA: {
reed@google.com672588b2014-01-08 15:42:01 +00001368 SkIPoint origin;
1369 origin.fX = buffer.readInt();
1370 origin.fY = buffer.readInt();
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001371 size_t offset = origin.fY * rowBytes + origin.fX * info.bytesPerPixel();
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +00001372 SkPixelRef* pr = buffer.readPixelRef();
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +00001373 if (!buffer.validate((NULL == pr) ||
1374 (pr->getAllocatedSizeInBytes() >= (offset + this->getSafeSize())))) {
reed@google.com672588b2014-01-08 15:42:01 +00001375 origin.setZero();
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +00001376 }
reed@google.com672588b2014-01-08 15:42:01 +00001377 SkSafeUnref(this->setPixelRef(pr, origin));
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +00001378 break;
1379 }
1380 case SERIALIZE_PIXELTYPE_NONE:
1381 break;
1382 default:
1383 SkDEBUGFAIL("unrecognized pixeltype in serialized data");
1384 sk_throw();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001385 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001386 }
1387}
1388
1389///////////////////////////////////////////////////////////////////////////////
1390
1391SkBitmap::RLEPixels::RLEPixels(int width, int height) {
1392 fHeight = height;
commit-bot@chromium.org235002f2013-10-09 18:39:59 +00001393 fYPtrs = (uint8_t**)sk_calloc_throw(height * sizeof(uint8_t*));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001394}
1395
1396SkBitmap::RLEPixels::~RLEPixels() {
1397 sk_free(fYPtrs);
1398}
1399
1400///////////////////////////////////////////////////////////////////////////////
1401
1402#ifdef SK_DEBUG
1403void SkBitmap::validate() const {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001404 fInfo.validate();
commit-bot@chromium.orgd5414e52014-02-13 22:30:38 +00001405
1406 // ImageInfo may not require this, but Bitmap ensures that opaque-only
1407 // colorTypes report opaque for their alphatype
1408 if (kRGB_565_SkColorType == fInfo.colorType()) {
1409 SkASSERT(kOpaque_SkAlphaType == fInfo.alphaType());
1410 }
1411
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001412 SkASSERT(fInfo.validRowBytes(fRowBytes));
scroggo@google.com8e990eb2013-06-14 15:55:56 +00001413 uint8_t allFlags = kImageIsOpaque_Flag | kImageIsVolatile_Flag | kImageIsImmutable_Flag;
1414#ifdef SK_BUILD_FOR_ANDROID
1415 allFlags |= kHasHardwareMipMap_Flag;
1416#endif
1417 SkASSERT(fFlags <= allFlags);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001418 SkASSERT(fPixelLockCount >= 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001419
reed@google.com615316c2014-01-15 19:15:23 +00001420 if (fPixels) {
1421 SkASSERT(fPixelRef);
1422 SkASSERT(fPixelLockCount > 0);
1423 SkASSERT(fPixelRef->isLocked());
1424 SkASSERT(fPixelRef->rowBytes() == fRowBytes);
1425 SkASSERT(fPixelRefOrigin.fX >= 0);
1426 SkASSERT(fPixelRefOrigin.fY >= 0);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001427 SkASSERT(fPixelRef->info().width() >= (int)this->width() + fPixelRefOrigin.fX);
1428 SkASSERT(fPixelRef->info().fHeight >= (int)this->height() + fPixelRefOrigin.fY);
1429 SkASSERT(fPixelRef->rowBytes() >= fInfo.minRowBytes());
reed@google.com615316c2014-01-15 19:15:23 +00001430 } else {
1431 SkASSERT(NULL == fColorTable);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001432 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001433}
1434#endif
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001435
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +00001436#ifndef SK_IGNORE_TO_STRING
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001437void SkBitmap::toString(SkString* str) const {
1438
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001439 static const char* gColorTypeNames[kLastEnum_SkColorType + 1] = {
1440 "UNKNOWN", "A8", "565", "4444", "RGBA", "BGRA", "INDEX8",
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001441 };
1442
1443 str->appendf("bitmap: ((%d, %d) %s", this->width(), this->height(),
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001444 gColorTypeNames[this->colorType()]);
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001445
1446 str->append(" (");
1447 if (this->isOpaque()) {
1448 str->append("opaque");
1449 } else {
1450 str->append("transparent");
1451 }
1452 if (this->isImmutable()) {
1453 str->append(", immutable");
1454 } else {
1455 str->append(", not-immutable");
1456 }
1457 str->append(")");
1458
1459 SkPixelRef* pr = this->pixelRef();
1460 if (NULL == pr) {
1461 // show null or the explicit pixel address (rare)
1462 str->appendf(" pixels:%p", this->getPixels());
1463 } else {
1464 const char* uri = pr->getURI();
1465 if (NULL != uri) {
1466 str->appendf(" uri:\"%s\"", uri);
1467 } else {
1468 str->appendf(" pixelref:%p", pr);
1469 }
1470 }
1471
1472 str->append(")");
1473}
1474#endif
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001475
1476///////////////////////////////////////////////////////////////////////////////
1477
1478#ifdef SK_DEBUG
1479void SkImageInfo::validate() const {
1480 SkASSERT(fWidth >= 0);
1481 SkASSERT(fHeight >= 0);
1482 SkASSERT(SkColorTypeIsValid(fColorType));
1483 SkASSERT(SkAlphaTypeIsValid(fAlphaType));
1484}
1485#endif