-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathsurface.rs
146 lines (135 loc) · 5.5 KB
/
surface.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
use core_graphics_types::{
base::CGFloat,
geometry::{CGRect, CGSize},
};
use objc::{
class, msg_send,
runtime::{Object, BOOL, YES},
sel, sel_impl,
};
use std::{mem, ptr};
#[cfg(target_os = "macos")]
#[link(name = "QuartzCore", kind = "framework")]
extern "C" {
#[allow(non_upper_case_globals)]
static kCAGravityTopLeft: *mut Object;
}
impl super::Surface {
pub unsafe fn from_view(view: *mut Object) -> Self {
let main_layer: *mut Object = msg_send![view, layer];
let class = class!(CAMetalLayer);
let is_valid_layer: BOOL = msg_send![main_layer, isKindOfClass: class];
let raw_layer = if is_valid_layer == YES {
main_layer
} else {
// If the main layer is not a CAMetalLayer, we create a CAMetalLayer and use it.
let new_layer: *mut Object = msg_send![class, new];
let frame: CGRect = msg_send![main_layer, bounds];
let () = msg_send![new_layer, setFrame: frame];
#[cfg(target_os = "ios")]
{
// Unlike NSView, UIView does not allow to replace main layer.
let () = msg_send![main_layer, addSublayer: new_layer];
let () = msg_send![main_layer, setAutoresizingMask: 0x1Fu64];
let screen: *mut Object = msg_send![class!(UIScreen), mainScreen];
let scale_factor: CGFloat = msg_send![screen, nativeScale];
let () = msg_send![view, setContentScaleFactor: scale_factor];
};
#[cfg(target_os = "macos")]
{
let () = msg_send![view, setLayer: new_layer];
let () = msg_send![view, setWantsLayer: YES];
let () = msg_send![new_layer, setContentsGravity: kCAGravityTopLeft];
let window: *mut Object = msg_send![view, window];
if !window.is_null() {
let scale_factor: CGFloat = msg_send![window, backingScaleFactor];
let () = msg_send![new_layer, setContentsScale: scale_factor];
}
}
new_layer
};
Self {
view: msg_send![view, retain],
render_layer: mem::transmute::<_, &metal::MetalLayerRef>(raw_layer).to_owned(),
info: crate::SurfaceInfo {
format: crate::TextureFormat::Rgba8Unorm,
alpha: crate::AlphaMode::Ignored,
},
}
}
/// Get the CALayerMetal for this surface, if any.
/// This is platform specific API.
pub fn metal_layer(&self) -> metal::MetalLayer {
self.render_layer.clone()
}
pub fn info(&self) -> crate::SurfaceInfo {
self.info
}
pub fn acquire_frame(&self) -> super::Frame {
let (drawable, texture) = objc::rc::autoreleasepool(|| {
let drawable = self.render_layer.next_drawable().unwrap();
(drawable.to_owned(), drawable.texture().to_owned())
});
super::Frame { drawable, texture }
}
}
impl super::Context {
pub fn create_surface<I: raw_window_handle::HasWindowHandle>(
&self,
window: &I,
) -> Result<super::Surface, crate::NotSupportedError> {
Ok(match window.window_handle().unwrap().as_raw() {
#[cfg(target_os = "ios")]
raw_window_handle::RawWindowHandle::UiKit(handle) => unsafe {
super::Surface::from_view(handle.ui_view.as_ptr() as *mut _)
},
#[cfg(target_os = "macos")]
raw_window_handle::RawWindowHandle::AppKit(handle) => unsafe {
super::Surface::from_view(handle.ns_view.as_ptr() as *mut _)
},
_ => return Err(crate::NotSupportedError::PlatformNotSupported),
})
}
pub fn destroy_surface(&self, surface: &mut super::Surface) {
unsafe {
let () = msg_send![surface.view, release];
}
surface.view = ptr::null_mut();
}
pub fn reconfigure_surface(&self, surface: &mut super::Surface, config: crate::SurfaceConfig) {
let device = self.device.lock().unwrap();
surface.info = crate::SurfaceInfo {
format: match config.color_space {
crate::ColorSpace::Linear => crate::TextureFormat::Bgra8UnormSrgb,
crate::ColorSpace::Srgb => crate::TextureFormat::Bgra8Unorm,
},
alpha: if config.transparent {
crate::AlphaMode::PostMultiplied
} else {
//Warning: it's not really ignored! Instead, it's assumed to be 1:
// https://developer.apple.com/documentation/quartzcore/calayer/1410763-isopaque
crate::AlphaMode::Ignored
},
};
let vsync = match config.display_sync {
crate::DisplaySync::Block => true,
crate::DisplaySync::Recent | crate::DisplaySync::Tear => false,
};
surface.render_layer.set_opaque(!config.transparent);
surface.render_layer.set_device(&*device);
surface
.render_layer
.set_pixel_format(super::map_texture_format(surface.info.format));
surface
.render_layer
.set_framebuffer_only(config.usage == crate::TextureUsage::TARGET);
surface.render_layer.set_maximum_drawable_count(3);
surface.render_layer.set_drawable_size(CGSize::new(
config.size.width as f64,
config.size.height as f64,
));
unsafe {
let () = msg_send![surface.render_layer, setDisplaySyncEnabled: vsync];
}
}
}