| 1 | //! Interface implemented by backends |
| 2 | |
| 3 | use crate::{InitError, Rect, SoftBufferError}; |
| 4 | |
| 5 | use raw_window_handle::{HasDisplayHandle, HasWindowHandle}; |
| 6 | use std::num::NonZeroU32; |
| 7 | |
| 8 | pub(crate) trait ContextInterface<D: HasDisplayHandle + ?Sized> { |
| 9 | fn new(display: D) -> Result<Self, InitError<D>> |
| 10 | where |
| 11 | D: Sized, |
| 12 | Self: Sized; |
| 13 | } |
| 14 | |
| 15 | pub(crate) trait SurfaceInterface<D: HasDisplayHandle + ?Sized, W: HasWindowHandle + ?Sized> { |
| 16 | type Context: ContextInterface<D>; |
| 17 | type Buffer<'a>: BufferInterface |
| 18 | where |
| 19 | Self: 'a; |
| 20 | |
| 21 | fn new(window: W, context: &Self::Context) -> Result<Self, InitError<W>> |
| 22 | where |
| 23 | W: Sized, |
| 24 | Self: Sized; |
| 25 | /// Get the inner window handle. |
| 26 | fn window(&self) -> &W; |
| 27 | /// Resize the internal buffer to the given width and height. |
| 28 | fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError>; |
| 29 | /// Get a mutable reference to the buffer. |
| 30 | fn buffer_mut(&mut self) -> Result<Self::Buffer<'_>, SoftBufferError>; |
| 31 | /// Fetch the buffer from the window. |
| 32 | fn fetch(&mut self) -> Result<Vec<u32>, SoftBufferError> { |
| 33 | Err(SoftBufferError::Unimplemented) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | pub(crate) trait BufferInterface { |
| 38 | fn pixels(&self) -> &[u32]; |
| 39 | fn pixels_mut(&mut self) -> &mut [u32]; |
| 40 | fn age(&self) -> u8; |
| 41 | fn present_with_damage(self, damage: &[Rect]) -> Result<(), SoftBufferError>; |
| 42 | fn present(self) -> Result<(), SoftBufferError>; |
| 43 | } |
| 44 | |