Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(201)

Side by Side Diff: src/ports/SkFontHost_fontconfig.cpp

Issue 1919183002: Store null font lookup result into font cache (Closed) Base URL: https://chromium.googlesource.com/skia.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 124
125 const Key& getKey() const override { return *fRequest; } 125 const Key& getKey() const override { return *fRequest; }
126 size_t bytesUsed() const override { return fRequest->size() + sizeof(fFa ce); } 126 size_t bytesUsed() const override { return fRequest->size() + sizeof(fFa ce); }
127 const char* getCategory() const override { return "request_cache"; } 127 const char* getCategory() const override { return "request_cache"; }
128 SkDiscardableMemory* diagnostic_only_getDiscardable() const override { r eturn nullptr; } 128 SkDiscardableMemory* diagnostic_only_getDiscardable() const override { r eturn nullptr; }
129 129
130 SkAutoTDelete<Request> fRequest; 130 SkAutoTDelete<Request> fRequest;
131 SkAutoTUnref<SkTypeface> fFace; 131 SkAutoTUnref<SkTypeface> fFace;
132 }; 132 };
133 133
134 struct LookUpContext {
135 bool found;
136 SkTypeface* face;
137 };
138
134 SkResourceCache fCachedResults; 139 SkResourceCache fCachedResults;
135 140
136 public: 141 public:
137 SkFontHostRequestCache(size_t maxSize) : fCachedResults(maxSize) {} 142 SkFontHostRequestCache(size_t maxSize) : fCachedResults(maxSize) {}
138 143
139 /** Takes ownership of request. It will be deleted when no longer needed. */ 144 /** Takes ownership of request. It will be deleted when no longer needed. */
140 void add(SkTypeface* face, Request* request) { 145 void add(SkTypeface* face, Request* request) {
141 fCachedResults.add(new Result(request, face)); 146 fCachedResults.add(new Result(request, face));
142 } 147 }
143 /** Does not take ownership of request. */ 148 /** Does not take ownership of request. */
144 SkTypeface* findAndRef(Request* request) { 149 bool findAndRef(Request* request, SkTypeface** face) {
145 SkTypeface* face = nullptr; 150 LookUpContext lookup_context = {false, nullptr};
146 fCachedResults.find(*request, [](const SkResourceCache::Rec& rec, void* context) -> bool { 151 fCachedResults.find(*request, [](const SkResourceCache::Rec& rec, void* context) -> bool {
147 const Result& result = static_cast<const Result&>(rec); 152 const Result& result = static_cast<const Result&>(rec);
148 SkTypeface** face = static_cast<SkTypeface**>(context); 153 LookUpContext* lookup_context = static_cast<LookUpContext*>(context) ;
149 154
150 *face = result.fFace; 155 lookup_context->found = true;
156 lookup_context->face = result.fFace;
151 return true; 157 return true;
152 }, &face); 158 }, &lookup_context);
153 return SkSafeRef(face); 159 *face = SkSafeRef(lookup_context.face);
160 return lookup_context.found;
154 } 161 }
155 162
156 /** Takes ownership of request. It will be deleted when no longer needed. */ 163 /** Takes ownership of request. It will be deleted when no longer needed. */
157 static void Add(SkTypeface* face, Request* request) { 164 static void Add(SkTypeface* face, Request* request) {
158 SkAutoMutexAcquire ama(gSkFontHostRequestCacheMutex); 165 SkAutoMutexAcquire ama(gSkFontHostRequestCacheMutex);
159 Get().add(face, request); 166 Get().add(face, request);
160 } 167 }
161 168
162 /** Does not take ownership of request. */ 169 /** Does not take ownership of request. */
163 static SkTypeface* FindAndRef(Request* request) { 170 static bool FindAndRef(Request* request, SkTypeface** face) {
164 SkAutoMutexAcquire ama(gSkFontHostRequestCacheMutex); 171 SkAutoMutexAcquire ama(gSkFontHostRequestCacheMutex);
165 return Get().findAndRef(request); 172 return Get().findAndRef(request, face);
166 } 173 }
167 }; 174 };
168 175
169 SkTypeface* FontConfigTypeface::LegacyCreateTypeface(const char requestedFamilyN ame[], 176 SkTypeface* FontConfigTypeface::LegacyCreateTypeface(const char requestedFamilyN ame[],
170 SkFontStyle requestedStyle) 177 SkFontStyle requestedStyle)
171 { 178 {
172 SkAutoTUnref<SkFontConfigInterface> fci(RefFCI()); 179 SkAutoTUnref<SkFontConfigInterface> fci(RefFCI());
173 if (nullptr == fci.get()) { 180 if (nullptr == fci.get()) {
174 return nullptr; 181 return nullptr;
175 } 182 }
176 183
177 // Check if this request is already in the request cache. 184 // Check if this request is already in the request cache.
178 using Request = SkFontHostRequestCache::Request; 185 using Request = SkFontHostRequestCache::Request;
179 SkAutoTDelete<Request> request(Request::Create(requestedFamilyName, requeste dStyle)); 186 SkAutoTDelete<Request> request(Request::Create(requestedFamilyName, requeste dStyle));
180 SkTypeface* face = SkFontHostRequestCache::FindAndRef(request); 187 SkTypeface* face;
181 if (face) { 188 if (SkFontHostRequestCache::FindAndRef(request, &face)) {
182 return face; 189 return face;
183 } 190 }
184 191
185 SkFontConfigInterface::FontIdentity identity; 192 SkFontConfigInterface::FontIdentity identity;
186 SkString outFamilyName; 193 SkString outFamilyName;
187 SkFontStyle outStyle; 194 SkFontStyle outStyle;
188 if (!fci->matchFamilyName(requestedFamilyName, requestedStyle, 195 if (!fci->matchFamilyName(requestedFamilyName, requestedStyle,
189 &identity, &outFamilyName, &outStyle)) 196 &identity, &outFamilyName, &outStyle))
190 { 197 {
198 SkFontHostRequestCache::Add(nullptr, request.release());
191 return nullptr; 199 return nullptr;
192 } 200 }
193 201
194 // Check if a typeface with this FontIdentity is already in the FontIdentity cache. 202 // Check if a typeface with this FontIdentity is already in the FontIdentity cache.
195 face = SkTypefaceCache::FindByProcAndRef(find_by_FontIdentity, &identity); 203 face = SkTypefaceCache::FindByProcAndRef(find_by_FontIdentity, &identity);
196 if (!face) { 204 if (!face) {
197 face = FontConfigTypeface::Create(outStyle, identity, outFamilyName); 205 face = FontConfigTypeface::Create(outStyle, identity, outFamilyName);
198 // Add this FontIdentity to the FontIdentity cache. 206 // Add this FontIdentity to the FontIdentity cache.
199 SkTypefaceCache::Add(face); 207 SkTypefaceCache::Add(face);
200 } 208 }
(...skipping 26 matching lines...) Expand all
227 *familyName = fFamilyName; 235 *familyName = fFamilyName;
228 } 236 }
229 237
230 void FontConfigTypeface::onGetFontDescriptor(SkFontDescriptor* desc, 238 void FontConfigTypeface::onGetFontDescriptor(SkFontDescriptor* desc,
231 bool* isLocalStream) const { 239 bool* isLocalStream) const {
232 SkString name; 240 SkString name;
233 this->getFamilyName(&name); 241 this->getFamilyName(&name);
234 desc->setFamilyName(name.c_str()); 242 desc->setFamilyName(name.c_str());
235 *isLocalStream = SkToBool(this->getLocalStream()); 243 *isLocalStream = SkToBool(this->getLocalStream());
236 } 244 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698