SkPixelRef.cpp revision f3aead2829fa515f790631ee65784a7b9b073343
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 LockRec rec; 172 if (!this->onNewLockPixels(&rec)) { 173 return false; 174 } 175 fRec = rec; 176 } 177 } 178 *rec = fRec; 179 return true; 180} 181 182bool SkPixelRef::lockPixels() { 183 LockRec rec; 184 return this->lockPixels(&rec); 185} 186 187void SkPixelRef::unlockPixels() { 188 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); 189 190 if (!fPreLocked) { 191 SkAutoMutexAcquire ac(*fMutex); 192 193 SkASSERT(fLockCount > 0); 194 if (0 == --fLockCount) { 195 this->onUnlockPixels(); 196 fRec.zero(); 197 } 198 } 199} 200 201bool SkPixelRef::lockPixelsAreWritable() const { 202 return this->onLockPixelsAreWritable(); 203} 204 205bool SkPixelRef::onLockPixelsAreWritable() const { 206 return true; 207} 208 209bool SkPixelRef::onImplementsDecodeInto() { 210 return false; 211} 212 213bool SkPixelRef::onDecodeInto(int pow2, SkBitmap* bitmap) { 214 return false; 215} 216 217uint32_t SkPixelRef::getGenerationID() const { 218 if (0 == fGenerationID) { 219 fGenerationID = SkNextPixelRefGenerationID(); 220 fUniqueGenerationID = true; // The only time we can be sure of this! 221 } 222 return fGenerationID; 223} 224 225void SkPixelRef::addGenIDChangeListener(GenIDChangeListener* listener) { 226 if (NULL == listener || !fUniqueGenerationID) { 227 // No point in tracking this if we're not going to call it. 228 SkDELETE(listener); 229 return; 230 } 231 *fGenIDChangeListeners.append() = listener; 232} 233 234void SkPixelRef::callGenIDChangeListeners() { 235 // We don't invalidate ourselves if we think another SkPixelRef is sharing our genID. 236 if (fUniqueGenerationID) { 237 for (int i = 0; i < fGenIDChangeListeners.count(); i++) { 238 fGenIDChangeListeners[i]->onChange(); 239 } 240 } 241 // Listeners get at most one shot, so whether these triggered or not, blow them away. 242 fGenIDChangeListeners.deleteAll(); 243} 244 245void SkPixelRef::notifyPixelsChanged() { 246#ifdef SK_DEBUG 247 if (fIsImmutable) { 248 SkDebugf("========== notifyPixelsChanged called on immutable pixelref"); 249 } 250#endif 251 this->callGenIDChangeListeners(); 252 this->needsNewGenID(); 253} 254 255void SkPixelRef::setImmutable() { 256 fIsImmutable = true; 257} 258 259bool SkPixelRef::readPixels(SkBitmap* dst, const SkIRect* subset) { 260 return this->onReadPixels(dst, subset); 261} 262 263bool SkPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) { 264 return false; 265} 266 267SkData* SkPixelRef::onRefEncodedData() { 268 return NULL; 269} 270 271size_t SkPixelRef::getAllocatedSizeInBytes() const { 272 return 0; 273} 274 275/////////////////////////////////////////////////////////////////////////////// 276 277#ifdef SK_SUPPORT_LEGACY_ONLOCKPIXELS 278 279void* SkPixelRef::onLockPixels(SkColorTable** ctable) { 280 return NULL; 281} 282 283bool SkPixelRef::onNewLockPixels(LockRec* rec) { 284 SkColorTable* ctable; 285 void* pixels = this->onLockPixels(&ctable); 286 if (!pixels) { 287 return false; 288 } 289 290 rec->fPixels = pixels; 291 rec->fColorTable = ctable; 292 rec->fRowBytes = 0; // callers don't currently need this (thank goodness) 293 return true; 294} 295 296#endif 297 298/////////////////////////////////////////////////////////////////////////////// 299 300#ifdef SK_BUILD_FOR_ANDROID 301void SkPixelRef::globalRef(void* data) { 302 this->ref(); 303} 304 305void SkPixelRef::globalUnref() { 306 this->unref(); 307} 308#endif 309