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 | |
| 8 | #ifndef SkGlyphCache_Globals_DEFINED |
| 9 | #define SkGlyphCache_Globals_DEFINED |
| 10 | |
| 11 | #include "SkGlyphCache.h" |
mtklein | 1b24933 | 2015-07-07 12:21:21 -0700 | [diff] [blame] | 12 | #include "SkMutex.h" |
herb | 3547505 | 2015-07-09 11:37:35 -0700 | [diff] [blame] | 13 | #include "SkSpinlock.h" |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 14 | #include "SkTLS.h" |
| 15 | |
| 16 | #ifndef SK_DEFAULT_FONT_CACHE_COUNT_LIMIT |
reed@google.com | 12eb781 | 2013-10-09 12:45:51 +0000 | [diff] [blame] | 17 | #define SK_DEFAULT_FONT_CACHE_COUNT_LIMIT 2048 |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 18 | #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.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 26 | class SkGlyphCache_Globals { |
| 27 | public: |
herb | 3547505 | 2015-07-09 11:37:35 -0700 | [diff] [blame] | 28 | SkGlyphCache_Globals() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 29 | fHead = nullptr; |
herb | cd7f035 | 2015-09-15 15:15:40 -0700 | [diff] [blame] | 30 | fTotalMemoryUsed = 0; |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 31 | fCacheSizeLimit = SK_DEFAULT_FONT_CACHE_LIMIT; |
| 32 | fCacheCount = 0; |
| 33 | fCacheCountLimit = SK_DEFAULT_FONT_CACHE_COUNT_LIMIT; |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 34 | } |
skia.committer@gmail.com | 65caeaf | 2013-09-27 07:01:29 +0000 | [diff] [blame] | 35 | |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 36 | ~SkGlyphCache_Globals() { |
| 37 | SkGlyphCache* cache = fHead; |
| 38 | while (cache) { |
| 39 | SkGlyphCache* next = cache->fNext; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 40 | delete cache; |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 41 | cache = next; |
| 42 | } |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 43 | } |
skia.committer@gmail.com | 65caeaf | 2013-09-27 07:01:29 +0000 | [diff] [blame] | 44 | |
herb | 632d004 | 2016-06-22 10:19:16 -0700 | [diff] [blame] | 45 | mutable SkSpinlock fLock; |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 46 | |
| 47 | SkGlyphCache* internalGetHead() const { return fHead; } |
| 48 | SkGlyphCache* internalGetTail() const; |
| 49 | |
herb | 632d004 | 2016-06-22 10:19:16 -0700 | [diff] [blame] | 50 | size_t getTotalMemoryUsed() const; |
| 51 | int getCacheCountUsed() const; |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 52 | |
| 53 | #ifdef SK_DEBUG |
| 54 | void validate() const; |
| 55 | #else |
| 56 | void validate() const {} |
| 57 | #endif |
| 58 | |
herb | 632d004 | 2016-06-22 10:19:16 -0700 | [diff] [blame] | 59 | int getCacheCountLimit() const; |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 60 | int setCacheCountLimit(int limit); |
| 61 | |
herb | 632d004 | 2016-06-22 10:19:16 -0700 | [diff] [blame] | 62 | size_t getCacheSizeLimit() const; |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 63 | size_t setCacheSizeLimit(size_t limit); |
| 64 | |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 65 | void purgeAll(); // does not change budget |
| 66 | |
| 67 | // call when a glyphcache is available for caching (i.e. not in use) |
herb | cd7f035 | 2015-09-15 15:15:40 -0700 | [diff] [blame] | 68 | void attachCacheToHead(SkGlyphCache*); |
jyasskin | 951d854 | 2015-09-10 18:11:29 -0700 | [diff] [blame] | 69 | |
herb | 014ffdb | 2015-09-15 07:03:03 -0700 | [diff] [blame] | 70 | // can only be called when the mutex is already held |
herb | cd7f035 | 2015-09-15 15:15:40 -0700 | [diff] [blame] | 71 | void internalDetachCache(SkGlyphCache*); |
| 72 | void internalAttachCacheToHead(SkGlyphCache*); |
| 73 | |
| 74 | private: |
| 75 | SkGlyphCache* fHead; |
| 76 | size_t fTotalMemoryUsed; |
| 77 | size_t fCacheSizeLimit; |
| 78 | int32_t fCacheCountLimit; |
| 79 | int32_t fCacheCount; |
skia.committer@gmail.com | 65caeaf | 2013-09-27 07:01:29 +0000 | [diff] [blame] | 80 | |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 81 | // 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.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | #endif |