euv_core/render/pipeline/
impl.rs1use 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 Self {
126 config,
127 active_mode,
128 active_backend,
129 software: SoftwareRenderer::with_font_provider(
130 width,
131 height,
132 config,
133 font_provider.clone(),
134 ),
135 hardware: HardwareRenderer::with_backend_and_font(
136 active_backend,
137 width,
138 height,
139 config,
140 readback_mirror,
141 font_provider.clone(),
142 ),
143 hybrid: HybridRenderer::with_backend_and_font(
144 active_backend,
145 width,
146 height,
147 config,
148 font_provider,
149 ),
150 paint_pass: PaintPass,
151 paint_fragment_cache: PaintFragmentCache::default(),
152 display_list,
153 frame_index: 0,
154 compute_pixel_hash,
155 last_hashed_mode: None,
156 last_hashed_dims: (0, 0),
157 last_stats: FrameStats::default(),
158 }
159 }
160
161 #[must_use]
173 pub fn resolve_mode_and_backend(
174 platform: PlatformKind,
175 capabilities: PlatformCapabilities,
176 config: RenderConfig,
177 ) -> (RenderMode, GraphicsBackend) {
178 let default_gpu: GraphicsBackend = match platform {
179 PlatformKind::MacOs | PlatformKind::Ios | PlatformKind::IpadOs => {
180 GraphicsBackend::Metal
181 }
182 PlatformKind::Windows => GraphicsBackend::Direct3D12,
183 PlatformKind::Linux | PlatformKind::Android => GraphicsBackend::Vulkan,
184 PlatformKind::Web => GraphicsBackend::WebGpu,
185 PlatformKind::Headless => GraphicsBackend::Headless,
186 };
187 let mode: RenderMode = match config.get_mode() {
188 RenderMode::Auto => {
189 if capabilities.get_hardware_gpu() {
190 RenderMode::Hardware
191 } else {
192 RenderMode::Software
193 }
194 }
195 explicit => explicit,
196 };
197 let backend: GraphicsBackend = match mode {
198 RenderMode::Software => match platform {
199 PlatformKind::Web => GraphicsBackend::Canvas2d,
200 _ => GraphicsBackend::SoftwareCpu,
201 },
202 RenderMode::Hardware | RenderMode::Hybrid => {
203 config.preferred_backend_value().unwrap_or(default_gpu)
204 }
205 RenderMode::Auto => default_gpu,
206 };
207 (mode, backend)
208 }
209
210 pub fn switch_mode(
219 &mut self,
220 platform: PlatformKind,
221 capabilities: PlatformCapabilities,
222 mode: RenderMode,
223 ) {
224 let mut next_cfg: RenderConfig = self.get_config();
225 next_cfg.set_mode(mode);
226 let (resolved_mode, resolved_backend): (RenderMode, GraphicsBackend) =
227 Self::resolve_mode_and_backend(platform, capabilities, next_cfg);
228 self.set_config(next_cfg);
229 self.set_active_mode(resolved_mode);
230 self.set_active_backend(resolved_backend);
231 let Self {
232 hardware,
233 hybrid,
234 last_hashed_mode,
235 ..
236 } = self;
237 *last_hashed_mode = None;
238 hardware.get_mut_driver().switch_backend(resolved_backend);
239 hybrid
240 .get_mut_compositor()
241 .set_backend_kind(resolved_backend);
242 }
243
244 pub fn render_frame(
260 &mut self,
261 tree: &SceneTree,
262 viewport: Size,
263 scale_factor: f32,
264 clear_color: Color,
265 layout_stats: LayoutStats,
266 damage: DamageRegion,
267 ) -> Result<FrameStats, PlatformError> {
268 let scale: f32 = scale_factor.max(0.5);
269 let phys_w: u32 = ((viewport.get_width() * scale).round() as u32).max(1);
270 let phys_h: u32 = ((viewport.get_height() * scale).round() as u32).max(1);
271 let phys_dims: (u32, u32) = (phys_w, phys_h);
272 let Self {
273 active_mode,
274 active_backend,
275 software,
276 hardware,
277 hybrid,
278 paint_pass,
279 paint_fragment_cache,
280 display_list,
281 frame_index,
282 compute_pixel_hash,
283 last_hashed_mode,
284 last_hashed_dims,
285 last_stats,
286 ..
287 } = self;
288 let mode: RenderMode = *active_mode;
289 let backend: GraphicsBackend = *active_backend;
290 let do_pixel_hash: bool = *compute_pixel_hash;
291 let hash_reusable: bool = *last_hashed_mode == Some(mode) && *last_hashed_dims == phys_dims;
292 if !damage.get_full_damage() && damage.bounds_rect().is_none() && hash_reusable {
293 *frame_index = frame_index.wrapping_add(1);
294 let stats: FrameStats = FrameStats {
295 frame_index: *frame_index,
296 active_mode: mode,
297 active_backend: backend,
298 layout_nodes: layout_stats.get_nodes_computed(),
299 layout_skipped: layout_stats.get_subtrees_skipped(),
300 draw_commands: 0,
301 draw_batches: 0,
302 pixels_shaded: 0,
303 frame_hash: last_stats.get_frame_hash(),
304 };
305 *last_stats = stats;
306 return Ok(stats);
307 }
308 let _: &PaintPass = paint_pass;
309 let layout_changed: bool = layout_stats.get_nodes_computed() > 0;
310 PaintPass::build_retained(
311 tree,
312 viewport,
313 scale,
314 clear_color,
315 layout_changed,
316 display_list,
317 paint_fragment_cache,
318 );
319 let cmd_count: usize = display_list.len();
320 let (batches, pixels, hash): (usize, usize, u64) = match mode {
321 RenderMode::Software => {
322 let (b, p): (usize, usize) = software.render(display_list, damage);
323 let h: u64 = if p == 0 && hash_reusable {
324 last_stats.get_frame_hash()
325 } else if do_pixel_hash {
326 software.get_framebuffer().fnv1a_hash()
327 } else {
328 display_list.fast_hash()
329 };
330 (b, p, h)
331 }
332 RenderMode::Hybrid => {
333 let (b, p): (usize, usize) = hybrid.render(display_list, damage)?;
334 let h: u64 = if p == 0 && hash_reusable {
335 last_stats.get_frame_hash()
336 } else if do_pixel_hash {
337 hybrid.get_software().get_framebuffer().fnv1a_hash()
338 } else {
339 display_list.fast_hash()
340 };
341 (b, p, h)
342 }
343 RenderMode::Hardware | RenderMode::Auto => {
344 let (b, p): (usize, usize) = hardware.render(display_list, damage)?;
345 let h: u64 = if p == 0 && hash_reusable {
346 last_stats.get_frame_hash()
347 } else if hardware.get_enable_readback() {
348 if do_pixel_hash {
349 hardware.get_readback().get_framebuffer().fnv1a_hash()
350 } else {
351 display_list.fast_hash()
352 }
353 } else {
354 hardware.batch_hash()
355 };
356 (b, p, h)
357 }
358 };
359 *last_hashed_mode = Some(mode);
360 *last_hashed_dims = phys_dims;
361 *frame_index = frame_index.wrapping_add(1);
362 let stats: FrameStats = FrameStats {
363 frame_index: *frame_index,
364 active_mode: mode,
365 active_backend: backend,
366 layout_nodes: layout_stats.get_nodes_computed(),
367 layout_skipped: layout_stats.get_subtrees_skipped(),
368 draw_commands: cmd_count,
369 draw_batches: batches,
370 pixels_shaded: pixels,
371 frame_hash: hash,
372 };
373 *last_stats = stats;
374 Ok(stats)
375 }
376
377 #[inline(always)]
389 pub fn render_with(
390 &mut self,
391 tree: &SceneTree,
392 params: &FrameRenderParams,
393 ) -> Result<FrameStats, PlatformError> {
394 self.render_frame(
395 tree,
396 params.get_viewport(),
397 params.get_scale_factor(),
398 params.get_clear_color(),
399 params.get_layout_stats(),
400 params.get_damage(),
401 )
402 }
403
404 pub fn record_skipped_frame(&mut self) -> FrameStats {
414 let next_idx: u64 = self.get_frame_index().wrapping_add(1);
415 self.set_frame_index(next_idx);
416 let mut stats: FrameStats = self.get_last_stats();
417 stats.set_frame_index(next_idx);
418 stats.set_layout_nodes(0);
419 stats.set_draw_batches(0);
420 stats.set_pixels_shaded(0);
421 self.set_last_stats(stats);
422 stats
423 }
424
425 #[must_use]
435 pub fn paint_fragment_cache_stats(&self) -> (u64, u64, u64, u64) {
436 self.get_paint_fragment_cache().stats()
437 }
438
439 #[must_use]
449 pub fn framebuffer(&self) -> Option<&Framebuffer> {
450 match self.get_active_mode() {
451 RenderMode::Software => Some(self.get_software().get_framebuffer()),
452 RenderMode::Hybrid => Some(self.get_hybrid().get_software().get_framebuffer()),
453 RenderMode::Hardware | RenderMode::Auto => {
454 if self.get_hardware().get_enable_readback() {
455 Some(self.get_hardware().get_readback().get_framebuffer())
456 } else {
457 None
458 }
459 }
460 }
461 }
462}