Skip to content

Truly independent web browser

LibWeb+Compositor: Create 2D canvas contexts without a round trip

Andreas Klinge1ad7969b2b5
diff --git a/Libraries/LibCompositing/DisplayList/CanvasSurfaceRegistry.h b/Libraries/LibCompositing/DisplayList/CanvasSurfaceRegistry.h
index c39ad9b4d06..e35f32cacb9 100644
--- a/Libraries/LibCompositing/DisplayList/CanvasSurfaceRegistry.h
+++ b/Libraries/LibCompositing/DisplayList/CanvasSurfaceRegistry.h
@@ -24,14 +24,14 @@ public:
CanvasId allocate_canvas_id()
{
- return CanvasId { m_next_canvas_id++ };
+ return reserve_canvas_ids(1);
}
- CanvasId create_canvas_surface(NonnullRefPtr<Gfx::PaintingSurface> surface)
+ CanvasId reserve_canvas_ids(u64 count)
{
- auto id = allocate_canvas_id();
- set_canvas_surface(id, move(surface));
- return id;
+ auto first = CanvasId { m_next_canvas_id };
+ m_next_canvas_id += count;
+ return first;
}
void set_canvas_surface(CanvasId id, NonnullRefPtr<Gfx::PaintingSurface> surface)
diff --git a/Libraries/LibGfx/CanvasCommandList.h b/Libraries/LibGfx/CanvasCommandList.h
index 59f3df1a2b9..12fc6f84768 100644
--- a/Libraries/LibGfx/CanvasCommandList.h
+++ b/Libraries/LibGfx/CanvasCommandList.h
@@ -60,6 +60,11 @@ using CanvasPaintStyle = Variant<Color, CanvasLinearGradient, CanvasRadialGradie
inline constexpr i64 max_canvas_area = 16384 * 16384;
+inline constexpr bool is_valid_canvas_2d_size(IntSize size)
+{
+ return !size.is_empty() && static_cast<i64>(size.width()) * static_cast<i64>(size.height()) <= max_canvas_area;
+}
+
struct CanvasGlyph {
FloatPoint position;
u32 glyph_id { 0 };
diff --git a/Libraries/LibWeb/Compositor/CompositorConnection.cpp b/Libraries/LibWeb/Compositor/CompositorConnection.cpp
index 97290797639..bfd94dcb812 100644
--- a/Libraries/LibWeb/Compositor/CompositorConnection.cpp
+++ b/Libraries/LibWeb/Compositor/CompositorConnection.cpp
@@ -304,10 +304,28 @@ Optional<Compositing::CanvasId> CompositorConnection::create_canvas_2d_context(G
if (!can_send_message_to_compositor())
return {};
- auto response = send_sync<Messages::CompositorWebContentServer::CreateCanvas2dContext>(size, alpha);
- if (!response->success())
+ // NB: The Compositor refuses the same sizes, so a context it is asked to create always exists afterwards.
+ if (!Gfx::is_valid_canvas_2d_size(size))
return {};
- return response->canvas_id();
+
+ // OPTIMIZATION: Creating a context is asynchronous, under an ID from a range reserved up front. A synchronous
+ // reply would wait for the Compositor to replay every canvas command queued before it.
+ if (m_reserved_canvas_ids_remaining == 0) {
+ auto response = send_sync_but_allow_failure<Messages::CompositorWebContentServer::ReserveCanvasIds>();
+ if (!response) {
+ did_lose_compositor();
+ return {};
+ }
+ m_next_reserved_canvas_id = response->first_canvas_id().value();
+ m_reserved_canvas_ids_remaining = response->count();
+ if (m_reserved_canvas_ids_remaining == 0)
+ return {};
+ }
+ auto canvas_id = Compositing::CanvasId { m_next_reserved_canvas_id++ };
+ --m_reserved_canvas_ids_remaining;
+
+ async_create_canvas_2d_context(canvas_id, size, alpha);
+ return canvas_id;
}
void CompositorConnection::update_canvas_2d_stream(Compositing::Canvas2DCommandStream& stream)
diff --git a/Libraries/LibWeb/Compositor/CompositorConnection.h b/Libraries/LibWeb/Compositor/CompositorConnection.h
index f661dde82ad..218eea817a3 100644
--- a/Libraries/LibWeb/Compositor/CompositorConnection.h
+++ b/Libraries/LibWeb/Compositor/CompositorConnection.h
@@ -131,6 +131,8 @@ private:
Optional<PendingScreenshot> take_screenshot(Compositing::ScreenshotRequestId);
NonnullRefPtr<CompositorConnectionFrameSink> m_frame_sink;
+ u64 m_next_reserved_canvas_id { 0 };
+ u64 m_reserved_canvas_ids_remaining { 0 };
HashMap<Compositing::ScreenshotRequestId, PendingScreenshot> m_screenshots;
// What the compositor process scrolled since the last rendering update adopted it, per context.
diff --git a/Services/Compositor/CanvasHost.cpp b/Services/Compositor/CanvasHost.cpp
index d241ffe63a1..ff35f18b1ba 100644
--- a/Services/Compositor/CanvasHost.cpp
+++ b/Services/Compositor/CanvasHost.cpp
@@ -31,7 +31,7 @@ CanvasHost::~CanvasHost()
OwnPtr<Gfx::CanvasCommandPlayer> CanvasHost::create_2d_command_player(Gfx::IntSize size, bool alpha)
{
- if (size.is_empty() || static_cast<i64>(size.width()) * static_cast<i64>(size.height()) > Gfx::max_canvas_area)
+ if (!Gfx::is_valid_canvas_2d_size(size))
return nullptr;
auto format = alpha ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;
@@ -90,20 +90,20 @@ HostWebGLContext& CanvasHost::as_webgl(Context& context)
return **webgl_context;
}
-Optional<Compositing::CanvasId> CanvasHost::create_2d_context(Gfx::IntSize size, bool alpha)
+bool CanvasHost::create_2d_context(Compositing::CanvasId canvas_id, Gfx::IntSize size, bool alpha)
{
auto command_player = create_2d_command_player(size, alpha);
if (!command_player)
- return {};
+ return false;
auto presented_surface = create_presented_canvas_surface(command_player->surface());
- auto canvas_id = m_canvas_surface_registry.create_canvas_surface(presented_surface);
+ m_canvas_surface_registry.set_canvas_surface(canvas_id, presented_surface);
Canvas2DContext context {
.command_player = command_player.release_nonnull(),
.presented_surface = move(presented_surface),
};
m_contexts.set(canvas_id, move(context));
- return canvas_id;
+ return true;
}
CanvasHost::CreateWebGLContextResult CanvasHost::create_webgl_context(Compositing::WebGL::WebGLVersion version, Gfx::IntSize size, bool depth, bool stencil, bool antialias)
diff --git a/Services/Compositor/CanvasHost.h b/Services/Compositor/CanvasHost.h
index d1f1d1d18ed..ef08aafd939 100644
--- a/Services/Compositor/CanvasHost.h
+++ b/Services/Compositor/CanvasHost.h
@@ -50,7 +50,7 @@ public:
CanvasHost(RefPtr<Gfx::SkiaBackendContext>, Compositing::CanvasSurfaceRegistry&);
~CanvasHost();
- Optional<Compositing::CanvasId> create_2d_context(Gfx::IntSize, bool alpha);
+ bool create_2d_context(Compositing::CanvasId, Gfx::IntSize, bool alpha);
CreateWebGLContextResult create_webgl_context(Compositing::WebGL::WebGLVersion, Gfx::IntSize, bool depth, bool stencil, bool antialias);
void destroy_context(Compositing::CanvasId);
bool has_context(Compositing::CanvasId) const;
diff --git a/Services/Compositor/CompositorWebContentServer.ipc b/Services/Compositor/CompositorWebContentServer.ipc
index 80ca4f3c6f0..0b992e19054 100644
--- a/Services/Compositor/CompositorWebContentServer.ipc
+++ b/Services/Compositor/CompositorWebContentServer.ipc
@@ -35,7 +35,9 @@ endpoint CompositorWebContentServer
update_visual_context_tree(Web::CompositorContextId context_id, Compositing::AccumulatedVisualContextTree visual_context_tree, Compositing::DisplayListResourceTransaction resource_transaction) =|
update_scroll_state(Web::CompositorContextId context_id, Compositing::ScrollStateSnapshot scroll_state_snapshot, Compositing::KeyboardScrollState keyboard_scroll_state) =|
- create_canvas_2d_context(Gfx::IntSize size, bool alpha) => (bool success, Compositing::CanvasId canvas_id)
+ // Hands WebContent a range of canvas IDs it may name its own 2D contexts with, so creating one needs no round trip.
+ reserve_canvas_ids() => (Compositing::CanvasId first_canvas_id, u64 count)
+ create_canvas_2d_context(Compositing::CanvasId canvas_id, Gfx::IntSize size, bool alpha) =|
update_canvas_2d_stream(Vector<Compositing::Canvas2DCommandStreamSegment> segments, Vector<Compositing::DisplayListFontResource> fonts) =|
destroy_canvas_context(Compositing::CanvasId canvas_id) =|
get_canvas_pixels(Compositing::CanvasId canvas_id, Gfx::IntRect rect, Gfx::AlphaType alpha_type) => (Gfx::ShareableBitmap pixels)
diff --git a/Services/Compositor/ConnectionFromWebContent.cpp b/Services/Compositor/ConnectionFromWebContent.cpp
index a7f8dc0489a..f1b67907a95 100644
--- a/Services/Compositor/ConnectionFromWebContent.cpp
+++ b/Services/Compositor/ConnectionFromWebContent.cpp
@@ -242,12 +242,28 @@ void ConnectionFromWebContent::update_scroll_state(Web::CompositorContextId cont
m_compositor_state->update_scroll_state(context_id, move(scroll_state_snapshot), move(keyboard_scroll_state));
}
-Messages::CompositorWebContentServer::CreateCanvas2dContextResponse ConnectionFromWebContent::create_canvas_2d_context(Gfx::IntSize size, bool alpha)
+Messages::CompositorWebContentServer::ReserveCanvasIdsResponse ConnectionFromWebContent::reserve_canvas_ids()
{
- auto canvas_id = m_canvas_host.create_2d_context(size, alpha);
- if (!canvas_id.has_value())
- return { false, Compositing::CanvasId { 0 } };
- return { true, *canvas_id };
+ static constexpr u64 reserved_canvas_id_count = 1 << 20;
+ if (!m_reserved_canvas_ids.is_used_up()) {
+ did_misbehave("WebContent reserved canvas IDs before using up its range");
+ return { Compositing::CanvasId { 0 }, 0 };
+ }
+ auto first_canvas_id = m_compositor_state->canvas_surface_registry().reserve_canvas_ids(reserved_canvas_id_count);
+ m_reserved_canvas_ids = { first_canvas_id.value(), first_canvas_id.value() + reserved_canvas_id_count };
+ return { first_canvas_id, reserved_canvas_id_count };
+}
+
+void ConnectionFromWebContent::create_canvas_2d_context(Compositing::CanvasId canvas_id, Gfx::IntSize size, bool alpha)
+{
+ // NB: WebContent names its contexts with the IDs of its range in order, so each ID names one context only.
+ if (m_reserved_canvas_ids.is_used_up() || canvas_id.value() != m_reserved_canvas_ids.next) {
+ did_misbehave("WebContent named a 2D canvas context with an ID other than the next one it reserved");
+ return;
+ }
+ ++m_reserved_canvas_ids.next;
+ // NB: A context that cannot be created is not a protocol violation: commands recorded for it are dropped.
+ (void)m_canvas_host.create_2d_context(canvas_id, size, alpha);
}
void ConnectionFromWebContent::update_canvas_2d_stream(Vector<Compositing::Canvas2DCommandStreamSegment> segments, Vector<Compositing::DisplayListFontResource> fonts)
diff --git a/Services/Compositor/ConnectionFromWebContent.h b/Services/Compositor/ConnectionFromWebContent.h
index cfc6f63feca..4e95d4bef89 100644
--- a/Services/Compositor/ConnectionFromWebContent.h
+++ b/Services/Compositor/ConnectionFromWebContent.h
@@ -52,7 +52,8 @@ private:
virtual void update_visual_context_tree(Web::CompositorContextId, Compositing::AccumulatedVisualContextTree, Compositing::DisplayListResourceTransaction) override;
virtual void update_scroll_state(Web::CompositorContextId, Compositing::ScrollStateSnapshot, Compositing::KeyboardScrollState) override;
virtual void update_display_list_resources(Web::CompositorContextId, Compositing::DisplayListResourceTransaction) override;
- virtual Messages::CompositorWebContentServer::CreateCanvas2dContextResponse create_canvas_2d_context(Gfx::IntSize, bool) override;
+ virtual Messages::CompositorWebContentServer::ReserveCanvasIdsResponse reserve_canvas_ids() override;
+ virtual void create_canvas_2d_context(Compositing::CanvasId, Gfx::IntSize, bool) override;
virtual void update_canvas_2d_stream(Vector<Compositing::Canvas2DCommandStreamSegment>, Vector<Compositing::DisplayListFontResource>) override;
virtual void destroy_canvas_context(Compositing::CanvasId) override;
virtual Messages::CompositorWebContentServer::GetCanvasPixelsResponse get_canvas_pixels(Compositing::CanvasId, Gfx::IntRect, Gfx::AlphaType) override;
@@ -104,6 +105,15 @@ private:
NonnullRefPtr<CompositorState> m_compositor_state;
CanvasHost m_canvas_host;
+
+ // The canvas IDs this WebContent has reserved and not yet named a 2D context with.
+ struct ReservedCanvasIds {
+ u64 next { 0 };
+ u64 end { 0 };
+
+ bool is_used_up() const { return next == end; }
+ };
+ ReservedCanvasIds m_reserved_canvas_ids;
Function<void(ConnectionFromWebContent&)> m_on_death;
// The presentation client end of this WebContent's video presentation channel (connect-only for now).