Use Surface::buffer directly for drawing in iced_tiny_skia
... with a nice little color trick :^)
This commit is contained in:
parent
df5d66423d
commit
744f3028f4
3 changed files with 16 additions and 23 deletions
|
|
@ -11,6 +11,7 @@ svg = []
|
|||
raw-window-handle = "0.5"
|
||||
softbuffer = "0.2"
|
||||
tiny-skia = "0.8"
|
||||
bytemuck = "1"
|
||||
|
||||
[dependencies.iced_native]
|
||||
version = "0.9"
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ impl Backend {
|
|||
|
||||
pub fn draw<T: AsRef<str>>(
|
||||
&mut self,
|
||||
pixels: &mut tiny_skia::Pixmap,
|
||||
pixels: &mut tiny_skia::PixmapMut<'_>,
|
||||
primitives: &[Primitive],
|
||||
viewport: &Viewport,
|
||||
background_color: Color,
|
||||
|
|
@ -39,7 +39,7 @@ impl Backend {
|
|||
|
||||
fn draw_primitive(
|
||||
primitive: &Primitive,
|
||||
pixels: &mut tiny_skia::Pixmap,
|
||||
pixels: &mut tiny_skia::PixmapMut<'_>,
|
||||
clip_mask: Option<&tiny_skia::ClipMask>,
|
||||
scale_factor: f32,
|
||||
translation: Vector,
|
||||
|
|
@ -153,12 +153,12 @@ fn draw_primitive(
|
|||
}
|
||||
|
||||
fn into_color(color: Color) -> tiny_skia::Color {
|
||||
tiny_skia::Color::from_rgba(color.r, color.g, color.b, color.a)
|
||||
tiny_skia::Color::from_rgba(color.b, color.g, color.r, color.a)
|
||||
.expect("Convert color from iced to tiny_skia")
|
||||
}
|
||||
|
||||
fn rectangular_clip_mask(
|
||||
pixels: &tiny_skia::Pixmap,
|
||||
pixels: &tiny_skia::PixmapMut<'_>,
|
||||
bounds: Rectangle,
|
||||
) -> tiny_skia::ClipMask {
|
||||
let mut clip_mask = tiny_skia::ClipMask::new();
|
||||
|
|
|
|||
|
|
@ -12,7 +12,6 @@ pub struct Compositor<Theme> {
|
|||
|
||||
pub struct Surface {
|
||||
window: softbuffer::GraphicsContext,
|
||||
pixels: tiny_skia::Pixmap,
|
||||
buffer: Vec<u32>,
|
||||
}
|
||||
|
||||
|
|
@ -40,13 +39,9 @@ impl<Theme> iced_graphics::window::Compositor for Compositor<Theme> {
|
|||
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],
|
||||
buffer: vec![0; width as usize * height as usize],
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -56,9 +51,6 @@ impl<Theme> iced_graphics::window::Compositor for Compositor<Theme> {
|
|||
width: u32,
|
||||
height: u32,
|
||||
) {
|
||||
surface.pixels = tiny_skia::Pixmap::new(width, height)
|
||||
.expect("Create pixmap for window");
|
||||
|
||||
surface.buffer.resize((width * height) as usize, 0);
|
||||
}
|
||||
|
||||
|
|
@ -110,26 +102,26 @@ pub fn present<Theme, T: AsRef<str>>(
|
|||
background_color: Color,
|
||||
overlay: &[T],
|
||||
) -> Result<(), compositor::SurfaceError> {
|
||||
let physical_size = viewport.physical_size();
|
||||
|
||||
backend.draw(
|
||||
&mut surface.pixels,
|
||||
&mut tiny_skia::PixmapMut::from_bytes(
|
||||
bytemuck::cast_slice_mut(&mut surface.buffer),
|
||||
physical_size.width,
|
||||
physical_size.height,
|
||||
)
|
||||
.expect("Create pixel map"),
|
||||
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,
|
||||
physical_size.width as u16,
|
||||
physical_size.height as u16,
|
||||
);
|
||||
|
||||
// TODO
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue