blob: 32cb7b25cd57360cd94434a1e0dbc5de0d0a930c [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2008 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkBitmap.h"
11#include "SkColorPriv.h"
12#include "SkDither.h"
13#include "SkFlattenable.h"
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000014#include "SkImagePriv.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkMallocPixelRef.h"
16#include "SkMask.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000017#include "SkReadBuffer.h"
18#include "SkWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000019#include "SkPixelRef.h"
20#include "SkThread.h"
vandebo@chromium.org112706d2011-02-24 22:50:55 +000021#include "SkUnPreMultiply.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000022#include "SkUtils.h"
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +000023#include "SkValidationUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000024#include "SkPackBits.h"
25#include <new>
26
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +000027static bool reset_return_false(SkBitmap* bm) {
28 bm->reset();
29 return false;
30}
31
reed@android.com8a1c16f2008-12-17 15:59:43 +000032struct MipLevel {
33 void* fPixels;
34 uint32_t fRowBytes;
reed@android.comf459a492009-03-27 12:33:50 +000035 uint32_t fWidth, fHeight;
reed@android.com8a1c16f2008-12-17 15:59:43 +000036};
37
38struct SkBitmap::MipMap : SkNoncopyable {
39 int32_t fRefCnt;
40 int fLevelCount;
41// MipLevel fLevel[fLevelCount];
42// Pixels[]
weita@google.comf9ab99a2009-05-03 18:23:30 +000043
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 static MipMap* Alloc(int levelCount, size_t pixelSize) {
reed@android.com149e2f62009-05-22 14:39:03 +000045 if (levelCount < 0) {
46 return NULL;
47 }
reed@google.com57212f92013-12-30 14:40:38 +000048 int64_t size = (levelCount + 1) * sizeof(MipLevel);
49 size += sizeof(MipMap) + pixelSize;
50 if (!sk_64_isS32(size)) {
reed@android.com149e2f62009-05-22 14:39:03 +000051 return NULL;
52 }
reed@google.com57212f92013-12-30 14:40:38 +000053 MipMap* mm = (MipMap*)sk_malloc_throw(sk_64_asS32(size));
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 mm->fRefCnt = 1;
55 mm->fLevelCount = levelCount;
56 return mm;
57 }
58
59 const MipLevel* levels() const { return (const MipLevel*)(this + 1); }
60 MipLevel* levels() { return (MipLevel*)(this + 1); }
61
62 const void* pixels() const { return levels() + fLevelCount; }
63 void* pixels() { return levels() + fLevelCount; }
weita@google.comf9ab99a2009-05-03 18:23:30 +000064
reed@android.com149e2f62009-05-22 14:39:03 +000065 void ref() {
66 if (SK_MaxS32 == sk_atomic_inc(&fRefCnt)) {
67 sk_throw();
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 }
69 }
reed@android.com149e2f62009-05-22 14:39:03 +000070 void unref() {
71 SkASSERT(fRefCnt > 0);
72 if (sk_atomic_dec(&fRefCnt) == 1) {
73 sk_free(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 }
75 }
76};
reed@android.com8a1c16f2008-12-17 15:59:43 +000077
78///////////////////////////////////////////////////////////////////////////////
79///////////////////////////////////////////////////////////////////////////////
80
81SkBitmap::SkBitmap() {
reed@android.com4516f472009-06-29 16:25:36 +000082 sk_bzero(this, sizeof(*this));
reed@android.com8a1c16f2008-12-17 15:59:43 +000083}
84
85SkBitmap::SkBitmap(const SkBitmap& src) {
86 SkDEBUGCODE(src.validate();)
reed@android.com4516f472009-06-29 16:25:36 +000087 sk_bzero(this, sizeof(*this));
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 *this = src;
89 SkDEBUGCODE(this->validate();)
90}
91
92SkBitmap::~SkBitmap() {
93 SkDEBUGCODE(this->validate();)
94 this->freePixels();
95}
96
97SkBitmap& SkBitmap::operator=(const SkBitmap& src) {
98 if (this != &src) {
99 this->freePixels();
100 memcpy(this, &src, sizeof(src));
101
102 // inc src reference counts
reed@android.com83f7bc32009-07-17 02:42:41 +0000103 SkSafeRef(src.fPixelRef);
reed@android.com149e2f62009-05-22 14:39:03 +0000104 SkSafeRef(src.fMipMap);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105
106 // we reset our locks if we get blown away
107 fPixelLockCount = 0;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000108
reed@google.com5f62ed72014-01-15 19:59:45 +0000109 if (fPixelRef) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110 // ignore the values from the memcpy
111 fPixels = NULL;
112 fColorTable = NULL;
bsalomon@google.com586f48c2011-04-14 15:07:22 +0000113 // Note that what to for genID is somewhat arbitrary. We have no
114 // way to track changes to raw pixels across multiple SkBitmaps.
115 // Would benefit from an SkRawPixelRef type created by
116 // setPixels.
117 // Just leave the memcpy'ed one but they'll get out of sync
118 // as soon either is modified.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119 }
120 }
121
122 SkDEBUGCODE(this->validate();)
123 return *this;
124}
125
126void SkBitmap::swap(SkBitmap& other) {
bsalomon@google.com586f48c2011-04-14 15:07:22 +0000127 SkTSwap(fColorTable, other.fColorTable);
128 SkTSwap(fPixelRef, other.fPixelRef);
reed@google.com672588b2014-01-08 15:42:01 +0000129 SkTSwap(fPixelRefOrigin, other.fPixelRefOrigin);
bsalomon@google.com586f48c2011-04-14 15:07:22 +0000130 SkTSwap(fPixelLockCount, other.fPixelLockCount);
131 SkTSwap(fMipMap, other.fMipMap);
132 SkTSwap(fPixels, other.fPixels);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000133 SkTSwap(fInfo, other.fInfo);
bsalomon@google.com586f48c2011-04-14 15:07:22 +0000134 SkTSwap(fRowBytes, other.fRowBytes);
bsalomon@google.com586f48c2011-04-14 15:07:22 +0000135 SkTSwap(fFlags, other.fFlags);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136
137 SkDEBUGCODE(this->validate();)
138}
139
140void SkBitmap::reset() {
141 this->freePixels();
reed@android.com4516f472009-06-29 16:25:36 +0000142 sk_bzero(this, sizeof(*this));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143}
144
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000145SkBitmap::Config SkBitmap::config() const {
146 return SkColorTypeToBitmapConfig(fInfo.colorType());
147}
148
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149int SkBitmap::ComputeBytesPerPixel(SkBitmap::Config config) {
150 int bpp;
151 switch (config) {
152 case kNo_Config:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153 bpp = 0; // not applicable
154 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 case kA8_Config:
156 case kIndex8_Config:
157 bpp = 1;
158 break;
159 case kRGB_565_Config:
160 case kARGB_4444_Config:
161 bpp = 2;
162 break;
163 case kARGB_8888_Config:
164 bpp = 4;
165 break;
166 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000167 SkDEBUGFAIL("unknown config");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000168 bpp = 0; // error
169 break;
170 }
171 return bpp;
172}
173
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000174size_t SkBitmap::ComputeRowBytes(Config c, int width) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000175 return SkColorTypeMinRowBytes(SkBitmapConfigToColorType(c), width);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176}
177
reed@google.com57212f92013-12-30 14:40:38 +0000178int64_t SkBitmap::ComputeSize64(Config config, int width, int height) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000179 SkColorType ct = SkBitmapConfigToColorType(config);
180 int64_t rowBytes = sk_64_mul(SkColorTypeBytesPerPixel(ct), width);
reed@google.com57212f92013-12-30 14:40:38 +0000181 return rowBytes * height;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000182}
183
184size_t SkBitmap::ComputeSize(Config c, int width, int height) {
reed@google.com57212f92013-12-30 14:40:38 +0000185 int64_t size = SkBitmap::ComputeSize64(c, width, height);
186 return sk_64_isS32(size) ? sk_64_asS32(size) : 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000187}
188
reed@google.com57212f92013-12-30 14:40:38 +0000189int64_t SkBitmap::ComputeSafeSize64(Config config,
190 uint32_t width,
191 uint32_t height,
192 size_t rowBytes) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000193 SkImageInfo info = SkImageInfo::Make(width, height,
194 SkBitmapConfigToColorType(config),
195 kPremul_SkAlphaType);
196 return info.getSafeSize64(rowBytes);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000197}
198
199size_t SkBitmap::ComputeSafeSize(Config config,
200 uint32_t width,
201 uint32_t height,
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000202 size_t rowBytes) {
reed@google.com57212f92013-12-30 14:40:38 +0000203 int64_t safeSize = ComputeSafeSize64(config, width, height, rowBytes);
204 int32_t safeSize32 = (int32_t)safeSize;
skia.committer@gmail.comf5e1f632013-12-31 07:01:36 +0000205
reed@google.com57212f92013-12-30 14:40:38 +0000206 if (safeSize32 != safeSize) {
207 safeSize32 = 0;
208 }
209 return safeSize32;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000210}
211
reed@google.com86b2e432012-03-15 21:17:03 +0000212void SkBitmap::getBounds(SkRect* bounds) const {
213 SkASSERT(bounds);
214 bounds->set(0, 0,
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000215 SkIntToScalar(fInfo.fWidth), SkIntToScalar(fInfo.fHeight));
reed@google.com86b2e432012-03-15 21:17:03 +0000216}
217
reed@google.com80e14592012-03-16 14:58:07 +0000218void SkBitmap::getBounds(SkIRect* bounds) const {
219 SkASSERT(bounds);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000220 bounds->set(0, 0, fInfo.fWidth, fInfo.fHeight);
reed@google.com80e14592012-03-16 14:58:07 +0000221}
222
reed@google.com86b2e432012-03-15 21:17:03 +0000223///////////////////////////////////////////////////////////////////////////////
224
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000225static bool validate_alphaType(SkColorType colorType, SkAlphaType alphaType,
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000226 SkAlphaType* canonical = NULL) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000227 switch (colorType) {
228 case kUnknown_SkColorType:
reed@google.com383a6972013-10-21 14:00:07 +0000229 alphaType = kIgnore_SkAlphaType;
230 break;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000231 case kAlpha_8_SkColorType:
reed@google.com383a6972013-10-21 14:00:07 +0000232 if (kUnpremul_SkAlphaType == alphaType) {
233 alphaType = kPremul_SkAlphaType;
234 }
235 // fall-through
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000236 case kIndex_8_SkColorType:
237 case kARGB_4444_SkColorType:
238 case kRGBA_8888_SkColorType:
239 case kBGRA_8888_SkColorType:
reed@google.com383a6972013-10-21 14:00:07 +0000240 if (kIgnore_SkAlphaType == alphaType) {
241 return false;
242 }
243 break;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000244 case kRGB_565_SkColorType:
reed@google.com383a6972013-10-21 14:00:07 +0000245 alphaType = kOpaque_SkAlphaType;
246 break;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000247 default:
248 return false;
reed@android.com149e2f62009-05-22 14:39:03 +0000249 }
reed@google.com383a6972013-10-21 14:00:07 +0000250 if (canonical) {
251 *canonical = alphaType;
252 }
253 return true;
254}
reed@android.com149e2f62009-05-22 14:39:03 +0000255
commit-bot@chromium.orgd5414e52014-02-13 22:30:38 +0000256bool SkBitmap::setConfig(const SkImageInfo& origInfo, size_t rowBytes) {
257 SkImageInfo info = origInfo;
258
259 if (!validate_alphaType(info.fColorType, info.fAlphaType,
260 &info.fAlphaType)) {
261 return reset_return_false(this);
262 }
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000263
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000264 // require that rowBytes fit in 31bits
265 int64_t mrb = info.minRowBytes64();
266 if ((int32_t)mrb != mrb) {
267 return reset_return_false(this);
reed@google.com383a6972013-10-21 14:00:07 +0000268 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000269 if ((int64_t)rowBytes != (int32_t)rowBytes) {
270 return reset_return_false(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000271 }
reed@android.com89bb83a2009-05-29 21:30:42 +0000272
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000273 if (info.width() < 0 || info.height() < 0) {
274 return reset_return_false(this);
275 }
276
277 if (kUnknown_SkColorType == info.colorType()) {
278 rowBytes = 0;
279 } else if (0 == rowBytes) {
280 rowBytes = (size_t)mrb;
281 } else if (rowBytes < info.minRowBytes()) {
282 return reset_return_false(this);
reed@google.com383a6972013-10-21 14:00:07 +0000283 }
284
285 this->freePixels();
286
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000287 fInfo = info;
288 fRowBytes = SkToU32(rowBytes);
reed@google.com383a6972013-10-21 14:00:07 +0000289 return true;
reed@google.com383a6972013-10-21 14:00:07 +0000290}
291
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000292bool SkBitmap::setConfig(Config config, int width, int height, size_t rowBytes,
293 SkAlphaType alphaType) {
294 SkColorType ct = SkBitmapConfigToColorType(config);
295 return this->setConfig(SkImageInfo::Make(width, height, ct, alphaType),
296 rowBytes);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000297}
298
reed@google.com383a6972013-10-21 14:00:07 +0000299bool SkBitmap::setAlphaType(SkAlphaType alphaType) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000300 if (!validate_alphaType(fInfo.fColorType, alphaType, &alphaType)) {
reed@google.com383a6972013-10-21 14:00:07 +0000301 return false;
302 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000303 if (fInfo.fAlphaType != alphaType) {
304 fInfo.fAlphaType = alphaType;
commit-bot@chromium.org0e8d0d62014-01-27 15:41:07 +0000305 if (fPixelRef) {
reed@google.comc1587f92014-01-28 16:05:39 +0000306 fPixelRef->changeAlphaType(alphaType);
commit-bot@chromium.org0e8d0d62014-01-27 15:41:07 +0000307 }
308 }
reed@google.com383a6972013-10-21 14:00:07 +0000309 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000310}
311
312void SkBitmap::updatePixelsFromRef() const {
313 if (NULL != fPixelRef) {
314 if (fPixelLockCount > 0) {
reed@google.comff0da4f2012-05-17 13:14:52 +0000315 SkASSERT(fPixelRef->isLocked());
weita@google.comf9ab99a2009-05-03 18:23:30 +0000316
reed@android.com8a1c16f2008-12-17 15:59:43 +0000317 void* p = fPixelRef->pixels();
318 if (NULL != p) {
reed@google.com672588b2014-01-08 15:42:01 +0000319 p = (char*)p
reed@google.com303c4752014-01-09 20:00:14 +0000320 + fPixelRefOrigin.fY * fRowBytes
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000321 + fPixelRefOrigin.fX * fInfo.bytesPerPixel();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000322 }
323 fPixels = p;
reed@google.com5f62ed72014-01-15 19:59:45 +0000324 fColorTable = fPixelRef->colorTable();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000325 } else {
326 SkASSERT(0 == fPixelLockCount);
327 fPixels = NULL;
reed@google.com5f62ed72014-01-15 19:59:45 +0000328 fColorTable = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000329 }
330 }
331}
332
reed@google.com9230ea22013-12-09 22:01:03 +0000333static bool config_to_colorType(SkBitmap::Config config, SkColorType* ctOut) {
334 SkColorType ct;
335 switch (config) {
336 case SkBitmap::kA8_Config:
337 ct = kAlpha_8_SkColorType;
338 break;
339 case SkBitmap::kIndex8_Config:
340 ct = kIndex_8_SkColorType;
341 break;
342 case SkBitmap::kRGB_565_Config:
343 ct = kRGB_565_SkColorType;
344 break;
345 case SkBitmap::kARGB_4444_Config:
346 ct = kARGB_4444_SkColorType;
347 break;
348 case SkBitmap::kARGB_8888_Config:
349 ct = kPMColor_SkColorType;
350 break;
351 case SkBitmap::kNo_Config:
352 default:
353 return false;
354 }
355 if (ctOut) {
356 *ctOut = ct;
357 }
358 return true;
359}
360
reed@google.com672588b2014-01-08 15:42:01 +0000361SkPixelRef* SkBitmap::setPixelRef(SkPixelRef* pr, int dx, int dy) {
reed@google.comdcea5302014-01-03 13:43:01 +0000362#ifdef SK_DEBUG
reed@google.com672588b2014-01-08 15:42:01 +0000363 if (pr) {
reed@google.comdcea5302014-01-03 13:43:01 +0000364 SkImageInfo info;
365 if (this->asImageInfo(&info)) {
366 const SkImageInfo& prInfo = pr->info();
367 SkASSERT(info.fWidth <= prInfo.fWidth);
368 SkASSERT(info.fHeight <= prInfo.fHeight);
369 SkASSERT(info.fColorType == prInfo.fColorType);
370 switch (prInfo.fAlphaType) {
371 case kIgnore_SkAlphaType:
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000372 SkASSERT(fInfo.fAlphaType == kIgnore_SkAlphaType);
reed@google.comdcea5302014-01-03 13:43:01 +0000373 break;
374 case kOpaque_SkAlphaType:
375 case kPremul_SkAlphaType:
376 SkASSERT(info.fAlphaType == kOpaque_SkAlphaType ||
377 info.fAlphaType == kPremul_SkAlphaType);
378 break;
379 case kUnpremul_SkAlphaType:
380 SkASSERT(info.fAlphaType == kOpaque_SkAlphaType ||
381 info.fAlphaType == kUnpremul_SkAlphaType);
382 break;
383 }
384 }
385 }
386#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000387
reed@google.com672588b2014-01-08 15:42:01 +0000388 if (pr) {
389 const SkImageInfo& info = pr->info();
390 fPixelRefOrigin.set(SkPin32(dx, 0, info.fWidth),
391 SkPin32(dy, 0, info.fHeight));
392 } else {
393 // ignore dx,dy if there is no pixelref
394 fPixelRefOrigin.setZero();
395 }
396
397 if (fPixelRef != pr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000398 if (fPixelRef != pr) {
399 this->freePixels();
400 SkASSERT(NULL == fPixelRef);
weita@google.comf9ab99a2009-05-03 18:23:30 +0000401
reed@google.com82065d62011-02-07 15:30:46 +0000402 SkSafeRef(pr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000403 fPixelRef = pr;
404 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000405 this->updatePixelsFromRef();
406 }
407
408 SkDEBUGCODE(this->validate();)
409 return pr;
410}
411
412void SkBitmap::lockPixels() const {
djsollen@google.com7c6d2642013-08-06 12:19:38 +0000413 if (NULL != fPixelRef && 0 == sk_atomic_inc(&fPixelLockCount)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000414 fPixelRef->lockPixels();
415 this->updatePixelsFromRef();
416 }
417 SkDEBUGCODE(this->validate();)
418}
419
420void SkBitmap::unlockPixels() const {
421 SkASSERT(NULL == fPixelRef || fPixelLockCount > 0);
422
djsollen@google.com7c6d2642013-08-06 12:19:38 +0000423 if (NULL != fPixelRef && 1 == sk_atomic_dec(&fPixelLockCount)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000424 fPixelRef->unlockPixels();
425 this->updatePixelsFromRef();
426 }
427 SkDEBUGCODE(this->validate();)
428}
429
reed@google.com9c49bc32011-07-07 13:42:37 +0000430bool SkBitmap::lockPixelsAreWritable() const {
djsollen@google.comc84b8332012-07-27 13:41:44 +0000431 return (fPixelRef) ? fPixelRef->lockPixelsAreWritable() : false;
reed@google.com9c49bc32011-07-07 13:42:37 +0000432}
433
reed@android.com8a1c16f2008-12-17 15:59:43 +0000434void SkBitmap::setPixels(void* p, SkColorTable* ctable) {
reed@google.com8e1034e2012-07-30 13:16:35 +0000435 if (NULL == p) {
reed@google.com672588b2014-01-08 15:42:01 +0000436 this->setPixelRef(NULL);
reed@google.com8e1034e2012-07-30 13:16:35 +0000437 return;
438 }
439
reed@google.combf790232013-12-13 19:45:58 +0000440 SkImageInfo info;
441 if (!this->asImageInfo(&info)) {
reed@google.com672588b2014-01-08 15:42:01 +0000442 this->setPixelRef(NULL);
reed@google.combf790232013-12-13 19:45:58 +0000443 return;
444 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000445
reed@google.combf790232013-12-13 19:45:58 +0000446 SkPixelRef* pr = SkMallocPixelRef::NewDirect(info, p, fRowBytes, ctable);
447 if (NULL == pr) {
reed@google.com672588b2014-01-08 15:42:01 +0000448 this->setPixelRef(NULL);
reed@google.combf790232013-12-13 19:45:58 +0000449 return;
450 }
451
452 this->setPixelRef(pr)->unref();
453
djsollen@google.comc84b8332012-07-27 13:41:44 +0000454 // since we're already allocated, we lockPixels right away
455 this->lockPixels();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000456 SkDEBUGCODE(this->validate();)
457}
458
459bool SkBitmap::allocPixels(Allocator* allocator, SkColorTable* ctable) {
460 HeapAllocator stdalloc;
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000461
reed@android.com8a1c16f2008-12-17 15:59:43 +0000462 if (NULL == allocator) {
463 allocator = &stdalloc;
464 }
465 return allocator->allocPixelRef(this, ctable);
466}
467
reed@google.com9ebcac52014-01-24 18:53:42 +0000468///////////////////////////////////////////////////////////////////////////////
469
reed@google.com9ebcac52014-01-24 18:53:42 +0000470bool SkBitmap::allocPixels(const SkImageInfo& info, SkPixelRefFactory* factory,
471 SkColorTable* ctable) {
472 if (kIndex_8_SkColorType == info.fColorType && NULL == ctable) {
473 return reset_return_false(this);
474 }
475 if (!this->setConfig(info)) {
476 return reset_return_false(this);
477 }
478
479 SkMallocPixelRef::PRFactory defaultFactory;
480 if (NULL == factory) {
481 factory = &defaultFactory;
482 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000483
reed@google.com9ebcac52014-01-24 18:53:42 +0000484 SkPixelRef* pr = factory->create(info, ctable);
485 if (NULL == pr) {
486 return reset_return_false(this);
487 }
488 this->setPixelRef(pr)->unref();
489
490 // TODO: lockPixels could/should return bool or void*/NULL
491 this->lockPixels();
492 if (NULL == this->getPixels()) {
493 return reset_return_false(this);
494 }
495 return true;
496}
497
498bool SkBitmap::installPixels(const SkImageInfo& info, void* pixels, size_t rb,
499 void (*releaseProc)(void* addr, void* context),
500 void* context) {
reed@google.comc2d768c2014-02-05 21:02:35 +0000501 if (!this->setConfig(info, rb)) {
reed@google.com9ebcac52014-01-24 18:53:42 +0000502 this->reset();
503 return false;
504 }
505
506 SkPixelRef* pr = SkMallocPixelRef::NewWithProc(info, rb, NULL, pixels,
507 releaseProc, context);
508 if (!pr) {
509 this->reset();
510 return false;
511 }
512
513 this->setPixelRef(pr)->unref();
mike@reedtribe.org6e58cf32014-02-16 20:54:21 +0000514
515 // since we're already allocated, we lockPixels right away
516 this->lockPixels();
mike@reedtribe.org6e58cf32014-02-16 20:54:21 +0000517 SkDEBUGCODE(this->validate();)
reed@google.com9ebcac52014-01-24 18:53:42 +0000518 return true;
519}
520
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000521bool SkBitmap::installMaskPixels(const SkMask& mask) {
522 if (SkMask::kA8_Format != mask.fFormat) {
523 this->reset();
524 return false;
525 }
526 return this->installPixels(SkImageInfo::MakeA8(mask.fBounds.width(),
527 mask.fBounds.height()),
528 mask.fImage, mask.fRowBytes);
529}
530
reed@google.comeb9a46c2014-01-25 16:46:20 +0000531bool SkBitmap::allocConfigPixels(Config config, int width, int height,
532 bool isOpaque) {
533 SkColorType ct;
534 if (!config_to_colorType(config, &ct)) {
535 return false;
536 }
skia.committer@gmail.com33c749a2014-01-26 07:01:45 +0000537
reed@google.comeb9a46c2014-01-25 16:46:20 +0000538 SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
reed@google.comeb9a46c2014-01-25 16:46:20 +0000539 return this->allocPixels(SkImageInfo::Make(width, height, ct, at));
540}
541
542///////////////////////////////////////////////////////////////////////////////
543
reed@android.com8a1c16f2008-12-17 15:59:43 +0000544void SkBitmap::freePixels() {
545 // if we're gonna free the pixels, we certainly need to free the mipmap
546 this->freeMipMap();
547
reed@android.com8a1c16f2008-12-17 15:59:43 +0000548 if (NULL != fPixelRef) {
549 if (fPixelLockCount > 0) {
550 fPixelRef->unlockPixels();
551 }
552 fPixelRef->unref();
553 fPixelRef = NULL;
reed@google.com672588b2014-01-08 15:42:01 +0000554 fPixelRefOrigin.setZero();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000555 }
556 fPixelLockCount = 0;
557 fPixels = NULL;
reed@google.com5f62ed72014-01-15 19:59:45 +0000558 fColorTable = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000559}
560
561void SkBitmap::freeMipMap() {
reed@android.com149e2f62009-05-22 14:39:03 +0000562 if (fMipMap) {
563 fMipMap->unref();
564 fMipMap = NULL;
565 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000566}
567
568uint32_t SkBitmap::getGenerationID() const {
djsollen@google.comc84b8332012-07-27 13:41:44 +0000569 return (fPixelRef) ? fPixelRef->getGenerationID() : 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000570}
571
572void SkBitmap::notifyPixelsChanged() const {
junov@chromium.orgb0521292011-12-15 20:14:06 +0000573 SkASSERT(!this->isImmutable());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000574 if (fPixelRef) {
575 fPixelRef->notifyPixelsChanged();
576 }
577}
578
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000579GrTexture* SkBitmap::getTexture() const {
reed@android.comce4e53a2010-09-09 16:01:26 +0000580 return fPixelRef ? fPixelRef->getTexture() : NULL;
581}
582
reed@android.com8a1c16f2008-12-17 15:59:43 +0000583///////////////////////////////////////////////////////////////////////////////
584
reed@android.com8a1c16f2008-12-17 15:59:43 +0000585/** We explicitly use the same allocator for our pixels that SkMask does,
586 so that we can freely assign memory allocated by one class to the other.
587 */
588bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst,
589 SkColorTable* ctable) {
reed@google.combf790232013-12-13 19:45:58 +0000590 SkImageInfo info;
591 if (!dst->asImageInfo(&info)) {
592// SkDebugf("unsupported config for info %d\n", dst->config());
593 return false;
594 }
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000595
reed@google.combf790232013-12-13 19:45:58 +0000596 SkPixelRef* pr = SkMallocPixelRef::NewAllocate(info, dst->rowBytes(),
597 ctable);
598 if (NULL == pr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000599 return false;
600 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000601
reed@google.com672588b2014-01-08 15:42:01 +0000602 dst->setPixelRef(pr)->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000603 // since we're already allocated, we lockPixels right away
604 dst->lockPixels();
605 return true;
606}
607
608///////////////////////////////////////////////////////////////////////////////
609
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000610bool SkBitmap::copyPixelsTo(void* const dst, size_t dstSize,
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000611 size_t dstRowBytes, bool preserveDstPad) const {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000612
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000613 if (0 == dstRowBytes) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000614 dstRowBytes = fRowBytes;
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000615 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000616
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000617 if (dstRowBytes < fInfo.minRowBytes() ||
618 dst == NULL || (getPixels() == NULL && pixelRef() == NULL)) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000619 return false;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000620 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000621
bsalomon@google.comc6980972011-11-02 19:57:21 +0000622 if (!preserveDstPad && static_cast<uint32_t>(dstRowBytes) == fRowBytes) {
reed@google.com44699382013-10-31 17:28:30 +0000623 size_t safeSize = this->getSafeSize();
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000624 if (safeSize > dstSize || safeSize == 0)
625 return false;
626 else {
627 SkAutoLockPixels lock(*this);
628 // This implementation will write bytes beyond the end of each row,
629 // excluding the last row, if the bitmap's stride is greater than
630 // strictly required by the current config.
631 memcpy(dst, getPixels(), safeSize);
632
633 return true;
634 }
635 } else {
636 // If destination has different stride than us, then copy line by line.
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000637 if (fInfo.getSafeSize(dstRowBytes) > dstSize) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000638 return false;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000639 } else {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000640 // Just copy what we need on each line.
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000641 size_t rowBytes = fInfo.minRowBytes();
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000642 SkAutoLockPixels lock(*this);
643 const uint8_t* srcP = reinterpret_cast<const uint8_t*>(getPixels());
644 uint8_t* dstP = reinterpret_cast<uint8_t*>(dst);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000645 for (int row = 0; row < fInfo.fHeight;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000646 row++, srcP += fRowBytes, dstP += dstRowBytes) {
647 memcpy(dstP, srcP, rowBytes);
648 }
649
650 return true;
651 }
652 }
653}
654
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000655///////////////////////////////////////////////////////////////////////////////
656
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000657bool SkBitmap::isImmutable() const {
junov@chromium.orgb0521292011-12-15 20:14:06 +0000658 return fPixelRef ? fPixelRef->isImmutable() :
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000659 fFlags & kImageIsImmutable_Flag;
junov@chromium.orgb0521292011-12-15 20:14:06 +0000660}
661
662void SkBitmap::setImmutable() {
663 if (fPixelRef) {
664 fPixelRef->setImmutable();
665 } else {
666 fFlags |= kImageIsImmutable_Flag;
667 }
668}
669
junov@google.com4ee7ae52011-06-30 17:30:49 +0000670bool SkBitmap::isVolatile() const {
671 return (fFlags & kImageIsVolatile_Flag) != 0;
672}
673
674void SkBitmap::setIsVolatile(bool isVolatile) {
675 if (isVolatile) {
676 fFlags |= kImageIsVolatile_Flag;
677 } else {
678 fFlags &= ~kImageIsVolatile_Flag;
679 }
680}
681
reed@android.com8a1c16f2008-12-17 15:59:43 +0000682void* SkBitmap::getAddr(int x, int y) const {
683 SkASSERT((unsigned)x < (unsigned)this->width());
684 SkASSERT((unsigned)y < (unsigned)this->height());
685
686 char* base = (char*)this->getPixels();
687 if (base) {
688 base += y * this->rowBytes();
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000689 switch (this->colorType()) {
690 case kRGBA_8888_SkColorType:
691 case kBGRA_8888_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000692 base += x << 2;
693 break;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000694 case kARGB_4444_SkColorType:
695 case kRGB_565_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000696 base += x << 1;
697 break;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000698 case kAlpha_8_SkColorType:
699 case kIndex_8_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000700 base += x;
701 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000702 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000703 SkDEBUGFAIL("Can't return addr for config");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000704 base = NULL;
705 break;
706 }
707 }
708 return base;
709}
710
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000711SkColor SkBitmap::getColor(int x, int y) const {
712 SkASSERT((unsigned)x < (unsigned)this->width());
713 SkASSERT((unsigned)y < (unsigned)this->height());
714
715 switch (this->config()) {
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000716 case SkBitmap::kA8_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000717 uint8_t* addr = this->getAddr8(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000718 return SkColorSetA(0, addr[0]);
719 }
720 case SkBitmap::kIndex8_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000721 SkPMColor c = this->getIndex8Color(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000722 return SkUnPreMultiply::PMColorToColor(c);
723 }
724 case SkBitmap::kRGB_565_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000725 uint16_t* addr = this->getAddr16(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000726 return SkPixel16ToColor(addr[0]);
727 }
728 case SkBitmap::kARGB_4444_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000729 uint16_t* addr = this->getAddr16(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000730 SkPMColor c = SkPixel4444ToPixel32(addr[0]);
731 return SkUnPreMultiply::PMColorToColor(c);
732 }
733 case SkBitmap::kARGB_8888_Config: {
reed@google.com3b521d02011-04-29 11:53:41 +0000734 uint32_t* addr = this->getAddr32(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000735 return SkUnPreMultiply::PMColorToColor(addr[0]);
736 }
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000737 case kNo_Config:
rmistry@google.comd6bab022013-12-02 13:50:38 +0000738 default:
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000739 SkASSERT(false);
740 return 0;
741 }
742 SkASSERT(false); // Not reached.
743 return 0;
744}
745
reed@google.com2a7579d2012-11-07 18:30:18 +0000746bool SkBitmap::ComputeIsOpaque(const SkBitmap& bm) {
747 SkAutoLockPixels alp(bm);
748 if (!bm.getPixels()) {
749 return false;
750 }
751
752 const int height = bm.height();
753 const int width = bm.width();
754
755 switch (bm.config()) {
reed@google.com2a7579d2012-11-07 18:30:18 +0000756 case SkBitmap::kA8_Config: {
757 unsigned a = 0xFF;
758 for (int y = 0; y < height; ++y) {
759 const uint8_t* row = bm.getAddr8(0, y);
760 for (int x = 0; x < width; ++x) {
761 a &= row[x];
762 }
763 if (0xFF != a) {
764 return false;
765 }
766 }
767 return true;
768 } break;
reed@google.com2a7579d2012-11-07 18:30:18 +0000769 case SkBitmap::kIndex8_Config: {
770 SkAutoLockColors alc(bm);
771 const SkPMColor* table = alc.colors();
772 if (!table) {
773 return false;
774 }
reed@google.com140d7282013-01-07 20:25:04 +0000775 SkPMColor c = (SkPMColor)~0;
reed@google.com2a7579d2012-11-07 18:30:18 +0000776 for (int i = bm.getColorTable()->count() - 1; i >= 0; --i) {
777 c &= table[i];
778 }
779 return 0xFF == SkGetPackedA32(c);
780 } break;
781 case SkBitmap::kRGB_565_Config:
782 return true;
783 break;
784 case SkBitmap::kARGB_4444_Config: {
785 unsigned c = 0xFFFF;
786 for (int y = 0; y < height; ++y) {
787 const SkPMColor16* row = bm.getAddr16(0, y);
788 for (int x = 0; x < width; ++x) {
789 c &= row[x];
790 }
791 if (0xF != SkGetPackedA4444(c)) {
792 return false;
793 }
794 }
795 return true;
796 } break;
797 case SkBitmap::kARGB_8888_Config: {
reed@google.com140d7282013-01-07 20:25:04 +0000798 SkPMColor c = (SkPMColor)~0;
reed@google.com2a7579d2012-11-07 18:30:18 +0000799 for (int y = 0; y < height; ++y) {
800 const SkPMColor* row = bm.getAddr32(0, y);
801 for (int x = 0; x < width; ++x) {
802 c &= row[x];
803 }
804 if (0xFF != SkGetPackedA32(c)) {
805 return false;
806 }
807 }
808 return true;
809 }
810 default:
811 break;
812 }
813 return false;
814}
815
816
reed@android.com8a1c16f2008-12-17 15:59:43 +0000817///////////////////////////////////////////////////////////////////////////////
818///////////////////////////////////////////////////////////////////////////////
819
reed@google.com45f746f2013-06-21 19:51:31 +0000820static uint16_t pack_8888_to_4444(unsigned a, unsigned r, unsigned g, unsigned b) {
821 unsigned pixel = (SkA32To4444(a) << SK_A4444_SHIFT) |
822 (SkR32To4444(r) << SK_R4444_SHIFT) |
823 (SkG32To4444(g) << SK_G4444_SHIFT) |
824 (SkB32To4444(b) << SK_B4444_SHIFT);
825 return SkToU16(pixel);
826}
827
reed@google.com60d32352013-06-28 19:40:50 +0000828void SkBitmap::internalErase(const SkIRect& area,
829 U8CPU a, U8CPU r, U8CPU g, U8CPU b) const {
830#ifdef SK_DEBUG
reed@android.com8a1c16f2008-12-17 15:59:43 +0000831 SkDEBUGCODE(this->validate();)
reed@google.com60d32352013-06-28 19:40:50 +0000832 SkASSERT(!area.isEmpty());
833 {
reed@google.com92833f92013-06-28 19:47:43 +0000834 SkIRect total = { 0, 0, this->width(), this->height() };
reed@google.com60d32352013-06-28 19:40:50 +0000835 SkASSERT(total.contains(area));
836 }
837#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000838
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000839 switch (fInfo.colorType()) {
840 case kUnknown_SkColorType:
841 case kIndex_8_SkColorType:
842 return; // can't erase
843 default:
844 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000845 }
846
847 SkAutoLockPixels alp(*this);
848 // perform this check after the lock call
849 if (!this->readyToDraw()) {
850 return;
851 }
852
reed@google.com60d32352013-06-28 19:40:50 +0000853 int height = area.height();
854 const int width = area.width();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000855 const int rowBytes = fRowBytes;
856
857 // make rgb premultiplied
858 if (255 != a) {
859 r = SkAlphaMul(r, a);
860 g = SkAlphaMul(g, a);
861 b = SkAlphaMul(b, a);
862 }
863
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000864 switch (this->colorType()) {
865 case kAlpha_8_SkColorType: {
reed@google.com60d32352013-06-28 19:40:50 +0000866 uint8_t* p = this->getAddr8(area.fLeft, area.fTop);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000867 while (--height >= 0) {
868 memset(p, a, width);
869 p += rowBytes;
870 }
871 break;
872 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000873 case kARGB_4444_SkColorType:
874 case kRGB_565_SkColorType: {
reed@google.com60d32352013-06-28 19:40:50 +0000875 uint16_t* p = this->getAddr16(area.fLeft, area.fTop);;
reed@google.com45f746f2013-06-21 19:51:31 +0000876 uint16_t v;
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000877
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000878 if (kARGB_4444_SkColorType == this->colorType()) {
reed@google.com45f746f2013-06-21 19:51:31 +0000879 v = pack_8888_to_4444(a, r, g, b);
880 } else {
881 v = SkPackRGB16(r >> (8 - SK_R16_BITS),
882 g >> (8 - SK_G16_BITS),
883 b >> (8 - SK_B16_BITS));
884 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000885 while (--height >= 0) {
886 sk_memset16(p, v, width);
887 p = (uint16_t*)((char*)p + rowBytes);
888 }
889 break;
890 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000891 case kPMColor_SkColorType: {
892 // what to do about BGRA or RGBA (which ever is != PMColor ?
893 // for now we don't support them.
reed@google.com60d32352013-06-28 19:40:50 +0000894 uint32_t* p = this->getAddr32(area.fLeft, area.fTop);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000895 uint32_t v = SkPackARGB32(a, r, g, b);
896
897 while (--height >= 0) {
898 sk_memset32(p, v, width);
899 p = (uint32_t*)((char*)p + rowBytes);
900 }
901 break;
902 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000903 default:
904 return; // no change, so don't call notifyPixelsChanged()
reed@android.com8a1c16f2008-12-17 15:59:43 +0000905 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000906
reed@android.com8a1c16f2008-12-17 15:59:43 +0000907 this->notifyPixelsChanged();
908}
909
reed@google.com60d32352013-06-28 19:40:50 +0000910void SkBitmap::eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const {
reed@google.com92833f92013-06-28 19:47:43 +0000911 SkIRect area = { 0, 0, this->width(), this->height() };
reed@google.com60d32352013-06-28 19:40:50 +0000912 if (!area.isEmpty()) {
913 this->internalErase(area, a, r, g, b);
914 }
915}
916
917void SkBitmap::eraseArea(const SkIRect& rect, SkColor c) const {
reed@google.com92833f92013-06-28 19:47:43 +0000918 SkIRect area = { 0, 0, this->width(), this->height() };
reed@google.com60d32352013-06-28 19:40:50 +0000919 if (area.intersect(rect)) {
920 this->internalErase(area, SkColorGetA(c), SkColorGetR(c),
921 SkColorGetG(c), SkColorGetB(c));
922 }
923}
924
reed@android.com8a1c16f2008-12-17 15:59:43 +0000925//////////////////////////////////////////////////////////////////////////////////////
926//////////////////////////////////////////////////////////////////////////////////////
927
reed@android.com8a1c16f2008-12-17 15:59:43 +0000928bool SkBitmap::extractSubset(SkBitmap* result, const SkIRect& subset) const {
929 SkDEBUGCODE(this->validate();)
930
djsollen@google.comc84b8332012-07-27 13:41:44 +0000931 if (NULL == result || NULL == fPixelRef) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000932 return false; // no src pixels
933 }
934
935 SkIRect srcRect, r;
936 srcRect.set(0, 0, this->width(), this->height());
937 if (!r.intersect(srcRect, subset)) {
938 return false; // r is empty (i.e. no intersection)
939 }
940
scroggo@google.coma2a31922012-12-07 19:14:45 +0000941 if (fPixelRef->getTexture() != NULL) {
942 // Do a deep copy
943 SkPixelRef* pixelRef = fPixelRef->deepCopy(this->config(), &subset);
944 if (pixelRef != NULL) {
945 SkBitmap dst;
reed@google.com383a6972013-10-21 14:00:07 +0000946 dst.setConfig(this->config(), subset.width(), subset.height(), 0,
947 this->alphaType());
scroggo@google.coma2a31922012-12-07 19:14:45 +0000948 dst.setIsVolatile(this->isVolatile());
scroggo@google.coma2a31922012-12-07 19:14:45 +0000949 dst.setPixelRef(pixelRef)->unref();
950 SkDEBUGCODE(dst.validate());
951 result->swap(dst);
952 return true;
953 }
954 }
955
scroggo@google.coma2a31922012-12-07 19:14:45 +0000956 // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have
957 // exited above.
958 SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width()));
959 SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height()));
960
reed@android.com8a1c16f2008-12-17 15:59:43 +0000961 SkBitmap dst;
reed@google.com383a6972013-10-21 14:00:07 +0000962 dst.setConfig(this->config(), r.width(), r.height(), this->rowBytes(),
963 this->alphaType());
skyostil@google.com0eb75762012-01-16 10:45:53 +0000964 dst.setIsVolatile(this->isVolatile());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000965
966 if (fPixelRef) {
reed@google.com672588b2014-01-08 15:42:01 +0000967 SkIPoint origin = fPixelRefOrigin;
968 origin.fX += r.fLeft;
969 origin.fY += r.fTop;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000970 // share the pixelref with a custom offset
reed@google.com672588b2014-01-08 15:42:01 +0000971 dst.setPixelRef(fPixelRef, origin);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000972 }
973 SkDEBUGCODE(dst.validate();)
974
975 // we know we're good, so commit to result
976 result->swap(dst);
977 return true;
978}
979
980///////////////////////////////////////////////////////////////////////////////
981
982#include "SkCanvas.h"
983#include "SkPaint.h"
984
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000985#ifdef SK_SUPPORT_LEGACY_COPYTO_CONFIG
986bool SkBitmap::copyTo(SkBitmap* dst, Config c, Allocator* allocator) const {
987 return this->copyTo(dst, SkBitmapConfigToSkColorType(c), allocator);
988}
989
990bool SkBitmap::canCopyTo(Config newConfig) const {
991 return this->canCopyTo(SkBitmapConfigToSkColorType(c));
992}
993#endif
994
995bool SkBitmap::canCopyTo(SkColorType dstColorType) const {
996 if (this->colorType() == kUnknown_SkColorType) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000997 return false;
998 }
999
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +00001000 bool sameConfigs = (this->colorType() == dstColorType);
1001 switch (dstColorType) {
1002 case kAlpha_8_SkColorType:
1003 case kRGB_565_SkColorType:
1004 case kPMColor_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +00001005 break;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +00001006 case kIndex_8_SkColorType:
weita@google.comf9ab99a2009-05-03 18:23:30 +00001007 if (!sameConfigs) {
1008 return false;
1009 }
1010 break;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +00001011 case kARGB_4444_SkColorType:
1012 return sameConfigs || kPMColor_SkColorType == this->colorType();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001013 default:
1014 return false;
1015 }
reed@android.comfbaa88d2009-05-06 17:44:34 +00001016 return true;
1017}
1018
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +00001019bool SkBitmap::copyTo(SkBitmap* dst, SkColorType dstColorType,
1020 Allocator* alloc) const {
1021 if (!this->canCopyTo(dstColorType)) {
reed@android.comfbaa88d2009-05-06 17:44:34 +00001022 return false;
1023 }
1024
reed@google.com50dfa012011-04-01 19:05:36 +00001025 // if we have a texture, first get those pixels
1026 SkBitmap tmpSrc;
1027 const SkBitmap* src = this;
1028
scroggo@google.coma2a31922012-12-07 19:14:45 +00001029 if (fPixelRef) {
1030 SkIRect subset;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001031 subset.setXYWH(fPixelRefOrigin.fX, fPixelRefOrigin.fY,
1032 fInfo.width(), fInfo.height());
reed@google.com672588b2014-01-08 15:42:01 +00001033 if (fPixelRef->readPixels(&tmpSrc, &subset)) {
1034 SkASSERT(tmpSrc.width() == this->width());
1035 SkASSERT(tmpSrc.height() == this->height());
reed@google.com50dfa012011-04-01 19:05:36 +00001036
reed@google.com672588b2014-01-08 15:42:01 +00001037 // did we get lucky and we can just return tmpSrc?
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +00001038 if (tmpSrc.colorType() == dstColorType && NULL == alloc) {
reed@google.com672588b2014-01-08 15:42:01 +00001039 dst->swap(tmpSrc);
scroggo@google.com5ccae2c2014-01-15 16:56:52 +00001040 // If the result is an exact copy, clone the gen ID.
1041 if (dst->pixelRef() && dst->pixelRef()->info() == fPixelRef->info()) {
reed@google.com672588b2014-01-08 15:42:01 +00001042 dst->pixelRef()->cloneGenID(*fPixelRef);
scroggo@google.coma2a31922012-12-07 19:14:45 +00001043 }
reed@google.com672588b2014-01-08 15:42:01 +00001044 return true;
scroggo@google.comd5764e82012-08-22 15:00:05 +00001045 }
reed@google.com672588b2014-01-08 15:42:01 +00001046
1047 // fall through to the raster case
1048 src = &tmpSrc;
reed@google.com50dfa012011-04-01 19:05:36 +00001049 }
reed@android.comfbaa88d2009-05-06 17:44:34 +00001050 }
reed@android.com311c82d2009-05-05 23:13:23 +00001051
reed@google.com50dfa012011-04-01 19:05:36 +00001052 // we lock this now, since we may need its colortable
1053 SkAutoLockPixels srclock(*src);
1054 if (!src->readyToDraw()) {
1055 return false;
1056 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001057
scroggo@google.com5ccae2c2014-01-15 16:56:52 +00001058 // The only way to be readyToDraw is if fPixelRef is non NULL.
1059 SkASSERT(fPixelRef != NULL);
1060
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +00001061 SkImageInfo dstInfo = src->info();
1062 dstInfo.fColorType = dstColorType;
1063
reed@google.com50dfa012011-04-01 19:05:36 +00001064 SkBitmap tmpDst;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +00001065 if (!tmpDst.setConfig(dstInfo)) {
1066 return false;
1067 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001068
weita@google.comf9ab99a2009-05-03 18:23:30 +00001069 // allocate colortable if srcConfig == kIndex8_Config
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +00001070 SkAutoTUnref<SkColorTable> ctable;
1071 if (dstColorType == kIndex_8_SkColorType) {
1072 // TODO: can we just ref() the src colortable? Is it reentrant-safe?
1073 ctable.reset(SkNEW_ARGS(SkColorTable, (*src->getColorTable())));
1074 }
reed@google.com50dfa012011-04-01 19:05:36 +00001075 if (!tmpDst.allocPixels(alloc, ctable)) {
weita@google.comf9ab99a2009-05-03 18:23:30 +00001076 return false;
1077 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001078
reed@google.com50dfa012011-04-01 19:05:36 +00001079 if (!tmpDst.readyToDraw()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001080 // allocator/lock failed
1081 return false;
1082 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001083
scroggo@google.com5ccae2c2014-01-15 16:56:52 +00001084 // pixelRef must be non NULL or tmpDst.readyToDraw() would have
1085 // returned false.
1086 SkASSERT(tmpDst.pixelRef() != NULL);
1087
reed@android.comfbaa88d2009-05-06 17:44:34 +00001088 /* do memcpy for the same configs cases, else use drawing
weita@google.comf9ab99a2009-05-03 18:23:30 +00001089 */
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +00001090 if (src->colorType() == dstColorType) {
reed@google.com50dfa012011-04-01 19:05:36 +00001091 if (tmpDst.getSize() == src->getSize()) {
1092 memcpy(tmpDst.getPixels(), src->getPixels(), src->getSafeSize());
scroggo@google.comd5764e82012-08-22 15:00:05 +00001093 SkPixelRef* pixelRef = tmpDst.pixelRef();
scroggo@google.com5ccae2c2014-01-15 16:56:52 +00001094
1095 // In order to reach this point, we know that the width, config and
1096 // rowbytes of the SkPixelRefs are the same, but it is possible for
1097 // the heights to differ, if this SkBitmap's height is a subset of
1098 // fPixelRef. Only if the SkPixelRefs' heights match are we
1099 // guaranteed that this is an exact copy, meaning we should clone
1100 // the genID.
1101 if (pixelRef->info().fHeight == fPixelRef->info().fHeight) {
1102 // TODO: what to do if the two infos match, BUT
1103 // fPixelRef is premul and pixelRef is opaque?
1104 // skipping assert for now
1105 // https://code.google.com/p/skia/issues/detail?id=2012
1106// SkASSERT(pixelRef->info() == fPixelRef->info());
1107 SkASSERT(pixelRef->info().fWidth == fPixelRef->info().fWidth);
1108 SkASSERT(pixelRef->info().fColorType == fPixelRef->info().fColorType);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +00001109 pixelRef->cloneGenID(*fPixelRef);
scroggo@google.comd5764e82012-08-22 15:00:05 +00001110 }
reed@android.com311c82d2009-05-05 23:13:23 +00001111 } else {
reed@google.com50dfa012011-04-01 19:05:36 +00001112 const char* srcP = reinterpret_cast<const char*>(src->getPixels());
1113 char* dstP = reinterpret_cast<char*>(tmpDst.getPixels());
reed@android.com311c82d2009-05-05 23:13:23 +00001114 // to be sure we don't read too much, only copy our logical pixels
reed@google.com50dfa012011-04-01 19:05:36 +00001115 size_t bytesToCopy = tmpDst.width() * tmpDst.bytesPerPixel();
1116 for (int y = 0; y < tmpDst.height(); y++) {
reed@android.com311c82d2009-05-05 23:13:23 +00001117 memcpy(dstP, srcP, bytesToCopy);
reed@google.com50dfa012011-04-01 19:05:36 +00001118 srcP += src->rowBytes();
1119 dstP += tmpDst.rowBytes();
reed@android.com311c82d2009-05-05 23:13:23 +00001120 }
1121 }
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +00001122 } else if (kARGB_4444_SkColorType == dstColorType
1123 && kPMColor_SkColorType == src->colorType()) {
scroggo@google.com8dc8bc52013-08-07 19:16:05 +00001124 SkASSERT(src->height() == tmpDst.height());
1125 SkASSERT(src->width() == tmpDst.width());
1126 for (int y = 0; y < src->height(); ++y) {
1127 SkPMColor16* SK_RESTRICT dstRow = (SkPMColor16*) tmpDst.getAddr16(0, y);
1128 SkPMColor* SK_RESTRICT srcRow = (SkPMColor*) src->getAddr32(0, y);
1129 DITHER_4444_SCAN(y);
1130 for (int x = 0; x < src->width(); ++x) {
1131 dstRow[x] = SkDitherARGB32To4444(srcRow[x],
1132 DITHER_VALUE(x));
1133 }
1134 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001135 } else {
robertphillips@google.com0197b322013-10-10 15:48:16 +00001136 // Always clear the dest in case one of the blitters accesses it
1137 // TODO: switch the allocation of tmpDst to call sk_calloc_throw
1138 tmpDst.eraseColor(SK_ColorTRANSPARENT);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001139
reed@google.com50dfa012011-04-01 19:05:36 +00001140 SkCanvas canvas(tmpDst);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001141 SkPaint paint;
1142
1143 paint.setDither(true);
reed@google.com50dfa012011-04-01 19:05:36 +00001144 canvas.drawBitmap(*src, 0, 0, &paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001145 }
1146
reed@google.com50dfa012011-04-01 19:05:36 +00001147 dst->swap(tmpDst);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001148 return true;
1149}
1150
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001151bool SkBitmap::deepCopyTo(SkBitmap* dst, Config dstConfig) const {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001152 const SkColorType dstCT = SkBitmapConfigToColorType(dstConfig);
1153
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +00001154 if (!this->canCopyTo(dstCT)) {
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001155 return false;
1156 }
1157
1158 // If we have a PixelRef, and it supports deep copy, use it.
1159 // Currently supported only by texture-backed bitmaps.
1160 if (fPixelRef) {
1161 SkPixelRef* pixelRef = fPixelRef->deepCopy(dstConfig);
1162 if (pixelRef) {
scroggo@google.coma2a31922012-12-07 19:14:45 +00001163 uint32_t rowBytes;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001164 if (this->colorType() == dstCT) {
scroggo@google.com5ccae2c2014-01-15 16:56:52 +00001165 // Since there is no subset to pass to deepCopy, and deepCopy
1166 // succeeded, the new pixel ref must be identical.
1167 SkASSERT(fPixelRef->info() == pixelRef->info());
commit-bot@chromium.org50a30432013-10-24 17:44:27 +00001168 pixelRef->cloneGenID(*fPixelRef);
scroggo@google.coma2a31922012-12-07 19:14:45 +00001169 // Use the same rowBytes as the original.
1170 rowBytes = fRowBytes;
1171 } else {
1172 // With the new config, an appropriate fRowBytes will be computed by setConfig.
1173 rowBytes = 0;
scroggo@google.comd5764e82012-08-22 15:00:05 +00001174 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001175
1176 SkImageInfo info = fInfo;
1177 info.fColorType = dstCT;
1178 if (!dst->setConfig(info, rowBytes)) {
1179 return false;
1180 }
reed@google.com672588b2014-01-08 15:42:01 +00001181 dst->setPixelRef(pixelRef, fPixelRefOrigin)->unref();
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001182 return true;
1183 }
1184 }
1185
1186 if (this->getTexture()) {
1187 return false;
1188 } else {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +00001189 return this->copyTo(dst, dstCT, NULL);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001190 }
1191}
1192
commit-bot@chromium.org6285f4f2014-02-20 19:08:07 +00001193bool SkBitmap::deepCopyTo(SkBitmap* dst) const {
1194 return this->deepCopyTo(dst, this->config());
1195}
1196
reed@android.com8a1c16f2008-12-17 15:59:43 +00001197///////////////////////////////////////////////////////////////////////////////
1198///////////////////////////////////////////////////////////////////////////////
1199
1200static void downsampleby2_proc32(SkBitmap* dst, int x, int y,
1201 const SkBitmap& src) {
1202 x <<= 1;
1203 y <<= 1;
1204 const SkPMColor* p = src.getAddr32(x, y);
reed@android.com829c83c2009-06-08 12:05:31 +00001205 const SkPMColor* baseP = p;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001206 SkPMColor c, ag, rb;
1207
1208 c = *p; ag = (c >> 8) & 0xFF00FF; rb = c & 0xFF00FF;
1209 if (x < src.width() - 1) {
1210 p += 1;
1211 }
1212 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
1213
reed@android.com829c83c2009-06-08 12:05:31 +00001214 p = baseP;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001215 if (y < src.height() - 1) {
reed@android.com829c83c2009-06-08 12:05:31 +00001216 p += src.rowBytes() >> 2;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001217 }
1218 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
1219 if (x < src.width() - 1) {
1220 p += 1;
1221 }
1222 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
1223
1224 *dst->getAddr32(x >> 1, y >> 1) =
1225 ((rb >> 2) & 0xFF00FF) | ((ag << 6) & 0xFF00FF00);
1226}
1227
1228static inline uint32_t expand16(U16CPU c) {
1229 return (c & ~SK_G16_MASK_IN_PLACE) | ((c & SK_G16_MASK_IN_PLACE) << 16);
1230}
1231
1232// returns dirt in the top 16bits, but we don't care, since we only
1233// store the low 16bits.
1234static inline U16CPU pack16(uint32_t c) {
1235 return (c & ~SK_G16_MASK_IN_PLACE) | ((c >> 16) & SK_G16_MASK_IN_PLACE);
1236}
1237
1238static void downsampleby2_proc16(SkBitmap* dst, int x, int y,
1239 const SkBitmap& src) {
1240 x <<= 1;
1241 y <<= 1;
1242 const uint16_t* p = src.getAddr16(x, y);
reed@android.com829c83c2009-06-08 12:05:31 +00001243 const uint16_t* baseP = p;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001244 SkPMColor c;
weita@google.comf9ab99a2009-05-03 18:23:30 +00001245
reed@android.com8a1c16f2008-12-17 15:59:43 +00001246 c = expand16(*p);
reed@android.com829c83c2009-06-08 12:05:31 +00001247 if (x < src.width() - 1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001248 p += 1;
1249 }
1250 c += expand16(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001251
reed@android.com829c83c2009-06-08 12:05:31 +00001252 p = baseP;
1253 if (y < src.height() - 1) {
1254 p += src.rowBytes() >> 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001255 }
1256 c += expand16(*p);
reed@android.com829c83c2009-06-08 12:05:31 +00001257 if (x < src.width() - 1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001258 p += 1;
1259 }
1260 c += expand16(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001261
reed@android.com8a1c16f2008-12-17 15:59:43 +00001262 *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)pack16(c >> 2);
1263}
1264
1265static uint32_t expand4444(U16CPU c) {
1266 return (c & 0xF0F) | ((c & ~0xF0F) << 12);
1267}
1268
1269static U16CPU collaps4444(uint32_t c) {
1270 return (c & 0xF0F) | ((c >> 12) & ~0xF0F);
1271}
1272
1273static void downsampleby2_proc4444(SkBitmap* dst, int x, int y,
1274 const SkBitmap& src) {
1275 x <<= 1;
1276 y <<= 1;
1277 const uint16_t* p = src.getAddr16(x, y);
reed@android.com829c83c2009-06-08 12:05:31 +00001278 const uint16_t* baseP = p;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001279 uint32_t c;
weita@google.comf9ab99a2009-05-03 18:23:30 +00001280
reed@android.com8a1c16f2008-12-17 15:59:43 +00001281 c = expand4444(*p);
1282 if (x < src.width() - 1) {
1283 p += 1;
1284 }
1285 c += expand4444(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001286
reed@android.com829c83c2009-06-08 12:05:31 +00001287 p = baseP;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001288 if (y < src.height() - 1) {
reed@android.com829c83c2009-06-08 12:05:31 +00001289 p += src.rowBytes() >> 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001290 }
1291 c += expand4444(*p);
1292 if (x < src.width() - 1) {
1293 p += 1;
1294 }
1295 c += expand4444(*p);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001296
reed@android.com8a1c16f2008-12-17 15:59:43 +00001297 *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)collaps4444(c >> 2);
1298}
1299
1300void SkBitmap::buildMipMap(bool forceRebuild) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001301 if (forceRebuild)
1302 this->freeMipMap();
1303 else if (fMipMap)
1304 return; // we're already built
1305
1306 SkASSERT(NULL == fMipMap);
1307
1308 void (*proc)(SkBitmap* dst, int x, int y, const SkBitmap& src);
1309
reed@google.com44699382013-10-31 17:28:30 +00001310 const SkBitmap::Config config = this->config();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001311
1312 switch (config) {
1313 case kARGB_8888_Config:
1314 proc = downsampleby2_proc32;
1315 break;
1316 case kRGB_565_Config:
1317 proc = downsampleby2_proc16;
1318 break;
1319 case kARGB_4444_Config:
1320 proc = downsampleby2_proc4444;
1321 break;
1322 case kIndex8_Config:
1323 case kA8_Config:
1324 default:
1325 return; // don't build mipmaps for these configs
1326 }
reed@android.com89bb83a2009-05-29 21:30:42 +00001327
reed@android.com149e2f62009-05-22 14:39:03 +00001328 SkAutoLockPixels alp(*this);
1329 if (!this->readyToDraw()) {
1330 return;
1331 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001332
1333 // whip through our loop to compute the exact size needed
1334 size_t size = 0;
1335 int maxLevels = 0;
1336 {
reed@android.com149e2f62009-05-22 14:39:03 +00001337 int width = this->width();
1338 int height = this->height();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001339 for (;;) {
1340 width >>= 1;
1341 height >>= 1;
1342 if (0 == width || 0 == height) {
1343 break;
1344 }
1345 size += ComputeRowBytes(config, width) * height;
1346 maxLevels += 1;
1347 }
1348 }
reed@android.com89bb83a2009-05-29 21:30:42 +00001349
reed@android.com149e2f62009-05-22 14:39:03 +00001350 // nothing to build
reed@android.com8a1c16f2008-12-17 15:59:43 +00001351 if (0 == maxLevels) {
1352 return;
1353 }
1354
reed@android.com149e2f62009-05-22 14:39:03 +00001355 SkBitmap srcBM(*this);
1356 srcBM.lockPixels();
1357 if (!srcBM.readyToDraw()) {
1358 return;
1359 }
1360
1361 MipMap* mm = MipMap::Alloc(maxLevels, size);
1362 if (NULL == mm) {
1363 return;
1364 }
1365
reed@android.com8a1c16f2008-12-17 15:59:43 +00001366 MipLevel* level = mm->levels();
1367 uint8_t* addr = (uint8_t*)mm->pixels();
reed@android.com149e2f62009-05-22 14:39:03 +00001368 int width = this->width();
1369 int height = this->height();
scroggo@google.come5f48242013-02-25 21:47:41 +00001370 uint32_t rowBytes;
reed@android.com149e2f62009-05-22 14:39:03 +00001371 SkBitmap dstBM;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001372
1373 for (int i = 0; i < maxLevels; i++) {
1374 width >>= 1;
1375 height >>= 1;
scroggo@google.come5f48242013-02-25 21:47:41 +00001376 rowBytes = SkToU32(ComputeRowBytes(config, width));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001377
1378 level[i].fPixels = addr;
reed@android.comf459a492009-03-27 12:33:50 +00001379 level[i].fWidth = width;
1380 level[i].fHeight = height;
reed@android.com49f0ff22009-03-19 21:52:42 +00001381 level[i].fRowBytes = rowBytes;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001382
1383 dstBM.setConfig(config, width, height, rowBytes);
1384 dstBM.setPixels(addr);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001385
bungeman@google.com7cf0e9e2012-07-25 16:09:10 +00001386 srcBM.lockPixels();
reed@android.com149e2f62009-05-22 14:39:03 +00001387 for (int y = 0; y < height; y++) {
1388 for (int x = 0; x < width; x++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001389 proc(&dstBM, x, y, srcBM);
1390 }
1391 }
bungeman@google.com7cf0e9e2012-07-25 16:09:10 +00001392 srcBM.unlockPixels();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001393
1394 srcBM = dstBM;
1395 addr += height * rowBytes;
1396 }
1397 SkASSERT(addr == (uint8_t*)mm->pixels() + size);
1398 fMipMap = mm;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001399}
1400
1401bool SkBitmap::hasMipMap() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001402 return fMipMap != NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001403}
1404
1405int SkBitmap::extractMipLevel(SkBitmap* dst, SkFixed sx, SkFixed sy) {
reed@android.com83f7bc32009-07-17 02:42:41 +00001406 if (NULL == fMipMap) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001407 return 0;
reed@android.com83f7bc32009-07-17 02:42:41 +00001408 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001409
reed@android.com8a1c16f2008-12-17 15:59:43 +00001410 int level = ComputeMipLevel(sx, sy) >> 16;
1411 SkASSERT(level >= 0);
1412 if (level <= 0) {
1413 return 0;
1414 }
1415
1416 if (level >= fMipMap->fLevelCount) {
1417 level = fMipMap->fLevelCount - 1;
1418 }
1419 if (dst) {
1420 const MipLevel& mip = fMipMap->levels()[level - 1];
1421 dst->setConfig((SkBitmap::Config)this->config(),
1422 mip.fWidth, mip.fHeight, mip.fRowBytes);
1423 dst->setPixels(mip.fPixels);
1424 }
1425 return level;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001426}
1427
1428SkFixed SkBitmap::ComputeMipLevel(SkFixed sx, SkFixed sy) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001429 sx = SkAbs32(sx);
1430 sy = SkAbs32(sy);
1431 if (sx < sy) {
1432 sx = sy;
1433 }
1434 if (sx < SK_Fixed1) {
1435 return 0;
1436 }
1437 int clz = SkCLZ(sx);
1438 SkASSERT(clz >= 1 && clz <= 15);
1439 return SkIntToFixed(15 - clz) + ((unsigned)(sx << (clz + 1)) >> 16);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001440}
1441
1442///////////////////////////////////////////////////////////////////////////////
1443
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +00001444static bool GetBitmapAlpha(const SkBitmap& src, uint8_t* SK_RESTRICT alpha,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001445 int alphaRowBytes) {
1446 SkASSERT(alpha != NULL);
1447 SkASSERT(alphaRowBytes >= src.width());
1448
reed@google.com44699382013-10-31 17:28:30 +00001449 SkBitmap::Config config = src.config();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001450 int w = src.width();
1451 int h = src.height();
scroggo@google.come5f48242013-02-25 21:47:41 +00001452 size_t rb = src.rowBytes();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001453
reed@android.com1cdcb512009-08-24 19:11:00 +00001454 SkAutoLockPixels alp(src);
1455 if (!src.readyToDraw()) {
1456 // zero out the alpha buffer and return
1457 while (--h >= 0) {
1458 memset(alpha, 0, w);
1459 alpha += alphaRowBytes;
1460 }
1461 return false;
1462 }
reed@google.com82065d62011-02-07 15:30:46 +00001463
reed@android.com8a1c16f2008-12-17 15:59:43 +00001464 if (SkBitmap::kA8_Config == config && !src.isOpaque()) {
1465 const uint8_t* s = src.getAddr8(0, 0);
1466 while (--h >= 0) {
1467 memcpy(alpha, s, w);
1468 s += rb;
1469 alpha += alphaRowBytes;
1470 }
1471 } else if (SkBitmap::kARGB_8888_Config == config && !src.isOpaque()) {
1472 const SkPMColor* SK_RESTRICT s = src.getAddr32(0, 0);
1473 while (--h >= 0) {
1474 for (int x = 0; x < w; x++) {
1475 alpha[x] = SkGetPackedA32(s[x]);
1476 }
1477 s = (const SkPMColor*)((const char*)s + rb);
1478 alpha += alphaRowBytes;
1479 }
1480 } else if (SkBitmap::kARGB_4444_Config == config && !src.isOpaque()) {
1481 const SkPMColor16* SK_RESTRICT s = src.getAddr16(0, 0);
1482 while (--h >= 0) {
1483 for (int x = 0; x < w; x++) {
1484 alpha[x] = SkPacked4444ToA32(s[x]);
1485 }
1486 s = (const SkPMColor16*)((const char*)s + rb);
1487 alpha += alphaRowBytes;
1488 }
1489 } else if (SkBitmap::kIndex8_Config == config && !src.isOpaque()) {
1490 SkColorTable* ct = src.getColorTable();
1491 if (ct) {
1492 const SkPMColor* SK_RESTRICT table = ct->lockColors();
1493 const uint8_t* SK_RESTRICT s = src.getAddr8(0, 0);
1494 while (--h >= 0) {
1495 for (int x = 0; x < w; x++) {
1496 alpha[x] = SkGetPackedA32(table[s[x]]);
1497 }
1498 s += rb;
1499 alpha += alphaRowBytes;
1500 }
reed@google.com0a6151d2013-10-10 14:44:56 +00001501 ct->unlockColors();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001502 }
1503 } else { // src is opaque, so just fill alpha[] with 0xFF
1504 memset(alpha, 0xFF, h * alphaRowBytes);
1505 }
reed@android.com1cdcb512009-08-24 19:11:00 +00001506 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001507}
1508
1509#include "SkPaint.h"
1510#include "SkMaskFilter.h"
1511#include "SkMatrix.h"
1512
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001513bool SkBitmap::extractAlpha(SkBitmap* dst, const SkPaint* paint,
djsollen@google.com57f49692011-02-23 20:46:31 +00001514 Allocator *allocator, SkIPoint* offset) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001515 SkDEBUGCODE(this->validate();)
1516
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001517 SkBitmap tmpBitmap;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001518 SkMatrix identity;
1519 SkMask srcM, dstM;
1520
1521 srcM.fBounds.set(0, 0, this->width(), this->height());
1522 srcM.fRowBytes = SkAlign4(this->width());
1523 srcM.fFormat = SkMask::kA8_Format;
1524
1525 SkMaskFilter* filter = paint ? paint->getMaskFilter() : NULL;
1526
1527 // compute our (larger?) dst bounds if we have a filter
1528 if (NULL != filter) {
1529 identity.reset();
1530 srcM.fImage = NULL;
1531 if (!filter->filterMask(&dstM, srcM, identity, NULL)) {
1532 goto NO_FILTER_CASE;
1533 }
1534 dstM.fRowBytes = SkAlign4(dstM.fBounds.width());
1535 } else {
1536 NO_FILTER_CASE:
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001537 tmpBitmap.setConfig(SkBitmap::kA8_Config, this->width(), this->height(),
reed@android.com8a1c16f2008-12-17 15:59:43 +00001538 srcM.fRowBytes);
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001539 if (!tmpBitmap.allocPixels(allocator, NULL)) {
1540 // Allocation of pixels for alpha bitmap failed.
1541 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
1542 tmpBitmap.width(), tmpBitmap.height());
1543 return false;
1544 }
1545 GetBitmapAlpha(*this, tmpBitmap.getAddr8(0, 0), srcM.fRowBytes);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001546 if (offset) {
1547 offset->set(0, 0);
1548 }
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001549 tmpBitmap.swap(*dst);
1550 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001551 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001552 srcM.fImage = SkMask::AllocImage(srcM.computeImageSize());
1553 SkAutoMaskFreeImage srcCleanup(srcM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001554
1555 GetBitmapAlpha(*this, srcM.fImage, srcM.fRowBytes);
1556 if (!filter->filterMask(&dstM, srcM, identity, NULL)) {
1557 goto NO_FILTER_CASE;
1558 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001559 SkAutoMaskFreeImage dstCleanup(dstM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001560
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001561 tmpBitmap.setConfig(SkBitmap::kA8_Config, dstM.fBounds.width(),
reed@android.com8a1c16f2008-12-17 15:59:43 +00001562 dstM.fBounds.height(), dstM.fRowBytes);
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001563 if (!tmpBitmap.allocPixels(allocator, NULL)) {
1564 // Allocation of pixels for alpha bitmap failed.
1565 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
1566 tmpBitmap.width(), tmpBitmap.height());
1567 return false;
1568 }
1569 memcpy(tmpBitmap.getPixels(), dstM.fImage, dstM.computeImageSize());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001570 if (offset) {
1571 offset->set(dstM.fBounds.fLeft, dstM.fBounds.fTop);
1572 }
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001573 SkDEBUGCODE(tmpBitmap.validate();)
1574
1575 tmpBitmap.swap(*dst);
1576 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001577}
1578
1579///////////////////////////////////////////////////////////////////////////////
1580
1581enum {
1582 SERIALIZE_PIXELTYPE_NONE,
djsollen@google.com21830d92012-08-07 19:49:41 +00001583 SERIALIZE_PIXELTYPE_REF_DATA
reed@android.com8a1c16f2008-12-17 15:59:43 +00001584};
1585
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +00001586void SkBitmap::flatten(SkWriteBuffer& buffer) const {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001587 fInfo.flatten(buffer);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001588 buffer.writeInt(fRowBytes);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001589
reed@android.com8a1c16f2008-12-17 15:59:43 +00001590 if (fPixelRef) {
djsollen@google.com5370cd92012-03-28 20:47:01 +00001591 if (fPixelRef->getFactory()) {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001592 buffer.writeInt(SERIALIZE_PIXELTYPE_REF_DATA);
reed@google.com672588b2014-01-08 15:42:01 +00001593 buffer.writeInt(fPixelRefOrigin.fX);
1594 buffer.writeInt(fPixelRefOrigin.fY);
djsollen@google.com5370cd92012-03-28 20:47:01 +00001595 buffer.writeFlattenable(fPixelRef);
1596 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001597 }
1598 // if we get here, we can't record the pixels
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001599 buffer.writeInt(SERIALIZE_PIXELTYPE_NONE);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001600 } else {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001601 buffer.writeInt(SERIALIZE_PIXELTYPE_NONE);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001602 }
1603}
1604
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +00001605void SkBitmap::unflatten(SkReadBuffer& buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001606 this->reset();
weita@google.comf9ab99a2009-05-03 18:23:30 +00001607
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001608 SkImageInfo info;
1609 info.unflatten(buffer);
1610 size_t rowBytes = buffer.readInt();
commit-bot@chromium.org33fed142014-02-13 18:46:13 +00001611 if (!buffer.validate((info.width() >= 0) && (info.height() >= 0) &&
1612 SkColorTypeIsValid(info.fColorType) &&
1613 SkAlphaTypeIsValid(info.fAlphaType) &&
1614 validate_alphaType(info.fColorType, info.fAlphaType) &&
1615 info.validRowBytes(rowBytes))) {
1616 return;
1617 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001618
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001619 bool configIsValid = this->setConfig(info, rowBytes);
1620 buffer.validate(configIsValid);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001621
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001622 int reftype = buffer.readInt();
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +00001623 if (buffer.validate((SERIALIZE_PIXELTYPE_REF_DATA == reftype) ||
1624 (SERIALIZE_PIXELTYPE_NONE == reftype))) {
1625 switch (reftype) {
1626 case SERIALIZE_PIXELTYPE_REF_DATA: {
reed@google.com672588b2014-01-08 15:42:01 +00001627 SkIPoint origin;
1628 origin.fX = buffer.readInt();
1629 origin.fY = buffer.readInt();
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001630 size_t offset = origin.fY * rowBytes + origin.fX * info.bytesPerPixel();
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +00001631 SkPixelRef* pr = buffer.readPixelRef();
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +00001632 if (!buffer.validate((NULL == pr) ||
1633 (pr->getAllocatedSizeInBytes() >= (offset + this->getSafeSize())))) {
reed@google.com672588b2014-01-08 15:42:01 +00001634 origin.setZero();
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +00001635 }
reed@google.com672588b2014-01-08 15:42:01 +00001636 SkSafeUnref(this->setPixelRef(pr, origin));
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +00001637 break;
1638 }
1639 case SERIALIZE_PIXELTYPE_NONE:
1640 break;
1641 default:
1642 SkDEBUGFAIL("unrecognized pixeltype in serialized data");
1643 sk_throw();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001644 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001645 }
1646}
1647
1648///////////////////////////////////////////////////////////////////////////////
1649
1650SkBitmap::RLEPixels::RLEPixels(int width, int height) {
1651 fHeight = height;
commit-bot@chromium.org235002f2013-10-09 18:39:59 +00001652 fYPtrs = (uint8_t**)sk_calloc_throw(height * sizeof(uint8_t*));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001653}
1654
1655SkBitmap::RLEPixels::~RLEPixels() {
1656 sk_free(fYPtrs);
1657}
1658
1659///////////////////////////////////////////////////////////////////////////////
1660
1661#ifdef SK_DEBUG
1662void SkBitmap::validate() const {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001663 fInfo.validate();
commit-bot@chromium.orgd5414e52014-02-13 22:30:38 +00001664
1665 // ImageInfo may not require this, but Bitmap ensures that opaque-only
1666 // colorTypes report opaque for their alphatype
1667 if (kRGB_565_SkColorType == fInfo.colorType()) {
1668 SkASSERT(kOpaque_SkAlphaType == fInfo.alphaType());
1669 }
1670
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001671 SkASSERT(fInfo.validRowBytes(fRowBytes));
scroggo@google.com8e990eb2013-06-14 15:55:56 +00001672 uint8_t allFlags = kImageIsOpaque_Flag | kImageIsVolatile_Flag | kImageIsImmutable_Flag;
1673#ifdef SK_BUILD_FOR_ANDROID
1674 allFlags |= kHasHardwareMipMap_Flag;
1675#endif
1676 SkASSERT(fFlags <= allFlags);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001677 SkASSERT(fPixelLockCount >= 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001678
reed@google.com615316c2014-01-15 19:15:23 +00001679 if (fPixels) {
1680 SkASSERT(fPixelRef);
1681 SkASSERT(fPixelLockCount > 0);
1682 SkASSERT(fPixelRef->isLocked());
1683 SkASSERT(fPixelRef->rowBytes() == fRowBytes);
1684 SkASSERT(fPixelRefOrigin.fX >= 0);
1685 SkASSERT(fPixelRefOrigin.fY >= 0);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001686 SkASSERT(fPixelRef->info().width() >= (int)this->width() + fPixelRefOrigin.fX);
1687 SkASSERT(fPixelRef->info().fHeight >= (int)this->height() + fPixelRefOrigin.fY);
1688 SkASSERT(fPixelRef->rowBytes() >= fInfo.minRowBytes());
reed@google.com615316c2014-01-15 19:15:23 +00001689 } else {
1690 SkASSERT(NULL == fColorTable);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001691 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001692}
1693#endif
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001694
1695#ifdef SK_DEVELOPER
1696void SkBitmap::toString(SkString* str) const {
1697
1698 static const char* gConfigNames[kConfigCount] = {
rmistry@google.comd6bab022013-12-02 13:50:38 +00001699 "NONE", "A8", "INDEX8", "565", "4444", "8888"
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001700 };
1701
1702 str->appendf("bitmap: ((%d, %d) %s", this->width(), this->height(),
1703 gConfigNames[this->config()]);
1704
1705 str->append(" (");
1706 if (this->isOpaque()) {
1707 str->append("opaque");
1708 } else {
1709 str->append("transparent");
1710 }
1711 if (this->isImmutable()) {
1712 str->append(", immutable");
1713 } else {
1714 str->append(", not-immutable");
1715 }
1716 str->append(")");
1717
1718 SkPixelRef* pr = this->pixelRef();
1719 if (NULL == pr) {
1720 // show null or the explicit pixel address (rare)
1721 str->appendf(" pixels:%p", this->getPixels());
1722 } else {
1723 const char* uri = pr->getURI();
1724 if (NULL != uri) {
1725 str->appendf(" uri:\"%s\"", uri);
1726 } else {
1727 str->appendf(" pixelref:%p", pr);
1728 }
1729 }
1730
1731 str->append(")");
1732}
1733#endif
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001734
1735///////////////////////////////////////////////////////////////////////////////
1736
1737#ifdef SK_DEBUG
1738void SkImageInfo::validate() const {
1739 SkASSERT(fWidth >= 0);
1740 SkASSERT(fHeight >= 0);
1741 SkASSERT(SkColorTypeIsValid(fColorType));
1742 SkASSERT(SkAlphaTypeIsValid(fAlphaType));
1743}
1744#endif