reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2008 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
herb | b906daf | 2015-09-29 09:37:59 -0700 | [diff] [blame] | 8 | #include "SkAtomics.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | #include "SkBitmap.h" |
| 10 | #include "SkColorPriv.h" |
lsalzman | a2415ac | 2016-10-11 14:29:12 -0700 | [diff] [blame] | 11 | #include "SkConfig8888.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 12 | #include "SkData.h" |
| 13 | #include "SkFilterQuality.h" |
Hal Canary | 4cba3fe | 2016-12-07 14:59:27 -0500 | [diff] [blame] | 14 | #include "SkHalf.h" |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 15 | #include "SkImageInfoPriv.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | #include "SkMallocPixelRef.h" |
| 17 | #include "SkMask.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 18 | #include "SkMath.h" |
jvanverth | 02802f6 | 2015-07-02 06:42:49 -0700 | [diff] [blame] | 19 | #include "SkPixelRef.h" |
mtklein | 1b24933 | 2015-07-07 12:21:21 -0700 | [diff] [blame] | 20 | #include "SkReadBuffer.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 21 | #include "SkRect.h" |
| 22 | #include "SkScalar.h" |
scroggo | 565901d | 2015-12-10 10:44:13 -0800 | [diff] [blame] | 23 | #include "SkTemplates.h" |
vandebo@chromium.org | 112706d | 2011-02-24 22:50:55 +0000 | [diff] [blame] | 24 | #include "SkUnPreMultiply.h" |
mtklein | 1b24933 | 2015-07-07 12:21:21 -0700 | [diff] [blame] | 25 | #include "SkWriteBuffer.h" |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 26 | #include "SkWritePixelsRec.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 27 | |
| 28 | #include <string.h> |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 30 | static bool reset_return_false(SkBitmap* bm) { |
| 31 | bm->reset(); |
| 32 | return false; |
| 33 | } |
| 34 | |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 35 | SkBitmap::SkBitmap() |
| 36 | : fPixelLockCount(0) |
| 37 | , fPixels (nullptr) |
| 38 | , fColorTable (nullptr) |
| 39 | , fPixelRefOrigin{0, 0} |
| 40 | , fRowBytes (0) |
| 41 | , fFlags (0) {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 42 | |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 43 | // copy pixelref, but don't copy lock. |
| 44 | SkBitmap::SkBitmap(const SkBitmap& src) |
| 45 | : fPixelRef (src.fPixelRef) |
| 46 | , fPixelLockCount(0) |
| 47 | , fPixels (nullptr) |
| 48 | , fColorTable (nullptr) |
| 49 | , fPixelRefOrigin(src.fPixelRefOrigin) |
| 50 | , fInfo (src.fInfo) |
| 51 | , fRowBytes (src.fRowBytes) |
| 52 | , fFlags (src.fFlags) |
| 53 | { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | SkDEBUGCODE(src.validate();) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | SkDEBUGCODE(this->validate();) |
| 56 | } |
| 57 | |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 58 | // take lock and lockcount from other. |
| 59 | SkBitmap::SkBitmap(SkBitmap&& other) |
| 60 | : fPixelRef (std::move(other.fPixelRef)) |
| 61 | , fPixelLockCount (other.fPixelLockCount) |
| 62 | , fPixels (other.fPixels) |
| 63 | , fColorTable (other.fColorTable) |
| 64 | , fPixelRefOrigin (other.fPixelRefOrigin) |
| 65 | , fInfo (std::move(other.fInfo)) |
| 66 | , fRowBytes (other.fRowBytes) |
| 67 | , fFlags (other.fFlags) { |
| 68 | SkASSERT(!other.fPixelRef); |
| 69 | other.fInfo.reset(); |
| 70 | other.fPixelLockCount = 0; |
| 71 | other.fPixels = nullptr; |
| 72 | other.fColorTable = nullptr; |
| 73 | other.fPixelRefOrigin = SkIPoint{0, 0}; |
| 74 | other.fRowBytes = 0; |
| 75 | other.fFlags = 0; |
| 76 | } |
halcanary | 023bda0 | 2015-12-14 10:19:17 -0800 | [diff] [blame] | 77 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | SkBitmap::~SkBitmap() { |
| 79 | SkDEBUGCODE(this->validate();) |
| 80 | this->freePixels(); |
| 81 | } |
| 82 | |
| 83 | SkBitmap& SkBitmap::operator=(const SkBitmap& src) { |
| 84 | if (this != &src) { |
| 85 | this->freePixels(); |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 86 | SkASSERT(!fPixels); |
| 87 | SkASSERT(!fColorTable); |
| 88 | SkASSERT(!fPixelLockCount); |
| 89 | fPixelRef = src.fPixelRef; |
| 90 | fPixelRefOrigin = src.fPixelRefOrigin; |
| 91 | fInfo = src.fInfo; |
| 92 | fRowBytes = src.fRowBytes; |
| 93 | fFlags = src.fFlags; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 95 | SkDEBUGCODE(this->validate();) |
| 96 | return *this; |
| 97 | } |
| 98 | |
halcanary | 023bda0 | 2015-12-14 10:19:17 -0800 | [diff] [blame] | 99 | SkBitmap& SkBitmap::operator=(SkBitmap&& other) { |
| 100 | if (this != &other) { |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 101 | this->freePixels(); |
| 102 | SkASSERT(!fPixels); |
| 103 | SkASSERT(!fColorTable); |
| 104 | SkASSERT(!fPixelLockCount); |
| 105 | fPixelRef = std::move(other.fPixelRef); |
| 106 | fInfo = std::move(other.fInfo); |
| 107 | fPixelLockCount = other.fPixelLockCount; |
| 108 | fPixels = other.fPixels; |
| 109 | fColorTable = other.fColorTable; |
| 110 | fPixelRefOrigin = other.fPixelRefOrigin; |
| 111 | fRowBytes = other.fRowBytes; |
| 112 | fFlags = other.fFlags; |
| 113 | SkASSERT(!other.fPixelRef); |
| 114 | other.fInfo.reset(); |
| 115 | other.fPixelLockCount = 0; |
| 116 | other.fPixels = nullptr; |
| 117 | other.fColorTable = nullptr; |
| 118 | other.fPixelRefOrigin = SkIPoint{0, 0}; |
| 119 | other.fRowBytes = 0; |
| 120 | other.fFlags = 0; |
halcanary | 023bda0 | 2015-12-14 10:19:17 -0800 | [diff] [blame] | 121 | } |
| 122 | return *this; |
| 123 | } |
| 124 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 125 | void SkBitmap::swap(SkBitmap& other) { |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 126 | SkTSwap(*this, other); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 127 | SkDEBUGCODE(this->validate();) |
| 128 | } |
| 129 | |
| 130 | void SkBitmap::reset() { |
| 131 | this->freePixels(); |
msarett | 23c5110 | 2016-05-27 07:39:02 -0700 | [diff] [blame] | 132 | this->fInfo.reset(); |
reed@android.com | 4516f47 | 2009-06-29 16:25:36 +0000 | [diff] [blame] | 133 | sk_bzero(this, sizeof(*this)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 134 | } |
| 135 | |
reed@google.com | 86b2e43 | 2012-03-15 21:17:03 +0000 | [diff] [blame] | 136 | void SkBitmap::getBounds(SkRect* bounds) const { |
| 137 | SkASSERT(bounds); |
| 138 | bounds->set(0, 0, |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 139 | SkIntToScalar(fInfo.width()), SkIntToScalar(fInfo.height())); |
reed@google.com | 86b2e43 | 2012-03-15 21:17:03 +0000 | [diff] [blame] | 140 | } |
| 141 | |
reed@google.com | 80e1459 | 2012-03-16 14:58:07 +0000 | [diff] [blame] | 142 | void SkBitmap::getBounds(SkIRect* bounds) const { |
| 143 | SkASSERT(bounds); |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 144 | bounds->set(0, 0, fInfo.width(), fInfo.height()); |
reed@google.com | 80e1459 | 2012-03-16 14:58:07 +0000 | [diff] [blame] | 145 | } |
| 146 | |
reed@google.com | 86b2e43 | 2012-03-15 21:17:03 +0000 | [diff] [blame] | 147 | /////////////////////////////////////////////////////////////////////////////// |
| 148 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 149 | bool SkBitmap::setInfo(const SkImageInfo& info, size_t rowBytes) { |
| 150 | SkAlphaType newAT = info.alphaType(); |
| 151 | if (!SkColorTypeValidateAlphaType(info.colorType(), info.alphaType(), &newAT)) { |
commit-bot@chromium.org | d5414e5 | 2014-02-13 22:30:38 +0000 | [diff] [blame] | 152 | return reset_return_false(this); |
| 153 | } |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 154 | // 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] | 155 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 156 | // require that rowBytes fit in 31bits |
| 157 | int64_t mrb = info.minRowBytes64(); |
| 158 | if ((int32_t)mrb != mrb) { |
| 159 | return reset_return_false(this); |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 160 | } |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 161 | if ((int64_t)rowBytes != (int32_t)rowBytes) { |
| 162 | return reset_return_false(this); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 163 | } |
reed@android.com | 89bb83a | 2009-05-29 21:30:42 +0000 | [diff] [blame] | 164 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 165 | if (info.width() < 0 || info.height() < 0) { |
| 166 | return reset_return_false(this); |
| 167 | } |
| 168 | |
| 169 | if (kUnknown_SkColorType == info.colorType()) { |
| 170 | rowBytes = 0; |
| 171 | } else if (0 == rowBytes) { |
| 172 | rowBytes = (size_t)mrb; |
reed | f0aed97 | 2014-07-01 12:48:11 -0700 | [diff] [blame] | 173 | } else if (!info.validRowBytes(rowBytes)) { |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 174 | return reset_return_false(this); |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | this->freePixels(); |
| 178 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 179 | fInfo = info.makeAlphaType(newAT); |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 180 | fRowBytes = SkToU32(rowBytes); |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 181 | return true; |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 182 | } |
| 183 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 184 | bool SkBitmap::setAlphaType(SkAlphaType newAlphaType) { |
| 185 | if (!SkColorTypeValidateAlphaType(fInfo.colorType(), newAlphaType, &newAlphaType)) { |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 186 | return false; |
| 187 | } |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 188 | if (fInfo.alphaType() != newAlphaType) { |
| 189 | fInfo = fInfo.makeAlphaType(newAlphaType); |
commit-bot@chromium.org | 0e8d0d6 | 2014-01-27 15:41:07 +0000 | [diff] [blame] | 190 | if (fPixelRef) { |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 191 | fPixelRef->changeAlphaType(newAlphaType); |
commit-bot@chromium.org | 0e8d0d6 | 2014-01-27 15:41:07 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 194 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | void SkBitmap::updatePixelsFromRef() const { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 198 | if (fPixelRef) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 199 | if (fPixelLockCount > 0) { |
reed@google.com | ff0da4f | 2012-05-17 13:14:52 +0000 | [diff] [blame] | 200 | SkASSERT(fPixelRef->isLocked()); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 201 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 202 | void* p = fPixelRef->pixels(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 203 | if (p) { |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 204 | p = (char*)p |
reed@google.com | 303c475 | 2014-01-09 20:00:14 +0000 | [diff] [blame] | 205 | + fPixelRefOrigin.fY * fRowBytes |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 206 | + fPixelRefOrigin.fX * fInfo.bytesPerPixel(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 207 | } |
| 208 | fPixels = p; |
reed@google.com | 5f62ed7 | 2014-01-15 19:59:45 +0000 | [diff] [blame] | 209 | fColorTable = fPixelRef->colorTable(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 210 | } else { |
| 211 | SkASSERT(0 == fPixelLockCount); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 212 | fPixels = nullptr; |
| 213 | fColorTable = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 218 | #ifdef SK_SUPPORT_LEGACY_BITMAP_SETPIXELREF |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 219 | SkPixelRef* SkBitmap::setPixelRef(SkPixelRef* pr, int dx, int dy) { |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 220 | this->setPixelRef(sk_ref_sp(pr), dx, dy); |
| 221 | return pr; |
| 222 | } |
| 223 | #endif |
| 224 | |
| 225 | void SkBitmap::setPixelRef(sk_sp<SkPixelRef> pr, int dx, int dy) { |
reed@google.com | dcea530 | 2014-01-03 13:43:01 +0000 | [diff] [blame] | 226 | #ifdef SK_DEBUG |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 227 | if (pr) { |
commit-bot@chromium.org | 466f5f3 | 2014-05-27 21:30:37 +0000 | [diff] [blame] | 228 | if (kUnknown_SkColorType != fInfo.colorType()) { |
reed@google.com | dcea530 | 2014-01-03 13:43:01 +0000 | [diff] [blame] | 229 | const SkImageInfo& prInfo = pr->info(); |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 230 | SkASSERT(fInfo.width() <= prInfo.width()); |
| 231 | SkASSERT(fInfo.height() <= prInfo.height()); |
| 232 | SkASSERT(fInfo.colorType() == prInfo.colorType()); |
| 233 | switch (prInfo.alphaType()) { |
reed | 4497748 | 2015-02-27 10:23:00 -0800 | [diff] [blame] | 234 | case kUnknown_SkAlphaType: |
| 235 | SkASSERT(fInfo.alphaType() == kUnknown_SkAlphaType); |
reed@google.com | dcea530 | 2014-01-03 13:43:01 +0000 | [diff] [blame] | 236 | break; |
| 237 | case kOpaque_SkAlphaType: |
| 238 | case kPremul_SkAlphaType: |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 239 | SkASSERT(fInfo.alphaType() == kOpaque_SkAlphaType || |
| 240 | fInfo.alphaType() == kPremul_SkAlphaType); |
reed@google.com | dcea530 | 2014-01-03 13:43:01 +0000 | [diff] [blame] | 241 | break; |
| 242 | case kUnpremul_SkAlphaType: |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 243 | SkASSERT(fInfo.alphaType() == kOpaque_SkAlphaType || |
| 244 | fInfo.alphaType() == kUnpremul_SkAlphaType); |
reed@google.com | dcea530 | 2014-01-03 13:43:01 +0000 | [diff] [blame] | 245 | break; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 250 | |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 251 | if (pr) { |
| 252 | const SkImageInfo& info = pr->info(); |
bungeman | 62ce030 | 2015-08-28 09:09:32 -0700 | [diff] [blame] | 253 | fPixelRefOrigin.set(SkTPin(dx, 0, info.width()), SkTPin(dy, 0, info.height())); |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 254 | } else { |
| 255 | // ignore dx,dy if there is no pixelref |
| 256 | fPixelRefOrigin.setZero(); |
| 257 | } |
| 258 | |
| 259 | if (fPixelRef != pr) { |
piotaixr | 0eb02a6 | 2014-06-16 11:50:49 -0700 | [diff] [blame] | 260 | this->freePixels(); |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 261 | SkASSERT(!fPixelRef); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 262 | |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 263 | fPixelRef = std::move(pr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 264 | this->updatePixelsFromRef(); |
| 265 | } |
| 266 | |
| 267 | SkDEBUGCODE(this->validate();) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | void SkBitmap::lockPixels() const { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 271 | if (fPixelRef && 0 == sk_atomic_inc(&fPixelLockCount)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 272 | fPixelRef->lockPixels(); |
| 273 | this->updatePixelsFromRef(); |
| 274 | } |
| 275 | SkDEBUGCODE(this->validate();) |
| 276 | } |
| 277 | |
| 278 | void SkBitmap::unlockPixels() const { |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 279 | SkASSERT(!fPixelRef || fPixelLockCount > 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 280 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 281 | if (fPixelRef && 1 == sk_atomic_dec(&fPixelLockCount)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 282 | fPixelRef->unlockPixels(); |
| 283 | this->updatePixelsFromRef(); |
| 284 | } |
| 285 | SkDEBUGCODE(this->validate();) |
| 286 | } |
| 287 | |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 288 | bool SkBitmap::lockPixelsAreWritable() const { |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 289 | return fPixelRef ? fPixelRef->lockPixelsAreWritable() : false; |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 290 | } |
| 291 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 292 | void SkBitmap::setPixels(void* p, SkColorTable* ctable) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 293 | if (nullptr == p) { |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 294 | this->setPixelRef(nullptr, 0, 0); |
reed@google.com | 8e1034e | 2012-07-30 13:16:35 +0000 | [diff] [blame] | 295 | return; |
| 296 | } |
| 297 | |
commit-bot@chromium.org | 466f5f3 | 2014-05-27 21:30:37 +0000 | [diff] [blame] | 298 | if (kUnknown_SkColorType == fInfo.colorType()) { |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 299 | this->setPixelRef(nullptr, 0, 0); |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 300 | return; |
| 301 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 302 | |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 303 | sk_sp<SkPixelRef> pr(SkMallocPixelRef::NewDirect(fInfo, p, fRowBytes, ctable)); |
| 304 | this->setPixelRef(std::move(pr), 0, 0); |
| 305 | if (!fPixelRef) { |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 306 | return; |
| 307 | } |
djsollen@google.com | c84b833 | 2012-07-27 13:41:44 +0000 | [diff] [blame] | 308 | // since we're already allocated, we lockPixels right away |
| 309 | this->lockPixels(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 310 | SkDEBUGCODE(this->validate();) |
| 311 | } |
| 312 | |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 313 | bool SkBitmap::tryAllocPixels(Allocator* allocator, SkColorTable* ctable) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 314 | HeapAllocator stdalloc; |
skia.committer@gmail.com | d2ac07b | 2014-01-25 07:01:49 +0000 | [diff] [blame] | 315 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 316 | if (nullptr == allocator) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 317 | allocator = &stdalloc; |
| 318 | } |
| 319 | return allocator->allocPixelRef(this, ctable); |
| 320 | } |
| 321 | |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 322 | /////////////////////////////////////////////////////////////////////////////// |
| 323 | |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 324 | bool SkBitmap::tryAllocPixels(const SkImageInfo& requestedInfo, size_t rowBytes) { |
reed | bae704b | 2014-06-28 14:26:35 -0700 | [diff] [blame] | 325 | if (kIndex_8_SkColorType == requestedInfo.colorType()) { |
| 326 | return reset_return_false(this); |
| 327 | } |
reed | f0aed97 | 2014-07-01 12:48:11 -0700 | [diff] [blame] | 328 | if (!this->setInfo(requestedInfo, rowBytes)) { |
reed | bae704b | 2014-06-28 14:26:35 -0700 | [diff] [blame] | 329 | return reset_return_false(this); |
| 330 | } |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 331 | |
reed | bae704b | 2014-06-28 14:26:35 -0700 | [diff] [blame] | 332 | // setInfo may have corrected info (e.g. 565 is always opaque). |
| 333 | const SkImageInfo& correctedInfo = this->info(); |
reed | f0aed97 | 2014-07-01 12:48:11 -0700 | [diff] [blame] | 334 | // setInfo may have computed a valid rowbytes if 0 were passed in |
| 335 | rowBytes = this->rowBytes(); |
| 336 | |
reed | bae704b | 2014-06-28 14:26:35 -0700 | [diff] [blame] | 337 | SkMallocPixelRef::PRFactory defaultFactory; |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 338 | |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 339 | sk_sp<SkPixelRef> pr(defaultFactory.create(correctedInfo, rowBytes, nullptr)); |
| 340 | if (!pr) { |
reed | bae704b | 2014-06-28 14:26:35 -0700 | [diff] [blame] | 341 | return reset_return_false(this); |
| 342 | } |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 343 | this->setPixelRef(std::move(pr), 0, 0); |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 344 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 345 | // TODO: lockPixels could/should return bool or void*/nullptr |
reed | bae704b | 2014-06-28 14:26:35 -0700 | [diff] [blame] | 346 | this->lockPixels(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 347 | if (nullptr == this->getPixels()) { |
reed | bae704b | 2014-06-28 14:26:35 -0700 | [diff] [blame] | 348 | return reset_return_false(this); |
| 349 | } |
| 350 | return true; |
| 351 | } |
| 352 | |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 353 | bool SkBitmap::tryAllocPixels(const SkImageInfo& requestedInfo, SkPixelRefFactory* factory, |
| 354 | SkColorTable* ctable) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 355 | if (kIndex_8_SkColorType == requestedInfo.colorType() && nullptr == ctable) { |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 356 | return reset_return_false(this); |
| 357 | } |
scroggo | 0187dc2 | 2014-06-05 11:18:04 -0700 | [diff] [blame] | 358 | if (!this->setInfo(requestedInfo)) { |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 359 | return reset_return_false(this); |
| 360 | } |
| 361 | |
scroggo | 0187dc2 | 2014-06-05 11:18:04 -0700 | [diff] [blame] | 362 | // setInfo may have corrected info (e.g. 565 is always opaque). |
| 363 | const SkImageInfo& correctedInfo = this->info(); |
| 364 | |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 365 | SkMallocPixelRef::PRFactory defaultFactory; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 366 | if (nullptr == factory) { |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 367 | factory = &defaultFactory; |
| 368 | } |
skia.committer@gmail.com | d2ac07b | 2014-01-25 07:01:49 +0000 | [diff] [blame] | 369 | |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 370 | sk_sp<SkPixelRef> pr(factory->create(correctedInfo, correctedInfo.minRowBytes(), ctable)); |
| 371 | if (!pr) { |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 372 | return reset_return_false(this); |
| 373 | } |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 374 | this->setPixelRef(std::move(pr), 0, 0); |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 375 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 376 | // TODO: lockPixels could/should return bool or void*/nullptr |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 377 | this->lockPixels(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 378 | if (nullptr == this->getPixels()) { |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 379 | return reset_return_false(this); |
| 380 | } |
| 381 | return true; |
| 382 | } |
| 383 | |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 384 | static void invoke_release_proc(void (*proc)(void* pixels, void* ctx), void* pixels, void* ctx) { |
| 385 | if (proc) { |
| 386 | proc(pixels, ctx); |
| 387 | } |
| 388 | } |
| 389 | |
scroggo | 0187dc2 | 2014-06-05 11:18:04 -0700 | [diff] [blame] | 390 | bool SkBitmap::installPixels(const SkImageInfo& requestedInfo, void* pixels, size_t rb, |
| 391 | SkColorTable* ct, void (*releaseProc)(void* addr, void* context), |
| 392 | void* context) { |
| 393 | if (!this->setInfo(requestedInfo, rb)) { |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 394 | invoke_release_proc(releaseProc, pixels, context); |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 395 | this->reset(); |
| 396 | return false; |
| 397 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 398 | if (nullptr == pixels) { |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 399 | invoke_release_proc(releaseProc, pixels, context); |
| 400 | return true; // we behaved as if they called setInfo() |
| 401 | } |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 402 | |
scroggo | 0187dc2 | 2014-06-05 11:18:04 -0700 | [diff] [blame] | 403 | // setInfo may have corrected info (e.g. 565 is always opaque). |
| 404 | const SkImageInfo& correctedInfo = this->info(); |
| 405 | |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 406 | sk_sp<SkPixelRef> pr(SkMallocPixelRef::NewWithProc(correctedInfo, rb, ct, pixels, releaseProc, |
| 407 | context)); |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 408 | if (!pr) { |
| 409 | this->reset(); |
| 410 | return false; |
| 411 | } |
| 412 | |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 413 | this->setPixelRef(std::move(pr), 0, 0); |
mike@reedtribe.org | 6e58cf3 | 2014-02-16 20:54:21 +0000 | [diff] [blame] | 414 | |
| 415 | // since we're already allocated, we lockPixels right away |
| 416 | this->lockPixels(); |
mike@reedtribe.org | 6e58cf3 | 2014-02-16 20:54:21 +0000 | [diff] [blame] | 417 | SkDEBUGCODE(this->validate();) |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 418 | return true; |
| 419 | } |
| 420 | |
halcanary | e36ec87 | 2015-12-09 11:36:59 -0800 | [diff] [blame] | 421 | bool SkBitmap::installPixels(const SkPixmap& pixmap) { |
| 422 | return this->installPixels(pixmap.info(), pixmap.writable_addr(), |
| 423 | pixmap.rowBytes(), pixmap.ctable(), |
| 424 | nullptr, nullptr); |
| 425 | } |
| 426 | |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 427 | bool SkBitmap::installMaskPixels(const SkMask& mask) { |
| 428 | if (SkMask::kA8_Format != mask.fFormat) { |
| 429 | this->reset(); |
| 430 | return false; |
| 431 | } |
| 432 | return this->installPixels(SkImageInfo::MakeA8(mask.fBounds.width(), |
| 433 | mask.fBounds.height()), |
| 434 | mask.fImage, mask.fRowBytes); |
| 435 | } |
| 436 | |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 437 | /////////////////////////////////////////////////////////////////////////////// |
| 438 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 439 | void SkBitmap::freePixels() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 440 | if (fPixelRef) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 441 | if (fPixelLockCount > 0) { |
| 442 | fPixelRef->unlockPixels(); |
| 443 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 444 | fPixelRef = nullptr; |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 445 | fPixelRefOrigin.setZero(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 446 | } |
| 447 | fPixelLockCount = 0; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 448 | fPixels = nullptr; |
| 449 | fColorTable = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 450 | } |
| 451 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 452 | uint32_t SkBitmap::getGenerationID() const { |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 453 | return fPixelRef ? fPixelRef->getGenerationID() : 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | void SkBitmap::notifyPixelsChanged() const { |
junov@chromium.org | b052129 | 2011-12-15 20:14:06 +0000 | [diff] [blame] | 457 | SkASSERT(!this->isImmutable()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 458 | if (fPixelRef) { |
| 459 | fPixelRef->notifyPixelsChanged(); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | /////////////////////////////////////////////////////////////////////////////// |
| 464 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 465 | /** We explicitly use the same allocator for our pixels that SkMask does, |
| 466 | so that we can freely assign memory allocated by one class to the other. |
| 467 | */ |
| 468 | bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst, |
| 469 | SkColorTable* ctable) { |
commit-bot@chromium.org | 466f5f3 | 2014-05-27 21:30:37 +0000 | [diff] [blame] | 470 | const SkImageInfo info = dst->info(); |
| 471 | if (kUnknown_SkColorType == info.colorType()) { |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 472 | // SkDebugf("unsupported config for info %d\n", dst->config()); |
| 473 | return false; |
| 474 | } |
skia.committer@gmail.com | 96f5fa0 | 2013-12-16 07:01:40 +0000 | [diff] [blame] | 475 | |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 476 | sk_sp<SkPixelRef> pr(SkMallocPixelRef::NewAllocate(info, dst->rowBytes(), ctable)); |
| 477 | if (!pr) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 478 | return false; |
| 479 | } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 480 | |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 481 | dst->setPixelRef(std::move(pr), 0, 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 482 | // since we're already allocated, we lockPixels right away |
| 483 | dst->lockPixels(); |
| 484 | return true; |
| 485 | } |
| 486 | |
| 487 | /////////////////////////////////////////////////////////////////////////////// |
| 488 | |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 489 | static bool copy_pixels_to(const SkPixmap& src, void* const dst, size_t dstSize, |
| 490 | size_t dstRowBytes, bool preserveDstPad) { |
| 491 | const SkImageInfo& info = src.info(); |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 492 | |
scroggo@google.com | 0ba4bf4 | 2013-02-25 16:02:36 +0000 | [diff] [blame] | 493 | if (0 == dstRowBytes) { |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 494 | dstRowBytes = src.rowBytes(); |
scroggo@google.com | 0ba4bf4 | 2013-02-25 16:02:36 +0000 | [diff] [blame] | 495 | } |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 496 | if (dstRowBytes < info.minRowBytes()) { |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 497 | return false; |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 498 | } |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 499 | |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 500 | if (!preserveDstPad && static_cast<uint32_t>(dstRowBytes) == src.rowBytes()) { |
| 501 | size_t safeSize = src.getSafeSize(); |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 502 | if (safeSize > dstSize || safeSize == 0) |
| 503 | return false; |
| 504 | else { |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 505 | // This implementation will write bytes beyond the end of each row, |
| 506 | // excluding the last row, if the bitmap's stride is greater than |
| 507 | // strictly required by the current config. |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 508 | memcpy(dst, src.addr(), safeSize); |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 509 | return true; |
| 510 | } |
| 511 | } else { |
| 512 | // If destination has different stride than us, then copy line by line. |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 513 | if (info.getSafeSize(dstRowBytes) > dstSize) { |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 514 | return false; |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 515 | } else { |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 516 | // Just copy what we need on each line. |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 517 | size_t rowBytes = info.minRowBytes(); |
| 518 | const uint8_t* srcP = reinterpret_cast<const uint8_t*>(src.addr()); |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 519 | uint8_t* dstP = reinterpret_cast<uint8_t*>(dst); |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 520 | for (int row = 0; row < info.height(); ++row) { |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 521 | memcpy(dstP, srcP, rowBytes); |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 522 | srcP += src.rowBytes(); |
| 523 | dstP += dstRowBytes; |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | return true; |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 531 | bool SkBitmap::copyPixelsTo(void* dst, size_t dstSize, size_t dstRB, bool preserveDstPad) const { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 532 | if (nullptr == dst) { |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 533 | return false; |
| 534 | } |
| 535 | SkAutoPixmapUnlock result; |
| 536 | if (!this->requestLock(&result)) { |
| 537 | return false; |
| 538 | } |
| 539 | return copy_pixels_to(result.pixmap(), dst, dstSize, dstRB, preserveDstPad); |
| 540 | } |
| 541 | |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 542 | /////////////////////////////////////////////////////////////////////////////// |
| 543 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 544 | bool SkBitmap::isImmutable() const { |
scroggo | 0847059 | 2014-07-15 19:56:48 -0700 | [diff] [blame] | 545 | return fPixelRef ? fPixelRef->isImmutable() : false; |
junov@chromium.org | b052129 | 2011-12-15 20:14:06 +0000 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | void SkBitmap::setImmutable() { |
| 549 | if (fPixelRef) { |
| 550 | fPixelRef->setImmutable(); |
junov@chromium.org | b052129 | 2011-12-15 20:14:06 +0000 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 554 | bool SkBitmap::isVolatile() const { |
| 555 | return (fFlags & kImageIsVolatile_Flag) != 0; |
| 556 | } |
| 557 | |
| 558 | void SkBitmap::setIsVolatile(bool isVolatile) { |
| 559 | if (isVolatile) { |
| 560 | fFlags |= kImageIsVolatile_Flag; |
| 561 | } else { |
| 562 | fFlags &= ~kImageIsVolatile_Flag; |
| 563 | } |
| 564 | } |
| 565 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 566 | void* SkBitmap::getAddr(int x, int y) const { |
| 567 | SkASSERT((unsigned)x < (unsigned)this->width()); |
| 568 | SkASSERT((unsigned)y < (unsigned)this->height()); |
| 569 | |
| 570 | char* base = (char*)this->getPixels(); |
| 571 | if (base) { |
| 572 | base += y * this->rowBytes(); |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 573 | switch (this->colorType()) { |
mtklein | 7fd93e3 | 2016-07-26 13:05:30 -0700 | [diff] [blame] | 574 | case kRGBA_F16_SkColorType: |
| 575 | base += x << 3; |
| 576 | break; |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 577 | case kRGBA_8888_SkColorType: |
| 578 | case kBGRA_8888_SkColorType: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 579 | base += x << 2; |
| 580 | break; |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 581 | case kARGB_4444_SkColorType: |
| 582 | case kRGB_565_SkColorType: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 583 | base += x << 1; |
| 584 | break; |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 585 | case kAlpha_8_SkColorType: |
| 586 | case kIndex_8_SkColorType: |
reed | 0c9b1a8 | 2015-03-17 17:44:06 -0700 | [diff] [blame] | 587 | case kGray_8_SkColorType: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 588 | base += x; |
| 589 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 590 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 591 | SkDEBUGFAIL("Can't return addr for config"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 592 | base = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 593 | break; |
| 594 | } |
| 595 | } |
| 596 | return base; |
| 597 | } |
| 598 | |
| 599 | /////////////////////////////////////////////////////////////////////////////// |
| 600 | /////////////////////////////////////////////////////////////////////////////// |
| 601 | |
reed | 7aefe03 | 2015-06-08 10:22:22 -0700 | [diff] [blame] | 602 | void SkBitmap::erase(SkColor c, const SkIRect& area) const { |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 603 | SkDEBUGCODE(this->validate();) |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 604 | |
| 605 | switch (fInfo.colorType()) { |
| 606 | case kUnknown_SkColorType: |
| 607 | case kIndex_8_SkColorType: |
| 608 | // TODO: can we ASSERT that we never get here? |
| 609 | return; // can't erase. Should we bzero so the memory is not uninitialized? |
| 610 | default: |
| 611 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 612 | } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 613 | |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 614 | SkAutoPixmapUnlock result; |
| 615 | if (!this->requestLock(&result)) { |
| 616 | return; |
| 617 | } |
| 618 | |
reed | 7aefe03 | 2015-06-08 10:22:22 -0700 | [diff] [blame] | 619 | if (result.pixmap().erase(c, area)) { |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 620 | this->notifyPixelsChanged(); |
| 621 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 622 | } |
| 623 | |
reed | 7aefe03 | 2015-06-08 10:22:22 -0700 | [diff] [blame] | 624 | void SkBitmap::eraseColor(SkColor c) const { |
| 625 | this->erase(c, SkIRect::MakeWH(this->width(), this->height())); |
reed@google.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 626 | } |
| 627 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 628 | ////////////////////////////////////////////////////////////////////////////////////// |
| 629 | ////////////////////////////////////////////////////////////////////////////////////// |
| 630 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 631 | bool SkBitmap::extractSubset(SkBitmap* result, const SkIRect& subset) const { |
| 632 | SkDEBUGCODE(this->validate();) |
| 633 | |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 634 | if (nullptr == result || !fPixelRef) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 635 | return false; // no src pixels |
| 636 | } |
| 637 | |
| 638 | SkIRect srcRect, r; |
| 639 | srcRect.set(0, 0, this->width(), this->height()); |
| 640 | if (!r.intersect(srcRect, subset)) { |
| 641 | return false; // r is empty (i.e. no intersection) |
| 642 | } |
| 643 | |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 644 | // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have |
| 645 | // exited above. |
| 646 | SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width())); |
| 647 | SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height())); |
| 648 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 649 | SkBitmap dst; |
herb | b5d7468 | 2016-04-21 08:45:39 -0700 | [diff] [blame] | 650 | dst.setInfo(this->info().makeWH(r.width(), r.height()), this->rowBytes()); |
skyostil@google.com | 0eb7576 | 2012-01-16 10:45:53 +0000 | [diff] [blame] | 651 | dst.setIsVolatile(this->isVolatile()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 652 | |
| 653 | if (fPixelRef) { |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 654 | SkIPoint origin = fPixelRefOrigin; |
| 655 | origin.fX += r.fLeft; |
| 656 | origin.fY += r.fTop; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 657 | // share the pixelref with a custom offset |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 658 | dst.setPixelRef(fPixelRef, origin.x(), origin.y()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 659 | } |
| 660 | SkDEBUGCODE(dst.validate();) |
| 661 | |
| 662 | // we know we're good, so commit to result |
| 663 | result->swap(dst); |
| 664 | return true; |
| 665 | } |
| 666 | |
| 667 | /////////////////////////////////////////////////////////////////////////////// |
| 668 | |
Mike Reed | 22f3482 | 2016-11-28 17:17:38 -0500 | [diff] [blame] | 669 | bool SkBitmap::canCopyTo(SkColorType dstCT) const { |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 670 | const SkColorType srcCT = this->colorType(); |
| 671 | |
| 672 | if (srcCT == kUnknown_SkColorType) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 673 | return false; |
| 674 | } |
Mike Reed | 22f3482 | 2016-11-28 17:17:38 -0500 | [diff] [blame] | 675 | if (srcCT == kAlpha_8_SkColorType && dstCT != kAlpha_8_SkColorType) { |
| 676 | return false; // can't convert from alpha to non-alpha |
| 677 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 678 | |
Mike Reed | 22f3482 | 2016-11-28 17:17:38 -0500 | [diff] [blame] | 679 | bool sameConfigs = (srcCT == dstCT); |
| 680 | switch (dstCT) { |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 681 | case kAlpha_8_SkColorType: |
| 682 | case kRGB_565_SkColorType: |
commit-bot@chromium.org | 60b5dce | 2014-04-22 20:24:33 +0000 | [diff] [blame] | 683 | case kRGBA_8888_SkColorType: |
| 684 | case kBGRA_8888_SkColorType: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 685 | break; |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 686 | case kGray_8_SkColorType: |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 687 | if (!sameConfigs) { |
| 688 | return false; |
| 689 | } |
| 690 | break; |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 691 | case kARGB_4444_SkColorType: |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 692 | return sameConfigs || kN32_SkColorType == srcCT || kIndex_8_SkColorType == srcCT; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 693 | default: |
| 694 | return false; |
| 695 | } |
reed@android.com | fbaa88d | 2009-05-06 17:44:34 +0000 | [diff] [blame] | 696 | return true; |
| 697 | } |
| 698 | |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 699 | bool SkBitmap::readPixels(const SkImageInfo& requestedDstInfo, void* dstPixels, size_t dstRB, |
| 700 | int x, int y) const { |
reed | 95d343f | 2015-05-23 13:21:06 -0700 | [diff] [blame] | 701 | SkAutoPixmapUnlock src; |
| 702 | if (!this->requestLock(&src)) { |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 703 | return false; |
| 704 | } |
reed | 95d343f | 2015-05-23 13:21:06 -0700 | [diff] [blame] | 705 | return src.pixmap().readPixels(requestedDstInfo, dstPixels, dstRB, x, y); |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 706 | } |
| 707 | |
Mike Reed | 68dd8d0 | 2017-01-04 16:34:31 -0500 | [diff] [blame] | 708 | bool SkBitmap::readPixels(const SkPixmap& dst, int srcX, int srcY) const { |
| 709 | return this->readPixels(dst.info(), dst.writable_addr(), dst.rowBytes(), srcX, srcY); |
| 710 | } |
| 711 | |
| 712 | bool SkBitmap::writePixels(const SkPixmap& src, int dstX, int dstY) { |
| 713 | SkAutoPixmapUnlock dst; |
| 714 | if (!this->requestLock(&dst)) { |
| 715 | return false; |
| 716 | } |
| 717 | |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 718 | if (!SkImageInfoValidConversion(fInfo, src.info())) { |
Mike Reed | 68dd8d0 | 2017-01-04 16:34:31 -0500 | [diff] [blame] | 719 | return false; |
| 720 | } |
| 721 | |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 722 | SkWritePixelsRec rec(src.info(), src.addr(), src.rowBytes(), dstX, dstY); |
| 723 | if (!rec.trim(fInfo.width(), fInfo.height())) { |
| 724 | return false; |
| 725 | } |
| 726 | |
| 727 | void* dstPixels = this->getAddr(rec.fX, rec.fY); |
| 728 | const SkImageInfo dstInfo = fInfo.makeWH(rec.fInfo.width(), rec.fInfo.height()); |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame^] | 729 | SkPixelInfo::CopyPixels(dstInfo, dstPixels, this->rowBytes(), rec.fInfo, rec.fPixels, |
| 730 | rec.fRowBytes, src.ctable()); |
| 731 | return true; |
Mike Reed | 68dd8d0 | 2017-01-04 16:34:31 -0500 | [diff] [blame] | 732 | } |
| 733 | |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 734 | bool SkBitmap::copyTo(SkBitmap* dst, SkColorType dstColorType, Allocator* alloc) const { |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 735 | if (!this->canCopyTo(dstColorType)) { |
reed@android.com | fbaa88d | 2009-05-06 17:44:34 +0000 | [diff] [blame] | 736 | return false; |
| 737 | } |
| 738 | |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 739 | // if we have a texture, first get those pixels |
| 740 | SkBitmap tmpSrc; |
| 741 | const SkBitmap* src = this; |
| 742 | |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 743 | if (fPixelRef) { |
| 744 | SkIRect subset; |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 745 | subset.setXYWH(fPixelRefOrigin.fX, fPixelRefOrigin.fY, |
| 746 | fInfo.width(), fInfo.height()); |
bsalomon | 9d22fd6 | 2016-01-11 11:14:17 -0800 | [diff] [blame] | 747 | if (fPixelRef->readPixels(&tmpSrc, dstColorType, &subset)) { |
scroggo | 0187dc2 | 2014-06-05 11:18:04 -0700 | [diff] [blame] | 748 | if (fPixelRef->info().alphaType() == kUnpremul_SkAlphaType) { |
| 749 | // FIXME: The only meaningful implementation of readPixels |
| 750 | // (GrPixelRef) assumes premultiplied pixels. |
| 751 | return false; |
| 752 | } |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 753 | SkASSERT(tmpSrc.width() == this->width()); |
| 754 | SkASSERT(tmpSrc.height() == this->height()); |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 755 | |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 756 | // did we get lucky and we can just return tmpSrc? |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 757 | if (tmpSrc.colorType() == dstColorType && nullptr == alloc) { |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 758 | dst->swap(tmpSrc); |
scroggo@google.com | 5ccae2c | 2014-01-15 16:56:52 +0000 | [diff] [blame] | 759 | // If the result is an exact copy, clone the gen ID. |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 760 | SkPixelRef* dstPixelRef = dst->pixelRef(); |
| 761 | if (!dstPixelRef && dstPixelRef->info() == fPixelRef->info()) { |
| 762 | dstPixelRef->cloneGenID(*fPixelRef); |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 763 | } |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 764 | return true; |
scroggo@google.com | d5764e8 | 2012-08-22 15:00:05 +0000 | [diff] [blame] | 765 | } |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 766 | |
| 767 | // fall through to the raster case |
| 768 | src = &tmpSrc; |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 769 | } |
reed@android.com | fbaa88d | 2009-05-06 17:44:34 +0000 | [diff] [blame] | 770 | } |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 771 | |
reed | 95d343f | 2015-05-23 13:21:06 -0700 | [diff] [blame] | 772 | SkAutoPixmapUnlock srcUnlocker; |
| 773 | if (!src->requestLock(&srcUnlocker)) { |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 774 | return false; |
| 775 | } |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 776 | SkPixmap srcPM = srcUnlocker.pixmap(); |
| 777 | if (kRGB_565_SkColorType == dstColorType && kOpaque_SkAlphaType != srcPM.alphaType()) { |
| 778 | // copyTo() is not strict on alpha type. Here we set the src to opaque to allow |
| 779 | // the call to readPixels() to succeed and preserve this lenient behavior. |
| 780 | srcPM = SkPixmap(srcPM.info().makeAlphaType(kOpaque_SkAlphaType), srcPM.addr(), |
| 781 | srcPM.rowBytes(), srcPM.ctable()); |
| 782 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 783 | |
reed | 95d343f | 2015-05-23 13:21:06 -0700 | [diff] [blame] | 784 | const SkImageInfo dstInfo = srcPM.info().makeColorType(dstColorType); |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 785 | SkBitmap tmpDst; |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 786 | if (!tmpDst.setInfo(dstInfo)) { |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 787 | return false; |
| 788 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 789 | |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 790 | // allocate colortable if srcConfig == kIndex8_Config |
Hal Canary | 704cd32 | 2016-11-07 14:13:52 -0500 | [diff] [blame] | 791 | sk_sp<SkColorTable> ctable; |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 792 | if (dstColorType == kIndex_8_SkColorType) { |
reed | 95d343f | 2015-05-23 13:21:06 -0700 | [diff] [blame] | 793 | ctable.reset(SkRef(srcPM.ctable())); |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 794 | } |
Hal Canary | 704cd32 | 2016-11-07 14:13:52 -0500 | [diff] [blame] | 795 | if (!tmpDst.tryAllocPixels(alloc, ctable.get())) { |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 796 | return false; |
| 797 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 798 | |
reed | 95d343f | 2015-05-23 13:21:06 -0700 | [diff] [blame] | 799 | SkAutoPixmapUnlock dstUnlocker; |
| 800 | if (!tmpDst.requestLock(&dstUnlocker)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 801 | return false; |
| 802 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 803 | |
reed | 95d343f | 2015-05-23 13:21:06 -0700 | [diff] [blame] | 804 | if (!srcPM.readPixels(dstUnlocker.pixmap())) { |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 805 | return false; |
| 806 | } |
scroggo@google.com | 5ccae2c | 2014-01-15 16:56:52 +0000 | [diff] [blame] | 807 | |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 808 | // (for BitmapHeap) Clone the pixelref genID even though we have a new pixelref. |
| 809 | // The old copyTo impl did this, so we continue it for now. |
| 810 | // |
| 811 | // TODO: should we ignore rowbytes (i.e. getSize)? Then it could just be |
| 812 | // if (src_pixelref->info == dst_pixelref->info) |
| 813 | // |
reed | 95d343f | 2015-05-23 13:21:06 -0700 | [diff] [blame] | 814 | if (srcPM.colorType() == dstColorType && tmpDst.getSize() == srcPM.getSize64()) { |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 815 | SkPixelRef* dstPixelRef = tmpDst.pixelRef(); |
| 816 | if (dstPixelRef->info() == fPixelRef->info()) { |
| 817 | dstPixelRef->cloneGenID(*fPixelRef); |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 818 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 819 | } |
| 820 | |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 821 | dst->swap(tmpDst); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 822 | return true; |
| 823 | } |
| 824 | |
reed | c7ec7c9 | 2016-07-25 08:29:10 -0700 | [diff] [blame] | 825 | // TODO: can we merge this with copyTo? |
commit-bot@chromium.org | fab349c | 2014-03-05 02:34:58 +0000 | [diff] [blame] | 826 | bool SkBitmap::deepCopyTo(SkBitmap* dst) const { |
reed | e4538f5 | 2014-06-11 06:09:50 -0700 | [diff] [blame] | 827 | const SkColorType dstCT = this->colorType(); |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 828 | |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 829 | if (!this->canCopyTo(dstCT)) { |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 830 | return false; |
| 831 | } |
reed | c7ec7c9 | 2016-07-25 08:29:10 -0700 | [diff] [blame] | 832 | return this->copyTo(dst, dstCT, nullptr); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 833 | } |
| 834 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 835 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 836 | |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 837 | static bool GetBitmapAlpha(const SkBitmap& src, uint8_t* SK_RESTRICT alpha, int alphaRowBytes) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 838 | SkASSERT(alpha != nullptr); |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 839 | SkASSERT(alphaRowBytes >= src.width()); |
| 840 | |
| 841 | SkAutoPixmapUnlock apl; |
| 842 | if (!src.requestLock(&apl)) { |
lsalzman | a2415ac | 2016-10-11 14:29:12 -0700 | [diff] [blame] | 843 | for (int y = 0; y < src.height(); ++y) { |
| 844 | memset(alpha, 0, src.width()); |
| 845 | alpha += alphaRowBytes; |
| 846 | } |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 847 | return false; |
| 848 | } |
lsalzman | a2415ac | 2016-10-11 14:29:12 -0700 | [diff] [blame] | 849 | const SkPixmap& pmap = apl.pixmap(); |
| 850 | SkPixelInfo::CopyPixels(SkImageInfo::MakeA8(pmap.width(), pmap.height()), alpha, alphaRowBytes, |
| 851 | pmap.info(), pmap.addr(), pmap.rowBytes(), pmap.ctable()); |
reed@android.com | 1cdcb51 | 2009-08-24 19:11:00 +0000 | [diff] [blame] | 852 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | #include "SkPaint.h" |
| 856 | #include "SkMaskFilter.h" |
| 857 | #include "SkMatrix.h" |
| 858 | |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 859 | bool SkBitmap::extractAlpha(SkBitmap* dst, const SkPaint* paint, |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 860 | Allocator *allocator, SkIPoint* offset) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 861 | SkDEBUGCODE(this->validate();) |
| 862 | |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 863 | SkBitmap tmpBitmap; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 864 | SkMatrix identity; |
| 865 | SkMask srcM, dstM; |
| 866 | |
| 867 | srcM.fBounds.set(0, 0, this->width(), this->height()); |
| 868 | srcM.fRowBytes = SkAlign4(this->width()); |
| 869 | srcM.fFormat = SkMask::kA8_Format; |
| 870 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 871 | SkMaskFilter* filter = paint ? paint->getMaskFilter() : nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 872 | |
| 873 | // compute our (larger?) dst bounds if we have a filter |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 874 | if (filter) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 875 | identity.reset(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 876 | if (!filter->filterMask(&dstM, srcM, identity, nullptr)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 877 | goto NO_FILTER_CASE; |
| 878 | } |
| 879 | dstM.fRowBytes = SkAlign4(dstM.fBounds.width()); |
| 880 | } else { |
| 881 | NO_FILTER_CASE: |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 882 | tmpBitmap.setInfo(SkImageInfo::MakeA8(this->width(), this->height()), srcM.fRowBytes); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 883 | if (!tmpBitmap.tryAllocPixels(allocator, nullptr)) { |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 884 | // Allocation of pixels for alpha bitmap failed. |
| 885 | SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n", |
| 886 | tmpBitmap.width(), tmpBitmap.height()); |
| 887 | return false; |
| 888 | } |
| 889 | GetBitmapAlpha(*this, tmpBitmap.getAddr8(0, 0), srcM.fRowBytes); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 890 | if (offset) { |
| 891 | offset->set(0, 0); |
| 892 | } |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 893 | tmpBitmap.swap(*dst); |
| 894 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 895 | } |
bungeman@google.com | 02f5584 | 2011-10-04 21:25:00 +0000 | [diff] [blame] | 896 | srcM.fImage = SkMask::AllocImage(srcM.computeImageSize()); |
| 897 | SkAutoMaskFreeImage srcCleanup(srcM.fImage); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 898 | |
| 899 | GetBitmapAlpha(*this, srcM.fImage, srcM.fRowBytes); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 900 | if (!filter->filterMask(&dstM, srcM, identity, nullptr)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 901 | goto NO_FILTER_CASE; |
| 902 | } |
bungeman@google.com | 02f5584 | 2011-10-04 21:25:00 +0000 | [diff] [blame] | 903 | SkAutoMaskFreeImage dstCleanup(dstM.fImage); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 904 | |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 905 | tmpBitmap.setInfo(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.height()), |
| 906 | dstM.fRowBytes); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 907 | if (!tmpBitmap.tryAllocPixels(allocator, nullptr)) { |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 908 | // Allocation of pixels for alpha bitmap failed. |
| 909 | SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n", |
| 910 | tmpBitmap.width(), tmpBitmap.height()); |
| 911 | return false; |
| 912 | } |
| 913 | memcpy(tmpBitmap.getPixels(), dstM.fImage, dstM.computeImageSize()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 914 | if (offset) { |
| 915 | offset->set(dstM.fBounds.fLeft, dstM.fBounds.fTop); |
| 916 | } |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 917 | SkDEBUGCODE(tmpBitmap.validate();) |
| 918 | |
| 919 | tmpBitmap.swap(*dst); |
| 920 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | /////////////////////////////////////////////////////////////////////////////// |
| 924 | |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 925 | static void write_raw_pixels(SkWriteBuffer* buffer, const SkPixmap& pmap) { |
| 926 | const SkImageInfo& info = pmap.info(); |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 927 | const size_t snugRB = info.width() * info.bytesPerPixel(); |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 928 | const char* src = (const char*)pmap.addr(); |
| 929 | const size_t ramRB = pmap.rowBytes(); |
skia.committer@gmail.com | 3c134a9 | 2014-05-24 03:05:26 +0000 | [diff] [blame] | 930 | |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 931 | buffer->write32(SkToU32(snugRB)); |
| 932 | info.flatten(*buffer); |
| 933 | |
| 934 | const size_t size = snugRB * info.height(); |
scroggo | 565901d | 2015-12-10 10:44:13 -0800 | [diff] [blame] | 935 | SkAutoTMalloc<char> storage(size); |
| 936 | char* dst = storage.get(); |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 937 | for (int y = 0; y < info.height(); ++y) { |
| 938 | memcpy(dst, src, snugRB); |
| 939 | dst += snugRB; |
| 940 | src += ramRB; |
| 941 | } |
| 942 | buffer->writeByteArray(storage.get(), size); |
| 943 | |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 944 | const SkColorTable* ct = pmap.ctable(); |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 945 | if (kIndex_8_SkColorType == info.colorType() && ct) { |
| 946 | buffer->writeBool(true); |
| 947 | ct->writeToBuffer(*buffer); |
| 948 | } else { |
| 949 | buffer->writeBool(false); |
| 950 | } |
| 951 | } |
| 952 | |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 953 | void SkBitmap::WriteRawPixels(SkWriteBuffer* buffer, const SkBitmap& bitmap) { |
| 954 | const SkImageInfo info = bitmap.info(); |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 955 | if (0 == info.width() || 0 == info.height() || bitmap.isNull()) { |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 956 | buffer->writeUInt(0); // instead of snugRB, signaling no pixels |
| 957 | return; |
| 958 | } |
| 959 | |
| 960 | SkAutoPixmapUnlock result; |
| 961 | if (!bitmap.requestLock(&result)) { |
| 962 | buffer->writeUInt(0); // instead of snugRB, signaling no pixels |
| 963 | return; |
| 964 | } |
| 965 | |
| 966 | write_raw_pixels(buffer, result.pixmap()); |
| 967 | } |
| 968 | |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 969 | bool SkBitmap::ReadRawPixels(SkReadBuffer* buffer, SkBitmap* bitmap) { |
| 970 | const size_t snugRB = buffer->readUInt(); |
| 971 | if (0 == snugRB) { // no pixels |
| 972 | return false; |
| 973 | } |
| 974 | |
| 975 | SkImageInfo info; |
| 976 | info.unflatten(*buffer); |
| 977 | |
Robert Phillips | b2cb535 | 2016-12-20 12:44:41 -0500 | [diff] [blame] | 978 | if (info.width() < 0 || info.height() < 0) { |
| 979 | return false; |
| 980 | } |
| 981 | |
mtklein | 58e389b | 2016-07-15 07:00:11 -0700 | [diff] [blame] | 982 | // If there was an error reading "info" or if it is bogus, |
robertphillips | 74139f1 | 2016-06-28 09:04:34 -0700 | [diff] [blame] | 983 | // don't use it to compute minRowBytes() |
| 984 | if (!buffer->validate(SkColorTypeValidateAlphaType(info.colorType(), |
| 985 | info.alphaType()))) { |
sugoi | ca95c19 | 2014-07-08 09:18:48 -0700 | [diff] [blame] | 986 | return false; |
| 987 | } |
| 988 | |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 989 | const size_t ramRB = info.minRowBytes(); |
sugoi | bd0d9da | 2015-01-07 08:47:44 -0800 | [diff] [blame] | 990 | const int height = SkMax32(info.height(), 0); |
| 991 | const uint64_t snugSize = sk_64_mul(snugRB, height); |
| 992 | const uint64_t ramSize = sk_64_mul(ramRB, height); |
| 993 | static const uint64_t max_size_t = (size_t)(-1); |
| 994 | if (!buffer->validate((snugSize <= ramSize) && (ramSize <= max_size_t))) { |
commit-bot@chromium.org | 0585843 | 2014-05-30 01:06:44 +0000 | [diff] [blame] | 995 | return false; |
| 996 | } |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 997 | |
reed | fde0511 | 2016-03-11 13:02:28 -0800 | [diff] [blame] | 998 | sk_sp<SkData> data(SkData::MakeUninitialized(SkToSizeT(ramSize))); |
robertphillips | 2893784 | 2015-06-08 07:10:49 -0700 | [diff] [blame] | 999 | unsigned char* dst = (unsigned char*)data->writable_data(); |
sugoi | bd0d9da | 2015-01-07 08:47:44 -0800 | [diff] [blame] | 1000 | buffer->readByteArray(dst, SkToSizeT(snugSize)); |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 1001 | |
| 1002 | if (snugSize != ramSize) { |
robertphillips | 2893784 | 2015-06-08 07:10:49 -0700 | [diff] [blame] | 1003 | const unsigned char* srcRow = dst + snugRB * (height - 1); |
| 1004 | unsigned char* dstRow = dst + ramRB * (height - 1); |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 1005 | for (int y = height - 1; y >= 1; --y) { |
| 1006 | memmove(dstRow, srcRow, snugRB); |
| 1007 | srcRow -= snugRB; |
| 1008 | dstRow -= ramRB; |
| 1009 | } |
| 1010 | SkASSERT(srcRow == dstRow); // first row does not need to be moved |
| 1011 | } |
skia.committer@gmail.com | 3c134a9 | 2014-05-24 03:05:26 +0000 | [diff] [blame] | 1012 | |
Hal Canary | 704cd32 | 2016-11-07 14:13:52 -0500 | [diff] [blame] | 1013 | sk_sp<SkColorTable> ctable; |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 1014 | if (buffer->readBool()) { |
reed | b236d1a | 2015-08-28 10:14:18 -0700 | [diff] [blame] | 1015 | ctable.reset(SkColorTable::Create(*buffer)); |
| 1016 | if (!ctable) { |
| 1017 | return false; |
| 1018 | } |
robertphillips | 2893784 | 2015-06-08 07:10:49 -0700 | [diff] [blame] | 1019 | |
reed | b236d1a | 2015-08-28 10:14:18 -0700 | [diff] [blame] | 1020 | if (info.isEmpty()) { |
| 1021 | // require an empty ctable |
| 1022 | if (ctable->count() != 0) { |
| 1023 | buffer->validate(false); |
| 1024 | return false; |
| 1025 | } |
| 1026 | } else { |
| 1027 | // require a non-empty ctable |
| 1028 | if (ctable->count() == 0) { |
| 1029 | buffer->validate(false); |
| 1030 | return false; |
| 1031 | } |
| 1032 | unsigned char maxIndex = ctable->count() - 1; |
| 1033 | for (uint64_t i = 0; i < ramSize; ++i) { |
| 1034 | dst[i] = SkTMin(dst[i], maxIndex); |
| 1035 | } |
robertphillips | 2893784 | 2015-06-08 07:10:49 -0700 | [diff] [blame] | 1036 | } |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 1037 | } |
skia.committer@gmail.com | 3c134a9 | 2014-05-24 03:05:26 +0000 | [diff] [blame] | 1038 | |
Hal Canary | 704cd32 | 2016-11-07 14:13:52 -0500 | [diff] [blame] | 1039 | sk_sp<SkPixelRef> pr(SkMallocPixelRef::NewWithData(info, info.minRowBytes(), |
| 1040 | ctable.get(), data.get())); |
sugoi | 6af3147 | 2015-01-28 13:15:32 -0800 | [diff] [blame] | 1041 | if (!pr.get()) { |
| 1042 | return false; |
| 1043 | } |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 1044 | bitmap->setInfo(pr->info()); |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 1045 | bitmap->setPixelRef(std::move(pr), 0, 0); |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 1046 | return true; |
| 1047 | } |
| 1048 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1049 | enum { |
| 1050 | SERIALIZE_PIXELTYPE_NONE, |
djsollen@google.com | 21830d9 | 2012-08-07 19:49:41 +0000 | [diff] [blame] | 1051 | SERIALIZE_PIXELTYPE_REF_DATA |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1052 | }; |
| 1053 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1054 | /////////////////////////////////////////////////////////////////////////////// |
| 1055 | |
| 1056 | SkBitmap::RLEPixels::RLEPixels(int width, int height) { |
| 1057 | fHeight = height; |
commit-bot@chromium.org | 235002f | 2013-10-09 18:39:59 +0000 | [diff] [blame] | 1058 | fYPtrs = (uint8_t**)sk_calloc_throw(height * sizeof(uint8_t*)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | SkBitmap::RLEPixels::~RLEPixels() { |
| 1062 | sk_free(fYPtrs); |
| 1063 | } |
| 1064 | |
| 1065 | /////////////////////////////////////////////////////////////////////////////// |
| 1066 | |
| 1067 | #ifdef SK_DEBUG |
| 1068 | void SkBitmap::validate() const { |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 1069 | fInfo.validate(); |
commit-bot@chromium.org | d5414e5 | 2014-02-13 22:30:38 +0000 | [diff] [blame] | 1070 | |
| 1071 | // ImageInfo may not require this, but Bitmap ensures that opaque-only |
| 1072 | // colorTypes report opaque for their alphatype |
| 1073 | if (kRGB_565_SkColorType == fInfo.colorType()) { |
| 1074 | SkASSERT(kOpaque_SkAlphaType == fInfo.alphaType()); |
| 1075 | } |
| 1076 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 1077 | SkASSERT(fInfo.validRowBytes(fRowBytes)); |
scroggo | 0847059 | 2014-07-15 19:56:48 -0700 | [diff] [blame] | 1078 | uint8_t allFlags = kImageIsVolatile_Flag; |
scroggo@google.com | 8e990eb | 2013-06-14 15:55:56 +0000 | [diff] [blame] | 1079 | #ifdef SK_BUILD_FOR_ANDROID |
| 1080 | allFlags |= kHasHardwareMipMap_Flag; |
| 1081 | #endif |
scroggo | 0847059 | 2014-07-15 19:56:48 -0700 | [diff] [blame] | 1082 | SkASSERT((~allFlags & fFlags) == 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1083 | SkASSERT(fPixelLockCount >= 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1084 | |
reed@google.com | 615316c | 2014-01-15 19:15:23 +0000 | [diff] [blame] | 1085 | if (fPixels) { |
| 1086 | SkASSERT(fPixelRef); |
robertphillips | e77dadd | 2014-11-21 05:50:21 -0800 | [diff] [blame] | 1087 | SkASSERT(fPixelLockCount > 0); |
reed@google.com | 615316c | 2014-01-15 19:15:23 +0000 | [diff] [blame] | 1088 | SkASSERT(fPixelRef->isLocked()); |
| 1089 | SkASSERT(fPixelRef->rowBytes() == fRowBytes); |
| 1090 | SkASSERT(fPixelRefOrigin.fX >= 0); |
| 1091 | SkASSERT(fPixelRefOrigin.fY >= 0); |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 1092 | SkASSERT(fPixelRef->info().width() >= (int)this->width() + fPixelRefOrigin.fX); |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 1093 | SkASSERT(fPixelRef->info().height() >= (int)this->height() + fPixelRefOrigin.fY); |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 1094 | SkASSERT(fPixelRef->rowBytes() >= fInfo.minRowBytes()); |
reed@google.com | 615316c | 2014-01-15 19:15:23 +0000 | [diff] [blame] | 1095 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1096 | SkASSERT(nullptr == fColorTable); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1097 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1098 | } |
| 1099 | #endif |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 1100 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 1101 | #ifndef SK_IGNORE_TO_STRING |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 1102 | #include "SkString.h" |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 1103 | void SkBitmap::toString(SkString* str) const { |
| 1104 | |
commit-bot@chromium.org | cba7378 | 2014-05-29 15:57:47 +0000 | [diff] [blame] | 1105 | static const char* gColorTypeNames[kLastEnum_SkColorType + 1] = { |
| 1106 | "UNKNOWN", "A8", "565", "4444", "RGBA", "BGRA", "INDEX8", |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 1107 | }; |
| 1108 | |
| 1109 | str->appendf("bitmap: ((%d, %d) %s", this->width(), this->height(), |
commit-bot@chromium.org | cba7378 | 2014-05-29 15:57:47 +0000 | [diff] [blame] | 1110 | gColorTypeNames[this->colorType()]); |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 1111 | |
| 1112 | str->append(" ("); |
| 1113 | if (this->isOpaque()) { |
| 1114 | str->append("opaque"); |
| 1115 | } else { |
| 1116 | str->append("transparent"); |
| 1117 | } |
| 1118 | if (this->isImmutable()) { |
| 1119 | str->append(", immutable"); |
| 1120 | } else { |
| 1121 | str->append(", not-immutable"); |
| 1122 | } |
| 1123 | str->append(")"); |
| 1124 | |
| 1125 | SkPixelRef* pr = this->pixelRef(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1126 | if (nullptr == pr) { |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 1127 | // show null or the explicit pixel address (rare) |
| 1128 | str->appendf(" pixels:%p", this->getPixels()); |
| 1129 | } else { |
| 1130 | const char* uri = pr->getURI(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1131 | if (uri) { |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 1132 | str->appendf(" uri:\"%s\"", uri); |
| 1133 | } else { |
| 1134 | str->appendf(" pixelref:%p", pr); |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | str->append(")"); |
| 1139 | } |
| 1140 | #endif |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 1141 | |
| 1142 | /////////////////////////////////////////////////////////////////////////////// |
| 1143 | |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 1144 | bool SkBitmap::requestLock(SkAutoPixmapUnlock* result) const { |
| 1145 | SkASSERT(result); |
| 1146 | |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 1147 | SkPixelRef* pr = fPixelRef.get(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1148 | if (nullptr == pr) { |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 1149 | return false; |
| 1150 | } |
| 1151 | |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 1152 | // We have to lock the whole thing (using the pixelref's dimensions) until the api supports |
| 1153 | // a partial lock (with offset/origin). Hence we can't use our fInfo. |
| 1154 | SkPixelRef::LockRequest req = { pr->info().dimensions(), kNone_SkFilterQuality }; |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 1155 | SkPixelRef::LockResult res; |
| 1156 | if (pr->requestLock(req, &res)) { |
reed | e800657 | 2015-05-28 14:06:06 -0700 | [diff] [blame] | 1157 | SkASSERT(res.fPixels); |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 1158 | // The bitmap may be a subset of the pixelref's dimensions |
| 1159 | SkASSERT(fPixelRefOrigin.x() + fInfo.width() <= res.fSize.width()); |
| 1160 | SkASSERT(fPixelRefOrigin.y() + fInfo.height() <= res.fSize.height()); |
| 1161 | const void* addr = (const char*)res.fPixels + SkColorTypeComputeOffset(fInfo.colorType(), |
| 1162 | fPixelRefOrigin.x(), |
| 1163 | fPixelRefOrigin.y(), |
| 1164 | res.fRowBytes); |
| 1165 | |
| 1166 | result->reset(SkPixmap(this->info(), addr, res.fRowBytes, res.fCTable), |
| 1167 | res.fUnlockProc, res.fUnlockContext); |
| 1168 | return true; |
| 1169 | } |
| 1170 | return false; |
| 1171 | } |
| 1172 | |
reed | cb67414 | 2015-06-05 06:58:22 -0700 | [diff] [blame] | 1173 | bool SkBitmap::peekPixels(SkPixmap* pmap) const { |
| 1174 | if (fPixels) { |
| 1175 | if (pmap) { |
| 1176 | pmap->reset(fInfo, fPixels, fRowBytes, fColorTable); |
| 1177 | } |
| 1178 | return true; |
| 1179 | } |
| 1180 | return false; |
| 1181 | } |
| 1182 | |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 1183 | /////////////////////////////////////////////////////////////////////////////// |
| 1184 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 1185 | #ifdef SK_DEBUG |
| 1186 | void SkImageInfo::validate() const { |
| 1187 | SkASSERT(fWidth >= 0); |
| 1188 | SkASSERT(fHeight >= 0); |
| 1189 | SkASSERT(SkColorTypeIsValid(fColorType)); |
| 1190 | SkASSERT(SkAlphaTypeIsValid(fAlphaType)); |
| 1191 | } |
| 1192 | #endif |