blob: 85095618b787f5fce18eb70995eeaa3a42afb944 [file] [log] [blame]
scroggo@google.comf8d7d272013-02-22 21:38:35 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkLruImageCache_DEFINED
9#define SkLruImageCache_DEFINED
10
11#include "SkImageCache.h"
12#include "SkThread.h"
13#include "SkTInternalLList.h"
14
15class CachedPixels;
16
17/**
18 * SkImageCache implementation that uses an LRU cache to age out old images.
19 */
20class SkLruImageCache : public SkImageCache {
21
22public:
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +000023 SK_DECLARE_INST_COUNT(SkLruImageCache)
24
scroggo@google.comf8d7d272013-02-22 21:38:35 +000025 SkLruImageCache(size_t budget);
26
27 virtual ~SkLruImageCache();
28
29#ifdef SK_DEBUG
reed@google.com6e8b7dd2013-07-09 21:31:54 +000030 virtual MemoryStatus getMemoryStatus(ID) const SK_OVERRIDE;
scroggo@google.combb281f72013-03-18 21:37:39 +000031 virtual void purgeAllUnpinnedCaches() SK_OVERRIDE;
scroggo@google.comf8d7d272013-02-22 21:38:35 +000032#endif
33
scroggo@google.coma560d00b2013-03-04 21:32:32 +000034 /**
35 * Set the byte limit on cached pixels. If more bytes are used than this, the cache will free
36 * unpinned memory until under the new limit or until all unpinned memory is freed. This will
37 * never free pinned memory, so the cache can potentially remain over the limit. The limit is
38 * enforced each time memory is allocated or released.
39 * 0 is a special flag for an infinite budget.
40 * @return size_t The previous limit.
41 */
42 size_t setImageCacheLimit(size_t newLimit);
43
44 /**
45 * Return the number of bytes of memory currently in use by the cache. Can include memory that
46 * is no longer pinned, but has not been freed.
47 */
48 size_t getImageCacheUsed() const { return fRamUsed; }
scroggo@google.comf8d7d272013-02-22 21:38:35 +000049
reed@google.com6e8b7dd2013-07-09 21:31:54 +000050 virtual void* allocAndPinCache(size_t bytes, ID*) SK_OVERRIDE;
51 virtual void* pinCache(ID, SkImageCache::DataStatus*) SK_OVERRIDE;
52 virtual void releaseCache(ID) SK_OVERRIDE;
53 virtual void throwAwayCache(ID) SK_OVERRIDE;
scroggo@google.comf8d7d272013-02-22 21:38:35 +000054
55private:
56 // Linked list of recently used. Head is the most recently used, and tail is the least.
57 SkTInternalLList<CachedPixels> fLRU;
58 typedef SkTInternalLList<CachedPixels>::Iter Iter;
59
60#ifdef SK_DEBUG
scroggo@google.combb281f72013-03-18 21:37:39 +000061 // fMutex is mutable so that getMemoryStatus can be const
scroggo@google.comf8d7d272013-02-22 21:38:35 +000062 mutable
63#endif
64 SkMutex fMutex;
65 size_t fRamBudget;
66 size_t fRamUsed;
67
68 /**
69 * Find the CachedPixels represented by ID, or NULL if not in the cache. Mutex must be locked
70 * before calling.
71 */
reed@google.com6e8b7dd2013-07-09 21:31:54 +000072 CachedPixels* findByID(ID) const;
scroggo@google.comf8d7d272013-02-22 21:38:35 +000073
74 /**
75 * If over budget, throw away pixels which are not currently in use until below budget or there
76 * are no more pixels eligible to be thrown away. Mutex must be locked before calling.
77 */
78 void purgeIfNeeded();
79
80 /**
81 * Purge until below limit. Mutex must be locked before calling.
82 */
83 void purgeTilAtOrBelow(size_t limit);
84
85 /**
86 * Remove a set of CachedPixels. Mutex must be locked before calling.
87 */
88 void removePixels(CachedPixels*);
commit-bot@chromium.orgab1c1382013-12-05 12:08:12 +000089 typedef SkImageCache INHERITED;
scroggo@google.comf8d7d272013-02-22 21:38:35 +000090};
91
92#endif // SkLruImageCache_DEFINED