Skip to main content

compose_rust/
boundary.rs

1use crate::Selection;
2use crate::measure::MeasureResult;
3use crate::protocol::{HostEvent, Mutation, PropertyValue, ProtocolError, decode_event};
4use crate::runtime::Runtime;
5use crate::schema::{
6    AssetKind, EventPayload, IconRole, LoopMode, PROTOCOL_VERSION, SCHEMA_HASH, Theme,
7};
8use std::cell::RefCell;
9use std::ffi::c_int;
10use std::panic::{AssertUnwindSafe, catch_unwind};
11use std::sync::atomic::{AtomicBool, Ordering};
12use std::sync::{Arc, Mutex, OnceLock};
13use std::task::{Context, Wake, Waker};
14
15pub const STATUS_OK: i32 = 0;
16pub const STATUS_PROTOCOL_ERROR: i32 = -1;
17pub const STATUS_NOT_INITIALIZED: i32 = -2;
18pub const STATUS_ALREADY_INITIALIZED: i32 = -3;
19pub const STATUS_PANIC: i32 = -4;
20/// There is no renderer in this build to run. Returned by the loop entry point, never by
21/// a boundary call: a call could not have got this far without a renderer to make it.
22pub const STATUS_NO_RENDERER: i32 = -5;
23
24/// What the process exits with when the renderer loop does not finish successfully.
25///
26/// 1, not the status itself: exit codes are a single byte on Unix, so `STATUS_NO_RENDERER`
27/// would reach the shell as 251 and read as a signal rather than an ordinary failure.
28const EXIT_FAILURE: i32 = 1;
29
30#[repr(C)]
31#[derive(Clone, Copy, Debug)]
32pub struct MutationBatch {
33    pub ptr: *const u8,
34    pub len: u32,
35    pub result: i64,
36}
37
38impl Default for MutationBatch {
39    fn default() -> Self {
40        Self {
41            ptr: std::ptr::null(),
42            len: 0,
43            result: 0,
44        }
45    }
46}
47
48/// The Renderer's half of the boundary, as the Host calls it.
49#[derive(Clone, Copy)]
50pub struct RendererApi {
51    pub run: extern "C" fn() -> c_int,
52    pub request_frame: extern "C" fn(),
53    /// Reads `count` request records from the buffer and writes `count` results. Only on
54    /// the UI thread, and normally from inside a Host call the Renderer made.
55    pub measure: unsafe extern "C" fn(*const u8, u32, u32, *mut MeasureResult) -> i32,
56}
57
58static RENDERER_API: OnceLock<RendererApi> = OnceLock::new();
59static FRAME_REQUESTED: AtomicBool = AtomicBool::new(false);
60static EVENT_DISPATCH_ACTIVE: AtomicBool = AtomicBool::new(false);
61static DEFERRED_FRAME_REQUEST: AtomicBool = AtomicBool::new(false);
62/// Set between a stop and the start that follows it. While it is set a worker's frame
63/// request is remembered but not delivered, so a process that is not on screen is never
64/// asked to draw.
65static LIFECYCLE_SUPPRESSED: AtomicBool = AtomicBool::new(false);
66
67pub fn install_renderer_api(api: RendererApi) -> Result<(), RendererApi> {
68    RENDERER_API.set(api)
69}
70
71// `renderer_linked` is set by the build script, and only when a renderer was really
72// linked. That is not the same as the feature being on: a documentation build turns the
73// feature on and links nothing, and the result has to behave like the build it is.
74#[cfg(all(renderer_linked, not(any(test, feature = "mock-renderer"))))]
75unsafe extern "C" {
76    fn compose_rust_renderer_run() -> c_int;
77    fn compose_rust_renderer_request_frame();
78    fn compose_rust_renderer_measure(
79        requests: *const u8,
80        length: u32,
81        count: u32,
82        results: *mut MeasureResult,
83    ) -> i32;
84}
85
86#[cfg(all(renderer_linked, not(any(test, feature = "mock-renderer"))))]
87extern "C" fn native_run() -> c_int {
88    // The Linux static renderer refers to the five Host functions weakly, so that its own
89    // test executable links without a Host, and a weak reference does not make a linker
90    // pull in the code it names. This one does: running the renderer keeps them linked,
91    // whichever object file of this crate the compiler happened to put them in.
92    std::hint::black_box(&BOUNDARY_EXPORTS);
93    // SAFETY: The application links the Renderer implementation of this declared C ABI.
94    unsafe { compose_rust_renderer_run() }
95}
96
97#[cfg(all(renderer_linked, not(any(test, feature = "mock-renderer"))))]
98extern "C" fn native_request_frame() {
99    // SAFETY: The Renderer contract makes request_frame thread-safe.
100    unsafe { compose_rust_renderer_request_frame() }
101}
102
103/// # Safety
104/// As `compose_rust_renderer_measure`: readable requests, writable results.
105#[cfg(all(renderer_linked, not(any(test, feature = "mock-renderer"))))]
106unsafe extern "C" fn native_measure(
107    requests: *const u8,
108    length: u32,
109    count: u32,
110    results: *mut MeasureResult,
111) -> i32 {
112    // SAFETY: The caller's promise is passed on unchanged, and the Renderer refuses a call
113    // from the wrong thread rather than measuring on it.
114    unsafe { compose_rust_renderer_measure(requests, length, count, results) }
115}
116
117// The mock renderer and the crate's own tests drive the boundary directly and never want
118// a window, so doing nothing is the correct answer for them and always has been.
119#[cfg(any(test, feature = "mock-renderer"))]
120extern "C" fn native_run() -> c_int {
121    STATUS_OK
122}
123
124#[cfg(any(test, feature = "mock-renderer"))]
125extern "C" fn native_request_frame() {}
126
127// The mock renderer answers by fixed rules, so a test of the Host can predict every size.
128#[cfg(any(test, feature = "mock-renderer"))]
129use crate::measure::fake_measure as native_measure;
130
131// A real build with no renderer. This used to return success, so an application built
132// this way opened no window, drew nothing, printed nothing and exited 0, and there was
133// no way to tell that from a program that had simply finished.
134#[cfg(all(not(renderer_linked), not(any(test, feature = "mock-renderer"))))]
135extern "C" fn native_run() -> c_int {
136    eprintln!("{}", no_renderer_message());
137    STATUS_NO_RENDERER
138}
139
140#[cfg(all(not(renderer_linked), not(any(test, feature = "mock-renderer"))))]
141extern "C" fn native_request_frame() {}
142
143/// There is nothing to measure with, which the caller hears as a refusal and carries on
144/// past with every size at zero.
145#[cfg(all(not(renderer_linked), not(any(test, feature = "mock-renderer"))))]
146unsafe extern "C" fn native_measure(
147    _requests: *const u8,
148    _length: u32,
149    _count: u32,
150    _results: *mut MeasureResult,
151) -> i32 {
152    crate::measure::MEASURE_UNAVAILABLE
153}
154
155/// What a build with no renderer says on its way out.
156///
157/// Compiled into every build, not only the one that prints it, so that a test can read it
158/// whatever the build it is running in was configured with.
159pub fn no_renderer_message() -> String {
160    format!(
161        "compose-rust: this application was built without a renderer, so there is nothing\n\
162         to draw with and nothing to draw on. Exiting {EXIT_FAILURE} rather than looking like\n\
163         a program that ran and finished.\n\
164         \n\
165         A default `cargo build` links the renderer for the platform it is building for and\n\
166         downloads it if it has to. A build reaches this message by turning that off:\n\
167         \n\
168         \x20   default-features = false, without re-enabling `native-renderer`\n\
169         \x20   a documentation build, which has no network to fetch a renderer with\n\
170         \n\
171         Building for a test or with the `mock-renderer` feature is a third way, and that\n\
172         one is silent on purpose: those builds drive the boundary directly and want no\n\
173         window."
174    )
175}
176
177fn renderer_api() -> RendererApi {
178    RENDERER_API.get().copied().unwrap_or(RendererApi {
179        run: native_run,
180        request_frame: native_request_frame,
181        measure: native_measure,
182    })
183}
184
185/// Asks the Renderer to measure, through whichever Renderer is installed.
186///
187/// Crate-internal: the safe way in is `measure::measurer`, which only hands out the right
188/// to call this inside a Host call. The pointers come from slices that outlive the call.
189pub(crate) fn renderer_measure(
190    requests: *const u8,
191    length: u32,
192    count: u32,
193    results: *mut MeasureResult,
194) -> i32 {
195    // SAFETY: the caller passes a readable buffer of `length` bytes and room for `count`
196    // results, both borrowed for the length of this call.
197    unsafe { (renderer_api().measure)(requests, length, count, results) }
198}
199
200/// Coalesced wake used internally by the runtime's scheduler waker.
201///
202/// The flag spans the moment of delivery, not the wait for the frame that answers it. The
203/// Renderer folds requests into its own frame clock, so however many arrive between two
204/// frames it draws once; holding the flag until the frame came back would instead mean
205/// that one request the Renderer was not yet listening for silenced every later one. That
206/// happens on a cold start, where the first composition can be seconds after the first
207/// worker request, and it leaves the application frozen with nothing to unfreeze it.
208pub fn request_frame_from_worker() {
209    if !FRAME_REQUESTED.swap(true, Ordering::AcqRel) {
210        if LIFECYCLE_SUPPRESSED.load(Ordering::Acquire)
211            || EVENT_DISPATCH_ACTIVE.load(Ordering::Acquire)
212        {
213            DEFERRED_FRAME_REQUEST.store(true, Ordering::Release);
214        } else {
215            (renderer_api().request_frame)();
216            FRAME_REQUESTED.store(false, Ordering::Release);
217        }
218    }
219}
220
221thread_local! {
222    /// How deep this thread is inside a Host call. A notification posted from inside one is
223    /// written into the batch that call returns, so it needs no frame of its own.
224    static HOST_CALL_DEPTH: std::cell::Cell<u32> = const { std::cell::Cell::new(0) };
225}
226
227/// Whether this thread is inside `rebuild`, `dispatch` or `render_frame` right now.
228pub(crate) fn in_host_call() -> bool {
229    HOST_CALL_DEPTH.with(std::cell::Cell::get) > 0
230}
231
232/// Marks the span of one Host call on this thread.
233struct HostCallGuard;
234
235impl HostCallGuard {
236    fn enter() -> Self {
237        HOST_CALL_DEPTH.with(|depth| depth.set(depth.get() + 1));
238        Self
239    }
240}
241
242impl Drop for HostCallGuard {
243    fn drop(&mut self) {
244        HOST_CALL_DEPTH.with(|depth| depth.set(depth.get().saturating_sub(1)));
245    }
246}
247
248/// Asks for the frame that carries a queued notification out.
249///
250/// The one request a stopped UI does not hold back. Holding requests while the platform
251/// has the UI off screen is right for timers and animations, which nobody is watching, and
252/// wrong for a notification, whose whole point is the moment nobody is watching. So while
253/// stopped this goes to the Renderer directly, and the Renderer serves it outside its
254/// stopped frame clock. Everything else stays held, and the frame it was waiting for is
255/// still delivered on start.
256pub(crate) fn request_frame_for_notifications() {
257    if LIFECYCLE_SUPPRESSED.load(Ordering::Acquire) {
258        (renderer_api().request_frame)();
259        return;
260    }
261    request_frame_from_worker();
262}
263
264struct EventDispatchGuard;
265
266impl EventDispatchGuard {
267    fn enter() -> Self {
268        EVENT_DISPATCH_ACTIVE.store(true, Ordering::Release);
269        Self
270    }
271}
272
273impl Drop for EventDispatchGuard {
274    fn drop(&mut self) {
275        EVENT_DISPATCH_ACTIVE.store(false, Ordering::Release);
276        if LIFECYCLE_SUPPRESSED.load(Ordering::Acquire) {
277            return;
278        }
279        if DEFERRED_FRAME_REQUEST.swap(false, Ordering::AcqRel) {
280            (renderer_api().request_frame)();
281            FRAME_REQUESTED.store(false, Ordering::Release);
282        }
283    }
284}
285
286struct FrameWake;
287
288impl Wake for FrameWake {
289    fn wake(self: Arc<Self>) {
290        request_frame_from_worker();
291    }
292
293    fn wake_by_ref(self: &Arc<Self>) {
294        request_frame_from_worker();
295    }
296}
297
298/// One streaming Text node's tail, accumulated between frames.
299struct PendingAppend {
300    node_id: u32,
301    text: String,
302    dirty: bool,
303}
304
305/// Makes the runtime a Host drives. Called once per Host, and again on a resync, which
306/// is the one thing a diff against a lost node table cannot answer.
307///
308/// Shared across threads because `launch` runs on the Rust main thread and the Host is
309/// built on the Renderer's UI thread, which is a different one under
310/// `LoopMode::Renderer`. The runtime it makes is not shared: it lives and dies on the UI
311/// thread.
312pub type RuntimeFactory = Arc<dyn Fn() -> Box<dyn Runtime> + Send + Sync>;
313
314pub struct Host {
315    /// Kept so the tree can be built again from nothing when the Renderer asks for a
316    /// resync, which is the one thing a diff against a lost node table cannot answer.
317    factory: RuntimeFactory,
318    theme: Theme,
319    window: crate::schema::Window,
320    runtime: Box<dyn Runtime>,
321    frame_waker: Waker,
322    pending_appends: Vec<PendingAppend>,
323}
324
325impl Host {
326    /// A Host for the runtime `factory` makes, in the theme the application launched with.
327    pub fn new(factory: impl Fn() -> Box<dyn Runtime> + Send + Sync + 'static) -> Self {
328        Self::from_factory(Arc::new(factory), launched_theme())
329    }
330
331    /// The theme the application chose. Choosing nothing follows the host platform, with
332    /// Material 3 where the platform has no look of its own.
333    pub fn with_theme(
334        factory: impl Fn() -> Box<dyn Runtime> + Send + Sync + 'static,
335        theme: Theme,
336    ) -> Self {
337        Self::from_factory(Arc::new(factory), theme)
338    }
339
340    fn from_factory(factory: RuntimeFactory, theme: Theme) -> Self {
341        // A fresh Host has not been measured yet, and the Renderer that is about to drive
342        // it starts from the same assumption. Leaving a previous Host's last measurement
343        // behind would put the two sides out of step, because the Renderer reports only
344        // differences.
345        crate::window::reset_window_size();
346        // Messages queued against a Host that is going away would otherwise be said by
347        // the one replacing it, out of any context that made them make sense.
348        //
349        crate::message::reset_messages();
350        // The Renderer reports its zoom again at start, and a level the last Host asked for
351        // and never sent is that Host's business.
352        crate::zoom::reset_zoom();
353        // The windows' commands and registrations belong to the Host that queued them.
354        crate::windows::reset_windows();
355        // The assets do not go with them. The Renderer this Host is about to talk to has
356        // an empty cache, so every registration has to be made again, which is what this
357        // does: the ids stay as they were and the bytes are queued for the first batch.
358        //
359        // Forgetting them instead is what this used to do, and it was wrong in a way that
360        // only showed on the screen. A theme names its fonts by id and a window names its
361        // icon by id, and both are built by the caller before the Host is made out of
362        // them, so the ids travelled and the bytes they named had been thrown away.
363        crate::asset::requeue_all();
364        // The theme a running application changes from, and nothing queued against the
365        // Host this one replaces.
366        crate::theme::install(theme);
367        let runtime = factory();
368        Self {
369            factory,
370            theme,
371            window: launched_window(),
372            runtime,
373            frame_waker: Waker::from(Arc::new(FrameWake)),
374            pending_appends: Vec::new(),
375        }
376    }
377
378    /// Replaces the window settings this Host was made with, which are the launch's. For a
379    /// test or a tool that builds a Host without launching an application, such as one
380    /// that wants several windows: `Window::new().with_windows()`.
381    pub fn set_window(&mut self, window: crate::schema::Window) {
382        self.window = window;
383    }
384
385    fn batch(&mut self) -> &mut crate::runtime::Batch {
386        self.runtime.batch_mut()
387    }
388
389    pub fn rebuild(&mut self) -> Result<&[u8], ProtocolError> {
390        let _call = HostCallGuard::enter();
391        self.batch().begin();
392        // One record at the root, before any node exists. The Renderer resolves roles to
393        // values, so switching theme or colour scheme costs this one record rather than a
394        // SetProp for every node in the tree.
395        let (theme, window) = (self.theme, self.window);
396        self.batch().set_theme(theme);
397        // Beside the theme, and for the same reason: it is about the window rather than
398        // any node in it, and it is settled once rather than every frame. The Renderer
399        // reads it out of this batch before it stands the window up.
400        self.batch().set_window(window);
401        self.runtime.rebuild();
402        self.flush_messages();
403        self.arm_scheduler_wake();
404        self.runtime.batch_mut().finish()
405    }
406
407    pub fn dispatch_event(&mut self, bytes: &[u8]) -> Result<(&[u8], i64), ProtocolError> {
408        let event = decode_event(bytes)?;
409        self.dispatch(event)
410    }
411
412    /// The batch arena, reported to the Renderer on every boundary call.
413    pub fn arena(&self) -> (*const u8, usize) {
414        self.runtime.batch().arena()
415    }
416
417    /// Answers with the whole tree, for a Renderer that no longer has a node table.
418    ///
419    /// The Host keeps no shadow of what it has already sent, so the only way to produce a
420    /// full-tree batch is to build the application again, and that resets component state.
421    /// A Renderer that keeps its node table across a configuration change never needs
422    /// this call, which is what the Android host does.
423    pub fn resync(&mut self) -> Result<(&[u8], i64), ProtocolError> {
424        *self = Self::from_factory(self.factory.clone(), self.theme);
425        Ok((self.rebuild()?, 0))
426    }
427
428    /// Suppresses timers and animations while the UI is off screen, and releases the
429    /// request that arrived while it was, so nothing is lost by stopping.
430    fn set_lifecycle_running(&mut self, running: bool) -> Result<(&[u8], i64), ProtocolError> {
431        LIFECYCLE_SUPPRESSED.store(!running, Ordering::Release);
432        if running {
433            if DEFERRED_FRAME_REQUEST.swap(false, Ordering::AcqRel) {
434                (renderer_api().request_frame)();
435            }
436            FRAME_REQUESTED.store(false, Ordering::Release);
437        }
438        // An empty batch, not a frame: starting again is the Renderer's cue to draw, and
439        // it asks for that frame itself.
440        self.batch().begin();
441        Ok((self.runtime.batch_mut().finish()?, 0))
442    }
443
444    /// Renders what the runtime has waiting, writes the Host's own records after it, and
445    /// closes the batch. The tail every call that renders shares.
446    fn render_and_finish(&mut self, result: i64) -> Result<(&[u8], i64), ProtocolError> {
447        self.batch().begin();
448        self.runtime.render(None);
449        self.flush_messages();
450        self.arm_scheduler_wake();
451        Ok((self.runtime.batch_mut().finish()?, result))
452    }
453
454    /// An empty batch, for an event that changed nothing anyone is reading.
455    fn empty_batch(&mut self) -> Result<(&[u8], i64), ProtocolError> {
456        self.batch().begin();
457        Ok((self.runtime.batch_mut().finish()?, 0))
458    }
459
460    /// Hands a pressed notification to the components listening for one.
461    ///
462    /// No node and no handler: the notification is not in the tree, and the component that
463    /// posted it may be long gone. What it carries is the key, which the application reads.
464    fn notification_activated(
465        &mut self,
466        action: u32,
467        key: &str,
468    ) -> Result<(&[u8], i64), ProtocolError> {
469        let woke = {
470            let _dispatch_guard = EventDispatchGuard::enter();
471            crate::notification::activate(crate::notification::NotificationActivation {
472                key: key.to_owned(),
473                action,
474            })
475        };
476        self.batch().begin();
477        if woke {
478            self.runtime.render(None);
479        }
480        self.flush_messages();
481        self.arm_scheduler_wake();
482        Ok((self.runtime.batch_mut().finish()?, 0))
483    }
484
485    /// Records whether notifications may be shown, and renders the components that asked.
486    ///
487    /// The same shape as the resolved design system: sent once after start and again only
488    /// when it changes, and a repeat wakes nobody.
489    fn publish_notification_permission(
490        &mut self,
491        state: crate::schema::NotificationPermission,
492    ) -> Result<(&[u8], i64), ProtocolError> {
493        self.batch().begin();
494        if crate::notification::publish_permission(state) {
495            self.runtime.render(None);
496        }
497        self.flush_messages();
498        self.arm_scheduler_wake();
499        Ok((self.runtime.batch_mut().finish()?, 0))
500    }
501
502    /// Records which design system the Renderer resolved the theme to.
503    ///
504    /// The same shape as a size class arriving: it belongs to no node and no handler, and
505    /// a repeat of the same answer wakes nothing, so an application that never asks pays
506    /// a comparison once per change and nothing per frame.
507    fn publish_design_system(
508        &mut self,
509        system: crate::schema::DesignSystem,
510    ) -> Result<(&[u8], i64), ProtocolError> {
511        if crate::design::publish(system) {
512            return self.render_and_finish(0);
513        }
514        self.empty_batch()
515    }
516
517    /// Records how large the Renderer is drawing, and wakes the components that read it.
518    ///
519    /// The level the Renderer reports is also kept as the window's own, so a resync
520    /// rebuilds the window at the level the reader is looking at rather than at one the
521    /// application asked for before the reader changed it.
522    fn publish_zoom(&mut self, k: f32, os: f32, level: i32) -> Result<(&[u8], i64), ProtocolError> {
523        let level = i8::try_from(level).map_err(|_| ProtocolError::InvalidValueKind(0))?;
524        if self.window.zoom_level.is_some() {
525            self.window.zoom_level = Some(level);
526        }
527        if crate::zoom::publish(crate::zoom::Zoom { k, os, level }) {
528            return self.render_and_finish(0);
529        }
530        self.empty_batch()
531    }
532
533    /// Records how large one window is drawing, and wakes the components in that window
534    /// that read it.
535    fn publish_window_zoom(
536        &mut self,
537        window: u32,
538        k: f32,
539        os: f32,
540        level: i32,
541    ) -> Result<(&[u8], i64), ProtocolError> {
542        let level = i8::try_from(level).map_err(|_| ProtocolError::InvalidValueKind(0))?;
543        if crate::zoom::publish_window(window, crate::zoom::Zoom { k, os, level }) {
544            return self.render_and_finish(0);
545        }
546        self.empty_batch()
547    }
548
549    /// Hands a window nobody has a node for to the components that listen for one.
550    fn window_requested(&mut self, key: &str, request: u32) -> Result<(&[u8], i64), ProtocolError> {
551        let requested = crate::windows::WindowRequest {
552            key: key.to_owned(),
553            request,
554        };
555        let mut delivered = false;
556        {
557            let _dispatch_guard = EventDispatchGuard::enter();
558            self.runtime
559                .run_in_context(&mut || delivered = crate::windows::deliver_request(&requested));
560        }
561        if !delivered {
562            return self.empty_batch();
563        }
564        self.render_and_finish(0)
565    }
566
567    /// Tells the components that listen that the application was opened with no window.
568    fn application_reopen(&mut self) -> Result<(&[u8], i64), ProtocolError> {
569        let mut delivered = false;
570        {
571            let _dispatch_guard = EventDispatchGuard::enter();
572            self.runtime
573                .run_in_context(&mut || delivered = crate::windows::deliver_reopen());
574        }
575        if !delivered {
576            return self.empty_batch();
577        }
578        self.render_and_finish(0)
579    }
580
581    pub fn dispatch(&mut self, event: HostEvent<'_>) -> Result<(&[u8], i64), ProtocolError> {
582        let _call = HostCallGuard::enter();
583        // These address the Host itself: no node, no handler, and an answer before
584        // anything is looked up.
585        match event.payload {
586            EventPayload::Resync => return self.resync(),
587            EventPayload::DesignSystemResolved(system) => {
588                return self.publish_design_system(system);
589            }
590            EventPayload::LifecycleStart => return self.set_lifecycle_running(true),
591            EventPayload::LifecycleStop => return self.set_lifecycle_running(false),
592            EventPayload::NotificationActivated { action, key } => {
593                if event.node_id != 0 || event.handler_id != 0 {
594                    return Err(ProtocolError::InvalidValueKind(0));
595                }
596                return self.notification_activated(action, key);
597            }
598            EventPayload::NotificationPermissionChanged(state) => {
599                if event.node_id != 0 || event.handler_id != 0 {
600                    return Err(ProtocolError::InvalidValueKind(0));
601                }
602                return self.publish_notification_permission(state);
603            }
604            // About the platform, like the permission above: no node and no handler, and
605            // a repeat of the same answer wakes nobody.
606            EventPayload::ReducedMotionChanged(state) => {
607                if event.node_id != 0 || event.handler_id != 0 {
608                    return Err(ProtocolError::InvalidValueKind(0));
609                }
610                self.batch().begin();
611                if crate::animation::publish_reduced_motion(state) {
612                    self.runtime.render(None);
613                }
614                self.flush_messages();
615                self.arm_scheduler_wake();
616                return Ok((self.runtime.batch_mut().finish()?, 0));
617            }
618            // A moment in an animation the Renderer is playing. It names the node the
619            // animation plays on but no handler: whoever registered for animation events
620            // hears about it, and what they change is rendered in the same call, so a
621            // handler that starts the next animation does not leave a frame between them.
622            EventPayload::AnimationEvent {
623                animation_id,
624                kind,
625                property,
626                slot,
627                iteration,
628                elapsed_ms,
629                time_nanos,
630            } => {
631                if event.handler_id != 0 {
632                    return Err(ProtocolError::InvalidValueKind(0));
633                }
634                let moment = crate::animation::AnimationMoment {
635                    node_id: event.node_id,
636                    animation_id,
637                    kind,
638                    property,
639                    slot,
640                    iteration,
641                    elapsed_ms,
642                    time_nanos,
643                };
644                let _dispatch_guard = EventDispatchGuard::enter();
645                let mut delivered = false;
646                self.runtime
647                    .run_in_context(&mut || delivered = crate::animation::deliver(moment));
648                if !delivered {
649                    return self.empty_batch();
650                }
651                return self.render_and_finish(0);
652            }
653            EventPayload::ZoomChanged { k, os, level } => {
654                if event.handler_id != 0 {
655                    return Err(ProtocolError::InvalidValueKind(0));
656                }
657                // In a multi-window application the report names the window it is about
658                //; a single window's is node 0.
659                if event.node_id != 0 {
660                    if !self.runtime.is_window(event.node_id) {
661                        return Err(ProtocolError::InvalidValueKind(0));
662                    }
663                    return self.publish_window_zoom(event.node_id, k, os, level);
664                }
665                return self.publish_zoom(k, os, level);
666            }
667            // The two events that belong to the application and to no window: no node and
668            // no handler, and an answer before anything is looked up.
669            EventPayload::WindowRequested { key, request } => {
670                if event.node_id != 0 || event.handler_id != 0 {
671                    return Err(ProtocolError::InvalidValueKind(0));
672                }
673                return self.window_requested(key, request);
674            }
675            EventPayload::ApplicationReopen => {
676                if event.node_id != 0 || event.handler_id != 0 {
677                    return Err(ProtocolError::InvalidValueKind(0));
678                }
679                return self.application_reopen();
680            }
681            _ => {}
682        }
683        // The window's size belongs to no node and no handler: the Renderer measures the
684        // root content and reports it. It takes the same synchronous path as every other
685        // event, so the batch it produces is applied in the frame that asked for it.
686        if let EventPayload::WindowSizeChanged {
687            width_dp,
688            height_dp,
689            class,
690            height_class,
691        } = event.payload
692        {
693            // Node id zero is the window; anything else is one of its nodes. One event
694            // for both, because they are the same fact measured at two scales and a
695            // second way of saying it would be a second thing to keep in step.
696            let size = crate::window::WindowSize::new(width_dp, height_dp, class, height_class);
697            // A window's report names the window's node, and that is not an
698            // observed node: it is the same fact for a window of its own.
699            if event.node_id != 0 && self.runtime.is_window(event.node_id) {
700                if !crate::window::publish_window(event.node_id, size) {
701                    return self.empty_batch();
702                }
703                return self.render_and_finish(0);
704            }
705            if event.node_id != 0 {
706                let woke = self
707                    .runtime
708                    .size_token(event.node_id)
709                    .is_some_and(|token| crate::window::publish_node(token, size));
710                if !woke {
711                    return self.empty_batch();
712                }
713                return self.render_and_finish(0);
714            }
715            crate::window::publish(size);
716            return self.render_and_finish(0);
717        }
718        // The action on a transient message. It belongs to no node, because the message is
719        // not in the tree, so it is found by its handler id alone and the event carries the
720        // "no node" id the Renderer uses for anything the tree does not own.
721        let message_action = match event.payload {
722            EventPayload::Clicked => crate::message::take_action(event.handler_id),
723            _ => None,
724        };
725        if let Some(mut action) = message_action {
726            if event.node_id != 0 {
727                return Err(ProtocolError::InvalidValueKind(0));
728            }
729            let _dispatch_guard = EventDispatchGuard::enter();
730            self.runtime.run_in_context(&mut *action);
731            return self.render_and_finish(0);
732        }
733        // Everything else names a handler on a node, and handlers belong to the runtime.
734        if let EventPayload::ProtocolError { .. } = event.payload {
735            return Err(ProtocolError::InvalidValueKind(0));
736        }
737        let _dispatch_guard = EventDispatchGuard::enter();
738        let result = self.runtime.handle_event(&event)?;
739        self.render_and_finish(result)
740    }
741
742    pub fn render_frame(&mut self, frame_time_nanos: u64) -> Result<&[u8], ProtocolError> {
743        let _call = HostCallGuard::enter();
744        if LIFECYCLE_SUPPRESSED.load(Ordering::Acquire) {
745            // The platform has the UI off screen and the Renderer called anyway, which it
746            // does for one reason: a notification was queued, and a notification is what
747            // has to get out while nobody is looking. Only that goes. The components are
748            // not rendered and the held frame request is left held, so timers and
749            // animations stay suppressed until start delivers it.
750            self.batch().begin();
751            self.flush_notifications();
752            return self.runtime.batch_mut().finish();
753        }
754        FRAME_REQUESTED.store(false, Ordering::Release);
755        EVENT_DISPATCH_ACTIVE.store(false, Ordering::Release);
756        DEFERRED_FRAME_REQUEST.store(false, Ordering::Release);
757        self.batch().begin();
758        self.runtime.render(Some(frame_time_nanos));
759        self.flush_pending_appends();
760        self.flush_messages();
761        self.arm_scheduler_wake();
762        self.runtime.batch_mut().finish()
763    }
764
765    /// Queues a streamed tail for the next frame. Tokens arriving inside one frame are
766    /// merged into a single `AppendText` record, so a token never costs a batch of its own.
767    pub fn append_text(&mut self, node_id: u32, tail: &str) {
768        match self
769            .pending_appends
770            .iter_mut()
771            .find(|pending| pending.node_id == node_id)
772        {
773            Some(pending) => {
774                pending.text.push_str(tail);
775                pending.dirty = true;
776            }
777            None => self.pending_appends.push(PendingAppend {
778                node_id,
779                text: tail.to_owned(),
780                dirty: true,
781            }),
782        }
783        // Repeated calls collapse into one frame request; see `request_frame_from_worker`.
784        request_frame_from_worker();
785    }
786
787    /// Writes whatever the tree asked to say during this call into the batch it produced.
788    ///
789    /// A message therefore arrives in the same call as the change it is about, which is
790    /// what makes "deleted" and the row disappearing one frame rather than two.
791    fn flush_messages(&mut self) {
792        // A theme the application changed during this call, one record for the whole
793        // tree. Kept as the Host's own as well, so a resync rebuilds with it.
794        if let Some(theme) = crate::theme::take_pending() {
795            self.theme = theme;
796            self.batch().set_theme(theme);
797        }
798        // A zoom level the application asked for, on the window record that carries it.
799        if let Some(level) = crate::zoom::take_pending_level() {
800            self.window.zoom_level = Some(level);
801            let window = self.window;
802            self.batch().set_window(window);
803        }
804        // What the application asked of a window that happens once: forward, a theme of its
805        // own, a zoom level of its own.
806        {
807            let batch = self.runtime.batch_mut();
808            crate::windows::drain_commands(|command| match command {
809                crate::windows::Command::Focus(node_id) => {
810                    batch.write(Mutation::FocusWindow { node_id });
811                }
812                crate::windows::Command::Theme(node_id, theme) => {
813                    batch.write(Mutation::SetWindowTheme { node_id, theme });
814                }
815                crate::windows::Command::ZoomLevel(node_id, level) => {
816                    batch.write(Mutation::SetProp {
817                        node_id,
818                        property: crate::schema::PropertyKind::WindowZoomLevel,
819                        value: PropertyValue::Integer(i64::from(level)),
820                    });
821                }
822            });
823        }
824        let batch = self.runtime.batch_mut();
825        // Registrations first. Not because the Renderer needs them first, it applies the
826        // whole batch before drawing any of it, but because a batch read by a person
827        // debugging one reads in the order the screen was built.
828        crate::asset::drain(|pending| {
829            batch.register_asset(pending.asset_id, pending.kind, pending.bytes);
830        });
831        crate::asset::drain_releases(|asset_id| batch.release_asset(asset_id));
832        crate::message::drain(|message| {
833            batch.show_message(
834                message.handler_id,
835                &message.text,
836                &message.action,
837                message.duration,
838            );
839        });
840        self.flush_notifications();
841        // Animations an application started from a handler, a component or a worker.
842        let batch = self.runtime.batch_mut();
843        crate::animation::drain(|mutation| batch.write(mutation));
844    }
845
846    /// Writes the notification commands waiting in the Host into this batch.
847    ///
848    /// Workers queue them from their own threads; this is the one place they leave, on the
849    /// UI thread, inside a call. One atomic read when there are none.
850    fn flush_notifications(&mut self) {
851        let batch = self.runtime.batch_mut();
852        let posts = crate::notification::drain(|command| batch.notification(command));
853        crate::notification::note_posted(posts);
854    }
855
856    fn flush_pending_appends(&mut self) {
857        let batch = self.runtime.batch_mut();
858        for pending in &self.pending_appends {
859            if pending.dirty {
860                batch.append_text_node(pending.node_id, &pending.text);
861            }
862        }
863        // Buffers are kept so steady-state streaming reuses their capacity and a streamed
864        // token does not allocate.
865        for pending in &mut self.pending_appends {
866            pending.text.clear();
867            pending.dirty = false;
868        }
869    }
870
871    /// Registers one asset and returns the batch that carries it.
872    ///
873    /// The bytes are copied once here and once more by the Renderer into its cache. After
874    /// that the id is all that travels, so drawing the same image every frame costs a
875    /// fixed-layout property record and nothing else.
876    pub fn register_asset(
877        &mut self,
878        asset_id: u32,
879        kind: AssetKind,
880        bytes: &[u8],
881    ) -> Result<&[u8], ProtocolError> {
882        let batch = self.runtime.batch_mut();
883        batch.begin();
884        batch.register_asset(asset_id, kind, bytes);
885        batch.finish()
886    }
887
888    /// Registers an icon by the meaning it carries. The Renderer holds the artwork for
889    /// every design system, so what crosses is the role and not a picture or a name.
890    pub fn register_icon(&mut self, asset_id: u32, role: IconRole) -> Result<&[u8], ProtocolError> {
891        self.register_asset(
892            asset_id,
893            AssetKind::VectorIcon,
894            &(role as u16).to_le_bytes(),
895        )
896    }
897
898    /// Drops the asset from the Renderer's cache. Using the id afterwards is a reported
899    /// protocol error.
900    pub fn release_asset(&mut self, asset_id: u32) -> Result<&[u8], ProtocolError> {
901        let batch = self.runtime.batch_mut();
902        batch.begin();
903        batch.release_asset(asset_id);
904        batch.finish()
905    }
906
907    pub fn set_text(
908        &mut self,
909        node_id: u32,
910        text: &str,
911        selection: Option<Selection>,
912    ) -> Result<&[u8], ProtocolError> {
913        let batch = self.runtime.batch_mut();
914        batch.begin();
915        batch.set_text_node(node_id, text, selection);
916        batch.finish()
917    }
918
919    fn arm_scheduler_wake(&mut self) {
920        let mut context = Context::from_waker(&self.frame_waker);
921        if self.runtime.poll_work(&mut context).is_ready() {
922            request_frame_from_worker();
923        }
924    }
925}
926
927/// Holds the thread's `Host` and, crucially, keeps it out of thread-local teardown.
928///
929/// Dropping a runtime can reach back into thread-locals of its own: a Dioxus
930/// `VirtualDom` does. Thread-local destruction order is unspecified, so if the UI thread
931/// ends without `compose_rust_host_shutdown`, that drop can run after those locals are
932/// already gone and panic with "cannot access a TLS value during or after destruction".
933/// A panic in a destructor is non-unwinding: it aborts the process, which
934/// is exactly what must not happen: a protocol or teardown fault has to stay recoverable.
935///
936/// So the slot empties itself and leaks the `Host` when the thread is tearing down. An
937/// orderly `shutdown` still drops it properly; only the unorderly path leaks, and that
938/// path is a thread ending anyway.
939struct HostSlot(RefCell<Option<Host>>);
940
941impl Drop for HostSlot {
942    fn drop(&mut self) {
943        std::mem::forget(self.0.borrow_mut().take());
944    }
945}
946
947impl std::ops::Deref for HostSlot {
948    type Target = RefCell<Option<Host>>;
949
950    fn deref(&self) -> &Self::Target {
951        &self.0
952    }
953}
954
955/// The launched application, as the factory for its runtime.
956///
957/// The runtime and every `compose_rust_host_*` call run on the Renderer UI thread, and
958/// that thread is not the one that called `launch`: with `LoopMode::Renderer` the Rust
959/// main thread blocks inside `compose_rust_renderer_run` while Compose composes on the
960/// toolkit's own thread. So the factory, unlike the `Host` it builds, has to be reachable
961/// across threads, which is why it is `Send + Sync` and the runtime it makes is not.
962///
963/// `shutdown` deliberately leaves this set: it belongs to `launch`, not to one UI thread's
964/// `Host`. That is also what `LoopMode::Platform` needs, where the Renderer may tear the
965/// Host down and initialize it again (Android recreates its surface on a configuration
966/// change) without relaunching.
967static APP: Mutex<Option<RuntimeFactory>> = Mutex::new(None);
968
969/// Chosen by `LaunchBuilder::with_theme`, read once when the Host is built.
970static THEME: Mutex<Theme> = Mutex::new(Theme::unified(crate::schema::DesignSystem::Material3));
971
972/// Chosen by `LaunchBuilder::with_window`, read once when the Host is built.
973static WINDOW: Mutex<crate::schema::Window> = Mutex::new(crate::schema::Window::new());
974
975thread_local! {
976    static HOST: HostSlot = const { HostSlot(RefCell::new(None)) };
977}
978
979fn launched_app() -> Option<RuntimeFactory> {
980    APP.lock().map_or(None, |app| (*app).clone())
981}
982
983fn launched_window() -> crate::schema::Window {
984    WINDOW
985        .lock()
986        .map_or_else(|error| *error.into_inner(), |window| *window)
987}
988
989fn launched_theme() -> Theme {
990    THEME
991        .lock()
992        .map_or_else(|error| *error.into_inner(), |theme| *theme)
993}
994
995#[derive(Clone, Copy, Debug)]
996pub struct LaunchBuilder {
997    mode: LoopMode,
998    theme: Theme,
999    window: crate::schema::Window,
1000}
1001
1002impl Default for LaunchBuilder {
1003    fn default() -> Self {
1004        Self {
1005            mode: LoopMode::Renderer,
1006            theme: Theme::default(),
1007            window: crate::schema::Window::new(),
1008        }
1009    }
1010}
1011
1012impl LaunchBuilder {
1013    pub fn new() -> Self {
1014        Self::default()
1015    }
1016
1017    pub fn with_mode(mut self, mode: LoopMode) -> Self {
1018        self.mode = mode;
1019        self
1020    }
1021
1022    /// `Theme::unified` for one design system everywhere, `Theme::adaptive` to follow the
1023    /// host platform. Not calling this follows the host platform, falling back to
1024    /// Material 3.
1025    pub fn with_theme(mut self, theme: Theme) -> Self {
1026        self.theme = theme;
1027        self
1028    }
1029
1030    /// What the application asks of its own window: its size, and whether it wears the
1031    /// platform's title bar or has content run into it.
1032    ///
1033    /// Not calling this gets a modern window that the Renderer sizes. The window belongs
1034    /// to the Renderer and most of what it looks like is the design system's, so what can
1035    /// be said here is short on purpose: nothing names a colour, a corner, or where the
1036    /// window buttons go.
1037    pub fn with_window(mut self, window: crate::schema::Window) -> Self {
1038        let (mode, policy) = (self.window.mode, self.window.last_window_closed);
1039        self.window = window;
1040        // A builder that already asked for windows keeps asking, whichever order the two
1041        // calls came in.
1042        if mode == crate::schema::WindowMode::Windows {
1043            self.window.mode = mode;
1044            self.window.last_window_closed = policy;
1045        }
1046        self
1047    }
1048
1049    /// Runs several windows in one process: the root's children are `Window`
1050    /// nodes, each a platform window, and they share one composition and one set of
1051    /// states. Without this the root's content fills the one window.
1052    ///
1053    /// The window that closes last ends the application, except on macOS where the
1054    /// application stays; [`LaunchBuilder::on_last_window_closed`] chooses.
1055    pub fn windows(mut self) -> Self {
1056        self.window = self.window.with_windows();
1057        self
1058    }
1059
1060    /// What happens when the last `Window` node is removed. Mobile platforms
1061    /// ignore it: the system decides how long the process lives.
1062    pub fn on_last_window_closed(mut self, policy: crate::schema::LastWindowClosed) -> Self {
1063        self.window = self.window.with_last_window_closed(policy);
1064        self
1065    }
1066
1067    /// Runs the application whose tree `runtime` builds. Does not return while it is
1068    /// running, and ends the process with a failing status if the renderer loop could
1069    /// not run at all.
1070    ///
1071    /// `runtime` is called on the Renderer's UI thread each time a Host is made: once at
1072    /// start, and again whenever the Renderer asks for the whole tree back.
1073    ///
1074    /// Exiting rather than returning is the point. Launching is the last statement of
1075    /// `main` in every application that uses this crate, so returning from it means `main`
1076    /// returns, which means the process exits 0. A window that never opened would then be
1077    /// indistinguishable from a program that did its work and stopped.
1078    pub fn launch_runtime(self, runtime: impl Fn() -> Box<dyn Runtime> + Send + Sync + 'static) {
1079        let status = self.try_launch_runtime(runtime);
1080        if status != STATUS_OK {
1081            std::process::exit(EXIT_FAILURE);
1082        }
1083    }
1084
1085    /// [`LaunchBuilder::launch_runtime`] without the exit: the status the renderer loop
1086    /// ended with, handed back for a caller that has its own idea of what to do with it.
1087    pub fn try_launch_runtime(
1088        self,
1089        runtime: impl Fn() -> Box<dyn Runtime> + Send + Sync + 'static,
1090    ) -> i32 {
1091        let factory: RuntimeFactory = Arc::new(runtime);
1092        if let Ok(mut slot) = APP.lock() {
1093            *slot = Some(factory);
1094        }
1095        if let Ok(mut slot) = THEME.lock() {
1096            *slot = self.theme;
1097        }
1098        // A palette that leaves something unreadable is said once, at start, in a debug
1099        // build. It does not stop the launch: a screen whose contrast is a little short is
1100        // better than no screen, and what to do about it is the application's call.
1101        #[cfg(debug_assertions)]
1102        for line in palette_report(&self.theme) {
1103            eprintln!("{line}");
1104        }
1105        if let Ok(mut slot) = WINDOW.lock() {
1106            *slot = self.window;
1107        }
1108        // Under `LoopMode::Platform` the platform owns the loop and calls in through the
1109        // boundary when it is ready. There is nothing to run and nothing to fail.
1110        if self.mode != LoopMode::Renderer {
1111            return STATUS_OK;
1112        }
1113        (renderer_api().run)()
1114    }
1115}
1116
1117/// [`LaunchBuilder::launch_runtime`] with every choice left to its default.
1118pub fn launch_runtime(runtime: impl Fn() -> Box<dyn Runtime> + Send + Sync + 'static) {
1119    LaunchBuilder::new().launch_runtime(runtime);
1120}
1121
1122/// What a theme's palette leaves short of contrast, as lines to print.
1123///
1124/// Every system an adaptive theme could land on is checked, because which one it lands on
1125/// is decided by the Renderer on a machine this code has not seen. A unified theme is
1126/// checked against the one system it names. Empty where there is no palette or nothing is
1127/// short.
1128pub fn palette_report(theme: &Theme) -> Vec<String> {
1129    let Some(palette) = theme.palette else {
1130        return Vec::new();
1131    };
1132    let systems: Vec<crate::schema::DesignSystem> = if theme.adaptive {
1133        crate::schema::DESIGN_SYSTEM_SCHEMA
1134            .iter()
1135            .filter_map(|variant| crate::schema::DesignSystem::try_from(variant.tag).ok())
1136            .collect()
1137    } else {
1138        vec![theme.design_system]
1139    };
1140    systems
1141        .into_iter()
1142        .flat_map(|system| {
1143            palette.check(system).into_iter().map(move |violation| {
1144                format!("compose-rust: palette under {system:?}: {violation}")
1145            })
1146        })
1147        .collect()
1148}
1149
1150fn parse_handshake(bytes: &[u8]) -> Result<LoopMode, ProtocolError> {
1151    if bytes.len() < 12 {
1152        return Err(ProtocolError::Truncated);
1153    }
1154    let hash = u64::from_le_bytes(
1155        bytes[0..8]
1156            .try_into()
1157            .map_err(|_| ProtocolError::Truncated)?,
1158    );
1159    let version = u16::from_le_bytes(
1160        bytes[8..10]
1161            .try_into()
1162            .map_err(|_| ProtocolError::Truncated)?,
1163    );
1164    if hash != SCHEMA_HASH || version != PROTOCOL_VERSION {
1165        // Say what did not match, and say it here rather than leaving the status code to
1166        // carry it. A mismatch means the two halves were generated from different
1167        // versions of the schema, which on a desktop build means a renderer library
1168        // compiled before the last codegen run. The window still opens, because the
1169        // renderer stands it up before it asks, so the only thing on screen is an empty
1170        // page in the default theme: a symptom that looks like a blank application rather
1171        // than like a stale build, and one that cost a morning to read the first time.
1172        eprintln!(
1173            "compose-rust: the renderer was built from a different schema than this              program. It sent hash {hash:#x} version {version}, and this build expects              hash {SCHEMA_HASH:#x} version {PROTOCOL_VERSION}. Rebuild the renderer after              running codegen; if it was already rebuilt, its build directory is holding a              cached copy of the generated protocol and has to be cleared."
1174        );
1175        return Err(ProtocolError::InvalidEnvelope);
1176    }
1177    match bytes[10] {
1178        0 => Ok(LoopMode::Renderer),
1179        1 => Ok(LoopMode::Platform),
1180        other => Err(ProtocolError::InvalidValueKind(u16::from(other))),
1181    }
1182}
1183
1184unsafe fn input_slice<'a>(ptr: *const u8, len: u32) -> Result<&'a [u8], ProtocolError> {
1185    if len == 0 {
1186        return Ok(&[]);
1187    }
1188    if ptr.is_null() {
1189        return Err(ProtocolError::Truncated);
1190    }
1191    // SAFETY: The C caller promises a readable buffer of `len` bytes for this call.
1192    Ok(unsafe { std::slice::from_raw_parts(ptr, len as usize) })
1193}
1194
1195unsafe fn write_batch(
1196    out: *mut MutationBatch,
1197    bytes: &[u8],
1198    result: i64,
1199) -> Result<(), ProtocolError> {
1200    if out.is_null() {
1201        return Err(ProtocolError::Truncated);
1202    }
1203    let len = u32::try_from(bytes.len()).map_err(|_| ProtocolError::LengthOverflow)?;
1204    // SAFETY: Null was rejected and the C caller promises writable storage.
1205    unsafe {
1206        out.write(MutationBatch {
1207            ptr: bytes.as_ptr(),
1208            len,
1209            result,
1210        });
1211    }
1212    Ok(())
1213}
1214
1215/// A call-order failure is not a malformed message: the boundary declares distinct statuses
1216/// so the Renderer can tell "you called me too early" from "your bytes were wrong". The
1217/// closures below therefore yield a status directly.
1218fn ffi_status(operation: impl FnOnce() -> Result<(), i32>) -> i32 {
1219    // There is no channel for the Host to raise a ProtocolError event back to the Renderer:
1220    // the boundary is a synchronous call that returns a status, and events only travel
1221    // Renderer to Host. Malformed calls therefore return STATUS_PROTOCOL_ERROR.
1222    match catch_unwind(AssertUnwindSafe(operation)) {
1223        Ok(Ok(())) => STATUS_OK,
1224        Ok(Err(status)) => status,
1225        Err(_) => STATUS_PANIC,
1226    }
1227}
1228
1229/// Every `ProtocolError` leaving an export is reported as one status code.
1230fn protocol_error(_error: ProtocolError) -> i32 {
1231    STATUS_PROTOCOL_ERROR
1232}
1233
1234#[unsafe(no_mangle)]
1235/// Initializes the UI-thread Host from the handshake and returns the initial batch.
1236///
1237/// # Safety
1238/// `handshake` must address `len` readable bytes and `out` must be writable.
1239pub unsafe extern "C" fn compose_rust_host_init(
1240    handshake: *const u8,
1241    len: u32,
1242    out: *mut MutationBatch,
1243) -> i32 {
1244    ffi_status(|| {
1245        // SAFETY: Validated according to the function's C ABI contract.
1246        let bytes = unsafe { input_slice(handshake, len) }.map_err(protocol_error)?;
1247        let _mode = parse_handshake(bytes).map_err(protocol_error)?;
1248        let factory = launched_app().ok_or(STATUS_NOT_INITIALIZED)?;
1249        HOST.with(|slot| {
1250            let mut host_slot = slot.borrow_mut();
1251            if host_slot.is_some() {
1252                return Err(STATUS_ALREADY_INITIALIZED);
1253            }
1254            let mut host = Host::from_factory(factory, launched_theme());
1255            let batch = host.rebuild().map_err(protocol_error)?;
1256            // SAFETY: `out` is checked before writing.
1257            unsafe { write_batch(out, batch, 0) }.map_err(protocol_error)?;
1258            *host_slot = Some(host);
1259            Ok(())
1260        })
1261    })
1262}
1263
1264#[unsafe(no_mangle)]
1265/// Dispatches one encoded event synchronously and returns its diff batch.
1266///
1267/// # Safety
1268/// `event` must address `len` readable bytes and `out` must be writable.
1269pub unsafe extern "C" fn compose_rust_host_dispatch_event(
1270    event: *const u8,
1271    len: u32,
1272    out: *mut MutationBatch,
1273) -> i32 {
1274    ffi_status(|| {
1275        // SAFETY: Validated according to the function's C ABI contract.
1276        let bytes = unsafe { input_slice(event, len) }.map_err(protocol_error)?;
1277        HOST.with(|slot| {
1278            let mut host_slot = slot.borrow_mut();
1279            let host = host_slot.as_mut().ok_or(STATUS_NOT_INITIALIZED)?;
1280            let (batch, result) = host.dispatch_event(bytes).map_err(protocol_error)?;
1281            // SAFETY: `out` is checked before writing.
1282            unsafe { write_batch(out, batch, result) }.map_err(protocol_error)
1283        })
1284    })
1285}
1286
1287#[unsafe(no_mangle)]
1288/// Renders work scheduled before the current platform frame.
1289///
1290/// # Safety
1291/// `out` must point to writable storage for one [`MutationBatch`].
1292pub unsafe extern "C" fn compose_rust_host_render_frame(
1293    frame_time_nanos: u64,
1294    out: *mut MutationBatch,
1295) -> i32 {
1296    ffi_status(|| {
1297        HOST.with(|slot| {
1298            let mut host_slot = slot.borrow_mut();
1299            let host = host_slot.as_mut().ok_or(STATUS_NOT_INITIALIZED)?;
1300            let batch = host
1301                .render_frame(frame_time_nanos)
1302                .map_err(protocol_error)?;
1303            // SAFETY: `out` is checked before writing.
1304            unsafe { write_batch(out, batch, 0) }.map_err(protocol_error)
1305        })
1306    })
1307}
1308
1309#[unsafe(no_mangle)]
1310/// Releases a batch after the Renderer has applied it on the same call stack.
1311///
1312/// # Safety
1313/// `batch` must be null or point to writable storage for one [`MutationBatch`].
1314pub unsafe extern "C" fn compose_rust_host_release_batch(batch: *mut MutationBatch) {
1315    let _ = catch_unwind(AssertUnwindSafe(|| {
1316        if !batch.is_null() {
1317            // SAFETY: The caller supplied its own writable MutationBatch.
1318            unsafe { batch.write(MutationBatch::default()) };
1319        }
1320    }));
1321}
1322
1323/// The UI thread's batch arena.
1324///
1325/// Not a boundary entry point: it is crate-internal, and the generated Android shims use
1326/// it to tell the Renderer where the arena is so it can map it once instead of per call.
1327pub fn current_arena() -> (*const u8, usize) {
1328    HOST.with(|slot| {
1329        slot.borrow()
1330            .as_ref()
1331            .map_or((std::ptr::null(), 0), Host::arena)
1332    })
1333}
1334
1335#[unsafe(no_mangle)]
1336pub extern "C" fn compose_rust_host_shutdown() {
1337    let _ = catch_unwind(AssertUnwindSafe(|| {
1338        HOST.with(|slot| *slot.borrow_mut() = None);
1339        FRAME_REQUESTED.store(false, Ordering::Release);
1340    }));
1341}
1342
1343#[cfg(test)]
1344mod tests {
1345    use super::*;
1346
1347    /// `APP` is process-global, as launching is, so two tests that launch at the same
1348    /// time would each see the other's. Poisoning is ignored: a failing test has already
1349    /// reported itself, and the rest still need the lock.
1350    static LAUNCH: Mutex<()> = Mutex::new(());
1351
1352    fn launch_guard() -> std::sync::MutexGuard<'static, ()> {
1353        LAUNCH
1354            .lock()
1355            .unwrap_or_else(|poisoned| poisoned.into_inner())
1356    }
1357
1358    #[test]
1359    fn malformed_ffi_input_returns_protocol_error() {
1360        let mut output = MutationBatch::default();
1361        // SAFETY: The test passes a valid output pointer and intentionally null input.
1362        let status = unsafe { compose_rust_host_dispatch_event(std::ptr::null(), 4, &mut output) };
1363        assert_eq!(status, STATUS_PROTOCOL_ERROR);
1364    }
1365
1366    #[test]
1367    fn all_exports_isolate_invalid_calls() {
1368        let _launch = launch_guard();
1369        let mut output = MutationBatch::default();
1370        // SAFETY: Each call uses null or valid test-owned pointers as documented.
1371        unsafe {
1372            assert_eq!(
1373                compose_rust_host_init(std::ptr::null(), 1, &mut output),
1374                STATUS_PROTOCOL_ERROR
1375            );
1376            // No handshake has run on this thread, so this is a call-order error.
1377            assert_eq!(
1378                compose_rust_host_render_frame(0, &mut output),
1379                STATUS_NOT_INITIALIZED
1380            );
1381            compose_rust_host_release_batch(std::ptr::null_mut());
1382        }
1383        compose_rust_host_shutdown();
1384    }
1385}
1386
1387/// The design system a demonstration should start in.
1388///
1389/// An application picks its own theme and never needs this. The samples do, because the
1390/// point of a sample is to show what one declaration looks like under each system, and on
1391/// any given machine the adaptive default can only ever show you one of them. Material 3
1392/// and Fluent went unseen for weeks for exactly that reason: everything was checked on a
1393/// Mac, so everything was Cupertino.
1394///
1395/// `DXC_DESIGN` names the system. Anything else, including nothing, adapts to the host.
1396/// `DXC_SCHEME` names `light` or `dark`; anything else leaves the reader's own setting
1397/// alone.
1398pub fn demo_theme() -> Theme {
1399    demo_theme_for(Theme::adaptive(crate::schema::DesignSystem::Material3))
1400}
1401
1402/// The same, for a demonstration that has a theme of its own to start from.
1403///
1404/// A sample whose design is one particular design system in one particular colour scheme
1405/// says so, and that declaration is the sample. But the machine it is being looked at on
1406/// can only draw one of the seven at a time, so without a way to point it somewhere else
1407/// the other six are never seen. This keeps the sample's own theme as the answer and lets
1408/// the two variables override the part they name, so what is on screen is either the
1409/// design the sample chose or exactly the one that was asked for.
1410pub fn demo_theme_for(theme: Theme) -> Theme {
1411    use crate::schema::DesignSystem;
1412    let theme = match std::env::var("DXC_DESIGN").as_deref().map(str::trim) {
1413        Ok("material3") => Theme::unified(DesignSystem::Material3),
1414        Ok("cupertino") => Theme::unified(DesignSystem::Cupertino),
1415        Ok("fluent") => Theme::unified(DesignSystem::Fluent),
1416        Ok("gnome") => Theme::unified(DesignSystem::Gnome),
1417        Ok("breeze") => Theme::unified(DesignSystem::Breeze),
1418        Ok("deepin") => Theme::unified(DesignSystem::Deepin),
1419        // Spelled both ways, because the name is two words everywhere it is written down
1420        // and nobody remembers which one a shell variable wants.
1421        Ok("liquidglass") | Ok("liquid-glass") => Theme::unified(DesignSystem::LiquidGlass),
1422        // Naming a system replaces the sample's design system but keeps its colour
1423        // scheme, so asking to see one design does not also change how light it is.
1424        _ => return with_scheme_override(theme),
1425    }
1426    .with_color_scheme(theme.color_scheme);
1427    with_scheme_override(theme)
1428}
1429
1430/// Applies `DXC_SCHEME` if it names one of the two schemes.
1431fn with_scheme_override(theme: Theme) -> Theme {
1432    match std::env::var("DXC_SCHEME").as_deref().map(str::trim) {
1433        Ok("light") => theme.with_color_scheme(crate::schema::ColorScheme::Light),
1434        Ok("dark") => theme.with_color_scheme(crate::schema::ColorScheme::Dark),
1435        _ => theme,
1436    }
1437}
1438
1439#[cfg(test)]
1440mod demo_theme_tests {
1441    use super::*;
1442    use crate::schema::{ColorScheme, DESIGN_SYSTEM_SCHEMA, DesignSystem};
1443    use std::sync::{Mutex, MutexGuard};
1444
1445    /// Held for the length of any test that sets one of the variables.
1446    ///
1447    /// The variables belong to the process, not to the test, so two of these running at
1448    /// once read each other's settings and fail on a value neither of them asked for.
1449    static ENVIRONMENT: Mutex<()> = Mutex::new(());
1450
1451    /// Clears both variables and keeps everyone else out until the guard is dropped.
1452    fn exclusive_environment() -> MutexGuard<'static, ()> {
1453        let guard = ENVIRONMENT
1454            .lock()
1455            .unwrap_or_else(|error| error.into_inner());
1456        // SAFETY: the lock makes this the only thread touching the environment, and the
1457        // variables are read only by the functions called under the same lock.
1458        unsafe {
1459            std::env::remove_var("DXC_DESIGN");
1460            std::env::remove_var("DXC_SCHEME");
1461        }
1462        guard
1463    }
1464
1465    /// Named for what it defends: a sample that cannot be pointed at a design system
1466    /// leaves five of the six unseen on any one machine.
1467    #[test]
1468    fn fr14_a_named_design_system_is_unified_and_anything_else_adapts() {
1469        let _environment = exclusive_environment();
1470        // SAFETY: `exclusive_environment` holds the lock, so nothing else is reading or
1471        // writing these while this runs.
1472        for (name, system) in [
1473            ("material3", DesignSystem::Material3),
1474            ("cupertino", DesignSystem::Cupertino),
1475            ("fluent", DesignSystem::Fluent),
1476            ("gnome", DesignSystem::Gnome),
1477            ("breeze", DesignSystem::Breeze),
1478            ("deepin", DesignSystem::Deepin),
1479            ("liquidglass", DesignSystem::LiquidGlass),
1480            ("liquid-glass", DesignSystem::LiquidGlass),
1481        ] {
1482            unsafe { std::env::set_var("DXC_DESIGN", name) };
1483            assert_eq!(
1484                demo_theme(),
1485                Theme::unified(system),
1486                "{name} is not selectable"
1487            );
1488        }
1489        assert_eq!(
1490            DESIGN_SYSTEM_SCHEMA.len(),
1491            7,
1492            "a system nobody can select goes unseen"
1493        );
1494
1495        unsafe { std::env::set_var("DXC_DESIGN", "nonsense") };
1496        assert_eq!(demo_theme(), Theme::adaptive(DesignSystem::Material3));
1497
1498        unsafe { std::env::remove_var("DXC_DESIGN") };
1499        assert_eq!(demo_theme(), Theme::adaptive(DesignSystem::Material3));
1500    }
1501
1502    /// Named for what it defends: a machine set to dark draws every sample dark, so a
1503    /// design that is meant to be read light cannot be looked at at all without this.
1504    #[test]
1505    fn fr14_a_named_colour_scheme_is_pinned_and_anything_else_is_left_alone() {
1506        let _environment = exclusive_environment();
1507        // SAFETY: as above.
1508        for (name, scheme) in [("light", ColorScheme::Light), ("dark", ColorScheme::Dark)] {
1509            unsafe { std::env::set_var("DXC_SCHEME", name) };
1510            assert_eq!(
1511                demo_theme().color_scheme,
1512                scheme,
1513                "{name} is not selectable"
1514            );
1515        }
1516
1517        unsafe { std::env::set_var("DXC_SCHEME", "nonsense") };
1518        assert_eq!(demo_theme().color_scheme, ColorScheme::FollowSystem);
1519
1520        unsafe { std::env::remove_var("DXC_SCHEME") };
1521        assert_eq!(demo_theme().color_scheme, ColorScheme::FollowSystem);
1522    }
1523
1524    /// A sample that names its own theme keeps it, and each variable overrides only the
1525    /// half it names. Asking to see Liquid Glass must not also throw away the colour
1526    /// scheme the design was drawn for.
1527    #[test]
1528    fn fr14_a_samples_own_theme_survives_everything_the_variables_do_not_name() {
1529        let _environment = exclusive_environment();
1530        let sample = Theme::unified(DesignSystem::Cupertino).with_color_scheme(ColorScheme::Light);
1531        assert_eq!(demo_theme_for(sample), sample);
1532
1533        unsafe { std::env::set_var("DXC_DESIGN", "liquidglass") };
1534        assert_eq!(
1535            demo_theme_for(sample),
1536            Theme::unified(DesignSystem::LiquidGlass).with_color_scheme(ColorScheme::Light)
1537        );
1538
1539        unsafe { std::env::set_var("DXC_SCHEME", "dark") };
1540        assert_eq!(
1541            demo_theme_for(sample),
1542            Theme::unified(DesignSystem::LiquidGlass).with_color_scheme(ColorScheme::Dark)
1543        );
1544
1545        unsafe {
1546            std::env::remove_var("DXC_DESIGN");
1547            std::env::remove_var("DXC_SCHEME");
1548        };
1549    }
1550    /// A renderer built from a different schema is refused, and says so.
1551    ///
1552    /// The refusal already worked. What did not was finding out why: the renderer stands
1553    /// its window up before it asks the Host anything, so a mismatch leaves an empty page
1554    /// in the default theme and a status code, which reads as an application that draws
1555    /// nothing rather than as a build that is out of date. It cost a morning once.
1556    #[test]
1557    fn pr2_a_handshake_from_another_schema_is_refused() {
1558        let mut wrong = Vec::with_capacity(12);
1559        wrong.extend_from_slice(&SCHEMA_HASH.wrapping_add(1).to_le_bytes());
1560        wrong.extend_from_slice(&PROTOCOL_VERSION.to_le_bytes());
1561        wrong.extend_from_slice(&[LoopMode::Renderer as u8, 0]);
1562        assert!(
1563            matches!(
1564                parse_handshake(&wrong),
1565                Err(crate::protocol::ProtocolError::InvalidEnvelope)
1566            ),
1567            "a handshake carrying another schema's hash was accepted, so the two halves \
1568             would go on to disagree about what every record means"
1569        );
1570
1571        let mut right = Vec::with_capacity(12);
1572        right.extend_from_slice(&SCHEMA_HASH.to_le_bytes());
1573        right.extend_from_slice(&PROTOCOL_VERSION.to_le_bytes());
1574        right.extend_from_slice(&[LoopMode::Renderer as u8, 0]);
1575        assert!(
1576            parse_handshake(&right).is_ok(),
1577            "the handshake this build generates is not one it accepts"
1578        );
1579    }
1580}
1581
1582/// Keeps the five exported boundary functions in the final executable.
1583///
1584/// The Renderer resolves them by name once it is loaded, which means nothing in the
1585/// application ever refers to them and the linker is free to conclude they are dead. It
1586/// does exactly that in any binary whose own code happens not to reach them, a test
1587/// harness for an application being the ordinary case, and the result is a process that
1588/// dies on startup with the dynamic loader unable to bind a symbol that should have been
1589/// right there in the executable.
1590///
1591/// Taking their addresses in a static the compiler is told to keep marks them as roots
1592/// for the dead-code pass, which is the whole job. It costs five pointers.
1593#[used]
1594static BOUNDARY_EXPORTS: BoundaryExports = BoundaryExports([
1595    compose_rust_host_init as *const (),
1596    compose_rust_host_dispatch_event as *const (),
1597    compose_rust_host_render_frame as *const (),
1598    compose_rust_host_release_batch as *const (),
1599    compose_rust_host_shutdown as *const (),
1600]);
1601
1602// The same five names, as a linker directive the object file carries.
1603//
1604// The Renderer finds these with GetProcAddress against the running executable, which
1605// reads the PE export table, and an executable has no export table unless the link is
1606// told to make one. The build script asks for that with `/EXPORT:` arguments, and those
1607// reach the binaries of this package and no further: Cargo does not pass a dependency's
1608// link arguments on to whatever depends on it.
1609//
1610// So every application built on this crate linked without an export table, the Renderer
1611// could not reach back into it, and the window came up empty. It looked like it worked
1612// because the only Windows binaries anyone had run were this package's own examples.
1613//
1614// `.drectve` is how an object file carries linker arguments of its own. The MSVC linker
1615// reads that section out of every object it links, so the directive travels inside the
1616// rlib and applies wherever the rlib ends up.
1617#[cfg(all(target_os = "windows", target_env = "msvc"))]
1618core::arch::global_asm!(
1619    // A section of directives and nothing else, in the COFF flags the MSVC linker reads
1620    // them from: information only ("i") and dropped from the image ("n").
1621    //
1622    // Assembled rather than written as a `static` placed in the section. A static is a
1623    // symbol, and a crate's own DLL asks for every symbol it keeps by name; the LLVM linker
1624    // takes the section's bytes as directives, keeps no symbol for them, and the DLL then
1625    // fails to link on a symbol nobody can define.
1626    ".section .drectve,\"yni\"",
1627    ".ascii \" /EXPORT:compose_rust_host_init\"",
1628    ".ascii \" /EXPORT:compose_rust_host_dispatch_event\"",
1629    ".ascii \" /EXPORT:compose_rust_host_render_frame\"",
1630    ".ascii \" /EXPORT:compose_rust_host_release_batch\"",
1631    ".ascii \" /EXPORT:compose_rust_host_shutdown\"",
1632    ".text",
1633);
1634
1635// Which C runtime an application linking the Kotlin/Native Windows renderer gets, said
1636// where the linker reads it from every object: the build script cannot say it, for the
1637// same reason it cannot ask for the exports above.
1638//
1639// Set by the build script when it links that renderer and the application asked for
1640// nothing (`build/windows_crt.rs` says why each of these is here). The Universal C Runtime
1641// stays the DLL that is part of Windows; vcruntime and the C++ standard library are linked
1642// in, so the executable needs no Visual C++ runtime DLL beside it. An application that asked
1643// for `+crt-static` links all of it statically and needs none of these.
1644#[cfg(all(target_os = "windows", target_env = "msvc", windows_crt_linked_in))]
1645core::arch::global_asm!(
1646    ".section .drectve,\"yni\"",
1647    ".ascii \" /NODEFAULTLIB:vcruntime.lib\"",
1648    ".ascii \" /DEFAULTLIB:libvcruntime.lib\"",
1649    ".ascii \" /NODEFAULTLIB:msvcprt.lib\"",
1650    ".ascii \" /NODEFAULTLIB:libcpmt.lib\"",
1651    ".ascii \" /NODEFAULTLIB:libcmt.lib\"",
1652    ".ascii \" /INCLUDE:longjmp\"",
1653    ".text",
1654);
1655
1656// The same for an application that asked for vcruntime from its DLL
1657// (`DXC_WINDOWS_CRT=dynamic`): the stock static C++ library and C runtime that a prebuilt
1658// object may still name as defaults would define, a second time, what the build script's
1659// blanked copy and the DLLs' import libraries already do.
1660#[cfg(all(target_os = "windows", target_env = "msvc", windows_crt_dynamic))]
1661core::arch::global_asm!(
1662    ".section .drectve,\"yni\"",
1663    ".ascii \" /NODEFAULTLIB:libcpmt.lib\"",
1664    ".ascii \" /NODEFAULTLIB:libcmt.lib\"",
1665    ".text",
1666);
1667
1668// Never read. Being referenced is the entire contract.
1669struct BoundaryExports(#[allow(dead_code)] [*const (); 5]);
1670
1671// SAFETY: The addresses are written once, at compile time, and never read. A static has
1672// to be Sync to exist at all, and raw pointers decline to be only because of what they
1673// might point at; these point at code.
1674unsafe impl Sync for BoundaryExports {}