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/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);
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(GenIDChangeListener * listener)76 void SkPixelRef::addGenIDChangeListener(GenIDChangeListener* listener) {
77 if (nullptr == listener || !this->genIDIsUnique()) {
78 // No point in tracking this if we're not going to call it.
79 delete listener;
80 return;
81 }
82 SkAutoMutexExclusive lock(fGenIDChangeListenersMutex);
83 *fGenIDChangeListeners.append() = listener;
84 }
85
86 // we need to be called *before* the genID gets changed or zerod
callGenIDChangeListeners()87 void SkPixelRef::callGenIDChangeListeners() {
88 SkAutoMutexExclusive lock(fGenIDChangeListenersMutex);
89 // We don't invalidate ourselves if we think another SkPixelRef is sharing our genID.
90 if (this->genIDIsUnique()) {
91 for (int i = 0; i < fGenIDChangeListeners.count(); i++) {
92 fGenIDChangeListeners[i]->onChange();
93 }
94
95 if (fAddedToCache.exchange(false)) {
96 SkNotifyBitmapGenIDIsStale(this->getGenerationID());
97 }
98 }
99 // Listeners get at most one shot, so whether these triggered or not, blow them away.
100 fGenIDChangeListeners.deleteAll();
101 }
102
notifyPixelsChanged()103 void SkPixelRef::notifyPixelsChanged() {
104 #ifdef SK_DEBUG
105 if (this->isImmutable()) {
106 SkDebugf("========== notifyPixelsChanged called on immutable pixelref");
107 }
108 #endif
109 this->callGenIDChangeListeners();
110 this->needsNewGenID();
111 }
112
setImmutable()113 void SkPixelRef::setImmutable() {
114 fMutability = kImmutable;
115 }
116
setImmutableWithID(uint32_t genID)117 void SkPixelRef::setImmutableWithID(uint32_t genID) {
118 /*
119 * We are forcing the genID to match an external value. The caller must ensure that this
120 * value does not conflict with other content.
121 *
122 * One use is to force this pixelref's id to match an SkImage's id
123 */
124 fMutability = kImmutable;
125 fTaggedGenID.store(genID);
126 }
127
setTemporarilyImmutable()128 void SkPixelRef::setTemporarilyImmutable() {
129 SkASSERT(fMutability != kImmutable);
130 fMutability = kTemporarilyImmutable;
131 }
132
restoreMutability()133 void SkPixelRef::restoreMutability() {
134 SkASSERT(fMutability != kImmutable);
135 fMutability = kMutable;
136 }
137
SkMakePixelRefWithProc(int width,int height,size_t rowBytes,void * addr,void (* releaseProc)(void * addr,void * ctx),void * ctx)138 sk_sp<SkPixelRef> SkMakePixelRefWithProc(int width, int height, size_t rowBytes, void* addr,
139 void (*releaseProc)(void* addr, void* ctx), void* ctx) {
140 SkASSERT(width >= 0 && height >= 0);
141 if (nullptr == releaseProc) {
142 return sk_make_sp<SkPixelRef>(width, height, addr, rowBytes);
143 }
144 struct PixelRef final : public SkPixelRef {
145 void (*fReleaseProc)(void*, void*);
146 void* fReleaseProcContext;
147 PixelRef(int w, int h, void* s, size_t r, void (*proc)(void*, void*), void* ctx)
148 : SkPixelRef(w, h, s, r), fReleaseProc(proc), fReleaseProcContext(ctx) {}
149 ~PixelRef() override { fReleaseProc(this->pixels(), fReleaseProcContext); }
150 };
151 return sk_sp<SkPixelRef>(new PixelRef(width, height, addr, rowBytes, releaseProc, ctx));
152 }
153