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