Skip to content
This repository has been archived by the owner on Feb 16, 2025. It is now read-only.

Create surfman context from scratch in glwindow #257

Merged
merged 2 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions webxr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ log = "0.4.6"
openxr = { version = "0.19", optional = true }
serde = { version = "1.0", optional = true }
glow = "0.16"
raw-window-handle = "0.6"
surfman = { git = "https://github.com/servo/surfman", rev = "300789ddbda45c89e9165c31118bf1c4c07f89f6", features = [
"chains",
"sm-raw-window-handle-06",
] }

[target.'cfg(target_os = "windows")'.dependencies]
Expand Down
34 changes: 20 additions & 14 deletions webxr/glwindow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use euclid::{
Angle, Point2D, Rect, RigidTransform3D, Rotation3D, Size2D, Transform3D, UnknownUnit, Vector3D,
};
use glow::{self as gl, Context as Gl, HasContext};
use std::ffi::c_void;
use raw_window_handle::DisplayHandle;
use std::num::NonZeroU32;
use std::rc::Rc;
use surfman::chains::{PreserveBuffer, SwapChain, SwapChainAPI, SwapChains, SwapChainsAPI};
use surfman::{
Adapter, Connection, Context as SurfmanContext, ContextAttributes, Device as SurfmanDevice,
GLApi, NativeWidget, SurfaceAccess, SurfaceType,
Adapter, Connection, Context as SurfmanContext, ContextAttributeFlags, ContextAttributes,
Device as SurfmanDevice, GLApi, GLVersion, NativeWidget, SurfaceAccess, SurfaceType,
};
use webxr_api::util::ClipPlanes;
use webxr_api::{
Expand Down Expand Up @@ -52,6 +52,7 @@ pub trait GlWindow {
fn get_mode(&self) -> GlWindowMode {
GlWindowMode::Blit
}
fn display_handle(&self) -> DisplayHandle;
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
Expand All @@ -72,21 +73,26 @@ pub struct GlWindowDiscovery {
connection: Connection,
adapter: Adapter,
context_attributes: ContextAttributes,
factory: Box<dyn Fn() -> Result<Box<dyn GlWindow>, ()>>,
window: Rc<dyn GlWindow>,
}

impl GlWindowDiscovery {
pub fn new(
connection: Connection,
adapter: Adapter,
context_attributes: ContextAttributes,
factory: Box<dyn Fn() -> Result<Box<dyn GlWindow>, ()>>,
) -> GlWindowDiscovery {
pub fn new(window: Rc<dyn GlWindow>) -> GlWindowDiscovery {
let connection = Connection::from_display_handle(window.display_handle()).unwrap();
let adapter = connection.create_adapter().unwrap();
let flags = ContextAttributeFlags::ALPHA
| ContextAttributeFlags::DEPTH
| ContextAttributeFlags::STENCIL;
let version = match connection.gl_api() {
GLApi::GLES => GLVersion { major: 3, minor: 0 },
GLApi::GL => GLVersion { major: 3, minor: 2 },
};
let context_attributes = ContextAttributes { flags, version };
GlWindowDiscovery {
connection,
adapter,
context_attributes,
factory,
window,
}
}
}
Expand All @@ -103,7 +109,7 @@ impl DiscoveryAPI<SurfmanGL> for GlWindowDiscovery {
let connection = self.connection.clone();
let adapter = self.adapter.clone();
let context_attributes = self.context_attributes.clone();
let window = (self.factory)().or(Err(Error::NoMatchingDevice))?;
let window = self.window.clone();
xr.run_on_main_thread(move |grand_manager| {
GlWindowDevice::new(
connection,
Expand All @@ -128,7 +134,7 @@ pub struct GlWindowDevice {
device: SurfmanDevice,
context: SurfmanContext,
gl: Rc<Gl>,
window: Box<dyn GlWindow>,
window: Rc<dyn GlWindow>,
grand_manager: LayerGrandManager<SurfmanGL>,
layer_manager: Option<LayerManager>,
target_swap_chain: Option<SwapChain<SurfmanDevice>>,
Expand Down Expand Up @@ -339,7 +345,7 @@ impl GlWindowDevice {
connection: Connection,
adapter: Adapter,
context_attributes: ContextAttributes,
window: Box<dyn GlWindow>,
window: Rc<dyn GlWindow>,
granted_features: Vec<String>,
grand_manager: LayerGrandManager<SurfmanGL>,
) -> Result<GlWindowDevice, Error> {
Expand Down
Loading