blob: eaf360b361e101d763e3b297ce824d52802e8512 [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
Herb Derby840c66c2018-04-16 16:42:08 -04008#ifndef SkStrikeCache_DEFINED
9#define SkStrikeCache_DEFINED
reed@google.combaed71f2013-09-26 19:28:27 +000010
Herbert Derby671e7ee2018-04-17 08:04:47 -040011#include "SkDescriptor.h"
herb35475052015-07-09 11:37:35 -070012#include "SkSpinlock.h"
Herbert Derby671e7ee2018-04-17 08:04:47 -040013#include "SkTemplates.h"
Herb Derbydfeb2aa2018-02-28 18:47:27 -050014
15class SkGlyphCache;
Herbert Derby671e7ee2018-04-17 08:04:47 -040016class SkTraceMemoryDump;
reed@google.combaed71f2013-09-26 19:28:27 +000017
18#ifndef SK_DEFAULT_FONT_CACHE_COUNT_LIMIT
reed@google.com12eb7812013-10-09 12:45:51 +000019 #define SK_DEFAULT_FONT_CACHE_COUNT_LIMIT 2048
reed@google.combaed71f2013-09-26 19:28:27 +000020#endif
21
22#ifndef SK_DEFAULT_FONT_CACHE_LIMIT
23 #define SK_DEFAULT_FONT_CACHE_LIMIT (2 * 1024 * 1024)
24#endif
25
Mike Reedd5bee5d2017-06-01 14:45:44 -040026#ifndef SK_DEFAULT_FONT_CACHE_POINT_SIZE_LIMIT
27 #define SK_DEFAULT_FONT_CACHE_POINT_SIZE_LIMIT 256
28#endif
29
reed@google.combaed71f2013-09-26 19:28:27 +000030///////////////////////////////////////////////////////////////////////////////
31
Herbert Derby671e7ee2018-04-17 08:04:47 -040032class SkGlyphCache;
33
Herb Derby840c66c2018-04-16 16:42:08 -040034class SkStrikeCache {
reed@google.combaed71f2013-09-26 19:28:27 +000035public:
Herbert Derby671e7ee2018-04-17 08:04:47 -040036 SkStrikeCache() = default;
Herb Derby840c66c2018-04-16 16:42:08 -040037 ~SkStrikeCache();
Herb Derbydfeb2aa2018-02-28 18:47:27 -050038
39 static void AttachCache(SkGlyphCache* cache);
skia.committer@gmail.com65caeaf2013-09-27 07:01:29 +000040
Herbert Derby671e7ee2018-04-17 08:04:47 -040041 using ExclusiveStrikePtr = std::unique_ptr<
42 SkGlyphCache,
43 SkFunctionWrapper<void, SkGlyphCache, SkStrikeCache::AttachCache>>;
reed@google.combaed71f2013-09-26 19:28:27 +000044
Herbert Derby671e7ee2018-04-17 08:04:47 -040045 static ExclusiveStrikePtr FindStrikeExclusive(const SkDescriptor&);
reed@google.combaed71f2013-09-26 19:28:27 +000046
Herbert Derby671e7ee2018-04-17 08:04:47 -040047 static void PurgeAll();
48
49 static void Dump();
50
51 // Dump memory usage statistics of all the attaches caches in the process using the
52 // SkTraceMemoryDump interface.
53 static void DumpMemoryStatistics(SkTraceMemoryDump* dump);
54
55 // call when a glyphcache is available for caching (i.e. not in use)
56 void attachCache(SkGlyphCache *cache);
57 ExclusiveStrikePtr findStrikeExclusive(const SkDescriptor&);
58
59 void purgeAll(); // does not change budget
60
61 int getCacheCountLimit() const;
62 int setCacheCountLimit(int limit);
herb632d0042016-06-22 10:19:16 -070063 int getCacheCountUsed() const;
reed@google.combaed71f2013-09-26 19:28:27 +000064
Herbert Derby671e7ee2018-04-17 08:04:47 -040065 size_t getCacheSizeLimit() const;
66 size_t setCacheSizeLimit(size_t limit);
67 size_t getTotalMemoryUsed() const;
68
69 int getCachePointSizeLimit() const;
70 int setCachePointSizeLimit(int limit);
71
72
reed@google.combaed71f2013-09-26 19:28:27 +000073#ifdef SK_DEBUG
74 void validate() const;
75#else
76 void validate() const {}
77#endif
78
Herbert Derby671e7ee2018-04-17 08:04:47 -040079private:
80 // The following methods can only be called when mutex is already held.
81 SkGlyphCache* internalGetHead() const { return fHead; }
82 SkGlyphCache* internalGetTail() const;
herbcd7f0352015-09-15 15:15:40 -070083 void internalDetachCache(SkGlyphCache*);
84 void internalAttachCacheToHead(SkGlyphCache*);
85
reed@google.combaed71f2013-09-26 19:28:27 +000086 // Checkout budgets, modulated by the specified min-bytes-needed-to-purge,
87 // and attempt to purge caches to match.
88 // Returns number of bytes freed.
89 size_t internalPurge(size_t minBytesNeeded = 0);
Herbert Derby671e7ee2018-04-17 08:04:47 -040090
91 void forEachStrike(std::function<void(const SkGlyphCache&)> visitor) const;
92
93 mutable SkSpinlock fLock;
94 SkGlyphCache* fHead{nullptr};
95 size_t fTotalMemoryUsed{0};
96 size_t fCacheSizeLimit{SK_DEFAULT_FONT_CACHE_LIMIT};
97 int32_t fCacheCountLimit{SK_DEFAULT_FONT_CACHE_COUNT_LIMIT};
98 int32_t fCacheCount{0};
99 int32_t fPointSizeLimit{SK_DEFAULT_FONT_CACHE_POINT_SIZE_LIMIT};
reed@google.combaed71f2013-09-26 19:28:27 +0000100};
101
Herbert Derby671e7ee2018-04-17 08:04:47 -0400102using SkExclusiveStrikePtr = SkStrikeCache::ExclusiveStrikePtr;
103
Herb Derby840c66c2018-04-16 16:42:08 -0400104#endif // SkStrikeCache_DEFINED