From dc36f1025af29281144a7d34cf9b91bfcbfbf89c Mon Sep 17 00:00:00 2001 From: Jinlei Li Date: Thu, 2 May 2024 11:32:19 +0800 Subject: [PATCH] Test CAMetalLayer's setPresentsWithTransaction --- wgpu-in-app/Cargo.toml | 3 +++ wgpu-in-app/src/wgpu_canvas.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/wgpu-in-app/Cargo.toml b/wgpu-in-app/Cargo.toml index ae08e7f..ffab1c3 100644 --- a/wgpu-in-app/Cargo.toml +++ b/wgpu-in-app/Cargo.toml @@ -26,6 +26,9 @@ pollster.workspace = true rand.workspace = true wgpu.workspace = true +[target.'cfg(target_os = "macos")'.dependencies] +objc = "*" + [target.'cfg(any(target_os = "macos", target_os = "windows", target_os = "linux"))'.dependencies] winit.workspace = true diff --git a/wgpu-in-app/src/wgpu_canvas.rs b/wgpu-in-app/src/wgpu_canvas.rs index 9e84034..8f35a1a 100644 --- a/wgpu-in-app/src/wgpu_canvas.rs +++ b/wgpu-in-app/src/wgpu_canvas.rs @@ -1,5 +1,12 @@ use crate::examples::*; use app_surface::{AppSurface, SurfaceFrame}; +#[cfg(target_os = "macos")] +use objc::{ + class, msg_send, + runtime::{Object, BOOL, YES}, + sel, sel_impl, +}; +use wgpu::rwh::{HasWindowHandle, RawWindowHandle}; pub struct WgpuCanvas { pub app_surface: AppSurface, @@ -9,8 +16,29 @@ pub struct WgpuCanvas { #[allow(dead_code)] impl WgpuCanvas { pub fn new(app_surface: AppSurface, idx: i32) -> Self { + let mut app_surface = app_surface; + #[cfg(target_os = "macos")] + { + if let Some(v) = app_surface.view.as_mut() { + match v.window_handle().unwrap().as_raw() { + RawWindowHandle::AppKit(handle) => { + let view = handle.ns_view.as_ptr() as *mut Object; + let class = class!(CAMetalLayer); + unsafe { + let metal_layer: *mut Object = msg_send![view, layer]; + let is_metal_layer: BOOL = msg_send![metal_layer, isKindOfClass: class]; + if is_metal_layer == YES { + let () = msg_send![metal_layer, setPresentsWithTransaction: YES]; + } + } + } + _ => (), + } + } + } let example = Box::new(Empty::new(&app_surface)); log::info!("example created"); + let mut instance = WgpuCanvas { app_surface, example,