1#![cfg(windows)]
3mod bitmap;
4mod external_objects;
5mod writeable_bitmap;
6pub use external_objects::{device_luid, native_device, D3D11Texture};
7pub fn device_generation() -> u64 {
8 unsafe { ffi::d2_device_generation() }
9}
10#[cfg(feature = "media-controls")]
11pub use external_objects::D3D11TextureImporter;
12#[cfg(feature = "runtime-shaders")]
13mod custom_shaders;
14mod drawing;
15mod ffi;
16mod fonts;
17mod presentation;
18pub use fonts::FontManager as Direct2DFontManager;
19pub use fonts::TextShaper as DirectWriteTextShaper;
20pub fn statistics() -> [u64; 12] {
24 extern "C" {
25 fn d2_statistics(out: *mut u64, count: usize);
26 }
27 let mut counters = [0; 12];
28 unsafe {
29 d2_statistics(counters.as_mut_ptr(), counters.len());
30 }
31 counters
32}
33#[doc(hidden)]
41pub fn profiling_statistics() -> ([u64; 20], [u64; 20]) {
42 extern "C" {
43 fn d2_profile(nanoseconds: *mut u64, counts: *mut u64, count: usize);
44 }
45 let mut nanoseconds = [0; 20];
46 let mut counts = [0; 20];
47 unsafe { d2_profile(nanoseconds.as_mut_ptr(), counts.as_mut_ptr(), counts.len()) };
48 (nanoseconds, counts)
49}
50mod geometry;
51mod path_measure;
52mod region;
53use bitmap::Bitmap;
54use drawing::GlyphRun;
55use ferroui_base::{
56 media::{
57 imaging::BitmapInterpolationMode, text_formatting::GlyphInfo, FillRule,
58 GeometryCombineMode, GlyphRun as MediaGlyphRun, GlyphTypeface,
59 },
60 platform::*,
61 FerroLocator, PixelSize, Point, Rect, Vector,
62};
63use ferroui_controls::AppBuilder;
64use geometry::{Builder, Geometry};
65use kurbo::BezPath;
66use std::{
67 io::{self, Read},
68 rc::Rc,
69 sync::Arc,
70};
71
72pub trait Direct2DApplicationExtensions {
73 fn use_direct2d(&self) -> AppBuilder;
74}
75impl Direct2DApplicationExtensions for AppBuilder {
76 fn use_direct2d(&self) -> AppBuilder {
77 self.use_rendering_subsystem(
78 || {
79 assert!(unsafe { ffi::d2_supported() } != 0, "Native rendering requires Windows 10 1709 or later and hardware D3D11 feature level 11.0 or later");
80 FerroLocator::current_mutable()
81 .bind::<dyn IPlatformRenderInterface>()
82 .to_constant(Rc::new(Direct2DRenderInterface) as Rc<dyn IPlatformRenderInterface>)
83 .bind::<dyn IFontManagerImpl>()
84 .to_constant(Rc::new(fonts::FontManager::default()) as Rc<dyn IFontManagerImpl>);
85 },
86 "Direct2D",
87 )
88 .use_text_shaping_subsystem(
89 || {
90 FerroLocator::current_mutable()
91 .bind::<dyn ITextShaperImpl>()
92 .to_constant(Rc::new(fonts::TextShaper) as Rc<dyn ITextShaperImpl>);
93 },
94 "DirectWrite",
95 )
96 }
97}
98pub struct Direct2DRenderInterface;
99fn resized(
100 stream: &mut dyn Read,
101 dimension: i32,
102 width: bool,
103 mode: BitmapInterpolationMode,
104) -> io::Result<Bitmap> {
105 if dimension <= 0 {
106 return Err(io::Error::new(
107 io::ErrorKind::InvalidInput,
108 "Decode dimension must be positive",
109 ));
110 }
111 let bitmap = Bitmap::decode(stream)?;
112 let s = bitmap.pixel_size();
113 let size = if width {
114 PixelSize::new(
115 dimension,
116 (s.height as f64 * dimension as f64 / s.width as f64)
117 .floor()
118 .max(1.) as i32,
119 )
120 } else {
121 PixelSize::new(
122 (s.width as f64 * dimension as f64 / s.height as f64)
123 .floor()
124 .max(1.) as i32,
125 dimension,
126 )
127 };
128 let resized = Bitmap::resized(&bitmap, size, mode);
129 if resized.is_corrupted() {
132 return Err(io::Error::other(
133 "Direct2D could not create a surface for the resized image",
134 ));
135 }
136 Ok(resized)
137}
138impl IPlatformRenderInterface for Direct2DRenderInterface {
139 fn create_ellipse_geometry(&self, r: Rect) -> Arc<dyn IGeometryImpl> {
140 Geometry::ellipse(r)
141 }
142 fn create_line_geometry(&self, a: Point, b: Point) -> Arc<dyn IGeometryImpl> {
143 let mut p = BezPath::new();
144 p.move_to((a.x, a.y));
145 p.line_to((b.x, b.y));
146 Geometry::paths(p, None, FillRule::NonZero)
147 }
148 fn create_rectangle_geometry(&self, r: Rect) -> Arc<dyn IGeometryImpl> {
149 Geometry::rectangle(r)
150 }
151 fn create_stream_geometry_builder(
152 &self,
153 s: Option<&dyn IGeometryImpl>,
154 ) -> Box<dyn IStreamGeometryBuilder> {
155 Box::new(Builder::new(s))
156 }
157 fn create_geometry_group(
158 &self,
159 rule: FillRule,
160 children: &[Arc<dyn IGeometryImpl>],
161 ) -> Arc<dyn IGeometryImpl> {
162 let mut stroke = BezPath::new();
163 let mut fill = BezPath::new();
164 for child in children {
165 let g = child
166 .as_any()
167 .downcast_ref::<Geometry>()
168 .expect("Direct2D geometry group requires Direct2D children");
169 stroke.extend(g.stroke.elements().iter().copied());
170 if let Some(p) = &g.fill {
171 fill.extend(p.elements().iter().copied())
172 }
173 }
174 Geometry::paths(stroke, Some(fill), rule)
175 }
176 fn create_combined_geometry(
177 &self,
178 m: GeometryCombineMode,
179 a: Arc<dyn IGeometryImpl>,
180 b: Arc<dyn IGeometryImpl>,
181 ) -> Arc<dyn IGeometryImpl> {
182 Geometry::combine(&*a, &*b, m)
183 }
184 fn build_glyph_run_geometry(&self, g: &MediaGlyphRun) -> Arc<dyn IGeometryImpl> {
185 let run = g.platform_impl();
186 let run = run
187 .as_any()
188 .downcast_ref::<GlyphRun>()
189 .expect("Direct2D glyph run");
190 Geometry::new(run.outlines().clone(), FillRule::NonZero)
191 }
192 fn create_glyph_run(
193 &self,
194 f: &Rc<GlyphTypeface>,
195 s: f64,
196 g: &[GlyphInfo],
197 p: Point,
198 ) -> Arc<dyn IGlyphRunImpl> {
199 Arc::new(GlyphRun::new(f, s, g, p))
200 }
201 fn create_render_target_bitmap(
202 &self,
203 s: PixelSize,
204 d: Vector,
205 ) -> Arc<dyn IRenderTargetBitmapImpl> {
206 Arc::new(Bitmap::new(s, d))
207 }
208 fn create_writeable_bitmap(
209 &self,
210 s: PixelSize,
211 d: Vector,
212 f: PixelFormat,
213 a: AlphaFormat,
214 ) -> Arc<dyn IWriteableBitmapImpl> {
215 Arc::new(writeable_bitmap::WriteableBitmap::new(s, d, f, a))
216 }
217 fn load_bitmap_from_file(&self, p: &str) -> io::Result<Arc<SharedBitmapImpl>> {
218 self.load_bitmap(&mut std::fs::File::open(p)?)
219 }
220 fn load_bitmap(&self, s: &mut dyn Read) -> io::Result<Arc<SharedBitmapImpl>> {
221 Ok(Arc::new(Bitmap::decode(s)?))
222 }
223 fn load_writeable_bitmap_to_width(
224 &self,
225 s: &mut dyn Read,
226 w: i32,
227 mode: BitmapInterpolationMode,
228 ) -> io::Result<Arc<dyn IWriteableBitmapImpl>> {
229 Ok(Arc::new(resized(s, w, true, mode)?))
230 }
231 fn load_writeable_bitmap_to_height(
232 &self,
233 s: &mut dyn Read,
234 h: i32,
235 mode: BitmapInterpolationMode,
236 ) -> io::Result<Arc<dyn IWriteableBitmapImpl>> {
237 Ok(Arc::new(resized(s, h, false, mode)?))
238 }
239 fn load_writeable_bitmap_from_file(
240 &self,
241 p: &str,
242 ) -> io::Result<Arc<dyn IWriteableBitmapImpl>> {
243 self.load_writeable_bitmap(&mut std::fs::File::open(p)?)
244 }
245 fn load_writeable_bitmap(&self, s: &mut dyn Read) -> io::Result<Arc<dyn IWriteableBitmapImpl>> {
246 Ok(Arc::new(Bitmap::decode(s)?))
247 }
248 fn load_bitmap_to_width(
249 &self,
250 s: &mut dyn Read,
251 w: i32,
252 mode: BitmapInterpolationMode,
253 ) -> io::Result<Arc<SharedBitmapImpl>> {
254 Ok(Arc::new(resized(s, w, true, mode)?))
255 }
256 fn load_bitmap_to_height(
257 &self,
258 s: &mut dyn Read,
259 h: i32,
260 mode: BitmapInterpolationMode,
261 ) -> io::Result<Arc<SharedBitmapImpl>> {
262 Ok(Arc::new(resized(s, h, false, mode)?))
263 }
264 fn resize_bitmap(
265 &self,
266 b: &dyn IBitmapImpl,
267 s: PixelSize,
268 mode: BitmapInterpolationMode,
269 ) -> Arc<SharedBitmapImpl> {
270 Arc::new(Bitmap::resized(b, s, mode))
271 }
272 fn load_bitmap_from_pixels(
273 &self,
274 f: PixelFormat,
275 a: AlphaFormat,
276 data: &[u8],
277 s: PixelSize,
278 d: Vector,
279 stride: i32,
280 ) -> Arc<SharedBitmapImpl> {
281 Arc::new(Bitmap::from_pixels(f, a, data, s, d, stride))
282 }
283 fn create_backend_context(
284 &self,
285 graphics: Option<Rc<dyn IPlatformGraphicsContext>>,
286 ) -> Rc<dyn IPlatformRenderInterfaceContext> {
287 let _ = graphics;
288 Rc::new(presentation::Context::new())
289 }
290 fn supports_individual_round_rects(&self) -> bool {
291 true
292 }
293 fn supports_compositing_layers(&self) -> bool {
294 true
295 }
296 fn default_alpha_format(&self) -> AlphaFormat {
297 AlphaFormat::Premul
298 }
299 fn default_pixel_format(&self) -> PixelFormat {
300 PixelFormat::BGRA8888
301 }
302 fn is_supported_bitmap_pixel_format(&self, f: PixelFormat) -> bool {
303 matches!(
304 f,
305 PixelFormat::BGRA8888 | PixelFormat::RGBA8888 | PixelFormat::RGB565
306 )
307 }
308 fn supports_regions(&self) -> bool {
309 true
310 }
311 fn create_region(&self) -> Rc<dyn IPlatformRenderInterfaceRegion> {
312 Rc::new(region::Direct2DRegionImpl::new())
313 }
314}
315
316#[cfg(test)]
317mod tests;
318
319#[cfg(test)]
320mod effects_tests;