Skip to main content

WebGpuChartBackend

Struct WebGpuChartBackend 

Source
pub struct WebGpuChartBackend { /* private fields */ }
Expand description

The WebGPU rendering backend for the chart.

This is the backend the chart prefers: vector geometry is tessellated into triangles once, uploaded to the GPU once, and replayed with a handful of draw calls. The display list is retained between frames, so a chart that has not changed does not re-tessellate and does not re-upload anything — the backend keeps the GPU buffers alive and reuses them as long as the list is unchanged.

The backend is created asynchronously, because acquiring a GPU adapter and a GPU device returns JavaScript promises. When the host cannot provide WebGPU, WebGpuChartBackend::create returns None and the factory falls back to WebGL2 and then to Canvas2D.

Implementations§

Source§

impl WebGpuChartBackend

Inherent implementation of WebGpuChartBackend.

Source

pub fn is_supported() -> bool

Returns true when the host browser exposes WebGPU.

The check is synchronous and cheap: it only looks for navigator.gpu. It says nothing about whether an adapter can actually be acquired — headless browsers, GPU-blacklisted devices, and sandboxed frames all expose the entry point while failing to hand out a device. For the definitive answer, create the backend: WebGpuChartBackend::create asks for an adapter and a device for real, and returns None when either fails.

§Returns
  • bool - A boolean.
Source

pub async fn create(canvas: &HtmlCanvasElement) -> Option<Self>

Creates a backend around the given canvas.

The whole acquisition is asynchronous: requestAdapter and requestDevice return JavaScript promises. Every failure mode — no navigator.gpu, no adapter, no device, a canvas that already has a context of another kind — returns None, so the backend factory can fall back to WebGL2 and then to Canvas2D.

The GPU buffers are not allocated here. They are created on the first render, once the size of the glyph atlas is known, and then kept until the display list changes.

§Arguments
  • &HtmlCanvasElement - The &HtmlCanvasElement value.
§Returns
  • Option<Self> - The Option<Self> value.
Source

pub fn canvas(&self) -> &HtmlCanvasElement

Returns the canvas the backend draws into.

§Returns
  • &HtmlCanvasElement - A HtmlCanvasElement value.
Source

pub fn dpr(&self) -> f64

Returns the device pixel ratio the backend renders at.

§Returns
  • f64 - A 64-bit float.
Source

pub fn format(&self) -> &str

Returns the swap-chain texture format the backend renders into.

§Returns
  • &str - A str value.
Source

pub fn batch_count(&self) -> usize

Returns the number of draw calls the last frame used.

One batch is one draw call, so this is the number the chart pays per frame. A chart that has not changed keeps the same number.

§Returns
  • usize - A non-negative integer.
Source

pub fn invalidate(&mut self)

Forces the backend to rebuild its geometry on the next render.

The backend notices a changed display list on its own, by fingerprinting it. This is for the cases the fingerprint cannot see — a web font that finished loading, for instance, changes the metrics of the text without changing the display list.

Trait Implementations§

Source§

impl ChartBackend for WebGpuChartBackend

Source§

fn kind(&self) -> BackendKind

Implements the kind operation on WebGpuChartBackend.

§Returns
  • BackendKind - A BackendKind value.
Source§

fn resize(&mut self, width: f64, height: f64, dpr: f64)

Implements the resize operation on WebGpuChartBackend.

§Arguments
  • f64 - A 64-bit float (f64).
Source§

fn render(&mut self, list: &DisplayList, shaper: &mut TextShaper)

Implements the render operation on WebGpuChartBackend.

§Arguments
  • &DisplayList - Shared reference to a DisplayList.
  • &mut TextShaper - Mutable reference to a TextShaper (mutated in place).
Source§

fn dispose(&mut self)

Implements the dispose operation on WebGpuChartBackend.

Source§

impl Drop for WebGpuChartBackend

Implements Drop for WebGpuChartBackend.

Source§

fn drop(&mut self)

Releases the GPU resources when the backend is dropped.

A chart that is dropped without being disposed would hold on to its buffers until the browser collects them, and a chart over a million points holds on to a lot of memory.

Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more