OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkDiscardableMemoryPool_DEFINED | 8 #ifndef SkDiscardableMemoryPool_DEFINED |
9 #define SkDiscardableMemoryPool_DEFINED | 9 #define SkDiscardableMemoryPool_DEFINED |
10 | 10 |
11 #include "SkDiscardableMemory.h" | 11 #include "SkDiscardableMemory.h" |
12 #include "SkTInternalLList.h" | |
13 #include "SkThread.h" | |
14 | 12 |
15 class SkPoolDiscardableMemory; | 13 #ifndef SK_LAZY_CACHE_STATS |
16 | 14 #ifdef SK_DEBUG |
17 #ifdef SK_DEBUG | 15 #define SK_LAZY_CACHE_STATS 1 |
18 #define LAZY_CACHE_STATS 1 | 16 #else |
19 #elif !defined(LAZY_CACHE_STATS) | 17 #define SK_LAZY_CACHE_STATS 0 |
20 #define LAZY_CACHE_STATS 0 | 18 #endif |
21 #endif | 19 #endif |
22 | 20 |
23 /** | 21 /** |
24 * This non-global pool can be used for unit tests to verify that the | 22 * An implementation of Discardable Memory that manages a fixed-size |
25 * pool works. | 23 * budget of memory. When the allocated memory exceeds this size, |
| 24 * unlocked blocks of memory are purged. If all memory is locked, it |
| 25 * can exceed the memory-use budget. |
26 */ | 26 */ |
27 class SkDiscardableMemoryPool : public SkDiscardableMemory::Factory { | 27 class SkDiscardableMemoryPool : public SkDiscardableMemory::Factory { |
28 public: | 28 public: |
| 29 virtual ~SkDiscardableMemoryPool() { } |
| 30 |
| 31 virtual size_t getRAMUsed() = 0; |
| 32 virtual void setRAMBudget(size_t budget) = 0; |
| 33 virtual size_t getRAMBudget() = 0; |
| 34 |
| 35 /** purges all unlocked DMs */ |
| 36 virtual void dumpPool() = 0; |
| 37 |
| 38 #if SK_LAZY_CACHE_STATS |
29 /** | 39 /** |
| 40 * These two values are a count of the number of successful and |
| 41 * failed calls to SkDiscardableMemory::lock() for all DMs managed |
| 42 * by this pool. |
| 43 */ |
| 44 virtual int getCacheHits() = 0; |
| 45 virtual int getCacheMisses() = 0; |
| 46 virtual void resetCacheHitsAndMisses() = 0; |
| 47 #endif |
| 48 |
| 49 /** |
| 50 * This non-global pool can be used for unit tests to verify that |
| 51 * the pool works. |
30 * Without mutex, will be not be thread safe. | 52 * Without mutex, will be not be thread safe. |
31 */ | 53 */ |
32 SkDiscardableMemoryPool(size_t budget, SkBaseMutex* mutex = NULL); | 54 static SkDiscardableMemoryPool* Create( |
33 virtual ~SkDiscardableMemoryPool(); | 55 size_t size, SkBaseMutex* mutex = NULL); |
34 | |
35 virtual SkDiscardableMemory* create(size_t bytes) SK_OVERRIDE; | |
36 | |
37 size_t getRAMUsed(); | |
38 void setRAMBudget(size_t budget); | |
39 | |
40 /** purges all unlocked DMs */ | |
41 void dumpPool(); | |
42 | |
43 #if LAZY_CACHE_STATS | |
44 int fCacheHits; | |
45 int fCacheMisses; | |
46 #endif // LAZY_CACHE_STATS | |
47 | |
48 private: | |
49 SkBaseMutex* fMutex; | |
50 size_t fBudget; | |
51 size_t fUsed; | |
52 SkTInternalLList<SkPoolDiscardableMemory> fList; | |
53 | |
54 /** Function called to free memory if needed */ | |
55 void dumpDownTo(size_t budget); | |
56 /** called by SkDiscardableMemoryPool upon destruction */ | |
57 void free(SkPoolDiscardableMemory* dm); | |
58 /** called by SkDiscardableMemoryPool::lock() */ | |
59 bool lock(SkPoolDiscardableMemory* dm); | |
60 /** called by SkDiscardableMemoryPool::unlock() */ | |
61 void unlock(SkPoolDiscardableMemory* dm); | |
62 | |
63 friend class SkPoolDiscardableMemory; | |
64 | |
65 typedef SkDiscardableMemory::Factory INHERITED; | |
66 }; | 56 }; |
67 | 57 |
68 /** | 58 /** |
69 * Returns (and creates if needed) a threadsafe global | 59 * Returns (and creates if needed) a threadsafe global |
70 * SkDiscardableMemoryPool. | 60 * SkDiscardableMemoryPool. |
71 */ | 61 */ |
72 SkDiscardableMemoryPool* SkGetGlobalDiscardableMemoryPool(); | 62 SkDiscardableMemoryPool* SkGetGlobalDiscardableMemoryPool(); |
73 | 63 |
74 #if !defined(SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE) | 64 #if !defined(SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE) |
75 #define SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE (128 * 1024 * 1024) | 65 #define SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE (128 * 1024 * 1024) |
76 #endif | 66 #endif |
77 | 67 |
78 #endif // SkDiscardableMemoryPool_DEFINED | 68 #endif // SkDiscardableMemoryPool_DEFINED |
OLD | NEW |