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

Side by Side Diff: third_party/WebKit/Source/platform/fonts/FontCache.cpp

Issue 1962263002: Introduce FontCache content scaling (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix 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
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved. 2 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2007 Nicholas Shanks <webkit@nickshanks.com> 3 * Copyright (C) 2007 Nicholas Shanks <webkit@nickshanks.com>
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 namespace blink { 60 namespace blink {
61 61
62 #if !OS(WIN) && !OS(LINUX) 62 #if !OS(WIN) && !OS(LINUX)
63 FontCache::FontCache() 63 FontCache::FontCache()
64 : m_purgePreventCount(0) 64 : m_purgePreventCount(0)
65 , m_fontManager(nullptr) 65 , m_fontManager(nullptr)
66 { 66 {
67 } 67 }
68 #endif // !OS(WIN) && !OS(LINUX) 68 #endif // !OS(WIN) && !OS(LINUX)
69 69
70 typedef HashMap<FontCacheKey, OwnPtr<FontPlatformData>, FontCacheKeyHash, FontCa cheKeyTraits> FontPlatformDataCache; 70 typedef HashMap<unsigned, OwnPtr<FontPlatformData>> SizedFontPlatformDataSet;
71 typedef HashMap<FontCacheKey, SizedFontPlatformDataSet, FontCacheKeyHash, FontCa cheKeyTraits> FontPlatformDataCache;
drott 2016/05/18 07:22:48 Do we need to keep the rescaled sizes in a cache a
tzik 2016/05/18 07:26:53 It's not so cheap actually on Linux. The scaling c
71 typedef HashMap<FallbackListCompositeKey, OwnPtr<ShapeCache>, FallbackListCompos iteKeyHash, FallbackListCompositeKeyTraits> FallbackListShaperCache; 72 typedef HashMap<FallbackListCompositeKey, OwnPtr<ShapeCache>, FallbackListCompos iteKeyHash, FallbackListCompositeKeyTraits> FallbackListShaperCache;
72 73
73 static FontPlatformDataCache* gFontPlatformDataCache = nullptr; 74 static FontPlatformDataCache* gFontPlatformDataCache = nullptr;
74 static FallbackListShaperCache* gFallbackListShaperCache = nullptr; 75 static FallbackListShaperCache* gFallbackListShaperCache = nullptr;
75 76
76 SkFontMgr* FontCache::s_fontManager = nullptr; 77 SkFontMgr* FontCache::s_fontManager = nullptr;
77 78
78 #if OS(WIN) 79 #if OS(WIN)
79 bool FontCache::s_antialiasedTextEnabled = false; 80 bool FontCache::s_antialiasedTextEnabled = false;
80 bool FontCache::s_lcdTextEnabled = false; 81 bool FontCache::s_lcdTextEnabled = false;
81 float FontCache::s_deviceScaleFactor = 1.0; 82 float FontCache::s_deviceScaleFactor = 1.0;
82 bool FontCache::s_useSkiaFontFallback = false; 83 bool FontCache::s_useSkiaFontFallback = false;
83 #endif // OS(WIN) 84 #endif // OS(WIN)
84 85
85 FontCache* FontCache::fontCache() 86 FontCache* FontCache::fontCache()
86 { 87 {
87 DEFINE_STATIC_LOCAL(FontCache, globalFontCache, ()); 88 DEFINE_STATIC_LOCAL(FontCache, globalFontCache, ());
88 return &globalFontCache; 89 return &globalFontCache;
89 } 90 }
90 91
91 FontPlatformData* FontCache::getFontPlatformData(const FontDescription& fontDesc ription, 92 FontPlatformData* FontCache::getFontPlatformData(const FontDescription& fontDesc ription,
92 const FontFaceCreationParams& creationParams, bool checkingAlternateName) 93 const FontFaceCreationParams& creationParams, bool checkingAlternateName)
93 { 94 {
94 if (!gFontPlatformDataCache) { 95 if (!gFontPlatformDataCache) {
95 gFontPlatformDataCache = new FontPlatformDataCache; 96 gFontPlatformDataCache = new FontPlatformDataCache;
96 platformInit(); 97 platformInit();
97 } 98 }
98 99
100 float size = fontDescription.effectiveFontSize();
99 FontCacheKey key = fontDescription.cacheKey(creationParams); 101 FontCacheKey key = fontDescription.cacheKey(creationParams);
102
103 // Remove the font size from the cache key, and handle the font size separat ely in the inner
104 // HashMap. So that different size of FontPlatformData can share underlying SkTypeface.
105 key.clearFontSize();
106
100 FontPlatformData* result; 107 FontPlatformData* result;
101 bool foundResult; 108 bool foundResult;
109
102 { 110 {
103 // addResult's scope must end before we recurse for alternate family nam es below, 111 // addResult's scope must end before we recurse for alternate family nam es below,
104 // to avoid trigering its dtor hash-changed asserts. 112 // to avoid trigering its dtor hash-changed asserts.
105 auto addResult = gFontPlatformDataCache->add(key, nullptr); 113 SizedFontPlatformDataSet* sizedFonts = &gFontPlatformDataCache->add(key, SizedFontPlatformDataSet()).storedValue->value;
106 if (addResult.isNewEntry) 114 bool wasEmpty = sizedFonts->isEmpty();
107 addResult.storedValue->value = createFontPlatformData(fontDescriptio n, creationParams, fontDescription.effectiveFontSize());
108 115
109 result = addResult.storedValue->value.get(); 116 // Take a different size instance of the same font before adding an entr y to |sizedFont|.
117 FontPlatformData* anotherSize = wasEmpty ? nullptr : sizedFonts->begin() ->value.get();
118
119 // Use -2 instead of 0 for the default size. We can not use 0 or -1 for HashMap key, since
120 // they are reserved for empty and deleted value.
121 unsigned roundedSize = (unsigned)size ? size * FontCacheKey::precisionMu ltiplier() : -2;
122 auto addResult = sizedFonts->add(roundedSize, nullptr);
123 OwnPtr<FontPlatformData>* found = &addResult.storedValue->value;
124 if (addResult.isNewEntry) {
125 if (wasEmpty)
126 *found = createFontPlatformData(fontDescription, creationParams, size);
127 else if (anotherSize)
128 *found = scaleFontPlatformData(*anotherSize, fontDescription, cr eationParams, size);
129 }
130
131 result = found->get();
110 foundResult = result || !addResult.isNewEntry; 132 foundResult = result || !addResult.isNewEntry;
111 } 133 }
112 134
113 if (!foundResult && !checkingAlternateName && creationParams.creationType() == CreateFontByFamily) { 135 if (!foundResult && !checkingAlternateName && creationParams.creationType() == CreateFontByFamily) {
114 // We were unable to find a font. We have a small set of fonts that we a lias to other names, 136 // We were unable to find a font. We have a small set of fonts that we a lias to other names,
115 // e.g., Arial/Helvetica, Courier/Courier New, etc. Try looking up the f ont under the aliased name. 137 // e.g., Arial/Helvetica, Courier/Courier New, etc. Try looking up the f ont under the aliased name.
116 const AtomicString& alternateName = alternateFamilyName(creationParams.f amily()); 138 const AtomicString& alternateName = alternateFamilyName(creationParams.f amily());
117 if (!alternateName.isEmpty()) { 139 if (!alternateName.isEmpty()) {
118 FontFaceCreationParams createByAlternateFamily(alternateName); 140 FontFaceCreationParams createByAlternateFamily(alternateName);
119 result = getFontPlatformData(fontDescription, createByAlternateFamil y, true); 141 result = getFontPlatformData(fontDescription, createByAlternateFamil y, true);
120 } 142 }
121 if (result) 143 if (result) {
122 gFontPlatformDataCache->set(key, adoptPtr(new FontPlatformData(*resu lt))); // Cache the result under the old name. 144 // Cache the result under the old name.
145 auto adding = &gFontPlatformDataCache->add(key, SizedFontPlatformDat aSet()).storedValue->value;
146 adding->set(size, adoptPtr(new FontPlatformData(*result)));
147 }
123 } 148 }
124 149
125 return result; 150 return result;
126 } 151 }
127 152
153 PassOwnPtr<FontPlatformData> FontCache::scaleFontPlatformData(const FontPlatform Data& fontPlatformData, const FontDescription& fontDescription, const FontFaceCr eationParams& creationParams, float fontSize)
154 {
155 #if OS(MACOSX)
156 return createFontPlatformData(fontDescription, creationParams, fontSize);
157 #else
158 return adoptPtr(new FontPlatformData(fontPlatformData, fontSize));
159 #endif
160 }
161
128 ShapeCache* FontCache::getShapeCache(const FallbackListCompositeKey& key) 162 ShapeCache* FontCache::getShapeCache(const FallbackListCompositeKey& key)
129 { 163 {
130 if (!gFallbackListShaperCache) 164 if (!gFallbackListShaperCache)
131 gFallbackListShaperCache = new FallbackListShaperCache; 165 gFallbackListShaperCache = new FallbackListShaperCache;
132 166
133 FallbackListShaperCache::iterator it = gFallbackListShaperCache->find(key); 167 FallbackListShaperCache::iterator it = gFallbackListShaperCache->find(key);
134 ShapeCache* result = nullptr; 168 ShapeCache* result = nullptr;
135 if (it == gFallbackListShaperCache->end()) { 169 if (it == gFallbackListShaperCache->end()) {
136 result = new ShapeCache(); 170 result = new ShapeCache();
137 gFallbackListShaperCache->set(key, adoptPtr(result)); 171 gFallbackListShaperCache->set(key, adoptPtr(result));
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 gFontDataCache->release(fontData); 253 gFontDataCache->release(fontData);
220 } 254 }
221 255
222 static inline void purgePlatformFontDataCache() 256 static inline void purgePlatformFontDataCache()
223 { 257 {
224 if (!gFontPlatformDataCache) 258 if (!gFontPlatformDataCache)
225 return; 259 return;
226 260
227 Vector<FontCacheKey> keysToRemove; 261 Vector<FontCacheKey> keysToRemove;
228 keysToRemove.reserveInitialCapacity(gFontPlatformDataCache->size()); 262 keysToRemove.reserveInitialCapacity(gFontPlatformDataCache->size());
229 FontPlatformDataCache::iterator platformDataEnd = gFontPlatformDataCache->en d(); 263 for (auto& sizedFonts : *gFontPlatformDataCache) {
230 for (FontPlatformDataCache::iterator platformData = gFontPlatformDataCache-> begin(); platformData != platformDataEnd; ++platformData) { 264 Vector<float> sizesToRemove;
231 if (platformData->value && !gFontDataCache->contains(platformData->value .get())) 265 sizesToRemove.reserveInitialCapacity(sizedFonts.value.size());
232 keysToRemove.append(platformData->key); 266 for (const auto& platformData : sizedFonts.value) {
267 if (platformData.value && !gFontDataCache->contains(platformData.val ue.get()))
268 sizesToRemove.append(platformData.key);
269 }
270 sizedFonts.value.removeAll(sizesToRemove);
271 if (sizedFonts.value.isEmpty())
272 keysToRemove.append(sizedFonts.key);
233 } 273 }
234 gFontPlatformDataCache->removeAll(keysToRemove); 274 gFontPlatformDataCache->removeAll(keysToRemove);
235 } 275 }
236 276
237 static inline void purgeFontVerticalDataCache() 277 static inline void purgeFontVerticalDataCache()
238 { 278 {
239 FontVerticalDataCache& fontVerticalDataCache = fontVerticalDataCacheInstance (); 279 FontVerticalDataCache& fontVerticalDataCache = fontVerticalDataCacheInstance ();
240 if (!fontVerticalDataCache.isEmpty()) { 280 if (!fontVerticalDataCache.isEmpty()) {
241 // Mark & sweep unused verticalData 281 // Mark & sweep unused verticalData
242 FontVerticalDataCache::iterator verticalDataEnd = fontVerticalDataCache. end(); 282 FontVerticalDataCache::iterator verticalDataEnd = fontVerticalDataCache. end();
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
382 case USCRIPT_SIMPLIFIED_HAN: 422 case USCRIPT_SIMPLIFIED_HAN:
383 return "zh-Hans"; 423 return "zh-Hans";
384 case USCRIPT_TRADITIONAL_HAN: 424 case USCRIPT_TRADITIONAL_HAN:
385 return "zh-Hant"; 425 return "zh-Hant";
386 default: 426 default:
387 return locale.ascii(); 427 return locale.ascii();
388 } 428 }
389 } 429 }
390 430
391 } // namespace blink 431 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/platform/fonts/FontCache.h ('k') | third_party/WebKit/Source/platform/fonts/FontCacheKey.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698