Commit bb607f4e authored by Koji Ishii's avatar Koji Ishii Committed by Commit Bot

[FragmentItem] Make NGFragmentItem RefCounted

This patch makes |NGFragmentItem| RefCounted.

Bug: 982194
Change-Id: I04340c91b52ad4e274c70f3c99f8e4a3e32f4ad3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2135672
Commit-Queue: Koji Ishii <kojii@chromium.org>
Reviewed-by: default avatarIan Kilpatrick <ikilpatrick@chromium.org>
Cr-Commit-Position: refs/heads/master@{#756542}
parent 8c1ec4c0
......@@ -5,6 +5,7 @@
#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_LAYOUT_NG_INLINE_NG_FRAGMENT_ITEM_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_LAYOUT_NG_INLINE_NG_FRAGMENT_ITEM_H_
#include "base/memory/scoped_refptr.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/layout/geometry/logical_offset.h"
#include "third_party/blink/renderer/core/layout/layout_object.h"
......@@ -14,6 +15,7 @@
#include "third_party/blink/renderer/core/layout/ng/inline/ng_text_offset.h"
#include "third_party/blink/renderer/core/layout/ng/ng_ink_overflow.h"
#include "third_party/blink/renderer/platform/graphics/paint/display_item_client.h"
#include "third_party/blink/renderer/platform/wtf/ref_counted.h"
namespace blink {
......@@ -26,7 +28,8 @@ struct NGTextFragmentPaintInfo;
//
// This class consumes less memory than a full fragment, and can be stored in a
// flat list (NGFragmentItems) for easier and faster traversal.
class CORE_EXPORT NGFragmentItem : public DisplayItemClient {
class CORE_EXPORT NGFragmentItem : public RefCounted<NGFragmentItem>,
public DisplayItemClient {
public:
// Represents regular text that exists in the DOM.
struct TextItem {
......
......@@ -23,7 +23,7 @@ NGFragmentItems::NGFragmentItems(NGFragmentItemsBuilder* builder)
first_line_text_content_(std::move(builder->first_line_text_content_)) {}
void NGFragmentItems::AssociateWithLayoutObject() const {
const Vector<std::unique_ptr<NGFragmentItem>>* items = &items_;
const Vector<scoped_refptr<NGFragmentItem>>* items = &items_;
DCHECK(std::all_of(items->begin(), items->end(), [](const auto& item) {
return !item->DeltaToNextForSameLayoutObject();
}));
......@@ -62,7 +62,7 @@ void NGFragmentItems::ClearAssociatedFragments() const {
if (items_.size() <= 1)
return;
LayoutObject* last_object = nullptr;
for (const auto& item : base::span<const std::unique_ptr<NGFragmentItem>>(
for (const auto& item : base::span<const scoped_refptr<NGFragmentItem>>(
items_.begin() + 1, items_.end())) {
if (!ShouldAssociateWithLayoutObject(*item)) {
// These items are not associated and that no need to clear.
......
......@@ -20,9 +20,7 @@ class CORE_EXPORT NGFragmentItems {
public:
NGFragmentItems(NGFragmentItemsBuilder* builder);
const Vector<std::unique_ptr<NGFragmentItem>>& Items() const {
return items_;
}
const Vector<scoped_refptr<NGFragmentItem>>& Items() const { return items_; }
const String& Text(bool first_line) const {
return UNLIKELY(first_line) ? first_line_text_content_ : text_content_;
......@@ -38,7 +36,7 @@ class CORE_EXPORT NGFragmentItems {
private:
// TODO(kojii): inline capacity TBD.
Vector<std::unique_ptr<NGFragmentItem>> items_;
Vector<scoped_refptr<NGFragmentItem>> items_;
String text_content_;
String first_line_text_content_;
};
......
......@@ -50,7 +50,8 @@ void NGFragmentItemsBuilder::AddLine(const NGPhysicalLineBoxFragment& line,
// All children are added. Create an item for the start of the line.
wtf_size_t item_count = items_.size() - line_start_index;
items_[line_start_index] = std::make_unique<NGFragmentItem>(line, item_count);
items_[line_start_index] =
base::MakeRefCounted<NGFragmentItem>(line, item_count);
// TODO(kojii): We probably need an end marker too for the reverse-order
// traversals.
......@@ -70,7 +71,7 @@ void NGFragmentItemsBuilder::AddItems(Child* child_begin, Child* child_end) {
for (Child* child_iter = child_begin; child_iter != child_end;) {
Child& child = *child_iter;
if (const NGPhysicalTextFragment* text = child.fragment.get()) {
items_.push_back(std::make_unique<NGFragmentItem>(*text));
items_.push_back(base::MakeRefCounted<NGFragmentItem>(*text));
offsets_.push_back(child.rect.offset);
++child_iter;
continue;
......@@ -78,14 +79,15 @@ void NGFragmentItemsBuilder::AddItems(Child* child_begin, Child* child_end) {
if (child.layout_result || child.inline_item) {
// Create an item if this box has no inline children.
std::unique_ptr<NGFragmentItem> item;
scoped_refptr<NGFragmentItem> item;
if (child.layout_result) {
const NGPhysicalBoxFragment& box =
To<NGPhysicalBoxFragment>(child.layout_result->PhysicalFragment());
item = std::make_unique<NGFragmentItem>(box, child.ResolvedDirection());
item = base::MakeRefCounted<NGFragmentItem>(box,
child.ResolvedDirection());
} else {
DCHECK(child.inline_item);
item = std::make_unique<NGFragmentItem>(
item = base::MakeRefCounted<NGFragmentItem>(
*child.inline_item,
ToPhysicalSize(child.rect.size,
child.inline_item->Style()->GetWritingMode()));
......@@ -140,12 +142,12 @@ void NGFragmentItemsBuilder::AddListMarker(
// Resolved direction matters only for inline items, and outside list markers
// are not inline.
const TextDirection resolved_direction = TextDirection::kLtr;
items_.push_back(
std::make_unique<NGFragmentItem>(marker_fragment, resolved_direction));
items_.push_back(base::MakeRefCounted<NGFragmentItem>(marker_fragment,
resolved_direction));
offsets_.push_back(offset);
}
const Vector<std::unique_ptr<NGFragmentItem>>& NGFragmentItemsBuilder::Items(
const Vector<scoped_refptr<NGFragmentItem>>& NGFragmentItemsBuilder::Items(
WritingMode writing_mode,
TextDirection direction,
const PhysicalSize& outer_size) {
......@@ -166,7 +168,7 @@ void NGFragmentItemsBuilder::ConvertToPhysical(WritingMode writing_mode,
// convert their logical offsets.
const WritingMode line_writing_mode = ToLineWritingMode(writing_mode);
std::unique_ptr<NGFragmentItem>* item_iter = items_.begin();
scoped_refptr<NGFragmentItem>* item_iter = items_.begin();
const LogicalOffset* offset = offsets_.begin();
for (; item_iter != items_.end(); ++item_iter, ++offset) {
DCHECK_NE(offset, offsets_.end());
......@@ -205,7 +207,7 @@ void NGFragmentItemsBuilder::ConvertToPhysical(WritingMode writing_mode,
base::Optional<LogicalOffset> NGFragmentItemsBuilder::LogicalOffsetFor(
const LayoutObject& layout_object) const {
DCHECK_EQ(items_.size(), offsets_.size());
for (const std::unique_ptr<NGFragmentItem>& item : items_) {
for (const scoped_refptr<NGFragmentItem>& item : items_) {
if (item->GetLayoutObject() == &layout_object)
return offsets_[&item - items_.begin()];
}
......
......@@ -67,7 +67,7 @@ class CORE_EXPORT NGFragmentItemsBuilder {
// containing block geometry for OOF-positioned nodes.
//
// Once this method has been called, new items cannot be added.
const Vector<std::unique_ptr<NGFragmentItem>>&
const Vector<scoped_refptr<NGFragmentItem>>&
Items(WritingMode, TextDirection, const PhysicalSize& outer_size);
// Build a |NGFragmentItems|. The builder cannot build twice because data set
......@@ -84,7 +84,7 @@ class CORE_EXPORT NGFragmentItemsBuilder {
TextDirection direction,
const PhysicalSize& outer_size);
Vector<std::unique_ptr<NGFragmentItem>> items_;
Vector<scoped_refptr<NGFragmentItem>> items_;
Vector<LogicalOffset> offsets_;
String text_content_;
String first_line_text_content_;
......
......@@ -45,7 +45,7 @@ class CORE_EXPORT NGInlineCursorPosition {
STACK_ALLOCATED();
public:
using ItemsSpan = base::span<const std::unique_ptr<NGFragmentItem>>;
using ItemsSpan = base::span<const scoped_refptr<NGFragmentItem>>;
const NGPaintFragment* PaintFragment() const { return paint_fragment_; }
const NGFragmentItem* Item() const { return item_; }
......@@ -170,7 +170,7 @@ class CORE_EXPORT NGInlineCursor {
STACK_ALLOCATED();
public:
using ItemsSpan = base::span<const std::unique_ptr<NGFragmentItem>>;
using ItemsSpan = base::span<const scoped_refptr<NGFragmentItem>>;
explicit NGInlineCursor(const LayoutBlockFlow& block_flow);
explicit NGInlineCursor(const NGFragmentItems& items);
......
......@@ -75,8 +75,10 @@ LayoutUnit NGPhysicalTextFragment::InlinePositionForOffset(
unsigned offset,
LayoutUnit (*round_function)(float),
AdjustMidCluster adjust_mid_cluster) const {
return NGFragmentItem(*this).InlinePositionForOffset(
Text(), offset, round_function, adjust_mid_cluster);
scoped_refptr<NGFragmentItem> item =
base::MakeRefCounted<NGFragmentItem>(*this);
return item->InlinePositionForOffset(Text(), offset, round_function,
adjust_mid_cluster);
}
// TODO(yosin): We should move |NGFragmentItem::InlinePositionForOffset" to
......@@ -120,14 +122,17 @@ LayoutUnit NGFragmentItem::InlinePositionForOffset(StringView text,
LayoutUnit NGPhysicalTextFragment::InlinePositionForOffset(
unsigned offset) const {
return NGFragmentItem(*this).InlinePositionForOffset(Text(), offset);
scoped_refptr<NGFragmentItem> item =
base::MakeRefCounted<NGFragmentItem>(*this);
return item->InlinePositionForOffset(Text(), offset);
}
std::pair<LayoutUnit, LayoutUnit>
NGPhysicalTextFragment::LineLeftAndRightForOffsets(unsigned start_offset,
unsigned end_offset) const {
return NGFragmentItem(*this).LineLeftAndRightForOffsets(Text(), start_offset,
end_offset);
scoped_refptr<NGFragmentItem> item =
base::MakeRefCounted<NGFragmentItem>(*this);
return item->LineLeftAndRightForOffsets(Text(), start_offset, end_offset);
}
// TODO(yosin): We should move |NGFragmentItem::InlinePositionForOffset" to
......@@ -154,7 +159,9 @@ std::pair<LayoutUnit, LayoutUnit> NGFragmentItem::LineLeftAndRightForOffsets(
PhysicalRect NGPhysicalTextFragment::LocalRect(unsigned start_offset,
unsigned end_offset) const {
return NGFragmentItem(*this).LocalRect(Text(), start_offset, end_offset);
scoped_refptr<NGFragmentItem> item =
base::MakeRefCounted<NGFragmentItem>(*this);
return item->LocalRect(Text(), start_offset, end_offset);
}
// TODO(yosin): We should move |NGFragmentItem::InlinePositionForOffset" to
......
......@@ -78,7 +78,7 @@ void GatherInlineContainerFragmentsFromLinebox(
}
void GatherInlineContainerFragmentsFromItems(
const Vector<std::unique_ptr<NGFragmentItem>>& items,
const Vector<scoped_refptr<NGFragmentItem>>& items,
const PhysicalOffset& box_offset,
NGBoxFragmentBuilder::InlineContainingBlockMap* inline_containing_block_map,
HashMap<const LayoutObject*, LineBoxPair>* containing_linebox_map) {
......
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