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