Skip to content

Commit

Permalink
app-surface: update_device_pixel_ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
jinleili committed Nov 23, 2024
1 parent daab811 commit 5626a80
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

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, Android and Web apps without relying on winit."
edition = "2021"
version = "1.3.1"
version = "1.3.2"
rust-version = "1.76"
repository = "https://github.com/jinleili/wgpu-in-app"
keywords = ["android", "SurfaceView", "CAMetalLayer", "Canvas", "wgpu"]
Expand Down
11 changes: 10 additions & 1 deletion app-surface/src/web_rwh/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use super::SendSyncWrapper;
use raw_window_handle::{
HasDisplayHandle, HasWindowHandle, RawWindowHandle, WebCanvasWindowHandle, WindowHandle,
};
use std::{ops::Deref, ptr::NonNull};
use std::{
ops::{Deref, DerefMut},
ptr::NonNull,
};
use wasm_bindgen::{JsCast, JsValue};

#[derive(Debug)]
Expand All @@ -21,6 +24,12 @@ impl Deref for CanvasWrapper {
}
}

impl DerefMut for CanvasWrapper {
fn deref_mut(&mut self) -> &mut Canvas {
&mut self.0 .0
}
}

#[derive(Debug, Clone)]
pub struct Canvas {
pub(crate) element: web_sys::HtmlCanvasElement,
Expand Down
25 changes: 23 additions & 2 deletions app-surface/src/web_rwh/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,18 @@ impl AppSurface {
}

/// 用 OffscreenCanvas 创建 AppSurface
///
///
/// handle: 用于 WebGPU 的 raw handle number, 0 是保留的值, 不能使用
pub async fn from_offscreen_canvas(
offscreen_canvas: web_sys::OffscreenCanvas,
scale_factor: f32,
handle: u32,
) -> Self {
let wrapper = OffscreenCanvasWrapper::new(OffscreenCanvas::new(offscreen_canvas, scale_factor, handle));
let wrapper = OffscreenCanvasWrapper::new(OffscreenCanvas::new(
offscreen_canvas,
scale_factor,
handle,
));
Self::new(ViewObj::Offscreen(wrapper)).await
}

Expand All @@ -89,6 +93,23 @@ impl AppSurface {
(logical_size.1 as f32 * scale_factor) as u32,
)
}

pub fn get_view_logical_size(&self) -> (u32, u32) {
let (_, logical_size) = match self.view {
ViewObj::Canvas(ref canvas) => (canvas.scale_factor, canvas.logical_resolution()),
ViewObj::Offscreen(ref offscreen) => {
(offscreen.scale_factor, offscreen.logical_resolution())
}
};
logical_size
}

pub fn update_device_pixel_ratio(&mut self, ratio: f32) {
match self.view {
ViewObj::Canvas(ref mut canvas) => canvas.scale_factor = ratio,
ViewObj::Offscreen(ref mut offscreen) => offscreen.scale_factor = ratio,
}
}
}

// 封装 ViewObj 来同时支持 Canvas 与 Offscreen
Expand Down
11 changes: 10 additions & 1 deletion app-surface/src/web_rwh/offscreen_canvas.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use super::{Canvas, SendSyncWrapper};
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
use std::{ops::Deref, ptr::NonNull};
use std::{
ops::{Deref, DerefMut},
ptr::NonNull,
};
use wasm_bindgen::JsValue;

#[derive(Debug)]
Expand All @@ -19,6 +22,12 @@ impl Deref for OffscreenCanvasWrapper {
}
}

impl DerefMut for OffscreenCanvasWrapper {
fn deref_mut(&mut self) -> &mut OffscreenCanvas {
&mut self.0 .0
}
}

#[derive(Debug, Clone)]
pub struct OffscreenCanvas {
inner: web_sys::OffscreenCanvas,
Expand Down

0 comments on commit 5626a80

Please sign in to comment.