| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2010 Google Inc. | 2 * Copyright 2010 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef SkGlyphCache_Globals_DEFINED | 8 #ifndef SkGlyphCache_Globals_DEFINED |
| 9 #define SkGlyphCache_Globals_DEFINED | 9 #define SkGlyphCache_Globals_DEFINED |
| 10 | 10 |
| 11 #include "SkGlyphCache.h" | 11 #include "SkGlyphCache.h" |
| 12 #include "SkMutex.h" | 12 #include "SkMutex.h" |
| 13 #include "SkSpinlock.h" | 13 #include "SkSpinlock.h" |
| 14 #include "SkTLS.h" | 14 #include "SkTLS.h" |
| 15 | 15 |
| 16 #ifndef SK_DEFAULT_FONT_CACHE_COUNT_LIMIT | 16 #ifndef SK_DEFAULT_FONT_CACHE_COUNT_LIMIT |
| 17 #define SK_DEFAULT_FONT_CACHE_COUNT_LIMIT 2048 | 17 #define SK_DEFAULT_FONT_CACHE_COUNT_LIMIT 2048 |
| 18 #endif | 18 #endif |
| 19 | 19 |
| 20 #ifndef SK_DEFAULT_FONT_CACHE_LIMIT | 20 #ifndef SK_DEFAULT_FONT_CACHE_LIMIT |
| 21 #define SK_DEFAULT_FONT_CACHE_LIMIT (2 * 1024 * 1024) | 21 #define SK_DEFAULT_FONT_CACHE_LIMIT (2 * 1024 * 1024) |
| 22 #endif | 22 #endif |
| 23 | 23 |
| 24 /////////////////////////////////////////////////////////////////////////////// | 24 /////////////////////////////////////////////////////////////////////////////// |
| 25 | 25 |
| 26 class SkGlyphCache_Globals { | 26 class SkGlyphCache_Globals { |
| 27 public: | 27 public: |
| 28 SkGlyphCache_Globals() { | 28 SkGlyphCache_Globals() { |
| 29 | |
| 30 fHead = nullptr; | 29 fHead = nullptr; |
| 31 fTotalMemoryUsed.store(0); | 30 fTotalMemoryUsed = 0; |
| 32 fCacheSizeLimit = SK_DEFAULT_FONT_CACHE_LIMIT; | 31 fCacheSizeLimit = SK_DEFAULT_FONT_CACHE_LIMIT; |
| 33 fCacheCount = 0; | 32 fCacheCount = 0; |
| 34 fCacheCountLimit = SK_DEFAULT_FONT_CACHE_COUNT_LIMIT; | 33 fCacheCountLimit = SK_DEFAULT_FONT_CACHE_COUNT_LIMIT; |
| 35 } | 34 } |
| 36 | 35 |
| 37 ~SkGlyphCache_Globals() { | 36 ~SkGlyphCache_Globals() { |
| 38 SkGlyphCache* cache = fHead; | 37 SkGlyphCache* cache = fHead; |
| 39 while (cache) { | 38 while (cache) { |
| 40 SkGlyphCache* next = cache->fNext; | 39 SkGlyphCache* next = cache->fNext; |
| 41 delete cache; | 40 delete cache; |
| 42 cache = next; | 41 cache = next; |
| 43 } | 42 } |
| 44 } | 43 } |
| 45 | 44 |
| 46 SkSpinlock fLock; | 45 SkSpinlock fLock; |
| 47 | 46 |
| 48 SkGlyphCache* internalGetHead() const { return fHead; } | 47 SkGlyphCache* internalGetHead() const { return fHead; } |
| 49 SkGlyphCache* internalGetTail() const; | 48 SkGlyphCache* internalGetTail() const; |
| 50 | 49 |
| 51 size_t getTotalMemoryUsed() const { return fTotalMemoryUsed.load(); } | 50 size_t getTotalMemoryUsed() const { return fTotalMemoryUsed; } |
| 52 void increaseTotalMemoryUsed(size_t increase) { fTotalMemoryUsed.fetch_add(i
ncrease);} | |
| 53 int getCacheCountUsed() const { return fCacheCount; } | 51 int getCacheCountUsed() const { return fCacheCount; } |
| 54 | 52 |
| 55 #ifdef SK_DEBUG | 53 #ifdef SK_DEBUG |
| 56 void validate() const; | 54 void validate() const; |
| 57 #else | 55 #else |
| 58 void validate() const {} | 56 void validate() const {} |
| 59 #endif | 57 #endif |
| 60 | 58 |
| 61 int getCacheCountLimit() const { return fCacheCountLimit; } | 59 int getCacheCountLimit() const { return fCacheCountLimit; } |
| 62 int setCacheCountLimit(int limit); | 60 int setCacheCountLimit(int limit); |
| 63 | 61 |
| 64 size_t getCacheSizeLimit() const { return fCacheSizeLimit; } | 62 size_t getCacheSizeLimit() const { return fCacheSizeLimit; } |
| 65 size_t setCacheSizeLimit(size_t limit); | 63 size_t setCacheSizeLimit(size_t limit); |
| 66 | 64 |
| 67 // returns true if this cache is over-budget either due to size limit | 65 // returns true if this cache is over-budget either due to size limit |
| 68 // or count limit. | 66 // or count limit. |
| 69 bool isOverBudget() const { | 67 bool isOverBudget() const { |
| 70 return fCacheCount > fCacheCountLimit || | 68 return fCacheCount > fCacheCountLimit || |
| 71 fTotalMemoryUsed.load() > fCacheSizeLimit; | 69 fTotalMemoryUsed > fCacheSizeLimit; |
| 72 } | 70 } |
| 73 | 71 |
| 74 void purgeAll(); // does not change budget | 72 void purgeAll(); // does not change budget |
| 75 | 73 |
| 76 // call when a glyphcache is available for caching (i.e. not in use) | 74 // call when a glyphcache is available for caching (i.e. not in use) |
| 77 void attachCacheToHead(SkGlyphCache*); | 75 void attachCacheToHead(SkGlyphCache*); |
| 78 | 76 |
| 79 // can only be called when the mutex is already held | 77 // can only be called when the mutex is already held |
| 80 void internalMoveToHead(SkGlyphCache *); | 78 void internalDetachCache(SkGlyphCache*); |
| 79 void internalAttachCacheToHead(SkGlyphCache*); |
| 80 |
| 81 private: |
| 82 SkGlyphCache* fHead; |
| 83 size_t fTotalMemoryUsed; |
| 84 size_t fCacheSizeLimit; |
| 85 int32_t fCacheCountLimit; |
| 86 int32_t fCacheCount; |
| 81 | 87 |
| 82 // Checkout budgets, modulated by the specified min-bytes-needed-to-purge, | 88 // Checkout budgets, modulated by the specified min-bytes-needed-to-purge, |
| 83 // and attempt to purge caches to match. | 89 // and attempt to purge caches to match. |
| 84 // Returns number of bytes freed. | 90 // Returns number of bytes freed. |
| 85 void internalDetachCache(SkGlyphCache* cache); | |
| 86 size_t internalPurge(size_t minBytesNeeded = 0); | 91 size_t internalPurge(size_t minBytesNeeded = 0); |
| 87 | |
| 88 private: | |
| 89 SkGlyphCache* fHead; | |
| 90 SkAtomic<size_t> fTotalMemoryUsed; | |
| 91 size_t fCacheSizeLimit; | |
| 92 int32_t fCacheCountLimit; | |
| 93 int32_t fCacheCount; | |
| 94 | |
| 95 }; | 92 }; |
| 96 | 93 |
| 97 #endif | 94 #endif |
| OLD | NEW |