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