Skip to main content

mln_renderer/
renderer_event.rs

1//! What a renderer reports to the map it renders: frames, resource errors,
2//! missing images, glyph and tile activity. The renderer pushes the events
3//! into the map's [`RendererEvents`] queue, and the map handles them when it
4//! drains the queue.
5
6use std::cell::RefCell;
7use std::rc::Rc;
8use std::sync::Arc;
9
10pub use mln_gfx::rendering_stats::RenderingStats;
11pub use mln_text::glyph::GlyphRange;
12pub use mln_tile_id::OverscaledTileID;
13use mln_util::error::Error;
14use mln_util::event_queue::EventQueue;
15pub use mln_util::font_stack::FontStack;
16
17pub use crate::tile::TileOperation;
18
19#[derive(Clone, Copy, Debug, PartialEq, Eq)]
20#[repr(u32)]
21pub enum RenderMode {
22    Partial,
23    Full,
24}
25
26/// An event of a renderer.
27#[derive(Clone, Debug)]
28pub enum RendererEvent {
29    /// The renderer needs a new frame, as when a tile arrived.
30    Invalidate,
31    /// A resource failed to load or parse.
32    ResourceError(Error),
33    /// The renderer is about to draw its first frame.
34    RenderMapStarted,
35    /// The renderer is about to draw a frame.
36    RenderFrameStarted,
37    /// The renderer drew a frame.
38    RenderFrameFinished {
39        /// `Full` when everything the frame shows has loaded.
40        mode: RenderMode,
41        /// The frame has transitions still running.
42        needs_repaint: bool,
43        placement_changed: bool,
44        /// The still image request of the update that the frame drew, or 0
45        /// when the update requested none. Only a frame of the request's own
46        /// updates completes it, not one of an earlier update that a renderer
47        /// on another thread drew late.
48        still_image_request_id: u64,
49        /// The context's counters after the frame.
50        stats: Arc<RenderingStats>,
51    },
52    /// The renderer drew its first frame with everything loaded.
53    RenderMapFinished,
54    /// A layer uses an image that the style lacks. The renderer lays out
55    /// without it until an update adds it.
56    StyleImageMissing(String),
57    /// These images were requested before, and no layer uses them anymore.
58    UnusedStyleImages(Vec<String>),
59    GlyphsRequested {
60        font_stack: FontStack,
61        range: GlyphRange,
62    },
63    GlyphsLoaded {
64        font_stack: FontStack,
65        range: GlyphRange,
66    },
67    /// A glyph range of a font stack failed to load or parse.
68    GlyphsError {
69        font_stack: FontStack,
70        range: GlyphRange,
71        error: Error,
72    },
73    /// A tile of the source `source_id` went through `op`: a request, a
74    /// response, the start or end of its parse, an error or a cancel.
75    TileAction {
76        op: TileOperation,
77        id: OverscaledTileID,
78        source_id: String,
79    },
80    /// A layer or a drawable failed to render.
81    RenderError(Error),
82}
83
84/// The queue a renderer pushes its events to.
85pub type RendererEvents = EventQueue<RendererEvent>;
86
87// A renderer on another thread than its map sends it the events.
88const _: () = {
89    const fn assert_send<T: Send>() {}
90    assert_send::<RendererEvent>();
91};
92
93/// The queue that the renderer's parts push to, once the renderer has one.
94#[derive(Clone, Default)]
95pub(crate) struct EventSink(Rc<RefCell<Option<RendererEvents>>>);
96
97impl EventSink {
98    pub(crate) fn set(&self, events: Option<RendererEvents>) {
99        *self.0.borrow_mut() = events;
100    }
101
102    pub(crate) fn emit(&self, event: RendererEvent) {
103        let events = self.0.borrow().clone();
104        if let Some(events) = events {
105            events.push(event);
106        }
107    }
108}