11use crate :: context:: paths:: path:: Path ;
22use crate :: context:: text_styles:: text_direction:: TextDirection ;
33use crate :: context:: { Context , State , SurfaceData , SurfaceEngine , SurfaceState } ;
4+ use canvas_core:: context_attributes:: ColorSpace ;
45use canvas_core:: gpu:: metal:: MetalContext ;
56use foreign_types_shared:: ForeignTypeRef ;
7+ use objc2:: rc:: Retained ;
8+ use objc2_metal:: MTLTexture ;
9+ use objc2_quartz_core:: CAMetalDrawable ;
610use skia_safe:: gpu:: mtl:: TextureInfo ;
711use skia_safe:: { gpu, Color , ColorType } ;
812use std:: os:: raw:: c_void;
9- use canvas_core:: context_attributes:: ColorSpace ;
1013
1114#[ cfg( feature = "metal" ) ]
1215impl Context {
@@ -35,19 +38,19 @@ impl Context {
3538 None ,
3639 ) {
3740 surface. canvas ( ) . draw_color ( Color :: RED , None ) ;
38-
41+
3942 // surface
4043 // .canvas()
4144 // .draw_image(snapshot, skia_safe::Point::new(0.0, 0.0), None);
42- context. flush_surface ( & mut surface) ;
43- context. flush_submit_and_sync_cpu ( ) ;
45+ context. flush_surface ( & mut surface) ;
46+ context. flush_submit_and_sync_cpu ( ) ;
4447
4548 return true ;
4649 }
4750 }
4851 false
4952 }
50-
53+
5154 pub fn new_metal (
5255 view : * mut c_void ,
5356 density : f32 ,
@@ -56,7 +59,7 @@ impl Context {
5659 font_color : i32 ,
5760 ppi : f32 ,
5861 direction : TextDirection ,
59- color_space : ColorSpace
62+ color_space : ColorSpace ,
6063 ) -> Self {
6164 let mtl_context = MetalContext :: new ( view) ;
6265 let backend = unsafe {
@@ -71,7 +74,8 @@ impl Context {
7174 let mut context = gpu:: direct_contexts:: make_metal ( & backend, None ) . unwrap ( ) ;
7275
7376 let drawable = mtl_context. drawable ( ) . unwrap ( ) ;
74- let info = unsafe { TextureInfo :: new ( drawable. texture ( ) . as_ptr ( ) as gpu:: mtl:: Handle ) } ;
77+ let info =
78+ unsafe { TextureInfo :: new ( Retained :: as_ptr ( & drawable. texture ( ) ) as gpu:: mtl:: Handle ) } ;
7579 let bt = unsafe {
7680 gpu:: backend_textures:: make_mtl (
7781 ( width as i32 , height as i32 ) ,
@@ -103,7 +107,7 @@ impl Context {
103107 engine : SurfaceEngine :: Metal ,
104108 state : Default :: default ( ) ,
105109 is_opaque : !alpha,
106- color_space
110+ color_space,
107111 } ,
108112 surface,
109113 surface_state : Default :: default ( ) ,
@@ -134,7 +138,7 @@ impl Context {
134138 font_color : i32 ,
135139 ppi : f32 ,
136140 direction : TextDirection ,
137- color_space : ColorSpace
141+ color_space : ColorSpace ,
138142 ) -> Self {
139143 let mut mtl_context = unsafe { MetalContext :: new_device_queue ( view, device, queue) } ;
140144 let backend = unsafe {
@@ -144,7 +148,7 @@ impl Context {
144148
145149 let mut context = gpu:: direct_contexts:: make_metal ( & backend, None ) . unwrap ( ) ;
146150 let drawable = mtl_context. current_drawable ( ) . unwrap ( ) ;
147- let info = unsafe { TextureInfo :: new ( drawable. texture ( ) . as_ptr ( ) as gpu:: mtl:: Handle ) } ;
151+ let info = unsafe { TextureInfo :: new ( Retained :: as_ptr ( & drawable. texture ( ) ) as gpu:: mtl:: Handle ) } ;
148152 let bt = unsafe {
149153 gpu:: backend_textures:: make_mtl (
150154 ( width as i32 , height as i32 ) ,
@@ -176,7 +180,7 @@ impl Context {
176180 engine : SurfaceEngine :: Metal ,
177181 state : Default :: default ( ) ,
178182 is_opaque : !alpha,
179- color_space
183+ color_space,
180184 } ,
181185 surface,
182186 surface_state : Default :: default ( ) ,
@@ -206,7 +210,7 @@ impl Context {
206210 font_color : i32 ,
207211 ppi : f32 ,
208212 direction : TextDirection ,
209- color_space : ColorSpace
213+ color_space : ColorSpace ,
210214 ) -> Self {
211215 let mtl_context = MetalContext :: new_offscreen ( width, height) ;
212216 let backend = unsafe {
@@ -221,7 +225,7 @@ impl Context {
221225 let mut context = gpu:: direct_contexts:: make_metal ( & backend, None ) . unwrap ( ) ;
222226
223227 let drawable = mtl_context. drawable ( ) . unwrap ( ) ;
224- let info = unsafe { TextureInfo :: new ( drawable. texture ( ) . as_ptr ( ) as gpu:: mtl:: Handle ) } ;
228+ let info = unsafe { TextureInfo :: new ( Retained :: as_ptr ( & drawable. texture ( ) ) as gpu:: mtl:: Handle ) } ;
225229 let bt = unsafe {
226230 gpu:: backend_textures:: make_mtl (
227231 ( width as i32 , height as i32 ) ,
@@ -253,7 +257,7 @@ impl Context {
253257 engine : SurfaceEngine :: Metal ,
254258 state : Default :: default ( ) ,
255259 is_opaque : !alpha,
256- color_space
260+ color_space,
257261 } ,
258262 surface,
259263 surface_state : Default :: default ( ) ,
@@ -293,7 +297,7 @@ impl Context {
293297 if let Some ( drawable) = context. next_drawable ( ) {
294298 info = unsafe {
295299 Some ( TextureInfo :: new (
296- drawable. texture ( ) . as_ptr ( ) as gpu:: mtl:: Handle
300+ Retained :: as_ptr ( & drawable. texture ( ) ) as gpu:: mtl:: Handle
297301 ) )
298302 } ;
299303 }
@@ -357,7 +361,7 @@ impl Context {
357361 let texture = drawable. texture ( ) ;
358362 width = texture. width ( ) ;
359363 height = texture. height ( ) ;
360- info = unsafe { Some ( TextureInfo :: new ( texture . as_ptr ( ) as gpu:: mtl:: Handle ) ) } ;
364+ info = unsafe { Some ( TextureInfo :: new ( Retained :: as_ptr ( & drawable . texture ( ) ) as gpu:: mtl:: Handle ) ) } ;
361365 }
362366 }
363367
0 commit comments