Use surfman with glow bindings (take II) (#35422)

* Reapply "Use surfman with glow bindings (#34328)" (#35402)

This reverts commit 0fed99590a.

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>

* update surfman

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>

---------

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
This commit is contained in:
Samson 2025-02-13 08:26:38 +01:00 committed by GitHub
parent 48e38bcaa2
commit 610a1c2303
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 69 additions and 97 deletions

2
Cargo.lock generated
View file

@ -7343,7 +7343,7 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
[[package]]
name = "surfman"
version = "0.9.8"
source = "git+https://github.com/servo/surfman?rev=300789ddbda45c89e9165c31118bf1c4c07f89f6#300789ddbda45c89e9165c31118bf1c4c07f89f6"
source = "git+https://github.com/servo/surfman?rev=f7688b4585f9e0b5d4bf8ee8e4a91e82349610b1#f7688b4585f9e0b5d4bf8ee8e4a91e82349610b1"
dependencies = [
"bitflags 2.8.0",
"cfg_aliases",

View file

@ -134,7 +134,7 @@ style_config = { git = "https://github.com/servo/stylo", branch = "2025-02-03" }
style_dom = { git = "https://github.com/servo/stylo", package = "dom", branch = "2025-02-03" }
style_malloc_size_of = { package = "malloc_size_of", git = "https://github.com/servo/stylo", branch = "2025-02-03", features = ["servo"] }
style_traits = { git = "https://github.com/servo/stylo", branch = "2025-02-03", features = ["servo"] }
surfman = { git = "https://github.com/servo/surfman", rev = "300789ddbda45c89e9165c31118bf1c4c07f89f6", features = ["chains"] }
surfman = { git = "https://github.com/servo/surfman", rev = "f7688b4585f9e0b5d4bf8ee8e4a91e82349610b1", features = ["chains"] }
syn = { version = "2", default-features = false, features = ["clone-impls", "derive", "parsing"] }
synstructure = "0.13"
taffy = { version = "0.7.5", default-features = false, features = ["detailed_layout_info", "grid", "serde", "std"] }

View file

@ -26,8 +26,8 @@ use euclid::default::Size2D;
use fnv::FnvHashMap;
use glow::{
self as gl, bytes_per_type, components_per_format, ActiveTransformFeedback, Context as Gl,
HasContext, NativeFramebuffer, NativeTransformFeedback, NativeUniformLocation,
NativeVertexArray, PixelUnpackData, ShaderPrecisionFormat,
HasContext, NativeTransformFeedback, NativeUniformLocation, NativeVertexArray, PixelUnpackData,
ShaderPrecisionFormat,
};
use half::f16;
use log::{debug, error, trace, warn};
@ -49,7 +49,6 @@ use crate::webgl_limits::GLLimitsDetect;
#[cfg(feature = "webxr")]
use crate::webxr::{WebXRBridge, WebXRBridgeContexts, WebXRBridgeInit};
type GLuint = u32;
type GLint = i32;
fn native_uniform_location(location: i32) -> Option<NativeUniformLocation> {
@ -606,10 +605,7 @@ impl WebGLThread {
.framebuffer_object;
unsafe {
gl.bind_framebuffer(
gl::FRAMEBUFFER,
NonZeroU32::new(framebuffer).map(NativeFramebuffer),
);
gl.bind_framebuffer(gl::FRAMEBUFFER, framebuffer);
gl.viewport(0, 0, size.width as i32, size.height as i32);
gl.scissor(0, 0, size.width as i32, size.height as i32);
gl.clear_color(0., 0., 0., !has_alpha as u32 as f32);
@ -839,7 +835,7 @@ impl WebGLThread {
.unwrap()
.unwrap();
debug!(
"... rebound framebuffer {}, new back buffer surface is {:?}",
"... rebound framebuffer {:?}, new back buffer surface is {:?}",
framebuffer_object, id
);
@ -2699,14 +2695,13 @@ impl WebGLImpl {
) {
let id = match request {
WebGLFramebufferBindingRequest::Explicit(id) => Some(id.glow()),
WebGLFramebufferBindingRequest::Default => NonZeroU32::new(
WebGLFramebufferBindingRequest::Default => {
device
.context_surface_info(ctx)
.unwrap()
.expect("No surface attached!")
.framebuffer_object,
)
.map(NativeFramebuffer),
.framebuffer_object
},
};
debug!("WebGLImpl::bind_framebuffer: {:?}", id);
@ -3189,9 +3184,8 @@ struct FramebufferRebindingInfo {
impl FramebufferRebindingInfo {
fn detect(device: &Device, context: &Context, gl: &Gl) -> FramebufferRebindingInfo {
unsafe {
let (mut read_framebuffer, mut draw_framebuffer) = ([0], [0]);
gl.get_parameter_i32_slice(gl::READ_FRAMEBUFFER_BINDING, &mut read_framebuffer);
gl.get_parameter_i32_slice(gl::DRAW_FRAMEBUFFER_BINDING, &mut draw_framebuffer);
let read_framebuffer = gl.get_parameter_framebuffer(gl::READ_FRAMEBUFFER_BINDING);
let draw_framebuffer = gl.get_parameter_framebuffer(gl::DRAW_FRAMEBUFFER_BINDING);
let context_surface_framebuffer = device
.context_surface_info(context)
@ -3200,10 +3194,10 @@ impl FramebufferRebindingInfo {
.framebuffer_object;
let mut flags = FramebufferRebindingFlags::empty();
if context_surface_framebuffer == read_framebuffer[0] as GLuint {
if context_surface_framebuffer == read_framebuffer {
flags.insert(FramebufferRebindingFlags::REBIND_READ_FRAMEBUFFER);
}
if context_surface_framebuffer == draw_framebuffer[0] as GLuint {
if context_surface_framebuffer == draw_framebuffer {
flags.insert(FramebufferRebindingFlags::REBIND_DRAW_FRAMEBUFFER);
}
@ -3228,23 +3222,13 @@ impl FramebufferRebindingInfo {
.flags
.contains(FramebufferRebindingFlags::REBIND_READ_FRAMEBUFFER)
{
unsafe {
gl.bind_framebuffer(
gl::READ_FRAMEBUFFER,
NonZeroU32::new(context_surface_framebuffer).map(NativeFramebuffer),
)
};
unsafe { gl.bind_framebuffer(gl::READ_FRAMEBUFFER, context_surface_framebuffer) };
}
if self
.flags
.contains(FramebufferRebindingFlags::REBIND_DRAW_FRAMEBUFFER)
{
unsafe {
gl.bind_framebuffer(
gl::DRAW_FRAMEBUFFER,
NonZeroU32::new(context_surface_framebuffer).map(NativeFramebuffer),
)
};
unsafe { gl.bind_framebuffer(gl::DRAW_FRAMEBUFFER, context_surface_framebuffer) };
}
unsafe {

View file

@ -3,6 +3,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::collections::HashMap;
use std::num::NonZeroU32;
use canvas_traits::webgl::{
webgl_channel, WebGLContextId, WebGLMsg, WebGLSender, WebXRCommand, WebXRLayerManagerId,
@ -25,7 +26,7 @@ use crate::webgl_thread::{GLContextData, WebGLThread};
pub(crate) struct WebXRBridge {
factory_receiver: crossbeam_channel::Receiver<WebXRLayerManagerFactory<WebXRSurfman>>,
managers: HashMap<WebXRLayerManagerId, Box<dyn WebXRLayerManagerAPI<WebXRSurfman>>>,
next_manager_id: u32,
next_manager_id: NonZeroU32,
}
impl WebXRBridge {
@ -34,7 +35,7 @@ impl WebXRBridge {
factory_receiver, ..
} = init;
let managers = HashMap::new();
let next_manager_id = 1;
let next_manager_id = NonZeroU32::MIN;
WebXRBridge {
factory_receiver,
managers,
@ -55,8 +56,11 @@ impl WebXRBridge {
.recv()
.map_err(|_| WebXRError::CommunicationError)?;
let manager = factory.build(device, contexts)?;
let manager_id = unsafe { WebXRLayerManagerId::new(self.next_manager_id) };
self.next_manager_id += 1;
let manager_id = WebXRLayerManagerId::new(self.next_manager_id);
self.next_manager_id = self
.next_manager_id
.checked_add(1)
.expect("next_manager_id should not overflow");
self.managers.insert(manager_id, manager);
Ok(manager_id)
}

View file

@ -152,8 +152,7 @@ impl XRWebGLLayer {
let sub_images = frame.get_sub_images(self.layer_id()?)?;
let session = self.session();
// TODO: Cache this texture
let color_texture_id =
WebGLTextureId::maybe_new(sub_images.sub_image.as_ref()?.color_texture)?;
let color_texture_id = WebGLTextureId::new(sub_images.sub_image.as_ref()?.color_texture?);
let color_texture = WebGLTexture::new_webxr(context, color_texture_id, session);
let target = self.texture_target();
@ -187,7 +186,7 @@ impl XRWebGLLayer {
.ok()?;
if let Some(id) = sub_images.sub_image.as_ref()?.depth_stencil_texture {
// TODO: Cache this texture
let depth_stencil_texture_id = WebGLTextureId::maybe_new(id)?;
let depth_stencil_texture_id = WebGLTextureId::new(id);
let depth_stencil_texture =
WebGLTexture::new_webxr(context, depth_stencil_texture_id, session);
framebuffer

View file

@ -570,15 +570,9 @@ macro_rules! define_resource_id {
pub struct $name(nonzero_type!($type));
impl $name {
#[allow(unsafe_code)]
#[inline]
/// Create a new $name.
///
/// # Safety
///
/// Using an invalid OpenGL id may result in undefined behavior.
pub unsafe fn new(id: $type) -> Self {
$name(<nonzero_type!($type)>::new_unchecked(id))
pub fn new(id: nonzero_type!($type)) -> Self {
Self(id)
}
#[inline]
@ -599,10 +593,10 @@ macro_rules! define_resource_id {
D: ::serde::Deserializer<'de>,
{
let id = <$type>::deserialize(deserializer)?;
if id == 0 {
Err(::serde::de::Error::custom("expected a non-zero value"))
if let Some(id) = <nonzero_type!($type)>::new(id) {
Ok($name(id))
} else {
Ok(unsafe { $name::new(id) })
Err(::serde::de::Error::custom("expected a non-zero value"))
}
}
}

View file

@ -180,7 +180,8 @@ impl RenderingContext for SurfmanRenderingContext {
fn framebuffer_object(&self) -> u32 {
self.context_surface_info()
.unwrap_or(None)
.map(|info| info.framebuffer_object)
.and_then(|info| info.framebuffer_object)
.map(|fbo| fbo.0.get())
.unwrap_or(0)
}
#[allow(unsafe_code)]
@ -217,7 +218,10 @@ impl RenderingContext for SurfmanRenderingContext {
} = device.surface_info(&surface);
debug!("... getting texture for surface {:?}", front_buffer_id);
let surface_texture = device.create_surface_texture(context, surface).unwrap();
let gl_texture = device.surface_texture_object(&surface_texture);
let gl_texture = device
.surface_texture_object(&surface_texture)
.map(|tex| tex.0.get())
.unwrap_or(0);
Some((surface_texture, gl_texture, size))
}
@ -425,7 +429,10 @@ impl SurfmanRenderingContext {
pub fn surface_texture_object(&self, surface: &SurfaceTexture) -> u32 {
let device = &self.0.device.borrow();
device.surface_texture_object(surface)
device
.surface_texture_object(surface)
.map(|t| t.0.get())
.unwrap_or_default()
}
pub fn get_proc_address(&self, name: &str) -> *const c_void {

View file

@ -3,6 +3,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::fmt::Debug;
use std::num::NonZeroU32;
use std::sync::atomic::{AtomicUsize, Ordering};
use euclid::{Rect, Size2D};
@ -286,9 +287,8 @@ pub struct SubImages {
#[derive(Clone, Debug)]
#[cfg_attr(feature = "ipc", derive(Deserialize, Serialize))]
pub struct SubImage {
pub color_texture: u32,
// TODO: make this Option<NonZeroU32>
pub depth_stencil_texture: Option<u32>,
pub color_texture: Option<NonZeroU32>,
pub depth_stencil_texture: Option<NonZeroU32>,
pub texture_array_index: Option<u32>,
pub viewport: Rect<i32, Viewport>,
}

View file

@ -3,7 +3,6 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::collections::HashMap;
use std::num::NonZero;
use glow as gl;
use glow::{Context as Gl, HasContext};
@ -12,10 +11,6 @@ use webxr_api::{ContextId, GLContexts, LayerId};
use crate::SurfmanGL;
pub(crate) fn framebuffer(framebuffer: u32) -> Option<gl::NativeFramebuffer> {
NonZero::new(framebuffer).map(gl::NativeFramebuffer)
}
// A utility to clear a color texture and optional depth/stencil texture
pub(crate) struct GlClearer {
fbos: HashMap<
@ -52,10 +47,9 @@ impl GlClearer {
.entry((layer_id, color, depth_stencil))
.or_insert_with(|| {
// Save the current GL state
let mut bound_fbos = [0, 0];
unsafe {
gl.get_parameter_i32_slice(gl::DRAW_FRAMEBUFFER_BINDING, &mut bound_fbos[0..]);
gl.get_parameter_i32_slice(gl::READ_FRAMEBUFFER_BINDING, &mut bound_fbos[1..]);
let draw_fbo = gl.get_parameter_framebuffer(gl::DRAW_FRAMEBUFFER_BINDING);
let read_fbo = gl.get_parameter_framebuffer(gl::READ_FRAMEBUFFER_BINDING);
// Generate and set attachments of a new FBO
let fbo = gl.create_framebuffer().ok();
@ -83,8 +77,8 @@ impl GlClearer {
}
// Restore the GL state
gl.bind_framebuffer(gl::DRAW_FRAMEBUFFER, framebuffer(bound_fbos[0] as _));
gl.bind_framebuffer(gl::READ_FRAMEBUFFER, framebuffer(bound_fbos[1] as _));
gl.bind_framebuffer(gl::DRAW_FRAMEBUFFER, draw_fbo);
gl.bind_framebuffer(gl::READ_FRAMEBUFFER, read_fbo);
debug_assert_eq!(gl.get_error(), gl::NO_ERROR);
fbo
@ -110,7 +104,6 @@ impl GlClearer {
let fbo = self.fbo(gl, layer_id, color, color_target, depth_stencil);
unsafe {
// Save the current GL state
let mut bound_fbos = [0, 0];
let mut clear_color = [0., 0., 0., 0.];
let mut clear_depth = [0.];
let mut clear_stencil = [0];
@ -118,8 +111,8 @@ impl GlClearer {
let scissor_enabled = gl.is_enabled(gl::SCISSOR_TEST);
let rasterizer_enabled = gl.is_enabled(gl::RASTERIZER_DISCARD);
gl.get_parameter_i32_slice(gl::DRAW_FRAMEBUFFER_BINDING, &mut bound_fbos[0..]);
gl.get_parameter_i32_slice(gl::READ_FRAMEBUFFER_BINDING, &mut bound_fbos[1..]);
let draw_fbo = gl.get_parameter_framebuffer(gl::DRAW_FRAMEBUFFER_BINDING);
let read_fbo = gl.get_parameter_framebuffer(gl::READ_FRAMEBUFFER_BINDING);
gl.get_parameter_f32_slice(gl::COLOR_CLEAR_VALUE, &mut clear_color[..]);
gl.get_parameter_f32_slice(gl::DEPTH_CLEAR_VALUE, &mut clear_depth[..]);
gl.get_parameter_i32_slice(gl::STENCIL_CLEAR_VALUE, &mut clear_stencil[..]);
@ -140,8 +133,8 @@ impl GlClearer {
gl.clear(gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT | gl::STENCIL_BUFFER_BIT);
// Restore the GL state
gl.bind_framebuffer(gl::DRAW_FRAMEBUFFER, framebuffer(bound_fbos[0] as _));
gl.bind_framebuffer(gl::READ_FRAMEBUFFER, framebuffer(bound_fbos[1] as _));
gl.bind_framebuffer(gl::DRAW_FRAMEBUFFER, draw_fbo);
gl.bind_framebuffer(gl::READ_FRAMEBUFFER, read_fbo);
gl.clear_color(
clear_color[0],
clear_color[1],

View file

@ -3,7 +3,6 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use core::slice;
use std::num::NonZeroU32;
use std::rc::Rc;
use euclid::{
@ -25,7 +24,6 @@ use webxr_api::{
LEFT_EYE, RIGHT_EYE, VIEWER,
};
use crate::gl_utils::framebuffer;
use crate::{SurfmanGL, SurfmanLayerManager};
// How far off the ground are the viewer's eyes?
@ -222,11 +220,10 @@ impl DeviceAPI for GlWindowDevice {
.device
.context_surface_info(&self.context)
.unwrap()
.map(|info| info.framebuffer_object)
.unwrap_or(0);
.and_then(|info| info.framebuffer_object);
unsafe {
self.gl
.bind_framebuffer(gl::FRAMEBUFFER, framebuffer(framebuffer_object));
.bind_framebuffer(gl::FRAMEBUFFER, framebuffer_object);
debug_assert_eq!(
(
self.gl.get_error(),
@ -254,10 +251,9 @@ impl DeviceAPI for GlWindowDevice {
.device
.create_surface_texture(&mut self.context, surface)
.unwrap();
let raw_texture_id = self.device.surface_texture_object(&surface_texture);
let texture_id = NonZeroU32::new(raw_texture_id).map(gl::NativeTexture);
let texture_id = self.device.surface_texture_object(&surface_texture);
let texture_target = self.device.surface_gl_texture_target();
log::debug!("Presenting texture {}", raw_texture_id);
log::debug!("Presenting texture {:?}", texture_id);
if let Some(ref shader) = self.shader {
shader.draw_texture(
@ -392,9 +388,8 @@ impl GlWindowDevice {
let framebuffer_object = device
.context_surface_info(&context)
.unwrap()
.map(|info| info.framebuffer_object)
.unwrap_or(0);
gl.bind_framebuffer(gl::FRAMEBUFFER, framebuffer(framebuffer_object));
.and_then(|info| info.framebuffer_object);
gl.bind_framebuffer(gl::FRAMEBUFFER, framebuffer_object);
debug_assert_eq!(
(gl.get_error(), gl.check_framebuffer_status(gl::FRAMEBUFFER)),
(gl::NO_ERROR, gl::FRAMEBUFFER_COMPLETE)

View file

@ -3,7 +3,6 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::collections::HashMap;
use std::num::NonZeroU32;
use std::ops::Deref;
use std::sync::{Arc, Mutex};
use std::time::Duration;
@ -767,15 +766,13 @@ impl LayerManagerAPI<SurfmanGL> for OpenXrLayerManager {
})?;
let color_texture = device.surface_texture_object(color_surface_texture);
let color_target = device.surface_gl_texture_target();
let depth_stencil_texture = openxr_layer
.depth_stencil_texture
.map(|texture| texture.0.get());
let depth_stencil_texture = openxr_layer.depth_stencil_texture;
let texture_array_index = None;
let origin = Point2D::new(0, 0);
let texture_size = openxr_layer.size;
let sub_image = Some(SubImage {
color_texture,
depth_stencil_texture,
color_texture: color_texture.map(|t| t.0),
depth_stencil_texture: depth_stencil_texture.map(|t| t.0),
texture_array_index,
viewport: Rect::new(origin, texture_size),
});
@ -784,8 +781,8 @@ impl LayerManagerAPI<SurfmanGL> for OpenXrLayerManager {
.viewports
.iter()
.map(|&viewport| SubImage {
color_texture,
depth_stencil_texture,
color_texture: color_texture.map(|t| t.0),
depth_stencil_texture: depth_stencil_texture.map(|t| t.0),
texture_array_index,
viewport,
})
@ -795,7 +792,7 @@ impl LayerManagerAPI<SurfmanGL> for OpenXrLayerManager {
contexts,
context_id,
layer_id,
NonZeroU32::new(color_texture).map(glow::NativeTexture),
color_texture,
color_target,
openxr_layer.depth_stencil_texture,
);

View file

@ -5,7 +5,6 @@
//! An implementation of layer management using surfman
use std::collections::HashMap;
use std::num::NonZeroU32;
use euclid::{Point2D, Rect, Size2D};
use glow::{self as gl, Context as Gl, HasContext, PixelUnpackData};
@ -165,8 +164,8 @@ impl LayerManagerAPI<SurfmanGL> for SurfmanLayerManager {
let texture_array_index = None;
let origin = Point2D::new(0, 0);
let sub_image = Some(SubImage {
color_texture,
depth_stencil_texture: depth_stencil_texture.map(|nt| nt.0.get()),
color_texture: color_texture.map(|nt| nt.0),
depth_stencil_texture: depth_stencil_texture.map(|nt| nt.0),
texture_array_index,
viewport: Rect::new(origin, surface_size),
});
@ -175,8 +174,8 @@ impl LayerManagerAPI<SurfmanGL> for SurfmanLayerManager {
.viewports
.iter()
.map(|&viewport| SubImage {
color_texture,
depth_stencil_texture: depth_stencil_texture.map(|texture| texture.0.get()),
color_texture: color_texture.map(|nt| nt.0),
depth_stencil_texture: depth_stencil_texture.map(|texture| texture.0),
texture_array_index,
viewport,
})
@ -187,7 +186,7 @@ impl LayerManagerAPI<SurfmanGL> for SurfmanLayerManager {
contexts,
context_id,
layer_id,
NonZeroU32::new(color_texture).map(gl::NativeTexture),
color_texture,
color_target,
depth_stencil_texture,
);

View file

@ -100,7 +100,7 @@ impl Minibrowser {
.options_mut(|options| options.zoom_with_keyboard = false);
let widget_surface_fbo = match rendering_context.context_surface_info() {
Ok(Some(info)) => NonZeroU32::new(info.framebuffer_object).map(NativeFramebuffer),
Ok(Some(info)) => info.framebuffer_object,
Ok(None) => panic!("Failed to get widget surface info from surfman!"),
Err(error) => panic!("Failed to get widget surface info from surfman! {error:?}"),
};