Truly independent web browser
Everywhere: Take page snapshots from the Compositor when it holds them
Aliaksandr Kalenikf507b54afa79
diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cppindex 7da1eb1e7aa..95601cd64ef 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::take_frame_snapshot(Web::CompositorContextId context_id, Optional<Gfx::IntSize> maximum_size, 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));+}+void Application::update_compositor_context_presents_from_cpu_memory(Web::CompositorContextId context_id, bool presents_from_cpu_memory){if (!can_send_compositor_process_ipc(m_compositor_client))@@ -1911,8 +1920,11 @@ void Application::notify_compositor_gpu_presentation_unavailable()void Application::handle_compositor_process_death(){m_compositor_client = nullptr;- if (m_compositor_presentation_client)+ if (m_compositor_presentation_client) {m_compositor_presentation_client->on_death = nullptr;+ // The client can go before it hears that the connection closed, and the snapshots it waits for never come.+ m_compositor_presentation_client->fail_pending_frame_snapshots();+ }m_compositor_presentation_client = nullptr;// Nothing will forward or hand back the input events the dead compositor still held.diff --git a/Libraries/LibWebView/Application.h b/Libraries/LibWebView/Application.hindex bd7a593385b..4ae89337cd9 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 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&);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 37c7dfe5d51..cd1390470f9 100644--- a/Libraries/LibWebView/CompositorPresentationClient.cpp+++ b/Libraries/LibWebView/CompositorPresentationClient.cpp@@ -19,6 +19,8 @@ CompositorPresentationClient::CompositorPresentationClient(NonnullOwnPtr<IPC::Trvoid CompositorPresentationClient::die(){+ fail_pending_frame_snapshots();+if (auto callback = move(on_death)) {Core::deferred_invoke([callback = move(callback)]() mutable {callback();@@ -76,6 +78,26 @@ 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)+{+ 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);+}++void CompositorPresentationClient::fail_pending_frame_snapshots()+{+ auto pending_frame_snapshots = move(m_pending_frame_snapshots);+ for (auto& [_, callback] : pending_frame_snapshots)+ callback({});+}++void CompositorPresentationClient::did_take_frame_snapshot(u64 request_id, Optional<Gfx::ShareableBitmap> snapshot)+{+ if (auto callback = m_pending_frame_snapshots.take(request_id); callback.has_value())+ (*callback)(move(snapshot));+}+void CompositorPresentationClient::did_add_backing_stores(Web::CompositorContextId context_id, Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores){if (auto* page = page_for_compositor_context_id(context_id))diff --git a/Libraries/LibWebView/CompositorPresentationClient.h b/Libraries/LibWebView/CompositorPresentationClient.hindex 7b4a94f9686..09f02ab403b 100644--- a/Libraries/LibWebView/CompositorPresentationClient.h+++ b/Libraries/LibWebView/CompositorPresentationClient.h@@ -7,10 +7,12 @@#pragma once#include <AK/Function.h>+#include <AK/HashMap.h>#include <Compositor/CompositorPresentationClientEndpoint.h>#include <Compositor/CompositorPresentationServerEndpoint.h>#include <LibCompositing/Types.h>#include <LibGfx/Rect.h>+#include <LibGfx/ShareableBitmap.h>#include <LibGfx/SharedImage.h>#include <LibIPC/ConnectionToServer.h>#include <LibWebView/Forward.h>@@ -28,6 +30,12 @@ public:Function<void()> on_death;+ 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 fail_pending_frame_snapshots();+private:virtual void die() override;@@ -39,6 +47,10 @@ private:virtual void did_consume_input_event(Web::CompositorContextId, u64 event_id) override;virtual void did_not_dispatch_input_event(Web::CompositorContextId, u64 event_id) override;virtual void did_dispatch_input_event(Web::CompositorContextId, u64 event_id, bool performed_default_action) override;+ virtual void did_take_frame_snapshot(u64 request_id, Optional<Gfx::ShareableBitmap>) override;++ HashMap<u64, FrameSnapshotCallback> m_pending_frame_snapshots;+ u64 m_next_frame_snapshot_request_id { 1 };};}diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cppindex ae7143cb214..59ec4d6b2f4 100644--- a/Libraries/LibWebView/ViewImplementation.cpp+++ b/Libraries/LibWebView/ViewImplementation.cpp@@ -478,6 +478,11 @@ void ViewImplementation::set_hosts_remote_layer(bool hosts_remote_layer)did_create_remote_layer(remote_layer->core_animation_context_id);}+void ViewImplementation::take_frame_snapshot(Optional<Gfx::IntSize> maximum_size, Function<void(Optional<Gfx::ShareableBitmap>)> callback)+{+ Application::the().take_frame_snapshot(page().compositor_context_id(), maximum_size, move(callback));+}+void ViewImplementation::recreate_remote_layer_after_compositor_reconnect(Badge<WebContentClient>){if (!m_hosts_remote_layer)diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.hindex fa89b61fa30..f428a5048fa 100644--- a/Libraries/LibWebView/ViewImplementation.h+++ b/Libraries/LibWebView/ViewImplementation.h@@ -237,6 +237,8 @@ public:// Whether the UI shows the view's frames in a remote layer, which the Compositor commits them into itself, rather// 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>)>);// 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>);void set_has_system_focus(bool);diff --git a/Services/Compositor/CompositorPresentationClient.ipc b/Services/Compositor/CompositorPresentationClient.ipcindex a145595c8b8..e50b075092e 100644--- a/Services/Compositor/CompositorPresentationClient.ipc+++ b/Services/Compositor/CompositorPresentationClient.ipc@@ -1,4 +1,5 @@#include <LibGfx/Rect.h>+#include <LibGfx/ShareableBitmap.h>#include <LibGfx/SharedImage.h>#include <LibCompositing/Types.h>@@ -9,6 +10,7 @@ endpoint CompositorPresentationClientdid_retire_backing_stores(Web::CompositorContextId context_id, Vector<i32> bitmap_ids) =|did_present_frame(Web::CompositorContextId context_id, Gfx::IntRect content_rect, Gfx::IntRect damage_rect, i32 bitmap_id, double device_pixel_ratio) =|did_present_frame_in_remote_layer(Web::CompositorContextId context_id, Gfx::IntRect content_rect, Gfx::IntRect damage_rect, double device_pixel_ratio) =|+ did_take_frame_snapshot(u64 request_id, Optional<Gfx::ShareableBitmap> snapshot) =|[RustSender] did_consume_input_event(Web::CompositorContextId context_id, u64 event_id) =|[RustSender] did_not_dispatch_input_event(Web::CompositorContextId context_id, u64 event_id) =|[RustSender] did_dispatch_input_event(Web::CompositorContextId context_id, u64 event_id, bool performed_default_action) =|diff --git a/Services/Compositor/CompositorPresentationServer.ipc b/Services/Compositor/CompositorPresentationServer.ipcindex 4b61e79137c..95436ac5a38 100644--- a/Services/Compositor/CompositorPresentationServer.ipc+++ b/Services/Compositor/CompositorPresentationServer.ipc@@ -1,4 +1,5 @@#include <LibCompositing/Types.h>+#include <LibGfx/Size.h>#include <LibWebCommon/Page/InputEvent.h>endpoint CompositorPresentationServer@@ -7,6 +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) =|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 31b6300b0d3..8162842aa43 100644--- a/Services/Compositor/CompositorState.cpp+++ b/Services/Compositor/CompositorState.cpp@@ -1358,6 +1358,14 @@ bool CompositorState::request_screenshot(Web::CompositorContextId context_id, Gfreturn true;}+Optional<Gfx::ShareableBitmap> CompositorState::take_frame_snapshot(Web::CompositorContextId context_id, Optional<Gfx::IntSize> maximum_size)+{+ auto* context = presented_context_if_present(context_id);+ if (!context || !context->presenter.presents_to_client())+ return {};+ return context->presenter.snapshot_of_presented_frame(maximum_size);+}+void CompositorState::presented_bitmap_ready_to_paint(Web::CompositorContextId context_id, i32 bitmap_id){auto* context = presented_context_if_present(context_id);@@ -1423,7 +1431,7 @@ void CompositorState::deliver_rendered_frame(PendingAsyncPresent& pending_presenauto* context = presented_context_if_present(context_id);VERIFY(context);- context->presenter.did_finish_gpu_present(pending_present.bitmap_id);+ context->presenter.did_finish_gpu_present(pending_present.bitmap_id, pending_present.viewport_rect.size());auto* presentation = pending_present.publish_to_presentation ? presentation_for_context(context_id) : nullptr;if (pending_present.shows_in_remote_layer) {log_presented_frame(context->page_id, pending_present.damage_rect);diff --git a/Services/Compositor/CompositorState.h b/Services/Compositor/CompositorState.hindex c771f34db9e..59c71a0e2ea 100644--- a/Services/Compositor/CompositorState.h+++ b/Services/Compositor/CompositorState.h@@ -152,6 +152,7 @@ public:// Asks for the next display tick the context's maximum rate lets through, for its render clock.void request_clock_tick(Web::CompositorContextId, double maximum_frames_per_second);bool request_screenshot(Web::CompositorContextId, Gfx::ShareableBitmap&);+ Optional<Gfx::ShareableBitmap> take_frame_snapshot(Web::CompositorContextId, Optional<Gfx::IntSize> maximum_size);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 57ca722fe74..0097b865ba8 100644--- a/Services/Compositor/ConnectionFromPresentation.cpp+++ b/Services/Compositor/ConnectionFromPresentation.cpp@@ -79,6 +79,14 @@ 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)+{+ Optional<Gfx::ShareableBitmap> snapshot;+ if (is_bound_to(context_id))+ snapshot = m_compositor_state->take_frame_snapshot(context_id, maximum_size);+ async_did_take_frame_snapshot(request_id, move(snapshot));+}+void ConnectionFromPresentation::set_client_gpu_presentation_capability(bool supported, u64 adapter_luid){m_compositor_state->set_client_gpu_presentation_capability(*this, supported, adapter_luid);diff --git a/Services/Compositor/ConnectionFromPresentation.h b/Services/Compositor/ConnectionFromPresentation.hindex b602f955b8d..52ed2a85aaa 100644--- a/Services/Compositor/ConnectionFromPresentation.h+++ b/Services/Compositor/ConnectionFromPresentation.h@@ -40,6 +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 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/Compositor/ContextPresenter.cpp b/Services/Compositor/ContextPresenter.cppindex 2be029756ab..9b741de7574 100644--- a/Services/Compositor/ContextPresenter.cpp+++ b/Services/Compositor/ContextPresenter.cpp@@ -169,10 +169,11 @@ bool ContextPresenter::acknowledge_presented_bitmap(i32 bitmap_id)return m_backing_store_manager.release_buffer(bitmap_id);}-void ContextPresenter::did_finish_gpu_present(i32 bitmap_id)+void ContextPresenter::did_finish_gpu_present(i32 bitmap_id, Gfx::IntSize frame_size){m_backing_store_manager.complete_rendering(bitmap_id);m_latest_rendered_surface = m_backing_store_manager.latest_rendered_surface();+ m_size_of_latest_rendered_frame = frame_size;}bool ContextPresenter::update_composited_raster_transform(Gfx::FloatRect destination_rect, Gfx::FloatMatrix4x4 const& transform)@@ -363,6 +364,7 @@ bool ContextPresenter::present_synchronously(DisplayListPlayerSkia& display_listdisplay_list_player.flush(back_store);m_backing_store_manager.complete_rendering(render_target->bitmap_id);m_latest_rendered_surface = m_backing_store_manager.latest_rendered_surface();+ m_size_of_latest_rendered_frame = pending_frame->viewport_rect.size();m_presented_frame = pending_frame->viewport_rect;m_pending_present_frame.clear();m_pending_present_frame_scheduled = false;@@ -376,6 +378,64 @@ void ContextPresenter::paint_screenshot(DisplayListPlayerSkia& display_list_playdisplay_list_player.flush(*target_surface);}+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> ContextPresenter::snapshot_of_presented_frame(Optional<Gfx::IntSize> maximum_size) const+{+ if (!m_latest_rendered_surface)+ return {};+ auto const& source = *m_latest_rendered_surface;+ Gfx::IntSize frame_size { min(m_size_of_latest_rendered_frame.width(), source.size().width()), min(m_size_of_latest_rendered_frame.height(), source.size().height()) };+ if (frame_size.is_empty())+ return {};+ 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);+ 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);+ return Gfx::ShareableBitmap { move(bitmap), Gfx::ShareableBitmap::ConstructWithKnownGoodBitmap };+}+void ContextPresenter::stop_backing_store_shrink_timer(){if (!m_backing_store_shrink_timer)diff --git a/Services/Compositor/ContextPresenter.h b/Services/Compositor/ContextPresenter.hindex 7d6d7908787..cf701c50b10 100644--- a/Services/Compositor/ContextPresenter.h+++ b/Services/Compositor/ContextPresenter.h@@ -89,7 +89,7 @@ public:void schedule_surplus_backing_store_retirement(Function<void()>);bool acknowledge_presented_bitmap(i32 bitmap_id);void acknowledge_all_presented_bitmaps() { m_backing_store_manager.release_all_presented_buffers(); }- void did_finish_gpu_present(i32 bitmap_id);+ void did_finish_gpu_present(i32 bitmap_id, Gfx::IntSize frame_size);RefPtr<Gfx::PaintingSurface> latest_rendered_surface() const { return m_latest_rendered_surface; }Gfx::SharedImageBuffer const* shared_image_buffer_of_latest_rendered_store() const { return m_backing_store_manager.shared_image_buffer_of_latest_rendered_store(); }BackingStoreManager::PublicationMode backing_store_publication_mode() const { return m_backing_store_manager.publication_mode(); }@@ -113,6 +113,8 @@ public:void did_submit_prepared_frame(Gfx::IntRect);bool present_synchronously(DisplayListPlayerSkia&, Optional<FrameInputs>&&, CompositedContextResolver const*);void paint_screenshot(DisplayListPlayerSkia&, Gfx::ShareableBitmap&, FrameInputs const&, CompositedContextResolver const*);+ // The frame on screen, read back from the store that holds it, and scaled down to fit within the size given.+ Optional<Gfx::ShareableBitmap> snapshot_of_presented_frame(Optional<Gfx::IntSize> maximum_size) const;private:struct RasterizedFrame {@@ -155,6 +157,9 @@ private:BackingStoreManager m_backing_store_manager;RefPtr<Gfx::PaintingSurface> m_latest_rendered_surface;+ // The frame that the latest rendered surface holds, which the presented frame runs ahead of while its GPU work is+ // in flight.+ Gfx::IntSize m_size_of_latest_rendered_frame;RefPtr<Gfx::PaintingSurface> m_damage_surface;Optional<RasterizedFrame> m_last_rasterized_frame;Gfx::FloatSize m_unbounded_raster_scale_of_embedding { 1, 1 };diff --git a/Services/Hub/ConnectionFromUI.cpp b/Services/Hub/ConnectionFromUI.cppindex 298d4f82530..f0960c46e09 100644--- a/Services/Hub/ConnectionFromUI.cpp+++ b/Services/Hub/ConnectionFromUI.cpp@@ -922,6 +922,19 @@ 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)+{+ 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) {+ if (auto* ui = ConnectionFromUI::the())+ ui->async_view_did_take_frame_snapshot(view_id, request_id, move(snapshot));+ });+}+void ConnectionFromUI::view_set_presents_from_cpu_memory(u64 view_id, bool presents_from_cpu_memory){if (auto* view = find_view(view_id)) {diff --git a/Services/Hub/ConnectionFromUI.h b/Services/Hub/ConnectionFromUI.hindex ae07af5fb93..9a2a57bbfed 100644--- a/Services/Hub/ConnectionFromUI.h+++ b/Services/Hub/ConnectionFromUI.h@@ -161,6 +161,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_set_has_system_focus(u64 view_id, bool has_system_focus) override;virtual void view_open_url_in_new_tab(u64 view_id, URL::URL url, Web::HTML::ActivateTab activate_tab) override;virtual void view_open_url_in_new_window(u64 view_id, URL::URL url, WebView::IsPrivate is_private) override;diff --git a/Services/Hub/HubClient.ipc b/Services/Hub/HubClient.ipcindex 98d92e5e12b..1d88b98ef16 100644--- a/Services/Hub/HubClient.ipc+++ b/Services/Hub/HubClient.ipc@@ -328,6 +328,7 @@ endpoint HubClient// The ID is 0 when the view has no remote layer, and the UI goes on showing backing stores.view_set_remote_layer(u64 view_id, u32 core_animation_context_id) =|view_did_present_frame_in_remote_layer(u64 view_id, Gfx::IntSize size, double device_pixel_ratio) =|+ view_did_take_frame_snapshot(u64 view_id, u64 request_id, Optional<Gfx::ShareableBitmap> snapshot) =|view_finish_handling_key_event(u64 view_id, u64 input_id, Web::KeyEvent event) =|view_finish_handling_mouse_event(u64 view_id, u64 input_id, Web::MouseEvent event, Web::EventResult result) =|view_finish_handling_drag_event(u64 view_id, u64 input_id, Web::DragEvent event) =|diff --git a/Services/Hub/HubServer.ipc b/Services/Hub/HubServer.ipcindex 2c665f2c3e8..313c27d5222 100644--- a/Services/Hub/HubServer.ipc+++ b/Services/Hub/HubServer.ipc@@ -239,6 +239,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_set_has_system_focus(u64 view_id, bool has_system_focus) =|view_open_url_in_new_tab(u64 view_id, URL::URL url, Web::HTML::ActivateTab activate_tab) =|view_open_url_in_new_window(u64 view_id, URL::URL url, WebView::IsPrivate is_private) =|diff --git a/UI/Qt/HubConnection.cpp b/UI/Qt/HubConnection.cppindex e3c10abd770..5c55785028b 100644--- a/UI/Qt/HubConnection.cpp+++ b/UI/Qt/HubConnection.cpp@@ -160,6 +160,12 @@ void HubConnection::view_did_present_frame_in_remote_layer(u64 view_id, Gfx::Intview->did_present_frame_in_remote_layer(size, device_pixel_ratio);}+void HubConnection::view_did_take_frame_snapshot(u64 view_id, u64 request_id, Optional<Gfx::ShareableBitmap> snapshot)+{+ if (auto* view = RemoteView::find(view_id))+ view->did_take_frame_snapshot(request_id, move(snapshot));+}+void HubConnection::view_finish_handling_key_event(u64 view_id, u64 input_id, Web::KeyEvent event){if (auto* view = RemoteView::find(view_id))diff --git a/UI/Qt/HubConnection.h b/UI/Qt/HubConnection.hindex eac317034a7..a607dd612e3 100644--- a/UI/Qt/HubConnection.h+++ b/UI/Qt/HubConnection.h@@ -167,6 +167,7 @@ private:virtual void view_clear_backing_stores(u64 view_id) override;virtual void view_set_remote_layer(u64 view_id, u32 core_animation_context_id) override;virtual void view_did_present_frame_in_remote_layer(u64 view_id, Gfx::IntSize size, double device_pixel_ratio) override;+ virtual void view_did_take_frame_snapshot(u64 view_id, u64 request_id, Optional<Gfx::ShareableBitmap> snapshot) override;virtual void view_finish_handling_key_event(u64 view_id, u64 input_id, Web::KeyEvent event) override;virtual void view_finish_handling_mouse_event(u64 view_id, u64 input_id, Web::MouseEvent event, Web::EventResult result) override;virtual void view_finish_handling_drag_event(u64 view_id, u64 input_id, Web::DragEvent event) override;diff --git a/UI/Qt/RemoteView.cpp b/UI/Qt/RemoteView.cppindex 6cfee44c697..082f043e6d1 100644--- a/UI/Qt/RemoteView.cpp+++ b/UI/Qt/RemoteView.cpp@@ -183,6 +183,19 @@ 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)+{+ 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);+}++void RemoteView::did_take_frame_snapshot(u64 request_id, Optional<Gfx::ShareableBitmap> snapshot)+{+ if (auto callback = m_pending_frame_snapshots.take(request_id); callback.has_value())+ (*callback)(move(snapshot));+}+void RemoteView::release_backing_store_to_page(u64 page_id, i32 bitmap_id){if (auto* hub = HubConnection::the_if_connected())diff --git a/UI/Qt/RemoteView.h b/UI/Qt/RemoteView.hindex fcf464031aa..d8364fbdcaa 100644--- a/UI/Qt/RemoteView.h+++ b/UI/Qt/RemoteView.h@@ -94,6 +94,9 @@ public:void set_presents_from_cpu_memory(bool presents_from_cpu_memory) { HubConnection::the().async_view_set_presents_from_cpu_memory(view_id(), presents_from_cpu_memory); }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 did_take_frame_snapshot(u64 request_id, Optional<Gfx::ShareableBitmap>);void set_has_system_focus(bool has_system_focus) { HubConnection::the().async_view_set_has_system_focus(view_id(), has_system_focus); }void load(URL::URL const& url, Web::Bindings::NavigationHistoryBehavior navigation_history_behavior = Web::Bindings::NavigationHistoryBehavior::Auto) { HubConnection::the().async_view_load(view_id(), url, navigation_history_behavior); }void load_from_user_input(URL::URL const&);@@ -315,6 +318,7 @@ private:HashMap<u64, NonnullRefPtr<Core::Promise<ByteString>>> m_pending_selected_text;HashMap<u64, Function<void(bool)>> m_pending_close_preflights;HashMap<u64, NonnullRefPtr<Core::Promise<String>>> m_pending_page_dumps;+ HashMap<u64, Function<void(Optional<Gfx::ShareableBitmap>)>> m_pending_frame_snapshots;u64 m_next_request_id { 1 };u64 m_next_navigation_listener_id { 1 };WebView::PageBackingStores m_backing_stores;diff --git a/UI/Qt/ScreenshotSelectionOverlay.cpp b/UI/Qt/ScreenshotSelectionOverlay.cppindex d958a61da47..80161c1dcf3 100644--- a/UI/Qt/ScreenshotSelectionOverlay.cpp+++ b/UI/Qt/ScreenshotSelectionOverlay.cpp@@ -19,6 +19,7 @@#include <QLabel>#include <QMouseEvent>#include <QPainter>+#include <QPointer>#include <QPushButton>#include <QTimer>#include <QVBoxLayout>@@ -189,6 +190,7 @@ void ScreenshotSelectionOverlay::page_did_scroll()m_scroll_settle_timer->start();m_follow_frames_until.setRemainingTime(MAXIMUM_SCROLL_ANIMATION_TIME_MS);+ ++m_reveal_generation;if (m_reveals_page)return;@@ -213,18 +215,39 @@ void ScreenshotSelectionOverlay::cover_page()return;m_scroll_settle_timer->stop();- if (auto frame = on_request_page_frame(); !frame.isNull())- m_frame = AK::move(frame);- m_reveals_page = false;- m_instructions->show();- update();- if (on_page_visibility_change)- on_page_visibility_change();+ on_request_page_frame([self = QPointer { this }, generation = m_reveal_generation](QImage frame) {+ if (!self || !self->m_reveals_page || self->m_reveal_generation != generation)+ return;+ if (!frame.isNull())+ self->m_frame = AK::move(frame);+ self->m_reveals_page = false;+ self->m_instructions->show();+ self->update();+ if (self->on_page_visibility_change)+ self->on_page_visibility_change();+ if (auto action = AK::move(self->m_action_once_page_is_covered)) {+ self->m_action_once_page_is_covered = nullptr;+ action();+ }+ });+}++// The frame that covers the page arrives later, so a selection or a capture made while the page is revealed waits for+// it, rather than taking the frame from before the page scrolled.+void ScreenshotSelectionOverlay::once_page_is_covered(Function<void()> action)+{+ if (!m_reveals_page) {+ action();+ return;+ }+ m_action_once_page_is_covered = AK::move(action);+ cover_page();}void ScreenshotSelectionOverlay::dismiss(){m_scroll_settle_timer->stop();+ m_action_once_page_is_covered = nullptr;hide();if (on_close)on_close();@@ -290,7 +313,7 @@ void ScreenshotSelectionOverlay::mouseReleaseEvent(QMouseEvent* event)auto selected = selection();m_drag_start.clear();update();- select(selected);+ once_page_is_covered([this, selected] { select(selected); });}// The user drags the card out of the way of the part of the page they want to select, rather than starting a selection.@@ -330,14 +353,19 @@ void ScreenshotSelectionOverlay::keyPressEvent(QKeyEvent* event){switch (event->key()) {case Qt::Key_Return:- case Qt::Key_Enter:- cover_page();+ case Qt::Key_Enter: {// A focused button does what it says. Otherwise, the whole frame is taken.- if (auto* button = qobject_cast<QPushButton*>(QApplication::focusWidget()); button && m_buttons.contains_slow(button))- button->click();- else- use_whole_frame();+ QPointer<QPushButton> focused_button = qobject_cast<QPushButton*>(QApplication::focusWidget());+ if (focused_button && !m_buttons.contains_slow(focused_button.data()))+ focused_button = nullptr;+ once_page_is_covered([this, focused_button] {+ if (focused_button)+ focused_button->click();+ else+ use_whole_frame();+ });return;+ }case Qt::Key_Escape:dismiss();return;diff --git a/UI/Qt/ScreenshotSelectionOverlay.h b/UI/Qt/ScreenshotSelectionOverlay.hindex 8523a08457b..fc2283f01b6 100644--- a/UI/Qt/ScreenshotSelectionOverlay.h+++ b/UI/Qt/ScreenshotSelectionOverlay.h@@ -48,7 +48,8 @@ public:Function<void()> on_close;Function<void(QImage)> on_finished;Function<void()> on_full_page_requested;- Function<QImage()> on_request_page_frame;+ // The frame can arrive later, when the process that holds it sends it.+ Function<void(Function<void(QImage)>)> on_request_page_frame;Function<void()> on_page_visibility_change;protected:@@ -65,12 +66,17 @@ protected:private:void place_instructions();void cover_page();+ void once_page_is_covered(Function<void()>);QRect selection() const;void finish(QImage);void update_chrome_style();QImage m_frame;bool m_reveals_page { false };+ // Moves on whenever the page scrolls, so a frame that was asked for before then is not taken.+ u64 m_reveal_generation { 0 };+ // What the user asked for while the page was revealed, which takes the frame that covers it.+ Function<void()> m_action_once_page_is_covered;QTimer* m_scroll_settle_timer { nullptr };QDeadlineTimer m_follow_frames_until;Optional<QPoint> m_drag_start;diff --git a/UI/Qt/TabBar.cpp b/UI/Qt/TabBar.cppindex 804f59a8f9e..3aca7cb80bb 100644--- a/UI/Qt/TabBar.cpp+++ b/UI/Qt/TabBar.cpp@@ -2706,8 +2706,11 @@ void TabBar::show_tab_preview()}if (m_previewed_tab != tab) {+ stop_following_previewed_tab_frames();m_previewed_tab = tab;m_tab_preview_paint_connection = connect(&tab->view(), &WebContentView::ready_to_paint, this, &TabBar::show_tab_preview);+ m_tab_preview_change_connection = connect(&tab->view(), &WebContentView::tab_preview_changed, this, &TabBar::show_tab_preview);+ tab->view().add_frame_notification_interest();// NB: Background tabs may never have painted. Allow rendering while their preview is requested,// without selecting the tab or showing its widget.tab->view().set_system_visibility_state(Web::HTML::VisibilityState::Visible);@@ -2724,9 +2727,17 @@ void TabBar::show_tab_preview()m_tab_preview_pointer_timer->start();}-void TabBar::hide_tab_preview()+void TabBar::stop_following_previewed_tab_frames(){disconnect(m_tab_preview_paint_connection);+ disconnect(m_tab_preview_change_connection);+ if (m_previewed_tab)+ m_previewed_tab->view().remove_frame_notification_interest();+}++void TabBar::hide_tab_preview()+{+ stop_following_previewed_tab_frames();if (m_previewed_tab) {m_previewed_tab->view().set_system_visibility_state(m_previewed_tab->view().isVisible() ? Web::HTML::VisibilityState::Visible : Web::HTML::VisibilityState::Hidden);m_previewed_tab = nullptr;diff --git a/UI/Qt/TabBar.h b/UI/Qt/TabBar.hindex bee01cfc942..8546b91a955 100644--- a/UI/Qt/TabBar.h+++ b/UI/Qt/TabBar.h@@ -186,6 +186,7 @@ private:void schedule_tab_preview(int index);void show_tab_preview();void hide_tab_preview();+ void stop_following_previewed_tab_frames();QPoint tab_preview_position_for(int index, QSize const& popup_size) const;// Where the pinned tabs sit in vertical tabs: a grid of tiles when expanded, one icon column when collapsed, and a@@ -294,6 +295,7 @@ private:int m_tab_preview_index { -1 };QPointer<Tab> m_previewed_tab;QMetaObject::Connection m_tab_preview_paint_connection;+ QMetaObject::Connection m_tab_preview_change_connection;};class TabWidget final : public QWidget {diff --git a/UI/Qt/WebContentView.cpp b/UI/Qt/WebContentView.cppindex dcd91f2288c..90ce72bd960 100644--- a/UI/Qt/WebContentView.cpp+++ b/UI/Qt/WebContentView.cpp@@ -307,6 +307,7 @@ WebContentView::WebContentView(QWidget* window, Optional<u64> hub_view_id, WebCo#endifon_ready_to_paint = [this]() {+ m_tab_preview_is_stale = true;if (m_screenshot_selection_overlay)m_screenshot_selection_overlay->page_did_paint();@@ -1265,11 +1266,79 @@ QImage WebContentView::visible_snapshot() const#endif}-Optional<QPixmap> WebContentView::tab_preview_pixmap(QSize const& maximum_size) const+bool WebContentView::holds_displayed_frame() const+{+ auto paintable = current_paintable();+ return paintable.has_value() && paintable->shared_image_buffer;+}++void WebContentView::take_visible_snapshot(Optional<QSize> maximum_size, Function<void(QImage)> on_snapshot)+{+ if (holds_displayed_frame() || !current_paintable().has_value()) {+ auto snapshot = visible_snapshot();+ if (maximum_size.has_value() && !snapshot.isNull() && (snapshot.width() > maximum_size->width() || snapshot.height() > maximum_size->height()))+ snapshot = snapshot.scaled(*maximum_size, Qt::KeepAspectRatio, Qt::SmoothTransformation);+ on_snapshot(AK::move(snapshot));+ return;+ }++ Optional<Gfx::IntSize> maximum_bitmap_size;+ if (maximum_size.has_value())+ maximum_bitmap_size = Gfx::IntSize { maximum_size->width(), maximum_size->height() };+ take_frame_snapshot(maximum_bitmap_size, [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;+ if (!bitmap) {+ on_snapshot({});+ return;+ }+ QImage image(bitmap->scanline_u8(0), bitmap->width(), bitmap->height(), bitmap->pitch(), QImage::Format_RGB32);+ on_snapshot(image.copy());+ });+}++void WebContentView::add_frame_notification_interest()+{+ if (m_frame_notification_interest++ == 0)+ set_wants_frame_notifications(true);+}++void WebContentView::remove_frame_notification_interest()+{+ VERIFY(m_frame_notification_interest > 0);+ if (--m_frame_notification_interest == 0)+ set_wants_frame_notifications(false);+}++// One request at a time, and only once a frame was presented since the last, as a page that animates presents a frame+// each refresh.+void WebContentView::refresh_tab_preview(QSize const& maximum_size)+{+ if (m_tab_preview_request_in_flight || (!m_tab_preview_is_stale && m_tab_preview_maximum_size == maximum_size))+ return;+ m_tab_preview_request_in_flight = true;+ m_tab_preview_is_stale = false;+ take_visible_snapshot(maximum_size, [this, maximum_size](QImage snapshot) {+ m_tab_preview_request_in_flight = false;+ m_tab_preview_maximum_size = maximum_size;+ m_tab_preview = snapshot.isNull() ? QPixmap {} : QPixmap::fromImage(snapshot);+ emit tab_preview_changed();+ });+}++Optional<QPixmap> WebContentView::tab_preview_pixmap(QSize const& maximum_size){if (maximum_size.isEmpty())return {};+ if (!holds_displayed_frame() && current_paintable().has_value()) {+ refresh_tab_preview(maximum_size);+ if (m_tab_preview.isNull())+ return {};+ return m_tab_preview;+ }+auto snapshot = visible_snapshot();if (snapshot.isNull())return {};@@ -1559,15 +1628,15 @@ void WebContentView::select_web_compat_report_screenshot(Function<void(QImage)>if (m_screenshot_selection_overlay)return;- auto frame = visible_snapshot();- if (frame.isNull()) {- if (on_close)- on_close();- on_selected({});- return;- }-- select_screenshot(frame, ScreenshotSelectionOverlay::Purpose::ReportIssue, AK::move(on_close)).on_finished = AK::move(on_selected);+ take_visible_snapshot({}, [this, on_selected = AK::move(on_selected), on_close = AK::move(on_close)](QImage frame) mutable {+ if (frame.isNull() || m_screenshot_selection_overlay) {+ if (on_close)+ on_close();+ on_selected({});+ return;+ }+ select_screenshot(frame, ScreenshotSelectionOverlay::Purpose::ReportIssue, AK::move(on_close)).on_finished = AK::move(on_selected);+ });}// The tab was opened only for the review, so no web content runs behind it, and leaving the review closes it.@@ -1643,12 +1712,15 @@ ScreenshotSelectionOverlay& WebContentView::select_screenshot(QImage const& framauto* overlay = new ScreenshotSelectionOverlay(frame, this, purpose);m_screenshot_selection_overlay = overlay;auto focus_to_restore = show_transient_overlay(*overlay);+ // The overlay follows the page's frames while it scrolls underneath.+ add_frame_notification_interest();overlay->on_close = [this, overlay, focus_to_restore, on_close = AK::move(on_close)] {+ remove_frame_notification_interest();close_transient_overlay(*overlay, focus_to_restore);if (on_close)on_close();};- overlay->on_request_page_frame = [this] { return visible_snapshot(); };+ overlay->on_request_page_frame = [this](Function<void(QImage)> on_frame) { take_visible_snapshot({}, AK::move(on_frame)); };overlay->on_page_visibility_change = [this] { schedule_repaint(); };return *overlay;}@@ -1660,21 +1732,24 @@ void WebContentView::start_screenshot()if (m_screenshot_selection_overlay)return;- auto frame = visible_snapshot();- if (frame.isNull()) {- take_and_save_screenshot(ScreenshotType::Visible);- return;- }+ take_visible_snapshot({}, [this](QImage frame) {+ if (m_screenshot_selection_overlay)+ return;+ if (frame.isNull()) {+ take_and_save_screenshot(ScreenshotType::Visible);+ return;+ }- auto& overlay = select_screenshot(frame, ScreenshotSelectionOverlay::Purpose::SaveScreenshot);- overlay.on_finished = [this](QImage screenshot) {- auto image = screenshot.convertToFormat(QImage::Format_ARGB32);- auto bitmap = MUST(Gfx::Bitmap::create_wrapper(Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Unpremultiplied, { image.width(), image.height() }, image.bytesPerLine(), image.bits()));- save_screenshot(*bitmap);- };- overlay.on_full_page_requested = [this] {- take_and_save_screenshot(ScreenshotType::Full);- };+ auto& overlay = select_screenshot(frame, ScreenshotSelectionOverlay::Purpose::SaveScreenshot);+ overlay.on_finished = [this](QImage screenshot) {+ auto image = screenshot.convertToFormat(QImage::Format_ARGB32);+ auto bitmap = MUST(Gfx::Bitmap::create_wrapper(Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Unpremultiplied, { image.width(), image.height() }, image.bytesPerLine(), image.bits()));+ save_screenshot(*bitmap);+ };+ overlay.on_full_page_requested = [this] {+ take_and_save_screenshot(ScreenshotType::Full);+ };+ });}// An overlay shown for a moment, such as to select or view a screenshot, hands the focus back once it is done.diff --git a/UI/Qt/WebContentView.h b/UI/Qt/WebContentView.hindex c2b6b2c9c99..cd4b567719c 100644--- a/UI/Qt/WebContentView.h+++ b/UI/Qt/WebContentView.h@@ -197,7 +197,12 @@ public:Dark,};void update_palette(PaletteMode = PaletteMode::Default);- Optional<QPixmap> tab_preview_pixmap(QSize const& maximum_size) const;+ // A preview of a page that the Compositor shows itself comes from it, so it can lag a frame behind the page.+ Optional<QPixmap> tab_preview_pixmap(QSize const& maximum_size);++ // Frames that the Compositor shows itself are only announced while something here wants to hear of them.+ void add_frame_notification_interest();+ void remove_frame_notification_interest();WebView::AccessibilityTreeManager const* accessibility_tree_manager() const { return m_accessibility_manager.ptr(); }@@ -213,6 +218,7 @@ public:signals:void ready_to_paint();+ void tab_preview_changed();void crash_overlay_changed();void image_overlay_unavailable();void urls_dropped(QList<QUrl> const&);@@ -248,6 +254,10 @@ private:// The pixels of a frame for Qt to paint, which there is no need to read back while the page's window covers them.RefPtr<Gfx::Bitmap const> cpu_bitmap_to_paint(Paintable const&) const;QImage visible_snapshot() const;+ // What the view shows, which the Compositor takes where the view does not hold the frame itself.+ void take_visible_snapshot(Optional<QSize> maximum_size, Function<void(QImage)>);+ bool holds_displayed_frame() const;+ void refresh_tab_preview(QSize const& maximum_size);void update_viewport_size();void update_cursor(Gfx::Cursor cursor);@@ -339,6 +349,11 @@ private:CrashReportReviewWidget* m_crash_report_review { nullptr };QPointer<ScreenshotSelectionOverlay> m_screenshot_selection_overlay;+ size_t m_frame_notification_interest { 0 };+ QPixmap m_tab_preview;+ QSize m_tab_preview_maximum_size;+ bool m_tab_preview_is_stale { true };+ bool m_tab_preview_request_in_flight { false };QPointer<ScreenshotViewer> m_screenshot_viewer;Vector<QPointer<QWidget>> m_floating_surfaces;bool m_suggestions_over_page { false };