| 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 |
*/ |
| 7 |
|
| 8 |
#include "include/core/SkPixelRef.h" |
| 9 |
|
| 10 |
#include "include/private/base/SkAssert.h" |
| 11 |
#include "include/private/base/SkDebug.h" |
| 12 |
#include "src/core/SkBitmapCache.h" |
| 13 |
#include "src/core/SkNextID.h" |
| 14 |
#include "src/core/SkPixelRefPriv.h" |
| 15 |
|
| 16 |
#include <atomic> |
| 17 |
#include <utility> |
| 18 |
|
| 19 |
uint32_t SkNextID::ImageID() { |
| 20 |
// We never set the low bit.... see SkPixelRef::genIDIsUnique(). |
| 21 |
static std::atomic<uint32_t> nextID{2}; |
| 22 |
|
| 23 |
uint32_t id; |
| 24 |
do { |
| 25 |
id = nextID.fetch_add(2, std::memory_order_relaxed); |
| 26 |
} while (id == 0); |
| 27 |
return id; |
| 28 |
} |
| 29 |
|
| 30 |
/////////////////////////////////////////////////////////////////////////////// |
| 31 |
|
| 32 |
SkPixelRef::SkPixelRef(int width, int height, void* pixels, size_t rowBytes) |
| 33 |
: fWidth(width) |
| 34 |
, fHeight(height) |
| 35 |
, fPixels(pixels) |
| 36 |
, fRowBytes(rowBytes) |
| 37 |
, fAddedToCache(false) |
| 38 |
{ |
| 39 |
this->needsNewGenID(); |
| 40 |
fMutability = kMutable; |
| 41 |
} |
| 42 |
|
| 43 |
SkPixelRef::~SkPixelRef() { |
| 44 |
this->callGenIDChangeListeners(); |
| 45 |
} |
| 46 |
|
| 47 |
// This is undefined if there are clients in-flight trying to use us |
| 48 |
void SkPixelRef::android_only_reset(int width, int height, size_t rowBytes) { |
| 49 |
fWidth = width; |
| 50 |
fHeight = height; |
| 51 |
fRowBytes = rowBytes; |
| 52 |
// note: we do not change fPixels |
| 53 |
|
| 54 |
// conservative, since its possible the "new" settings are the same as the old. |
| 55 |
this->notifyPixelsChanged(); |
| 56 |
} |
| 57 |
|
| 58 |
void SkPixelRef::needsNewGenID() { |
| 59 |
fTaggedGenID.store(0); |
| 60 |
SkASSERT(!this->genIDIsUnique()); // This method isn't threadsafe, so the assert should be fine. |
| 61 |
} |
| 62 |
|
| 63 |
uint32_t SkPixelRef::getGenerationID() const { |
| 64 |
uint32_t id = fTaggedGenID.load(); |
| 65 |
if (0 == id) { |
| 66 |
uint32_t next = SkNextID::ImageID() | 1u; |
| 67 |
if (fTaggedGenID.compare_exchange_strong(id, next)) { |
| 68 |
id = next; // There was no race or we won the race. fTaggedGenID is next now. |
| 69 |
} else { |
| 70 |
// We lost a race to set fTaggedGenID. compare_exchange() filled id with the winner. |
| 71 |
} |
| 72 |
// We can't quite SkASSERT(this->genIDIsUnique()). It could be non-unique |
| 73 |
// if we got here via the else path (pretty unlikely, but possible). |
| 74 |
} |
| 75 |
return id & ~1u; // Mask off bottom unique bit. |
| 76 |
} |
| 77 |
|
| 78 |
void SkPixelRef::addGenIDChangeListener(sk_sp<SkIDChangeListener> listener) { |
| 79 |
if (!listener || !this->genIDIsUnique()) { |
| 80 |
// No point in tracking this if we're not going to call it. |
| 81 |
return; |
| 82 |
} |
| 83 |
SkASSERT(!listener->shouldDeregister()); |
| 84 |
fGenIDChangeListeners.add(std::move(listener)); |
| 85 |
} |
| 86 |
|
| 87 |
// we need to be called *before* the genID gets changed or zerod |
| 88 |
void SkPixelRef::callGenIDChangeListeners() { |
| 89 |
// We don't invalidate ourselves if we think another SkPixelRef is sharing our genID. |
| 90 |
if (this->genIDIsUnique()) { |
| 91 |
fGenIDChangeListeners.changed(); |
| 92 |
if (fAddedToCache.exchange(false)) { |
| 93 |
SkNotifyBitmapGenIDIsStale(this->getGenerationID()); |
| 94 |
} |
| 95 |
} else { |
| 96 |
// Listeners get at most one shot, so even though these weren't triggered or not, blow them |
| 97 |
// away. |
| 98 |
fGenIDChangeListeners.reset(); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
void SkPixelRef::notifyPixelsChanged() { |
| 103 |
#ifdef SK_DEBUG |
| 104 |
if (this->isImmutable()) { |
| 105 |
SkDebugf("========== notifyPixelsChanged called on immutable pixelref"); |
| 106 |
} |
| 107 |
#endif |
| 108 |
this->callGenIDChangeListeners(); |
| 109 |
this->needsNewGenID(); |
| 110 |
} |
| 111 |
|
| 112 |
void SkPixelRef::setImmutable() { |
| 113 |
fMutability = kImmutable; |
| 114 |
} |
| 115 |
|
| 116 |
void SkPixelRef::setImmutableWithID(uint32_t genID) { |
| 117 |
/* |
| 118 |
* We are forcing the genID to match an external value. The caller must ensure that this |
| 119 |
* value does not conflict with other content. |
| 120 |
* |
| 121 |
* One use is to force this pixelref's id to match an SkImage's id |
| 122 |
*/ |
| 123 |
fMutability = kImmutable; |
| 124 |
fTaggedGenID.store(genID); |
| 125 |
} |
| 126 |
|
| 127 |
void SkPixelRef::setTemporarilyImmutable() { |
| 128 |
SkASSERT(fMutability != kImmutable); |
| 129 |
fMutability = kTemporarilyImmutable; |
| 130 |
} |
| 131 |
|
| 132 |
void SkPixelRef::restoreMutability() { |
| 133 |
SkASSERT(fMutability != kImmutable); |
| 134 |
fMutability = kMutable; |
| 135 |
} |
| 136 |
|
| 137 |
sk_sp<SkPixelRef> SkMakePixelRefWithProc(int width, int height, size_t rowBytes, void* addr, |
| 138 |
void (*releaseProc)(void* addr, void* ctx), void* ctx) { |
| 139 |
SkASSERT(width >= 0 && height >= 0); |
| 140 |
if (nullptr == releaseProc) { |
| 141 |
return sk_make_sp<SkPixelRef>(width, height, addr, rowBytes); |
| 142 |
} |
| 143 |
struct PixelRef final : public SkPixelRef { |
| 144 |
void (*fReleaseProc)(void*, void*); |
| 145 |
void* fReleaseProcContext; |
| 146 |
PixelRef(int w, int h, void* s, size_t r, void (*proc)(void*, void*), void* ctx) |
| 147 |
: SkPixelRef(w, h, s, r), fReleaseProc(proc), fReleaseProcContext(ctx) {} |
| 148 |
~PixelRef() override { fReleaseProc(this->pixels(), fReleaseProcContext); } |
| 149 |
}; |
| 150 |
return sk_sp<SkPixelRef>(new PixelRef(width, height, addr, rowBytes, releaseProc, ctx)); |
| 151 |
} |