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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use super::surfaces::IPlatformRenderSurface;
use super::{
AlphaFormat, IBitmapImpl, IDrawingContextLayerImpl, IGeometryImpl, IOptionalFeatureProvider,
IPlatformGraphicsContext, IPlatformRenderInterfaceRegion, IRenderTarget, IRenderTargetBitmapImpl,
IStreamGeometryBuilder, IWriteableBitmapImpl, PixelFormat,
};
use crate::media::imaging::BitmapInterpolationMode;
use crate::media::{FillRule, GeometryCombineMode};
use crate::{PixelSize, Point, Rect, Vector};
use std::io::Read;
use std::rc::Rc;
use std::sync::Arc;
/// Defines the main platform-specific interface for the rendering subsystem.
///
/// This is the contract a rendering backend implements; it is registered in
/// the service locator and everything that needs platform rendering objects
/// goes through it.
pub trait IPlatformRenderInterface: 'static {
/// Creates an ellipse geometry implementation.
fn create_ellipse_geometry(&self, rect: Rect) -> Arc<dyn IGeometryImpl>;
/// Creates a line geometry implementation.
fn create_line_geometry(&self, p1: Point, p2: Point) -> Arc<dyn IGeometryImpl>;
/// Creates a rectangle geometry implementation.
fn create_rectangle_geometry(&self, rect: Rect) -> Arc<dyn IGeometryImpl>;
/// Creates a builder used to define a stream geometry.
///
/// `source` is a geometry created by this render interface whose figures
/// the new geometry starts with, or `None` to start with an empty
/// geometry. `source` itself isn't modified.
fn create_stream_geometry_builder(&self, source: Option<&dyn IGeometryImpl>) -> Box<dyn IStreamGeometryBuilder>;
/// Creates a geometry group implementation.
fn create_geometry_group(&self, fill_rule: FillRule, children: &[Arc<dyn IGeometryImpl>]) -> Arc<dyn IGeometryImpl>;
/// Creates a geometry that combines two geometries.
fn create_combined_geometry(
&self,
combine_mode: GeometryCombineMode,
g1: Arc<dyn IGeometryImpl>,
g2: Arc<dyn IGeometryImpl>,
) -> Arc<dyn IGeometryImpl>;
/// Builds a glyph run geometry.
fn build_glyph_run_geometry(&self, glyph_run: &crate::media::GlyphRun) -> Arc<dyn IGeometryImpl>;
/// Builds all glyph contours without silently omitting unavailable glyphs.
///
/// Returns `None` if the backend cannot verify a complete monochrome
/// outline, or if any glyph requires bitmap or colour-font rendering.
/// Whitespace with an available empty outline is supported. This capability
/// is separate from `build_glyph_run_geometry`, whose traditional contract
/// permits partial outlines for effects and clipping.
fn try_build_glyph_run_geometry(&self, _glyph_run: &crate::media::GlyphRun) -> Option<Arc<dyn IGeometryImpl>> {
None
}
/// Creates a platform implementation of a glyph run.
///
/// * `glyph_typeface` — the glyph typeface.
/// * `font_rendering_em_size` — the font rendering em size.
/// * `glyph_infos` — the list of glyphs.
/// * `baseline_origin` — the baseline origin of the run. Can be zero.
fn create_glyph_run(
&self,
glyph_typeface: &Rc<crate::media::GlyphTypeface>,
font_rendering_em_size: f64,
glyph_infos: &[crate::media::text_formatting::GlyphInfo],
baseline_origin: Point,
) -> std::sync::Arc<dyn super::IGlyphRunImpl>;
/// Creates a render target bitmap.
///
/// `dpi` is the DPI of the bitmap; 96 means one pixel per logical unit.
fn create_render_target_bitmap(&self, size: PixelSize, dpi: Vector) -> std::sync::Arc<dyn IRenderTargetBitmapImpl>;
/// Creates a writeable bitmap.
fn create_writeable_bitmap(
&self,
size: PixelSize,
dpi: Vector,
format: PixelFormat,
alpha_format: AlphaFormat,
) -> std::sync::Arc<dyn IWriteableBitmapImpl>;
/// Loads a bitmap from a file.
fn load_bitmap_from_file(&self, file_name: &str) -> std::io::Result<std::sync::Arc<crate::platform::SharedBitmapImpl>>;
/// Loads a bitmap from a stream.
fn load_bitmap(&self, stream: &mut dyn Read) -> std::io::Result<std::sync::Arc<crate::platform::SharedBitmapImpl>>;
/// Loads a writeable bitmap from a stream, decoded to the given width.
fn load_writeable_bitmap_to_width(
&self,
stream: &mut dyn Read,
width: i32,
interpolation_mode: BitmapInterpolationMode,
) -> std::io::Result<std::sync::Arc<dyn IWriteableBitmapImpl>>;
/// Loads a writeable bitmap from a stream, decoded to the given height.
fn load_writeable_bitmap_to_height(
&self,
stream: &mut dyn Read,
height: i32,
interpolation_mode: BitmapInterpolationMode,
) -> std::io::Result<std::sync::Arc<dyn IWriteableBitmapImpl>>;
/// Loads a writeable bitmap from a file.
fn load_writeable_bitmap_from_file(&self, file_name: &str) -> std::io::Result<std::sync::Arc<dyn IWriteableBitmapImpl>>;
/// Loads a writeable bitmap from a stream.
fn load_writeable_bitmap(&self, stream: &mut dyn Read) -> std::io::Result<std::sync::Arc<dyn IWriteableBitmapImpl>>;
/// Loads a bitmap from a stream, decoded to the given width.
fn load_bitmap_to_width(
&self,
stream: &mut dyn Read,
width: i32,
interpolation_mode: BitmapInterpolationMode,
) -> std::io::Result<std::sync::Arc<crate::platform::SharedBitmapImpl>>;
/// Loads a bitmap from a stream, decoded to the given height.
fn load_bitmap_to_height(
&self,
stream: &mut dyn Read,
height: i32,
interpolation_mode: BitmapInterpolationMode,
) -> std::io::Result<std::sync::Arc<crate::platform::SharedBitmapImpl>>;
/// Creates a resized copy of a bitmap.
fn resize_bitmap(
&self,
bitmap_impl: &dyn IBitmapImpl,
destination_size: PixelSize,
interpolation_mode: BitmapInterpolationMode,
) -> std::sync::Arc<crate::platform::SharedBitmapImpl>;
/// Loads a bitmap from raw pixel data.
///
/// `data` holds `stride * size.height` bytes in the given formats.
fn load_bitmap_from_pixels(
&self,
format: PixelFormat,
alpha_format: AlphaFormat,
data: &[u8],
size: PixelSize,
dpi: Vector,
stride: i32,
) -> std::sync::Arc<crate::platform::SharedBitmapImpl>;
/// Creates a backend-specific render context from a platform graphics
/// context (`None` selects software rendering).
fn create_backend_context(
&self,
graphics_api_context: Option<Rc<dyn IPlatformGraphicsContext>>,
) -> Rc<dyn IPlatformRenderInterfaceContext>;
/// Whether the backend supports individual corner radii on rounded
/// rectangles.
fn supports_individual_round_rects(&self) -> bool;
/// Whether drawing contexts created by this backend support isolated
/// compositing layers for all drawing primitives. Recording contexts use
/// this capability before accepting a compositing-layer push.
fn supports_compositing_layers(&self) -> bool {
false
}
/// The default alpha format of the backend.
fn default_alpha_format(&self) -> AlphaFormat;
/// The default pixel format of the backend.
fn default_pixel_format(&self) -> PixelFormat;
/// Whether bitmaps can be created in the given pixel format.
fn is_supported_bitmap_pixel_format(&self, format: PixelFormat) -> bool;
/// Whether the backend supports regions.
fn supports_regions(&self) -> bool;
/// Creates an empty region.
fn create_region(&self) -> Rc<dyn IPlatformRenderInterfaceRegion>;
}
/// A render backend bound to a graphics context (or to software rendering).
pub trait IPlatformRenderInterfaceContext: IOptionalFeatureProvider {
/// Creates a render target for one of the given surfaces.
fn create_render_target(&self, surfaces: &[std::sync::Arc<dyn IPlatformRenderSurface>]) -> Rc<dyn IRenderTarget>;
/// Creates an offscreen render target.
fn create_offscreen_render_target(
&self,
pixel_size: PixelSize,
scaling: Vector,
enable_text_antialiasing: bool,
) -> Rc<dyn IDrawingContextLayerImpl>;
/// Whether the context is lost and must be recreated.
fn is_lost(&self) -> bool;
/// The largest offscreen render target the context can create, if
/// limited.
fn max_offscreen_render_target_pixel_size(&self) -> Option<PixelSize>;
/// The features of the context that are exposed to user code, by type.
fn public_features(&self) -> std::collections::HashMap<std::any::TypeId, Rc<dyn std::any::Any>> {
std::collections::HashMap::new()
}
/// Whether a render target can be created for the surfaces right now.
fn is_ready_to_create_render_target(&self, _surfaces: &[std::sync::Arc<dyn IPlatformRenderSurface>]) -> bool {
true
}
/// Releases the context.
fn dispose(&self);
}