1 /* 2 * Copyright 2011 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 9 10 #ifndef SkTypefaceCache_DEFINED 11 #define SkTypefaceCache_DEFINED 12 13 #include "include/core/SkRefCnt.h" 14 #include "include/core/SkTypeface.h" 15 #include "include/private/base/SkTArray.h" 16 17 class SkTypefaceCache { 18 public: 19 SkTypefaceCache(); 20 21 /** 22 * Callback for FindByProc. Returns true if the given typeface is a match 23 * for the given context. The passed typeface is owned by the cache and is 24 * not additionally ref()ed. The typeface may be in the disposed state. 25 */ 26 typedef bool(*FindProc)(SkTypeface*, void* context); 27 28 /** 29 * Add a typeface to the cache. Later, if we need to purge the cache, 30 * typefaces uniquely owned by the cache will be unref()ed. 31 */ 32 void add(sk_sp<SkTypeface>); 33 34 /** 35 * Iterate through the cache, calling proc(typeface, ctx) for each typeface. 36 * If proc returns true, then return that typeface. 37 * If it never returns true, return nullptr. 38 */ 39 sk_sp<SkTypeface> findByProcAndRef(FindProc proc, void* ctx) const; 40 41 /** 42 * This will unref all of the typefaces in the cache for which the cache 43 * is the only owner. Normally this is handled automatically as needed. 44 * This function is exposed for clients that explicitly want to purge the 45 * cache (e.g. to look for leaks). 46 */ 47 void purgeAll(); 48 49 /** 50 * Helper: returns a unique typefaceID to pass to the constructor of 51 * your subclass of SkTypeface 52 */ 53 static SkTypefaceID NewTypefaceID(); 54 55 // These are static wrappers around a global instance of a cache. 56 57 static void Add(sk_sp<SkTypeface>); 58 static sk_sp<SkTypeface> FindByProcAndRef(FindProc proc, void* ctx); 59 static void PurgeAll(); 60 61 /** 62 * Debugging only: dumps the status of the typefaces in the cache 63 */ 64 static void Dump(); 65 66 private: 67 static SkTypefaceCache& Get(); 68 69 void purge(int count); 70 71 skia_private::TArray<sk_sp<SkTypeface>> fTypefaces; 72 }; 73 74 #endif 75