Chromium Code Reviews| Index: src/lazy/SkLazyPixelRef.cpp |
| diff --git a/src/lazy/SkLazyPixelRef.cpp b/src/lazy/SkLazyPixelRef.cpp |
| index 0454362b942f4e5ceb1aa5ece04270caa3e8f710..c1ad1a86cbea53b9c8ce69c32dc1ea9219a04ae3 100644 |
| --- a/src/lazy/SkLazyPixelRef.cpp |
| +++ b/src/lazy/SkLazyPixelRef.cpp |
| @@ -11,6 +11,7 @@ |
| #include "SkData.h" |
| #include "SkImageCache.h" |
| #include "SkImagePriv.h" |
| +#include "SkScaledImageCache.h" |
| #if LAZY_CACHE_STATS |
| #include "SkThread.h" |
| @@ -22,11 +23,14 @@ int32_t SkLazyPixelRef::gCacheMisses; |
| SkLazyPixelRef::SkLazyPixelRef(SkData* data, SkBitmapFactory::DecodeProc proc, SkImageCache* cache) |
| // Pass NULL for the Mutex so that the default (ring buffer) will be used. |
| : INHERITED(NULL) |
| + , fErrorInDecoding(false) |
| , fDecodeProc(proc) |
| , fImageCache(cache) |
| - , fCacheId(SkImageCache::UNINITIALIZED_ID) |
| - , fRowBytes(0) { |
| + , fCacheId(0) |
| + , fRowBytes(0) |
| + , fAllocator(NULL) { |
| SkASSERT(fDecodeProc != NULL); |
| + SkASSERT(0 == SkImageCache::UNINITIALIZED_ID); // for fCacheId |
| if (NULL == data) { |
| fData = SkData::NewEmpty(); |
| fErrorInDecoding = true; |
| @@ -35,8 +39,9 @@ SkLazyPixelRef::SkLazyPixelRef(SkData* data, SkBitmapFactory::DecodeProc proc, S |
| fData->ref(); |
| fErrorInDecoding = data->size() == 0; |
| } |
| - SkASSERT(cache != NULL); |
| - cache->ref(); |
| + if (fImageCache != NULL) { |
| + fImageCache->ref(); |
| + } |
| // mark as uninitialized -- all fields are -1 |
| memset(&fLazilyCachedInfo, 0xFF, sizeof(fLazilyCachedInfo)); |
| @@ -48,6 +53,14 @@ SkLazyPixelRef::SkLazyPixelRef(SkData* data, SkBitmapFactory::DecodeProc proc, S |
| SkLazyPixelRef::~SkLazyPixelRef() { |
| SkASSERT(fData != NULL); |
| fData->unref(); |
| + SkSafeUnref(fAllocator); |
| + if (NULL == fImageCache) { |
| + if (fCacheId != 0) { |
| + SkScaledImageCache::Unlock((SkScaledImageCache::ID *)(fCacheId)); |
|
mtklein
2013/10/24 17:03:25
* affinity
|
| + // SkScaledImageCache needs a throwAwayCache(id) method. |
| + } |
| + return; |
| + } |
| SkASSERT(fImageCache); |
| if (fCacheId != SkImageCache::UNINITIALIZED_ID) { |
| fImageCache->throwAwayCache(fCacheId); |
| @@ -79,10 +92,89 @@ const SkImage::Info* SkLazyPixelRef::getCachedInfo() { |
| return &fLazilyCachedInfo; |
| } |
| +/** |
| + Returns bitmap->getPixels() on success; NULL on failure */ |
| +static void* decode_into_bitmap(SkImage::Info* info, |
| + SkBitmapFactory::DecodeProc decodeProc, |
| + size_t* rowBytes, |
| + SkData* data, |
| + SkBitmap::Allocator* allocator, |
| + SkBitmap* bm) { |
| + SkASSERT(info && decodeProc && rowBytes && data && bm); |
| + if (!(bm->setConfig(SkImageInfoToBitmapConfig(*info), info->fWidth, |
| + info->fHeight, *rowBytes, info->fAlphaType) |
| + && bm->allocPixels(allocator, NULL))) { |
| + return NULL; |
| + } |
| + SkBitmapFactory::Target target; |
| + target.fAddr = bm->getPixels(); |
| + target.fRowBytes = bm->rowBytes(); |
| + *rowBytes = target.fRowBytes; |
| + if (!decodeProc(data->data(), data->size(), info, &target)) { |
| + return NULL; |
| + } |
| + return target.fAddr; |
| +} |
| + |
| +void* SkLazyPixelRef::lockScaledImageCachePixels() { |
| + SkASSERT(!fErrorInDecoding); |
| + SkASSERT(NULL == fImageCache); |
| + SkBitmap bitmap; |
| + if (fLazilyCachedInfo.fWidth > 0) { // fInfo already populated |
| + fCacheId = (intptr_t)(SkScaledImageCache::FindAndLock( |
| + this->getGenerationID(), |
| + fLazilyCachedInfo.fWidth, |
| + fLazilyCachedInfo.fHeight, |
| + &bitmap)); |
| + if (fCacheId != 0) { |
| + SkAutoLockPixels autoLockPixels(bitmap); |
| + void* pixels = bitmap.getPixels(); |
| + SkASSERT(NULL != pixels); |
| + // At this point, the autoLockPixels will unlockPixels() |
| + // to remove bitmap's lock on the pixels. We will then |
| + // destroy bitmap. The *only* guarantee that this pointer |
| + // remains valid is the guarantee made by |
| + // SkScaledImageCache that it will not destroy the *other* |
| + // bitmap (SkScaledImageCache::Rec.fBitmap) that holds a |
| + // reference to the concrete PixelRef while this record is |
| + // locked. |
| + return pixels; |
| + } // else cache has been purged, must re-decode. |
| + } else { // first time through this code, must populate info |
| + (void)this->getCachedInfo(); |
| + if (fErrorInDecoding) { |
| + return NULL; |
| + } |
| + } |
| + SkASSERT(fLazilyCachedInfo.fWidth > 0); |
| + void * ptr = decode_into_bitmap(&fLazilyCachedInfo, fDecodeProc, |
|
mtklein
2013/10/24 17:03:25
*
|
| + &fRowBytes, fData, fAllocator, &bitmap); |
| + if (NULL == ptr) { |
| + fErrorInDecoding = true; |
| + return NULL; |
| + } |
| + fCacheId = (intptr_t)(SkScaledImageCache::AddAndLock( |
| + this->getGenerationID(), |
| + fLazilyCachedInfo.fWidth, |
| + fLazilyCachedInfo.fHeight, |
| + bitmap)); |
| + return ptr; |
| +} |
| + |
| void* SkLazyPixelRef::onLockPixels(SkColorTable**) { |
| if (fErrorInDecoding) { |
| return NULL; |
| } |
| + if (NULL == fImageCache) { |
| + return this->lockScaledImageCachePixels(); |
| + } else { |
| + return this->lockImageCachePixels(); |
| + } |
| +} |
| + |
| +void* SkLazyPixelRef::lockImageCachePixels() { |
| + SkASSERT(fImageCache != NULL); |
| + SkASSERT(!fErrorInDecoding); |
| SkBitmapFactory::Target target; |
| // Check to see if the pixels still exist in the cache. |
| if (SkImageCache::UNINITIALIZED_ID == fCacheId) { |
| @@ -144,10 +236,14 @@ void* SkLazyPixelRef::onLockPixels(SkColorTable**) { |
| } |
| void SkLazyPixelRef::onUnlockPixels() { |
| - if (fErrorInDecoding) { |
| + if (fErrorInDecoding || (0 == fCacheId)) { |
| return; |
| } |
| - if (fCacheId != SkImageCache::UNINITIALIZED_ID) { |
| + if (NULL == fImageCache) { |
| + SkScaledImageCache::Unlock((SkScaledImageCache::ID *)(fCacheId)); |
|
mtklein
2013/10/24 17:03:25
*
|
| + fCacheId = 0; |
| + return; |
| + } else { // use fImageCache |
| fImageCache->releaseCache(fCacheId); |
| } |
| } |
| @@ -157,8 +253,6 @@ SkData* SkLazyPixelRef::onRefEncodedData() { |
| return fData; |
| } |
| -#include "SkImagePriv.h" |
| - |
| static bool init_from_info(SkBitmap* bm, const SkImage::Info& info, |
| size_t rowBytes) { |
| SkBitmap::Config config = SkImageInfoToBitmapConfig(info); |
| @@ -206,3 +300,7 @@ bool SkLazyPixelRef::onDecodeInto(int pow2, SkBitmap* bitmap) { |
| *bitmap = tmp; |
| return true; |
| } |
| + |
| +void SkLazyPixelRef::setAllocator(SkBitmap::Allocator * allocator) { |
|
mtklein
2013/10/24 17:03:25
*
|
| + SkRefCnt_SafeAssign(fAllocator, allocator); |
| +} |