blob: d141507004151360931df09b8622862dc480962b [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"
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000012
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +000013#ifndef SK_LAZY_CACHE_STATS
14 #ifdef SK_DEBUG
15 #define SK_LAZY_CACHE_STATS 1
16 #else
17 #define SK_LAZY_CACHE_STATS 0
18 #endif
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000019#endif
20
21/**
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +000022 * An implementation of Discardable Memory that manages a fixed-size
23 * budget of memory. When the allocated memory exceeds this size,
24 * unlocked blocks of memory are purged. If all memory is locked, it
25 * can exceed the memory-use budget.
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000026 */
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000027class SkDiscardableMemoryPool : public SkDiscardableMemory::Factory {
28public:
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +000029 virtual ~SkDiscardableMemoryPool() { }
reed@google.com772443a2013-12-11 15:30:24 +000030
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +000031 virtual size_t getRAMUsed() = 0;
32 virtual void setRAMBudget(size_t budget) = 0;
33 virtual size_t getRAMBudget() = 0;
reed@google.com772443a2013-12-11 15:30:24 +000034
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000035 /** purges all unlocked DMs */
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +000036 virtual void dumpPool() = 0;
reed@google.com772443a2013-12-11 15:30:24 +000037
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +000038 #if SK_LAZY_CACHE_STATS
39 /**
40 * These two values are a count of the number of successful and
41 * failed calls to SkDiscardableMemory::lock() for all DMs managed
42 * by this pool.
43 */
44 virtual int getCacheHits() = 0;
45 virtual int getCacheMisses() = 0;
46 virtual void resetCacheHitsAndMisses() = 0;
47 #endif
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000048
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +000049 /**
50 * This non-global pool can be used for unit tests to verify that
51 * the pool works.
52 * Without mutex, will be not be thread safe.
53 */
54 static SkDiscardableMemoryPool* Create(
55 size_t size, SkBaseMutex* mutex = NULL);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000056};
57
58/**
59 * Returns (and creates if needed) a threadsafe global
60 * SkDiscardableMemoryPool.
61 */
62SkDiscardableMemoryPool* SkGetGlobalDiscardableMemoryPool();
63
64#if !defined(SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE)
65#define SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE (128 * 1024 * 1024)
66#endif
67
68#endif // SkDiscardableMemoryPool_DEFINED