Skip to content

Commit b312258

Browse files
committed
[mtl] use metal::Drawable and present via command buffers
1 parent 8bb7921 commit b312258

File tree

2 files changed

+22
-34
lines changed

2 files changed

+22
-34
lines changed

src/backend/metal/src/command.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1304,12 +1304,18 @@ impl RawCommandQueue<Backend> for CommandQueue {
13041304
IW: IntoIterator,
13051305
IW::Item: Borrow<native::Semaphore>,
13061306
{
1307+
let queue = self.shared.queue.lock().unwrap();
1308+
let command_buffer = queue.raw.new_command_buffer();
1309+
13071310
for (swapchain, index) in swapchains {
13081311
// TODO: wait for semaphores
13091312
debug!("presenting frame {}", index);
1310-
swapchain.borrow().present(index);
1313+
let drawable = swapchain.borrow().take_drawable(index);
1314+
command_buffer.present_drawable(&drawable);
13111315
}
13121316

1317+
command_buffer.commit();
1318+
13131319
let shared_capture_manager = CaptureManager::shared();
13141320
if shared_capture_manager.is_capturing() {
13151321
shared_capture_manager.stop_capture();

src/backend/metal/src/window.rs

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use internal::Channel;
33
use native;
44
use device::{Device, PhysicalDevice};
55

6-
use std::cell::Cell;
6+
use std::mem;
77
use std::sync::{Arc, Mutex};
88

99
use hal::{self, format, image};
@@ -19,7 +19,6 @@ use foreign_types::{ForeignType, ForeignTypeRef};
1919

2020

2121
pub type CAMetalLayer = *mut Object;
22-
pub type CADrawable = *mut Object;
2322

2423
pub struct Surface {
2524
pub(crate) inner: Arc<SurfaceInner>,
@@ -43,20 +42,10 @@ impl Drop for SurfaceInner {
4342

4443
#[derive(Debug)]
4544
struct Frame {
46-
drawable: Cell<Option<CADrawable>>,
45+
drawable: Mutex<Option<metal::Drawable>>,
4746
texture: metal::Texture,
4847
}
4948

50-
impl Drop for Frame {
51-
fn drop(&mut self) {
52-
if let Some(drawable) = self.drawable.get() {
53-
unsafe {
54-
msg_send![drawable, release];
55-
}
56-
}
57-
}
58-
}
59-
6049
pub struct Swapchain {
6150
frames: Vec<Frame>,
6251
surface: Arc<SurfaceInner>,
@@ -67,15 +56,12 @@ unsafe impl Send for Swapchain {}
6756
unsafe impl Sync for Swapchain {}
6857

6958
impl Swapchain {
70-
pub(crate) fn present(&self, index: hal::SwapImageIndex) {
71-
let drawable = self.frames[index as usize].drawable
72-
.replace(None)
73-
.unwrap();
74-
unsafe {
75-
msg_send![drawable, present];
76-
//TODO: delay the actual release
77-
msg_send![drawable, release];
78-
}
59+
pub(crate) fn take_drawable(&self, index: hal::SwapImageIndex) -> metal::Drawable {
60+
self.frames[index as usize].drawable
61+
.lock()
62+
.unwrap()
63+
.take()
64+
.expect("Drawable has not been acquired!")
7965
}
8066
}
8167

@@ -184,12 +170,11 @@ impl Device {
184170

185171
let frames = (0 .. config.image_count)
186172
.map(|_| unsafe {
187-
let drawable: *mut Object = msg_send![render_layer, nextDrawable];
188-
assert!(!drawable.is_null());
173+
let drawable: &metal::DrawableRef = msg_send![render_layer, nextDrawable];
189174
let texture: metal::Texture = msg_send![drawable, texture];
190175
//HACK: not retaining the texture here
191176
Frame {
192-
drawable: Cell::new(None), //Note: careful!
177+
drawable: Mutex::new(None),
193178
texture,
194179
}
195180
})
@@ -237,20 +222,17 @@ impl hal::Swapchain<Backend> for Swapchain {
237222
}
238223

239224
let layer_ref = self.surface.render_layer.lock().unwrap();
240-
let drawable: CADrawable = unsafe {
241-
msg_send![*layer_ref, nextDrawable]
242-
};
243-
let texture_temp: &metal::TextureRef = unsafe {
244-
msg_send![drawable, retain];
245-
msg_send![drawable, texture]
225+
let (drawable, texture_temp): (metal::Drawable, &metal::TextureRef) = unsafe {
226+
let drawable: &metal::DrawableRef = msg_send![*layer_ref, nextDrawable];
227+
(drawable.to_owned(), msg_send![drawable, texture])
246228
};
247229

248230
let index = self.frames
249231
.iter()
250232
.position(|f| f.texture.as_ptr() == texture_temp.as_ptr())
251233
.expect("Surface lost?");
252-
let old = self.frames[index].drawable.replace(Some(drawable));
253-
assert_eq!(old, None);
234+
let old = mem::replace(&mut *self.frames[index].drawable.lock().unwrap(), Some(drawable));
235+
assert!(old.is_none());
254236

255237
Ok(index as _)
256238
}

0 commit comments

Comments
 (0)