Commit 82f006f6 authored by Xiaocheng Hu's avatar Xiaocheng Hu Committed by Commit Bot

Eliminate RenderedPosition::UncachedInlineBox with Optional

RenderedPosition caches two |InlineBox*|, and uses 1 as the magic value
indicating the uncached value, which is a hacky approach. This patch
changes them to Optional<InlineBox*> to make the caching not hacky.

Bug: 771398
Change-Id: If8bef20755fcc75b04093de6c2211138d336600e
Reviewed-on: https://chromium-review.googlesource.com/702924
Commit-Queue: Xiaocheng Hu <xiaochengh@chromium.org>
Commit-Queue: Emil A Eklund <eae@chromium.org>
Reviewed-by: default avatarEmil A Eklund <eae@chromium.org>
Cr-Commit-Position: refs/heads/master@{#506861}
parent c0ad9a7b
...@@ -78,11 +78,7 @@ RenderedPosition::RenderedPosition(const VisiblePositionInFlatTree& position) ...@@ -78,11 +78,7 @@ RenderedPosition::RenderedPosition(const VisiblePositionInFlatTree& position)
RenderedPosition::RenderedPosition(const Position& position, RenderedPosition::RenderedPosition(const Position& position,
TextAffinity affinity) TextAffinity affinity)
: layout_object_(nullptr), : layout_object_(nullptr), inline_box_(nullptr), offset_(0) {
inline_box_(nullptr),
offset_(0),
prev_leaf_child_(UncachedInlineBox()),
next_leaf_child_(UncachedInlineBox()) {
if (position.IsNull()) if (position.IsNull())
return; return;
InlineBoxPosition box_position = ComputeInlineBoxPosition(position, affinity); InlineBoxPosition box_position = ComputeInlineBoxPosition(position, affinity);
...@@ -97,11 +93,7 @@ RenderedPosition::RenderedPosition(const Position& position, ...@@ -97,11 +93,7 @@ RenderedPosition::RenderedPosition(const Position& position,
RenderedPosition::RenderedPosition(const PositionInFlatTree& position, RenderedPosition::RenderedPosition(const PositionInFlatTree& position,
TextAffinity affinity) TextAffinity affinity)
: layout_object_(nullptr), : layout_object_(nullptr), inline_box_(nullptr), offset_(0) {
inline_box_(nullptr),
offset_(0),
prev_leaf_child_(UncachedInlineBox()),
next_leaf_child_(UncachedInlineBox()) {
if (position.IsNull()) if (position.IsNull())
return; return;
InlineBoxPosition box_position = ComputeInlineBoxPosition(position, affinity); InlineBoxPosition box_position = ComputeInlineBoxPosition(position, affinity);
...@@ -115,15 +107,15 @@ RenderedPosition::RenderedPosition(const PositionInFlatTree& position, ...@@ -115,15 +107,15 @@ RenderedPosition::RenderedPosition(const PositionInFlatTree& position,
} }
InlineBox* RenderedPosition::PrevLeafChild() const { InlineBox* RenderedPosition::PrevLeafChild() const {
if (prev_leaf_child_ == UncachedInlineBox()) if (!prev_leaf_child_.has_value())
prev_leaf_child_ = inline_box_->PrevLeafChildIgnoringLineBreak(); prev_leaf_child_ = inline_box_->PrevLeafChildIgnoringLineBreak();
return prev_leaf_child_; return prev_leaf_child_.value();
} }
InlineBox* RenderedPosition::NextLeafChild() const { InlineBox* RenderedPosition::NextLeafChild() const {
if (next_leaf_child_ == UncachedInlineBox()) if (!next_leaf_child_.has_value())
next_leaf_child_ = inline_box_->NextLeafChildIgnoringLineBreak(); next_leaf_child_ = inline_box_->NextLeafChildIgnoringLineBreak();
return next_leaf_child_; return next_leaf_child_.value();
} }
bool RenderedPosition::IsEquivalent(const RenderedPosition& other) const { bool RenderedPosition::IsEquivalent(const RenderedPosition& other) const {
......
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
#include "core/editing/Forward.h" #include "core/editing/Forward.h"
#include "core/layout/line/InlineBox.h" #include "core/layout/line/InlineBox.h"
#include "platform/wtf/Allocator.h" #include "platform/wtf/Allocator.h"
#include "platform/wtf/Optional.h"
namespace blink { namespace blink {
...@@ -125,32 +126,19 @@ class CORE_EXPORT RenderedPosition { ...@@ -125,32 +126,19 @@ class CORE_EXPORT RenderedPosition {
InlineBox* inline_box_; InlineBox* inline_box_;
int offset_; int offset_;
static InlineBox* UncachedInlineBox() { mutable Optional<InlineBox*> prev_leaf_child_;
return reinterpret_cast<InlineBox*>(1); mutable Optional<InlineBox*> next_leaf_child_;
}
// Needs to be different form 0 so pick 1 because it's also on the null page.
mutable InlineBox* prev_leaf_child_;
mutable InlineBox* next_leaf_child_;
FRIEND_TEST_ALL_PREFIXES(RenderedPositionTest, GetSamplePointForVisibility); FRIEND_TEST_ALL_PREFIXES(RenderedPositionTest, GetSamplePointForVisibility);
}; };
inline RenderedPosition::RenderedPosition() inline RenderedPosition::RenderedPosition()
: layout_object_(nullptr), : layout_object_(nullptr), inline_box_(nullptr), offset_(0) {}
inline_box_(nullptr),
offset_(0),
prev_leaf_child_(UncachedInlineBox()),
next_leaf_child_(UncachedInlineBox()) {}
inline RenderedPosition::RenderedPosition(LayoutObject* layout_object, inline RenderedPosition::RenderedPosition(LayoutObject* layout_object,
InlineBox* box, InlineBox* box,
int offset) int offset)
: layout_object_(layout_object), : layout_object_(layout_object), inline_box_(box), offset_(offset) {}
inline_box_(box),
offset_(offset),
prev_leaf_child_(UncachedInlineBox()),
next_leaf_child_(UncachedInlineBox()) {}
CORE_EXPORT bool LayoutObjectContainsPosition(LayoutObject*, const Position&); CORE_EXPORT bool LayoutObjectContainsPosition(LayoutObject*, const Position&);
......
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