Skip to content

Truly independent web browser

LibWeb: Avoid pushing video paint facts on every rendering opportunity

Zaggy1024539d7eebf487
diff --git a/Libraries/LibWeb/HTML/HTMLMediaElement.cpp b/Libraries/LibWeb/HTML/HTMLMediaElement.cpp
index a3876b3a1ad..8ccb54928a5 100644
--- a/Libraries/LibWeb/HTML/HTMLMediaElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLMediaElement.cpp
@@ -1957,7 +1957,6 @@ void HTMLMediaElement::upon_current_playback_position_possibly_changed()
time_marches_on();
upon_has_ended_playback_possibly_changed();
- update_natural_dimensions();
// AD-HOC: Run the SourceBuffer monitoring algorithm to update readyState based on buffered data relative to
// the current playback position. This satisfies the periodic buffer monitoring in MSE:
@@ -2067,8 +2066,6 @@ void HTMLMediaElement::on_video_track_added(Media::Track const& track)
auto event = TrackEvent::create(HTML::EventNames::addtrack, move(event_init), event_time_stamp_for_element(*this));
m_video_tracks->dispatch_event(event);
-
- set_needs_repaint(InvalidateDisplayList::PaintCommands);
}
void HTMLMediaElement::on_metadata_parsed(SourceType source_type)
@@ -2484,7 +2481,7 @@ void HTMLMediaElement::set_ready_state(ReadyState ready_state)
ScopeGuard guard { [&] {
upon_has_ended_playback_possibly_changed();
update_screen_wake_lock();
- update_natural_dimensions();
+ update_representation();
} };
// When the ready state of a media element whose networkState is not NETWORK_EMPTY changes, the user agent must
@@ -3064,8 +3061,7 @@ void HTMLMediaElement::set_show_poster(bool show_poster)
m_show_poster = show_poster;
- update_natural_dimensions();
- set_needs_repaint(InvalidateDisplayList::PaintCommands);
+ update_representation();
}
void HTMLMediaElement::set_paused(bool paused)
@@ -3086,8 +3082,7 @@ void HTMLMediaElement::set_paused(bool paused)
update_screen_wake_lock();
- update_natural_dimensions();
- set_needs_repaint(InvalidateDisplayList::PaintCommands);
+ update_representation();
CSS::Invalidation::invalidate_style_after_media_paused_state_change(*this, paused);
}
diff --git a/Libraries/LibWeb/HTML/HTMLMediaElement.h b/Libraries/LibWeb/HTML/HTMLMediaElement.h
index 11b428fec95..e570547e6ae 100644
--- a/Libraries/LibWeb/HTML/HTMLMediaElement.h
+++ b/Libraries/LibWeb/HTML/HTMLMediaElement.h
@@ -190,7 +190,7 @@ public:
Optional<Compositing::VideoSinkResourceId> video_sink_resource_id() const;
- virtual void update_natural_dimensions() { }
+ virtual void update_representation() { }
protected:
HTMLMediaElement(DOM::Document&, DOM::QualifiedName);
diff --git a/Libraries/LibWeb/HTML/HTMLVideoElement.cpp b/Libraries/LibWeb/HTML/HTMLVideoElement.cpp
index 8c72b4d0d8d..000f7fbb3ea 100644
--- a/Libraries/LibWeb/HTML/HTMLVideoElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLVideoElement.cpp
@@ -99,7 +99,7 @@ void HTMLVideoElement::set_intrinsic_video_dimensions(Optional<Gfx::Size<u32>> d
});
}
- update_natural_dimensions();
+ update_representation();
}
// https://html.spec.whatwg.org/multipage/media.html#dom-video-videowidth
@@ -134,8 +134,10 @@ u32 HTMLVideoElement::video_height() const
return 0;
}
-void HTMLVideoElement::update_natural_dimensions()
+void HTMLVideoElement::update_representation()
{
+ auto representation = current_representation();
+
// https://html.spec.whatwg.org/multipage/media.html#concept-video-intrinsic-width
// The natural width of a video element's playback area is the natural width of the poster frame, if that is
// available and the element currently represents its poster frame; otherwise, it is the natural width of the video
@@ -146,17 +148,22 @@ void HTMLVideoElement::update_natural_dimensions()
// resource, if that is available; otherwise the natural height is missing.
auto natural_dimensions = m_intrinsic_video_dimensions.map([](Gfx::Size<u32> size) { return size.to_type<CSSPixels>(); });
- if (current_representation() == Representation::PosterFrame && m_poster_frame.has_value())
+ Optional<u64> poster_frame_id;
+ if (representation == Representation::PosterFrame && m_poster_frame.has_value()) {
natural_dimensions = m_poster_frame->size().to_type<CSSPixels>();
+ poster_frame_id = m_poster_frame->id();
+ }
- if (natural_dimensions == m_natural_dimensions) {
- Painting::push_video_paint_facts(*this);
+ if (representation == m_representation && poster_frame_id == m_represented_poster_frame_id && natural_dimensions == m_natural_dimensions)
return;
- }
+ m_representation = representation;
+ m_represented_poster_frame_id = poster_frame_id;
- set_needs_layout_update(DOM::SetNeedsLayoutReason::HTMLVideoElementNaturalDimensionsChanged);
- m_natural_dimensions = natural_dimensions;
- CSS::record_element_replaced_content_input(*this);
+ if (natural_dimensions != m_natural_dimensions) {
+ set_needs_layout_update(DOM::SetNeedsLayoutReason::HTMLVideoElementNaturalDimensionsChanged);
+ m_natural_dimensions = natural_dimensions;
+ CSS::record_element_replaced_content_input(*this);
+ }
Painting::push_video_paint_facts(*this);
}
@@ -185,7 +192,7 @@ WebIDL::ExceptionOr<void> HTMLVideoElement::determine_element_poster_frame(Optio
self.m_poster_frame = Gfx::DecodedImageFrame { *poster_frame };
self.m_load_event_delayer.clear();
self.m_fetch_controller = nullptr;
- self.update_natural_dimensions();
+ self.update_representation();
};
// 2. If the poster attribute's value is the empty string or if the attribute is absent, then there is no poster
diff --git a/Libraries/LibWeb/HTML/HTMLVideoElement.h b/Libraries/LibWeb/HTML/HTMLVideoElement.h
index bec5436def9..145118a011e 100644
--- a/Libraries/LibWeb/HTML/HTMLVideoElement.h
+++ b/Libraries/LibWeb/HTML/HTMLVideoElement.h
@@ -33,7 +33,7 @@ public:
u32 video_width() const;
u32 video_height() const;
- virtual void update_natural_dimensions() override;
+ virtual void update_representation() override;
Optional<Gfx::Size<u32>> natural_media_size() const;
Optional<CSSPixelSize> natural_element_size() const;
@@ -79,6 +79,8 @@ private:
Optional<Gfx::Size<u32>> m_intrinsic_video_dimensions;
Optional<CSSPixelSize> m_natural_dimensions;
+ Optional<Representation> m_representation;
+ Optional<u64> m_represented_poster_frame_id;
GC::Ptr<Fetch::Infrastructure::FetchController> m_fetch_controller;
Optional<DOM::DocumentLoadEventDelayer> m_load_event_delayer;
diff --git a/Libraries/LibWeb/HTML/VideoTrackList.cpp b/Libraries/LibWeb/HTML/VideoTrackList.cpp
index 17fe65d7d12..624316f3b43 100644
--- a/Libraries/LibWeb/HTML/VideoTrackList.cpp
+++ b/Libraries/LibWeb/HTML/VideoTrackList.cpp
@@ -40,7 +40,7 @@ void VideoTrackList::add_track(GC::Ref<VideoTrack> video_track)
m_video_tracks.append(video_track);
video_track->add_video_track_list({}, *this);
if (m_media_element)
- m_media_element->update_natural_dimensions();
+ m_media_element->update_representation();
}
void VideoTrackList::remove_all_tracks()
@@ -51,7 +51,7 @@ void VideoTrackList::remove_all_tracks()
}
m_video_tracks.clear();
if (m_media_element)
- m_media_element->update_natural_dimensions();
+ m_media_element->update_representation();
}
// https://html.spec.whatwg.org/multipage/media.html#dom-videotracklist-gettrackbyid
diff --git a/Libraries/LibWeb/Page/Page.cpp b/Libraries/LibWeb/Page/Page.cpp
index 333fbadbd71..b53ded832e3 100644
--- a/Libraries/LibWeb/Page/Page.cpp
+++ b/Libraries/LibWeb/Page/Page.cpp
@@ -1517,8 +1517,6 @@ void Page::sync_media_element_video_sink_ticking()
{
for_each_media_element([&](auto& media_element) {
media_element.sync_video_sink_ticking();
- if (auto* video_element = as_if<HTML::HTMLVideoElement>(&media_element))
- Painting::push_video_paint_facts(*video_element);
});
}