xref: /aosp_15_r20/external/skia/src/lazy/SkDiscardableMemoryPool.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 "include/private/base/SkMutex.h"
12 #include "include/private/chromium/SkDiscardableMemory.h"
13 
14 #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
20 #endif
21 
22 /**
23  *  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.
27  */
28 class SkDiscardableMemoryPool : public SkDiscardableMemory::Factory {
29 public:
30     virtual size_t getRAMUsed() = 0;
31     virtual void setRAMBudget(size_t budget) = 0;
32     virtual size_t getRAMBudget() = 0;
33 
34     /** purges all unlocked DMs */
35     virtual void dumpPool() = 0;
36 
37     #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
47 
48     /**
49      *  This non-global pool can be used for unit tests to verify that
50      *  the pool works.
51      */
52     static sk_sp<SkDiscardableMemoryPool> Make(size_t size);
53 };
54 
55 /**
56  *  Returns (and creates if needed) a threadsafe global
57  *  SkDiscardableMemoryPool.
58  */
59 SkDiscardableMemoryPool* SkGetGlobalDiscardableMemoryPool();
60 
61 #if !defined(SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE)
62 #define SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE (128 * 1024 * 1024)
63 #endif
64 
65 #endif  // SkDiscardableMemoryPool_DEFINED
66