Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions crates/canvas-2d/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ pub struct Context {
pub(crate) state_stack: Vec<State>,
pub(crate) font_color: Color,
pub(crate) recording: Option<recording::Recording>,
/// GPU readback of the surface, with the generation it was read at
pub(crate) snapshot: Option<(u32, Image)>,
}

impl Drop for Context {
Expand Down Expand Up @@ -520,6 +522,10 @@ impl Context {
}

pub fn get_image(&mut self) -> Option<Image> {
if let Some(image) = self.cached_snapshot() {
return Some(image);
}

#[cfg(feature = "gl")]
{
if let Some(ref context) = self.gl_context {
Expand All @@ -529,16 +535,28 @@ impl Context {

self.flush();

let snapshot = self.surface.image_snapshot();
if self.surface_data.engine == SurfaceEngine::CPU {
return Some(self.surface.image_snapshot());
}

// A GPU snapshot is only readable through its context: copy it to the CPU.
let ret = if self.surface_data.engine != SurfaceEngine::CPU {
snapshot.make_raster_image(self.direct_context.as_mut(), CachingHint::Allow)
} else {
Some(snapshot)
};
// read back once per change
let generation = self.surface.generation_id();
let snapshot = self.surface.image_snapshot();
let image = snapshot.make_raster_image(self.direct_context.as_mut(), CachingHint::Allow)?;
self.snapshot = Some((generation, image.clone()));
Some(image)
}

ret
/// The last readback, if nothing was drawn since.
fn cached_snapshot(&mut self) -> Option<Image> {
if self.recording.is_some() {
return None;
}
let generation = self.surface.generation_id();
match &self.snapshot {
Some((cached, image)) if *cached == generation => Some(image.clone()),
_ => None,
}
}

pub fn draw_pixels(&mut self, image: &Image) {
Expand Down
31 changes: 30 additions & 1 deletion crates/canvas-2d/src/context/recording.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,24 @@ use skia_safe::{
Rect, M44,
};

use std::sync::atomic::{AtomicU64, Ordering};

use crate::context::{Context, State};

/// Recordings open with a save of their own: a bottom-level clip can't be undone, and `reset()` must.
pub(crate) const RECORDING_BASE: usize = 2;

static REVISIONS: AtomicU64 = AtomicU64::new(1);

pub(crate) struct Recording {
recorder: PictureRecorder,
bounds: Rect,
ops: Vec<FrameOp>,
/// Began with a full-canvas clear, so no earlier frame shows through.
cleared: bool,
/// Changes whenever something may have been drawn
revision: u64,
touched: bool,
}

impl Recording {
Expand All @@ -28,17 +35,32 @@ impl Recording {
bounds,
ops: Vec::new(),
cleared: false,
revision: REVISIONS.fetch_add(1, Ordering::Relaxed),
touched: false,
}
}

#[inline]
pub(crate) fn canvas(&mut self) -> &Canvas {
self.touched = true;
self.recording_canvas()
}

#[inline]
fn recording_canvas(&mut self) -> &Canvas {
// `restart` begins the next recording before returning.
self.recorder
.recording_canvas()
.expect("recording canvas is always live")
}

fn revision(&mut self) -> u64 {
if std::mem::take(&mut self.touched) {
self.revision = REVISIONS.fetch_add(1, Ordering::Relaxed);
}
self.revision
}

fn cut(&mut self, stack: &[State], current: &State) {
if let Some(picture) = self.restart(stack, current) {
self.ops.push(FrameOp::Picture(picture));
Expand All @@ -52,7 +74,7 @@ impl Recording {
}

fn restart(&mut self, stack: &[State], current: &State) -> Option<Picture> {
let matrix = self.canvas().local_to_device();
let matrix = self.recording_canvas().local_to_device();
let picture = self.recorder.finish_recording_as_picture(Some(&self.bounds));
self.recorder.begin_recording(self.bounds, false).save();
let canvas = self.recorder.recording_canvas().expect("just begun");
Expand Down Expand Up @@ -210,6 +232,11 @@ impl Context {
self.recording = Some(Recording::new(self.surface_data.bounds));
}

/// Unchanged while nothing is drawn.
pub fn revision(&mut self) -> Option<u64> {
self.recording.as_mut().map(Recording::revision)
}

/// Frames must be replayed in order.
pub fn take_frame(&mut self) -> Frame {
let Some(recording) = self.recording.as_mut() else {
Expand Down Expand Up @@ -249,6 +276,7 @@ impl Context {
return false;
};
recording.cut(&self.state_stack, &self.state);
recording.touched = true;
let len = (row_bytes * info.height().max(0) as usize).min(bytes.len());
recording.ops.push(FrameOp::Pixels(PixelWrite {
width: info.width(),
Expand Down Expand Up @@ -301,6 +329,7 @@ impl Context {
};
let matrix = recording.canvas().local_to_device();
recording.cut(&self.state_stack, &self.state);
recording.touched = true;
recording.ops.push(FrameOp::External(ExternalDraw {
state: StateSnapshot::take(&self.state_stack, &self.state, matrix),
src,
Expand Down
1 change: 1 addition & 0 deletions crates/canvas-2d/src/context/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ impl Context {
state_stack: vec![],
font_color: Color::new(font_color as u32),
recording: None,
snapshot: None,
#[cfg(feature = "gl")]
window_surface: None,
#[cfg(any(feature = "gl", feature = "vulkan", feature = "metal"))]
Expand Down
1 change: 1 addition & 0 deletions crates/canvas-2d/src/context/surface_d3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ impl Context {
state_stack: vec![],
font_color: Color::new(font_color as u32),
recording: None,
snapshot: None,
#[cfg(feature = "gl")]
window_surface: None,
#[cfg(any(feature = "gl", feature = "vulkan", feature = "metal"))]
Expand Down
1 change: 1 addition & 0 deletions crates/canvas-2d/src/context/surface_gl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ impl Context {
state_stack: vec![],
font_color: Color::new(font_color as u32),
recording: None,
snapshot: None,
surface_state: SurfaceState::None,
})
}
Expand Down
3 changes: 3 additions & 0 deletions crates/canvas-2d/src/context/surface_metal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ impl Context {
state_stack: vec![],
font_color: Color::new(font_color as u32),
recording: None,
snapshot: None,
#[cfg(feature = "gl")]
window_surface: None,
#[cfg(any(feature = "gl", feature = "vulkan", feature = "metal"))]
Expand Down Expand Up @@ -269,6 +270,7 @@ impl Context {
state_stack: vec![],
font_color: Color::new(font_color as u32),
recording: None,
snapshot: None,
#[cfg(feature = "gl")]
window_surface: None,
#[cfg(any(feature = "gl", feature = "vulkan", feature = "metal"))]
Expand Down Expand Up @@ -353,6 +355,7 @@ impl Context {
state_stack: vec![],
font_color: Color::new(font_color as u32),
recording: None,
snapshot: None,
#[cfg(feature = "gl")]
window_surface: None,
#[cfg(any(feature = "gl", feature = "vulkan", feature = "metal"))]
Expand Down
1 change: 1 addition & 0 deletions crates/canvas-2d/src/context/surface_vulkan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ impl Context {
state_stack: vec![],
font_color: skia_safe::Color::new(font_color as u32),
recording: None,
snapshot: None,
#[cfg(feature = "gl")]
window_surface: None,
#[cfg(any(feature = "gl", feature = "vulkan", feature = "metal"))]
Expand Down
22 changes: 21 additions & 1 deletion crates/canvas-c/src/c2d/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ pub struct CanvasRenderingContext2D {
refs: AtomicUsize,
/// `context` only records; the real one lives on the render thread.
threaded: Option<crate::c2d::render_thread::RenderTarget>,
/// Threaded: last image and the revision it shows
snapshot: Option<(u64, canvas_2d::context::Image)>,
}

impl CanvasRenderingContext2D {
Expand Down Expand Up @@ -245,6 +247,7 @@ impl CanvasRenderingContext2D {
engine: Engine::CPU,
refs: AtomicUsize::new(1),
threaded: None,
snapshot: None,
}
}

Expand All @@ -256,6 +259,7 @@ impl CanvasRenderingContext2D {
engine: Engine::GL,
refs: AtomicUsize::new(1),
threaded: None,
snapshot: None,
}
}

Expand All @@ -267,6 +271,7 @@ impl CanvasRenderingContext2D {
engine: Engine::Vulkan,
refs: AtomicUsize::new(1),
threaded: None,
snapshot: None,
}
}

Expand All @@ -278,6 +283,7 @@ impl CanvasRenderingContext2D {
engine: Engine::Metal,
refs: AtomicUsize::new(1),
threaded: None,
snapshot: None,
}
}

Expand All @@ -289,6 +295,7 @@ impl CanvasRenderingContext2D {
engine: Engine::D3D,
refs: AtomicUsize::new(1),
threaded: None,
snapshot: None,
}
}

Expand Down Expand Up @@ -323,6 +330,7 @@ impl CanvasRenderingContext2D {
engine: Engine::CPU,
refs: AtomicUsize::new(1),
threaded: Some(target),
snapshot: None,
}
}

Expand Down Expand Up @@ -373,7 +381,16 @@ impl CanvasRenderingContext2D {
}

pub fn image(&mut self) -> Option<canvas_2d::context::Image> {
self.with_pixels_source(|context| context.get_image()).flatten()
// skip the render thread if nothing was drawn
let revision = self.context.revision().filter(|_| !self.gpu_lost());
if let (Some(revision), Some((cached, image))) = (revision, &self.snapshot) {
if *cached == revision {
return Some(image.clone());
}
}
let image = self.with_pixels_source(|context| context.get_image()).flatten();
self.snapshot = revision.zip(image.clone());
image
}

pub fn image_data(
Expand Down Expand Up @@ -843,6 +860,7 @@ pub extern "C" fn canvas_native_context_create(
engine: Engine::CPU,
refs: AtomicUsize::new(1),
threaded: None,
snapshot: None,
}))
}

Expand Down Expand Up @@ -878,6 +896,7 @@ pub extern "C" fn canvas_native_context_create_gl(
engine: Engine::GL,
refs: AtomicUsize::new(1),
threaded: None,
snapshot: None,
}))
}

Expand Down Expand Up @@ -924,6 +943,7 @@ pub extern "C" fn canvas_native_context_create_gl_no_window(
engine: Engine::GL,
refs: AtomicUsize::new(1),
threaded: None,
snapshot: None,
}))
}

Expand Down
Loading