blob: e1825a2f0f90d935a8c5d90637c373d293b07d2b [file] [log] [blame]
reed@google.combaed71f2013-09-26 19:28:27 +00001/*
2 * Copyright 2010 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 SkGlyphCache_Globals_DEFINED
9#define SkGlyphCache_Globals_DEFINED
10
11#include "SkGlyphCache.h"
mtklein1b249332015-07-07 12:21:21 -070012#include "SkMutex.h"
herb35475052015-07-09 11:37:35 -070013#include "SkSpinlock.h"
reed@google.combaed71f2013-09-26 19:28:27 +000014#include "SkTLS.h"
15
16#ifndef SK_DEFAULT_FONT_CACHE_COUNT_LIMIT
reed@google.com12eb7812013-10-09 12:45:51 +000017 #define SK_DEFAULT_FONT_CACHE_COUNT_LIMIT 2048
reed@google.combaed71f2013-09-26 19:28:27 +000018#endif
19
20#ifndef SK_DEFAULT_FONT_CACHE_LIMIT
21 #define SK_DEFAULT_FONT_CACHE_LIMIT (2 * 1024 * 1024)
22#endif
23
24///////////////////////////////////////////////////////////////////////////////
25
reed@google.combaed71f2013-09-26 19:28:27 +000026class SkGlyphCache_Globals {
27public:
herb35475052015-07-09 11:37:35 -070028 SkGlyphCache_Globals() {
halcanary96fcdcc2015-08-27 07:41:13 -070029 fHead = nullptr;
herbcd7f0352015-09-15 15:15:40 -070030 fTotalMemoryUsed = 0;
reed@google.combaed71f2013-09-26 19:28:27 +000031 fCacheSizeLimit = SK_DEFAULT_FONT_CACHE_LIMIT;
32 fCacheCount = 0;
33 fCacheCountLimit = SK_DEFAULT_FONT_CACHE_COUNT_LIMIT;
reed@google.combaed71f2013-09-26 19:28:27 +000034 }
skia.committer@gmail.com65caeaf2013-09-27 07:01:29 +000035
reed@google.combaed71f2013-09-26 19:28:27 +000036 ~SkGlyphCache_Globals() {
37 SkGlyphCache* cache = fHead;
38 while (cache) {
39 SkGlyphCache* next = cache->fNext;
halcanary385fe4d2015-08-26 13:07:48 -070040 delete cache;
reed@google.combaed71f2013-09-26 19:28:27 +000041 cache = next;
42 }
reed@google.combaed71f2013-09-26 19:28:27 +000043 }
skia.committer@gmail.com65caeaf2013-09-27 07:01:29 +000044
herb35475052015-07-09 11:37:35 -070045 SkSpinlock fLock;
reed@google.combaed71f2013-09-26 19:28:27 +000046
47 SkGlyphCache* internalGetHead() const { return fHead; }
48 SkGlyphCache* internalGetTail() const;
49
herbcd7f0352015-09-15 15:15:40 -070050 size_t getTotalMemoryUsed() const { return fTotalMemoryUsed; }
reed@google.combaed71f2013-09-26 19:28:27 +000051 int getCacheCountUsed() const { return fCacheCount; }
52
53#ifdef SK_DEBUG
54 void validate() const;
55#else
56 void validate() const {}
57#endif
58
59 int getCacheCountLimit() const { return fCacheCountLimit; }
60 int setCacheCountLimit(int limit);
61
62 size_t getCacheSizeLimit() const { return fCacheSizeLimit; }
63 size_t setCacheSizeLimit(size_t limit);
64
65 // returns true if this cache is over-budget either due to size limit
66 // or count limit.
67 bool isOverBudget() const {
68 return fCacheCount > fCacheCountLimit ||
herbcd7f0352015-09-15 15:15:40 -070069 fTotalMemoryUsed > fCacheSizeLimit;
reed@google.combaed71f2013-09-26 19:28:27 +000070 }
71
72 void purgeAll(); // does not change budget
73
74 // call when a glyphcache is available for caching (i.e. not in use)
herbcd7f0352015-09-15 15:15:40 -070075 void attachCacheToHead(SkGlyphCache*);
jyasskin951d8542015-09-10 18:11:29 -070076
herb014ffdb2015-09-15 07:03:03 -070077 // can only be called when the mutex is already held
herbcd7f0352015-09-15 15:15:40 -070078 void internalDetachCache(SkGlyphCache*);
79 void internalAttachCacheToHead(SkGlyphCache*);
80
81private:
82 SkGlyphCache* fHead;
83 size_t fTotalMemoryUsed;
84 size_t fCacheSizeLimit;
85 int32_t fCacheCountLimit;
86 int32_t fCacheCount;
skia.committer@gmail.com65caeaf2013-09-27 07:01:29 +000087
reed@google.combaed71f2013-09-26 19:28:27 +000088 // Checkout budgets, modulated by the specified min-bytes-needed-to-purge,
89 // and attempt to purge caches to match.
90 // Returns number of bytes freed.
91 size_t internalPurge(size_t minBytesNeeded = 0);
reed@google.combaed71f2013-09-26 19:28:27 +000092};
93
94#endif