blob: 4d7fe22d807dcd2dc8b12ce55acae983cd1ef522 [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
herb632d0042016-06-22 10:19:16 -070045 mutable SkSpinlock fLock;
reed@google.combaed71f2013-09-26 19:28:27 +000046
47 SkGlyphCache* internalGetHead() const { return fHead; }
48 SkGlyphCache* internalGetTail() const;
49
herb632d0042016-06-22 10:19:16 -070050 size_t getTotalMemoryUsed() const;
51 int getCacheCountUsed() const;
reed@google.combaed71f2013-09-26 19:28:27 +000052
53#ifdef SK_DEBUG
54 void validate() const;
55#else
56 void validate() const {}
57#endif
58
herb632d0042016-06-22 10:19:16 -070059 int getCacheCountLimit() const;
reed@google.combaed71f2013-09-26 19:28:27 +000060 int setCacheCountLimit(int limit);
61
herb632d0042016-06-22 10:19:16 -070062 size_t getCacheSizeLimit() const;
reed@google.combaed71f2013-09-26 19:28:27 +000063 size_t setCacheSizeLimit(size_t limit);
64
reed@google.combaed71f2013-09-26 19:28:27 +000065 void purgeAll(); // does not change budget
66
67 // call when a glyphcache is available for caching (i.e. not in use)
herbcd7f0352015-09-15 15:15:40 -070068 void attachCacheToHead(SkGlyphCache*);
jyasskin951d8542015-09-10 18:11:29 -070069
herb014ffdb2015-09-15 07:03:03 -070070 // can only be called when the mutex is already held
herbcd7f0352015-09-15 15:15:40 -070071 void internalDetachCache(SkGlyphCache*);
72 void internalAttachCacheToHead(SkGlyphCache*);
73
74private:
75 SkGlyphCache* fHead;
76 size_t fTotalMemoryUsed;
77 size_t fCacheSizeLimit;
78 int32_t fCacheCountLimit;
79 int32_t fCacheCount;
skia.committer@gmail.com65caeaf2013-09-27 07:01:29 +000080
reed@google.combaed71f2013-09-26 19:28:27 +000081 // Checkout budgets, modulated by the specified min-bytes-needed-to-purge,
82 // and attempt to purge caches to match.
83 // Returns number of bytes freed.
84 size_t internalPurge(size_t minBytesNeeded = 0);
reed@google.combaed71f2013-09-26 19:28:27 +000085};
86
87#endif