Truly independent web browser
LibWeb: Report a screenshot the compositor didn’t take as a failure
sideshowbarkere92b8251d9c5
diff --git a/Libraries/LibWeb/Compositor/CompositorConnection.cpp b/Libraries/LibWeb/Compositor/CompositorConnection.cppindex 2fd4faf8a1a..a3ce8af4245 100644--- a/Libraries/LibWeb/Compositor/CompositorConnection.cpp+++ b/Libraries/LibWeb/Compositor/CompositorConnection.cpp@@ -799,11 +799,11 @@ bool CompositorConnection::read_webgl_buffer_sub_data(Compositing::CanvasId canvreturn response->success();}-void CompositorConnection::request_screenshot(Web::CompositorContextId context_id, NonnullRefPtr<Gfx::Bitmap> target_bitmap, Function<void()>&& callback)+void CompositorConnection::request_screenshot(Web::CompositorContextId context_id, NonnullRefPtr<Gfx::Bitmap> target_bitmap, Function<void(ScreenshotResult)>&& callback){if (!can_send_message_to_compositor()) {if (callback)- callback();+ callback(ScreenshotResult::Failed);return;}@@ -878,7 +878,7 @@ void CompositorConnection::did_complete_screenshot(Compositing::ScreenshotRequesreturn;if (pending_screenshot->callback)- pending_screenshot->callback();+ pending_screenshot->callback(ScreenshotResult::Taken);}void CompositorConnection::did_fail_screenshot(Compositing::ScreenshotRequestId request_id)@@ -888,7 +888,7 @@ void CompositorConnection::did_fail_screenshot(Compositing::ScreenshotRequestIdreturn;if (pending_screenshot->callback)- pending_screenshot->callback();+ pending_screenshot->callback(ScreenshotResult::Failed);}void CompositorConnection::did_lose_compositor()@@ -900,7 +900,7 @@ void CompositorConnection::did_lose_compositor()for (auto& entry : m_screenshots) {if (entry.value.callback)- entry.value.callback();+ entry.value.callback(ScreenshotResult::Failed);}m_screenshots.clear();diff --git a/Libraries/LibWeb/Compositor/CompositorConnection.h b/Libraries/LibWeb/Compositor/CompositorConnection.hindex c500fb56a59..31f37c7134c 100644--- a/Libraries/LibWeb/Compositor/CompositorConnection.h+++ b/Libraries/LibWeb/Compositor/CompositorConnection.h@@ -77,7 +77,7 @@ public:void viewport_size_updated(Web::CompositorContextId, Gfx::IntSize, double device_pixel_ratio, Compositing::WindowResizingInProgress);bool request_rendering_opportunity(Web::CompositorContextId, double maximum_frames_per_second);void hurry_rendering_opportunity(Web::CompositorContextId);- void request_screenshot(Web::CompositorContextId, NonnullRefPtr<Gfx::Bitmap>, Function<void()>&&);+ void request_screenshot(Web::CompositorContextId, NonnullRefPtr<Gfx::Bitmap>, Function<void(ScreenshotResult)>&&);Optional<Compositing::CanvasId> create_webgl_context(Compositing::WebGL::WebGLVersion, Gfx::IntSize, bool depth, bool stencil, bool antialias, Vector<String>& out_supported_extensions);void set_webgl_command_buffer(Compositing::CanvasId, Core::AnonymousBuffer const&);@@ -105,7 +105,7 @@ private:struct PendingScreenshot {NonnullRefPtr<Gfx::Bitmap> target_bitmap;- Function<void()> callback;+ Function<void(ScreenshotResult)> callback;};virtual void die() override;diff --git a/Libraries/LibWeb/Compositor/CompositorHost.cpp b/Libraries/LibWeb/Compositor/CompositorHost.cppindex 2a8309d6a69..22058db39ff 100644--- a/Libraries/LibWeb/Compositor/CompositorHost.cpp+++ b/Libraries/LibWeb/Compositor/CompositorHost.cpp@@ -117,7 +117,7 @@ void CompositorContextHandle::hurry_rendering_opportunity()m_host.hurry_rendering_opportunity(m_context_id);}-void CompositorContextHandle::request_screenshot(NonnullRefPtr<Gfx::Bitmap> target_bitmap, Function<void()>&& callback)+void CompositorContextHandle::request_screenshot(NonnullRefPtr<Gfx::Bitmap> target_bitmap, Function<void(ScreenshotResult)>&& callback){m_host.flush_canvas_2d_stream();m_host.request_screenshot(m_context_id, move(target_bitmap), move(callback));@@ -535,14 +535,14 @@ void CompositorHost::hurry_rendering_opportunity(Web::CompositorContextId contexconnection->hurry_rendering_opportunity(context_id);}-void CompositorHost::request_screenshot(Web::CompositorContextId context_id, NonnullRefPtr<Gfx::Bitmap> target_bitmap, Function<void()>&& callback)+void CompositorHost::request_screenshot(Web::CompositorContextId context_id, NonnullRefPtr<Gfx::Bitmap> target_bitmap, Function<void(ScreenshotResult)>&& callback){if (auto* connection = compositor_connection()) {connection->request_screenshot(context_id, move(target_bitmap), move(callback));return;}if (callback)- callback();+ callback(ScreenshotResult::Failed);}}diff --git a/Libraries/LibWeb/Compositor/CompositorHost.h b/Libraries/LibWeb/Compositor/CompositorHost.hindex 3afb17ce110..e57f137eb61 100644--- a/Libraries/LibWeb/Compositor/CompositorHost.h+++ b/Libraries/LibWeb/Compositor/CompositorHost.h@@ -33,6 +33,12 @@ class CompositorHost;class NavigablePresenter;struct CompositorFrame;+// Whether the compositor took a requested screenshot, so painted the bitmap the request handed it.+enum class ScreenshotResult : u8 {+ Taken,+ Failed,+};+struct PlaceholderCanvasLink {Compositing::CanvasId canvas_id;u64 secret { 0 };@@ -71,7 +77,7 @@ public:void viewport_size_updated(Gfx::IntSize, double device_pixel_ratio, Compositing::WindowResizingInProgress);bool request_rendering_opportunity(double maximum_frames_per_second);void hurry_rendering_opportunity();- void request_screenshot(NonnullRefPtr<Gfx::Bitmap>, Function<void()>&& callback);+ void request_screenshot(NonnullRefPtr<Gfx::Bitmap>, Function<void(ScreenshotResult)>&& callback);// Sends the canvas commands a frame may sample ahead of it, and answers whether the compositor can be reached.bool ready_for_frame();@@ -133,7 +139,7 @@ public:void viewport_size_updated(Web::CompositorContextId, Gfx::IntSize, double device_pixel_ratio, Compositing::WindowResizingInProgress);bool request_rendering_opportunity(Web::CompositorContextId, double maximum_frames_per_second);void hurry_rendering_opportunity(Web::CompositorContextId);- void request_screenshot(Web::CompositorContextId, NonnullRefPtr<Gfx::Bitmap>, Function<void()>&& callback);+ void request_screenshot(Web::CompositorContextId, NonnullRefPtr<Gfx::Bitmap>, Function<void(ScreenshotResult)>&& callback);protected:CompositorHost();diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cppindex b6663914fc2..ce0d80dc0cb 100644--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp@@ -7124,10 +7124,10 @@ bool LocalNavigable::paint_next_frame_if_needed(DOM::UpdateLayoutReason layout_rreturn true;}-void LocalNavigable::render_screenshot(NonnullRefPtr<Gfx::Bitmap> target, PaintConfig paint_config, Function<void()>&& callback)+void LocalNavigable::render_screenshot(NonnullRefPtr<Gfx::Bitmap> target, PaintConfig paint_config, Function<void(Compositor::ScreenshotResult)>&& callback){if (!has_compositor_context()) {- callback();+ callback(Compositor::ScreenshotResult::Failed);return;}@@ -7141,7 +7141,7 @@ void LocalNavigable::render_screenshot(NonnullRefPtr<Gfx::Bitmap> target, PaintC}if (!commit_frame(paint_config)) {- callback();+ callback(Compositor::ScreenshotResult::Failed);return;}// The screenshot is of what the compositor composes, so every frame committed before it is presented first.diff --git a/Libraries/LibWeb/HTML/LocalNavigable.h b/Libraries/LibWeb/HTML/LocalNavigable.hindex 60f38d23aeb..ada126c9399 100644--- a/Libraries/LibWeb/HTML/LocalNavigable.h+++ b/Libraries/LibWeb/HTML/LocalNavigable.h@@ -318,7 +318,7 @@ public:bool has_recording_in_flight() const { return m_recording_in_flight; }// Paints into the target, which must be a shareable bitmap, then calls the callback.- void render_screenshot(NonnullRefPtr<Gfx::Bitmap> target, PaintConfig, Function<void()>&& callback);+ void render_screenshot(NonnullRefPtr<Gfx::Bitmap> target, PaintConfig, Function<void(Compositor::ScreenshotResult)>&& callback);Compositing::DisplayListResourceStorage& display_list_resource_storage() { return presenter().display_list_resource_storage(); }// Tells the clock lanes of the active document whether a task begins or the event loop goes idle: a task that beginsdiff --git a/Libraries/LibWeb/Page/Page.cpp b/Libraries/LibWeb/Page/Page.cppindex 0450ffb736f..1a33c40507a 100644--- a/Libraries/LibWeb/Page/Page.cpp+++ b/Libraries/LibWeb/Page/Page.cpp@@ -298,7 +298,11 @@ void Page::process_screenshot_requests()}auto bitmap = bitmap_or_error.release_value();HTML::PaintConfig paint_config { .canvas_fill_rect = rect.to_type<int>() };- navigable->render_screenshot(bitmap, paint_config, [bitmap, &client] {+ navigable->render_screenshot(bitmap, paint_config, [bitmap, &client](auto result) {+ if (result == Compositor::ScreenshotResult::Failed) {+ client.page_did_take_screenshot({});+ return;+ }client.page_did_take_screenshot(bitmap->to_shareable_bitmap());});} else {@@ -315,7 +319,11 @@ void Page::process_screenshot_requests()}auto bitmap = bitmap_or_error.release_value();HTML::PaintConfig paint_config { .paint_overlay = true, .canvas_fill_rect = rect.to_type<int>() };- navigable->render_screenshot(bitmap, paint_config, [bitmap, &client] {+ navigable->render_screenshot(bitmap, paint_config, [bitmap, &client](auto result) {+ if (result == Compositor::ScreenshotResult::Failed) {+ client.page_did_take_screenshot({});+ return;+ }client.page_did_take_screenshot(bitmap->to_shareable_bitmap());});}diff --git a/Libraries/LibWeb/WebDriver/BiDi/BrowsingContext.cpp b/Libraries/LibWeb/WebDriver/BiDi/BrowsingContext.cppindex 12c257ab89f..d9ad5159766 100644--- a/Libraries/LibWeb/WebDriver/BiDi/BrowsingContext.cpp+++ b/Libraries/LibWeb/WebDriver/BiDi/BrowsingContext.cpp@@ -1051,7 +1051,10 @@ void capture_screenshot(HTML::Navigable& navigable, JsonObject const& params, Onauto bitmap = framebuffer.release_value();HTML::PaintConfig paint_config { .canvas_fill_rect = Gfx::IntRect { {}, viewport_device_rect.size() } };- navigable->render_screenshot(bitmap, paint_config, [state, framebuffer = bitmap, source_rectangle, destination_size, format_type = move(format_type), format_quality] mutable {+ navigable->render_screenshot(bitmap, paint_config, [state, framebuffer = bitmap, source_rectangle, destination_size, format_type = move(format_type), format_quality](auto result) mutable {+ if (result == Compositor::ScreenshotResult::Failed)+ return state->complete(Error::from_code(ErrorCode::UnsupportedOperation, "The compositor did not take the screenshot"sv));+auto cropped = framebuffer->cropped(source_rectangle);// NB: A screenshot too large to allocate is an unsupported operation.if (cropped.is_error())diff --git a/Libraries/LibWeb/WebDriver/Screenshot.cpp b/Libraries/LibWeb/WebDriver/Screenshot.cppindex 0380af15fc4..4012d469c6c 100644--- a/Libraries/LibWeb/WebDriver/Screenshot.cpp+++ b/Libraries/LibWeb/WebDriver/Screenshot.cpp@@ -68,7 +68,12 @@ void draw_bounding_box_from_the_framebuffer(HTML::BrowsingContext& browsing_contauto bitmap = MUST(Gfx::Bitmap::create_shareable(Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, Gfx::IntSize { paint_width, paint_height }));HTML::PaintConfig paint_config { .canvas_fill_rect = paint_rect };- browsing_context.active_document()->navigable()->render_screenshot(bitmap, paint_config, [bitmap, element = GC::make_root(element), canvas = GC::make_root(canvas), callback = move(callback)] mutable {+ browsing_context.active_document()->navigable()->render_screenshot(bitmap, paint_config, [bitmap, element = GC::make_root(element), canvas = GC::make_root(canvas), callback = move(callback)](auto result) mutable {+ if (result == Compositor::ScreenshotResult::Failed) {+ callback(Error::from_code(ErrorCode::UnableToCaptureScreen, "The compositor did not take the screenshot"sv));+ return;+ }+HTML::TemporaryExecutionContext execution_context { element->document().relevant_settings_object() };auto image_bitmap = HTML::ImageBitmap::create(bitmap);diff --git a/Tests/LibWeb/test-web/main.cpp b/Tests/LibWeb/test-web/main.cppindex 75afa6f1bd5..401c792def7 100644--- a/Tests/LibWeb/test-web/main.cpp+++ b/Tests/LibWeb/test-web/main.cpp@@ -727,6 +727,10 @@ static void run_ref_test(TestWebView& view, TestRunContext& context, Test& test,auto& test = context.tests[test_index];test.expectation_screenshot = move(screenshot);view.reset_zoom();+ if (!test.expectation_screenshot) {+ view.on_test_complete({ test_index, TestResult::Fail });+ return;+ }// A test passes if at least one of its match references matches, and all of its mismatch references// mismatch. https://web-platform-tests.org/writing-tests/reftests.html#multiple-references@@ -767,6 +771,10 @@ static void run_ref_test(TestWebView& view, TestRunContext& context, Test& test,view.take_screenshot()->when_resolved([&view, &context, test_index](RefPtr<Gfx::Bitmap const> screenshot) {context.tests[test_index].actual_screenshot = move(screenshot);view.reset_zoom();+ if (!context.tests[test_index].actual_screenshot) {+ view.on_test_complete({ test_index, TestResult::Fail });+ return;+ }view.run_javascript("internals.loadReferenceTestMetadata();"_string);});}@@ -836,6 +844,8 @@ static void run_screenshot_test(TestWebView& view, TestRunContext& context, Testauto handle_completed_test = [&context, test_index, url]() -> ErrorOr<TestResult> {auto& test = context.tests[test_index];+ if (!test.actual_screenshot)+ return Error::from_string_literal("The compositor did not take the screenshot");auto& actual = *test.actual_screenshot;// Try to load and compare against existing expected PNG first.