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" |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 11 | #include "SkColorTable.h" |
Matt Sarett | 485c499 | 2017-02-14 14:18:27 -0500 | [diff] [blame] | 12 | #include "SkConvertPixels.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 13 | #include "SkData.h" |
| 14 | #include "SkFilterQuality.h" |
Hal Canary | 4cba3fe | 2016-12-07 14:59:27 -0500 | [diff] [blame] | 15 | #include "SkHalf.h" |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 16 | #include "SkImageInfoPriv.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | #include "SkMallocPixelRef.h" |
| 18 | #include "SkMask.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 19 | #include "SkMath.h" |
jvanverth | 02802f6 | 2015-07-02 06:42:49 -0700 | [diff] [blame] | 20 | #include "SkPixelRef.h" |
mtklein | 1b24933 | 2015-07-07 12:21:21 -0700 | [diff] [blame] | 21 | #include "SkReadBuffer.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 22 | #include "SkRect.h" |
| 23 | #include "SkScalar.h" |
scroggo | 565901d | 2015-12-10 10:44:13 -0800 | [diff] [blame] | 24 | #include "SkTemplates.h" |
vandebo@chromium.org | 112706d | 2011-02-24 22:50:55 +0000 | [diff] [blame] | 25 | #include "SkUnPreMultiply.h" |
mtklein | 1b24933 | 2015-07-07 12:21:21 -0700 | [diff] [blame] | 26 | #include "SkWriteBuffer.h" |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 27 | #include "SkWritePixelsRec.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 28 | |
| 29 | #include <string.h> |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 30 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 31 | static bool reset_return_false(SkBitmap* bm) { |
| 32 | bm->reset(); |
| 33 | return false; |
| 34 | } |
| 35 | |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 36 | SkBitmap::SkBitmap() |
Mike Reed | b712089 | 2017-04-14 17:16:36 -0400 | [diff] [blame] | 37 | : fPixels (nullptr) |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 38 | , fPixelRefOrigin{0, 0} |
| 39 | , fRowBytes (0) |
| 40 | , fFlags (0) {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 41 | |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 42 | SkBitmap::SkBitmap(const SkBitmap& src) |
| 43 | : fPixelRef (src.fPixelRef) |
Mike Reed | b712089 | 2017-04-14 17:16:36 -0400 | [diff] [blame] | 44 | , fPixels (src.fPixels) |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 45 | , fPixelRefOrigin(src.fPixelRefOrigin) |
| 46 | , fInfo (src.fInfo) |
| 47 | , fRowBytes (src.fRowBytes) |
| 48 | , fFlags (src.fFlags) |
| 49 | { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 50 | SkDEBUGCODE(src.validate();) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 51 | SkDEBUGCODE(this->validate();) |
| 52 | } |
| 53 | |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 54 | SkBitmap::SkBitmap(SkBitmap&& other) |
| 55 | : fPixelRef (std::move(other.fPixelRef)) |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 56 | , fPixels (other.fPixels) |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 57 | , fPixelRefOrigin (other.fPixelRefOrigin) |
| 58 | , fInfo (std::move(other.fInfo)) |
| 59 | , fRowBytes (other.fRowBytes) |
Mike Reed | b712089 | 2017-04-14 17:16:36 -0400 | [diff] [blame] | 60 | , fFlags (other.fFlags) |
| 61 | { |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 62 | SkASSERT(!other.fPixelRef); |
| 63 | other.fInfo.reset(); |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 64 | other.fPixels = nullptr; |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 65 | other.fPixelRefOrigin = SkIPoint{0, 0}; |
| 66 | other.fRowBytes = 0; |
| 67 | other.fFlags = 0; |
| 68 | } |
halcanary | 023bda0 | 2015-12-14 10:19:17 -0800 | [diff] [blame] | 69 | |
Mike Reed | b712089 | 2017-04-14 17:16:36 -0400 | [diff] [blame] | 70 | SkBitmap::~SkBitmap() {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | |
| 72 | SkBitmap& SkBitmap::operator=(const SkBitmap& src) { |
| 73 | if (this != &src) { |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 74 | fPixelRef = src.fPixelRef; |
Mike Reed | b712089 | 2017-04-14 17:16:36 -0400 | [diff] [blame] | 75 | fPixels = src.fPixels; |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 76 | fPixelRefOrigin = src.fPixelRefOrigin; |
| 77 | fInfo = src.fInfo; |
| 78 | fRowBytes = src.fRowBytes; |
| 79 | fFlags = src.fFlags; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 81 | SkDEBUGCODE(this->validate();) |
| 82 | return *this; |
| 83 | } |
| 84 | |
halcanary | 023bda0 | 2015-12-14 10:19:17 -0800 | [diff] [blame] | 85 | SkBitmap& SkBitmap::operator=(SkBitmap&& other) { |
| 86 | if (this != &other) { |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 87 | fPixelRef = std::move(other.fPixelRef); |
| 88 | fInfo = std::move(other.fInfo); |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 89 | fPixels = other.fPixels; |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 90 | fPixelRefOrigin = other.fPixelRefOrigin; |
| 91 | fRowBytes = other.fRowBytes; |
| 92 | fFlags = other.fFlags; |
| 93 | SkASSERT(!other.fPixelRef); |
| 94 | other.fInfo.reset(); |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 95 | other.fPixels = nullptr; |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 96 | other.fPixelRefOrigin = SkIPoint{0, 0}; |
| 97 | other.fRowBytes = 0; |
| 98 | other.fFlags = 0; |
halcanary | 023bda0 | 2015-12-14 10:19:17 -0800 | [diff] [blame] | 99 | } |
| 100 | return *this; |
| 101 | } |
| 102 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 103 | void SkBitmap::swap(SkBitmap& other) { |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 104 | SkTSwap(*this, other); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | SkDEBUGCODE(this->validate();) |
| 106 | } |
| 107 | |
| 108 | void SkBitmap::reset() { |
| 109 | this->freePixels(); |
msarett | 23c5110 | 2016-05-27 07:39:02 -0700 | [diff] [blame] | 110 | this->fInfo.reset(); |
reed@android.com | 4516f47 | 2009-06-29 16:25:36 +0000 | [diff] [blame] | 111 | sk_bzero(this, sizeof(*this)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 112 | } |
| 113 | |
reed@google.com | 86b2e43 | 2012-03-15 21:17:03 +0000 | [diff] [blame] | 114 | void SkBitmap::getBounds(SkRect* bounds) const { |
| 115 | SkASSERT(bounds); |
| 116 | bounds->set(0, 0, |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 117 | SkIntToScalar(fInfo.width()), SkIntToScalar(fInfo.height())); |
reed@google.com | 86b2e43 | 2012-03-15 21:17:03 +0000 | [diff] [blame] | 118 | } |
| 119 | |
reed@google.com | 80e1459 | 2012-03-16 14:58:07 +0000 | [diff] [blame] | 120 | void SkBitmap::getBounds(SkIRect* bounds) const { |
| 121 | SkASSERT(bounds); |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 122 | bounds->set(0, 0, fInfo.width(), fInfo.height()); |
reed@google.com | 80e1459 | 2012-03-16 14:58:07 +0000 | [diff] [blame] | 123 | } |
| 124 | |
reed@google.com | 86b2e43 | 2012-03-15 21:17:03 +0000 | [diff] [blame] | 125 | /////////////////////////////////////////////////////////////////////////////// |
| 126 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 127 | bool SkBitmap::setInfo(const SkImageInfo& info, size_t rowBytes) { |
| 128 | SkAlphaType newAT = info.alphaType(); |
| 129 | if (!SkColorTypeValidateAlphaType(info.colorType(), info.alphaType(), &newAT)) { |
commit-bot@chromium.org | d5414e5 | 2014-02-13 22:30:38 +0000 | [diff] [blame] | 130 | return reset_return_false(this); |
| 131 | } |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 132 | // 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] | 133 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 134 | // require that rowBytes fit in 31bits |
| 135 | int64_t mrb = info.minRowBytes64(); |
| 136 | if ((int32_t)mrb != mrb) { |
| 137 | return reset_return_false(this); |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 138 | } |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 139 | if ((int64_t)rowBytes != (int32_t)rowBytes) { |
| 140 | return reset_return_false(this); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 141 | } |
reed@android.com | 89bb83a | 2009-05-29 21:30:42 +0000 | [diff] [blame] | 142 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 143 | if (info.width() < 0 || info.height() < 0) { |
| 144 | return reset_return_false(this); |
| 145 | } |
| 146 | |
| 147 | if (kUnknown_SkColorType == info.colorType()) { |
| 148 | rowBytes = 0; |
| 149 | } else if (0 == rowBytes) { |
| 150 | rowBytes = (size_t)mrb; |
reed | f0aed97 | 2014-07-01 12:48:11 -0700 | [diff] [blame] | 151 | } else if (!info.validRowBytes(rowBytes)) { |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 152 | return reset_return_false(this); |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | this->freePixels(); |
| 156 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 157 | fInfo = info.makeAlphaType(newAT); |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 158 | fRowBytes = SkToU32(rowBytes); |
Mike Reed | b712089 | 2017-04-14 17:16:36 -0400 | [diff] [blame] | 159 | SkDEBUGCODE(this->validate();) |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 160 | return true; |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 161 | } |
| 162 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 163 | bool SkBitmap::setAlphaType(SkAlphaType newAlphaType) { |
| 164 | if (!SkColorTypeValidateAlphaType(fInfo.colorType(), newAlphaType, &newAlphaType)) { |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 165 | return false; |
| 166 | } |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 167 | if (fInfo.alphaType() != newAlphaType) { |
| 168 | fInfo = fInfo.makeAlphaType(newAlphaType); |
commit-bot@chromium.org | 0e8d0d6 | 2014-01-27 15:41:07 +0000 | [diff] [blame] | 169 | } |
Mike Reed | b712089 | 2017-04-14 17:16:36 -0400 | [diff] [blame] | 170 | SkDEBUGCODE(this->validate();) |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 171 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Mike Reed | b712089 | 2017-04-14 17:16:36 -0400 | [diff] [blame] | 174 | void SkBitmap::updatePixelsFromRef() { |
| 175 | void* p = nullptr; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 176 | if (fPixelRef) { |
Mike Reed | b712089 | 2017-04-14 17:16:36 -0400 | [diff] [blame] | 177 | // wish we could assert that a pixelref *always* has pixels |
| 178 | p = fPixelRef->pixels(); |
| 179 | if (p) { |
| 180 | SkASSERT(fRowBytes == fPixelRef->rowBytes()); |
| 181 | p = (char*)p |
| 182 | + fPixelRefOrigin.fY * fRowBytes |
| 183 | + fPixelRefOrigin.fX * fInfo.bytesPerPixel(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
Mike Reed | b712089 | 2017-04-14 17:16:36 -0400 | [diff] [blame] | 186 | fPixels = p; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 189 | void SkBitmap::setPixelRef(sk_sp<SkPixelRef> pr, int dx, int dy) { |
reed@google.com | dcea530 | 2014-01-03 13:43:01 +0000 | [diff] [blame] | 190 | #ifdef SK_DEBUG |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 191 | if (pr) { |
commit-bot@chromium.org | 466f5f3 | 2014-05-27 21:30:37 +0000 | [diff] [blame] | 192 | if (kUnknown_SkColorType != fInfo.colorType()) { |
Matt Sarett | f758311 | 2017-05-01 10:22:31 -0400 | [diff] [blame] | 193 | SkASSERT(fInfo.width() + dx <= pr->width()); |
| 194 | SkASSERT(fInfo.height() + dy <= pr->height()); |
reed@google.com | dcea530 | 2014-01-03 13:43:01 +0000 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 198 | |
Mike Reed | b712089 | 2017-04-14 17:16:36 -0400 | [diff] [blame] | 199 | fPixelRef = std::move(pr); |
| 200 | if (fPixelRef) { |
Matt Sarett | f758311 | 2017-05-01 10:22:31 -0400 | [diff] [blame] | 201 | fPixelRefOrigin.set(SkTPin(dx, 0, fPixelRef->width()), SkTPin(dy, 0, fPixelRef->height())); |
Mike Reed | b712089 | 2017-04-14 17:16:36 -0400 | [diff] [blame] | 202 | this->updatePixelsFromRef(); |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 203 | } else { |
| 204 | // ignore dx,dy if there is no pixelref |
| 205 | fPixelRefOrigin.setZero(); |
Mike Reed | b712089 | 2017-04-14 17:16:36 -0400 | [diff] [blame] | 206 | fPixels = nullptr; |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 207 | } |
| 208 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 209 | SkDEBUGCODE(this->validate();) |
| 210 | } |
| 211 | |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 212 | void SkBitmap::setPixels(void* p) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 213 | if (nullptr == p) { |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 214 | this->setPixelRef(nullptr, 0, 0); |
reed@google.com | 8e1034e | 2012-07-30 13:16:35 +0000 | [diff] [blame] | 215 | return; |
| 216 | } |
| 217 | |
commit-bot@chromium.org | 466f5f3 | 2014-05-27 21:30:37 +0000 | [diff] [blame] | 218 | if (kUnknown_SkColorType == fInfo.colorType()) { |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 219 | this->setPixelRef(nullptr, 0, 0); |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 220 | return; |
| 221 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 222 | |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 223 | this->setPixelRef(SkMallocPixelRef::MakeDirect(fInfo, p, fRowBytes), 0, 0); |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 224 | if (!fPixelRef) { |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 225 | return; |
| 226 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 227 | SkDEBUGCODE(this->validate();) |
| 228 | } |
| 229 | |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 230 | bool SkBitmap::tryAllocPixels(Allocator* allocator) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 231 | HeapAllocator stdalloc; |
skia.committer@gmail.com | d2ac07b | 2014-01-25 07:01:49 +0000 | [diff] [blame] | 232 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 233 | if (nullptr == allocator) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 234 | allocator = &stdalloc; |
| 235 | } |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 236 | return allocator->allocPixelRef(this); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 237 | } |
| 238 | |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 239 | /////////////////////////////////////////////////////////////////////////////// |
| 240 | |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 241 | bool SkBitmap::tryAllocPixels(const SkImageInfo& requestedInfo, size_t rowBytes) { |
reed | f0aed97 | 2014-07-01 12:48:11 -0700 | [diff] [blame] | 242 | if (!this->setInfo(requestedInfo, rowBytes)) { |
reed | bae704b | 2014-06-28 14:26:35 -0700 | [diff] [blame] | 243 | return reset_return_false(this); |
| 244 | } |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 245 | |
reed | bae704b | 2014-06-28 14:26:35 -0700 | [diff] [blame] | 246 | // setInfo may have corrected info (e.g. 565 is always opaque). |
| 247 | const SkImageInfo& correctedInfo = this->info(); |
reed | f0aed97 | 2014-07-01 12:48:11 -0700 | [diff] [blame] | 248 | // setInfo may have computed a valid rowbytes if 0 were passed in |
| 249 | rowBytes = this->rowBytes(); |
| 250 | |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 251 | sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeAllocate(correctedInfo, rowBytes); |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 252 | if (!pr) { |
reed | bae704b | 2014-06-28 14:26:35 -0700 | [diff] [blame] | 253 | return reset_return_false(this); |
| 254 | } |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 255 | this->setPixelRef(std::move(pr), 0, 0); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 256 | if (nullptr == this->getPixels()) { |
reed | bae704b | 2014-06-28 14:26:35 -0700 | [diff] [blame] | 257 | return reset_return_false(this); |
| 258 | } |
Mike Reed | b712089 | 2017-04-14 17:16:36 -0400 | [diff] [blame] | 259 | SkDEBUGCODE(this->validate();) |
reed | bae704b | 2014-06-28 14:26:35 -0700 | [diff] [blame] | 260 | return true; |
| 261 | } |
| 262 | |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 263 | bool SkBitmap::tryAllocPixelsFlags(const SkImageInfo& requestedInfo, uint32_t allocFlags) { |
scroggo | 0187dc2 | 2014-06-05 11:18:04 -0700 | [diff] [blame] | 264 | if (!this->setInfo(requestedInfo)) { |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 265 | return reset_return_false(this); |
| 266 | } |
| 267 | |
scroggo | 0187dc2 | 2014-06-05 11:18:04 -0700 | [diff] [blame] | 268 | // setInfo may have corrected info (e.g. 565 is always opaque). |
| 269 | const SkImageInfo& correctedInfo = this->info(); |
| 270 | |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame] | 271 | sk_sp<SkPixelRef> pr = (allocFlags & kZeroPixels_AllocFlag) ? |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 272 | SkMallocPixelRef::MakeZeroed(correctedInfo, correctedInfo.minRowBytes()) : |
| 273 | SkMallocPixelRef::MakeAllocate(correctedInfo, correctedInfo.minRowBytes()); |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 274 | if (!pr) { |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 275 | return reset_return_false(this); |
| 276 | } |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 277 | this->setPixelRef(std::move(pr), 0, 0); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 278 | if (nullptr == this->getPixels()) { |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 279 | return reset_return_false(this); |
| 280 | } |
Mike Reed | b712089 | 2017-04-14 17:16:36 -0400 | [diff] [blame] | 281 | SkDEBUGCODE(this->validate();) |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 282 | return true; |
| 283 | } |
| 284 | |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 285 | static void invoke_release_proc(void (*proc)(void* pixels, void* ctx), void* pixels, void* ctx) { |
| 286 | if (proc) { |
| 287 | proc(pixels, ctx); |
| 288 | } |
| 289 | } |
| 290 | |
scroggo | 0187dc2 | 2014-06-05 11:18:04 -0700 | [diff] [blame] | 291 | bool SkBitmap::installPixels(const SkImageInfo& requestedInfo, void* pixels, size_t rb, |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 292 | void (*releaseProc)(void* addr, void* context), void* context) { |
scroggo | 0187dc2 | 2014-06-05 11:18:04 -0700 | [diff] [blame] | 293 | if (!this->setInfo(requestedInfo, rb)) { |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 294 | invoke_release_proc(releaseProc, pixels, context); |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 295 | this->reset(); |
| 296 | return false; |
| 297 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 298 | if (nullptr == pixels) { |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 299 | invoke_release_proc(releaseProc, pixels, context); |
| 300 | return true; // we behaved as if they called setInfo() |
| 301 | } |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 302 | |
scroggo | 0187dc2 | 2014-06-05 11:18:04 -0700 | [diff] [blame] | 303 | // setInfo may have corrected info (e.g. 565 is always opaque). |
| 304 | const SkImageInfo& correctedInfo = this->info(); |
| 305 | |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 306 | sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeWithProc(correctedInfo, rb, pixels, |
| 307 | releaseProc, context); |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 308 | if (!pr) { |
| 309 | this->reset(); |
| 310 | return false; |
| 311 | } |
| 312 | |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 313 | this->setPixelRef(std::move(pr), 0, 0); |
mike@reedtribe.org | 6e58cf3 | 2014-02-16 20:54:21 +0000 | [diff] [blame] | 314 | SkDEBUGCODE(this->validate();) |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 315 | return true; |
| 316 | } |
| 317 | |
halcanary | e36ec87 | 2015-12-09 11:36:59 -0800 | [diff] [blame] | 318 | bool SkBitmap::installPixels(const SkPixmap& pixmap) { |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 319 | return this->installPixels(pixmap.info(), pixmap.writable_addr(), pixmap.rowBytes(), |
halcanary | e36ec87 | 2015-12-09 11:36:59 -0800 | [diff] [blame] | 320 | nullptr, nullptr); |
| 321 | } |
| 322 | |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 323 | bool SkBitmap::installMaskPixels(const SkMask& mask) { |
| 324 | if (SkMask::kA8_Format != mask.fFormat) { |
| 325 | this->reset(); |
| 326 | return false; |
| 327 | } |
| 328 | return this->installPixels(SkImageInfo::MakeA8(mask.fBounds.width(), |
| 329 | mask.fBounds.height()), |
| 330 | mask.fImage, mask.fRowBytes); |
| 331 | } |
| 332 | |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 333 | /////////////////////////////////////////////////////////////////////////////// |
| 334 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 335 | void SkBitmap::freePixels() { |
Mike Reed | b712089 | 2017-04-14 17:16:36 -0400 | [diff] [blame] | 336 | fPixelRef = nullptr; |
| 337 | fPixelRefOrigin.setZero(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 338 | fPixels = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 339 | } |
| 340 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 341 | uint32_t SkBitmap::getGenerationID() const { |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 342 | return fPixelRef ? fPixelRef->getGenerationID() : 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | void SkBitmap::notifyPixelsChanged() const { |
junov@chromium.org | b052129 | 2011-12-15 20:14:06 +0000 | [diff] [blame] | 346 | SkASSERT(!this->isImmutable()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 347 | if (fPixelRef) { |
| 348 | fPixelRef->notifyPixelsChanged(); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | /////////////////////////////////////////////////////////////////////////////// |
| 353 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 354 | /** We explicitly use the same allocator for our pixels that SkMask does, |
| 355 | so that we can freely assign memory allocated by one class to the other. |
| 356 | */ |
Mike Reed | b7648a2 | 2017-07-19 15:45:45 -0400 | [diff] [blame] | 357 | bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst) { |
commit-bot@chromium.org | 466f5f3 | 2014-05-27 21:30:37 +0000 | [diff] [blame] | 358 | const SkImageInfo info = dst->info(); |
| 359 | if (kUnknown_SkColorType == info.colorType()) { |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 360 | // SkDebugf("unsupported config for info %d\n", dst->config()); |
| 361 | return false; |
| 362 | } |
skia.committer@gmail.com | 96f5fa0 | 2013-12-16 07:01:40 +0000 | [diff] [blame] | 363 | |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 364 | sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeAllocate(info, dst->rowBytes()); |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 365 | if (!pr) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 366 | return false; |
| 367 | } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 368 | |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 369 | dst->setPixelRef(std::move(pr), 0, 0); |
Mike Reed | b712089 | 2017-04-14 17:16:36 -0400 | [diff] [blame] | 370 | SkDEBUGCODE(dst->validate();) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 371 | return true; |
| 372 | } |
| 373 | |
| 374 | /////////////////////////////////////////////////////////////////////////////// |
| 375 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 376 | bool SkBitmap::isImmutable() const { |
scroggo | 0847059 | 2014-07-15 19:56:48 -0700 | [diff] [blame] | 377 | return fPixelRef ? fPixelRef->isImmutable() : false; |
junov@chromium.org | b052129 | 2011-12-15 20:14:06 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | void SkBitmap::setImmutable() { |
| 381 | if (fPixelRef) { |
| 382 | fPixelRef->setImmutable(); |
junov@chromium.org | b052129 | 2011-12-15 20:14:06 +0000 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 386 | bool SkBitmap::isVolatile() const { |
| 387 | return (fFlags & kImageIsVolatile_Flag) != 0; |
| 388 | } |
| 389 | |
| 390 | void SkBitmap::setIsVolatile(bool isVolatile) { |
| 391 | if (isVolatile) { |
| 392 | fFlags |= kImageIsVolatile_Flag; |
| 393 | } else { |
| 394 | fFlags &= ~kImageIsVolatile_Flag; |
| 395 | } |
| 396 | } |
| 397 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 398 | void* SkBitmap::getAddr(int x, int y) const { |
| 399 | SkASSERT((unsigned)x < (unsigned)this->width()); |
| 400 | SkASSERT((unsigned)y < (unsigned)this->height()); |
| 401 | |
| 402 | char* base = (char*)this->getPixels(); |
| 403 | if (base) { |
| 404 | base += y * this->rowBytes(); |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 405 | switch (this->colorType()) { |
mtklein | 7fd93e3 | 2016-07-26 13:05:30 -0700 | [diff] [blame] | 406 | case kRGBA_F16_SkColorType: |
| 407 | base += x << 3; |
| 408 | break; |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 409 | case kRGBA_8888_SkColorType: |
| 410 | case kBGRA_8888_SkColorType: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 411 | base += x << 2; |
| 412 | break; |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 413 | case kARGB_4444_SkColorType: |
| 414 | case kRGB_565_SkColorType: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 415 | base += x << 1; |
| 416 | break; |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 417 | case kAlpha_8_SkColorType: |
reed | 0c9b1a8 | 2015-03-17 17:44:06 -0700 | [diff] [blame] | 418 | case kGray_8_SkColorType: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 419 | base += x; |
| 420 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 421 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 422 | SkDEBUGFAIL("Can't return addr for config"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 423 | base = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 424 | break; |
| 425 | } |
| 426 | } |
| 427 | return base; |
| 428 | } |
| 429 | |
| 430 | /////////////////////////////////////////////////////////////////////////////// |
| 431 | /////////////////////////////////////////////////////////////////////////////// |
| 432 | |
reed | 7aefe03 | 2015-06-08 10:22:22 -0700 | [diff] [blame] | 433 | void SkBitmap::erase(SkColor c, const SkIRect& area) const { |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 434 | SkDEBUGCODE(this->validate();) |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 435 | |
| 436 | switch (fInfo.colorType()) { |
| 437 | case kUnknown_SkColorType: |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 438 | // TODO: can we ASSERT that we never get here? |
| 439 | return; // can't erase. Should we bzero so the memory is not uninitialized? |
| 440 | default: |
| 441 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 442 | } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 443 | |
Mike Reed | 4edb5d2 | 2017-04-17 11:02:51 -0400 | [diff] [blame] | 444 | SkPixmap result; |
| 445 | if (!this->peekPixels(&result)) { |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 446 | return; |
| 447 | } |
| 448 | |
Mike Reed | 4edb5d2 | 2017-04-17 11:02:51 -0400 | [diff] [blame] | 449 | if (result.erase(c, area)) { |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 450 | this->notifyPixelsChanged(); |
| 451 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 452 | } |
| 453 | |
reed | 7aefe03 | 2015-06-08 10:22:22 -0700 | [diff] [blame] | 454 | void SkBitmap::eraseColor(SkColor c) const { |
| 455 | this->erase(c, SkIRect::MakeWH(this->width(), this->height())); |
reed@google.com | 60d3235 | 2013-06-28 19:40:50 +0000 | [diff] [blame] | 456 | } |
| 457 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 458 | ////////////////////////////////////////////////////////////////////////////////////// |
| 459 | ////////////////////////////////////////////////////////////////////////////////////// |
| 460 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 461 | bool SkBitmap::extractSubset(SkBitmap* result, const SkIRect& subset) const { |
| 462 | SkDEBUGCODE(this->validate();) |
| 463 | |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 464 | if (nullptr == result || !fPixelRef) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 465 | return false; // no src pixels |
| 466 | } |
| 467 | |
| 468 | SkIRect srcRect, r; |
| 469 | srcRect.set(0, 0, this->width(), this->height()); |
| 470 | if (!r.intersect(srcRect, subset)) { |
| 471 | return false; // r is empty (i.e. no intersection) |
| 472 | } |
| 473 | |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 474 | // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have |
| 475 | // exited above. |
| 476 | SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width())); |
| 477 | SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height())); |
| 478 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 479 | SkBitmap dst; |
herb | b5d7468 | 2016-04-21 08:45:39 -0700 | [diff] [blame] | 480 | dst.setInfo(this->info().makeWH(r.width(), r.height()), this->rowBytes()); |
skyostil@google.com | 0eb7576 | 2012-01-16 10:45:53 +0000 | [diff] [blame] | 481 | dst.setIsVolatile(this->isVolatile()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 482 | |
| 483 | if (fPixelRef) { |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 484 | SkIPoint origin = fPixelRefOrigin; |
| 485 | origin.fX += r.fLeft; |
| 486 | origin.fY += r.fTop; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 487 | // share the pixelref with a custom offset |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 488 | dst.setPixelRef(fPixelRef, origin.x(), origin.y()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 489 | } |
| 490 | SkDEBUGCODE(dst.validate();) |
| 491 | |
| 492 | // we know we're good, so commit to result |
| 493 | result->swap(dst); |
| 494 | return true; |
| 495 | } |
| 496 | |
| 497 | /////////////////////////////////////////////////////////////////////////////// |
| 498 | |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 499 | bool SkBitmap::readPixels(const SkImageInfo& requestedDstInfo, void* dstPixels, size_t dstRB, |
Matt Sarett | 3928ff8 | 2017-06-06 10:11:34 -0400 | [diff] [blame] | 500 | int x, int y, SkTransferFunctionBehavior behavior) const { |
Mike Reed | 4edb5d2 | 2017-04-17 11:02:51 -0400 | [diff] [blame] | 501 | SkPixmap src; |
| 502 | if (!this->peekPixels(&src)) { |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 503 | return false; |
| 504 | } |
Matt Sarett | 3928ff8 | 2017-06-06 10:11:34 -0400 | [diff] [blame] | 505 | return src.readPixels(requestedDstInfo, dstPixels, dstRB, x, y, behavior); |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 506 | } |
| 507 | |
Mike Reed | 68dd8d0 | 2017-01-04 16:34:31 -0500 | [diff] [blame] | 508 | bool SkBitmap::readPixels(const SkPixmap& dst, int srcX, int srcY) const { |
| 509 | return this->readPixels(dst.info(), dst.writable_addr(), dst.rowBytes(), srcX, srcY); |
| 510 | } |
| 511 | |
Matt Sarett | d2adc66 | 2017-03-27 15:07:35 -0400 | [diff] [blame] | 512 | bool SkBitmap::writePixels(const SkPixmap& src, int dstX, int dstY, |
| 513 | SkTransferFunctionBehavior behavior) { |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 514 | if (!SkImageInfoValidConversion(fInfo, src.info())) { |
Mike Reed | 68dd8d0 | 2017-01-04 16:34:31 -0500 | [diff] [blame] | 515 | return false; |
| 516 | } |
| 517 | |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 518 | SkWritePixelsRec rec(src.info(), src.addr(), src.rowBytes(), dstX, dstY); |
| 519 | if (!rec.trim(fInfo.width(), fInfo.height())) { |
| 520 | return false; |
| 521 | } |
| 522 | |
| 523 | void* dstPixels = this->getAddr(rec.fX, rec.fY); |
| 524 | const SkImageInfo dstInfo = fInfo.makeWH(rec.fInfo.width(), rec.fInfo.height()); |
Matt Sarett | 485c499 | 2017-02-14 14:18:27 -0500 | [diff] [blame] | 525 | SkConvertPixels(dstInfo, dstPixels, this->rowBytes(), rec.fInfo, rec.fPixels, rec.fRowBytes, |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 526 | nullptr, behavior); |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 527 | return true; |
Mike Reed | 68dd8d0 | 2017-01-04 16:34:31 -0500 | [diff] [blame] | 528 | } |
| 529 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 530 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 531 | |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 532 | static bool GetBitmapAlpha(const SkBitmap& src, uint8_t* SK_RESTRICT alpha, int alphaRowBytes) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 533 | SkASSERT(alpha != nullptr); |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 534 | SkASSERT(alphaRowBytes >= src.width()); |
| 535 | |
Mike Reed | 4edb5d2 | 2017-04-17 11:02:51 -0400 | [diff] [blame] | 536 | SkPixmap pmap; |
| 537 | if (!src.peekPixels(&pmap)) { |
lsalzman | a2415ac | 2016-10-11 14:29:12 -0700 | [diff] [blame] | 538 | for (int y = 0; y < src.height(); ++y) { |
| 539 | memset(alpha, 0, src.width()); |
| 540 | alpha += alphaRowBytes; |
| 541 | } |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 542 | return false; |
| 543 | } |
Matt Sarett | 485c499 | 2017-02-14 14:18:27 -0500 | [diff] [blame] | 544 | SkConvertPixels(SkImageInfo::MakeA8(pmap.width(), pmap.height()), alpha, alphaRowBytes, |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 545 | pmap.info(), pmap.addr(), pmap.rowBytes(), nullptr, |
Matt Sarett | d2adc66 | 2017-03-27 15:07:35 -0400 | [diff] [blame] | 546 | SkTransferFunctionBehavior::kRespect); |
reed@android.com | 1cdcb51 | 2009-08-24 19:11:00 +0000 | [diff] [blame] | 547 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | #include "SkPaint.h" |
| 551 | #include "SkMaskFilter.h" |
| 552 | #include "SkMatrix.h" |
| 553 | |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 554 | bool SkBitmap::extractAlpha(SkBitmap* dst, const SkPaint* paint, |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 555 | Allocator *allocator, SkIPoint* offset) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 556 | SkDEBUGCODE(this->validate();) |
| 557 | |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 558 | SkBitmap tmpBitmap; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 559 | SkMatrix identity; |
| 560 | SkMask srcM, dstM; |
| 561 | |
| 562 | srcM.fBounds.set(0, 0, this->width(), this->height()); |
| 563 | srcM.fRowBytes = SkAlign4(this->width()); |
| 564 | srcM.fFormat = SkMask::kA8_Format; |
| 565 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 566 | SkMaskFilter* filter = paint ? paint->getMaskFilter() : nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 567 | |
| 568 | // compute our (larger?) dst bounds if we have a filter |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 569 | if (filter) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 570 | identity.reset(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 571 | if (!filter->filterMask(&dstM, srcM, identity, nullptr)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 572 | goto NO_FILTER_CASE; |
| 573 | } |
| 574 | dstM.fRowBytes = SkAlign4(dstM.fBounds.width()); |
| 575 | } else { |
| 576 | NO_FILTER_CASE: |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 577 | tmpBitmap.setInfo(SkImageInfo::MakeA8(this->width(), this->height()), srcM.fRowBytes); |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 578 | if (!tmpBitmap.tryAllocPixels(allocator)) { |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 579 | // Allocation of pixels for alpha bitmap failed. |
| 580 | SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n", |
| 581 | tmpBitmap.width(), tmpBitmap.height()); |
| 582 | return false; |
| 583 | } |
| 584 | GetBitmapAlpha(*this, tmpBitmap.getAddr8(0, 0), srcM.fRowBytes); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 585 | if (offset) { |
| 586 | offset->set(0, 0); |
| 587 | } |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 588 | tmpBitmap.swap(*dst); |
| 589 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 590 | } |
bungeman@google.com | 02f5584 | 2011-10-04 21:25:00 +0000 | [diff] [blame] | 591 | srcM.fImage = SkMask::AllocImage(srcM.computeImageSize()); |
| 592 | SkAutoMaskFreeImage srcCleanup(srcM.fImage); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 593 | |
| 594 | GetBitmapAlpha(*this, srcM.fImage, srcM.fRowBytes); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 595 | if (!filter->filterMask(&dstM, srcM, identity, nullptr)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 596 | goto NO_FILTER_CASE; |
| 597 | } |
bungeman@google.com | 02f5584 | 2011-10-04 21:25:00 +0000 | [diff] [blame] | 598 | SkAutoMaskFreeImage dstCleanup(dstM.fImage); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 599 | |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 600 | tmpBitmap.setInfo(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.height()), |
| 601 | dstM.fRowBytes); |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 602 | if (!tmpBitmap.tryAllocPixels(allocator)) { |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 603 | // Allocation of pixels for alpha bitmap failed. |
| 604 | SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n", |
| 605 | tmpBitmap.width(), tmpBitmap.height()); |
| 606 | return false; |
| 607 | } |
| 608 | memcpy(tmpBitmap.getPixels(), dstM.fImage, dstM.computeImageSize()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 609 | if (offset) { |
| 610 | offset->set(dstM.fBounds.fLeft, dstM.fBounds.fTop); |
| 611 | } |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 612 | SkDEBUGCODE(tmpBitmap.validate();) |
| 613 | |
| 614 | tmpBitmap.swap(*dst); |
| 615 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | /////////////////////////////////////////////////////////////////////////////// |
| 619 | |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 620 | static void write_raw_pixels(SkWriteBuffer* buffer, const SkPixmap& pmap) { |
| 621 | const SkImageInfo& info = pmap.info(); |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 622 | const size_t snugRB = info.width() * info.bytesPerPixel(); |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 623 | const char* src = (const char*)pmap.addr(); |
| 624 | const size_t ramRB = pmap.rowBytes(); |
skia.committer@gmail.com | 3c134a9 | 2014-05-24 03:05:26 +0000 | [diff] [blame] | 625 | |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 626 | buffer->write32(SkToU32(snugRB)); |
| 627 | info.flatten(*buffer); |
| 628 | |
| 629 | const size_t size = snugRB * info.height(); |
scroggo | 565901d | 2015-12-10 10:44:13 -0800 | [diff] [blame] | 630 | SkAutoTMalloc<char> storage(size); |
| 631 | char* dst = storage.get(); |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 632 | for (int y = 0; y < info.height(); ++y) { |
| 633 | memcpy(dst, src, snugRB); |
| 634 | dst += snugRB; |
| 635 | src += ramRB; |
| 636 | } |
| 637 | buffer->writeByteArray(storage.get(), size); |
Mike Reed | 262b319 | 2017-07-17 10:13:58 -0400 | [diff] [blame] | 638 | // no colortable |
| 639 | buffer->writeBool(false); |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 640 | } |
| 641 | |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 642 | void SkBitmap::WriteRawPixels(SkWriteBuffer* buffer, const SkBitmap& bitmap) { |
| 643 | const SkImageInfo info = bitmap.info(); |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 644 | if (0 == info.width() || 0 == info.height() || bitmap.isNull()) { |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 645 | buffer->writeUInt(0); // instead of snugRB, signaling no pixels |
| 646 | return; |
| 647 | } |
| 648 | |
Mike Reed | 4edb5d2 | 2017-04-17 11:02:51 -0400 | [diff] [blame] | 649 | SkPixmap result; |
| 650 | if (!bitmap.peekPixels(&result)) { |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 651 | buffer->writeUInt(0); // instead of snugRB, signaling no pixels |
| 652 | return; |
| 653 | } |
| 654 | |
Mike Reed | 4edb5d2 | 2017-04-17 11:02:51 -0400 | [diff] [blame] | 655 | write_raw_pixels(buffer, result); |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 656 | } |
| 657 | |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 658 | bool SkBitmap::ReadRawPixels(SkReadBuffer* buffer, SkBitmap* bitmap) { |
| 659 | const size_t snugRB = buffer->readUInt(); |
| 660 | if (0 == snugRB) { // no pixels |
| 661 | return false; |
| 662 | } |
| 663 | |
| 664 | SkImageInfo info; |
| 665 | info.unflatten(*buffer); |
| 666 | |
Robert Phillips | b2cb535 | 2016-12-20 12:44:41 -0500 | [diff] [blame] | 667 | if (info.width() < 0 || info.height() < 0) { |
| 668 | return false; |
| 669 | } |
| 670 | |
mtklein | 58e389b | 2016-07-15 07:00:11 -0700 | [diff] [blame] | 671 | // If there was an error reading "info" or if it is bogus, |
robertphillips | 74139f1 | 2016-06-28 09:04:34 -0700 | [diff] [blame] | 672 | // don't use it to compute minRowBytes() |
| 673 | if (!buffer->validate(SkColorTypeValidateAlphaType(info.colorType(), |
| 674 | info.alphaType()))) { |
sugoi | ca95c19 | 2014-07-08 09:18:48 -0700 | [diff] [blame] | 675 | return false; |
| 676 | } |
| 677 | |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 678 | const size_t ramRB = info.minRowBytes(); |
sugoi | bd0d9da | 2015-01-07 08:47:44 -0800 | [diff] [blame] | 679 | const int height = SkMax32(info.height(), 0); |
| 680 | const uint64_t snugSize = sk_64_mul(snugRB, height); |
| 681 | const uint64_t ramSize = sk_64_mul(ramRB, height); |
| 682 | static const uint64_t max_size_t = (size_t)(-1); |
| 683 | if (!buffer->validate((snugSize <= ramSize) && (ramSize <= max_size_t))) { |
commit-bot@chromium.org | 0585843 | 2014-05-30 01:06:44 +0000 | [diff] [blame] | 684 | return false; |
| 685 | } |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 686 | |
reed | fde0511 | 2016-03-11 13:02:28 -0800 | [diff] [blame] | 687 | sk_sp<SkData> data(SkData::MakeUninitialized(SkToSizeT(ramSize))); |
robertphillips | 2893784 | 2015-06-08 07:10:49 -0700 | [diff] [blame] | 688 | unsigned char* dst = (unsigned char*)data->writable_data(); |
sugoi | bd0d9da | 2015-01-07 08:47:44 -0800 | [diff] [blame] | 689 | buffer->readByteArray(dst, SkToSizeT(snugSize)); |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 690 | |
| 691 | if (snugSize != ramSize) { |
robertphillips | 2893784 | 2015-06-08 07:10:49 -0700 | [diff] [blame] | 692 | const unsigned char* srcRow = dst + snugRB * (height - 1); |
| 693 | unsigned char* dstRow = dst + ramRB * (height - 1); |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 694 | for (int y = height - 1; y >= 1; --y) { |
| 695 | memmove(dstRow, srcRow, snugRB); |
| 696 | srcRow -= snugRB; |
| 697 | dstRow -= ramRB; |
| 698 | } |
| 699 | SkASSERT(srcRow == dstRow); // first row does not need to be moved |
| 700 | } |
skia.committer@gmail.com | 3c134a9 | 2014-05-24 03:05:26 +0000 | [diff] [blame] | 701 | |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 702 | if (buffer->readBool()) { |
Mike Reed | 323ae0e | 2017-07-24 22:05:25 -0400 | [diff] [blame^] | 703 | SkColorTable::Skip(*buffer); |
| 704 | if (!buffer->isValid()) { |
reed | b236d1a | 2015-08-28 10:14:18 -0700 | [diff] [blame] | 705 | return false; |
| 706 | } |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 707 | } |
skia.committer@gmail.com | 3c134a9 | 2014-05-24 03:05:26 +0000 | [diff] [blame] | 708 | |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame] | 709 | sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeWithData(info, info.minRowBytes(), |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 710 | std::move(data)); |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame] | 711 | if (!pr) { |
sugoi | 6af3147 | 2015-01-28 13:15:32 -0800 | [diff] [blame] | 712 | return false; |
| 713 | } |
Matt Sarett | f758311 | 2017-05-01 10:22:31 -0400 | [diff] [blame] | 714 | bitmap->setInfo(info); |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 715 | bitmap->setPixelRef(std::move(pr), 0, 0); |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 716 | return true; |
| 717 | } |
| 718 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 719 | enum { |
| 720 | SERIALIZE_PIXELTYPE_NONE, |
djsollen@google.com | 21830d9 | 2012-08-07 19:49:41 +0000 | [diff] [blame] | 721 | SERIALIZE_PIXELTYPE_REF_DATA |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 722 | }; |
| 723 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 724 | /////////////////////////////////////////////////////////////////////////////// |
| 725 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 726 | #ifdef SK_DEBUG |
| 727 | void SkBitmap::validate() const { |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 728 | fInfo.validate(); |
commit-bot@chromium.org | d5414e5 | 2014-02-13 22:30:38 +0000 | [diff] [blame] | 729 | |
| 730 | // ImageInfo may not require this, but Bitmap ensures that opaque-only |
| 731 | // colorTypes report opaque for their alphatype |
| 732 | if (kRGB_565_SkColorType == fInfo.colorType()) { |
| 733 | SkASSERT(kOpaque_SkAlphaType == fInfo.alphaType()); |
| 734 | } |
| 735 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 736 | SkASSERT(fInfo.validRowBytes(fRowBytes)); |
scroggo | 0847059 | 2014-07-15 19:56:48 -0700 | [diff] [blame] | 737 | uint8_t allFlags = kImageIsVolatile_Flag; |
scroggo@google.com | 8e990eb | 2013-06-14 15:55:56 +0000 | [diff] [blame] | 738 | #ifdef SK_BUILD_FOR_ANDROID |
| 739 | allFlags |= kHasHardwareMipMap_Flag; |
| 740 | #endif |
scroggo | 0847059 | 2014-07-15 19:56:48 -0700 | [diff] [blame] | 741 | SkASSERT((~allFlags & fFlags) == 0); |
Mike Reed | b712089 | 2017-04-14 17:16:36 -0400 | [diff] [blame] | 742 | |
| 743 | if (fPixelRef && fPixelRef->pixels()) { |
| 744 | SkASSERT(fPixels); |
| 745 | } else { |
| 746 | SkASSERT(!fPixels); |
| 747 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 748 | |
reed@google.com | 615316c | 2014-01-15 19:15:23 +0000 | [diff] [blame] | 749 | if (fPixels) { |
| 750 | SkASSERT(fPixelRef); |
reed@google.com | 615316c | 2014-01-15 19:15:23 +0000 | [diff] [blame] | 751 | SkASSERT(fPixelRef->rowBytes() == fRowBytes); |
| 752 | SkASSERT(fPixelRefOrigin.fX >= 0); |
| 753 | SkASSERT(fPixelRefOrigin.fY >= 0); |
Matt Sarett | f758311 | 2017-05-01 10:22:31 -0400 | [diff] [blame] | 754 | SkASSERT(fPixelRef->width() >= (int)this->width() + fPixelRefOrigin.fX); |
| 755 | SkASSERT(fPixelRef->height() >= (int)this->height() + fPixelRefOrigin.fY); |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 756 | SkASSERT(fPixelRef->rowBytes() >= fInfo.minRowBytes()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 757 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 758 | } |
| 759 | #endif |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 760 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 761 | #ifndef SK_IGNORE_TO_STRING |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 762 | #include "SkString.h" |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 763 | void SkBitmap::toString(SkString* str) const { |
| 764 | |
commit-bot@chromium.org | cba7378 | 2014-05-29 15:57:47 +0000 | [diff] [blame] | 765 | static const char* gColorTypeNames[kLastEnum_SkColorType + 1] = { |
| 766 | "UNKNOWN", "A8", "565", "4444", "RGBA", "BGRA", "INDEX8", |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 767 | }; |
| 768 | |
| 769 | str->appendf("bitmap: ((%d, %d) %s", this->width(), this->height(), |
commit-bot@chromium.org | cba7378 | 2014-05-29 15:57:47 +0000 | [diff] [blame] | 770 | gColorTypeNames[this->colorType()]); |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 771 | |
| 772 | str->append(" ("); |
| 773 | if (this->isOpaque()) { |
| 774 | str->append("opaque"); |
| 775 | } else { |
| 776 | str->append("transparent"); |
| 777 | } |
| 778 | if (this->isImmutable()) { |
| 779 | str->append(", immutable"); |
| 780 | } else { |
| 781 | str->append(", not-immutable"); |
| 782 | } |
| 783 | str->append(")"); |
| 784 | |
Mike Reed | 96d5b9a | 2017-04-12 22:29:00 -0400 | [diff] [blame] | 785 | str->appendf(" pixelref:%p", this->pixelRef()); |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 786 | str->append(")"); |
| 787 | } |
| 788 | #endif |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 789 | |
| 790 | /////////////////////////////////////////////////////////////////////////////// |
| 791 | |
reed | cb67414 | 2015-06-05 06:58:22 -0700 | [diff] [blame] | 792 | bool SkBitmap::peekPixels(SkPixmap* pmap) const { |
| 793 | if (fPixels) { |
| 794 | if (pmap) { |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 795 | pmap->reset(fInfo, fPixels, fRowBytes); |
reed | cb67414 | 2015-06-05 06:58:22 -0700 | [diff] [blame] | 796 | } |
| 797 | return true; |
| 798 | } |
| 799 | return false; |
| 800 | } |
| 801 | |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 802 | /////////////////////////////////////////////////////////////////////////////// |
| 803 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 804 | #ifdef SK_DEBUG |
| 805 | void SkImageInfo::validate() const { |
| 806 | SkASSERT(fWidth >= 0); |
| 807 | SkASSERT(fHeight >= 0); |
| 808 | SkASSERT(SkColorTypeIsValid(fColorType)); |
| 809 | SkASSERT(SkAlphaTypeIsValid(fAlphaType)); |
| 810 | } |
| 811 | #endif |