SkPixelRef.cpp revision 93b40594b78ab40170b5a7fa0811fa95ab8fc30f
1
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8#include "SkPixelRef.h"
9#include "SkFlattenableBuffers.h"
10#include "SkThread.h"
11
12#ifdef SK_USE_POSIX_THREADS
13
14    static SkBaseMutex gPixelRefMutexRing[] = {
15        { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER },
16        { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER },
17        { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER },
18        { PTHREAD_MUTEX_INITIALIZER }, { PTHREAD_MUTEX_INITIALIZER },
19
20        { 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
25        { 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
30        { 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    };
35
36    // must be a power-of-2. undef to just use 1 mutex
37    #define PIXELREF_MUTEX_RING_COUNT SK_ARRAY_COUNT(gPixelRefMutexRing)
38
39#else // not pthreads
40
41    // must be a power-of-2. undef to just use 1 mutex
42    #define PIXELREF_MUTEX_RING_COUNT       32
43    static SkBaseMutex gPixelRefMutexRing[PIXELREF_MUTEX_RING_COUNT];
44
45#endif
46
47static SkBaseMutex* get_default_mutex() {
48    static int32_t gPixelRefMutexRingIndex;
49
50    SkASSERT(SkIsPow2(PIXELREF_MUTEX_RING_COUNT));
51
52    // atomic_inc might be overkill here. It may be fine if once in a while
53    // we hit a race-condition and two subsequent calls get the same index...
54    int index = sk_atomic_inc(&gPixelRefMutexRingIndex);
55    return &gPixelRefMutexRing[index & (PIXELREF_MUTEX_RING_COUNT - 1)];
56}
57
58///////////////////////////////////////////////////////////////////////////////
59
60int32_t SkNextPixelRefGenerationID();
61
62int32_t SkNextPixelRefGenerationID() {
63    static int32_t  gPixelRefGenerationID;
64    // do a loop in case our global wraps around, as we never want to
65    // return a 0
66    int32_t genID;
67    do {
68        genID = sk_atomic_inc(&gPixelRefGenerationID) + 1;
69    } while (0 == genID);
70    return genID;
71}
72
73///////////////////////////////////////////////////////////////////////////////
74
75void SkPixelRef::setMutex(SkBaseMutex* mutex) {
76    if (NULL == mutex) {
77        mutex = get_default_mutex();
78    }
79    fMutex = mutex;
80}
81
82// just need a > 0 value, so pick a funny one to aid in debugging
83#define SKPIXELREF_PRELOCKED_LOCKCOUNT     123456789
84
85SkPixelRef::SkPixelRef(const SkImageInfo& info) {
86    this->setMutex(NULL);
87    fInfo = info;
88    fRec.zero();
89    fLockCount = 0;
90    this->needsNewGenID();
91    fIsImmutable = false;
92    fPreLocked = false;
93}
94
95SkPixelRef::SkPixelRef(const SkImageInfo& info, SkBaseMutex* mutex) {
96    this->setMutex(mutex);
97    fInfo = info;
98    fRec.zero();
99    fLockCount = 0;
100    this->needsNewGenID();
101    fIsImmutable = false;
102    fPreLocked = false;
103}
104
105SkPixelRef::SkPixelRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex)
106        : INHERITED(buffer) {
107    this->setMutex(mutex);
108
109    fInfo.unflatten(buffer);
110    fRec.zero();
111    fLockCount = 0;
112    fIsImmutable = buffer.readBool();
113    fGenerationID = buffer.readUInt();
114    fUniqueGenerationID = false;  // Conservatively assuming the original still exists.
115    fPreLocked = false;
116}
117
118SkPixelRef::~SkPixelRef() {
119    this->callGenIDChangeListeners();
120}
121
122void SkPixelRef::needsNewGenID() {
123    fGenerationID = 0;
124    fUniqueGenerationID = false;
125}
126
127void SkPixelRef::cloneGenID(const SkPixelRef& that) {
128    // This is subtle.  We must call that.getGenerationID() to make sure its genID isn't 0.
129    this->fGenerationID = that.getGenerationID();
130    this->fUniqueGenerationID = false;
131    that.fUniqueGenerationID = false;
132}
133
134void SkPixelRef::setPreLocked(void* pixels, size_t rowBytes, SkColorTable* ctable) {
135#ifndef SK_IGNORE_PIXELREF_SETPRELOCKED
136    // only call me in your constructor, otherwise fLockCount tracking can get
137    // out of sync.
138    fRec.fPixels = pixels;
139    fRec.fColorTable = ctable;
140    fRec.fRowBytes = rowBytes;
141    fLockCount = SKPIXELREF_PRELOCKED_LOCKCOUNT;
142    fPreLocked = true;
143#endif
144}
145
146void SkPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const {
147    this->INHERITED::flatten(buffer);
148
149    fInfo.flatten(buffer);
150    buffer.writeBool(fIsImmutable);
151    // We write the gen ID into the picture for within-process recording. This
152    // is safe since the same genID will never refer to two different sets of
153    // pixels (barring overflow). However, each process has its own "namespace"
154    // of genIDs. So for cross-process recording we write a zero which will
155    // trigger assignment of a new genID in playback.
156    if (buffer.isCrossProcess()) {
157        buffer.writeUInt(0);
158    } else {
159        buffer.writeUInt(fGenerationID);
160        fUniqueGenerationID = false;  // Conservative, a copy is probably about to exist.
161    }
162}
163
164bool SkPixelRef::lockPixels(LockRec* rec) {
165    SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount);
166
167    if (!fPreLocked) {
168        SkAutoMutexAcquire  ac(*fMutex);
169
170        if (1 == ++fLockCount) {
171            SkASSERT(fRec.isZero());
172
173            LockRec rec;
174            if (!this->onNewLockPixels(&rec)) {
175                return false;
176            }
177            SkASSERT(!rec.isZero());    // else why did onNewLock return true?
178            fRec = rec;
179        }
180    }
181    *rec = fRec;
182    return true;
183}
184
185bool SkPixelRef::lockPixels() {
186    LockRec rec;
187    return this->lockPixels(&rec);
188}
189
190void SkPixelRef::unlockPixels() {
191    SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount);
192
193    if (!fPreLocked) {
194        SkAutoMutexAcquire  ac(*fMutex);
195
196        SkASSERT(fLockCount > 0);
197        if (0 == --fLockCount) {
198            // don't call onUnlockPixels unless onLockPixels succeeded
199            if (fRec.fPixels) {
200                this->onUnlockPixels();
201                fRec.zero();
202            } else {
203                SkASSERT(fRec.isZero());
204            }
205        }
206    }
207}
208
209bool SkPixelRef::lockPixelsAreWritable() const {
210    return this->onLockPixelsAreWritable();
211}
212
213bool SkPixelRef::onLockPixelsAreWritable() const {
214    return true;
215}
216
217bool SkPixelRef::onImplementsDecodeInto() {
218    return false;
219}
220
221bool SkPixelRef::onDecodeInto(int pow2, SkBitmap* bitmap) {
222    return false;
223}
224
225uint32_t SkPixelRef::getGenerationID() const {
226    if (0 == fGenerationID) {
227        fGenerationID = SkNextPixelRefGenerationID();
228        fUniqueGenerationID = true;  // The only time we can be sure of this!
229    }
230    return fGenerationID;
231}
232
233void SkPixelRef::addGenIDChangeListener(GenIDChangeListener* listener) {
234    if (NULL == listener || !fUniqueGenerationID) {
235        // No point in tracking this if we're not going to call it.
236        SkDELETE(listener);
237        return;
238    }
239    *fGenIDChangeListeners.append() = listener;
240}
241
242void SkPixelRef::callGenIDChangeListeners() {
243    // We don't invalidate ourselves if we think another SkPixelRef is sharing our genID.
244    if (fUniqueGenerationID) {
245        for (int i = 0; i < fGenIDChangeListeners.count(); i++) {
246            fGenIDChangeListeners[i]->onChange();
247        }
248    }
249    // Listeners get at most one shot, so whether these triggered or not, blow them away.
250    fGenIDChangeListeners.deleteAll();
251}
252
253void SkPixelRef::notifyPixelsChanged() {
254#ifdef SK_DEBUG
255    if (fIsImmutable) {
256        SkDebugf("========== notifyPixelsChanged called on immutable pixelref");
257    }
258#endif
259    this->callGenIDChangeListeners();
260    this->needsNewGenID();
261}
262
263void SkPixelRef::setImmutable() {
264    fIsImmutable = true;
265}
266
267bool SkPixelRef::readPixels(SkBitmap* dst, const SkIRect* subset) {
268    return this->onReadPixels(dst, subset);
269}
270
271bool SkPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) {
272    return false;
273}
274
275SkData* SkPixelRef::onRefEncodedData() {
276    return NULL;
277}
278
279size_t SkPixelRef::getAllocatedSizeInBytes() const {
280    return 0;
281}
282
283///////////////////////////////////////////////////////////////////////////////
284
285#ifdef SK_SUPPORT_LEGACY_ONLOCKPIXELS
286
287void* SkPixelRef::onLockPixels(SkColorTable** ctable) {
288    return NULL;
289}
290
291bool SkPixelRef::onNewLockPixels(LockRec* rec) {
292    SkColorTable* ctable;
293    void* pixels = this->onLockPixels(&ctable);
294    if (!pixels) {
295        return false;
296    }
297
298    rec->fPixels = pixels;
299    rec->fColorTable = ctable;
300    rec->fRowBytes = 0; // callers don't currently need this (thank goodness)
301    return true;
302}
303
304#endif
305
306///////////////////////////////////////////////////////////////////////////////
307
308#ifdef SK_BUILD_FOR_ANDROID
309void SkPixelRef::globalRef(void* data) {
310    this->ref();
311}
312
313void SkPixelRef::globalUnref() {
314    this->unref();
315}
316#endif
317