diff options
author | scroggo@google.com <scroggo@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2013-02-22 21:38:35 +0000 |
---|---|---|
committer | scroggo@google.com <scroggo@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2013-02-22 21:38:35 +0000 |
commit | f8d7d2731318cdf510ab68e6b3f5ec68ab22c8e2 (patch) | |
tree | b62a29fabe0b0af3f8f17ffbe8d607508c1d2c00 /src/lazy/SkBitmapFactory.cpp | |
parent | 5c90e291425b2788f47679266d9584845ceefc2e (diff) |
Create SkLazyPixelRef which performs lazy decoding.
The new pixel ref behaves similarly to SkImageRef, with some key differences:
It does not depend on the images project.
It requires an SkImageCache, which handles allocation and caching of the pixel
memory.
It takes a function signature for decoding which decodes into already allocated
pixel memory rather than into an SkBitmap.
Add two implementations of SkImageCache: SkLruImageCache and SkAshmemImageCache.
Replace SkSerializationHelpers::DecodeBitmap with SkPicture::InstallPixelRefProc,
and update sites that referenced it.
SkBitmapFactory now sets the pixel ref to a new object of the new
class SkLazyPixelRef, provided it has an SkImageCache for caching.
Provide an option to do lazy decodes in render_pictures and bench_pictures.
SkPicture:
Eliminate the default parameters in the constructor.
If a proc for decoding bitmaps is installed, use it to decode any encoded
data in subpictures.
When parsing deserializing subpictures, check for success.
When serializing subpictures, pass the picture's bitmap encoder to the
subpicture's call to serialize.
Update BitmapFactoryTest to test its new behavior.
BUG=https://code.google.com/p/skia/issues/detail?id=1008
BUG=https://code.google.com/p/skia/issues/detail?id=1009
Review URL: https://codereview.appspot.com/7060052
git-svn-id: http://skia.googlecode.com/svn/trunk@7835 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'src/lazy/SkBitmapFactory.cpp')
-rw-r--r-- | src/lazy/SkBitmapFactory.cpp | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/src/lazy/SkBitmapFactory.cpp b/src/lazy/SkBitmapFactory.cpp new file mode 100644 index 0000000000..d67e019755 --- /dev/null +++ b/src/lazy/SkBitmapFactory.cpp @@ -0,0 +1,79 @@ +/* + * Copyright 2012 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SkBitmapFactory.h" + +#include "SkBitmap.h" +#include "SkData.h" +#include "SkImageCache.h" +#include "SkImagePriv.h" +#include "SkLazyPixelRef.h" + +SkBitmapFactory::SkBitmapFactory(SkBitmapFactory::DecodeProc proc) + : fDecodeProc(proc) + , fImageCache(NULL) + , fCacheSelector(NULL) { + SkASSERT(fDecodeProc != NULL); +} + +SkBitmapFactory::~SkBitmapFactory() { + SkSafeUnref(fImageCache); +} + +void SkBitmapFactory::setImageCache(SkImageCache *cache) { + SkRefCnt_SafeAssign(fImageCache, cache); + if (cache != NULL) { + fCacheSelector = NULL; + } +} + +void SkBitmapFactory::setCacheSelector(CacheSelector selector) { + fCacheSelector = selector; + if (selector != NULL) { + SkSafeUnref(fImageCache); + fImageCache = NULL; + } +} + +bool SkBitmapFactory::installPixelRef(SkData* data, SkBitmap* dst) { + if (NULL == data || 0 == data->size() || dst == NULL) { + return false; + } + + SkImage::Info info; + if (!fDecodeProc(data->data(), data->size(), &info, NULL)) { + return false; + } + + bool isOpaque = false; + SkBitmap::Config config = SkImageInfoToBitmapConfig(info, &isOpaque); + + Target target; + // FIMXE: There will be a problem if this rowbytes is calculated differently from + // in SkLazyPixelRef. + target.fRowBytes = SkImageMinRowBytes(info); + + dst->setConfig(config, info.fWidth, info.fHeight, target.fRowBytes); + dst->setIsOpaque(isOpaque); + + // fImageCache and fCacheSelector are mutually exclusive. + SkASSERT(NULL == fImageCache || NULL == fCacheSelector); + + SkImageCache* cache = NULL == fCacheSelector ? fImageCache : fCacheSelector(info); + + if (cache != NULL) { + // Now set a new LazyPixelRef on dst. + SkAutoTUnref<SkLazyPixelRef> lazyRef(SkNEW_ARGS(SkLazyPixelRef, + (data, fDecodeProc, cache))); + dst->setPixelRef(lazyRef); + return true; + } else { + dst->allocPixels(); + target.fAddr = dst->getPixels(); + return fDecodeProc(data->data(), data->size(), &info, &target); + } +} |