SkPixelRef.cpp revision 728f2a62526c8643ba5d234e6f570a4ebd06271a
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, SkBaseMutex* mutex) { 86 this->setMutex(mutex); 87 fInfo = info; 88 fPixels = NULL; 89 fColorTable = NULL; // we do not track ownership of this 90 fLockCount = 0; 91 this->needsNewGenID(); 92 fIsImmutable = false; 93 fPreLocked = false; 94} 95 96SkPixelRef::SkPixelRef(const SkImageInfo& info) { 97 this->setMutex(NULL); 98 fInfo = info; 99 fPixels = NULL; 100 fColorTable = NULL; // we do not track ownership of this 101 fLockCount = 0; 102 this->needsNewGenID(); 103 fIsImmutable = false; 104 fPreLocked = false; 105} 106 107#ifdef SK_SUPPORT_LEGACY_PIXELREF_CONSTRUCTOR 108// THIS GUY IS DEPRECATED -- don't use me! 109SkPixelRef::SkPixelRef(SkBaseMutex* mutex) { 110 this->setMutex(mutex); 111 // Fill with dummy values. 112 sk_bzero(&fInfo, sizeof(fInfo)); 113 fPixels = NULL; 114 fColorTable = NULL; // we do not track ownership of this 115 fLockCount = 0; 116 this->needsNewGenID(); 117 fIsImmutable = false; 118 fPreLocked = false; 119} 120#endif 121 122SkPixelRef::SkPixelRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex) 123 : INHERITED(buffer) { 124 this->setMutex(mutex); 125 fPixels = NULL; 126 fColorTable = NULL; // we do not track ownership of this 127 fLockCount = 0; 128 fIsImmutable = buffer.readBool(); 129 fGenerationID = buffer.readUInt(); 130 fUniqueGenerationID = false; // Conservatively assuming the original still exists. 131 fPreLocked = false; 132} 133 134SkPixelRef::~SkPixelRef() { 135 this->callGenIDChangeListeners(); 136} 137 138void SkPixelRef::needsNewGenID() { 139 fGenerationID = 0; 140 fUniqueGenerationID = false; 141} 142 143void SkPixelRef::cloneGenID(const SkPixelRef& that) { 144 // This is subtle. We must call that.getGenerationID() to make sure its genID isn't 0. 145 this->fGenerationID = that.getGenerationID(); 146 this->fUniqueGenerationID = false; 147 that.fUniqueGenerationID = false; 148} 149 150void SkPixelRef::setPreLocked(void* pixels, SkColorTable* ctable) { 151#ifndef SK_IGNORE_PIXELREF_SETPRELOCKED 152 // only call me in your constructor, otherwise fLockCount tracking can get 153 // out of sync. 154 fPixels = pixels; 155 fColorTable = ctable; 156 fLockCount = SKPIXELREF_PRELOCKED_LOCKCOUNT; 157 fPreLocked = true; 158#endif 159} 160 161void SkPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const { 162 this->INHERITED::flatten(buffer); 163 buffer.writeBool(fIsImmutable); 164 // We write the gen ID into the picture for within-process recording. This 165 // is safe since the same genID will never refer to two different sets of 166 // pixels (barring overflow). However, each process has its own "namespace" 167 // of genIDs. So for cross-process recording we write a zero which will 168 // trigger assignment of a new genID in playback. 169 if (buffer.isCrossProcess()) { 170 buffer.writeUInt(0); 171 } else { 172 buffer.writeUInt(fGenerationID); 173 fUniqueGenerationID = false; // Conservative, a copy is probably about to exist. 174 } 175} 176 177void SkPixelRef::lockPixels() { 178 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); 179 180 if (!fPreLocked) { 181 SkAutoMutexAcquire ac(*fMutex); 182 183 if (1 == ++fLockCount) { 184 fPixels = this->onLockPixels(&fColorTable); 185 } 186 } 187} 188 189void SkPixelRef::unlockPixels() { 190 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); 191 192 if (!fPreLocked) { 193 SkAutoMutexAcquire ac(*fMutex); 194 195 SkASSERT(fLockCount > 0); 196 if (0 == --fLockCount) { 197 this->onUnlockPixels(); 198 fPixels = NULL; 199 fColorTable = NULL; 200 } 201 } 202} 203 204bool SkPixelRef::lockPixelsAreWritable() const { 205 return this->onLockPixelsAreWritable(); 206} 207 208bool SkPixelRef::onLockPixelsAreWritable() const { 209 return true; 210} 211 212bool SkPixelRef::onImplementsDecodeInto() { 213 return false; 214} 215 216bool SkPixelRef::onDecodeInto(int pow2, SkBitmap* bitmap) { 217 return false; 218} 219 220uint32_t SkPixelRef::getGenerationID() const { 221 if (0 == fGenerationID) { 222 fGenerationID = SkNextPixelRefGenerationID(); 223 fUniqueGenerationID = true; // The only time we can be sure of this! 224 } 225 return fGenerationID; 226} 227 228void SkPixelRef::addGenIDChangeListener(GenIDChangeListener* listener) { 229 if (NULL == listener || !fUniqueGenerationID) { 230 // No point in tracking this if we're not going to call it. 231 SkDELETE(listener); 232 return; 233 } 234 *fGenIDChangeListeners.append() = listener; 235} 236 237void SkPixelRef::callGenIDChangeListeners() { 238 // We don't invalidate ourselves if we think another SkPixelRef is sharing our genID. 239 if (fUniqueGenerationID) { 240 for (int i = 0; i < fGenIDChangeListeners.count(); i++) { 241 fGenIDChangeListeners[i]->onChange(); 242 } 243 } 244 // Listeners get at most one shot, so whether these triggered or not, blow them away. 245 fGenIDChangeListeners.deleteAll(); 246} 247 248void SkPixelRef::notifyPixelsChanged() { 249#ifdef SK_DEBUG 250 if (fIsImmutable) { 251 SkDebugf("========== notifyPixelsChanged called on immutable pixelref"); 252 } 253#endif 254 this->callGenIDChangeListeners(); 255 this->needsNewGenID(); 256} 257 258void SkPixelRef::setImmutable() { 259 fIsImmutable = true; 260} 261 262bool SkPixelRef::readPixels(SkBitmap* dst, const SkIRect* subset) { 263 return this->onReadPixels(dst, subset); 264} 265 266bool SkPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) { 267 return false; 268} 269 270SkData* SkPixelRef::onRefEncodedData() { 271 return NULL; 272} 273 274size_t SkPixelRef::getAllocatedSizeInBytes() const { 275 return 0; 276} 277 278/////////////////////////////////////////////////////////////////////////////// 279 280#ifdef SK_BUILD_FOR_ANDROID 281void SkPixelRef::globalRef(void* data) { 282 this->ref(); 283} 284 285void SkPixelRef::globalUnref() { 286 this->unref(); 287} 288#endif 289