Skip to content

Commit

Permalink
Changed render_offscreen method to screenshot for clarity;
Browse files Browse the repository at this point in the history
Adjusted documentation to be more clear;
Fixed some formatting
  • Loading branch information
bungoboingo committed Apr 7, 2023
1 parent 418b3b1 commit 3b5df3d
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 59 deletions.
5 changes: 0 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ palette = ["iced_core/palette"]
system = ["iced_winit/system"]
# Enables the advanced module
advanced = []
# Enables chrome tracing support for Iced
chrome-trace = [
"iced_winit/chrome-trace",
"iced_renderer/tracing",
]

[badges]
maintenance = { status = "actively-developed" }
Expand Down
1 change: 0 additions & 1 deletion examples/screenshot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ publish = false
iced = { path = "../..", features = ["debug", "image", "advanced"] }
image = { version = "0.24.6", features = ["png"]}
env_logger = "0.10.0"
wasm-bindgen = "0.2"
6 changes: 3 additions & 3 deletions graphics/src/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ pub trait Compositor: Sized {
overlay: &[T],
) -> Result<(), SurfaceError>;

/// Renders the current [`Renderer`] primitives to an offscreen texture, and returns the bytes of
/// the texture.
/// Screenshots the current [`Renderer`] primitives to an offscreen texture, and returns the bytes of
/// the texture ordered as `RGBA` in the sRGB color space.
///
/// [`Renderer`]: Self::Renderer;
fn render_offscreen<T: AsRef<str>>(
fn screenshot<T: AsRef<str>>(
&mut self,
renderer: &mut Self::Renderer,
viewport: &Viewport,
Expand Down
6 changes: 3 additions & 3 deletions renderer/src/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
})
}

fn render_offscreen<T: AsRef<str>>(
fn screenshot<T: AsRef<str>>(
&mut self,
renderer: &mut Self::Renderer,
viewport: &Viewport,
Expand All @@ -193,7 +193,7 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
renderer.with_primitives(|backend, primitives| match (self, backend) {
#[cfg(feature = "wgpu")]
(Self::Wgpu(compositor), crate::Backend::Wgpu(backend)) => {
iced_wgpu::window::compositor::render_offscreen(
iced_wgpu::window::compositor::screenshot(
compositor,
backend,
primitives,
Expand All @@ -204,7 +204,7 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
},
#[cfg(feature = "tiny-skia")]
(Self::TinySkia(compositor), crate::Backend::TinySkia(backend)) => {
iced_tiny_skia::window::compositor::render_offscreen(
iced_tiny_skia::window::compositor::screenshot(
compositor,
backend,
primitives,
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/screenshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt::{Debug, Formatter};

/// Data of a screenshot, captured with `window::screenshot()`.
///
/// This screenshot will always be in RGBA format.
/// The `bytes` of this screenshot will always be ordered as `RGBA` in the sRGB color space.
#[derive(Clone)]
pub struct Screenshot {
/// The bytes of the [`Screenshot`].
Expand Down
14 changes: 7 additions & 7 deletions tiny_skia/src/window/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
})
}

fn render_offscreen<T: AsRef<str>>(
fn screenshot<T: AsRef<str>>(
&mut self,
renderer: &mut Self::Renderer,
viewport: &Viewport,
background_color: Color,
overlay: &[T],
) -> Vec<u8> {
renderer.with_primitives(|backend, primitives| {
render_offscreen(
screenshot(
self,
backend,
primitives,
Expand Down Expand Up @@ -148,7 +148,7 @@ pub fn present<Theme, T: AsRef<str>>(
Ok(())
}

pub fn render_offscreen<Theme, T: AsRef<str>>(
pub fn screenshot<Theme, T: AsRef<str>>(
compositor: &mut Compositor<Theme>,
backend: &mut Backend,
primitives: &[Primitive],
Expand Down Expand Up @@ -181,10 +181,10 @@ pub fn render_offscreen<Theme, T: AsRef<str>>(
offscreen_buffer.iter().fold(
Vec::with_capacity(offscreen_buffer.len() * 4),
|mut acc, pixel| {
const A_MASK: u32 = 0xFF_000000;
const R_MASK: u32 = 0x00_FF_0000;
const G_MASK: u32 = 0x0000_FF_00;
const B_MASK: u32 = 0x000000_FF;
const A_MASK: u32 = 0xFF_00_00_00;
const R_MASK: u32 = 0x00_FF_00_00;
const G_MASK: u32 = 0x00_00_FF_00;
const B_MASK: u32 = 0x00_00_00_FF;

let a = ((A_MASK & pixel) >> 24) as u8;
let r = ((R_MASK & pixel) >> 16) as u8;
Expand Down
40 changes: 5 additions & 35 deletions wgpu/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,47 +145,17 @@ impl Backend {
#[cfg(feature = "tracing")]
let _ = info_span!("iced_wgpu::offscreen", "DRAW").entered();

let target_size = viewport.physical_size();
let scale_factor = viewport.scale_factor() as f32;
let transformation = viewport.projection();

let mut layers = Layer::generate(primitives, viewport);
layers.push(Layer::overlay(overlay_text, viewport));

self.prepare(
device,
queue,
encoder,
scale_factor,
transformation,
&layers,
);

while !self.prepare_text(
self.present(
device,
queue,
scale_factor,
target_size,
&layers,
) {}

self.render(
device,
encoder,
frame,
clear_color,
scale_factor,
target_size,
&layers,
frame,
primitives,
viewport,
overlay_text,
);

self.quad_pipeline.end_frame();
self.text_pipeline.end_frame();
self.triangle_pipeline.end_frame();

#[cfg(any(feature = "image", feature = "svg"))]
self.image_pipeline.end_frame();

if format != wgpu::TextureFormat::Rgba8UnormSrgb {
log::info!("Texture format is {format:?}; performing conversion to rgba8..");

Expand Down
6 changes: 3 additions & 3 deletions wgpu/src/window/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,15 +267,15 @@ impl<Theme> graphics::Compositor for Compositor<Theme> {
})
}

fn render_offscreen<T: AsRef<str>>(
fn screenshot<T: AsRef<str>>(
&mut self,
renderer: &mut Self::Renderer,
viewport: &Viewport,
background_color: Color,
overlay: &[T],
) -> Vec<u8> {
renderer.with_primitives(|backend, primitives| {
render_offscreen(
screenshot(
self,
backend,
primitives,
Expand All @@ -287,7 +287,7 @@ impl<Theme> graphics::Compositor for Compositor<Theme> {
}
}

pub fn render_offscreen<Theme, T: AsRef<str>>(
pub fn screenshot<Theme, T: AsRef<str>>(
compositor: &Compositor<Theme>,
backend: &mut Backend,
primitives: &[Primitive],
Expand Down
2 changes: 1 addition & 1 deletion winit/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ pub fn run_command<A, C, E>(
.expect("Send message to event loop");
}
window::Action::Screenshot(tag) => {
let bytes = compositor.render_offscreen(
let bytes = compositor.screenshot(
renderer,
state.viewport(),
state.background_color(),
Expand Down

0 comments on commit 3b5df3d

Please sign in to comment.