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