blob: 793e58a2ae26a1b93bb68fbe23eed89ab8efb9fc [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/private/SkMutex.h"
12#include "src/core/SkDiscardableMemory.h"
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000013
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +000014#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.com2c7c7ee2013-12-05 18:31:42 +000020#endif
21
22/**
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +000023 * 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.com2c7c7ee2013-12-05 18:31:42 +000027 */
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000028class SkDiscardableMemoryPool : public SkDiscardableMemory::Factory {
29public:
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +000030 virtual size_t getRAMUsed() = 0;
31 virtual void setRAMBudget(size_t budget) = 0;
32 virtual size_t getRAMBudget() = 0;
reed@google.com772443a2013-12-11 15:30:24 +000033
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000034 /** purges all unlocked DMs */
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +000035 virtual void dumpPool() = 0;
reed@google.com772443a2013-12-11 15:30:24 +000036
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +000037 #if SK_LAZY_CACHE_STATS
38 /**
39 * These two values are a count of the number of successful and
40 * failed calls to SkDiscardableMemory::lock() for all DMs managed
41 * by this pool.
42 */
43 virtual int getCacheHits() = 0;
44 virtual int getCacheMisses() = 0;
45 virtual void resetCacheHitsAndMisses() = 0;
46 #endif
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000047
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +000048 /**
49 * This non-global pool can be used for unit tests to verify that
50 * the pool works.
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +000051 */
Hal Canary788c3c42017-04-25 08:58:57 -040052 static sk_sp<SkDiscardableMemoryPool> Make(size_t size);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000053};
54
55/**
56 * Returns (and creates if needed) a threadsafe global
57 * SkDiscardableMemoryPool.
58 */
59SkDiscardableMemoryPool* SkGetGlobalDiscardableMemoryPool();
60
61#if !defined(SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE)
halcanaryf91b47f2014-08-01 11:54:48 -070062#define SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE (128 * 1024 * 1024)
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000063#endif
64
65#endif // SkDiscardableMemoryPool_DEFINED