OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2008 Google Inc. | 2 * Copyright 2008 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkFontConfigInterface.h" | 8 #include "SkFontConfigInterface.h" |
9 #include "SkFontConfigTypeface.h" | 9 #include "SkFontConfigTypeface.h" |
10 #include "SkFontDescriptor.h" | 10 #include "SkFontDescriptor.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
49 } | 49 } |
50 | 50 |
51 // export this to SkFontMgr_fontconfig.cpp until this file just goes away. | 51 // export this to SkFontMgr_fontconfig.cpp until this file just goes away. |
52 SkFontConfigInterface* SkFontHost_fontconfig_ref_global(); | 52 SkFontConfigInterface* SkFontHost_fontconfig_ref_global(); |
53 SkFontConfigInterface* SkFontHost_fontconfig_ref_global() { | 53 SkFontConfigInterface* SkFontHost_fontconfig_ref_global() { |
54 return RefFCI(); | 54 return RefFCI(); |
55 } | 55 } |
56 | 56 |
57 /////////////////////////////////////////////////////////////////////////////// | 57 /////////////////////////////////////////////////////////////////////////////// |
58 | 58 |
59 struct NameStyle { | |
60 NameStyle(const char* name, const SkFontStyle& style) | |
61 : fFamilyName(name) // don't need to make a deep copy | |
62 , fStyle(style) {} | |
63 | |
64 const char* fFamilyName; | |
65 SkFontStyle fStyle; | |
66 }; | |
67 | |
68 static bool find_by_NameStyle(SkTypeface* cachedTypeface, | |
69 const SkFontStyle& cachedStyle, | |
70 void* ctx) | |
71 { | |
72 FontConfigTypeface* cachedFCTypeface = static_cast<FontConfigTypeface*>(cach edTypeface); | |
73 const NameStyle* nameStyle = static_cast<const NameStyle*>(ctx); | |
74 | |
75 return nameStyle->fStyle == cachedStyle && | |
76 cachedFCTypeface->isFamilyName(nameStyle->fFamilyName); | |
77 } | |
78 | |
79 static bool find_by_FontIdentity(SkTypeface* cachedTypeface, const SkFontStyle&, void* ctx) { | 59 static bool find_by_FontIdentity(SkTypeface* cachedTypeface, const SkFontStyle&, void* ctx) { |
80 typedef SkFontConfigInterface::FontIdentity FontIdentity; | 60 typedef SkFontConfigInterface::FontIdentity FontIdentity; |
81 FontConfigTypeface* cachedFCTypeface = static_cast<FontConfigTypeface*>(cach edTypeface); | 61 FontConfigTypeface* cachedFCTypeface = static_cast<FontConfigTypeface*>(cach edTypeface); |
82 FontIdentity* indentity = static_cast<FontIdentity*>(ctx); | 62 FontIdentity* identity = static_cast<FontIdentity*>(ctx); |
83 | 63 |
84 return cachedFCTypeface->getIdentity() == *indentity; | 64 return cachedFCTypeface->getIdentity() == *identity; |
85 } | 65 } |
86 | 66 |
87 SkTypeface* FontConfigTypeface::LegacyCreateTypeface(const char familyName[], | 67 SK_DECLARE_STATIC_MUTEX(gSkFontHostRequestCacheMutex); |
88 SkTypeface::Style style) | 68 class SkFontHostRequestCache { |
69 public: | |
70 void add(SkTypeface* face, const char requestedFamilyName[], SkFontStyle req uestedStyle) { | |
71 if (fCachedResults.count() < maxEntries) { | |
72 fCachedResults.emplace_back(face, Request(requestedFamilyName, reque stedStyle)); | |
mtklein
2016/02/10 16:14:10
I found myself wondering whether or not these type
bungeman-skia
2016/02/11 17:46:02
Hmmm... so the reason things are written the way t
| |
73 } else { | |
74 fCachedResults[fLRUIndex] = Result(face, Request(requestedFamilyName , requestedStyle)); | |
75 fLRUIndex++; | |
mtklein
2016/02/10 16:14:10
fLRUIndex seems like the wrong name here. There's
bungeman-skia
2016/02/11 17:46:02
Yeah, it's actually the fLRAIndex (least recently
| |
76 if (fLRUIndex == maxEntries) { | |
77 fLRUIndex = 0; | |
78 } | |
79 } | |
80 } | |
81 SkTypeface* findAndRef(const char requestedFamilyName[], SkFontStyle request edStyle) const { | |
82 for (const auto& cachedResult : fCachedResults) { | |
83 if (cachedResult.fRequest.fStyle == requestedStyle && | |
84 cachedResult.fRequest.fFamilyName.equals(requestedFamilyName)) | |
85 { | |
86 return SkSafeRef(cachedResult.fFace.get()); | |
87 } | |
88 } | |
89 return nullptr; | |
90 } | |
91 | |
92 static void Add(SkTypeface* face, const char requestedFamilyName[], SkFontSt yle requestedStyle){ | |
93 SkAutoMutexAcquire ama(gSkFontHostRequestCacheMutex); | |
94 Get().add(face, requestedFamilyName, requestedStyle); | |
95 } | |
96 | |
97 static SkTypeface* FindAndRef(const char requestedFamilyName[], SkFontStyle requestedStyle) { | |
98 SkAutoMutexAcquire ama(gSkFontHostRequestCacheMutex); | |
99 return Get().findAndRef(requestedFamilyName, requestedStyle); | |
100 } | |
101 | |
102 private: | |
103 static SkFontHostRequestCache& Get() { | |
mtklein
2016/02/10 16:14:10
add
gSKFontHostRequestCacheMutex.assertHeld()
bungeman-skia
2016/02/11 17:46:02
Done.
| |
104 static SkFontHostRequestCache gCache; | |
105 return gCache; | |
106 } | |
107 | |
108 // Set the maxEntries to a small value in debug to ensure testing of wrap ar ound. | |
mtklein
2016/02/10 16:14:10
// 64 for Release builds is... (arbitrary?) (pull
bungeman-skia
2016/02/11 17:46:02
A totally magic number.
| |
109 static const int maxEntries = 64 SkDEBUGCODE( - 62); | |
110 | |
111 struct Request { | |
112 Request(const char* name, const SkFontStyle& style) : fFamilyName(name), fStyle(style) {} | |
113 Request(Request&&) = default; | |
114 Request& operator=(Request&&) = default; | |
115 SkString fFamilyName; | |
116 SkFontStyle fStyle; | |
117 }; | |
118 struct Result { | |
119 Result(SkTypeface* typeface, Request&& request) | |
mtklein
2016/02/10 16:14:10
I'd slightly prefer passing (Request&&, SkTypeface
bungeman-skia
2016/02/11 17:46:02
Done.
| |
120 : fFace(SkSafeRef(typeface)) | |
121 , fRequest(std::move(request)) {} | |
122 Result(Result&&) = default; | |
123 Result& operator=(Result&&) = default; | |
124 SkAutoTUnref<SkTypeface> fFace; | |
125 Request fRequest; | |
126 }; | |
127 | |
128 SkTArray<Result> fCachedResults; | |
129 int fLRUIndex = 0; | |
130 }; | |
131 | |
132 SkTypeface* FontConfigTypeface::LegacyCreateTypeface(const char requestedFamilyN ame[], | |
133 SkTypeface::Style requested OldStyle) | |
89 { | 134 { |
90 SkAutoTUnref<SkFontConfigInterface> fci(RefFCI()); | 135 SkAutoTUnref<SkFontConfigInterface> fci(RefFCI()); |
91 if (nullptr == fci.get()) { | 136 if (nullptr == fci.get()) { |
92 return nullptr; | 137 return nullptr; |
93 } | 138 } |
94 | 139 |
95 // Check if requested NameStyle is in the NameStyle cache. | 140 // Check if requested NameStyle is in the NameStyle cache. |
mtklein
2016/02/10 16:14:10
Needs an update / deletion?
bungeman-skia
2016/02/11 17:46:02
Done.
| |
96 SkFontStyle requestedStyle(style); | 141 SkFontStyle requestedStyle(requestedOldStyle); |
97 NameStyle nameStyle(familyName, requestedStyle); | 142 SkTypeface* face = SkFontHostRequestCache::FindAndRef(requestedFamilyName, r equestedStyle); |
98 SkTypeface* face = SkTypefaceCache::FindByProcAndRef(find_by_NameStyle, &nam eStyle); | |
mtklein
2016/02/10 16:14:10
Is this new request-caching approach something we
bungeman-skia
2016/02/11 17:46:02
Yeah, SkFontHost_mac has its own find_by_NameStyle
| |
99 if (face) { | 143 if (face) { |
100 //SkDebugf("found cached face <%s> <%s> %p [%d]\n", | |
101 // familyName, ((FontConfigTypeface*)face)->getFamilyName(), | |
102 // face, face->getRefCnt()); | |
103 return face; | 144 return face; |
104 } | 145 } |
105 | 146 |
106 SkFontConfigInterface::FontIdentity indentity; | 147 SkFontConfigInterface::FontIdentity identity; |
107 SkString outFamilyName; | 148 SkString outFamilyName; |
108 SkTypeface::Style outStyle; | 149 SkTypeface::Style outOldStyle; |
109 if (!fci->matchFamilyName(familyName, style, &indentity, &outFamilyName, &ou tStyle)) { | 150 if (!fci->matchFamilyName(requestedFamilyName, requestedOldStyle, |
151 &identity, &outFamilyName, &outOldStyle)) | |
152 { | |
110 return nullptr; | 153 return nullptr; |
111 } | 154 } |
112 | 155 |
113 // Check if a typeface with this FontIdentity is already in the FontIdentity cache. | 156 // Check if a typeface with this FontIdentity is already in the FontIdentity cache. |
114 face = SkTypefaceCache::FindByProcAndRef(find_by_FontIdentity, &indentity); | 157 face = SkTypefaceCache::FindByProcAndRef(find_by_FontIdentity, &identity); |
115 if (!face) { | 158 if (!face) { |
116 face = FontConfigTypeface::Create(SkFontStyle(outStyle), indentity, outF amilyName); | 159 face = FontConfigTypeface::Create(SkFontStyle(outOldStyle), identity, ou tFamilyName); |
117 // Add this FontIdentity to the FontIdentity cache. | 160 // Add this FontIdentity to the FontIdentity cache. |
118 SkTypefaceCache::Add(face, requestedStyle); | 161 SkTypefaceCache::Add(face, SkFontStyle(outOldStyle)); |
119 } | 162 } |
120 // TODO: Ensure requested NameStyle and resolved NameStyle are both in the N ameStyle cache. | 163 // Add this NameStyle to the NameStyle cache. |
mtklein
2016/02/10 16:14:10
Update? Seems weird when there's no NameStyle obj
bungeman-skia
2016/02/11 17:46:02
Done.
| |
164 SkFontHostRequestCache::Add(face, requestedFamilyName, requestedStyle); | |
121 | 165 |
122 //SkDebugf("add face <%s> <%s> %p [%d]\n", | |
123 // familyName, outFamilyName.c_str(), | |
124 // face, face->getRefCnt()); | |
125 return face; | 166 return face; |
126 } | 167 } |
127 | 168 |
128 /////////////////////////////////////////////////////////////////////////////// | 169 /////////////////////////////////////////////////////////////////////////////// |
129 | 170 |
130 SkStreamAsset* FontConfigTypeface::onOpenStream(int* ttcIndex) const { | 171 SkStreamAsset* FontConfigTypeface::onOpenStream(int* ttcIndex) const { |
131 SkStreamAsset* stream = this->getLocalStream(); | 172 SkStreamAsset* stream = this->getLocalStream(); |
132 if (stream) { | 173 if (stream) { |
133 // TODO: should have been provided by CreateFromStream() | 174 // TODO: should have been provided by CreateFromStream() |
134 *ttcIndex = 0; | 175 *ttcIndex = 0; |
(...skipping 13 matching lines...) Expand all Loading... | |
148 *familyName = fFamilyName; | 189 *familyName = fFamilyName; |
149 } | 190 } |
150 | 191 |
151 void FontConfigTypeface::onGetFontDescriptor(SkFontDescriptor* desc, | 192 void FontConfigTypeface::onGetFontDescriptor(SkFontDescriptor* desc, |
152 bool* isLocalStream) const { | 193 bool* isLocalStream) const { |
153 SkString name; | 194 SkString name; |
154 this->getFamilyName(&name); | 195 this->getFamilyName(&name); |
155 desc->setFamilyName(name.c_str()); | 196 desc->setFamilyName(name.c_str()); |
156 *isLocalStream = SkToBool(this->getLocalStream()); | 197 *isLocalStream = SkToBool(this->getLocalStream()); |
157 } | 198 } |
OLD | NEW |