-
Notifications
You must be signed in to change notification settings - Fork 623
/
metal_surface.rs
196 lines (167 loc) · 7.12 KB
/
metal_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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
use core_graphics_types::geometry::CGSize;
use foreign_types::{ForeignType, ForeignTypeRef};
use i_slint_core::api::{OpenGLAPI, PhysicalSize as PhysicalWindowSize};
use metal::MTLPixelFormat;
use objc::{msg_send, sel, sel_impl};
use objc::{
rc::autoreleasepool,
runtime::{Object, BOOL, NO},
};
use skia_safe::gpu::mtl;
use std::cell::RefCell;
use std::rc::Rc;
#[link(name = "QuartzCore", kind = "framework")]
extern "C" {
#[allow(non_upper_case_globals)]
static kCAGravityTopLeft: *mut Object;
#[allow(non_upper_case_globals)]
static kCAGravityBottomLeft: *mut Object;
}
/// This surface renders into the given window using Metal. The provided display argument
/// is ignored, as it has no meaning on macOS.
pub struct MetalSurface {
command_queue: metal::CommandQueue,
layer: metal::MetalLayer,
gr_context: RefCell<skia_safe::gpu::DirectContext>,
}
impl super::Surface for MetalSurface {
fn new(
window_handle: Rc<dyn raw_window_handle::HasWindowHandle>,
_display_handle: Rc<dyn raw_window_handle::HasDisplayHandle>,
size: PhysicalWindowSize,
_opengl_api: Option<OpenGLAPI>,
) -> Result<Self, i_slint_core::platform::PlatformError> {
let layer = match window_handle
.window_handle()
.map_err(|e| format!("Error obtaining window handle for skia metal renderer: {e}"))?
.as_raw()
{
raw_window_handle::RawWindowHandle::AppKit(handle) => unsafe {
raw_window_metal::Layer::from_ns_view(handle.ns_view)
},
raw_window_handle::RawWindowHandle::UiKit(handle) => unsafe {
raw_window_metal::Layer::from_ui_view(handle.ui_view)
},
_ => return Err("Skia Renderer: Metal surface is only supported with AppKit".into()),
};
// SAFETY: The layer is an initialized instance of `CAMetalLayer`, and
// we transfer the retain count to `MetalLayer` using `into_raw`.
let layer = unsafe { metal::MetalLayer::from_ptr(layer.into_raw().cast().as_ptr()) };
let device = metal::Device::system_default()
.ok_or_else(|| format!("Skia Renderer: No metal device found"))?;
layer.set_device(&device);
layer.set_pixel_format(MTLPixelFormat::BGRA8Unorm);
layer.set_opaque(false);
layer.set_presents_with_transaction(false);
layer.set_drawable_size(CGSize::new(size.width as f64, size.height as f64));
let flipped: BOOL = unsafe { msg_send![layer.as_ptr(), contentsAreFlipped] };
let gravity = if flipped == NO {
unsafe { kCAGravityTopLeft }
} else {
unsafe { kCAGravityBottomLeft }
};
let _: () = unsafe { msg_send![layer.as_ptr(), setContentsGravity: gravity] };
let command_queue = device.new_command_queue();
let backend = unsafe {
mtl::BackendContext::new(
device.as_ptr() as mtl::Handle,
command_queue.as_ptr() as mtl::Handle,
)
};
let gr_context =
skia_safe::gpu::direct_contexts::make_metal(&backend, None).unwrap().into();
Ok(Self { command_queue, layer, gr_context })
}
fn name(&self) -> &'static str {
"metal"
}
fn resize_event(
&self,
size: PhysicalWindowSize,
) -> Result<(), i_slint_core::platform::PlatformError> {
self.layer.set_drawable_size(CGSize::new(size.width as f64, size.height as f64));
Ok(())
}
fn render(
&self,
_size: PhysicalWindowSize,
callback: &dyn Fn(&skia_safe::Canvas, Option<&mut skia_safe::gpu::DirectContext>),
pre_present_callback: &RefCell<Option<Box<dyn FnMut()>>>,
) -> Result<(), i_slint_core::platform::PlatformError> {
autoreleasepool(|| {
let drawable = match self.layer.next_drawable() {
Some(drawable) => drawable,
None => {
return Err(format!(
"Skia Metal Renderer: Failed to retrieve next drawable for rendering"
)
.into())
}
};
let gr_context = &mut self.gr_context.borrow_mut();
let size = self.layer.drawable_size();
let mut surface = unsafe {
let texture_info =
mtl::TextureInfo::new(drawable.texture().as_ptr() as mtl::Handle);
let backend_render_target = skia_safe::gpu::backend_render_targets::make_mtl(
(size.width as i32, size.height as i32),
&texture_info,
);
skia_safe::gpu::surfaces::wrap_backend_render_target(
gr_context,
&backend_render_target,
skia_safe::gpu::SurfaceOrigin::TopLeft,
skia_safe::ColorType::BGRA8888,
None,
None,
)
.unwrap()
};
callback(surface.canvas(), Some(gr_context));
drop(surface);
gr_context.submit(None);
if let Some(pre_present_callback) = pre_present_callback.borrow_mut().as_mut() {
pre_present_callback();
}
let command_buffer = self.command_queue.new_command_buffer();
command_buffer.present_drawable(drawable);
command_buffer.commit();
Ok(())
})
}
fn bits_per_pixel(&self) -> Result<u8, i_slint_core::platform::PlatformError> {
// From https://developer.apple.com/documentation/metal/mtlpixelformat:
// The storage size of each pixel format is determined by the sum of its components.
// For example, the storage size of BGRA8Unorm is 32 bits (four 8-bit components) and
// the storage size of BGR5A1Unorm is 16 bits (three 5-bit components and one 1-bit component).
Ok(match self.layer.pixel_format() {
MTLPixelFormat::B5G6R5Unorm
| MTLPixelFormat::A1BGR5Unorm
| MTLPixelFormat::ABGR4Unorm
| MTLPixelFormat::BGR5A1Unorm => 16,
MTLPixelFormat::RGBA8Unorm
| MTLPixelFormat::RGBA8Unorm_sRGB
| MTLPixelFormat::RGBA8Snorm
| MTLPixelFormat::RGBA8Uint
| MTLPixelFormat::RGBA8Sint
| MTLPixelFormat::BGRA8Unorm
| MTLPixelFormat::BGRA8Unorm_sRGB => 32,
MTLPixelFormat::RGB10A2Unorm
| MTLPixelFormat::RGB10A2Uint
| MTLPixelFormat::BGR10A2Unorm => 32,
MTLPixelFormat::RGBA16Unorm
| MTLPixelFormat::RGBA16Snorm
| MTLPixelFormat::RGBA16Uint
| MTLPixelFormat::RGBA16Sint => 64,
MTLPixelFormat::RGBA32Uint | MTLPixelFormat::RGBA32Sint => 128,
fmt @ _ => {
return Err(format!(
"Skia Metal Renderer: Unsupported layer pixel format found {fmt:?}"
)
.into())
}
})
}
}