blob: 799104b04a9d1149ee1e4734c740753e3d51a223 [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
reedddd014e2014-06-05 08:51:20 -070098#ifdef SK_SUPPORT_LEGACY_COMPUTE_CONFIG_SIZE
reed@android.com8a1c16f2008-12-17 15:59:43 +000099int SkBitmap::ComputeBytesPerPixel(SkBitmap::Config config) {
100 int bpp;
101 switch (config) {
102 case kNo_Config:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 bpp = 0; // not applicable
104 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 case kA8_Config:
106 case kIndex8_Config:
107 bpp = 1;
108 break;
109 case kRGB_565_Config:
110 case kARGB_4444_Config:
111 bpp = 2;
112 break;
113 case kARGB_8888_Config:
114 bpp = 4;
115 break;
116 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000117 SkDEBUGFAIL("unknown config");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 bpp = 0; // error
119 break;
120 }
121 return bpp;
122}
123
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000124size_t SkBitmap::ComputeRowBytes(Config c, int width) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000125 return SkColorTypeMinRowBytes(SkBitmapConfigToColorType(c), width);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126}
127
reed@google.com57212f92013-12-30 14:40:38 +0000128int64_t SkBitmap::ComputeSize64(Config config, int width, int height) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000129 SkColorType ct = SkBitmapConfigToColorType(config);
130 int64_t rowBytes = sk_64_mul(SkColorTypeBytesPerPixel(ct), width);
reed@google.com57212f92013-12-30 14:40:38 +0000131 return rowBytes * height;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132}
133
134size_t SkBitmap::ComputeSize(Config c, int width, int height) {
reed@google.com57212f92013-12-30 14:40:38 +0000135 int64_t size = SkBitmap::ComputeSize64(c, width, height);
136 return sk_64_isS32(size) ? sk_64_asS32(size) : 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137}
reedddd014e2014-06-05 08:51:20 -0700138#endif
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000139
reed@google.com86b2e432012-03-15 21:17:03 +0000140void SkBitmap::getBounds(SkRect* bounds) const {
141 SkASSERT(bounds);
142 bounds->set(0, 0,
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000143 SkIntToScalar(fInfo.fWidth), SkIntToScalar(fInfo.fHeight));
reed@google.com86b2e432012-03-15 21:17:03 +0000144}
145
reed@google.com80e14592012-03-16 14:58:07 +0000146void SkBitmap::getBounds(SkIRect* bounds) const {
147 SkASSERT(bounds);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000148 bounds->set(0, 0, fInfo.fWidth, fInfo.fHeight);
reed@google.com80e14592012-03-16 14:58:07 +0000149}
150
reed@google.com86b2e432012-03-15 21:17:03 +0000151///////////////////////////////////////////////////////////////////////////////
152
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000153static bool validate_alphaType(SkColorType colorType, SkAlphaType alphaType,
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000154 SkAlphaType* canonical = NULL) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000155 switch (colorType) {
156 case kUnknown_SkColorType:
reed@google.com383a6972013-10-21 14:00:07 +0000157 alphaType = kIgnore_SkAlphaType;
158 break;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000159 case kAlpha_8_SkColorType:
reed@google.com383a6972013-10-21 14:00:07 +0000160 if (kUnpremul_SkAlphaType == alphaType) {
161 alphaType = kPremul_SkAlphaType;
162 }
163 // fall-through
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000164 case kIndex_8_SkColorType:
165 case kARGB_4444_SkColorType:
166 case kRGBA_8888_SkColorType:
167 case kBGRA_8888_SkColorType:
reed@google.com383a6972013-10-21 14:00:07 +0000168 if (kIgnore_SkAlphaType == alphaType) {
169 return false;
170 }
171 break;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000172 case kRGB_565_SkColorType:
reed@google.com383a6972013-10-21 14:00:07 +0000173 alphaType = kOpaque_SkAlphaType;
174 break;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000175 default:
176 return false;
reed@android.com149e2f62009-05-22 14:39:03 +0000177 }
reed@google.com383a6972013-10-21 14:00:07 +0000178 if (canonical) {
179 *canonical = alphaType;
180 }
181 return true;
182}
reed@android.com149e2f62009-05-22 14:39:03 +0000183
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000184bool SkBitmap::setInfo(const SkImageInfo& origInfo, size_t rowBytes) {
commit-bot@chromium.orgd5414e52014-02-13 22:30:38 +0000185 SkImageInfo info = origInfo;
186
187 if (!validate_alphaType(info.fColorType, info.fAlphaType,
188 &info.fAlphaType)) {
189 return reset_return_false(this);
190 }
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000191
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000192 // require that rowBytes fit in 31bits
193 int64_t mrb = info.minRowBytes64();
194 if ((int32_t)mrb != mrb) {
195 return reset_return_false(this);
reed@google.com383a6972013-10-21 14:00:07 +0000196 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000197 if ((int64_t)rowBytes != (int32_t)rowBytes) {
198 return reset_return_false(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000199 }
reed@android.com89bb83a2009-05-29 21:30:42 +0000200
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000201 if (info.width() < 0 || info.height() < 0) {
202 return reset_return_false(this);
203 }
204
205 if (kUnknown_SkColorType == info.colorType()) {
206 rowBytes = 0;
207 } else if (0 == rowBytes) {
208 rowBytes = (size_t)mrb;
209 } else if (rowBytes < info.minRowBytes()) {
210 return reset_return_false(this);
reed@google.com383a6972013-10-21 14:00:07 +0000211 }
212
213 this->freePixels();
214
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000215 fInfo = info;
216 fRowBytes = SkToU32(rowBytes);
reed@google.com383a6972013-10-21 14:00:07 +0000217 return true;
reed@google.com383a6972013-10-21 14:00:07 +0000218}
219
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000220bool SkBitmap::setConfig(Config config, int width, int height, size_t rowBytes,
221 SkAlphaType alphaType) {
222 SkColorType ct = SkBitmapConfigToColorType(config);
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000223 return this->setInfo(SkImageInfo::Make(width, height, ct, alphaType), rowBytes);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000224}
225
reed@google.com383a6972013-10-21 14:00:07 +0000226bool SkBitmap::setAlphaType(SkAlphaType alphaType) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000227 if (!validate_alphaType(fInfo.fColorType, alphaType, &alphaType)) {
reed@google.com383a6972013-10-21 14:00:07 +0000228 return false;
229 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000230 if (fInfo.fAlphaType != alphaType) {
231 fInfo.fAlphaType = alphaType;
commit-bot@chromium.org0e8d0d62014-01-27 15:41:07 +0000232 if (fPixelRef) {
reed@google.comc1587f92014-01-28 16:05:39 +0000233 fPixelRef->changeAlphaType(alphaType);
commit-bot@chromium.org0e8d0d62014-01-27 15:41:07 +0000234 }
235 }
reed@google.com383a6972013-10-21 14:00:07 +0000236 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000237}
238
239void SkBitmap::updatePixelsFromRef() const {
240 if (NULL != fPixelRef) {
241 if (fPixelLockCount > 0) {
reed@google.comff0da4f2012-05-17 13:14:52 +0000242 SkASSERT(fPixelRef->isLocked());
weita@google.comf9ab99a2009-05-03 18:23:30 +0000243
reed@android.com8a1c16f2008-12-17 15:59:43 +0000244 void* p = fPixelRef->pixels();
245 if (NULL != p) {
reed@google.com672588b2014-01-08 15:42:01 +0000246 p = (char*)p
reed@google.com303c4752014-01-09 20:00:14 +0000247 + fPixelRefOrigin.fY * fRowBytes
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000248 + fPixelRefOrigin.fX * fInfo.bytesPerPixel();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000249 }
250 fPixels = p;
reed@google.com5f62ed72014-01-15 19:59:45 +0000251 fColorTable = fPixelRef->colorTable();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000252 } else {
253 SkASSERT(0 == fPixelLockCount);
254 fPixels = NULL;
reed@google.com5f62ed72014-01-15 19:59:45 +0000255 fColorTable = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000256 }
257 }
258}
259
reed@google.com672588b2014-01-08 15:42:01 +0000260SkPixelRef* SkBitmap::setPixelRef(SkPixelRef* pr, int dx, int dy) {
reed@google.comdcea5302014-01-03 13:43:01 +0000261#ifdef SK_DEBUG
reed@google.com672588b2014-01-08 15:42:01 +0000262 if (pr) {
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000263 if (kUnknown_SkColorType != fInfo.colorType()) {
reed@google.comdcea5302014-01-03 13:43:01 +0000264 const SkImageInfo& prInfo = pr->info();
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000265 SkASSERT(fInfo.fWidth <= prInfo.fWidth);
266 SkASSERT(fInfo.fHeight <= prInfo.fHeight);
267 SkASSERT(fInfo.fColorType == prInfo.fColorType);
reed@google.comdcea5302014-01-03 13:43:01 +0000268 switch (prInfo.fAlphaType) {
269 case kIgnore_SkAlphaType:
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000270 SkASSERT(fInfo.fAlphaType == kIgnore_SkAlphaType);
reed@google.comdcea5302014-01-03 13:43:01 +0000271 break;
272 case kOpaque_SkAlphaType:
273 case kPremul_SkAlphaType:
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000274 SkASSERT(fInfo.fAlphaType == kOpaque_SkAlphaType ||
275 fInfo.fAlphaType == kPremul_SkAlphaType);
reed@google.comdcea5302014-01-03 13:43:01 +0000276 break;
277 case kUnpremul_SkAlphaType:
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000278 SkASSERT(fInfo.fAlphaType == kOpaque_SkAlphaType ||
279 fInfo.fAlphaType == kUnpremul_SkAlphaType);
reed@google.comdcea5302014-01-03 13:43:01 +0000280 break;
281 }
282 }
283 }
284#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000285
reed@google.com672588b2014-01-08 15:42:01 +0000286 if (pr) {
287 const SkImageInfo& info = pr->info();
288 fPixelRefOrigin.set(SkPin32(dx, 0, info.fWidth),
289 SkPin32(dy, 0, info.fHeight));
290 } else {
291 // ignore dx,dy if there is no pixelref
292 fPixelRefOrigin.setZero();
293 }
294
295 if (fPixelRef != pr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000296 if (fPixelRef != pr) {
297 this->freePixels();
298 SkASSERT(NULL == fPixelRef);
weita@google.comf9ab99a2009-05-03 18:23:30 +0000299
reed@google.com82065d62011-02-07 15:30:46 +0000300 SkSafeRef(pr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000301 fPixelRef = pr;
302 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000303 this->updatePixelsFromRef();
304 }
305
306 SkDEBUGCODE(this->validate();)
307 return pr;
308}
309
310void SkBitmap::lockPixels() const {
djsollen@google.com7c6d2642013-08-06 12:19:38 +0000311 if (NULL != fPixelRef && 0 == sk_atomic_inc(&fPixelLockCount)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000312 fPixelRef->lockPixels();
313 this->updatePixelsFromRef();
314 }
315 SkDEBUGCODE(this->validate();)
316}
317
318void SkBitmap::unlockPixels() const {
319 SkASSERT(NULL == fPixelRef || fPixelLockCount > 0);
320
djsollen@google.com7c6d2642013-08-06 12:19:38 +0000321 if (NULL != fPixelRef && 1 == sk_atomic_dec(&fPixelLockCount)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000322 fPixelRef->unlockPixels();
323 this->updatePixelsFromRef();
324 }
325 SkDEBUGCODE(this->validate();)
326}
327
reed@google.com9c49bc32011-07-07 13:42:37 +0000328bool SkBitmap::lockPixelsAreWritable() const {
djsollen@google.comc84b8332012-07-27 13:41:44 +0000329 return (fPixelRef) ? fPixelRef->lockPixelsAreWritable() : false;
reed@google.com9c49bc32011-07-07 13:42:37 +0000330}
331
reed@android.com8a1c16f2008-12-17 15:59:43 +0000332void SkBitmap::setPixels(void* p, SkColorTable* ctable) {
reed@google.com8e1034e2012-07-30 13:16:35 +0000333 if (NULL == p) {
reed@google.com672588b2014-01-08 15:42:01 +0000334 this->setPixelRef(NULL);
reed@google.com8e1034e2012-07-30 13:16:35 +0000335 return;
336 }
337
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000338 if (kUnknown_SkColorType == fInfo.colorType()) {
reed@google.com672588b2014-01-08 15:42:01 +0000339 this->setPixelRef(NULL);
reed@google.combf790232013-12-13 19:45:58 +0000340 return;
341 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000342
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000343 SkPixelRef* pr = SkMallocPixelRef::NewDirect(fInfo, p, fRowBytes, ctable);
reed@google.combf790232013-12-13 19:45:58 +0000344 if (NULL == pr) {
reed@google.com672588b2014-01-08 15:42:01 +0000345 this->setPixelRef(NULL);
reed@google.combf790232013-12-13 19:45:58 +0000346 return;
347 }
348
349 this->setPixelRef(pr)->unref();
350
djsollen@google.comc84b8332012-07-27 13:41:44 +0000351 // since we're already allocated, we lockPixels right away
352 this->lockPixels();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000353 SkDEBUGCODE(this->validate();)
354}
355
356bool SkBitmap::allocPixels(Allocator* allocator, SkColorTable* ctable) {
357 HeapAllocator stdalloc;
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000358
reed@android.com8a1c16f2008-12-17 15:59:43 +0000359 if (NULL == allocator) {
360 allocator = &stdalloc;
361 }
362 return allocator->allocPixelRef(this, ctable);
363}
364
reed@google.com9ebcac52014-01-24 18:53:42 +0000365///////////////////////////////////////////////////////////////////////////////
366
reed@google.com9ebcac52014-01-24 18:53:42 +0000367bool SkBitmap::allocPixels(const SkImageInfo& info, SkPixelRefFactory* factory,
368 SkColorTable* ctable) {
369 if (kIndex_8_SkColorType == info.fColorType && NULL == ctable) {
370 return reset_return_false(this);
371 }
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000372 if (!this->setInfo(info)) {
reed@google.com9ebcac52014-01-24 18:53:42 +0000373 return reset_return_false(this);
374 }
375
376 SkMallocPixelRef::PRFactory defaultFactory;
377 if (NULL == factory) {
378 factory = &defaultFactory;
379 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000380
reed@google.com9ebcac52014-01-24 18:53:42 +0000381 SkPixelRef* pr = factory->create(info, ctable);
382 if (NULL == pr) {
383 return reset_return_false(this);
384 }
385 this->setPixelRef(pr)->unref();
386
387 // TODO: lockPixels could/should return bool or void*/NULL
388 this->lockPixels();
389 if (NULL == this->getPixels()) {
390 return reset_return_false(this);
391 }
392 return true;
393}
394
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000395bool SkBitmap::installPixels(const SkImageInfo& info, void* pixels, size_t rb, SkColorTable* ct,
396 void (*releaseProc)(void* addr, void* context), void* context) {
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000397 if (!this->setInfo(info, rb)) {
reed@google.com9ebcac52014-01-24 18:53:42 +0000398 this->reset();
399 return false;
400 }
401
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000402 SkPixelRef* pr = SkMallocPixelRef::NewWithProc(info, rb, ct, pixels, releaseProc, context);
reed@google.com9ebcac52014-01-24 18:53:42 +0000403 if (!pr) {
404 this->reset();
405 return false;
406 }
407
408 this->setPixelRef(pr)->unref();
mike@reedtribe.org6e58cf32014-02-16 20:54:21 +0000409
410 // since we're already allocated, we lockPixels right away
411 this->lockPixels();
mike@reedtribe.org6e58cf32014-02-16 20:54:21 +0000412 SkDEBUGCODE(this->validate();)
reed@google.com9ebcac52014-01-24 18:53:42 +0000413 return true;
414}
415
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000416bool SkBitmap::installMaskPixels(const SkMask& mask) {
417 if (SkMask::kA8_Format != mask.fFormat) {
418 this->reset();
419 return false;
420 }
421 return this->installPixels(SkImageInfo::MakeA8(mask.fBounds.width(),
422 mask.fBounds.height()),
423 mask.fImage, mask.fRowBytes);
424}
425
reed@google.comeb9a46c2014-01-25 16:46:20 +0000426///////////////////////////////////////////////////////////////////////////////
427
reed@android.com8a1c16f2008-12-17 15:59:43 +0000428void SkBitmap::freePixels() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000429 if (NULL != fPixelRef) {
430 if (fPixelLockCount > 0) {
431 fPixelRef->unlockPixels();
432 }
433 fPixelRef->unref();
434 fPixelRef = NULL;
reed@google.com672588b2014-01-08 15:42:01 +0000435 fPixelRefOrigin.setZero();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000436 }
437 fPixelLockCount = 0;
438 fPixels = NULL;
reed@google.com5f62ed72014-01-15 19:59:45 +0000439 fColorTable = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000440}
441
reed@android.com8a1c16f2008-12-17 15:59:43 +0000442uint32_t SkBitmap::getGenerationID() const {
djsollen@google.comc84b8332012-07-27 13:41:44 +0000443 return (fPixelRef) ? fPixelRef->getGenerationID() : 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000444}
445
446void SkBitmap::notifyPixelsChanged() const {
junov@chromium.orgb0521292011-12-15 20:14:06 +0000447 SkASSERT(!this->isImmutable());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000448 if (fPixelRef) {
449 fPixelRef->notifyPixelsChanged();
450 }
451}
452
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000453GrTexture* SkBitmap::getTexture() const {
reed@android.comce4e53a2010-09-09 16:01:26 +0000454 return fPixelRef ? fPixelRef->getTexture() : NULL;
455}
456
reed@android.com8a1c16f2008-12-17 15:59:43 +0000457///////////////////////////////////////////////////////////////////////////////
458
reed@android.com8a1c16f2008-12-17 15:59:43 +0000459/** We explicitly use the same allocator for our pixels that SkMask does,
460 so that we can freely assign memory allocated by one class to the other.
461 */
462bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst,
463 SkColorTable* ctable) {
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000464 const SkImageInfo info = dst->info();
465 if (kUnknown_SkColorType == info.colorType()) {
reed@google.combf790232013-12-13 19:45:58 +0000466// SkDebugf("unsupported config for info %d\n", dst->config());
467 return false;
468 }
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000469
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000470 SkPixelRef* pr = SkMallocPixelRef::NewAllocate(info, dst->rowBytes(), ctable);
reed@google.combf790232013-12-13 19:45:58 +0000471 if (NULL == pr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000472 return false;
473 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000474
reed@google.com672588b2014-01-08 15:42:01 +0000475 dst->setPixelRef(pr)->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000476 // since we're already allocated, we lockPixels right away
477 dst->lockPixels();
478 return true;
479}
480
481///////////////////////////////////////////////////////////////////////////////
482
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000483bool SkBitmap::copyPixelsTo(void* const dst, size_t dstSize,
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000484 size_t dstRowBytes, bool preserveDstPad) const {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000485
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000486 if (0 == dstRowBytes) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000487 dstRowBytes = fRowBytes;
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000488 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000489
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000490 if (dstRowBytes < fInfo.minRowBytes() ||
491 dst == NULL || (getPixels() == NULL && pixelRef() == NULL)) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000492 return false;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000493 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000494
bsalomon@google.comc6980972011-11-02 19:57:21 +0000495 if (!preserveDstPad && static_cast<uint32_t>(dstRowBytes) == fRowBytes) {
reed@google.com44699382013-10-31 17:28:30 +0000496 size_t safeSize = this->getSafeSize();
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000497 if (safeSize > dstSize || safeSize == 0)
498 return false;
499 else {
500 SkAutoLockPixels lock(*this);
501 // This implementation will write bytes beyond the end of each row,
502 // excluding the last row, if the bitmap's stride is greater than
503 // strictly required by the current config.
504 memcpy(dst, getPixels(), safeSize);
505
506 return true;
507 }
508 } else {
509 // If destination has different stride than us, then copy line by line.
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000510 if (fInfo.getSafeSize(dstRowBytes) > dstSize) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000511 return false;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000512 } else {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000513 // Just copy what we need on each line.
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000514 size_t rowBytes = fInfo.minRowBytes();
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000515 SkAutoLockPixels lock(*this);
516 const uint8_t* srcP = reinterpret_cast<const uint8_t*>(getPixels());
517 uint8_t* dstP = reinterpret_cast<uint8_t*>(dst);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000518 for (int row = 0; row < fInfo.fHeight;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000519 row++, srcP += fRowBytes, dstP += dstRowBytes) {
520 memcpy(dstP, srcP, rowBytes);
521 }
522
523 return true;
524 }
525 }
526}
527
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000528///////////////////////////////////////////////////////////////////////////////
529
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000530bool SkBitmap::isImmutable() const {
junov@chromium.orgb0521292011-12-15 20:14:06 +0000531 return fPixelRef ? fPixelRef->isImmutable() :
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000532 fFlags & kImageIsImmutable_Flag;
junov@chromium.orgb0521292011-12-15 20:14:06 +0000533}
534
535void SkBitmap::setImmutable() {
536 if (fPixelRef) {
537 fPixelRef->setImmutable();
538 } else {
539 fFlags |= kImageIsImmutable_Flag;
540 }
541}
542
junov@google.com4ee7ae52011-06-30 17:30:49 +0000543bool SkBitmap::isVolatile() const {
544 return (fFlags & kImageIsVolatile_Flag) != 0;
545}
546
547void SkBitmap::setIsVolatile(bool isVolatile) {
548 if (isVolatile) {
549 fFlags |= kImageIsVolatile_Flag;
550 } else {
551 fFlags &= ~kImageIsVolatile_Flag;
552 }
553}
554
reed@android.com8a1c16f2008-12-17 15:59:43 +0000555void* SkBitmap::getAddr(int x, int y) const {
556 SkASSERT((unsigned)x < (unsigned)this->width());
557 SkASSERT((unsigned)y < (unsigned)this->height());
558
559 char* base = (char*)this->getPixels();
560 if (base) {
561 base += y * this->rowBytes();
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000562 switch (this->colorType()) {
563 case kRGBA_8888_SkColorType:
564 case kBGRA_8888_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000565 base += x << 2;
566 break;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000567 case kARGB_4444_SkColorType:
568 case kRGB_565_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000569 base += x << 1;
570 break;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000571 case kAlpha_8_SkColorType:
572 case kIndex_8_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000573 base += x;
574 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000575 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000576 SkDEBUGFAIL("Can't return addr for config");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000577 base = NULL;
578 break;
579 }
580 }
581 return base;
582}
583
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000584SkColor SkBitmap::getColor(int x, int y) const {
585 SkASSERT((unsigned)x < (unsigned)this->width());
586 SkASSERT((unsigned)y < (unsigned)this->height());
587
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000588 switch (this->colorType()) {
589 case kAlpha_8_SkColorType: {
reed@google.com3b521d02011-04-29 11:53:41 +0000590 uint8_t* addr = this->getAddr8(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000591 return SkColorSetA(0, addr[0]);
592 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000593 case kIndex_8_SkColorType: {
reed@google.com3b521d02011-04-29 11:53:41 +0000594 SkPMColor c = this->getIndex8Color(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000595 return SkUnPreMultiply::PMColorToColor(c);
596 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000597 case kRGB_565_SkColorType: {
reed@google.com3b521d02011-04-29 11:53:41 +0000598 uint16_t* addr = this->getAddr16(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000599 return SkPixel16ToColor(addr[0]);
600 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000601 case kARGB_4444_SkColorType: {
reed@google.com3b521d02011-04-29 11:53:41 +0000602 uint16_t* addr = this->getAddr16(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000603 SkPMColor c = SkPixel4444ToPixel32(addr[0]);
604 return SkUnPreMultiply::PMColorToColor(c);
605 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000606 case kBGRA_8888_SkColorType:
607 case kRGBA_8888_SkColorType: {
reed@google.com3b521d02011-04-29 11:53:41 +0000608 uint32_t* addr = this->getAddr32(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000609 return SkUnPreMultiply::PMColorToColor(addr[0]);
610 }
rmistry@google.comd6bab022013-12-02 13:50:38 +0000611 default:
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000612 SkASSERT(false);
613 return 0;
614 }
615 SkASSERT(false); // Not reached.
616 return 0;
617}
618
reed@google.com2a7579d2012-11-07 18:30:18 +0000619bool SkBitmap::ComputeIsOpaque(const SkBitmap& bm) {
620 SkAutoLockPixels alp(bm);
621 if (!bm.getPixels()) {
622 return false;
623 }
624
625 const int height = bm.height();
626 const int width = bm.width();
627
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000628 switch (bm.colorType()) {
629 case kAlpha_8_SkColorType: {
reed@google.com2a7579d2012-11-07 18:30:18 +0000630 unsigned a = 0xFF;
631 for (int y = 0; y < height; ++y) {
632 const uint8_t* row = bm.getAddr8(0, y);
633 for (int x = 0; x < width; ++x) {
634 a &= row[x];
635 }
636 if (0xFF != a) {
637 return false;
638 }
639 }
640 return true;
641 } break;
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000642 case kIndex_8_SkColorType: {
reed@google.com2a7579d2012-11-07 18:30:18 +0000643 SkAutoLockColors alc(bm);
644 const SkPMColor* table = alc.colors();
645 if (!table) {
646 return false;
647 }
reed@google.com140d7282013-01-07 20:25:04 +0000648 SkPMColor c = (SkPMColor)~0;
reed@google.com2a7579d2012-11-07 18:30:18 +0000649 for (int i = bm.getColorTable()->count() - 1; i >= 0; --i) {
650 c &= table[i];
651 }
652 return 0xFF == SkGetPackedA32(c);
653 } break;
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000654 case kRGB_565_SkColorType:
reed@google.com2a7579d2012-11-07 18:30:18 +0000655 return true;
656 break;
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000657 case kARGB_4444_SkColorType: {
reed@google.com2a7579d2012-11-07 18:30:18 +0000658 unsigned c = 0xFFFF;
659 for (int y = 0; y < height; ++y) {
660 const SkPMColor16* row = bm.getAddr16(0, y);
661 for (int x = 0; x < width; ++x) {
662 c &= row[x];
663 }
664 if (0xF != SkGetPackedA4444(c)) {
665 return false;
666 }
667 }
668 return true;
669 } break;
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000670 case kBGRA_8888_SkColorType:
671 case kRGBA_8888_SkColorType: {
reed@google.com140d7282013-01-07 20:25:04 +0000672 SkPMColor c = (SkPMColor)~0;
reed@google.com2a7579d2012-11-07 18:30:18 +0000673 for (int y = 0; y < height; ++y) {
674 const SkPMColor* row = bm.getAddr32(0, y);
675 for (int x = 0; x < width; ++x) {
676 c &= row[x];
677 }
678 if (0xFF != SkGetPackedA32(c)) {
679 return false;
680 }
681 }
682 return true;
683 }
684 default:
685 break;
686 }
687 return false;
688}
689
690
reed@android.com8a1c16f2008-12-17 15:59:43 +0000691///////////////////////////////////////////////////////////////////////////////
692///////////////////////////////////////////////////////////////////////////////
693
reed@google.com45f746f2013-06-21 19:51:31 +0000694static uint16_t pack_8888_to_4444(unsigned a, unsigned r, unsigned g, unsigned b) {
695 unsigned pixel = (SkA32To4444(a) << SK_A4444_SHIFT) |
696 (SkR32To4444(r) << SK_R4444_SHIFT) |
697 (SkG32To4444(g) << SK_G4444_SHIFT) |
698 (SkB32To4444(b) << SK_B4444_SHIFT);
699 return SkToU16(pixel);
700}
701
reed@google.com60d32352013-06-28 19:40:50 +0000702void SkBitmap::internalErase(const SkIRect& area,
703 U8CPU a, U8CPU r, U8CPU g, U8CPU b) const {
704#ifdef SK_DEBUG
reed@android.com8a1c16f2008-12-17 15:59:43 +0000705 SkDEBUGCODE(this->validate();)
reed@google.com60d32352013-06-28 19:40:50 +0000706 SkASSERT(!area.isEmpty());
707 {
reed@google.com92833f92013-06-28 19:47:43 +0000708 SkIRect total = { 0, 0, this->width(), this->height() };
reed@google.com60d32352013-06-28 19:40:50 +0000709 SkASSERT(total.contains(area));
710 }
711#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000712
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000713 switch (fInfo.colorType()) {
714 case kUnknown_SkColorType:
715 case kIndex_8_SkColorType:
commit-bot@chromium.org7669a772014-03-27 15:30:35 +0000716 return; // can't erase. Should we bzero so the memory is not uninitialized?
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000717 default:
718 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000719 }
720
721 SkAutoLockPixels alp(*this);
722 // perform this check after the lock call
723 if (!this->readyToDraw()) {
724 return;
725 }
726
reed@google.com60d32352013-06-28 19:40:50 +0000727 int height = area.height();
728 const int width = area.width();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000729 const int rowBytes = fRowBytes;
730
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000731 switch (this->colorType()) {
732 case kAlpha_8_SkColorType: {
reed@google.com60d32352013-06-28 19:40:50 +0000733 uint8_t* p = this->getAddr8(area.fLeft, area.fTop);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000734 while (--height >= 0) {
735 memset(p, a, width);
736 p += rowBytes;
737 }
738 break;
739 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000740 case kARGB_4444_SkColorType:
741 case kRGB_565_SkColorType: {
reed@google.com60d32352013-06-28 19:40:50 +0000742 uint16_t* p = this->getAddr16(area.fLeft, area.fTop);;
reed@google.com45f746f2013-06-21 19:51:31 +0000743 uint16_t v;
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000744
commit-bot@chromium.org7669a772014-03-27 15:30:35 +0000745 // make rgb premultiplied
746 if (255 != a) {
747 r = SkAlphaMul(r, a);
748 g = SkAlphaMul(g, a);
749 b = SkAlphaMul(b, a);
750 }
751
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000752 if (kARGB_4444_SkColorType == this->colorType()) {
reed@google.com45f746f2013-06-21 19:51:31 +0000753 v = pack_8888_to_4444(a, r, g, b);
754 } else {
755 v = SkPackRGB16(r >> (8 - SK_R16_BITS),
756 g >> (8 - SK_G16_BITS),
757 b >> (8 - SK_B16_BITS));
758 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000759 while (--height >= 0) {
760 sk_memset16(p, v, width);
761 p = (uint16_t*)((char*)p + rowBytes);
762 }
763 break;
764 }
commit-bot@chromium.org7669a772014-03-27 15:30:35 +0000765 case kBGRA_8888_SkColorType:
766 case kRGBA_8888_SkColorType: {
reed@google.com60d32352013-06-28 19:40:50 +0000767 uint32_t* p = this->getAddr32(area.fLeft, area.fTop);
commit-bot@chromium.org7669a772014-03-27 15:30:35 +0000768
769 if (255 != a && kPremul_SkAlphaType == this->alphaType()) {
770 r = SkAlphaMul(r, a);
771 g = SkAlphaMul(g, a);
772 b = SkAlphaMul(b, a);
773 }
774 uint32_t v = kRGBA_8888_SkColorType == this->colorType() ?
775 SkPackARGB_as_RGBA(a, r, g, b) : SkPackARGB_as_BGRA(a, r, g, b);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000776
777 while (--height >= 0) {
778 sk_memset32(p, v, width);
779 p = (uint32_t*)((char*)p + rowBytes);
780 }
781 break;
782 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000783 default:
784 return; // no change, so don't call notifyPixelsChanged()
reed@android.com8a1c16f2008-12-17 15:59:43 +0000785 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000786
reed@android.com8a1c16f2008-12-17 15:59:43 +0000787 this->notifyPixelsChanged();
788}
789
reed@google.com60d32352013-06-28 19:40:50 +0000790void SkBitmap::eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const {
reed@google.com92833f92013-06-28 19:47:43 +0000791 SkIRect area = { 0, 0, this->width(), this->height() };
reed@google.com60d32352013-06-28 19:40:50 +0000792 if (!area.isEmpty()) {
793 this->internalErase(area, a, r, g, b);
794 }
795}
796
797void SkBitmap::eraseArea(const SkIRect& rect, SkColor c) const {
reed@google.com92833f92013-06-28 19:47:43 +0000798 SkIRect area = { 0, 0, this->width(), this->height() };
reed@google.com60d32352013-06-28 19:40:50 +0000799 if (area.intersect(rect)) {
800 this->internalErase(area, SkColorGetA(c), SkColorGetR(c),
801 SkColorGetG(c), SkColorGetB(c));
802 }
803}
804
reed@android.com8a1c16f2008-12-17 15:59:43 +0000805//////////////////////////////////////////////////////////////////////////////////////
806//////////////////////////////////////////////////////////////////////////////////////
807
reed@android.com8a1c16f2008-12-17 15:59:43 +0000808bool SkBitmap::extractSubset(SkBitmap* result, const SkIRect& subset) const {
809 SkDEBUGCODE(this->validate();)
810
djsollen@google.comc84b8332012-07-27 13:41:44 +0000811 if (NULL == result || NULL == fPixelRef) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000812 return false; // no src pixels
813 }
814
815 SkIRect srcRect, r;
816 srcRect.set(0, 0, this->width(), this->height());
817 if (!r.intersect(srcRect, subset)) {
818 return false; // r is empty (i.e. no intersection)
819 }
820
scroggo@google.coma2a31922012-12-07 19:14:45 +0000821 if (fPixelRef->getTexture() != NULL) {
822 // Do a deep copy
commit-bot@chromium.orgeeef0cc2014-05-21 15:58:00 +0000823 SkPixelRef* pixelRef = fPixelRef->deepCopy(this->config(), &subset);
scroggo@google.coma2a31922012-12-07 19:14:45 +0000824 if (pixelRef != NULL) {
825 SkBitmap dst;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000826 dst.setInfo(SkImageInfo::Make(subset.width(), subset.height(),
827 this->colorType(), this->alphaType()));
scroggo@google.coma2a31922012-12-07 19:14:45 +0000828 dst.setIsVolatile(this->isVolatile());
scroggo@google.coma2a31922012-12-07 19:14:45 +0000829 dst.setPixelRef(pixelRef)->unref();
830 SkDEBUGCODE(dst.validate());
831 result->swap(dst);
832 return true;
833 }
834 }
835
scroggo@google.coma2a31922012-12-07 19:14:45 +0000836 // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have
837 // exited above.
838 SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width()));
839 SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height()));
840
reed@android.com8a1c16f2008-12-17 15:59:43 +0000841 SkBitmap dst;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000842 dst.setInfo(SkImageInfo::Make(r.width(), r.height(), this->colorType(), this->alphaType()),
843 this->rowBytes());
skyostil@google.com0eb75762012-01-16 10:45:53 +0000844 dst.setIsVolatile(this->isVolatile());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000845
846 if (fPixelRef) {
reed@google.com672588b2014-01-08 15:42:01 +0000847 SkIPoint origin = fPixelRefOrigin;
848 origin.fX += r.fLeft;
849 origin.fY += r.fTop;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000850 // share the pixelref with a custom offset
reed@google.com672588b2014-01-08 15:42:01 +0000851 dst.setPixelRef(fPixelRef, origin);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000852 }
853 SkDEBUGCODE(dst.validate();)
854
855 // we know we're good, so commit to result
856 result->swap(dst);
857 return true;
858}
859
860///////////////////////////////////////////////////////////////////////////////
861
862#include "SkCanvas.h"
863#include "SkPaint.h"
864
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000865bool SkBitmap::canCopyTo(SkColorType dstColorType) const {
866 if (this->colorType() == kUnknown_SkColorType) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000867 return false;
868 }
869
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000870 bool sameConfigs = (this->colorType() == dstColorType);
871 switch (dstColorType) {
872 case kAlpha_8_SkColorType:
873 case kRGB_565_SkColorType:
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000874 case kRGBA_8888_SkColorType:
875 case kBGRA_8888_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000876 break;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000877 case kIndex_8_SkColorType:
weita@google.comf9ab99a2009-05-03 18:23:30 +0000878 if (!sameConfigs) {
879 return false;
880 }
881 break;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000882 case kARGB_4444_SkColorType:
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000883 return sameConfigs || kN32_SkColorType == this->colorType();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000884 default:
885 return false;
886 }
reed@android.comfbaa88d2009-05-06 17:44:34 +0000887 return true;
888}
889
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000890bool SkBitmap::copyTo(SkBitmap* dst, SkColorType dstColorType,
891 Allocator* alloc) const {
892 if (!this->canCopyTo(dstColorType)) {
reed@android.comfbaa88d2009-05-06 17:44:34 +0000893 return false;
894 }
895
reed@google.com50dfa012011-04-01 19:05:36 +0000896 // if we have a texture, first get those pixels
897 SkBitmap tmpSrc;
898 const SkBitmap* src = this;
899
scroggo@google.coma2a31922012-12-07 19:14:45 +0000900 if (fPixelRef) {
901 SkIRect subset;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000902 subset.setXYWH(fPixelRefOrigin.fX, fPixelRefOrigin.fY,
903 fInfo.width(), fInfo.height());
reed@google.com672588b2014-01-08 15:42:01 +0000904 if (fPixelRef->readPixels(&tmpSrc, &subset)) {
905 SkASSERT(tmpSrc.width() == this->width());
906 SkASSERT(tmpSrc.height() == this->height());
reed@google.com50dfa012011-04-01 19:05:36 +0000907
reed@google.com672588b2014-01-08 15:42:01 +0000908 // did we get lucky and we can just return tmpSrc?
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000909 if (tmpSrc.colorType() == dstColorType && NULL == alloc) {
reed@google.com672588b2014-01-08 15:42:01 +0000910 dst->swap(tmpSrc);
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000911 // If the result is an exact copy, clone the gen ID.
912 if (dst->pixelRef() && dst->pixelRef()->info() == fPixelRef->info()) {
reed@google.com672588b2014-01-08 15:42:01 +0000913 dst->pixelRef()->cloneGenID(*fPixelRef);
scroggo@google.coma2a31922012-12-07 19:14:45 +0000914 }
reed@google.com672588b2014-01-08 15:42:01 +0000915 return true;
scroggo@google.comd5764e82012-08-22 15:00:05 +0000916 }
reed@google.com672588b2014-01-08 15:42:01 +0000917
918 // fall through to the raster case
919 src = &tmpSrc;
reed@google.com50dfa012011-04-01 19:05:36 +0000920 }
reed@android.comfbaa88d2009-05-06 17:44:34 +0000921 }
reed@android.com311c82d2009-05-05 23:13:23 +0000922
reed@google.com50dfa012011-04-01 19:05:36 +0000923 // we lock this now, since we may need its colortable
924 SkAutoLockPixels srclock(*src);
925 if (!src->readyToDraw()) {
926 return false;
927 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000928
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000929 // The only way to be readyToDraw is if fPixelRef is non NULL.
930 SkASSERT(fPixelRef != NULL);
931
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000932 SkImageInfo dstInfo = src->info();
933 dstInfo.fColorType = dstColorType;
934
reed@google.com50dfa012011-04-01 19:05:36 +0000935 SkBitmap tmpDst;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000936 if (!tmpDst.setInfo(dstInfo)) {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000937 return false;
938 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000939
weita@google.comf9ab99a2009-05-03 18:23:30 +0000940 // allocate colortable if srcConfig == kIndex8_Config
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000941 SkAutoTUnref<SkColorTable> ctable;
942 if (dstColorType == kIndex_8_SkColorType) {
943 // TODO: can we just ref() the src colortable? Is it reentrant-safe?
944 ctable.reset(SkNEW_ARGS(SkColorTable, (*src->getColorTable())));
945 }
reed@google.com50dfa012011-04-01 19:05:36 +0000946 if (!tmpDst.allocPixels(alloc, ctable)) {
weita@google.comf9ab99a2009-05-03 18:23:30 +0000947 return false;
948 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000949
reed@google.com50dfa012011-04-01 19:05:36 +0000950 if (!tmpDst.readyToDraw()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000951 // allocator/lock failed
952 return false;
953 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000954
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000955 // pixelRef must be non NULL or tmpDst.readyToDraw() would have
956 // returned false.
957 SkASSERT(tmpDst.pixelRef() != NULL);
958
reed@android.comfbaa88d2009-05-06 17:44:34 +0000959 /* do memcpy for the same configs cases, else use drawing
weita@google.comf9ab99a2009-05-03 18:23:30 +0000960 */
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000961 if (src->colorType() == dstColorType) {
reed@google.com50dfa012011-04-01 19:05:36 +0000962 if (tmpDst.getSize() == src->getSize()) {
963 memcpy(tmpDst.getPixels(), src->getPixels(), src->getSafeSize());
scroggo@google.comd5764e82012-08-22 15:00:05 +0000964 SkPixelRef* pixelRef = tmpDst.pixelRef();
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000965
966 // In order to reach this point, we know that the width, config and
967 // rowbytes of the SkPixelRefs are the same, but it is possible for
968 // the heights to differ, if this SkBitmap's height is a subset of
969 // fPixelRef. Only if the SkPixelRefs' heights match are we
970 // guaranteed that this is an exact copy, meaning we should clone
971 // the genID.
972 if (pixelRef->info().fHeight == fPixelRef->info().fHeight) {
973 // TODO: what to do if the two infos match, BUT
974 // fPixelRef is premul and pixelRef is opaque?
975 // skipping assert for now
976 // https://code.google.com/p/skia/issues/detail?id=2012
977// SkASSERT(pixelRef->info() == fPixelRef->info());
978 SkASSERT(pixelRef->info().fWidth == fPixelRef->info().fWidth);
979 SkASSERT(pixelRef->info().fColorType == fPixelRef->info().fColorType);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000980 pixelRef->cloneGenID(*fPixelRef);
scroggo@google.comd5764e82012-08-22 15:00:05 +0000981 }
reed@android.com311c82d2009-05-05 23:13:23 +0000982 } else {
reed@google.com50dfa012011-04-01 19:05:36 +0000983 const char* srcP = reinterpret_cast<const char*>(src->getPixels());
984 char* dstP = reinterpret_cast<char*>(tmpDst.getPixels());
reed@android.com311c82d2009-05-05 23:13:23 +0000985 // to be sure we don't read too much, only copy our logical pixels
reed@google.com50dfa012011-04-01 19:05:36 +0000986 size_t bytesToCopy = tmpDst.width() * tmpDst.bytesPerPixel();
987 for (int y = 0; y < tmpDst.height(); y++) {
reed@android.com311c82d2009-05-05 23:13:23 +0000988 memcpy(dstP, srcP, bytesToCopy);
reed@google.com50dfa012011-04-01 19:05:36 +0000989 srcP += src->rowBytes();
990 dstP += tmpDst.rowBytes();
reed@android.com311c82d2009-05-05 23:13:23 +0000991 }
992 }
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000993 } else if (kARGB_4444_SkColorType == dstColorType
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000994 && kN32_SkColorType == src->colorType()) {
scroggo@google.com8dc8bc52013-08-07 19:16:05 +0000995 SkASSERT(src->height() == tmpDst.height());
996 SkASSERT(src->width() == tmpDst.width());
997 for (int y = 0; y < src->height(); ++y) {
998 SkPMColor16* SK_RESTRICT dstRow = (SkPMColor16*) tmpDst.getAddr16(0, y);
999 SkPMColor* SK_RESTRICT srcRow = (SkPMColor*) src->getAddr32(0, y);
1000 DITHER_4444_SCAN(y);
1001 for (int x = 0; x < src->width(); ++x) {
1002 dstRow[x] = SkDitherARGB32To4444(srcRow[x],
1003 DITHER_VALUE(x));
1004 }
1005 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001006 } else {
robertphillips@google.com0197b322013-10-10 15:48:16 +00001007 // Always clear the dest in case one of the blitters accesses it
1008 // TODO: switch the allocation of tmpDst to call sk_calloc_throw
1009 tmpDst.eraseColor(SK_ColorTRANSPARENT);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001010
reed@google.com50dfa012011-04-01 19:05:36 +00001011 SkCanvas canvas(tmpDst);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001012 SkPaint paint;
1013
1014 paint.setDither(true);
reed@google.com50dfa012011-04-01 19:05:36 +00001015 canvas.drawBitmap(*src, 0, 0, &paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001016 }
1017
reed@google.com50dfa012011-04-01 19:05:36 +00001018 dst->swap(tmpDst);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001019 return true;
1020}
1021
commit-bot@chromium.orgfab349c2014-03-05 02:34:58 +00001022bool SkBitmap::deepCopyTo(SkBitmap* dst) const {
commit-bot@chromium.orgeeef0cc2014-05-21 15:58:00 +00001023 const SkBitmap::Config dstConfig = this->config();
1024 const SkColorType dstCT = SkBitmapConfigToColorType(dstConfig);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001025
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +00001026 if (!this->canCopyTo(dstCT)) {
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001027 return false;
1028 }
1029
1030 // If we have a PixelRef, and it supports deep copy, use it.
1031 // Currently supported only by texture-backed bitmaps.
1032 if (fPixelRef) {
commit-bot@chromium.orgeeef0cc2014-05-21 15:58:00 +00001033 SkPixelRef* pixelRef = fPixelRef->deepCopy(dstConfig);
1034 if (pixelRef) {
1035 uint32_t rowBytes;
1036 if (this->colorType() == dstCT) {
1037 // Since there is no subset to pass to deepCopy, and deepCopy
1038 // succeeded, the new pixel ref must be identical.
1039 SkASSERT(fPixelRef->info() == pixelRef->info());
1040 pixelRef->cloneGenID(*fPixelRef);
1041 // Use the same rowBytes as the original.
1042 rowBytes = fRowBytes;
1043 } else {
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001044 // With the new config, an appropriate fRowBytes will be computed by setInfo.
commit-bot@chromium.orgeeef0cc2014-05-21 15:58:00 +00001045 rowBytes = 0;
1046 }
1047
1048 SkImageInfo info = fInfo;
1049 info.fColorType = dstCT;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001050 if (!dst->setInfo(info, rowBytes)) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001051 return false;
1052 }
commit-bot@chromium.orgeeef0cc2014-05-21 15:58:00 +00001053 dst->setPixelRef(pixelRef, fPixelRefOrigin)->unref();
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001054 return true;
1055 }
1056 }
1057
1058 if (this->getTexture()) {
1059 return false;
1060 } else {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +00001061 return this->copyTo(dst, dstCT, NULL);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001062 }
1063}
1064
reed@android.com8a1c16f2008-12-17 15:59:43 +00001065///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +00001066
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +00001067static bool GetBitmapAlpha(const SkBitmap& src, uint8_t* SK_RESTRICT alpha,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001068 int alphaRowBytes) {
1069 SkASSERT(alpha != NULL);
1070 SkASSERT(alphaRowBytes >= src.width());
1071
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001072 SkColorType colorType = src.colorType();
1073 int w = src.width();
1074 int h = src.height();
1075 size_t rb = src.rowBytes();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001076
reed@android.com1cdcb512009-08-24 19:11:00 +00001077 SkAutoLockPixels alp(src);
1078 if (!src.readyToDraw()) {
1079 // zero out the alpha buffer and return
1080 while (--h >= 0) {
1081 memset(alpha, 0, w);
1082 alpha += alphaRowBytes;
1083 }
1084 return false;
1085 }
reed@google.com82065d62011-02-07 15:30:46 +00001086
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001087 if (kAlpha_8_SkColorType == colorType && !src.isOpaque()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001088 const uint8_t* s = src.getAddr8(0, 0);
1089 while (--h >= 0) {
1090 memcpy(alpha, s, w);
1091 s += rb;
1092 alpha += alphaRowBytes;
1093 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001094 } else if (kN32_SkColorType == colorType && !src.isOpaque()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001095 const SkPMColor* SK_RESTRICT s = src.getAddr32(0, 0);
1096 while (--h >= 0) {
1097 for (int x = 0; x < w; x++) {
1098 alpha[x] = SkGetPackedA32(s[x]);
1099 }
1100 s = (const SkPMColor*)((const char*)s + rb);
1101 alpha += alphaRowBytes;
1102 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001103 } else if (kARGB_4444_SkColorType == colorType && !src.isOpaque()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001104 const SkPMColor16* SK_RESTRICT s = src.getAddr16(0, 0);
1105 while (--h >= 0) {
1106 for (int x = 0; x < w; x++) {
1107 alpha[x] = SkPacked4444ToA32(s[x]);
1108 }
1109 s = (const SkPMColor16*)((const char*)s + rb);
1110 alpha += alphaRowBytes;
1111 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001112 } else if (kIndex_8_SkColorType == colorType && !src.isOpaque()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001113 SkColorTable* ct = src.getColorTable();
1114 if (ct) {
1115 const SkPMColor* SK_RESTRICT table = ct->lockColors();
1116 const uint8_t* SK_RESTRICT s = src.getAddr8(0, 0);
1117 while (--h >= 0) {
1118 for (int x = 0; x < w; x++) {
1119 alpha[x] = SkGetPackedA32(table[s[x]]);
1120 }
1121 s += rb;
1122 alpha += alphaRowBytes;
1123 }
reed@google.com0a6151d2013-10-10 14:44:56 +00001124 ct->unlockColors();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001125 }
1126 } else { // src is opaque, so just fill alpha[] with 0xFF
1127 memset(alpha, 0xFF, h * alphaRowBytes);
1128 }
reed@android.com1cdcb512009-08-24 19:11:00 +00001129 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001130}
1131
1132#include "SkPaint.h"
1133#include "SkMaskFilter.h"
1134#include "SkMatrix.h"
1135
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001136bool SkBitmap::extractAlpha(SkBitmap* dst, const SkPaint* paint,
djsollen@google.com57f49692011-02-23 20:46:31 +00001137 Allocator *allocator, SkIPoint* offset) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001138 SkDEBUGCODE(this->validate();)
1139
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001140 SkBitmap tmpBitmap;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001141 SkMatrix identity;
1142 SkMask srcM, dstM;
1143
1144 srcM.fBounds.set(0, 0, this->width(), this->height());
1145 srcM.fRowBytes = SkAlign4(this->width());
1146 srcM.fFormat = SkMask::kA8_Format;
1147
1148 SkMaskFilter* filter = paint ? paint->getMaskFilter() : NULL;
1149
1150 // compute our (larger?) dst bounds if we have a filter
1151 if (NULL != filter) {
1152 identity.reset();
1153 srcM.fImage = NULL;
1154 if (!filter->filterMask(&dstM, srcM, identity, NULL)) {
1155 goto NO_FILTER_CASE;
1156 }
1157 dstM.fRowBytes = SkAlign4(dstM.fBounds.width());
1158 } else {
1159 NO_FILTER_CASE:
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001160 tmpBitmap.setInfo(SkImageInfo::MakeA8(this->width(), this->height()), srcM.fRowBytes);
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001161 if (!tmpBitmap.allocPixels(allocator, NULL)) {
1162 // Allocation of pixels for alpha bitmap failed.
1163 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
1164 tmpBitmap.width(), tmpBitmap.height());
1165 return false;
1166 }
1167 GetBitmapAlpha(*this, tmpBitmap.getAddr8(0, 0), srcM.fRowBytes);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001168 if (offset) {
1169 offset->set(0, 0);
1170 }
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001171 tmpBitmap.swap(*dst);
1172 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001173 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001174 srcM.fImage = SkMask::AllocImage(srcM.computeImageSize());
1175 SkAutoMaskFreeImage srcCleanup(srcM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001176
1177 GetBitmapAlpha(*this, srcM.fImage, srcM.fRowBytes);
1178 if (!filter->filterMask(&dstM, srcM, identity, NULL)) {
1179 goto NO_FILTER_CASE;
1180 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001181 SkAutoMaskFreeImage dstCleanup(dstM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001182
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001183 tmpBitmap.setInfo(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.height()),
1184 dstM.fRowBytes);
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001185 if (!tmpBitmap.allocPixels(allocator, NULL)) {
1186 // Allocation of pixels for alpha bitmap failed.
1187 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
1188 tmpBitmap.width(), tmpBitmap.height());
1189 return false;
1190 }
1191 memcpy(tmpBitmap.getPixels(), dstM.fImage, dstM.computeImageSize());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001192 if (offset) {
1193 offset->set(dstM.fBounds.fLeft, dstM.fBounds.fTop);
1194 }
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001195 SkDEBUGCODE(tmpBitmap.validate();)
1196
1197 tmpBitmap.swap(*dst);
1198 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001199}
1200
1201///////////////////////////////////////////////////////////////////////////////
1202
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001203void SkBitmap::WriteRawPixels(SkWriteBuffer* buffer, const SkBitmap& bitmap) {
1204 const SkImageInfo info = bitmap.info();
1205 SkAutoLockPixels alp(bitmap);
1206 if (0 == info.width() || 0 == info.height() || NULL == bitmap.getPixels()) {
1207 buffer->writeUInt(0); // instead of snugRB, signaling no pixels
1208 return;
1209 }
1210
1211 const size_t snugRB = info.width() * info.bytesPerPixel();
1212 const char* src = (const char*)bitmap.getPixels();
1213 const size_t ramRB = bitmap.rowBytes();
skia.committer@gmail.com3c134a92014-05-24 03:05:26 +00001214
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001215 buffer->write32(SkToU32(snugRB));
1216 info.flatten(*buffer);
1217
1218 const size_t size = snugRB * info.height();
1219 SkAutoMalloc storage(size);
1220 char* dst = (char*)storage.get();
1221 for (int y = 0; y < info.height(); ++y) {
1222 memcpy(dst, src, snugRB);
1223 dst += snugRB;
1224 src += ramRB;
1225 }
1226 buffer->writeByteArray(storage.get(), size);
1227
1228 SkColorTable* ct = bitmap.getColorTable();
1229 if (kIndex_8_SkColorType == info.colorType() && ct) {
1230 buffer->writeBool(true);
1231 ct->writeToBuffer(*buffer);
1232 } else {
1233 buffer->writeBool(false);
1234 }
1235}
1236
1237bool SkBitmap::ReadRawPixels(SkReadBuffer* buffer, SkBitmap* bitmap) {
1238 const size_t snugRB = buffer->readUInt();
1239 if (0 == snugRB) { // no pixels
1240 return false;
1241 }
1242
1243 SkImageInfo info;
1244 info.unflatten(*buffer);
1245
1246 const size_t ramRB = info.minRowBytes();
1247 const int height = info.height();
1248 const size_t snugSize = snugRB * height;
1249 const size_t ramSize = ramRB * height;
commit-bot@chromium.org05858432014-05-30 01:06:44 +00001250 if (!buffer->validate(snugSize <= ramSize)) {
1251 return false;
1252 }
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001253
1254 char* dst = (char*)sk_malloc_throw(ramSize);
1255 buffer->readByteArray(dst, snugSize);
1256 SkAutoDataUnref data(SkData::NewFromMalloc(dst, ramSize));
1257
1258 if (snugSize != ramSize) {
1259 const char* srcRow = dst + snugRB * (height - 1);
1260 char* dstRow = dst + ramRB * (height - 1);
1261 for (int y = height - 1; y >= 1; --y) {
1262 memmove(dstRow, srcRow, snugRB);
1263 srcRow -= snugRB;
1264 dstRow -= ramRB;
1265 }
1266 SkASSERT(srcRow == dstRow); // first row does not need to be moved
1267 }
skia.committer@gmail.com3c134a92014-05-24 03:05:26 +00001268
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001269 SkAutoTUnref<SkColorTable> ctable;
1270 if (buffer->readBool()) {
1271 ctable.reset(SkNEW_ARGS(SkColorTable, (*buffer)));
1272 }
skia.committer@gmail.com3c134a92014-05-24 03:05:26 +00001273
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001274 SkAutoTUnref<SkPixelRef> pr(SkMallocPixelRef::NewWithData(info, info.minRowBytes(),
1275 ctable.get(), data.get()));
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001276 bitmap->setInfo(pr->info());
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001277 bitmap->setPixelRef(pr, 0, 0);
1278 return true;
1279}
1280
reed@android.com8a1c16f2008-12-17 15:59:43 +00001281enum {
1282 SERIALIZE_PIXELTYPE_NONE,
djsollen@google.com21830d92012-08-07 19:49:41 +00001283 SERIALIZE_PIXELTYPE_REF_DATA
reed@android.com8a1c16f2008-12-17 15:59:43 +00001284};
1285
commit-bot@chromium.org851155c2014-05-27 14:03:51 +00001286void SkBitmap::legacyUnflatten(SkReadBuffer& buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001287 this->reset();
weita@google.comf9ab99a2009-05-03 18:23:30 +00001288
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001289 SkImageInfo info;
1290 info.unflatten(buffer);
1291 size_t rowBytes = buffer.readInt();
commit-bot@chromium.org33fed142014-02-13 18:46:13 +00001292 if (!buffer.validate((info.width() >= 0) && (info.height() >= 0) &&
1293 SkColorTypeIsValid(info.fColorType) &&
1294 SkAlphaTypeIsValid(info.fAlphaType) &&
1295 validate_alphaType(info.fColorType, info.fAlphaType) &&
1296 info.validRowBytes(rowBytes))) {
1297 return;
1298 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001299
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001300 bool configIsValid = this->setInfo(info, rowBytes);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001301 buffer.validate(configIsValid);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001302
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001303 int reftype = buffer.readInt();
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +00001304 if (buffer.validate((SERIALIZE_PIXELTYPE_REF_DATA == reftype) ||
1305 (SERIALIZE_PIXELTYPE_NONE == reftype))) {
1306 switch (reftype) {
1307 case SERIALIZE_PIXELTYPE_REF_DATA: {
reed@google.com672588b2014-01-08 15:42:01 +00001308 SkIPoint origin;
1309 origin.fX = buffer.readInt();
1310 origin.fY = buffer.readInt();
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001311 size_t offset = origin.fY * rowBytes + origin.fX * info.bytesPerPixel();
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +00001312 SkPixelRef* pr = buffer.readPixelRef();
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +00001313 if (!buffer.validate((NULL == pr) ||
1314 (pr->getAllocatedSizeInBytes() >= (offset + this->getSafeSize())))) {
reed@google.com672588b2014-01-08 15:42:01 +00001315 origin.setZero();
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +00001316 }
reed@google.com672588b2014-01-08 15:42:01 +00001317 SkSafeUnref(this->setPixelRef(pr, origin));
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +00001318 break;
1319 }
1320 case SERIALIZE_PIXELTYPE_NONE:
1321 break;
1322 default:
1323 SkDEBUGFAIL("unrecognized pixeltype in serialized data");
1324 sk_throw();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001325 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001326 }
1327}
1328
1329///////////////////////////////////////////////////////////////////////////////
1330
1331SkBitmap::RLEPixels::RLEPixels(int width, int height) {
1332 fHeight = height;
commit-bot@chromium.org235002f2013-10-09 18:39:59 +00001333 fYPtrs = (uint8_t**)sk_calloc_throw(height * sizeof(uint8_t*));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001334}
1335
1336SkBitmap::RLEPixels::~RLEPixels() {
1337 sk_free(fYPtrs);
1338}
1339
1340///////////////////////////////////////////////////////////////////////////////
1341
1342#ifdef SK_DEBUG
1343void SkBitmap::validate() const {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001344 fInfo.validate();
commit-bot@chromium.orgd5414e52014-02-13 22:30:38 +00001345
1346 // ImageInfo may not require this, but Bitmap ensures that opaque-only
1347 // colorTypes report opaque for their alphatype
1348 if (kRGB_565_SkColorType == fInfo.colorType()) {
1349 SkASSERT(kOpaque_SkAlphaType == fInfo.alphaType());
1350 }
1351
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001352 SkASSERT(fInfo.validRowBytes(fRowBytes));
scroggo@google.com8e990eb2013-06-14 15:55:56 +00001353 uint8_t allFlags = kImageIsOpaque_Flag | kImageIsVolatile_Flag | kImageIsImmutable_Flag;
1354#ifdef SK_BUILD_FOR_ANDROID
1355 allFlags |= kHasHardwareMipMap_Flag;
1356#endif
1357 SkASSERT(fFlags <= allFlags);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001358 SkASSERT(fPixelLockCount >= 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001359
reed@google.com615316c2014-01-15 19:15:23 +00001360 if (fPixels) {
1361 SkASSERT(fPixelRef);
1362 SkASSERT(fPixelLockCount > 0);
1363 SkASSERT(fPixelRef->isLocked());
1364 SkASSERT(fPixelRef->rowBytes() == fRowBytes);
1365 SkASSERT(fPixelRefOrigin.fX >= 0);
1366 SkASSERT(fPixelRefOrigin.fY >= 0);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001367 SkASSERT(fPixelRef->info().width() >= (int)this->width() + fPixelRefOrigin.fX);
1368 SkASSERT(fPixelRef->info().fHeight >= (int)this->height() + fPixelRefOrigin.fY);
1369 SkASSERT(fPixelRef->rowBytes() >= fInfo.minRowBytes());
reed@google.com615316c2014-01-15 19:15:23 +00001370 } else {
1371 SkASSERT(NULL == fColorTable);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001372 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001373}
1374#endif
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001375
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +00001376#ifndef SK_IGNORE_TO_STRING
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001377void SkBitmap::toString(SkString* str) const {
1378
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001379 static const char* gColorTypeNames[kLastEnum_SkColorType + 1] = {
1380 "UNKNOWN", "A8", "565", "4444", "RGBA", "BGRA", "INDEX8",
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001381 };
1382
1383 str->appendf("bitmap: ((%d, %d) %s", this->width(), this->height(),
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001384 gColorTypeNames[this->colorType()]);
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001385
1386 str->append(" (");
1387 if (this->isOpaque()) {
1388 str->append("opaque");
1389 } else {
1390 str->append("transparent");
1391 }
1392 if (this->isImmutable()) {
1393 str->append(", immutable");
1394 } else {
1395 str->append(", not-immutable");
1396 }
1397 str->append(")");
1398
1399 SkPixelRef* pr = this->pixelRef();
1400 if (NULL == pr) {
1401 // show null or the explicit pixel address (rare)
1402 str->appendf(" pixels:%p", this->getPixels());
1403 } else {
1404 const char* uri = pr->getURI();
1405 if (NULL != uri) {
1406 str->appendf(" uri:\"%s\"", uri);
1407 } else {
1408 str->appendf(" pixelref:%p", pr);
1409 }
1410 }
1411
1412 str->append(")");
1413}
1414#endif
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001415
1416///////////////////////////////////////////////////////////////////////////////
1417
1418#ifdef SK_DEBUG
1419void SkImageInfo::validate() const {
1420 SkASSERT(fWidth >= 0);
1421 SkASSERT(fHeight >= 0);
1422 SkASSERT(SkColorTypeIsValid(fColorType));
1423 SkASSERT(SkAlphaTypeIsValid(fAlphaType));
1424}
1425#endif