aboutsummaryrefslogtreecommitdiffhomepage
path: root/include/lazy/SkImageCache.h
diff options
context:
space:
mode:
authorGravatar scroggo@google.com <scroggo@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-02-22 21:38:35 +0000
committerGravatar scroggo@google.com <scroggo@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-02-22 21:38:35 +0000
commitf8d7d2731318cdf510ab68e6b3f5ec68ab22c8e2 (patch)
treeb62a29fabe0b0af3f8f17ffbe8d607508c1d2c00 /include/lazy/SkImageCache.h
parent5c90e291425b2788f47679266d9584845ceefc2e (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 'include/lazy/SkImageCache.h')
-rw-r--r--include/lazy/SkImageCache.h76
1 files changed, 76 insertions, 0 deletions
diff --git a/include/lazy/SkImageCache.h b/include/lazy/SkImageCache.h
new file mode 100644
index 0000000000..045ce2c384
--- /dev/null
+++ b/include/lazy/SkImageCache.h
@@ -0,0 +1,76 @@
+/*
+ * 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 SkImageCache_DEFINED
+#define SkImageCache_DEFINED
+
+#include "SkRefCnt.h"
+#include "SkTypes.h"
+
+/**
+ * Interface for a cache that manages pixel memory.
+ */
+class SkImageCache : public SkRefCnt {
+
+public:
+ /**
+ * Allocate memory whose lifetime is managed by the cache. On success, MUST be balanced with a
+ * call to releaseCache and a call to throwAwayCache.
+ * @param bytes Number of bytes needed.
+ * @param ID Output parameter which must not be NULL. On success, ID will be set to a value
+ * associated with that memory which can be used as a parameter to the other functions
+ * in SkImageCache. On failure, ID is unchanged.
+ * @return Pointer to the newly allocated memory, or NULL. This memory is safe to use until
+ * releaseCache is called with ID.
+ */
+ virtual void* allocAndPinCache(size_t bytes, intptr_t* ID) = 0;
+
+ /**
+ * Re-request the memory associated with ID.
+ * @param ID Unique ID for the memory block.
+ * @return Pointer: If non-NULL, points to the previously allocated memory, in which case
+ * this call must be balanced with a call to releaseCache. If NULL, the memory
+ * has been reclaimed, so allocAndPinCache must be called again, and ID is no
+ * longer valid (thus throwAwayCache need not be called).
+ */
+ virtual void* pinCache(intptr_t ID) = 0;
+
+ /**
+ * Inform the cache that it is safe to free the block of memory corresponding to ID. After
+ * calling this function, the pointer returnted by allocAndPinCache or pinCache must not be
+ * used again. In order to access the same memory after this, pinCache must be called.
+ * @param ID Unique ID for the memory block which is now safe to age out of the cache.
+ */
+ virtual void releaseCache(intptr_t ID) = 0;
+
+ /**
+ * Inform the cache that the block of memory associated with ID will not be asked for again.
+ * After this call, ID is no longer valid. Must not be called while the associated memory is
+ * pinned. Must be called to balance a successful allocAndPinCache, unless a later pinCache
+ * returns NULL.
+ */
+ virtual void throwAwayCache(intptr_t ID) = 0;
+
+ /**
+ * ID which does not correspond to any valid cache.
+ */
+ static const intptr_t UNINITIALIZED_ID = 0;
+
+#ifdef SK_DEBUG
+ enum CacheStatus {
+ kPinned_CacheStatus,
+ kUnpinned_CacheStatus,
+ kThrownAway_CacheStatus,
+ };
+
+ /**
+ * Debug only function to get the status of a particular block of memory.
+ */
+ virtual CacheStatus getCacheStatus(intptr_t ID) const = 0;
+#endif
+};
+#endif // SkImageCache_DEFINED