1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/core/SkGlyphCache_Globals.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,122 @@ 1.4 +/* 1.5 + * Copyright 2010 Google Inc. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 + 1.11 +#ifndef SkGlyphCache_Globals_DEFINED 1.12 +#define SkGlyphCache_Globals_DEFINED 1.13 + 1.14 +#include "SkGlyphCache.h" 1.15 +#include "SkTLS.h" 1.16 + 1.17 +#ifndef SK_DEFAULT_FONT_CACHE_COUNT_LIMIT 1.18 + #define SK_DEFAULT_FONT_CACHE_COUNT_LIMIT 2048 1.19 +#endif 1.20 + 1.21 +#ifndef SK_DEFAULT_FONT_CACHE_LIMIT 1.22 + #define SK_DEFAULT_FONT_CACHE_LIMIT (2 * 1024 * 1024) 1.23 +#endif 1.24 + 1.25 +/////////////////////////////////////////////////////////////////////////////// 1.26 + 1.27 +class SkMutex; 1.28 + 1.29 +class SkGlyphCache_Globals { 1.30 +public: 1.31 + enum UseMutex { 1.32 + kNo_UseMutex, // thread-local cache 1.33 + kYes_UseMutex // shared cache 1.34 + }; 1.35 + 1.36 + SkGlyphCache_Globals(UseMutex um) { 1.37 + fHead = NULL; 1.38 + fTotalMemoryUsed = 0; 1.39 + fCacheSizeLimit = SK_DEFAULT_FONT_CACHE_LIMIT; 1.40 + fCacheCount = 0; 1.41 + fCacheCountLimit = SK_DEFAULT_FONT_CACHE_COUNT_LIMIT; 1.42 + 1.43 + fMutex = (kYes_UseMutex == um) ? SkNEW(SkMutex) : NULL; 1.44 + } 1.45 + 1.46 + ~SkGlyphCache_Globals() { 1.47 + SkGlyphCache* cache = fHead; 1.48 + while (cache) { 1.49 + SkGlyphCache* next = cache->fNext; 1.50 + SkDELETE(cache); 1.51 + cache = next; 1.52 + } 1.53 + 1.54 + SkDELETE(fMutex); 1.55 + } 1.56 + 1.57 + SkMutex* fMutex; 1.58 + 1.59 + SkGlyphCache* internalGetHead() const { return fHead; } 1.60 + SkGlyphCache* internalGetTail() const; 1.61 + 1.62 + size_t getTotalMemoryUsed() const { return fTotalMemoryUsed; } 1.63 + int getCacheCountUsed() const { return fCacheCount; } 1.64 + 1.65 +#ifdef SK_DEBUG 1.66 + void validate() const; 1.67 +#else 1.68 + void validate() const {} 1.69 +#endif 1.70 + 1.71 + int getCacheCountLimit() const { return fCacheCountLimit; } 1.72 + int setCacheCountLimit(int limit); 1.73 + 1.74 + size_t getCacheSizeLimit() const { return fCacheSizeLimit; } 1.75 + size_t setCacheSizeLimit(size_t limit); 1.76 + 1.77 + // returns true if this cache is over-budget either due to size limit 1.78 + // or count limit. 1.79 + bool isOverBudget() const { 1.80 + return fCacheCount > fCacheCountLimit || 1.81 + fTotalMemoryUsed > fCacheSizeLimit; 1.82 + } 1.83 + 1.84 + void purgeAll(); // does not change budget 1.85 + 1.86 + // call when a glyphcache is available for caching (i.e. not in use) 1.87 + void attachCacheToHead(SkGlyphCache*); 1.88 + 1.89 + // can only be called when the mutex is already held 1.90 + void internalDetachCache(SkGlyphCache*); 1.91 + void internalAttachCacheToHead(SkGlyphCache*); 1.92 + 1.93 + // can return NULL 1.94 + static SkGlyphCache_Globals* FindTLS() { 1.95 + return (SkGlyphCache_Globals*)SkTLS::Find(CreateTLS); 1.96 + } 1.97 + 1.98 + static SkGlyphCache_Globals& GetTLS() { 1.99 + return *(SkGlyphCache_Globals*)SkTLS::Get(CreateTLS, DeleteTLS); 1.100 + } 1.101 + 1.102 + static void DeleteTLS() { SkTLS::Delete(CreateTLS); } 1.103 + 1.104 +private: 1.105 + SkGlyphCache* fHead; 1.106 + size_t fTotalMemoryUsed; 1.107 + size_t fCacheSizeLimit; 1.108 + int32_t fCacheCountLimit; 1.109 + int32_t fCacheCount; 1.110 + 1.111 + // Checkout budgets, modulated by the specified min-bytes-needed-to-purge, 1.112 + // and attempt to purge caches to match. 1.113 + // Returns number of bytes freed. 1.114 + size_t internalPurge(size_t minBytesNeeded = 0); 1.115 + 1.116 + static void* CreateTLS() { 1.117 + return SkNEW_ARGS(SkGlyphCache_Globals, (kNo_UseMutex)); 1.118 + } 1.119 + 1.120 + static void DeleteTLS(void* ptr) { 1.121 + SkDELETE((SkGlyphCache_Globals*)ptr); 1.122 + } 1.123 +}; 1.124 + 1.125 +#endif