Implement basic presentation with softbuffer for iced_tiny_skia

This commit is contained in:
Héctor Ramón Jiménez 2023-02-25 16:05:42 +01:00
parent 8c373cd497
commit 535d7a4d57
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
6 changed files with 90 additions and 30 deletions

View file

@ -10,7 +10,11 @@ pub struct Compositor<Theme> {
_theme: PhantomData<Theme>,
}
pub struct Surface;
pub struct Surface {
window: softbuffer::GraphicsContext,
pixels: tiny_skia::Pixmap,
buffer: Vec<u32>,
}
impl<Theme> iced_graphics::window::Compositor for Compositor<Theme> {
type Settings = Settings;
@ -28,19 +32,33 @@ impl<Theme> iced_graphics::window::Compositor for Compositor<Theme> {
fn create_surface<W: HasRawWindowHandle + HasRawDisplayHandle>(
&mut self,
_window: &W,
window: &W,
width: u32,
height: u32,
) -> Surface {
// TODO
Surface
let window =
unsafe { softbuffer::GraphicsContext::new(window, window) }
.expect("Create softbuffer for window");
let pixels = tiny_skia::Pixmap::new(width, height)
.expect("Create pixmap for window");
Surface {
window,
pixels,
buffer: vec![0; (width * height) as usize],
}
}
fn configure_surface(
&mut self,
_surface: &mut Surface,
_width: u32,
_height: u32,
surface: &mut Surface,
width: u32,
height: u32,
) {
// TODO
surface.pixels = tiny_skia::Pixmap::new(width, height)
.expect("Create pixmap for window");
surface.buffer = vec![0; (width * height) as usize];
}
fn fetch_information(&self) -> Information {
@ -84,13 +102,33 @@ pub fn new<Theme>(settings: Settings) -> (Compositor<Theme>, Backend) {
pub fn present<Theme, T: AsRef<str>>(
_compositor: &mut Compositor<Theme>,
_backend: &mut Backend,
_surface: &mut Surface,
_primitives: &[Primitive],
_viewport: &Viewport,
_background_color: Color,
_overlay: &[T],
backend: &mut Backend,
surface: &mut Surface,
primitives: &[Primitive],
viewport: &Viewport,
background_color: Color,
overlay: &[T],
) -> Result<(), compositor::SurfaceError> {
backend.draw(
&mut surface.pixels,
primitives,
viewport,
background_color,
overlay,
);
for (i, pixel) in surface.pixels.pixels_mut().iter().enumerate() {
surface.buffer[i] = u32::from(pixel.red()) << 16
| u32::from(pixel.green()) << 8
| u32::from(pixel.blue());
}
surface.window.set_buffer(
&surface.buffer,
surface.pixels.width() as u16,
surface.pixels.height() as u16,
);
// TODO
Ok(())
}