1use super::*;
2
3impl FrameRenderParams {
5 #[must_use]
15 pub fn new(viewport: Size) -> Self {
16 Self {
17 viewport,
18 scale_factor: 1.0,
19 clear_color: COLOR_WHITE,
20 layout_stats: LayoutStats::default(),
21 damage: DamageRegion::full(),
22 }
23 }
24
25 #[must_use]
36 pub fn with_scale(mut self, scale_factor: f32) -> Self {
37 self.set_scale_factor(scale_factor.max(0.5));
38 self
39 }
40
41 #[must_use]
52 pub fn with_clear(mut self, clear_color: Color) -> Self {
53 self.set_clear_color(clear_color);
54 self
55 }
56
57 #[must_use]
68 pub fn with_layout_stats(mut self, layout_stats: LayoutStats) -> Self {
69 self.set_layout_stats(layout_stats);
70 self
71 }
72
73 #[must_use]
84 pub fn with_damage(mut self, damage: DamageRegion) -> Self {
85 self.set_damage(damage);
86 self
87 }
88}
89
90impl Renderer {
92 #[must_use]
106 pub fn new(
107 platform: PlatformKind,
108 capabilities: PlatformCapabilities,
109 width: u32,
110 height: u32,
111 config: RenderConfig,
112 ) -> Self {
113 let (active_mode, active_backend): (RenderMode, GraphicsBackend) =
114 Self::resolve_mode_and_backend(platform, capabilities, config);
115 let readback_mirror: bool = true;
116 let compute_pixel_hash: bool =
117 platform == PlatformKind::Headless || active_backend == GraphicsBackend::Headless;
118 let font_provider: NativeSystemFont = if platform == PlatformKind::Headless {
119 NativeSystemFont::deterministic()
120 } else {
121 NativeSystemFont::system()
122 };
123 let mut display_list: DisplayList = DisplayList::default();
124 display_list.get_mut_commands().reserve(128);
125 let (sw_w, sw_h): (u32, u32) = if active_mode == RenderMode::Software {
126 (width, height)
127 } else {
128 (1, 1)
129 };
130 let (hw_w, hw_h, hw_backend): (u32, u32, GraphicsBackend) =
131 if matches!(active_mode, RenderMode::Hardware | RenderMode::Auto) {
132 (width, height, active_backend)
133 } else {
134 (1, 1, GraphicsBackend::Headless)
135 };
136 let (hy_w, hy_h): (u32, u32) = if active_mode == RenderMode::Hybrid {
137 (width, height)
138 } else {
139 (1, 1)
140 };
141 Self {
142 config,
143 active_mode,
144 active_backend,
145 software: SoftwareRenderer::with_font_provider(
146 sw_w,
147 sw_h,
148 config,
149 font_provider.clone(),
150 ),
151 hardware: HardwareRenderer::with_backend_and_font(
152 hw_backend,
153 hw_w,
154 hw_h,
155 config,
156 readback_mirror,
157 font_provider.clone(),
158 ),
159 hybrid: HybridRenderer::with_backend_and_font(
160 active_backend,
161 hy_w,
162 hy_h,
163 config,
164 font_provider,
165 ),
166 paint_pass: PaintPass,
167 paint_fragment_cache: PaintFragmentCache::default(),
168 display_list,
169 frame_index: 0,
170 compute_pixel_hash,
171 last_hashed_mode: None,
172 last_hashed_dims: (0, 0),
173 last_stats: FrameStats::default(),
174 }
175 }
176
177 #[must_use]
189 pub fn resolve_mode_and_backend(
190 platform: PlatformKind,
191 capabilities: PlatformCapabilities,
192 config: RenderConfig,
193 ) -> (RenderMode, GraphicsBackend) {
194 let default_gpu: GraphicsBackend = match platform {
195 PlatformKind::MacOs | PlatformKind::Ios | PlatformKind::IpadOs => {
196 GraphicsBackend::Metal
197 }
198 PlatformKind::Windows => GraphicsBackend::Direct3D12,
199 PlatformKind::Linux | PlatformKind::Android => GraphicsBackend::Vulkan,
200 PlatformKind::Web => GraphicsBackend::WebGpu,
201 PlatformKind::Headless => GraphicsBackend::Headless,
202 };
203 let mode: RenderMode = match config.get_mode() {
204 RenderMode::Auto => {
205 if capabilities.get_hardware_gpu() {
206 RenderMode::Hardware
207 } else {
208 RenderMode::Software
209 }
210 }
211 explicit => explicit,
212 };
213 let backend: GraphicsBackend = match mode {
214 RenderMode::Software => match platform {
215 PlatformKind::Web => GraphicsBackend::Canvas2d,
216 _ => GraphicsBackend::SoftwareCpu,
217 },
218 RenderMode::Hardware | RenderMode::Hybrid => {
219 config.preferred_backend_value().unwrap_or(default_gpu)
220 }
221 RenderMode::Auto => default_gpu,
222 };
223 (mode, backend)
224 }
225
226 pub fn switch_mode(
235 &mut self,
236 platform: PlatformKind,
237 capabilities: PlatformCapabilities,
238 mode: RenderMode,
239 ) {
240 let mut next_cfg: RenderConfig = self.get_config();
241 next_cfg.set_mode(mode);
242 let (resolved_mode, resolved_backend): (RenderMode, GraphicsBackend) =
243 Self::resolve_mode_and_backend(platform, capabilities, next_cfg);
244 self.set_config(next_cfg);
245 self.set_active_mode(resolved_mode);
246 self.set_active_backend(resolved_backend);
247 let Self {
248 hardware,
249 hybrid,
250 last_hashed_mode,
251 ..
252 } = self;
253 *last_hashed_mode = None;
254 hardware.get_mut_driver().switch_backend(resolved_backend);
255 hybrid
256 .get_mut_compositor()
257 .set_backend_kind(resolved_backend);
258 }
259
260 pub fn render_frame(
276 &mut self,
277 tree: &SceneTree,
278 viewport: Size,
279 scale_factor: f32,
280 clear_color: Color,
281 layout_stats: LayoutStats,
282 damage: DamageRegion,
283 ) -> Result<FrameStats, PlatformError> {
284 let scale: f32 = scale_factor.max(0.5);
285 let phys_w: u32 = ((viewport.get_width() * scale).round() as u32).max(1);
286 let phys_h: u32 = ((viewport.get_height() * scale).round() as u32).max(1);
287 let phys_dims: (u32, u32) = (phys_w, phys_h);
288 let Self {
289 active_mode,
290 active_backend,
291 software,
292 hardware,
293 hybrid,
294 paint_pass,
295 paint_fragment_cache,
296 display_list,
297 frame_index,
298 compute_pixel_hash,
299 last_hashed_mode,
300 last_hashed_dims,
301 last_stats,
302 ..
303 } = self;
304 let mode: RenderMode = *active_mode;
305 let backend: GraphicsBackend = *active_backend;
306 let do_pixel_hash: bool = *compute_pixel_hash;
307 let hash_reusable: bool = *last_hashed_mode == Some(mode) && *last_hashed_dims == phys_dims;
308 if !damage.get_full_damage() && damage.bounds_rect().is_none() && hash_reusable {
309 *frame_index = frame_index.wrapping_add(1);
310 let stats: FrameStats = FrameStats {
311 frame_index: *frame_index,
312 active_mode: mode,
313 active_backend: backend,
314 layout_nodes: layout_stats.get_nodes_computed(),
315 layout_skipped: layout_stats.get_subtrees_skipped(),
316 draw_commands: 0,
317 draw_batches: 0,
318 pixels_shaded: 0,
319 frame_hash: last_stats.get_frame_hash(),
320 };
321 *last_stats = stats;
322 return Ok(stats);
323 }
324 let _: &PaintPass = paint_pass;
325 let layout_changed: bool = layout_stats.get_nodes_computed() > 0;
326 PaintPass::build_retained(
327 tree,
328 viewport,
329 scale,
330 clear_color,
331 layout_changed,
332 display_list,
333 paint_fragment_cache,
334 );
335 let cmd_count: usize = display_list.len();
336 let (batches, pixels, hash): (usize, usize, u64) = match mode {
337 RenderMode::Software => {
338 let (b, p): (usize, usize) = software.render(display_list, damage);
339 let h: u64 = if p == 0 && hash_reusable {
340 last_stats.get_frame_hash()
341 } else if do_pixel_hash {
342 software.get_framebuffer().fnv1a_hash()
343 } else {
344 display_list.fast_hash()
345 };
346 (b, p, h)
347 }
348 RenderMode::Hybrid => {
349 let (b, p): (usize, usize) = hybrid.render(display_list, damage)?;
350 let h: u64 = if p == 0 && hash_reusable {
351 last_stats.get_frame_hash()
352 } else if do_pixel_hash {
353 hybrid.get_software().get_framebuffer().fnv1a_hash()
354 } else {
355 display_list.fast_hash()
356 };
357 (b, p, h)
358 }
359 RenderMode::Hardware | RenderMode::Auto => {
360 let (b, p): (usize, usize) = hardware.render(display_list, damage)?;
361 let h: u64 = if p == 0 && hash_reusable {
362 last_stats.get_frame_hash()
363 } else if hardware.get_enable_readback() {
364 if do_pixel_hash {
365 hardware.get_readback().get_framebuffer().fnv1a_hash()
366 } else {
367 display_list.fast_hash()
368 }
369 } else {
370 hardware.batch_hash()
371 };
372 (b, p, h)
373 }
374 };
375 *last_hashed_mode = Some(mode);
376 *last_hashed_dims = phys_dims;
377 *frame_index = frame_index.wrapping_add(1);
378 let stats: FrameStats = FrameStats {
379 frame_index: *frame_index,
380 active_mode: mode,
381 active_backend: backend,
382 layout_nodes: layout_stats.get_nodes_computed(),
383 layout_skipped: layout_stats.get_subtrees_skipped(),
384 draw_commands: cmd_count,
385 draw_batches: batches,
386 pixels_shaded: pixels,
387 frame_hash: hash,
388 };
389 *last_stats = stats;
390 Ok(stats)
391 }
392
393 #[inline(always)]
405 pub fn render_with(
406 &mut self,
407 tree: &SceneTree,
408 params: &FrameRenderParams,
409 ) -> Result<FrameStats, PlatformError> {
410 self.render_frame(
411 tree,
412 params.get_viewport(),
413 params.get_scale_factor(),
414 params.get_clear_color(),
415 params.get_layout_stats(),
416 params.get_damage(),
417 )
418 }
419
420 pub fn record_skipped_frame(&mut self) -> FrameStats {
430 let next_idx: u64 = self.get_frame_index().wrapping_add(1);
431 self.set_frame_index(next_idx);
432 let mut stats: FrameStats = self.get_last_stats();
433 stats.set_frame_index(next_idx);
434 stats.set_layout_nodes(0);
435 stats.set_draw_batches(0);
436 stats.set_pixels_shaded(0);
437 self.set_last_stats(stats);
438 stats
439 }
440
441 #[must_use]
451 pub fn paint_fragment_cache_stats(&self) -> (u64, u64, u64, u64) {
452 self.get_paint_fragment_cache().stats()
453 }
454
455 #[must_use]
465 pub fn framebuffer(&self) -> Option<&Framebuffer> {
466 match self.get_active_mode() {
467 RenderMode::Software => Some(self.get_software().get_framebuffer()),
468 RenderMode::Hybrid => Some(self.get_hybrid().get_software().get_framebuffer()),
469 RenderMode::Hardware | RenderMode::Auto => {
470 if self.get_hardware().get_enable_readback() {
471 Some(self.get_hardware().get_readback().get_framebuffer())
472 } else {
473 None
474 }
475 }
476 }
477 }
478}