blob: bf90dae3e2086407cabfc4022bb1ba4f5ba5b190 [file] [log] [blame]
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.com2f3dc9d2011-05-02 17:33:45 +00006 */
7
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@google.com2f3dc9d2011-05-02 17:33:45 +000010#ifndef SkTypefaceCache_DEFINED
11#define SkTypefaceCache_DEFINED
12
bungeman82a455f2016-04-14 08:04:45 -070013#include "SkRefCnt.h"
reed@google.com2f3dc9d2011-05-02 17:33:45 +000014#include "SkTypeface.h"
bungeman82a455f2016-04-14 08:04:45 -070015#include "SkTArray.h"
reed@google.com2f3dc9d2011-05-02 17:33:45 +000016
17class SkTypefaceCache {
18public:
reed@google.com03b87812013-08-13 14:59:17 +000019 SkTypefaceCache();
reed@google.com03b87812013-08-13 14:59:17 +000020
bungeman@google.comee51d1a2012-02-16 12:40:48 +000021 /**
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.coma02bc152012-05-16 18:21:56 +000024 * not additionally ref()ed. The typeface may be in the disposed state.
bungeman@google.comee51d1a2012-02-16 12:40:48 +000025 */
bungeman82a455f2016-04-14 08:04:45 -070026 typedef bool(*FindProc)(SkTypeface*, void* context);
reed@google.com2f3dc9d2011-05-02 17:33:45 +000027
28 /**
reed@google.com2f3dc9d2011-05-02 17:33:45 +000029 * Add a typeface to the cache. This ref()s the typeface, so that the
bungeman@google.comee51d1a2012-02-16 12:40:48 +000030 * 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.com2f3dc9d2011-05-02 17:33:45 +000033 */
bungeman82a455f2016-04-14 08:04:45 -070034 void add(SkTypeface*);
reed@google.com2f3dc9d2011-05-02 17:33:45 +000035
36 /**
reed@google.com2f3dc9d2011-05-02 17:33:45 +000037 * Iterate through the cache, calling proc(typeface, ctx) with each
bungeman@google.comee51d1a2012-02-16 12:40:48 +000038 * typeface. If proc returns true, then we return that typeface (this
halcanary96fcdcc2015-08-27 07:41:13 -070039 * ref()s the typeface). If it never returns true, we return nullptr.
reed@google.com2f3dc9d2011-05-02 17:33:45 +000040 */
reed@google.com03b87812013-08-13 14:59:17 +000041 SkTypeface* findByProcAndRef(FindProc proc, void* ctx) const;
reed@google.com2f3dc9d2011-05-02 17:33:45 +000042
43 /**
bungeman@google.comee51d1a2012-02-16 12:40:48 +000044 * 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.comd40da642011-10-20 13:18:37 +000048 */
reed@google.com03b87812013-08-13 14:59:17 +000049 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
bungeman82a455f2016-04-14 08:04:45 -070059 static void Add(SkTypeface*);
reed@google.com03b87812013-08-13 14:59:17 +000060 static SkTypeface* FindByProcAndRef(FindProc proc, void* ctx);
reed@google.comd40da642011-10-20 13:18:37 +000061 static void PurgeAll();
62
63 /**
reed@google.com2f3dc9d2011-05-02 17:33:45 +000064 * Debugging only: dumps the status of the typefaces in the cache
65 */
66 static void Dump();
67
68private:
69 static SkTypefaceCache& Get();
70
reed@google.com2f3dc9d2011-05-02 17:33:45 +000071 void purge(int count);
reed@google.com2f3dc9d2011-05-02 17:33:45 +000072
bungeman82a455f2016-04-14 08:04:45 -070073 SkTArray<sk_sp<SkTypeface>> fTypefaces;
reed@google.com2f3dc9d2011-05-02 17:33:45 +000074};
75
76#endif