1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/lazy/SkDiscardableMemoryPool.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,78 @@ 1.4 +/* 1.5 + * Copyright 2013 Google Inc. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 + 1.11 +#ifndef SkDiscardableMemoryPool_DEFINED 1.12 +#define SkDiscardableMemoryPool_DEFINED 1.13 + 1.14 +#include "SkDiscardableMemory.h" 1.15 +#include "SkTInternalLList.h" 1.16 +#include "SkThread.h" 1.17 + 1.18 +class SkPoolDiscardableMemory; 1.19 + 1.20 +#ifdef SK_DEBUG 1.21 + #define LAZY_CACHE_STATS 1 1.22 +#elif !defined(LAZY_CACHE_STATS) 1.23 + #define LAZY_CACHE_STATS 0 1.24 +#endif 1.25 + 1.26 +/** 1.27 + * This non-global pool can be used for unit tests to verify that the 1.28 + * pool works. 1.29 + */ 1.30 +class SkDiscardableMemoryPool : public SkDiscardableMemory::Factory { 1.31 +public: 1.32 + /** 1.33 + * Without mutex, will be not be thread safe. 1.34 + */ 1.35 + SkDiscardableMemoryPool(size_t budget, SkBaseMutex* mutex = NULL); 1.36 + virtual ~SkDiscardableMemoryPool(); 1.37 + 1.38 + virtual SkDiscardableMemory* create(size_t bytes) SK_OVERRIDE; 1.39 + 1.40 + size_t getRAMUsed(); 1.41 + void setRAMBudget(size_t budget); 1.42 + 1.43 + /** purges all unlocked DMs */ 1.44 + void dumpPool(); 1.45 + 1.46 + #if LAZY_CACHE_STATS 1.47 + int fCacheHits; 1.48 + int fCacheMisses; 1.49 + #endif // LAZY_CACHE_STATS 1.50 + 1.51 +private: 1.52 + SkBaseMutex* fMutex; 1.53 + size_t fBudget; 1.54 + size_t fUsed; 1.55 + SkTInternalLList<SkPoolDiscardableMemory> fList; 1.56 + 1.57 + /** Function called to free memory if needed */ 1.58 + void dumpDownTo(size_t budget); 1.59 + /** called by SkDiscardableMemoryPool upon destruction */ 1.60 + void free(SkPoolDiscardableMemory* dm); 1.61 + /** called by SkDiscardableMemoryPool::lock() */ 1.62 + bool lock(SkPoolDiscardableMemory* dm); 1.63 + /** called by SkDiscardableMemoryPool::unlock() */ 1.64 + void unlock(SkPoolDiscardableMemory* dm); 1.65 + 1.66 + friend class SkPoolDiscardableMemory; 1.67 + 1.68 + typedef SkDiscardableMemory::Factory INHERITED; 1.69 +}; 1.70 + 1.71 +/** 1.72 + * Returns (and creates if needed) a threadsafe global 1.73 + * SkDiscardableMemoryPool. 1.74 + */ 1.75 +SkDiscardableMemoryPool* SkGetGlobalDiscardableMemoryPool(); 1.76 + 1.77 +#if !defined(SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE) 1.78 +#define SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE (128 * 1024 * 1024) 1.79 +#endif 1.80 + 1.81 +#endif // SkDiscardableMemoryPool_DEFINED