use crate::Error;
use crate::GLApi;
use crate::platform::macos::system::connection::Connection as SystemConnection;
use crate::platform::macos::system::device::NativeDevice;
use crate::platform::macos::system::surface::NativeWidget;
use super::device::{Adapter, Device};
use euclid::default::Size2D;
use std::os::raw::c_void;
#[cfg(feature = "sm-raw-window-handle")]
use crate::platform::macos::system::surface::NSView;
#[cfg(feature = "sm-raw-window-handle")]
use cocoa::base::id;
#[cfg(feature = "sm-winit")]
use winit::Window;
pub use crate::platform::macos::system::connection::NativeConnection;
#[derive(Clone)]
pub struct Connection(pub SystemConnection);
impl Connection {
#[inline]
pub fn new() -> Result<Connection, Error> {
SystemConnection::new().map(Connection)
}
#[inline]
pub unsafe fn from_native_connection(native_connection: NativeConnection)
-> Result<Connection, Error> {
SystemConnection::from_native_connection(native_connection).map(Connection)
}
#[inline]
pub fn native_connection(&self) -> NativeConnection {
self.0.native_connection()
}
#[inline]
pub fn gl_api(&self) -> GLApi {
GLApi::GL
}
#[inline]
pub fn create_adapter(&self) -> Result<Adapter, Error> {
self.0.create_adapter().map(Adapter)
}
#[inline]
pub fn create_hardware_adapter(&self) -> Result<Adapter, Error> {
self.0.create_hardware_adapter().map(Adapter)
}
#[inline]
pub fn create_low_power_adapter(&self) -> Result<Adapter, Error> {
self.0.create_low_power_adapter().map(Adapter)
}
#[inline]
pub fn create_software_adapter(&self) -> Result<Adapter, Error> {
self.0.create_software_adapter().map(Adapter)
}
#[inline]
pub fn create_device(&self, adapter: &Adapter) -> Result<Device, Error> {
self.0.create_device(&adapter.0).map(Device)
}
#[inline]
pub unsafe fn create_device_from_native_device(&self, native_device: NativeDevice)
-> Result<Device, Error> {
self.0.create_device_from_native_device(native_device).map(Device)
}
#[cfg(feature = "sm-winit")]
pub fn from_winit_window(window: &Window) -> Result<Connection, Error> {
SystemConnection::from_winit_window(window).map(Connection)
}
#[cfg(feature = "sm-winit")]
#[inline]
pub fn create_native_widget_from_winit_window(&self, window: &Window)
-> Result<NativeWidget, Error> {
self.0.create_native_widget_from_winit_window(window)
}
pub unsafe fn create_native_widget_from_ptr(&self, raw: *mut c_void, size: Size2D<i32>) -> NativeWidget {
self.0.create_native_widget_from_ptr(raw, size)
}
#[cfg(feature = "sm-raw-window-handle")]
#[inline]
pub fn create_native_widget_from_rwh(&self, raw_handle: raw_window_handle::RawWindowHandle)
-> Result<NativeWidget, Error> {
use raw_window_handle::RawWindowHandle::MacOS;
match raw_handle {
MacOS(handle) => Ok(NativeWidget {
view: NSView(unsafe {
msg_send![handle.ns_view as id, retain]
}),
}),
_ => Err(Error::IncompatibleNativeWidget),
}
}
}