@@ -3,7 +3,7 @@ use internal::Channel;
3
3
use native;
4
4
use device:: { Device , PhysicalDevice } ;
5
5
6
- use std:: cell :: Cell ;
6
+ use std:: mem ;
7
7
use std:: sync:: { Arc , Mutex } ;
8
8
9
9
use hal:: { self , format, image} ;
@@ -19,7 +19,6 @@ use foreign_types::{ForeignType, ForeignTypeRef};
19
19
20
20
21
21
pub type CAMetalLayer = * mut Object ;
22
- pub type CADrawable = * mut Object ;
23
22
24
23
pub struct Surface {
25
24
pub ( crate ) inner : Arc < SurfaceInner > ,
@@ -43,20 +42,10 @@ impl Drop for SurfaceInner {
43
42
44
43
#[ derive( Debug ) ]
45
44
struct Frame {
46
- drawable : Cell < Option < CADrawable > > ,
45
+ drawable : Mutex < Option < metal :: Drawable > > ,
47
46
texture : metal:: Texture ,
48
47
}
49
48
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
-
60
49
pub struct Swapchain {
61
50
frames : Vec < Frame > ,
62
51
surface : Arc < SurfaceInner > ,
@@ -67,15 +56,12 @@ unsafe impl Send for Swapchain {}
67
56
unsafe impl Sync for Swapchain { }
68
57
69
58
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!" )
79
65
}
80
66
}
81
67
@@ -184,12 +170,11 @@ impl Device {
184
170
185
171
let frames = ( 0 .. config. image_count )
186
172
. 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] ;
189
174
let texture: metal:: Texture = msg_send ! [ drawable, texture] ;
190
175
//HACK: not retaining the texture here
191
176
Frame {
192
- drawable : Cell :: new ( None ) , //Note: careful!
177
+ drawable : Mutex :: new ( None ) ,
193
178
texture,
194
179
}
195
180
} )
@@ -237,20 +222,17 @@ impl hal::Swapchain<Backend> for Swapchain {
237
222
}
238
223
239
224
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] )
246
228
} ;
247
229
248
230
let index = self . frames
249
231
. iter ( )
250
232
. position ( |f| f. texture . as_ptr ( ) == texture_temp. as_ptr ( ) )
251
233
. 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 ( ) ) ;
254
236
255
237
Ok ( index as _ )
256
238
}
0 commit comments