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 { |
| 343 | if (NULL != fPixelRef && 1 == ++fPixelLockCount) { |
| 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 | |
| 353 | if (NULL != fPixelRef && 0 == --fPixelLockCount) { |
| 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 | |
reed@android.com | ce4e53a | 2010-09-09 16:01:26 +0000 | [diff] [blame] | 427 | SkGpuTexture* SkBitmap::getTexture() const { |
| 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: { |
| 539 | uint32_t flags = 0; |
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(); |
| 542 | // if lockPixels failed, we may not have a ctable ptr |
| 543 | if (fColorTable) { |
| 544 | flags = fColorTable->getFlags(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 545 | } |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 546 | this->unlockPixels(); |
| 547 | |
| 548 | return (flags & SkColorTable::kColorsAreOpaque_Flag) != 0; |
| 549 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 550 | |
| 551 | case kRGB_565_Config: |
| 552 | return true; |
| 553 | |
| 554 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 555 | SkDEBUGFAIL("unknown bitmap config pased to isOpaque"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 556 | return false; |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | void SkBitmap::setIsOpaque(bool isOpaque) { |
| 561 | /* we record this regardless of fConfig, though it is ignored in |
| 562 | isOpaque() for configs that can't support per-pixel alpha. |
| 563 | */ |
| 564 | if (isOpaque) { |
| 565 | fFlags |= kImageIsOpaque_Flag; |
| 566 | } else { |
| 567 | fFlags &= ~kImageIsOpaque_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; |
| 602 | case SkBitmap::kA1_Config: |
| 603 | base += x >> 3; |
| 604 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 605 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 606 | SkDEBUGFAIL("Can't return addr for config"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 607 | base = NULL; |
| 608 | break; |
| 609 | } |
| 610 | } |
| 611 | return base; |
| 612 | } |
| 613 | |
vandebo@chromium.org | 112706d | 2011-02-24 22:50:55 +0000 | [diff] [blame] | 614 | SkColor SkBitmap::getColor(int x, int y) const { |
| 615 | SkASSERT((unsigned)x < (unsigned)this->width()); |
| 616 | SkASSERT((unsigned)y < (unsigned)this->height()); |
| 617 | |
| 618 | switch (this->config()) { |
| 619 | case SkBitmap::kA1_Config: { |
reed@google.com | 3b521d0 | 2011-04-29 11:53:41 +0000 | [diff] [blame] | 620 | uint8_t* addr = this->getAddr1(x, y); |
vandebo@chromium.org | 112706d | 2011-02-24 22:50:55 +0000 | [diff] [blame] | 621 | uint8_t mask = 1 << (7 - (x % 8)); |
| 622 | if (addr[0] & mask) { |
| 623 | return SK_ColorBLACK; |
| 624 | } else { |
| 625 | return 0; |
| 626 | } |
| 627 | } |
| 628 | case SkBitmap::kA8_Config: { |
reed@google.com | 3b521d0 | 2011-04-29 11:53:41 +0000 | [diff] [blame] | 629 | uint8_t* addr = this->getAddr8(x, y); |
vandebo@chromium.org | 112706d | 2011-02-24 22:50:55 +0000 | [diff] [blame] | 630 | return SkColorSetA(0, addr[0]); |
| 631 | } |
| 632 | case SkBitmap::kIndex8_Config: { |
reed@google.com | 3b521d0 | 2011-04-29 11:53:41 +0000 | [diff] [blame] | 633 | SkPMColor c = this->getIndex8Color(x, y); |
vandebo@chromium.org | 112706d | 2011-02-24 22:50:55 +0000 | [diff] [blame] | 634 | return SkUnPreMultiply::PMColorToColor(c); |
| 635 | } |
| 636 | case SkBitmap::kRGB_565_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 | return SkPixel16ToColor(addr[0]); |
| 639 | } |
| 640 | case SkBitmap::kARGB_4444_Config: { |
reed@google.com | 3b521d0 | 2011-04-29 11:53:41 +0000 | [diff] [blame] | 641 | uint16_t* addr = this->getAddr16(x, y); |
vandebo@chromium.org | 112706d | 2011-02-24 22:50:55 +0000 | [diff] [blame] | 642 | SkPMColor c = SkPixel4444ToPixel32(addr[0]); |
| 643 | return SkUnPreMultiply::PMColorToColor(c); |
| 644 | } |
| 645 | case SkBitmap::kARGB_8888_Config: { |
reed@google.com | 3b521d0 | 2011-04-29 11:53:41 +0000 | [diff] [blame] | 646 | uint32_t* addr = this->getAddr32(x, y); |
vandebo@chromium.org | 112706d | 2011-02-24 22:50:55 +0000 | [diff] [blame] | 647 | return SkUnPreMultiply::PMColorToColor(addr[0]); |
| 648 | } |
vandebo@chromium.org | 112706d | 2011-02-24 22:50:55 +0000 | [diff] [blame] | 649 | case kNo_Config: |
vandebo@chromium.org | 112706d | 2011-02-24 22:50:55 +0000 | [diff] [blame] | 650 | SkASSERT(false); |
| 651 | return 0; |
| 652 | } |
| 653 | SkASSERT(false); // Not reached. |
| 654 | return 0; |
| 655 | } |
| 656 | |
reed@google.com | 2a7579d | 2012-11-07 18:30:18 +0000 | [diff] [blame] | 657 | bool SkBitmap::ComputeIsOpaque(const SkBitmap& bm) { |
| 658 | SkAutoLockPixels alp(bm); |
| 659 | if (!bm.getPixels()) { |
| 660 | return false; |
| 661 | } |
| 662 | |
| 663 | const int height = bm.height(); |
| 664 | const int width = bm.width(); |
| 665 | |
| 666 | switch (bm.config()) { |
| 667 | case SkBitmap::kA1_Config: { |
| 668 | // TODO |
| 669 | } break; |
| 670 | case SkBitmap::kA8_Config: { |
| 671 | unsigned a = 0xFF; |
| 672 | for (int y = 0; y < height; ++y) { |
| 673 | const uint8_t* row = bm.getAddr8(0, y); |
| 674 | for (int x = 0; x < width; ++x) { |
| 675 | a &= row[x]; |
| 676 | } |
| 677 | if (0xFF != a) { |
| 678 | return false; |
| 679 | } |
| 680 | } |
| 681 | return true; |
| 682 | } break; |
reed@google.com | 2a7579d | 2012-11-07 18:30:18 +0000 | [diff] [blame] | 683 | case SkBitmap::kIndex8_Config: { |
| 684 | SkAutoLockColors alc(bm); |
| 685 | const SkPMColor* table = alc.colors(); |
| 686 | if (!table) { |
| 687 | return false; |
| 688 | } |
reed@google.com | 140d728 | 2013-01-07 20:25:04 +0000 | [diff] [blame] | 689 | SkPMColor c = (SkPMColor)~0; |
reed@google.com | 2a7579d | 2012-11-07 18:30:18 +0000 | [diff] [blame] | 690 | for (int i = bm.getColorTable()->count() - 1; i >= 0; --i) { |
| 691 | c &= table[i]; |
| 692 | } |
| 693 | return 0xFF == SkGetPackedA32(c); |
| 694 | } break; |
| 695 | case SkBitmap::kRGB_565_Config: |
| 696 | return true; |
| 697 | break; |
| 698 | case SkBitmap::kARGB_4444_Config: { |
| 699 | unsigned c = 0xFFFF; |
| 700 | for (int y = 0; y < height; ++y) { |
| 701 | const SkPMColor16* row = bm.getAddr16(0, y); |
| 702 | for (int x = 0; x < width; ++x) { |
| 703 | c &= row[x]; |
| 704 | } |
| 705 | if (0xF != SkGetPackedA4444(c)) { |
| 706 | return false; |
| 707 | } |
| 708 | } |
| 709 | return true; |
| 710 | } break; |
| 711 | case SkBitmap::kARGB_8888_Config: { |
reed@google.com | 140d728 | 2013-01-07 20:25:04 +0000 | [diff] [blame] | 712 | SkPMColor c = (SkPMColor)~0; |
reed@google.com | 2a7579d | 2012-11-07 18:30:18 +0000 | [diff] [blame] | 713 | for (int y = 0; y < height; ++y) { |
| 714 | const SkPMColor* row = bm.getAddr32(0, y); |
| 715 | for (int x = 0; x < width; ++x) { |
| 716 | c &= row[x]; |
| 717 | } |
| 718 | if (0xFF != SkGetPackedA32(c)) { |
| 719 | return false; |
| 720 | } |
| 721 | } |
| 722 | return true; |
| 723 | } |
| 724 | default: |
| 725 | break; |
| 726 | } |
| 727 | return false; |
| 728 | } |
| 729 | |
| 730 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 731 | /////////////////////////////////////////////////////////////////////////////// |
| 732 | /////////////////////////////////////////////////////////////////////////////// |
| 733 | |
reed@google.com | 45f746f | 2013-06-21 19:51:31 +0000 | [diff] [blame] | 734 | static uint16_t pack_8888_to_4444(unsigned a, unsigned r, unsigned g, unsigned b) { |
| 735 | unsigned pixel = (SkA32To4444(a) << SK_A4444_SHIFT) | |
| 736 | (SkR32To4444(r) << SK_R4444_SHIFT) | |
| 737 | (SkG32To4444(g) << SK_G4444_SHIFT) | |
| 738 | (SkB32To4444(b) << SK_B4444_SHIFT); |
| 739 | return SkToU16(pixel); |
| 740 | } |
| 741 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 742 | void SkBitmap::eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const { |
| 743 | SkDEBUGCODE(this->validate();) |
| 744 | |
| 745 | if (0 == fWidth || 0 == fHeight || |
| 746 | kNo_Config == fConfig || kIndex8_Config == fConfig) { |
| 747 | return; |
| 748 | } |
| 749 | |
| 750 | SkAutoLockPixels alp(*this); |
| 751 | // perform this check after the lock call |
| 752 | if (!this->readyToDraw()) { |
| 753 | return; |
| 754 | } |
| 755 | |
| 756 | int height = fHeight; |
| 757 | const int width = fWidth; |
| 758 | const int rowBytes = fRowBytes; |
| 759 | |
| 760 | // make rgb premultiplied |
| 761 | if (255 != a) { |
| 762 | r = SkAlphaMul(r, a); |
| 763 | g = SkAlphaMul(g, a); |
| 764 | b = SkAlphaMul(b, a); |
| 765 | } |
| 766 | |
| 767 | switch (fConfig) { |
| 768 | case kA1_Config: { |
| 769 | uint8_t* p = (uint8_t*)fPixels; |
| 770 | const int count = (width + 7) >> 3; |
| 771 | a = (a >> 7) ? 0xFF : 0; |
| 772 | SkASSERT(count <= rowBytes); |
| 773 | while (--height >= 0) { |
| 774 | memset(p, a, count); |
| 775 | p += rowBytes; |
| 776 | } |
| 777 | break; |
| 778 | } |
| 779 | case kA8_Config: { |
| 780 | uint8_t* p = (uint8_t*)fPixels; |
| 781 | while (--height >= 0) { |
| 782 | memset(p, a, width); |
| 783 | p += rowBytes; |
| 784 | } |
| 785 | break; |
| 786 | } |
reed@google.com | 45f746f | 2013-06-21 19:51:31 +0000 | [diff] [blame] | 787 | case kARGB_4444_Config: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 788 | case kRGB_565_Config: { |
| 789 | uint16_t* p = (uint16_t*)fPixels; |
reed@google.com | 45f746f | 2013-06-21 19:51:31 +0000 | [diff] [blame] | 790 | uint16_t v; |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 791 | |
reed@google.com | 45f746f | 2013-06-21 19:51:31 +0000 | [diff] [blame] | 792 | if (kARGB_4444_Config == fConfig) { |
| 793 | v = pack_8888_to_4444(a, r, g, b); |
| 794 | } else { |
| 795 | v = SkPackRGB16(r >> (8 - SK_R16_BITS), |
| 796 | g >> (8 - SK_G16_BITS), |
| 797 | b >> (8 - SK_B16_BITS)); |
| 798 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 799 | while (--height >= 0) { |
| 800 | sk_memset16(p, v, width); |
| 801 | p = (uint16_t*)((char*)p + rowBytes); |
| 802 | } |
| 803 | break; |
| 804 | } |
| 805 | case kARGB_8888_Config: { |
| 806 | uint32_t* p = (uint32_t*)fPixels; |
| 807 | uint32_t v = SkPackARGB32(a, r, g, b); |
| 808 | |
| 809 | while (--height >= 0) { |
| 810 | sk_memset32(p, v, width); |
| 811 | p = (uint32_t*)((char*)p + rowBytes); |
| 812 | } |
| 813 | break; |
| 814 | } |
| 815 | } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 816 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 817 | this->notifyPixelsChanged(); |
| 818 | } |
| 819 | |
| 820 | ////////////////////////////////////////////////////////////////////////////////////// |
| 821 | ////////////////////////////////////////////////////////////////////////////////////// |
| 822 | |
| 823 | #define SUB_OFFSET_FAILURE ((size_t)-1) |
| 824 | |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 825 | /** |
| 826 | * Based on the Config and rowBytes() of bm, return the offset into an SkPixelRef of the pixel at |
| 827 | * (x, y). |
| 828 | * Note that the SkPixelRef does not need to be set yet. deepCopyTo takes advantage of this fact. |
| 829 | * Also note that (x, y) may be outside the range of (0 - width(), 0 - height()), so long as it is |
| 830 | * within the bounds of the SkPixelRef being used. |
| 831 | */ |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 832 | 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] | 833 | switch (bm.getConfig()) { |
| 834 | case SkBitmap::kA8_Config: |
| 835 | case SkBitmap:: kIndex8_Config: |
| 836 | // x is fine as is for the calculation |
| 837 | break; |
| 838 | |
| 839 | case SkBitmap::kRGB_565_Config: |
| 840 | case SkBitmap::kARGB_4444_Config: |
| 841 | x <<= 1; |
| 842 | break; |
| 843 | |
| 844 | case SkBitmap::kARGB_8888_Config: |
| 845 | x <<= 2; |
| 846 | break; |
| 847 | |
| 848 | case SkBitmap::kNo_Config: |
| 849 | case SkBitmap::kA1_Config: |
| 850 | default: |
| 851 | return SUB_OFFSET_FAILURE; |
| 852 | } |
| 853 | return y * bm.rowBytes() + x; |
| 854 | } |
| 855 | |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 856 | /** |
| 857 | * Using the pixelRefOffset(), rowBytes(), and Config of bm, determine the (x, y) coordinate of the |
| 858 | * upper left corner of bm relative to its SkPixelRef. |
| 859 | * x and y must be non-NULL. |
| 860 | */ |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 861 | 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] | 862 | int32_t* x, int32_t* y); |
| 863 | 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] | 864 | int32_t* x, int32_t* y) { |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 865 | SkASSERT(x != NULL && y != NULL); |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 866 | if (0 == offset) { |
| 867 | *x = *y = 0; |
| 868 | return true; |
| 869 | } |
| 870 | // Use integer division to find the correct y position. |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 871 | *y = SkToS32(offset / rowBytes); |
| 872 | // The remainder will be the x position, after we reverse get_sub_offset. |
| 873 | *x = SkToS32(offset % rowBytes); |
| 874 | switch (config) { |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 875 | case SkBitmap::kA8_Config: |
| 876 | // Fall through. |
| 877 | case SkBitmap::kIndex8_Config: |
| 878 | // x is unmodified |
| 879 | break; |
| 880 | |
| 881 | case SkBitmap::kRGB_565_Config: |
| 882 | // Fall through. |
| 883 | case SkBitmap::kARGB_4444_Config: |
| 884 | *x >>= 1; |
| 885 | break; |
| 886 | |
| 887 | case SkBitmap::kARGB_8888_Config: |
| 888 | *x >>= 2; |
| 889 | break; |
| 890 | |
| 891 | case SkBitmap::kNo_Config: |
| 892 | // Fall through. |
| 893 | case SkBitmap::kA1_Config: |
| 894 | // Fall through. |
| 895 | default: |
| 896 | return false; |
| 897 | } |
| 898 | return true; |
| 899 | } |
| 900 | |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 901 | static bool get_upper_left_from_offset(const SkBitmap& bm, int32_t* x, int32_t* y) { |
| 902 | return get_upper_left_from_offset(bm.config(), bm.pixelRefOffset(), bm.rowBytes(), x, y); |
| 903 | } |
| 904 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 905 | bool SkBitmap::extractSubset(SkBitmap* result, const SkIRect& subset) const { |
| 906 | SkDEBUGCODE(this->validate();) |
| 907 | |
djsollen@google.com | c84b833 | 2012-07-27 13:41:44 +0000 | [diff] [blame] | 908 | if (NULL == result || NULL == fPixelRef) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 909 | return false; // no src pixels |
| 910 | } |
| 911 | |
| 912 | SkIRect srcRect, r; |
| 913 | srcRect.set(0, 0, this->width(), this->height()); |
| 914 | if (!r.intersect(srcRect, subset)) { |
| 915 | return false; // r is empty (i.e. no intersection) |
| 916 | } |
| 917 | |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 918 | if (fPixelRef->getTexture() != NULL) { |
| 919 | // Do a deep copy |
| 920 | SkPixelRef* pixelRef = fPixelRef->deepCopy(this->config(), &subset); |
| 921 | if (pixelRef != NULL) { |
| 922 | SkBitmap dst; |
| 923 | dst.setConfig(this->config(), subset.width(), subset.height()); |
| 924 | dst.setIsVolatile(this->isVolatile()); |
| 925 | dst.setIsOpaque(this->isOpaque()); |
| 926 | dst.setPixelRef(pixelRef)->unref(); |
| 927 | SkDEBUGCODE(dst.validate()); |
| 928 | result->swap(dst); |
| 929 | return true; |
| 930 | } |
| 931 | } |
| 932 | |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 933 | // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have |
| 934 | // exited above. |
| 935 | SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width())); |
| 936 | SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height())); |
| 937 | |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 938 | size_t offset = get_sub_offset(*this, r.fLeft, r.fTop); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 939 | if (SUB_OFFSET_FAILURE == offset) { |
| 940 | return false; // config not supported |
| 941 | } |
| 942 | |
| 943 | SkBitmap dst; |
| 944 | dst.setConfig(this->config(), r.width(), r.height(), this->rowBytes()); |
skyostil@google.com | 0eb7576 | 2012-01-16 10:45:53 +0000 | [diff] [blame] | 945 | dst.setIsVolatile(this->isVolatile()); |
reed@google.com | 04685d2 | 2012-10-15 13:45:40 +0000 | [diff] [blame] | 946 | dst.setIsOpaque(this->isOpaque()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 947 | |
| 948 | if (fPixelRef) { |
| 949 | // share the pixelref with a custom offset |
| 950 | dst.setPixelRef(fPixelRef, fPixelRefOffset + offset); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 951 | } |
| 952 | SkDEBUGCODE(dst.validate();) |
| 953 | |
| 954 | // we know we're good, so commit to result |
| 955 | result->swap(dst); |
| 956 | return true; |
| 957 | } |
| 958 | |
| 959 | /////////////////////////////////////////////////////////////////////////////// |
| 960 | |
| 961 | #include "SkCanvas.h" |
| 962 | #include "SkPaint.h" |
| 963 | |
reed@android.com | fbaa88d | 2009-05-06 17:44:34 +0000 | [diff] [blame] | 964 | bool SkBitmap::canCopyTo(Config dstConfig) const { |
| 965 | if (this->getConfig() == kNo_Config) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 966 | return false; |
| 967 | } |
| 968 | |
reed@android.com | fbaa88d | 2009-05-06 17:44:34 +0000 | [diff] [blame] | 969 | bool sameConfigs = (this->config() == dstConfig); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 970 | switch (dstConfig) { |
| 971 | case kA8_Config: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 972 | case kRGB_565_Config: |
| 973 | case kARGB_8888_Config: |
| 974 | break; |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 975 | case kA1_Config: |
| 976 | case kIndex8_Config: |
reed@google.com | 6ba4572 | 2013-06-21 18:30:53 +0000 | [diff] [blame] | 977 | case kARGB_4444_Config: |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 978 | if (!sameConfigs) { |
| 979 | return false; |
| 980 | } |
| 981 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 982 | default: |
| 983 | return false; |
| 984 | } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 985 | |
| 986 | // do not copy src if srcConfig == kA1_Config while dstConfig != kA1_Config |
| 987 | if (this->getConfig() == kA1_Config && !sameConfigs) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 988 | return false; |
| 989 | } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 990 | |
reed@android.com | fbaa88d | 2009-05-06 17:44:34 +0000 | [diff] [blame] | 991 | return true; |
| 992 | } |
| 993 | |
| 994 | bool SkBitmap::copyTo(SkBitmap* dst, Config dstConfig, Allocator* alloc) const { |
| 995 | if (!this->canCopyTo(dstConfig)) { |
| 996 | return false; |
| 997 | } |
| 998 | |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 999 | // if we have a texture, first get those pixels |
| 1000 | SkBitmap tmpSrc; |
| 1001 | const SkBitmap* src = this; |
| 1002 | |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 1003 | if (fPixelRef) { |
| 1004 | SkIRect subset; |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 1005 | if (get_upper_left_from_offset(*this, &subset.fLeft, &subset.fTop)) { |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 1006 | subset.fRight = subset.fLeft + fWidth; |
| 1007 | subset.fBottom = subset.fTop + fHeight; |
| 1008 | if (fPixelRef->readPixels(&tmpSrc, &subset)) { |
| 1009 | SkASSERT(tmpSrc.width() == this->width()); |
| 1010 | SkASSERT(tmpSrc.height() == this->height()); |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 1011 | |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 1012 | // did we get lucky and we can just return tmpSrc? |
| 1013 | if (tmpSrc.config() == dstConfig && NULL == alloc) { |
| 1014 | dst->swap(tmpSrc); |
| 1015 | if (dst->pixelRef() && this->config() == dstConfig) { |
| 1016 | dst->pixelRef()->fGenerationID = fPixelRef->getGenerationID(); |
| 1017 | } |
| 1018 | return true; |
| 1019 | } |
| 1020 | |
| 1021 | // fall through to the raster case |
| 1022 | src = &tmpSrc; |
scroggo@google.com | d5764e8 | 2012-08-22 15:00:05 +0000 | [diff] [blame] | 1023 | } |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 1024 | } |
reed@android.com | fbaa88d | 2009-05-06 17:44:34 +0000 | [diff] [blame] | 1025 | } |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 1026 | |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 1027 | // we lock this now, since we may need its colortable |
| 1028 | SkAutoLockPixels srclock(*src); |
| 1029 | if (!src->readyToDraw()) { |
| 1030 | return false; |
| 1031 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1032 | |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 1033 | SkBitmap tmpDst; |
| 1034 | tmpDst.setConfig(dstConfig, src->width(), src->height()); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1035 | |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1036 | // allocate colortable if srcConfig == kIndex8_Config |
| 1037 | SkColorTable* ctable = (dstConfig == kIndex8_Config) ? |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 1038 | new SkColorTable(*src->getColorTable()) : NULL; |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1039 | SkAutoUnref au(ctable); |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 1040 | if (!tmpDst.allocPixels(alloc, ctable)) { |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1041 | return false; |
| 1042 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1043 | |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 1044 | if (!tmpDst.readyToDraw()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1045 | // allocator/lock failed |
| 1046 | return false; |
| 1047 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1048 | |
reed@android.com | fbaa88d | 2009-05-06 17:44:34 +0000 | [diff] [blame] | 1049 | /* do memcpy for the same configs cases, else use drawing |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1050 | */ |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 1051 | if (src->config() == dstConfig) { |
| 1052 | if (tmpDst.getSize() == src->getSize()) { |
| 1053 | memcpy(tmpDst.getPixels(), src->getPixels(), src->getSafeSize()); |
scroggo@google.com | d5764e8 | 2012-08-22 15:00:05 +0000 | [diff] [blame] | 1054 | SkPixelRef* pixelRef = tmpDst.pixelRef(); |
| 1055 | if (pixelRef != NULL) { |
| 1056 | pixelRef->fGenerationID = this->getGenerationID(); |
| 1057 | } |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 1058 | } else { |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 1059 | const char* srcP = reinterpret_cast<const char*>(src->getPixels()); |
| 1060 | char* dstP = reinterpret_cast<char*>(tmpDst.getPixels()); |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 1061 | // 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] | 1062 | size_t bytesToCopy = tmpDst.width() * tmpDst.bytesPerPixel(); |
| 1063 | for (int y = 0; y < tmpDst.height(); y++) { |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 1064 | memcpy(dstP, srcP, bytesToCopy); |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 1065 | srcP += src->rowBytes(); |
| 1066 | dstP += tmpDst.rowBytes(); |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 1067 | } |
| 1068 | } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1069 | } else { |
| 1070 | // if the src has alpha, we have to clear the dst first |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 1071 | if (!src->isOpaque()) { |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 1072 | tmpDst.eraseColor(SK_ColorTRANSPARENT); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 1075 | SkCanvas canvas(tmpDst); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1076 | SkPaint paint; |
| 1077 | |
| 1078 | paint.setDither(true); |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 1079 | canvas.drawBitmap(*src, 0, 0, &paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 1082 | tmpDst.setIsOpaque(src->isOpaque()); |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 1083 | |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 1084 | dst->swap(tmpDst); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1085 | return true; |
| 1086 | } |
| 1087 | |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 1088 | bool SkBitmap::deepCopyTo(SkBitmap* dst, Config dstConfig) const { |
| 1089 | if (!this->canCopyTo(dstConfig)) { |
| 1090 | return false; |
| 1091 | } |
| 1092 | |
| 1093 | // If we have a PixelRef, and it supports deep copy, use it. |
| 1094 | // Currently supported only by texture-backed bitmaps. |
| 1095 | if (fPixelRef) { |
| 1096 | SkPixelRef* pixelRef = fPixelRef->deepCopy(dstConfig); |
| 1097 | if (pixelRef) { |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 1098 | uint32_t rowBytes; |
scroggo@google.com | d5764e8 | 2012-08-22 15:00:05 +0000 | [diff] [blame] | 1099 | if (dstConfig == fConfig) { |
| 1100 | pixelRef->fGenerationID = fPixelRef->getGenerationID(); |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 1101 | // Use the same rowBytes as the original. |
| 1102 | rowBytes = fRowBytes; |
| 1103 | } else { |
| 1104 | // With the new config, an appropriate fRowBytes will be computed by setConfig. |
| 1105 | rowBytes = 0; |
scroggo@google.com | d5764e8 | 2012-08-22 15:00:05 +0000 | [diff] [blame] | 1106 | } |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 1107 | dst->setConfig(dstConfig, fWidth, fHeight, rowBytes); |
| 1108 | |
| 1109 | size_t pixelRefOffset; |
| 1110 | if (0 == fPixelRefOffset || dstConfig == fConfig) { |
| 1111 | // Use the same offset as the original. |
| 1112 | pixelRefOffset = fPixelRefOffset; |
| 1113 | } else { |
| 1114 | // Find the correct offset in the new config. This needs to be done after calling |
| 1115 | // setConfig so dst's fConfig and fRowBytes have been set properly. |
scroggo@google.com | e5f4824 | 2013-02-25 21:47:41 +0000 | [diff] [blame] | 1116 | int32_t x, y; |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 1117 | if (!get_upper_left_from_offset(*this, &x, &y)) { |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 1118 | return false; |
| 1119 | } |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 1120 | pixelRefOffset = get_sub_offset(*dst, x, y); |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 1121 | if (SUB_OFFSET_FAILURE == pixelRefOffset) { |
| 1122 | return false; |
| 1123 | } |
| 1124 | } |
| 1125 | dst->setPixelRef(pixelRef, pixelRefOffset)->unref(); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 1126 | return true; |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | if (this->getTexture()) { |
| 1131 | return false; |
| 1132 | } else { |
| 1133 | return this->copyTo(dst, dstConfig, NULL); |
| 1134 | } |
| 1135 | } |
| 1136 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1137 | /////////////////////////////////////////////////////////////////////////////// |
| 1138 | /////////////////////////////////////////////////////////////////////////////// |
| 1139 | |
| 1140 | static void downsampleby2_proc32(SkBitmap* dst, int x, int y, |
| 1141 | const SkBitmap& src) { |
| 1142 | x <<= 1; |
| 1143 | y <<= 1; |
| 1144 | const SkPMColor* p = src.getAddr32(x, y); |
reed@android.com | 829c83c | 2009-06-08 12:05:31 +0000 | [diff] [blame] | 1145 | const SkPMColor* baseP = p; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1146 | SkPMColor c, ag, rb; |
| 1147 | |
| 1148 | c = *p; ag = (c >> 8) & 0xFF00FF; rb = c & 0xFF00FF; |
| 1149 | if (x < src.width() - 1) { |
| 1150 | p += 1; |
| 1151 | } |
| 1152 | c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF; |
| 1153 | |
reed@android.com | 829c83c | 2009-06-08 12:05:31 +0000 | [diff] [blame] | 1154 | p = baseP; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1155 | if (y < src.height() - 1) { |
reed@android.com | 829c83c | 2009-06-08 12:05:31 +0000 | [diff] [blame] | 1156 | p += src.rowBytes() >> 2; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1157 | } |
| 1158 | c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF; |
| 1159 | if (x < src.width() - 1) { |
| 1160 | p += 1; |
| 1161 | } |
| 1162 | c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF; |
| 1163 | |
| 1164 | *dst->getAddr32(x >> 1, y >> 1) = |
| 1165 | ((rb >> 2) & 0xFF00FF) | ((ag << 6) & 0xFF00FF00); |
| 1166 | } |
| 1167 | |
| 1168 | static inline uint32_t expand16(U16CPU c) { |
| 1169 | return (c & ~SK_G16_MASK_IN_PLACE) | ((c & SK_G16_MASK_IN_PLACE) << 16); |
| 1170 | } |
| 1171 | |
| 1172 | // returns dirt in the top 16bits, but we don't care, since we only |
| 1173 | // store the low 16bits. |
| 1174 | static inline U16CPU pack16(uint32_t c) { |
| 1175 | return (c & ~SK_G16_MASK_IN_PLACE) | ((c >> 16) & SK_G16_MASK_IN_PLACE); |
| 1176 | } |
| 1177 | |
| 1178 | static void downsampleby2_proc16(SkBitmap* dst, int x, int y, |
| 1179 | const SkBitmap& src) { |
| 1180 | x <<= 1; |
| 1181 | y <<= 1; |
| 1182 | const uint16_t* p = src.getAddr16(x, y); |
reed@android.com | 829c83c | 2009-06-08 12:05:31 +0000 | [diff] [blame] | 1183 | const uint16_t* baseP = p; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1184 | SkPMColor c; |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1185 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1186 | c = expand16(*p); |
reed@android.com | 829c83c | 2009-06-08 12:05:31 +0000 | [diff] [blame] | 1187 | if (x < src.width() - 1) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1188 | p += 1; |
| 1189 | } |
| 1190 | c += expand16(*p); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1191 | |
reed@android.com | 829c83c | 2009-06-08 12:05:31 +0000 | [diff] [blame] | 1192 | p = baseP; |
| 1193 | if (y < src.height() - 1) { |
| 1194 | p += src.rowBytes() >> 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1195 | } |
| 1196 | c += expand16(*p); |
reed@android.com | 829c83c | 2009-06-08 12:05:31 +0000 | [diff] [blame] | 1197 | if (x < src.width() - 1) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1198 | p += 1; |
| 1199 | } |
| 1200 | c += expand16(*p); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1201 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1202 | *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)pack16(c >> 2); |
| 1203 | } |
| 1204 | |
| 1205 | static uint32_t expand4444(U16CPU c) { |
| 1206 | return (c & 0xF0F) | ((c & ~0xF0F) << 12); |
| 1207 | } |
| 1208 | |
| 1209 | static U16CPU collaps4444(uint32_t c) { |
| 1210 | return (c & 0xF0F) | ((c >> 12) & ~0xF0F); |
| 1211 | } |
| 1212 | |
| 1213 | static void downsampleby2_proc4444(SkBitmap* dst, int x, int y, |
| 1214 | const SkBitmap& src) { |
| 1215 | x <<= 1; |
| 1216 | y <<= 1; |
| 1217 | const uint16_t* p = src.getAddr16(x, y); |
reed@android.com | 829c83c | 2009-06-08 12:05:31 +0000 | [diff] [blame] | 1218 | const uint16_t* baseP = p; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1219 | uint32_t c; |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1220 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1221 | c = expand4444(*p); |
| 1222 | if (x < src.width() - 1) { |
| 1223 | p += 1; |
| 1224 | } |
| 1225 | c += expand4444(*p); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1226 | |
reed@android.com | 829c83c | 2009-06-08 12:05:31 +0000 | [diff] [blame] | 1227 | p = baseP; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1228 | if (y < src.height() - 1) { |
reed@android.com | 829c83c | 2009-06-08 12:05:31 +0000 | [diff] [blame] | 1229 | p += src.rowBytes() >> 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1230 | } |
| 1231 | c += expand4444(*p); |
| 1232 | if (x < src.width() - 1) { |
| 1233 | p += 1; |
| 1234 | } |
| 1235 | c += expand4444(*p); |
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 | *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)collaps4444(c >> 2); |
| 1238 | } |
| 1239 | |
| 1240 | void SkBitmap::buildMipMap(bool forceRebuild) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1241 | if (forceRebuild) |
| 1242 | this->freeMipMap(); |
| 1243 | else if (fMipMap) |
| 1244 | return; // we're already built |
| 1245 | |
| 1246 | SkASSERT(NULL == fMipMap); |
| 1247 | |
| 1248 | void (*proc)(SkBitmap* dst, int x, int y, const SkBitmap& src); |
| 1249 | |
| 1250 | const SkBitmap::Config config = this->getConfig(); |
| 1251 | |
| 1252 | switch (config) { |
| 1253 | case kARGB_8888_Config: |
| 1254 | proc = downsampleby2_proc32; |
| 1255 | break; |
| 1256 | case kRGB_565_Config: |
| 1257 | proc = downsampleby2_proc16; |
| 1258 | break; |
| 1259 | case kARGB_4444_Config: |
| 1260 | proc = downsampleby2_proc4444; |
| 1261 | break; |
| 1262 | case kIndex8_Config: |
| 1263 | case kA8_Config: |
| 1264 | default: |
| 1265 | return; // don't build mipmaps for these configs |
| 1266 | } |
reed@android.com | 89bb83a | 2009-05-29 21:30:42 +0000 | [diff] [blame] | 1267 | |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 1268 | SkAutoLockPixels alp(*this); |
| 1269 | if (!this->readyToDraw()) { |
| 1270 | return; |
| 1271 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1272 | |
| 1273 | // whip through our loop to compute the exact size needed |
| 1274 | size_t size = 0; |
| 1275 | int maxLevels = 0; |
| 1276 | { |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 1277 | int width = this->width(); |
| 1278 | int height = this->height(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1279 | for (;;) { |
| 1280 | width >>= 1; |
| 1281 | height >>= 1; |
| 1282 | if (0 == width || 0 == height) { |
| 1283 | break; |
| 1284 | } |
| 1285 | size += ComputeRowBytes(config, width) * height; |
| 1286 | maxLevels += 1; |
| 1287 | } |
| 1288 | } |
reed@android.com | 89bb83a | 2009-05-29 21:30:42 +0000 | [diff] [blame] | 1289 | |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 1290 | // nothing to build |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1291 | if (0 == maxLevels) { |
| 1292 | return; |
| 1293 | } |
| 1294 | |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 1295 | SkBitmap srcBM(*this); |
| 1296 | srcBM.lockPixels(); |
| 1297 | if (!srcBM.readyToDraw()) { |
| 1298 | return; |
| 1299 | } |
| 1300 | |
| 1301 | MipMap* mm = MipMap::Alloc(maxLevels, size); |
| 1302 | if (NULL == mm) { |
| 1303 | return; |
| 1304 | } |
| 1305 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1306 | MipLevel* level = mm->levels(); |
| 1307 | uint8_t* addr = (uint8_t*)mm->pixels(); |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 1308 | int width = this->width(); |
| 1309 | int height = this->height(); |
scroggo@google.com | e5f4824 | 2013-02-25 21:47:41 +0000 | [diff] [blame] | 1310 | uint32_t rowBytes; |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 1311 | SkBitmap dstBM; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1312 | |
| 1313 | for (int i = 0; i < maxLevels; i++) { |
| 1314 | width >>= 1; |
| 1315 | height >>= 1; |
scroggo@google.com | e5f4824 | 2013-02-25 21:47:41 +0000 | [diff] [blame] | 1316 | rowBytes = SkToU32(ComputeRowBytes(config, width)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1317 | |
| 1318 | level[i].fPixels = addr; |
reed@android.com | f459a49 | 2009-03-27 12:33:50 +0000 | [diff] [blame] | 1319 | level[i].fWidth = width; |
| 1320 | level[i].fHeight = height; |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 1321 | level[i].fRowBytes = rowBytes; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1322 | |
| 1323 | dstBM.setConfig(config, width, height, rowBytes); |
| 1324 | dstBM.setPixels(addr); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1325 | |
bungeman@google.com | 7cf0e9e | 2012-07-25 16:09:10 +0000 | [diff] [blame] | 1326 | srcBM.lockPixels(); |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 1327 | for (int y = 0; y < height; y++) { |
| 1328 | for (int x = 0; x < width; x++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1329 | proc(&dstBM, x, y, srcBM); |
| 1330 | } |
| 1331 | } |
bungeman@google.com | 7cf0e9e | 2012-07-25 16:09:10 +0000 | [diff] [blame] | 1332 | srcBM.unlockPixels(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1333 | |
| 1334 | srcBM = dstBM; |
| 1335 | addr += height * rowBytes; |
| 1336 | } |
| 1337 | SkASSERT(addr == (uint8_t*)mm->pixels() + size); |
| 1338 | fMipMap = mm; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | bool SkBitmap::hasMipMap() const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1342 | return fMipMap != NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
| 1345 | int SkBitmap::extractMipLevel(SkBitmap* dst, SkFixed sx, SkFixed sy) { |
reed@android.com | 83f7bc3 | 2009-07-17 02:42:41 +0000 | [diff] [blame] | 1346 | if (NULL == fMipMap) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1347 | return 0; |
reed@android.com | 83f7bc3 | 2009-07-17 02:42:41 +0000 | [diff] [blame] | 1348 | } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1349 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1350 | int level = ComputeMipLevel(sx, sy) >> 16; |
| 1351 | SkASSERT(level >= 0); |
| 1352 | if (level <= 0) { |
| 1353 | return 0; |
| 1354 | } |
| 1355 | |
| 1356 | if (level >= fMipMap->fLevelCount) { |
| 1357 | level = fMipMap->fLevelCount - 1; |
| 1358 | } |
| 1359 | if (dst) { |
| 1360 | const MipLevel& mip = fMipMap->levels()[level - 1]; |
| 1361 | dst->setConfig((SkBitmap::Config)this->config(), |
| 1362 | mip.fWidth, mip.fHeight, mip.fRowBytes); |
| 1363 | dst->setPixels(mip.fPixels); |
| 1364 | } |
| 1365 | return level; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
| 1368 | SkFixed SkBitmap::ComputeMipLevel(SkFixed sx, SkFixed sy) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1369 | sx = SkAbs32(sx); |
| 1370 | sy = SkAbs32(sy); |
| 1371 | if (sx < sy) { |
| 1372 | sx = sy; |
| 1373 | } |
| 1374 | if (sx < SK_Fixed1) { |
| 1375 | return 0; |
| 1376 | } |
| 1377 | int clz = SkCLZ(sx); |
| 1378 | SkASSERT(clz >= 1 && clz <= 15); |
| 1379 | return SkIntToFixed(15 - clz) + ((unsigned)(sx << (clz + 1)) >> 16); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
| 1382 | /////////////////////////////////////////////////////////////////////////////// |
| 1383 | |
tomhudson@google.com | a87cd2a | 2011-06-15 16:50:27 +0000 | [diff] [blame] | 1384 | static bool GetBitmapAlpha(const SkBitmap& src, uint8_t* SK_RESTRICT alpha, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1385 | int alphaRowBytes) { |
| 1386 | SkASSERT(alpha != NULL); |
| 1387 | SkASSERT(alphaRowBytes >= src.width()); |
| 1388 | |
| 1389 | SkBitmap::Config config = src.getConfig(); |
| 1390 | int w = src.width(); |
| 1391 | int h = src.height(); |
scroggo@google.com | e5f4824 | 2013-02-25 21:47:41 +0000 | [diff] [blame] | 1392 | size_t rb = src.rowBytes(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1393 | |
reed@android.com | 1cdcb51 | 2009-08-24 19:11:00 +0000 | [diff] [blame] | 1394 | SkAutoLockPixels alp(src); |
| 1395 | if (!src.readyToDraw()) { |
| 1396 | // zero out the alpha buffer and return |
| 1397 | while (--h >= 0) { |
| 1398 | memset(alpha, 0, w); |
| 1399 | alpha += alphaRowBytes; |
| 1400 | } |
| 1401 | return false; |
| 1402 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 1403 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1404 | if (SkBitmap::kA8_Config == config && !src.isOpaque()) { |
| 1405 | const uint8_t* s = src.getAddr8(0, 0); |
| 1406 | while (--h >= 0) { |
| 1407 | memcpy(alpha, s, w); |
| 1408 | s += rb; |
| 1409 | alpha += alphaRowBytes; |
| 1410 | } |
| 1411 | } else if (SkBitmap::kARGB_8888_Config == config && !src.isOpaque()) { |
| 1412 | const SkPMColor* SK_RESTRICT s = src.getAddr32(0, 0); |
| 1413 | while (--h >= 0) { |
| 1414 | for (int x = 0; x < w; x++) { |
| 1415 | alpha[x] = SkGetPackedA32(s[x]); |
| 1416 | } |
| 1417 | s = (const SkPMColor*)((const char*)s + rb); |
| 1418 | alpha += alphaRowBytes; |
| 1419 | } |
| 1420 | } else if (SkBitmap::kARGB_4444_Config == config && !src.isOpaque()) { |
| 1421 | const SkPMColor16* SK_RESTRICT s = src.getAddr16(0, 0); |
| 1422 | while (--h >= 0) { |
| 1423 | for (int x = 0; x < w; x++) { |
| 1424 | alpha[x] = SkPacked4444ToA32(s[x]); |
| 1425 | } |
| 1426 | s = (const SkPMColor16*)((const char*)s + rb); |
| 1427 | alpha += alphaRowBytes; |
| 1428 | } |
| 1429 | } else if (SkBitmap::kIndex8_Config == config && !src.isOpaque()) { |
| 1430 | SkColorTable* ct = src.getColorTable(); |
| 1431 | if (ct) { |
| 1432 | const SkPMColor* SK_RESTRICT table = ct->lockColors(); |
| 1433 | const uint8_t* SK_RESTRICT s = src.getAddr8(0, 0); |
| 1434 | while (--h >= 0) { |
| 1435 | for (int x = 0; x < w; x++) { |
| 1436 | alpha[x] = SkGetPackedA32(table[s[x]]); |
| 1437 | } |
| 1438 | s += rb; |
| 1439 | alpha += alphaRowBytes; |
| 1440 | } |
| 1441 | ct->unlockColors(false); |
| 1442 | } |
| 1443 | } else { // src is opaque, so just fill alpha[] with 0xFF |
| 1444 | memset(alpha, 0xFF, h * alphaRowBytes); |
| 1445 | } |
reed@android.com | 1cdcb51 | 2009-08-24 19:11:00 +0000 | [diff] [blame] | 1446 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | #include "SkPaint.h" |
| 1450 | #include "SkMaskFilter.h" |
| 1451 | #include "SkMatrix.h" |
| 1452 | |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 1453 | bool SkBitmap::extractAlpha(SkBitmap* dst, const SkPaint* paint, |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 1454 | Allocator *allocator, SkIPoint* offset) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1455 | SkDEBUGCODE(this->validate();) |
| 1456 | |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 1457 | SkBitmap tmpBitmap; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1458 | SkMatrix identity; |
| 1459 | SkMask srcM, dstM; |
| 1460 | |
| 1461 | srcM.fBounds.set(0, 0, this->width(), this->height()); |
| 1462 | srcM.fRowBytes = SkAlign4(this->width()); |
| 1463 | srcM.fFormat = SkMask::kA8_Format; |
| 1464 | |
| 1465 | SkMaskFilter* filter = paint ? paint->getMaskFilter() : NULL; |
| 1466 | |
| 1467 | // compute our (larger?) dst bounds if we have a filter |
| 1468 | if (NULL != filter) { |
| 1469 | identity.reset(); |
| 1470 | srcM.fImage = NULL; |
| 1471 | if (!filter->filterMask(&dstM, srcM, identity, NULL)) { |
| 1472 | goto NO_FILTER_CASE; |
| 1473 | } |
| 1474 | dstM.fRowBytes = SkAlign4(dstM.fBounds.width()); |
| 1475 | } else { |
| 1476 | NO_FILTER_CASE: |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 1477 | tmpBitmap.setConfig(SkBitmap::kA8_Config, this->width(), this->height(), |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1478 | srcM.fRowBytes); |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 1479 | if (!tmpBitmap.allocPixels(allocator, NULL)) { |
| 1480 | // Allocation of pixels for alpha bitmap failed. |
| 1481 | SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n", |
| 1482 | tmpBitmap.width(), tmpBitmap.height()); |
| 1483 | return false; |
| 1484 | } |
| 1485 | GetBitmapAlpha(*this, tmpBitmap.getAddr8(0, 0), srcM.fRowBytes); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1486 | if (offset) { |
| 1487 | offset->set(0, 0); |
| 1488 | } |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 1489 | tmpBitmap.swap(*dst); |
| 1490 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1491 | } |
bungeman@google.com | 02f5584 | 2011-10-04 21:25:00 +0000 | [diff] [blame] | 1492 | srcM.fImage = SkMask::AllocImage(srcM.computeImageSize()); |
| 1493 | SkAutoMaskFreeImage srcCleanup(srcM.fImage); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1494 | |
| 1495 | GetBitmapAlpha(*this, srcM.fImage, srcM.fRowBytes); |
| 1496 | if (!filter->filterMask(&dstM, srcM, identity, NULL)) { |
| 1497 | goto NO_FILTER_CASE; |
| 1498 | } |
bungeman@google.com | 02f5584 | 2011-10-04 21:25:00 +0000 | [diff] [blame] | 1499 | SkAutoMaskFreeImage dstCleanup(dstM.fImage); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1500 | |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 1501 | tmpBitmap.setConfig(SkBitmap::kA8_Config, dstM.fBounds.width(), |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1502 | dstM.fBounds.height(), dstM.fRowBytes); |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 1503 | if (!tmpBitmap.allocPixels(allocator, NULL)) { |
| 1504 | // Allocation of pixels for alpha bitmap failed. |
| 1505 | SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n", |
| 1506 | tmpBitmap.width(), tmpBitmap.height()); |
| 1507 | return false; |
| 1508 | } |
| 1509 | memcpy(tmpBitmap.getPixels(), dstM.fImage, dstM.computeImageSize()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1510 | if (offset) { |
| 1511 | offset->set(dstM.fBounds.fLeft, dstM.fBounds.fTop); |
| 1512 | } |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 1513 | SkDEBUGCODE(tmpBitmap.validate();) |
| 1514 | |
| 1515 | tmpBitmap.swap(*dst); |
| 1516 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | /////////////////////////////////////////////////////////////////////////////// |
| 1520 | |
| 1521 | enum { |
| 1522 | SERIALIZE_PIXELTYPE_NONE, |
djsollen@google.com | 21830d9 | 2012-08-07 19:49:41 +0000 | [diff] [blame] | 1523 | SERIALIZE_PIXELTYPE_REF_DATA |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1524 | }; |
| 1525 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1526 | void SkBitmap::flatten(SkFlattenableWriteBuffer& buffer) const { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 1527 | buffer.writeInt(fWidth); |
| 1528 | buffer.writeInt(fHeight); |
| 1529 | buffer.writeInt(fRowBytes); |
| 1530 | buffer.writeInt(fConfig); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1531 | buffer.writeBool(this->isOpaque()); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1532 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1533 | if (fPixelRef) { |
djsollen@google.com | 5370cd9 | 2012-03-28 20:47:01 +0000 | [diff] [blame] | 1534 | if (fPixelRef->getFactory()) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 1535 | buffer.writeInt(SERIALIZE_PIXELTYPE_REF_DATA); |
scroggo@google.com | e5f4824 | 2013-02-25 21:47:41 +0000 | [diff] [blame] | 1536 | buffer.writeUInt(SkToU32(fPixelRefOffset)); |
djsollen@google.com | 5370cd9 | 2012-03-28 20:47:01 +0000 | [diff] [blame] | 1537 | buffer.writeFlattenable(fPixelRef); |
| 1538 | return; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1539 | } |
| 1540 | // if we get here, we can't record the pixels |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 1541 | buffer.writeInt(SERIALIZE_PIXELTYPE_NONE); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1542 | } else { |
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 | } |
| 1545 | } |
| 1546 | |
| 1547 | void SkBitmap::unflatten(SkFlattenableReadBuffer& buffer) { |
| 1548 | this->reset(); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1549 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1550 | int width = buffer.readInt(); |
| 1551 | int height = buffer.readInt(); |
| 1552 | int rowBytes = buffer.readInt(); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 1553 | int config = buffer.readInt(); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1554 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1555 | this->setConfig((Config)config, width, height, rowBytes); |
| 1556 | this->setIsOpaque(buffer.readBool()); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1557 | |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 1558 | int reftype = buffer.readInt(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1559 | switch (reftype) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1560 | case SERIALIZE_PIXELTYPE_REF_DATA: { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 1561 | size_t offset = buffer.readUInt(); |
| 1562 | SkPixelRef* pr = buffer.readFlattenableT<SkPixelRef>(); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 1563 | SkSafeUnref(this->setPixelRef(pr, offset)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1564 | break; |
| 1565 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1566 | case SERIALIZE_PIXELTYPE_NONE: |
| 1567 | break; |
| 1568 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 1569 | SkDEBUGFAIL("unrecognized pixeltype in serialized data"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1570 | sk_throw(); |
| 1571 | } |
| 1572 | } |
| 1573 | |
| 1574 | /////////////////////////////////////////////////////////////////////////////// |
| 1575 | |
| 1576 | SkBitmap::RLEPixels::RLEPixels(int width, int height) { |
| 1577 | fHeight = height; |
| 1578 | fYPtrs = (uint8_t**)sk_malloc_throw(height * sizeof(uint8_t*)); |
reed@android.com | 4516f47 | 2009-06-29 16:25:36 +0000 | [diff] [blame] | 1579 | sk_bzero(fYPtrs, height * sizeof(uint8_t*)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | SkBitmap::RLEPixels::~RLEPixels() { |
| 1583 | sk_free(fYPtrs); |
| 1584 | } |
| 1585 | |
| 1586 | /////////////////////////////////////////////////////////////////////////////// |
| 1587 | |
| 1588 | #ifdef SK_DEBUG |
| 1589 | void SkBitmap::validate() const { |
| 1590 | SkASSERT(fConfig < kConfigCount); |
| 1591 | SkASSERT(fRowBytes >= (unsigned)ComputeRowBytes((Config)fConfig, fWidth)); |
scroggo@google.com | 8e990eb | 2013-06-14 15:55:56 +0000 | [diff] [blame] | 1592 | uint8_t allFlags = kImageIsOpaque_Flag | kImageIsVolatile_Flag | kImageIsImmutable_Flag; |
| 1593 | #ifdef SK_BUILD_FOR_ANDROID |
| 1594 | allFlags |= kHasHardwareMipMap_Flag; |
| 1595 | #endif |
| 1596 | SkASSERT(fFlags <= allFlags); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1597 | SkASSERT(fPixelLockCount >= 0); |
| 1598 | SkASSERT(NULL == fColorTable || (unsigned)fColorTable->getRefCnt() < 10000); |
| 1599 | SkASSERT((uint8_t)ComputeBytesPerPixel((Config)fConfig) == fBytesPerPixel); |
| 1600 | |
| 1601 | #if 0 // these asserts are not thread-correct, so disable for now |
| 1602 | if (fPixelRef) { |
| 1603 | if (fPixelLockCount > 0) { |
reed@google.com | ff0da4f | 2012-05-17 13:14:52 +0000 | [diff] [blame] | 1604 | SkASSERT(fPixelRef->isLocked()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1605 | } else { |
| 1606 | SkASSERT(NULL == fPixels); |
| 1607 | SkASSERT(NULL == fColorTable); |
| 1608 | } |
| 1609 | } |
| 1610 | #endif |
| 1611 | } |
| 1612 | #endif |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 1613 | |
| 1614 | #ifdef SK_DEVELOPER |
| 1615 | void SkBitmap::toString(SkString* str) const { |
| 1616 | |
| 1617 | static const char* gConfigNames[kConfigCount] = { |
edisonn@google.com | afe397b | 2013-06-26 14:52:34 +0000 | [diff] [blame^] | 1618 | "NONE", "A1", "A8", "INDEX8", "565", "4444", "8888" |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 1619 | }; |
| 1620 | |
| 1621 | str->appendf("bitmap: ((%d, %d) %s", this->width(), this->height(), |
| 1622 | gConfigNames[this->config()]); |
| 1623 | |
| 1624 | str->append(" ("); |
| 1625 | if (this->isOpaque()) { |
| 1626 | str->append("opaque"); |
| 1627 | } else { |
| 1628 | str->append("transparent"); |
| 1629 | } |
| 1630 | if (this->isImmutable()) { |
| 1631 | str->append(", immutable"); |
| 1632 | } else { |
| 1633 | str->append(", not-immutable"); |
| 1634 | } |
| 1635 | str->append(")"); |
| 1636 | |
| 1637 | SkPixelRef* pr = this->pixelRef(); |
| 1638 | if (NULL == pr) { |
| 1639 | // show null or the explicit pixel address (rare) |
| 1640 | str->appendf(" pixels:%p", this->getPixels()); |
| 1641 | } else { |
| 1642 | const char* uri = pr->getURI(); |
| 1643 | if (NULL != uri) { |
| 1644 | str->appendf(" uri:\"%s\"", uri); |
| 1645 | } else { |
| 1646 | str->appendf(" pixelref:%p", pr); |
| 1647 | } |
| 1648 | } |
| 1649 | |
| 1650 | str->append(")"); |
| 1651 | } |
| 1652 | #endif |