aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/SkGlyphCache_Globals.h
blob: 8e0ceb55c4a0939bb3f151b89e03fa12cbc7b629 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * Copyright 2010 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SkGlyphCache_Globals_DEFINED
#define SkGlyphCache_Globals_DEFINED

#include "SkGlyphCache.h"
#include "SkMutex.h"
#include "SkSpinlock.h"
#include "SkTLS.h"

#ifndef SK_DEFAULT_FONT_CACHE_COUNT_LIMIT
    #define SK_DEFAULT_FONT_CACHE_COUNT_LIMIT   2048
#endif

#ifndef SK_DEFAULT_FONT_CACHE_LIMIT
    #define SK_DEFAULT_FONT_CACHE_LIMIT     (2 * 1024 * 1024)
#endif

///////////////////////////////////////////////////////////////////////////////

class SkGlyphCache_Globals {
public:
    SkGlyphCache_Globals() {
        fHead = NULL;
        fTotalMemoryUsed = 0;
        fCacheSizeLimit = SK_DEFAULT_FONT_CACHE_LIMIT;
        fCacheCount = 0;
        fCacheCountLimit = SK_DEFAULT_FONT_CACHE_COUNT_LIMIT;
    }

    ~SkGlyphCache_Globals() {
        SkGlyphCache* cache = fHead;
        while (cache) {
            SkGlyphCache* next = cache->fNext;
            SkDELETE(cache);
            cache = next;
        }
    }

    SkSpinlock     fLock;

    SkGlyphCache* internalGetHead() const { return fHead; }
    SkGlyphCache* internalGetTail() const;

    size_t getTotalMemoryUsed() const { return fTotalMemoryUsed; }
    int getCacheCountUsed() const { return fCacheCount; }

#ifdef SK_DEBUG
    void validate() const;
#else
    void validate() const {}
#endif

    int getCacheCountLimit() const { return fCacheCountLimit; }
    int setCacheCountLimit(int limit);

    size_t  getCacheSizeLimit() const { return fCacheSizeLimit; }
    size_t  setCacheSizeLimit(size_t limit);

    // returns true if this cache is over-budget either due to size limit
    // or count limit.
    bool isOverBudget() const {
        return fCacheCount > fCacheCountLimit ||
               fTotalMemoryUsed > fCacheSizeLimit;
    }

    void purgeAll(); // does not change budget

    // call when a glyphcache is available for caching (i.e. not in use)
    void attachCacheToHead(SkGlyphCache*);

    // can only be called when the mutex is already held
    void internalDetachCache(SkGlyphCache*);
    void internalAttachCacheToHead(SkGlyphCache*);

private:
    SkGlyphCache* fHead;
    size_t  fTotalMemoryUsed;
    size_t  fCacheSizeLimit;
    int32_t fCacheCountLimit;
    int32_t fCacheCount;

    // Checkout budgets, modulated by the specified min-bytes-needed-to-purge,
    // and attempt to purge caches to match.
    // Returns number of bytes freed.
    size_t internalPurge(size_t minBytesNeeded = 0);
};

#endif