euv-core 0.48.0

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Compiled WebGL2 direct GPU primitive pipeline handles returned by `web_init_gl2_direct_pipeline`.
pub(crate) struct WebGl2DirectPipeline {
    /// Compiled direct primitive `WebGlProgram`.
    pub(crate) program: WebGlProgram,
    /// `u_view` physical viewport uniform location.
    pub(crate) view_loc: Option<WebGlUniformLocation>,
    /// `u_rect` primitive bounds uniform location.
    pub(crate) rect_loc: Option<WebGlUniformLocation>,
    /// `u_color` primitive RGBA color uniform location.
    pub(crate) color_loc: Option<WebGlUniformLocation>,
}

/// Compiled WebGL2 fullscreen blit pipeline handles returned by `web_init_gl2_blit_pipeline`.
pub(crate) struct WebGl2BlitPipeline {
    /// Compiled fullscreen blit `WebGlProgram`.
    pub(crate) program: WebGlProgram,
    /// Resident WebGL2 texture object.
    pub(crate) texture: Option<JsValue>,
    /// Optional `u_view` drawing-buffer size uniform location.
    pub(crate) view_loc: Option<WebGlUniformLocation>,
}

/// Initialized DOM canvas surface bundle returned by `web_create_canvas` or `web_create_independent_canvas`.
pub(crate) struct WebCanvasSurface {
    /// Mounted `<canvas>` element.
    pub(crate) canvas: HtmlCanvasElement,
    /// Selected graphics context tier (`WebGpu`, `WebGl2`, or fallback `Canvas2d`).
    pub(crate) context_kind: WebCanvasContextKind,
    /// Active presentation mode (`GpuDirect`, `GpuUpload`, or `Software`).
    pub(crate) presentation_mode: WebPresentationMode,
    /// Optional WebGPU `GPUCanvasContext`.
    pub(crate) webgpu_ctx: Option<Object>,
    /// Optional hardware-composited `WebGl2RenderingContext`.
    pub(crate) gl2: Option<WebGl2RenderingContext>,
    /// Optional compiled WebGL2 direct GPU primitive program (`cpu_mirror_active = false`).
    pub(crate) gl2_direct_program: Option<WebGlProgram>,
    /// Optional `u_view` uniform location for direct GPU primitive rendering.
    pub(crate) gl2_direct_view_loc: Option<WebGlUniformLocation>,
    /// Optional `u_rect` uniform location for direct GPU primitive rendering.
    pub(crate) gl2_direct_rect_loc: Option<WebGlUniformLocation>,
    /// Optional `u_color` uniform location for direct GPU primitive rendering.
    pub(crate) gl2_direct_color_loc: Option<WebGlUniformLocation>,
    /// Optional compiled WebGL2 fullscreen blit program (`GpuUpload`).
    pub(crate) gl2_program: Option<WebGlProgram>,
    /// Optional resident WebGL2 texture object.
    pub(crate) gl2_texture: Option<JsValue>,
    /// Optional `u_view` drawing-buffer size uniform location of the blit program.
    pub(crate) gl2_view_loc: Option<WebGlUniformLocation>,
    /// Optional last-resort `CanvasRenderingContext2d`.
    pub(crate) ctx2d: Option<CanvasRenderingContext2d>,
}

/// Truthful browser presentation telemetry separating `GpuDirect`, `GpuUpload`, and `Software` and always tagging rAF as `SubmissionProxy`.
#[derive(Clone, Copy, Data, Debug, Default, Eq, PartialEq)]
pub struct WebPresentTelemetry {
    /// Active Web presentation mode (`GpuDirect`, `GpuUpload`, or `Software`).
    #[get(type(copy))]
    pub(crate) mode: WebPresentationMode,
    /// Explicit CPU-mirror telemetry flag (`false` for `GpuDirect`; `true` for `GpuUpload` and `Software`).
    #[get(type(copy))]
    pub(crate) cpu_mirror_active: bool,
    /// Exact framebuffer bytes uploaded via WebGL2 `texSubImage2D`/`texImage2D` or Canvas2D `putImageData` (`0` for `GpuDirect`).
    #[get(type(copy))]
    pub(crate) upload_bytes: u64,
    /// Number of GPU primitives drawn directly on WebGL2 in the latest `GpuDirect` frame (`0` on upload/2D).
    #[get(type(copy))]
    pub(crate) primitive_count: u32,
    /// Truthful timing source (`SubmissionProxy` for browser rAF without compositor proof).
    #[get(type(copy))]
    pub(crate) present_timing_source: WebPresentTimingSource,
    /// Monotonic generation ID of the currently visible foreground `<canvas>`.
    #[get(type(copy))]
    pub(crate) active_canvas_generation: u64,
    /// Total atomic independent-canvas handoff swaps completed.
    #[get(type(copy))]
    pub(crate) handoff_swaps_completed: u64,
    /// Total aborted candidate handoffs where the previous active `<canvas>` remained untouched.
    #[get(type(copy))]
    pub(crate) handoff_aborts: u64,
    /// Count of browser rAF ticks recorded strictly as submission proxies.
    #[get(type(copy))]
    pub(crate) raf_callback_count: u64,
}

/// Hidden independent sibling `<canvas>` pre-warming a candidate context before atomic frame-boundary visibility swap.
#[derive(Clone, CustomDebug, Data)]
pub(crate) struct WebStandbyCanvasTarget {
    /// Monotonic generation identifier assigned to this independent `<canvas>`.
    #[get(type(copy))]
    pub(crate) generation: u64,
    /// Target presentation mode (`GpuDirect`, `GpuUpload`, or `Software`).
    #[get(type(copy))]
    pub(crate) target_mode: WebPresentationMode,
    /// Locked browser context family (`WebGpu`, `WebGl2`, or `Canvas2d`).
    #[get(type(copy))]
    pub(crate) context_kind: WebCanvasContextKind,
    /// Backing DOM `<canvas>` element mounted hidden behind the active canvas.
    #[debug(skip)]
    #[get(skip)]
    pub(crate) canvas: HtmlCanvasElement,
    /// Optional WebGPU context on this standby canvas.
    #[debug(skip)]
    #[get(skip)]
    pub(crate) webgpu_ctx: Option<Object>,
    /// Optional WebGL2 context on this standby canvas.
    #[debug(skip)]
    #[get(skip)]
    pub(crate) gl2: Option<WebGl2RenderingContext>,
    /// Optional direct primitive WebGL2 program on this standby canvas.
    #[debug(skip)]
    #[get(skip)]
    pub(crate) gl2_direct_program: Option<WebGlProgram>,
    /// Optional `u_view` location for standby direct primitive program.
    #[debug(skip)]
    #[get(skip)]
    pub(crate) gl2_direct_view_loc: Option<WebGlUniformLocation>,
    /// Optional `u_rect` location for standby direct primitive program.
    #[debug(skip)]
    #[get(skip)]
    pub(crate) gl2_direct_rect_loc: Option<WebGlUniformLocation>,
    /// Optional `u_color` location for standby direct primitive program.
    #[debug(skip)]
    #[get(skip)]
    pub(crate) gl2_direct_color_loc: Option<WebGlUniformLocation>,
    /// Optional blit WebGL2 program on this standby canvas.
    #[debug(skip)]
    #[get(skip)]
    pub(crate) gl2_program: Option<WebGlProgram>,
    /// Optional resident WebGL2 texture on this standby canvas.
    #[debug(skip)]
    #[get(skip)]
    pub(crate) gl2_texture: Option<JsValue>,
    /// Optional `u_view` uniform location on standby blit program.
    #[debug(skip)]
    #[get(skip)]
    pub(crate) gl2_view_loc: Option<WebGlUniformLocation>,
    /// Allocated standby texture width in pixels.
    #[get(type(copy))]
    pub(crate) gl2_tex_width: u32,
    /// Allocated standby texture height in pixels.
    #[get(type(copy))]
    pub(crate) gl2_tex_height: u32,
    /// Optional 2D context on this standby canvas.
    #[debug(skip)]
    #[get(skip)]
    pub(crate) ctx2d: Option<CanvasRenderingContext2d>,
    /// True once a candidate first frame rendered cleanly onto this hidden canvas.
    #[get(type(copy))]
    pub(crate) ready: bool,
    /// Physical width of the pre-warmed candidate frame.
    #[get(type(copy))]
    pub(crate) ready_width: u32,
    /// Physical height of the pre-warmed candidate frame.
    #[get(type(copy))]
    pub(crate) ready_height: u32,
    /// Scene revision matching the pre-warmed candidate frame.
    #[get(type(copy))]
    pub(crate) ready_scene_version: u64,
}

/// Browser canvas window record storing live DOM `HtmlCanvasElement`, GPU/WebGL2/2D context, independent standby canvas, and telemetry.
#[derive(Clone, CustomDebug, Data)]
pub struct WebWindowRecord {
    /// Declarative window configuration.
    pub(crate) config: WindowConfig,
    /// Live window state and DPR metrics.
    #[get(type(copy))]
    pub(crate) state: WindowState,
    /// Assigned canvas registration identifier.
    #[get(type(copy))]
    pub(crate) canvas_id: u32,
    /// Active graphics context tier (`WebGpu`, `WebGl2`, or last-resort `Canvas2d`).
    #[get(type(copy))]
    pub(crate) context_kind: WebCanvasContextKind,
    /// Active presentation mode (`GpuDirect`, `GpuUpload`, or `Software`).
    #[get(type(copy))]
    pub(crate) presentation_mode: WebPresentationMode,
    /// Explicit CPU-mirror flag (`false` for `GpuDirect`; `true` for `GpuUpload` and `Software`).
    #[get(type(copy))]
    pub(crate) cpu_mirror_active: bool,
    /// Monotonic generation ID of the active foreground `<canvas>`.
    #[get(type(copy))]
    pub(crate) active_canvas_generation: u64,
    /// Backing DOM canvas element.
    #[debug(skip)]
    #[get(skip)]
    pub(crate) canvas: Option<HtmlCanvasElement>,
    /// Active WebGPU `GPUCanvasContext` when WebGPU is primary.
    #[debug(skip)]
    #[get(skip)]
    pub(crate) webgpu_ctx: Option<Object>,
    /// Active hardware-composited `WebGl2RenderingContext` when WebGL2 is bound.
    #[debug(skip)]
    #[get(skip)]
    pub(crate) gl2: Option<WebGl2RenderingContext>,
    /// Compiled WebGL2 direct GPU primitive program (`GpuDirect`, zero CPU pixel mirror).
    #[debug(skip)]
    #[get(skip)]
    pub(crate) gl2_direct_program: Option<WebGlProgram>,
    /// `u_view` uniform location for direct WebGL2 primitive rendering.
    #[debug(skip)]
    #[get(skip)]
    pub(crate) gl2_direct_view_loc: Option<WebGlUniformLocation>,
    /// `u_rect` uniform location for direct WebGL2 primitive rendering.
    #[debug(skip)]
    #[get(skip)]
    pub(crate) gl2_direct_rect_loc: Option<WebGlUniformLocation>,
    /// `u_color` uniform location for direct WebGL2 primitive rendering.
    #[debug(skip)]
    #[get(skip)]
    pub(crate) gl2_direct_color_loc: Option<WebGlUniformLocation>,
    /// Compiled fullscreen blit `WebGlProgram` for WebGL2 dirty-rect texture presentation.
    #[debug(skip)]
    #[get(skip)]
    pub(crate) gl2_program: Option<WebGlProgram>,
    /// Resident WebGL2 texture object backing dirty-rect `texSubImage2D` uploads.
    #[debug(skip)]
    #[get(skip)]
    pub(crate) gl2_texture: Option<JsValue>,
    /// `u_view` drawing-buffer size uniform location enabling 1:1 `texelFetch` presentation.
    #[debug(skip)]
    #[get(skip)]
    pub(crate) gl2_view_loc: Option<WebGlUniformLocation>,
    /// Allocated WebGL2 texture width in pixels.
    #[get(type(copy))]
    pub(crate) gl2_tex_width: u32,
    /// Allocated WebGL2 texture height in pixels.
    #[get(type(copy))]
    pub(crate) gl2_tex_height: u32,
    /// Last-resort 2D software blit context (only acquired when WebGPU & WebGL2 are unavailable).
    #[debug(skip)]
    #[get(skip)]
    pub(crate) ctx2d: Option<CanvasRenderingContext2d>,
    /// Optional hidden independent sibling `<canvas>` pre-warming a candidate backend before atomic frame-boundary handoff.
    #[debug(skip)]
    #[get(skip)]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) standby_canvas: Option<WebStandbyCanvasTarget>,
    /// Latest browser presentation telemetry snapshot.
    #[get(type(copy))]
    pub(crate) last_telemetry: WebPresentTelemetry,
    /// Attached browser `ResizeObserver` watching canvas layout changes without polling reflows.
    #[debug(skip)]
    #[get(skip)]
    pub(crate) resize_observer: Option<Object>,
}

/// Direct Browser WebAssembly (`web-sys` WebGPU / WebGL2 / Canvas2D / DOM Events) platform backend.
#[derive(Clone, Data, Debug)]
pub struct WebPlatform {
    /// Monotonic window handle allocator.
    #[get(pub(crate), type(copy))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) next_id: u64,
    /// Active web canvas windows.
    pub(crate) windows: HashMap<WindowId, WebWindowRecord>,
    /// Queued browser pointer, keyboard, wheel, and resize events.
    pub(crate) pending_events: Vec<WindowEvent>,
    /// Clipboard cache.
    #[get(skip)]
    pub(crate) clipboard_cache: Option<String>,
    /// Last observed `document.hidden` visibility state.
    #[get(pub(crate), type(copy))]
    #[set(pub(crate))]
    pub(crate) last_hidden: bool,
    /// Last observed `navigator.onLine` connectivity state.
    #[get(pub(crate), type(copy))]
    #[set(pub(crate))]
    pub(crate) last_online: bool,
    /// Last observed browser dark/light appearance mode.
    #[get(pub(crate), type(copy))]
    #[set(pub(crate))]
    pub(crate) last_theme: ThemeMode,
    /// Last observed browser BCP-47 locale tag.
    #[get(pub(crate))]
    #[set(pub(crate))]
    pub(crate) last_locale: String,
    /// Last measured CSS `env(safe-area-inset-*)` insets in logical CSS pixels.
    #[get(pub(crate), type(copy))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) last_safe_area: SafeAreaInsets,
    /// Measured browser display refresh rate in Hertz (`60.0` default, updated from `requestAnimationFrame` cadence).
    #[get(pub(crate), type(copy))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) refresh_rate_hz: f32,
    /// Whether the browser `requestAnimationFrame` loop is paused while hidden/backgrounded.
    #[get(pub(crate), type(copy))]
    #[set(pub(crate))]
    pub(crate) pump_paused: bool,
    /// Demand-driven `rAF` dirty flag (`true` when input, signal, or animation requires a frame; `false` sleeps at 0 Hz).
    #[get(pub(crate), type(copy))]
    #[set(pub(crate))]
    pub(crate) needs_frame: bool,
    /// Whether a browser `requestAnimationFrame` callback is currently scheduled.
    #[get(pub(crate), type(copy))]
    #[set(pub(crate))]
    pub(crate) raf_scheduled: bool,
    /// Active `requestAnimationFrame` handle (`0` when no frame callback is armed).
    #[get(pub(crate), type(copy))]
    #[set(pub(crate))]
    pub(crate) raf_handle: i32,
}

/// Thread-local sensor fusion state bridging browser `devicemotion` and `deviceorientation` events into `DeviceMotionSample`.
#[derive(Clone, Copy, Debug)]
pub(crate) struct WebDeviceMotionState {
    /// Whether `devicemotion` and `deviceorientation` DOM listeners have already been attached.
    pub(crate) listeners_attached: bool,
    /// Whether at least one `devicemotion` callback has delivered live inertial readings.
    pub(crate) has_motion_event: bool,
    /// Latest device-frame gravity vector derived from `deviceorientation` (`+X_right, +Y_up, +Z_out`, `m/s²`).
    pub(crate) orient_gravity_dev: Option<[f32; 3]>,
    /// Latest azimuth/yaw angle in radians from `deviceorientation.alpha`.
    pub(crate) last_yaw_rad: f32,
    /// Low-pass filtered device-frame gravity fallback used when `event.acceleration` is null.
    pub(crate) filtered_gravity_dev: Option<[f32; 3]>,
    /// Auto-detected sign multiplier converting raw `accelerationIncludingGravity` to downward gravity (`-1.0` W3C / `+1.0` Safari).
    pub(crate) gravity_sign: f32,
    /// Whether `pending_sample` holds an unread sensor update for `WebPlatform::poll_events`.
    pub(crate) has_pending_sample: bool,
    /// Most recently fused screen-aligned `DeviceMotionSample` pending delivery into `WebPlatform::poll_events`.
    pub(crate) pending_sample: DeviceMotionSample,
}