Skip to content

Commit

Permalink
Remove surface argument of Compositor::screenshot
Browse files Browse the repository at this point in the history
This argument was completely ignored by the wgpu renderer, and used only
for the `clip_mask` by the `tiny_skia` renderer. I believe creating a
new clip mask is correct.

This way it's possible to render offscreen without needing a surface.
  • Loading branch information
ids1024 committed Nov 22, 2024
1 parent 4fe9e3d commit c34c478
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 32 deletions.
2 changes: 0 additions & 2 deletions graphics/src/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ pub trait Compositor: Sized {
fn screenshot<T: AsRef<str>>(
&mut self,
renderer: &mut Self::Renderer,
surface: &mut Self::Surface,
viewport: &Viewport,
background_color: Color,
overlay: &[T],
Expand Down Expand Up @@ -213,7 +212,6 @@ impl Compositor for () {
fn screenshot<T: AsRef<str>>(
&mut self,
_renderer: &mut Self::Renderer,
_surface: &mut Self::Surface,
_viewport: &Viewport,
_background_color: Color,
_overlay: &[T],
Expand Down
41 changes: 17 additions & 24 deletions renderer/src/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,34 +378,27 @@ where
fn screenshot<T: AsRef<str>>(
&mut self,
renderer: &mut Self::Renderer,
surface: &mut Self::Surface,
viewport: &graphics::Viewport,
background_color: Color,
overlay: &[T],
) -> Vec<u8> {
match (self, renderer, surface) {
(
Self::Primary(compositor),
Renderer::Primary(renderer),
Surface::Primary(surface),
) => compositor.screenshot(
renderer,
surface,
viewport,
background_color,
overlay,
),
(
Self::Secondary(compositor),
Renderer::Secondary(renderer),
Surface::Secondary(surface),
) => compositor.screenshot(
renderer,
surface,
viewport,
background_color,
overlay,
),
match (self, renderer) {
(Self::Primary(compositor), Renderer::Primary(renderer)) => {
compositor.screenshot(
renderer,
viewport,
background_color,
overlay,
)
}
(Self::Secondary(compositor), Renderer::Secondary(renderer)) => {
compositor.screenshot(
renderer,
viewport,
background_color,
overlay,
)
}
_ => unreachable!(),
}
}
Expand Down
9 changes: 5 additions & 4 deletions tiny_skia/src/window/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,11 @@ impl crate::graphics::Compositor for Compositor {
fn screenshot<T: AsRef<str>>(
&mut self,
renderer: &mut Self::Renderer,
surface: &mut Self::Surface,
viewport: &Viewport,
background_color: Color,
overlay: &[T],
) -> Vec<u8> {
screenshot(renderer, surface, viewport, background_color, overlay)
screenshot(renderer, viewport, background_color, overlay)
}
}

Expand Down Expand Up @@ -219,7 +218,6 @@ pub fn present<T: AsRef<str>>(

pub fn screenshot<T: AsRef<str>>(
renderer: &mut Renderer,
surface: &mut Surface,
viewport: &Viewport,
background_color: Color,
overlay: &[T],
Expand All @@ -229,14 +227,17 @@ pub fn screenshot<T: AsRef<str>>(
let mut offscreen_buffer: Vec<u32> =
vec![0; size.width as usize * size.height as usize];

let mut clip_mask = tiny_skia::Mask::new(size.width, size.height)
.expect("Create clip mask");

renderer.draw(
&mut tiny_skia::PixmapMut::from_bytes(
bytemuck::cast_slice_mut(&mut offscreen_buffer),
size.width,
size.height,
)
.expect("Create offscreen pixel map"),
&mut surface.clip_mask,
&mut clip_mask,
viewport,
&[Rectangle::with_size(Size::new(
size.width as f32,
Expand Down
1 change: 0 additions & 1 deletion wgpu/src/window/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,6 @@ impl graphics::Compositor for Compositor {
fn screenshot<T: AsRef<str>>(
&mut self,
renderer: &mut Self::Renderer,
_surface: &mut Self::Surface,
viewport: &Viewport,
background_color: Color,
overlay: &[T],
Expand Down
1 change: 0 additions & 1 deletion winit/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2185,7 +2185,6 @@ where
if let Some(window) = window_manager.get_mut(id) {
let bytes = compositor.screenshot(
&mut window.renderer,
&mut window.surface,
window.state.viewport(),
window.state.background_color(),
&debug.overlay(),
Expand Down

0 comments on commit c34c478

Please sign in to comment.