forked from servo/surfman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.rs
169 lines (147 loc) · 6.03 KB
/
connection.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// surfman/surfman/src/platform/macos/cgl/connection.rs
//
//! Represents the connection to the Core Graphics window server.
//!
//! Connection types are zero-sized on macOS, because the system APIs automatically manage the
//! global window server connection.
use super::device::{Adapter, Device};
use crate::platform::macos::system::connection::Connection as SystemConnection;
use crate::platform::macos::system::device::NativeDevice;
use crate::platform::macos::system::surface::NativeWidget;
use crate::Error;
use crate::GLApi;
use euclid::default::Size2D;
use std::os::raw::c_void;
pub use crate::platform::macos::system::connection::NativeConnection;
/// A connection to the display server.
#[derive(Clone)]
pub struct Connection(pub SystemConnection);
impl Connection {
/// Connects to the default display.
#[inline]
pub fn new() -> Result<Connection, Error> {
SystemConnection::new().map(Connection)
}
/// An alias for `Connection::new()`, present for consistency with other backends.
#[inline]
pub unsafe fn from_native_connection(
native_connection: NativeConnection,
) -> Result<Connection, Error> {
SystemConnection::from_native_connection(native_connection).map(Connection)
}
/// Returns the underlying native connection.
#[inline]
pub fn native_connection(&self) -> NativeConnection {
self.0.native_connection()
}
/// Returns the OpenGL API flavor that this connection supports (OpenGL or OpenGL ES).
#[inline]
pub fn gl_api(&self) -> GLApi {
GLApi::GL
}
/// Returns the "best" adapter on this system, preferring high-performance hardware adapters.
///
/// This is an alias for `Connection::create_hardware_adapter()`.
#[inline]
pub fn create_adapter(&self) -> Result<Adapter, Error> {
self.0.create_adapter().map(Adapter)
}
/// Returns the "best" adapter on this system, preferring high-performance hardware adapters.
#[inline]
pub fn create_hardware_adapter(&self) -> Result<Adapter, Error> {
self.0.create_hardware_adapter().map(Adapter)
}
/// Returns the "best" adapter on this system, preferring low-power hardware adapters.
#[inline]
pub fn create_low_power_adapter(&self) -> Result<Adapter, Error> {
self.0.create_low_power_adapter().map(Adapter)
}
/// Returns the "best" adapter on this system, preferring software adapters.
#[inline]
pub fn create_software_adapter(&self) -> Result<Adapter, Error> {
self.0.create_software_adapter().map(Adapter)
}
/// Opens the hardware device corresponding to the given adapter.
///
/// Device handles are local to a single thread.
#[inline]
pub fn create_device(&self, adapter: &Adapter) -> Result<Device, Error> {
self.0.create_device(&adapter.0).map(Device)
}
/// An alias for `connection.create_device()` with the default adapter.
#[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)
}
/// Opens the display connection corresponding to the given `RawDisplayHandle`.
#[cfg(feature = "sm-raw-window-handle-05")]
pub fn from_raw_display_handle(
raw_handle: rwh_05::RawDisplayHandle,
) -> Result<Connection, Error> {
SystemConnection::from_raw_display_handle(raw_handle).map(Connection)
}
/// Opens the display connection corresponding to the given `DisplayHandle`.
#[cfg(feature = "sm-raw-window-handle-06")]
pub fn from_display_handle(handle: rwh_06::DisplayHandle) -> Result<Connection, Error> {
SystemConnection::from_display_handle(handle).map(Connection)
}
/// Creates a native widget from a raw pointer
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)
}
/// Create a native widget type from the given `RawWindowHandle`.
#[cfg(feature = "sm-raw-window-handle-05")]
#[inline]
pub fn create_native_widget_from_raw_window_handle(
&self,
raw_handle: rwh_05::RawWindowHandle,
_size: Size2D<i32>,
) -> Result<NativeWidget, Error> {
use crate::platform::macos::system::surface::NSView;
use cocoa::base::id;
use rwh_05::RawWindowHandle::AppKit;
match raw_handle {
AppKit(handle) => Ok(NativeWidget {
view: NSView(unsafe { msg_send![handle.ns_view as id, retain] }),
opaque: unsafe { msg_send![handle.ns_window as id, isOpaque] },
}),
_ => Err(Error::IncompatibleNativeWidget),
}
}
/// Create a native widget type from the given `WindowHandle`.
#[cfg(feature = "sm-raw-window-handle-06")]
#[inline]
pub fn create_native_widget_from_window_handle(
&self,
handle: rwh_06::WindowHandle,
_size: Size2D<i32>,
) -> Result<NativeWidget, Error> {
use crate::platform::macos::system::surface::NSView;
use cocoa::base::id;
use rwh_06::RawWindowHandle::AppKit;
match handle.as_raw() {
AppKit(handle) => {
let ns_view = handle.ns_view.as_ptr() as id;
// https://developer.apple.com/documentation/appkit/nsview/1483301-window
let ns_window: id = unsafe { msg_send![ns_view, window] };
Ok(NativeWidget {
// Increment the nsview's reference count with retain. See:
// https://developer.apple.com/documentation/objectivec/1418956-nsobject/1571946-retain
view: NSView(unsafe { msg_send![ns_view, retain] }),
// https://developer.apple.com/documentation/appkit/nswindow/1419086-isopaque
opaque: unsafe { msg_send![ns_window, isOpaque] },
})
}
_ => Err(Error::IncompatibleNativeWidget),
}
}
}