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