nsh_core/gpu.rs
1//! GPU views: views whose box a renderer of the app's own draws with the GPU
2//! (`GpuView` of `nsh`, Rust [`crate::element::gpu_view`]), each frame on a
3//! thread of its own, composited by the platform under the view's children
4//! as a shader's frames are.
5//!
6//! The renderers themselves (wgpu code) are the `nsh-gpu` crate's
7//! `GpuRenderer`s; the core never names them. It carries a view's renderer
8//! as a [`GpuSource`], an opaque handle `nsh-gpu` makes and reads back:
9//! one a Rust element set, or one a UI set registered under a name
10//! ([`crate::element::Registry::gpu_renderer_source`]), which TSX names
11//! (`gpuView("gallery/scene", …)`). The view's own props, animated props
12//! and events are a native component's ([`crate::Prop::ComponentProp`],
13//! [`crate::Prop::ComponentPropBinding`], `onNativeEvent`), sent with
14//! [`crate::MountItem::ComponentProps`] and
15//! [`crate::MountItem::ComponentPropSlots`]; the renderer arrives with
16//! [`crate::MountItem::GpuView`].
17
18use std::{any::Any, fmt, sync::Arc};
19
20/// What makes a GPU view's renderer: opaque to the core, made and read by
21/// `nsh-gpu`. Cheap to clone; two sources are equal when they are the
22/// same one.
23#[derive(Clone)]
24pub struct GpuSource(Arc<dyn Any + Send + Sync>);
25
26impl GpuSource {
27 /// Wraps `inner` (`nsh-gpu`'s factory).
28 pub fn new(inner: impl Any + Send + Sync) -> Self {
29 GpuSource(Arc::new(inner))
30 }
31
32 /// What [`Self::new`] wrapped, when it is a `T`.
33 pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
34 self.0.downcast_ref()
35 }
36}
37
38impl PartialEq for GpuSource {
39 fn eq(&self, other: &Self) -> bool {
40 Arc::ptr_eq(&self.0, &other.0)
41 }
42}
43
44impl fmt::Debug for GpuSource {
45 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46 f.write_str("GpuSource")
47 }
48}