blob: f655230a9328783fcb1bdbe0ed0aa356d1ed6019 [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:
23 SkLruImageCache(size_t budget);
24
25 virtual ~SkLruImageCache();
26
27#ifdef SK_DEBUG
scroggo@google.combb281f72013-03-18 21:37:39 +000028 virtual MemoryStatus getMemoryStatus(intptr_t ID) const SK_OVERRIDE;
29 virtual void purgeAllUnpinnedCaches() SK_OVERRIDE;
scroggo@google.comf8d7d272013-02-22 21:38:35 +000030#endif
31
scroggo@google.coma560d00b2013-03-04 21:32:32 +000032 /**
33 * Set the byte limit on cached pixels. If more bytes are used than this, the cache will free
34 * unpinned memory until under the new limit or until all unpinned memory is freed. This will
35 * never free pinned memory, so the cache can potentially remain over the limit. The limit is
36 * enforced each time memory is allocated or released.
37 * 0 is a special flag for an infinite budget.
38 * @return size_t The previous limit.
39 */
40 size_t setImageCacheLimit(size_t newLimit);
41
42 /**
43 * Return the number of bytes of memory currently in use by the cache. Can include memory that
44 * is no longer pinned, but has not been freed.
45 */
46 size_t getImageCacheUsed() const { return fRamUsed; }
scroggo@google.comf8d7d272013-02-22 21:38:35 +000047
48 virtual void* allocAndPinCache(size_t bytes, intptr_t* ID) SK_OVERRIDE;
scroggo@google.combb281f72013-03-18 21:37:39 +000049 virtual void* pinCache(intptr_t ID, SkImageCache::DataStatus*) SK_OVERRIDE;
scroggo@google.comf8d7d272013-02-22 21:38:35 +000050 virtual void releaseCache(intptr_t ID) SK_OVERRIDE;
51 virtual void throwAwayCache(intptr_t ID) SK_OVERRIDE;
52
53private:
54 // Linked list of recently used. Head is the most recently used, and tail is the least.
55 SkTInternalLList<CachedPixels> fLRU;
56 typedef SkTInternalLList<CachedPixels>::Iter Iter;
57
58#ifdef SK_DEBUG
scroggo@google.combb281f72013-03-18 21:37:39 +000059 // fMutex is mutable so that getMemoryStatus can be const
scroggo@google.comf8d7d272013-02-22 21:38:35 +000060 mutable
61#endif
62 SkMutex fMutex;
63 size_t fRamBudget;
64 size_t fRamUsed;
65
66 /**
67 * Find the CachedPixels represented by ID, or NULL if not in the cache. Mutex must be locked
68 * before calling.
69 */
70 CachedPixels* findByID(intptr_t ID) const;
71
72 /**
73 * If over budget, throw away pixels which are not currently in use until below budget or there
74 * are no more pixels eligible to be thrown away. Mutex must be locked before calling.
75 */
76 void purgeIfNeeded();
77
78 /**
79 * Purge until below limit. Mutex must be locked before calling.
80 */
81 void purgeTilAtOrBelow(size_t limit);
82
83 /**
84 * Remove a set of CachedPixels. Mutex must be locked before calling.
85 */
86 void removePixels(CachedPixels*);
87};
88
89#endif // SkLruImageCache_DEFINED