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