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

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: win layout test 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>, WTF::IntHash<unsigned>, WTF: :UnsignedWithZeroKeyHashTraits<unsigned>> SizedFontPlatformDataSet;
71 typedef HashMap<FontCacheKey, SizedFontPlatformDataSet, FontCacheKeyHash, FontCa cheKeyTraits> FontPlatformDataCache;
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();
101 unsigned roundedSize = size * FontCacheKey::precisionMultiplier();
99 FontCacheKey key = fontDescription.cacheKey(creationParams); 102 FontCacheKey key = fontDescription.cacheKey(creationParams);
103
104 // Remove the font size from the cache key, and handle the font size separat ely in the inner
105 // HashMap. So that different size of FontPlatformData can share underlying SkTypeface.
106 if (RuntimeEnabledFeatures::fontCacheScalingEnabled())
107 key.clearFontSize();
108
100 FontPlatformData* result; 109 FontPlatformData* result;
101 bool foundResult; 110 bool foundResult;
111
102 { 112 {
103 // addResult's scope must end before we recurse for alternate family nam es below, 113 // addResult's scope must end before we recurse for alternate family nam es below,
104 // to avoid trigering its dtor hash-changed asserts. 114 // to avoid trigering its dtor hash-changed asserts.
105 auto addResult = gFontPlatformDataCache->add(key, nullptr); 115 SizedFontPlatformDataSet* sizedFonts = &gFontPlatformDataCache->add(key, SizedFontPlatformDataSet()).storedValue->value;
106 if (addResult.isNewEntry) 116 bool wasEmpty = sizedFonts->isEmpty();
107 addResult.storedValue->value = createFontPlatformData(fontDescriptio n, creationParams, fontDescription.effectiveFontSize());
108 117
109 result = addResult.storedValue->value.get(); 118 // Take a different size instance of the same font before adding an entr y to |sizedFont|.
119 FontPlatformData* anotherSize = wasEmpty ? nullptr : sizedFonts->begin() ->value.get();
120 auto addResult = sizedFonts->add(roundedSize, nullptr);
121 OwnPtr<FontPlatformData>* found = &addResult.storedValue->value;
122 if (addResult.isNewEntry) {
123 if (wasEmpty)
124 *found = createFontPlatformData(fontDescription, creationParams, size);
125 else if (anotherSize)
126 *found = scaleFontPlatformData(*anotherSize, fontDescription, cr eationParams, size);
127 }
128
129 result = found->get();
110 foundResult = result || !addResult.isNewEntry; 130 foundResult = result || !addResult.isNewEntry;
111 } 131 }
112 132
113 if (!foundResult && !checkingAlternateName && creationParams.creationType() == CreateFontByFamily) { 133 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, 134 // 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. 135 // 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()); 136 const AtomicString& alternateName = alternateFamilyName(creationParams.f amily());
117 if (!alternateName.isEmpty()) { 137 if (!alternateName.isEmpty()) {
118 FontFaceCreationParams createByAlternateFamily(alternateName); 138 FontFaceCreationParams createByAlternateFamily(alternateName);
119 result = getFontPlatformData(fontDescription, createByAlternateFamil y, true); 139 result = getFontPlatformData(fontDescription, createByAlternateFamil y, true);
120 } 140 }
121 if (result) 141 if (result) {
122 gFontPlatformDataCache->set(key, adoptPtr(new FontPlatformData(*resu lt))); // Cache the result under the old name. 142 // Cache the result under the old name.
143 auto adding = &gFontPlatformDataCache->add(key, SizedFontPlatformDat aSet()).storedValue->value;
144 adding->set(roundedSize, adoptPtr(new FontPlatformData(*result)));
145 }
123 } 146 }
124 147
125 return result; 148 return result;
126 } 149 }
127 150
151 PassOwnPtr<FontPlatformData> FontCache::scaleFontPlatformData(const FontPlatform Data& fontPlatformData, const FontDescription& fontDescription, const FontFaceCr eationParams& creationParams, float fontSize)
152 {
153 #if OS(MACOSX)
154 return createFontPlatformData(fontDescription, creationParams, fontSize);
155 #else
156 return adoptPtr(new FontPlatformData(fontPlatformData, fontSize));
157 #endif
158 }
159
128 ShapeCache* FontCache::getShapeCache(const FallbackListCompositeKey& key) 160 ShapeCache* FontCache::getShapeCache(const FallbackListCompositeKey& key)
129 { 161 {
130 if (!gFallbackListShaperCache) 162 if (!gFallbackListShaperCache)
131 gFallbackListShaperCache = new FallbackListShaperCache; 163 gFallbackListShaperCache = new FallbackListShaperCache;
132 164
133 FallbackListShaperCache::iterator it = gFallbackListShaperCache->find(key); 165 FallbackListShaperCache::iterator it = gFallbackListShaperCache->find(key);
134 ShapeCache* result = nullptr; 166 ShapeCache* result = nullptr;
135 if (it == gFallbackListShaperCache->end()) { 167 if (it == gFallbackListShaperCache->end()) {
136 result = new ShapeCache(); 168 result = new ShapeCache();
137 gFallbackListShaperCache->set(key, adoptPtr(result)); 169 gFallbackListShaperCache->set(key, adoptPtr(result));
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 gFontDataCache->release(fontData); 251 gFontDataCache->release(fontData);
220 } 252 }
221 253
222 static inline void purgePlatformFontDataCache() 254 static inline void purgePlatformFontDataCache()
223 { 255 {
224 if (!gFontPlatformDataCache) 256 if (!gFontPlatformDataCache)
225 return; 257 return;
226 258
227 Vector<FontCacheKey> keysToRemove; 259 Vector<FontCacheKey> keysToRemove;
228 keysToRemove.reserveInitialCapacity(gFontPlatformDataCache->size()); 260 keysToRemove.reserveInitialCapacity(gFontPlatformDataCache->size());
229 FontPlatformDataCache::iterator platformDataEnd = gFontPlatformDataCache->en d(); 261 for (auto& sizedFonts : *gFontPlatformDataCache) {
230 for (FontPlatformDataCache::iterator platformData = gFontPlatformDataCache-> begin(); platformData != platformDataEnd; ++platformData) { 262 Vector<unsigned> sizesToRemove;
231 if (platformData->value && !gFontDataCache->contains(platformData->value .get())) 263 sizesToRemove.reserveInitialCapacity(sizedFonts.value.size());
232 keysToRemove.append(platformData->key); 264 for (const auto& platformData : sizedFonts.value) {
265 if (platformData.value && !gFontDataCache->contains(platformData.val ue.get()))
266 sizesToRemove.append(platformData.key);
267 }
268 sizedFonts.value.removeAll(sizesToRemove);
269 if (sizedFonts.value.isEmpty())
270 keysToRemove.append(sizedFonts.key);
233 } 271 }
234 gFontPlatformDataCache->removeAll(keysToRemove); 272 gFontPlatformDataCache->removeAll(keysToRemove);
235 } 273 }
236 274
237 static inline void purgeFontVerticalDataCache() 275 static inline void purgeFontVerticalDataCache()
238 { 276 {
239 FontVerticalDataCache& fontVerticalDataCache = fontVerticalDataCacheInstance (); 277 FontVerticalDataCache& fontVerticalDataCache = fontVerticalDataCacheInstance ();
240 if (!fontVerticalDataCache.isEmpty()) { 278 if (!fontVerticalDataCache.isEmpty()) {
241 // Mark & sweep unused verticalData 279 // Mark & sweep unused verticalData
242 FontVerticalDataCache::iterator verticalDataEnd = fontVerticalDataCache. end(); 280 FontVerticalDataCache::iterator verticalDataEnd = fontVerticalDataCache. end();
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
382 case USCRIPT_SIMPLIFIED_HAN: 420 case USCRIPT_SIMPLIFIED_HAN:
383 return "zh-Hans"; 421 return "zh-Hans";
384 case USCRIPT_TRADITIONAL_HAN: 422 case USCRIPT_TRADITIONAL_HAN:
385 return "zh-Hant"; 423 return "zh-Hant";
386 default: 424 default:
387 return locale.ascii(); 425 return locale.ascii();
388 } 426 }
389 } 427 }
390 428
391 } // namespace blink 429 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698