blob: e8f950f6114a4d6ddc0f3b7db5cd086021622c5c [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"
reed@google.combaed71f2013-09-26 19:28:27 +000013#include "SkTLS.h"
14
15#ifndef SK_DEFAULT_FONT_CACHE_COUNT_LIMIT
reed@google.com12eb7812013-10-09 12:45:51 +000016 #define SK_DEFAULT_FONT_CACHE_COUNT_LIMIT 2048
reed@google.combaed71f2013-09-26 19:28:27 +000017#endif
18
19#ifndef SK_DEFAULT_FONT_CACHE_LIMIT
20 #define SK_DEFAULT_FONT_CACHE_LIMIT (2 * 1024 * 1024)
21#endif
22
23///////////////////////////////////////////////////////////////////////////////
24
25class SkMutex;
26
27class SkGlyphCache_Globals {
28public:
29 enum UseMutex {
30 kNo_UseMutex, // thread-local cache
31 kYes_UseMutex // shared cache
32 };
skia.committer@gmail.com65caeaf2013-09-27 07:01:29 +000033
reed@google.combaed71f2013-09-26 19:28:27 +000034 SkGlyphCache_Globals(UseMutex um) {
35 fHead = NULL;
36 fTotalMemoryUsed = 0;
37 fCacheSizeLimit = SK_DEFAULT_FONT_CACHE_LIMIT;
38 fCacheCount = 0;
39 fCacheCountLimit = SK_DEFAULT_FONT_CACHE_COUNT_LIMIT;
skia.committer@gmail.com65caeaf2013-09-27 07:01:29 +000040
reed@google.combaed71f2013-09-26 19:28:27 +000041 fMutex = (kYes_UseMutex == um) ? SkNEW(SkMutex) : NULL;
42 }
skia.committer@gmail.com65caeaf2013-09-27 07:01:29 +000043
reed@google.combaed71f2013-09-26 19:28:27 +000044 ~SkGlyphCache_Globals() {
45 SkGlyphCache* cache = fHead;
46 while (cache) {
47 SkGlyphCache* next = cache->fNext;
48 SkDELETE(cache);
49 cache = next;
50 }
skia.committer@gmail.com65caeaf2013-09-27 07:01:29 +000051
reed@google.combaed71f2013-09-26 19:28:27 +000052 SkDELETE(fMutex);
53 }
skia.committer@gmail.com65caeaf2013-09-27 07:01:29 +000054
reed@google.combaed71f2013-09-26 19:28:27 +000055 SkMutex* fMutex;
56
57 SkGlyphCache* internalGetHead() const { return fHead; }
58 SkGlyphCache* internalGetTail() const;
59
60 size_t getTotalMemoryUsed() const { return fTotalMemoryUsed; }
61 int getCacheCountUsed() const { return fCacheCount; }
62
63#ifdef SK_DEBUG
64 void validate() const;
65#else
66 void validate() const {}
67#endif
68
69 int getCacheCountLimit() const { return fCacheCountLimit; }
70 int setCacheCountLimit(int limit);
71
72 size_t getCacheSizeLimit() const { return fCacheSizeLimit; }
73 size_t setCacheSizeLimit(size_t limit);
74
75 // returns true if this cache is over-budget either due to size limit
76 // or count limit.
77 bool isOverBudget() const {
78 return fCacheCount > fCacheCountLimit ||
79 fTotalMemoryUsed > fCacheSizeLimit;
80 }
81
82 void purgeAll(); // does not change budget
83
84 // call when a glyphcache is available for caching (i.e. not in use)
85 void attachCacheToHead(SkGlyphCache*);
skia.committer@gmail.com65caeaf2013-09-27 07:01:29 +000086
reed@google.combaed71f2013-09-26 19:28:27 +000087 // can only be called when the mutex is already held
88 void internalDetachCache(SkGlyphCache*);
89 void internalAttachCacheToHead(SkGlyphCache*);
90
91 // can return NULL
92 static SkGlyphCache_Globals* FindTLS() {
93 return (SkGlyphCache_Globals*)SkTLS::Find(CreateTLS);
94 }
skia.committer@gmail.com65caeaf2013-09-27 07:01:29 +000095
reed@google.combaed71f2013-09-26 19:28:27 +000096 static SkGlyphCache_Globals& GetTLS() {
97 return *(SkGlyphCache_Globals*)SkTLS::Get(CreateTLS, DeleteTLS);
98 }
skia.committer@gmail.com65caeaf2013-09-27 07:01:29 +000099
reed@google.combaed71f2013-09-26 19:28:27 +0000100 static void DeleteTLS() { SkTLS::Delete(CreateTLS); }
skia.committer@gmail.com65caeaf2013-09-27 07:01:29 +0000101
reed@google.combaed71f2013-09-26 19:28:27 +0000102private:
103 SkGlyphCache* fHead;
104 size_t fTotalMemoryUsed;
105 size_t fCacheSizeLimit;
106 int32_t fCacheCountLimit;
107 int32_t fCacheCount;
skia.committer@gmail.com65caeaf2013-09-27 07:01:29 +0000108
reed@google.combaed71f2013-09-26 19:28:27 +0000109 // Checkout budgets, modulated by the specified min-bytes-needed-to-purge,
110 // and attempt to purge caches to match.
111 // Returns number of bytes freed.
112 size_t internalPurge(size_t minBytesNeeded = 0);
113
114 static void* CreateTLS() {
115 return SkNEW_ARGS(SkGlyphCache_Globals, (kNo_UseMutex));
116 }
skia.committer@gmail.com65caeaf2013-09-27 07:01:29 +0000117
reed@google.combaed71f2013-09-26 19:28:27 +0000118 static void DeleteTLS(void* ptr) {
119 SkDELETE((SkGlyphCache_Globals*)ptr);
120 }
121};
122
123#endif