diff options
author | 2013-11-06 10:08:30 +0000 | |
---|---|---|
committer | 2013-11-06 10:08:30 +0000 | |
commit | 6e3e42296b0d7a93325146d9c9a7e23ef90760fe (patch) | |
tree | 7b9c3f75c7e8da1e5c3f0510e8173b0fdbd490bb /src/lazy/SkCachingPixelRef.cpp | |
parent | 6006d0f8c4f19d19a12de20826f731f52ac822a7 (diff) |
Break up SkLazyPixelRef functionally into class hierarchy.
The reason for this CL is to allow greater decoder flexibility.
Chrome currently uses its own decoding functions. These allow for
greater flexibility in dealing with images with multiple frames or
partial data. The DecodeProc function was not flexible enough to
handle these. Instead of asking the decoder to squeeze everything
into the DecodeProc, we now ask the downstream library to inherit from
SkCachingPixelRef. If WebKit's LazyDecodingPixelRef is re-tooled to
inherit from SkCachingPixelRef, then it can make use of Skia's caching
ability while still allowing it to deal with multiple frames, scaling,
subsetting, and partial data.
- The abstract SkCachingPixelRef class handles caching the decoded
data in a SkScaledImageCache. This class relies on the virtual
functions onDecodeInfo() and onDecode() to do the actual decoding
of data.
- The SkLazyCachingPixelRef class is derived from SkCachingPixelRef.
It provides an implementation of onDecodeInfo() and onDecode() in
terms of calls to a SkBitmapFactory::DecodeProc function. It also
provides an Install() static method which installs a new
SkLazyCachingPixelRef into a SkBitmap.
SkLazyCachingPixelRef exists for two reasons: to test
SkCachingPixelRef within Skia and as an example for downstream
developers to make their own classes that inherit from
SkCachingPixelRef.
- The CachedDecodingPixelRefTest was updated to test the
SkLazyCachingPixelRef class and indirectly the SkCachingPixelRef
class.
BUG=
R=reed@google.com, scroggo@google.com
Author: halcanary@google.com
Review URL: https://codereview.chromium.org/54203006
git-svn-id: http://skia.googlecode.com/svn/trunk@12149 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'src/lazy/SkCachingPixelRef.cpp')
-rw-r--r-- | src/lazy/SkCachingPixelRef.cpp | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/src/lazy/SkCachingPixelRef.cpp b/src/lazy/SkCachingPixelRef.cpp new file mode 100644 index 0000000000..2761f1e8bb --- /dev/null +++ b/src/lazy/SkCachingPixelRef.cpp @@ -0,0 +1,112 @@ +/* + * Copyright 2013 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SkCachingPixelRef.h" +#include "SkScaledImageCache.h" + +SkCachingPixelRef::SkCachingPixelRef() + : fErrorInDecoding(false) + , fScaledCacheId(NULL) { + memset(&fInfo, 0xFF, sizeof(fInfo)); +} +SkCachingPixelRef::~SkCachingPixelRef() { + SkASSERT(NULL == fScaledCacheId); + // Assert always unlock before unref. +} + +bool SkCachingPixelRef::getInfo(SkImageInfo* info) { + SkASSERT(info != NULL); + if (fErrorInDecoding) { + return false; // Don't try again. + } + if (fInfo.fWidth < 0) { + SkImageInfo tmp; + if (!this->onDecodeInfo(&tmp)) { + fErrorInDecoding = true; + return false; + } + SkASSERT(tmp.fWidth >= 0); + fInfo = tmp; + } + *info = fInfo; + return true; +} + +bool SkCachingPixelRef::configure(SkBitmap* bitmap) { + SkASSERT(bitmap != NULL); + SkImageInfo info; + if (!this->getInfo(&info)) { + return false; + } + return bitmap->setConfig(info, 0); +} + +void* SkCachingPixelRef::onLockPixels(SkColorTable** colorTable) { + (void)colorTable; + SkImageInfo info; + if (!this->getInfo(&info)) { + return NULL; + } + SkBitmap bitmap; + + fScaledCacheId = SkScaledImageCache::FindAndLock(this->getGenerationID(), + info.fWidth, + info.fHeight, + &bitmap); + if (NULL == fScaledCacheId) { + // Cache has been purged, must re-decode. + if (!this->onDecodeInto(0, &bitmap)) { + return NULL; + } + fScaledCacheId = SkScaledImageCache::AddAndLock(this->getGenerationID(), + info.fWidth, + info.fHeight, + bitmap); + SkASSERT(fScaledCacheId != NULL); + } + // Now bitmap should contain a concrete PixelRef of the decoded + // image. + SkAutoLockPixels autoLockPixels(bitmap); + void* pixels = bitmap.getPixels(); + SkASSERT(pixels != NULL); + // 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; +} + +void SkCachingPixelRef::onUnlockPixels() { + if (fScaledCacheId != NULL) { + SkScaledImageCache::Unlock( + static_cast<SkScaledImageCache::ID*>(fScaledCacheId)); + fScaledCacheId = NULL; + } +} + +bool SkCachingPixelRef::onDecodeInto(int pow2, SkBitmap* bitmap) { + SkASSERT(bitmap != NULL); + SkBitmap tmp; + SkImageInfo info; + // TODO(halcanary) - Enable SkCachingPixelRef to use a custom + // allocator. `tmp.allocPixels(fAllocator, NULL)` + if (!(this->configure(&tmp) && tmp.allocPixels())) { + return false; + } + SkAssertResult(this->getInfo(&info)); // since configure() succeeded. + if (!this->onDecodePixels(info, tmp.getPixels(), tmp.rowBytes())) { + fErrorInDecoding = true; + return false; + } + *bitmap = tmp; + return true; +} + |