euv-core 0.48.2

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::*;

/// CoreGraphics 2D point ABI layout matching `CGPoint`.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct CgPoint {
    /// Horizontal coordinate.
    pub(crate) x: f64,
    /// Vertical coordinate.
    pub(crate) y: f64,
}

/// CoreGraphics 2D size ABI layout matching `CGSize`.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct CgSize {
    /// Width in points.
    pub(crate) width: f64,
    /// Height in points.
    pub(crate) height: f64,
}

/// CoreGraphics rectangle ABI layout matching `CGRect`.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct CgRect {
    /// Rectangle origin.
    pub(crate) origin: CgPoint,
    /// Rectangle size.
    pub(crate) size: CgSize,
}

/// CoreFoundation timer context ABI layout matching `CFRunLoopTimerContext`.
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub(crate) struct CfRunLoopTimerContext {
    /// Structure version (`0`).
    pub(crate) version: isize,
    /// Opaque user info pointer forwarded to timer callout.
    pub(crate) info: *mut c_void,
    /// Optional retain callback (`null`).
    pub(crate) retain: *const c_void,
    /// Optional release callback (`null`).
    pub(crate) release: *const c_void,
    /// Optional copyDescription callback (`null`).
    pub(crate) copy_description: *const c_void,
}

/// CoreVideo `CVTime` ABI layout returned by `CVDisplayLinkGetNominalOutputVideoRefreshPeriod`.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct CvTime {
    /// Numerator time value in `time_scale` units.
    pub(crate) time_value: i64,
    /// Denominator time scale in ticks per second.
    pub(crate) time_scale: i32,
    /// `CVTimeFlags` bitmask (`kCVTimeIsIndefinite = 1`).
    pub(crate) flags: i32,
}

/// CoreVideo `CVTimeStamp` ABI layout passed to `CVDisplayLinkOutputCallback`.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct CvTimeStamp {
    /// Structure version (`0`).
    pub(crate) version: u32,
    /// Video time scale in ticks per second.
    pub(crate) video_time_scale: i32,
    /// Current video time tick.
    pub(crate) video_time: i64,
    /// Monotonic Mach host time.
    pub(crate) host_time: u64,
    /// Instantaneous playback rate scalar.
    pub(crate) rate_scalar: f64,
    /// Video refresh period in `video_time_scale` ticks.
    pub(crate) video_refresh_period: i64,
    /// Embedded `CVSMPTETime` struct (24 bytes).
    pub(crate) smpte_time: [u32; 6],
    /// Validity flags (`CVTimeStampFlags`).
    pub(crate) flags: u64,
    /// Reserved padding word.
    pub(crate) reserved: u64,
}

/// RAII Objective-C autorelease pool guard draining per-tick Cocoa objects automatically.
pub(crate) struct MacOsAutoreleasePoolGuard {
    /// Opaque pool token returned by `objc_autoreleasePoolPush`.
    pub(crate) pool: *mut c_void,
}

/// Explicit per-frame macOS presentation telemetry separating Metal direct draw from `CGImage` software fallback.
#[derive(Clone, Copy, Data, Debug, Default, Eq, PartialEq)]
pub struct MacOsPresentTelemetry {
    /// Active presentation path (`GpuDirect` vs `SoftwareCgImage`).
    #[get(type(copy))]
    pub(crate) path: MacOsPresentationPath,
    /// Explicit CPU-mirror telemetry flag (`false` for `GpuDirect`; `true` for `SoftwareCgImage`).
    #[get(type(copy))]
    pub(crate) cpu_mirror_active: bool,
    /// Timing source (`CompositorMeasured` via `MTLDrawable.presentedTime` vs `SubmissionProxy`).
    #[get(type(copy))]
    pub(crate) timing_source: MacOsPresentTimingSource,
    /// Hardware presentation timestamp in nanoseconds reported by `MTLDrawable.addPresentedHandler:` (`0` when unavailable).
    #[get(type(copy))]
    pub(crate) presented_time_ns: u64,
    /// Hardware GPU execution time in nanoseconds (`GPUEndTime - GPUStartTime`, `0` on software fallback).
    #[get(type(copy))]
    pub(crate) gpu_exec_ns: u64,
    /// Framebuffer pixel bytes copied to `CGImage`/`CALayer` (`0` on `GpuDirect`).
    #[get(type(copy))]
    pub(crate) upload_bytes: u64,
    /// Cumulative frames presented directly through `CAMetalLayer` / `CAMetalDrawable`.
    #[get(type(copy))]
    pub(crate) direct_gpu_frames: u64,
    /// Cumulative frames presented through `CGImage` / `CALayer` software fallback.
    #[get(type(copy))]
    pub(crate) software_fallback_frames: u64,
    /// Cumulative failed presentation attempts.
    #[get(type(copy))]
    pub(crate) failed_frames: u64,
}

/// Native macOS window record storing direct Cocoa/AppKit/QuartzCore pointers and Metal direct presentation state.
#[derive(Clone, Data, Debug)]
pub struct MacOsWindowRecord {
    /// Active window configuration.
    pub(crate) config: WindowConfig,
    /// Live window state and metrics.
    #[get(type(copy))]
    pub(crate) state: WindowState,
    /// Raw `NSWindow*` pointer.
    #[get(type(copy))]
    pub(crate) ns_window: usize,
    /// Raw `NSView*` content view pointer.
    #[get(type(copy))]
    pub(crate) ns_view: usize,
    /// Raw `CAMetalLayer*` pointer attached to `ns_view`.
    #[get(type(copy))]
    pub(crate) metal_layer: usize,
    /// Whether the native `NSWindow` has been ordered front after Frame 1 presentation.
    #[get(type(copy))]
    pub(crate) revealed: bool,
    /// Last observed `NSScreen*` pointer used to detect monitor changes and refresh Hz live.
    #[get(type(copy))]
    pub(crate) last_screen_ptr: usize,
    /// Active presentation path (`GpuDirect` on Metal or `SoftwareCgImage` fallback).
    #[get(type(copy))]
    pub(crate) presentation_path: MacOsPresentationPath,
    /// Explicit CPU-mirror telemetry flag (`false` on Metal `GpuDirect`, `true` on `CGImage` software fallback).
    #[get(type(copy))]
    pub(crate) cpu_mirror_active: bool,
    /// Most recent macOS presentation telemetry snapshot.
    #[get(type(copy))]
    pub(crate) last_telemetry: MacOsPresentTelemetry,
    /// Retained native Metal GPU driver attached directly to `metal_layer` for zero-CPU-mirror rendering.
    #[get(skip)]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) metal_driver: Option<Arc<Mutex<NativeGpuDriver>>>,
}

/// Direct macOS Cocoa / AppKit / CoreGraphics / Metal platform backend.
#[derive(Clone, Data, Debug)]
pub struct MacOsPlatform {
    /// Monotonic window handle generator.
    #[get(pub(crate), type(copy))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) next_id: u64,
    /// Open native macOS windows.
    pub(crate) windows: HashMap<WindowId, MacOsWindowRecord>,
    /// Queued window and input events.
    pub(crate) pending_events: Vec<WindowEvent>,
    /// Local clipboard cache synchronized with pasteboard operations.
    #[get(skip)]
    pub(crate) clipboard_cache: Option<String>,
    /// Cached `CGColorSpaceRef` reused across all frame presentations.
    #[get(pub(crate), type(copy))]
    #[set(pub(crate))]
    pub(crate) cached_color_space: usize,
    /// Last polled `NSApplication.isActive` state for deduplicating foreground/inactive transitions.
    #[get(pub(crate), type(copy))]
    #[set(pub(crate))]
    pub(crate) last_app_active: bool,
    /// Last polled `NSWindow.isMiniaturized` state for background/foreground normalization.
    #[get(pub(crate), type(copy))]
    #[set(pub(crate))]
    pub(crate) last_miniaturized: bool,
    /// Last polled `NSProcessInfo.isLowPowerModeEnabled` flag.
    #[get(pub(crate), type(copy))]
    #[set(pub(crate))]
    pub(crate) last_low_power: bool,
    /// Last polled `NSPasteboard.changeCount` sentinel (`-1` before first poll).
    #[get(pub(crate), type(copy))]
    #[set(pub(crate))]
    pub(crate) last_pasteboard_count: isize,
    /// Last polled macOS appearance theme for change detection.
    #[get(pub(crate), type(copy))]
    #[set(pub(crate))]
    pub(crate) last_theme: ThemeMode,
    /// Last polled `NSApplication.isHidden` state for Cmd+H background normalization.
    #[get(pub(crate), type(copy))]
    #[set(pub(crate))]
    pub(crate) last_app_hidden: bool,
    /// Whether the macOS run-loop frame pump is paused while background-frozen.
    #[get(pub(crate), type(copy))]
    #[set(pub(crate))]
    pub(crate) pump_paused: bool,
    /// Active CoreVideo `CVDisplayLinkRef` handle (`0` when running headless or stopped).
    #[get(pub(crate), type(copy))]
    #[set(pub(crate))]
    pub(crate) display_link: usize,
    /// Cached live display refresh rate in Hertz updated on screen change or `CVDisplayLink` tick.
    #[get(pub(crate), type(copy))]
    #[set(pub(crate))]
    pub(crate) cached_refresh_hz: f32,
    /// Timestamp of the last slow system metadata poll (throttling `NSPasteboard` Mach IPC).
    #[get(skip)]
    pub(crate) last_system_poll: Instant,
}