aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-11-06 10:08:30 +0000
committerGravatar commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-11-06 10:08:30 +0000
commit6e3e42296b0d7a93325146d9c9a7e23ef90760fe (patch)
tree7b9c3f75c7e8da1e5c3f0510e8173b0fdbd490bb /src
parent6006d0f8c4f19d19a12de20826f731f52ac822a7 (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')
-rw-r--r--src/core/SkBitmap.cpp6
-rw-r--r--src/image/SkImage.cpp8
-rw-r--r--src/lazy/SkCachingPixelRef.cpp112
-rw-r--r--src/lazy/SkCachingPixelRef.h81
-rw-r--r--src/lazy/SkLazyCachingPixelRef.cpp62
-rw-r--r--src/lazy/SkLazyCachingPixelRef.h99
6 files changed, 368 insertions, 0 deletions
diff --git a/src/core/SkBitmap.cpp b/src/core/SkBitmap.cpp
index bff08801fc..2fae75ae38 100644
--- a/src/core/SkBitmap.cpp
+++ b/src/core/SkBitmap.cpp
@@ -11,6 +11,7 @@
#include "SkColorPriv.h"
#include "SkDither.h"
#include "SkFlattenable.h"
+#include "SkImagePriv.h"
#include "SkMallocPixelRef.h"
#include "SkMask.h"
#include "SkOrderedReadBuffer.h"
@@ -332,6 +333,11 @@ BAD_CONFIG:
return false;
}
+bool SkBitmap::setConfig(const SkImageInfo& info, size_t rowBytes) {
+ return this->setConfig(SkImageInfoToBitmapConfig(info), info.fWidth,
+ info.fHeight, rowBytes, info.fAlphaType);
+}
+
bool SkBitmap::setAlphaType(SkAlphaType alphaType) {
if (!validate_alphaType(this->config(), alphaType, &alphaType)) {
return false;
diff --git a/src/image/SkImage.cpp b/src/image/SkImage.cpp
index 39fd93acc6..c8559e6533 100644
--- a/src/image/SkImage.cpp
+++ b/src/image/SkImage.cpp
@@ -12,6 +12,14 @@
SK_DEFINE_INST_COUNT(SkImage)
+bool operator==(const SkImageInfo& lhs, const SkImageInfo& rhs) {
+ return 0 == memcmp(&lhs, &rhs, sizeof(SkImageInfo));
+}
+bool operator!=(const SkImageInfo& lhs, const SkImageInfo& rhs) {
+ return 0 != memcmp(&lhs, &rhs, sizeof(SkImageInfo));
+}
+
+
static SkImage_Base* as_IB(SkImage* image) {
return static_cast<SkImage_Base*>(image);
}
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;
+}
+
diff --git a/src/lazy/SkCachingPixelRef.h b/src/lazy/SkCachingPixelRef.h
new file mode 100644
index 0000000000..7caba45138
--- /dev/null
+++ b/src/lazy/SkCachingPixelRef.h
@@ -0,0 +1,81 @@
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkCachingPixelRef_DEFINED
+#define SkCachingPixelRef_DEFINED
+
+#include "SkImage.h"
+#include "SkPixelRef.h"
+
+class SkColorTable;
+
+/**
+ * PixelRef which defers decoding until SkBitmap::lockPixels() is
+ * called. Caches the decoded images in the global
+ * SkScaledImageCache. When the pixels are unlocked, this cache may
+ * or be destroyed before the next lock. If so, onLockPixels will
+ * attempt to re-decode.
+ *
+ * Decoding is handled by the pure-virtual functions onDecodeInfo()
+ * and onDecodePixels(). Subclasses of this class need only provide
+ * those two functions.
+ */
+class SkCachingPixelRef : public SkPixelRef {
+public:
+ SkCachingPixelRef();
+ virtual ~SkCachingPixelRef();
+
+protected:
+ virtual void* onLockPixels(SkColorTable** colorTable) SK_OVERRIDE;
+ virtual void onUnlockPixels() SK_OVERRIDE;
+ virtual bool onLockPixelsAreWritable() const SK_OVERRIDE { return false; }
+ virtual bool onImplementsDecodeInto() SK_OVERRIDE { return true; }
+ virtual bool onDecodeInto(int pow2, SkBitmap*) SK_OVERRIDE;
+
+ /**
+ * Configure the supplied bitmap for this pixelRef, based on
+ * information provided by onDecodeInfo(). Does not set the
+ * bitmap's pixelRef. */
+ bool configure(SkBitmap* bitmap);
+
+ /**
+ * Cache info from onDecodeInfo(). Returns false on failure.
+ */
+ bool getInfo(SkImageInfo* info);
+
+ /**
+ * Return some information about the pixels, allowing this class
+ * to allocate pixels. @return false if anything goes wrong.
+ */
+ virtual bool onDecodeInfo(SkImageInfo* info) = 0;
+ /**
+ * Decode into the given pixels, a block of memory of size
+ * (info.fHeight - 1) * rowBytes + (info.fWidth * bytesPerPixel)
+ *
+ * @param info Should be identical to the info returned by
+ * onDecodeInfo so that the implementation can confirm
+ * that the caller knows what it is asking for (config,
+ * size). Thiscontract also allows the caller to specify
+ * different output-configs, which the implementation can
+ * decide to support or not.
+ *
+ * @return false if anything goes wrong.
+ */
+ virtual bool onDecodePixels(const SkImageInfo& info,
+ void* pixels,
+ size_t rowBytes) = 0;
+
+private:
+ bool fErrorInDecoding;
+ void* fScaledCacheId;
+ SkImageInfo fInfo;
+
+ typedef SkPixelRef INHERITED;
+};
+
+#endif // SkCachingPixelRef_DEFINED
+
diff --git a/src/lazy/SkLazyCachingPixelRef.cpp b/src/lazy/SkLazyCachingPixelRef.cpp
new file mode 100644
index 0000000000..59ac9c3809
--- /dev/null
+++ b/src/lazy/SkLazyCachingPixelRef.cpp
@@ -0,0 +1,62 @@
+/*
+ * 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 "Sk64.h"
+#include "SkColorTable.h"
+#include "SkData.h"
+#include "SkImageDecoder.h"
+#include "SkImagePriv.h"
+#include "SkLazyCachingPixelRef.h"
+#include "SkPostConfig.h"
+
+SkLazyCachingPixelRef::SkLazyCachingPixelRef(SkData* data,
+ SkBitmapFactory::DecodeProc proc)
+ : fDecodeProc(proc) {
+ if (NULL == data) {
+ fData = SkData::NewEmpty();
+ } else {
+ fData = data;
+ fData->ref();
+ }
+ if (NULL == fDecodeProc) { // use a reasonable default.
+ fDecodeProc = SkImageDecoder::DecodeMemoryToTarget;
+ }
+ this->setImmutable();
+}
+
+SkLazyCachingPixelRef::~SkLazyCachingPixelRef() {
+ SkASSERT(fData != NULL);
+ fData->unref();
+}
+
+bool SkLazyCachingPixelRef::onDecodeInfo(SkImageInfo* info) {
+ SkASSERT(info);
+ return fDecodeProc(fData->data(), fData->size(), info, NULL);
+}
+
+bool SkLazyCachingPixelRef::onDecodePixels(const SkImageInfo& passedInfo,
+ void* pixels, size_t rowBytes) {
+ SkASSERT(pixels);
+ SkImageInfo info;
+ if (!this->getInfo(&info)) {
+ return false;
+ }
+ if (passedInfo != info) {
+ return false; // This implementation can not handle this case.
+ }
+ SkBitmapFactory::Target target = {pixels, rowBytes};
+ return fDecodeProc(fData->data(), fData->size(), &info, &target);
+}
+
+bool SkLazyCachingPixelRef::Install(SkBitmapFactory::DecodeProc proc,
+ SkData* data,
+ SkBitmap* destination) {
+ SkAutoTUnref<SkLazyCachingPixelRef> ref(
+ SkNEW_ARGS(SkLazyCachingPixelRef, (data, proc)));
+ return ref->configure(destination) && destination->setPixelRef(ref);
+}
+
diff --git a/src/lazy/SkLazyCachingPixelRef.h b/src/lazy/SkLazyCachingPixelRef.h
new file mode 100644
index 0000000000..c13a2cd02b
--- /dev/null
+++ b/src/lazy/SkLazyCachingPixelRef.h
@@ -0,0 +1,99 @@
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkLazyCachingPixelRef_DEFINED
+#define SkLazyCachingPixelRef_DEFINED
+
+#include "SkBitmapFactory.h"
+#include "SkCachingPixelRef.h"
+
+class SkData;
+
+/**
+ * PixelRef which defers decoding until SkBitmap::lockPixels() is
+ * called. Makes use of a supplied decode procedure. Will decode at
+ * the procedure's preferred size.
+ */
+class SkLazyCachingPixelRef : public SkCachingPixelRef {
+public:
+ /**
+ * @param data Encoded data representing the pixels. NULL is
+ * equivalent to an empty data, and will be passed to
+ * DecodeProc with length zero.
+ *
+ * @param procedure Called to decode the pixels when
+ * needed. If NULL, use SkImageDecoder::DecodeMemoryToTarget.
+ */
+ SkLazyCachingPixelRef(SkData* data,
+ SkBitmapFactory::DecodeProc procedure);
+
+ virtual ~SkLazyCachingPixelRef();
+
+ virtual SkData* onRefEncodedData() SK_OVERRIDE { return SkSafeRef(fData); }
+
+ /**
+ * A simplified version of SkBitmapFactory. Installs a new
+ * SkLazyCachingPixelRef into the provided bitmap. Will
+ * immediately call onDecodeInfo() to configure the bitmap, but
+ * will defer decoding until the first time the bitmap's pixels
+ * are locked.
+ *
+ * @param data Encoded data representing the pixels. NULL is
+ * equivalent to an empty data, and will be passed to
+ * DecodeProc with length zero.
+ *
+ * @param procedure Called to decode the pixels when
+ * needed. If NULL, use SkImageDecoder::DecodeMemoryToTarget.
+ *
+ * @param destination Bitmap that will be modified on success.
+ *
+ * @returns true on success.
+ */
+ static bool Install(SkBitmapFactory::DecodeProc procedure,
+ SkData* data,
+ SkBitmap* destination);
+
+ // No need to flatten this object. When flattening an SkBitmap,
+ // SkOrderedWriteBuffer will check the encoded data and write that
+ // instead.
+ // Future implementations of SkFlattenableWriteBuffer will need to
+ // special case for onRefEncodedData as well.
+ SK_DECLARE_UNFLATTENABLE_OBJECT()
+
+protected:
+ /**
+ * Return some information about the pixels, allowing this class
+ * to allocate pixels. @return false if anything goes wrong.
+ *
+ * This implementation calls SkBitmapFactory::DecodeProc with a
+ * NULL target.
+ */
+ virtual bool onDecodeInfo(SkImageInfo* info) SK_OVERRIDE;
+ /**
+ * Decode into the given pixels, a block of memory of size
+ * (info.fHeight * rowBytes) bytes.
+ *
+ * @param info Should be identical to the info returned by
+ * onDecodeInfo so that the implementation can confirm
+ * that the caller knows what its asking for (config,
+ * size).
+ *
+ * @return false if anything goes wrong.
+ */
+ virtual bool onDecodePixels(const SkImageInfo& info,
+ void* pixels,
+ size_t rowBytes) SK_OVERRIDE;
+
+private:
+ SkData* fData;
+ SkBitmapFactory::DecodeProc fDecodeProc;
+
+ typedef SkCachingPixelRef INHERITED;
+};
+
+#endif // SkLazyCachingPixelRef_DEFINED
+