epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2008 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "SkBitmap.h" |
| 11 | #include "SkColorPriv.h" |
| 12 | #include "SkDither.h" |
| 13 | #include "SkFlattenable.h" |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 14 | #include "SkImagePriv.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | #include "SkMallocPixelRef.h" |
| 16 | #include "SkMask.h" |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 17 | #include "SkOrderedReadBuffer.h" |
| 18 | #include "SkOrderedWriteBuffer.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | #include "SkPixelRef.h" |
| 20 | #include "SkThread.h" |
vandebo@chromium.org | 112706d | 2011-02-24 22:50:55 +0000 | [diff] [blame] | 21 | #include "SkUnPreMultiply.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | #include "SkUtils.h" |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 23 | #include "SkValidationUtils.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 24 | #include "SkPackBits.h" |
| 25 | #include <new> |
| 26 | |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 27 | static bool isPos32Bits(const Sk64& value) { |
| 28 | return !value.isNeg() && value.is32(); |
| 29 | } |
| 30 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | struct MipLevel { |
| 32 | void* fPixels; |
| 33 | uint32_t fRowBytes; |
reed@android.com | f459a49 | 2009-03-27 12:33:50 +0000 | [diff] [blame] | 34 | uint32_t fWidth, fHeight; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | struct SkBitmap::MipMap : SkNoncopyable { |
| 38 | int32_t fRefCnt; |
| 39 | int fLevelCount; |
| 40 | // MipLevel fLevel[fLevelCount]; |
| 41 | // Pixels[] |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 42 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 43 | static MipMap* Alloc(int levelCount, size_t pixelSize) { |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 44 | if (levelCount < 0) { |
| 45 | return NULL; |
| 46 | } |
| 47 | Sk64 size; |
| 48 | size.setMul(levelCount + 1, sizeof(MipLevel)); |
| 49 | size.add(sizeof(MipMap)); |
scroggo@google.com | e5f4824 | 2013-02-25 21:47:41 +0000 | [diff] [blame] | 50 | size.add(SkToS32(pixelSize)); |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 51 | if (!isPos32Bits(size)) { |
| 52 | return NULL; |
| 53 | } |
| 54 | MipMap* mm = (MipMap*)sk_malloc_throw(size.get32()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | mm->fRefCnt = 1; |
| 56 | mm->fLevelCount = levelCount; |
| 57 | return mm; |
| 58 | } |
| 59 | |
| 60 | const MipLevel* levels() const { return (const MipLevel*)(this + 1); } |
| 61 | MipLevel* levels() { return (MipLevel*)(this + 1); } |
| 62 | |
| 63 | const void* pixels() const { return levels() + fLevelCount; } |
| 64 | void* pixels() { return levels() + fLevelCount; } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 65 | |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 66 | void ref() { |
| 67 | if (SK_MaxS32 == sk_atomic_inc(&fRefCnt)) { |
| 68 | sk_throw(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 71 | void unref() { |
| 72 | SkASSERT(fRefCnt > 0); |
| 73 | if (sk_atomic_dec(&fRefCnt) == 1) { |
| 74 | sk_free(this); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | |
| 79 | /////////////////////////////////////////////////////////////////////////////// |
| 80 | /////////////////////////////////////////////////////////////////////////////// |
| 81 | |
| 82 | SkBitmap::SkBitmap() { |
reed@android.com | 4516f47 | 2009-06-29 16:25:36 +0000 | [diff] [blame] | 83 | sk_bzero(this, sizeof(*this)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | SkBitmap::SkBitmap(const SkBitmap& src) { |
| 87 | SkDEBUGCODE(src.validate();) |
reed@android.com | 4516f47 | 2009-06-29 16:25:36 +0000 | [diff] [blame] | 88 | sk_bzero(this, sizeof(*this)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 89 | *this = src; |
| 90 | SkDEBUGCODE(this->validate();) |
| 91 | } |
| 92 | |
| 93 | SkBitmap::~SkBitmap() { |
| 94 | SkDEBUGCODE(this->validate();) |
| 95 | this->freePixels(); |
| 96 | } |
| 97 | |
| 98 | SkBitmap& SkBitmap::operator=(const SkBitmap& src) { |
| 99 | if (this != &src) { |
| 100 | this->freePixels(); |
| 101 | memcpy(this, &src, sizeof(src)); |
| 102 | |
| 103 | // inc src reference counts |
reed@android.com | 83f7bc3 | 2009-07-17 02:42:41 +0000 | [diff] [blame] | 104 | SkSafeRef(src.fPixelRef); |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 105 | SkSafeRef(src.fMipMap); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 | |
| 107 | // we reset our locks if we get blown away |
| 108 | fPixelLockCount = 0; |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 109 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 110 | /* The src could be in 3 states |
| 111 | 1. no pixelref, in which case we just copy/ref the pixels/ctable |
| 112 | 2. unlocked pixelref, pixels/ctable should be null |
| 113 | 3. locked pixelref, we should lock the ref again ourselves |
| 114 | */ |
| 115 | if (NULL == fPixelRef) { |
| 116 | // leave fPixels as it is |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 117 | SkSafeRef(fColorTable); // ref the user's ctable if present |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 118 | } else { // we have a pixelref, so pixels/ctable reflect it |
| 119 | // ignore the values from the memcpy |
| 120 | fPixels = NULL; |
| 121 | fColorTable = NULL; |
bsalomon@google.com | 586f48c | 2011-04-14 15:07:22 +0000 | [diff] [blame] | 122 | // Note that what to for genID is somewhat arbitrary. We have no |
| 123 | // way to track changes to raw pixels across multiple SkBitmaps. |
| 124 | // Would benefit from an SkRawPixelRef type created by |
| 125 | // setPixels. |
| 126 | // Just leave the memcpy'ed one but they'll get out of sync |
| 127 | // as soon either is modified. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
| 131 | SkDEBUGCODE(this->validate();) |
| 132 | return *this; |
| 133 | } |
| 134 | |
| 135 | void SkBitmap::swap(SkBitmap& other) { |
bsalomon@google.com | 586f48c | 2011-04-14 15:07:22 +0000 | [diff] [blame] | 136 | SkTSwap(fColorTable, other.fColorTable); |
| 137 | SkTSwap(fPixelRef, other.fPixelRef); |
| 138 | SkTSwap(fPixelRefOffset, other.fPixelRefOffset); |
| 139 | SkTSwap(fPixelLockCount, other.fPixelLockCount); |
| 140 | SkTSwap(fMipMap, other.fMipMap); |
| 141 | SkTSwap(fPixels, other.fPixels); |
bsalomon@google.com | 586f48c | 2011-04-14 15:07:22 +0000 | [diff] [blame] | 142 | SkTSwap(fRowBytes, other.fRowBytes); |
| 143 | SkTSwap(fWidth, other.fWidth); |
| 144 | SkTSwap(fHeight, other.fHeight); |
| 145 | SkTSwap(fConfig, other.fConfig); |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 146 | SkTSwap(fAlphaType, other.fAlphaType); |
bsalomon@google.com | 586f48c | 2011-04-14 15:07:22 +0000 | [diff] [blame] | 147 | SkTSwap(fFlags, other.fFlags); |
| 148 | SkTSwap(fBytesPerPixel, other.fBytesPerPixel); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 149 | |
| 150 | SkDEBUGCODE(this->validate();) |
| 151 | } |
| 152 | |
| 153 | void SkBitmap::reset() { |
| 154 | this->freePixels(); |
reed@android.com | 4516f47 | 2009-06-29 16:25:36 +0000 | [diff] [blame] | 155 | sk_bzero(this, sizeof(*this)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | int SkBitmap::ComputeBytesPerPixel(SkBitmap::Config config) { |
| 159 | int bpp; |
| 160 | switch (config) { |
| 161 | case kNo_Config: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 162 | bpp = 0; // not applicable |
| 163 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 164 | case kA8_Config: |
| 165 | case kIndex8_Config: |
| 166 | bpp = 1; |
| 167 | break; |
| 168 | case kRGB_565_Config: |
| 169 | case kARGB_4444_Config: |
| 170 | bpp = 2; |
| 171 | break; |
| 172 | case kARGB_8888_Config: |
| 173 | bpp = 4; |
| 174 | break; |
| 175 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 176 | SkDEBUGFAIL("unknown config"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 177 | bpp = 0; // error |
| 178 | break; |
| 179 | } |
| 180 | return bpp; |
| 181 | } |
| 182 | |
scroggo@google.com | 0ba4bf4 | 2013-02-25 16:02:36 +0000 | [diff] [blame] | 183 | size_t SkBitmap::ComputeRowBytes(Config c, int width) { |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 184 | if (width < 0) { |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | Sk64 rowBytes; |
| 189 | rowBytes.setZero(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 190 | |
| 191 | switch (c) { |
| 192 | case kNo_Config: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 193 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 194 | case kA8_Config: |
| 195 | case kIndex8_Config: |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 196 | rowBytes.set(width); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 197 | break; |
| 198 | case kRGB_565_Config: |
| 199 | case kARGB_4444_Config: |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 200 | rowBytes.set(width); |
| 201 | rowBytes.shiftLeft(1); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 202 | break; |
| 203 | case kARGB_8888_Config: |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 204 | rowBytes.set(width); |
| 205 | rowBytes.shiftLeft(2); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 206 | break; |
| 207 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 208 | SkDEBUGFAIL("unknown config"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 209 | break; |
| 210 | } |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 211 | return isPos32Bits(rowBytes) ? rowBytes.get32() : 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | Sk64 SkBitmap::ComputeSize64(Config c, int width, int height) { |
| 215 | Sk64 size; |
scroggo@google.com | e5f4824 | 2013-02-25 21:47:41 +0000 | [diff] [blame] | 216 | size.setMul(SkToS32(SkBitmap::ComputeRowBytes(c, width)), height); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 217 | return size; |
| 218 | } |
| 219 | |
| 220 | size_t SkBitmap::ComputeSize(Config c, int width, int height) { |
| 221 | Sk64 size = SkBitmap::ComputeSize64(c, width, height); |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 222 | return isPos32Bits(size) ? size.get32() : 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 223 | } |
| 224 | |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 225 | Sk64 SkBitmap::ComputeSafeSize64(Config config, |
| 226 | uint32_t width, |
| 227 | uint32_t height, |
scroggo@google.com | 0ba4bf4 | 2013-02-25 16:02:36 +0000 | [diff] [blame] | 228 | size_t rowBytes) { |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 229 | Sk64 safeSize; |
| 230 | safeSize.setZero(); |
| 231 | if (height > 0) { |
scroggo@google.com | e5f4824 | 2013-02-25 21:47:41 +0000 | [diff] [blame] | 232 | // TODO: Handle the case where the return value from |
| 233 | // ComputeRowBytes is more than 31 bits. |
| 234 | safeSize.set(SkToS32(ComputeRowBytes(config, width))); |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 235 | Sk64 sizeAllButLastRow; |
scroggo@google.com | e5f4824 | 2013-02-25 21:47:41 +0000 | [diff] [blame] | 236 | sizeAllButLastRow.setMul(height - 1, SkToS32(rowBytes)); |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 237 | safeSize.add(sizeAllButLastRow); |
| 238 | } |
| 239 | SkASSERT(!safeSize.isNeg()); |
| 240 | return safeSize; |
| 241 | } |
| 242 | |
| 243 | size_t SkBitmap::ComputeSafeSize(Config config, |
| 244 | uint32_t width, |
| 245 | uint32_t height, |
scroggo@google.com | 0ba4bf4 | 2013-02-25 16:02:36 +0000 | [diff] [blame] | 246 | size_t rowBytes) { |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 247 | Sk64 safeSize = ComputeSafeSize64(config, width, height, rowBytes); |
| 248 | return (safeSize.is32() ? safeSize.get32() : 0); |
| 249 | } |
| 250 | |
reed@google.com | 86b2e43 | 2012-03-15 21:17:03 +0000 | [diff] [blame] | 251 | void SkBitmap::getBounds(SkRect* bounds) const { |
| 252 | SkASSERT(bounds); |
| 253 | bounds->set(0, 0, |
| 254 | SkIntToScalar(fWidth), SkIntToScalar(fHeight)); |
| 255 | } |
| 256 | |
reed@google.com | 80e1459 | 2012-03-16 14:58:07 +0000 | [diff] [blame] | 257 | void SkBitmap::getBounds(SkIRect* bounds) const { |
| 258 | SkASSERT(bounds); |
| 259 | bounds->set(0, 0, fWidth, fHeight); |
| 260 | } |
| 261 | |
reed@google.com | 86b2e43 | 2012-03-15 21:17:03 +0000 | [diff] [blame] | 262 | /////////////////////////////////////////////////////////////////////////////// |
| 263 | |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 264 | static bool validate_alphaType(SkBitmap::Config config, SkAlphaType alphaType, |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 265 | SkAlphaType* canonical = NULL) { |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 266 | switch (config) { |
| 267 | case SkBitmap::kNo_Config: |
| 268 | alphaType = kIgnore_SkAlphaType; |
| 269 | break; |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 270 | case SkBitmap::kA8_Config: |
| 271 | if (kUnpremul_SkAlphaType == alphaType) { |
| 272 | alphaType = kPremul_SkAlphaType; |
| 273 | } |
| 274 | // fall-through |
| 275 | case SkBitmap::kIndex8_Config: |
| 276 | case SkBitmap::kARGB_4444_Config: |
| 277 | case SkBitmap::kARGB_8888_Config: |
| 278 | if (kIgnore_SkAlphaType == alphaType) { |
| 279 | return false; |
| 280 | } |
| 281 | break; |
| 282 | case SkBitmap::kRGB_565_Config: |
| 283 | alphaType = kOpaque_SkAlphaType; |
| 284 | break; |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 285 | default: |
| 286 | return false; |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 287 | } |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 288 | if (canonical) { |
| 289 | *canonical = alphaType; |
| 290 | } |
| 291 | return true; |
| 292 | } |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 293 | |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 294 | bool SkBitmap::setConfig(Config config, int width, int height, size_t rowBytes, |
| 295 | SkAlphaType alphaType) { |
| 296 | if ((width | height) < 0) { |
reed@google.com | 9cd697c | 2013-10-21 14:12:13 +0000 | [diff] [blame] | 297 | goto BAD_CONFIG; |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 298 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 299 | if (rowBytes == 0) { |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 300 | rowBytes = SkBitmap::ComputeRowBytes(config, width); |
| 301 | if (0 == rowBytes && kNo_Config != config) { |
reed@google.com | 9cd697c | 2013-10-21 14:12:13 +0000 | [diff] [blame] | 302 | goto BAD_CONFIG; |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 303 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 304 | } |
reed@android.com | 89bb83a | 2009-05-29 21:30:42 +0000 | [diff] [blame] | 305 | |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 306 | if (!validate_alphaType(config, alphaType, &alphaType)) { |
reed@google.com | 9cd697c | 2013-10-21 14:12:13 +0000 | [diff] [blame] | 307 | goto BAD_CONFIG; |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | this->freePixels(); |
| 311 | |
| 312 | fConfig = SkToU8(config); |
| 313 | fAlphaType = SkToU8(alphaType); |
reed@android.com | f459a49 | 2009-03-27 12:33:50 +0000 | [diff] [blame] | 314 | fWidth = width; |
| 315 | fHeight = height; |
scroggo@google.com | e5f4824 | 2013-02-25 21:47:41 +0000 | [diff] [blame] | 316 | fRowBytes = SkToU32(rowBytes); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 317 | |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 318 | fBytesPerPixel = (uint8_t)ComputeBytesPerPixel(config); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 319 | |
| 320 | SkDEBUGCODE(this->validate();) |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 321 | return true; |
agl@chromium.org | 6b8cb25 | 2009-06-01 23:52:37 +0000 | [diff] [blame] | 322 | |
reed@android.com | 9e0c2fc | 2009-06-02 18:54:28 +0000 | [diff] [blame] | 323 | // if we got here, we had an error, so we reset the bitmap to empty |
reed@google.com | 9cd697c | 2013-10-21 14:12:13 +0000 | [diff] [blame] | 324 | BAD_CONFIG: |
agl@chromium.org | 6b8cb25 | 2009-06-01 23:52:37 +0000 | [diff] [blame] | 325 | this->reset(); |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 326 | return false; |
| 327 | } |
| 328 | |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 329 | bool SkBitmap::setConfig(const SkImageInfo& info, size_t rowBytes) { |
| 330 | return this->setConfig(SkImageInfoToBitmapConfig(info), info.fWidth, |
| 331 | info.fHeight, rowBytes, info.fAlphaType); |
| 332 | } |
| 333 | |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 334 | bool SkBitmap::setAlphaType(SkAlphaType alphaType) { |
| 335 | if (!validate_alphaType(this->config(), alphaType, &alphaType)) { |
| 336 | return false; |
| 337 | } |
| 338 | fAlphaType = SkToU8(alphaType); |
| 339 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | void SkBitmap::updatePixelsFromRef() const { |
| 343 | if (NULL != fPixelRef) { |
| 344 | if (fPixelLockCount > 0) { |
reed@google.com | ff0da4f | 2012-05-17 13:14:52 +0000 | [diff] [blame] | 345 | SkASSERT(fPixelRef->isLocked()); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 346 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 347 | void* p = fPixelRef->pixels(); |
| 348 | if (NULL != p) { |
| 349 | p = (char*)p + fPixelRefOffset; |
| 350 | } |
| 351 | fPixels = p; |
| 352 | SkRefCnt_SafeAssign(fColorTable, fPixelRef->colorTable()); |
| 353 | } else { |
| 354 | SkASSERT(0 == fPixelLockCount); |
| 355 | fPixels = NULL; |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 356 | if (fColorTable) { |
| 357 | fColorTable->unref(); |
| 358 | fColorTable = NULL; |
| 359 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | SkPixelRef* SkBitmap::setPixelRef(SkPixelRef* pr, size_t offset) { |
| 365 | // do this first, we that we never have a non-zero offset with a null ref |
| 366 | if (NULL == pr) { |
| 367 | offset = 0; |
| 368 | } |
| 369 | |
| 370 | if (fPixelRef != pr || fPixelRefOffset != offset) { |
| 371 | if (fPixelRef != pr) { |
| 372 | this->freePixels(); |
| 373 | SkASSERT(NULL == fPixelRef); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 374 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 375 | SkSafeRef(pr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 376 | fPixelRef = pr; |
| 377 | } |
| 378 | fPixelRefOffset = offset; |
| 379 | this->updatePixelsFromRef(); |
| 380 | } |
| 381 | |
| 382 | SkDEBUGCODE(this->validate();) |
| 383 | return pr; |
| 384 | } |
| 385 | |
| 386 | void SkBitmap::lockPixels() const { |
djsollen@google.com | 7c6d264 | 2013-08-06 12:19:38 +0000 | [diff] [blame] | 387 | if (NULL != fPixelRef && 0 == sk_atomic_inc(&fPixelLockCount)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 388 | fPixelRef->lockPixels(); |
| 389 | this->updatePixelsFromRef(); |
| 390 | } |
| 391 | SkDEBUGCODE(this->validate();) |
| 392 | } |
| 393 | |
| 394 | void SkBitmap::unlockPixels() const { |
| 395 | SkASSERT(NULL == fPixelRef || fPixelLockCount > 0); |
| 396 | |
djsollen@google.com | 7c6d264 | 2013-08-06 12:19:38 +0000 | [diff] [blame] | 397 | if (NULL != fPixelRef && 1 == sk_atomic_dec(&fPixelLockCount)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 398 | fPixelRef->unlockPixels(); |
| 399 | this->updatePixelsFromRef(); |
| 400 | } |
| 401 | SkDEBUGCODE(this->validate();) |
| 402 | } |
| 403 | |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 404 | bool SkBitmap::lockPixelsAreWritable() const { |
djsollen@google.com | c84b833 | 2012-07-27 13:41:44 +0000 | [diff] [blame] | 405 | return (fPixelRef) ? fPixelRef->lockPixelsAreWritable() : false; |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 406 | } |
| 407 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 408 | void SkBitmap::setPixels(void* p, SkColorTable* ctable) { |
reed@google.com | 8e1034e | 2012-07-30 13:16:35 +0000 | [diff] [blame] | 409 | if (NULL == p) { |
| 410 | this->setPixelRef(NULL, 0); |
| 411 | return; |
| 412 | } |
| 413 | |
reed@google.com | 5b132b2 | 2013-12-06 18:51:08 +0000 | [diff] [blame^] | 414 | Sk64 size = this->getSize64(); |
| 415 | SkASSERT(!size.isNeg() && size.is32()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 416 | |
reed@google.com | 5b132b2 | 2013-12-06 18:51:08 +0000 | [diff] [blame^] | 417 | this->setPixelRef(new SkMallocPixelRef(p, size.get32(), ctable, false))->unref(); |
djsollen@google.com | c84b833 | 2012-07-27 13:41:44 +0000 | [diff] [blame] | 418 | // since we're already allocated, we lockPixels right away |
| 419 | this->lockPixels(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 420 | SkDEBUGCODE(this->validate();) |
| 421 | } |
| 422 | |
| 423 | bool SkBitmap::allocPixels(Allocator* allocator, SkColorTable* ctable) { |
| 424 | HeapAllocator stdalloc; |
| 425 | |
| 426 | if (NULL == allocator) { |
| 427 | allocator = &stdalloc; |
| 428 | } |
| 429 | return allocator->allocPixelRef(this, ctable); |
| 430 | } |
| 431 | |
| 432 | void SkBitmap::freePixels() { |
| 433 | // if we're gonna free the pixels, we certainly need to free the mipmap |
| 434 | this->freeMipMap(); |
| 435 | |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 436 | if (fColorTable) { |
| 437 | fColorTable->unref(); |
| 438 | fColorTable = NULL; |
| 439 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 440 | |
| 441 | if (NULL != fPixelRef) { |
| 442 | if (fPixelLockCount > 0) { |
| 443 | fPixelRef->unlockPixels(); |
| 444 | } |
| 445 | fPixelRef->unref(); |
| 446 | fPixelRef = NULL; |
| 447 | fPixelRefOffset = 0; |
| 448 | } |
| 449 | fPixelLockCount = 0; |
| 450 | fPixels = NULL; |
| 451 | } |
| 452 | |
| 453 | void SkBitmap::freeMipMap() { |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 454 | if (fMipMap) { |
| 455 | fMipMap->unref(); |
| 456 | fMipMap = NULL; |
| 457 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | uint32_t SkBitmap::getGenerationID() const { |
djsollen@google.com | c84b833 | 2012-07-27 13:41:44 +0000 | [diff] [blame] | 461 | return (fPixelRef) ? fPixelRef->getGenerationID() : 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | void SkBitmap::notifyPixelsChanged() const { |
junov@chromium.org | b052129 | 2011-12-15 20:14:06 +0000 | [diff] [blame] | 465 | SkASSERT(!this->isImmutable()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 466 | if (fPixelRef) { |
| 467 | fPixelRef->notifyPixelsChanged(); |
| 468 | } |
| 469 | } |
| 470 | |
commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 471 | GrTexture* SkBitmap::getTexture() const { |
reed@android.com | ce4e53a | 2010-09-09 16:01:26 +0000 | [diff] [blame] | 472 | return fPixelRef ? fPixelRef->getTexture() : NULL; |
| 473 | } |
| 474 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 475 | /////////////////////////////////////////////////////////////////////////////// |
| 476 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 477 | /** We explicitly use the same allocator for our pixels that SkMask does, |
| 478 | so that we can freely assign memory allocated by one class to the other. |
| 479 | */ |
| 480 | bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst, |
| 481 | SkColorTable* ctable) { |
reed@google.com | 5b132b2 | 2013-12-06 18:51:08 +0000 | [diff] [blame^] | 482 | Sk64 size = dst->getSize64(); |
| 483 | if (size.isNeg() || !size.is32()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 484 | return false; |
| 485 | } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 486 | |
reed@google.com | 5b132b2 | 2013-12-06 18:51:08 +0000 | [diff] [blame^] | 487 | void* addr = sk_malloc_flags(size.get32(), 0); // returns NULL on failure |
| 488 | if (NULL == addr) { |
| 489 | return false; |
| 490 | } |
| 491 | |
| 492 | dst->setPixelRef(new SkMallocPixelRef(addr, size.get32(), ctable))->unref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 493 | // since we're already allocated, we lockPixels right away |
| 494 | dst->lockPixels(); |
| 495 | return true; |
| 496 | } |
| 497 | |
| 498 | /////////////////////////////////////////////////////////////////////////////// |
| 499 | |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 500 | size_t SkBitmap::getSafeSize() const { |
| 501 | // This is intended to be a size_t version of ComputeSafeSize64(), just |
| 502 | // faster. The computation is meant to be identical. |
| 503 | return (fHeight ? ((fHeight - 1) * fRowBytes) + |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 504 | ComputeRowBytes(this->config(), fWidth): 0); |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | Sk64 SkBitmap::getSafeSize64() const { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 508 | return ComputeSafeSize64(this->config(), fWidth, fHeight, fRowBytes); |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 509 | } |
| 510 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 511 | bool SkBitmap::copyPixelsTo(void* const dst, size_t dstSize, |
scroggo@google.com | 0ba4bf4 | 2013-02-25 16:02:36 +0000 | [diff] [blame] | 512 | size_t dstRowBytes, bool preserveDstPad) const { |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 513 | |
scroggo@google.com | 0ba4bf4 | 2013-02-25 16:02:36 +0000 | [diff] [blame] | 514 | if (0 == dstRowBytes) { |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 515 | dstRowBytes = fRowBytes; |
scroggo@google.com | 0ba4bf4 | 2013-02-25 16:02:36 +0000 | [diff] [blame] | 516 | } |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 517 | |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 518 | if (dstRowBytes < ComputeRowBytes(this->config(), fWidth) || |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 519 | dst == NULL || (getPixels() == NULL && pixelRef() == NULL)) |
| 520 | return false; |
| 521 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 522 | if (!preserveDstPad && static_cast<uint32_t>(dstRowBytes) == fRowBytes) { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 523 | size_t safeSize = this->getSafeSize(); |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 524 | if (safeSize > dstSize || safeSize == 0) |
| 525 | return false; |
| 526 | else { |
| 527 | SkAutoLockPixels lock(*this); |
| 528 | // This implementation will write bytes beyond the end of each row, |
| 529 | // excluding the last row, if the bitmap's stride is greater than |
| 530 | // strictly required by the current config. |
| 531 | memcpy(dst, getPixels(), safeSize); |
| 532 | |
| 533 | return true; |
| 534 | } |
| 535 | } else { |
| 536 | // If destination has different stride than us, then copy line by line. |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 537 | if (ComputeSafeSize(this->config(), fWidth, fHeight, dstRowBytes) > |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 538 | dstSize) |
| 539 | return false; |
| 540 | else { |
| 541 | // Just copy what we need on each line. |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 542 | size_t rowBytes = ComputeRowBytes(this->config(), fWidth); |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 543 | SkAutoLockPixels lock(*this); |
| 544 | const uint8_t* srcP = reinterpret_cast<const uint8_t*>(getPixels()); |
| 545 | uint8_t* dstP = reinterpret_cast<uint8_t*>(dst); |
| 546 | for (uint32_t row = 0; row < fHeight; |
| 547 | row++, srcP += fRowBytes, dstP += dstRowBytes) { |
| 548 | memcpy(dstP, srcP, rowBytes); |
| 549 | } |
| 550 | |
| 551 | return true; |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 556 | /////////////////////////////////////////////////////////////////////////////// |
| 557 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 558 | bool SkBitmap::isImmutable() const { |
junov@chromium.org | b052129 | 2011-12-15 20:14:06 +0000 | [diff] [blame] | 559 | return fPixelRef ? fPixelRef->isImmutable() : |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 560 | fFlags & kImageIsImmutable_Flag; |
junov@chromium.org | b052129 | 2011-12-15 20:14:06 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | void SkBitmap::setImmutable() { |
| 564 | if (fPixelRef) { |
| 565 | fPixelRef->setImmutable(); |
| 566 | } else { |
| 567 | fFlags |= kImageIsImmutable_Flag; |
| 568 | } |
| 569 | } |
| 570 | |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 571 | bool SkBitmap::isVolatile() const { |
| 572 | return (fFlags & kImageIsVolatile_Flag) != 0; |
| 573 | } |
| 574 | |
| 575 | void SkBitmap::setIsVolatile(bool isVolatile) { |
| 576 | if (isVolatile) { |
| 577 | fFlags |= kImageIsVolatile_Flag; |
| 578 | } else { |
| 579 | fFlags &= ~kImageIsVolatile_Flag; |
| 580 | } |
| 581 | } |
| 582 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 583 | void* SkBitmap::getAddr(int x, int y) const { |
| 584 | SkASSERT((unsigned)x < (unsigned)this->width()); |
| 585 | SkASSERT((unsigned)y < (unsigned)this->height()); |
| 586 | |
| 587 | char* base = (char*)this->getPixels(); |
| 588 | if (base) { |
| 589 | base += y * this->rowBytes(); |
| 590 | switch (this->config()) { |
| 591 | case SkBitmap::kARGB_8888_Config: |
| 592 | base += x << 2; |
| 593 | break; |
| 594 | case SkBitmap::kARGB_4444_Config: |
| 595 | case SkBitmap::kRGB_565_Config: |
| 596 | base += x << 1; |
| 597 | break; |
| 598 | case SkBitmap::kA8_Config: |
| 599 | case SkBitmap::kIndex8_Config: |
| 600 | base += x; |
| 601 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 602 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 603 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 604 | SkDEBUGFAIL("Can't return addr for config"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 605 | base = NULL; |
| 606 | break; |
| 607 | } |
| 608 | } |
| 609 | return base; |
| 610 | } |
| 611 | |
vandebo@chromium.org | 112706d | 2011-02-24 22:50:55 +0000 | [diff] [blame] | 612 | SkColor SkBitmap::getColor(int x, int y) const { |
| 613 | SkASSERT((unsigned)x < (unsigned)this->width()); |
| 614 | SkASSERT((unsigned)y < (unsigned)this->height()); |
| 615 | |
| 616 | switch (this->config()) { |
vandebo@chromium.org | 112706d | 2011-02-24 22:50:55 +0000 | [diff] [blame] | 617 | case SkBitmap::kA8_Config: { |
reed@google.com | 3b521d0 | 2011-04-29 11:53:41 +0000 | [diff] [blame] | 618 | uint8_t* addr = this->getAddr8(x, y); |
vandebo@chromium.org | 112706d | 2011-02-24 22:50:55 +0000 | [diff] [blame] | 619 | return SkColorSetA(0, addr[0]); |
| 620 | } |
| 621 | case SkBitmap::kIndex8_Config: { |
reed@google.com | 3b521d0 | 2011-04-29 11:53:41 +0000 | [diff] [blame] | 622 | SkPMColor c = this->getIndex8Color(x, y); |
vandebo@chromium.org | 112706d | 2011-02-24 22:50:55 +0000 | [diff] [blame] | 623 | return SkUnPreMultiply::PMColorToColor(c); |
| 624 | } |
| 625 | case SkBitmap::kRGB_565_Config: { |
reed@google.com | 3b521d0 | 2011-04-29 11:53:41 +0000 | [diff] [blame] | 626 | uint16_t* addr = this->getAddr16(x, y); |
vandebo@chromium.org | 112706d | 2011-02-24 22:50:55 +0000 | [diff] [blame] | 627 | return SkPixel16ToColor(addr[0]); |
| 628 | } |
| 629 | case SkBitmap::kARGB_4444_Config: { |
reed@google.com | 3b521d0 | 2011-04-29 11:53:41 +0000 | [diff] [blame] | 630 | uint16_t* addr = this->getAddr16(x, y); |
vandebo@chromium.org | 112706d | 2011-02-24 22:50:55 +0000 | [diff] [blame] | 631 | SkPMColor c = SkPixel4444ToPixel32(addr[0]); |
| 632 | return SkUnPreMultiply::PMColorToColor(c); |
| 633 | } |
| 634 | case SkBitmap::kARGB_8888_Config: { |
reed@google.com | 3b521d0 | 2011-04-29 11:53:41 +0000 | [diff] [blame] | 635 | uint32_t* addr = this->getAddr32(x, y); |
vandebo@chromium.org | 112706d | 2011-02-24 22:50:55 +0000 | [diff] [blame] | 636 | return SkUnPreMultiply::PMColorToColor(addr[0]); |
| 637 | } |
vandebo@chromium.org | 112706d | 2011-02-24 22:50:55 +0000 | [diff] [blame] | 638 | case kNo_Config: |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 639 | default: |
vandebo@chromium.org | 112706d | 2011-02-24 22:50:55 +0000 | [diff] [blame] | 640 | SkASSERT(false); |
| 641 | return 0; |
| 642 | } |
| 643 | SkASSERT(false); // Not reached. |
| 644 | return 0; |
| 645 | } |
| 646 | |
reed@google.com | 2a7579d | 2012-11-07 18:30:18 +0000 | [diff] [blame] | 647 | bool SkBitmap::ComputeIsOpaque(const SkBitmap& bm) { |
| 648 | SkAutoLockPixels alp(bm); |
| 649 | if (!bm.getPixels()) { |
| 650 | return false; |
| 651 | } |
| 652 | |
| 653 | const int height = bm.height(); |
| 654 | const int width = bm.width(); |
| 655 | |
| 656 | switch (bm.config()) { |
reed@google.com | 2a7579d | 2012-11-07 18:30:18 +0000 | [diff] [blame] | 657 | case SkBitmap::kA8_Config: { |
| 658 | unsigned a = 0xFF; |
| 659 | for (int y = 0; y < height; ++y) { |
| 660 | const uint8_t* row = bm.getAddr8(0, y); |
| 661 | for (int x = 0; x < width; ++x) { |
| 662 | a &= row[x]; |
| 663 | } |
| 664 | if (0xFF != a) { |
| 665 | return false; |
| 666 | } |
| 667 | } |
| 668 | return true; |
| 669 | } break; |
reed@google.com | 2a7579d | 2012-11-07 18:30:18 +0000 | [diff] [blame] | 670 | case SkBitmap::kIndex8_Config: { |
| 671 | SkAutoLockColors alc(bm); |
| 672 | const SkPMColor* table = alc.colors(); |
| 673 | if (!table) { |
| 674 | return false; |
| 675 | } |
reed@google.com | 140d728 | 2013-01-07 20:25:04 +0000 | [diff] [blame] | 676 | SkPMColor c = (SkPMColor)~0; |
reed@google.com | 2a7579d | 2012-11-07 18:30:18 +0000 | [diff] [blame] | 677 | for (int i = bm.getColorTable()->count() - 1; i >= 0; --i) { |
| 678 | c &= table[i]; |
| 679 | } |
| 680 | return 0xFF == SkGetPackedA32(c); |
| 681 | } break; |
| 682 | case SkBitmap::kRGB_565_Config: |
| 683 | return true; |
| 684 | break; |
| 685 | case SkBitmap::kARGB_4444_Config: { |
| 686 | unsigned c = 0xFFFF; |
| 687 | for (int y = 0; y < height; ++y) { |
| 688 | const SkPMColor16* row = bm.getAddr16(0, y); |
| 689 | for (int x = 0; x < width; ++x) { |
| 690 | c &= row[x]; |
| 691 | } |
| 692 | if (0xF != SkGetPackedA4444(c)) { |
| 693 | return false; |
| 694 | } |
| 695 | } |
| 696 | return true; |
| 697 | } break; |
| 698 | case SkBitmap::kARGB_8888_Config: { |
reed@google.com | 140d728 | 2013-01-07 20:25:04 +0000 | [diff] [blame] | 699 | SkPMColor c = (SkPMColor)~0; |
reed@google.com | 2a7579d | 2012-11-07 18:30:18 +0000 | [diff] [blame] | 700 | for (int y = 0; y < height; ++y) { |
| 701 | const SkPMColor* row = bm.getAddr32(0, y); |
| 702 | for (int x = 0; x < width; ++x) { |
| 703 | c &= row[x]; |
| 704 | } |
| 705 | if (0xFF != SkGetPackedA32(c)) { |
| 706 | return false; |
| 707 | } |
| 708 | } |
| 709 | return true; |
| 710 | } |
| 711 | default: |
| 712 | break; |
| 713 | } |
| 714 | return false; |
| 715 | } |
| 716 | |
| 717 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 718 | /////////////////////////////////////////////////////////////////////////////// |
| 719 | /////////////////////////////////////////////////////////////////////////////// |
| 720 | |
reed@google.com | 45f746f | 2013-06-21 19:51:31 +0000 | [diff] [blame] | 721 | static uint16_t pack_8888_to_4444(unsigned a, unsigned r, unsigned g, unsigned b) { |
| 722 | unsigned pixel = (SkA32To4444(a) << SK_A4444_SHIFT) | |
| 723 | (SkR32To4444(r) << SK_R4444_SHIFT) | |
| 724 | (SkG32To4444(g) << SK_G4444_SHIFT) | |
| 725 | (SkB32To4444(b) << SK_B4444_SHIFT); |
| 726 | return SkToU16(pixel); |
| 727 | } |
| 728 | |
reed@google.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 729 | void SkBitmap::internalErase(const SkIRect& area, |
| 730 | U8CPU a, U8CPU r, U8CPU g, U8CPU b) const { |
| 731 | #ifdef SK_DEBUG |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 732 | SkDEBUGCODE(this->validate();) |
reed@google.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 733 | SkASSERT(!area.isEmpty()); |
| 734 | { |
reed@google.com | 92833f9 | 2013-06-28 19:47:43 +0000 | [diff] [blame] | 735 | SkIRect total = { 0, 0, this->width(), this->height() }; |
reed@google.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 736 | SkASSERT(total.contains(area)); |
| 737 | } |
| 738 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 739 | |
reed@google.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 740 | if (kNo_Config == fConfig || kIndex8_Config == fConfig) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 741 | return; |
| 742 | } |
| 743 | |
| 744 | SkAutoLockPixels alp(*this); |
| 745 | // perform this check after the lock call |
| 746 | if (!this->readyToDraw()) { |
| 747 | return; |
| 748 | } |
| 749 | |
reed@google.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 750 | int height = area.height(); |
| 751 | const int width = area.width(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 752 | const int rowBytes = fRowBytes; |
| 753 | |
| 754 | // make rgb premultiplied |
| 755 | if (255 != a) { |
| 756 | r = SkAlphaMul(r, a); |
| 757 | g = SkAlphaMul(g, a); |
| 758 | b = SkAlphaMul(b, a); |
| 759 | } |
| 760 | |
| 761 | switch (fConfig) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 762 | case kA8_Config: { |
reed@google.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 763 | uint8_t* p = this->getAddr8(area.fLeft, area.fTop); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 764 | while (--height >= 0) { |
| 765 | memset(p, a, width); |
| 766 | p += rowBytes; |
| 767 | } |
| 768 | break; |
| 769 | } |
reed@google.com | 45f746f | 2013-06-21 19:51:31 +0000 | [diff] [blame] | 770 | case kARGB_4444_Config: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 771 | case kRGB_565_Config: { |
reed@google.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 772 | uint16_t* p = this->getAddr16(area.fLeft, area.fTop);; |
reed@google.com | 45f746f | 2013-06-21 19:51:31 +0000 | [diff] [blame] | 773 | uint16_t v; |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 774 | |
reed@google.com | 45f746f | 2013-06-21 19:51:31 +0000 | [diff] [blame] | 775 | if (kARGB_4444_Config == fConfig) { |
| 776 | v = pack_8888_to_4444(a, r, g, b); |
| 777 | } else { |
| 778 | v = SkPackRGB16(r >> (8 - SK_R16_BITS), |
| 779 | g >> (8 - SK_G16_BITS), |
| 780 | b >> (8 - SK_B16_BITS)); |
| 781 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 782 | while (--height >= 0) { |
| 783 | sk_memset16(p, v, width); |
| 784 | p = (uint16_t*)((char*)p + rowBytes); |
| 785 | } |
| 786 | break; |
| 787 | } |
| 788 | case kARGB_8888_Config: { |
reed@google.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 789 | uint32_t* p = this->getAddr32(area.fLeft, area.fTop); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 790 | uint32_t v = SkPackARGB32(a, r, g, b); |
| 791 | |
| 792 | while (--height >= 0) { |
| 793 | sk_memset32(p, v, width); |
| 794 | p = (uint32_t*)((char*)p + rowBytes); |
| 795 | } |
| 796 | break; |
| 797 | } |
| 798 | } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 799 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 800 | this->notifyPixelsChanged(); |
| 801 | } |
| 802 | |
reed@google.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 803 | void SkBitmap::eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const { |
reed@google.com | 92833f9 | 2013-06-28 19:47:43 +0000 | [diff] [blame] | 804 | SkIRect area = { 0, 0, this->width(), this->height() }; |
reed@google.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 805 | if (!area.isEmpty()) { |
| 806 | this->internalErase(area, a, r, g, b); |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | void SkBitmap::eraseArea(const SkIRect& rect, SkColor c) const { |
reed@google.com | 92833f9 | 2013-06-28 19:47:43 +0000 | [diff] [blame] | 811 | SkIRect area = { 0, 0, this->width(), this->height() }; |
reed@google.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 812 | if (area.intersect(rect)) { |
| 813 | this->internalErase(area, SkColorGetA(c), SkColorGetR(c), |
| 814 | SkColorGetG(c), SkColorGetB(c)); |
| 815 | } |
| 816 | } |
| 817 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 818 | ////////////////////////////////////////////////////////////////////////////////////// |
| 819 | ////////////////////////////////////////////////////////////////////////////////////// |
| 820 | |
| 821 | #define SUB_OFFSET_FAILURE ((size_t)-1) |
| 822 | |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 823 | /** |
| 824 | * Based on the Config and rowBytes() of bm, return the offset into an SkPixelRef of the pixel at |
| 825 | * (x, y). |
| 826 | * Note that the SkPixelRef does not need to be set yet. deepCopyTo takes advantage of this fact. |
| 827 | * Also note that (x, y) may be outside the range of (0 - width(), 0 - height()), so long as it is |
| 828 | * within the bounds of the SkPixelRef being used. |
| 829 | */ |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 830 | static size_t get_sub_offset(const SkBitmap& bm, int x, int y) { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 831 | switch (bm.config()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 832 | case SkBitmap::kA8_Config: |
| 833 | case SkBitmap:: kIndex8_Config: |
| 834 | // x is fine as is for the calculation |
| 835 | break; |
| 836 | |
| 837 | case SkBitmap::kRGB_565_Config: |
| 838 | case SkBitmap::kARGB_4444_Config: |
| 839 | x <<= 1; |
| 840 | break; |
| 841 | |
| 842 | case SkBitmap::kARGB_8888_Config: |
| 843 | x <<= 2; |
| 844 | break; |
| 845 | |
| 846 | case SkBitmap::kNo_Config: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 847 | default: |
| 848 | return SUB_OFFSET_FAILURE; |
| 849 | } |
| 850 | return y * bm.rowBytes() + x; |
| 851 | } |
| 852 | |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 853 | /** |
| 854 | * Using the pixelRefOffset(), rowBytes(), and Config of bm, determine the (x, y) coordinate of the |
| 855 | * upper left corner of bm relative to its SkPixelRef. |
| 856 | * x and y must be non-NULL. |
| 857 | */ |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 858 | bool get_upper_left_from_offset(SkBitmap::Config config, size_t offset, size_t rowBytes, |
scroggo@google.com | 61d6c9e | 2013-05-21 20:38:40 +0000 | [diff] [blame] | 859 | int32_t* x, int32_t* y); |
| 860 | bool get_upper_left_from_offset(SkBitmap::Config config, size_t offset, size_t rowBytes, |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 861 | int32_t* x, int32_t* y) { |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 862 | SkASSERT(x != NULL && y != NULL); |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 863 | if (0 == offset) { |
| 864 | *x = *y = 0; |
| 865 | return true; |
| 866 | } |
| 867 | // Use integer division to find the correct y position. |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 868 | // The remainder will be the x position, after we reverse get_sub_offset. |
commit-bot@chromium.org | 2c86fbb | 2013-09-26 19:22:54 +0000 | [diff] [blame] | 869 | SkTDivMod(offset, rowBytes, y, x); |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 870 | switch (config) { |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 871 | case SkBitmap::kA8_Config: |
| 872 | // Fall through. |
| 873 | case SkBitmap::kIndex8_Config: |
| 874 | // x is unmodified |
| 875 | break; |
| 876 | |
| 877 | case SkBitmap::kRGB_565_Config: |
| 878 | // Fall through. |
| 879 | case SkBitmap::kARGB_4444_Config: |
| 880 | *x >>= 1; |
| 881 | break; |
| 882 | |
| 883 | case SkBitmap::kARGB_8888_Config: |
| 884 | *x >>= 2; |
| 885 | break; |
| 886 | |
| 887 | case SkBitmap::kNo_Config: |
| 888 | // Fall through. |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 889 | default: |
| 890 | return false; |
| 891 | } |
| 892 | return true; |
| 893 | } |
| 894 | |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 895 | static bool get_upper_left_from_offset(const SkBitmap& bm, int32_t* x, int32_t* y) { |
| 896 | return get_upper_left_from_offset(bm.config(), bm.pixelRefOffset(), bm.rowBytes(), x, y); |
| 897 | } |
| 898 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 899 | bool SkBitmap::extractSubset(SkBitmap* result, const SkIRect& subset) const { |
| 900 | SkDEBUGCODE(this->validate();) |
| 901 | |
djsollen@google.com | c84b833 | 2012-07-27 13:41:44 +0000 | [diff] [blame] | 902 | if (NULL == result || NULL == fPixelRef) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 903 | return false; // no src pixels |
| 904 | } |
| 905 | |
| 906 | SkIRect srcRect, r; |
| 907 | srcRect.set(0, 0, this->width(), this->height()); |
| 908 | if (!r.intersect(srcRect, subset)) { |
| 909 | return false; // r is empty (i.e. no intersection) |
| 910 | } |
| 911 | |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 912 | if (fPixelRef->getTexture() != NULL) { |
| 913 | // Do a deep copy |
| 914 | SkPixelRef* pixelRef = fPixelRef->deepCopy(this->config(), &subset); |
| 915 | if (pixelRef != NULL) { |
| 916 | SkBitmap dst; |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 917 | dst.setConfig(this->config(), subset.width(), subset.height(), 0, |
| 918 | this->alphaType()); |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 919 | dst.setIsVolatile(this->isVolatile()); |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 920 | dst.setPixelRef(pixelRef)->unref(); |
| 921 | SkDEBUGCODE(dst.validate()); |
| 922 | result->swap(dst); |
| 923 | return true; |
| 924 | } |
| 925 | } |
| 926 | |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 927 | // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have |
| 928 | // exited above. |
| 929 | SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width())); |
| 930 | SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height())); |
| 931 | |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 932 | size_t offset = get_sub_offset(*this, r.fLeft, r.fTop); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 933 | if (SUB_OFFSET_FAILURE == offset) { |
| 934 | return false; // config not supported |
| 935 | } |
| 936 | |
| 937 | SkBitmap dst; |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 938 | dst.setConfig(this->config(), r.width(), r.height(), this->rowBytes(), |
| 939 | this->alphaType()); |
skyostil@google.com | 0eb7576 | 2012-01-16 10:45:53 +0000 | [diff] [blame] | 940 | dst.setIsVolatile(this->isVolatile()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 941 | |
| 942 | if (fPixelRef) { |
| 943 | // share the pixelref with a custom offset |
| 944 | dst.setPixelRef(fPixelRef, fPixelRefOffset + offset); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 945 | } |
| 946 | SkDEBUGCODE(dst.validate();) |
| 947 | |
| 948 | // we know we're good, so commit to result |
| 949 | result->swap(dst); |
| 950 | return true; |
| 951 | } |
| 952 | |
| 953 | /////////////////////////////////////////////////////////////////////////////// |
| 954 | |
| 955 | #include "SkCanvas.h" |
| 956 | #include "SkPaint.h" |
| 957 | |
reed@android.com | fbaa88d | 2009-05-06 17:44:34 +0000 | [diff] [blame] | 958 | bool SkBitmap::canCopyTo(Config dstConfig) const { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 959 | if (this->config() == kNo_Config) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 960 | return false; |
| 961 | } |
| 962 | |
reed@android.com | fbaa88d | 2009-05-06 17:44:34 +0000 | [diff] [blame] | 963 | bool sameConfigs = (this->config() == dstConfig); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 964 | switch (dstConfig) { |
| 965 | case kA8_Config: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 966 | case kRGB_565_Config: |
| 967 | case kARGB_8888_Config: |
| 968 | break; |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 969 | case kIndex8_Config: |
| 970 | if (!sameConfigs) { |
| 971 | return false; |
| 972 | } |
| 973 | break; |
scroggo@google.com | 8dc8bc5 | 2013-08-07 19:16:05 +0000 | [diff] [blame] | 974 | case kARGB_4444_Config: |
| 975 | return sameConfigs || kARGB_8888_Config == this->config(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 976 | default: |
| 977 | return false; |
| 978 | } |
reed@android.com | fbaa88d | 2009-05-06 17:44:34 +0000 | [diff] [blame] | 979 | return true; |
| 980 | } |
| 981 | |
| 982 | bool SkBitmap::copyTo(SkBitmap* dst, Config dstConfig, Allocator* alloc) const { |
| 983 | if (!this->canCopyTo(dstConfig)) { |
| 984 | return false; |
| 985 | } |
| 986 | |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 987 | // if we have a texture, first get those pixels |
| 988 | SkBitmap tmpSrc; |
| 989 | const SkBitmap* src = this; |
| 990 | |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 991 | if (fPixelRef) { |
| 992 | SkIRect subset; |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 993 | if (get_upper_left_from_offset(*this, &subset.fLeft, &subset.fTop)) { |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 994 | subset.fRight = subset.fLeft + fWidth; |
| 995 | subset.fBottom = subset.fTop + fHeight; |
| 996 | if (fPixelRef->readPixels(&tmpSrc, &subset)) { |
| 997 | SkASSERT(tmpSrc.width() == this->width()); |
| 998 | SkASSERT(tmpSrc.height() == this->height()); |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 999 | |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 1000 | // did we get lucky and we can just return tmpSrc? |
| 1001 | if (tmpSrc.config() == dstConfig && NULL == alloc) { |
| 1002 | dst->swap(tmpSrc); |
| 1003 | if (dst->pixelRef() && this->config() == dstConfig) { |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 1004 | // TODO(scroggo): fix issue 1742 |
| 1005 | dst->pixelRef()->cloneGenID(*fPixelRef); |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 1006 | } |
| 1007 | return true; |
| 1008 | } |
| 1009 | |
| 1010 | // fall through to the raster case |
| 1011 | src = &tmpSrc; |
scroggo@google.com | d5764e8 | 2012-08-22 15:00:05 +0000 | [diff] [blame] | 1012 | } |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 1013 | } |
reed@android.com | fbaa88d | 2009-05-06 17:44:34 +0000 | [diff] [blame] | 1014 | } |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 1015 | |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 1016 | // we lock this now, since we may need its colortable |
| 1017 | SkAutoLockPixels srclock(*src); |
| 1018 | if (!src->readyToDraw()) { |
| 1019 | return false; |
| 1020 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1021 | |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 1022 | SkBitmap tmpDst; |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 1023 | tmpDst.setConfig(dstConfig, src->width(), src->height(), 0, |
| 1024 | src->alphaType()); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1025 | |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1026 | // allocate colortable if srcConfig == kIndex8_Config |
| 1027 | SkColorTable* ctable = (dstConfig == kIndex8_Config) ? |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 1028 | new SkColorTable(*src->getColorTable()) : NULL; |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1029 | SkAutoUnref au(ctable); |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 1030 | if (!tmpDst.allocPixels(alloc, ctable)) { |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1031 | return false; |
| 1032 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1033 | |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 1034 | if (!tmpDst.readyToDraw()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1035 | // allocator/lock failed |
| 1036 | return false; |
| 1037 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1038 | |
reed@android.com | fbaa88d | 2009-05-06 17:44:34 +0000 | [diff] [blame] | 1039 | /* do memcpy for the same configs cases, else use drawing |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1040 | */ |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 1041 | if (src->config() == dstConfig) { |
| 1042 | if (tmpDst.getSize() == src->getSize()) { |
| 1043 | memcpy(tmpDst.getPixels(), src->getPixels(), src->getSafeSize()); |
scroggo@google.com | d5764e8 | 2012-08-22 15:00:05 +0000 | [diff] [blame] | 1044 | SkPixelRef* pixelRef = tmpDst.pixelRef(); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 1045 | if (NULL != pixelRef && NULL != fPixelRef) { |
| 1046 | // TODO(scroggo): fix issue 1742 |
| 1047 | pixelRef->cloneGenID(*fPixelRef); |
scroggo@google.com | d5764e8 | 2012-08-22 15:00:05 +0000 | [diff] [blame] | 1048 | } |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 1049 | } else { |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 1050 | const char* srcP = reinterpret_cast<const char*>(src->getPixels()); |
| 1051 | char* dstP = reinterpret_cast<char*>(tmpDst.getPixels()); |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 1052 | // to be sure we don't read too much, only copy our logical pixels |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 1053 | size_t bytesToCopy = tmpDst.width() * tmpDst.bytesPerPixel(); |
| 1054 | for (int y = 0; y < tmpDst.height(); y++) { |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 1055 | memcpy(dstP, srcP, bytesToCopy); |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 1056 | srcP += src->rowBytes(); |
| 1057 | dstP += tmpDst.rowBytes(); |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 1058 | } |
| 1059 | } |
scroggo@google.com | 8dc8bc5 | 2013-08-07 19:16:05 +0000 | [diff] [blame] | 1060 | } else if (SkBitmap::kARGB_4444_Config == dstConfig |
| 1061 | && SkBitmap::kARGB_8888_Config == src->config()) { |
| 1062 | SkASSERT(src->height() == tmpDst.height()); |
| 1063 | SkASSERT(src->width() == tmpDst.width()); |
| 1064 | for (int y = 0; y < src->height(); ++y) { |
| 1065 | SkPMColor16* SK_RESTRICT dstRow = (SkPMColor16*) tmpDst.getAddr16(0, y); |
| 1066 | SkPMColor* SK_RESTRICT srcRow = (SkPMColor*) src->getAddr32(0, y); |
| 1067 | DITHER_4444_SCAN(y); |
| 1068 | for (int x = 0; x < src->width(); ++x) { |
| 1069 | dstRow[x] = SkDitherARGB32To4444(srcRow[x], |
| 1070 | DITHER_VALUE(x)); |
| 1071 | } |
| 1072 | } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1073 | } else { |
robertphillips@google.com | 0197b32 | 2013-10-10 15:48:16 +0000 | [diff] [blame] | 1074 | // Always clear the dest in case one of the blitters accesses it |
| 1075 | // TODO: switch the allocation of tmpDst to call sk_calloc_throw |
| 1076 | tmpDst.eraseColor(SK_ColorTRANSPARENT); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1077 | |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 1078 | SkCanvas canvas(tmpDst); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1079 | SkPaint paint; |
| 1080 | |
| 1081 | paint.setDither(true); |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 1082 | canvas.drawBitmap(*src, 0, 0, &paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 1085 | dst->swap(tmpDst); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1086 | return true; |
| 1087 | } |
| 1088 | |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 1089 | bool SkBitmap::deepCopyTo(SkBitmap* dst, Config dstConfig) const { |
| 1090 | if (!this->canCopyTo(dstConfig)) { |
| 1091 | return false; |
| 1092 | } |
| 1093 | |
| 1094 | // If we have a PixelRef, and it supports deep copy, use it. |
| 1095 | // Currently supported only by texture-backed bitmaps. |
| 1096 | if (fPixelRef) { |
| 1097 | SkPixelRef* pixelRef = fPixelRef->deepCopy(dstConfig); |
| 1098 | if (pixelRef) { |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 1099 | uint32_t rowBytes; |
scroggo@google.com | d5764e8 | 2012-08-22 15:00:05 +0000 | [diff] [blame] | 1100 | if (dstConfig == fConfig) { |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 1101 | // TODO(scroggo): fix issue 1742 |
| 1102 | pixelRef->cloneGenID(*fPixelRef); |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 1103 | // Use the same rowBytes as the original. |
| 1104 | rowBytes = fRowBytes; |
| 1105 | } else { |
| 1106 | // With the new config, an appropriate fRowBytes will be computed by setConfig. |
| 1107 | rowBytes = 0; |
scroggo@google.com | d5764e8 | 2012-08-22 15:00:05 +0000 | [diff] [blame] | 1108 | } |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 1109 | dst->setConfig(dstConfig, fWidth, fHeight, rowBytes); |
| 1110 | |
| 1111 | size_t pixelRefOffset; |
| 1112 | if (0 == fPixelRefOffset || dstConfig == fConfig) { |
| 1113 | // Use the same offset as the original. |
| 1114 | pixelRefOffset = fPixelRefOffset; |
| 1115 | } else { |
| 1116 | // Find the correct offset in the new config. This needs to be done after calling |
| 1117 | // setConfig so dst's fConfig and fRowBytes have been set properly. |
scroggo@google.com | e5f4824 | 2013-02-25 21:47:41 +0000 | [diff] [blame] | 1118 | int32_t x, y; |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 1119 | if (!get_upper_left_from_offset(*this, &x, &y)) { |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 1120 | return false; |
| 1121 | } |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 1122 | pixelRefOffset = get_sub_offset(*dst, x, y); |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 1123 | if (SUB_OFFSET_FAILURE == pixelRefOffset) { |
| 1124 | return false; |
| 1125 | } |
| 1126 | } |
| 1127 | dst->setPixelRef(pixelRef, pixelRefOffset)->unref(); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 1128 | return true; |
| 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | if (this->getTexture()) { |
| 1133 | return false; |
| 1134 | } else { |
| 1135 | return this->copyTo(dst, dstConfig, NULL); |
| 1136 | } |
| 1137 | } |
| 1138 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1139 | /////////////////////////////////////////////////////////////////////////////// |
| 1140 | /////////////////////////////////////////////////////////////////////////////// |
| 1141 | |
| 1142 | static void downsampleby2_proc32(SkBitmap* dst, int x, int y, |
| 1143 | const SkBitmap& src) { |
| 1144 | x <<= 1; |
| 1145 | y <<= 1; |
| 1146 | const SkPMColor* p = src.getAddr32(x, y); |
reed@android.com | 829c83c | 2009-06-08 12:05:31 +0000 | [diff] [blame] | 1147 | const SkPMColor* baseP = p; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1148 | SkPMColor c, ag, rb; |
| 1149 | |
| 1150 | c = *p; ag = (c >> 8) & 0xFF00FF; rb = c & 0xFF00FF; |
| 1151 | if (x < src.width() - 1) { |
| 1152 | p += 1; |
| 1153 | } |
| 1154 | c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF; |
| 1155 | |
reed@android.com | 829c83c | 2009-06-08 12:05:31 +0000 | [diff] [blame] | 1156 | p = baseP; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1157 | if (y < src.height() - 1) { |
reed@android.com | 829c83c | 2009-06-08 12:05:31 +0000 | [diff] [blame] | 1158 | p += src.rowBytes() >> 2; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1159 | } |
| 1160 | c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF; |
| 1161 | if (x < src.width() - 1) { |
| 1162 | p += 1; |
| 1163 | } |
| 1164 | c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF; |
| 1165 | |
| 1166 | *dst->getAddr32(x >> 1, y >> 1) = |
| 1167 | ((rb >> 2) & 0xFF00FF) | ((ag << 6) & 0xFF00FF00); |
| 1168 | } |
| 1169 | |
| 1170 | static inline uint32_t expand16(U16CPU c) { |
| 1171 | return (c & ~SK_G16_MASK_IN_PLACE) | ((c & SK_G16_MASK_IN_PLACE) << 16); |
| 1172 | } |
| 1173 | |
| 1174 | // returns dirt in the top 16bits, but we don't care, since we only |
| 1175 | // store the low 16bits. |
| 1176 | static inline U16CPU pack16(uint32_t c) { |
| 1177 | return (c & ~SK_G16_MASK_IN_PLACE) | ((c >> 16) & SK_G16_MASK_IN_PLACE); |
| 1178 | } |
| 1179 | |
| 1180 | static void downsampleby2_proc16(SkBitmap* dst, int x, int y, |
| 1181 | const SkBitmap& src) { |
| 1182 | x <<= 1; |
| 1183 | y <<= 1; |
| 1184 | const uint16_t* p = src.getAddr16(x, y); |
reed@android.com | 829c83c | 2009-06-08 12:05:31 +0000 | [diff] [blame] | 1185 | const uint16_t* baseP = p; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1186 | SkPMColor c; |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1187 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1188 | c = expand16(*p); |
reed@android.com | 829c83c | 2009-06-08 12:05:31 +0000 | [diff] [blame] | 1189 | if (x < src.width() - 1) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1190 | p += 1; |
| 1191 | } |
| 1192 | c += expand16(*p); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1193 | |
reed@android.com | 829c83c | 2009-06-08 12:05:31 +0000 | [diff] [blame] | 1194 | p = baseP; |
| 1195 | if (y < src.height() - 1) { |
| 1196 | p += src.rowBytes() >> 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1197 | } |
| 1198 | c += expand16(*p); |
reed@android.com | 829c83c | 2009-06-08 12:05:31 +0000 | [diff] [blame] | 1199 | if (x < src.width() - 1) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1200 | p += 1; |
| 1201 | } |
| 1202 | c += expand16(*p); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1203 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1204 | *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)pack16(c >> 2); |
| 1205 | } |
| 1206 | |
| 1207 | static uint32_t expand4444(U16CPU c) { |
| 1208 | return (c & 0xF0F) | ((c & ~0xF0F) << 12); |
| 1209 | } |
| 1210 | |
| 1211 | static U16CPU collaps4444(uint32_t c) { |
| 1212 | return (c & 0xF0F) | ((c >> 12) & ~0xF0F); |
| 1213 | } |
| 1214 | |
| 1215 | static void downsampleby2_proc4444(SkBitmap* dst, int x, int y, |
| 1216 | const SkBitmap& src) { |
| 1217 | x <<= 1; |
| 1218 | y <<= 1; |
| 1219 | const uint16_t* p = src.getAddr16(x, y); |
reed@android.com | 829c83c | 2009-06-08 12:05:31 +0000 | [diff] [blame] | 1220 | const uint16_t* baseP = p; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1221 | uint32_t c; |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1222 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1223 | c = expand4444(*p); |
| 1224 | if (x < src.width() - 1) { |
| 1225 | p += 1; |
| 1226 | } |
| 1227 | c += expand4444(*p); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1228 | |
reed@android.com | 829c83c | 2009-06-08 12:05:31 +0000 | [diff] [blame] | 1229 | p = baseP; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1230 | if (y < src.height() - 1) { |
reed@android.com | 829c83c | 2009-06-08 12:05:31 +0000 | [diff] [blame] | 1231 | p += src.rowBytes() >> 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1232 | } |
| 1233 | c += expand4444(*p); |
| 1234 | if (x < src.width() - 1) { |
| 1235 | p += 1; |
| 1236 | } |
| 1237 | c += expand4444(*p); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1238 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1239 | *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)collaps4444(c >> 2); |
| 1240 | } |
| 1241 | |
| 1242 | void SkBitmap::buildMipMap(bool forceRebuild) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1243 | if (forceRebuild) |
| 1244 | this->freeMipMap(); |
| 1245 | else if (fMipMap) |
| 1246 | return; // we're already built |
| 1247 | |
| 1248 | SkASSERT(NULL == fMipMap); |
| 1249 | |
| 1250 | void (*proc)(SkBitmap* dst, int x, int y, const SkBitmap& src); |
| 1251 | |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 1252 | const SkBitmap::Config config = this->config(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1253 | |
| 1254 | switch (config) { |
| 1255 | case kARGB_8888_Config: |
| 1256 | proc = downsampleby2_proc32; |
| 1257 | break; |
| 1258 | case kRGB_565_Config: |
| 1259 | proc = downsampleby2_proc16; |
| 1260 | break; |
| 1261 | case kARGB_4444_Config: |
| 1262 | proc = downsampleby2_proc4444; |
| 1263 | break; |
| 1264 | case kIndex8_Config: |
| 1265 | case kA8_Config: |
| 1266 | default: |
| 1267 | return; // don't build mipmaps for these configs |
| 1268 | } |
reed@android.com | 89bb83a | 2009-05-29 21:30:42 +0000 | [diff] [blame] | 1269 | |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 1270 | SkAutoLockPixels alp(*this); |
| 1271 | if (!this->readyToDraw()) { |
| 1272 | return; |
| 1273 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1274 | |
| 1275 | // whip through our loop to compute the exact size needed |
| 1276 | size_t size = 0; |
| 1277 | int maxLevels = 0; |
| 1278 | { |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 1279 | int width = this->width(); |
| 1280 | int height = this->height(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1281 | for (;;) { |
| 1282 | width >>= 1; |
| 1283 | height >>= 1; |
| 1284 | if (0 == width || 0 == height) { |
| 1285 | break; |
| 1286 | } |
| 1287 | size += ComputeRowBytes(config, width) * height; |
| 1288 | maxLevels += 1; |
| 1289 | } |
| 1290 | } |
reed@android.com | 89bb83a | 2009-05-29 21:30:42 +0000 | [diff] [blame] | 1291 | |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 1292 | // nothing to build |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1293 | if (0 == maxLevels) { |
| 1294 | return; |
| 1295 | } |
| 1296 | |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 1297 | SkBitmap srcBM(*this); |
| 1298 | srcBM.lockPixels(); |
| 1299 | if (!srcBM.readyToDraw()) { |
| 1300 | return; |
| 1301 | } |
| 1302 | |
| 1303 | MipMap* mm = MipMap::Alloc(maxLevels, size); |
| 1304 | if (NULL == mm) { |
| 1305 | return; |
| 1306 | } |
| 1307 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1308 | MipLevel* level = mm->levels(); |
| 1309 | uint8_t* addr = (uint8_t*)mm->pixels(); |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 1310 | int width = this->width(); |
| 1311 | int height = this->height(); |
scroggo@google.com | e5f4824 | 2013-02-25 21:47:41 +0000 | [diff] [blame] | 1312 | uint32_t rowBytes; |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 1313 | SkBitmap dstBM; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1314 | |
| 1315 | for (int i = 0; i < maxLevels; i++) { |
| 1316 | width >>= 1; |
| 1317 | height >>= 1; |
scroggo@google.com | e5f4824 | 2013-02-25 21:47:41 +0000 | [diff] [blame] | 1318 | rowBytes = SkToU32(ComputeRowBytes(config, width)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1319 | |
| 1320 | level[i].fPixels = addr; |
reed@android.com | f459a49 | 2009-03-27 12:33:50 +0000 | [diff] [blame] | 1321 | level[i].fWidth = width; |
| 1322 | level[i].fHeight = height; |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 1323 | level[i].fRowBytes = rowBytes; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1324 | |
| 1325 | dstBM.setConfig(config, width, height, rowBytes); |
| 1326 | dstBM.setPixels(addr); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1327 | |
bungeman@google.com | 7cf0e9e | 2012-07-25 16:09:10 +0000 | [diff] [blame] | 1328 | srcBM.lockPixels(); |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 1329 | for (int y = 0; y < height; y++) { |
| 1330 | for (int x = 0; x < width; x++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1331 | proc(&dstBM, x, y, srcBM); |
| 1332 | } |
| 1333 | } |
bungeman@google.com | 7cf0e9e | 2012-07-25 16:09:10 +0000 | [diff] [blame] | 1334 | srcBM.unlockPixels(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1335 | |
| 1336 | srcBM = dstBM; |
| 1337 | addr += height * rowBytes; |
| 1338 | } |
| 1339 | SkASSERT(addr == (uint8_t*)mm->pixels() + size); |
| 1340 | fMipMap = mm; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | bool SkBitmap::hasMipMap() const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1344 | return fMipMap != NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | int SkBitmap::extractMipLevel(SkBitmap* dst, SkFixed sx, SkFixed sy) { |
reed@android.com | 83f7bc3 | 2009-07-17 02:42:41 +0000 | [diff] [blame] | 1348 | if (NULL == fMipMap) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1349 | return 0; |
reed@android.com | 83f7bc3 | 2009-07-17 02:42:41 +0000 | [diff] [blame] | 1350 | } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1351 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1352 | int level = ComputeMipLevel(sx, sy) >> 16; |
| 1353 | SkASSERT(level >= 0); |
| 1354 | if (level <= 0) { |
| 1355 | return 0; |
| 1356 | } |
| 1357 | |
| 1358 | if (level >= fMipMap->fLevelCount) { |
| 1359 | level = fMipMap->fLevelCount - 1; |
| 1360 | } |
| 1361 | if (dst) { |
| 1362 | const MipLevel& mip = fMipMap->levels()[level - 1]; |
| 1363 | dst->setConfig((SkBitmap::Config)this->config(), |
| 1364 | mip.fWidth, mip.fHeight, mip.fRowBytes); |
| 1365 | dst->setPixels(mip.fPixels); |
| 1366 | } |
| 1367 | return level; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | SkFixed SkBitmap::ComputeMipLevel(SkFixed sx, SkFixed sy) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1371 | sx = SkAbs32(sx); |
| 1372 | sy = SkAbs32(sy); |
| 1373 | if (sx < sy) { |
| 1374 | sx = sy; |
| 1375 | } |
| 1376 | if (sx < SK_Fixed1) { |
| 1377 | return 0; |
| 1378 | } |
| 1379 | int clz = SkCLZ(sx); |
| 1380 | SkASSERT(clz >= 1 && clz <= 15); |
| 1381 | return SkIntToFixed(15 - clz) + ((unsigned)(sx << (clz + 1)) >> 16); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | /////////////////////////////////////////////////////////////////////////////// |
| 1385 | |
tomhudson@google.com | a87cd2a | 2011-06-15 16:50:27 +0000 | [diff] [blame] | 1386 | static bool GetBitmapAlpha(const SkBitmap& src, uint8_t* SK_RESTRICT alpha, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1387 | int alphaRowBytes) { |
| 1388 | SkASSERT(alpha != NULL); |
| 1389 | SkASSERT(alphaRowBytes >= src.width()); |
| 1390 | |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 1391 | SkBitmap::Config config = src.config(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1392 | int w = src.width(); |
| 1393 | int h = src.height(); |
scroggo@google.com | e5f4824 | 2013-02-25 21:47:41 +0000 | [diff] [blame] | 1394 | size_t rb = src.rowBytes(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1395 | |
reed@android.com | 1cdcb51 | 2009-08-24 19:11:00 +0000 | [diff] [blame] | 1396 | SkAutoLockPixels alp(src); |
| 1397 | if (!src.readyToDraw()) { |
| 1398 | // zero out the alpha buffer and return |
| 1399 | while (--h >= 0) { |
| 1400 | memset(alpha, 0, w); |
| 1401 | alpha += alphaRowBytes; |
| 1402 | } |
| 1403 | return false; |
| 1404 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 1405 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1406 | if (SkBitmap::kA8_Config == config && !src.isOpaque()) { |
| 1407 | const uint8_t* s = src.getAddr8(0, 0); |
| 1408 | while (--h >= 0) { |
| 1409 | memcpy(alpha, s, w); |
| 1410 | s += rb; |
| 1411 | alpha += alphaRowBytes; |
| 1412 | } |
| 1413 | } else if (SkBitmap::kARGB_8888_Config == config && !src.isOpaque()) { |
| 1414 | const SkPMColor* SK_RESTRICT s = src.getAddr32(0, 0); |
| 1415 | while (--h >= 0) { |
| 1416 | for (int x = 0; x < w; x++) { |
| 1417 | alpha[x] = SkGetPackedA32(s[x]); |
| 1418 | } |
| 1419 | s = (const SkPMColor*)((const char*)s + rb); |
| 1420 | alpha += alphaRowBytes; |
| 1421 | } |
| 1422 | } else if (SkBitmap::kARGB_4444_Config == config && !src.isOpaque()) { |
| 1423 | const SkPMColor16* SK_RESTRICT s = src.getAddr16(0, 0); |
| 1424 | while (--h >= 0) { |
| 1425 | for (int x = 0; x < w; x++) { |
| 1426 | alpha[x] = SkPacked4444ToA32(s[x]); |
| 1427 | } |
| 1428 | s = (const SkPMColor16*)((const char*)s + rb); |
| 1429 | alpha += alphaRowBytes; |
| 1430 | } |
| 1431 | } else if (SkBitmap::kIndex8_Config == config && !src.isOpaque()) { |
| 1432 | SkColorTable* ct = src.getColorTable(); |
| 1433 | if (ct) { |
| 1434 | const SkPMColor* SK_RESTRICT table = ct->lockColors(); |
| 1435 | const uint8_t* SK_RESTRICT s = src.getAddr8(0, 0); |
| 1436 | while (--h >= 0) { |
| 1437 | for (int x = 0; x < w; x++) { |
| 1438 | alpha[x] = SkGetPackedA32(table[s[x]]); |
| 1439 | } |
| 1440 | s += rb; |
| 1441 | alpha += alphaRowBytes; |
| 1442 | } |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 1443 | ct->unlockColors(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1444 | } |
| 1445 | } else { // src is opaque, so just fill alpha[] with 0xFF |
| 1446 | memset(alpha, 0xFF, h * alphaRowBytes); |
| 1447 | } |
reed@android.com | 1cdcb51 | 2009-08-24 19:11:00 +0000 | [diff] [blame] | 1448 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | #include "SkPaint.h" |
| 1452 | #include "SkMaskFilter.h" |
| 1453 | #include "SkMatrix.h" |
| 1454 | |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 1455 | bool SkBitmap::extractAlpha(SkBitmap* dst, const SkPaint* paint, |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 1456 | Allocator *allocator, SkIPoint* offset) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1457 | SkDEBUGCODE(this->validate();) |
| 1458 | |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 1459 | SkBitmap tmpBitmap; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1460 | SkMatrix identity; |
| 1461 | SkMask srcM, dstM; |
| 1462 | |
| 1463 | srcM.fBounds.set(0, 0, this->width(), this->height()); |
| 1464 | srcM.fRowBytes = SkAlign4(this->width()); |
| 1465 | srcM.fFormat = SkMask::kA8_Format; |
| 1466 | |
| 1467 | SkMaskFilter* filter = paint ? paint->getMaskFilter() : NULL; |
| 1468 | |
| 1469 | // compute our (larger?) dst bounds if we have a filter |
| 1470 | if (NULL != filter) { |
| 1471 | identity.reset(); |
| 1472 | srcM.fImage = NULL; |
| 1473 | if (!filter->filterMask(&dstM, srcM, identity, NULL)) { |
| 1474 | goto NO_FILTER_CASE; |
| 1475 | } |
| 1476 | dstM.fRowBytes = SkAlign4(dstM.fBounds.width()); |
| 1477 | } else { |
| 1478 | NO_FILTER_CASE: |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 1479 | tmpBitmap.setConfig(SkBitmap::kA8_Config, this->width(), this->height(), |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1480 | srcM.fRowBytes); |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 1481 | if (!tmpBitmap.allocPixels(allocator, NULL)) { |
| 1482 | // Allocation of pixels for alpha bitmap failed. |
| 1483 | SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n", |
| 1484 | tmpBitmap.width(), tmpBitmap.height()); |
| 1485 | return false; |
| 1486 | } |
| 1487 | GetBitmapAlpha(*this, tmpBitmap.getAddr8(0, 0), srcM.fRowBytes); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1488 | if (offset) { |
| 1489 | offset->set(0, 0); |
| 1490 | } |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 1491 | tmpBitmap.swap(*dst); |
| 1492 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1493 | } |
bungeman@google.com | 02f5584 | 2011-10-04 21:25:00 +0000 | [diff] [blame] | 1494 | srcM.fImage = SkMask::AllocImage(srcM.computeImageSize()); |
| 1495 | SkAutoMaskFreeImage srcCleanup(srcM.fImage); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1496 | |
| 1497 | GetBitmapAlpha(*this, srcM.fImage, srcM.fRowBytes); |
| 1498 | if (!filter->filterMask(&dstM, srcM, identity, NULL)) { |
| 1499 | goto NO_FILTER_CASE; |
| 1500 | } |
bungeman@google.com | 02f5584 | 2011-10-04 21:25:00 +0000 | [diff] [blame] | 1501 | SkAutoMaskFreeImage dstCleanup(dstM.fImage); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1502 | |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 1503 | tmpBitmap.setConfig(SkBitmap::kA8_Config, dstM.fBounds.width(), |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1504 | dstM.fBounds.height(), dstM.fRowBytes); |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 1505 | if (!tmpBitmap.allocPixels(allocator, NULL)) { |
| 1506 | // Allocation of pixels for alpha bitmap failed. |
| 1507 | SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n", |
| 1508 | tmpBitmap.width(), tmpBitmap.height()); |
| 1509 | return false; |
| 1510 | } |
| 1511 | memcpy(tmpBitmap.getPixels(), dstM.fImage, dstM.computeImageSize()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1512 | if (offset) { |
| 1513 | offset->set(dstM.fBounds.fLeft, dstM.fBounds.fTop); |
| 1514 | } |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 1515 | SkDEBUGCODE(tmpBitmap.validate();) |
| 1516 | |
| 1517 | tmpBitmap.swap(*dst); |
| 1518 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | /////////////////////////////////////////////////////////////////////////////// |
| 1522 | |
| 1523 | enum { |
| 1524 | SERIALIZE_PIXELTYPE_NONE, |
djsollen@google.com | 21830d9 | 2012-08-07 19:49:41 +0000 | [diff] [blame] | 1525 | SERIALIZE_PIXELTYPE_REF_DATA |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1526 | }; |
| 1527 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1528 | void SkBitmap::flatten(SkFlattenableWriteBuffer& buffer) const { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 1529 | buffer.writeInt(fWidth); |
| 1530 | buffer.writeInt(fHeight); |
| 1531 | buffer.writeInt(fRowBytes); |
| 1532 | buffer.writeInt(fConfig); |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 1533 | buffer.writeInt(fAlphaType); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1534 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1535 | if (fPixelRef) { |
djsollen@google.com | 5370cd9 | 2012-03-28 20:47:01 +0000 | [diff] [blame] | 1536 | if (fPixelRef->getFactory()) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 1537 | buffer.writeInt(SERIALIZE_PIXELTYPE_REF_DATA); |
scroggo@google.com | e5f4824 | 2013-02-25 21:47:41 +0000 | [diff] [blame] | 1538 | buffer.writeUInt(SkToU32(fPixelRefOffset)); |
djsollen@google.com | 5370cd9 | 2012-03-28 20:47:01 +0000 | [diff] [blame] | 1539 | buffer.writeFlattenable(fPixelRef); |
| 1540 | return; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1541 | } |
| 1542 | // if we get here, we can't record the pixels |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 1543 | buffer.writeInt(SERIALIZE_PIXELTYPE_NONE); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1544 | } else { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 1545 | buffer.writeInt(SERIALIZE_PIXELTYPE_NONE); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | void SkBitmap::unflatten(SkFlattenableReadBuffer& buffer) { |
| 1550 | this->reset(); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1551 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1552 | int width = buffer.readInt(); |
| 1553 | int height = buffer.readInt(); |
| 1554 | int rowBytes = buffer.readInt(); |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 1555 | Config config = (Config)buffer.readInt(); |
| 1556 | SkAlphaType alphaType = (SkAlphaType)buffer.readInt(); |
| 1557 | buffer.validate((width >= 0) && (height >= 0) && (rowBytes >= 0) && |
| 1558 | SkIsValidConfig(config) && validate_alphaType(config, alphaType)); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1559 | |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 1560 | this->setConfig(config, width, height, rowBytes, alphaType); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 1561 | buffer.validate(fRowBytes >= (fWidth * fBytesPerPixel)); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1562 | |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 1563 | int reftype = buffer.readInt(); |
commit-bot@chromium.org | ce33d60 | 2013-11-25 21:46:31 +0000 | [diff] [blame] | 1564 | if (buffer.validate((SERIALIZE_PIXELTYPE_REF_DATA == reftype) || |
| 1565 | (SERIALIZE_PIXELTYPE_NONE == reftype))) { |
| 1566 | switch (reftype) { |
| 1567 | case SERIALIZE_PIXELTYPE_REF_DATA: { |
| 1568 | size_t offset = buffer.readUInt(); |
| 1569 | SkPixelRef* pr = buffer.readPixelRef(); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 1570 | if (!buffer.validate((NULL == pr) || |
| 1571 | (pr->getAllocatedSizeInBytes() >= (offset + this->getSafeSize())))) { |
| 1572 | offset = 0; |
| 1573 | } |
commit-bot@chromium.org | ce33d60 | 2013-11-25 21:46:31 +0000 | [diff] [blame] | 1574 | SkSafeUnref(this->setPixelRef(pr, offset)); |
| 1575 | break; |
| 1576 | } |
| 1577 | case SERIALIZE_PIXELTYPE_NONE: |
| 1578 | break; |
| 1579 | default: |
| 1580 | SkDEBUGFAIL("unrecognized pixeltype in serialized data"); |
| 1581 | sk_throw(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1582 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1583 | } |
| 1584 | } |
| 1585 | |
| 1586 | /////////////////////////////////////////////////////////////////////////////// |
| 1587 | |
| 1588 | SkBitmap::RLEPixels::RLEPixels(int width, int height) { |
| 1589 | fHeight = height; |
commit-bot@chromium.org | 235002f | 2013-10-09 18:39:59 +0000 | [diff] [blame] | 1590 | fYPtrs = (uint8_t**)sk_calloc_throw(height * sizeof(uint8_t*)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | SkBitmap::RLEPixels::~RLEPixels() { |
| 1594 | sk_free(fYPtrs); |
| 1595 | } |
| 1596 | |
| 1597 | /////////////////////////////////////////////////////////////////////////////// |
| 1598 | |
| 1599 | #ifdef SK_DEBUG |
| 1600 | void SkBitmap::validate() const { |
| 1601 | SkASSERT(fConfig < kConfigCount); |
| 1602 | SkASSERT(fRowBytes >= (unsigned)ComputeRowBytes((Config)fConfig, fWidth)); |
scroggo@google.com | 8e990eb | 2013-06-14 15:55:56 +0000 | [diff] [blame] | 1603 | uint8_t allFlags = kImageIsOpaque_Flag | kImageIsVolatile_Flag | kImageIsImmutable_Flag; |
| 1604 | #ifdef SK_BUILD_FOR_ANDROID |
| 1605 | allFlags |= kHasHardwareMipMap_Flag; |
| 1606 | #endif |
| 1607 | SkASSERT(fFlags <= allFlags); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1608 | SkASSERT(fPixelLockCount >= 0); |
| 1609 | SkASSERT(NULL == fColorTable || (unsigned)fColorTable->getRefCnt() < 10000); |
| 1610 | SkASSERT((uint8_t)ComputeBytesPerPixel((Config)fConfig) == fBytesPerPixel); |
| 1611 | |
| 1612 | #if 0 // these asserts are not thread-correct, so disable for now |
| 1613 | if (fPixelRef) { |
| 1614 | if (fPixelLockCount > 0) { |
reed@google.com | ff0da4f | 2012-05-17 13:14:52 +0000 | [diff] [blame] | 1615 | SkASSERT(fPixelRef->isLocked()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1616 | } else { |
| 1617 | SkASSERT(NULL == fPixels); |
| 1618 | SkASSERT(NULL == fColorTable); |
| 1619 | } |
| 1620 | } |
| 1621 | #endif |
| 1622 | } |
| 1623 | #endif |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 1624 | |
| 1625 | #ifdef SK_DEVELOPER |
| 1626 | void SkBitmap::toString(SkString* str) const { |
| 1627 | |
| 1628 | static const char* gConfigNames[kConfigCount] = { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 1629 | "NONE", "A8", "INDEX8", "565", "4444", "8888" |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 1630 | }; |
| 1631 | |
| 1632 | str->appendf("bitmap: ((%d, %d) %s", this->width(), this->height(), |
| 1633 | gConfigNames[this->config()]); |
| 1634 | |
| 1635 | str->append(" ("); |
| 1636 | if (this->isOpaque()) { |
| 1637 | str->append("opaque"); |
| 1638 | } else { |
| 1639 | str->append("transparent"); |
| 1640 | } |
| 1641 | if (this->isImmutable()) { |
| 1642 | str->append(", immutable"); |
| 1643 | } else { |
| 1644 | str->append(", not-immutable"); |
| 1645 | } |
| 1646 | str->append(")"); |
| 1647 | |
| 1648 | SkPixelRef* pr = this->pixelRef(); |
| 1649 | if (NULL == pr) { |
| 1650 | // show null or the explicit pixel address (rare) |
| 1651 | str->appendf(" pixels:%p", this->getPixels()); |
| 1652 | } else { |
| 1653 | const char* uri = pr->getURI(); |
| 1654 | if (NULL != uri) { |
| 1655 | str->appendf(" uri:\"%s\"", uri); |
| 1656 | } else { |
| 1657 | str->appendf(" pixelref:%p", pr); |
| 1658 | } |
| 1659 | } |
| 1660 | |
| 1661 | str->append(")"); |
| 1662 | } |
| 1663 | #endif |