Skip to main content

ferroui_base/platform/
i_platform_render_interface.rs

1use super::surfaces::IPlatformRenderSurface;
2use super::{
3    AlphaFormat, IBitmapImpl, IDrawingContextLayerImpl, IGeometryImpl, IOptionalFeatureProvider,
4    IPlatformGraphicsContext, IPlatformRenderInterfaceRegion, IRenderTarget, IRenderTargetBitmapImpl,
5    IStreamGeometryBuilder, IWriteableBitmapImpl, PixelFormat,
6};
7use crate::media::imaging::BitmapInterpolationMode;
8use crate::media::{FillRule, GeometryCombineMode};
9use crate::{PixelSize, Point, Rect, Vector};
10use std::io::Read;
11use std::rc::Rc;
12use std::sync::Arc;
13
14/// Defines the main platform-specific interface for the rendering subsystem.
15///
16/// This is the contract a rendering backend implements; it is registered in
17/// the service locator and everything that needs platform rendering objects
18/// goes through it.
19pub trait IPlatformRenderInterface: 'static {
20    /// Creates an ellipse geometry implementation.
21    fn create_ellipse_geometry(&self, rect: Rect) -> Arc<dyn IGeometryImpl>;
22
23    /// Creates a line geometry implementation.
24    fn create_line_geometry(&self, p1: Point, p2: Point) -> Arc<dyn IGeometryImpl>;
25
26    /// Creates a rectangle geometry implementation.
27    fn create_rectangle_geometry(&self, rect: Rect) -> Arc<dyn IGeometryImpl>;
28
29    /// Creates a builder used to define a stream geometry.
30    ///
31    /// `source` is a geometry created by this render interface whose figures
32    /// the new geometry starts with, or `None` to start with an empty
33    /// geometry. `source` itself isn't modified.
34    fn create_stream_geometry_builder(&self, source: Option<&dyn IGeometryImpl>) -> Box<dyn IStreamGeometryBuilder>;
35
36    /// Creates a geometry group implementation.
37    fn create_geometry_group(&self, fill_rule: FillRule, children: &[Arc<dyn IGeometryImpl>]) -> Arc<dyn IGeometryImpl>;
38
39    /// Creates a geometry that combines two geometries.
40    fn create_combined_geometry(
41        &self,
42        combine_mode: GeometryCombineMode,
43        g1: Arc<dyn IGeometryImpl>,
44        g2: Arc<dyn IGeometryImpl>,
45    ) -> Arc<dyn IGeometryImpl>;
46
47    /// Builds a glyph run geometry.
48    fn build_glyph_run_geometry(&self, glyph_run: &crate::media::GlyphRun) -> Arc<dyn IGeometryImpl>;
49
50    /// Builds all glyph contours without silently omitting unavailable glyphs.
51    ///
52    /// Returns `None` if the backend cannot verify a complete monochrome
53    /// outline, or if any glyph requires bitmap or colour-font rendering.
54    /// Whitespace with an available empty outline is supported. This capability
55    /// is separate from `build_glyph_run_geometry`, whose traditional contract
56    /// permits partial outlines for effects and clipping.
57    fn try_build_glyph_run_geometry(&self, _glyph_run: &crate::media::GlyphRun) -> Option<Arc<dyn IGeometryImpl>> {
58        None
59    }
60
61    /// Creates a platform implementation of a glyph run.
62    ///
63    /// * `glyph_typeface` — the glyph typeface.
64    /// * `font_rendering_em_size` — the font rendering em size.
65    /// * `glyph_infos` — the list of glyphs.
66    /// * `baseline_origin` — the baseline origin of the run. Can be zero.
67    fn create_glyph_run(
68        &self,
69        glyph_typeface: &Rc<crate::media::GlyphTypeface>,
70        font_rendering_em_size: f64,
71        glyph_infos: &[crate::media::text_formatting::GlyphInfo],
72        baseline_origin: Point,
73    ) -> std::sync::Arc<dyn super::IGlyphRunImpl>;
74
75    /// Creates a render target bitmap.
76    ///
77    /// `dpi` is the DPI of the bitmap; 96 means one pixel per logical unit.
78    fn create_render_target_bitmap(&self, size: PixelSize, dpi: Vector) -> std::sync::Arc<dyn IRenderTargetBitmapImpl>;
79
80    /// Creates a writeable bitmap.
81    fn create_writeable_bitmap(
82        &self,
83        size: PixelSize,
84        dpi: Vector,
85        format: PixelFormat,
86        alpha_format: AlphaFormat,
87    ) -> std::sync::Arc<dyn IWriteableBitmapImpl>;
88
89    /// Loads a bitmap from a file.
90    fn load_bitmap_from_file(&self, file_name: &str) -> std::io::Result<std::sync::Arc<crate::platform::SharedBitmapImpl>>;
91
92    /// Loads a bitmap from a stream.
93    fn load_bitmap(&self, stream: &mut dyn Read) -> std::io::Result<std::sync::Arc<crate::platform::SharedBitmapImpl>>;
94
95    /// Loads a writeable bitmap from a stream, decoded to the given width.
96    fn load_writeable_bitmap_to_width(
97        &self,
98        stream: &mut dyn Read,
99        width: i32,
100        interpolation_mode: BitmapInterpolationMode,
101    ) -> std::io::Result<std::sync::Arc<dyn IWriteableBitmapImpl>>;
102
103    /// Loads a writeable bitmap from a stream, decoded to the given height.
104    fn load_writeable_bitmap_to_height(
105        &self,
106        stream: &mut dyn Read,
107        height: i32,
108        interpolation_mode: BitmapInterpolationMode,
109    ) -> std::io::Result<std::sync::Arc<dyn IWriteableBitmapImpl>>;
110
111    /// Loads a writeable bitmap from a file.
112    fn load_writeable_bitmap_from_file(&self, file_name: &str) -> std::io::Result<std::sync::Arc<dyn IWriteableBitmapImpl>>;
113
114    /// Loads a writeable bitmap from a stream.
115    fn load_writeable_bitmap(&self, stream: &mut dyn Read) -> std::io::Result<std::sync::Arc<dyn IWriteableBitmapImpl>>;
116
117    /// Loads a bitmap from a stream, decoded to the given width.
118    fn load_bitmap_to_width(
119        &self,
120        stream: &mut dyn Read,
121        width: i32,
122        interpolation_mode: BitmapInterpolationMode,
123    ) -> std::io::Result<std::sync::Arc<crate::platform::SharedBitmapImpl>>;
124
125    /// Loads a bitmap from a stream, decoded to the given height.
126    fn load_bitmap_to_height(
127        &self,
128        stream: &mut dyn Read,
129        height: i32,
130        interpolation_mode: BitmapInterpolationMode,
131    ) -> std::io::Result<std::sync::Arc<crate::platform::SharedBitmapImpl>>;
132
133    /// Creates a resized copy of a bitmap.
134    fn resize_bitmap(
135        &self,
136        bitmap_impl: &dyn IBitmapImpl,
137        destination_size: PixelSize,
138        interpolation_mode: BitmapInterpolationMode,
139    ) -> std::sync::Arc<crate::platform::SharedBitmapImpl>;
140
141    /// Loads a bitmap from raw pixel data.
142    ///
143    /// `data` holds `stride * size.height` bytes in the given formats.
144    fn load_bitmap_from_pixels(
145        &self,
146        format: PixelFormat,
147        alpha_format: AlphaFormat,
148        data: &[u8],
149        size: PixelSize,
150        dpi: Vector,
151        stride: i32,
152    ) -> std::sync::Arc<crate::platform::SharedBitmapImpl>;
153
154    /// Creates a backend-specific render context from a platform graphics
155    /// context (`None` selects software rendering).
156    fn create_backend_context(
157        &self,
158        graphics_api_context: Option<Rc<dyn IPlatformGraphicsContext>>,
159    ) -> Rc<dyn IPlatformRenderInterfaceContext>;
160
161    /// Whether the backend supports individual corner radii on rounded
162    /// rectangles.
163    fn supports_individual_round_rects(&self) -> bool;
164
165    /// Whether drawing contexts created by this backend support isolated
166    /// compositing layers for all drawing primitives. Recording contexts use
167    /// this capability before accepting a compositing-layer push.
168    fn supports_compositing_layers(&self) -> bool {
169        false
170    }
171
172    /// The default alpha format of the backend.
173    fn default_alpha_format(&self) -> AlphaFormat;
174
175    /// The default pixel format of the backend.
176    fn default_pixel_format(&self) -> PixelFormat;
177
178    /// Whether bitmaps can be created in the given pixel format.
179    fn is_supported_bitmap_pixel_format(&self, format: PixelFormat) -> bool;
180
181    /// Whether the backend supports regions.
182    fn supports_regions(&self) -> bool;
183
184    /// Creates an empty region.
185    fn create_region(&self) -> Rc<dyn IPlatformRenderInterfaceRegion>;
186}
187
188/// A render backend bound to a graphics context (or to software rendering).
189pub trait IPlatformRenderInterfaceContext: IOptionalFeatureProvider {
190    /// Creates a render target for one of the given surfaces.
191    fn create_render_target(&self, surfaces: &[std::sync::Arc<dyn IPlatformRenderSurface>]) -> Rc<dyn IRenderTarget>;
192
193    /// Creates an offscreen render target.
194    fn create_offscreen_render_target(
195        &self,
196        pixel_size: PixelSize,
197        scaling: Vector,
198        enable_text_antialiasing: bool,
199    ) -> Rc<dyn IDrawingContextLayerImpl>;
200
201    /// Whether the context is lost and must be recreated.
202    fn is_lost(&self) -> bool;
203
204    /// The largest offscreen render target the context can create, if
205    /// limited.
206    fn max_offscreen_render_target_pixel_size(&self) -> Option<PixelSize>;
207
208    /// The features of the context that are exposed to user code, by type.
209    fn public_features(&self) -> std::collections::HashMap<std::any::TypeId, Rc<dyn std::any::Any>> {
210        std::collections::HashMap::new()
211    }
212
213    /// Whether a render target can be created for the surfaces right now.
214    fn is_ready_to_create_render_target(&self, _surfaces: &[std::sync::Arc<dyn IPlatformRenderSurface>]) -> bool {
215        true
216    }
217
218    /// Releases the context.
219    fn dispose(&self);
220}