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