use std::sync::{Arc, Mutex};
use refineable::Refineable as _;
use crate::{
App, Bounds, Element, ElementId, GlobalElementId, InspectorElementId, IntoElement, LayoutId,
Pixels, Style, StyleRefinement, Styled, Window,
platform::surface_registry::{SurfaceId, SurfacePresentation, SurfaceRegistry},
};
struct WgpuSurfaceHandleInner {
surface_id: SurfaceId,
registry: Arc<SurfaceRegistry>,
presentation: Arc<SurfacePresentation>,
device: wgpu::Device,
queue: wgpu::Queue,
present_trigger: Arc<dyn Fn() + Send + Sync>,
size: Mutex<(u32, u32)>,
format: wgpu::TextureFormat,
}
impl Drop for WgpuSurfaceHandleInner {
fn drop(&mut self) {
self.registry.remove(self.surface_id);
}
}
#[derive(Clone, Debug)]
pub struct WgpuGpu {
pub instance: wgpu::Instance,
pub adapter: wgpu::Adapter,
pub device: wgpu::Device,
pub queue: wgpu::Queue,
}
#[derive(Clone)]
pub struct WgpuSurfaceHandle {
inner: Arc<WgpuSurfaceHandleInner>,
}
impl WgpuSurfaceHandle {
fn benchmark_mode() -> bool {
std::env::var("WGPUI_BENCHMARK").is_ok()
}
pub(crate) fn new(
device: wgpu::Device,
queue: wgpu::Queue,
surface_id: SurfaceId,
registry: Arc<SurfaceRegistry>,
present_trigger: Arc<dyn Fn() + Send + Sync>,
width: u32,
height: u32,
format: wgpu::TextureFormat,
) -> Self {
Self {
inner: Arc::new(WgpuSurfaceHandleInner {
surface_id,
presentation: registry.presentation(surface_id),
registry,
device,
queue,
present_trigger,
size: Mutex::new((width, height)),
format,
}),
}
}
pub fn device(&self) -> &wgpu::Device {
&self.inner.device
}
pub fn queue(&self) -> &wgpu::Queue {
&self.inner.queue
}
pub fn back_buffer_view(&self) -> Option<wgpu::TextureView> {
self.inner.registry.back_view(self.inner.surface_id)
}
pub fn back_view_with_size(&self) -> Option<(wgpu::TextureView, (u32, u32))> {
self.inner
.registry
.lock_and_get_back_with_size(self.inner.surface_id)
}
pub fn swap_buffers(&self) {
self.inner.registry.swap_buffers(self.inner.surface_id);
}
pub fn request_present(&self) {
if !self.is_closed() {
(self.inner.present_trigger)();
}
}
pub fn is_closed(&self) -> bool {
self.inner.presentation.is_closed()
}
pub fn present(&self) {
self.swap_buffers();
if Self::benchmark_mode() {
return;
}
if !self
.inner
.registry
.set_present_pending(self.inner.surface_id)
{
self.request_present();
}
}
pub fn size(&self) -> (u32, u32) {
*self.inner.size.lock().unwrap()
}
pub fn is_present_pending(&self) -> bool {
self.inner
.registry
.is_present_pending(self.inner.surface_id)
}
pub fn wait_for_present(&self) {
if Self::benchmark_mode() {
return;
}
self.inner.presentation.wait();
}
pub fn format(&self) -> wgpu::TextureFormat {
self.inner.format
}
pub(crate) fn id(&self) -> SurfaceId {
self.inner.surface_id
}
pub(crate) fn resize(&self, width: u32, height: u32, keep_front: bool) {
let mut size = self.inner.size.lock().unwrap();
if size.0 == width && size.1 == height {
return;
}
self.inner.registry.resize(
&self.inner.device,
self.inner.surface_id,
width,
height,
keep_front,
);
*size = (width, height);
}
}
pub fn wgpu_surface(handle: WgpuSurfaceHandle) -> WgpuSurface {
WgpuSurface {
handle,
style: StyleRefinement::default(),
on_resize: None,
hold_size: false,
keep_frame_on_resize: false,
}
}
pub struct WgpuSurface {
handle: WgpuSurfaceHandle,
style: StyleRefinement,
on_resize: Option<Box<dyn Fn(u32, u32, &WgpuSurfaceHandle) + 'static>>,
hold_size: bool,
keep_frame_on_resize: bool,
}
impl WgpuSurface {
pub fn hold_size(mut self, hold: bool) -> Self {
self.hold_size = hold;
self
}
pub fn keep_frame_on_resize(mut self, keep: bool) -> Self {
self.keep_frame_on_resize = keep;
self
}
pub fn on_resize(mut self, callback: impl Fn(u32, u32, &WgpuSurfaceHandle) + 'static) -> Self {
self.on_resize = Some(Box::new(callback));
self
}
}
fn resize_to(hold: bool, bounds: (u32, u32), current: (u32, u32)) -> Option<(u32, u32)> {
(!hold && bounds != current).then_some(bounds)
}
impl Element for WgpuSurface {
type RequestLayoutState = Style;
type PrepaintState = ();
fn id(&self) -> Option<ElementId> {
None
}
fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
None
}
fn request_layout(
&mut self,
_id: Option<&GlobalElementId>,
_inspector_id: Option<&InspectorElementId>,
window: &mut Window,
cx: &mut App,
) -> (LayoutId, Self::RequestLayoutState) {
let mut style = Style::default();
style.refine(&self.style);
let layout_id = window.request_layout(style.clone(), [], cx);
(layout_id, style)
}
fn prepaint(
&mut self,
_id: Option<&GlobalElementId>,
_inspector_id: Option<&InspectorElementId>,
bounds: Bounds<Pixels>,
_request_layout: &mut Self::RequestLayoutState,
window: &mut Window,
_cx: &mut App,
) -> Self::PrepaintState {
let scale = window.scale_factor();
let pixel_w = (bounds.size.width.0 * scale).round() as u32;
let pixel_h = (bounds.size.height.0 * scale).round() as u32;
if let Some((pixel_w, pixel_h)) =
resize_to(self.hold_size, (pixel_w, pixel_h), self.handle.size())
{
self.handle
.resize(pixel_w, pixel_h, self.keep_frame_on_resize);
if let Some(cb) = &self.on_resize {
cb(pixel_w, pixel_h, &self.handle);
}
}
}
fn paint(
&mut self,
_id: Option<&GlobalElementId>,
_inspector_id: Option<&InspectorElementId>,
bounds: Bounds<Pixels>,
style: &mut Self::RequestLayoutState,
_prepaint: &mut Self::PrepaintState,
window: &mut Window,
cx: &mut App,
) {
style.paint(bounds, window, cx, |window, _cx| {
window.paint_wgpu_surface(bounds, self.handle.id());
});
}
}
impl IntoElement for WgpuSurface {
type Element = Self;
fn into_element(self) -> Self::Element {
self
}
}
impl Styled for WgpuSurface {
fn style(&mut self) -> &mut StyleRefinement {
&mut self.style
}
}
#[cfg(test)]
mod tests {
use super::resize_to;
#[test]
fn hold_size_skips_the_resize() {
assert_eq!(resize_to(false, (200, 100), (100, 50)), Some((200, 100)));
assert_eq!(resize_to(false, (100, 50), (100, 50)), None);
assert_eq!(resize_to(true, (200, 100), (100, 50)), None);
}
}