@@ -3,13 +3,15 @@ use raw_window_handle::{HasRawWindowHandle, AppKitHandle};
3
3
use core_graphics:: base:: { kCGBitmapByteOrder32Little, kCGImageAlphaNoneSkipFirst, kCGRenderingIntentDefault} ;
4
4
use core_graphics:: color_space:: CGColorSpace ;
5
5
use core_graphics:: data_provider:: CGDataProvider ;
6
- use core_graphics:: geometry:: CGSize ;
7
6
use core_graphics:: image:: CGImage ;
8
7
9
- use cocoa:: base:: id;
8
+ use cocoa:: base:: { id, nil} ;
9
+ use cocoa:: appkit:: { NSView , NSViewWidthSizable , NSViewHeightSizable } ;
10
10
use cocoa:: quartzcore:: { CALayer , ContentsGravity } ;
11
11
use foreign_types:: ForeignType ;
12
12
13
+ use std:: sync:: Arc ;
14
+
13
15
pub struct CGImpl {
14
16
layer : CALayer ,
15
17
}
@@ -18,19 +20,25 @@ impl CGImpl {
18
20
pub unsafe fn new < W : HasRawWindowHandle > ( handle : AppKitHandle ) -> Result < Self , SoftBufferError < W > > {
19
21
let view = handle. ns_view as id ;
20
22
let layer = CALayer :: new ( ) ;
23
+ let subview: id = NSView :: alloc ( nil) . initWithFrame_ ( view. frame ( ) ) ;
21
24
layer. set_contents_gravity ( ContentsGravity :: TopLeft ) ;
22
- let _: ( ) = msg_send ! [ view, setLayer: layer. clone( ) ] ;
25
+ layer. set_needs_display_on_bounds_change ( false ) ;
26
+ subview. setLayer ( layer. id ( ) ) ;
27
+ subview. setAutoresizingMask_ ( NSViewWidthSizable | NSViewHeightSizable ) ;
28
+
29
+ view. addSubview_ ( subview) ; // retains subview (+1) = 2
30
+ let _: ( ) = msg_send ! [ subview, release] ; // releases subview (-1) = 1
23
31
Ok ( Self { layer} )
24
32
}
25
33
}
26
34
27
35
impl GraphicsContextImpl for CGImpl {
28
36
unsafe fn set_buffer ( & mut self , buffer : & [ u32 ] , width : u16 , height : u16 ) {
29
37
let color_space = CGColorSpace :: create_device_rgb ( ) ;
30
- let slice = std:: slice:: from_raw_parts (
38
+ let data = std:: slice:: from_raw_parts (
31
39
buffer. as_ptr ( ) as * const u8 ,
32
- buffer. len ( ) * 4 ) ;
33
- let data_provider = CGDataProvider :: from_slice ( slice ) ;
40
+ buffer. len ( ) * 4 ) . to_vec ( ) ;
41
+ let data_provider = CGDataProvider :: from_buffer ( Arc :: new ( data ) ) ;
34
42
let image = CGImage :: new (
35
43
width as usize ,
36
44
height as usize ,
@@ -43,17 +51,6 @@ impl GraphicsContextImpl for CGImpl {
43
51
false ,
44
52
kCGRenderingIntentDefault,
45
53
) ;
46
-
47
- let size = CGSize :: new ( width as f64 , height as f64 ) ;
48
- let rep: id = msg_send ! [ class!( NSCGImageRep ) , alloc] ;
49
- let rep: id = msg_send ! [ rep, initWithCGImage: image. as_ptr( ) size: size] ;
50
-
51
- let nsimage: id = msg_send ! [ class!( NSImage ) , alloc] ;
52
- let nsimage: id = msg_send ! [ nsimage, initWithSize: size] ;
53
- let _: ( ) = msg_send ! [ nsimage, addRepresentation: rep] ;
54
- let _: ( ) = msg_send ! [ rep, release] ;
55
-
56
- self . layer . set_contents ( nsimage) ;
57
- let _: ( ) = msg_send ! [ nsimage, release] ;
54
+ self . layer . set_contents ( image. as_ptr ( ) as id ) ;
58
55
}
59
56
}
0 commit comments