epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
reed | 3b72f79 | 2014-07-14 10:13:57 -0700 | [diff] [blame] | 7 | |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 8 | #include "SkBitmapCache.h" |
mtklein | 1b24933 | 2015-07-07 12:21:21 -0700 | [diff] [blame] | 9 | #include "SkMutex.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "SkPixelRef.h" |
enne | 54b666b | 2015-04-17 12:42:49 -0700 | [diff] [blame] | 11 | #include "SkTraceEvent.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | |
reed | ab86577 | 2015-05-21 06:29:05 -0700 | [diff] [blame] | 13 | //#define SK_SUPPORT_LEGACY_UNBALANCED_PIXELREF_LOCKCOUNT |
reed | 67ca2a9 | 2015-05-20 13:22:58 -0700 | [diff] [blame] | 14 | //#define SK_TRACE_PIXELREF_LIFETIME |
| 15 | |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 16 | #include "SkNextID.h" |
bsalomon@google.com | 586f48c | 2011-04-14 15:07:22 +0000 | [diff] [blame] | 17 | |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 18 | uint32_t SkNextID::ImageID() { |
| 19 | static uint32_t gID = 0; |
| 20 | uint32_t id; |
mtklein | 63d0024 | 2015-02-25 09:10:57 -0800 | [diff] [blame] | 21 | // Loop in case our global wraps around, as we never want to return a 0. |
bsalomon@google.com | 586f48c | 2011-04-14 15:07:22 +0000 | [diff] [blame] | 22 | do { |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 23 | id = sk_atomic_fetch_add(&gID, 2u) + 2; // Never set the low bit. |
| 24 | } while (0 == id); |
| 25 | return id; |
bsalomon@google.com | 586f48c | 2011-04-14 15:07:22 +0000 | [diff] [blame] | 26 | } |
| 27 | |
reed@google.com | ff0da4f | 2012-05-17 13:14:52 +0000 | [diff] [blame] | 28 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | |
reed@google.com | ff0da4f | 2012-05-17 13:14:52 +0000 | [diff] [blame] | 30 | // just need a > 0 value, so pick a funny one to aid in debugging |
| 31 | #define SKPIXELREF_PRELOCKED_LOCKCOUNT 123456789 |
| 32 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 33 | static SkImageInfo validate_info(const SkImageInfo& info) { |
| 34 | SkAlphaType newAlphaType = info.alphaType(); |
| 35 | SkAssertResult(SkColorTypeValidateAlphaType(info.colorType(), info.alphaType(), &newAlphaType)); |
| 36 | return info.makeAlphaType(newAlphaType); |
| 37 | } |
scroggo | 2fd0d14 | 2014-07-01 07:08:19 -0700 | [diff] [blame] | 38 | |
reed | 67ca2a9 | 2015-05-20 13:22:58 -0700 | [diff] [blame] | 39 | #ifdef SK_TRACE_PIXELREF_LIFETIME |
| 40 | static int32_t gInstCounter; |
| 41 | #endif |
| 42 | |
scroggo | f3ca41c | 2014-11-25 13:42:12 -0800 | [diff] [blame] | 43 | SkPixelRef::SkPixelRef(const SkImageInfo& info) |
| 44 | : fInfo(validate_info(info)) |
| 45 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
reed | 685f277 | 2015-07-31 11:46:12 -0700 | [diff] [blame] | 46 | , fStableID(SkNextID::ImageID()) |
scroggo | f3ca41c | 2014-11-25 13:42:12 -0800 | [diff] [blame] | 47 | #endif |
| 48 | |
| 49 | { |
reed | 67ca2a9 | 2015-05-20 13:22:58 -0700 | [diff] [blame] | 50 | #ifdef SK_TRACE_PIXELREF_LIFETIME |
| 51 | SkDebugf(" pixelref %d\n", sk_atomic_inc(&gInstCounter)); |
| 52 | #endif |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 53 | fRec.zero(); |
reed@google.com | 398337b | 2013-12-11 21:22:39 +0000 | [diff] [blame] | 54 | fLockCount = 0; |
| 55 | this->needsNewGenID(); |
reed | 26e0e58 | 2015-07-29 11:44:52 -0700 | [diff] [blame] | 56 | fMutability = kMutable; |
reed@google.com | 398337b | 2013-12-11 21:22:39 +0000 | [diff] [blame] | 57 | fPreLocked = false; |
mtklein | 6101077 | 2015-02-25 08:27:41 -0800 | [diff] [blame] | 58 | fAddedToCache.store(false); |
reed@google.com | 398337b | 2013-12-11 21:22:39 +0000 | [diff] [blame] | 59 | } |
reed@google.com | 398337b | 2013-12-11 21:22:39 +0000 | [diff] [blame] | 60 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 61 | SkPixelRef::~SkPixelRef() { |
reed | ab86577 | 2015-05-21 06:29:05 -0700 | [diff] [blame] | 62 | #ifndef SK_SUPPORT_LEGACY_UNBALANCED_PIXELREF_LOCKCOUNT |
| 63 | SkASSERT(SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount || 0 == fLockCount); |
| 64 | #endif |
| 65 | |
reed | 67ca2a9 | 2015-05-20 13:22:58 -0700 | [diff] [blame] | 66 | #ifdef SK_TRACE_PIXELREF_LIFETIME |
| 67 | SkDebugf("~pixelref %d\n", sk_atomic_dec(&gInstCounter) - 1); |
| 68 | #endif |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 69 | this->callGenIDChangeListeners(); |
| 70 | } |
| 71 | |
| 72 | void SkPixelRef::needsNewGenID() { |
mtklein | 63d0024 | 2015-02-25 09:10:57 -0800 | [diff] [blame] | 73 | fTaggedGenID.store(0); |
| 74 | SkASSERT(!this->genIDIsUnique()); // This method isn't threadsafe, so the assert should be fine. |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void SkPixelRef::cloneGenID(const SkPixelRef& that) { |
| 78 | // This is subtle. We must call that.getGenerationID() to make sure its genID isn't 0. |
mtklein | 63d0024 | 2015-02-25 09:10:57 -0800 | [diff] [blame] | 79 | uint32_t genID = that.getGenerationID(); |
| 80 | |
| 81 | // Neither ID is unique any more. |
| 82 | // (These & ~1u are actually redundant. that.getGenerationID() just did it for us.) |
| 83 | this->fTaggedGenID.store(genID & ~1u); |
| 84 | that. fTaggedGenID.store(genID & ~1u); |
| 85 | |
| 86 | // This method isn't threadsafe, so these asserts should be fine. |
| 87 | SkASSERT(!this->genIDIsUnique()); |
| 88 | SkASSERT(!that. genIDIsUnique()); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 89 | } |
| 90 | |
reed | bca1400 | 2015-07-27 12:19:16 -0700 | [diff] [blame] | 91 | static void validate_pixels_ctable(const SkImageInfo& info, const SkColorTable* ctable) { |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 92 | if (info.isEmpty()) { |
reed | bca1400 | 2015-07-27 12:19:16 -0700 | [diff] [blame] | 93 | return; // can't require ctable if the dimensions are empty |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 94 | } |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 95 | if (kIndex_8_SkColorType == info.colorType()) { |
| 96 | SkASSERT(ctable); |
| 97 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 98 | SkASSERT(nullptr == ctable); |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
reed@google.com | 7627c65 | 2014-01-06 15:01:48 +0000 | [diff] [blame] | 102 | void SkPixelRef::setPreLocked(void* pixels, size_t rowBytes, SkColorTable* ctable) { |
reed | bca1400 | 2015-07-27 12:19:16 -0700 | [diff] [blame] | 103 | SkASSERT(pixels); |
| 104 | validate_pixels_ctable(fInfo, ctable); |
reed@google.com | ff0da4f | 2012-05-17 13:14:52 +0000 | [diff] [blame] | 105 | // only call me in your constructor, otherwise fLockCount tracking can get |
| 106 | // out of sync. |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 107 | fRec.fPixels = pixels; |
| 108 | fRec.fColorTable = ctable; |
| 109 | fRec.fRowBytes = rowBytes; |
reed@google.com | ff0da4f | 2012-05-17 13:14:52 +0000 | [diff] [blame] | 110 | fLockCount = SKPIXELREF_PRELOCKED_LOCKCOUNT; |
| 111 | fPreLocked = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 112 | } |
| 113 | |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 114 | // Increments fLockCount only on success |
| 115 | bool SkPixelRef::lockPixelsInsideMutex() { |
mtklein | 7e6d9c0 | 2015-08-13 14:02:06 -0700 | [diff] [blame] | 116 | fMutex.assertHeld(); |
skia.committer@gmail.com | 98272d9 | 2014-01-04 07:01:40 +0000 | [diff] [blame] | 117 | |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 118 | if (1 == ++fLockCount) { |
| 119 | SkASSERT(fRec.isZero()); |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 120 | if (!this->onNewLockPixels(&fRec)) { |
| 121 | fRec.zero(); |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 122 | fLockCount -= 1; // we return fLockCount unchanged if we fail. |
| 123 | return false; |
reed@google.com | ff0da4f | 2012-05-17 13:14:52 +0000 | [diff] [blame] | 124 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 125 | } |
reed | bca1400 | 2015-07-27 12:19:16 -0700 | [diff] [blame] | 126 | if (fRec.fPixels) { |
| 127 | validate_pixels_ctable(fInfo, fRec.fColorTable); |
| 128 | return true; |
| 129 | } |
reed | 7cdfa8a | 2015-08-11 12:23:45 -0700 | [diff] [blame] | 130 | // no pixels, so we failed (somehow) |
| 131 | --fLockCount; |
reed | bca1400 | 2015-07-27 12:19:16 -0700 | [diff] [blame] | 132 | return false; |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 133 | } |
| 134 | |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 135 | // For historical reasons, we always inc fLockCount, even if we return false. |
| 136 | // It would be nice to change this (it seems), and only inc if we actually succeed... |
| 137 | bool SkPixelRef::lockPixels() { |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 138 | SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); |
| 139 | |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 140 | if (!fPreLocked) { |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 141 | TRACE_EVENT_BEGIN0("skia", "SkPixelRef::lockPixelsMutex"); |
mtklein | 7e6d9c0 | 2015-08-13 14:02:06 -0700 | [diff] [blame] | 142 | SkAutoMutexAcquire ac(fMutex); |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 143 | TRACE_EVENT_END0("skia", "SkPixelRef::lockPixelsMutex"); |
| 144 | SkDEBUGCODE(int oldCount = fLockCount;) |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 145 | bool success = this->lockPixelsInsideMutex(); |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 146 | // lockPixelsInsideMutex only increments the count if it succeeds. |
| 147 | SkASSERT(oldCount + (int)success == fLockCount); |
| 148 | |
| 149 | if (!success) { |
| 150 | // For compatibility with SkBitmap calling lockPixels, we still want to increment |
| 151 | // fLockCount even if we failed. If we updated SkBitmap we could remove this oddity. |
| 152 | fLockCount += 1; |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 153 | return false; |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 154 | } |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 155 | } |
reed | bca1400 | 2015-07-27 12:19:16 -0700 | [diff] [blame] | 156 | if (fRec.fPixels) { |
| 157 | validate_pixels_ctable(fInfo, fRec.fColorTable); |
| 158 | return true; |
| 159 | } |
| 160 | return false; |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 161 | } |
| 162 | |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 163 | bool SkPixelRef::lockPixels(LockRec* rec) { |
| 164 | if (this->lockPixels()) { |
| 165 | *rec = fRec; |
| 166 | return true; |
| 167 | } |
| 168 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void SkPixelRef::unlockPixels() { |
reed@google.com | ff0da4f | 2012-05-17 13:14:52 +0000 | [diff] [blame] | 172 | SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 173 | |
reed@google.com | ff0da4f | 2012-05-17 13:14:52 +0000 | [diff] [blame] | 174 | if (!fPreLocked) { |
mtklein | 7e6d9c0 | 2015-08-13 14:02:06 -0700 | [diff] [blame] | 175 | SkAutoMutexAcquire ac(fMutex); |
reed@google.com | 93c5f9e | 2011-02-24 18:09:46 +0000 | [diff] [blame] | 176 | |
reed@google.com | ff0da4f | 2012-05-17 13:14:52 +0000 | [diff] [blame] | 177 | SkASSERT(fLockCount > 0); |
| 178 | if (0 == --fLockCount) { |
reed@google.com | c83a91f | 2013-12-13 13:41:14 +0000 | [diff] [blame] | 179 | // don't call onUnlockPixels unless onLockPixels succeeded |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 180 | if (fRec.fPixels) { |
reed@google.com | c83a91f | 2013-12-13 13:41:14 +0000 | [diff] [blame] | 181 | this->onUnlockPixels(); |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 182 | fRec.zero(); |
reed@google.com | c83a91f | 2013-12-13 13:41:14 +0000 | [diff] [blame] | 183 | } else { |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 184 | SkASSERT(fRec.isZero()); |
reed@google.com | c83a91f | 2013-12-13 13:41:14 +0000 | [diff] [blame] | 185 | } |
reed@google.com | ff0da4f | 2012-05-17 13:14:52 +0000 | [diff] [blame] | 186 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 190 | bool SkPixelRef::requestLock(const LockRequest& request, LockResult* result) { |
| 191 | SkASSERT(result); |
| 192 | if (request.fSize.isEmpty()) { |
| 193 | return false; |
| 194 | } |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 195 | // until we support subsets, we have to check this... |
| 196 | if (request.fSize.width() != fInfo.width() || request.fSize.height() != fInfo.height()) { |
| 197 | return false; |
| 198 | } |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 199 | |
| 200 | if (fPreLocked) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 201 | result->fUnlockProc = nullptr; |
| 202 | result->fUnlockContext = nullptr; |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 203 | result->fCTable = fRec.fColorTable; |
| 204 | result->fPixels = fRec.fPixels; |
| 205 | result->fRowBytes = fRec.fRowBytes; |
| 206 | result->fSize.set(fInfo.width(), fInfo.height()); |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 207 | } else { |
mtklein | 7e6d9c0 | 2015-08-13 14:02:06 -0700 | [diff] [blame] | 208 | SkAutoMutexAcquire ac(fMutex); |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 209 | if (!this->onRequestLock(request, result)) { |
| 210 | return false; |
| 211 | } |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 212 | } |
reed | bca1400 | 2015-07-27 12:19:16 -0700 | [diff] [blame] | 213 | if (result->fPixels) { |
| 214 | validate_pixels_ctable(fInfo, result->fCTable); |
| 215 | return true; |
| 216 | } |
| 217 | return false; |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 218 | } |
| 219 | |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 220 | bool SkPixelRef::lockPixelsAreWritable() const { |
| 221 | return this->onLockPixelsAreWritable(); |
| 222 | } |
| 223 | |
| 224 | bool SkPixelRef::onLockPixelsAreWritable() const { |
| 225 | return true; |
| 226 | } |
| 227 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 228 | uint32_t SkPixelRef::getGenerationID() const { |
mtklein | 63d0024 | 2015-02-25 09:10:57 -0800 | [diff] [blame] | 229 | uint32_t id = fTaggedGenID.load(); |
mtklein | 86821b5 | 2015-02-24 14:38:12 -0800 | [diff] [blame] | 230 | if (0 == id) { |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 231 | uint32_t next = SkNextID::ImageID() | 1u; |
mtklein | 59c9203 | 2015-02-25 12:51:55 -0800 | [diff] [blame] | 232 | if (fTaggedGenID.compare_exchange(&id, next)) { |
| 233 | id = next; // There was no race or we won the race. fTaggedGenID is next now. |
| 234 | } else { |
| 235 | // We lost a race to set fTaggedGenID. compare_exchange() filled id with the winner. |
| 236 | } |
| 237 | // We can't quite SkASSERT(this->genIDIsUnique()). It could be non-unique |
| 238 | // if we got here via the else path (pretty unlikely, but possible). |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 239 | } |
mtklein | 63d0024 | 2015-02-25 09:10:57 -0800 | [diff] [blame] | 240 | return id & ~1u; // Mask off bottom unique bit. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 241 | } |
| 242 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 243 | void SkPixelRef::addGenIDChangeListener(GenIDChangeListener* listener) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 244 | if (nullptr == listener || !this->genIDIsUnique()) { |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 245 | // No point in tracking this if we're not going to call it. |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 246 | delete listener; |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 247 | return; |
| 248 | } |
| 249 | *fGenIDChangeListeners.append() = listener; |
| 250 | } |
| 251 | |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 252 | // we need to be called *before* the genID gets changed or zerod |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 253 | void SkPixelRef::callGenIDChangeListeners() { |
| 254 | // We don't invalidate ourselves if we think another SkPixelRef is sharing our genID. |
mtklein | 63d0024 | 2015-02-25 09:10:57 -0800 | [diff] [blame] | 255 | if (this->genIDIsUnique()) { |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 256 | for (int i = 0; i < fGenIDChangeListeners.count(); i++) { |
| 257 | fGenIDChangeListeners[i]->onChange(); |
| 258 | } |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 259 | |
mtklein | 6101077 | 2015-02-25 08:27:41 -0800 | [diff] [blame] | 260 | // TODO: SkAtomic could add "old_value = atomic.xchg(new_value)" to make this clearer. |
| 261 | if (fAddedToCache.load()) { |
reed | 83787d0 | 2015-02-25 07:17:11 -0800 | [diff] [blame] | 262 | SkNotifyBitmapGenIDIsStale(this->getGenerationID()); |
mtklein | 6101077 | 2015-02-25 08:27:41 -0800 | [diff] [blame] | 263 | fAddedToCache.store(false); |
reed | 83787d0 | 2015-02-25 07:17:11 -0800 | [diff] [blame] | 264 | } |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 265 | } |
| 266 | // Listeners get at most one shot, so whether these triggered or not, blow them away. |
| 267 | fGenIDChangeListeners.deleteAll(); |
| 268 | } |
| 269 | |
reed@google.com | c1587f9 | 2014-01-28 16:05:39 +0000 | [diff] [blame] | 270 | void SkPixelRef::notifyPixelsChanged() { |
reed@android.com | 3eab80c | 2009-03-24 18:47:35 +0000 | [diff] [blame] | 271 | #ifdef SK_DEBUG |
reed | 26e0e58 | 2015-07-29 11:44:52 -0700 | [diff] [blame] | 272 | if (this->isImmutable()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 273 | SkDebugf("========== notifyPixelsChanged called on immutable pixelref"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 274 | } |
reed@android.com | 3eab80c | 2009-03-24 18:47:35 +0000 | [diff] [blame] | 275 | #endif |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 276 | this->callGenIDChangeListeners(); |
| 277 | this->needsNewGenID(); |
junov | da5469d | 2015-06-15 09:48:15 -0700 | [diff] [blame] | 278 | this->onNotifyPixelsChanged(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 279 | } |
| 280 | |
reed@google.com | c1587f9 | 2014-01-28 16:05:39 +0000 | [diff] [blame] | 281 | void SkPixelRef::changeAlphaType(SkAlphaType at) { |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 282 | *const_cast<SkImageInfo*>(&fInfo) = fInfo.makeAlphaType(at); |
reed@google.com | c1587f9 | 2014-01-28 16:05:39 +0000 | [diff] [blame] | 283 | } |
| 284 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 285 | void SkPixelRef::setImmutable() { |
reed | 26e0e58 | 2015-07-29 11:44:52 -0700 | [diff] [blame] | 286 | fMutability = kImmutable; |
| 287 | } |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 288 | |
| 289 | void SkPixelRef::setImmutableWithID(uint32_t genID) { |
| 290 | /* |
| 291 | * We are forcing the genID to match an external value. The caller must ensure that this |
| 292 | * value does not conflict with other content. |
| 293 | * |
| 294 | * One use is to force this pixelref's id to match an SkImage's id |
| 295 | */ |
| 296 | fMutability = kImmutable; |
| 297 | fTaggedGenID.store(genID); |
| 298 | } |
| 299 | |
reed | 26e0e58 | 2015-07-29 11:44:52 -0700 | [diff] [blame] | 300 | void SkPixelRef::setTemporarilyImmutable() { |
| 301 | SkASSERT(fMutability != kImmutable); |
| 302 | fMutability = kTemporarilyImmutable; |
| 303 | } |
| 304 | |
| 305 | void SkPixelRef::restoreMutability() { |
| 306 | SkASSERT(fMutability != kImmutable); |
| 307 | fMutability = kMutable; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 308 | } |
| 309 | |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 310 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 311 | |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 312 | |
junov | da5469d | 2015-06-15 09:48:15 -0700 | [diff] [blame] | 313 | void SkPixelRef::onNotifyPixelsChanged() { } |
| 314 | |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 315 | size_t SkPixelRef::getAllocatedSizeInBytes() const { |
| 316 | return 0; |
| 317 | } |
| 318 | |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 319 | static void unlock_legacy_result(void* ctx) { |
| 320 | SkPixelRef* pr = (SkPixelRef*)ctx; |
| 321 | pr->unlockPixels(); |
| 322 | pr->unref(); // balancing the Ref in onRequestLoc |
| 323 | } |
| 324 | |
| 325 | bool SkPixelRef::onRequestLock(const LockRequest& request, LockResult* result) { |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 326 | if (!this->lockPixelsInsideMutex()) { |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 327 | return false; |
| 328 | } |
| 329 | |
| 330 | result->fUnlockProc = unlock_legacy_result; |
| 331 | result->fUnlockContext = SkRef(this); // this is balanced in our fUnlockProc |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 332 | result->fCTable = fRec.fColorTable; |
| 333 | result->fPixels = fRec.fPixels; |
| 334 | result->fRowBytes = fRec.fRowBytes; |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 335 | result->fSize.set(fInfo.width(), fInfo.height()); |
| 336 | return true; |
| 337 | } |