Skip to content

Commit 92d61e5

Browse files
committed
Use softbuffer fork with owned pixel buffer
1 parent e134a82 commit 92d61e5

2 files changed

Lines changed: 30 additions & 17 deletions

File tree

tiny_skia/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ geometry = ["iced_graphics/geometry"]
1010

1111
[dependencies]
1212
raw-window-handle = "0.5"
13-
softbuffer = "0.2"
1413
tiny-skia = "0.8"
1514
bytemuck = "1"
1615
rustc-hash = "1.1"
@@ -22,6 +21,10 @@ version = "0.7"
2221
path = "../graphics"
2322
features = ["tiny-skia"]
2423

24+
[dependencies.softbuffer]
25+
git = "https://github.com/rust-windowing/softbuffer"
26+
rev = "872c66a4c05fd7cb6cb133154f75fdce45a175a6"
27+
2528
[dependencies.cosmic-text]
2629
features = ["std", "swash"]
2730
git = "https://github.com/pop-os/cosmic-text"

tiny_skia/src/window/compositor.rs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ use crate::{Backend, Renderer, Settings};
55

66
use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle};
77
use std::marker::PhantomData;
8+
use std::num::NonZeroU32;
89

910
pub struct Compositor<Theme> {
1011
clip_mask: tiny_skia::ClipMask,
1112
_theme: PhantomData<Theme>,
1213
}
1314

1415
pub struct Surface {
15-
window: softbuffer::GraphicsContext,
16-
buffer: Vec<u32>,
16+
window: softbuffer::Surface,
1717
}
1818

1919
impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
@@ -36,14 +36,20 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
3636
width: u32,
3737
height: u32,
3838
) -> Surface {
39-
let window =
40-
unsafe { softbuffer::GraphicsContext::new(window, window) }
41-
.expect("Create softbuffer for window");
39+
let platform = unsafe { softbuffer::Context::new(window) }
40+
.expect("Create softbuffer context");
4241

43-
Surface {
44-
window,
45-
buffer: vec![0; width as usize * height as usize],
46-
}
42+
let mut window = unsafe { softbuffer::Surface::new(&platform, window) }
43+
.expect("Create softbuffer surface");
44+
45+
window
46+
.resize(
47+
NonZeroU32::new(width).unwrap(),
48+
NonZeroU32::new(height).unwrap(),
49+
)
50+
.expect("Resize surface");
51+
52+
Surface { window }
4753
}
4854

4955
fn configure_surface(
@@ -52,7 +58,13 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
5258
width: u32,
5359
height: u32,
5460
) {
55-
surface.buffer.resize((width * height) as usize, 0);
61+
surface
62+
.window
63+
.resize(
64+
NonZeroU32::new(width).unwrap(),
65+
NonZeroU32::new(height).unwrap(),
66+
)
67+
.expect("Resize surface");
5668
}
5769

5870
fn fetch_information(&self) -> Information {
@@ -106,9 +118,11 @@ pub fn present<Theme, T: AsRef<str>>(
106118
) -> Result<(), compositor::SurfaceError> {
107119
let physical_size = viewport.physical_size();
108120

121+
let mut buffer = surface.window.buffer_mut().expect("Get window buffer");
122+
109123
let drawn = backend.draw(
110124
&mut tiny_skia::PixmapMut::from_bytes(
111-
bytemuck::cast_slice_mut(&mut surface.buffer),
125+
bytemuck::cast_slice_mut(&mut buffer),
112126
physical_size.width,
113127
physical_size.height,
114128
)
@@ -121,11 +135,7 @@ pub fn present<Theme, T: AsRef<str>>(
121135
);
122136

123137
if drawn {
124-
surface.window.set_buffer(
125-
&surface.buffer,
126-
physical_size.width as u16,
127-
physical_size.height as u16,
128-
);
138+
let _ = buffer.present();
129139
}
130140

131141
Ok(())

0 commit comments

Comments
 (0)