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" |
mtklein | 7b274c7 | 2015-02-03 13:38:58 -0800 | [diff] [blame] | 12 | #include "SkMutex.h" |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 13 | |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 14 | #ifndef SK_LAZY_CACHE_STATS |
| 15 | #ifdef SK_DEBUG |
| 16 | #define SK_LAZY_CACHE_STATS 1 |
| 17 | #else |
| 18 | #define SK_LAZY_CACHE_STATS 0 |
| 19 | #endif |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 20 | #endif |
| 21 | |
| 22 | /** |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 23 | * An implementation of Discardable Memory that manages a fixed-size |
| 24 | * budget of memory. When the allocated memory exceeds this size, |
| 25 | * unlocked blocks of memory are purged. If all memory is locked, it |
| 26 | * can exceed the memory-use budget. |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 27 | */ |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 28 | class SkDiscardableMemoryPool : public SkDiscardableMemory::Factory { |
| 29 | public: |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 30 | virtual ~SkDiscardableMemoryPool() { } |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 31 | |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 32 | virtual size_t getRAMUsed() = 0; |
| 33 | virtual void setRAMBudget(size_t budget) = 0; |
| 34 | virtual size_t getRAMBudget() = 0; |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 35 | |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 36 | /** purges all unlocked DMs */ |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 37 | virtual void dumpPool() = 0; |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 38 | |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 39 | #if SK_LAZY_CACHE_STATS |
| 40 | /** |
| 41 | * These two values are a count of the number of successful and |
| 42 | * failed calls to SkDiscardableMemory::lock() for all DMs managed |
| 43 | * by this pool. |
| 44 | */ |
| 45 | virtual int getCacheHits() = 0; |
| 46 | virtual int getCacheMisses() = 0; |
| 47 | virtual void resetCacheHitsAndMisses() = 0; |
| 48 | #endif |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 49 | |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 50 | /** |
| 51 | * This non-global pool can be used for unit tests to verify that |
| 52 | * the pool works. |
| 53 | * Without mutex, will be not be thread safe. |
| 54 | */ |
| 55 | static SkDiscardableMemoryPool* Create( |
| 56 | size_t size, SkBaseMutex* mutex = NULL); |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | /** |
| 60 | * Returns (and creates if needed) a threadsafe global |
| 61 | * SkDiscardableMemoryPool. |
| 62 | */ |
| 63 | SkDiscardableMemoryPool* SkGetGlobalDiscardableMemoryPool(); |
| 64 | |
| 65 | #if !defined(SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE) |
halcanary | f91b47f | 2014-08-01 11:54:48 -0700 | [diff] [blame] | 66 | #define SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE (128 * 1024 * 1024) |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 67 | #endif |
| 68 | |
| 69 | #endif // SkDiscardableMemoryPool_DEFINED |