blob: 61bad701825aaa26279d22b4c9f75c7acabfb6a5 [file] [log] [blame]
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +00001/*
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.com772443a2013-12-11 15:30:24 +000015class SkPoolDiscardableMemory;
16
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000017#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.com2c7c7ee2013-12-05 18:31:42 +000027class SkDiscardableMemoryPool : public SkDiscardableMemory::Factory {
28public:
29 /**
30 * Without mutex, will be not be thread safe.
31 */
32 SkDiscardableMemoryPool(size_t budget, SkBaseMutex* mutex = NULL);
reed@google.com772443a2013-12-11 15:30:24 +000033 virtual ~SkDiscardableMemoryPool();
34
35 virtual SkDiscardableMemory* create(size_t bytes) SK_OVERRIDE;
36
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000037 size_t getRAMUsed();
38 void setRAMBudget(size_t budget);
reed@google.com772443a2013-12-11 15:30:24 +000039
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000040 /** purges all unlocked DMs */
41 void dumpPool();
reed@google.com772443a2013-12-11 15:30:24 +000042
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000043 #if LAZY_CACHE_STATS
44 int fCacheHits;
45 int fCacheMisses;
46 #endif // LAZY_CACHE_STATS
47
48private:
49 SkBaseMutex* fMutex;
50 size_t fBudget;
51 size_t fUsed;
52 SkTInternalLList<SkPoolDiscardableMemory> fList;
reed@google.com772443a2013-12-11 15:30:24 +000053
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000054 /** 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.com772443a2013-12-11 15:30:24 +000062
63 friend class SkPoolDiscardableMemory;
64
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000065 typedef SkDiscardableMemory::Factory INHERITED;
66};
67
68/**
69 * Returns (and creates if needed) a threadsafe global
70 * SkDiscardableMemoryPool.
71 */
72SkDiscardableMemoryPool* 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