blob: 44446000cdf0f96370c8ecbed5cbe40a555d8b89 [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
28 CacheStatus getCacheStatus(intptr_t ID) const SK_OVERRIDE;
29#endif
30
31 void setBudget(size_t newBudget);
32
33 virtual void* allocAndPinCache(size_t bytes, intptr_t* ID) SK_OVERRIDE;
34 virtual void* pinCache(intptr_t ID) SK_OVERRIDE;
35 virtual void releaseCache(intptr_t ID) SK_OVERRIDE;
36 virtual void throwAwayCache(intptr_t ID) SK_OVERRIDE;
37
38private:
39 // Linked list of recently used. Head is the most recently used, and tail is the least.
40 SkTInternalLList<CachedPixels> fLRU;
41 typedef SkTInternalLList<CachedPixels>::Iter Iter;
42
43#ifdef SK_DEBUG
44 // fMutex is mutable so that getCacheStatus can be const
45 mutable
46#endif
47 SkMutex fMutex;
48 size_t fRamBudget;
49 size_t fRamUsed;
50
51 /**
52 * Find the CachedPixels represented by ID, or NULL if not in the cache. Mutex must be locked
53 * before calling.
54 */
55 CachedPixels* findByID(intptr_t ID) const;
56
57 /**
58 * If over budget, throw away pixels which are not currently in use until below budget or there
59 * are no more pixels eligible to be thrown away. Mutex must be locked before calling.
60 */
61 void purgeIfNeeded();
62
63 /**
64 * Purge until below limit. Mutex must be locked before calling.
65 */
66 void purgeTilAtOrBelow(size_t limit);
67
68 /**
69 * Remove a set of CachedPixels. Mutex must be locked before calling.
70 */
71 void removePixels(CachedPixels*);
72};
73
74#endif // SkLruImageCache_DEFINED