Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkTypefaceCache.cpp
Go to the documentation of this file.
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
9
15
16#include <atomic>
17#include <cstdint>
18#include <utility>
19
21
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
34 for (const sk_sp<SkTypeface>& typeface : fTypefaces) {
35 if (proc(typeface.get(), ctx)) {
36 return typeface;
37 }
38 }
39 return nullptr;
40}
41
42void 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
59 this->purge(fTypefaces.size());
60}
61
62///////////////////////////////////////////////////////////////////////////////
63
64SkTypefaceCache& SkTypefaceCache::Get() {
65 static SkTypefaceCache gCache;
66 return gCache;
67}
68
70 static std::atomic<int32_t> nextID{1};
71 return nextID.fetch_add(1, std::memory_order_relaxed);
72}
73
74static SkMutex& typeface_cache_mutex() {
75 static SkMutex& mutex = *(new SkMutex);
76 return mutex;
77}
78
81 Get().add(std::move(face));
82}
83
86 return Get().findByProcAndRef(proc, ctx);
87}
88
93
94///////////////////////////////////////////////////////////////////////////////
95
96#ifdef SK_DEBUG
97static 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
109#ifdef SK_DEBUG
110 (void)Get().findByProcAndRef(DumpProc, nullptr);
111#endif
112}
int count
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
static SkMutex & typeface_cache_mutex()
uint32_t SkTypefaceID
Definition SkTypeface.h:38
static int GetTypefaceCacheCountLimit()
const char * c_str() const
Definition SkString.h:133
static sk_sp< SkTypeface > FindByProcAndRef(FindProc proc, void *ctx)
sk_sp< SkTypeface > findByProcAndRef(FindProc proc, void *ctx) const
void add(sk_sp< SkTypeface >)
static void Add(sk_sp< SkTypeface >)
static void PurgeAll()
static SkTypefaceID NewTypefaceID()
SkTypefaceID uniqueID() const
Definition SkTypeface.h:101
void getFamilyName(SkString *name) const
SkFontStyle fontStyle() const
Definition SkTypeface.h:55
void removeShuffle(int n)
Definition SkTArray.h:183
int size() const
Definition SkTArray.h:416
T & emplace_back(Args &&... args)
Definition SkTArray.h:243
struct MyStruct s