Add raw window handle implementations to GPUI (#7101)

This is in preparation for experiments with wgpu. This should have no
external effect.

Release Notes:

- N/A
This commit is contained in:
Mikayla Maki 2024-01-30 11:42:28 -08:00 committed by GitHub
parent 1d794dbb37
commit a54eaaecee
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 60 additions and 6 deletions

9
Cargo.lock generated
View file

@ -3129,6 +3129,7 @@ dependencies = [
"png", "png",
"postage", "postage",
"rand 0.8.5", "rand 0.8.5",
"raw-window-handle 0.6.0",
"refineable", "refineable",
"resvg", "resvg",
"schemars", "schemars",
@ -4495,7 +4496,7 @@ dependencies = [
"jni-sys", "jni-sys",
"ndk-sys", "ndk-sys",
"num_enum", "num_enum",
"raw-window-handle", "raw-window-handle 0.5.2",
"thiserror", "thiserror",
] ]
@ -5953,6 +5954,12 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2ff9a1f06a88b01621b7ae906ef0211290d1c8a168a15542486a8f61c0833b9" checksum = "f2ff9a1f06a88b01621b7ae906ef0211290d1c8a168a15542486a8f61c0833b9"
[[package]]
name = "raw-window-handle"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42a9830a0e1b9fb145ebb365b8bc4ccd75f290f98c0247deafbbe2c75cefb544"
[[package]] [[package]]
name = "rawpointer" name = "rawpointer"
version = "0.2.1" version = "0.2.1"

View file

@ -58,6 +58,7 @@ slotmap = "1.0.6"
schemars.workspace = true schemars.workspace = true
bitflags = "2.4.0" bitflags = "2.4.0"
anyhow.workspace = true anyhow.workspace = true
raw-window-handle = "0.6.0"
[dev-dependencies] [dev-dependencies]
backtrace = "0.3" backtrace = "0.3"

View file

@ -8,13 +8,14 @@ mod test;
use crate::{ use crate::{
Action, AnyWindowHandle, AsyncWindowContext, BackgroundExecutor, Bounds, DevicePixels, Font, Action, AnyWindowHandle, AsyncWindowContext, BackgroundExecutor, Bounds, DevicePixels, Font,
FontId, FontMetrics, FontRun, ForegroundExecutor, GlobalPixels, GlyphId, Keymap, LineLayout, FontId, FontMetrics, FontRun, ForegroundExecutor, GlobalPixels, GlyphId, Keymap, LineLayout,
Pixels, PlatformInput, Point, RenderGlyphParams, RenderImageParams, RenderSvgParams, Result, Pixels, PlatformInput, Point, RenderGlyphParams, RenderImageParams, RenderSvgParams, Scene,
Scene, SharedString, Size, Task, TaskLabel, WindowContext, SharedString, Size, Task, TaskLabel, WindowContext,
}; };
use anyhow::anyhow; use anyhow::{anyhow, Result};
use async_task::Runnable; use async_task::Runnable;
use futures::channel::oneshot; use futures::channel::oneshot;
use parking::Unparker; use parking::Unparker;
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
use seahash::SeaHasher; use seahash::SeaHasher;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::borrow::Cow; use std::borrow::Cow;
@ -138,7 +139,7 @@ impl Debug for DisplayId {
unsafe impl Send for DisplayId {} unsafe impl Send for DisplayId {}
pub(crate) trait PlatformWindow { pub(crate) trait PlatformWindow: HasWindowHandle + HasDisplayHandle {
fn bounds(&self) -> WindowBounds; fn bounds(&self) -> WindowBounds;
fn content_size(&self) -> Size<Pixels>; fn content_size(&self) -> Size<Pixels>;
fn scale_factor(&self) -> f32; fn scale_factor(&self) -> f32;

View file

@ -32,6 +32,10 @@ use objc::{
sel, sel_impl, sel, sel_impl,
}; };
use parking_lot::Mutex; use parking_lot::Mutex;
use raw_window_handle::{
AppKitDisplayHandle, AppKitWindowHandle, DisplayHandle, HasDisplayHandle, HasWindowHandle,
RawWindowHandle, WindowHandle,
};
use smallvec::SmallVec; use smallvec::SmallVec;
use std::{ use std::{
any::Any, any::Any,
@ -41,7 +45,7 @@ use std::{
ops::Range, ops::Range,
os::raw::c_char, os::raw::c_char,
path::PathBuf, path::PathBuf,
ptr, ptr::{self, NonNull},
rc::Rc, rc::Rc,
sync::{Arc, Weak}, sync::{Arc, Weak},
time::Duration, time::Duration,
@ -316,6 +320,7 @@ struct MacWindowState {
handle: AnyWindowHandle, handle: AnyWindowHandle,
executor: ForegroundExecutor, executor: ForegroundExecutor,
native_window: id, native_window: id,
native_view: NonNull<id>,
renderer: MetalRenderer, renderer: MetalRenderer,
kind: WindowKind, kind: WindowKind,
request_frame_callback: Option<Box<dyn FnMut()>>, request_frame_callback: Option<Box<dyn FnMut()>>,
@ -523,6 +528,7 @@ impl MacWindow {
handle, handle,
executor, executor,
native_window, native_window,
native_view: NonNull::new_unchecked(native_view as *mut _),
renderer: MetalRenderer::new(true), renderer: MetalRenderer::new(true),
kind: options.kind, kind: options.kind,
request_frame_callback: None, request_frame_callback: None,
@ -1011,6 +1017,28 @@ impl PlatformWindow for MacWindow {
} }
} }
impl HasWindowHandle for MacWindow {
fn window_handle(
&self,
) -> Result<raw_window_handle::WindowHandle<'_>, raw_window_handle::HandleError> {
// SAFETY: The AppKitWindowHandle is a wrapper around a pointer to an NSView
unsafe {
Ok(WindowHandle::borrow_raw(RawWindowHandle::AppKit(
AppKitWindowHandle::new(self.0.lock().native_view.cast()),
)))
}
}
}
impl HasDisplayHandle for MacWindow {
fn display_handle(
&self,
) -> Result<raw_window_handle::DisplayHandle<'_>, raw_window_handle::HandleError> {
// SAFETY: This is a no-op on macOS
unsafe { Ok(DisplayHandle::borrow_raw(AppKitDisplayHandle::new().into())) }
}
}
fn get_scale_factor(native_window: id) -> f32 { fn get_scale_factor(native_window: id) -> f32 {
let factor = unsafe { let factor = unsafe {
let screen: id = msg_send![native_window, screen]; let screen: id = msg_send![native_window, screen];

View file

@ -5,6 +5,7 @@ use crate::{
}; };
use collections::HashMap; use collections::HashMap;
use parking_lot::Mutex; use parking_lot::Mutex;
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
use std::{ use std::{
rc::{Rc, Weak}, rc::{Rc, Weak},
sync::{self, Arc}, sync::{self, Arc},
@ -29,6 +30,22 @@ pub(crate) struct TestWindowState {
#[derive(Clone)] #[derive(Clone)]
pub(crate) struct TestWindow(pub(crate) Arc<Mutex<TestWindowState>>); pub(crate) struct TestWindow(pub(crate) Arc<Mutex<TestWindowState>>);
impl HasWindowHandle for TestWindow {
fn window_handle(
&self,
) -> Result<raw_window_handle::WindowHandle<'_>, raw_window_handle::HandleError> {
unimplemented!("Test Windows are not backed by a real platform window")
}
}
impl HasDisplayHandle for TestWindow {
fn display_handle(
&self,
) -> Result<raw_window_handle::DisplayHandle<'_>, raw_window_handle::HandleError> {
unimplemented!("Test Windows are not backed by a real platform window")
}
}
impl TestWindow { impl TestWindow {
pub fn new( pub fn new(
options: WindowOptions, options: WindowOptions,