| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkBitmapCache.h" | 8 #include "SkBitmapCache.h" |
| 9 #include "SkPixelRef.h" | 9 #include "SkPixelRef.h" |
| 10 #include "SkThread.h" | 10 #include "SkThread.h" |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 // Neither ID is unique any more. | 153 // Neither ID is unique any more. |
| 154 // (These & ~1u are actually redundant. that.getGenerationID() just did it
for us.) | 154 // (These & ~1u are actually redundant. that.getGenerationID() just did it
for us.) |
| 155 this->fTaggedGenID.store(genID & ~1u); | 155 this->fTaggedGenID.store(genID & ~1u); |
| 156 that. fTaggedGenID.store(genID & ~1u); | 156 that. fTaggedGenID.store(genID & ~1u); |
| 157 | 157 |
| 158 // This method isn't threadsafe, so these asserts should be fine. | 158 // This method isn't threadsafe, so these asserts should be fine. |
| 159 SkASSERT(!this->genIDIsUnique()); | 159 SkASSERT(!this->genIDIsUnique()); |
| 160 SkASSERT(!that. genIDIsUnique()); | 160 SkASSERT(!that. genIDIsUnique()); |
| 161 } | 161 } |
| 162 | 162 |
| 163 static void validate_pixels_ctable(const void* pixels, const SkColorTable* ctabl
e, SkColorType ct) { | |
| 164 SkASSERT(pixels); | |
| 165 if (kIndex_8_SkColorType == ct) { | |
| 166 SkASSERT(ctable); | |
| 167 } else { | |
| 168 SkASSERT(NULL == ctable); | |
| 169 } | |
| 170 } | |
| 171 | |
| 172 void SkPixelRef::setPreLocked(void* pixels, size_t rowBytes, SkColorTable* ctabl
e) { | 163 void SkPixelRef::setPreLocked(void* pixels, size_t rowBytes, SkColorTable* ctabl
e) { |
| 173 #ifndef SK_IGNORE_PIXELREF_SETPRELOCKED | 164 #ifndef SK_IGNORE_PIXELREF_SETPRELOCKED |
| 174 validate_pixels_ctable(pixels, ctable, fInfo.colorType()); | |
| 175 // only call me in your constructor, otherwise fLockCount tracking can get | 165 // only call me in your constructor, otherwise fLockCount tracking can get |
| 176 // out of sync. | 166 // out of sync. |
| 177 fRec.fPixels = pixels; | 167 fRec.fPixels = pixels; |
| 178 fRec.fColorTable = ctable; | 168 fRec.fColorTable = ctable; |
| 179 fRec.fRowBytes = rowBytes; | 169 fRec.fRowBytes = rowBytes; |
| 180 fLockCount = SKPIXELREF_PRELOCKED_LOCKCOUNT; | 170 fLockCount = SKPIXELREF_PRELOCKED_LOCKCOUNT; |
| 181 fPreLocked = true; | 171 fPreLocked = true; |
| 182 #endif | 172 #endif |
| 183 } | 173 } |
| 184 | 174 |
| 185 // Increments fLockCount only on success | 175 bool SkPixelRef::lockPixelsInsideMutex(LockRec* rec) { |
| 186 bool SkPixelRef::lockPixelsInsideMutex() { | |
| 187 fMutex->assertHeld(); | 176 fMutex->assertHeld(); |
| 188 | 177 |
| 178 // For historical reasons, we always inc fLockCount, even if we return false
. |
| 179 // It would be nice to change this (it seems), and only inc if we actually s
ucceed... |
| 189 if (1 == ++fLockCount) { | 180 if (1 == ++fLockCount) { |
| 190 SkASSERT(fRec.isZero()); | 181 SkASSERT(fRec.isZero()); |
| 191 if (!this->onNewLockPixels(&fRec)) { | 182 |
| 192 fRec.zero(); | 183 LockRec rec; |
| 184 if (!this->onNewLockPixels(&rec)) { |
| 193 fLockCount -= 1; // we return fLockCount unchanged if we fail. | 185 fLockCount -= 1; // we return fLockCount unchanged if we fail. |
| 194 return false; | 186 return false; |
| 195 } | 187 } |
| 188 SkASSERT(!rec.isZero()); // else why did onNewLock return true? |
| 189 fRec = rec; |
| 196 } | 190 } |
| 197 validate_pixels_ctable(fRec.fPixels, fRec.fColorTable, fInfo.colorType()); | 191 *rec = fRec; |
| 198 return true; | 192 return true; |
| 199 } | 193 } |
| 200 | 194 |
| 201 // For historical reasons, we always inc fLockCount, even if we return false. | 195 bool SkPixelRef::lockPixels(LockRec* rec) { |
| 202 // It would be nice to change this (it seems), and only inc if we actually succe
ed... | |
| 203 bool SkPixelRef::lockPixels() { | |
| 204 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); | 196 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); |
| 205 | 197 |
| 206 if (!fPreLocked) { | 198 if (fPreLocked) { |
| 199 *rec = fRec; |
| 200 return true; |
| 201 } else { |
| 207 TRACE_EVENT_BEGIN0("skia", "SkPixelRef::lockPixelsMutex"); | 202 TRACE_EVENT_BEGIN0("skia", "SkPixelRef::lockPixelsMutex"); |
| 208 SkAutoMutexAcquire ac(*fMutex); | 203 SkAutoMutexAcquire ac(*fMutex); |
| 209 TRACE_EVENT_END0("skia", "SkPixelRef::lockPixelsMutex"); | 204 TRACE_EVENT_END0("skia", "SkPixelRef::lockPixelsMutex"); |
| 210 SkDEBUGCODE(int oldCount = fLockCount;) | 205 SkDEBUGCODE(int oldCount = fLockCount;) |
| 211 bool success = this->lockPixelsInsideMutex(); | 206 bool success = this->lockPixelsInsideMutex(rec); |
| 212 // lockPixelsInsideMutex only increments the count if it succeeds. | 207 // lockPixelsInsideMutex only increments the count if it succeeds. |
| 213 SkASSERT(oldCount + (int)success == fLockCount); | 208 SkASSERT(oldCount + (int)success == fLockCount); |
| 214 | 209 |
| 215 if (!success) { | 210 if (!success) { |
| 216 // For compatibility with SkBitmap calling lockPixels, we still want
to increment | 211 // For compatibility with SkBitmap calling lockPixels, we still want
to increment |
| 217 // fLockCount even if we failed. If we updated SkBitmap we could rem
ove this oddity. | 212 // fLockCount even if we failed. If we updated SkBitmap we could rem
ove this oddity. |
| 218 fLockCount += 1; | 213 fLockCount += 1; |
| 219 return false; | |
| 220 } | 214 } |
| 215 return success; |
| 221 } | 216 } |
| 222 validate_pixels_ctable(fRec.fPixels, fRec.fColorTable, fInfo.colorType()); | |
| 223 return true; | |
| 224 } | 217 } |
| 225 | 218 |
| 226 bool SkPixelRef::lockPixels(LockRec* rec) { | 219 bool SkPixelRef::lockPixels() { |
| 227 if (this->lockPixels()) { | 220 LockRec rec; |
| 228 *rec = fRec; | 221 return this->lockPixels(&rec); |
| 229 return true; | |
| 230 } | |
| 231 return false; | |
| 232 } | 222 } |
| 233 | 223 |
| 234 void SkPixelRef::unlockPixels() { | 224 void SkPixelRef::unlockPixels() { |
| 235 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); | 225 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); |
| 236 | 226 |
| 237 if (!fPreLocked) { | 227 if (!fPreLocked) { |
| 238 SkAutoMutexAcquire ac(*fMutex); | 228 SkAutoMutexAcquire ac(*fMutex); |
| 239 | 229 |
| 240 SkASSERT(fLockCount > 0); | 230 SkASSERT(fLockCount > 0); |
| 241 if (0 == --fLockCount) { | 231 if (0 == --fLockCount) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 256 return false; | 246 return false; |
| 257 } | 247 } |
| 258 | 248 |
| 259 if (fPreLocked) { | 249 if (fPreLocked) { |
| 260 result->fUnlockProc = NULL; | 250 result->fUnlockProc = NULL; |
| 261 result->fUnlockContext = NULL; | 251 result->fUnlockContext = NULL; |
| 262 result->fCTable = fRec.fColorTable; | 252 result->fCTable = fRec.fColorTable; |
| 263 result->fPixels = fRec.fPixels; | 253 result->fPixels = fRec.fPixels; |
| 264 result->fRowBytes = fRec.fRowBytes; | 254 result->fRowBytes = fRec.fRowBytes; |
| 265 result->fSize.set(fInfo.width(), fInfo.height()); | 255 result->fSize.set(fInfo.width(), fInfo.height()); |
| 256 return true; |
| 266 } else { | 257 } else { |
| 267 SkAutoMutexAcquire ac(*fMutex); | 258 SkAutoMutexAcquire ac(*fMutex); |
| 268 if (!this->onRequestLock(request, result)) { | 259 return this->onRequestLock(request, result); |
| 269 return false; | |
| 270 } | |
| 271 } | 260 } |
| 272 validate_pixels_ctable(result->fPixels, result->fCTable, fInfo.colorType()); | |
| 273 return true; | |
| 274 } | 261 } |
| 275 | 262 |
| 276 bool SkPixelRef::lockPixelsAreWritable() const { | 263 bool SkPixelRef::lockPixelsAreWritable() const { |
| 277 return this->onLockPixelsAreWritable(); | 264 return this->onLockPixelsAreWritable(); |
| 278 } | 265 } |
| 279 | 266 |
| 280 bool SkPixelRef::onLockPixelsAreWritable() const { | 267 bool SkPixelRef::onLockPixelsAreWritable() const { |
| 281 return true; | 268 return true; |
| 282 } | 269 } |
| 283 | 270 |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 return 0; | 351 return 0; |
| 365 } | 352 } |
| 366 | 353 |
| 367 static void unlock_legacy_result(void* ctx) { | 354 static void unlock_legacy_result(void* ctx) { |
| 368 SkPixelRef* pr = (SkPixelRef*)ctx; | 355 SkPixelRef* pr = (SkPixelRef*)ctx; |
| 369 pr->unlockPixels(); | 356 pr->unlockPixels(); |
| 370 pr->unref(); // balancing the Ref in onRequestLoc | 357 pr->unref(); // balancing the Ref in onRequestLoc |
| 371 } | 358 } |
| 372 | 359 |
| 373 bool SkPixelRef::onRequestLock(const LockRequest& request, LockResult* result) { | 360 bool SkPixelRef::onRequestLock(const LockRequest& request, LockResult* result) { |
| 374 if (!this->lockPixelsInsideMutex()) { | 361 LockRec rec; |
| 362 if (!this->lockPixelsInsideMutex(&rec)) { |
| 375 return false; | 363 return false; |
| 376 } | 364 } |
| 377 | 365 |
| 378 result->fUnlockProc = unlock_legacy_result; | 366 result->fUnlockProc = unlock_legacy_result; |
| 379 result->fUnlockContext = SkRef(this); // this is balanced in our fUnlockPr
oc | 367 result->fUnlockContext = SkRef(this); // this is balanced in our fUnlockPr
oc |
| 380 result->fCTable = fRec.fColorTable; | 368 result->fCTable = rec.fColorTable; |
| 381 result->fPixels = fRec.fPixels; | 369 result->fPixels = rec.fPixels; |
| 382 result->fRowBytes = fRec.fRowBytes; | 370 result->fRowBytes = rec.fRowBytes; |
| 383 result->fSize.set(fInfo.width(), fInfo.height()); | 371 result->fSize.set(fInfo.width(), fInfo.height()); |
| 384 return true; | 372 return true; |
| 385 } | 373 } |
| OLD | NEW |