Debian Codegraph @

src/core/SkTypefaceCache.cpp

raw
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 #include "src/core/SkTypefaceCache.h"
9
10 #include "include/core/SkFontStyle.h"
11 #include "include/core/SkGraphics.h"
12 #include "include/core/SkString.h"
13 #include "include/private/base/SkAssert.h"
14 #include "include/private/base/SkDebug.h"
15 #include "include/private/base/SkMutex.h"
16
17 #include <atomic>
18 #include <cstdint>
19 #include <utility>
20
21 SkTypefaceCache::SkTypefaceCache() {}
22
23 void SkTypefaceCache::add(sk_sp<SkTypeface> face) {
24 SkASSERT_RELEASE(face);
25 const auto limit = SkGraphics::GetTypefaceCacheCountLimit();
26
27 if (fTypefaces.size() >= limit) {
28 this->purge(limit >> 2);
29 }
30 if (limit > 0) {
31 fTypefaces.emplace_back(std::move(face));
32 }
33 }
34
35 sk_sp<SkTypeface> SkTypefaceCache::findByProcAndRef(FindProc proc, void* ctx) const {
36 for (const sk_sp<SkTypeface>& typeface : fTypefaces) {
37 if (proc(typeface.get(), ctx)) {
38 return typeface;
39 }
40 }
41 return nullptr;
42 }
43
44 void SkTypefaceCache::purge(int numToPurge) {
45 int count = fTypefaces.size();
46 int i = 0;
47 while (i < count) {
48 if (fTypefaces[i]->unique()) {
49 fTypefaces.removeShuffle(i);
50 --count;
51 if (--numToPurge == 0) {
52 return;
53 }
54 } else {
55 ++i;
56 }
57 }
58 }
59
60 void SkTypefaceCache::purgeAll() {
61 this->purge(fTypefaces.size());
62 }
63
64 ///////////////////////////////////////////////////////////////////////////////
65
66 SkTypefaceCache& SkTypefaceCache::Get() {
67 static SkTypefaceCache gCache;
68 return gCache;
69 }
70
71 SkTypefaceID SkTypefaceCache::NewTypefaceID() {
72 static std::atomic<int32_t> nextID{1};
73 return nextID.fetch_add(1, std::memory_order_relaxed);
74 }
75
76 static SkMutex& typeface_cache_mutex() {
77 static SkMutex& mutex = *(new SkMutex);
78 return mutex;
79 }
80
81 void SkTypefaceCache::Add(sk_sp<SkTypeface> face) {
82 SkAutoMutexExclusive ama(typeface_cache_mutex());
83 Get().add(std::move(face));
84 }
85
86 sk_sp<SkTypeface> SkTypefaceCache::FindByProcAndRef(FindProc proc, void* ctx) {
87 SkAutoMutexExclusive ama(typeface_cache_mutex());
88 return Get().findByProcAndRef(proc, ctx);
89 }
90
91 void SkTypefaceCache::PurgeAll() {
92 SkAutoMutexExclusive ama(typeface_cache_mutex());
93 Get().purgeAll();
94 }
95
96 ///////////////////////////////////////////////////////////////////////////////
97
98 #ifdef SK_DEBUG
99 static bool DumpProc(SkTypeface* face, void* ctx) {
100 SkString n;
101 face->getFamilyName(&n);
102 SkFontStyle s = face->fontStyle();
103 SkTypefaceID id = face->uniqueID();
104 SkDebugf("SkTypefaceCache: face %p typefaceID %u weight %d width %d style %d name %s\n",
105 face, id, s.weight(), s.width(), s.slant(), n.c_str());
106 return false;
107 }
108 #endif
109
110 void SkTypefaceCache::Dump() {
111 #ifdef SK_DEBUG
112 (void)Get().findByProcAndRef(DumpProc, nullptr);
113 #endif
114 }