pub trait SceneRenderer: Any {
// Required methods
fn draw(&mut self, scene: &Scene) -> bool;
fn sprite_atlas(&self) -> Arc<dyn PlatformAtlas> ⓘ;
// Provided methods
fn set_viewport_size(&mut self, _size: Size<DevicePixels>) { ... }
fn render_scene(
&mut self,
_scene: &Scene,
_size: Size<DevicePixels>,
) -> Result<()> { ... }
fn read_pixels(&mut self) -> Result<PixelBuffer> { ... }
fn render_scene_to_image(
&mut self,
scene: &Scene,
size: Size<DevicePixels>,
) -> Result<PixelBuffer> { ... }
}Expand description
A renderer that presents a Scene to some target.
OS backends and alternative engines implement this; gpui produces the
Scene and submits it through the window’s platform implementation.
Offscreen rendering is part of this contract rather than a testing shim: a renderer that is
never given a window is how server-side rendering, headless capture and snapshotting are
expressed, and the window’s renderer is the only thing that owns the device. Onscreen
renderers therefore only need draw and sprite_atlas;
the rest have defaults that report themselves unsupported.
Rendering and readback are separate steps on purpose. render_scene
fills a target without a CPU round trip, which is what a consumer on the GPU wants;
read_pixels pays for system memory only when a consumer on the CPU
needs it; render_scene_to_image is the two together.
Required Methods§
Sourcefn draw(&mut self, scene: &Scene) -> bool
fn draw(&mut self, scene: &Scene) -> bool
Encodes and submits scene, returning whether it was presented.
Renderers that cannot observe presentation (or that render offscreen)
return true; window backends that pace themselves on compositor
callbacks use false to retry the frame later.
Sourcefn sprite_atlas(&self) -> Arc<dyn PlatformAtlas> ⓘ
fn sprite_atlas(&self) -> Arc<dyn PlatformAtlas> ⓘ
Returns the sprite atlas used by this renderer.
Provided Methods§
Sourcefn set_viewport_size(&mut self, _size: Size<DevicePixels>)
fn set_viewport_size(&mut self, _size: Size<DevicePixels>)
Sets the size of the target that draw renders into.
Onscreen renderers derive their target from the window and can ignore this; headless renderers use it to size an offscreen texture.
Sourcefn render_scene(
&mut self,
_scene: &Scene,
_size: Size<DevicePixels>,
) -> Result<()>
fn render_scene( &mut self, _scene: &Scene, _size: Size<DevicePixels>, ) -> Result<()>
Renders scene into an offscreen target of size, without reading it back.
This is the headless analogue of presenting a frame: it performs the same CPU-side scene encoding and GPU submission as drawing to a real window, but does not block on GPU completion or copy pixels back.
Sourcefn read_pixels(&mut self) -> Result<PixelBuffer>
fn read_pixels(&mut self) -> Result<PixelBuffer>
Reads the offscreen target of the last render_scene back.
Only a renderer that has rendered offscreen can answer this; the failure is the same “unsupported” one, one step later.
Sourcefn render_scene_to_image(
&mut self,
scene: &Scene,
size: Size<DevicePixels>,
) -> Result<PixelBuffer>
fn render_scene_to_image( &mut self, scene: &Scene, size: Size<DevicePixels>, ) -> Result<PixelBuffer>
Renders scene offscreen and reads the result back.
Implementations§
Source§impl dyn SceneRenderer + '_
Downcasting for SceneRenderer trait objects.
impl dyn SceneRenderer + '_
Downcasting for SceneRenderer trait objects.
SceneRenderer is the GPU-agnostic seam: it presents a Scene and names no graphics API.
The renderer behind it is what owns the device — and the caches, the textures and the other
resources built on it — so a caller that needs those downcasts to the concrete backend renderer
here, rather than having the window lend its guest’s internals.
Sourcepub fn as_renderer<T: Any>(&self) -> Option<&T>
pub fn as_renderer<T: Any>(&self) -> Option<&T>
self as the concrete renderer T, if that is what it is.
Sourcepub fn as_renderer_mut<T: Any>(&mut self) -> Option<&mut T>
pub fn as_renderer_mut<T: Any>(&mut self) -> Option<&mut T>
The mutable form of as_renderer.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".