Truly independent web browser
LibWebView+UI/Qt: Scale a Picture-in-Picture window's frames to fit
Luke Wildef42e98d78d31
diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cppindex 03206d2f228..8fc95f3b520 100644--- a/Libraries/LibWebView/ViewImplementation.cpp+++ b/Libraries/LibWebView/ViewImplementation.cpp@@ -2392,10 +2392,22 @@ void ViewImplementation::set_device_pixel_ratio_for_testing(WebContentPage& pagepage.async_set_viewport(viewport_size(), m_device_pixel_ratio, m_is_fullscreen);}+Gfx::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 });+}+void ViewImplementation::handle_resize(){page().async_set_viewport(viewport_size(), m_device_pixel_ratio, m_is_fullscreen);- Application::the().update_compositor_viewport(page().compositor_context_id(), viewport_size().to_type<int>(), Compositing::WindowResizingInProgress::Yes);++ // 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.+ if (!m_scales_frames_to_fit)+ Application::the().update_compositor_viewport(page().compositor_context_id(), viewport_size().to_type<int>(), Compositing::WindowResizingInProgress::Yes);+if (m_debugger_paused) {m_debugger_overlay_pointer_state.cancel();if (m_debugger_overlay_hovered_action.has_value())diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.hindex 397a9a5d7b2..86dc5d4944f 100644--- a/Libraries/LibWebView/ViewImplementation.h+++ b/Libraries/LibWebView/ViewImplementation.h@@ -195,6 +195,7 @@ public:double device_pixel_ratio() const { return m_device_pixel_ratio; }Optional<u64> display_id() const { return m_display_id; }double maximum_frames_per_second() const { return m_maximum_frames_per_second; }+ void set_scales_frames_to_fit(bool scales_frames_to_fit) { m_scales_frames_to_fit = scales_frames_to_fit; }void enqueue_input_event(Web::InputEvent);// The page reporting on an event must be the one handling it.void did_finish_handling_input_event(Badge<WebContentPage>, WebContentPage const& reporting_page, u64 event_id, Web::EventResult event_result);@@ -586,6 +587,7 @@ protected:virtual bool defer_backing_store_release(i32) { return false; }virtual void did_accept_presented_backing_store(i32, Gfx::IntRect) { }void release_backing_store(i32 bitmap_id);+ static Gfx::IntRect rect_fitting_frame(Gfx::IntSize frame_size, Gfx::IntSize target_size);static constexpr auto ZOOM_STEP = 0.1;@@ -725,6 +727,7 @@ protected:double m_device_pixel_ratio { 1.0 };Optional<u64> m_display_id;double m_maximum_frames_per_second { 60.0 };+ bool m_scales_frames_to_fit { false };RefPtr<Menu> m_page_context_menu;RefPtr<Menu> m_link_context_menu;diff --git a/UI/Qt/PictureInPictureWindow.cpp b/UI/Qt/PictureInPictureWindow.cppindex f05d7bdcffa..9d99eb759e6 100644--- a/UI/Qt/PictureInPictureWindow.cpp+++ b/UI/Qt/PictureInPictureWindow.cpp@@ -85,6 +85,10 @@ public:page_did_handle_press(result);};+ // The page lays out a new window size only once its main thread is free, so until then its last frame is+ // scaled to keep the video filling the window.+ m_view->set_scales_frames_to_fit(true);+auto* layout = new QVBoxLayout(this);layout->setContentsMargins(0, 0, 0, 0);layout->addWidget(m_view);diff --git a/UI/Qt/WebContentView.cpp b/UI/Qt/WebContentView.cppindex 6734be8c5e3..2f9e9660d4e 100644--- a/UI/Qt/WebContentView.cpp+++ b/UI/Qt/WebContentView.cpp@@ -1060,10 +1060,21 @@ void WebContentView::paintEvent(QPaintEvent*)if (bitmap) {QImage q_image(bitmap->scanline_u8(0), bitmap->width(), bitmap->height(), bitmap->pitch(), QImage::Format_RGB32);- painter.drawImage(QPoint(0, 0), q_image, QRect(0, 0, bitmap_size.width(), bitmap_size.height()));+ QRect source_rect(0, 0, bitmap_size.width(), bitmap_size.height());auto background_color = page_background_color();auto fallback_color = QColor(background_color.red(), background_color.green(), background_color.blue());++ if (m_scales_frames_to_fit && !bitmap_size.is_empty()) {+ auto destination = rect_fitting_frame(bitmap_size, m_viewport_size);+ painter.fillRect(QRect(0, 0, m_viewport_size.width(), m_viewport_size.height()), fallback_color);+ painter.setRenderHint(QPainter::SmoothPixmapTransform);+ painter.drawImage(QRect(destination.x(), destination.y(), destination.width(), destination.height()), q_image, source_rect);+ return;+ }++ painter.drawImage(QPoint(0, 0), q_image, source_rect);+if (bitmap_size.width() < m_viewport_size.width()) {painter.fillRect(bitmap_size.width(), 0, m_viewport_size.width() - bitmap_size.width(), bitmap->height(), fallback_color);}diff --git a/UI/Qt/WebContentViewLinux.cpp b/UI/Qt/WebContentViewLinux.cppindex 791a80601c6..45fd3ab5379 100644--- a/UI/Qt/WebContentViewLinux.cpp+++ b/UI/Qt/WebContentViewLinux.cpp@@ -712,7 +712,7 @@ struct WebContentView::VulkanRenderer {return imported_texture;}- bool render(VkCommandBuffer command_buffer, Gfx::SharedImageBuffer const& shared_image_buffer, Gfx::IntSize bitmap_size, QSize target_size)+ bool render(VkCommandBuffer command_buffer, Gfx::SharedImageBuffer const& shared_image_buffer, Gfx::IntSize bitmap_size, QSize target_size, bool scale_to_fit){auto* imported_texture = imported_texture_for(shared_image_buffer);if (!imported_texture)@@ -721,22 +721,30 @@ struct WebContentView::VulkanRenderer {if (command_buffer == VK_NULL_HANDLE)return false;- auto content_width = min(bitmap_size.width(), target_size.width());- auto content_height = min(bitmap_size.height(), target_size.height());+ auto content_limit = scale_to_fit ? imported_texture->size : Gfx::IntSize { target_size.width(), target_size.height() };+ auto content_width = min(bitmap_size.width(), content_limit.width());+ auto content_height = min(bitmap_size.height(), content_limit.height());if (content_width <= 0 || content_height <= 0)return false;+ // The shader maps content pixels one-to-one onto the viewport, so scaling to fit shrinks the viewport to the+ // fitted rect and makes the content its whole extent.+ Gfx::IntRect viewport_rect { 0, 0, target_size.width(), target_size.height() };+ if (scale_to_fit)+ viewport_rect = rect_fitting_frame({ content_width, content_height }, viewport_rect.size());+ auto quad_target_size = scale_to_fit ? Gfx::IntSize { content_width, content_height } : viewport_rect.size();+PushConstants push_constants {- { static_cast<float>(target_size.width()), static_cast<float>(target_size.height()) },+ { static_cast<float>(quad_target_size.width()), static_cast<float>(quad_target_size.height()) },{ static_cast<float>(content_width), static_cast<float>(content_height) },{ static_cast<float>(imported_texture->size.width()), static_cast<float>(imported_texture->size.height()) },};VkViewport viewport {- .x = 0.0f,- .y = 0.0f,- .width = static_cast<float>(target_size.width()),- .height = static_cast<float>(target_size.height()),+ .x = static_cast<float>(viewport_rect.x()),+ .y = static_cast<float>(viewport_rect.y()),+ .width = static_cast<float>(viewport_rect.width()),+ .height = static_cast<float>(viewport_rect.height()),.minDepth = 0.0f,.maxDepth = 1.0f,};@@ -855,7 +863,7 @@ struct WebContentView::VulkanWindowRenderer final : public QVulkanWindowRenderer};vkCmdBeginRenderPass(command_buffer, &render_pass_begin_info, VK_SUBPASS_CONTENTS_INLINE);- bool rendered = m_renderer.render(command_buffer, *paintable->shared_image_buffer, paintable->bitmap_size, target_size);+ bool rendered = m_renderer.render(command_buffer, *paintable->shared_image_buffer, paintable->bitmap_size, target_size, m_view.m_scales_frames_to_fit);if (rendered && m_view.m_vulkan_window_supports_alpha_blending.value_or(false)) {// Clear the strips overlaid by hover-expanded vertical tabs to transparent so the tab column painted in thediff --git a/UI/Qt/WebContentViewMac.mm b/UI/Qt/WebContentViewMac.mmindex 1001f71ee49..5e43a6022e8 100644--- a/UI/Qt/WebContentViewMac.mm+++ b/UI/Qt/WebContentViewMac.mm@@ -91,10 +91,12 @@ void WebContentView::present_current_paintable_as_layer_contents()auto painted_unit_height = static_cast<CGFloat>(painted_height) / surface_height;// The surface can be larger than what was painted into it (a live resize pads it), so only the painted part is- // shown, one surface pixel per device pixel, anchored at the visual top-left. Inside Qt's flipped view the y-axis- // of this layer points down, which turns both the gravity constant and the unit rectangle upside down.+ // shown: unless frames are scaled to fit, one surface pixel per device pixel, anchored at the visual top-left.+ // Inside Qt's flipped view the y-axis of this layer points down, which turns both the top-left gravity constant+ // and the unit rectangle upside down.bool y_axis_points_down = [layer contentsAreFlipped];- layer.contentsGravity = y_axis_points_down ? kCAGravityBottomLeft : kCAGravityTopLeft;+ auto top_left_gravity = y_axis_points_down ? kCAGravityBottomLeft : kCAGravityTopLeft;+ layer.contentsGravity = m_scales_frames_to_fit ? kCAGravityResizeAspect : top_left_gravity;layer.contentsRect = y_axis_points_down? CGRectMake(0, 0, painted_unit_width, painted_unit_height): CGRectMake(0, 1 - painted_unit_height, painted_unit_width, painted_unit_height);diff --git a/UI/Qt/WebContentViewWindows.cpp b/UI/Qt/WebContentViewWindows.cppindex 93ef2d4d857..9b37316a933 100644--- a/UI/Qt/WebContentViewWindows.cpp+++ b/UI/Qt/WebContentViewWindows.cpp@@ -113,6 +113,7 @@ void WebContentView::render(QRhiCommandBuffer* command_buffer)if (content_width <= 0 || content_height <= 0)return;+ // FIXME: Scale the backing store to fit when the view scales frames to fit, which needs a draw rather than a copy.if (auto const* d3d_handle = paintable->shared_image_buffer->windows_d3d_handle()) {auto* imported_texture = imported_d3d_texture_for(*paintable->shared_image_buffer, *d3d_handle);if (!imported_texture) {