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