From fb92b54fe093f329bac0706e8c165a768d7113da Mon Sep 17 00:00:00 2001 From: Martin Robinson Date: Sun, 25 Oct 2026 10:05:43 +0100 Subject: [PATCH] Rename `Connection` and `Device` to their platform-specific variants This is the first step toward converting these two types to an `enum`. The types are re-exported at the root using the `Connection` and `Device` names and also into the `tests.rs` file which is included in an odd way for each backend. Signed-off-by: Martin Robinson --- src/angle/connection.rs | 32 ++++++++-------- src/angle/device.rs | 18 ++++----- src/angle/mod.rs | 6 ++- src/angle/surface.rs | 4 +- src/cgl/connection.rs | 26 ++++++------- src/cgl/device.rs | 4 +- src/cgl/mod.rs | 6 ++- src/hardware_buffer/connection.rs | 30 ++++++++------- src/hardware_buffer/device.rs | 10 ++--- src/hardware_buffer/mod.rs | 7 +++- .../surface/android_surface.rs | 4 +- src/hardware_buffer/surface/mod.rs | 5 ++- src/hardware_buffer/surface/ohos_surface.rs | 4 +- src/lib.rs | 17 ++++++++- src/macros.rs | 6 +-- src/mesa_surfaceless/connection.rs | 26 +++++++------ src/mesa_surfaceless/device.rs | 13 ++++--- src/mesa_surfaceless/mod.rs | 6 ++- src/tests.rs | 3 +- src/unix.rs | 12 +++--- src/wayland/connection.rs | 36 +++++++++--------- src/wayland/device.rs | 13 ++++--- src/wayland/mod.rs | 6 ++- src/wgl/connection.rs | 28 +++++++------- src/wgl/device.rs | 14 +++---- src/wgl/mod.rs | 6 ++- src/x11/connection.rs | 37 ++++++++++--------- src/x11/device.rs | 13 ++++--- src/x11/mod.rs | 6 ++- 29 files changed, 230 insertions(+), 168 deletions(-) diff --git a/src/angle/connection.rs b/src/angle/connection.rs index 443d3d53..2a7aa9e8 100644 --- a/src/angle/connection.rs +++ b/src/angle/connection.rs @@ -6,7 +6,7 @@ //! implicit in the Win32 API, and as such this type is a no-op. use super::adapter::AngleAdapter; -use super::device::{Device, NativeDevice, VendorPreference}; +use super::device::{AngleDevice, NativeDevice, VendorPreference}; use crate::egl::types::EGLDisplay; use crate::{Adapter, AdapterPreferences, Error, GLApi, PowerPreference, RenderingPreference}; @@ -22,7 +22,7 @@ const INTEL_PCI_ID: UINT = 0x8086; /// thread-safe. So we need to use the DXGI/Direct3D concept of a connection instead. These are /// implicit in the Win32 API, and as such this type is a no-op. #[derive(Clone)] -pub struct Connection; +pub struct AngleConnection; /// An empty placeholder for native connections. /// @@ -33,17 +33,17 @@ pub struct Connection; #[derive(Clone)] pub struct NativeConnection; -impl Connection { +impl AngleConnection { /// Connects to the default display. #[inline] - pub fn new() -> Result { - Ok(Connection) + pub fn new() -> Result { + Ok(AngleConnection) } /// An alias for `Connection::new()`, present for consistency with other backends. #[inline] - pub unsafe fn from_native_connection(_: NativeConnection) -> Result { - Connection::new() + pub unsafe fn from_native_connection(_: NativeConnection) -> Result { + AngleConnection::new() } /// Returns the underlying native connection. @@ -76,8 +76,8 @@ impl Connection { /// /// Device handles are local to a single thread. #[inline] - pub fn create_device(&self, adapter: &Adapter) -> Result { - Device::new(adapter.angle()?) + pub fn create_device(&self, adapter: &Adapter) -> Result { + AngleDevice::new(adapter.angle()?) } /// Wraps a `NativeDevice` in a `Device` and returns it. @@ -85,8 +85,8 @@ impl Connection { pub unsafe fn create_device_from_native_device( &self, native_device: NativeDevice, - ) -> Result { - Device::from_native_device(native_device) + ) -> Result { + AngleDevice::from_native_device(native_device) } /// Wraps an ANGLE `EGLDisplay`, along with the associated Direct3D device, in a `Device` and @@ -99,13 +99,15 @@ impl Connection { pub unsafe fn create_device_from_egl_display( &self, egl_display: EGLDisplay, - ) -> Result { - Device::from_egl_display(egl_display) + ) -> Result { + AngleDevice::from_egl_display(egl_display) } /// Opens the display connection corresponding to the given `DisplayHandle`. - pub fn from_display_handle(_: raw_window_handle::DisplayHandle) -> Result { - Connection::new() + pub fn from_display_handle( + _: raw_window_handle::DisplayHandle, + ) -> Result { + AngleConnection::new() } } diff --git a/src/angle/device.rs b/src/angle/device.rs index e2da2672..659eaf9f 100644 --- a/src/angle/device.rs +++ b/src/angle/device.rs @@ -49,7 +49,7 @@ const SURFACE_GL_TEXTURE_TARGET: u32 = gl::TEXTURE_2D; /// A thread-local handle to a device. /// /// Devices contain most of the relevant surface management methods. -pub struct Device { +pub struct AngleDevice { pub(crate) egl_display: EGLDisplay, pub(crate) d3d11_device: ComPtr, pub(crate) d3d_driver_type: D3D_DRIVER_TYPE, @@ -72,9 +72,9 @@ pub struct NativeDevice { pub d3d_driver_type: D3D_DRIVER_TYPE, } -impl Device { +impl AngleDevice { #[allow(non_snake_case)] - pub(crate) fn new(adapter: &AngleAdapter) -> Result { + pub(crate) fn new(adapter: &AngleAdapter) -> Result { let d3d_driver_type = adapter.d3d_driver_type; unsafe { let mut d3d11_device = ptr::null_mut(); @@ -126,7 +126,7 @@ impl Device { let result = egl.Initialize(egl_display, &mut major_version, &mut minor_version); assert_ne!(result, egl::FALSE); - Ok(Device { + Ok(AngleDevice { egl_display, d3d11_device, d3d_driver_type, @@ -136,10 +136,10 @@ impl Device { } } - pub(crate) fn from_native_device(native_device: NativeDevice) -> Result { + pub(crate) fn from_native_device(native_device: NativeDevice) -> Result { unsafe { (*native_device.d3d11_device).AddRef(); - Ok(Device { + Ok(AngleDevice { egl_display: native_device.egl_display, d3d11_device: ComPtr::from_raw(native_device.d3d11_device), d3d_driver_type: native_device.d3d_driver_type, @@ -149,7 +149,7 @@ impl Device { } #[allow(non_snake_case)] - pub(crate) fn from_egl_display(egl_display: EGLDisplay) -> Result { + pub(crate) fn from_egl_display(egl_display: EGLDisplay) -> Result { let eglQueryDisplayAttribEXT = EGL_EXTENSION_FUNCTIONS .QueryDisplayAttribEXT .expect("Where's the `EGL_EXT_device_query` extension?"); @@ -178,7 +178,7 @@ impl Device { unsafe { (*d3d11_device).AddRef(); - Ok(Device { + Ok(AngleDevice { egl_display: egl_display, d3d11_device: ComPtr::from_raw(d3d11_device), d3d_driver_type: D3D_DRIVER_TYPE_UNKNOWN, @@ -1102,7 +1102,7 @@ impl Device { } } -impl Drop for Device { +impl Drop for AngleDevice { fn drop(&mut self) { unsafe { if self.display_is_owned { diff --git a/src/angle/mod.rs b/src/angle/mod.rs index 1deb6b77..a1812608 100644 --- a/src/angle/mod.rs +++ b/src/angle/mod.rs @@ -6,8 +6,12 @@ pub mod context; pub mod device; pub mod surface; -crate::implement_interfaces!(); +crate::implement_interfaces!(AngleConnection, AngleDevice); +#[cfg(test)] +use self::connection::AngleConnection as Connection; +#[cfg(test)] +use self::device::AngleDevice as Device; #[cfg(test)] #[path = "../tests.rs"] mod tests; diff --git a/src/angle/surface.rs b/src/angle/surface.rs index e2723363..5b9c861a 100644 --- a/src/angle/surface.rs +++ b/src/angle/surface.rs @@ -1,6 +1,6 @@ //! Surface management for Direct3D 11 on Windows using the ANGLE library as a frontend. -use super::device::Device; +use super::device::AngleDevice; use crate::base::egl::device::EGL_FUNCTIONS; use crate::context::ContextID; use crate::egl::types::EGLSurface; @@ -98,7 +98,7 @@ impl AngleSurface { } } - pub(crate) fn present(&self, device: &Device) -> Result<(), Error> { + pub(crate) fn present(&self, device: &AngleDevice) -> Result<(), Error> { match self.win32_objects { Win32Objects::Window { .. } => {} _ => return Err(Error::NoWidgetAttached), diff --git a/src/cgl/connection.rs b/src/cgl/connection.rs index 9a6a3fcc..31fcde50 100644 --- a/src/cgl/connection.rs +++ b/src/cgl/connection.rs @@ -3,7 +3,7 @@ //! Connection types are zero-sized on macOS, because the system APIs automatically manage the //! global window server connection. -use super::device::Device; +use super::device::CglDevice; use crate::adapter::{Adapter, AdapterPreferences}; use crate::base::io_surface::connection::Connection as SystemConnection; use crate::base::io_surface::device::NativeDevice; @@ -14,21 +14,21 @@ pub use crate::base::io_surface::connection::NativeConnection; /// A connection to the display server. #[derive(Clone)] -pub struct Connection(pub SystemConnection); +pub struct CglConnection(pub SystemConnection); -impl Connection { +impl CglConnection { /// Connects to the default display. #[inline] - pub fn new() -> Result { - SystemConnection::new().map(Connection) + pub fn new() -> Result { + SystemConnection::new().map(CglConnection) } /// An alias for `Connection::new()`, present for consistency with other backends. #[inline] pub unsafe fn from_native_connection( native_connection: NativeConnection, - ) -> Result { - SystemConnection::from_native_connection(native_connection).map(Connection) + ) -> Result { + SystemConnection::from_native_connection(native_connection).map(CglConnection) } /// Returns the underlying native connection. @@ -53,8 +53,8 @@ impl Connection { /// /// Device handles are local to a single thread. #[inline] - pub fn create_device(&self, adapter: &Adapter) -> Result { - self.0.create_device(adapter.apple()?).map(Device) + pub fn create_device(&self, adapter: &Adapter) -> Result { + self.0.create_device(adapter.apple()?).map(CglDevice) } /// An alias for `connection.create_device()` with the default adapter. @@ -62,16 +62,16 @@ impl Connection { pub unsafe fn create_device_from_native_device( &self, native_device: NativeDevice, - ) -> Result { + ) -> Result { self.0 .create_device_from_native_device(native_device) - .map(Device) + .map(CglDevice) } /// Opens the display connection corresponding to the given `DisplayHandle`. pub fn from_display_handle( handle: raw_window_handle::DisplayHandle, - ) -> Result { - SystemConnection::from_display_handle(handle).map(Connection) + ) -> Result { + SystemConnection::from_display_handle(handle).map(CglConnection) } } diff --git a/src/cgl/device.rs b/src/cgl/device.rs index 636885ef..1babab54 100644 --- a/src/cgl/device.rs +++ b/src/cgl/device.rs @@ -63,9 +63,9 @@ thread_local! { /// /// Devices contain most of the relevant surface management methods. #[derive(Clone)] -pub struct Device(pub(crate) SystemDevice); +pub struct CglDevice(pub(crate) SystemDevice); -impl Device { +impl CglDevice { /// Returns the native device corresponding to this device. #[inline] pub fn native_device(&self) -> NativeDevice { diff --git a/src/cgl/mod.rs b/src/cgl/mod.rs index d8ac75eb..fcb42dcf 100644 --- a/src/cgl/mod.rs +++ b/src/cgl/mod.rs @@ -8,8 +8,12 @@ pub mod surface; mod error; mod ffi; -crate::implement_interfaces!(); +crate::implement_interfaces!(CglConnection, CglDevice); +#[cfg(test)] +use self::connection::CglConnection as Connection; +#[cfg(test)] +use self::device::CglDevice as Device; #[cfg(test)] #[path = "../tests.rs"] mod tests; diff --git a/src/hardware_buffer/connection.rs b/src/hardware_buffer/connection.rs index 0d9d7313..f1ef9aa8 100644 --- a/src/hardware_buffer/connection.rs +++ b/src/hardware_buffer/connection.rs @@ -3,28 +3,30 @@ //! FIXME(pcwalton): Should this instead wrap `EGLDisplay`? Is that thread-safe on Android? use super::adapter::HardwareBufferAdapter; -use super::device::{Device, NativeDevice}; +use super::device::{HardwareBufferDevice, NativeDevice}; use crate::{Adapter, AdapterPreferences, Error, GLApi}; /// A connection to the display server. #[derive(Clone)] -pub struct Connection; +pub struct HardwareBufferConnection; /// An empty placeholder for native connections. #[derive(Clone)] pub struct NativeConnection; -impl Connection { +impl HardwareBufferConnection { /// Connects to the default display. #[inline] - pub fn new() -> Result { - Ok(Connection) + pub fn new() -> Result { + Ok(HardwareBufferConnection) } /// An alias for `Connection::new()`, present for consistency with other backends. #[inline] - pub unsafe fn from_native_connection(_: NativeConnection) -> Result { - Connection::new() + pub unsafe fn from_native_connection( + _: NativeConnection, + ) -> Result { + HardwareBufferConnection::new() } /// Returns the underlying native connection. @@ -49,8 +51,8 @@ impl Connection { /// /// Device handles are local to a single thread. #[inline] - pub fn create_device(&self, _: &Adapter) -> Result { - Device::new() + pub fn create_device(&self, _: &Adapter) -> Result { + HardwareBufferDevice::new() } /// Wraps an Android `EGLDisplay` in a device and returns it. @@ -62,16 +64,18 @@ impl Connection { pub unsafe fn create_device_from_native_device( &self, native_device: NativeDevice, - ) -> Result { - Ok(Device { + ) -> Result { + Ok(HardwareBufferDevice { egl_display: native_device.0, display_is_owned: false, }) } /// Opens the display connection corresponding to the given `DisplayHandle`. - pub fn from_display_handle(_: raw_window_handle::DisplayHandle) -> Result { - Ok(Connection) + pub fn from_display_handle( + _: raw_window_handle::DisplayHandle, + ) -> Result { + Ok(HardwareBufferConnection) } } diff --git a/src/hardware_buffer/device.rs b/src/hardware_buffer/device.rs index 5555008a..153f8d90 100644 --- a/src/hardware_buffer/device.rs +++ b/src/hardware_buffer/device.rs @@ -21,7 +21,7 @@ use std::os::raw::c_void; /// A thread-local handle to a device. /// /// Devices contain most of the relevant surface management methods. -pub struct Device { +pub struct HardwareBufferDevice { pub(crate) egl_display: EGLDisplay, pub(crate) display_is_owned: bool, } @@ -30,7 +30,7 @@ pub struct Device { #[derive(Clone, Copy)] pub struct NativeDevice(pub EGLDisplay); -impl Drop for Device { +impl Drop for HardwareBufferDevice { fn drop(&mut self) { EGL_FUNCTIONS.with(|egl| unsafe { if !self.display_is_owned { @@ -52,9 +52,9 @@ impl NativeDevice { } } -impl Device { +impl HardwareBufferDevice { #[inline] - pub(crate) fn new() -> Result { + pub(crate) fn new() -> Result { EGL_FUNCTIONS.with(|egl| { unsafe { let egl_display = egl.GetDisplay(egl::DEFAULT_DISPLAY); @@ -65,7 +65,7 @@ impl Device { let result = egl.Initialize(egl_display, &mut major_version, &mut minor_version); assert_ne!(result, egl::FALSE); - Ok(Device { + Ok(HardwareBufferDevice { egl_display, display_is_owned: true, }) diff --git a/src/hardware_buffer/mod.rs b/src/hardware_buffer/mod.rs index bba4fc6c..131ee6f5 100644 --- a/src/hardware_buffer/mod.rs +++ b/src/hardware_buffer/mod.rs @@ -12,7 +12,12 @@ mod android_ffi; #[cfg(ohos_platform)] mod ohos_ffi; -crate::implement_interfaces!(); +crate::implement_interfaces!(HardwareBufferConnection, HardwareBufferDevice); + +#[cfg(any(test, feature = "sm-test"))] +use self::connection::HardwareBufferConnection as Connection; +#[cfg(any(test, feature = "sm-test"))] +use self::device::HardwareBufferDevice as Device; #[cfg(feature = "sm-test")] #[path = "../tests.rs"] diff --git a/src/hardware_buffer/surface/android_surface.rs b/src/hardware_buffer/surface/android_surface.rs index 7a54f532..85acdccd 100644 --- a/src/hardware_buffer/surface/android_surface.rs +++ b/src/hardware_buffer/surface/android_surface.rs @@ -11,7 +11,7 @@ use super::super::android_ffi::{ use super::super::android_ffi::{ AHARDWAREBUFFER_USAGE_CPU_WRITE_NEVER, AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER, }; -use super::super::device::Device; +use super::super::device::HardwareBufferDevice; use crate::base::egl::device::EGL_FUNCTIONS; use crate::base::egl::ffi::EGLImageKHR; use crate::base::egl::ffi::EGL_EXTENSION_FUNCTIONS; @@ -50,7 +50,7 @@ pub(crate) enum SurfaceObjects { }, } -impl Device { +impl HardwareBufferDevice { /// Creates either a generic or a widget surface, depending on the supplied surface type. /// /// Only the given context may ever render to the surface, but generic surfaces can be wrapped diff --git a/src/hardware_buffer/surface/mod.rs b/src/hardware_buffer/surface/mod.rs index 9b6fc2e2..e4b6f6bd 100644 --- a/src/hardware_buffer/surface/mod.rs +++ b/src/hardware_buffer/surface/mod.rs @@ -3,7 +3,8 @@ use crate::base::egl::ffi::EGLImageKHR; use crate::context::ContextID; use crate::hardware_buffer::context::HardwareBufferContext; -use crate::{Context, Device, Error, Surface, SurfaceID, SurfaceInfo}; +use crate::hardware_buffer::device::HardwareBufferDevice; +use crate::{Context, Error, Surface, SurfaceID, SurfaceInfo}; use crate::base::egl::device::EGL_FUNCTIONS; use euclid::default::Size2D; @@ -89,7 +90,7 @@ impl Debug for HardwareBufferSurfaceTexture { } } -impl Device { +impl HardwareBufferDevice { /// Displays the contents of a widget surface on screen. /// /// Widget surfaces are internally double-buffered, so changes to them don't show up in their diff --git a/src/hardware_buffer/surface/ohos_surface.rs b/src/hardware_buffer/surface/ohos_surface.rs index 15f9de80..1e8872ec 100644 --- a/src/hardware_buffer/surface/ohos_surface.rs +++ b/src/hardware_buffer/surface/ohos_surface.rs @@ -15,6 +15,7 @@ use crate::base::egl::ffi::EGL_IMAGE_PRESERVED_KHR; use crate::base::egl::ffi::EGL_NO_IMAGE_KHR; use crate::egl::types::{EGLSurface, EGLint}; use crate::hardware_buffer::context::HardwareBufferContext; +use crate::hardware_buffer::device::HardwareBufferDevice; use crate::hardware_buffer::surface::HardwareBufferSurface; use crate::hardware_buffer::surface::HardwareBufferSurfaceTexture; use crate::renderbuffers::Renderbuffers; @@ -22,7 +23,6 @@ use crate::{ egl, gl, gl_utils, Context, Error, Surface, SurfaceAccess, SurfaceTexture, SurfaceType, }; -use super::super::device::Device; use super::super::ohos_ffi::{ eglGetNativeClientBufferANDROID, NativeWindowOperation, OHNativeWindow, OH_NativeBuffer, OH_NativeBuffer_Alloc, OH_NativeBuffer_Config, OH_NativeBuffer_Format, @@ -46,7 +46,7 @@ pub(crate) enum SurfaceObjects { }, } -impl Device { +impl HardwareBufferDevice { /// Creates either a generic or a widget surface, depending on the supplied surface type. /// /// Only the given context may ever render to the surface, but generic surfaces can be wrapped diff --git a/src/lib.rs b/src/lib.rs index bafcb2c2..08efd799 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -68,11 +68,24 @@ pub use crate::info::{GLApi, GLVersion}; pub use crate::surface::{Surface, SurfaceAccess, SurfaceID, SurfaceInfo, SurfaceType}; pub use crate::surface_texture::SurfaceTexture; pub use adapter::{Adapter, AdapterPreferences, PowerPreference, RenderingPreference}; -pub use default::connection::Connection; -pub use default::device::Device; pub(crate) use glow::{self as gl, Context as Gl}; pub(crate) use macros::implement_interfaces; +#[cfg(all(windows_platform, angle_default))] +pub use angle::{connection::AngleConnection as Connection, device::AngleDevice as Device}; +#[cfg(macos_platform)] +pub use cgl::{connection::CglConnection as Connection, device::CglDevice as Device}; +#[cfg(any(android_platform, ohos_platform))] +pub use hardware_buffer::{ + connection::HardwareBufferConnection as Connection, device::HardwareBufferDevice as Device, +}; +#[cfg(all(x11_platform, not(wayland_default)))] +pub use unix::{connection::Connection, device::Device}; +#[cfg(wayland_default)] +pub use wayland::{connection::WaylandConnection as Connection, device::WaylandDevice as Device}; +#[cfg(all(windows_platform, not(angle_default)))] +pub use wgl::{connection::WglConnection as Connection, device::WglDevice as Device}; + // TODO(pcwalton): Fill this in with other OS's. #[cfg(target_os = "macos")] pub use base::io_surface::adapter::AppleAdapter; diff --git a/src/macros.rs b/src/macros.rs index 0d267bb2..3a39508c 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -32,10 +32,10 @@ macro_rules! declare_surfman { /// Internal macro used for generating implementations of the `Connection` and `Device` traits. macro_rules! implement_interfaces { - () => { + ($connection:ident, $device:ident) => { mod implementation { - use super::connection::Connection; - use super::device::Device; + use super::connection::$connection as Connection; + use super::device::$device as Device; use euclid::default::Size2D; use glow::Texture; use std::os::raw::c_void; diff --git a/src/mesa_surfaceless/connection.rs b/src/mesa_surfaceless/connection.rs index e4dced39..387571b1 100644 --- a/src/mesa_surfaceless/connection.rs +++ b/src/mesa_surfaceless/connection.rs @@ -1,6 +1,6 @@ //! Represents a connection to a display server. -use super::device::{Device, NativeDevice}; +use super::device::{NativeDevice, SurfacelessMesaDevice}; use crate::base::egl::device::EGL_FUNCTIONS; use crate::base::egl::ffi::EGL_PLATFORM_SURFACELESS_MESA; use crate::egl::types::{EGLAttrib, EGLDisplay}; @@ -14,7 +14,7 @@ use std::sync::Arc; /// A no-op connection. #[derive(Clone)] -pub struct Connection { +pub struct SurfacelessMesaConnection { pub(crate) native_connection: Arc, } @@ -30,10 +30,10 @@ pub struct NativeConnectionWrapper { unsafe impl Send for NativeConnectionWrapper {} unsafe impl Sync for NativeConnectionWrapper {} -impl Connection { +impl SurfacelessMesaConnection { /// Opens a surfaceless Mesa display. #[inline] - pub fn new() -> Result { + pub fn new() -> Result { unsafe { EGL_FUNCTIONS.with(|egl| { let egl_display_attributes = [egl::NONE as EGLAttrib]; @@ -56,7 +56,7 @@ impl Connection { let native_connection = NativeConnection(Arc::new(NativeConnectionWrapper { egl_display })); - Connection::from_native_connection(native_connection) + SurfacelessMesaConnection::from_native_connection(native_connection) }) } } @@ -65,8 +65,8 @@ impl Connection { #[inline] pub unsafe fn from_native_connection( native_connection: NativeConnection, - ) -> Result { - Ok(Connection { + ) -> Result { + Ok(SurfacelessMesaConnection { native_connection: native_connection.0, }) } @@ -93,8 +93,8 @@ impl Connection { /// /// Device handles are local to a single thread. #[inline] - pub fn create_device(&self, adapter: &Adapter) -> Result { - Device::new(self, adapter) + pub fn create_device(&self, adapter: &Adapter) -> Result { + SurfacelessMesaDevice::new(self, adapter) } /// An alias for `connection.create_device()` with the default adapter. @@ -102,12 +102,14 @@ impl Connection { pub unsafe fn create_device_from_native_device( &self, _: NativeDevice, - ) -> Result { - Device::new(self, &self.create_adapter(Default::default())?) + ) -> Result { + SurfacelessMesaDevice::new(self, &self.create_adapter(Default::default())?) } /// Opens the display connection corresponding to the given `DisplayHandle`. - pub fn from_display_handle(_: raw_window_handle::DisplayHandle) -> Result { + pub fn from_display_handle( + _: raw_window_handle::DisplayHandle, + ) -> Result { Err(Error::IncompatibleRawDisplayHandle) } } diff --git a/src/mesa_surfaceless/device.rs b/src/mesa_surfaceless/device.rs index 941d5e6c..59069436 100644 --- a/src/mesa_surfaceless/device.rs +++ b/src/mesa_surfaceless/device.rs @@ -1,6 +1,6 @@ //! A wrapper around surfaceless Mesa `EGLDisplay`s. -use super::connection::{Connection, NativeConnectionWrapper}; +use super::connection::{NativeConnectionWrapper, SurfacelessMesaConnection}; use super::surface::SurfaceDataGuard; use crate::base::egl::context::{self, CurrentContextGuard, EGLBackedContext}; use crate::base::egl::surface::EGLBackedSurface; @@ -24,7 +24,7 @@ const SURFACE_GL_TEXTURE_TARGET: u32 = gl::TEXTURE_2D; /// A thread-local handle to a device. /// /// Devices contain most of the relevant surface management methods. -pub struct Device { +pub struct SurfacelessMesaDevice { pub(crate) native_connection: Arc, pub(crate) adapter: FreeUnixAdapter, } @@ -38,10 +38,13 @@ pub struct NativeDevice { pub adapter: FreeUnixAdapter, } -impl Device { +impl SurfacelessMesaDevice { #[inline] - pub(crate) fn new(connection: &Connection, adapter: &Adapter) -> Result { - Ok(Device { + pub(crate) fn new( + connection: &SurfacelessMesaConnection, + adapter: &Adapter, + ) -> Result { + Ok(SurfacelessMesaDevice { native_connection: connection.native_connection.clone(), adapter: adapter.free_unix()?.clone(), }) diff --git a/src/mesa_surfaceless/mod.rs b/src/mesa_surfaceless/mod.rs index 3327f5f7..99e72e78 100644 --- a/src/mesa_surfaceless/mod.rs +++ b/src/mesa_surfaceless/mod.rs @@ -6,8 +6,12 @@ pub mod context; pub mod device; pub mod surface; -crate::implement_interfaces!(); +crate::implement_interfaces!(SurfacelessMesaConnection, SurfacelessMesaDevice); +#[cfg(test)] +use self::connection::SurfacelessMesaConnection as Connection; +#[cfg(test)] +use self::device::SurfacelessMesaDevice as Device; #[cfg(test)] #[path = "../tests.rs"] mod tests; diff --git a/src/tests.rs b/src/tests.rs index 0d872ba2..ed39417a 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -6,9 +6,8 @@ #![allow(missing_docs)] -use super::connection::Connection; use super::context::NativeContext; -use super::device::Device; +use super::{Connection, Device}; use crate::{ gl, Adapter, AdapterPreferences, Context, ContextAttributeFlags, ContextAttributes, ContextDescriptor, Error, GLApi, GLVersion, Gl, PowerPreference, RenderingPreference, Surface, diff --git a/src/unix.rs b/src/unix.rs index e21692f8..affd9657 100644 --- a/src/unix.rs +++ b/src/unix.rs @@ -4,11 +4,11 @@ /// Wayland or X11 display server connections. pub mod connection { - use crate::mesa_surfaceless::device::Device as SWDevice; + use crate::mesa_surfaceless::device::SurfacelessMesaDevice as SWDevice; use crate::multi::connection::Connection as MultiConnection; use crate::multi::device::Device as MultiDevice; - use crate::wayland::device::Device as WaylandDevice; - use crate::x11::device::Device as X11Device; + use crate::wayland::device::WaylandDevice; + use crate::x11::device::X11Device; type HWDevice = MultiDevice; /// Either a Wayland or an X11 display server connection. @@ -17,9 +17,9 @@ pub mod connection { /// Thread-local handles to devices. pub mod device { - use crate::mesa_surfaceless::device::Device as SWDevice; - use crate::wayland::device::Device as WaylandDevice; - use crate::x11::device::Device as X11Device; + use crate::mesa_surfaceless::device::SurfacelessMesaDevice as SWDevice; + use crate::wayland::device::WaylandDevice; + use crate::x11::device::X11Device; use crate::multi::device::Device as MultiDevice; type HWDevice = MultiDevice; diff --git a/src/wayland/connection.rs b/src/wayland/connection.rs index 0e656a42..80bfc188 100644 --- a/src/wayland/connection.rs +++ b/src/wayland/connection.rs @@ -1,6 +1,6 @@ //! A wrapper for Wayland connections (displays). -use super::device::{Device, NativeDevice}; +use super::device::{NativeDevice, WaylandDevice}; use crate::base::egl::device::EGL_FUNCTIONS; use crate::base::egl::ffi::EGL_PLATFORM_WAYLAND_KHR; use crate::egl::types::{EGLAttrib, EGLDisplay}; @@ -16,7 +16,7 @@ use wayland_sys::client::{wayland_client_handle, wl_display}; /// A connection to the Wayland server. #[derive(Clone)] -pub struct Connection { +pub struct WaylandConnection { pub(crate) native_connection: Arc, } @@ -28,15 +28,15 @@ pub(crate) struct NativeConnectionWrapper { /// An EGL display wrapping a Wayland display. pub struct NativeConnection(pub EGLDisplay); -unsafe impl Send for Connection {} +unsafe impl Send for WaylandConnection {} -impl Connection { +impl WaylandConnection { /// Connects to the default Wayland server. #[inline] - pub fn new() -> Result { + pub fn new() -> Result { unsafe { let wayland_display = (wayland_client_handle().wl_display_connect)(ptr::null()); - Connection::from_wayland_display(wayland_display, true) + WaylandConnection::from_wayland_display(wayland_display, true) } } @@ -47,8 +47,8 @@ impl Connection { /// connection is. pub unsafe fn from_native_connection( native_connection: NativeConnection, - ) -> Result { - Connection::from_egl_display(native_connection.0, None) + ) -> Result { + WaylandConnection::from_egl_display(native_connection.0, None) } /// Returns the underlying native connection. @@ -77,8 +77,8 @@ impl Connection { /// /// Device handles are local to a single thread. #[inline] - pub fn create_device(&self, adapter: &Adapter) -> Result { - Device::new(self, adapter.free_unix()?) + pub fn create_device(&self, adapter: &Adapter) -> Result { + WaylandDevice::new(self, adapter.free_unix()?) } /// Opens the hardware device corresponding to the adapter wrapped in the given native @@ -89,14 +89,14 @@ impl Connection { pub unsafe fn create_device_from_native_device( &self, native_device: NativeDevice, - ) -> Result { - Device::new(self, &native_device.adapter) + ) -> Result { + WaylandDevice::new(self, &native_device.adapter) } unsafe fn from_wayland_display( wayland_display: *mut wl_display, is_owned: bool, - ) -> Result { + ) -> Result { if wayland_display.is_null() { return Err(Error::ConnectionFailed); } @@ -121,15 +121,15 @@ impl Connection { } else { None }; - Connection::from_egl_display(egl_display, owned_display) + WaylandConnection::from_egl_display(egl_display, owned_display) }) } fn from_egl_display( egl_display: EGLDisplay, wayland_display: Option<*mut wl_display>, - ) -> Result { - Ok(Connection { + ) -> Result { + Ok(WaylandConnection { native_connection: Arc::new(NativeConnectionWrapper { egl_display, wayland_display, @@ -140,7 +140,7 @@ impl Connection { /// Opens the display connection corresponding to the given `DisplayHandle`. pub fn from_display_handle( handle: raw_window_handle::DisplayHandle, - ) -> Result { + ) -> Result { use raw_window_handle::RawDisplayHandle::Wayland; use raw_window_handle::WaylandDisplayHandle; unsafe { @@ -151,7 +151,7 @@ impl Connection { _ => return Err(Error::IncompatibleRawDisplayHandle), }; - Connection::from_wayland_display(wayland_display, false) + WaylandConnection::from_wayland_display(wayland_display, false) } } } diff --git a/src/wayland/device.rs b/src/wayland/device.rs index 4138d604..d495e60a 100644 --- a/src/wayland/device.rs +++ b/src/wayland/device.rs @@ -1,6 +1,6 @@ //! A wrapper around Wayland `EGLDisplay`s. -use super::connection::{Connection, NativeConnectionWrapper}; +use super::connection::{NativeConnectionWrapper, WaylandConnection}; use super::context::{NativeContext, WaylandContext}; use super::surface::SurfaceDataGuard; use crate::base::egl::context::{self, CurrentContextGuard, EGLBackedContext}; @@ -28,7 +28,7 @@ const SURFACE_GL_TEXTURE_TARGET: u32 = gl::TEXTURE_2D; /// A thread-local handle to a device. /// /// Devices contain most of the relevant surface management methods. -pub struct Device { +pub struct WaylandDevice { pub(crate) native_connection: Arc, pub(crate) adapter: FreeUnixAdapter, } @@ -42,10 +42,13 @@ pub struct NativeDevice { pub adapter: FreeUnixAdapter, } -impl Device { +impl WaylandDevice { #[inline] - pub(crate) fn new(connection: &Connection, adapter: &FreeUnixAdapter) -> Result { - Ok(Device { + pub(crate) fn new( + connection: &WaylandConnection, + adapter: &FreeUnixAdapter, + ) -> Result { + Ok(WaylandDevice { native_connection: connection.native_connection.clone(), adapter: adapter.clone(), }) diff --git a/src/wayland/mod.rs b/src/wayland/mod.rs index 26bfb6a9..1b81dabb 100644 --- a/src/wayland/mod.rs +++ b/src/wayland/mod.rs @@ -5,8 +5,12 @@ pub mod context; pub mod device; pub mod surface; -crate::implement_interfaces!(); +crate::implement_interfaces!(WaylandConnection, WaylandDevice); +#[cfg(test)] +use self::connection::WaylandConnection as Connection; +#[cfg(test)] +use self::device::WaylandDevice as Device; #[cfg(test)] #[path = "../tests.rs"] mod tests; diff --git a/src/wgl/connection.rs b/src/wgl/connection.rs index e593f2b4..cbc9f674 100644 --- a/src/wgl/connection.rs +++ b/src/wgl/connection.rs @@ -3,14 +3,14 @@ //! Window server connections are implicit in the Win32 API, so this is a zero-sized type. use super::adapter::WglAdapter; -use super::device::{Device, NativeDevice}; +use super::device::{NativeDevice, WglDevice}; use crate::{Adapter, AdapterPreferences, Error, GLApi}; /// Represents a connection to the display server. /// /// Window server connections are implicit in the Win32 API, so this is a zero-sized type. #[derive(Clone)] -pub struct Connection; +pub struct WglConnection; /// An empty placeholder for native connections. /// @@ -18,17 +18,17 @@ pub struct Connection; #[derive(Clone)] pub struct NativeConnection; -impl Connection { +impl WglConnection { /// Connects to the default display. #[inline] - pub fn new() -> Result { - Ok(Connection) + pub fn new() -> Result { + Ok(WglConnection) } /// An alias for `Connection::new()`, present for consistency with other backends. #[inline] - pub unsafe fn from_native_connection(_: NativeConnection) -> Result { - Connection::new() + pub unsafe fn from_native_connection(_: NativeConnection) -> Result { + WglConnection::new() } /// Returns the underlying native connection. @@ -51,8 +51,8 @@ impl Connection { /// Opens a device. #[inline] - pub fn create_device(&self, adapter: &Adapter) -> Result { - Device::new(adapter.wgl()?) + pub fn create_device(&self, adapter: &Adapter) -> Result { + WglDevice::new(adapter.wgl()?) } /// Creates a `Device` from a Direct3D 11 device and associated GL/DX interop handle. @@ -66,13 +66,15 @@ impl Connection { pub unsafe fn create_device_from_native_device( &self, native_device: NativeDevice, - ) -> Result { - Device::from_native_device(native_device) + ) -> Result { + WglDevice::from_native_device(native_device) } /// Opens the display connection corresponding to the given `DisplayHandle`. - pub fn from_display_handle(_: raw_window_handle::DisplayHandle) -> Result { - Connection::new() + pub fn from_display_handle( + _: raw_window_handle::DisplayHandle, + ) -> Result { + WglConnection::new() } } diff --git a/src/wgl/device.rs b/src/wgl/device.rs index 44992d44..117770c6 100644 --- a/src/wgl/device.rs +++ b/src/wgl/device.rs @@ -90,7 +90,7 @@ unsafe impl Send for SendableHWND {} /// /// Devices contain most of the relevant surface management methods. #[allow(dead_code)] -pub struct Device { +pub struct WglDevice { pub(crate) adapter: WglAdapter, pub(crate) d3d11_device: ComPtr, pub(crate) d3d11_device_context: ComPtr, @@ -111,7 +111,7 @@ pub struct NativeDevice { pub gl_dx_interop_device: HANDLE, } -impl Drop for Device { +impl Drop for WglDevice { fn drop(&mut self) { let dx_interop_functions = WGL_EXTENSION_FUNCTIONS .dx_interop_functions @@ -123,8 +123,8 @@ impl Drop for Device { } } -impl Device { - pub(crate) fn new(adapter: &WglAdapter) -> Result { +impl WglDevice { + pub(crate) fn new(adapter: &WglAdapter) -> Result { adapter.set_exported_variables(); let dx_interop_functions = match WGL_EXTENSION_FUNCTIONS.dx_interop_functions { @@ -160,7 +160,7 @@ impl Device { let hidden_window = HiddenWindow::new(); - Ok(Device { + Ok(WglDevice { adapter: adapter.clone(), d3d11_device, d3d11_device_context, @@ -170,7 +170,7 @@ impl Device { } } - pub(crate) fn from_native_device(native_device: NativeDevice) -> Result { + pub(crate) fn from_native_device(native_device: NativeDevice) -> Result { unsafe { (*native_device.d3d11_device).AddRef(); let d3d11_device = ComPtr::from_raw(native_device.d3d11_device); @@ -195,7 +195,7 @@ impl Device { let gl_dx_interop_device = native_device.gl_dx_interop_device; let hidden_window = HiddenWindow::new(); - Ok(Device { + Ok(WglDevice { adapter, d3d11_device, d3d11_device_context, diff --git a/src/wgl/mod.rs b/src/wgl/mod.rs index 59391fb6..fedcc698 100644 --- a/src/wgl/mod.rs +++ b/src/wgl/mod.rs @@ -8,8 +8,12 @@ pub mod context; pub mod device; pub mod surface; -crate::implement_interfaces!(); +crate::implement_interfaces!(WglConnection, WglDevice); +#[cfg(test)] +use self::connection::WglConnection as Connection; +#[cfg(test)] +use self::device::WglDevice as Device; #[cfg(test)] #[path = "../tests.rs"] mod tests; diff --git a/src/x11/connection.rs b/src/x11/connection.rs index b42ecbac..6b5f7e6c 100644 --- a/src/x11/connection.rs +++ b/src/x11/connection.rs @@ -1,6 +1,6 @@ //! A wrapper for X11 server connections (`DISPLAY` variables). -use super::device::{Device, NativeDevice}; +use super::device::{NativeDevice, X11Device}; use crate::base::egl::device::EGL_FUNCTIONS; use crate::base::egl::ffi::EGL_PLATFORM_X11_KHR; use crate::egl::types::{EGLAttrib, EGLDisplay}; @@ -19,11 +19,11 @@ static X_THREADS_INIT: Once = Once::new(); /// A connection to the X11 display server. #[derive(Clone)] -pub struct Connection { +pub struct X11Connection { pub(crate) native_connection: Arc, } -unsafe impl Send for Connection {} +unsafe impl Send for X11Connection {} pub(crate) struct NativeConnectionWrapper { pub(crate) xlib: Xlib, @@ -61,10 +61,10 @@ impl Drop for NativeConnectionWrapper { } } -impl Connection { +impl X11Connection { /// Connects to the default display. #[inline] - pub fn new() -> Result { + pub fn new() -> Result { unsafe { let xlib = Xlib::open().map_err(|_| Error::ConnectionFailed)?; @@ -79,7 +79,7 @@ impl Connection { let egl_display = create_egl_display(x11_display); - Ok(Connection { + Ok(X11Connection { native_connection: Arc::new(NativeConnectionWrapper { xlib, egl_display, @@ -104,9 +104,9 @@ impl Connection { #[inline] pub unsafe fn from_native_connection( native_connection: NativeConnection, - ) -> Result { + ) -> Result { let xlib = Xlib::open().map_err(|_| Error::ConnectionFailed)?; - Ok(Connection { + Ok(X11Connection { native_connection: Arc::new(NativeConnectionWrapper { xlib, egl_display: native_connection.egl_display, @@ -116,11 +116,11 @@ impl Connection { }) } - fn from_x11_display(x11_display: *mut Display) -> Result { + fn from_x11_display(x11_display: *mut Display) -> Result { let xlib = Xlib::open().map_err(|_| Error::ConnectionFailed)?; unsafe { let egl_display = create_egl_display(x11_display); - Ok(Connection { + Ok(X11Connection { native_connection: Arc::new(NativeConnectionWrapper { xlib, egl_display, @@ -156,8 +156,8 @@ impl Connection { /// /// Device handles are local to a single thread. #[inline] - pub fn create_device(&self, adapter: &Adapter) -> Result { - Device::new(self, adapter.free_unix()?) + pub fn create_device(&self, adapter: &Adapter) -> Result { + X11Device::new(self, adapter.free_unix()?) } /// Opens the hardware device corresponding to the adapter wrapped in the given native @@ -168,14 +168,14 @@ impl Connection { pub unsafe fn create_device_from_native_device( &self, native_device: NativeDevice, - ) -> Result { - Device::new(self, &native_device.adapter) + ) -> Result { + X11Device::new(self, &native_device.adapter) } /// Opens the display connection corresponding to the given `DisplayHandle`. pub fn from_display_handle( handle: raw_window_handle::DisplayHandle, - ) -> Result { + ) -> Result { use raw_window_handle::RawDisplayHandle::Xcb; use raw_window_handle::RawDisplayHandle::Xlib; use raw_window_handle::XlibDisplayHandle; @@ -188,7 +188,7 @@ impl Connection { _ => return Err(Error::IncompatibleRawDisplayHandle), }; - Connection::from_x11_display(display) + X11Connection::from_x11_display(display) } } @@ -261,7 +261,7 @@ mod tests { pub fn test_from_x11_display() { use crate::{ContextAttributeFlags, ContextAttributes, GLVersion, PowerPreference}; - let connection = Connection::new().unwrap(); + let connection = X11Connection::new().unwrap(); let adapter = connection .create_adapter(AdapterPreferences { power: PowerPreference::LowPower, @@ -289,7 +289,8 @@ mod tests { { let _second_connection = - Connection::from_x11_display(connection.native_connection().x11_display).unwrap(); + X11Connection::from_x11_display(connection.native_connection().x11_display) + .unwrap(); } device.destroy_context(&mut context).unwrap(); diff --git a/src/x11/device.rs b/src/x11/device.rs index 0dcd1fe2..0e29618a 100644 --- a/src/x11/device.rs +++ b/src/x11/device.rs @@ -1,6 +1,6 @@ //! A wrapper around X11 `EGLDisplay`s. -use super::connection::{Connection, NativeConnectionWrapper}; +use super::connection::{NativeConnectionWrapper, X11Connection}; use super::context::NativeContext; use crate::base::egl::{ context::{self, CurrentContextGuard, EGLBackedContext}, @@ -28,7 +28,7 @@ const SURFACE_GL_TEXTURE_TARGET: u32 = gl::TEXTURE_2D; /// A thread-local handle to a device. /// /// Devices contain most of the relevant surface management methods. -pub struct Device { +pub struct X11Device { pub(crate) native_connection: Arc, pub(crate) adapter: FreeUnixAdapter, } @@ -42,10 +42,13 @@ pub struct NativeDevice { pub adapter: FreeUnixAdapter, } -impl Device { +impl X11Device { #[inline] - pub(crate) fn new(connection: &Connection, adapter: &FreeUnixAdapter) -> Result { - Ok(Device { + pub(crate) fn new( + connection: &X11Connection, + adapter: &FreeUnixAdapter, + ) -> Result { + Ok(X11Device { native_connection: connection.native_connection.clone(), adapter: adapter.clone(), }) diff --git a/src/x11/mod.rs b/src/x11/mod.rs index beae5e84..dd5d51c2 100644 --- a/src/x11/mod.rs +++ b/src/x11/mod.rs @@ -5,8 +5,12 @@ pub mod context; pub mod device; pub mod surface; -crate::implement_interfaces!(); +crate::implement_interfaces!(X11Connection, X11Device); +#[cfg(test)] +use self::connection::X11Connection as Connection; +#[cfg(test)] +use self::device::X11Device as Device; #[cfg(test)] #[path = "../tests.rs"] mod tests;