reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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. |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 10 | #ifndef SkTypefaceCache_DEFINED |
| 11 | #define SkTypefaceCache_DEFINED |
| 12 | |
bungeman | 82a455f | 2016-04-14 08:04:45 -0700 | [diff] [blame] | 13 | #include "SkRefCnt.h" |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 14 | #include "SkTypeface.h" |
bungeman | 82a455f | 2016-04-14 08:04:45 -0700 | [diff] [blame] | 15 | #include "SkTArray.h" |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 16 | |
| 17 | class SkTypefaceCache { |
| 18 | public: |
reed@google.com | 03b8781 | 2013-08-13 14:59:17 +0000 | [diff] [blame] | 19 | SkTypefaceCache(); |
reed@google.com | 03b8781 | 2013-08-13 14:59:17 +0000 | [diff] [blame] | 20 | |
bungeman@google.com | ee51d1a | 2012-02-16 12:40:48 +0000 | [diff] [blame] | 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 |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 24 | * not additionally ref()ed. The typeface may be in the disposed state. |
bungeman@google.com | ee51d1a | 2012-02-16 12:40:48 +0000 | [diff] [blame] | 25 | */ |
bungeman | 82a455f | 2016-04-14 08:04:45 -0700 | [diff] [blame] | 26 | typedef bool(*FindProc)(SkTypeface*, void* context); |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 27 | |
| 28 | /** |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 29 | * Add a typeface to the cache. This ref()s the typeface, so that the |
bungeman@google.com | ee51d1a | 2012-02-16 12:40:48 +0000 | [diff] [blame] | 30 | * cache is also an owner. Later, if we need to purge the cache, typefaces |
| 31 | * whose refcnt is 1 (meaning only the cache is an owner) will be |
| 32 | * unref()ed. |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 33 | */ |
bungeman | 82a455f | 2016-04-14 08:04:45 -0700 | [diff] [blame] | 34 | void add(SkTypeface*); |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 35 | |
| 36 | /** |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 37 | * Iterate through the cache, calling proc(typeface, ctx) with each |
bungeman@google.com | ee51d1a | 2012-02-16 12:40:48 +0000 | [diff] [blame] | 38 | * typeface. If proc returns true, then we return that typeface (this |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 39 | * ref()s the typeface). If it never returns true, we return nullptr. |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 40 | */ |
reed@google.com | 03b8781 | 2013-08-13 14:59:17 +0000 | [diff] [blame] | 41 | SkTypeface* findByProcAndRef(FindProc proc, void* ctx) const; |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 42 | |
| 43 | /** |
bungeman@google.com | ee51d1a | 2012-02-16 12:40:48 +0000 | [diff] [blame] | 44 | * This will unref all of the typefaces in the cache for which the cache |
| 45 | * is the only owner. Normally this is handled automatically as needed. |
| 46 | * This function is exposed for clients that explicitly want to purge the |
| 47 | * cache (e.g. to look for leaks). |
reed@google.com | d40da64 | 2011-10-20 13:18:37 +0000 | [diff] [blame] | 48 | */ |
reed@google.com | 03b8781 | 2013-08-13 14:59:17 +0000 | [diff] [blame] | 49 | void purgeAll(); |
| 50 | |
| 51 | /** |
| 52 | * Helper: returns a unique fontID to pass to the constructor of |
| 53 | * your subclass of SkTypeface |
| 54 | */ |
| 55 | static SkFontID NewFontID(); |
| 56 | |
| 57 | // These are static wrappers around a global instance of a cache. |
| 58 | |
bungeman | 82a455f | 2016-04-14 08:04:45 -0700 | [diff] [blame] | 59 | static void Add(SkTypeface*); |
reed@google.com | 03b8781 | 2013-08-13 14:59:17 +0000 | [diff] [blame] | 60 | static SkTypeface* FindByProcAndRef(FindProc proc, void* ctx); |
reed@google.com | d40da64 | 2011-10-20 13:18:37 +0000 | [diff] [blame] | 61 | static void PurgeAll(); |
| 62 | |
| 63 | /** |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 64 | * Debugging only: dumps the status of the typefaces in the cache |
| 65 | */ |
| 66 | static void Dump(); |
| 67 | |
| 68 | private: |
| 69 | static SkTypefaceCache& Get(); |
| 70 | |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 71 | void purge(int count); |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 72 | |
bungeman | 82a455f | 2016-04-14 08:04:45 -0700 | [diff] [blame] | 73 | SkTArray<sk_sp<SkTypeface>> fTypefaces; |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | #endif |