mirror of
https://github.com/storytold/figcraft.git
synced 2026-11-17 07:35:10 +00:00
True wide-gamut display is blocked upstream (egui-wgpu 0.36 always picks an 8-bit sRGB surface and draws sRGB-encoded UI), so the colour picker warns when a Display P3 colour is outside sRGB and View > Soft-proof sRGB stripes the canvas pixels sRGB can't show. Exports keep wide-gamut colours as before.
182 lines
6.3 KiB
Rust
182 lines
6.3 KiB
Rust
//! Canvas rendering off the UI thread (desktop) or inline (web). The latest request wins (queued
|
|
//! older ones are skipped); the UI shows each finished image, re-projected to the current view,
|
|
//! until the next one arrives.
|
|
//! The renderer keeps its last image ([`figcraft_render::Retained`]): a pan renders only the
|
|
//! uncovered strips and an edit only the areas of the layers that changed.
|
|
|
|
use std::sync::Arc;
|
|
|
|
use figcraft_color::ColorProfile;
|
|
use figcraft_doc::{Document, NodeId};
|
|
use figcraft_render::{Pixmap, Retained, View, Work};
|
|
|
|
#[derive(Clone)]
|
|
pub struct Job {
|
|
pub doc: Arc<Document>,
|
|
pub page: NodeId,
|
|
pub view: View,
|
|
pub outline: bool,
|
|
/// Layers drawn as outlines (outlines for the selection).
|
|
pub outlined: Vec<NodeId>,
|
|
pub skip: Vec<NodeId>,
|
|
/// Only this layer (the focus view).
|
|
pub solo: Option<NodeId>,
|
|
/// Soft-proof sRGB: stripe the colours sRGB can't show.
|
|
pub proof: bool,
|
|
}
|
|
|
|
pub struct Done {
|
|
/// The image, ready for a texture (converted off the UI thread).
|
|
pub image: egui::ColorImage,
|
|
/// The view the image shows (the requested one snapped outwards to whole device pixels).
|
|
pub view: View,
|
|
pub ms: f64,
|
|
pub work: Work,
|
|
}
|
|
|
|
/// A render of a document in `profile` as a texture image for the screen (sRGB).
|
|
pub fn screen_image(pix: &Pixmap, profile: ColorProfile) -> egui::ColorImage {
|
|
let size = [pix.width() as usize, pix.height() as usize];
|
|
if profile.is_srgb() {
|
|
return egui::ColorImage::from_rgba_premultiplied(size, pix.data());
|
|
}
|
|
let mut data = pix.data().to_vec();
|
|
profile.conversion_to(ColorProfile::Srgb).apply_pixels(&mut data);
|
|
egui::ColorImage::from_rgba_premultiplied(size, &data)
|
|
}
|
|
|
|
/// Soft proof: grey diagonal stripes over the pixels whose colours sRGB can't show.
|
|
fn mark_outside_srgb(image: &mut egui::ColorImage, pix: &Pixmap, profile: ColorProfile) {
|
|
let w = image.size[0].max(1);
|
|
let clipped = profile.conversion_to(ColorProfile::Srgb).clipped_pixels(pix.data());
|
|
for (i, (px, out)) in image.pixels.iter_mut().zip(clipped).enumerate() {
|
|
if out && ((i % w) + (i / w)) % 8 < 4 {
|
|
*px = egui::Color32::from_gray(128);
|
|
}
|
|
}
|
|
}
|
|
|
|
fn run(job: &Job, retained: &mut Retained) -> Option<Done> {
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
let t0 = std::time::Instant::now();
|
|
// The canvas is a preview: what's too small to see in detail at this zoom is approximated.
|
|
let opts = figcraft_render::Options {
|
|
outline_mode: job.outline,
|
|
outlined: job.outlined.clone(),
|
|
skip: job.skip.clone(),
|
|
solo: job.solo,
|
|
lod: true,
|
|
..Default::default()
|
|
};
|
|
let (view, pix, work) = retained.render(&job.doc, job.page, &job.view, &opts)?;
|
|
let mut image = screen_image(pix, job.doc.color_profile);
|
|
if job.proof {
|
|
mark_outside_srgb(&mut image, pix, job.doc.color_profile);
|
|
}
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
let ms = t0.elapsed().as_secs_f64() * 1000.0;
|
|
#[cfg(target_arch = "wasm32")]
|
|
let ms = 0.0;
|
|
Some(Done { image, view, ms, work })
|
|
}
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
pub struct Worker {
|
|
tx: std::sync::mpsc::Sender<(u64, Job)>,
|
|
rx: std::sync::mpsc::Receiver<Done>,
|
|
/// Jobs submitted so far.
|
|
submitted: u64,
|
|
/// The newest job the thread is done with: rendered (or skipped for a newer one) and the UI
|
|
/// woken for it.
|
|
finished: Arc<std::sync::atomic::AtomicU64>,
|
|
}
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
impl Worker {
|
|
pub fn new(ctx: egui::Context) -> Self {
|
|
let (tx, jobs) = std::sync::mpsc::channel::<(u64, Job)>();
|
|
let (done_tx, rx) = std::sync::mpsc::channel::<Done>();
|
|
let finished = Arc::new(std::sync::atomic::AtomicU64::new(0));
|
|
let done_with = finished.clone();
|
|
// The window that renders with it (each window has its own worker).
|
|
let viewport = ctx.viewport_id();
|
|
let spawned = std::thread::Builder::new().name("figcraft-render".into()).spawn(move || {
|
|
let mut retained = Retained::default();
|
|
while let Ok((mut seq, mut job)) = jobs.recv() {
|
|
// Skip to the newest queued job.
|
|
while let Ok(newer) = jobs.try_recv() {
|
|
(seq, job) = newer;
|
|
}
|
|
let res = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| run(&job, &mut retained)));
|
|
let done = match res {
|
|
Ok(d) => d,
|
|
Err(_) => {
|
|
// Don't build on an image a panic may have left half drawn.
|
|
retained.clear();
|
|
None
|
|
}
|
|
};
|
|
if let Some(d) = done
|
|
&& done_tx.send(d).is_err()
|
|
{
|
|
break;
|
|
}
|
|
ctx.request_repaint_of(viewport);
|
|
done_with.store(seq, std::sync::atomic::Ordering::Release);
|
|
}
|
|
});
|
|
if let Err(e) = spawned {
|
|
log::error!("render thread: {e}");
|
|
}
|
|
Self { tx, rx, submitted: 0, finished }
|
|
}
|
|
|
|
pub fn submit(&mut self, job: Job) {
|
|
if self.tx.send((self.submitted + 1, job)).is_ok() {
|
|
self.submitted += 1;
|
|
}
|
|
}
|
|
|
|
/// Whether a submitted job is still rendering or hasn't woken the UI yet (a finished image
|
|
/// may still wait for [`Self::poll`]).
|
|
pub fn in_flight(&self) -> bool {
|
|
self.finished.load(std::sync::atomic::Ordering::Acquire) < self.submitted
|
|
}
|
|
|
|
pub fn poll(&mut self) -> Option<Done> {
|
|
let mut last = None;
|
|
while let Ok(d) = self.rx.try_recv() {
|
|
last = Some(d);
|
|
}
|
|
last
|
|
}
|
|
}
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
pub struct Worker {
|
|
pending: Option<Job>,
|
|
retained: Retained,
|
|
}
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
impl Worker {
|
|
pub fn new(_ctx: egui::Context) -> Self {
|
|
Self { pending: None, retained: Retained::default() }
|
|
}
|
|
pub fn submit(&mut self, job: Job) {
|
|
self.pending = Some(job);
|
|
}
|
|
pub fn in_flight(&self) -> bool {
|
|
self.pending.is_some()
|
|
}
|
|
pub fn poll(&mut self) -> Option<Done> {
|
|
let job = self.pending.take()?;
|
|
run(&job, &mut self.retained)
|
|
}
|
|
}
|
|
|
|
/// Render synchronously from scratch (tests, screenshots).
|
|
pub fn render_now(job: &Job) -> Option<Done> {
|
|
run(job, &mut Retained::default())
|
|
}
|