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