Skip to main content

SsContextWeb

Struct SsContextWeb 

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

The page’s WebGPU context: one adapter, one device, one SSRenderer, shared by every canvas and every player on the page.

const ctx = await SsContextWeb.create();
const surf = ctx.createSurface(canvas);
const player = surf.createPlayer();
await player.loadSsab(bytes);
player.setAnimation('idle');
player.play();

Implementations§

Source§

impl SsContextWeb

Source

pub async fn create() -> Result<SsContextWeb, JsValue>

Initialise WebGPU for the page. Rejects if the browser has no WebGPU adapter/device (the caller should surface a fallback message).

Call this once per page and share the result. Calling it twice builds two devices, and nothing is shared between them.

Source

pub fn create_surface( &self, canvas: HtmlCanvasElement, ) -> Result<SsSurfaceWeb, JsValue>

Attach a canvas and get the scene that draws into it. Any number of canvases may share this context.

Source

pub fn add_texture( &self, key: u32, rgba: &[u8], width: u32, height: u32, ) -> Result<(), JsValue>

Supply a texture under a textureRefs entry’s key, from raw straight-alpha RGBA.

Device-scoped, so it covers every canvas and every player at once — which is exactly why it is here and not on a surface or a player.

Prefer SsContextWeb::add_texture_from_bytes when what you have is an encoded image: this path premultiplies on the CPU and keeps the pixels for device-loss recovery, so a 2048² atlas costs two 16 MB passes through wasm memory that createImageBitmap does not.

Throws, uploading nothing, when rgba is not width * height * 4 bytes or the device cannot hold the texture (past its max_texture_dimension_2d).

Source

pub async fn add_texture_from_bytes( &self, key: u32, bytes: Vec<u8>, ) -> Result<(), JsValue>

Supply a texture under its key from encoded image bytes (PNG) — the main texture path of this face.

Both .ssab forms end here. An embedded pack’s atlases come from SsPlayerWeb::embedded_textures and need no fetch; a reference pack’s come from fetching each SsPlayerWeb::texture_refs entry whose embedded is false, resolved against the .ssab’s own URL — which is how this face supports both forms without carrying a decoder.

The bytes are kept (they are the encoded image, not the pixels) so SsContextWeb::restore_device can rebuild this texture on its own.

Source

pub fn add_texture_from_bitmap( &self, key: u32, bitmap: &ImageBitmap, ) -> Result<(), JsValue>

Supply a texture under its key from an ImageBitmap the page decoded.

For a host with its own asset pipeline — the analogue of crate::SSRenderer::set_texture. The bitmap must be premultiplied (createImageBitmap(blob, { premultiplyAlpha: 'premultiply' })), which is what the whole pipeline composites in.

Device loss is the supplier’s to repair on this path, exactly as on the Rust one: nothing is retained here, so the page registers it again after SsContextWeb::restore_device. Use SsContextWeb::add_texture_from_bytes to be spared that.

Throws, registering nothing, for a bitmap the device cannot hold — past its max_texture_dimension_2d, or closed.

Source

pub fn has_texture(&self, key: u32) -> bool

Whether an atlas is already registered for key.

The texture map is device-scoped, so this answers for the whole page: it is how a page that decodes atlases itself keeps “N players of one pack decode its atlas once between them” true. The player-side flush gets that for free (it filters before the decode); a page supplying textures itself has to ask.

Source

pub fn is_device_lost(&self) -> bool

Whether the browser has taken the WebGPU device away. While this is true every surface’s frame is a no-op; call SsContextWeb::restore_device to bring the whole page back.

Source

pub fn version(&self) -> String

This player’s version.

Source

pub fn sdk_version(&self) -> String

The SpriteStudio-SDK version whose runtime this build links, written as its release tag (v7.0.0), or local from a plain cargo build. The package carries the same string in wasm/VERSION.

Source

pub async fn restore_device(&self) -> Result<(), JsValue>

Rebuild the page’s WebGPU stack after a device loss and restore every surface and player on it (pipelines, buffers, atlases, projections) — playback continues from the frame it was interrupted on, with no re-fetch of any .ssab and no re-fetch of any atlas.

One call for the page, however many canvases it holds.

Rejects if WebGPU is gone for good, or if the new adapter cannot present a surface’s baked format (then the host should rebuild from create).

Source

pub fn surface_count(&self) -> usize

How many canvases this context currently drives (diagnostic).

Trait Implementations§

Source§

impl From<SsContextWeb> for JsValue

Source§

fn from(value: SsContextWeb) -> Self

Converts to this type from the input type.
Source§

impl FromWasmAbi for SsContextWeb

Source§

type Abi = WasmPtr<WasmRefCell<SsContextWeb>>

The Wasm ABI type that this converts from when coming back out from the ABI boundary.
Source§

unsafe fn from_abi(js: Self::Abi) -> Self

Recover a Self from Self::Abi. Read more
Source§

impl IntoWasmAbi for SsContextWeb

Source§

type Abi = WasmPtr<WasmRefCell<SsContextWeb>>

The Wasm ABI type that this converts into when crossing the ABI boundary.
Source§

fn into_abi(self) -> Self::Abi

Convert self into Self::Abi so that it can be sent across the wasm ABI boundary.
Source§

impl LongRefFromWasmAbi for SsContextWeb

Source§

type Abi = WasmPtr<WasmRefCell<SsContextWeb>>

Same as RefFromWasmAbi::Abi
Source§

type Anchor = RcRef<SsContextWeb>

Same as RefFromWasmAbi::Anchor
Source§

unsafe fn long_ref_from_abi(js: Self::Abi) -> Self::Anchor

Same as RefFromWasmAbi::ref_from_abi
Source§

impl NullableFromWasmAbi for SsContextWeb

Source§

fn is_null(abi: &Self::MaybeAbi) -> bool

Tests the ABI value for Nullable::Null.
Source§

impl NullableIntoWasmAbi for SsContextWeb

Source§

fn null() -> Self::MaybeAbi

Returns the ABI value for Nullable::Null, which JS reads as null. Read more
Source§

impl OptionFromWasmAbi for SsContextWeb

Source§

fn is_none(abi: &Self::Abi) -> bool

Tests whether the argument is a “none” instance. If so it will be deserialized as None, and otherwise it will be passed to FromWasmAbi.
Source§

impl OptionIntoWasmAbi for SsContextWeb

Source§

fn none() -> Self::Abi

Returns an ABI instance indicating “none”, which JS will interpret as the None branch of this option. Read more
Source§

impl RefFromWasmAbi for SsContextWeb

Source§

type Abi = WasmPtr<WasmRefCell<SsContextWeb>>

The Wasm ABI type references to Self are recovered from.
Source§

type Anchor = RcRef<SsContextWeb>

The type that holds the reference to Self for the duration of the invocation of the function that has an &Self parameter. This is required to ensure that the lifetimes don’t persist beyond one function call, and so that they remain anonymous.
Source§

unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor

Recover a Self::Anchor from Self::Abi. Read more
Source§

impl RefMutFromWasmAbi for SsContextWeb

Source§

type Abi = WasmPtr<WasmRefCell<SsContextWeb>>

Same as RefFromWasmAbi::Abi
Source§

type Anchor = RcRefMut<SsContextWeb>

Same as RefFromWasmAbi::Anchor
Source§

unsafe fn ref_mut_from_abi(js: Self::Abi) -> Self::Anchor

Same as RefFromWasmAbi::ref_from_abi
Source§

impl SupportsConstructor for SsContextWeb

Source§

impl SupportsInstanceProperty for SsContextWeb

Source§

impl SupportsStaticProperty for SsContextWeb

Source§

impl TryFromJsValue for SsContextWeb

Source§

fn try_from_js_value(value: JsValue) -> Result<Self, JsValue>

Performs the conversion.
Source§

fn try_from_js_value_ref(value: &JsValue) -> Option<Self>

Performs the conversion.
Source§

const NULLABLE: bool = false

Whether Self accepts null as a distinct value space, as Nullable<T> does. An enclosing Option then passes null through instead of reading it as None, so Option<T> is itself nullable exactly when T is not. JsValue does not set this: it accepts null as one value among all of them, not as a distinct space, and Option<JsValue> reads it as None like the ABI does. This is the JS-value side of NullableFromWasmAbi, which claims null at the ABI.
Source§

impl VectorFromWasmAbi for SsContextWeb

Source§

impl VectorIntoWasmAbi for SsContextWeb

Source§

impl WasmDescribe for SsContextWeb

Source§

impl WasmDescribeVector for SsContextWeb

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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

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> MaybeFromWasmAbi for T

Source§

type MaybeAbi = <T as FromWasmAbi>::Abi

The ABI of Option<Self>.
Source§

unsafe fn from_maybe_abi(js: <T as FromWasmAbi>::Abi) -> T

Recovers a value from the Option<Self> ABI, once is_none has ruled None out. Read more
Source§

unsafe fn try_from_maybe_abi( js: <T as FromWasmAbi>::Abi, ) -> Result<T, &'static str>

Source§

fn is_none(js: &<T as FromWasmAbi>::Abi) -> bool

Tests the ABI value for None.
Source§

impl<T> MaybeIntoWasmAbi for T

Source§

type MaybeAbi = <T as IntoWasmAbi>::Abi

The ABI of Option<Self>.
Source§

fn into_maybe_abi(self) -> <T as IntoWasmAbi>::Abi

Converts a value into the Option<Self> ABI.
Source§

fn none() -> <T as IntoWasmAbi>::Abi

Returns the ABI value for None, which JS reads as undefined. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ReturnWasmAbi for T
where T: IntoWasmAbi,

Source§

type Abi = <T as IntoWasmAbi>::Abi

Same as IntoWasmAbi::Abi
Source§

fn return_abi(self) -> <T as ReturnWasmAbi>::Abi

Same as IntoWasmAbi::into_abi, except that it may throw and never return in the case of Err.
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.
§

impl<T> Upcast<T> for T

§

fn upcast(&self) -> Option<&T>

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