Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(121)

Unified Diff: src/core/SkCachedData.cpp

Issue 592843003: Add SkCachedData and use it for SkMipMap (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: uses low-bit of refcnt to known when we're in the cache Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/core/SkCachedData.h ('k') | src/core/SkCachedData_priv.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/core/SkCachedData.cpp
diff --git a/src/core/SkCachedData.cpp b/src/core/SkCachedData.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..19bc42fc82c3a780ce70a35d4c50238dbded8ad3
--- /dev/null
+++ b/src/core/SkCachedData.cpp
@@ -0,0 +1,126 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkCachedData_priv.h"
+#include "SkRefCnt.h"
+#include "SkDiscardableMemory.h"
+
+SkCachedData::SkCachedData(void* data, size_t size)
+ : fData(data)
+ , fSize(size)
+ , fRefCnt(2) // 1 owner, not in cache
+{}
+
+void SkCachedData::ref() {
+ const int32_t count = sk_atomic_add(&fRefCnt, 2) + 2;
+ const int owners = count >> 1;
+ const int in_cache = count & 1;
+
+ SkASSERT(owners >= 2);
+
+ if (2 == owners) {
+ sk_membar_acquire__after_atomic_dec();
+ if (in_cache) {
mtklein 2014/09/23 22:16:55 So, if I add SkASSERT(in_cache) just before each i
reed2 2014/09/24 01:23:57 Sounds like I need to expand my tests to (try to)
+ this->lock();
+ }
+ }
+}
+
+void SkCachedData::unref() {
+ const int32_t count = sk_atomic_add(&fRefCnt, -2) - 2;
+ const int owners = count >> 1;
+ const int in_cache = count & 1;
+
+ switch (owners) {
+ case 0:
+ sk_membar_acquire__after_atomic_dec();
+ SkDELETE(this);
+ break;
+ case 1:
+ sk_membar_acquire__after_atomic_dec();
+ if (in_cache) {
+ this->unlock();
+ }
+ break;
+ default:
+ break;
+ }
+}
+
+void SkCachedData::ref_from_cache() {
+ SkASSERT(0 == (fRefCnt & 1));
+ sk_atomic_add(&fRefCnt, 3);
+}
+
+void SkCachedData::unref_from_cache() {
+ SkASSERT(1 == (fRefCnt & 1));
+ sk_atomic_add(&fRefCnt, -3);
+}
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+class SkCachedData_Discardable : public SkCachedData {
+public:
+ // we take ownership of dm
+ SkCachedData_Discardable(SkDiscardableMemory* dm, size_t size)
+ : INHERITED(dm->data(), size)
+ , fDM(dm)
+ , fSuccessfulLock(false)
+ {}
+
+ virtual ~SkCachedData_Discardable() {
+ SkDELETE(fDM);
+ }
+
+protected:
+ virtual void onLock() SK_OVERRIDE {
+ fSuccessfulLock = fDM->lock();
+ this->setData(fSuccessfulLock ? fDM->data() : NULL);
+ }
+
+ virtual void onUnlock() SK_OVERRIDE {
+ if (fSuccessfulLock) {
+ fDM->unlock();
+ }
+ }
+
+private:
+ SkDiscardableMemory* fDM;
+ bool fSuccessfulLock;
+
+ typedef SkCachedData INHERITED;
+};
+
+class SkCachedData_Malloc : public SkCachedData {
+public:
+ // data was allocated via sk_malloc
+ SkCachedData_Malloc(void* data, size_t size) : INHERITED(data, size), fMallocData(data) {}
+
+ virtual ~SkCachedData_Malloc() {
+ sk_free(fMallocData);
+ }
+
+ virtual void onLock() SK_OVERRIDE {
+ this->setData(fMallocData);
+ }
+
+ virtual void onUnlock() SK_OVERRIDE {}
+
+private:
+ void* fMallocData;
+
+ typedef SkCachedData INHERITED;
+};
+
+SkCachedData* SkCachedData::Create(size_t size, SkDiscardableMemory* dm) {
+ if (dm) {
+ return SkNEW_ARGS(SkCachedData_Discardable, (dm, size));
+ } else {
+ return SkNEW_ARGS(SkCachedData_Malloc, (sk_malloc_throw(size), size));
+ }
+}
+
« no previous file with comments | « src/core/SkCachedData.h ('k') | src/core/SkCachedData_priv.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698