aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/lazy
diff options
context:
space:
mode:
authorGravatar scroggo@google.com <scroggo@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-03-15 18:08:09 +0000
committerGravatar scroggo@google.com <scroggo@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-03-15 18:08:09 +0000
commitd9ef3a21d4b4c51c4798547505a76a891d50673a (patch)
treed427c051cb4077571a171b7de8f9a46c829afd2f /src/lazy
parent35e15633805081c8978c18910cb25ed53f06f6c2 (diff)
Create a platform-dependent object for accessing purgeable memory.
Siphoned off from https://codereview.chromium.org/12433020/ Create a Skia class which can provide purgeable memory in a platform- dependent way. Include an implementation for Ashmem and Mac/iOS. Review URL: https://codereview.chromium.org/12645006 git-svn-id: http://skia.googlecode.com/svn/trunk@8176 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'src/lazy')
-rw-r--r--src/lazy/SkPurgeableMemoryBlock.h94
-rw-r--r--src/lazy/SkPurgeableMemoryBlock_common.cpp17
2 files changed, 111 insertions, 0 deletions
diff --git a/src/lazy/SkPurgeableMemoryBlock.h b/src/lazy/SkPurgeableMemoryBlock.h
new file mode 100644
index 0000000000..1750ad9d6c
--- /dev/null
+++ b/src/lazy/SkPurgeableMemoryBlock.h
@@ -0,0 +1,94 @@
+/*
+ * 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 SkPurgeableMemoryBlock_DEFINED
+#define SkPurgeableMemoryBlock_DEFINED
+
+#include "SkTypes.h"
+
+class SkPurgeableMemoryBlock : public SkNoncopyable {
+
+public:
+ /**
+ * Whether or not this platform has an implementation for purgeable memory.
+ */
+ static bool IsSupported();
+
+ /**
+ * Create a new purgeable memory block of 'size' bytes. Returns NULL if not supported on this
+ * platform or on failure.
+ * @param size Number of bytes requested.
+ * @return A new block, or NULL on failure.
+ */
+ static SkPurgeableMemoryBlock* Create(size_t size);
+
+#ifdef SK_DEBUG
+ /**
+ * Whether the platform supports one shot purge of all unpinned blocks. If so,
+ * PurgeAllUnpinnedBlocks will be used to test a purge. Otherwise, purge will be called on
+ * individual blocks.
+ */
+ static bool PlatformSupportsPurgingAllUnpinnedBlocks();
+
+ /**
+ * Purge all unpinned blocks at once, if the platform supports it.
+ */
+ static bool PurgeAllUnpinnedBlocks();
+
+ // If PlatformSupportsPurgingAllUnpinnedBlocks returns true, this will not be called, so it can
+ // simply return false.
+ bool purge();
+
+ bool isPinned() const { return fPinned; }
+#endif
+
+ ~SkPurgeableMemoryBlock();
+
+ /**
+ * Output parameter for pin(), stating whether the data has been retained.
+ */
+ enum PinResult {
+ /**
+ * The data has been purged, or this is the first call to pin.
+ */
+ kUninitialized_PinResult,
+
+ /**
+ * The data has been retained. The memory contains the same data it held when unpin() was
+ * called.
+ */
+ kRetained_PinResult,
+ };
+
+ /**
+ * Pin the memory for use. Must not be called while already pinned.
+ * @param PinResult Whether the data was retained. Ignored on failure.
+ * @return Pointer to the pinned data on success. NULL on failure.
+ */
+ void* pin(PinResult*);
+
+ /**
+ * Unpin the data so it can be purged if necessary.
+ */
+ void unpin();
+
+private:
+ void* fAddr;
+ size_t fSize;
+ bool fPinned;
+#ifdef SK_BUILD_FOR_ANDROID
+ int fFD;
+#endif
+
+ // Unimplemented default constructor is private, to prevent manual creation.
+ SkPurgeableMemoryBlock();
+
+ // The correct way to create a new one is from the static Create.
+ SkPurgeableMemoryBlock(size_t);
+};
+
+#endif // SkPurgeableMemoryBlock_DEFINED
diff --git a/src/lazy/SkPurgeableMemoryBlock_common.cpp b/src/lazy/SkPurgeableMemoryBlock_common.cpp
new file mode 100644
index 0000000000..6ac9b822c6
--- /dev/null
+++ b/src/lazy/SkPurgeableMemoryBlock_common.cpp
@@ -0,0 +1,17 @@
+/*
+ * 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 "SkPurgeableMemoryBlock.h"
+
+SkPurgeableMemoryBlock* SkPurgeableMemoryBlock::Create(size_t size) {
+ SkASSERT(IsSupported());
+ if (!IsSupported()) {
+ return NULL;
+ }
+ return SkNEW_ARGS(SkPurgeableMemoryBlock, (size));
+}
+