halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 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 | #include "SkThread.h" |
| 14 | |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 15 | class SkPoolDiscardableMemory; |
| 16 | |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 17 | #ifdef SK_DEBUG |
| 18 | #define LAZY_CACHE_STATS 1 |
| 19 | #elif !defined(LAZY_CACHE_STATS) |
| 20 | #define LAZY_CACHE_STATS 0 |
| 21 | #endif |
| 22 | |
| 23 | /** |
| 24 | * This non-global pool can be used for unit tests to verify that the |
| 25 | * pool works. |
| 26 | */ |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 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); |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 33 | virtual ~SkDiscardableMemoryPool(); |
| 34 | |
| 35 | virtual SkDiscardableMemory* create(size_t bytes) SK_OVERRIDE; |
| 36 | |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 37 | size_t getRAMUsed(); |
| 38 | void setRAMBudget(size_t budget); |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 39 | |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 40 | /** purges all unlocked DMs */ |
| 41 | void dumpPool(); |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 42 | |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 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; |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 53 | |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 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); |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 62 | |
| 63 | friend class SkPoolDiscardableMemory; |
| 64 | |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 65 | typedef SkDiscardableMemory::Factory INHERITED; |
| 66 | }; |
| 67 | |
| 68 | /** |
| 69 | * Returns (and creates if needed) a threadsafe global |
| 70 | * SkDiscardableMemoryPool. |
| 71 | */ |
| 72 | SkDiscardableMemoryPool* SkGetGlobalDiscardableMemoryPool(); |
| 73 | |
| 74 | #if !defined(SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE) |
| 75 | #define SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE (128 * 1024 * 1024) |
| 76 | #endif |
| 77 | |
| 78 | #endif // SkDiscardableMemoryPool_DEFINED |