epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2008 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "SkBitmap.h" |
| 11 | #include "SkColorPriv.h" |
| 12 | #include "SkDither.h" |
| 13 | #include "SkFlattenable.h" |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 14 | #include "SkImagePriv.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | #include "SkMallocPixelRef.h" |
| 16 | #include "SkMask.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 17 | #include "SkReadBuffer.h" |
| 18 | #include "SkWriteBuffer.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | #include "SkPixelRef.h" |
| 20 | #include "SkThread.h" |
vandebo@chromium.org | 112706d | 2011-02-24 22:50:55 +0000 | [diff] [blame] | 21 | #include "SkUnPreMultiply.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | #include "SkUtils.h" |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 23 | #include "SkValidationUtils.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 24 | #include "SkPackBits.h" |
| 25 | #include <new> |
| 26 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 27 | static bool reset_return_false(SkBitmap* bm) { |
| 28 | bm->reset(); |
| 29 | return false; |
| 30 | } |
| 31 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | SkBitmap::SkBitmap() { |
reed@android.com | 4516f47 | 2009-06-29 16:25:36 +0000 | [diff] [blame] | 33 | sk_bzero(this, sizeof(*this)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | SkBitmap::SkBitmap(const SkBitmap& src) { |
| 37 | SkDEBUGCODE(src.validate();) |
reed@android.com | 4516f47 | 2009-06-29 16:25:36 +0000 | [diff] [blame] | 38 | sk_bzero(this, sizeof(*this)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 39 | *this = src; |
| 40 | SkDEBUGCODE(this->validate();) |
| 41 | } |
| 42 | |
| 43 | SkBitmap::~SkBitmap() { |
| 44 | SkDEBUGCODE(this->validate();) |
| 45 | this->freePixels(); |
| 46 | } |
| 47 | |
| 48 | SkBitmap& SkBitmap::operator=(const SkBitmap& src) { |
| 49 | if (this != &src) { |
| 50 | this->freePixels(); |
| 51 | memcpy(this, &src, sizeof(src)); |
| 52 | |
| 53 | // inc src reference counts |
reed@android.com | 83f7bc3 | 2009-07-17 02:42:41 +0000 | [diff] [blame] | 54 | SkSafeRef(src.fPixelRef); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | |
| 56 | // we reset our locks if we get blown away |
| 57 | fPixelLockCount = 0; |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 58 | |
reed@google.com | 5f62ed7 | 2014-01-15 19:59:45 +0000 | [diff] [blame] | 59 | if (fPixelRef) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 60 | // ignore the values from the memcpy |
| 61 | fPixels = NULL; |
| 62 | fColorTable = NULL; |
bsalomon@google.com | 586f48c | 2011-04-14 15:07:22 +0000 | [diff] [blame] | 63 | // Note that what to for genID is somewhat arbitrary. We have no |
| 64 | // way to track changes to raw pixels across multiple SkBitmaps. |
| 65 | // Would benefit from an SkRawPixelRef type created by |
| 66 | // setPixels. |
| 67 | // Just leave the memcpy'ed one but they'll get out of sync |
| 68 | // as soon either is modified. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| 72 | SkDEBUGCODE(this->validate();) |
| 73 | return *this; |
| 74 | } |
| 75 | |
| 76 | void SkBitmap::swap(SkBitmap& other) { |
bsalomon@google.com | 586f48c | 2011-04-14 15:07:22 +0000 | [diff] [blame] | 77 | SkTSwap(fColorTable, other.fColorTable); |
| 78 | SkTSwap(fPixelRef, other.fPixelRef); |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 79 | SkTSwap(fPixelRefOrigin, other.fPixelRefOrigin); |
bsalomon@google.com | 586f48c | 2011-04-14 15:07:22 +0000 | [diff] [blame] | 80 | SkTSwap(fPixelLockCount, other.fPixelLockCount); |
bsalomon@google.com | 586f48c | 2011-04-14 15:07:22 +0000 | [diff] [blame] | 81 | SkTSwap(fPixels, other.fPixels); |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 82 | SkTSwap(fInfo, other.fInfo); |
bsalomon@google.com | 586f48c | 2011-04-14 15:07:22 +0000 | [diff] [blame] | 83 | SkTSwap(fRowBytes, other.fRowBytes); |
bsalomon@google.com | 586f48c | 2011-04-14 15:07:22 +0000 | [diff] [blame] | 84 | SkTSwap(fFlags, other.fFlags); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | |
| 86 | SkDEBUGCODE(this->validate();) |
| 87 | } |
| 88 | |
| 89 | void SkBitmap::reset() { |
| 90 | this->freePixels(); |
reed@android.com | 4516f47 | 2009-06-29 16:25:36 +0000 | [diff] [blame] | 91 | sk_bzero(this, sizeof(*this)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 92 | } |
| 93 | |
reed@google.com | 86b2e43 | 2012-03-15 21:17:03 +0000 | [diff] [blame] | 94 | void SkBitmap::getBounds(SkRect* bounds) const { |
| 95 | SkASSERT(bounds); |
| 96 | bounds->set(0, 0, |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 97 | SkIntToScalar(fInfo.width()), SkIntToScalar(fInfo.height())); |
reed@google.com | 86b2e43 | 2012-03-15 21:17:03 +0000 | [diff] [blame] | 98 | } |
| 99 | |
reed@google.com | 80e1459 | 2012-03-16 14:58:07 +0000 | [diff] [blame] | 100 | void SkBitmap::getBounds(SkIRect* bounds) const { |
| 101 | SkASSERT(bounds); |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 102 | bounds->set(0, 0, fInfo.width(), fInfo.height()); |
reed@google.com | 80e1459 | 2012-03-16 14:58:07 +0000 | [diff] [blame] | 103 | } |
| 104 | |
reed@google.com | 86b2e43 | 2012-03-15 21:17:03 +0000 | [diff] [blame] | 105 | /////////////////////////////////////////////////////////////////////////////// |
| 106 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 107 | bool SkBitmap::setInfo(const SkImageInfo& info, size_t rowBytes) { |
| 108 | SkAlphaType newAT = info.alphaType(); |
| 109 | if (!SkColorTypeValidateAlphaType(info.colorType(), info.alphaType(), &newAT)) { |
commit-bot@chromium.org | d5414e5 | 2014-02-13 22:30:38 +0000 | [diff] [blame] | 110 | return reset_return_false(this); |
| 111 | } |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 112 | // don't look at info.alphaType(), since newAT is the real value... |
skia.committer@gmail.com | 02d6f54 | 2014-02-14 03:02:05 +0000 | [diff] [blame] | 113 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 114 | // require that rowBytes fit in 31bits |
| 115 | int64_t mrb = info.minRowBytes64(); |
| 116 | if ((int32_t)mrb != mrb) { |
| 117 | return reset_return_false(this); |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 118 | } |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 119 | if ((int64_t)rowBytes != (int32_t)rowBytes) { |
| 120 | return reset_return_false(this); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | } |
reed@android.com | 89bb83a | 2009-05-29 21:30:42 +0000 | [diff] [blame] | 122 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 123 | if (info.width() < 0 || info.height() < 0) { |
| 124 | return reset_return_false(this); |
| 125 | } |
| 126 | |
| 127 | if (kUnknown_SkColorType == info.colorType()) { |
| 128 | rowBytes = 0; |
| 129 | } else if (0 == rowBytes) { |
| 130 | rowBytes = (size_t)mrb; |
reed | f0aed97 | 2014-07-01 12:48:11 -0700 | [diff] [blame] | 131 | } else if (!info.validRowBytes(rowBytes)) { |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 132 | return reset_return_false(this); |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | this->freePixels(); |
| 136 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 137 | fInfo = info.makeAlphaType(newAT); |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 138 | fRowBytes = SkToU32(rowBytes); |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 139 | return true; |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 140 | } |
| 141 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 142 | bool SkBitmap::setAlphaType(SkAlphaType newAlphaType) { |
| 143 | if (!SkColorTypeValidateAlphaType(fInfo.colorType(), newAlphaType, &newAlphaType)) { |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 144 | return false; |
| 145 | } |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 146 | if (fInfo.alphaType() != newAlphaType) { |
| 147 | fInfo = fInfo.makeAlphaType(newAlphaType); |
commit-bot@chromium.org | 0e8d0d6 | 2014-01-27 15:41:07 +0000 | [diff] [blame] | 148 | if (fPixelRef) { |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 149 | fPixelRef->changeAlphaType(newAlphaType); |
commit-bot@chromium.org | 0e8d0d6 | 2014-01-27 15:41:07 +0000 | [diff] [blame] | 150 | } |
| 151 | } |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 152 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void SkBitmap::updatePixelsFromRef() const { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 156 | if (fPixelRef) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 157 | if (fPixelLockCount > 0) { |
reed@google.com | ff0da4f | 2012-05-17 13:14:52 +0000 | [diff] [blame] | 158 | SkASSERT(fPixelRef->isLocked()); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 159 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 160 | void* p = fPixelRef->pixels(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 161 | if (p) { |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 162 | p = (char*)p |
reed@google.com | 303c475 | 2014-01-09 20:00:14 +0000 | [diff] [blame] | 163 | + fPixelRefOrigin.fY * fRowBytes |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 164 | + fPixelRefOrigin.fX * fInfo.bytesPerPixel(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 165 | } |
| 166 | fPixels = p; |
reed@google.com | 5f62ed7 | 2014-01-15 19:59:45 +0000 | [diff] [blame] | 167 | fColorTable = fPixelRef->colorTable(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 168 | } else { |
| 169 | SkASSERT(0 == fPixelLockCount); |
| 170 | fPixels = NULL; |
reed@google.com | 5f62ed7 | 2014-01-15 19:59:45 +0000 | [diff] [blame] | 171 | fColorTable = NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 176 | SkPixelRef* SkBitmap::setPixelRef(SkPixelRef* pr, int dx, int dy) { |
reed@google.com | dcea530 | 2014-01-03 13:43:01 +0000 | [diff] [blame] | 177 | #ifdef SK_DEBUG |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 178 | if (pr) { |
commit-bot@chromium.org | 466f5f3 | 2014-05-27 21:30:37 +0000 | [diff] [blame] | 179 | if (kUnknown_SkColorType != fInfo.colorType()) { |
reed@google.com | dcea530 | 2014-01-03 13:43:01 +0000 | [diff] [blame] | 180 | const SkImageInfo& prInfo = pr->info(); |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 181 | SkASSERT(fInfo.width() <= prInfo.width()); |
| 182 | SkASSERT(fInfo.height() <= prInfo.height()); |
| 183 | SkASSERT(fInfo.colorType() == prInfo.colorType()); |
| 184 | switch (prInfo.alphaType()) { |
reed | 4497748 | 2015-02-27 10:23:00 -0800 | [diff] [blame^] | 185 | case kUnknown_SkAlphaType: |
| 186 | SkASSERT(fInfo.alphaType() == kUnknown_SkAlphaType); |
reed@google.com | dcea530 | 2014-01-03 13:43:01 +0000 | [diff] [blame] | 187 | break; |
| 188 | case kOpaque_SkAlphaType: |
| 189 | case kPremul_SkAlphaType: |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 190 | SkASSERT(fInfo.alphaType() == kOpaque_SkAlphaType || |
| 191 | fInfo.alphaType() == kPremul_SkAlphaType); |
reed@google.com | dcea530 | 2014-01-03 13:43:01 +0000 | [diff] [blame] | 192 | break; |
| 193 | case kUnpremul_SkAlphaType: |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 194 | SkASSERT(fInfo.alphaType() == kOpaque_SkAlphaType || |
| 195 | fInfo.alphaType() == kUnpremul_SkAlphaType); |
reed@google.com | dcea530 | 2014-01-03 13:43:01 +0000 | [diff] [blame] | 196 | break; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 201 | |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 202 | if (pr) { |
| 203 | const SkImageInfo& info = pr->info(); |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 204 | fPixelRefOrigin.set(SkPin32(dx, 0, info.width()), SkPin32(dy, 0, info.height())); |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 205 | } else { |
| 206 | // ignore dx,dy if there is no pixelref |
| 207 | fPixelRefOrigin.setZero(); |
| 208 | } |
| 209 | |
| 210 | if (fPixelRef != pr) { |
piotaixr | 0eb02a6 | 2014-06-16 11:50:49 -0700 | [diff] [blame] | 211 | this->freePixels(); |
| 212 | SkASSERT(NULL == fPixelRef); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 213 | |
piotaixr | 0eb02a6 | 2014-06-16 11:50:49 -0700 | [diff] [blame] | 214 | SkSafeRef(pr); |
| 215 | fPixelRef = pr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 216 | this->updatePixelsFromRef(); |
| 217 | } |
| 218 | |
| 219 | SkDEBUGCODE(this->validate();) |
| 220 | return pr; |
| 221 | } |
| 222 | |
| 223 | void SkBitmap::lockPixels() const { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 224 | if (fPixelRef && 0 == sk_atomic_inc(&fPixelLockCount)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 225 | fPixelRef->lockPixels(); |
| 226 | this->updatePixelsFromRef(); |
| 227 | } |
| 228 | SkDEBUGCODE(this->validate();) |
| 229 | } |
| 230 | |
| 231 | void SkBitmap::unlockPixels() const { |
| 232 | SkASSERT(NULL == fPixelRef || fPixelLockCount > 0); |
| 233 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 234 | if (fPixelRef && 1 == sk_atomic_dec(&fPixelLockCount)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 235 | fPixelRef->unlockPixels(); |
| 236 | this->updatePixelsFromRef(); |
| 237 | } |
| 238 | SkDEBUGCODE(this->validate();) |
| 239 | } |
| 240 | |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 241 | bool SkBitmap::lockPixelsAreWritable() const { |
djsollen@google.com | c84b833 | 2012-07-27 13:41:44 +0000 | [diff] [blame] | 242 | return (fPixelRef) ? fPixelRef->lockPixelsAreWritable() : false; |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 243 | } |
| 244 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 245 | void SkBitmap::setPixels(void* p, SkColorTable* ctable) { |
reed@google.com | 8e1034e | 2012-07-30 13:16:35 +0000 | [diff] [blame] | 246 | if (NULL == p) { |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 247 | this->setPixelRef(NULL); |
reed@google.com | 8e1034e | 2012-07-30 13:16:35 +0000 | [diff] [blame] | 248 | return; |
| 249 | } |
| 250 | |
commit-bot@chromium.org | 466f5f3 | 2014-05-27 21:30:37 +0000 | [diff] [blame] | 251 | if (kUnknown_SkColorType == fInfo.colorType()) { |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 252 | this->setPixelRef(NULL); |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 253 | return; |
| 254 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 255 | |
commit-bot@chromium.org | 466f5f3 | 2014-05-27 21:30:37 +0000 | [diff] [blame] | 256 | SkPixelRef* pr = SkMallocPixelRef::NewDirect(fInfo, p, fRowBytes, ctable); |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 257 | if (NULL == pr) { |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 258 | this->setPixelRef(NULL); |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 259 | return; |
| 260 | } |
| 261 | |
| 262 | this->setPixelRef(pr)->unref(); |
| 263 | |
djsollen@google.com | c84b833 | 2012-07-27 13:41:44 +0000 | [diff] [blame] | 264 | // since we're already allocated, we lockPixels right away |
| 265 | this->lockPixels(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 266 | SkDEBUGCODE(this->validate();) |
| 267 | } |
| 268 | |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 269 | bool SkBitmap::tryAllocPixels(Allocator* allocator, SkColorTable* ctable) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 270 | HeapAllocator stdalloc; |
skia.committer@gmail.com | d2ac07b | 2014-01-25 07:01:49 +0000 | [diff] [blame] | 271 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 272 | if (NULL == allocator) { |
| 273 | allocator = &stdalloc; |
| 274 | } |
| 275 | return allocator->allocPixelRef(this, ctable); |
| 276 | } |
| 277 | |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 278 | /////////////////////////////////////////////////////////////////////////////// |
| 279 | |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 280 | bool SkBitmap::tryAllocPixels(const SkImageInfo& requestedInfo, size_t rowBytes) { |
reed | bae704b | 2014-06-28 14:26:35 -0700 | [diff] [blame] | 281 | if (kIndex_8_SkColorType == requestedInfo.colorType()) { |
| 282 | return reset_return_false(this); |
| 283 | } |
reed | f0aed97 | 2014-07-01 12:48:11 -0700 | [diff] [blame] | 284 | if (!this->setInfo(requestedInfo, rowBytes)) { |
reed | bae704b | 2014-06-28 14:26:35 -0700 | [diff] [blame] | 285 | return reset_return_false(this); |
| 286 | } |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 287 | |
reed | bae704b | 2014-06-28 14:26:35 -0700 | [diff] [blame] | 288 | // setInfo may have corrected info (e.g. 565 is always opaque). |
| 289 | const SkImageInfo& correctedInfo = this->info(); |
reed | f0aed97 | 2014-07-01 12:48:11 -0700 | [diff] [blame] | 290 | // setInfo may have computed a valid rowbytes if 0 were passed in |
| 291 | rowBytes = this->rowBytes(); |
| 292 | |
reed | bae704b | 2014-06-28 14:26:35 -0700 | [diff] [blame] | 293 | SkMallocPixelRef::PRFactory defaultFactory; |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 294 | |
reed | f0aed97 | 2014-07-01 12:48:11 -0700 | [diff] [blame] | 295 | SkPixelRef* pr = defaultFactory.create(correctedInfo, rowBytes, NULL); |
reed | bae704b | 2014-06-28 14:26:35 -0700 | [diff] [blame] | 296 | if (NULL == pr) { |
| 297 | return reset_return_false(this); |
| 298 | } |
| 299 | this->setPixelRef(pr)->unref(); |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 300 | |
reed | bae704b | 2014-06-28 14:26:35 -0700 | [diff] [blame] | 301 | // TODO: lockPixels could/should return bool or void*/NULL |
| 302 | this->lockPixels(); |
| 303 | if (NULL == this->getPixels()) { |
| 304 | return reset_return_false(this); |
| 305 | } |
| 306 | return true; |
| 307 | } |
| 308 | |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 309 | bool SkBitmap::tryAllocPixels(const SkImageInfo& requestedInfo, SkPixelRefFactory* factory, |
| 310 | SkColorTable* ctable) { |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 311 | if (kIndex_8_SkColorType == requestedInfo.colorType() && NULL == ctable) { |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 312 | return reset_return_false(this); |
| 313 | } |
scroggo | 0187dc2 | 2014-06-05 11:18:04 -0700 | [diff] [blame] | 314 | if (!this->setInfo(requestedInfo)) { |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 315 | return reset_return_false(this); |
| 316 | } |
| 317 | |
scroggo | 0187dc2 | 2014-06-05 11:18:04 -0700 | [diff] [blame] | 318 | // setInfo may have corrected info (e.g. 565 is always opaque). |
| 319 | const SkImageInfo& correctedInfo = this->info(); |
| 320 | |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 321 | SkMallocPixelRef::PRFactory defaultFactory; |
| 322 | if (NULL == factory) { |
| 323 | factory = &defaultFactory; |
| 324 | } |
skia.committer@gmail.com | d2ac07b | 2014-01-25 07:01:49 +0000 | [diff] [blame] | 325 | |
reed | f0aed97 | 2014-07-01 12:48:11 -0700 | [diff] [blame] | 326 | SkPixelRef* pr = factory->create(correctedInfo, correctedInfo.minRowBytes(), ctable); |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 327 | if (NULL == pr) { |
| 328 | return reset_return_false(this); |
| 329 | } |
| 330 | this->setPixelRef(pr)->unref(); |
| 331 | |
| 332 | // TODO: lockPixels could/should return bool or void*/NULL |
| 333 | this->lockPixels(); |
| 334 | if (NULL == this->getPixels()) { |
| 335 | return reset_return_false(this); |
| 336 | } |
| 337 | return true; |
| 338 | } |
| 339 | |
scroggo | 0187dc2 | 2014-06-05 11:18:04 -0700 | [diff] [blame] | 340 | bool SkBitmap::installPixels(const SkImageInfo& requestedInfo, void* pixels, size_t rb, |
| 341 | SkColorTable* ct, void (*releaseProc)(void* addr, void* context), |
| 342 | void* context) { |
| 343 | if (!this->setInfo(requestedInfo, rb)) { |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 344 | this->reset(); |
| 345 | return false; |
| 346 | } |
| 347 | |
scroggo | 0187dc2 | 2014-06-05 11:18:04 -0700 | [diff] [blame] | 348 | // setInfo may have corrected info (e.g. 565 is always opaque). |
| 349 | const SkImageInfo& correctedInfo = this->info(); |
| 350 | |
| 351 | SkPixelRef* pr = SkMallocPixelRef::NewWithProc(correctedInfo, rb, ct, pixels, releaseProc, |
| 352 | context); |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 353 | if (!pr) { |
| 354 | this->reset(); |
| 355 | return false; |
| 356 | } |
| 357 | |
| 358 | this->setPixelRef(pr)->unref(); |
mike@reedtribe.org | 6e58cf3 | 2014-02-16 20:54:21 +0000 | [diff] [blame] | 359 | |
| 360 | // since we're already allocated, we lockPixels right away |
| 361 | this->lockPixels(); |
mike@reedtribe.org | 6e58cf3 | 2014-02-16 20:54:21 +0000 | [diff] [blame] | 362 | SkDEBUGCODE(this->validate();) |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 363 | return true; |
| 364 | } |
| 365 | |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 366 | bool SkBitmap::installMaskPixels(const SkMask& mask) { |
| 367 | if (SkMask::kA8_Format != mask.fFormat) { |
| 368 | this->reset(); |
| 369 | return false; |
| 370 | } |
| 371 | return this->installPixels(SkImageInfo::MakeA8(mask.fBounds.width(), |
| 372 | mask.fBounds.height()), |
| 373 | mask.fImage, mask.fRowBytes); |
| 374 | } |
| 375 | |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 376 | /////////////////////////////////////////////////////////////////////////////// |
| 377 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 378 | void SkBitmap::freePixels() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 379 | if (fPixelRef) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 380 | if (fPixelLockCount > 0) { |
| 381 | fPixelRef->unlockPixels(); |
| 382 | } |
| 383 | fPixelRef->unref(); |
| 384 | fPixelRef = NULL; |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 385 | fPixelRefOrigin.setZero(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 386 | } |
| 387 | fPixelLockCount = 0; |
| 388 | fPixels = NULL; |
reed@google.com | 5f62ed7 | 2014-01-15 19:59:45 +0000 | [diff] [blame] | 389 | fColorTable = NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 390 | } |
| 391 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 392 | uint32_t SkBitmap::getGenerationID() const { |
djsollen@google.com | c84b833 | 2012-07-27 13:41:44 +0000 | [diff] [blame] | 393 | return (fPixelRef) ? fPixelRef->getGenerationID() : 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | void SkBitmap::notifyPixelsChanged() const { |
junov@chromium.org | b052129 | 2011-12-15 20:14:06 +0000 | [diff] [blame] | 397 | SkASSERT(!this->isImmutable()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 398 | if (fPixelRef) { |
| 399 | fPixelRef->notifyPixelsChanged(); |
| 400 | } |
| 401 | } |
| 402 | |
commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 403 | GrTexture* SkBitmap::getTexture() const { |
reed@android.com | ce4e53a | 2010-09-09 16:01:26 +0000 | [diff] [blame] | 404 | return fPixelRef ? fPixelRef->getTexture() : NULL; |
| 405 | } |
| 406 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 407 | /////////////////////////////////////////////////////////////////////////////// |
| 408 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 409 | /** We explicitly use the same allocator for our pixels that SkMask does, |
| 410 | so that we can freely assign memory allocated by one class to the other. |
| 411 | */ |
| 412 | bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst, |
| 413 | SkColorTable* ctable) { |
commit-bot@chromium.org | 466f5f3 | 2014-05-27 21:30:37 +0000 | [diff] [blame] | 414 | const SkImageInfo info = dst->info(); |
| 415 | if (kUnknown_SkColorType == info.colorType()) { |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 416 | // SkDebugf("unsupported config for info %d\n", dst->config()); |
| 417 | return false; |
| 418 | } |
skia.committer@gmail.com | 96f5fa0 | 2013-12-16 07:01:40 +0000 | [diff] [blame] | 419 | |
commit-bot@chromium.org | 466f5f3 | 2014-05-27 21:30:37 +0000 | [diff] [blame] | 420 | SkPixelRef* pr = SkMallocPixelRef::NewAllocate(info, dst->rowBytes(), ctable); |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 421 | if (NULL == pr) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 422 | return false; |
| 423 | } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 424 | |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 425 | dst->setPixelRef(pr)->unref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 426 | // since we're already allocated, we lockPixels right away |
| 427 | dst->lockPixels(); |
| 428 | return true; |
| 429 | } |
| 430 | |
| 431 | /////////////////////////////////////////////////////////////////////////////// |
| 432 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 433 | bool SkBitmap::copyPixelsTo(void* const dst, size_t dstSize, |
scroggo@google.com | 0ba4bf4 | 2013-02-25 16:02:36 +0000 | [diff] [blame] | 434 | size_t dstRowBytes, bool preserveDstPad) const { |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 435 | |
scroggo@google.com | 0ba4bf4 | 2013-02-25 16:02:36 +0000 | [diff] [blame] | 436 | if (0 == dstRowBytes) { |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 437 | dstRowBytes = fRowBytes; |
scroggo@google.com | 0ba4bf4 | 2013-02-25 16:02:36 +0000 | [diff] [blame] | 438 | } |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 439 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 440 | if (dstRowBytes < fInfo.minRowBytes() || |
| 441 | dst == NULL || (getPixels() == NULL && pixelRef() == NULL)) { |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 442 | return false; |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 443 | } |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 444 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 445 | if (!preserveDstPad && static_cast<uint32_t>(dstRowBytes) == fRowBytes) { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 446 | size_t safeSize = this->getSafeSize(); |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 447 | if (safeSize > dstSize || safeSize == 0) |
| 448 | return false; |
| 449 | else { |
| 450 | SkAutoLockPixels lock(*this); |
| 451 | // This implementation will write bytes beyond the end of each row, |
| 452 | // excluding the last row, if the bitmap's stride is greater than |
| 453 | // strictly required by the current config. |
| 454 | memcpy(dst, getPixels(), safeSize); |
| 455 | |
| 456 | return true; |
| 457 | } |
| 458 | } else { |
| 459 | // If destination has different stride than us, then copy line by line. |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 460 | if (fInfo.getSafeSize(dstRowBytes) > dstSize) { |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 461 | return false; |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 462 | } else { |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 463 | // Just copy what we need on each line. |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 464 | size_t rowBytes = fInfo.minRowBytes(); |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 465 | SkAutoLockPixels lock(*this); |
| 466 | const uint8_t* srcP = reinterpret_cast<const uint8_t*>(getPixels()); |
| 467 | uint8_t* dstP = reinterpret_cast<uint8_t*>(dst); |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 468 | for (int row = 0; row < fInfo.height(); row++, srcP += fRowBytes, dstP += dstRowBytes) { |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 469 | memcpy(dstP, srcP, rowBytes); |
| 470 | } |
| 471 | |
| 472 | return true; |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 477 | /////////////////////////////////////////////////////////////////////////////// |
| 478 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 479 | bool SkBitmap::isImmutable() const { |
scroggo | 0847059 | 2014-07-15 19:56:48 -0700 | [diff] [blame] | 480 | return fPixelRef ? fPixelRef->isImmutable() : false; |
junov@chromium.org | b052129 | 2011-12-15 20:14:06 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | void SkBitmap::setImmutable() { |
| 484 | if (fPixelRef) { |
| 485 | fPixelRef->setImmutable(); |
junov@chromium.org | b052129 | 2011-12-15 20:14:06 +0000 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 489 | bool SkBitmap::isVolatile() const { |
| 490 | return (fFlags & kImageIsVolatile_Flag) != 0; |
| 491 | } |
| 492 | |
| 493 | void SkBitmap::setIsVolatile(bool isVolatile) { |
| 494 | if (isVolatile) { |
| 495 | fFlags |= kImageIsVolatile_Flag; |
| 496 | } else { |
| 497 | fFlags &= ~kImageIsVolatile_Flag; |
| 498 | } |
| 499 | } |
| 500 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 501 | void* SkBitmap::getAddr(int x, int y) const { |
| 502 | SkASSERT((unsigned)x < (unsigned)this->width()); |
| 503 | SkASSERT((unsigned)y < (unsigned)this->height()); |
| 504 | |
| 505 | char* base = (char*)this->getPixels(); |
| 506 | if (base) { |
| 507 | base += y * this->rowBytes(); |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 508 | switch (this->colorType()) { |
| 509 | case kRGBA_8888_SkColorType: |
| 510 | case kBGRA_8888_SkColorType: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 511 | base += x << 2; |
| 512 | break; |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 513 | case kARGB_4444_SkColorType: |
| 514 | case kRGB_565_SkColorType: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 515 | base += x << 1; |
| 516 | break; |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 517 | case kAlpha_8_SkColorType: |
| 518 | case kIndex_8_SkColorType: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 519 | base += x; |
| 520 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 521 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 522 | SkDEBUGFAIL("Can't return addr for config"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 523 | base = NULL; |
| 524 | break; |
| 525 | } |
| 526 | } |
| 527 | return base; |
| 528 | } |
| 529 | |
vandebo@chromium.org | 112706d | 2011-02-24 22:50:55 +0000 | [diff] [blame] | 530 | SkColor SkBitmap::getColor(int x, int y) const { |
| 531 | SkASSERT((unsigned)x < (unsigned)this->width()); |
| 532 | SkASSERT((unsigned)y < (unsigned)this->height()); |
| 533 | |
commit-bot@chromium.org | cba7378 | 2014-05-29 15:57:47 +0000 | [diff] [blame] | 534 | switch (this->colorType()) { |
| 535 | case kAlpha_8_SkColorType: { |
reed@google.com | 3b521d0 | 2011-04-29 11:53:41 +0000 | [diff] [blame] | 536 | uint8_t* addr = this->getAddr8(x, y); |
vandebo@chromium.org | 112706d | 2011-02-24 22:50:55 +0000 | [diff] [blame] | 537 | return SkColorSetA(0, addr[0]); |
| 538 | } |
commit-bot@chromium.org | cba7378 | 2014-05-29 15:57:47 +0000 | [diff] [blame] | 539 | case kIndex_8_SkColorType: { |
reed@google.com | 3b521d0 | 2011-04-29 11:53:41 +0000 | [diff] [blame] | 540 | SkPMColor c = this->getIndex8Color(x, y); |
vandebo@chromium.org | 112706d | 2011-02-24 22:50:55 +0000 | [diff] [blame] | 541 | return SkUnPreMultiply::PMColorToColor(c); |
| 542 | } |
commit-bot@chromium.org | cba7378 | 2014-05-29 15:57:47 +0000 | [diff] [blame] | 543 | case kRGB_565_SkColorType: { |
reed@google.com | 3b521d0 | 2011-04-29 11:53:41 +0000 | [diff] [blame] | 544 | uint16_t* addr = this->getAddr16(x, y); |
vandebo@chromium.org | 112706d | 2011-02-24 22:50:55 +0000 | [diff] [blame] | 545 | return SkPixel16ToColor(addr[0]); |
| 546 | } |
commit-bot@chromium.org | cba7378 | 2014-05-29 15:57:47 +0000 | [diff] [blame] | 547 | case kARGB_4444_SkColorType: { |
reed@google.com | 3b521d0 | 2011-04-29 11:53:41 +0000 | [diff] [blame] | 548 | uint16_t* addr = this->getAddr16(x, y); |
vandebo@chromium.org | 112706d | 2011-02-24 22:50:55 +0000 | [diff] [blame] | 549 | SkPMColor c = SkPixel4444ToPixel32(addr[0]); |
| 550 | return SkUnPreMultiply::PMColorToColor(c); |
| 551 | } |
commit-bot@chromium.org | cba7378 | 2014-05-29 15:57:47 +0000 | [diff] [blame] | 552 | case kBGRA_8888_SkColorType: |
| 553 | case kRGBA_8888_SkColorType: { |
reed@google.com | 3b521d0 | 2011-04-29 11:53:41 +0000 | [diff] [blame] | 554 | uint32_t* addr = this->getAddr32(x, y); |
vandebo@chromium.org | 112706d | 2011-02-24 22:50:55 +0000 | [diff] [blame] | 555 | return SkUnPreMultiply::PMColorToColor(addr[0]); |
| 556 | } |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 557 | default: |
vandebo@chromium.org | 112706d | 2011-02-24 22:50:55 +0000 | [diff] [blame] | 558 | SkASSERT(false); |
| 559 | return 0; |
| 560 | } |
| 561 | SkASSERT(false); // Not reached. |
| 562 | return 0; |
| 563 | } |
| 564 | |
reed@google.com | 2a7579d | 2012-11-07 18:30:18 +0000 | [diff] [blame] | 565 | bool SkBitmap::ComputeIsOpaque(const SkBitmap& bm) { |
| 566 | SkAutoLockPixels alp(bm); |
| 567 | if (!bm.getPixels()) { |
| 568 | return false; |
| 569 | } |
| 570 | |
| 571 | const int height = bm.height(); |
| 572 | const int width = bm.width(); |
| 573 | |
commit-bot@chromium.org | cba7378 | 2014-05-29 15:57:47 +0000 | [diff] [blame] | 574 | switch (bm.colorType()) { |
| 575 | case kAlpha_8_SkColorType: { |
reed@google.com | 2a7579d | 2012-11-07 18:30:18 +0000 | [diff] [blame] | 576 | unsigned a = 0xFF; |
| 577 | for (int y = 0; y < height; ++y) { |
| 578 | const uint8_t* row = bm.getAddr8(0, y); |
| 579 | for (int x = 0; x < width; ++x) { |
| 580 | a &= row[x]; |
| 581 | } |
| 582 | if (0xFF != a) { |
| 583 | return false; |
| 584 | } |
| 585 | } |
| 586 | return true; |
| 587 | } break; |
commit-bot@chromium.org | cba7378 | 2014-05-29 15:57:47 +0000 | [diff] [blame] | 588 | case kIndex_8_SkColorType: { |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 589 | if (!bm.getColorTable()) { |
reed@google.com | 2a7579d | 2012-11-07 18:30:18 +0000 | [diff] [blame] | 590 | return false; |
| 591 | } |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 592 | const SkPMColor* table = bm.getColorTable()->readColors(); |
reed@google.com | 140d728 | 2013-01-07 20:25:04 +0000 | [diff] [blame] | 593 | SkPMColor c = (SkPMColor)~0; |
reed@google.com | 2a7579d | 2012-11-07 18:30:18 +0000 | [diff] [blame] | 594 | for (int i = bm.getColorTable()->count() - 1; i >= 0; --i) { |
| 595 | c &= table[i]; |
| 596 | } |
| 597 | return 0xFF == SkGetPackedA32(c); |
| 598 | } break; |
commit-bot@chromium.org | cba7378 | 2014-05-29 15:57:47 +0000 | [diff] [blame] | 599 | case kRGB_565_SkColorType: |
reed@google.com | 2a7579d | 2012-11-07 18:30:18 +0000 | [diff] [blame] | 600 | return true; |
| 601 | break; |
commit-bot@chromium.org | cba7378 | 2014-05-29 15:57:47 +0000 | [diff] [blame] | 602 | case kARGB_4444_SkColorType: { |
reed@google.com | 2a7579d | 2012-11-07 18:30:18 +0000 | [diff] [blame] | 603 | unsigned c = 0xFFFF; |
| 604 | for (int y = 0; y < height; ++y) { |
| 605 | const SkPMColor16* row = bm.getAddr16(0, y); |
| 606 | for (int x = 0; x < width; ++x) { |
| 607 | c &= row[x]; |
| 608 | } |
| 609 | if (0xF != SkGetPackedA4444(c)) { |
| 610 | return false; |
| 611 | } |
| 612 | } |
| 613 | return true; |
| 614 | } break; |
commit-bot@chromium.org | cba7378 | 2014-05-29 15:57:47 +0000 | [diff] [blame] | 615 | case kBGRA_8888_SkColorType: |
| 616 | case kRGBA_8888_SkColorType: { |
reed@google.com | 140d728 | 2013-01-07 20:25:04 +0000 | [diff] [blame] | 617 | SkPMColor c = (SkPMColor)~0; |
reed@google.com | 2a7579d | 2012-11-07 18:30:18 +0000 | [diff] [blame] | 618 | for (int y = 0; y < height; ++y) { |
| 619 | const SkPMColor* row = bm.getAddr32(0, y); |
| 620 | for (int x = 0; x < width; ++x) { |
| 621 | c &= row[x]; |
| 622 | } |
| 623 | if (0xFF != SkGetPackedA32(c)) { |
| 624 | return false; |
| 625 | } |
| 626 | } |
| 627 | return true; |
| 628 | } |
| 629 | default: |
| 630 | break; |
| 631 | } |
| 632 | return false; |
| 633 | } |
| 634 | |
| 635 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 636 | /////////////////////////////////////////////////////////////////////////////// |
| 637 | /////////////////////////////////////////////////////////////////////////////// |
| 638 | |
reed@google.com | 45f746f | 2013-06-21 19:51:31 +0000 | [diff] [blame] | 639 | static uint16_t pack_8888_to_4444(unsigned a, unsigned r, unsigned g, unsigned b) { |
| 640 | unsigned pixel = (SkA32To4444(a) << SK_A4444_SHIFT) | |
| 641 | (SkR32To4444(r) << SK_R4444_SHIFT) | |
| 642 | (SkG32To4444(g) << SK_G4444_SHIFT) | |
| 643 | (SkB32To4444(b) << SK_B4444_SHIFT); |
| 644 | return SkToU16(pixel); |
| 645 | } |
| 646 | |
reed@google.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 647 | void SkBitmap::internalErase(const SkIRect& area, |
| 648 | U8CPU a, U8CPU r, U8CPU g, U8CPU b) const { |
| 649 | #ifdef SK_DEBUG |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 650 | SkDEBUGCODE(this->validate();) |
reed@google.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 651 | SkASSERT(!area.isEmpty()); |
| 652 | { |
reed@google.com | 92833f9 | 2013-06-28 19:47:43 +0000 | [diff] [blame] | 653 | SkIRect total = { 0, 0, this->width(), this->height() }; |
reed@google.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 654 | SkASSERT(total.contains(area)); |
| 655 | } |
| 656 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 657 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 658 | switch (fInfo.colorType()) { |
| 659 | case kUnknown_SkColorType: |
| 660 | case kIndex_8_SkColorType: |
commit-bot@chromium.org | 7669a77 | 2014-03-27 15:30:35 +0000 | [diff] [blame] | 661 | return; // can't erase. Should we bzero so the memory is not uninitialized? |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 662 | default: |
| 663 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | SkAutoLockPixels alp(*this); |
| 667 | // perform this check after the lock call |
| 668 | if (!this->readyToDraw()) { |
| 669 | return; |
| 670 | } |
| 671 | |
reed@google.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 672 | int height = area.height(); |
| 673 | const int width = area.width(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 674 | const int rowBytes = fRowBytes; |
| 675 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 676 | switch (this->colorType()) { |
| 677 | case kAlpha_8_SkColorType: { |
reed@google.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 678 | uint8_t* p = this->getAddr8(area.fLeft, area.fTop); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 679 | while (--height >= 0) { |
| 680 | memset(p, a, width); |
| 681 | p += rowBytes; |
| 682 | } |
| 683 | break; |
| 684 | } |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 685 | case kARGB_4444_SkColorType: |
| 686 | case kRGB_565_SkColorType: { |
reed@google.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 687 | uint16_t* p = this->getAddr16(area.fLeft, area.fTop);; |
reed@google.com | 45f746f | 2013-06-21 19:51:31 +0000 | [diff] [blame] | 688 | uint16_t v; |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 689 | |
commit-bot@chromium.org | 7669a77 | 2014-03-27 15:30:35 +0000 | [diff] [blame] | 690 | // make rgb premultiplied |
| 691 | if (255 != a) { |
| 692 | r = SkAlphaMul(r, a); |
| 693 | g = SkAlphaMul(g, a); |
| 694 | b = SkAlphaMul(b, a); |
| 695 | } |
| 696 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 697 | if (kARGB_4444_SkColorType == this->colorType()) { |
reed@google.com | 45f746f | 2013-06-21 19:51:31 +0000 | [diff] [blame] | 698 | v = pack_8888_to_4444(a, r, g, b); |
| 699 | } else { |
| 700 | v = SkPackRGB16(r >> (8 - SK_R16_BITS), |
| 701 | g >> (8 - SK_G16_BITS), |
| 702 | b >> (8 - SK_B16_BITS)); |
| 703 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 704 | while (--height >= 0) { |
| 705 | sk_memset16(p, v, width); |
| 706 | p = (uint16_t*)((char*)p + rowBytes); |
| 707 | } |
| 708 | break; |
| 709 | } |
commit-bot@chromium.org | 7669a77 | 2014-03-27 15:30:35 +0000 | [diff] [blame] | 710 | case kBGRA_8888_SkColorType: |
| 711 | case kRGBA_8888_SkColorType: { |
reed@google.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 712 | uint32_t* p = this->getAddr32(area.fLeft, area.fTop); |
commit-bot@chromium.org | 7669a77 | 2014-03-27 15:30:35 +0000 | [diff] [blame] | 713 | |
| 714 | if (255 != a && kPremul_SkAlphaType == this->alphaType()) { |
| 715 | r = SkAlphaMul(r, a); |
| 716 | g = SkAlphaMul(g, a); |
| 717 | b = SkAlphaMul(b, a); |
| 718 | } |
| 719 | uint32_t v = kRGBA_8888_SkColorType == this->colorType() ? |
| 720 | SkPackARGB_as_RGBA(a, r, g, b) : SkPackARGB_as_BGRA(a, r, g, b); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 721 | |
| 722 | while (--height >= 0) { |
| 723 | sk_memset32(p, v, width); |
| 724 | p = (uint32_t*)((char*)p + rowBytes); |
| 725 | } |
| 726 | break; |
| 727 | } |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 728 | default: |
| 729 | return; // no change, so don't call notifyPixelsChanged() |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 730 | } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 731 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 732 | this->notifyPixelsChanged(); |
| 733 | } |
| 734 | |
reed@google.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 735 | void SkBitmap::eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const { |
reed@google.com | 92833f9 | 2013-06-28 19:47:43 +0000 | [diff] [blame] | 736 | SkIRect area = { 0, 0, this->width(), this->height() }; |
reed@google.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 737 | if (!area.isEmpty()) { |
| 738 | this->internalErase(area, a, r, g, b); |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | void SkBitmap::eraseArea(const SkIRect& rect, SkColor c) const { |
reed@google.com | 92833f9 | 2013-06-28 19:47:43 +0000 | [diff] [blame] | 743 | SkIRect area = { 0, 0, this->width(), this->height() }; |
reed@google.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 744 | if (area.intersect(rect)) { |
| 745 | this->internalErase(area, SkColorGetA(c), SkColorGetR(c), |
| 746 | SkColorGetG(c), SkColorGetB(c)); |
| 747 | } |
| 748 | } |
| 749 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 750 | ////////////////////////////////////////////////////////////////////////////////////// |
| 751 | ////////////////////////////////////////////////////////////////////////////////////// |
| 752 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 753 | bool SkBitmap::extractSubset(SkBitmap* result, const SkIRect& subset) const { |
| 754 | SkDEBUGCODE(this->validate();) |
| 755 | |
djsollen@google.com | c84b833 | 2012-07-27 13:41:44 +0000 | [diff] [blame] | 756 | if (NULL == result || NULL == fPixelRef) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 757 | return false; // no src pixels |
| 758 | } |
| 759 | |
| 760 | SkIRect srcRect, r; |
| 761 | srcRect.set(0, 0, this->width(), this->height()); |
| 762 | if (!r.intersect(srcRect, subset)) { |
| 763 | return false; // r is empty (i.e. no intersection) |
| 764 | } |
| 765 | |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 766 | if (fPixelRef->getTexture() != NULL) { |
| 767 | // Do a deep copy |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 768 | SkPixelRef* pixelRef = fPixelRef->deepCopy(this->colorType(), this->profileType(), &subset); |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 769 | if (pixelRef != NULL) { |
| 770 | SkBitmap dst; |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 771 | dst.setInfo(SkImageInfo::Make(subset.width(), subset.height(), |
| 772 | this->colorType(), this->alphaType())); |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 773 | dst.setIsVolatile(this->isVolatile()); |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 774 | dst.setPixelRef(pixelRef)->unref(); |
| 775 | SkDEBUGCODE(dst.validate()); |
| 776 | result->swap(dst); |
| 777 | return true; |
| 778 | } |
| 779 | } |
| 780 | |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 781 | // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have |
| 782 | // exited above. |
| 783 | SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width())); |
| 784 | SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height())); |
| 785 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 786 | SkBitmap dst; |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 787 | dst.setInfo(SkImageInfo::Make(r.width(), r.height(), this->colorType(), this->alphaType()), |
| 788 | this->rowBytes()); |
skyostil@google.com | 0eb7576 | 2012-01-16 10:45:53 +0000 | [diff] [blame] | 789 | dst.setIsVolatile(this->isVolatile()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 790 | |
| 791 | if (fPixelRef) { |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 792 | SkIPoint origin = fPixelRefOrigin; |
| 793 | origin.fX += r.fLeft; |
| 794 | origin.fY += r.fTop; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 795 | // share the pixelref with a custom offset |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 796 | dst.setPixelRef(fPixelRef, origin); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 797 | } |
| 798 | SkDEBUGCODE(dst.validate();) |
| 799 | |
| 800 | // we know we're good, so commit to result |
| 801 | result->swap(dst); |
| 802 | return true; |
| 803 | } |
| 804 | |
| 805 | /////////////////////////////////////////////////////////////////////////////// |
| 806 | |
| 807 | #include "SkCanvas.h" |
| 808 | #include "SkPaint.h" |
| 809 | |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 810 | bool SkBitmap::canCopyTo(SkColorType dstColorType) const { |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 811 | const SkColorType srcCT = this->colorType(); |
| 812 | |
| 813 | if (srcCT == kUnknown_SkColorType) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 814 | return false; |
| 815 | } |
| 816 | |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 817 | bool sameConfigs = (srcCT == dstColorType); |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 818 | switch (dstColorType) { |
| 819 | case kAlpha_8_SkColorType: |
| 820 | case kRGB_565_SkColorType: |
commit-bot@chromium.org | 60b5dce | 2014-04-22 20:24:33 +0000 | [diff] [blame] | 821 | case kRGBA_8888_SkColorType: |
| 822 | case kBGRA_8888_SkColorType: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 823 | break; |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 824 | case kIndex_8_SkColorType: |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 825 | if (!sameConfigs) { |
| 826 | return false; |
| 827 | } |
| 828 | break; |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 829 | case kARGB_4444_SkColorType: |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 830 | return sameConfigs || kN32_SkColorType == srcCT || kIndex_8_SkColorType == srcCT; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 831 | default: |
| 832 | return false; |
| 833 | } |
reed@android.com | fbaa88d | 2009-05-06 17:44:34 +0000 | [diff] [blame] | 834 | return true; |
| 835 | } |
| 836 | |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 837 | #include "SkConfig8888.h" |
| 838 | |
| 839 | bool SkBitmap::readPixels(const SkImageInfo& requestedDstInfo, void* dstPixels, size_t dstRB, |
| 840 | int x, int y) const { |
| 841 | if (kUnknown_SkColorType == requestedDstInfo.colorType()) { |
| 842 | return false; |
| 843 | } |
| 844 | if (NULL == dstPixels || dstRB < requestedDstInfo.minRowBytes()) { |
| 845 | return false; |
| 846 | } |
| 847 | if (0 == requestedDstInfo.width() || 0 == requestedDstInfo.height()) { |
| 848 | return false; |
| 849 | } |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 850 | |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 851 | SkIRect srcR = SkIRect::MakeXYWH(x, y, requestedDstInfo.width(), requestedDstInfo.height()); |
| 852 | if (!srcR.intersect(0, 0, this->width(), this->height())) { |
| 853 | return false; |
| 854 | } |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 855 | |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 856 | // the intersect may have shrunk info's logical size |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 857 | const SkImageInfo dstInfo = requestedDstInfo.makeWH(srcR.width(), srcR.height()); |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 858 | |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 859 | // if x or y are negative, then we have to adjust pixels |
| 860 | if (x > 0) { |
| 861 | x = 0; |
| 862 | } |
| 863 | if (y > 0) { |
| 864 | y = 0; |
| 865 | } |
| 866 | // here x,y are either 0 or negative |
| 867 | dstPixels = ((char*)dstPixels - y * dstRB - x * dstInfo.bytesPerPixel()); |
| 868 | |
| 869 | ////////////// |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 870 | |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 871 | SkAutoLockPixels alp(*this); |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 872 | |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 873 | // since we don't stop creating un-pixeled devices yet, check for no pixels here |
| 874 | if (NULL == this->getPixels()) { |
| 875 | return false; |
| 876 | } |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 877 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 878 | const SkImageInfo srcInfo = this->info().makeWH(dstInfo.width(), dstInfo.height()); |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 879 | |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 880 | const void* srcPixels = this->getAddr(srcR.x(), srcR.y()); |
| 881 | return SkPixelInfo::CopyPixels(dstInfo, dstPixels, dstRB, srcInfo, srcPixels, this->rowBytes(), |
| 882 | this->getColorTable()); |
| 883 | } |
| 884 | |
| 885 | bool SkBitmap::copyTo(SkBitmap* dst, SkColorType dstColorType, Allocator* alloc) const { |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 886 | if (!this->canCopyTo(dstColorType)) { |
reed@android.com | fbaa88d | 2009-05-06 17:44:34 +0000 | [diff] [blame] | 887 | return false; |
| 888 | } |
| 889 | |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 890 | // if we have a texture, first get those pixels |
| 891 | SkBitmap tmpSrc; |
| 892 | const SkBitmap* src = this; |
| 893 | |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 894 | if (fPixelRef) { |
| 895 | SkIRect subset; |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 896 | subset.setXYWH(fPixelRefOrigin.fX, fPixelRefOrigin.fY, |
| 897 | fInfo.width(), fInfo.height()); |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 898 | if (fPixelRef->readPixels(&tmpSrc, &subset)) { |
scroggo | 0187dc2 | 2014-06-05 11:18:04 -0700 | [diff] [blame] | 899 | if (fPixelRef->info().alphaType() == kUnpremul_SkAlphaType) { |
| 900 | // FIXME: The only meaningful implementation of readPixels |
| 901 | // (GrPixelRef) assumes premultiplied pixels. |
| 902 | return false; |
| 903 | } |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 904 | SkASSERT(tmpSrc.width() == this->width()); |
| 905 | SkASSERT(tmpSrc.height() == this->height()); |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 906 | |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 907 | // did we get lucky and we can just return tmpSrc? |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 908 | if (tmpSrc.colorType() == dstColorType && NULL == alloc) { |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 909 | dst->swap(tmpSrc); |
scroggo@google.com | 5ccae2c | 2014-01-15 16:56:52 +0000 | [diff] [blame] | 910 | // If the result is an exact copy, clone the gen ID. |
| 911 | if (dst->pixelRef() && dst->pixelRef()->info() == fPixelRef->info()) { |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 912 | dst->pixelRef()->cloneGenID(*fPixelRef); |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 913 | } |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 914 | return true; |
scroggo@google.com | d5764e8 | 2012-08-22 15:00:05 +0000 | [diff] [blame] | 915 | } |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 916 | |
| 917 | // fall through to the raster case |
| 918 | src = &tmpSrc; |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 919 | } |
reed@android.com | fbaa88d | 2009-05-06 17:44:34 +0000 | [diff] [blame] | 920 | } |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 921 | |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 922 | // we lock this now, since we may need its colortable |
| 923 | SkAutoLockPixels srclock(*src); |
| 924 | if (!src->readyToDraw()) { |
| 925 | return false; |
| 926 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 927 | |
scroggo@google.com | 5ccae2c | 2014-01-15 16:56:52 +0000 | [diff] [blame] | 928 | // The only way to be readyToDraw is if fPixelRef is non NULL. |
| 929 | SkASSERT(fPixelRef != NULL); |
| 930 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 931 | const SkImageInfo dstInfo = src->info().makeColorType(dstColorType); |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 932 | |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 933 | SkBitmap tmpDst; |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 934 | if (!tmpDst.setInfo(dstInfo)) { |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 935 | return false; |
| 936 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 937 | |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 938 | // allocate colortable if srcConfig == kIndex8_Config |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 939 | SkAutoTUnref<SkColorTable> ctable; |
| 940 | if (dstColorType == kIndex_8_SkColorType) { |
mtklein | cc969c4 | 2014-12-02 10:02:47 -0800 | [diff] [blame] | 941 | ctable.reset(SkRef(src->getColorTable())); |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 942 | } |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 943 | if (!tmpDst.tryAllocPixels(alloc, ctable)) { |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 944 | return false; |
| 945 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 946 | |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 947 | if (!tmpDst.readyToDraw()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 948 | // allocator/lock failed |
| 949 | return false; |
| 950 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 951 | |
scroggo@google.com | 5ccae2c | 2014-01-15 16:56:52 +0000 | [diff] [blame] | 952 | // pixelRef must be non NULL or tmpDst.readyToDraw() would have |
| 953 | // returned false. |
| 954 | SkASSERT(tmpDst.pixelRef() != NULL); |
| 955 | |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 956 | if (!src->readPixels(tmpDst.info(), tmpDst.getPixels(), tmpDst.rowBytes(), 0, 0)) { |
| 957 | return false; |
| 958 | } |
scroggo@google.com | 5ccae2c | 2014-01-15 16:56:52 +0000 | [diff] [blame] | 959 | |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 960 | // (for BitmapHeap) Clone the pixelref genID even though we have a new pixelref. |
| 961 | // The old copyTo impl did this, so we continue it for now. |
| 962 | // |
| 963 | // TODO: should we ignore rowbytes (i.e. getSize)? Then it could just be |
| 964 | // if (src_pixelref->info == dst_pixelref->info) |
| 965 | // |
| 966 | if (src->colorType() == dstColorType && tmpDst.getSize() == src->getSize()) { |
| 967 | SkPixelRef* dstPixelRef = tmpDst.pixelRef(); |
| 968 | if (dstPixelRef->info() == fPixelRef->info()) { |
| 969 | dstPixelRef->cloneGenID(*fPixelRef); |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 970 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 971 | } |
| 972 | |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 973 | dst->swap(tmpDst); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 974 | return true; |
| 975 | } |
| 976 | |
commit-bot@chromium.org | fab349c | 2014-03-05 02:34:58 +0000 | [diff] [blame] | 977 | bool SkBitmap::deepCopyTo(SkBitmap* dst) const { |
reed | e4538f5 | 2014-06-11 06:09:50 -0700 | [diff] [blame] | 978 | const SkColorType dstCT = this->colorType(); |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 979 | const SkColorProfileType dstPT = this->profileType(); |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 980 | |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 981 | if (!this->canCopyTo(dstCT)) { |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 982 | return false; |
| 983 | } |
| 984 | |
| 985 | // If we have a PixelRef, and it supports deep copy, use it. |
| 986 | // Currently supported only by texture-backed bitmaps. |
| 987 | if (fPixelRef) { |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 988 | SkPixelRef* pixelRef = fPixelRef->deepCopy(dstCT, dstPT, NULL); |
commit-bot@chromium.org | eeef0cc | 2014-05-21 15:58:00 +0000 | [diff] [blame] | 989 | if (pixelRef) { |
| 990 | uint32_t rowBytes; |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 991 | if (this->colorType() == dstCT && this->profileType() == dstPT) { |
commit-bot@chromium.org | eeef0cc | 2014-05-21 15:58:00 +0000 | [diff] [blame] | 992 | // Since there is no subset to pass to deepCopy, and deepCopy |
| 993 | // succeeded, the new pixel ref must be identical. |
| 994 | SkASSERT(fPixelRef->info() == pixelRef->info()); |
| 995 | pixelRef->cloneGenID(*fPixelRef); |
| 996 | // Use the same rowBytes as the original. |
| 997 | rowBytes = fRowBytes; |
| 998 | } else { |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 999 | // With the new config, an appropriate fRowBytes will be computed by setInfo. |
commit-bot@chromium.org | eeef0cc | 2014-05-21 15:58:00 +0000 | [diff] [blame] | 1000 | rowBytes = 0; |
| 1001 | } |
| 1002 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 1003 | const SkImageInfo info = fInfo.makeColorType(dstCT); |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 1004 | if (!dst->setInfo(info, rowBytes)) { |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 1005 | return false; |
| 1006 | } |
commit-bot@chromium.org | eeef0cc | 2014-05-21 15:58:00 +0000 | [diff] [blame] | 1007 | dst->setPixelRef(pixelRef, fPixelRefOrigin)->unref(); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 1008 | return true; |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | if (this->getTexture()) { |
| 1013 | return false; |
| 1014 | } else { |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 1015 | return this->copyTo(dst, dstCT, NULL); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 1016 | } |
| 1017 | } |
| 1018 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1019 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1020 | |
tomhudson@google.com | a87cd2a | 2011-06-15 16:50:27 +0000 | [diff] [blame] | 1021 | static bool GetBitmapAlpha(const SkBitmap& src, uint8_t* SK_RESTRICT alpha, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1022 | int alphaRowBytes) { |
| 1023 | SkASSERT(alpha != NULL); |
| 1024 | SkASSERT(alphaRowBytes >= src.width()); |
| 1025 | |
commit-bot@chromium.org | cba7378 | 2014-05-29 15:57:47 +0000 | [diff] [blame] | 1026 | SkColorType colorType = src.colorType(); |
| 1027 | int w = src.width(); |
| 1028 | int h = src.height(); |
| 1029 | size_t rb = src.rowBytes(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1030 | |
reed@android.com | 1cdcb51 | 2009-08-24 19:11:00 +0000 | [diff] [blame] | 1031 | SkAutoLockPixels alp(src); |
| 1032 | if (!src.readyToDraw()) { |
| 1033 | // zero out the alpha buffer and return |
| 1034 | while (--h >= 0) { |
| 1035 | memset(alpha, 0, w); |
| 1036 | alpha += alphaRowBytes; |
| 1037 | } |
| 1038 | return false; |
| 1039 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 1040 | |
commit-bot@chromium.org | cba7378 | 2014-05-29 15:57:47 +0000 | [diff] [blame] | 1041 | if (kAlpha_8_SkColorType == colorType && !src.isOpaque()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1042 | const uint8_t* s = src.getAddr8(0, 0); |
| 1043 | while (--h >= 0) { |
| 1044 | memcpy(alpha, s, w); |
| 1045 | s += rb; |
| 1046 | alpha += alphaRowBytes; |
| 1047 | } |
commit-bot@chromium.org | cba7378 | 2014-05-29 15:57:47 +0000 | [diff] [blame] | 1048 | } else if (kN32_SkColorType == colorType && !src.isOpaque()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1049 | const SkPMColor* SK_RESTRICT s = src.getAddr32(0, 0); |
| 1050 | while (--h >= 0) { |
| 1051 | for (int x = 0; x < w; x++) { |
| 1052 | alpha[x] = SkGetPackedA32(s[x]); |
| 1053 | } |
| 1054 | s = (const SkPMColor*)((const char*)s + rb); |
| 1055 | alpha += alphaRowBytes; |
| 1056 | } |
commit-bot@chromium.org | cba7378 | 2014-05-29 15:57:47 +0000 | [diff] [blame] | 1057 | } else if (kARGB_4444_SkColorType == colorType && !src.isOpaque()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1058 | const SkPMColor16* SK_RESTRICT s = src.getAddr16(0, 0); |
| 1059 | while (--h >= 0) { |
| 1060 | for (int x = 0; x < w; x++) { |
| 1061 | alpha[x] = SkPacked4444ToA32(s[x]); |
| 1062 | } |
| 1063 | s = (const SkPMColor16*)((const char*)s + rb); |
| 1064 | alpha += alphaRowBytes; |
| 1065 | } |
commit-bot@chromium.org | cba7378 | 2014-05-29 15:57:47 +0000 | [diff] [blame] | 1066 | } else if (kIndex_8_SkColorType == colorType && !src.isOpaque()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1067 | SkColorTable* ct = src.getColorTable(); |
| 1068 | if (ct) { |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 1069 | const SkPMColor* SK_RESTRICT table = ct->readColors(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1070 | const uint8_t* SK_RESTRICT s = src.getAddr8(0, 0); |
| 1071 | while (--h >= 0) { |
| 1072 | for (int x = 0; x < w; x++) { |
| 1073 | alpha[x] = SkGetPackedA32(table[s[x]]); |
| 1074 | } |
| 1075 | s += rb; |
| 1076 | alpha += alphaRowBytes; |
| 1077 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1078 | } |
| 1079 | } else { // src is opaque, so just fill alpha[] with 0xFF |
| 1080 | memset(alpha, 0xFF, h * alphaRowBytes); |
| 1081 | } |
reed@android.com | 1cdcb51 | 2009-08-24 19:11:00 +0000 | [diff] [blame] | 1082 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | #include "SkPaint.h" |
| 1086 | #include "SkMaskFilter.h" |
| 1087 | #include "SkMatrix.h" |
| 1088 | |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 1089 | bool SkBitmap::extractAlpha(SkBitmap* dst, const SkPaint* paint, |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 1090 | Allocator *allocator, SkIPoint* offset) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1091 | SkDEBUGCODE(this->validate();) |
| 1092 | |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 1093 | SkBitmap tmpBitmap; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1094 | SkMatrix identity; |
| 1095 | SkMask srcM, dstM; |
| 1096 | |
| 1097 | srcM.fBounds.set(0, 0, this->width(), this->height()); |
| 1098 | srcM.fRowBytes = SkAlign4(this->width()); |
| 1099 | srcM.fFormat = SkMask::kA8_Format; |
| 1100 | |
| 1101 | SkMaskFilter* filter = paint ? paint->getMaskFilter() : NULL; |
| 1102 | |
| 1103 | // compute our (larger?) dst bounds if we have a filter |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1104 | if (filter) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1105 | identity.reset(); |
| 1106 | srcM.fImage = NULL; |
| 1107 | if (!filter->filterMask(&dstM, srcM, identity, NULL)) { |
| 1108 | goto NO_FILTER_CASE; |
| 1109 | } |
| 1110 | dstM.fRowBytes = SkAlign4(dstM.fBounds.width()); |
| 1111 | } else { |
| 1112 | NO_FILTER_CASE: |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 1113 | tmpBitmap.setInfo(SkImageInfo::MakeA8(this->width(), this->height()), srcM.fRowBytes); |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 1114 | if (!tmpBitmap.tryAllocPixels(allocator, NULL)) { |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 1115 | // Allocation of pixels for alpha bitmap failed. |
| 1116 | SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n", |
| 1117 | tmpBitmap.width(), tmpBitmap.height()); |
| 1118 | return false; |
| 1119 | } |
| 1120 | GetBitmapAlpha(*this, tmpBitmap.getAddr8(0, 0), srcM.fRowBytes); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1121 | if (offset) { |
| 1122 | offset->set(0, 0); |
| 1123 | } |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 1124 | tmpBitmap.swap(*dst); |
| 1125 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1126 | } |
bungeman@google.com | 02f5584 | 2011-10-04 21:25:00 +0000 | [diff] [blame] | 1127 | srcM.fImage = SkMask::AllocImage(srcM.computeImageSize()); |
| 1128 | SkAutoMaskFreeImage srcCleanup(srcM.fImage); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1129 | |
| 1130 | GetBitmapAlpha(*this, srcM.fImage, srcM.fRowBytes); |
| 1131 | if (!filter->filterMask(&dstM, srcM, identity, NULL)) { |
| 1132 | goto NO_FILTER_CASE; |
| 1133 | } |
bungeman@google.com | 02f5584 | 2011-10-04 21:25:00 +0000 | [diff] [blame] | 1134 | SkAutoMaskFreeImage dstCleanup(dstM.fImage); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1135 | |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 1136 | tmpBitmap.setInfo(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.height()), |
| 1137 | dstM.fRowBytes); |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 1138 | if (!tmpBitmap.tryAllocPixels(allocator, NULL)) { |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 1139 | // Allocation of pixels for alpha bitmap failed. |
| 1140 | SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n", |
| 1141 | tmpBitmap.width(), tmpBitmap.height()); |
| 1142 | return false; |
| 1143 | } |
| 1144 | memcpy(tmpBitmap.getPixels(), dstM.fImage, dstM.computeImageSize()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1145 | if (offset) { |
| 1146 | offset->set(dstM.fBounds.fLeft, dstM.fBounds.fTop); |
| 1147 | } |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 1148 | SkDEBUGCODE(tmpBitmap.validate();) |
| 1149 | |
| 1150 | tmpBitmap.swap(*dst); |
| 1151 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | /////////////////////////////////////////////////////////////////////////////// |
| 1155 | |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 1156 | void SkBitmap::WriteRawPixels(SkWriteBuffer* buffer, const SkBitmap& bitmap) { |
| 1157 | const SkImageInfo info = bitmap.info(); |
| 1158 | SkAutoLockPixels alp(bitmap); |
| 1159 | if (0 == info.width() || 0 == info.height() || NULL == bitmap.getPixels()) { |
| 1160 | buffer->writeUInt(0); // instead of snugRB, signaling no pixels |
| 1161 | return; |
| 1162 | } |
| 1163 | |
| 1164 | const size_t snugRB = info.width() * info.bytesPerPixel(); |
| 1165 | const char* src = (const char*)bitmap.getPixels(); |
| 1166 | const size_t ramRB = bitmap.rowBytes(); |
skia.committer@gmail.com | 3c134a9 | 2014-05-24 03:05:26 +0000 | [diff] [blame] | 1167 | |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 1168 | buffer->write32(SkToU32(snugRB)); |
| 1169 | info.flatten(*buffer); |
| 1170 | |
| 1171 | const size_t size = snugRB * info.height(); |
| 1172 | SkAutoMalloc storage(size); |
| 1173 | char* dst = (char*)storage.get(); |
| 1174 | for (int y = 0; y < info.height(); ++y) { |
| 1175 | memcpy(dst, src, snugRB); |
| 1176 | dst += snugRB; |
| 1177 | src += ramRB; |
| 1178 | } |
| 1179 | buffer->writeByteArray(storage.get(), size); |
| 1180 | |
| 1181 | SkColorTable* ct = bitmap.getColorTable(); |
| 1182 | if (kIndex_8_SkColorType == info.colorType() && ct) { |
| 1183 | buffer->writeBool(true); |
| 1184 | ct->writeToBuffer(*buffer); |
| 1185 | } else { |
| 1186 | buffer->writeBool(false); |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | bool SkBitmap::ReadRawPixels(SkReadBuffer* buffer, SkBitmap* bitmap) { |
| 1191 | const size_t snugRB = buffer->readUInt(); |
| 1192 | if (0 == snugRB) { // no pixels |
| 1193 | return false; |
| 1194 | } |
| 1195 | |
| 1196 | SkImageInfo info; |
| 1197 | info.unflatten(*buffer); |
| 1198 | |
sugoi | ca95c19 | 2014-07-08 09:18:48 -0700 | [diff] [blame] | 1199 | // If there was an error reading "info", don't use it to compute minRowBytes() |
| 1200 | if (!buffer->validate(true)) { |
| 1201 | return false; |
| 1202 | } |
| 1203 | |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 1204 | const size_t ramRB = info.minRowBytes(); |
sugoi | bd0d9da | 2015-01-07 08:47:44 -0800 | [diff] [blame] | 1205 | const int height = SkMax32(info.height(), 0); |
| 1206 | const uint64_t snugSize = sk_64_mul(snugRB, height); |
| 1207 | const uint64_t ramSize = sk_64_mul(ramRB, height); |
| 1208 | static const uint64_t max_size_t = (size_t)(-1); |
| 1209 | if (!buffer->validate((snugSize <= ramSize) && (ramSize <= max_size_t))) { |
commit-bot@chromium.org | 0585843 | 2014-05-30 01:06:44 +0000 | [diff] [blame] | 1210 | return false; |
| 1211 | } |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 1212 | |
sugoi | bd0d9da | 2015-01-07 08:47:44 -0800 | [diff] [blame] | 1213 | SkAutoDataUnref data(SkData::NewUninitialized(SkToSizeT(ramSize))); |
reed | 33a3050 | 2014-09-11 08:42:36 -0700 | [diff] [blame] | 1214 | char* dst = (char*)data->writable_data(); |
sugoi | bd0d9da | 2015-01-07 08:47:44 -0800 | [diff] [blame] | 1215 | buffer->readByteArray(dst, SkToSizeT(snugSize)); |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 1216 | |
| 1217 | if (snugSize != ramSize) { |
| 1218 | const char* srcRow = dst + snugRB * (height - 1); |
| 1219 | char* dstRow = dst + ramRB * (height - 1); |
| 1220 | for (int y = height - 1; y >= 1; --y) { |
| 1221 | memmove(dstRow, srcRow, snugRB); |
| 1222 | srcRow -= snugRB; |
| 1223 | dstRow -= ramRB; |
| 1224 | } |
| 1225 | SkASSERT(srcRow == dstRow); // first row does not need to be moved |
| 1226 | } |
skia.committer@gmail.com | 3c134a9 | 2014-05-24 03:05:26 +0000 | [diff] [blame] | 1227 | |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 1228 | SkAutoTUnref<SkColorTable> ctable; |
| 1229 | if (buffer->readBool()) { |
| 1230 | ctable.reset(SkNEW_ARGS(SkColorTable, (*buffer))); |
| 1231 | } |
skia.committer@gmail.com | 3c134a9 | 2014-05-24 03:05:26 +0000 | [diff] [blame] | 1232 | |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 1233 | SkAutoTUnref<SkPixelRef> pr(SkMallocPixelRef::NewWithData(info, info.minRowBytes(), |
| 1234 | ctable.get(), data.get())); |
sugoi | 6af3147 | 2015-01-28 13:15:32 -0800 | [diff] [blame] | 1235 | if (!pr.get()) { |
| 1236 | return false; |
| 1237 | } |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 1238 | bitmap->setInfo(pr->info()); |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 1239 | bitmap->setPixelRef(pr, 0, 0); |
| 1240 | return true; |
| 1241 | } |
| 1242 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1243 | enum { |
| 1244 | SERIALIZE_PIXELTYPE_NONE, |
djsollen@google.com | 21830d9 | 2012-08-07 19:49:41 +0000 | [diff] [blame] | 1245 | SERIALIZE_PIXELTYPE_REF_DATA |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1246 | }; |
| 1247 | |
commit-bot@chromium.org | 851155c | 2014-05-27 14:03:51 +0000 | [diff] [blame] | 1248 | void SkBitmap::legacyUnflatten(SkReadBuffer& buffer) { |
reed | 9a9eae2 | 2014-07-07 14:32:06 -0700 | [diff] [blame] | 1249 | #ifdef SK_SUPPORT_LEGACY_PIXELREF_UNFLATTENABLE |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1250 | this->reset(); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1251 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 1252 | SkImageInfo info; |
| 1253 | info.unflatten(buffer); |
| 1254 | size_t rowBytes = buffer.readInt(); |
commit-bot@chromium.org | 33fed14 | 2014-02-13 18:46:13 +0000 | [diff] [blame] | 1255 | if (!buffer.validate((info.width() >= 0) && (info.height() >= 0) && |
| 1256 | SkColorTypeIsValid(info.fColorType) && |
| 1257 | SkAlphaTypeIsValid(info.fAlphaType) && |
scroggo | 2fd0d14 | 2014-07-01 07:08:19 -0700 | [diff] [blame] | 1258 | SkColorTypeValidateAlphaType(info.fColorType, info.fAlphaType) && |
commit-bot@chromium.org | 33fed14 | 2014-02-13 18:46:13 +0000 | [diff] [blame] | 1259 | info.validRowBytes(rowBytes))) { |
| 1260 | return; |
| 1261 | } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1262 | |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 1263 | bool configIsValid = this->setInfo(info, rowBytes); |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 1264 | buffer.validate(configIsValid); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 1265 | |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 1266 | int reftype = buffer.readInt(); |
commit-bot@chromium.org | ce33d60 | 2013-11-25 21:46:31 +0000 | [diff] [blame] | 1267 | if (buffer.validate((SERIALIZE_PIXELTYPE_REF_DATA == reftype) || |
| 1268 | (SERIALIZE_PIXELTYPE_NONE == reftype))) { |
| 1269 | switch (reftype) { |
| 1270 | case SERIALIZE_PIXELTYPE_REF_DATA: { |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 1271 | SkIPoint origin; |
| 1272 | origin.fX = buffer.readInt(); |
| 1273 | origin.fY = buffer.readInt(); |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 1274 | size_t offset = origin.fY * rowBytes + origin.fX * info.bytesPerPixel(); |
reed | 9a9eae2 | 2014-07-07 14:32:06 -0700 | [diff] [blame] | 1275 | SkPixelRef* pr = buffer.readFlattenable<SkPixelRef>(); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 1276 | if (!buffer.validate((NULL == pr) || |
| 1277 | (pr->getAllocatedSizeInBytes() >= (offset + this->getSafeSize())))) { |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 1278 | origin.setZero(); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 1279 | } |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 1280 | SkSafeUnref(this->setPixelRef(pr, origin)); |
commit-bot@chromium.org | ce33d60 | 2013-11-25 21:46:31 +0000 | [diff] [blame] | 1281 | break; |
| 1282 | } |
| 1283 | case SERIALIZE_PIXELTYPE_NONE: |
| 1284 | break; |
| 1285 | default: |
| 1286 | SkDEBUGFAIL("unrecognized pixeltype in serialized data"); |
| 1287 | sk_throw(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1288 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1289 | } |
reed | 9a9eae2 | 2014-07-07 14:32:06 -0700 | [diff] [blame] | 1290 | #else |
| 1291 | sk_throw(); |
| 1292 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | /////////////////////////////////////////////////////////////////////////////// |
| 1296 | |
| 1297 | SkBitmap::RLEPixels::RLEPixels(int width, int height) { |
| 1298 | fHeight = height; |
commit-bot@chromium.org | 235002f | 2013-10-09 18:39:59 +0000 | [diff] [blame] | 1299 | fYPtrs = (uint8_t**)sk_calloc_throw(height * sizeof(uint8_t*)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | SkBitmap::RLEPixels::~RLEPixels() { |
| 1303 | sk_free(fYPtrs); |
| 1304 | } |
| 1305 | |
| 1306 | /////////////////////////////////////////////////////////////////////////////// |
| 1307 | |
| 1308 | #ifdef SK_DEBUG |
| 1309 | void SkBitmap::validate() const { |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 1310 | fInfo.validate(); |
commit-bot@chromium.org | d5414e5 | 2014-02-13 22:30:38 +0000 | [diff] [blame] | 1311 | |
| 1312 | // ImageInfo may not require this, but Bitmap ensures that opaque-only |
| 1313 | // colorTypes report opaque for their alphatype |
| 1314 | if (kRGB_565_SkColorType == fInfo.colorType()) { |
| 1315 | SkASSERT(kOpaque_SkAlphaType == fInfo.alphaType()); |
| 1316 | } |
| 1317 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 1318 | SkASSERT(fInfo.validRowBytes(fRowBytes)); |
scroggo | 0847059 | 2014-07-15 19:56:48 -0700 | [diff] [blame] | 1319 | uint8_t allFlags = kImageIsVolatile_Flag; |
scroggo@google.com | 8e990eb | 2013-06-14 15:55:56 +0000 | [diff] [blame] | 1320 | #ifdef SK_BUILD_FOR_ANDROID |
| 1321 | allFlags |= kHasHardwareMipMap_Flag; |
| 1322 | #endif |
scroggo | 0847059 | 2014-07-15 19:56:48 -0700 | [diff] [blame] | 1323 | SkASSERT((~allFlags & fFlags) == 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1324 | SkASSERT(fPixelLockCount >= 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1325 | |
reed@google.com | 615316c | 2014-01-15 19:15:23 +0000 | [diff] [blame] | 1326 | if (fPixels) { |
| 1327 | SkASSERT(fPixelRef); |
robertphillips | e77dadd | 2014-11-21 05:50:21 -0800 | [diff] [blame] | 1328 | SkASSERT(fPixelLockCount > 0); |
reed@google.com | 615316c | 2014-01-15 19:15:23 +0000 | [diff] [blame] | 1329 | SkASSERT(fPixelRef->isLocked()); |
| 1330 | SkASSERT(fPixelRef->rowBytes() == fRowBytes); |
| 1331 | SkASSERT(fPixelRefOrigin.fX >= 0); |
| 1332 | SkASSERT(fPixelRefOrigin.fY >= 0); |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 1333 | SkASSERT(fPixelRef->info().width() >= (int)this->width() + fPixelRefOrigin.fX); |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 1334 | SkASSERT(fPixelRef->info().height() >= (int)this->height() + fPixelRefOrigin.fY); |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 1335 | SkASSERT(fPixelRef->rowBytes() >= fInfo.minRowBytes()); |
reed@google.com | 615316c | 2014-01-15 19:15:23 +0000 | [diff] [blame] | 1336 | } else { |
| 1337 | SkASSERT(NULL == fColorTable); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1338 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1339 | } |
| 1340 | #endif |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 1341 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 1342 | #ifndef SK_IGNORE_TO_STRING |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 1343 | void SkBitmap::toString(SkString* str) const { |
| 1344 | |
commit-bot@chromium.org | cba7378 | 2014-05-29 15:57:47 +0000 | [diff] [blame] | 1345 | static const char* gColorTypeNames[kLastEnum_SkColorType + 1] = { |
| 1346 | "UNKNOWN", "A8", "565", "4444", "RGBA", "BGRA", "INDEX8", |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 1347 | }; |
| 1348 | |
| 1349 | str->appendf("bitmap: ((%d, %d) %s", this->width(), this->height(), |
commit-bot@chromium.org | cba7378 | 2014-05-29 15:57:47 +0000 | [diff] [blame] | 1350 | gColorTypeNames[this->colorType()]); |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 1351 | |
| 1352 | str->append(" ("); |
| 1353 | if (this->isOpaque()) { |
| 1354 | str->append("opaque"); |
| 1355 | } else { |
| 1356 | str->append("transparent"); |
| 1357 | } |
| 1358 | if (this->isImmutable()) { |
| 1359 | str->append(", immutable"); |
| 1360 | } else { |
| 1361 | str->append(", not-immutable"); |
| 1362 | } |
| 1363 | str->append(")"); |
| 1364 | |
| 1365 | SkPixelRef* pr = this->pixelRef(); |
| 1366 | if (NULL == pr) { |
| 1367 | // show null or the explicit pixel address (rare) |
| 1368 | str->appendf(" pixels:%p", this->getPixels()); |
| 1369 | } else { |
| 1370 | const char* uri = pr->getURI(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1371 | if (uri) { |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 1372 | str->appendf(" uri:\"%s\"", uri); |
| 1373 | } else { |
| 1374 | str->appendf(" pixelref:%p", pr); |
| 1375 | } |
| 1376 | } |
| 1377 | |
| 1378 | str->append(")"); |
| 1379 | } |
| 1380 | #endif |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 1381 | |
| 1382 | /////////////////////////////////////////////////////////////////////////////// |
| 1383 | |
| 1384 | #ifdef SK_DEBUG |
| 1385 | void SkImageInfo::validate() const { |
| 1386 | SkASSERT(fWidth >= 0); |
| 1387 | SkASSERT(fHeight >= 0); |
| 1388 | SkASSERT(SkColorTypeIsValid(fColorType)); |
| 1389 | SkASSERT(SkAlphaTypeIsValid(fAlphaType)); |
| 1390 | } |
| 1391 | #endif |