| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 #include "SkPixelRef.h" | 8 #include "SkPixelRef.h" |
| 9 #include "SkFlattenableBuffers.h" | 9 #include "SkFlattenableBuffers.h" |
| 10 #include "SkThread.h" | 10 #include "SkThread.h" |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 void SkPixelRef::setMutex(SkBaseMutex* mutex) { | 75 void SkPixelRef::setMutex(SkBaseMutex* mutex) { |
| 76 if (NULL == mutex) { | 76 if (NULL == mutex) { |
| 77 mutex = get_default_mutex(); | 77 mutex = get_default_mutex(); |
| 78 } | 78 } |
| 79 fMutex = mutex; | 79 fMutex = mutex; |
| 80 } | 80 } |
| 81 | 81 |
| 82 // just need a > 0 value, so pick a funny one to aid in debugging | 82 // just need a > 0 value, so pick a funny one to aid in debugging |
| 83 #define SKPIXELREF_PRELOCKED_LOCKCOUNT 123456789 | 83 #define SKPIXELREF_PRELOCKED_LOCKCOUNT 123456789 |
| 84 | 84 |
| 85 SkPixelRef::SkPixelRef(const SkImageInfo& info) : fInfo(info) { | 85 SkPixelRef::SkPixelRef(const SkImageInfo& info, SkBaseMutex* mutex) : fInfo(info
) { |
| 86 this->setMutex(NULL); | 86 this->setMutex(mutex); |
| 87 fRec.zero(); | 87 fPixels = NULL; |
| 88 fColorTable = NULL; // we do not track ownership of this |
| 88 fLockCount = 0; | 89 fLockCount = 0; |
| 89 this->needsNewGenID(); | 90 this->needsNewGenID(); |
| 90 fIsImmutable = false; | 91 fIsImmutable = false; |
| 91 fPreLocked = false; | 92 fPreLocked = false; |
| 92 } | 93 } |
| 93 | 94 |
| 94 | 95 SkPixelRef::SkPixelRef(const SkImageInfo& info) : fInfo(info) { |
| 95 SkPixelRef::SkPixelRef(const SkImageInfo& info, SkBaseMutex* mutex) : fInfo(info
) { | 96 this->setMutex(NULL); |
| 96 this->setMutex(mutex); | 97 fPixels = NULL; |
| 97 fRec.zero(); | 98 fColorTable = NULL; // we do not track ownership of this |
| 98 fLockCount = 0; | 99 fLockCount = 0; |
| 99 this->needsNewGenID(); | 100 this->needsNewGenID(); |
| 100 fIsImmutable = false; | 101 fIsImmutable = false; |
| 101 fPreLocked = false; | 102 fPreLocked = false; |
| 102 } | 103 } |
| 103 | 104 |
| 104 static SkImageInfo read_info(SkFlattenableReadBuffer& buffer) { | 105 static SkImageInfo read_info(SkFlattenableReadBuffer& buffer) { |
| 105 SkImageInfo info; | 106 SkImageInfo info; |
| 106 info.unflatten(buffer); | 107 info.unflatten(buffer); |
| 107 return info; | 108 return info; |
| 108 } | 109 } |
| 109 | 110 |
| 110 SkPixelRef::SkPixelRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex) | 111 SkPixelRef::SkPixelRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex) |
| 111 : INHERITED(buffer) | 112 : INHERITED(buffer) |
| 112 , fInfo(read_info(buffer)) | 113 , fInfo(read_info(buffer)) |
| 113 { | 114 { |
| 114 this->setMutex(mutex); | 115 this->setMutex(mutex); |
| 115 fRec.zero(); | 116 fPixels = NULL; |
| 117 fColorTable = NULL; // we do not track ownership of this |
| 116 fLockCount = 0; | 118 fLockCount = 0; |
| 117 fIsImmutable = buffer.readBool(); | 119 fIsImmutable = buffer.readBool(); |
| 118 fGenerationID = buffer.readUInt(); | 120 fGenerationID = buffer.readUInt(); |
| 119 fUniqueGenerationID = false; // Conservatively assuming the original still
exists. | 121 fUniqueGenerationID = false; // Conservatively assuming the original still
exists. |
| 120 fPreLocked = false; | 122 fPreLocked = false; |
| 121 } | 123 } |
| 122 | 124 |
| 123 SkPixelRef::~SkPixelRef() { | 125 SkPixelRef::~SkPixelRef() { |
| 124 this->callGenIDChangeListeners(); | 126 this->callGenIDChangeListeners(); |
| 125 } | 127 } |
| 126 | 128 |
| 127 void SkPixelRef::needsNewGenID() { | 129 void SkPixelRef::needsNewGenID() { |
| 128 fGenerationID = 0; | 130 fGenerationID = 0; |
| 129 fUniqueGenerationID = false; | 131 fUniqueGenerationID = false; |
| 130 } | 132 } |
| 131 | 133 |
| 132 void SkPixelRef::cloneGenID(const SkPixelRef& that) { | 134 void SkPixelRef::cloneGenID(const SkPixelRef& that) { |
| 133 // This is subtle. We must call that.getGenerationID() to make sure its gen
ID isn't 0. | 135 // This is subtle. We must call that.getGenerationID() to make sure its gen
ID isn't 0. |
| 134 this->fGenerationID = that.getGenerationID(); | 136 this->fGenerationID = that.getGenerationID(); |
| 135 this->fUniqueGenerationID = false; | 137 this->fUniqueGenerationID = false; |
| 136 that.fUniqueGenerationID = false; | 138 that.fUniqueGenerationID = false; |
| 137 } | 139 } |
| 138 | 140 |
| 139 void SkPixelRef::setPreLocked(void* pixels, size_t rowBytes, SkColorTable* ctabl
e) { | 141 void SkPixelRef::setPreLocked(void* pixels, SkColorTable* ctable) { |
| 140 #ifndef SK_IGNORE_PIXELREF_SETPRELOCKED | 142 #ifndef SK_IGNORE_PIXELREF_SETPRELOCKED |
| 141 // only call me in your constructor, otherwise fLockCount tracking can get | 143 // only call me in your constructor, otherwise fLockCount tracking can get |
| 142 // out of sync. | 144 // out of sync. |
| 143 fRec.fPixels = pixels; | 145 fPixels = pixels; |
| 144 fRec.fColorTable = ctable; | 146 fColorTable = ctable; |
| 145 fRec.fRowBytes = rowBytes; | |
| 146 fLockCount = SKPIXELREF_PRELOCKED_LOCKCOUNT; | 147 fLockCount = SKPIXELREF_PRELOCKED_LOCKCOUNT; |
| 147 fPreLocked = true; | 148 fPreLocked = true; |
| 148 #endif | 149 #endif |
| 149 } | 150 } |
| 150 | 151 |
| 151 void SkPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const { | 152 void SkPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const { |
| 152 this->INHERITED::flatten(buffer); | 153 this->INHERITED::flatten(buffer); |
| 153 fInfo.flatten(buffer); | 154 fInfo.flatten(buffer); |
| 154 buffer.writeBool(fIsImmutable); | 155 buffer.writeBool(fIsImmutable); |
| 155 // We write the gen ID into the picture for within-process recording. This | 156 // We write the gen ID into the picture for within-process recording. This |
| 156 // is safe since the same genID will never refer to two different sets of | 157 // is safe since the same genID will never refer to two different sets of |
| 157 // pixels (barring overflow). However, each process has its own "namespace" | 158 // pixels (barring overflow). However, each process has its own "namespace" |
| 158 // of genIDs. So for cross-process recording we write a zero which will | 159 // of genIDs. So for cross-process recording we write a zero which will |
| 159 // trigger assignment of a new genID in playback. | 160 // trigger assignment of a new genID in playback. |
| 160 if (buffer.isCrossProcess()) { | 161 if (buffer.isCrossProcess()) { |
| 161 buffer.writeUInt(0); | 162 buffer.writeUInt(0); |
| 162 } else { | 163 } else { |
| 163 buffer.writeUInt(fGenerationID); | 164 buffer.writeUInt(fGenerationID); |
| 164 fUniqueGenerationID = false; // Conservative, a copy is probably about
to exist. | 165 fUniqueGenerationID = false; // Conservative, a copy is probably about
to exist. |
| 165 } | 166 } |
| 166 } | 167 } |
| 167 | 168 |
| 168 bool SkPixelRef::lockPixels(LockRec* rec) { | 169 void SkPixelRef::lockPixels() { |
| 169 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); | 170 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); |
| 170 | 171 |
| 171 if (!fPreLocked) { | 172 if (!fPreLocked) { |
| 172 SkAutoMutexAcquire ac(*fMutex); | 173 SkAutoMutexAcquire ac(*fMutex); |
| 173 | 174 |
| 174 if (1 == ++fLockCount) { | 175 if (1 == ++fLockCount) { |
| 175 SkASSERT(fRec.isZero()); | 176 fPixels = this->onLockPixels(&fColorTable); |
| 176 | 177 // If onLockPixels failed, it will return NULL |
| 177 LockRec rec; | 178 if (NULL == fPixels) { |
| 178 if (!this->onNewLockPixels(&rec)) { | 179 fColorTable = NULL; |
| 179 return false; | |
| 180 } | 180 } |
| 181 SkASSERT(!rec.isZero()); // else why did onNewLock return true? | |
| 182 fRec = rec; | |
| 183 } | 181 } |
| 184 } | 182 } |
| 185 *rec = fRec; | |
| 186 return true; | |
| 187 } | |
| 188 | |
| 189 bool SkPixelRef::lockPixels() { | |
| 190 LockRec rec; | |
| 191 return this->lockPixels(&rec); | |
| 192 } | 183 } |
| 193 | 184 |
| 194 void SkPixelRef::unlockPixels() { | 185 void SkPixelRef::unlockPixels() { |
| 195 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); | 186 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); |
| 196 | 187 |
| 197 if (!fPreLocked) { | 188 if (!fPreLocked) { |
| 198 SkAutoMutexAcquire ac(*fMutex); | 189 SkAutoMutexAcquire ac(*fMutex); |
| 199 | 190 |
| 200 SkASSERT(fLockCount > 0); | 191 SkASSERT(fLockCount > 0); |
| 201 if (0 == --fLockCount) { | 192 if (0 == --fLockCount) { |
| 202 // don't call onUnlockPixels unless onLockPixels succeeded | 193 // don't call onUnlockPixels unless onLockPixels succeeded |
| 203 if (fRec.fPixels) { | 194 if (fPixels) { |
| 204 this->onUnlockPixels(); | 195 this->onUnlockPixels(); |
| 205 fRec.zero(); | 196 fPixels = NULL; |
| 197 fColorTable = NULL; |
| 206 } else { | 198 } else { |
| 207 SkASSERT(fRec.isZero()); | 199 SkASSERT(NULL == fColorTable); |
| 208 } | 200 } |
| 209 } | 201 } |
| 210 } | 202 } |
| 211 } | 203 } |
| 212 | 204 |
| 213 bool SkPixelRef::lockPixelsAreWritable() const { | 205 bool SkPixelRef::lockPixelsAreWritable() const { |
| 214 return this->onLockPixelsAreWritable(); | 206 return this->onLockPixelsAreWritable(); |
| 215 } | 207 } |
| 216 | 208 |
| 217 bool SkPixelRef::onLockPixelsAreWritable() const { | 209 bool SkPixelRef::onLockPixelsAreWritable() const { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 SkData* SkPixelRef::onRefEncodedData() { | 271 SkData* SkPixelRef::onRefEncodedData() { |
| 280 return NULL; | 272 return NULL; |
| 281 } | 273 } |
| 282 | 274 |
| 283 size_t SkPixelRef::getAllocatedSizeInBytes() const { | 275 size_t SkPixelRef::getAllocatedSizeInBytes() const { |
| 284 return 0; | 276 return 0; |
| 285 } | 277 } |
| 286 | 278 |
| 287 /////////////////////////////////////////////////////////////////////////////// | 279 /////////////////////////////////////////////////////////////////////////////// |
| 288 | 280 |
| 289 #ifdef SK_SUPPORT_LEGACY_ONLOCKPIXELS | |
| 290 | |
| 291 void* SkPixelRef::onLockPixels(SkColorTable** ctable) { | |
| 292 return NULL; | |
| 293 } | |
| 294 | |
| 295 bool SkPixelRef::onNewLockPixels(LockRec* rec) { | |
| 296 SkColorTable* ctable; | |
| 297 void* pixels = this->onLockPixels(&ctable); | |
| 298 if (!pixels) { | |
| 299 return false; | |
| 300 } | |
| 301 | |
| 302 rec->fPixels = pixels; | |
| 303 rec->fColorTable = ctable; | |
| 304 rec->fRowBytes = 0; // callers don't currently need this (thank goodness) | |
| 305 return true; | |
| 306 } | |
| 307 | |
| 308 #endif | |
| 309 | |
| 310 /////////////////////////////////////////////////////////////////////////////// | |
| 311 | |
| 312 #ifdef SK_BUILD_FOR_ANDROID | 281 #ifdef SK_BUILD_FOR_ANDROID |
| 313 void SkPixelRef::globalRef(void* data) { | 282 void SkPixelRef::globalRef(void* data) { |
| 314 this->ref(); | 283 this->ref(); |
| 315 } | 284 } |
| 316 | 285 |
| 317 void SkPixelRef::globalUnref() { | 286 void SkPixelRef::globalUnref() { |
| 318 this->unref(); | 287 this->unref(); |
| 319 } | 288 } |
| 320 #endif | 289 #endif |
| OLD | NEW |