Commit 2ae86a90 authored by Steve Kobes's avatar Steve Kobes Committed by Commit Bot

Move LayoutShift entry construction into LayoutShiftTracker.

Also declare the capacity of the HeapVector for the attribution list,
accept it in the LayoutShift constructor, and allow the binding layer
to cache the FrozenArray.

Bug: 1053510
Change-Id: If0dde547631d3fd95d087d7b8d2116055e382b22
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2153002
Commit-Queue: Steve Kobes <skobes@chromium.org>
Reviewed-by: default avatarNicolás Peña Moreno <npm@chromium.org>
Cr-Commit-Position: refs/heads/master@{#760547}
parent 08a4a6be
......@@ -363,6 +363,30 @@ void LayoutShiftTracker::NotifyPrePaintFinished() {
attributions_.fill(Attribution());
}
LayoutShift::AttributionList LayoutShiftTracker::CreateAttributionList() const {
// TODO(crbug.com/1053510): Implement.
return LayoutShift::AttributionList();
}
void LayoutShiftTracker::SubmitPerformanceEntry(double score_delta,
bool had_recent_input) const {
LocalDOMWindow* window = frame_view_->GetFrame().DomWindow();
if (!window)
return;
WindowPerformance* performance = DOMWindowPerformance::performance(*window);
DCHECK(performance);
double input_timestamp =
had_recent_input ? performance->MonotonicTimeToDOMHighResTimeStamp(
most_recent_input_timestamp_)
: 0.0;
LayoutShift* entry =
LayoutShift::Create(performance->now(), score_delta, had_recent_input,
input_timestamp, CreateAttributionList());
performance->AddLayoutShiftEntry(entry);
}
void LayoutShiftTracker::ReportShift(double score_delta,
double weighted_score_delta) {
LocalFrame& frame = frame_view_->GetFrame();
......@@ -377,14 +401,7 @@ void LayoutShiftTracker::ReportShift(double score_delta,
}
}
if (frame.DomWindow()) {
WindowPerformance* performance =
DOMWindowPerformance::performance(*frame.DomWindow());
if (performance) {
performance->AddLayoutShiftValue(score_delta, had_recent_input,
most_recent_input_timestamp_);
}
}
SubmitPerformanceEntry(score_delta, had_recent_input);
TRACE_EVENT_INSTANT2("loading", "LayoutShift", TRACE_EVENT_SCOPE_THREAD,
"data", PerFrameTraceData(score_delta, had_recent_input),
......
......@@ -9,6 +9,7 @@
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/layout/layout_shift_region.h"
#include "third_party/blink/renderer/core/scroll/scroll_types.h"
#include "third_party/blink/renderer/core/timing/layout_shift.h"
#include "third_party/blink/renderer/platform/geometry/region.h"
#include "third_party/blink/renderer/platform/graphics/dom_node_id.h"
#include "third_party/blink/renderer/platform/timer.h"
......@@ -100,6 +101,8 @@ class CORE_EXPORT LayoutShiftTracker {
double SubframeWeightingFactor() const;
void SetLayoutShiftRects(const Vector<IntRect>& int_rects);
void UpdateInputTimestamp(base::TimeTicks timestamp);
LayoutShift::AttributionList CreateAttributionList() const;
void SubmitPerformanceEntry(double score_delta, bool input_detected) const;
// This owns us.
UntracedMember<LocalFrameView> frame_view_;
......@@ -175,13 +178,11 @@ class CORE_EXPORT LayoutShiftTracker {
bool MoreImpactfulThan(const Attribution&) const;
int Area() const;
};
static constexpr int kMaxAttributions = 5;
void MaybeRecordAttribution(const Attribution&);
// Nodes that have contributed to the impact region for the current frame, for
// use in trace event. Only populated while tracing.
std::array<Attribution, kMaxAttributions> attributions_;
// Nodes that have contributed to the impact region for the current frame.
std::array<Attribution, LayoutShift::kMaxAttributions> attributions_;
};
} // namespace blink
......
......@@ -10,14 +10,26 @@
namespace blink {
// static
LayoutShift* LayoutShift::Create(double start_time,
double value,
bool input_detected,
double input_timestamp,
AttributionList sources) {
return MakeGarbageCollected<LayoutShift>(start_time, value, input_detected,
input_timestamp, sources);
}
LayoutShift::LayoutShift(double start_time,
double value,
bool input_detected,
double input_timestamp)
double input_timestamp,
AttributionList sources)
: PerformanceEntry(g_empty_atom, start_time, start_time),
value_(value),
had_recent_input_(input_detected),
most_recent_input_timestamp_(input_timestamp) {}
most_recent_input_timestamp_(input_timestamp),
sources_(sources) {}
LayoutShift::~LayoutShift() = default;
......@@ -34,7 +46,12 @@ void LayoutShift::BuildJSONValue(V8ObjectBuilder& builder) const {
builder.Add("value", value_);
builder.Add("hadRecentInput", had_recent_input_);
builder.Add("lastInputTime", most_recent_input_timestamp_);
// TODO(crbug.com/1053510): add sources_.
if (RuntimeEnabledFeatures::LayoutShiftAttributionEnabled()) {
ScriptState* script_state = builder.GetScriptState();
builder.Add("sources", FreezeV8Object(ToV8(sources_, script_state),
script_state->GetIsolate()));
}
}
void LayoutShift::Trace(Visitor* visitor) {
......
......@@ -18,10 +18,24 @@ class CORE_EXPORT LayoutShift final : public PerformanceEntry {
DEFINE_WRAPPERTYPEINFO();
public:
LayoutShift(double start_time,
// Maximum number of attributions (shifted elements) to record in any single
// animation frame.
static constexpr int kMaxAttributions = 5;
typedef HeapVector<Member<LayoutShiftAttribution>, kMaxAttributions>
AttributionList;
static LayoutShift* Create(double start_time,
double value,
bool input_detected,
double input_timestamp,
AttributionList sources);
explicit LayoutShift(double start_time,
double value,
bool input_detected,
double input_timestamp);
double input_timestamp,
AttributionList sources);
~LayoutShift() override;
AtomicString entryType() const override;
......@@ -30,9 +44,8 @@ class CORE_EXPORT LayoutShift final : public PerformanceEntry {
double value() const { return value_; }
bool hadRecentInput() const { return had_recent_input_; }
double lastInputTime() const { return most_recent_input_timestamp_; }
HeapVector<Member<LayoutShiftAttribution>> sources() const {
return sources_;
}
AttributionList sources() const { return sources_; }
void Trace(Visitor*) override;
......@@ -42,7 +55,7 @@ class CORE_EXPORT LayoutShift final : public PerformanceEntry {
double value_;
bool had_recent_input_;
DOMHighResTimeStamp most_recent_input_timestamp_;
HeapVector<Member<LayoutShiftAttribution>> sources_;
AttributionList sources_;
};
} // namespace blink
......
......@@ -8,7 +8,7 @@ interface LayoutShift : PerformanceEntry {
readonly attribute double value;
readonly attribute boolean hadRecentInput;
readonly attribute DOMHighResTimeStamp lastInputTime;
[RuntimeEnabled=LayoutShiftAttribution] readonly attribute FrozenArray<LayoutShiftAttribution> sources;
[RuntimeEnabled=LayoutShiftAttribution, SameObject, SaveSameObject] readonly attribute FrozenArray<LayoutShiftAttribution> sources;
[CallWith=ScriptState, ImplementedAs=toJSONForBinding] object toJSON();
};
......@@ -74,7 +74,8 @@ TEST_F(PerformanceObserverTest, ObserveWithBufferedFlag) {
EXPECT_EQ(0, NumPerformanceEntries());
// add a layout-shift to performance so getEntries() returns it
auto* entry = MakeGarbageCollected<LayoutShift>(0.0, 1234, true, 5678);
auto* entry = LayoutShift::Create(0.0, 1234, true, 5678,
LayoutShift::AttributionList());
base_->AddLayoutShiftBuffer(*entry);
// call observe with the buffered flag
......
......@@ -433,13 +433,7 @@ void WindowPerformance::DispatchFirstInputTiming(
first_input_timing_ = entry;
}
void WindowPerformance::AddLayoutShiftValue(double value,
bool input_detected,
base::TimeTicks input_timestamp) {
auto* entry = MakeGarbageCollected<LayoutShift>(
now(), value, input_detected,
input_detected ? MonotonicTimeToDOMHighResTimeStamp(input_timestamp)
: 0.0);
void WindowPerformance::AddLayoutShiftEntry(LayoutShift* entry) {
if (HasObserverFor(PerformanceEntry::kLayoutShift))
NotifyObserversOfEntry(*entry);
AddLayoutShiftBuffer(*entry);
......
......@@ -86,9 +86,7 @@ class CORE_EXPORT WindowPerformance final : public Performance,
const AtomicString& id,
Element*);
void AddLayoutShiftValue(double value,
bool input_detected,
base::TimeTicks input_timestamp);
void AddLayoutShiftEntry(LayoutShift*);
void OnLargestContentfulPaintUpdated(base::TimeTicks paint_time,
uint64_t paint_size,
......
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