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"
|
raw-window-handle = "0.5"
|
||||||
softbuffer = "0.2"
|
softbuffer = "0.2"
|
||||||
tiny-skia = "0.8"
|
tiny-skia = "0.8"
|
||||||
|
bytemuck = "1"
|
||||||
|
|
||||||
[dependencies.iced_native]
|
[dependencies.iced_native]
|
||||||
version = "0.9"
|
version = "0.9"
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ impl Backend {
|
||||||
|
|
||||||
pub fn draw<T: AsRef<str>>(
|
pub fn draw<T: AsRef<str>>(
|
||||||
&mut self,
|
&mut self,
|
||||||
pixels: &mut tiny_skia::Pixmap,
|
pixels: &mut tiny_skia::PixmapMut<'_>,
|
||||||
primitives: &[Primitive],
|
primitives: &[Primitive],
|
||||||
viewport: &Viewport,
|
viewport: &Viewport,
|
||||||
background_color: Color,
|
background_color: Color,
|
||||||
|
|
@ -39,7 +39,7 @@ impl Backend {
|
||||||
|
|
||||||
fn draw_primitive(
|
fn draw_primitive(
|
||||||
primitive: &Primitive,
|
primitive: &Primitive,
|
||||||
pixels: &mut tiny_skia::Pixmap,
|
pixels: &mut tiny_skia::PixmapMut<'_>,
|
||||||
clip_mask: Option<&tiny_skia::ClipMask>,
|
clip_mask: Option<&tiny_skia::ClipMask>,
|
||||||
scale_factor: f32,
|
scale_factor: f32,
|
||||||
translation: Vector,
|
translation: Vector,
|
||||||
|
|
@ -153,12 +153,12 @@ fn draw_primitive(
|
||||||
}
|
}
|
||||||
|
|
||||||
fn into_color(color: Color) -> tiny_skia::Color {
|
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")
|
.expect("Convert color from iced to tiny_skia")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rectangular_clip_mask(
|
fn rectangular_clip_mask(
|
||||||
pixels: &tiny_skia::Pixmap,
|
pixels: &tiny_skia::PixmapMut<'_>,
|
||||||
bounds: Rectangle,
|
bounds: Rectangle,
|
||||||
) -> tiny_skia::ClipMask {
|
) -> tiny_skia::ClipMask {
|
||||||
let mut clip_mask = tiny_skia::ClipMask::new();
|
let mut clip_mask = tiny_skia::ClipMask::new();
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,6 @@ pub struct Compositor<Theme> {
|
||||||
|
|
||||||
pub struct Surface {
|
pub struct Surface {
|
||||||
window: softbuffer::GraphicsContext,
|
window: softbuffer::GraphicsContext,
|
||||||
pixels: tiny_skia::Pixmap,
|
|
||||||
buffer: Vec<u32>,
|
buffer: Vec<u32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -40,13 +39,9 @@ impl<Theme> iced_graphics::window::Compositor for Compositor<Theme> {
|
||||||
unsafe { softbuffer::GraphicsContext::new(window, window) }
|
unsafe { softbuffer::GraphicsContext::new(window, window) }
|
||||||
.expect("Create softbuffer for window");
|
.expect("Create softbuffer for window");
|
||||||
|
|
||||||
let pixels = tiny_skia::Pixmap::new(width, height)
|
|
||||||
.expect("Create pixmap for window");
|
|
||||||
|
|
||||||
Surface {
|
Surface {
|
||||||
window,
|
window,
|
||||||
pixels,
|
buffer: vec![0; width as usize * height as usize],
|
||||||
buffer: vec![0; (width * height) as usize],
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -56,9 +51,6 @@ impl<Theme> iced_graphics::window::Compositor for Compositor<Theme> {
|
||||||
width: u32,
|
width: u32,
|
||||||
height: u32,
|
height: u32,
|
||||||
) {
|
) {
|
||||||
surface.pixels = tiny_skia::Pixmap::new(width, height)
|
|
||||||
.expect("Create pixmap for window");
|
|
||||||
|
|
||||||
surface.buffer.resize((width * height) as usize, 0);
|
surface.buffer.resize((width * height) as usize, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -110,26 +102,26 @@ pub fn present<Theme, T: AsRef<str>>(
|
||||||
background_color: Color,
|
background_color: Color,
|
||||||
overlay: &[T],
|
overlay: &[T],
|
||||||
) -> Result<(), compositor::SurfaceError> {
|
) -> Result<(), compositor::SurfaceError> {
|
||||||
|
let physical_size = viewport.physical_size();
|
||||||
|
|
||||||
backend.draw(
|
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,
|
primitives,
|
||||||
viewport,
|
viewport,
|
||||||
background_color,
|
background_color,
|
||||||
overlay,
|
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.window.set_buffer(
|
||||||
&surface.buffer,
|
&surface.buffer,
|
||||||
surface.pixels.width() as u16,
|
physical_size.width as u16,
|
||||||
surface.pixels.height() as u16,
|
physical_size.height as u16,
|
||||||
);
|
);
|
||||||
|
|
||||||
// TODO
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue