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    /// Creates a platform implementation of a glyph run.
51    ///
52    /// * `glyph_typeface` — the glyph typeface.
53    /// * `font_rendering_em_size` — the font rendering em size.
54    /// * `glyph_infos` — the list of glyphs.
55    /// * `baseline_origin` — the baseline origin of the run. Can be zero.
56    fn create_glyph_run(
57        &self,
58        glyph_typeface: &Rc<crate::media::GlyphTypeface>,
59        font_rendering_em_size: f64,
60        glyph_infos: &[crate::media::text_formatting::GlyphInfo],
61        baseline_origin: Point,
62    ) -> std::sync::Arc<dyn super::IGlyphRunImpl>;
63
64    /// Creates a render target bitmap.
65    ///
66    /// `dpi` is the DPI of the bitmap; 96 means one pixel per logical unit.
67    fn create_render_target_bitmap(&self, size: PixelSize, dpi: Vector) -> std::sync::Arc<dyn IRenderTargetBitmapImpl>;
68
69    /// Creates a writeable bitmap.
70    fn create_writeable_bitmap(
71        &self,
72        size: PixelSize,
73        dpi: Vector,
74        format: PixelFormat,
75        alpha_format: AlphaFormat,
76    ) -> std::sync::Arc<dyn IWriteableBitmapImpl>;
77
78    /// Loads a bitmap from a file.
79    fn load_bitmap_from_file(&self, file_name: &str) -> std::io::Result<std::sync::Arc<crate::platform::SharedBitmapImpl>>;
80
81    /// Loads a bitmap from a stream.
82    fn load_bitmap(&self, stream: &mut dyn Read) -> std::io::Result<std::sync::Arc<crate::platform::SharedBitmapImpl>>;
83
84    /// Loads a writeable bitmap from a stream, decoded to the given width.
85    fn load_writeable_bitmap_to_width(
86        &self,
87        stream: &mut dyn Read,
88        width: i32,
89        interpolation_mode: BitmapInterpolationMode,
90    ) -> std::io::Result<std::sync::Arc<dyn IWriteableBitmapImpl>>;
91
92    /// Loads a writeable bitmap from a stream, decoded to the given height.
93    fn load_writeable_bitmap_to_height(
94        &self,
95        stream: &mut dyn Read,
96        height: i32,
97        interpolation_mode: BitmapInterpolationMode,
98    ) -> std::io::Result<std::sync::Arc<dyn IWriteableBitmapImpl>>;
99
100    /// Loads a writeable bitmap from a file.
101    fn load_writeable_bitmap_from_file(&self, file_name: &str) -> std::io::Result<std::sync::Arc<dyn IWriteableBitmapImpl>>;
102
103    /// Loads a writeable bitmap from a stream.
104    fn load_writeable_bitmap(&self, stream: &mut dyn Read) -> std::io::Result<std::sync::Arc<dyn IWriteableBitmapImpl>>;
105
106    /// Loads a bitmap from a stream, decoded to the given width.
107    fn load_bitmap_to_width(
108        &self,
109        stream: &mut dyn Read,
110        width: i32,
111        interpolation_mode: BitmapInterpolationMode,
112    ) -> std::io::Result<std::sync::Arc<crate::platform::SharedBitmapImpl>>;
113
114    /// Loads a bitmap from a stream, decoded to the given height.
115    fn load_bitmap_to_height(
116        &self,
117        stream: &mut dyn Read,
118        height: i32,
119        interpolation_mode: BitmapInterpolationMode,
120    ) -> std::io::Result<std::sync::Arc<crate::platform::SharedBitmapImpl>>;
121
122    /// Creates a resized copy of a bitmap.
123    fn resize_bitmap(
124        &self,
125        bitmap_impl: &dyn IBitmapImpl,
126        destination_size: PixelSize,
127        interpolation_mode: BitmapInterpolationMode,
128    ) -> std::sync::Arc<crate::platform::SharedBitmapImpl>;
129
130    /// Loads a bitmap from raw pixel data.
131    ///
132    /// `data` holds `stride * size.height` bytes in the given formats.
133    fn load_bitmap_from_pixels(
134        &self,
135        format: PixelFormat,
136        alpha_format: AlphaFormat,
137        data: &[u8],
138        size: PixelSize,
139        dpi: Vector,
140        stride: i32,
141    ) -> std::sync::Arc<crate::platform::SharedBitmapImpl>;
142
143    /// Creates a backend-specific render context from a platform graphics
144    /// context (`None` selects software rendering).
145    fn create_backend_context(
146        &self,
147        graphics_api_context: Option<Rc<dyn IPlatformGraphicsContext>>,
148    ) -> Rc<dyn IPlatformRenderInterfaceContext>;
149
150    /// Whether the backend supports individual corner radii on rounded
151    /// rectangles.
152    fn supports_individual_round_rects(&self) -> bool;
153
154    /// The default alpha format of the backend.
155    fn default_alpha_format(&self) -> AlphaFormat;
156
157    /// The default pixel format of the backend.
158    fn default_pixel_format(&self) -> PixelFormat;
159
160    /// Whether bitmaps can be created in the given pixel format.
161    fn is_supported_bitmap_pixel_format(&self, format: PixelFormat) -> bool;
162
163    /// Whether the backend supports regions.
164    fn supports_regions(&self) -> bool;
165
166    /// Creates an empty region.
167    fn create_region(&self) -> Rc<dyn IPlatformRenderInterfaceRegion>;
168}
169
170/// A render backend bound to a graphics context (or to software rendering).
171pub trait IPlatformRenderInterfaceContext: IOptionalFeatureProvider {
172    /// Creates a render target for one of the given surfaces.
173    fn create_render_target(&self, surfaces: &[std::sync::Arc<dyn IPlatformRenderSurface>]) -> Rc<dyn IRenderTarget>;
174
175    /// Creates an offscreen render target.
176    fn create_offscreen_render_target(
177        &self,
178        pixel_size: PixelSize,
179        scaling: Vector,
180        enable_text_antialiasing: bool,
181    ) -> Rc<dyn IDrawingContextLayerImpl>;
182
183    /// Whether the context is lost and must be recreated.
184    fn is_lost(&self) -> bool;
185
186    /// The largest offscreen render target the context can create, if
187    /// limited.
188    fn max_offscreen_render_target_pixel_size(&self) -> Option<PixelSize>;
189
190    /// The features of the context that are exposed to user code, by type.
191    fn public_features(&self) -> std::collections::HashMap<std::any::TypeId, Rc<dyn std::any::Any>> {
192        std::collections::HashMap::new()
193    }
194
195    /// Whether a render target can be created for the surfaces right now.
196    fn is_ready_to_create_render_target(&self, _surfaces: &[std::sync::Arc<dyn IPlatformRenderSurface>]) -> bool {
197        true
198    }
199
200    /// Releases the context.
201    fn dispose(&self);
202}