Index: src/lazy/SkLazyPixelRef.cpp |
diff --git a/src/lazy/SkLazyPixelRef.cpp b/src/lazy/SkLazyPixelRef.cpp |
index 0454362b942f4e5ceb1aa5ece04270caa3e8f710..089935226599a1e4c27c8455eb28ad30c09496fd 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,12 @@ SkLazyPixelRef::SkLazyPixelRef(SkData* data, SkBitmapFactory::DecodeProc proc, S |
SkLazyPixelRef::~SkLazyPixelRef() { |
scroggo
2013/10/23 23:38:33
Now that we ref fAllocator, it needs to be unref'e
hal.canary
2013/10/24 14:21:55
Done.
|
SkASSERT(fData != NULL); |
fData->unref(); |
+ if (NULL == fImageCache) { |
+ if (fCacheId != 0) { |
+ SkScaledImageCache::Unlock((SkScaledImageCache::ID *)(fCacheId)); |
+ } |
+ return; |
+ } |
SkASSERT(fImageCache); |
if (fCacheId != SkImageCache::UNINITIALIZED_ID) { |
fImageCache->throwAwayCache(fCacheId); |
@@ -79,10 +90,79 @@ 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); |
scroggo
2013/10/23 23:38:33
Maybe assert NULL == fImageCache as well?
hal.canary
2013/10/24 14:21:55
Done.
|
+ 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); |
scroggo
2013/10/23 23:38:33
Maybe this is correct, but it seems odd to me:
-
hal.canary
2013/10/24 14:21:55
This is confusing enough that I will add comments
|
+ void* pixels = bitmap.getPixels(); |
+ SkASSERT(NULL != pixels); |
+ 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, |
+ &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(!fErrorInDecoding); |
scroggo
2013/10/23 23:38:33
Assert that fImageCache != NULL?
hal.canary
2013/10/24 14:21:55
Done.
|
SkBitmapFactory::Target target; |
// Check to see if the pixels still exist in the cache. |
if (SkImageCache::UNINITIALIZED_ID == fCacheId) { |
@@ -144,10 +224,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)); |
+ fCacheId = 0; |
+ return; |
+ } else { // use fImageCache |
fImageCache->releaseCache(fCacheId); |
} |
} |
@@ -157,8 +241,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 +288,7 @@ bool SkLazyPixelRef::onDecodeInto(int pow2, SkBitmap* bitmap) { |
*bitmap = tmp; |
return true; |
} |
+ |
+void SkLazyPixelRef::setAllocator(SkBitmap::Allocator * allocator) { |
+ SkRefCnt_SafeAssign(fAllocator, allocator); |
+} |