diff options
author | derat@chromium.org <derat@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-08-12 01:01:36 +0000 |
---|---|---|
committer | derat@chromium.org <derat@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-08-12 01:01:36 +0000 |
commit | 8162cae4faf2a4d82d4c5875abada81dde5fb55d (patch) | |
tree | 8fefc982f81599f3f4f41451d3c9aef756c31cbf | |
parent | 321c469eca891b9a34ebccf0e921d21fcfefee90 (diff) | |
download | chromium_src-8162cae4faf2a4d82d4c5875abada81dde5fb55d.zip chromium_src-8162cae4faf2a4d82d4c5875abada81dde5fb55d.tar.gz chromium_src-8162cae4faf2a4d82d4c5875abada81dde5fb55d.tar.bz2 |
linux: Synchronize access to FontRenderParams cache.
Also clean up cache-clearing (used by tests).
BUG=401993
Review URL: https://codereview.chromium.org/458593002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@288855 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | ui/gfx/font_render_params_linux.cc | 49 |
1 files changed, 33 insertions, 16 deletions
diff --git a/ui/gfx/font_render_params_linux.cc b/ui/gfx/font_render_params_linux.cc index 507c793..f02636e 100644 --- a/ui/gfx/font_render_params_linux.cc +++ b/ui/gfx/font_render_params_linux.cc @@ -7,10 +7,12 @@ #include "base/command_line.h" #include "base/containers/mru_cache.h" #include "base/hash.h" +#include "base/lazy_instance.h" #include "base/logging.h" #include "base/macros.h" #include "base/strings/string_util.h" #include "base/strings/stringprintf.h" +#include "base/synchronization/lock.h" #include "ui/gfx/font.h" #include "ui/gfx/linux_font_delegate.h" #include "ui/gfx/switches.h" @@ -21,12 +23,25 @@ namespace gfx { namespace { -// Should cache entries by dropped by the next call to GetFontRenderParams()? -// Used for tests. -bool g_clear_cache_for_test = false; +// Keyed by hashes of FontRenderParamQuery structs from +// HashFontRenderParamsQuery(). +typedef base::MRUCache<uint32, FontRenderParams> Cache; // Number of recent GetFontRenderParams() results to cache. -const size_t kCacheSize = 10; +const size_t kCacheSize = 20; + +// A cache and the lock that must be held while accessing it. +// GetFontRenderParams() is called by both the UI thread and the sandbox IPC +// thread. +struct SynchronizedCache { + SynchronizedCache() : cache(kCacheSize) {} + + base::Lock lock; + Cache cache; +}; + +base::LazyInstance<SynchronizedCache>::Leaky g_synchronized_cache = + LAZY_INSTANCE_INITIALIZER; // Converts Fontconfig FC_HINT_STYLE to FontRenderParams::Hinting. FontRenderParams::Hinting ConvertFontconfigHintStyle(int hint_style) { @@ -138,20 +153,14 @@ uint32 HashFontRenderParamsQuery(const FontRenderParamsQuery& query) { FontRenderParams GetFontRenderParams(const FontRenderParamsQuery& query, std::string* family_out) { - typedef base::MRUCache<uint32, FontRenderParams> Cache; - CR_DEFINE_STATIC_LOCAL(Cache, cache, (kCacheSize)); - - if (g_clear_cache_for_test) { - cache.Clear(); - g_clear_cache_for_test = false; - } - const uint32 hash = HashFontRenderParamsQuery(query); if (!family_out) { // 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. - Cache::const_iterator it = cache.Get(hash); - if (it != cache.end()) { + SynchronizedCache* synchronized_cache = g_synchronized_cache.Pointer(); + base::AutoLock lock(synchronized_cache->lock); + Cache::const_iterator it = synchronized_cache->cache.Get(hash); + if (it != synchronized_cache->cache.end()) { DVLOG(1) << "Returning cached params for " << hash; return it->second; } @@ -192,12 +201,20 @@ FontRenderParams GetFontRenderParams(const FontRenderParamsQuery& query, if (family_out && family_out->empty() && !query.families.empty()) *family_out = query.families[0]; - cache.Put(hash, params); + // 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 + // identical. + SynchronizedCache* synchronized_cache = g_synchronized_cache.Pointer(); + base::AutoLock lock(synchronized_cache->lock); + synchronized_cache->cache.Put(hash, params); + return params; } void ClearFontRenderParamsCacheForTest() { - g_clear_cache_for_test = true; + SynchronizedCache* synchronized_cache = g_synchronized_cache.Pointer(); + base::AutoLock lock(synchronized_cache->lock); + synchronized_cache->cache.Clear(); } } // namespace gfx |