use std::any::Any;
use std::borrow::Cow;
use std::fmt::Debug;
use std::sync::Arc;
use crate::error::Result;
use crate::layer::{
AfterGpu, Composite, DrawGroup, EvaluatedLayer, Frame, GroundHeights, HitInfo, IndexBuffer, LoadedFeatureInfo,
Mesh, MeshDesc, PaintAttributes, RasterTileInfo, Resources, Target, Texture, Tile, TileGeometry, VertexBinding,
VertexBuffer, View,
};
use crate::program::{Program, ProgramDesc};
use crate::source::TileResponse;
use crate::types::{
Alignment, CollisionShape, Color, DrawOptions, DrawRange, FrameInfo, GpuEnd, LatLng, LendAt, MapMode, PointI16,
ProjectedPoint, ProjectionUbo, RenderPasses, SamplerState, ScreenPoint, TextureChannelType, TextureFormat,
TileChanges, TileId, TileTransform, Vec2, ViewInfo,
};
pub trait HostBackend {
fn create_program(&self, desc: &ProgramDesc<'_>) -> Result<Program>;
fn create_ground_heights(&self) -> Result<GroundHeights>;
}
pub trait ProgramHandle: Send + Sync + Debug + Any {
fn buffer_id(&self, name: &str) -> Result<u32>;
fn texture_slot(&self, name: &str) -> Result<u32>;
fn as_any(&self) -> &dyn Any;
}
pub trait VertexBufferHandle: Debug + Any {
fn as_any(&self) -> &dyn Any;
}
pub trait IndexBufferHandle: Debug + Any {
fn as_any(&self) -> &dyn Any;
}
pub trait TextureHandle: Debug + Any {
fn as_any(&self) -> &dyn Any;
}
pub trait TargetHandle: Debug + Any {
fn texture(&self) -> Option<Texture>;
fn as_any(&self) -> &dyn Any;
}
pub trait PaintAttributesHandle: Debug + Any {
fn as_any(&self) -> &dyn Any;
}
pub trait MeshHandle: Debug + Any {
fn seq(&self) -> i64;
fn as_any(&self) -> &dyn Any;
}
pub trait GroundHeightsHandle: Debug + Any {
fn as_any_mut(&mut self) -> &mut dyn Any;
}
pub trait AfterGpuHandle: Send + Sync + Debug + Any {
fn push(&self, callback: Box<dyn FnOnce(GpuEnd) + Send + 'static>) -> Result<()>;
fn clone_handle(&self) -> Result<Arc<dyn AfterGpuHandle>>;
fn as_any(&self) -> &dyn Any;
}
pub trait EvaluatedLayerBackend {
fn id(&self) -> &str;
fn paint_is_constant(&self, slot: u32) -> bool;
fn paint_f32(&self, slot: u32) -> Result<f32>;
fn paint_f32_constant_or(&self, slot: u32, fallback: f32) -> Result<f32>;
fn paint_bool(&self, slot: u32) -> Result<bool>;
fn paint_color(&self, slot: u32) -> Result<Color>;
fn paint_color_constant_or(&self, slot: u32, fallback: Color) -> Result<Color>;
fn paint_vec2(&self, slot: u32) -> Result<Vec2>;
fn paint_lat_lng(&self, slot: u32) -> Result<LatLng>;
fn paint_rotation_degrees(&self, slot: u32) -> Result<f32>;
fn paint_str(&self, slot: u32) -> Result<&str>;
fn paint_enum(&self, slot: u32) -> Result<u32>;
fn as_any(&self) -> Option<&dyn Any> {
None
}
}
pub trait EvaluateCxBackend {
fn evaluated(&self) -> EvaluatedLayer<'_>;
fn time_point_ns(&self) -> Option<i64>;
}
pub trait ViewBackend {
fn info(&self) -> ViewInfo;
fn world_from_lat_lng(&self, lat_lng: LatLng) -> Result<(f64, f64)>;
fn lat_lng_near_center(&self, lat_lng: LatLng) -> Result<LatLng>;
fn ground_meters_per_world_unit_at(&self, lat_lng: LatLng) -> f64;
fn world_copies(&self) -> Result<Vec<i16>>;
fn project_tile_point(&self, tile_id: TileId, x: f64, y: f64, elevation_m: f64) -> Result<ProjectedPoint>;
fn map_aligned_scale_at(&self, tile_id: TileId, x: f64, y: f64) -> f64;
fn globe_extrude_scale(&self, tile_id: TileId) -> f32;
fn tile_unit_translation(&self, tile_id: TileId, translate: Vec2, anchor: Alignment) -> Result<Vec2>;
}
pub trait FrameBackend {
fn info(&self) -> FrameInfo;
fn view(&self) -> View<'_>;
fn tile_projection(&self, tile_id: TileId, transform: TileTransform) -> Result<ProjectionUbo>;
fn ground_height(&self, tile_id: TileId, x: f32, y: f32) -> f32;
fn ground_depth_texture(&self) -> Option<Texture>;
fn collision_shape_count(&self) -> usize;
fn collision_shape(&self, index: usize) -> Option<CollisionShape>;
fn ground_token(&self) -> Option<Box<dyn Any>> {
None
}
fn as_any(&self) -> Option<&dyn Any> {
None
}
}
pub trait TileGeometryBackend {
fn vertex_stream_count(&self) -> usize;
fn vertex_count(&self, stream_index: usize) -> usize;
fn vertex_stride(&self, stream_index: usize) -> u32;
fn index_stream_count(&self) -> usize;
fn index_count(&self, stream_index: usize) -> usize;
fn draw_ranges(&self) -> Cow<'_, [DrawRange]>;
fn metadata(&self) -> &[u8];
fn max_elevation(&self) -> f32;
fn binder_interpolation_factor(&self, layer_id: &str, paint_slot: u32, zoom: f32) -> f32;
fn binder_statistics_max_f32(&self, layer_id: &str, paint_slot: u32) -> Option<f32>;
}
pub trait TileBackend {
fn id(&self) -> TileId;
fn draw_id(&self) -> TileId;
fn render_passes(&self) -> RenderPasses;
fn has_data_for_pass(&self, pass: RenderPasses) -> bool;
fn geometry(&self) -> Option<TileGeometry<'_>>;
fn geometry_vertices(&self, stream_index: usize) -> Option<VertexBuffer>;
fn geometry_indexes(&self, stream_index: usize) -> Option<IndexBuffer>;
fn paint_attributes(&self, res: &mut Resources<'_>, evaluated: EvaluatedLayer<'_>) -> Option<PaintAttributes>;
fn raster_info(&self) -> Option<RasterTileInfo<'_>>;
}
pub trait UpdateCxReadBackend {
fn evaluated(&self) -> EvaluatedLayer<'_>;
fn source_max_zoom(&self) -> Option<u8>;
fn tile_count(&self) -> usize;
fn tile(&self, index: usize) -> Option<Tile<'_>>;
}
pub trait UpdateCxMutBackend {
fn pick_tiles(&mut self, tiles: &[TileId]) -> Result<()>;
}
pub trait PrepareCxReadBackend {
fn tile_count(&self) -> usize;
fn tile(&self, index: usize) -> Option<Tile<'_>>;
fn picked_tiles(&self) -> Vec<TileId>;
}
pub trait PrepareCxMutBackend {
fn resources(&mut self) -> Resources<'_>;
fn tile_resources_mut(&mut self, tile_id: TileId) -> Option<&mut dyn Any>;
fn tile_resources_and_allocator(&mut self, tile_id: TileId) -> (Option<&mut dyn Any>, Resources<'_>);
fn take_tile_changes(&mut self, tile_id: TileId) -> TileChanges;
fn read_shared_target(&mut self, name: &str) -> Option<Texture>;
}
pub trait TileBuildCxBackend {
fn tile_id(&self) -> TileId;
fn zoom(&self) -> f64;
fn mode(&self) -> MapMode;
fn pixel_ratio(&self) -> f32;
fn first_load(&self) -> bool;
fn layer_count(&self) -> usize;
fn layer(&self, index: usize) -> Option<EvaluatedLayer<'_>>;
fn eval_layout_f32(&self, layer_index: usize, layout_slot: u32, for_current_feature: bool) -> Result<f32>;
fn eval_layout_bool(&self, layer_index: usize, layout_slot: u32, for_current_feature: bool) -> Result<bool>;
fn eval_layout_color(&self, layer_index: usize, layout_slot: u32, for_current_feature: bool) -> Result<Color>;
fn eval_layout_vec2(&self, layer_index: usize, layout_slot: u32, for_current_feature: bool) -> Result<Vec2>;
fn eval_layout_lat_lng(&self, layer_index: usize, layout_slot: u32, for_current_feature: bool) -> Result<LatLng>;
fn eval_layout_rotation_degrees(
&self,
layer_index: usize,
layout_slot: u32,
for_current_feature: bool,
) -> Result<f32>;
fn eval_layout_str(&mut self, layer_index: usize, layout_slot: u32, for_current_feature: bool) -> Result<&str>;
fn eval_layout_enum(&self, layer_index: usize, layout_slot: u32, for_current_feature: bool) -> Result<u32>;
fn feature_count(&self) -> usize;
fn load_feature(&mut self, selected_index: usize) -> Result<LoadedFeatureInfo>;
fn feature_ring(&self, ring_index: usize) -> Option<Cow<'_, [PointI16]>>;
fn populate_binders(&mut self, vertex_count: usize) -> Result<()>;
fn index_current_feature(&mut self) -> Result<()>;
}
pub trait TileGeometryBuilderBackend {
fn add_vertices(&mut self, bytes: &[u8], vertex_count: usize, stride: u32) -> Result<usize>;
fn add_indexes(&mut self, indexes: &[u16]) -> Result<usize>;
fn add_draw_range(&mut self, range: DrawRange) -> Result<()>;
fn set_metadata(&mut self, bytes: &[u8]) -> Result<()>;
fn set_max_elevation(&mut self, max_elevation: f32) -> Result<()>;
}
pub trait ResourcesBackend {
fn create_program(&mut self, desc: &ProgramDesc<'_>) -> Result<Program>;
fn create_ground_heights(&mut self) -> Result<GroundHeights>;
fn create_vertices(&mut self, bytes: &[u8], vertex_count: usize, stride: u32) -> Result<VertexBuffer>;
fn create_dynamic_vertices(&mut self, bytes: &[u8], vertex_count: usize, stride: u32) -> Result<VertexBuffer>;
fn update_vertices(&mut self, buffer: &VertexBuffer, bytes: &[u8]) -> bool;
fn create_indexes(&mut self, indexes: &[u16]) -> Result<IndexBuffer>;
fn quad_indexes(&mut self) -> Result<IndexBuffer>;
fn create_texture(&mut self, format: TextureFormat, sampler: SamplerState) -> Result<Texture>;
fn write_texture(&mut self, texture: &Texture, data: &[u8], width: u32, height: u32) -> Result<()>;
fn create_target(
&mut self,
width: u32,
height: u32,
channel_type: TextureChannelType,
clear_color: Color,
) -> Result<Target>;
fn create_mesh(&mut self, desc: &MeshDesc<'_>) -> Result<Mesh>;
fn mesh_set_texture(&mut self, mesh: &Mesh, slot: u32, texture: Option<&Texture>) -> Result<()>;
fn mesh_set_sampler(&mut self, mesh: &Mesh, slot: u32, sampler: SamplerState) -> Result<()>;
fn mesh_set_paint(&mut self, mesh: &Mesh, paint: &PaintAttributes) -> Result<()>;
fn mesh_bind_vertices(&mut self, mesh: &Mesh, binding: &VertexBinding<'_>) -> Result<()>;
fn apply_ground_heights(
&mut self,
heights: &mut GroundHeights,
mesh: &Mesh,
frame: Frame<'_>,
tile_id: TileId,
attribute_name: &str,
centers: &[PointI16],
) -> Result<()>;
fn as_any_mut(&mut self) -> Option<&mut dyn Any> {
None
}
}
pub trait DrawsBackend {
fn main(&mut self) -> DrawGroup<'_>;
fn target(&mut self, target: &Target) -> DrawGroup<'_>;
fn lend(&mut self, at: LendAt) -> Result<()>;
}
pub trait DrawGroupBackend {
fn push(&mut self, mesh: &Mesh, options: DrawOptions, blocks: &[&[u8]]) -> Result<()>;
fn push_at(
&mut self,
mesh: &Mesh,
tile_id: TileId,
projection: &ProjectionUbo,
options: DrawOptions,
blocks: &[&[u8]],
) -> Result<()>;
fn set_block(&mut self, program: &Program, buffer_name: &str, bytes: &[u8]) -> Result<()>;
fn set_composite(&mut self, composite: Composite) -> Result<()>;
}
pub trait AfterGpuRefBackend {
fn clone_handle(&self) -> Result<AfterGpu>;
fn push(&self, callback: Box<dyn FnOnce(GpuEnd) + Send + 'static>) -> Result<()>;
}
pub trait HitCxBackend {
fn info(&self) -> HitInfo<'_>;
fn evaluated(&self) -> EvaluatedLayer<'_>;
fn view(&self) -> View<'_>;
fn feature_ring(&self, index: usize) -> Option<Cow<'_, [PointI16]>>;
fn eval_paint_f32(&self, slot: u32) -> Result<f32>;
fn eval_paint_bool(&self, slot: u32) -> Result<bool>;
fn eval_paint_color(&self, slot: u32) -> Result<Color>;
fn eval_paint_vec2(&self, slot: u32) -> Result<Vec2>;
fn eval_paint_enum(&self, slot: u32) -> Result<u32>;
fn eval_paint_string(&self, slot: u32) -> Result<String>;
fn ground_height(&self, point: PointI16) -> f64;
fn translated_query_geometry(&self, translate: Vec2, anchor: Alignment) -> Result<Vec<PointI16>>;
fn plane_geometry(&self, elevation_m: f64, translate: Vec2, anchor: Alignment) -> Result<Vec<PointI16>>;
fn viewport_point(&self, x: f64, y: f64, elevation_m: f64) -> Option<Vec2>;
fn map_aligned_scale_at(&self, x: f64, y: f64) -> f64;
fn pitch_scaled_size(
&self,
size: f32,
point: PointI16,
elevation_m: f64,
pitch_scale: Alignment,
pitch_alignment: Alignment,
) -> f32;
fn shows(&self, x: f64, y: f64, elevation_m: f64) -> bool;
}
pub trait FrameQueryCxReadBackend {
fn evaluated(&self) -> EvaluatedLayer<'_>;
fn view(&self) -> View<'_>;
fn geometry(&self) -> Cow<'_, [ScreenPoint]>;
fn has_ground(&self) -> bool;
fn ground_height(&self, tile_id: TileId, x: f32, y: f32) -> f32;
fn ground_visibility(&self, tile_id: TileId, x: f64, y: f64, elevation_m: f64) -> f64;
}
pub trait FrameQueryCxMutBackend {
fn push_feature_json(&mut self, geojson_feature: &str) -> Result<()>;
fn push_point_feature(&mut self, position: LatLng) -> Result<()>;
}
pub trait TileRequestBackend: Send + Debug {
fn deliver(self: Box<Self>, response: TileResponse<'_>) -> Result<()>;
}