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