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" | 12 #include "SkTInternalLList.h" |
13 #include "SkThread.h" | 13 #include "SkThread.h" |
14 | 14 |
| 15 class SkPoolDiscardableMemory; |
| 16 |
15 #ifdef SK_DEBUG | 17 #ifdef SK_DEBUG |
16 #define LAZY_CACHE_STATS 1 | 18 #define LAZY_CACHE_STATS 1 |
17 #elif !defined(LAZY_CACHE_STATS) | 19 #elif !defined(LAZY_CACHE_STATS) |
18 #define LAZY_CACHE_STATS 0 | 20 #define LAZY_CACHE_STATS 0 |
19 #endif | 21 #endif |
20 | 22 |
21 /** | 23 /** |
22 * This non-global pool can be used for unit tests to verify that the | 24 * This non-global pool can be used for unit tests to verify that the |
23 * pool works. | 25 * pool works. |
24 */ | 26 */ |
25 class SkPoolDiscardableMemory; | |
26 class SkDiscardableMemoryPool : public SkDiscardableMemory::Factory { | 27 class SkDiscardableMemoryPool : public SkDiscardableMemory::Factory { |
27 public: | 28 public: |
28 /** | 29 /** |
29 * Without mutex, will be not be thread safe. | 30 * Without mutex, will be not be thread safe. |
30 */ | 31 */ |
31 SkDiscardableMemoryPool(size_t budget, SkBaseMutex* mutex = NULL); | 32 SkDiscardableMemoryPool(size_t budget, SkBaseMutex* mutex = NULL); |
32 ~SkDiscardableMemoryPool(); | 33 virtual ~SkDiscardableMemoryPool(); |
33 SkDiscardableMemory* create(size_t bytes) SK_OVERRIDE; | 34 |
| 35 virtual SkDiscardableMemory* create(size_t bytes) SK_OVERRIDE; |
| 36 |
34 size_t getRAMUsed(); | 37 size_t getRAMUsed(); |
35 void setRAMBudget(size_t budget); | 38 void setRAMBudget(size_t budget); |
| 39 |
36 /** purges all unlocked DMs */ | 40 /** purges all unlocked DMs */ |
37 void dumpPool(); | 41 void dumpPool(); |
38 friend class SkPoolDiscardableMemory; | 42 |
39 #if LAZY_CACHE_STATS | 43 #if LAZY_CACHE_STATS |
40 int fCacheHits; | 44 int fCacheHits; |
41 int fCacheMisses; | 45 int fCacheMisses; |
42 #endif // LAZY_CACHE_STATS | 46 #endif // LAZY_CACHE_STATS |
43 | 47 |
44 private: | 48 private: |
45 SkBaseMutex* fMutex; | 49 SkBaseMutex* fMutex; |
46 size_t fBudget; | 50 size_t fBudget; |
47 size_t fUsed; | 51 size_t fUsed; |
48 SkTInternalLList<SkPoolDiscardableMemory> fList; | 52 SkTInternalLList<SkPoolDiscardableMemory> fList; |
| 53 |
49 /** Function called to free memory if needed */ | 54 /** Function called to free memory if needed */ |
50 void dumpDownTo(size_t budget); | 55 void dumpDownTo(size_t budget); |
51 /** called by SkDiscardableMemoryPool upon destruction */ | 56 /** called by SkDiscardableMemoryPool upon destruction */ |
52 void free(SkPoolDiscardableMemory* dm); | 57 void free(SkPoolDiscardableMemory* dm); |
53 /** called by SkDiscardableMemoryPool::lock() */ | 58 /** called by SkDiscardableMemoryPool::lock() */ |
54 bool lock(SkPoolDiscardableMemory* dm); | 59 bool lock(SkPoolDiscardableMemory* dm); |
55 /** called by SkDiscardableMemoryPool::unlock() */ | 60 /** called by SkDiscardableMemoryPool::unlock() */ |
56 void unlock(SkPoolDiscardableMemory* dm); | 61 void unlock(SkPoolDiscardableMemory* dm); |
| 62 |
| 63 friend class SkPoolDiscardableMemory; |
| 64 |
57 typedef SkDiscardableMemory::Factory INHERITED; | 65 typedef SkDiscardableMemory::Factory INHERITED; |
58 }; | 66 }; |
59 | 67 |
60 /** | 68 /** |
61 * Returns (and creates if needed) a threadsafe global | 69 * Returns (and creates if needed) a threadsafe global |
62 * SkDiscardableMemoryPool. | 70 * SkDiscardableMemoryPool. |
63 */ | 71 */ |
64 SkDiscardableMemoryPool* SkGetGlobalDiscardableMemoryPool(); | 72 SkDiscardableMemoryPool* SkGetGlobalDiscardableMemoryPool(); |
65 | 73 |
66 #if !defined(SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE) | 74 #if !defined(SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE) |
67 #define SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE (128 * 1024 * 1024) | 75 #define SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE (128 * 1024 * 1024) |
68 #endif | 76 #endif |
69 | 77 |
70 #endif // SkDiscardableMemoryPool_DEFINED | 78 #endif // SkDiscardableMemoryPool_DEFINED |
OLD | NEW |