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

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

Issue 56017: Linux: change Skia's fontconfig code to cache SkTypeface objects. (Closed)
Patch Set: Created 11 years, 8 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 /* libs/graphics/ports/SkFontHost_fontconfig.cpp 1 /* libs/graphics/ports/SkFontHost_fontconfig.cpp
2 ** 2 **
3 ** Copyright 2008, Google Inc. 3 ** Copyright 2008, Google Inc.
4 ** 4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License"); 5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License. 6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at 7 ** You may obtain a copy of the License at
8 ** 8 **
9 ** http://www.apache.org/licenses/LICENSE-2.0 9 ** http://www.apache.org/licenses/LICENSE-2.0
10 ** 10 **
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 // filenames to fileid numbers and back. 48 // filenames to fileid numbers and back.
49 // 49 //
50 // Note that there's also a unique id in the SkTypeface. This is unique over 50 // Note that there's also a unique id in the SkTypeface. This is unique over
51 // both filename and style. Thus we encode that id as (fileid << 8) | style. 51 // both filename and style. Thus we encode that id as (fileid << 8) | style.
52 // Although truetype fonts can support multiple faces in a single file, at the 52 // Although truetype fonts can support multiple faces in a single file, at the
53 // moment Skia doesn't. 53 // moment Skia doesn't.
54 // ----------------------------------------------------------------------------- 54 // -----------------------------------------------------------------------------
55 static SkMutex global_fc_map_lock; 55 static SkMutex global_fc_map_lock;
56 static std::map<std::string, unsigned> global_fc_map; 56 static std::map<std::string, unsigned> global_fc_map;
57 static std::map<unsigned, std::string> global_fc_map_inverted; 57 static std::map<unsigned, std::string> global_fc_map_inverted;
58 static std::map<uint32_t, SkTypeface *> global_fc_typefaces;
58 static unsigned global_fc_map_next_id = 0; 59 static unsigned global_fc_map_next_id = 0;
59 60
60 // This is the maximum size of the font cache. 61 // This is the maximum size of the font cache.
61 static const unsigned kFontCacheMemoryBudget = 2 * 1024 * 1024; // 2MB 62 static const unsigned kFontCacheMemoryBudget = 2 * 1024 * 1024; // 2MB
62 63
63 static unsigned UniqueIdToFileId(unsigned uniqueid) 64 static unsigned UniqueIdToFileId(unsigned uniqueid)
64 { 65 {
65 return uniqueid >> 8; 66 return uniqueid >> 8;
66 } 67 }
67 68
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 if (FcPatternGetString(match, FC_FILE, 0, &filename) != FcResultMatch) { 276 if (FcPatternGetString(match, FC_FILE, 0, &filename) != FcResultMatch) {
276 FcPatternDestroy(match); 277 FcPatternDestroy(match);
277 return NULL; 278 return NULL;
278 } 279 }
279 // Now @filename is pointing into @match 280 // Now @filename is pointing into @match
280 281
281 const unsigned fileid = FileIdFromFilename(reinterpret_cast<char*>(filename) ); 282 const unsigned fileid = FileIdFromFilename(reinterpret_cast<char*>(filename) );
282 const unsigned id = FileIdAndStyleToUniqueId(fileid, style); 283 const unsigned id = FileIdAndStyleToUniqueId(fileid, style);
283 SkTypeface* typeface = SkNEW_ARGS(FontConfigTypeface, (style, id)); 284 SkTypeface* typeface = SkNEW_ARGS(FontConfigTypeface, (style, id));
284 FcPatternDestroy(match); 285 FcPatternDestroy(match);
286
287 {
288 SkAutoMutexAcquire ac(global_fc_map_lock);
289 global_fc_typefaces[id] = typeface;
290 }
291
285 return typeface; 292 return typeface;
286 } 293 }
287 294
288 SkTypeface* SkFontHost::ResolveTypeface(uint32_t id) 295 SkTypeface* SkFontHost::ResolveTypeface(uint32_t id)
289 { 296 {
290 SkAutoMutexAcquire ac(global_fc_map_lock); 297 SkAutoMutexAcquire ac(global_fc_map_lock);
291 const SkTypeface::Style style = UniqueIdToStyle(id); 298 const std::map<uint32_t, SkTypeface *>::iterator
292 const unsigned fileid = UniqueIdToFileId(id); 299 i = global_fc_typefaces.find(id);
293 300
294 std::map<unsigned, std::string>::const_iterator i = 301 if (i == global_fc_typefaces.end())
295 global_fc_map_inverted.find(fileid); 302 return NULL;
296 if (i == global_fc_map_inverted.end()) 303 return i->second;
297 return NULL;
298
299 return SkNEW_ARGS(FontConfigTypeface, (style, id));
300 } 304 }
301 305
302 SkStream* SkFontHost::OpenStream(uint32_t id) 306 SkStream* SkFontHost::OpenStream(uint32_t id)
303 { 307 {
304 SkAutoMutexAcquire ac(global_fc_map_lock); 308 SkAutoMutexAcquire ac(global_fc_map_lock);
305 const unsigned fileid = UniqueIdToFileId(id); 309 const unsigned fileid = UniqueIdToFileId(id);
306 310
307 std::map<unsigned, std::string>::const_iterator i = 311 std::map<unsigned, std::string>::const_iterator i =
308 global_fc_map_inverted.find(fileid); 312 global_fc_map_inverted.find(fileid);
309 if (i == global_fc_map_inverted.end()) 313 if (i == global_fc_map_inverted.end())
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
364 368
365 /////////////////////////////////////////////////////////////////////////////// 369 ///////////////////////////////////////////////////////////////////////////////
366 370
367 size_t SkFontHost::ShouldPurgeFontCache(size_t sizeAllocatedSoFar) 371 size_t SkFontHost::ShouldPurgeFontCache(size_t sizeAllocatedSoFar)
368 { 372 {
369 if (sizeAllocatedSoFar > kFontCacheMemoryBudget) 373 if (sizeAllocatedSoFar > kFontCacheMemoryBudget)
370 return sizeAllocatedSoFar - kFontCacheMemoryBudget; 374 return sizeAllocatedSoFar - kFontCacheMemoryBudget;
371 else 375 else
372 return 0; // nothing to do 376 return 0; // nothing to do
373 } 377 }
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