Truly independent web browser
LibWeb/HTML: Stop updating hidden video controls
Luke Wilde5219af7dfcf9
diff --git a/Libraries/LibWeb/HTML/MediaControls.cpp b/Libraries/LibWeb/HTML/MediaControls.cppindex b92d0fdc22e..fc445c613c5 100644--- a/Libraries/LibWeb/HTML/MediaControls.cpp+++ b/Libraries/LibWeb/HTML/MediaControls.cpp@@ -59,6 +59,10 @@ void MediaControls::update_visibility()MUST(m_dom->container->class_list()->toggle("controls-hidden"_utf16, !host.should_expose_user_interface()));MUST(m_dom->container->class_list()->toggle("picture-in-picture"_utf16, host.is_picture_in_picture_element()));update_placeholder_visibility();++ update_timeline();+ update_timestamp();+ request_timeline_update();}void MediaControls::create_shadow_tree()@@ -586,6 +590,9 @@ void MediaControls::update_timeline()VERIFY(m_dom->timeline_track);VERIFY(m_dom->timeline_fill);+ if (!timeline_is_shown())+ return;+auto range = timeline_range();if (m_scrubbing_timeline == Scrubbing::No) {double progress = 0.0;@@ -644,11 +651,20 @@ void MediaControls::set_timeline_progress(double progress)m_last_timeline_progress = progress;}+// A hidden video control bar is not kept up to date, so that a playing video does not need a rendering update every+// frame. It catches up when it shows.+bool MediaControls::timeline_is_shown() const+{+ if (!is<HTMLVideoElement>(*m_host))+ return true;+ return m_control_bar_is_shown && m_host->should_expose_user_interface();+}+void MediaControls::request_timeline_update(){if (m_request_animation_frame_id != 0)return;- if (!m_media_element->potentially_playing())+ if (!m_media_element->potentially_playing() || !timeline_is_shown())return;auto& realm = HTML::relevant_realm(*m_host);@@ -663,6 +679,9 @@ void MediaControls::request_timeline_update()void MediaControls::update_timestamp(){VERIFY(m_media_element);+ if (!timeline_is_shown())+ return;+double time = static_cast<double>(m_last_timestamp_time);if (m_scrubbing_timeline == Scrubbing::No)time = m_media_element->current_time();@@ -794,8 +813,12 @@ void MediaControls::show_controls()MUST(m_dom->control_bar->class_list()->add(visible_class()));- if (!exchange(m_control_bar_is_shown, true))+ if (!exchange(m_control_bar_is_shown, true)) {+ update_timeline();+ update_timestamp();+ request_timeline_update();control_bar_visibility_did_change();+ }if (!m_hover_timer) {constexpr int hover_timeout_ms = 1000;diff --git a/Libraries/LibWeb/HTML/MediaControls.h b/Libraries/LibWeb/HTML/MediaControls.hindex e19e47eec60..c7ec4d858d3 100644--- a/Libraries/LibWeb/HTML/MediaControls.h+++ b/Libraries/LibWeb/HTML/MediaControls.h@@ -75,6 +75,7 @@ private:void update_timestamp();void set_timestamp(double time, double duration);void request_timeline_update();+ bool timeline_is_shown() const;void update_volume_and_mute_indicator();void update_fullscreen_icon();void update_placeholder_visibility();diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cppindex c60b705b4d6..a2035175b24 100644--- a/Libraries/LibWeb/Internals/Internals.cpp+++ b/Libraries/LibWeb/Internals/Internals.cpp@@ -1578,6 +1578,11 @@ bool Internals::needs_repaint()return local_root && local_root->needs_repaint();}+bool Internals::has_animation_frame_callbacks()+{+ return window().has_animation_frame_callbacks();+}+bool Internals::needs_display_list_record(){Layout::ForcedReadScope read { window().associated_document() };diff --git a/Libraries/LibWeb/Internals/Internals.h b/Libraries/LibWeb/Internals/Internals.hindex ff853913887..0aec48d9d22 100644--- a/Libraries/LibWeb/Internals/Internals.h+++ b/Libraries/LibWeb/Internals/Internals.h@@ -199,6 +199,7 @@ public:bool needs_repaint();bool needs_display_list_record();+ bool has_animation_frame_callbacks();Utf16String dump_display_list();Utf16String dump_accessibility_tree();diff --git a/Libraries/LibWeb/Internals/Internals.idl b/Libraries/LibWeb/Internals/Internals.idlindex 2ac277de657..c69f17835b5 100644--- a/Libraries/LibWeb/Internals/Internals.idl+++ b/Libraries/LibWeb/Internals/Internals.idl@@ -196,6 +196,7 @@ interface Internals {readonly attribute boolean needsRepaint;readonly attribute boolean needsDisplayListRecord;+ readonly attribute boolean hasAnimationFrameCallbacks;DOMString dumpDisplayList();DOMString dumpAccessibilityTree();diff --git a/Tests/LibWeb/Layout/expected/PictureInPicture/placeholder-stays-when-controls-are-added.txt b/Tests/LibWeb/Layout/expected/PictureInPicture/placeholder-stays-when-controls-are-added.txtindex be2c1363c86..90478261953 100644--- a/Tests/LibWeb/Layout/expected/PictureInPicture/placeholder-stays-when-controls-are-added.txt+++ b/Tests/LibWeb/Layout/expected/PictureInPicture/placeholder-stays-when-controls-are-added.txt@@ -17,6 +17,7 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] childrenBlockContainer <div.timeline> at [8,445] flex-item [0+0+0 854 0+0+0] [-5+0+5 5 5+0+-5] [BFC] children: not-inlineBlockContainer <div.timeline-track> at [8,445] positioned [0+0+0 854 0+0+0] [0+0+0 5 0+0+0] children: not-inlineBlockContainer <div.timeline-fill> at [8,445] [0+0+0 0 0+0+854] [0+0+0 5 0+0+0] children: not-inline+ BlockContainer <div.timeline-buffered> at [8,445] positioned [0+0+0 854 0+0+0] [0+0+0 5 0+0+0] [BFC] children: not-inlineBox <div.button-bar> at [16,454] flex-container(row) flex-item [0+0+8 838 8+0+0] [0+0+4 30 4+0+0] [FFC] children: not-inlineBox <button.control-button.play-pause-button> at [16,457] flex-container(row) flex-item [0+0+0 24 0+0+0] [0+0+0 24 0+0+0] [FFC] children: not-inlineSVGSVGBox <svg.icon.play-pause-icon> at [16,457] flex-item [0+0+0 24 0+0+0] [0+0+0 24 0+0+0] viewport-transform=[1 0 0 1 0 0] [SVG] children: not-inline@@ -42,3 +43,4 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] childrenSC for Viewport<#document> [0,0 800x600] (z-index: auto)SC for BlockContainer<HTML> [0,0 800x499.203125] (z-index: auto)SC for BlockContainer<DIV>.timeline [8,445 854x5] (z-index: 1)+ SC for BlockContainer<DIV>.timeline-buffered [8,445 854x5] (z-index: auto)diff --git a/Tests/LibWeb/Layout/input/PictureInPicture/placeholder-stays-when-controls-are-added.html b/Tests/LibWeb/Layout/input/PictureInPicture/placeholder-stays-when-controls-are-added.htmlindex a03b3f5e0d5..ae6b6cf1425 100644--- a/Tests/LibWeb/Layout/input/PictureInPicture/placeholder-stays-when-controls-are-added.html+++ b/Tests/LibWeb/Layout/input/PictureInPicture/placeholder-stays-when-controls-are-added.html@@ -3,11 +3,12 @@<video src="../../../Assets/test-webm.webm"></video><script>const video = document.querySelector("video");+ const isWhollyBuffered = () => video.buffered.length === 1 && video.buffered.start(0) === 0 && video.buffered.end(0) >= video.duration;(async () => {- // Once loading is done, no later media event can redraw the new controls' timeline with its buffered range.- if (video.readyState < HTMLMediaElement.HAVE_ENOUGH_DATA)- await new Promise(resolve => video.addEventListener("canplaythrough", resolve, { once: true }));+ // The new controls draw the buffered range, which can still grow after the video can play through.+ while (!isWhollyBuffered())+ await new Promise(resolve => requestAnimationFrame(resolve));internals.grantTransientActivation();await video.requestPictureInPicture();diff --git a/Tests/LibWeb/Text/expected/HTML/media-controls-stop-updating-while-hidden.txt b/Tests/LibWeb/Text/expected/HTML/media-controls-stop-updating-while-hidden.txtnew file mode 100644index 00000000000..5615c05b180--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/media-controls-stop-updating-while-hidden.txt@@ -0,0 +1,2 @@+animation frame callbacks while the control bar is hidden: false+animation frame callbacks once the control bar shows: truediff --git a/Tests/LibWeb/Text/expected/PictureInPicture/hidden-controls-do-not-update-in-picture-in-picture.txt b/Tests/LibWeb/Text/expected/PictureInPicture/hidden-controls-do-not-update-in-picture-in-picture.txtnew file mode 100644index 00000000000..3346f5b07de--- /dev/null+++ b/Tests/LibWeb/Text/expected/PictureInPicture/hidden-controls-do-not-update-in-picture-in-picture.txt@@ -0,0 +1 @@+animation frame callbacks while playing in Picture-in-Picture: falsediff --git a/Tests/LibWeb/Text/input/HTML/media-controls-stop-updating-while-hidden.html b/Tests/LibWeb/Text/input/HTML/media-controls-stop-updating-while-hidden.htmlnew file mode 100644index 00000000000..41756885561--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/media-controls-stop-updating-while-hidden.html@@ -0,0 +1,29 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<style>+ body { margin: 0 }+ video { display: block; width: 320px; height: 180px }+</style>+<video controls muted loop src="../../../Assets/test-webm.webm"></video>+<script>+ function nextFrame() {+ return new Promise(resolve => requestAnimationFrame(resolve));+ }++ asyncTest(async done => {+ const video = document.querySelector("video");+ await video.play();++ // The middle of the video, away from its control bar, and a point below the video.+ internals.mouseMove(160, 60);+ internals.mouseMove(160, 400);+ await nextFrame();+ println(`animation frame callbacks while the control bar is hidden: ${internals.hasAnimationFrameCallbacks}`);++ internals.mouseMove(160, 60);+ println(`animation frame callbacks once the control bar shows: ${internals.hasAnimationFrameCallbacks}`);++ video.pause();+ done();+ });+</script>diff --git a/Tests/LibWeb/Text/input/PictureInPicture/hidden-controls-do-not-update-in-picture-in-picture.html b/Tests/LibWeb/Text/input/PictureInPicture/hidden-controls-do-not-update-in-picture-in-picture.htmlnew file mode 100644index 00000000000..c70a381f1b2--- /dev/null+++ b/Tests/LibWeb/Text/input/PictureInPicture/hidden-controls-do-not-update-in-picture-in-picture.html@@ -0,0 +1,19 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<video muted loop src="../../../Assets/test-webm.webm"></video>+<script>+ asyncTest(async done => {+ const video = document.querySelector("video");+ await video.play();++ // The video has no controls of its own, so the controls that show its placeholder never show a control bar.+ internals.grantTransientActivation();+ await video.requestPictureInPicture();+ await new Promise(resolve => requestAnimationFrame(resolve));+ println(`animation frame callbacks while playing in Picture-in-Picture: ${internals.hasAnimationFrameCallbacks}`);++ video.pause();+ await document.exitPictureInPicture();+ done();+ });+</script>