gpui_engine/renderer.rs
1//! The renderer contract that engines implement.
2
3use crate::{PlatformAtlas, Scene};
4#[cfg(any(test, feature = "test-support", feature = "bench-support"))]
5use gpui_types::{DevicePixels, Size};
6use std::sync::Arc;
7
8/// A renderer that presents a [`Scene`] to some target.
9///
10/// OS backends and alternative engines implement this; `gpui` produces the
11/// [`Scene`] and submits it through the window's platform implementation.
12///
13/// Image capture and offscreen rendering are capabilities of the renderer
14/// rather than of the OS window, so onscreen GPU renderers only need to
15/// implement [`SceneRenderer::draw`] and [`SceneRenderer::sprite_atlas`].
16pub trait SceneRenderer: 'static {
17 /// Encodes and submits `scene`, returning whether it was presented.
18 ///
19 /// Renderers that cannot observe presentation (or that render offscreen)
20 /// return `true`; window backends that pace themselves on compositor
21 /// callbacks use `false` to retry the frame later.
22 fn draw(&mut self, scene: &Scene) -> bool;
23
24 /// Returns the sprite atlas used by this renderer.
25 fn sprite_atlas(&self) -> Arc<dyn PlatformAtlas>;
26
27 /// Sets the size of the target that [`draw`](Self::draw) renders into.
28 ///
29 /// Onscreen renderers derive their target from the window and can ignore
30 /// this; headless renderers use it to size an offscreen texture.
31 #[cfg(any(test, feature = "test-support", feature = "bench-support"))]
32 fn set_viewport_size(&mut self, _size: Size<DevicePixels>) {}
33
34 /// Render a scene and return the result as an RGBA image.
35 #[cfg(any(test, feature = "test-support", feature = "bench-support"))]
36 fn render_scene_to_image(
37 &mut self,
38 _scene: &Scene,
39 _size: Size<DevicePixels>,
40 ) -> anyhow::Result<image::RgbaImage> {
41 anyhow::bail!("render_scene_to_image is not supported by this renderer")
42 }
43
44 /// Render a scene to an offscreen target without reading the result back.
45 ///
46 /// This is the headless analogue of presenting a frame: it performs the
47 /// same CPU-side scene encoding and GPU submission as drawing to a real
48 /// window, but doesn't block on GPU completion or copy pixels back.
49 #[cfg(any(test, feature = "test-support", feature = "bench-support"))]
50 fn render_scene(&mut self, _scene: &Scene, _size: Size<DevicePixels>) -> anyhow::Result<()> {
51 Ok(())
52 }
53}