Skip to main content

graphics/call/
mod.rs

1//! The graphics call executor: one service behind every way a program reaches
2//! the UI.
3//!
4//! A caller names a call by its catalog ID and passes up to
5//! [`MAX_ARGUMENTS`] words. [`execute`] checks the call,
6//! takes the graphics lock, resolves object handles through the caller's
7//! [`State`], runs the call on LVGL and writes the result back. Whatever
8//! differs between callers is behind [`Transport`]: how a pointer, string or
9//! buffer word becomes memory. Wasm guests pass offsets into linear memory
10//! ([`LinearMemory`]); native code passes host pointers.
11//!
12//! The executor owns what every caller needs and none should reimplement:
13//!
14//! * the graphics lock, held for exactly one call;
15//! * the per-process object identity table in [`State`], deleted with the
16//!   process;
17//! * the event queue behind `window_pop_event`;
18//! * the Xila extension calls (IDs from `0xf000`).
19//!
20//! The call table (the [`Function`] enum and its dispatch) is generated at
21//! build time from the same catalog that generates guest bindings.
22
23mod canvas;
24mod error;
25mod event_code;
26mod events;
27mod handlers;
28mod marshal;
29mod state;
30pub(crate) mod transport;
31
32pub use error::{Error, Result};
33pub use event_code::{EVENT_CLOSE_REQUESTED, EventCode};
34pub use state::State;
35pub use transport::{LinearMemory, Transport, Word};
36
37use state::Kind;
38pub(crate) use state::drain_retired_states;
39
40/// How a call reports its result: nothing, a pointer-sized handle, or a
41/// 32-bit value, written at the call's `result` address.
42pub(crate) enum ResultKind {
43    None,
44    Word,
45    U32,
46    I32,
47}
48
49/// One call. `arguments` beyond the function's count are ignored but must be
50/// present; `count` must equal the function's argument count.
51#[derive(Debug, Clone, Copy)]
52pub struct Call {
53    pub function: i32,
54    pub arguments: [Word; MAX_ARGUMENTS],
55    pub count: i32,
56    pub result: Word,
57}
58
59include!(concat!(env!("OUT_DIR"), "/call_table.rs"));
60
61/// Run one call for the process that owns `state`, blocking the thread while
62/// another holder has the graphics lock. A caller on a cooperative executor
63/// takes the lock itself and uses [`execute_locked`].
64///
65/// The result address is validated before anything runs, so a call never has
66/// effects it cannot report.
67pub fn execute(state: &mut State, transport: &mut dyn Transport, call: &Call) -> Result<()> {
68    let (function, manager) = begin(state, transport, call)?;
69    execute_in_session(
70        &mut manager.synchronous_lock(),
71        state,
72        transport,
73        call,
74        function,
75    )
76}
77
78/// Run a call after the caller has acquired the graphics lock.
79pub fn execute_locked(
80    guard: &mut crate::GraphicsGuard<'_>,
81    state: &mut State,
82    transport: &mut dyn Transport,
83    call: &Call,
84) -> Result<()> {
85    let (function, _) = begin(state, transport, call)?;
86    execute_in_session(guard, state, transport, call, function)
87}
88
89fn execute_in_session(
90    guard: &mut crate::GraphicsGuard<'_>,
91    state: &mut State,
92    transport: &mut dyn Transport,
93    call: &Call,
94    function: Function,
95) -> Result<()> {
96    guard
97        .with_lvgl(|access| {
98            state.initialize(access)?;
99            run(state, transport, call, function, access)
100        })
101        .map_err(|_| Error::NativeFailure)?
102}
103
104/// Check the call and find the manager it runs on.
105fn begin(
106    state: &State,
107    transport: &mut dyn Transport,
108    call: &Call,
109) -> Result<(Function, &'static crate::Manager)> {
110    let (function, argument_count, result_kind) =
111        lookup(call.function).ok_or(Error::Unsupported)?;
112    if argument_count != call.count {
113        return Err(Error::InvalidArgumentsCount);
114    }
115    let manager = state.manager.ok_or(Error::Unavailable)?;
116    match result_kind {
117        ResultKind::None => (),
118        ResultKind::Word => transport.validate(call.result, transport.word_size())?,
119        ResultKind::U32 | ResultKind::I32 => transport.validate(call.result, size_of::<u32>())?,
120    }
121    Ok((function, manager))
122}