summaryrefslogtreecommitdiffstats
path: root/tiny_skia/src
diff options
context:
space:
mode:
Diffstat (limited to 'tiny_skia/src')
-rw-r--r--tiny_skia/src/backend.rs190
-rw-r--r--tiny_skia/src/geometry.rs47
-rw-r--r--tiny_skia/src/lib.rs2
-rw-r--r--tiny_skia/src/primitive.rs48
-rw-r--r--tiny_skia/src/text.rs152
-rw-r--r--tiny_skia/src/window/compositor.rs80
6 files changed, 383 insertions, 136 deletions
diff --git a/tiny_skia/src/backend.rs b/tiny_skia/src/backend.rs
index 9d0fc527..e0134220 100644
--- a/tiny_skia/src/backend.rs
+++ b/tiny_skia/src/backend.rs
@@ -2,7 +2,8 @@ use crate::core::text;
use crate::core::Gradient;
use crate::core::{Background, Color, Font, Point, Rectangle, Size, Vector};
use crate::graphics::backend;
-use crate::graphics::{Primitive, Viewport};
+use crate::graphics::{Damage, Viewport};
+use crate::primitive::{self, Primitive};
use crate::Settings;
use std::borrow::Cow;
@@ -174,7 +175,18 @@ impl Backend {
)
.post_scale(scale_factor, scale_factor);
- let path = rounded_rectangle(*bounds, *border_radius);
+ // Make sure the border radius is not larger than the bounds
+ let border_width = border_width
+ .min(bounds.width / 2.0)
+ .min(bounds.height / 2.0);
+
+ let mut fill_border_radius = *border_radius;
+ for radius in &mut fill_border_radius {
+ *radius = (*radius)
+ .min(bounds.width / 2.0)
+ .min(bounds.height / 2.0);
+ }
+ let path = rounded_rectangle(*bounds, fill_border_radius);
pixels.fill_path(
&path,
@@ -236,23 +248,120 @@ impl Backend {
clip_mask,
);
- if *border_width > 0.0 {
- pixels.stroke_path(
- &path,
- &tiny_skia::Paint {
- shader: tiny_skia::Shader::SolidColor(into_color(
- *border_color,
- )),
- anti_alias: true,
- ..tiny_skia::Paint::default()
- },
- &tiny_skia::Stroke {
- width: *border_width,
- ..tiny_skia::Stroke::default()
- },
- transform,
- clip_mask,
- );
+ if border_width > 0.0 {
+ // Border path is offset by half the border width
+ let border_bounds = Rectangle {
+ x: bounds.x + border_width / 2.0,
+ y: bounds.y + border_width / 2.0,
+ width: bounds.width - border_width,
+ height: bounds.height - border_width,
+ };
+
+ // Make sure the border radius is correct
+ let mut border_radius = *border_radius;
+ let mut is_simple_border = true;
+
+ for radius in &mut border_radius {
+ *radius = if *radius == 0.0 {
+ // Path should handle this fine
+ 0.0
+ } else if *radius > border_width / 2.0 {
+ *radius - border_width / 2.0
+ } else {
+ is_simple_border = false;
+ 0.0
+ }
+ .min(border_bounds.width / 2.0)
+ .min(border_bounds.height / 2.0);
+ }
+
+ // Stroking a path works well in this case
+ if is_simple_border {
+ let border_path =
+ rounded_rectangle(border_bounds, border_radius);
+
+ pixels.stroke_path(
+ &border_path,
+ &tiny_skia::Paint {
+ shader: tiny_skia::Shader::SolidColor(
+ into_color(*border_color),
+ ),
+ anti_alias: true,
+ ..tiny_skia::Paint::default()
+ },
+ &tiny_skia::Stroke {
+ width: border_width,
+ ..tiny_skia::Stroke::default()
+ },
+ transform,
+ clip_mask,
+ );
+ } else {
+ // Draw corners that have too small border radii as having no border radius,
+ // but mask them with the rounded rectangle with the correct border radius.
+ let mut temp_pixmap = tiny_skia::Pixmap::new(
+ bounds.width as u32,
+ bounds.height as u32,
+ )
+ .unwrap();
+
+ let mut quad_mask = tiny_skia::Mask::new(
+ bounds.width as u32,
+ bounds.height as u32,
+ )
+ .unwrap();
+
+ let zero_bounds = Rectangle {
+ x: 0.0,
+ y: 0.0,
+ width: bounds.width,
+ height: bounds.height,
+ };
+ let path =
+ rounded_rectangle(zero_bounds, fill_border_radius);
+
+ quad_mask.fill_path(
+ &path,
+ tiny_skia::FillRule::EvenOdd,
+ true,
+ transform,
+ );
+ let path_bounds = Rectangle {
+ x: border_width / 2.0,
+ y: border_width / 2.0,
+ width: bounds.width - border_width,
+ height: bounds.height - border_width,
+ };
+
+ let border_radius_path =
+ rounded_rectangle(path_bounds, border_radius);
+
+ temp_pixmap.stroke_path(
+ &border_radius_path,
+ &tiny_skia::Paint {
+ shader: tiny_skia::Shader::SolidColor(
+ into_color(*border_color),
+ ),
+ anti_alias: true,
+ ..tiny_skia::Paint::default()
+ },
+ &tiny_skia::Stroke {
+ width: border_width,
+ ..tiny_skia::Stroke::default()
+ },
+ transform,
+ Some(&quad_mask),
+ );
+
+ pixels.draw_pixmap(
+ bounds.x as i32,
+ bounds.y as i32,
+ temp_pixmap.as_ref(),
+ &tiny_skia::PixmapPaint::default(),
+ transform,
+ clip_mask,
+ );
+ }
}
}
Primitive::Text {
@@ -311,6 +420,13 @@ impl Backend {
self.raster_pipeline
.draw(handle, *bounds, pixels, transform, clip_mask);
}
+ #[cfg(not(feature = "image"))]
+ Primitive::Image { .. } => {
+ log::warn!(
+ "Unsupported primitive in `iced_tiny_skia`: {:?}",
+ primitive
+ );
+ }
#[cfg(feature = "svg")]
Primitive::Svg {
handle,
@@ -334,12 +450,19 @@ impl Backend {
clip_mask,
);
}
- Primitive::Fill {
+ #[cfg(not(feature = "svg"))]
+ Primitive::Svg { .. } => {
+ log::warn!(
+ "Unsupported primitive in `iced_tiny_skia`: {:?}",
+ primitive
+ );
+ }
+ Primitive::Custom(primitive::Custom::Fill {
path,
paint,
rule,
transform,
- } => {
+ }) => {
let bounds = path.bounds();
let physical_bounds = (Rectangle {
@@ -367,12 +490,12 @@ impl Backend {
clip_mask,
);
}
- Primitive::Stroke {
+ Primitive::Custom(primitive::Custom::Stroke {
path,
paint,
stroke,
transform,
- } => {
+ }) => {
let bounds = path.bounds();
let physical_bounds = (Rectangle {
@@ -472,21 +595,6 @@ impl Backend {
translation,
);
}
- Primitive::SolidMesh { .. } | Primitive::GradientMesh { .. } => {
- // Not supported!
- // TODO: Draw a placeholder (?)
- log::warn!(
- "Unsupported primitive in `iced_tiny_skia`: {:?}",
- primitive
- );
- }
- _ => {
- // Not supported!
- log::warn!(
- "Unsupported primitive in `iced_tiny_skia`: {:?}",
- primitive
- );
- }
}
}
}
@@ -659,9 +767,7 @@ fn adjust_clip_mask(clip_mask: &mut tiny_skia::Mask, bounds: Rectangle) {
}
impl iced_graphics::Backend for Backend {
- fn trim_measurements(&mut self) {
- self.text_pipeline.trim_measurement_cache();
- }
+ type Primitive = primitive::Custom;
}
impl backend::Text for Backend {
@@ -685,7 +791,7 @@ impl backend::Text for Backend {
font: Font,
bounds: Size,
shaping: text::Shaping,
- ) -> (f32, f32) {
+ ) -> Size {
self.text_pipeline.measure(
contents,
size,
diff --git a/tiny_skia/src/geometry.rs b/tiny_skia/src/geometry.rs
index ee347c73..9bd47556 100644
--- a/tiny_skia/src/geometry.rs
+++ b/tiny_skia/src/geometry.rs
@@ -3,7 +3,7 @@ use crate::graphics::geometry::fill::{self, Fill};
use crate::graphics::geometry::stroke::{self, Stroke};
use crate::graphics::geometry::{Path, Style, Text};
use crate::graphics::Gradient;
-use crate::graphics::Primitive;
+use crate::primitive::{self, Primitive};
pub struct Frame {
size: Size,
@@ -42,12 +42,13 @@ impl Frame {
let Some(path) = convert_path(path) else { return };
let fill = fill.into();
- self.primitives.push(Primitive::Fill {
- path,
- paint: into_paint(fill.style),
- rule: into_fill_rule(fill.rule),
- transform: self.transform,
- });
+ self.primitives
+ .push(Primitive::Custom(primitive::Custom::Fill {
+ path,
+ paint: into_paint(fill.style),
+ rule: into_fill_rule(fill.rule),
+ transform: self.transform,
+ }));
}
pub fn fill_rectangle(
@@ -59,15 +60,16 @@ impl Frame {
let Some(path) = convert_path(&Path::rectangle(top_left, size)) else { return };
let fill = fill.into();
- self.primitives.push(Primitive::Fill {
- path,
- paint: tiny_skia::Paint {
- anti_alias: false,
- ..into_paint(fill.style)
- },
- rule: into_fill_rule(fill.rule),
- transform: self.transform,
- });
+ self.primitives
+ .push(Primitive::Custom(primitive::Custom::Fill {
+ path,
+ paint: tiny_skia::Paint {
+ anti_alias: false,
+ ..into_paint(fill.style)
+ },
+ rule: into_fill_rule(fill.rule),
+ transform: self.transform,
+ }));
}
pub fn stroke<'a>(&mut self, path: &Path, stroke: impl Into<Stroke<'a>>) {
@@ -76,12 +78,13 @@ impl Frame {
let stroke = stroke.into();
let skia_stroke = into_stroke(&stroke);
- self.primitives.push(Primitive::Stroke {
- path,
- paint: into_paint(stroke.style),
- stroke: skia_stroke,
- transform: self.transform,
- });
+ self.primitives
+ .push(Primitive::Custom(primitive::Custom::Stroke {
+ path,
+ paint: into_paint(stroke.style),
+ stroke: skia_stroke,
+ transform: self.transform,
+ }));
}
pub fn fill_text(&mut self, text: impl Into<Text>) {
diff --git a/tiny_skia/src/lib.rs b/tiny_skia/src/lib.rs
index 83baef1c..15de6ce2 100644
--- a/tiny_skia/src/lib.rs
+++ b/tiny_skia/src/lib.rs
@@ -1,6 +1,7 @@
pub mod window;
mod backend;
+mod primitive;
mod settings;
mod text;
@@ -17,6 +18,7 @@ pub use iced_graphics as graphics;
pub use iced_graphics::core;
pub use backend::Backend;
+pub use primitive::Primitive;
pub use settings::Settings;
/// A [`tiny-skia`] graphics renderer for [`iced`].
diff --git a/tiny_skia/src/primitive.rs b/tiny_skia/src/primitive.rs
new file mode 100644
index 00000000..0ed24969
--- /dev/null
+++ b/tiny_skia/src/primitive.rs
@@ -0,0 +1,48 @@
+use crate::core::Rectangle;
+use crate::graphics::Damage;
+
+pub type Primitive = crate::graphics::Primitive<Custom>;
+
+#[derive(Debug, Clone, PartialEq)]
+pub enum Custom {
+ /// A path filled with some paint.
+ Fill {
+ /// The path to fill.
+ path: tiny_skia::Path,
+ /// The paint to use.
+ paint: tiny_skia::Paint<'static>,
+ /// The fill rule to follow.
+ rule: tiny_skia::FillRule,
+ /// The transform to apply to the path.
+ transform: tiny_skia::Transform,
+ },
+ /// A path stroked with some paint.
+ Stroke {
+ /// The path to stroke.
+ path: tiny_skia::Path,
+ /// The paint to use.
+ paint: tiny_skia::Paint<'static>,
+ /// The stroke settings.
+ stroke: tiny_skia::Stroke,
+ /// The transform to apply to the path.
+ transform: tiny_skia::Transform,
+ },
+}
+
+impl Damage for Custom {
+ fn bounds(&self) -> Rectangle {
+ match self {
+ Self::Fill { path, .. } | Self::Stroke { path, .. } => {
+ let bounds = path.bounds();
+
+ Rectangle {
+ x: bounds.x(),
+ y: bounds.y(),
+ width: bounds.width(),
+ height: bounds.height(),
+ }
+ .expand(1.0)
+ }
+ }
+ }
+}
diff --git a/tiny_skia/src/text.rs b/tiny_skia/src/text.rs
index a34c7317..8f494650 100644
--- a/tiny_skia/src/text.rs
+++ b/tiny_skia/src/text.rs
@@ -14,8 +14,7 @@ use std::sync::Arc;
pub struct Pipeline {
font_system: RefCell<cosmic_text::FontSystem>,
glyph_cache: GlyphCache,
- measurement_cache: RefCell<Cache>,
- render_cache: Cache,
+ cache: RefCell<Cache>,
}
impl Pipeline {
@@ -23,14 +22,12 @@ impl Pipeline {
Pipeline {
font_system: RefCell::new(cosmic_text::FontSystem::new_with_fonts(
[cosmic_text::fontdb::Source::Binary(Arc::new(
- include_bytes!("../../wgpu/fonts/Iced-Icons.ttf")
- .as_slice(),
+ include_bytes!("../fonts/Iced-Icons.ttf").as_slice(),
))]
.into_iter(),
)),
glyph_cache: GlyphCache::new(),
- measurement_cache: RefCell::new(Cache::new()),
- render_cache: Cache::new(),
+ cache: RefCell::new(Cache::new()),
}
}
@@ -38,6 +35,8 @@ impl Pipeline {
self.font_system.get_mut().db_mut().load_font_source(
cosmic_text::fontdb::Source::Binary(Arc::new(bytes.into_owned())),
);
+
+ self.cache = RefCell::new(Cache::new());
}
pub fn draw(
@@ -55,20 +54,11 @@ impl Pipeline {
pixels: &mut tiny_skia::PixmapMut<'_>,
clip_mask: Option<&tiny_skia::Mask>,
) {
- let line_height =
- f32::from(line_height.to_absolute(Pixels(size))) * scale_factor;
-
- let bounds = bounds * scale_factor;
- let size = size * scale_factor;
+ let line_height = f32::from(line_height.to_absolute(Pixels(size)));
let font_system = self.font_system.get_mut();
let key = Key {
- bounds: {
- let size = bounds.size();
-
- // TODO: Reuse buffers from layouting
- Size::new(size.width.ceil(), size.height.ceil())
- },
+ bounds: bounds.size(),
content,
font,
size,
@@ -76,16 +66,12 @@ impl Pipeline {
shaping,
};
- let (_, buffer) = self.render_cache.allocate(font_system, key);
+ let (_, entry) = self.cache.get_mut().allocate(font_system, key);
- let (total_lines, max_width) = buffer
- .layout_runs()
- .enumerate()
- .fold((0, 0.0), |(_, max), (i, buffer)| {
- (i + 1, buffer.line_w.max(max))
- });
+ let max_width = entry.bounds.width * scale_factor;
+ let total_height = entry.bounds.height * scale_factor;
- let total_height = total_lines as f32 * line_height;
+ let bounds = bounds * scale_factor;
let x = match horizontal_alignment {
alignment::Horizontal::Left => bounds.x,
@@ -99,16 +85,14 @@ impl Pipeline {
alignment::Vertical::Bottom => bounds.y - total_height,
};
- // TODO: Subpixel glyph positioning
- let x = x.round() as i32;
- let y = y.round() as i32;
-
let mut swash = cosmic_text::SwashCache::new();
- for run in buffer.layout_runs() {
+ for run in entry.buffer.layout_runs() {
for glyph in run.glyphs {
+ let physical_glyph = glyph.physical((x, y), scale_factor);
+
if let Some((buffer, placement)) = self.glyph_cache.allocate(
- glyph.cache_key,
+ physical_glyph.cache_key,
color,
font_system,
&mut swash,
@@ -121,8 +105,9 @@ impl Pipeline {
.expect("Create glyph pixel map");
pixels.draw_pixmap(
- x + glyph.x_int + placement.left,
- y - glyph.y_int - placement.top + run.line_y as i32,
+ physical_glyph.x + placement.left,
+ physical_glyph.y - placement.top
+ + (run.line_y * scale_factor).round() as i32,
pixmap,
&tiny_skia::PixmapPaint::default(),
tiny_skia::Transform::identity(),
@@ -134,7 +119,7 @@ impl Pipeline {
}
pub fn trim_cache(&mut self) {
- self.render_cache.trim();
+ self.cache.get_mut().trim();
self.glyph_cache.trim();
}
@@ -146,12 +131,12 @@ impl Pipeline {
font: Font,
bounds: Size,
shaping: Shaping,
- ) -> (f32, f32) {
- let mut measurement_cache = self.measurement_cache.borrow_mut();
+ ) -> Size {
+ let mut measurement_cache = self.cache.borrow_mut();
let line_height = f32::from(line_height.to_absolute(Pixels(size)));
- let (_, paragraph) = measurement_cache.allocate(
+ let (_, entry) = measurement_cache.allocate(
&mut self.font_system.borrow_mut(),
Key {
content,
@@ -163,14 +148,7 @@ impl Pipeline {
},
);
- let (total_lines, max_width) = paragraph
- .layout_runs()
- .enumerate()
- .fold((0, 0.0), |(_, max), (i, buffer)| {
- (i + 1, buffer.line_w.max(max))
- });
-
- (max_width, line_height * total_lines as f32)
+ entry.bounds
}
pub fn hit_test(
@@ -184,11 +162,11 @@ impl Pipeline {
point: Point,
_nearest_only: bool,
) -> Option<Hit> {
- let mut measurement_cache = self.measurement_cache.borrow_mut();
+ let mut measurement_cache = self.cache.borrow_mut();
let line_height = f32::from(line_height.to_absolute(Pixels(size)));
- let (_, paragraph) = measurement_cache.allocate(
+ let (_, entry) = measurement_cache.allocate(
&mut self.font_system.borrow_mut(),
Key {
content,
@@ -200,14 +178,20 @@ impl Pipeline {
},
);
- let cursor = paragraph.hit(point.x, point.y)?;
+ let cursor = entry.buffer.hit(point.x, point.y)?;
Some(Hit::CharOffset(cursor.index))
}
+}
- pub fn trim_measurement_cache(&mut self) {
- self.measurement_cache.borrow_mut().trim();
- }
+fn measure(buffer: &cosmic_text::Buffer) -> Size {
+ let (width, total_lines) = buffer
+ .layout_runs()
+ .fold((0.0, 0usize), |(width, total_lines), run| {
+ (run.line_w.max(width), total_lines + 1)
+ });
+
+ Size::new(width, total_lines as f32 * buffer.metrics().line_height)
}
fn to_family(family: font::Family) -> cosmic_text::Family<'static> {
@@ -366,12 +350,18 @@ impl GlyphCache {
}
struct Cache {
- entries: FxHashMap<KeyHash, cosmic_text::Buffer>,
+ entries: FxHashMap<KeyHash, Entry>,
+ measurements: FxHashMap<KeyHash, KeyHash>,
recently_used: FxHashSet<KeyHash>,
hasher: HashBuilder,
trim_count: usize,
}
+struct Entry {
+ buffer: cosmic_text::Buffer,
+ bounds: Size,
+}
+
#[cfg(not(target_arch = "wasm32"))]
type HashBuilder = twox_hash::RandomXxHashBuilder64;
@@ -384,6 +374,7 @@ impl Cache {
fn new() -> Self {
Self {
entries: FxHashMap::default(),
+ measurements: FxHashMap::default(),
recently_used: FxHashSet::default(),
hasher: HashBuilder::default(),
trim_count: 0,
@@ -394,20 +385,14 @@ impl Cache {
&mut self,
font_system: &mut cosmic_text::FontSystem,
key: Key<'_>,
- ) -> (KeyHash, &mut cosmic_text::Buffer) {
- let hash = {
- let mut hasher = self.hasher.build_hasher();
-
- key.content.hash(&mut hasher);
- key.size.to_bits().hash(&mut hasher);
- key.line_height.to_bits().hash(&mut hasher);
- key.font.hash(&mut hasher);
- key.bounds.width.to_bits().hash(&mut hasher);
- key.bounds.height.to_bits().hash(&mut hasher);
- key.shaping.hash(&mut hasher);
-
- hasher.finish()
- };
+ ) -> (KeyHash, &mut Entry) {
+ let hash = key.hash(self.hasher.build_hasher());
+
+ if let Some(hash) = self.measurements.get(&hash) {
+ let _ = self.recently_used.insert(*hash);
+
+ return (*hash, self.entries.get_mut(hash).unwrap());
+ }
if let hash_map::Entry::Vacant(entry) = self.entries.entry(hash) {
let metrics = cosmic_text::Metrics::new(key.size, key.size * 1.2);
@@ -428,7 +413,24 @@ impl Cache {
to_shaping(key.shaping),
);
- let _ = entry.insert(buffer);
+ let bounds = measure(&buffer);
+
+ let _ = entry.insert(Entry { buffer, bounds });
+
+ for bounds in [
+ bounds,
+ Size {
+ width: key.bounds.width,
+ ..bounds
+ },
+ ] {
+ if key.bounds != bounds {
+ let _ = self.measurements.insert(
+ Key { bounds, ..key }.hash(self.hasher.build_hasher()),
+ hash,
+ );
+ }
+ }
}
let _ = self.recently_used.insert(hash);
@@ -440,6 +442,8 @@ impl Cache {
if self.trim_count > Self::TRIM_INTERVAL {
self.entries
.retain(|key, _| self.recently_used.contains(key));
+ self.measurements
+ .retain(|_, value| self.recently_used.contains(value));
self.recently_used.clear();
@@ -460,4 +464,18 @@ struct Key<'a> {
shaping: Shaping,
}
+impl Key<'_> {
+ fn hash<H: Hasher>(self, mut hasher: H) -> KeyHash {
+ self.content.hash(&mut hasher);
+ self.size.to_bits().hash(&mut hasher);
+ self.line_height.to_bits().hash(&mut hasher);
+ self.font.hash(&mut hasher);
+ self.bounds.width.to_bits().hash(&mut hasher);
+ self.bounds.height.to_bits().hash(&mut hasher);
+ self.shaping.hash(&mut hasher);
+
+ hasher.finish()
+ }
+}
+
type KeyHash = u64;
diff --git a/tiny_skia/src/window/compositor.rs b/tiny_skia/src/window/compositor.rs
index 9999a188..775cf9e5 100644
--- a/tiny_skia/src/window/compositor.rs
+++ b/tiny_skia/src/window/compositor.rs
@@ -1,8 +1,8 @@
-use crate::core::{Color, Rectangle};
-use crate::graphics::compositor::{self, Information, SurfaceError};
+use crate::core::{Color, Rectangle, Size};
+use crate::graphics::compositor::{self, Information};
use crate::graphics::damage;
-use crate::graphics::{Error, Primitive, Viewport};
-use crate::{Backend, Renderer, Settings};
+use crate::graphics::{Error, Viewport};
+use crate::{Backend, Primitive, Renderer, Settings};
use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle};
use std::marker::PhantomData;
@@ -79,7 +79,7 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
viewport: &Viewport,
background_color: Color,
overlay: &[T],
- ) -> Result<(), SurfaceError> {
+ ) -> Result<(), compositor::SurfaceError> {
renderer.with_primitives(|backend, primitives| {
present(
backend,
@@ -91,6 +91,26 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
)
})
}
+
+ fn screenshot<T: AsRef<str>>(
+ &mut self,
+ renderer: &mut Self::Renderer,
+ surface: &mut Self::Surface,
+ viewport: &Viewport,
+ background_color: Color,
+ overlay: &[T],
+ ) -> Vec<u8> {
+ renderer.with_primitives(|backend, primitives| {
+ screenshot(
+ surface,
+ backend,
+ primitives,
+ viewport,
+ background_color,
+ overlay,
+ )
+ })
+ }
}
pub fn new<Theme>(settings: Settings) -> (Compositor<Theme>, Backend) {
@@ -156,3 +176,53 @@ pub fn present<T: AsRef<str>>(
Ok(())
}
+
+pub fn screenshot<T: AsRef<str>>(
+ surface: &mut Surface,
+ backend: &mut Backend,
+ primitives: &[Primitive],
+ viewport: &Viewport,
+ background_color: Color,
+ overlay: &[T],
+) -> Vec<u8> {
+ let size = viewport.physical_size();
+
+ let mut offscreen_buffer: Vec<u32> =
+ vec![0; size.width as usize * size.height as usize];
+
+ backend.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,
+ primitives,
+ viewport,
+ &[Rectangle::with_size(Size::new(
+ size.width as f32,
+ size.height as f32,
+ ))],
+ background_color,
+ overlay,
+ );
+
+ offscreen_buffer.iter().fold(
+ Vec::with_capacity(offscreen_buffer.len() * 4),
+ |mut acc, pixel| {
+ 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;
+ let g = ((G_MASK & pixel) >> 8) as u8;
+ let b = (B_MASK & pixel) as u8;
+
+ acc.extend([r, g, b, a]);
+ acc
+ },
+ )
+}