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(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     bool singleThreaded = this->unique();
83     fGenIDChangeListeners.add(std::move(listener), singleThreaded);
84 }
85 
86 // we need to be called *before* the genID gets changed or zerod
callGenIDChangeListeners()87 void SkPixelRef::callGenIDChangeListeners() {
88     bool singleThreaded = this->unique();
89     // We don't invalidate ourselves if we think another SkPixelRef is sharing our genID.
90     if (this->genIDIsUnique()) {
91         fGenIDChangeListeners.changed(singleThreaded);
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(singleThreaded);
99     }
100 }
101 
notifyPixelsChanged()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 
setImmutable()112 void SkPixelRef::setImmutable() {
113     fMutability = kImmutable;
114 }
115 
setImmutableWithID(uint32_t genID)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 
setTemporarilyImmutable()127 void SkPixelRef::setTemporarilyImmutable() {
128     SkASSERT(fMutability != kImmutable);
129     fMutability = kTemporarilyImmutable;
130 }
131 
restoreMutability()132 void SkPixelRef::restoreMutability() {
133     SkASSERT(fMutability != kImmutable);
134     fMutability = kMutable;
135 }
136 
SkMakePixelRefWithProc(int width,int height,size_t rowBytes,void * addr,void (* releaseProc)(void * addr,void * ctx),void * ctx)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 }
152