Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Upgrade raw-window-handle to 0.6.0 #8

Merged
merged 1 commit into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ categories = ["game-engines", "graphics"]
exclude = [".github/*"]

[dependencies]
raw-window-handle = "0.5.0"
raw-window-handle = "0.6.0"
objc = "0.2"

[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
Expand Down
24 changes: 6 additions & 18 deletions src/appkit.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
use crate::{CAMetalLayer, Layer};
use core::{ffi::c_void, mem};
use core::ffi::c_void;
use core_graphics::{base::CGFloat, geometry::CGRect};
use objc::{
msg_send,
runtime::{Object, BOOL, YES},
runtime::{BOOL, YES},
};
use raw_window_handle::AppKitWindowHandle;
use std::ptr::NonNull;

///
pub unsafe fn metal_layer_from_handle(handle: AppKitWindowHandle) -> Layer {
if !handle.ns_view.is_null() {
metal_layer_from_ns_view(handle.ns_view)
} else if !handle.ns_window.is_null() {
metal_layer_from_ns_window(handle.ns_window)
} else {
Layer::None
}
metal_layer_from_ns_view(handle.ns_view)
}

///
pub unsafe fn metal_layer_from_ns_view(view: *mut c_void) -> Layer {
let view: cocoa::base::id = mem::transmute(view);
pub unsafe fn metal_layer_from_ns_view(view: NonNull<c_void>) -> Layer {
let view: cocoa::base::id = view.cast().as_ptr();

// Check if the view is a CAMetalLayer
let class = class!(CAMetalLayer);
Expand Down Expand Up @@ -60,10 +55,3 @@ pub unsafe fn metal_layer_from_ns_view(view: *mut c_void) -> Layer {
let _: *mut c_void = msg_send![view, retain];
render_layer
}

///
pub unsafe fn metal_layer_from_ns_window(window: *mut c_void) -> Layer {
let ns_window = window as *mut Object;
let ns_view = msg_send![ns_window, contentView];
metal_layer_from_ns_view(ns_view)
}
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(any(target_os = "macos", target_os = "ios"))]
// #![cfg(any(target_os = "macos", target_os = "ios"))]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I think I forgot to drop this even after cross-compiling/building/testing to the right target 😅

#![allow(clippy::missing_safety_doc, clippy::let_unit_value)]

#[macro_use]
Expand All @@ -14,5 +14,4 @@ pub type CAMetalLayer = *mut Object;
pub enum Layer {
Existing(CAMetalLayer),
Allocated(CAMetalLayer),
None,
}
Comment on lines 14 to 17
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In ash-window we have a pattern that takes out the CAMetalLayer. Is there any significance here (e.g. lifetimes) or can we flatten this, store existing vs allocated in a separate member, or add a helper fn value(self) -> CAMetalLayer thing?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CAMetalLayer may be allocated in case there couldn't be extracted one from the window handle, and should be destroyed if not needed anymore (on surface destruction). To not leak the consumer should execute sth like msg_send![layer, release] in case of Layer::Allocated(layer) (but couldn't test due to lack of hw)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since raw-window-metal maps this nicely, should it also implement Drop that does this for Allocated but not for Existing? Or at least add a doc-comment and perhaps make this an "implementation detail" unless transferring ownership is a thing, which we should make more explicit to the caller?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could test these things out soon™, perhaps worth doing that before cutting a new release?

25 changes: 7 additions & 18 deletions src/uikit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,22 @@ use crate::{CAMetalLayer, Layer};
use core_graphics::{base::CGFloat, geometry::CGRect};
use objc::{
msg_send,
runtime::{Object, BOOL, YES},
runtime::{BOOL, YES},
};
use raw_window_handle::UiKitWindowHandle;
use std::{ffi::c_void, mem};
use std::{ffi::c_void, ptr::NonNull};

///
pub unsafe fn metal_layer_from_handle(handle: UiKitWindowHandle) -> Layer {
if !handle.ui_view.is_null() {
metal_layer_from_ui_view(handle.ui_view)
} else if !handle.ui_window.is_null() {
metal_layer_from_ui_window(handle.ui_window)
} else {
// TODO: ui_window & ui_view_controller support
Layer::None
if let Some(_ui_view_controller) = handle.ui_view_controller {
// TODO: ui_view_controller support
}
metal_layer_from_ui_view(handle.ui_view)
}

///
pub unsafe fn metal_layer_from_ui_view(view: *mut c_void) -> Layer {
let view: cocoa::base::id = mem::transmute(view);
pub unsafe fn metal_layer_from_ui_view(view: NonNull<c_void>) -> Layer {
let view: cocoa::base::id = view.cast().as_ptr();
let main_layer: CAMetalLayer = msg_send![view, layer];

let class = class!(CAMetalLayer);
Expand Down Expand Up @@ -51,10 +47,3 @@ pub unsafe fn metal_layer_from_ui_view(view: *mut c_void) -> Layer {

render_layer
}

///
pub unsafe fn metal_layer_from_ui_window(window: *mut c_void) -> Layer {
let ui_window = window as *mut Object;
let ui_view = msg_send![ui_window, contentView];
metal_layer_from_ui_view(ui_view)
}