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