Skip to content

Truly independent web browser

LibWeb: Give the WebGL transport a C ABI

Aliaksandr Kalenike5e54b4a8716
diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt
index 308a04d041e..df98f12cd41 100644
--- a/Libraries/LibWeb/CMakeLists.txt
+++ b/Libraries/LibWeb/CMakeLists.txt
@@ -1048,6 +1048,7 @@ set(SOURCES
WebDriver/Screenshot.cpp
WebDriver/UserPrompt.cpp
WebGL/EventNames.cpp
+ WebGL/RemoteWebGLTransportFFI.cpp
WebGL/Extensions/ANGLEInstancedArrays.cpp
WebGL/Extensions/EXTBlendMinMax.cpp
WebGL/Extensions/EXTColorBufferFloat.cpp
diff --git a/Libraries/LibWeb/Compositor/CompositorConnection.cpp b/Libraries/LibWeb/Compositor/CompositorConnection.cpp
index 941529fce5f..1b4a5bfbc61 100644
--- a/Libraries/LibWeb/Compositor/CompositorConnection.cpp
+++ b/Libraries/LibWeb/Compositor/CompositorConnection.cpp
@@ -606,13 +606,13 @@ bool CompositorConnection::drain_webgl_command_buffer(Compositing::CanvasId canv
return true;
}
-void CompositorConnection::send_webgl_commands(Compositing::CanvasId canvas_id, ByteBuffer const& commands, Vector<Gfx::DecodedImageFrame> const& bitmaps)
+void CompositorConnection::send_webgl_commands(Compositing::CanvasId canvas_id, ReadonlyBytes commands, Vector<Gfx::DecodedImageFrame> const& bitmaps)
{
if (!can_send_message_to_compositor())
return;
auto shared_commands = MUST(Core::AnonymousBuffer::create_with_size(commands.size()));
- commands.bytes().copy_to({ shared_commands.data<u8>(), shared_commands.size() });
+ commands.copy_to({ shared_commands.data<u8>(), shared_commands.size() });
auto encoded_message = MUST(Messages::CompositorWebContentServer::WebglCommands::static_encode(canvas_id, shared_commands, bitmaps));
if (post_message(encoded_message).is_error())
diff --git a/Libraries/LibWeb/Compositor/CompositorConnection.h b/Libraries/LibWeb/Compositor/CompositorConnection.h
index 09dd6375270..b10a0ae3804 100644
--- a/Libraries/LibWeb/Compositor/CompositorConnection.h
+++ b/Libraries/LibWeb/Compositor/CompositorConnection.h
@@ -81,7 +81,7 @@ public:
void set_webgl_command_buffer(Compositing::CanvasId, Core::AnonymousBuffer const&);
void send_webgl_commands_from_shared_buffer(Compositing::CanvasId, u64 offset, u64 size_in_bytes, u64 flush_sequence_number, Vector<Gfx::DecodedImageFrame> const& bitmaps);
bool drain_webgl_command_buffer(Compositing::CanvasId);
- void send_webgl_commands(Compositing::CanvasId, ByteBuffer const&, Vector<Gfx::DecodedImageFrame> const& bitmaps);
+ void send_webgl_commands(Compositing::CanvasId, ReadonlyBytes, Vector<Gfx::DecodedImageFrame> const& bitmaps);
void present_webgl_canvas(Compositing::CanvasId, bool preserve_drawing_buffer);
void clear_webgl_drawing_buffer(Compositing::CanvasId);
ByteBuffer webgl_sync_call(Compositing::CanvasId, ByteBuffer request);
diff --git a/Libraries/LibWeb/Compositor/CompositorHost.cpp b/Libraries/LibWeb/Compositor/CompositorHost.cpp
index 429fcb20c79..9882ec74e23 100644
--- a/Libraries/LibWeb/Compositor/CompositorHost.cpp
+++ b/Libraries/LibWeb/Compositor/CompositorHost.cpp
@@ -187,7 +187,7 @@ private:
return m_connection->drain_webgl_command_buffer(*m_canvas_id);
}
- virtual void send_commands(ByteBuffer const& commands, Vector<Gfx::DecodedImageFrame> const& bitmaps) override
+ virtual void send_commands(ReadonlyBytes commands, Vector<Gfx::DecodedImageFrame> const& bitmaps) override
{
if (!m_canvas_id.has_value())
return;
diff --git a/Libraries/LibWeb/WebGL/RemoteWebGLTransport.h b/Libraries/LibWeb/WebGL/RemoteWebGLTransport.h
index 50110ff636d..46d46536aab 100644
--- a/Libraries/LibWeb/WebGL/RemoteWebGLTransport.h
+++ b/Libraries/LibWeb/WebGL/RemoteWebGLTransport.h
@@ -38,7 +38,7 @@ public:
virtual void send_commands_from_shared_buffer(u64 offset, u64 size_in_bytes, u64 flush_sequence_number, Vector<Gfx::DecodedImageFrame> const& bitmaps) = 0;
virtual bool wait_until_published_commands_executed() = 0;
- virtual void send_commands(ByteBuffer const&, Vector<Gfx::DecodedImageFrame> const& bitmaps) = 0;
+ virtual void send_commands(ReadonlyBytes, Vector<Gfx::DecodedImageFrame> const& bitmaps) = 0;
virtual void present_canvas(bool preserve_drawing_buffer) = 0;
virtual void clear_drawing_buffer() = 0;
virtual ByteBuffer sync_call(ByteBuffer request) = 0;
diff --git a/Libraries/LibWeb/WebGL/RemoteWebGLTransportFFI.cpp b/Libraries/LibWeb/WebGL/RemoteWebGLTransportFFI.cpp
new file mode 100644
index 00000000000..f26e1c2f361
--- /dev/null
+++ b/Libraries/LibWeb/WebGL/RemoteWebGLTransportFFI.cpp
@@ -0,0 +1,176 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/ByteBuffer.h>
+#include <AK/Vector.h>
+#include <AK/kmalloc.h>
+#include <LibCore/AnonymousBuffer.h>
+#include <LibGfx/Bitmap.h>
+#include <LibGfx/DecodedImageFrame.h>
+#include <LibGfx/ShareableBitmap.h>
+#include <LibWeb/WebGL/RemoteWebGLTransport.h>
+
+// The C ABI of RemoteWebGLTransport, for the WebGL command recorder in Libraries/LibWeb/Bindings/Rust/src/web_gl,
+// which declares these functions in host.rs.
+//
+// The recorder holds a transport and a bitmap as a pointer that owns one reference, and an anonymous buffer and an
+// image frame as a pointer to an AnonymousBufferOwnedByRecorder or a Gfx::DecodedImageFrame that it owns.
+
+namespace Web::WebGL {
+
+static_assert(sizeof(ReadPixelsResult) == 3 * sizeof(i32));
+static_assert(sizeof(Compositing::CanvasId) == sizeof(u64));
+
+extern "C" {
+
+void web_webgl_transport_unref(void* transport);
+bool web_webgl_transport_canvas_id(void const* transport, u64* out_canvas_id);
+void web_webgl_transport_destroy_context(void* transport);
+void web_webgl_transport_set_shared_command_buffer(void* transport, void const* anonymous_buffer);
+void web_webgl_transport_send_commands_from_shared_buffer(void* transport, u64 offset, u64 size_in_bytes, u64 flush_sequence_number, void const* const* frames, size_t frame_count);
+bool web_webgl_transport_wait_until_published_commands_executed(void* transport);
+void web_webgl_transport_send_commands(void* transport, u8 const* commands, size_t size_in_bytes, void const* const* frames, size_t frame_count);
+void web_webgl_transport_present_canvas(void* transport, bool preserve_drawing_buffer);
+void web_webgl_transport_clear_drawing_buffer(void* transport);
+void web_webgl_transport_sync_call(void* transport, u8 const* request, size_t request_size, void* reply_sink, void (*write_reply)(void* reply_sink, u8 const* reply, size_t reply_size));
+void web_webgl_transport_read_pixels_robust_angle(void* transport, i32 x, i32 y, i32 width, i32 height, u32 format, u32 type, i32 buf_size, void const* pixels_anonymous_buffer_or_null, ReadPixelsResult* out_result);
+bool web_webgl_transport_read_buffer_sub_data(void* transport, u32 target, i64 offset, i64 size, void const* data_anonymous_buffer);
+void* web_webgl_transport_read_back_drawing_buffer(void* transport, i32 x, i32 y, i32 width, i32 height);
+void* web_webgl_anonymous_buffer_create(size_t size, u8** out_data);
+void web_webgl_anonymous_buffer_destroy(void* anonymous_buffer);
+void web_webgl_frame_destroy(void* frame);
+void web_webgl_bitmap_unref(void* bitmap);
+}
+
+static RemoteWebGLTransport& transport_from(void* transport)
+{
+ return *static_cast<RemoteWebGLTransport*>(transport);
+}
+
+// NB: Core::AnonymousBuffer cannot be allocated with kmalloc itself: a class that derives from it and from a
+// ref-counted base would then have two operator new.
+struct AnonymousBufferOwnedByRecorder {
+ AK_ALLOC_WITH_KMALLOC;
+
+ Core::AnonymousBuffer buffer;
+};
+
+static Core::AnonymousBuffer const& anonymous_buffer_from(void const* anonymous_buffer)
+{
+ return static_cast<AnonymousBufferOwnedByRecorder const*>(anonymous_buffer)->buffer;
+}
+
+static Vector<Gfx::DecodedImageFrame> copy_frames(void const* const* frames, size_t frame_count)
+{
+ Vector<Gfx::DecodedImageFrame> copied_frames;
+ copied_frames.ensure_capacity(frame_count);
+ for (size_t i = 0; i < frame_count; ++i)
+ copied_frames.unchecked_append(*static_cast<Gfx::DecodedImageFrame const*>(frames[i]));
+ return copied_frames;
+}
+
+void web_webgl_transport_unref(void* transport)
+{
+ transport_from(transport).unref();
+}
+
+bool web_webgl_transport_canvas_id(void const* transport, u64* out_canvas_id)
+{
+ auto canvas_id = static_cast<RemoteWebGLTransport const*>(transport)->canvas_id();
+ if (!canvas_id.has_value())
+ return false;
+ *out_canvas_id = canvas_id->value();
+ return true;
+}
+
+void web_webgl_transport_destroy_context(void* transport)
+{
+ transport_from(transport).destroy_context();
+}
+
+void web_webgl_transport_set_shared_command_buffer(void* transport, void const* anonymous_buffer)
+{
+ transport_from(transport).set_shared_command_buffer(anonymous_buffer_from(anonymous_buffer));
+}
+
+void web_webgl_transport_send_commands_from_shared_buffer(void* transport, u64 offset, u64 size_in_bytes, u64 flush_sequence_number, void const* const* frames, size_t frame_count)
+{
+ transport_from(transport).send_commands_from_shared_buffer(offset, size_in_bytes, flush_sequence_number, copy_frames(frames, frame_count));
+}
+
+bool web_webgl_transport_wait_until_published_commands_executed(void* transport)
+{
+ return transport_from(transport).wait_until_published_commands_executed();
+}
+
+void web_webgl_transport_send_commands(void* transport, u8 const* commands, size_t size_in_bytes, void const* const* frames, size_t frame_count)
+{
+ transport_from(transport).send_commands({ commands, size_in_bytes }, copy_frames(frames, frame_count));
+}
+
+void web_webgl_transport_present_canvas(void* transport, bool preserve_drawing_buffer)
+{
+ transport_from(transport).present_canvas(preserve_drawing_buffer);
+}
+
+void web_webgl_transport_clear_drawing_buffer(void* transport)
+{
+ transport_from(transport).clear_drawing_buffer();
+}
+
+void web_webgl_transport_sync_call(void* transport, u8 const* request, size_t request_size, void* reply_sink, void (*write_reply)(void* reply_sink, u8 const* reply, size_t reply_size))
+{
+ auto reply = transport_from(transport).sync_call(MUST(ByteBuffer::copy(request, request_size)));
+ write_reply(reply_sink, reply.data(), reply.size());
+}
+
+void web_webgl_transport_read_pixels_robust_angle(void* transport, i32 x, i32 y, i32 width, i32 height, u32 format, u32 type, i32 buf_size, void const* pixels_anonymous_buffer_or_null, ReadPixelsResult* out_result)
+{
+ Core::AnonymousBuffer pixels;
+ if (pixels_anonymous_buffer_or_null)
+ pixels = anonymous_buffer_from(pixels_anonymous_buffer_or_null);
+ *out_result = transport_from(transport).read_pixels_robust_angle(x, y, width, height, format, type, buf_size, move(pixels));
+}
+
+bool web_webgl_transport_read_buffer_sub_data(void* transport, u32 target, i64 offset, i64 size, void const* data_anonymous_buffer)
+{
+ return transport_from(transport).read_buffer_sub_data(target, offset, size, anonymous_buffer_from(data_anonymous_buffer));
+}
+
+void* web_webgl_transport_read_back_drawing_buffer(void* transport, i32 x, i32 y, i32 width, i32 height)
+{
+ auto shareable_bitmap = transport_from(transport).read_back_drawing_buffer({ x, y, width, height });
+ if (!shareable_bitmap.is_valid())
+ return nullptr;
+ return &NonnullRefPtr<Gfx::Bitmap> { *shareable_bitmap.bitmap() }.leak_ref();
+}
+
+void* web_webgl_anonymous_buffer_create(size_t size, u8** out_data)
+{
+ auto buffer_or_error = Core::AnonymousBuffer::create_with_size(size);
+ if (buffer_or_error.is_error())
+ return nullptr;
+ auto* owned_buffer = new AnonymousBufferOwnedByRecorder { buffer_or_error.release_value() };
+ *out_data = owned_buffer->buffer.data<u8>();
+ return owned_buffer;
+}
+
+void web_webgl_anonymous_buffer_destroy(void* anonymous_buffer)
+{
+ delete static_cast<AnonymousBufferOwnedByRecorder*>(anonymous_buffer);
+}
+
+void web_webgl_frame_destroy(void* frame)
+{
+ delete static_cast<Gfx::DecodedImageFrame*>(frame);
+}
+
+void web_webgl_bitmap_unref(void* bitmap)
+{
+ static_cast<Gfx::Bitmap*>(bitmap)->unref();
+}
+
+}
diff --git a/Libraries/LibWeb/WebGL/WebGLContextProxyBase.cpp b/Libraries/LibWeb/WebGL/WebGLContextProxyBase.cpp
index ca789eb1b0d..f0091197638 100644
--- a/Libraries/LibWeb/WebGL/WebGLContextProxyBase.cpp
+++ b/Libraries/LibWeb/WebGL/WebGLContextProxyBase.cpp
@@ -72,7 +72,7 @@ void WebGLContextProxyBase::record_bytes(WebGLCommandType type, ReadonlyBytes pa
flush_commands();
WebGLCommandList oversized_command_list;
oversized_command_list.append_bytes(type, payload, inline_data);
- m_transport->send_commands(oversized_command_list.buffer(), {});
+ m_transport->send_commands(oversized_command_list.bytes(), {});
return;
}
@@ -135,7 +135,7 @@ void WebGLContextProxyBase::flush_commands()
if (m_out_of_line_commands.is_empty())
return;
- m_transport->send_commands(m_out_of_line_commands.buffer(), m_pending_bitmaps);
+ m_transport->send_commands(m_out_of_line_commands.bytes(), m_pending_bitmaps);
m_out_of_line_commands.clear_with_capacity();
m_pending_bitmaps.clear_with_capacity();
}