summaryrefslogtreecommitdiff
path: root/chromium-125-debian-bad-font-gc11.patch
blob: da3019974efcc38b10a9fac7a4e7f80ae040e60c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
Revert the following commit:

commit 2eefeabb12fb7e92f2508116a5ed959c57659be1
Author: Ian Kilpatrick <ikilpatrick@chromium.org>
Date:   Tue Feb 20 17:40:39 2024 +0000

    [gc] Make HarfBuzzFontData & friends gc'd.
    
    Previously we had a HbFontCacheEntry which was used to hold onto the
    HarfBuzzFontData, and a hb_font_t.
    
    HarfBuzzFontData is used for holding data specific for various
    harfbuzz callbacks, but we can also hold onto the hb_font_t there.
    
    There should be no user-visible behaviour change.
    
    Bug: 41490008
    Change-Id: Icaa7ad3b2f75e9807b88014a9a15406cb76eb52e
    Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5302175
    Reviewed-by: Dominik Röttsches <drott@chromium.org>
    Commit-Queue: Ian Kilpatrick <ikilpatrick@chromium.org>
    Cr-Commit-Position: refs/heads/main@{#1262752}

--- a/third_party/blink/renderer/platform/fonts/font_global_context.cc
+++ b/third_party/blink/renderer/platform/fonts/font_global_context.cc
@@ -8,6 +8,7 @@
 #include "third_party/blink/renderer/platform/fonts/font_cache.h"
 #include "third_party/blink/renderer/platform/fonts/font_unique_name_lookup.h"
 #include "third_party/blink/renderer/platform/fonts/shaping/harfbuzz_face.h"
+#include "third_party/blink/renderer/platform/fonts/shaping/harfbuzz_font_cache.h"
 #include "third_party/blink/renderer/platform/privacy_budget/identifiability_digest_helpers.h"
 #include "third_party/blink/renderer/platform/wtf/thread_specific.h"
 
@@ -50,6 +51,15 @@ FontUniqueNameLookup* FontGlobalContext:
   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;
+}
+
 IdentifiableToken FontGlobalContext::GetOrComputeTypefaceDigest(
     const FontPlatformData& source) {
   SkTypeface* typeface = source.Typeface();
--- a/third_party/blink/renderer/platform/fonts/font_global_context.h
+++ b/third_party/blink/renderer/platform/fonts/font_global_context.h
@@ -9,7 +9,6 @@
 #include "base/types/pass_key.h"
 #include "third_party/blink/public/common/privacy_budget/identifiable_token.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/text/layout_locale.h"
 #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
@@ -34,19 +33,14 @@ class PLATFORM_EXPORT FontGlobalContext
   static FontGlobalContext& Get();
   static FontGlobalContext* TryGet();
 
-  void Trace(Visitor* visitor) const {
-    visitor->Trace(font_cache_);
-    visitor->Trace(harfbuzz_font_cache_);
-  }
+  void Trace(Visitor* visitor) const { visitor->Trace(font_cache_); }
 
   FontGlobalContext(const FontGlobalContext&) = delete;
   FontGlobalContext& operator=(const FontGlobalContext&) = delete;
 
   static inline FontCache& GetFontCache() { return Get().font_cache_; }
 
-  static HarfBuzzFontCache& GetHarfBuzzFontCache() {
-    return Get().harfbuzz_font_cache_;
-  }
+  static HarfBuzzFontCache& GetHarfBuzzFontCache();
 
   static FontUniqueNameLookup* GetFontUniqueNameLookup();
 
@@ -62,7 +56,7 @@ class PLATFORM_EXPORT FontGlobalContext
 
  private:
   FontCache font_cache_;
-  HarfBuzzFontCache harfbuzz_font_cache_;
+  std::unique_ptr<HarfBuzzFontCache> harfbuzz_font_cache_;
   std::unique_ptr<FontUniqueNameLookup> font_unique_name_lookup_;
   base::HashingLRUCache<SkTypefaceID, IdentifiableToken> typeface_digest_cache_;
   base::HashingLRUCache<SkTypefaceID, IdentifiableToken>
--- a/third_party/blink/renderer/platform/fonts/shaping/harfbuzz_face.cc
+++ b/third_party/blink/renderer/platform/fonts/shaping/harfbuzz_face.cc
@@ -65,14 +65,20 @@
 
 HarfBuzzFace::HarfBuzzFace(const FontPlatformData* platform_data,
                            uint64_t unique_id)
-    : platform_data_(platform_data),
-      harfbuzz_font_data_(FontGlobalContext::GetHarfBuzzFontCache().GetOrCreate(
-          unique_id,
-          platform_data)) {}
+    : platform_data_(platform_data), unique_id_(unique_id) {
+  HbFontCacheEntry* const cache_entry =
+      FontGlobalContext::GetHarfBuzzFontCache().RefOrNew(unique_id_,
+                                                         platform_data);
+  unscaled_font_ = cache_entry->HbFont();
+  harfbuzz_font_data_ = cache_entry->HbFontData();
+}
+
+HarfBuzzFace::~HarfBuzzFace() {
+  FontGlobalContext::GetHarfBuzzFontCache().Remove(unique_id_);
+}
 
 void HarfBuzzFace::Trace(Visitor* visitor) const {
   visitor->Trace(platform_data_);
-  visitor->Trace(harfbuzz_font_data_);
 }
 
 bool HarfBuzzFace::ignore_variation_selectors_ = false;
@@ -234,17 +240,14 @@ bool HarfBuzzFace::HasSpaceInLigaturesOr
 
   hb::unique_ptr<hb_set_t> glyphs(hb_set_create());
 
-  hb_font_t* unscaled_font = harfbuzz_font_data_->unscaled_font_.get();
-
   // Check whether computing is needed and compute for gpos/gsub.
   if (features & kKerning &&
       harfbuzz_font_data_->space_in_gpos_ ==
           HarfBuzzFontData::SpaceGlyphInOpenTypeTables::kUnknown) {
-    if (space == kInvalidCodepoint && !GetSpaceGlyph(unscaled_font, space)) {
+    if (space == kInvalidCodepoint && !GetSpaceGlyph(unscaled_font_, space))
       return false;
-    }
     // Compute for gpos.
-    hb_face_t* face = hb_font_get_face(unscaled_font);
+    hb_face_t* face = hb_font_get_face(unscaled_font_);
     DCHECK(face);
     harfbuzz_font_data_->space_in_gpos_ =
         hb_ot_layout_has_positioning(face) &&
@@ -258,11 +261,10 @@ bool HarfBuzzFace::HasSpaceInLigaturesOr
   if (features & kLigatures &&
       harfbuzz_font_data_->space_in_gsub_ ==
           HarfBuzzFontData::SpaceGlyphInOpenTypeTables::kUnknown) {
-    if (space == kInvalidCodepoint && !GetSpaceGlyph(unscaled_font, space)) {
+    if (space == kInvalidCodepoint && !GetSpaceGlyph(unscaled_font_, space))
       return false;
-    }
     // Compute for gpos.
-    hb_face_t* face = hb_font_get_face(unscaled_font);
+    hb_face_t* face = hb_font_get_face(unscaled_font_);
     DCHECK(face);
     harfbuzz_font_data_->space_in_gsub_ =
         hb_ot_layout_has_substitution(face) &&
@@ -280,14 +282,14 @@ bool HarfBuzzFace::HasSpaceInLigaturesOr
 }
 
 unsigned HarfBuzzFace::UnitsPerEmFromHeadTable() {
-  hb_face_t* face = hb_font_get_face(harfbuzz_font_data_->unscaled_font_.get());
+  hb_face_t* face = hb_font_get_face(unscaled_font_);
   return hb_face_get_upem(face);
 }
 
 Glyph HarfBuzzFace::HbGlyphForCharacter(UChar32 character) {
   hb_codepoint_t glyph = 0;
-  HarfBuzzGetNominalGlyph(harfbuzz_font_data_->unscaled_font_.get(),
-                          harfbuzz_font_data_, character, &glyph, nullptr);
+  HarfBuzzGetNominalGlyph(unscaled_font_, harfbuzz_font_data_, character,
+                          &glyph, nullptr);
   return glyph;
 }
 
@@ -329,7 +331,7 @@ hb_codepoint_t HarfBuzzFace::HarfBuzzGet
     UChar32 variation_selector) {
   DCHECK(RuntimeEnabledFeatures::FontVariationSequencesEnabled());
   hb_codepoint_t glyph = 0;
-  HarfBuzzGetGlyph(harfbuzz_font_data_->unscaled_font_.get(),
+  HarfBuzzGetGlyph(unscaled_font_,
                    harfbuzz_font_data_, character, variation_selector, &glyph,
                    nullptr);
   return glyph;
@@ -444,10 +446,9 @@ static hb::unique_ptr<hb_face_t> CreateF
   return face;
 }
 
-namespace {
-
-HarfBuzzFontData* CreateHarfBuzzFontData(hb_face_t* face,
-                                         SkTypeface* typeface) {
+static scoped_refptr<HbFontCacheEntry> CreateHbFontCacheEntry(
+    hb_face_t* face,
+    SkTypeface* typeface) {
   hb::unique_ptr<hb_font_t> ot_font(hb_font_create(face));
   hb_ot_font_set_funcs(ot_font.get());
 
@@ -466,26 +467,25 @@ HarfBuzzFontData* CreateHarfBuzzFontData
   // Creating a sub font means that non-available functions
   // are found from the parent.
   hb_font_t* const unscaled_font = hb_font_create_sub_font(ot_font.get());
-  HarfBuzzFontData* data =
-      MakeGarbageCollected<HarfBuzzFontData>(unscaled_font);
+  scoped_refptr<HbFontCacheEntry> cache_entry =
+      HbFontCacheEntry::Create(unscaled_font);
   hb_font_set_funcs(unscaled_font,
-                    HarfBuzzSkiaFontFuncs::Get().GetFunctions(typeface), data,
-                    nullptr);
-  return data;
+                    HarfBuzzSkiaFontFuncs::Get().GetFunctions(typeface),
+                    cache_entry->HbFontData(), nullptr);
+  return cache_entry;
 }
 
-}  // namespace
-
-HarfBuzzFontData* HarfBuzzFontCache::GetOrCreate(
+HbFontCacheEntry* HarfBuzzFontCache::RefOrNew(
     uint64_t unique_id,
     const FontPlatformData* platform_data) {
   const auto& result = font_map_.insert(unique_id, nullptr);
   if (result.is_new_entry) {
     hb::unique_ptr<hb_face_t> face = CreateFace(platform_data);
     result.stored_value->value =
-        CreateHarfBuzzFontData(face.get(), platform_data->Typeface());
+        CreateHbFontCacheEntry(face.get(), platform_data->Typeface());
   }
-  return result.stored_value->value.Get();
+  result.stored_value->value->AddRef();
+  return result.stored_value->value.get();
 }
 
 static_assert(
@@ -516,18 +516,17 @@ hb_font_t* HarfBuzzFace::GetScaledFont(s
                                                      vertical_layout);
 
   int scale = SkiaScalarToHarfBuzzPosition(platform_data_->size());
-  hb_font_t* unscaled_font = harfbuzz_font_data_->unscaled_font_.get();
-  hb_font_set_scale(unscaled_font, scale, scale);
+  hb_font_set_scale(unscaled_font_, scale, scale);
   // See contended discussion in https://github.com/harfbuzz/harfbuzz/pull/1484
   // Setting ptem here is critical for HarfBuzz to know where to lookup spacing
   // offset in the AAT trak table, the unit pt in ptem here means "CoreText"
   // points. After discussion on the pull request and with Apple developers, the
   // meaning of HarfBuzz' hb_font_set_ptem API was changed to expect the
   // equivalent of CSS pixels here.
-  hb_font_set_ptem(unscaled_font, specified_size > 0 ? specified_size
-                                                     : platform_data_->size());
+  hb_font_set_ptem(unscaled_font_, specified_size > 0 ? specified_size
+                                                      : platform_data_->size());
 
-  return unscaled_font;
+  return unscaled_font_;
 }
 
 hb_font_t* HarfBuzzFace::GetScaledFont() const {
--- a/third_party/blink/renderer/platform/fonts/shaping/harfbuzz_face.h
+++ b/third_party/blink/renderer/platform/fonts/shaping/harfbuzz_face.h
@@ -55,6 +55,7 @@ class HarfBuzzFace final : public Garbag
   HarfBuzzFace(const FontPlatformData* platform_data, uint64_t);
   HarfBuzzFace(const HarfBuzzFace&) = delete;
   HarfBuzzFace& operator=(const HarfBuzzFace&) = delete;
+  ~HarfBuzzFace();
 
   void Trace(Visitor*) const;
 
@@ -106,7 +106,11 @@
   void PrepareHarfBuzzFontData();
 
   Member<const FontPlatformData> platform_data_;
-  Member<HarfBuzzFontData> harfbuzz_font_data_;
+  const uint64_t unique_id_;
+  // TODO(crbug.com/1489080): When briefly given MiraclePtr protection,
+  // these members were both found dangling.
+  hb_font_t* unscaled_font_;
+  HarfBuzzFontData* harfbuzz_font_data_;
   static bool ignore_variation_selectors_;
 };
 
--- a/third_party/blink/renderer/platform/fonts/shaping/harfbuzz_font_cache.cc
+++ b/third_party/blink/renderer/platform/fonts/shaping/harfbuzz_font_cache.cc
@@ -8,8 +8,38 @@
 
 namespace blink {
 
-void HarfBuzzFontCache::Trace(Visitor* visitor) const {
-  visitor->Trace(font_map_);
+HbFontCacheEntry::HbFontCacheEntry(hb_font_t* font)
+    : hb_font_(hb::unique_ptr<hb_font_t>(font)),
+      hb_font_data_(std::make_unique<HarfBuzzFontData>()) {}
+
+HbFontCacheEntry::~HbFontCacheEntry() = default;
+
+scoped_refptr<HbFontCacheEntry> HbFontCacheEntry::Create(hb_font_t* hb_font) {
+  DCHECK(hb_font);
+  return base::AdoptRef(new HbFontCacheEntry(hb_font));
+}
+
+HarfBuzzFontCache::HarfBuzzFontCache() = default;
+HarfBuzzFontCache::~HarfBuzzFontCache() = default;
+
+// See "harfbuzz_face.cc" for |HarfBuzzFontCache::GetOrCreateFontData()|
+// implementation.
+
+void HarfBuzzFontCache::Remove(uint64_t unique_id) {
+  auto it = font_map_.find(unique_id);
+  // TODO(https://crbug.com/1417160): In tests such as FontObjectThreadedTest
+  // that test taking down FontGlobalContext an object may not be found due to
+  // existing issues with refcounting of font objects at thread destruction
+  // time.
+  if (it == font_map_.end()) {
+    return;
+  }
+  DCHECK(!it.Get()->value->HasOneRef());
+  it.Get()->value->Release();
+  if (!it.Get()->value->HasOneRef()) {
+    return;
+  }
+  font_map_.erase(it);
 }
 
 }  // namespace blink
--- a/third_party/blink/renderer/platform/fonts/shaping/harfbuzz_font_cache.h
+++ b/third_party/blink/renderer/platform/fonts/shaping/harfbuzz_font_cache.h
@@ -6,9 +6,12 @@
 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_SHAPING_HARFBUZZ_FONT_CACHE_H_
 
 #include "third_party/blink/renderer/platform/fonts/font_metrics.h"
-#include "third_party/blink/renderer/platform/heap/collection_support/heap_hash_map.h"
-#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
-#include "third_party/blink/renderer/platform/heap/member.h"
+#include "third_party/blink/renderer/platform/fonts/unicode_range_set.h"
+
+#include <hb.h>
+#include <hb-cplusplus.hh>
+
+#include <memory>
 
 namespace blink {
 
@@ -22,21 +25,39 @@ struct HarfBuzzFontData;
 // FIXME, crbug.com/609099: We should fix the FontCache to only keep one
 // FontPlatformData object independent of size, then consider using this here.
 
-class HarfBuzzFontCache final {
-  DISALLOW_NEW();
+class HbFontCacheEntry : public RefCounted<HbFontCacheEntry> {
+  USING_FAST_MALLOC(HbFontCacheEntry);
+
+ public:
+  static scoped_refptr<HbFontCacheEntry> Create(hb_font_t* hb_font);
+
+  hb_font_t* HbFont() { return hb_font_.get(); }
+  HarfBuzzFontData* HbFontData() { return hb_font_data_.get(); }
+
+  ~HbFontCacheEntry();
 
+ private:
+  explicit HbFontCacheEntry(hb_font_t* font);
+
+  hb::unique_ptr<hb_font_t> hb_font_;
+  std::unique_ptr<HarfBuzzFontData> hb_font_data_;
+};
+
+class HarfBuzzFontCache final {
  public:
-  void Trace(Visitor* visitor) const;
-  // See "harfbuzz_face.cc" for |HarfBuzzFontCache::GetOrCreateFontData()|
-  // implementation.
-  HarfBuzzFontData* GetOrCreate(uint64_t unique_id,
-                                const FontPlatformData* platform_data);
+  HarfBuzzFontCache();
+  ~HarfBuzzFontCache();
+
+  HbFontCacheEntry* RefOrNew(uint64_t unique_id,
+                             const FontPlatformData* platform_data);
+  void Remove(uint64_t unique_id);
 
  private:
-  HeapHashMap<uint64_t,
-              WeakMember<HarfBuzzFontData>,
-              IntWithZeroKeyHashTraits<uint64_t>>
-      font_map_;
+  using HbFontDataMap = HashMap<uint64_t,
+                                scoped_refptr<HbFontCacheEntry>,
+                                IntWithZeroKeyHashTraits<uint64_t>>;
+
+  HbFontDataMap font_map_;
 };
 
 }  // namespace blink
--- a/third_party/blink/renderer/platform/fonts/shaping/harfbuzz_font_data.h
+++ b/third_party/blink/renderer/platform/fonts/shaping/harfbuzz_font_data.h
@@ -22,18 +22,15 @@ const unsigned kInvalidFallbackMetricsVa
 // The HarfBuzzFontData struct carries user-pointer data for
 // |hb_font_t| callback functions/operations. It contains metrics and OpenType
 // layout information related to a font scaled to a particular size.
-struct HarfBuzzFontData final : public GarbageCollected<HarfBuzzFontData> {
+struct HarfBuzzFontData final {
+  USING_FAST_MALLOC(HarfBuzzFontData);
+
  public:
-  explicit HarfBuzzFontData(hb_font_t* unscaled_font)
-      : unscaled_font_(hb::unique_ptr<hb_font_t>(unscaled_font)),
-        vertical_data_(nullptr),
-        range_set_(nullptr) {}
+  HarfBuzzFontData() : vertical_data_(nullptr), range_set_(nullptr) {}
 
   HarfBuzzFontData(const HarfBuzzFontData&) = delete;
   HarfBuzzFontData& operator=(const HarfBuzzFontData&) = delete;
 
-  void Trace(Visitor*) const {}
-
   // The vertical origin and vertical advance functions in HarfBuzzFace require
   // the ascent and height metrics as fallback in case no specific vertical
   // layout information is found from the font.
@@ -81,7 +78,6 @@ struct HarfBuzzFontData final : public G
     return vertical_data_;
   }
 
-  const hb::unique_ptr<hb_font_t> unscaled_font_;
   SkFont font_;
 
   // Capture these scaled fallback metrics from FontPlatformData so that a