Commit 265af2d4 authored by derat's avatar derat Committed by Commit bot

linux: Cache families from FontRenderParams queries.

Cache results from FontRenderParams queries that request
font families. Previously, only family-less queries were
cached.

BUG=chromium:424082

Review URL: https://codereview.chromium.org/639123005

Cr-Commit-Position: refs/heads/master@{#299980}
parent af2a117b
...@@ -29,13 +29,25 @@ namespace { ...@@ -29,13 +29,25 @@ namespace {
float device_scale_factor_for_internal_display = 1.0f; float device_scale_factor_for_internal_display = 1.0f;
#endif #endif
// Keyed by hashes of FontRenderParamQuery structs from
// HashFontRenderParamsQuery().
typedef base::MRUCache<uint32, FontRenderParams> Cache;
// Number of recent GetFontRenderParams() results to cache. // Number of recent GetFontRenderParams() results to cache.
const size_t kCacheSize = 20; const size_t kCacheSize = 20;
// Cached result from a call to GetFontRenderParams().
struct QueryResult {
QueryResult(const FontRenderParams& params, const std::string& family)
: params(params),
family(family) {
}
~QueryResult() {}
FontRenderParams params;
std::string family;
};
// Keyed by hashes of FontRenderParamQuery structs from
// HashFontRenderParamsQuery().
typedef base::MRUCache<uint32, QueryResult> Cache;
// A cache and the lock that must be held while accessing it. // A cache and the lock that must be held while accessing it.
// GetFontRenderParams() is called by both the UI thread and the sandbox IPC // GetFontRenderParams() is called by both the UI thread and the sandbox IPC
// thread. // thread.
...@@ -168,21 +180,24 @@ uint32 HashFontRenderParamsQuery(const FontRenderParamsQuery& query) { ...@@ -168,21 +180,24 @@ uint32 HashFontRenderParamsQuery(const FontRenderParamsQuery& query) {
FontRenderParams GetFontRenderParams(const FontRenderParamsQuery& query, FontRenderParams GetFontRenderParams(const FontRenderParamsQuery& query,
std::string* family_out) { std::string* family_out) {
const uint32 hash = HashFontRenderParamsQuery(query); const uint32 hash = HashFontRenderParamsQuery(query);
if (!family_out) { SynchronizedCache* synchronized_cache = g_synchronized_cache.Pointer();
// The family returned by Fontconfig isn't part of FontRenderParams, so we
// can only return a value from the cache if it wasn't requested. {
SynchronizedCache* synchronized_cache = g_synchronized_cache.Pointer(); // Try to find a cached result so Fontconfig doesn't need to be queried.
base::AutoLock lock(synchronized_cache->lock); base::AutoLock lock(synchronized_cache->lock);
Cache::const_iterator it = synchronized_cache->cache.Get(hash); Cache::const_iterator it = synchronized_cache->cache.Get(hash);
if (it != synchronized_cache->cache.end()) { if (it != synchronized_cache->cache.end()) {
DVLOG(1) << "Returning cached params for " << hash; DVLOG(1) << "Returning cached params for " << hash;
return it->second; const QueryResult& result = it->second;
if (family_out)
*family_out = result.family;
return result.params;
} }
} else {
family_out->clear();
} }
DVLOG(1) << "Computing params for " << hash
<< (family_out ? " (family requested)" : ""); DVLOG(1) << "Computing params for " << hash;
if (family_out)
family_out->clear();
// Start with the delegate's settings, but let Fontconfig have the final say. // Start with the delegate's settings, but let Fontconfig have the final say.
FontRenderParams params; FontRenderParams params;
...@@ -215,12 +230,13 @@ FontRenderParams GetFontRenderParams(const FontRenderParamsQuery& query, ...@@ -215,12 +230,13 @@ FontRenderParams GetFontRenderParams(const FontRenderParamsQuery& query,
if (family_out && family_out->empty() && !query.families.empty()) if (family_out && family_out->empty() && !query.families.empty())
*family_out = query.families[0]; *family_out = query.families[0];
// Store the computed struct. It's fine if this overwrites a struct that was {
// cached by a different thread in the meantime; the values should be // Store the result. It's fine if this overwrites a result that was cached
// identical. // by a different thread in the meantime; the values should be identical.
SynchronizedCache* synchronized_cache = g_synchronized_cache.Pointer(); base::AutoLock lock(synchronized_cache->lock);
base::AutoLock lock(synchronized_cache->lock); synchronized_cache->cache.Put(hash,
synchronized_cache->cache.Put(hash, params); QueryResult(params, family_out ? *family_out : std::string()));
}
return params; return params;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment