aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/SkResourceCache.h
diff options
context:
space:
mode:
authorGravatar reed <reed@google.com>2014-09-15 11:39:44 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2014-09-15 11:39:45 -0700
commitc90e0149ec530075cae7bf51072a16628311855e (patch)
treed34427f8d88f6ca1b3563214e38eb6f568e199bc /src/core/SkResourceCache.h
parent327f905d2cb0d37c302d651d8f2b17ea56368467 (diff)
Change SkResourceCache to take a Visitor inside its find().
This reverts commit 595aa05efcb504e85358b8d328ac4a9fa1c46e2e. BUG=skia: R=mtklein@google.com, danakj@chromium.org Author: reed@google.com Review URL: https://codereview.chromium.org/569353002
Diffstat (limited to 'src/core/SkResourceCache.h')
-rw-r--r--src/core/SkResourceCache.h49
1 files changed, 33 insertions, 16 deletions
diff --git a/src/core/SkResourceCache.h b/src/core/SkResourceCache.h
index dacd62cedb..d4e1dfa376 100644
--- a/src/core/SkResourceCache.h
+++ b/src/core/SkResourceCache.h
@@ -60,7 +60,7 @@ public:
struct Rec {
typedef SkResourceCache::Key Key;
- Rec() : fLockCount(1) {}
+ Rec() {}
virtual ~Rec() {}
uint32_t getHash() const { return this->getKey().hash(); }
@@ -75,7 +75,6 @@ public:
private:
Rec* fNext;
Rec* fPrev;
- int32_t fLockCount;
friend class SkResourceCache;
};
@@ -83,6 +82,18 @@ public:
typedef const Rec* ID;
/**
+ * Callback function for find(). If called, the cache will have found a match for the
+ * specified Key, and will pass in the corresponding Rec, along with a caller-specified
+ * context. The function can read the data in Rec, and copy whatever it likes into context
+ * (casting context to whatever it really is).
+ *
+ * The return value determines what the cache will do with the Rec. If the function returns
+ * true, then the Rec is considered "valid". If false is returned, the Rec will be considered
+ * "stale" and will be purged from the cache.
+ */
+ typedef bool (*VisitorProc)(const Rec&, void* context);
+
+ /**
* Returns a locked/pinned SkDiscardableMemory instance for the specified
* number of bytes, or NULL on failure.
*/
@@ -93,11 +104,17 @@ public:
* instance of this cache.
*/
- static const Rec* FindAndLock(const Key& key);
- static const Rec* AddAndLock(Rec*);
+ /**
+ * Returns true if the visitor was called on a matching Key, and the visitor returned true.
+ *
+ * Find() will search the cache for the specified Key. If no match is found, return false and
+ * do not call the VisitorProc. If a match is found, return whatever the visitor returns.
+ * Its return value is interpreted to mean:
+ * true : Rec is valid
+ * false : Rec is "stale" -- the cache will purge it.
+ */
+ static bool Find(const Key& key, VisitorProc, void* context);
static void Add(Rec*);
- static void Unlock(ID);
- static void Remove(ID);
static size_t GetTotalBytesUsed();
static size_t GetTotalByteLimit();
@@ -139,18 +156,17 @@ public:
explicit SkResourceCache(size_t byteLimit);
~SkResourceCache();
- const Rec* findAndLock(const Key& key);
- const Rec* addAndLock(Rec*);
- void add(Rec*);
- void remove(Rec*);
-
/**
- * Given a non-null ID ptr returned by either findAndLock or addAndLock,
- * this releases the associated resources to be available to be purged
- * if needed. After this, the cached bitmap should no longer be
- * referenced by the caller.
+ * Returns true if the visitor was called on a matching Key, and the visitor returned true.
+ *
+ * find() will search the cache for the specified Key. If no match is found, return false and
+ * do not call the VisitorProc. If a match is found, return whatever the visitor returns.
+ * Its return value is interpreted to mean:
+ * true : Rec is valid
+ * false : Rec is "stale" -- the cache will purge it.
*/
- void unlock(ID);
+ bool find(const Key&, VisitorProc, void* context);
+ void add(Rec*);
size_t getTotalBytesUsed() const { return fTotalBytesUsed; }
size_t getTotalByteLimit() const { return fTotalByteLimit; }
@@ -202,6 +218,7 @@ private:
void moveToHead(Rec*);
void addToHead(Rec*);
void detach(Rec*);
+ void remove(Rec*);
void init(); // called by constructors