reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 1 | /* |
| 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 Derby | 840c66c | 2018-04-16 16:42:08 -0400 | [diff] [blame] | 8 | #ifndef SkStrikeCache_DEFINED |
| 9 | #define SkStrikeCache_DEFINED |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 10 | |
Herbert Derby | 671e7ee | 2018-04-17 08:04:47 -0400 | [diff] [blame^] | 11 | #include "SkDescriptor.h" |
herb | 3547505 | 2015-07-09 11:37:35 -0700 | [diff] [blame] | 12 | #include "SkSpinlock.h" |
Herbert Derby | 671e7ee | 2018-04-17 08:04:47 -0400 | [diff] [blame^] | 13 | #include "SkTemplates.h" |
Herb Derby | dfeb2aa | 2018-02-28 18:47:27 -0500 | [diff] [blame] | 14 | |
| 15 | class SkGlyphCache; |
Herbert Derby | 671e7ee | 2018-04-17 08:04:47 -0400 | [diff] [blame^] | 16 | class SkTraceMemoryDump; |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 17 | |
| 18 | #ifndef SK_DEFAULT_FONT_CACHE_COUNT_LIMIT |
reed@google.com | 12eb781 | 2013-10-09 12:45:51 +0000 | [diff] [blame] | 19 | #define SK_DEFAULT_FONT_CACHE_COUNT_LIMIT 2048 |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 20 | #endif |
| 21 | |
| 22 | #ifndef SK_DEFAULT_FONT_CACHE_LIMIT |
| 23 | #define SK_DEFAULT_FONT_CACHE_LIMIT (2 * 1024 * 1024) |
| 24 | #endif |
| 25 | |
Mike Reed | d5bee5d | 2017-06-01 14:45:44 -0400 | [diff] [blame] | 26 | #ifndef SK_DEFAULT_FONT_CACHE_POINT_SIZE_LIMIT |
| 27 | #define SK_DEFAULT_FONT_CACHE_POINT_SIZE_LIMIT 256 |
| 28 | #endif |
| 29 | |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 30 | /////////////////////////////////////////////////////////////////////////////// |
| 31 | |
Herbert Derby | 671e7ee | 2018-04-17 08:04:47 -0400 | [diff] [blame^] | 32 | class SkGlyphCache; |
| 33 | |
Herb Derby | 840c66c | 2018-04-16 16:42:08 -0400 | [diff] [blame] | 34 | class SkStrikeCache { |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 35 | public: |
Herbert Derby | 671e7ee | 2018-04-17 08:04:47 -0400 | [diff] [blame^] | 36 | SkStrikeCache() = default; |
Herb Derby | 840c66c | 2018-04-16 16:42:08 -0400 | [diff] [blame] | 37 | ~SkStrikeCache(); |
Herb Derby | dfeb2aa | 2018-02-28 18:47:27 -0500 | [diff] [blame] | 38 | |
| 39 | static void AttachCache(SkGlyphCache* cache); |
skia.committer@gmail.com | 65caeaf | 2013-09-27 07:01:29 +0000 | [diff] [blame] | 40 | |
Herbert Derby | 671e7ee | 2018-04-17 08:04:47 -0400 | [diff] [blame^] | 41 | using ExclusiveStrikePtr = std::unique_ptr< |
| 42 | SkGlyphCache, |
| 43 | SkFunctionWrapper<void, SkGlyphCache, SkStrikeCache::AttachCache>>; |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 44 | |
Herbert Derby | 671e7ee | 2018-04-17 08:04:47 -0400 | [diff] [blame^] | 45 | static ExclusiveStrikePtr FindStrikeExclusive(const SkDescriptor&); |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 46 | |
Herbert Derby | 671e7ee | 2018-04-17 08:04:47 -0400 | [diff] [blame^] | 47 | 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); |
herb | 632d004 | 2016-06-22 10:19:16 -0700 | [diff] [blame] | 63 | int getCacheCountUsed() const; |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 64 | |
Herbert Derby | 671e7ee | 2018-04-17 08:04:47 -0400 | [diff] [blame^] | 65 | 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.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 73 | #ifdef SK_DEBUG |
| 74 | void validate() const; |
| 75 | #else |
| 76 | void validate() const {} |
| 77 | #endif |
| 78 | |
Herbert Derby | 671e7ee | 2018-04-17 08:04:47 -0400 | [diff] [blame^] | 79 | private: |
| 80 | // The following methods can only be called when mutex is already held. |
| 81 | SkGlyphCache* internalGetHead() const { return fHead; } |
| 82 | SkGlyphCache* internalGetTail() const; |
herb | cd7f035 | 2015-09-15 15:15:40 -0700 | [diff] [blame] | 83 | void internalDetachCache(SkGlyphCache*); |
| 84 | void internalAttachCacheToHead(SkGlyphCache*); |
| 85 | |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 86 | // 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 Derby | 671e7ee | 2018-04-17 08:04:47 -0400 | [diff] [blame^] | 90 | |
| 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.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 100 | }; |
| 101 | |
Herbert Derby | 671e7ee | 2018-04-17 08:04:47 -0400 | [diff] [blame^] | 102 | using SkExclusiveStrikePtr = SkStrikeCache::ExclusiveStrikePtr; |
| 103 | |
Herb Derby | 840c66c | 2018-04-16 16:42:08 -0400 | [diff] [blame] | 104 | #endif // SkStrikeCache_DEFINED |