Commit 6b949cce authored by Dominik Röttsches's avatar Dominik Röttsches Committed by Commit Bot

Replace FallbackLruCache with generic LruCache from WTF

LruCache was added to WTF which allows us to remove the specific
instance that was added for OOP font fallback caching.

Bug: 1005234
Change-Id: Id20e7fb0b99128a4c3d5c4edc5bed5ffe881a17d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2050314Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Reviewed-by: default avatarKoji Ishii <kojii@chromium.org>
Commit-Queue: Dominik Röttsches <drott@chromium.org>
Cr-Commit-Position: refs/heads/master@{#740392}
parent 7baafcbe
......@@ -709,8 +709,6 @@ jumbo_component("platform") {
"fonts/win/dwrite_font_format_support.h",
"fonts/win/fallback_family_style_cache_win.cc",
"fonts/win/fallback_family_style_cache_win.h",
"fonts/win/fallback_lru_cache_win.cc",
"fonts/win/fallback_lru_cache_win.h",
"fonts/win/font_cache_skia_win.cc",
"fonts/win/font_fallback_win.cc",
"fonts/win/font_fallback_win.h",
......@@ -1917,8 +1915,10 @@ jumbo_source_set("blink_platform_unittests_sources") {
]
if (is_win) {
sources += [ "text/locale_win_test.cc" ]
sources += [ "fonts/win/fallback_lru_cache_win_test.cc" ]
sources += [
"fonts/win/fallback_lru_cache_win_test.cc",
"text/locale_win_test.cc",
]
} else if (is_mac) {
sources += [
"fonts/opentype/open_type_caps_support_test.mm",
......
......@@ -49,12 +49,11 @@ void FallbackFamilyStyleCache::Put(
String cache_key =
makeCacheKey(generic_family, bcp47_language_tag, fallback_priority);
FallbackLruCache::TypefaceVector* existing_typefaces =
recent_fallback_fonts_.Get(cache_key);
TypefaceVector* existing_typefaces = recent_fallback_fonts_.Get(cache_key);
if (existing_typefaces) {
existing_typefaces->insert(0, sk_ref_sp(typeface));
} else {
FallbackLruCache::TypefaceVector typefaces;
TypefaceVector typefaces;
typefaces.push_back(sk_ref_sp(typeface));
recent_fallback_fonts_.Put(std::move(cache_key), std::move(typefaces));
}
......@@ -67,7 +66,7 @@ void FallbackFamilyStyleCache::Get(
UChar32 character,
String* fallback_family,
SkFontStyle* fallback_style) {
FallbackLruCache::TypefaceVector* typefaces = recent_fallback_fonts_.Get(
TypefaceVector* typefaces = recent_fallback_fonts_.Get(
makeCacheKey(generic_family, bcp47_language_tag, fallback_priority));
if (!typefaces)
return;
......
......@@ -7,12 +7,15 @@
#include "third_party/blink/renderer/platform/fonts/font_description.h"
#include "third_party/blink/renderer/platform/fonts/font_fallback_priority.h"
#include "third_party/blink/renderer/platform/fonts/win/fallback_lru_cache_win.h"
#include "third_party/blink/renderer/platform/wtf/lru_cache.h"
#include "third_party/skia/include/core/SkRefCnt.h"
#include "third_party/skia/include/core/SkTypeface.h"
namespace blink {
using TypefaceVector = Vector<sk_sp<SkTypeface>>;
using FallbackLruCache = WTF::LruCache<String, TypefaceVector>;
class FallbackFamilyStyleCache {
USING_FAST_MALLOC(FallbackFamilyStyleCache);
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/platform/fonts/win/fallback_lru_cache_win.h"
#include "third_party/blink/renderer/platform/wtf/text/string_hash.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
namespace blink {
FallbackLruCache::FallbackLruCache(size_t max_size) : max_size_(max_size) {
DCHECK_GT(max_size_, 0u);
}
FallbackLruCache::TypefaceVector* FallbackLruCache::Get(const String& key) {
HashMapType::iterator find_result = map_.find(key);
if (find_result == map_.end())
return nullptr;
// Move result to beginning of list.
KeyListNode* node = find_result->value.ListNode();
ordering_.Remove(node);
ordering_.Push(node);
return find_result->value.value();
}
void FallbackLruCache::Put(String&& key, TypefaceVector&& arg) {
HashMapType::iterator find_result = map_.find(key);
if (find_result != map_.end()) {
ordering_.Remove(find_result->value.ListNode());
map_.erase(find_result);
}
if (map_.size() >= max_size_) {
RemoveLeastRecentlyUsed();
}
std::unique_ptr<KeyListNode> list_node = std::make_unique<KeyListNode>(key);
HashMapType::AddResult add_result = map_.insert(
std::move(key), MappedWithListNode(std::move(arg), std::move(list_node)));
DCHECK(add_result.is_new_entry);
ordering_.Push(add_result.stored_value->value.ListNode());
}
void FallbackLruCache::Clear() {
map_.clear();
ordering_.Clear();
}
void FallbackLruCache::RemoveLeastRecentlyUsed() {
KeyListNode* tail = ordering_.Tail();
ordering_.Remove(tail);
map_.erase(tail->key());
}
} // namespace blink
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/platform/platform_export.h"
#include "third_party/blink/renderer/platform/wtf/doubly_linked_list.h"
#include "third_party/blink/renderer/platform/wtf/hash_map.h"
#include "third_party/blink/renderer/platform/wtf/hash_table_deleted_value_type.h"
#include "third_party/blink/renderer/platform/wtf/text/string_hash.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "third_party/skia/include/core/SkRefCnt.h"
#include "third_party/skia/include/core/SkTypeface.h"
#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_WIN_FALLBACK_LRU_CACHE_WIN_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_WIN_FALLBACK_LRU_CACHE_WIN_H_
namespace blink {
/* A LRU cache for storing a a vector of typefaces for a particular key string,
* which would usually be a locale plus potential additional parameters. Uses a
* HashMap for storage and access and a DoublyLinkedList for managing age of
* entries. TODO(https://crbug.com/1010925): Potentially move this to a generic
* LRU Cache implementation once we have such in WTF. */
class PLATFORM_EXPORT FallbackLruCache {
USING_FAST_MALLOC(FallbackLruCache);
public:
FallbackLruCache(size_t max_size);
using TypefaceVector = Vector<sk_sp<SkTypeface>>;
TypefaceVector* Get(const String& key);
void Put(String&& key, TypefaceVector&& arg);
void Clear();
size_t size() const { return map_.size(); }
private:
class KeyListNode final : public DoublyLinkedListNode<KeyListNode> {
USING_FAST_MALLOC(KeyListNode);
public:
friend class DoublyLinkedListNode<KeyListNode>;
KeyListNode(const String& key) : key_(key) {}
const String& key() const { return key_; }
private:
String key_;
KeyListNode* prev_{nullptr};
KeyListNode* next_{nullptr};
};
class MappedWithListNode {
USING_FAST_MALLOC(MappedWithListNode);
public:
MappedWithListNode(TypefaceVector&& mapped_arg,
std::unique_ptr<KeyListNode>&& list_node)
: mapped_value_(std::move(mapped_arg)),
list_node_(std::move(list_node)) {}
MappedWithListNode(WTF::HashTableDeletedValueType) {
list_node_.reset(reinterpret_cast<KeyListNode*>(-1));
}
TypefaceVector* value() { return &mapped_value_; }
KeyListNode* ListNode() { return list_node_.get(); }
private:
TypefaceVector mapped_value_;
std::unique_ptr<KeyListNode> list_node_;
};
void RemoveLeastRecentlyUsed();
using HashMapType = HashMap<String,
MappedWithListNode,
DefaultHash<String>::Hash,
HashTraits<String>,
SimpleClassHashTraits<MappedWithListNode>>;
HashMapType map_;
DoublyLinkedList<KeyListNode> ordering_;
size_t max_size_;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_WIN_FALLBACK_LRU_CACHE_WIN_H_
......@@ -2,9 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/platform/fonts/win/fallback_lru_cache_win.h"
#include "third_party/blink/renderer/platform/fonts/win/fallback_family_style_cache_win.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/platform/wtf/text/character_names.h"
#include "third_party/blink/renderer/platform/wtf/text/string_hash.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "third_party/skia/include/core/SkFontMgr.h"
#include "third_party/skia/include/core/SkRefCnt.h"
#include "third_party/skia/include/core/SkTypeface.h"
......@@ -29,7 +32,7 @@ void fillCacheWithDummies(blink::FallbackLruCache& lru_cache,
const char* format_string,
size_t count) {
for (size_t i = 0; i < count; ++i) {
blink::FallbackLruCache::TypefaceVector dummy_typefaces;
blink::TypefaceVector dummy_typefaces;
dummy_typefaces.push_back(
SkTypeface::MakeFromName(kFontFamilyNameArial, SkFontStyle()));
lru_cache.Put(String::Format(format_string, i), std::move(dummy_typefaces));
......@@ -48,7 +51,7 @@ TEST(FallbackLruCacheTest, KeepChineseWhenFetched) {
// the Chinese font and ensure it's gone.
FallbackLruCache lru_cache(kLruCacheTestSize);
EXPECT_EQ(lru_cache.size(), 0u);
FallbackLruCache::TypefaceVector fallback_typefaces_zh;
TypefaceVector fallback_typefaces_zh;
fallback_typefaces_zh.push_back(
fallbackForLocale(kHanSimplifiedLocale, kFirstCJKIdeograph));
lru_cache.Put(kHanSimplifiedLocale, std::move(fallback_typefaces_zh));
......@@ -56,8 +59,7 @@ TEST(FallbackLruCacheTest, KeepChineseWhenFetched) {
EXPECT_EQ(lru_cache.size(), 1u);
fillCacheWithDummies(lru_cache, "dummy_locale_%zu", kLruCacheTestSize - 1);
FallbackLruCache::TypefaceVector* chinese_typefaces =
lru_cache.Get(kHanSimplifiedLocale);
TypefaceVector* chinese_typefaces = lru_cache.Get(kHanSimplifiedLocale);
EXPECT_TRUE(chinese_typefaces);
EXPECT_TRUE(chinese_typefaces->at(0)->unicharToGlyph(0x4E01));
EXPECT_EQ(lru_cache.size(), kLruCacheTestSize);
......
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