Commit 257fb2f7 authored by Dominik Röttsches's avatar Dominik Röttsches Committed by Commit Bot

Use HbScoped in Blink platform

Now that we have scope helpers defined in a shared location for typical
HarfBuzz types, use those in Blink. In order to be able to do that for
HarfBuzzFontCache, FontGlobalContext needs to forward-declare and
isolate HarfBuzzFontCache from leaking outside the header, otherwise all
clients need to be made aware of HarfBuzz types.

Bug: 1049527
Change-Id: Ic2e6a3fec01a4a6f22eb879d77615a986dc4b987
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2041471
Commit-Queue: Dominik Röttsches <drott@chromium.org>
Reviewed-by: default avatarDaniel Hosseinian <dhoss@chromium.org>
Reviewed-by: default avatarKoji Ishii <kojii@chromium.org>
Cr-Commit-Position: refs/heads/master@{#739318}
parent 0f56c25a
...@@ -1522,6 +1522,7 @@ jumbo_component("platform") { ...@@ -1522,6 +1522,7 @@ jumbo_component("platform") {
"//third_party/blink/public/strings", "//third_party/blink/public/strings",
"//third_party/ced", "//third_party/ced",
"//third_party/emoji-segmenter", "//third_party/emoji-segmenter",
"//third_party/harfbuzz-ng:hb_scoped_util",
"//third_party/icu", "//third_party/icu",
"//third_party/libyuv", "//third_party/libyuv",
"//third_party/webrtc_overrides:webrtc_component", "//third_party/webrtc_overrides:webrtc_component",
......
...@@ -32,6 +32,15 @@ FontUniqueNameLookup* FontGlobalContext::GetFontUniqueNameLookup() { ...@@ -32,6 +32,15 @@ FontUniqueNameLookup* FontGlobalContext::GetFontUniqueNameLookup() {
return Get()->font_unique_name_lookup_.get(); return Get()->font_unique_name_lookup_.get();
} }
HarfBuzzFontCache* FontGlobalContext::GetHarfBuzzFontCache() {
std::unique_ptr<HarfBuzzFontCache>& global_context_harfbuzz_font_cache =
Get()->harfbuzz_font_cache_;
if (!global_context_harfbuzz_font_cache) {
global_context_harfbuzz_font_cache = std::make_unique<HarfBuzzFontCache>();
}
return global_context_harfbuzz_font_cache.get();
}
void FontGlobalContext::ClearMemory() { void FontGlobalContext::ClearMemory() {
if (!Get(kDoNotCreate)) if (!Get(kDoNotCreate))
return; return;
......
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_FONT_GLOBAL_CONTEXT_H_ #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_FONT_GLOBAL_CONTEXT_H_
#include "third_party/blink/renderer/platform/fonts/font_cache.h" #include "third_party/blink/renderer/platform/fonts/font_cache.h"
#include "third_party/blink/renderer/platform/fonts/shaping/harfbuzz_font_cache.h"
#include "third_party/blink/renderer/platform/platform_export.h" #include "third_party/blink/renderer/platform/platform_export.h"
#include "third_party/blink/renderer/platform/text/layout_locale.h" #include "third_party/blink/renderer/platform/text/layout_locale.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h" #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
...@@ -17,6 +16,7 @@ namespace blink { ...@@ -17,6 +16,7 @@ namespace blink {
class FontCache; class FontCache;
class FontUniqueNameLookup; class FontUniqueNameLookup;
class HarfBuzzFontCache;
enum CreateIfNeeded { kDoNotCreate, kCreate }; enum CreateIfNeeded { kDoNotCreate, kCreate };
...@@ -30,9 +30,7 @@ class PLATFORM_EXPORT FontGlobalContext { ...@@ -30,9 +30,7 @@ class PLATFORM_EXPORT FontGlobalContext {
static inline FontCache& GetFontCache() { return Get()->font_cache_; } static inline FontCache& GetFontCache() { return Get()->font_cache_; }
static inline HarfBuzzFontCache& GetHarfBuzzFontCache() { static HarfBuzzFontCache* GetHarfBuzzFontCache();
return Get()->harfbuzz_font_cache_;
}
static hb_font_funcs_t* GetHarfBuzzFontFuncs() { static hb_font_funcs_t* GetHarfBuzzFontFuncs() {
return Get()->harfbuzz_font_funcs_; return Get()->harfbuzz_font_funcs_;
...@@ -54,7 +52,7 @@ class PLATFORM_EXPORT FontGlobalContext { ...@@ -54,7 +52,7 @@ class PLATFORM_EXPORT FontGlobalContext {
~FontGlobalContext(); ~FontGlobalContext();
FontCache font_cache_; FontCache font_cache_;
HarfBuzzFontCache harfbuzz_font_cache_; std::unique_ptr<HarfBuzzFontCache> harfbuzz_font_cache_;
hb_font_funcs_t* harfbuzz_font_funcs_; hb_font_funcs_t* harfbuzz_font_funcs_;
std::unique_ptr<FontUniqueNameLookup> font_unique_name_lookup_; std::unique_ptr<FontUniqueNameLookup> font_unique_name_lookup_;
......
...@@ -4,32 +4,21 @@ ...@@ -4,32 +4,21 @@
#include "third_party/blink/renderer/platform/fonts/opentype/font_format_check.h" #include "third_party/blink/renderer/platform/fonts/opentype/font_format_check.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"
#include "third_party/skia/include/core/SkTypeface.h"
// Include HarfBuzz to have a cross-platform way to retrieve table tags without // Include HarfBuzz to have a cross-platform way to retrieve table tags without
// having to rely on the platform being able to instantiate this font format. // having to rely on the platform being able to instantiate this font format.
#include <hb.h> #include <hb.h>
namespace blink { #include "third_party/blink/renderer/platform/wtf/vector.h"
#include "third_party/harfbuzz-ng/utils/hb_scoped.h"
namespace { #include "third_party/skia/include/core/SkTypeface.h"
struct HarfbuzzBlobDestroyer {
inline void operator()(hb_blob_t* blob) { hb_blob_destroy(blob); }
};
struct HarfbuzzFaceDestroyer { namespace blink {
inline void operator()(hb_face_t* face) { hb_face_destroy(face); }
};
} // namespace
FontFormatCheck::FontFormatCheck(sk_sp<SkData> sk_data) { FontFormatCheck::FontFormatCheck(sk_sp<SkData> sk_data) {
std::unique_ptr<hb_blob_t, HarfbuzzBlobDestroyer> font_blob(hb_blob_create( HbScoped<hb_blob_t> font_blob(hb_blob_create(
reinterpret_cast<const char*>(sk_data->bytes()), sk_data->size(), reinterpret_cast<const char*>(sk_data->bytes()), sk_data->size(),
HB_MEMORY_MODE_READONLY, nullptr, nullptr)); HB_MEMORY_MODE_READONLY, nullptr, nullptr));
std::unique_ptr<hb_face_t, HarfbuzzFaceDestroyer> face( HbScoped<hb_face_t> face(hb_face_create(font_blob.get(), 0));
hb_face_create(font_blob.get(), 0));
unsigned table_count = 0; unsigned table_count = 0;
table_count = hb_face_get_table_tags(face.get(), 0, nullptr, nullptr); table_count = hb_face_get_table_tags(face.get(), 0, nullptr, nullptr);
......
...@@ -2,10 +2,13 @@ ...@@ -2,10 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "third_party/blink/renderer/platform/fonts/opentype/open_type_caps_support.h" // clang-format off
#include <hb-aat.h>
#include <hb.h> #include <hb.h>
#include <hb-aat.h>
// clang-format on
#include "third_party/blink/renderer/platform/fonts/opentype/open_type_caps_support.h"
#include "third_party/harfbuzz-ng/utils/hb_scoped.h"
namespace blink { namespace blink {
...@@ -138,12 +141,10 @@ OpenTypeCapsSupport::FontFormat OpenTypeCapsSupport::GetFontFormat() const { ...@@ -138,12 +141,10 @@ OpenTypeCapsSupport::FontFormat OpenTypeCapsSupport::GetFontFormat() const {
hb_face_t* hb_face = hb_font_get_face( hb_face_t* hb_face = hb_font_get_face(
harfbuzz_face_->GetScaledFont(nullptr, HarfBuzzFace::NoVerticalLayout)); harfbuzz_face_->GetScaledFont(nullptr, HarfBuzzFace::NoVerticalLayout));
std::unique_ptr<hb_blob_t, decltype(&hb_blob_destroy)> morx_blob( HbScoped<hb_blob_t> morx_blob(
hb_face_reference_table(hb_face, HB_TAG('m', 'o', 'r', 'x')), hb_face_reference_table(hb_face, HB_TAG('m', 'o', 'r', 'x')));
hb_blob_destroy); HbScoped<hb_blob_t> mort_blob(
std::unique_ptr<hb_blob_t, decltype(&hb_blob_destroy)> mort_blob( hb_face_reference_table(hb_face, HB_TAG('m', 'o', 'r', 't')));
hb_face_reference_table(hb_face, HB_TAG('m', 'o', 'r', 't')),
hb_blob_destroy);
// TODO(crbug.com/911149): Use hb_aat_layout_has_substitution() for // TODO(crbug.com/911149): Use hb_aat_layout_has_substitution() for
// has_morx_or_mort and hb_ot_layout_has_substitution() for has_gsub once is // has_morx_or_mort and hb_ot_layout_has_substitution() for has_gsub once is
......
...@@ -30,8 +30,10 @@ ...@@ -30,8 +30,10 @@
#include "third_party/blink/renderer/platform/fonts/shaping/harfbuzz_face.h" #include "third_party/blink/renderer/platform/fonts/shaping/harfbuzz_face.h"
#include <hb-ot.h> // clang-format off
#include <hb.h> #include <hb.h>
#include <hb-ot.h>
// clang-format on
#include <memory> #include <memory>
...@@ -50,6 +52,7 @@ ...@@ -50,6 +52,7 @@
#include "third_party/blink/renderer/platform/wtf/hash_map.h" #include "third_party/blink/renderer/platform/wtf/hash_map.h"
#include "third_party/blink/renderer/platform/wtf/math_extras.h" #include "third_party/blink/renderer/platform/wtf/math_extras.h"
#include "third_party/blink/renderer/platform/wtf/std_lib_extras.h" #include "third_party/blink/renderer/platform/wtf/std_lib_extras.h"
#include "third_party/harfbuzz-ng/utils/hb_scoped.h"
#include "third_party/skia/include/core/SkPaint.h" #include "third_party/skia/include/core/SkPaint.h"
#include "third_party/skia/include/core/SkPath.h" #include "third_party/skia/include/core/SkPath.h"
#include "third_party/skia/include/core/SkPoint.h" #include "third_party/skia/include/core/SkPoint.h"
...@@ -59,33 +62,14 @@ ...@@ -59,33 +62,14 @@
namespace blink { namespace blink {
void HbFontDeleter::operator()(hb_font_t* font) {
if (font)
hb_font_destroy(font);
}
void HbFaceDeleter::operator()(hb_face_t* face) {
if (face)
hb_face_destroy(face);
}
struct HbSetDeleter {
void operator()(hb_set_t* set) {
if (set)
hb_set_destroy(set);
}
};
using HbSetUniquePtr = std::unique_ptr<hb_set_t, HbSetDeleter>;
static scoped_refptr<HbFontCacheEntry> CreateHbFontCacheEntry(hb_face_t*); static scoped_refptr<HbFontCacheEntry> CreateHbFontCacheEntry(hb_face_t*);
HarfBuzzFace::HarfBuzzFace(FontPlatformData* platform_data, uint64_t unique_id) HarfBuzzFace::HarfBuzzFace(FontPlatformData* platform_data, uint64_t unique_id)
: platform_data_(platform_data), unique_id_(unique_id) { : platform_data_(platform_data), unique_id_(unique_id) {
HarfBuzzFontCache::AddResult result = HarfBuzzFontCache::AddResult result =
FontGlobalContext::GetHarfBuzzFontCache().insert(unique_id_, nullptr); FontGlobalContext::GetHarfBuzzFontCache()->insert(unique_id_, nullptr);
if (result.is_new_entry) { if (result.is_new_entry) {
HbFaceUniquePtr face(CreateFace()); HbScoped<hb_face_t> face(CreateFace());
result.stored_value->value = CreateHbFontCacheEntry(face.get()); result.stored_value->value = CreateHbFontCacheEntry(face.get());
} }
result.stored_value->value->AddRef(); result.stored_value->value->AddRef();
...@@ -94,13 +78,14 @@ HarfBuzzFace::HarfBuzzFace(FontPlatformData* platform_data, uint64_t unique_id) ...@@ -94,13 +78,14 @@ HarfBuzzFace::HarfBuzzFace(FontPlatformData* platform_data, uint64_t unique_id)
} }
HarfBuzzFace::~HarfBuzzFace() { HarfBuzzFace::~HarfBuzzFace() {
HarfBuzzFontCache::iterator result = HarfBuzzFontCache* harfbuzz_font_cache =
FontGlobalContext::GetHarfBuzzFontCache().find(unique_id_); FontGlobalContext::GetHarfBuzzFontCache();
SECURITY_DCHECK(result != FontGlobalContext::GetHarfBuzzFontCache().end()); HarfBuzzFontCache::iterator result = harfbuzz_font_cache->find(unique_id_);
SECURITY_DCHECK(result != harfbuzz_font_cache->end());
DCHECK(!result.Get()->value->HasOneRef()); DCHECK(!result.Get()->value->HasOneRef());
result.Get()->value->Release(); result.Get()->value->Release();
if (result.Get()->value->HasOneRef()) if (result.Get()->value->HasOneRef())
FontGlobalContext::GetHarfBuzzFontCache().erase(unique_id_); harfbuzz_font_cache->erase(unique_id_);
} }
static hb_bool_t HarfBuzzGetGlyph(hb_font_t* hb_font, static hb_bool_t HarfBuzzGetGlyph(hb_font_t* hb_font,
...@@ -228,7 +213,7 @@ bool HarfBuzzFace::HasSpaceInLigaturesOrKerning(TypesettingFeatures features) { ...@@ -228,7 +213,7 @@ bool HarfBuzzFace::HasSpaceInLigaturesOrKerning(TypesettingFeatures features) {
const hb_codepoint_t kInvalidCodepoint = static_cast<hb_codepoint_t>(-1); const hb_codepoint_t kInvalidCodepoint = static_cast<hb_codepoint_t>(-1);
hb_codepoint_t space = kInvalidCodepoint; hb_codepoint_t space = kInvalidCodepoint;
HbSetUniquePtr glyphs(hb_set_create()); HbScoped<hb_set_t> glyphs(hb_set_create());
// Check whether computing is needed and compute for gpos/gsub. // Check whether computing is needed and compute for gpos/gsub.
if (features & kKerning && if (features & kKerning &&
...@@ -360,11 +345,10 @@ hb_face_t* HarfBuzzFace::CreateFace() { ...@@ -360,11 +345,10 @@ hb_face_t* HarfBuzzFace::CreateFace() {
if (tf_stream && tf_stream->getMemoryBase()) { if (tf_stream && tf_stream->getMemoryBase()) {
const void* tf_memory = tf_stream->getMemoryBase(); const void* tf_memory = tf_stream->getMemoryBase();
size_t tf_size = tf_stream->getLength(); size_t tf_size = tf_stream->getLength();
std::unique_ptr<hb_blob_t, void (*)(hb_blob_t*)> face_blob( HbScoped<hb_blob_t> face_blob(
hb_blob_create(reinterpret_cast<const char*>(tf_memory), hb_blob_create(reinterpret_cast<const char*>(tf_memory),
SafeCast<unsigned int>(tf_size), HB_MEMORY_MODE_READONLY, SafeCast<unsigned int>(tf_size), HB_MEMORY_MODE_READONLY,
tf_stream.release(), DeleteTypefaceStream), tf_stream.release(), DeleteTypefaceStream));
hb_blob_destroy);
face = hb_face_create(face_blob.get(), ttc_index); face = hb_face_create(face_blob.get(), ttc_index);
} }
#endif #endif
...@@ -383,7 +367,7 @@ hb_face_t* HarfBuzzFace::CreateFace() { ...@@ -383,7 +367,7 @@ hb_face_t* HarfBuzzFace::CreateFace() {
} }
scoped_refptr<HbFontCacheEntry> CreateHbFontCacheEntry(hb_face_t* face) { scoped_refptr<HbFontCacheEntry> CreateHbFontCacheEntry(hb_face_t* face) {
HbFontUniquePtr ot_font(hb_font_create(face)); HbScoped<hb_font_t> ot_font(hb_font_create(face));
hb_ot_font_set_funcs(ot_font.get()); hb_ot_font_set_funcs(ot_font.get());
// Creating a sub font means that non-available functions // Creating a sub font means that non-available functions
// are found from the parent. // are found from the parent.
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
namespace blink { namespace blink {
HbFontCacheEntry::HbFontCacheEntry(hb_font_t* font) HbFontCacheEntry::HbFontCacheEntry(hb_font_t* font)
: hb_font_(HbFontUniquePtr(font)), : hb_font_(HbScoped<hb_font_t>(font)),
hb_font_data_(std::make_unique<HarfBuzzFontData>()) {} hb_font_data_(std::make_unique<HarfBuzzFontData>()) {}
HbFontCacheEntry::~HbFontCacheEntry() = default; HbFontCacheEntry::~HbFontCacheEntry() = default;
......
...@@ -5,30 +5,18 @@ ...@@ -5,30 +5,18 @@
#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_SHAPING_HARFBUZZ_FONT_CACHE_H_ #ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_SHAPING_HARFBUZZ_FONT_CACHE_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_SHAPING_HARFBUZZ_FONT_CACHE_H_ #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_SHAPING_HARFBUZZ_FONT_CACHE_H_
#include <hb.h>
#include <memory> #include <memory>
#include "third_party/blink/renderer/platform/fonts/font_metrics.h" #include "third_party/blink/renderer/platform/fonts/font_metrics.h"
#include "third_party/blink/renderer/platform/fonts/unicode_range_set.h" #include "third_party/blink/renderer/platform/fonts/unicode_range_set.h"
#include "third_party/harfbuzz-ng/utils/hb_scoped.h"
struct hb_font_t;
struct hb_face_t;
namespace blink { namespace blink {
struct HarfBuzzFontData; struct HarfBuzzFontData;
struct HbFontDeleter {
void operator()(hb_font_t* font);
};
using HbFontUniquePtr = std::unique_ptr<hb_font_t, HbFontDeleter>;
struct HbFaceDeleter {
void operator()(hb_face_t* face);
};
using HbFaceUniquePtr = std::unique_ptr<hb_face_t, HbFaceDeleter>;
// Though we have FontCache class, which provides the cache mechanism for // Though we have FontCache class, which provides the cache mechanism for
// WebKit's font objects, we also need additional caching layer for HarfBuzz to // WebKit's font objects, we also need additional caching layer for HarfBuzz to
// reduce the number of hb_font_t objects created. Without it, we would create // reduce the number of hb_font_t objects created. Without it, we would create
...@@ -50,15 +38,17 @@ class HbFontCacheEntry : public RefCounted<HbFontCacheEntry> { ...@@ -50,15 +38,17 @@ class HbFontCacheEntry : public RefCounted<HbFontCacheEntry> {
private: private:
explicit HbFontCacheEntry(hb_font_t* font); explicit HbFontCacheEntry(hb_font_t* font);
HbFontUniquePtr hb_font_; HbScoped<hb_font_t> hb_font_;
std::unique_ptr<HarfBuzzFontData> hb_font_data_; std::unique_ptr<HarfBuzzFontData> hb_font_data_;
}; };
typedef HashMap<uint64_t, // Declare as derived class in order to be able to forward-declare it as class
scoped_refptr<HbFontCacheEntry>, // in FontGlobalContext.
WTF::IntHash<uint64_t>, class HarfBuzzFontCache
WTF::UnsignedWithZeroKeyHashTraits<uint64_t>> : public HashMap<uint64_t,
HarfBuzzFontCache; scoped_refptr<HbFontCacheEntry>,
WTF::IntHash<uint64_t>,
WTF::UnsignedWithZeroKeyHashTraits<uint64_t>> {};
} // namespace blink } // namespace blink
......
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