Skip to main content

pebble/rendering/
backend.rs

1use crate::prelude::GPUSurfaceHandle;
2
3pub trait FrameOperations: Sync + Send + 'static {
4    type Context<'a>;
5    fn context(&mut self) -> Self::Context<'_>;
6}
7
8pub trait Backend: Sized + Sync + Send + 'static {
9    type Frame: FrameOperations;
10
11    fn init(handle: impl GPUSurfaceHandle, width: u32, height: u32) -> Self;
12    fn resize(&mut self, width: u32, height: u32);
13
14    fn acquire(&mut self) -> Option<Self::Frame>;
15    fn present(&mut self, frame: Self::Frame);
16}
17
18pub trait Drawable<B: Backend> {
19    fn draw(&self, pass: &mut <B::Frame as FrameOperations>::Context<'_>);
20}
21
22pub trait Bindable<B: Backend> {
23    fn bind(&self, pass: &mut <B::Frame as FrameOperations>::Context<'_>);
24}
25
26pub struct CurrentFrame<B: Backend> {
27    pub(crate) frame: Option<B::Frame>,
28}
29
30impl<B: Backend> CurrentFrame<B> {
31    pub fn is_active(&self) -> bool {
32        self.frame.is_some()
33    }
34
35    pub fn get_render_context(&mut self) -> Option<<B::Frame as FrameOperations>::Context<'_>> {
36        self.frame.as_mut().map(|frame| frame.context())
37    }
38}