Skip to content

Commit

Permalink
Update to wgpu 0.19
Browse files Browse the repository at this point in the history
  • Loading branch information
jinleili committed Jan 18, 2024
1 parent e144c3f commit bb73f49
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 51 deletions.
43 changes: 25 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@ log = "0.4"
noise = { version = "0.8", default-features = false }
pollster = "0.3"
rand = "0.7.2"
# wgpu = "0.18"
wgpu = { git = "https://github.com/gfx-rs/wgpu", rev = "445fa6019b47079c9d336881dbee1c3be3ed4c38" }
# wgpu = { path = "../../forks/wgpu/wgpu" }
wgpu = "0.19"
# wgpu = { git = "https://github.com/gfx-rs/wgpu", rev = "445fa6019b47079c9d336881dbee1c3be3ed4c38" }
# wgpu = { git = "https://github.com/jinleili/wgpu", branch="visionOS" }

async-executor = "1.6"
# winit = "0.28.7"
winit = { version = "0.29.10" }
raw-window-handle = "0.6"
env_logger = "0.10"
Expand Down
2 changes: 1 addition & 1 deletion app-surface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "app-surface"
authors = ["jinleili"]
description = "Integrate wgpu into your existing iOS and Android apps."
edition = "2021"
version = "0.3.5"
version = "0.4.0"
rust-version = "1.65"
repository = "https://github.com/jinleili/wgpu-in-app"
keywords = ["android", "SurfaceView", "ios", "app", "wgpu"]
Expand Down
61 changes: 40 additions & 21 deletions app-surface/src/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use core::ffi::c_void;
use jni::sys::jobject;
use jni::JNIEnv;
use raw_window_handle::{
AndroidDisplayHandle, AndroidNdkWindowHandle, HasRawDisplayHandle, HasRawWindowHandle,
RawDisplayHandle, RawWindowHandle,
AndroidDisplayHandle, AndroidNdkWindowHandle, DisplayHandle, HandleError, HasDisplayHandle,
HasWindowHandle, RawDisplayHandle, RawWindowHandle, WindowHandle,
};
use std::sync::Arc;
use std::sync::{Arc, Mutex};

pub struct AppSurface {
pub native_window: NativeWindow,
pub native_window: Arc<NativeWindow>,
pub scale_factor: f32,
pub sdq: crate::SurfaceDeviceQueue,
pub instance: wgpu::Instance,
Expand All @@ -17,13 +17,16 @@ pub struct AppSurface {

impl AppSurface {
pub fn new(env: *mut JNIEnv, surface: jobject) -> Self {
let native_window = NativeWindow::new(env, surface);
let native_window = Arc::new(NativeWindow::new(env, surface));
let backends = wgpu::Backends::VULKAN;
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends,
..Default::default()
});
let surface = unsafe { instance.create_surface(&native_window).unwrap() };
let handle: Box<dyn wgpu::WindowHandle> = Box::new(native_window.clone());
let surface = instance
.create_surface(wgpu::SurfaceTarget::Window(handle))
.unwrap();
let (adapter, device, queue) =
pollster::block_on(crate::request_device(&instance, &surface));

Expand All @@ -38,6 +41,7 @@ impl AppSurface {
present_mode: wgpu::PresentMode::Fifo,
alpha_mode: caps.alpha_modes[0],
view_formats: vec![],
desired_maximum_frame_latency: 2,
};
surface.configure(&device, &config);

Expand Down Expand Up @@ -65,7 +69,7 @@ impl AppSurface {
}

pub struct NativeWindow {
a_native_window: *mut ndk_sys::ANativeWindow,
a_native_window: Arc<Mutex<*mut ndk_sys::ANativeWindow>>,
}

impl NativeWindow {
Expand All @@ -75,41 +79,56 @@ impl NativeWindow {
// 此函数在返回 ANativeWindow 的同时会自动将其引用计数 +1,以防止该对象在安卓端被意外释放。
ndk_sys::ANativeWindow_fromSurface(env as *mut _, surface as *mut _)
};
Self { a_native_window }
Self {
a_native_window: Arc::new(Mutex::new(a_native_window)),
}
}

pub fn get_raw_window(&self) -> *mut ndk_sys::ANativeWindow {
self.a_native_window
let a_native_window = self.a_native_window.lock().unwrap();
*a_native_window
}

fn get_width(&self) -> u32 {
unsafe { ndk_sys::ANativeWindow_getWidth(self.a_native_window) as u32 }
unsafe { ndk_sys::ANativeWindow_getWidth(*self.a_native_window.lock().unwrap()) as u32 }
}

fn get_height(&self) -> u32 {
unsafe { ndk_sys::ANativeWindow_getHeight(self.a_native_window) as u32 }
unsafe { ndk_sys::ANativeWindow_getHeight(*self.a_native_window.lock().unwrap()) as u32 }
}
}

impl Drop for NativeWindow {
fn drop(&mut self) {
unsafe {
ndk_sys::ANativeWindow_release(self.a_native_window);
ndk_sys::ANativeWindow_release(*self.a_native_window.lock().unwrap());
}
}
}

unsafe impl HasRawWindowHandle for NativeWindow {
fn raw_window_handle(&self) -> RawWindowHandle {
let mut handle = AndroidNdkWindowHandle::new(NonNull::new(
self.a_native_window as *mut _ as *mut c_void,
));
RawWindowHandle::AndroidNdk(handle)
impl HasWindowHandle for NativeWindow {
fn window_handle(&self) -> Result<WindowHandle, HandleError> {
unsafe {
let a_native_window = self.a_native_window.lock().unwrap();
let handle = AndroidNdkWindowHandle::new(
std::ptr::NonNull::new(*a_native_window as *mut _ as *mut c_void).unwrap(),
);
Ok(WindowHandle::borrow_raw(RawWindowHandle::AndroidNdk(
handle,
)))
}
}
}

unsafe impl HasRawDisplayHandle for NativeWindow {
fn raw_display_handle(&self) -> RawDisplayHandle {
RawDisplayHandle::Android(AndroidDisplayHandle::new())
impl HasDisplayHandle for NativeWindow {
fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
unsafe {
Ok(DisplayHandle::borrow_raw(RawDisplayHandle::Android(
AndroidDisplayHandle::new(),
)))
}
}
}

unsafe impl Send for NativeWindow {}
unsafe impl Sync for NativeWindow {}
7 changes: 4 additions & 3 deletions app-surface/src/app_surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ impl AppSurface {
if #[cfg(target_arch = "wasm32")] {
let surface = if is_offscreen_canvas {
// let offscreen = canvas.transfer_control_to_offscreen().unwrap();
instance.create_surface_from_offscreen_canvas(
view_setting.offscreen_canvas.unwrap(),
instance.create_surface(
wgpu::SurfaceTarget::OffscreenCanvas(view_setting.offscreen_canvas.unwrap())
)
} else {
use winit::platform::web::WindowExtWebSys;
let canvas: web_sys::HtmlCanvasElement =
view.as_ref().canvas().unwrap();
instance.create_surface_from_canvas(canvas)
instance.create_surface(wgpu::SurfaceTarget::Canvas(canvas))
};
} else {
let surface = instance.create_surface(view.clone());
Expand Down Expand Up @@ -139,6 +139,7 @@ impl AppSurface {
present_mode: wgpu::PresentMode::Fifo,
alpha_mode,
view_formats,
desired_maximum_frame_latency: 2,
};
surface.configure(&device, &config);

Expand Down
1 change: 1 addition & 0 deletions app-surface/src/ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ impl AppSurface {
present_mode: wgpu::PresentMode::Fifo,
alpha_mode: wgpu::CompositeAlphaMode::PostMultiplied,
view_formats: vec![],
desired_maximum_frame_latency: 2,
};
surface.configure(&device, &config);
AppSurface {
Expand Down
6 changes: 3 additions & 3 deletions wgpu-in-app/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "wgpu-in-app"
authors = ["jinleili"]
version = "0.2.3"
version = "0.3.0"
edition = "2021"
rust-version = "1.65"

Expand Down Expand Up @@ -165,8 +165,8 @@ web-sys = { workspace = true, features = [
"OffscreenCanvas",
"ImageBitmap",
"ImageBitmapRenderingContext",
"Window"
"Window",
] }
wasm-bindgen.workspace = true
js-sys.workspace = true
wasm-bindgen-futures.workspace = true
wasm-bindgen-futures.workspace = true

0 comments on commit bb73f49

Please sign in to comment.