Truly independent web browser
Everywhere: Stop scaling down frame snapshots in the Compositor
Andreas Klingc339f61ea1b0
diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cppindex 57a810a1e67..6af01a32a47 100644--- a/Libraries/LibWebView/Application.cpp+++ b/Libraries/LibWebView/Application.cpp@@ -1654,13 +1654,13 @@ void Application::commit_synchronized_resize(u64 remote_layer_id, u64 token)m_compositor_client->async_commit_synchronized_resize(remote_layer_id, token);}-void Application::take_frame_snapshot(Web::CompositorContextId context_id, Optional<Gfx::IntSize> maximum_size, Function<void(Optional<Gfx::ShareableBitmap>)> callback)+void Application::take_frame_snapshot(Web::CompositorContextId context_id, Function<void(Optional<Gfx::ShareableBitmap>)> callback){if (!m_compositor_presentation_client) {callback({});return;}- m_compositor_presentation_client->take_frame_snapshot(context_id, maximum_size, move(callback));+ m_compositor_presentation_client->take_frame_snapshot(context_id, move(callback));}void Application::update_compositor_context_presents_from_cpu_memory(Web::CompositorContextId context_id, bool presents_from_cpu_memory)diff --git a/Libraries/LibWebView/Application.h b/Libraries/LibWebView/Application.hindex c98c84f6550..4e4728fb917 100644--- a/Libraries/LibWebView/Application.h+++ b/Libraries/LibWebView/Application.h@@ -299,7 +299,7 @@ public:void begin_synchronized_resize(u64 remote_layer_id, u64 token);void await_synchronized_resize_frame(u64 remote_layer_id, u64 token, Gfx::IntSize);void commit_synchronized_resize(u64 remote_layer_id, u64 token);- void take_frame_snapshot(Web::CompositorContextId, Optional<Gfx::IntSize> maximum_size, Function<void(Optional<Gfx::ShareableBitmap>)>);+ void take_frame_snapshot(Web::CompositorContextId, 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&);bool handle_and_dispatch_mouse_event_in_compositor(Web::CompositorContextId, Web::MouseEvent const&);diff --git a/Libraries/LibWebView/CompositorPresentationClient.cpp b/Libraries/LibWebView/CompositorPresentationClient.cppindex 5aa8e348f1c..e8e37a7a5ce 100644--- a/Libraries/LibWebView/CompositorPresentationClient.cpp+++ b/Libraries/LibWebView/CompositorPresentationClient.cpp@@ -79,11 +79,11 @@ void CompositorPresentationClient::did_present_frame_in_remote_layer(Web::Compospage->did_present_frame_in_remote_layer(content_rect, damage_rect, device_pixel_ratio);}-void CompositorPresentationClient::take_frame_snapshot(Web::CompositorContextId context_id, Optional<Gfx::IntSize> maximum_size, FrameSnapshotCallback callback)+void CompositorPresentationClient::take_frame_snapshot(Web::CompositorContextId context_id, FrameSnapshotCallback callback){auto request_id = m_next_frame_snapshot_request_id++;m_pending_frame_snapshots.set(request_id, move(callback));- async_take_frame_snapshot(context_id, request_id, maximum_size);+ async_take_frame_snapshot(context_id, request_id);}void CompositorPresentationClient::fail_pending_frame_snapshots()diff --git a/Libraries/LibWebView/CompositorPresentationClient.h b/Libraries/LibWebView/CompositorPresentationClient.hindex 7e4aeacdb92..14511c75b54 100644--- a/Libraries/LibWebView/CompositorPresentationClient.h+++ b/Libraries/LibWebView/CompositorPresentationClient.h@@ -33,7 +33,7 @@ public:using FrameSnapshotCallback = Function<void(Optional<Gfx::ShareableBitmap>)>;// The frame the context shows, scaled down to fit within the size given. None if it has no frame, or if the// Compositor goes away first.- void take_frame_snapshot(Web::CompositorContextId, Optional<Gfx::IntSize> maximum_size, FrameSnapshotCallback);+ void take_frame_snapshot(Web::CompositorContextId, FrameSnapshotCallback);void fail_pending_frame_snapshots();private:diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cppindex be015946143..3969520f4c0 100644--- a/Libraries/LibWebView/ViewImplementation.cpp+++ b/Libraries/LibWebView/ViewImplementation.cpp@@ -482,9 +482,9 @@ void ViewImplementation::set_hosts_remote_layer(bool hosts_remote_layer)did_create_remote_layer(remote_layer->host_handle);}-void ViewImplementation::take_frame_snapshot(Optional<Gfx::IntSize> maximum_size, Function<void(Optional<Gfx::ShareableBitmap>)> callback)+void ViewImplementation::take_frame_snapshot(Function<void(Optional<Gfx::ShareableBitmap>)> callback){- Application::the().take_frame_snapshot(page().compositor_context_id(), maximum_size, move(callback));+ Application::the().take_frame_snapshot(page().compositor_context_id(), move(callback));}ViewImplementation* ViewImplementation::find_view_showing_remote_layer(u64 remote_layer_id)diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.hindex 815ccd8e5e1..55f7571706c 100644--- a/Libraries/LibWebView/ViewImplementation.h+++ b/Libraries/LibWebView/ViewImplementation.h@@ -236,7 +236,7 @@ public:// than in backing stores the view receives.void set_hosts_remote_layer(bool);// What the view shows, from the Compositor, scaled down to fit within the size given.- void take_frame_snapshot(Optional<Gfx::IntSize> maximum_size, Function<void(Optional<Gfx::ShareableBitmap>)>);+ void take_frame_snapshot(Function<void(Optional<Gfx::ShareableBitmap>)>);// The Compositor's remote layers went with its process, so the view asks the new one for another.void recreate_remote_layer_after_compositor_reconnect(Badge<WebContentClient>);static ViewImplementation* find_view_showing_remote_layer(u64 remote_layer_id);diff --git a/Services/Compositor/CompositorPresentationServer.ipc b/Services/Compositor/CompositorPresentationServer.ipcindex 95436ac5a38..556bd6ba1e2 100644--- a/Services/Compositor/CompositorPresentationServer.ipc+++ b/Services/Compositor/CompositorPresentationServer.ipc@@ -8,7 +8,7 @@ endpoint CompositorPresentationServer[RustReceiver] handle_and_dispatch_pinch_event(Web::CompositorContextId context_id, Web::PinchEvent event) =|[RustReceiver] handle_and_dispatch_mouse_event(Web::CompositorContextId context_id, Web::MouseEvent event) =|presented_bitmap_ready_to_paint(Web::CompositorContextId context_id, i32 bitmap_id) =|- take_frame_snapshot(Web::CompositorContextId context_id, u64 request_id, Optional<Gfx::IntSize> maximum_size) =|+ take_frame_snapshot(Web::CompositorContextId context_id, u64 request_id) =|set_client_gpu_presentation_capability(bool supported, u64 adapter_luid) =|set_scrollbar_track_press_jumps(bool jumps) =|}diff --git a/Services/Compositor/CompositorState.cpp b/Services/Compositor/CompositorState.cppindex ea8febd1465..e34ab43af67 100644--- a/Services/Compositor/CompositorState.cpp+++ b/Services/Compositor/CompositorState.cpp@@ -1011,62 +1011,19 @@ void CompositorState::finish_screenshot_preparation(u64 preparation_id)requester->tell_remote_descendants_prepared_for_screenshot(finished->request_id);}-static Gfx::IntSize size_fitting_within(Gfx::IntSize size, Gfx::IntSize maximum_size)-{- if (size.width() <= maximum_size.width() && size.height() <= maximum_size.height())- return size;- auto scale = min(static_cast<double>(maximum_size.width()) / size.width(), static_cast<double>(maximum_size.height()) / size.height());- return { max(1, static_cast<int>(round(size.width() * scale))), max(1, static_cast<int>(round(size.height() * scale))) };-}--Optional<Gfx::ShareableBitmap> CompositorState::take_frame_snapshot(Web::CompositorContextId context_id, Optional<Gfx::IntSize> maximum_size)+Optional<Gfx::ShareableBitmap> CompositorState::take_frame_snapshot(Web::CompositorContextId context_id){if (!presented_context_if_present(context_id))return {};auto presented_frame = m_core.presented_frame_for_snapshot(context_id);if (!presented_frame.has_value())return {};- auto const& source = *presented_frame->surface;- auto frame_size = presented_frame->frame_size;- auto snapshot_size = frame_size;- if (maximum_size.has_value() && !maximum_size->is_empty())- snapshot_size = size_fitting_within(frame_size, *maximum_size);- auto bitmap_or_error = Gfx::Bitmap::create_shareable(Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, snapshot_size);+ auto bitmap_or_error = Gfx::Bitmap::create_shareable(Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, presented_frame->frame_size);if (bitmap_or_error.is_error())return {};auto bitmap = bitmap_or_error.release_value();-- if (snapshot_size == frame_size) {- source.read_into_bitmap(*bitmap);- return Gfx::ShareableBitmap { move(bitmap), Gfx::ShareableBitmap::ConstructWithKnownGoodBitmap };- }-- // A linear filter reads only the pixels around each one it writes, so a frame scaled down to a fraction of its size- // in one step would skip most of them. Halving it first lets every pixel count.- auto image = source.sk_image_snapshot<sk_sp<SkImage>>();- auto image_size = frame_size;- SkSamplingOptions const sampling { SkFilterMode::kLinear };- auto draw_scaled = [&](Gfx::IntSize size) -> RefPtr<Gfx::PaintingSurface> {- auto surface_or_error = Gfx::PaintingSurface::try_create_with_size(size, Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, source.skia_backend_context());- if (surface_or_error.is_error())- return nullptr;- auto surface = surface_or_error.release_value();- surface->canvas().drawImageRect(image.get(), SkRect::MakeIWH(image_size.width(), image_size.height()), SkRect::MakeIWH(size.width(), size.height()), sampling, nullptr, SkCanvas::kStrict_SrcRectConstraint);- return surface;- };- while (image_size.width() / 2 >= snapshot_size.width() && image_size.height() / 2 >= snapshot_size.height()) {- Gfx::IntSize half_size { image_size.width() / 2, image_size.height() / 2 };- auto half = draw_scaled(half_size);- if (!half)- return {};- image = half->sk_image_snapshot<sk_sp<SkImage>>();- image_size = half_size;- }- auto snapshot_surface = draw_scaled(snapshot_size);- if (!snapshot_surface)- return {};- snapshot_surface->read_into_bitmap(*bitmap);+ presented_frame->surface->read_into_bitmap(*bitmap);return Gfx::ShareableBitmap { move(bitmap), Gfx::ShareableBitmap::ConstructWithKnownGoodBitmap };}diff --git a/Services/Compositor/CompositorState.h b/Services/Compositor/CompositorState.hindex a698ff57f73..5fcede539f8 100644--- a/Services/Compositor/CompositorState.h+++ b/Services/Compositor/CompositorState.h@@ -156,7 +156,7 @@ public:// it, and tells the requester once every one of them has handed over its frames, or gone away.void prepare_remote_descendants_for_screenshot(CompositorStateWebContentClient& requester, Web::CompositorContextId, u64 request_id);void did_paint_contexts_for_screenshot(CompositorStateWebContentClient& painter, u64 preparation_id);- Optional<Gfx::ShareableBitmap> take_frame_snapshot(Web::CompositorContextId, Optional<Gfx::IntSize> maximum_size);+ Optional<Gfx::ShareableBitmap> take_frame_snapshot(Web::CompositorContextId);void presented_bitmap_ready_to_paint(Web::CompositorContextId, i32 bitmap_id);void set_client_gpu_presentation_capability(CompositorStateClient&, bool supported, u64 adapter_luid);void set_scrollbar_track_press_jumps(CompositorStateClient&, bool jumps);diff --git a/Services/Compositor/ConnectionFromPresentation.cpp b/Services/Compositor/ConnectionFromPresentation.cppindex 770a27331c4..51665316e0b 100644--- a/Services/Compositor/ConnectionFromPresentation.cpp+++ b/Services/Compositor/ConnectionFromPresentation.cpp@@ -69,11 +69,11 @@ void ConnectionFromPresentation::presented_bitmap_ready_to_paint(Web::Compositorm_compositor_state->presented_bitmap_ready_to_paint(context_id, bitmap_id);}-void ConnectionFromPresentation::take_frame_snapshot(Web::CompositorContextId context_id, u64 request_id, Optional<Gfx::IntSize> maximum_size)+void ConnectionFromPresentation::take_frame_snapshot(Web::CompositorContextId context_id, u64 request_id){Optional<Gfx::ShareableBitmap> snapshot;if (is_bound_to(context_id))- snapshot = m_compositor_state->take_frame_snapshot(context_id, maximum_size);+ snapshot = m_compositor_state->take_frame_snapshot(context_id);async_did_take_frame_snapshot(request_id, move(snapshot));}diff --git a/Services/Compositor/ConnectionFromPresentation.h b/Services/Compositor/ConnectionFromPresentation.hindex 681d7d41211..eeafe21d880 100644--- a/Services/Compositor/ConnectionFromPresentation.h+++ b/Services/Compositor/ConnectionFromPresentation.h@@ -40,7 +40,7 @@ private:virtual ErrorOr<OwnPtr<IPC::MessageBuffer>> handle_raw(IPC::RawMessage&) override;virtual void presented_bitmap_ready_to_paint(Web::CompositorContextId, i32 bitmap_id) override;- virtual void take_frame_snapshot(Web::CompositorContextId, u64 request_id, Optional<Gfx::IntSize> maximum_size) override;+ virtual void take_frame_snapshot(Web::CompositorContextId, u64 request_id) override;virtual void set_client_gpu_presentation_capability(bool supported, u64 adapter_luid) override;virtual void set_scrollbar_track_press_jumps(bool jumps) override;diff --git a/Services/Hub/ConnectionFromUI.cpp b/Services/Hub/ConnectionFromUI.cppindex 530b76acf2a..8f31ffc5f82 100644--- a/Services/Hub/ConnectionFromUI.cpp+++ b/Services/Hub/ConnectionFromUI.cpp@@ -946,14 +946,14 @@ void ConnectionFromUI::view_set_wants_frame_notifications(u64 view_id, bool wantview->set_wants_frame_notifications(wants_frame_notifications);}-void ConnectionFromUI::view_take_frame_snapshot(u64 view_id, u64 request_id, Optional<Gfx::IntSize> maximum_size)+void ConnectionFromUI::view_take_frame_snapshot(u64 view_id, u64 request_id){auto* view = find_view(view_id);if (!view) {async_view_did_take_frame_snapshot(view_id, request_id, {});return;}- view->take_frame_snapshot(maximum_size, [view_id, request_id](Optional<Gfx::ShareableBitmap> snapshot) {+ view->take_frame_snapshot([view_id, request_id](Optional<Gfx::ShareableBitmap> snapshot) {if (auto* ui = ConnectionFromUI::the())ui->async_view_did_take_frame_snapshot(view_id, request_id, move(snapshot));});diff --git a/Services/Hub/ConnectionFromUI.h b/Services/Hub/ConnectionFromUI.hindex fa23c5a91fd..0b59e1d086c 100644--- a/Services/Hub/ConnectionFromUI.h+++ b/Services/Hub/ConnectionFromUI.h@@ -165,7 +165,7 @@ private:virtual void view_set_presents_from_cpu_memory(u64 view_id, bool presents_from_cpu_memory) override;virtual void view_set_hosts_remote_layer(u64 view_id, bool hosts_remote_layer) override;virtual void view_set_wants_frame_notifications(u64 view_id, bool wants_frame_notifications) override;- virtual void view_take_frame_snapshot(u64 view_id, u64 request_id, Optional<Gfx::IntSize> maximum_size) override;+ virtual void view_take_frame_snapshot(u64 view_id, u64 request_id) override;virtual void view_begin_synchronized_resize(u64 view_id, u64 token) override;virtual void view_await_synchronized_resize_frame(u64 view_id, u64 token, Gfx::IntSize size) override;virtual void view_commit_synchronized_resize(u64 view_id, u64 token) override;diff --git a/Services/Hub/HubServer.ipc b/Services/Hub/HubServer.ipcindex a6c262923cd..c832103bde0 100644--- a/Services/Hub/HubServer.ipc+++ b/Services/Hub/HubServer.ipc@@ -247,7 +247,7 @@ endpoint HubServerview_set_hosts_remote_layer(u64 view_id, bool hosts_remote_layer) =|// The UI hears of the first frame shown in a new remote layer, and of every frame while it wants to.view_set_wants_frame_notifications(u64 view_id, bool wants_frame_notifications) =|- view_take_frame_snapshot(u64 view_id, u64 request_id, Optional<Gfx::IntSize> maximum_size) =|+ view_take_frame_snapshot(u64 view_id, u64 request_id) =|view_begin_synchronized_resize(u64 view_id, u64 token) =|view_await_synchronized_resize_frame(u64 view_id, u64 token, Gfx::IntSize size) =|view_commit_synchronized_resize(u64 view_id, u64 token) =|diff --git a/UI/Qt/RemoteView.cpp b/UI/Qt/RemoteView.cppindex 755a557b847..010f4ae384e 100644--- a/UI/Qt/RemoteView.cpp+++ b/UI/Qt/RemoteView.cpp@@ -194,11 +194,11 @@ void RemoteView::did_present_frame_in_remote_layer(Gfx::IntSize size, double devon_ready_to_paint();}-void RemoteView::take_frame_snapshot(Optional<Gfx::IntSize> maximum_size, Function<void(Optional<Gfx::ShareableBitmap>)> callback)+void RemoteView::take_frame_snapshot(Function<void(Optional<Gfx::ShareableBitmap>)> callback){auto request_id = m_next_request_id++;m_pending_frame_snapshots.set(request_id, move(callback));- HubConnection::the().async_view_take_frame_snapshot(view_id(), request_id, maximum_size);+ HubConnection::the().async_view_take_frame_snapshot(view_id(), request_id);}void RemoteView::did_take_frame_snapshot(u64 request_id, Optional<Gfx::ShareableBitmap> snapshot)diff --git a/UI/Qt/RemoteView.h b/UI/Qt/RemoteView.hindex 20efdde882d..179bfc051ab 100644--- a/UI/Qt/RemoteView.h+++ b/UI/Qt/RemoteView.h@@ -95,7 +95,7 @@ public:void set_hosts_remote_layer(bool hosts_remote_layer) { HubConnection::the().async_view_set_hosts_remote_layer(view_id(), hosts_remote_layer); }void set_wants_frame_notifications(bool wants_frame_notifications) { HubConnection::the().async_view_set_wants_frame_notifications(view_id(), wants_frame_notifications); }// What the view shows, from the Compositor, scaled down to fit within the size given.- void take_frame_snapshot(Optional<Gfx::IntSize> maximum_size, Function<void(Optional<Gfx::ShareableBitmap>)>);+ void take_frame_snapshot(Function<void(Optional<Gfx::ShareableBitmap>)>);void begin_synchronized_resize(u64 token) { HubConnection::the().async_view_begin_synchronized_resize(view_id(), token); }void await_synchronized_resize_frame(u64 token, Gfx::IntSize size) { HubConnection::the().async_view_await_synchronized_resize_frame(view_id(), token, size); }void commit_synchronized_resize(u64 token) { HubConnection::the().async_view_commit_synchronized_resize(view_id(), token); }diff --git a/UI/Qt/WebContentView.cpp b/UI/Qt/WebContentView.cppindex bbe89a12f31..7b525529df6 100644--- a/UI/Qt/WebContentView.cpp+++ b/UI/Qt/WebContentView.cpp@@ -1281,7 +1281,7 @@ void WebContentView::take_visible_snapshot(Function<void(QImage)> on_snapshot)return;}- take_frame_snapshot({}, [self = QPointer { this }, on_snapshot = AK::move(on_snapshot)](Optional<Gfx::ShareableBitmap> snapshot) {+ take_frame_snapshot([self = QPointer { this }, on_snapshot = AK::move(on_snapshot)](Optional<Gfx::ShareableBitmap> snapshot) {if (!self)return;auto const* bitmap = snapshot.has_value() && snapshot->is_valid() ? snapshot->bitmap() : nullptr;