Skip to main content

mln_engine/render/
events.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::sync::Arc;
7
8pub use mln_gfx::rendering_stats::RenderingStats;
9use mln_style::error::Error;
10use mln_style::time::Instant;
11
12use crate::camera::Elevation;
13use crate::exec::event_queue::EventQueue;
14pub use crate::text::font_stack::FontStack;
15pub use crate::text::glyph::GlyphRange;
16use crate::tile::OverscaledTileID;
17pub use crate::tile_store::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 and its camera
40        /// stood as it would on the terrain's current ground
41        /// ([`UpdateParameters::elevation`]), though some of it may have
42        /// failed to draw (`error`).
43        ///
44        /// [`UpdateParameters::elevation`]: crate::UpdateParameters::elevation
45        mode: RenderMode,
46        /// The frame has transitions still running.
47        needs_repaint: bool,
48        /// When, without `needs_repaint`, the map changes next though
49        /// nothing asks for a frame before: a frame then places the symbols
50        /// again.
51        repaint_at: Option<Instant>,
52        placement_changed: bool,
53        /// The still image request of the update that the frame drew, or 0
54        /// when the update requested none. Only a frame of the request's own
55        /// updates completes it, not one of an earlier update that a renderer
56        /// on another thread drew late.
57        still_image_request_id: u64,
58        /// The [`update_id`] of the update that the frame drew.
59        ///
60        /// [`update_id`]: crate::UpdateParameters::update_id
61        update_id: u64,
62        /// The context's counters after the frame.
63        stats: Arc<RenderingStats>,
64        /// Why the frame failed on the GPU, if it did
65        /// ([`DrawnFrame::error`](crate::DrawnFrame::error)): its image
66        /// lacks what failed. Boxed, so that the events don't grow for it.
67        error: Option<Box<Error>>,
68    },
69    /// The ground of the terrain changed: the tiles that draw it, or the DEM
70    /// data they read; or it comes again for an update that carried a
71    /// ground that differs from it at the view's center. `None` once terrain
72    /// stops drawing, and for an update that carries a ground while terrain
73    /// doesn't draw. It comes before the frame of its update finishes. The
74    /// map's camera stands on it, and the map answers it with an update that
75    /// carries it ([`UpdateParameters::elevation`]), which a still image
76    /// waits for.
77    ///
78    /// [`UpdateParameters::elevation`]: crate::UpdateParameters::elevation
79    TerrainElevation(Option<Arc<dyn Elevation>>),
80    /// The renderer drew its first frame with everything loaded.
81    RenderMapFinished,
82    /// A layer uses an image that the style lacks. The renderer lays out
83    /// without it until an update adds it.
84    StyleImageMissing(String),
85    /// These images were requested before, and no layer uses them anymore.
86    UnusedStyleImages(Vec<String>),
87    GlyphsRequested {
88        font_stack: FontStack,
89        range: GlyphRange,
90    },
91    GlyphsLoaded {
92        font_stack: FontStack,
93        range: GlyphRange,
94    },
95    /// A glyph range of a font stack failed to load or parse.
96    GlyphsError {
97        font_stack: FontStack,
98        range: GlyphRange,
99        error: Error,
100    },
101    /// A tile of the source `source_id` went through `op`: a request, a
102    /// response, the start or end of its parse, an error or a cancel.
103    TileAction {
104        op: TileOperation,
105        id: OverscaledTileID,
106        source_id: String,
107    },
108}
109
110/// The queue a renderer pushes its events to.
111pub type RendererEvents = EventQueue<RendererEvent>;
112
113// A renderer on another thread than its map sends it the events.
114const _: () = {
115    const fn assert_send<T: Send>() {}
116    assert_send::<RendererEvent>();
117};