mln-rs-engine 0.0.19

Part of mln-rs, a Rust port of the MapLibre Native map rendering core. Depend on mln-rs instead.
Documentation
//! The one channel through which what arrives for the tiles reaches the
//! render thread: the replies to the tiles' loads, the workers' replies and lost lanes, glyph responses, and the device fonts
//! chosen for clusters. Senders on any thread push events and wake the
//! renderer; the tile store takes them in when the renderer updates
//! (`TileStore::receive`).

use std::any::Any;
use std::sync::Arc;
use std::sync::mpsc::{Receiver, Sender, channel};

use crate::text::font_stack::FontStack;
use crate::text::glyph::{FontFace, GlyphRange};
use crate::text::glyph_store::GlyphResult;
use crate::worker::job::{JobId, JobTimes, TileKey};
use crate::worker::tile_workers::{Lane, ReplyBody};
use crate::worker::worker_codec::RemoteEvent;

/// Asks the renderer for an update, from any thread.
pub(crate) type Waker = Arc<dyn Fn() + Send + Sync>;

/// Something that arrived for the tiles.
#[cfg_attr(
    target_pointer_width = "64",
    expect(clippy::large_enum_variant, reason = "a reply moves its layout once, from the worker to the tile")
)]
pub(crate) enum TileEvent {
    /// A reply to tile load `load`, of its origin's reply type. It applies
    /// only while that load is the tile's latest.
    Load { tile: TileKey, load: u64, reply: Box<dyn Any + Send> },
    /// The reply to job `id`, which ran on `lane`.
    Reply { lane: Lane, id: JobId, body: ReplyBody, times: JobTimes },
    /// The worker of `lane` is gone, with every job it had.
    LaneLost(Lane),
    /// What a remote worker instance sent, for the store's workers to read.
    Remote(RemoteEvent),
    /// The answer to the glyph store's request `id`, for a glyph range. It
    /// applies only while the range still waits for that request.
    Glyphs { font_stack: FontStack, range: GlyphRange, id: u64, result: GlyphResult },
    /// The device's fonts for clusters of `font_stack` that the glyph store
    /// asked its chooser about. Each applies only while its cluster still
    /// waits for a choice.
    DeviceFonts { font_stack: FontStack, faces: Vec<(Vec<char>, Option<FontFace>)> },
}

/// The receiving end, which the tile store owns.
pub(crate) struct Inbox {
    sender: InboxSender,
    receiver: Receiver<TileEvent>,
}

impl Inbox {
    /// An inbox whose senders call `wake` after each event.
    pub(crate) fn new(wake: Waker) -> Self {
        let (sender, receiver) = channel();
        Inbox { sender: InboxSender { sender, wake }, receiver }
    }

    /// Where to send events from.
    pub(crate) fn sender(&self) -> &InboxSender {
        &self.sender
    }

    /// The oldest event not taken yet.
    pub(crate) fn next(&self) -> Option<TileEvent> {
        self.receiver.try_recv().ok()
    }
}

/// Sends events to an [`Inbox`], from any thread.
#[derive(Clone)]
pub(crate) struct InboxSender {
    sender: Sender<TileEvent>,
    wake: Waker,
}

impl InboxSender {
    /// Sends `event` and wakes the renderer, unless the inbox is gone.
    pub(crate) fn send(&self, event: TileEvent) {
        if self.sender.send(event).is_ok() {
            (self.wake)();
        }
    }
}