Index: src/core/SkCachedData.h |
diff --git a/src/core/SkCachedData.h b/src/core/SkCachedData.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..39d8fd279b59583e8e93fc9a2af10536cf827007 |
--- /dev/null |
+++ b/src/core/SkCachedData.h |
@@ -0,0 +1,59 @@ |
+/* |
+ * Copyright 2014 Google Inc. |
+ * |
+ * Use of this source code is governed by a BSD-style license that can be |
+ * found in the LICENSE file. |
+ */ |
+ |
+#ifndef SkCachedData_DEFINED |
+#define SkCachedData_DEFINED |
+ |
+#include "SkTypes.h" |
+ |
+class SkDiscardableMemory; |
+ |
+class SkCachedData : ::SkNoncopyable { |
+public: |
+ static SkCachedData* Create(size_t bytes, SkDiscardableMemory* = NULL); |
+ |
+ virtual ~SkCachedData() {} |
+ |
+ size_t size() const { return fSize; } |
+ const void* data() const { return fData; } |
+ |
+ void* writable_data() { return fData; } |
+ |
+ void ref(); |
+ void unref(); |
+ |
+ int testing_only_getRefCnt() const { return fRefCnt >> 1; } |
+ bool testing_only_isInCache() const { return fRefCnt & 1; } |
+ |
+protected: |
+ SkCachedData(void* data, size_t size); |
+ |
+ virtual void onLock() = 0; // called when owner count rises above 1 |
+ virtual void onUnlock() = 0; // called when owner count falls back to 1 |
+ |
+ void setData(void* data) { fData = data; } |
+ |
+private: |
+ void* fData; |
+ size_t fSize; |
+ int32_t fRefCnt; // low-bit means we're owned by the cache |
+ |
+ void lock() { |
+ this->onLock(); |
+ } |
+ |
+ void unlock() { |
+ this->onUnlock(); |
+ fData = NULL; // signal that we're unlocked |
+ } |
+ |
+public: |
+ void ref_from_cache(); |
+ void unref_from_cache(); |
+}; |
+ |
+#endif |