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 "SkDiscardableMemory.h"
12#include "SkMutex.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 */
28class SkDiscardableMemoryPool : public SkDiscardableMemory::Factory {
29public:
30    virtual ~SkDiscardableMemoryPool() { }
31
32    virtual size_t getRAMUsed() = 0;
33    virtual void setRAMBudget(size_t budget) = 0;
34    virtual size_t getRAMBudget() = 0;
35
36    /** purges all unlocked DMs */
37    virtual void dumpPool() = 0;
38
39    #if SK_LAZY_CACHE_STATS
40    /**
41     * These two values are a count of the number of successful and
42     * failed calls to SkDiscardableMemory::lock() for all DMs managed
43     * by this pool.
44     */
45    virtual int getCacheHits() = 0;
46    virtual int getCacheMisses() = 0;
47    virtual void resetCacheHitsAndMisses() = 0;
48    #endif
49
50    /**
51     *  This non-global pool can be used for unit tests to verify that
52     *  the pool works.
53     */
54    static sk_sp<SkDiscardableMemoryPool> Make(size_t size);
55};
56
57/**
58 *  Returns (and creates if needed) a threadsafe global
59 *  SkDiscardableMemoryPool.
60 */
61SkDiscardableMemoryPool* SkGetGlobalDiscardableMemoryPool();
62
63#if !defined(SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE)
64#define SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE (128 * 1024 * 1024)
65#endif
66
67#endif  // SkDiscardableMemoryPool_DEFINED
68