3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
8 #include "SkPixelRef.h"
9 #include "SkReadBuffer.h"
10 #include "SkWriteBuffer.h"
13 #ifdef SK_USE_POSIX_THREADS
15 static SkBaseMutex gPixelRefMutexRing[] = {
16 { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER },
17 { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER },
18 { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER },
19 { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER },
21 { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER },
22 { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER },
23 { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER },
24 { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER },
26 { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER },
27 { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER },
28 { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER },
29 { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER },
31 { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER },
32 { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER },
33 { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER },
34 { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER },
37 // must be a power-of-2. undef to just use 1 mutex
38 #define PIXELREF_MUTEX_RING_COUNT SK_ARRAY_COUNT(gPixelRefMutexRing)
42 // must be a power-of-2. undef to just use 1 mutex
43 #define PIXELREF_MUTEX_RING_COUNT 32
44 static SkBaseMutex gPixelRefMutexRing[PIXELREF_MUTEX_RING_COUNT];
48 static SkBaseMutex* get_default_mutex() {
49 static int32_t gPixelRefMutexRingIndex;
51 SkASSERT(SkIsPow2(PIXELREF_MUTEX_RING_COUNT));
53 // atomic_inc might be overkill here. It may be fine if once in a while
54 // we hit a race-condition and two subsequent calls get the same index...
55 int index = sk_atomic_inc(&gPixelRefMutexRingIndex);
56 return &gPixelRefMutexRing[index & (PIXELREF_MUTEX_RING_COUNT - 1)];
59 ///////////////////////////////////////////////////////////////////////////////
61 int32_t SkNextPixelRefGenerationID();
63 int32_t SkNextPixelRefGenerationID() {
64 static int32_t gPixelRefGenerationID;
65 // do a loop in case our global wraps around, as we never want to
69 genID = sk_atomic_inc(&gPixelRefGenerationID) + 1;
74 ///////////////////////////////////////////////////////////////////////////////
76 void SkPixelRef::setMutex(SkBaseMutex* mutex) {
78 mutex = get_default_mutex();
83 // just need a > 0 value, so pick a funny one to aid in debugging
84 #define SKPIXELREF_PRELOCKED_LOCKCOUNT 123456789
86 SkPixelRef::SkPixelRef(const SkImageInfo& info) : fInfo(info) {
90 this->needsNewGenID();
96 SkPixelRef::SkPixelRef(const SkImageInfo& info, SkBaseMutex* mutex) : fInfo(info) {
97 this->setMutex(mutex);
100 this->needsNewGenID();
101 fIsImmutable = false;
105 static SkImageInfo read_info(SkReadBuffer& buffer) {
107 info.unflatten(buffer);
111 SkPixelRef::SkPixelRef(SkReadBuffer& buffer, SkBaseMutex* mutex)
113 , fInfo(read_info(buffer))
115 this->setMutex(mutex);
118 fIsImmutable = buffer.readBool();
119 fGenerationID = buffer.readUInt();
120 fUniqueGenerationID = false; // Conservatively assuming the original still exists.
124 SkPixelRef::~SkPixelRef() {
125 this->callGenIDChangeListeners();
128 void SkPixelRef::needsNewGenID() {
130 fUniqueGenerationID = false;
133 void SkPixelRef::cloneGenID(const SkPixelRef& that) {
134 // This is subtle. We must call that.getGenerationID() to make sure its genID isn't 0.
135 this->fGenerationID = that.getGenerationID();
136 this->fUniqueGenerationID = false;
137 that.fUniqueGenerationID = false;
140 void SkPixelRef::setPreLocked(void* pixels, size_t rowBytes, SkColorTable* ctable) {
141 #ifndef SK_IGNORE_PIXELREF_SETPRELOCKED
142 // only call me in your constructor, otherwise fLockCount tracking can get
144 fRec.fPixels = pixels;
145 fRec.fColorTable = ctable;
146 fRec.fRowBytes = rowBytes;
147 fLockCount = SKPIXELREF_PRELOCKED_LOCKCOUNT;
152 void SkPixelRef::flatten(SkWriteBuffer& buffer) const {
153 this->INHERITED::flatten(buffer);
154 fInfo.flatten(buffer);
155 buffer.writeBool(fIsImmutable);
156 // We write the gen ID into the picture for within-process recording. This
157 // is safe since the same genID will never refer to two different sets of
158 // pixels (barring overflow). However, each process has its own "namespace"
159 // of genIDs. So for cross-process recording we write a zero which will
160 // trigger assignment of a new genID in playback.
161 if (buffer.isCrossProcess()) {
164 buffer.writeUInt(fGenerationID);
165 fUniqueGenerationID = false; // Conservative, a copy is probably about to exist.
169 bool SkPixelRef::lockPixels(LockRec* rec) {
170 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount);
173 SkAutoMutexAcquire ac(*fMutex);
175 if (1 == ++fLockCount) {
176 SkASSERT(fRec.isZero());
179 if (!this->onNewLockPixels(&rec)) {
182 SkASSERT(!rec.isZero()); // else why did onNewLock return true?
190 bool SkPixelRef::lockPixels() {
192 return this->lockPixels(&rec);
195 void SkPixelRef::unlockPixels() {
196 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount);
199 SkAutoMutexAcquire ac(*fMutex);
201 SkASSERT(fLockCount > 0);
202 if (0 == --fLockCount) {
203 // don't call onUnlockPixels unless onLockPixels succeeded
205 this->onUnlockPixels();
208 SkASSERT(fRec.isZero());
214 bool SkPixelRef::lockPixelsAreWritable() const {
215 return this->onLockPixelsAreWritable();
218 bool SkPixelRef::onLockPixelsAreWritable() const {
222 bool SkPixelRef::onImplementsDecodeInto() {
226 bool SkPixelRef::onDecodeInto(int pow2, SkBitmap* bitmap) {
230 uint32_t SkPixelRef::getGenerationID() const {
231 if (0 == fGenerationID) {
232 fGenerationID = SkNextPixelRefGenerationID();
233 fUniqueGenerationID = true; // The only time we can be sure of this!
235 return fGenerationID;
238 void SkPixelRef::addGenIDChangeListener(GenIDChangeListener* listener) {
239 if (NULL == listener || !fUniqueGenerationID) {
240 // No point in tracking this if we're not going to call it.
244 *fGenIDChangeListeners.append() = listener;
247 void SkPixelRef::callGenIDChangeListeners() {
248 // We don't invalidate ourselves if we think another SkPixelRef is sharing our genID.
249 if (fUniqueGenerationID) {
250 for (int i = 0; i < fGenIDChangeListeners.count(); i++) {
251 fGenIDChangeListeners[i]->onChange();
254 // Listeners get at most one shot, so whether these triggered or not, blow them away.
255 fGenIDChangeListeners.deleteAll();
258 void SkPixelRef::notifyPixelsChanged() {
261 SkDebugf("========== notifyPixelsChanged called on immutable pixelref");
264 this->callGenIDChangeListeners();
265 this->needsNewGenID();
268 void SkPixelRef::changeAlphaType(SkAlphaType at) {
269 *const_cast<SkAlphaType*>(&fInfo.fAlphaType) = at;
272 void SkPixelRef::setImmutable() {
276 bool SkPixelRef::readPixels(SkBitmap* dst, const SkIRect* subset) {
277 return this->onReadPixels(dst, subset);
280 bool SkPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) {
284 SkData* SkPixelRef::onRefEncodedData() {
288 size_t SkPixelRef::getAllocatedSizeInBytes() const {
292 ///////////////////////////////////////////////////////////////////////////////
294 #ifdef SK_BUILD_FOR_ANDROID
295 void SkPixelRef::globalRef(void* data) {
299 void SkPixelRef::globalUnref() {