File: frame_shape_cache.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (127 lines) | stat: -rw-r--r-- 4,457 bytes parent folder | download | duplicates (5)
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
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_SHAPING_FRAME_SHAPE_CACHE_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_SHAPING_FRAME_SHAPE_CACHE_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/text/text_direction.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/blink/renderer/platform/wtf/vector_backed_linked_list.h"
#include "ui/gfx/geometry/rect_f.h"

namespace blink {

class PlainTextItem;
class PlainTextNode;
class ShapeResult;

// FrameShapeCache manages a cache for PlainTextNode, and a cache for
// ShapeResult and ink bounds. A PlainTextPainter instance owns multiple
// FrameShapeCache instances, and each FrameShapeCache instance is associated
// to a specific `Font`.
//
// The caches are aware of frames distinguished by DidSwitchFrame() calls. This
// allows the caches to purge old entries when the frame is switched.
class PLATFORM_EXPORT FrameShapeCache
    : public GarbageCollected<FrameShapeCache> {
 public:
  FrameShapeCache();
  void Trace(Visitor* visitor) const;

  FrameShapeCache(const FrameShapeCache&) = delete;
  FrameShapeCache& operator=(const FrameShapeCache&) = delete;

  // This function should be called between the end of an animation frame and
  // the beginning of the next animation frame.
  void DidSwitchFrame();

  // Cache entry for the PlainTextNode cache.
  // Clients must not access `list_index`.
  struct NodeEntry {
    DISALLOW_NEW();

   public:
    // Cached data.
    Member<PlainTextNode> node;
    // A field for LRU management. It's kNotFound for entries created
    // in the initial frame.
    wtf_size_t list_index;

    void Trace(Visitor* visitor) const;
  };

  // Find a PlainTextNode cache entry for the specified `text` and `direction`.
  // If it's not found, new entry is created.
  NodeEntry* FindOrCreateNodeEntry(const String& text, TextDirection direction);

  // This should be called if FindOrCreateNodeEntry() didn't find an
  // existing entry.
  void RegisterNodeEntry(const String& text,
                         TextDirection direction,
                         PlainTextNode* node,
                         NodeEntry* entry);

  // Cache entry for the ShapeResult cache.
  // Clients must not access `list_index`.
  struct ShapeEntry {
    DISALLOW_NEW();

   public:
    // Cached data.
    Member<const ShapeResult> shape_result;
    gfx::RectF ink_bounds;
    // A field for LRU management. It's kNotFound for entries created
    // in the initial frame.
    wtf_size_t list_index;

    void Trace(Visitor* visitor) const;
  };

  // Find a ShapeResult cache entry for the specified `text` and `direction`.
  // If it's not found, new entry is created.
  ShapeEntry* FindOrCreateShapeEntry(const String& word,
                                     TextDirection direction);

  // This should be called if FindOrCreateShapeEntry() didn't find an
  // existing entry.
  void RegisterShapeEntry(const PlainTextItem& item, ShapeEntry* entry);

 private:
  using KeyType = std::pair<String, TextDirection>;
  struct ListKey {
    KeyType key;
    uint32_t generation;
  };
  using LruList = VectorBackedLinkedList<ListKey>;

  template <typename E>
  E* FindOrCreateEntry(const String& text,
                       TextDirection direction,
                       HeapHashMap<KeyType, E>& map,
                       LruList& lru_list);
  wtf_size_t ListIndexForNewEntry(const String& text,
                                  TextDirection direction,
                                  LruList& lru_list);
  template <typename E>
  void RemoveOldEntries(HeapHashMap<KeyType, E>& map, LruList& lru_list);

  HeapHashMap<KeyType, NodeEntry> node_map_;
  LruList node_lru_list_;

  HeapHashMap<KeyType, ShapeEntry> shape_map_;
  LruList shape_lru_list_;

  static constexpr uint32_t kInitialFrame = 0;
  uint32_t frame_generation_ = kInitialFrame;

  // True if a cache has a new entry in the current frame.
  bool added_new_entries_ = false;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_SHAPING_FRAME_SHAPE_CACHE_H_