| 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" |
| 11 #include "SkStream.h" | 11 #include "SkStream.h" |
| 12 #include "SkTemplates.h" |
| 12 #include "SkTypeface.h" | 13 #include "SkTypeface.h" |
| 13 #include "SkTypefaceCache.h" | 14 #include "SkTypefaceCache.h" |
| 15 #include "SkResourceCache.h" |
| 14 | 16 |
| 15 /////////////////////////////////////////////////////////////////////////////// | 17 /////////////////////////////////////////////////////////////////////////////// |
| 16 /////////////////////////////////////////////////////////////////////////////// | 18 /////////////////////////////////////////////////////////////////////////////// |
| 17 | 19 |
| 18 SK_DECLARE_STATIC_MUTEX(gFontConfigInterfaceMutex); | 20 SK_DECLARE_STATIC_MUTEX(gFontConfigInterfaceMutex); |
| 19 static SkFontConfigInterface* gFontConfigInterface; | 21 static SkFontConfigInterface* gFontConfigInterface; |
| 20 | 22 |
| 21 SkFontConfigInterface* SkFontConfigInterface::RefGlobal() { | 23 SkFontConfigInterface* SkFontConfigInterface::RefGlobal() { |
| 22 SkAutoMutexAcquire ac(gFontConfigInterfaceMutex); | 24 SkAutoMutexAcquire ac(gFontConfigInterfaceMutex); |
| 23 | 25 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 49 } | 51 } |
| 50 | 52 |
| 51 // export this to SkFontMgr_fontconfig.cpp until this file just goes away. | 53 // export this to SkFontMgr_fontconfig.cpp until this file just goes away. |
| 52 SkFontConfigInterface* SkFontHost_fontconfig_ref_global(); | 54 SkFontConfigInterface* SkFontHost_fontconfig_ref_global(); |
| 53 SkFontConfigInterface* SkFontHost_fontconfig_ref_global() { | 55 SkFontConfigInterface* SkFontHost_fontconfig_ref_global() { |
| 54 return RefFCI(); | 56 return RefFCI(); |
| 55 } | 57 } |
| 56 | 58 |
| 57 /////////////////////////////////////////////////////////////////////////////// | 59 /////////////////////////////////////////////////////////////////////////////// |
| 58 | 60 |
| 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) { | 61 static bool find_by_FontIdentity(SkTypeface* cachedTypeface, const SkFontStyle&,
void* ctx) { |
| 80 typedef SkFontConfigInterface::FontIdentity FontIdentity; | 62 typedef SkFontConfigInterface::FontIdentity FontIdentity; |
| 81 FontConfigTypeface* cachedFCTypeface = static_cast<FontConfigTypeface*>(cach
edTypeface); | 63 FontConfigTypeface* cachedFCTypeface = static_cast<FontConfigTypeface*>(cach
edTypeface); |
| 82 FontIdentity* indentity = static_cast<FontIdentity*>(ctx); | 64 FontIdentity* identity = static_cast<FontIdentity*>(ctx); |
| 83 | 65 |
| 84 return cachedFCTypeface->getIdentity() == *indentity; | 66 return cachedFCTypeface->getIdentity() == *identity; |
| 85 } | 67 } |
| 86 | 68 |
| 87 SkTypeface* FontConfigTypeface::LegacyCreateTypeface(const char familyName[], | 69 SK_DECLARE_STATIC_MUTEX(gSkFontHostRequestCacheMutex); |
| 88 SkTypeface::Style style) | 70 class SkFontHostRequestCache { |
| 71 |
| 72 // The value of maxSize here is a compromise between cache hits and cache si
ze. |
| 73 static const size_t gMaxSize = 1 << 12; |
| 74 |
| 75 static SkFontHostRequestCache& Get() { |
| 76 gSkFontHostRequestCacheMutex.assertHeld(); |
| 77 static SkFontHostRequestCache gCache(gMaxSize); |
| 78 return gCache; |
| 79 } |
| 80 |
| 81 public: |
| 82 struct Request : public SkResourceCache::Key { |
| 83 private: |
| 84 Request(const char* name, size_t nameLen, const SkFontStyle& style) : fS
tyle(style) { |
| 85 /** Pointer to just after the last field of this class. */ |
| 86 char* content = const_cast<char*>(SkTAfter<const char>(&this->fStyle
)); |
| 87 |
| 88 // No holes. |
| 89 SkASSERT(SkTAddOffset<char>(this, sizeof(SkResourceCache::Key) + key
Size) == content); |
| 90 |
| 91 // Has a size divisible by size of uint32_t. |
| 92 SkASSERT((content - reinterpret_cast<char*>(this)) % sizeof(uint32_t
) == 0); |
| 93 |
| 94 size_t contentLen = SkAlign4(nameLen); |
| 95 sk_careful_memcpy(content, name, nameLen); |
| 96 sk_bzero(content + nameLen, contentLen - nameLen); |
| 97 this->init(&gSkFontHostRequestCacheMutex, 0, keySize + contentLen); |
| 98 } |
| 99 const SkFontStyle fStyle; |
| 100 /** The sum of the sizes of the fields of this class. */ |
| 101 static const size_t keySize = sizeof(fStyle); |
| 102 |
| 103 public: |
| 104 static Request* Create(const char* name, const SkFontStyle& style) { |
| 105 size_t nameLen = name ? strlen(name) : 0; |
| 106 size_t contentLen = SkAlign4(nameLen); |
| 107 char* storage = new char[sizeof(Request) + contentLen]; |
| 108 return new (storage) Request(name, nameLen, style); |
| 109 } |
| 110 void operator delete(void* storage) { |
| 111 delete[] reinterpret_cast<char*>(storage); |
| 112 } |
| 113 }; |
| 114 |
| 115 |
| 116 private: |
| 117 struct Result : public SkResourceCache::Rec { |
| 118 Result(Request* request, SkTypeface* typeface) |
| 119 : fRequest(request) |
| 120 , fFace(SkSafeRef(typeface)) {} |
| 121 Result(Result&&) = default; |
| 122 Result& operator=(Result&&) = default; |
| 123 |
| 124 const Key& getKey() const override { return *fRequest; } |
| 125 size_t bytesUsed() const override { return fRequest->size() + sizeof(fFa
ce); } |
| 126 const char* getCategory() const override { return "request_cache"; } |
| 127 SkDiscardableMemory* diagnostic_only_getDiscardable() const override { r
eturn nullptr; } |
| 128 |
| 129 SkAutoTDelete<Request> fRequest; |
| 130 SkAutoTUnref<SkTypeface> fFace; |
| 131 }; |
| 132 |
| 133 SkResourceCache fCachedResults; |
| 134 |
| 135 public: |
| 136 SkFontHostRequestCache(size_t maxSize) : fCachedResults(maxSize) {} |
| 137 |
| 138 /** Takes ownership of request. It will be deleted when no longer needed. */ |
| 139 void add(SkTypeface* face, Request* request) { |
| 140 fCachedResults.add(new Result(request, face)); |
| 141 } |
| 142 /** Does not take ownership of request. */ |
| 143 SkTypeface* findAndRef(Request* request) { |
| 144 SkTypeface* face = nullptr; |
| 145 fCachedResults.find(*request, [](const SkResourceCache::Rec& rec, void*
context) -> bool { |
| 146 const Result& result = static_cast<const Result&>(rec); |
| 147 SkTypeface** face = static_cast<SkTypeface**>(context); |
| 148 |
| 149 *face = result.fFace; |
| 150 return true; |
| 151 }, &face); |
| 152 return SkSafeRef(face); |
| 153 } |
| 154 |
| 155 /** Takes ownership of request. It will be deleted when no longer needed. */ |
| 156 static void Add(SkTypeface* face, Request* request) { |
| 157 SkAutoMutexAcquire ama(gSkFontHostRequestCacheMutex); |
| 158 Get().add(face, request); |
| 159 } |
| 160 |
| 161 /** Does not take ownership of request. */ |
| 162 static SkTypeface* FindAndRef(Request* request) { |
| 163 SkAutoMutexAcquire ama(gSkFontHostRequestCacheMutex); |
| 164 return Get().findAndRef(request); |
| 165 } |
| 166 }; |
| 167 |
| 168 SkTypeface* FontConfigTypeface::LegacyCreateTypeface(const char requestedFamilyN
ame[], |
| 169 SkTypeface::Style requested
OldStyle) |
| 89 { | 170 { |
| 90 SkAutoTUnref<SkFontConfigInterface> fci(RefFCI()); | 171 SkAutoTUnref<SkFontConfigInterface> fci(RefFCI()); |
| 91 if (nullptr == fci.get()) { | 172 if (nullptr == fci.get()) { |
| 92 return nullptr; | 173 return nullptr; |
| 93 } | 174 } |
| 94 | 175 |
| 95 // Check if requested NameStyle is in the NameStyle cache. | 176 // Check if this request is already in the request cache. |
| 96 SkFontStyle requestedStyle(style); | 177 using Request = SkFontHostRequestCache::Request; |
| 97 NameStyle nameStyle(familyName, requestedStyle); | 178 SkFontStyle requestedStyle(requestedOldStyle); |
| 98 SkTypeface* face = SkTypefaceCache::FindByProcAndRef(find_by_NameStyle, &nam
eStyle); | 179 SkAutoTDelete<Request> request(Request::Create(requestedFamilyName, requeste
dStyle)); |
| 180 SkTypeface* face = SkFontHostRequestCache::FindAndRef(request); |
| 99 if (face) { | 181 if (face) { |
| 100 //SkDebugf("found cached face <%s> <%s> %p [%d]\n", | |
| 101 // familyName, ((FontConfigTypeface*)face)->getFamilyName(), | |
| 102 // face, face->getRefCnt()); | |
| 103 return face; | 182 return face; |
| 104 } | 183 } |
| 105 | 184 |
| 106 SkFontConfigInterface::FontIdentity indentity; | 185 SkFontConfigInterface::FontIdentity identity; |
| 107 SkString outFamilyName; | 186 SkString outFamilyName; |
| 108 SkTypeface::Style outStyle; | 187 SkTypeface::Style outOldStyle; |
| 109 if (!fci->matchFamilyName(familyName, style, &indentity, &outFamilyName, &ou
tStyle)) { | 188 if (!fci->matchFamilyName(requestedFamilyName, requestedOldStyle, |
| 189 &identity, &outFamilyName, &outOldStyle)) |
| 190 { |
| 110 return nullptr; | 191 return nullptr; |
| 111 } | 192 } |
| 112 | 193 |
| 113 // Check if a typeface with this FontIdentity is already in the FontIdentity
cache. | 194 // Check if a typeface with this FontIdentity is already in the FontIdentity
cache. |
| 114 face = SkTypefaceCache::FindByProcAndRef(find_by_FontIdentity, &indentity); | 195 face = SkTypefaceCache::FindByProcAndRef(find_by_FontIdentity, &identity); |
| 115 if (!face) { | 196 if (!face) { |
| 116 face = FontConfigTypeface::Create(SkFontStyle(outStyle), indentity, outF
amilyName); | 197 face = FontConfigTypeface::Create(SkFontStyle(outOldStyle), identity, ou
tFamilyName); |
| 117 // Add this FontIdentity to the FontIdentity cache. | 198 // Add this FontIdentity to the FontIdentity cache. |
| 118 SkTypefaceCache::Add(face, requestedStyle); | 199 SkTypefaceCache::Add(face, SkFontStyle(outOldStyle)); |
| 119 } | 200 } |
| 120 // TODO: Ensure requested NameStyle and resolved NameStyle are both in the N
ameStyle cache. | 201 // Add this request to the request cache. |
| 202 SkFontHostRequestCache::Add(face, request.release()); |
| 121 | 203 |
| 122 //SkDebugf("add face <%s> <%s> %p [%d]\n", | |
| 123 // familyName, outFamilyName.c_str(), | |
| 124 // face, face->getRefCnt()); | |
| 125 return face; | 204 return face; |
| 126 } | 205 } |
| 127 | 206 |
| 128 /////////////////////////////////////////////////////////////////////////////// | 207 /////////////////////////////////////////////////////////////////////////////// |
| 129 | 208 |
| 130 SkStreamAsset* FontConfigTypeface::onOpenStream(int* ttcIndex) const { | 209 SkStreamAsset* FontConfigTypeface::onOpenStream(int* ttcIndex) const { |
| 131 SkStreamAsset* stream = this->getLocalStream(); | 210 SkStreamAsset* stream = this->getLocalStream(); |
| 132 if (stream) { | 211 if (stream) { |
| 133 // TODO: should have been provided by CreateFromStream() | 212 // TODO: should have been provided by CreateFromStream() |
| 134 *ttcIndex = 0; | 213 *ttcIndex = 0; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 148 *familyName = fFamilyName; | 227 *familyName = fFamilyName; |
| 149 } | 228 } |
| 150 | 229 |
| 151 void FontConfigTypeface::onGetFontDescriptor(SkFontDescriptor* desc, | 230 void FontConfigTypeface::onGetFontDescriptor(SkFontDescriptor* desc, |
| 152 bool* isLocalStream) const { | 231 bool* isLocalStream) const { |
| 153 SkString name; | 232 SkString name; |
| 154 this->getFamilyName(&name); | 233 this->getFamilyName(&name); |
| 155 desc->setFamilyName(name.c_str()); | 234 desc->setFamilyName(name.c_str()); |
| 156 *isLocalStream = SkToBool(this->getLocalStream()); | 235 *isLocalStream = SkToBool(this->getLocalStream()); |
| 157 } | 236 } |
| OLD | NEW |