Skip to content

Truly independent web browser

LibWeb: Pass WebGL extension names as AK spans

Andreas Kling11c4f56e40a0
diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_gl/exports.rs b/Libraries/LibWeb/Bindings/Rust/src/web_gl/exports.rs
index 578518cba17..80a6e5a6b80 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/web_gl/exports.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/web_gl/exports.rs
@@ -23,31 +23,9 @@ use super::web_gl_rendering_context::WebGLPowerPreference;
use super::web_gl_rendering_context_base::WebGLContextAttributes;
use crate::bindings::interface::{Interface, allocate, cell_into_abi, realm_arguments};
-#[repr(C)]
-pub struct ByteSpan {
- data: *const u8,
- size: usize,
-}
-
-/// # Safety
-///
-/// The spans are live, and each is the bytes of a string.
-unsafe fn extension_names(names: *const ByteSpan, name_count: usize) -> Vec<Vec<u8>> {
- if name_count == 0 {
- return Vec::new();
- }
- // SAFETY: The caller passes `name_count` live spans.
- let names = unsafe { ak::slice_from_raw_parts(names, name_count) };
- names
- .iter()
- .map(|name| {
- if name.size == 0 {
- return Vec::new();
- }
- // SAFETY: The caller passes the bytes of a string.
- unsafe { ak::slice_from_raw_parts(name.data, name.size) }.to_vec()
- })
- .collect()
+/// The bytes of the names of the extensions C++ lends.
+fn extension_names(names: ak::Span<'_, ak::Span<'_, u8>>) -> Vec<Vec<u8>> {
+ names.as_slice().iter().map(|name| name.as_slice().to_vec()).collect()
}
/// Mirrors `Web::WebGL::RustContextAttributes`.
@@ -112,7 +90,7 @@ impl From<WebGLContextAttributes> for ContextAttributes {
/// Mirrors `Web::WebGL::ContextCreationArguments`.
#[repr(C)]
-pub struct ContextCreationArguments {
+pub struct ContextCreationArguments<'a> {
/// A RemoteWebGLTransport that has created its context, with a reference that the new context takes over.
transport: *mut c_void,
is_webgl2: bool,
@@ -121,8 +99,7 @@ pub struct ContextCreationArguments {
canvas_host: *mut c_void,
canvas_is_offscreen: bool,
context_attributes: ContextAttributes,
- supported_extensions: *const ByteSpan,
- supported_extension_count: usize,
+ supported_extensions: ak::Span<'a, ak::Span<'a, u8>>,
}
/// The cell of a WebGLRenderingContext or of a WebGL2RenderingContext, which only C++ lends out, for as long as the
@@ -155,7 +132,7 @@ fn with_context<R>(context: &ContextCell, function: impl FnOnce(&ContextState) -
#[unsafe(no_mangle)]
pub unsafe extern "C" fn web_webgl_context_create(
vm: *mut c_void,
- arguments: &ContextCreationArguments,
+ arguments: &ContextCreationArguments<'_>,
) -> *mut c_void {
// SAFETY: The caller passes the live VM and a live realm.
let (vm, realm) = unsafe { realm_arguments(vm, arguments.realm) };
@@ -179,10 +156,7 @@ pub unsafe extern "C" fn web_webgl_context_create(
realm,
canvas,
context_attributes: arguments.context_attributes.into(),
- // SAFETY: The caller passes live names.
- supported_opengl_extensions: unsafe {
- extension_names(arguments.supported_extensions, arguments.supported_extension_count)
- },
+ supported_opengl_extensions: extension_names(arguments.supported_extensions),
});
match version {
WebGLVersion::WebGL1 => {
@@ -294,22 +268,20 @@ pub extern "C" fn web_webgl_context_actual_context_parameters(
///
/// # Safety
///
-/// `transport` is a RemoteWebGLTransport that has created its context, and the spans are live.
+/// `transport` is a RemoteWebGLTransport that has created its context.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn web_webgl_context_restore(
context: &ContextCell,
transport: *mut c_void,
- supported_extensions: *const ByteSpan,
- supported_extension_count: usize,
+ supported_extensions: ak::Span<'_, ak::Span<'_, u8>>,
) {
let transport = NonNull::new(transport).expect("C++ passes the transport");
- // SAFETY: The caller passes the transport with a reference, and live names.
- unsafe {
- let supported_opengl_extensions = extension_names(supported_extensions, supported_extension_count);
- with_context(context, |context| {
- context.restore(CppTransport::from_raw(transport), supported_opengl_extensions);
- });
- }
+ // SAFETY: The caller hands over a reference to the transport.
+ let transport = unsafe { CppTransport::from_raw(transport) };
+ let supported_opengl_extensions = extension_names(supported_extensions);
+ with_context(context, |context| {
+ context.restore(transport, supported_opengl_extensions);
+ });
}
#[unsafe(no_mangle)]
diff --git a/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.cpp b/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.cpp
index 70a0296af0b..7985e6e1706 100644
--- a/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.cpp
+++ b/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.cpp
@@ -61,11 +61,6 @@ struct RustContextAttributes {
bool xr_compatible { false };
};
-struct ByteSpan {
- u8 const* data { nullptr };
- size_t size { 0 };
-};
-
// `ContextCreationArguments` of Bindings/Rust/src/web_gl/exports.rs.
struct ContextCreationArguments {
void* transport { nullptr };
@@ -75,8 +70,7 @@ struct ContextCreationArguments {
void* canvas_host { nullptr };
bool canvas_is_offscreen { false };
RustContextAttributes context_attributes;
- ByteSpan const* supported_extensions { nullptr };
- size_t supported_extension_count { 0 };
+ ReadonlySpan<ReadonlyBytes> supported_extensions;
};
// `TexImageSourceKind` and `TexImageSourceResult` of Bindings/Rust/src/web_gl/canvas.rs.
@@ -125,7 +119,7 @@ bool web_webgl_context_mark_lost(void* context);
void web_webgl_context_set_restore_requested(void* context, bool requested);
bool web_webgl_context_wants_restoring(void* context);
void web_webgl_context_actual_context_parameters(void* context, RustContextAttributes* out_context_attributes);
-void web_webgl_context_restore(void* context, void* transport, ByteSpan const* supported_extensions, size_t supported_extension_count);
+void web_webgl_context_restore(void* context, void* transport, ReadonlySpan<ReadonlyBytes> supported_extensions);
bool web_webgl_context_xr_compatible(void* context);
void web_webgl_context_set_xr_compatible(void* context, bool xr_compatible);
@@ -216,12 +210,12 @@ struct RemoteWebGLContext {
NonnullRefPtr<RemoteWebGLTransport> transport;
Vector<String> supported_extensions;
- Vector<ByteSpan> supported_extension_spans() const
+ Vector<ReadonlyBytes> supported_extension_spans() const
{
- Vector<ByteSpan> spans;
+ Vector<ReadonlyBytes> spans;
spans.ensure_capacity(supported_extensions.size());
for (auto const& extension : supported_extensions)
- spans.unchecked_append({ extension.bytes().data(), extension.bytes().size() });
+ spans.unchecked_append(extension.bytes());
return spans;
}
};
@@ -269,8 +263,7 @@ JS::ThrowCompletionOr<WebGLRenderingContextBase*> WebGLRenderingContextBase::cre
.canvas_host = &canvas_host,
.canvas_is_offscreen = canvas.has<GC::Ref<HTML::OffscreenCanvas>>(),
.context_attributes = to_rust_context_attributes(context_attributes),
- .supported_extensions = supported_extensions.data(),
- .supported_extension_count = supported_extensions.size(),
+ .supported_extensions = supported_extensions.span(),
};
return static_cast<WebGLRenderingContextBase*>(web_webgl_context_create(JS::HostABI::vm_to_abi(realm.vm()), &arguments));
}
@@ -353,7 +346,7 @@ void WebGLRenderingContextBase::restore_context_after_compositor_reconnect()
return;
auto supported_extensions = remote->supported_extension_spans();
- web_webgl_context_restore(this, &remote->transport.leak_ref(), supported_extensions.data(), supported_extensions.size());
+ web_webgl_context_restore(this, &remote->transport.leak_ref(), supported_extensions.span());
HTML::queue_a_task(HTML::Task::Source::WebGL, nullptr, nullptr, GC::create_function(GC::Heap::the(), [this] {
fire_webgl_context_event(canvas_host(), EventNames::webglcontextrestored);