# mod.rs - source
Source of the Rust file `maplibre/src/stages/mod.rs`.
Keywords: rust, rustlang, rust-lang

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188

    //! [Stages](Stage) for requesting and preparing data

    use crate::coords::ZoomLevel;
    use crate::coords::{WorldCoords, WorldTileCoords, Zoom};
    use crate::error::Error;
    use crate::io::geometry_index::GeometryIndex;
    use crate::io::geometry_index::{IndexProcessor, IndexedGeometry, TileIndex};
    use crate::io::pipeline::Processable;
    use crate::io::pipeline::{PipelineContext, PipelineProcessor};
    use crate::io::source_client::{HttpSourceClient, SourceClient};
    use crate::io::tile_pipelines::build_vector_tile_pipeline;
    use crate::io::tile_repository::StoredLayer;
    use crate::io::tile_request_state::TileRequestState;
    use crate::io::{TileRequest, TileRequestID};
    use crate::render::ShaderVertex;
    use crate::schedule::Schedule;
    use crate::stages::message::{
        LayerTessellateMessage, MessageReceiver, MessageSender, TessellateMessage,
        TileTessellateMessage,
    };
    use crate::stages::populate_tile_store_stage::PopulateTileStore;
    use crate::tessellation::zero_tessellator::ZeroTessellator;
    use crate::tessellation::{IndexDataType, OverAlignedVertexBuffer};
    use crate::{HttpClient, ScheduleMethod, Scheduler};
    use geozero::mvt::tile;
    use geozero::GeozeroDatasource;
    use prost::Message;
    use request_stage::RequestStage;
    use std::collections::HashSet;
    use std::fmt;
    use std::sync::{mpsc, Arc, Mutex};

    mod message;
    mod populate_tile_store_stage;
    mod request_stage;

    /// Register stages required for requesting and preparing new tiles.
    pub fn register_stages<HC: HttpClient, SM: ScheduleMethod>(
        schedule: &mut Schedule,
        http_source_client: HttpSourceClient<HC>,
        scheduler: Box<Scheduler<SM>>,
    ) {
        let (message_sender, message_receiver): (MessageSender, MessageReceiver) = mpsc::channel();
        let shared_thread_state = SharedThreadState {
            tile_request_state: Arc::new(Mutex::new(TileRequestState::new())),
            message_sender,
            geometry_index: Arc::new(Mutex::new(GeometryIndex::new())),
        };

        schedule.add_stage(
            "request",
            RequestStage::new(shared_thread_state.clone(), http_source_client, *scheduler),
        );
        schedule.add_stage(
            "populate_tile_store",
            PopulateTileStore::new(shared_thread_state, message_receiver),
        );
    }

    pub struct HeadedPipelineProcessor {
        state: SharedThreadState,
    }

    impl PipelineProcessor for HeadedPipelineProcessor {
        fn tile_finished(&mut self, request_id: TileRequestID, coords: &WorldTileCoords) {
            self.state
                .message_sender
                .send(TessellateMessage::Tile(TileTessellateMessage {
                    request_id,
                    coords: *coords,
                }))
                .unwrap();
        }

        fn layer_unavailable(&mut self, coords: &WorldTileCoords, layer_name: &str) {
            self.state
                .message_sender
                .send(TessellateMessage::Layer(
                    LayerTessellateMessage::UnavailableLayer {
                        coords: *coords,
                        layer_name: layer_name.to_owned(),
                    },
                ))
                .unwrap();
        }

        fn layer_tesselation_finished(
            &mut self,
            coords: &WorldTileCoords,
            buffer: OverAlignedVertexBuffer<ShaderVertex, IndexDataType>,
            feature_indices: Vec<u32>,
            layer_data: tile::Layer,
        ) {
            self.state
                .message_sender
                .send(TessellateMessage::Layer(
                    LayerTessellateMessage::TessellatedLayer {
                        coords: *coords,
                        buffer,
                        feature_indices,
                        layer_data,
                    },
                ))
                .unwrap();
        }

        fn layer_indexing_finished(
            &mut self,
            coords: &WorldTileCoords,
            geometries: Vec<IndexedGeometry<f64>>,
        ) {
            if let Ok(mut geometry_index) = self.state.geometry_index.lock() {
                geometry_index.index_tile(&coords, TileIndex::Linear { list: geometries })
            }
        }
    }

    /// Stores and provides access to the thread safe data shared between the schedulers.
    #[derive(Clone)]
    pub struct SharedThreadState {
        pub tile_request_state: Arc<Mutex<TileRequestState>>,
        pub message_sender: mpsc::Sender<TessellateMessage>,
        pub geometry_index: Arc<Mutex<GeometryIndex>>,
    }

    impl SharedThreadState {
        fn get_tile_request(&self, request_id: TileRequestID) -> Option<TileRequest> {
            self.tile_request_state
                .lock()
                .ok()
                .and_then(|tile_request_state| tile_request_state.get_tile_request(request_id).cloned())
        }

        #[tracing::instrument(skip_all)]
        pub fn process_tile(&self, request_id: TileRequestID, data: Box<[u8]>) -> Result<(), Error> {
            if let Some(tile_request) = self.get_tile_request(request_id) {
                let mut pipeline_context = PipelineContext::new(HeadedPipelineProcessor {
                    state: self.clone(),
                });
                let pipeline = build_vector_tile_pipeline();
                pipeline.process((tile_request, request_id, data), &mut pipeline_context);
            }

            Ok(())
        }

        pub fn tile_unavailable(
            &self,
            coords: &WorldTileCoords,
            request_id: TileRequestID,
        ) -> Result<(), Error> {
            if let Some(tile_request) = self.get_tile_request(request_id) {
                for to_load in &tile_request.layers {
                    tracing::warn!("layer {} at {} unavailable", to_load, coords);
                    self.message_sender.send(TessellateMessage::Layer(
                        LayerTessellateMessage::UnavailableLayer {
                            coords: tile_request.coords,
                            layer_name: to_load.to_string(),
                        },
                    ))?;
                }
            }

            Ok(())
        }

        #[tracing::instrument(skip_all)]
        pub fn query_point(
            &self,
            world_coords: &WorldCoords,
            z: ZoomLevel,
            zoom: Zoom,
        ) -> Option<Vec<IndexedGeometry<f64>>> {
            if let Ok(geometry_index) = self.geometry_index.lock() {
                geometry_index
                    .query_point(world_coords, z, zoom)
                    .map(|geometries| {
                        geometries
                            .iter()
                            .cloned()
                            .cloned()
                            .collect::<Vec<IndexedGeometry<f64>>>()
                    })
            } else {
                unimplemented!()
            }
        }
    }