Revert "Use surfman with glow bindings (#34328)" (#35402)

This reverts commit 503bb10c5b.

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
This commit is contained in:
Samson 2025-02-10 18:20:27 +01:00 committed by GitHub
parent 90130315a7
commit 0fed99590a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 97 additions and 69 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=a8079ee2708619926d07c5a2088b8c9af99abdb3#a8079ee2708619926d07c5a2088b8c9af99abdb3"
source = "git+https://github.com/servo/surfman?rev=300789ddbda45c89e9165c31118bf1c4c07f89f6#300789ddbda45c89e9165c31118bf1c4c07f89f6"
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 = "a8079ee2708619926d07c5a2088b8c9af99abdb3", features = ["chains"] }
surfman = { git = "https://github.com/servo/surfman", rev = "300789ddbda45c89e9165c31118bf1c4c07f89f6", 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, NativeTransformFeedback, NativeUniformLocation, NativeVertexArray, PixelUnpackData,
ShaderPrecisionFormat,
HasContext, NativeFramebuffer, NativeTransformFeedback, NativeUniformLocation,
NativeVertexArray, PixelUnpackData, ShaderPrecisionFormat,
};
use half::f16;
use log::{debug, error, trace, warn};
@ -49,6 +49,7 @@ 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> {
@ -605,7 +606,10 @@ impl WebGLThread {
.framebuffer_object;
unsafe {
gl.bind_framebuffer(gl::FRAMEBUFFER, framebuffer);
gl.bind_framebuffer(
gl::FRAMEBUFFER,
NonZeroU32::new(framebuffer).map(NativeFramebuffer),
);
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);
@ -835,7 +839,7 @@ impl WebGLThread {
.unwrap()
.unwrap();
debug!(
"... rebound framebuffer {:?}, new back buffer surface is {:?}",
"... rebound framebuffer {}, new back buffer surface is {:?}",
framebuffer_object, id
);
@ -2695,13 +2699,14 @@ impl WebGLImpl {
) {
let id = match request {
WebGLFramebufferBindingRequest::Explicit(id) => Some(id.glow()),
WebGLFramebufferBindingRequest::Default => {
WebGLFramebufferBindingRequest::Default => NonZeroU32::new(
device
.context_surface_info(ctx)
.unwrap()
.expect("No surface attached!")
.framebuffer_object
},
.framebuffer_object,
)
.map(NativeFramebuffer),
};
debug!("WebGLImpl::bind_framebuffer: {:?}", id);
@ -3184,8 +3189,9 @@ struct FramebufferRebindingInfo {
impl FramebufferRebindingInfo {
fn detect(device: &Device, context: &Context, gl: &Gl) -> FramebufferRebindingInfo {
unsafe {
let read_framebuffer = gl.get_parameter_framebuffer(gl::READ_FRAMEBUFFER_BINDING);
let draw_framebuffer = gl.get_parameter_framebuffer(gl::DRAW_FRAMEBUFFER_BINDING);
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 context_surface_framebuffer = device
.context_surface_info(context)
@ -3194,10 +3200,10 @@ impl FramebufferRebindingInfo {
.framebuffer_object;
let mut flags = FramebufferRebindingFlags::empty();
if context_surface_framebuffer == read_framebuffer {
if context_surface_framebuffer == read_framebuffer[0] as GLuint {
flags.insert(FramebufferRebindingFlags::REBIND_READ_FRAMEBUFFER);
}
if context_surface_framebuffer == draw_framebuffer {
if context_surface_framebuffer == draw_framebuffer[0] as GLuint {
flags.insert(FramebufferRebindingFlags::REBIND_DRAW_FRAMEBUFFER);
}
@ -3222,13 +3228,23 @@ impl FramebufferRebindingInfo {
.flags
.contains(FramebufferRebindingFlags::REBIND_READ_FRAMEBUFFER)
{
unsafe { gl.bind_framebuffer(gl::READ_FRAMEBUFFER, context_surface_framebuffer) };
unsafe {
gl.bind_framebuffer(
gl::READ_FRAMEBUFFER,
NonZeroU32::new(context_surface_framebuffer).map(NativeFramebuffer),
)
};
}
if self
.flags
.contains(FramebufferRebindingFlags::REBIND_DRAW_FRAMEBUFFER)
{
unsafe { gl.bind_framebuffer(gl::DRAW_FRAMEBUFFER, context_surface_framebuffer) };
unsafe {
gl.bind_framebuffer(
gl::DRAW_FRAMEBUFFER,
NonZeroU32::new(context_surface_framebuffer).map(NativeFramebuffer),
)
};
}
unsafe {

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 canvas_traits::webgl::{
webgl_channel, WebGLContextId, WebGLMsg, WebGLSender, WebXRCommand, WebXRLayerManagerId,
@ -26,7 +25,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: NonZeroU32,
next_manager_id: u32,
}
impl WebXRBridge {
@ -35,7 +34,7 @@ impl WebXRBridge {
factory_receiver, ..
} = init;
let managers = HashMap::new();
let next_manager_id = NonZeroU32::MIN;
let next_manager_id = 1;
WebXRBridge {
factory_receiver,
managers,
@ -56,11 +55,8 @@ impl WebXRBridge {
.recv()
.map_err(|_| WebXRError::CommunicationError)?;
let manager = factory.build(device, contexts)?;
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");
let manager_id = unsafe { WebXRLayerManagerId::new(self.next_manager_id) };
self.next_manager_id += 1;
self.managers.insert(manager_id, manager);
Ok(manager_id)
}

View file

@ -152,7 +152,8 @@ 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::new(sub_images.sub_image.as_ref()?.color_texture?);
let color_texture_id =
WebGLTextureId::maybe_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();
@ -186,7 +187,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::new(id);
let depth_stencil_texture_id = WebGLTextureId::maybe_new(id)?;
let depth_stencil_texture =
WebGLTexture::new_webxr(context, depth_stencil_texture_id, session);
framebuffer

View file

@ -570,9 +570,15 @@ macro_rules! define_resource_id {
pub struct $name(nonzero_type!($type));
impl $name {
#[allow(unsafe_code)]
#[inline]
pub fn new(id: nonzero_type!($type)) -> Self {
Self(id)
/// 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))
}
#[inline]
@ -593,10 +599,10 @@ macro_rules! define_resource_id {
D: ::serde::Deserializer<'de>,
{
let id = <$type>::deserialize(deserializer)?;
if let Some(id) = <nonzero_type!($type)>::new(id) {
Ok($name(id))
} else {
if id == 0 {
Err(::serde::de::Error::custom("expected a non-zero value"))
} else {
Ok(unsafe { $name::new(id) })
}
}
}

View file

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

View file

@ -3,7 +3,6 @@
* 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};
@ -287,8 +286,9 @@ pub struct SubImages {
#[derive(Clone, Debug)]
#[cfg_attr(feature = "ipc", derive(Deserialize, Serialize))]
pub struct SubImage {
pub color_texture: Option<NonZeroU32>,
pub depth_stencil_texture: Option<NonZeroU32>,
pub color_texture: u32,
// TODO: make this Option<NonZeroU32>
pub depth_stencil_texture: Option<u32>,
pub texture_array_index: Option<u32>,
pub viewport: Rect<i32, Viewport>,
}

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::NonZero;
use glow as gl;
use glow::{Context as Gl, HasContext};
@ -11,6 +12,10 @@ 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<
@ -47,9 +52,10 @@ impl GlClearer {
.entry((layer_id, color, depth_stencil))
.or_insert_with(|| {
// Save the current GL state
let mut bound_fbos = [0, 0];
unsafe {
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_i32_slice(gl::DRAW_FRAMEBUFFER_BINDING, &mut bound_fbos[0..]);
gl.get_parameter_i32_slice(gl::READ_FRAMEBUFFER_BINDING, &mut bound_fbos[1..]);
// Generate and set attachments of a new FBO
let fbo = gl.create_framebuffer().ok();
@ -77,8 +83,8 @@ impl GlClearer {
}
// Restore the GL state
gl.bind_framebuffer(gl::DRAW_FRAMEBUFFER, draw_fbo);
gl.bind_framebuffer(gl::READ_FRAMEBUFFER, read_fbo);
gl.bind_framebuffer(gl::DRAW_FRAMEBUFFER, framebuffer(bound_fbos[0] as _));
gl.bind_framebuffer(gl::READ_FRAMEBUFFER, framebuffer(bound_fbos[1] as _));
debug_assert_eq!(gl.get_error(), gl::NO_ERROR);
fbo
@ -104,6 +110,7 @@ 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];
@ -111,8 +118,8 @@ impl GlClearer {
let scissor_enabled = gl.is_enabled(gl::SCISSOR_TEST);
let rasterizer_enabled = gl.is_enabled(gl::RASTERIZER_DISCARD);
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_i32_slice(gl::DRAW_FRAMEBUFFER_BINDING, &mut bound_fbos[0..]);
gl.get_parameter_i32_slice(gl::READ_FRAMEBUFFER_BINDING, &mut bound_fbos[1..]);
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[..]);
@ -133,8 +140,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, draw_fbo);
gl.bind_framebuffer(gl::READ_FRAMEBUFFER, read_fbo);
gl.bind_framebuffer(gl::DRAW_FRAMEBUFFER, framebuffer(bound_fbos[0] as _));
gl.bind_framebuffer(gl::READ_FRAMEBUFFER, framebuffer(bound_fbos[1] as _));
gl.clear_color(
clear_color[0],
clear_color[1],

View file

@ -3,6 +3,7 @@
* 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::{
@ -24,6 +25,7 @@ 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?
@ -220,10 +222,11 @@ impl DeviceAPI for GlWindowDevice {
.device
.context_surface_info(&self.context)
.unwrap()
.and_then(|info| info.framebuffer_object);
.map(|info| info.framebuffer_object)
.unwrap_or(0);
unsafe {
self.gl
.bind_framebuffer(gl::FRAMEBUFFER, framebuffer_object);
.bind_framebuffer(gl::FRAMEBUFFER, framebuffer(framebuffer_object));
debug_assert_eq!(
(
self.gl.get_error(),
@ -251,9 +254,10 @@ impl DeviceAPI for GlWindowDevice {
.device
.create_surface_texture(&mut self.context, surface)
.unwrap();
let texture_id = self.device.surface_texture_object(&surface_texture);
let raw_texture_id = self.device.surface_texture_object(&surface_texture);
let texture_id = NonZeroU32::new(raw_texture_id).map(gl::NativeTexture);
let texture_target = self.device.surface_gl_texture_target();
log::debug!("Presenting texture {:?}", texture_id);
log::debug!("Presenting texture {}", raw_texture_id);
if let Some(ref shader) = self.shader {
shader.draw_texture(
@ -388,8 +392,9 @@ impl GlWindowDevice {
let framebuffer_object = device
.context_surface_info(&context)
.unwrap()
.and_then(|info| info.framebuffer_object);
gl.bind_framebuffer(gl::FRAMEBUFFER, framebuffer_object);
.map(|info| info.framebuffer_object)
.unwrap_or(0);
gl.bind_framebuffer(gl::FRAMEBUFFER, framebuffer(framebuffer_object));
debug_assert_eq!(
(gl.get_error(), gl.check_framebuffer_status(gl::FRAMEBUFFER)),
(gl::NO_ERROR, gl::FRAMEBUFFER_COMPLETE)

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 std::ops::Deref;
use std::sync::{Arc, Mutex};
use std::time::Duration;
@ -766,13 +767,15 @@ 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;
let depth_stencil_texture = openxr_layer
.depth_stencil_texture
.map(|texture| texture.0.get());
let texture_array_index = None;
let origin = Point2D::new(0, 0);
let texture_size = openxr_layer.size;
let sub_image = Some(SubImage {
color_texture: color_texture.map(|t| t.0),
depth_stencil_texture: depth_stencil_texture.map(|t| t.0),
color_texture,
depth_stencil_texture,
texture_array_index,
viewport: Rect::new(origin, texture_size),
});
@ -781,8 +784,8 @@ impl LayerManagerAPI<SurfmanGL> for OpenXrLayerManager {
.viewports
.iter()
.map(|&viewport| SubImage {
color_texture: color_texture.map(|t| t.0),
depth_stencil_texture: depth_stencil_texture.map(|t| t.0),
color_texture,
depth_stencil_texture,
texture_array_index,
viewport,
})
@ -792,7 +795,7 @@ impl LayerManagerAPI<SurfmanGL> for OpenXrLayerManager {
contexts,
context_id,
layer_id,
color_texture,
NonZeroU32::new(color_texture).map(glow::NativeTexture),
color_target,
openxr_layer.depth_stencil_texture,
);

View file

@ -5,6 +5,7 @@
//! 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};
@ -164,8 +165,8 @@ impl LayerManagerAPI<SurfmanGL> for SurfmanLayerManager {
let texture_array_index = None;
let origin = Point2D::new(0, 0);
let sub_image = Some(SubImage {
color_texture: color_texture.map(|nt| nt.0),
depth_stencil_texture: depth_stencil_texture.map(|nt| nt.0),
color_texture,
depth_stencil_texture: depth_stencil_texture.map(|nt| nt.0.get()),
texture_array_index,
viewport: Rect::new(origin, surface_size),
});
@ -174,8 +175,8 @@ impl LayerManagerAPI<SurfmanGL> for SurfmanLayerManager {
.viewports
.iter()
.map(|&viewport| SubImage {
color_texture: color_texture.map(|nt| nt.0),
depth_stencil_texture: depth_stencil_texture.map(|texture| texture.0),
color_texture,
depth_stencil_texture: depth_stencil_texture.map(|texture| texture.0.get()),
texture_array_index,
viewport,
})
@ -186,7 +187,7 @@ impl LayerManagerAPI<SurfmanGL> for SurfmanLayerManager {
contexts,
context_id,
layer_id,
color_texture,
NonZeroU32::new(color_texture).map(gl::NativeTexture),
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)) => info.framebuffer_object,
Ok(Some(info)) => NonZeroU32::new(info.framebuffer_object).map(NativeFramebuffer),
Ok(None) => panic!("Failed to get widget surface info from surfman!"),
Err(error) => panic!("Failed to get widget surface info from surfman! {error:?}"),
};