Skip to main content
QAROS Rust API Dev (main) · versions: Dev (main) · Developer Guide

qaros/
pending_sender.rs

1//! Render sender creation without blocking (`qar_render_sender_create_async`), on the shared
2//! [`PendingCall`] engine (`pending.rs`).
3//!
4//! - **Init.** The init and the extensions it points to live in the call's state until the
5//!   callback; the header does not say the start copies them.
6//! - **Device references.** A D3D11 creation keeps the references on its device and context
7//!   until the callback: in the sender it delivers, or, when the pending creation was dropped,
8//!   in the abandoned call its session keeps until the callback has come.
9//! - **Teardown.** The header has the session's destroy end a creation still running with
10//!   `QAR_STATUS_TIMEOUT` and return only after its callback, so a session never waits or leaks
11//!   on its own account; a sender a dropped creation delivered is destroyed by the session's next
12//!   start, poll or [`crate::Session::release_abandoned_creations`], or by its teardown.
13
14use std::cell::Cell;
15use std::rc::Rc;
16use std::time::Duration;
17
18use qaros_sys::{self as sys, QarApi};
19
20use crate::error::{Result, check};
21use crate::library::require_handle;
22use crate::pending::{self, CallSpec, Keep, PendingCall, RawOutcome};
23use crate::render::{
24    GraphicsReferences, RenderSender, RenderSenderConfig, RenderSenderInitStorage, SenderGraphics,
25};
26use crate::session::SessionInner;
27
28const CREATE_ASYNC: &str = "qar_render_sender_create_async";
29
30/// A render sender being created by [`crate::Session::start_render_sender`],
31/// `Session::start_vulkan_render_sender` (feature `vulkan`) or
32/// `Session::start_d3d11_render_sender` (feature `d3d11`). Keeps its session alive, as a sender
33/// does, so dropping the [`crate::Session`] meanwhile is safe. A D3D11 creation also keeps the
34/// device references of its parameters.
35///
36/// [`PendingCall::poll`] never blocks: `None` while the library is still creating the sender,
37/// then the sender or the error it delivered ([`crate::StatusCode::TIMEOUT`] for a cancelled or
38/// timed-out creation). Dropping it before the sender is taken cancels the creation and returns
39/// at once; the session keeps the creation until its callback, and destroys a sender the library
40/// still delivers.
41pub type PendingRenderSender = PendingCall<RenderSender>;
42
43/// Destroys a sender a dropped creation delivered. Only called while its session lives.
44fn release_unwanted_sender(api: &QarApi, sender: usize) {
45    if sender != 0 {
46        // SAFETY: delivered to us, used by nobody, and its session is alive.
47        unsafe { (api.qar_impl_render_stream_handle_destroy)(sender as *mut sys::QarRenderSender) };
48    }
49}
50
51/// Starts `qar_render_sender_create_async` on `session`.
52pub(crate) fn start(
53    session: &Rc<SessionInner>,
54    config: &RenderSenderConfig,
55    graphics: &SenderGraphics,
56    timeout: Option<Duration>,
57    wake: Box<dyn Fn() + Send + Sync>,
58) -> Result<PendingRenderSender> {
59    session.release_delivered_abandoned_calls();
60    let mut storage = RenderSenderInitStorage::new(config, graphics)?;
61    let graphics_api = storage.graphics_api();
62    let init = storage.wired_init();
63    // The sender takes the references on success; a dropped creation hands them to its session.
64    let references = Rc::new(Cell::new(Some(graphics.references())));
65    let finish_references = Rc::clone(&references);
66    let finish_session = Rc::clone(session);
67    let abandon_session = Rc::clone(session);
68    let poll_session = Rc::clone(session);
69    let spec = CallSpec {
70        function: CREATE_ASYNC,
71        progress: None,
72        timeout,
73        wake,
74        keep: Some(Box::new(Keep(storage))),
75        finish: Box::new(move |outcome: RawOutcome| {
76            let api = finish_session.api();
77            if let Err(error) = check(api, CREATE_ASYNC, outcome.result) {
78                release_unwanted_sender(api, outcome.handle);
79                return Err(error);
80            }
81            let handle = require_handle(outcome.handle as *mut sys::QarRenderSender, CREATE_ASYNC)?;
82            Ok(RenderSender::from_created_handle(
83                &finish_session,
84                handle,
85                graphics_api,
86                finish_references.take().unwrap_or_default(),
87            ))
88        }),
89        abandon: Box::new(move |mut call| {
90            let kept: Option<GraphicsReferences> = references.take();
91            call.keep_until_released(Box::new(kept));
92            abandon_session.abandon_call(call);
93        }),
94        before_poll: Box::new(move || poll_session.release_delivered_abandoned_calls()),
95        destroy: Some(release_unwanted_sender),
96    };
97    let api = session.api();
98    let session_handle = session.handle.as_ptr();
99    pending::start(session.library(), spec, |user_state, _progress, token| {
100        // SAFETY: live session; init and what it points to live in the call's state until the
101        // callback; the token outlives the call (the pending creation or its session keeps it).
102        unsafe {
103            (api.qar_impl_render_sender_create_async)(
104                session_handle,
105                init,
106                Some(pending::handle_result_trampoline::<sys::QarRenderSender>),
107                user_state,
108                token,
109            )
110        }
111    })
112}