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