OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2013 Google Inc. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. |
| 6 */ |
| 7 |
| 8 #include "SkCachingPixelRef.h" |
| 9 |
| 10 SkCachingPixelRef::SkCachingPixelRef(SkScaledImageCache* cache) |
| 11 : fErrorInDecoding(false) |
| 12 , fCache(cache) |
| 13 , fScaledCacheId(NULL) |
| 14 , fBitmapSize(SkISize::Make(0, 0)) { |
| 15 if (NULL == fCache) { |
| 16 fCache = SkScaledImageCache::GetGlobalInstance(); |
| 17 } |
| 18 } |
| 19 SkCachingPixelRef::~SkCachingPixelRef() { |
| 20 SkASSERT(NULL == fScaledCacheId); |
| 21 // Assert always unlock before unref. |
| 22 } |
| 23 |
| 24 bool SkCachingPixelRef::updateInfo() { |
| 25 if (fErrorInDecoding) { |
| 26 return false; // Don't try again. |
| 27 } |
| 28 if (!fBitmapSize.isZero()) { |
| 29 return true; // Use cached values. |
| 30 } |
| 31 if (!this->onDecodeInfo(&fBitmapSize, &fBitmapConfig, &fBitmapAlphaType)) { |
| 32 fErrorInDecoding = true; |
| 33 return false; |
| 34 } |
| 35 SkASSERT(!fBitmapSize.isZero()); |
| 36 return true; |
| 37 } |
| 38 |
| 39 bool SkCachingPixelRef::configure(SkBitmap* bitmap) { |
| 40 if (!this->updateInfo()) { |
| 41 return false; |
| 42 } |
| 43 return bitmap->setConfig(fBitmapConfig, fBitmapSize.width(), |
| 44 fBitmapSize.height(), 0, fBitmapAlphaType); |
| 45 } |
| 46 |
| 47 void* SkCachingPixelRef::onLockPixels(SkColorTable** colorTable) { |
| 48 (void)colorTable; |
| 49 if (!this->updateInfo()) { |
| 50 return NULL; |
| 51 } |
| 52 SkBitmap bitmap; |
| 53 |
| 54 fScaledCacheId = fCache->findAndLock(this->getGenerationID(), |
| 55 fBitmapSize.fWidth, |
| 56 fBitmapSize.fHeight, |
| 57 &bitmap); |
| 58 if (NULL == fScaledCacheId) { |
| 59 // Cache has been purged, must re-decode. |
| 60 if (!this->onDecodeInto(0, &bitmap)) { |
| 61 return NULL; |
| 62 } |
| 63 fScaledCacheId = fCache->addAndLock(this->getGenerationID(), |
| 64 fBitmapSize.fWidth, |
| 65 fBitmapSize.fHeight, |
| 66 bitmap); |
| 67 SkASSERT(fScaledCacheId != NULL); |
| 68 } |
| 69 // Now bitmap should contain a concrete PixelRef of the decoded |
| 70 // image. |
| 71 SkAutoLockPixels autoLockPixels(bitmap); |
| 72 void* pixels = bitmap.getPixels(); |
| 73 SkASSERT(NULL != pixels); |
| 74 // At this point, the autoLockPixels will unlockPixels() |
| 75 // to remove bitmap's lock on the pixels. We will then |
| 76 // destroy bitmap. The *only* guarantee that this pointer |
| 77 // remains valid is the guarantee made by |
| 78 // SkScaledImageCache that it will not destroy the *other* |
| 79 // bitmap (SkScaledImageCache::Rec.fBitmap) that holds a |
| 80 // reference to the concrete PixelRef while this record is |
| 81 // locked. |
| 82 return pixels; |
| 83 } |
| 84 |
| 85 void SkCachingPixelRef::onUnlockPixels() { |
| 86 if (NULL != fScaledCacheId) { |
| 87 fCache->unlock(fScaledCacheId); |
| 88 fScaledCacheId = NULL; |
| 89 } |
| 90 } |
| 91 |
| 92 bool SkCachingPixelRef::onDecodeInto(int pow2, SkBitmap* bitmap) { |
| 93 SkBitmap tmp; |
| 94 if (!(this->configure(&tmp) && tmp.allocPixels(NULL, NULL))) { |
| 95 fErrorInDecoding = true; |
| 96 return false; |
| 97 } |
| 98 if (!this->onDecode(tmp.getPixels(), tmp.rowBytes())) { |
| 99 fErrorInDecoding = true; |
| 100 return false; |
| 101 } |
| 102 *bitmap = tmp; |
| 103 return true; |
| 104 } |
| 105 |
OLD | NEW |