Truly independent web browser
Everywhere: Fit frames into hosted picture-in-picture views
Aliaksandr Kalenik575d6c76c2d1
diff --git a/Libraries/LibCompositing/FrameFitting.h b/Libraries/LibCompositing/FrameFitting.hnew file mode 100644index 00000000000..9465e9b7fc5--- /dev/null+++ b/Libraries/LibCompositing/FrameFitting.h@@ -0,0 +1,24 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <AK/StdLibExtras.h>+#include <LibGfx/Rect.h>+#include <LibGfx/Size.h>++namespace Compositing {++// Where a frame shows when it is scaled to fit within the target, keeping its aspect ratio, in the target's middle.+inline Gfx::IntRect rect_fitting_frame(Gfx::IntSize frame_size, Gfx::IntSize target_size)+{+ auto scale = min(static_cast<float>(target_size.width()) / frame_size.width(), static_cast<float>(target_size.height()) / frame_size.height());+ auto fitted_size = frame_size.to_type<float>().scaled(scale).to_rounded<int>();+ fitted_size = { max(1, fitted_size.width()), max(1, fitted_size.height()) };+ return Gfx::IntRect { {}, fitted_size }.centered_within({ {}, target_size });+}++}diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cppindex 95601cd64ef..a868ae0b562 100644--- a/Libraries/LibWebView/Application.cpp+++ b/Libraries/LibWebView/Application.cpp@@ -1500,6 +1500,15 @@ void Application::show_context_in_remote_layer(u64 remote_layer_id, Optional<Webm_compositor_client->async_show_context_in_remote_layer(remote_layer_id, context_id);}+void Application::set_remote_layer_fit(u64 remote_layer_id, Optional<Gfx::IntSize> fit_within)+{+ if (!can_send_compositor_process_ipc(m_compositor_client))+ return;+ VERIFY(m_compositor_client);++ m_compositor_client->async_set_remote_layer_fit(remote_layer_id, fit_within);+}+void Application::take_frame_snapshot(Web::CompositorContextId context_id, Optional<Gfx::IntSize> maximum_size, Function<void(Optional<Gfx::ShareableBitmap>)> callback){if (!m_compositor_presentation_client) {diff --git a/Libraries/LibWebView/Application.h b/Libraries/LibWebView/Application.hindex 4ae89337cd9..2261bae5537 100644--- a/Libraries/LibWebView/Application.h+++ b/Libraries/LibWebView/Application.h@@ -275,6 +275,7 @@ public:Optional<RemoteLayer> create_remote_layer();void destroy_remote_layer(u64 remote_layer_id);void show_context_in_remote_layer(u64 remote_layer_id, Optional<Web::CompositorContextId>);+ void set_remote_layer_fit(u64 remote_layer_id, Optional<Gfx::IntSize> fit_within);void take_frame_snapshot(Web::CompositorContextId, Optional<Gfx::IntSize> maximum_size, Function<void(Optional<Gfx::ShareableBitmap>)>);bool handle_and_dispatch_key_event_in_compositor(Web::CompositorContextId, Web::KeyEvent const&, bool allow_keyboard_scroll);bool handle_and_dispatch_pinch_event_in_compositor(Web::CompositorContextId, Web::PinchEvent const&);diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cppindex 59ec4d6b2f4..332279fc2bc 100644--- a/Libraries/LibWebView/ViewImplementation.cpp+++ b/Libraries/LibWebView/ViewImplementation.cpp@@ -14,6 +14,7 @@#include <AK/ScopeGuard.h>#include <AK/String.h>#include <AK/Time.h>+#include <LibCompositing/FrameFitting.h>#include <LibCompositing/PausedDebuggerOverlay.h>#include <LibCore/EventLoop.h>#include <LibCore/StandardPaths.h>@@ -474,6 +475,7 @@ void ViewImplementation::set_hosts_remote_layer(bool hosts_remote_layer)return;}m_remote_layer_id = remote_layer->id;+ update_remote_layer_fit();Application::the().show_context_in_remote_layer(remote_layer->id, page().compositor_context_id());did_create_remote_layer(remote_layer->core_animation_context_id);}@@ -483,6 +485,23 @@ void ViewImplementation::take_frame_snapshot(Optional<Gfx::IntSize> maximum_sizeApplication::the().take_frame_snapshot(page().compositor_context_id(), maximum_size, move(callback));}+void ViewImplementation::set_scales_frames_to_fit(bool scales_frames_to_fit)+{+ m_scales_frames_to_fit = scales_frames_to_fit;+ update_remote_layer_fit();+}++// The Compositor fits the frames into the view itself, for a view that scales them to fit.+void ViewImplementation::update_remote_layer_fit()+{+ if (!m_remote_layer_id.has_value())+ return;+ Optional<Gfx::IntSize> fit_within;+ if (m_scales_frames_to_fit && !viewport_size().is_empty())+ fit_within = viewport_size().to_type<int>();+ Application::the().set_remote_layer_fit(*m_remote_layer_id, fit_within);+}+void ViewImplementation::recreate_remote_layer_after_compositor_reconnect(Badge<WebContentClient>){if (!m_hosts_remote_layer)@@ -3104,14 +3123,13 @@ void ViewImplementation::set_device_pixel_ratio_for_testing(WebContentPage& pageGfx::IntRect ViewImplementation::rect_fitting_frame(Gfx::IntSize frame_size, Gfx::IntSize target_size){- auto scale = min(static_cast<float>(target_size.width()) / frame_size.width(), static_cast<float>(target_size.height()) / frame_size.height());- auto fitted_size = frame_size.to_type<float>().scaled(scale).to_rounded<int>();- return Gfx::IntRect { {}, fitted_size }.centered_within({ {}, target_size });+ return Compositing::rect_fitting_frame(frame_size, target_size);}void ViewImplementation::handle_resize(){page().async_set_viewport(viewport_size(), m_device_pixel_ratio, m_is_fullscreen);+ update_remote_layer_fit();// The page forwards its new size to the compositor once it has laid it out. Until then, scaling frames to fit needs// the compositor to keep presenting them whole at the size they were laid out for.diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.hindex f428a5048fa..3fb8cff07b0 100644--- a/Libraries/LibWebView/ViewImplementation.h+++ b/Libraries/LibWebView/ViewImplementation.h@@ -306,7 +306,7 @@ public:};// Sends the screens that screen_rects() reports to every page hosting documents of the tab.void update_screen_rects();- void set_scales_frames_to_fit(bool scales_frames_to_fit) { m_scales_frames_to_fit = scales_frames_to_fit; }+ void set_scales_frames_to_fit(bool);void enable_picture_in_picture_controls();void enqueue_input_event(Web::InputEvent);// The page reporting on an event must be the one handling it.@@ -901,6 +901,7 @@ protected:bool m_presents_from_cpu_memory { false };bool m_hosts_remote_layer { false };Optional<u64> m_remote_layer_id;+ void update_remote_layer_fit();// Set once an assistive technology asked for the page tree; every display page the view shows from then on gets// asked for its tree too.diff --git a/Services/Compositor/CompositorControlServer.ipc b/Services/Compositor/CompositorControlServer.ipcindex 920a2893ee5..33c026da33a 100644--- a/Services/Compositor/CompositorControlServer.ipc+++ b/Services/Compositor/CompositorControlServer.ipc@@ -19,6 +19,7 @@ endpoint CompositorControlServercreate_remote_layer(u64 presentation_id, u64 remote_layer_id) => (u32 core_animation_context_id)destroy_remote_layer(u64 remote_layer_id) =|show_context_in_remote_layer(u64 remote_layer_id, Optional<Web::CompositorContextId> context_id) =|+ set_remote_layer_fit(u64 remote_layer_id, Optional<Gfx::IntSize> fit_within) =|create_context(Web::CompositorContextId context_id, Optional<u64> page_id, i32 web_content_connection_id) => ()diff --git a/Services/Compositor/CompositorState.cpp b/Services/Compositor/CompositorState.cppindex 8162842aa43..a8ea0bb2d46 100644--- a/Services/Compositor/CompositorState.cpp+++ b/Services/Compositor/CompositorState.cpp@@ -204,7 +204,7 @@ u32 CompositorState::create_remote_layer(CompositorStateClient& presentation, u6if (!layer)return 0;auto core_animation_context_id = layer->core_animation_context_id();- m_remote_layers.set(remote_layer_id, { .presentation = &presentation, .layer = move(layer), .shown_context_id = {} });+ m_remote_layers.set(remote_layer_id, { .presentation = &presentation, .layer = move(layer), .shown_context_id = {}, .fit_within = {} });return core_animation_context_id;}@@ -249,6 +249,15 @@ void CompositorState::show_context_in_remote_layer(u64 remote_layer_id, Optionalschedule_pending_present_frame_if_unblocked(*context_id, *context);}+void CompositorState::set_remote_layer_fit(u64 remote_layer_id, Optional<Gfx::IntSize> fit_within)+{+ auto remote_layer = m_remote_layers.find(remote_layer_id);+ if (remote_layer == m_remote_layers.end() || remote_layer->value.fit_within == fit_within)+ return;+ remote_layer->value.fit_within = fit_within;+ remote_layer->value.layer->place_frame(fit_within);+}+void CompositorState::stop_showing_context_in_remote_layer(Web::CompositorContextId context_id, PresentedContext& context){context.remote_layer_id = {};@@ -1418,7 +1427,7 @@ void CompositorState::show_frame_in_remote_layer(Web::CompositorContextId contexauto const* shared_image_buffer = context.presenter.shared_image_buffer_of_latest_rendered_store();VERIFY(shared_image_buffer);- remote_layer->value.layer->show_frame(*shared_image_buffer, pending_present.viewport_rect.size(), pending_present.device_pixel_ratio);+ remote_layer->value.layer->show_frame(*shared_image_buffer, pending_present.viewport_rect.size(), pending_present.device_pixel_ratio, remote_layer->value.fit_within);remote_layer->value.presentation->did_present_frame_in_remote_layer(context_id, pending_present.viewport_rect, pending_present.damage_rect, pending_present.device_pixel_ratio);}diff --git a/Services/Compositor/CompositorState.h b/Services/Compositor/CompositorState.hindex 59c71a0e2ea..3442d761a21 100644--- a/Services/Compositor/CompositorState.h+++ b/Services/Compositor/CompositorState.h@@ -163,6 +163,8 @@ public:void destroy_remote_layer(u64 remote_layer_id);// The layer keeps the frame it shows until the context it is given next presents one.void show_context_in_remote_layer(u64 remote_layer_id, Optional<Web::CompositorContextId>);+ // A view that scales the frames to fit it gives its size, in device pixels.+ void set_remote_layer_fit(u64 remote_layer_id, Optional<Gfx::IntSize> fit_within);struct PlaceholderCanvasAllocation {Compositing::CanvasId canvas_id;@@ -318,6 +320,7 @@ private:CompositorStateClient* presentation { nullptr };OwnPtr<RemoteLayer> layer;Optional<Web::CompositorContextId> shown_context_id;+ Optional<Gfx::IntSize> fit_within;};HashMap<u64, RemoteLayerState> m_remote_layers;diff --git a/Services/Compositor/ConnectionFromClient.cpp b/Services/Compositor/ConnectionFromClient.cppindex e6c555e513e..f2877572b97 100644--- a/Services/Compositor/ConnectionFromClient.cpp+++ b/Services/Compositor/ConnectionFromClient.cpp@@ -110,6 +110,15 @@ void ConnectionFromClient::show_context_in_remote_layer(u64 remote_layer_id, Optm_compositor_state->show_context_in_remote_layer(remote_layer_id, context_id);}+void ConnectionFromClient::set_remote_layer_fit(u64 remote_layer_id, Optional<Gfx::IntSize> fit_within)+{+ if (fit_within.has_value() && fit_within->is_empty()) {+ did_misbehave("Empty size for a remote layer to fit frames within");+ return;+ }+ m_compositor_state->set_remote_layer_fit(remote_layer_id, fit_within);+}+void ConnectionFromClient::create_context(Web::CompositorContextId context_id, Optional<u64> page_id, i32 web_content_connection_id){auto* connection = web_content_connection(web_content_connection_id);diff --git a/Services/Compositor/ConnectionFromClient.h b/Services/Compositor/ConnectionFromClient.hindex b911b10d218..878ef380eb1 100644--- a/Services/Compositor/ConnectionFromClient.h+++ b/Services/Compositor/ConnectionFromClient.h@@ -38,6 +38,7 @@ private:virtual Messages::CompositorControlServer::CreateRemoteLayerResponse create_remote_layer(u64 presentation_id, u64 remote_layer_id) override;virtual void destroy_remote_layer(u64 remote_layer_id) override;virtual void show_context_in_remote_layer(u64 remote_layer_id, Optional<Web::CompositorContextId>) override;+ virtual void set_remote_layer_fit(u64 remote_layer_id, Optional<Gfx::IntSize> fit_within) override;virtual void create_context(Web::CompositorContextId, Optional<u64> page_id, i32 web_content_connection_id) override;virtual void viewport_size_updated(Web::CompositorContextId, Gfx::IntSize, double device_pixel_ratio, Compositing::WindowResizingInProgress) override;virtual void set_ui_overlays(Web::CompositorContextId, Compositing::UIOverlays) override;diff --git a/Services/Compositor/RemoteLayer.h b/Services/Compositor/RemoteLayer.hindex e416d106c6d..048d325ed50 100644--- a/Services/Compositor/RemoteLayer.h+++ b/Services/Compositor/RemoteLayer.h@@ -7,6 +7,7 @@#pragma once#include <AK/Noncopyable.h>+#include <AK/Optional.h>#include <AK/OwnPtr.h>#include <AK/Platform.h>#include <AK/Types.h>@@ -31,16 +32,23 @@ public:u32 core_animation_context_id() const { return m_core_animation_context_id; }- // Shows the part of the buffer that the frame was painted into, at the top left of the layer.- void show_frame(Gfx::SharedImageBuffer const&, Gfx::IntSize painted_size, double device_pixel_ratio);+ // Shows the part of the buffer that the frame was painted into, at the top left of the layer at its own size, or+ // scaled to fit within the size of the view given, in device pixels.+ void show_frame(Gfx::SharedImageBuffer const&, Gfx::IntSize painted_size, double device_pixel_ratio, Optional<Gfx::IntSize> fit_within);+ // Places the frame on display again, as when the view that it is scaled into changes size.+ void place_frame(Optional<Gfx::IntSize> fit_within);private:RemoteLayer() = default;#ifdef AK_OS_MACOS+ void lay_out_frame(Optional<Gfx::IntSize> fit_within);+void* m_context { nullptr };void* m_root_layer { nullptr };void* m_content_layer { nullptr };+ Gfx::IntSize m_painted_size;+ double m_device_pixel_ratio { 1.0 };#endifu32 m_core_animation_context_id { 0 };};@@ -53,7 +61,11 @@ inline OwnPtr<RemoteLayer> RemoteLayer::try_create()inline RemoteLayer::~RemoteLayer() = default;-inline void RemoteLayer::show_frame(Gfx::SharedImageBuffer const&, Gfx::IntSize, double)+inline void RemoteLayer::show_frame(Gfx::SharedImageBuffer const&, Gfx::IntSize, double, Optional<Gfx::IntSize>)+{+}++inline void RemoteLayer::place_frame(Optional<Gfx::IntSize>){}#endifdiff --git a/Services/Compositor/RemoteLayer.mm b/Services/Compositor/RemoteLayer.mmindex 44752564ab2..4c2e7b81ca5 100644--- a/Services/Compositor/RemoteLayer.mm+++ b/Services/Compositor/RemoteLayer.mm@@ -6,6 +6,7 @@#include <AK/StdLibExtras.h>#include <Compositor/RemoteLayer.h>+#include <LibCompositing/FrameFitting.h>#include <LibGfx/SharedImageBuffer.h>#include <dlfcn.h>@@ -110,7 +111,30 @@ RemoteLayer::~RemoteLayer()});}-void RemoteLayer::show_frame(Gfx::SharedImageBuffer const& buffer, Gfx::IntSize painted_size, double device_pixel_ratio)+static CGRect in_points(Gfx::IntRect rect, double device_pixel_ratio)+{+ return CGRectMake(rect.x() / device_pixel_ratio, rect.y() / device_pixel_ratio, rect.width() / device_pixel_ratio, rect.height() / device_pixel_ratio);+}++// The root layer spans the view a frame is fitted into, as its y-axis is flipped within its own height.+void RemoteLayer::lay_out_frame(Optional<Gfx::IntSize> fit_within)+{+ Gfx::IntRect root_rect { {}, fit_within.value_or(m_painted_size) };+ auto content_rect = fit_within.has_value() ? Compositing::rect_fitting_frame(m_painted_size, *fit_within) : root_rect;+ as_layer(m_root_layer).frame = in_points(root_rect, m_device_pixel_ratio);+ as_layer(m_content_layer).frame = in_points(content_rect, m_device_pixel_ratio);+}++void RemoteLayer::place_frame(Optional<Gfx::IntSize> fit_within)+{+ if (m_painted_size.is_empty())+ return;+ commit_layer_changes([&] {+ lay_out_frame(fit_within);+ });+}++void RemoteLayer::show_frame(Gfx::SharedImageBuffer const& buffer, Gfx::IntSize painted_size, double device_pixel_ratio, Optional<Gfx::IntSize> fit_within){auto const& iosurface = buffer.iosurface_handle();auto surface_width = static_cast<int>(iosurface.width());@@ -119,6 +143,8 @@ void RemoteLayer::show_frame(Gfx::SharedImageBuffer const& buffer, Gfx::IntSizeauto painted_height = min(painted_size.height(), surface_height);if (painted_width <= 0 || painted_height <= 0)return;+ m_painted_size = { painted_width, painted_height };+ m_device_pixel_ratio = device_pixel_ratio;commit_layer_changes([&] {auto* content_layer = as_layer(m_content_layer);@@ -130,9 +156,7 @@ void RemoteLayer::show_frame(Gfx::SharedImageBuffer const& buffer, Gfx::IntSize? CGRectMake(0, 0, painted_unit_width, painted_unit_height): CGRectMake(0, 1 - painted_unit_height, painted_unit_width, painted_unit_height);content_layer.contentsScale = device_pixel_ratio;- auto frame = CGRectMake(0, 0, painted_width / device_pixel_ratio, painted_height / device_pixel_ratio);- content_layer.frame = frame;- as_layer(m_root_layer).frame = frame;+ lay_out_frame(fit_within);content_layer.contents = (__bridge id)iosurface.core_foundation_pointer();});}diff --git a/UI/Qt/WebContentViewMac.mm b/UI/Qt/WebContentViewMac.mmindex c4132f3ec4c..dcc47ae98b4 100644--- a/UI/Qt/WebContentViewMac.mm+++ b/UI/Qt/WebContentViewMac.mm@@ -123,8 +123,7 @@ bool WebContentView::ensure_iosurface_layer_attached_to_native_view()update_remote_layer_host();}- // A view that scales its frames to fit, as for Picture-in-Picture, goes on showing the backing stores it is given.- if (!m_asked_for_remote_layer && !scales_frames_to_fit() && can_host_remote_layers()) {+ if (!m_asked_for_remote_layer && can_host_remote_layers()) {m_asked_for_remote_layer = true;set_hosts_remote_layer(true);}