GitCorner

Code

ossartcraftvectorcraft🌐
2bc41aeb8602
use super::{canvas, cursors, render_worker};

/// Cached canvas raster.
pub struct CanvasCache {
    pub renderer: vectorcraft_render::Renderer,
    pub texture: Option<egui::TextureHandle>,
    pub key: Option<CacheKey>,
    /// The document the frame shown was rendered from, and the options it was rendered with, with
    /// [`Self::key`]: when only the document changes, the canvas renders again only where it
    /// changed, measured by [`Self::renderer`], which drew it ([`vectorcraft_render::Renderer::changed`]).
    pub shown: Option<(std::sync::Arc<vectorcraft_doc::Document>, vectorcraft_render::RenderOptions)>,
    pub last_ms: f64,
    /// The frame shown drew raster effects from rasters made before their objects moved, or at a
    /// drag's lower resolution ([`vectorcraft_render::FrameStats::moved`]): the next frame outside
    /// an interaction runs them again, on the worker when there is one.
    pub moved: bool,
    /// The frame shown was rendered on the worker: an interaction's frames stay there
    /// ([`canvas::show`]).
    pub from_worker: bool,
    /// The frame shown holds pixels the worker copied from its last frame, and when it was shown
    /// (seconds, egui's input time): it renders whole once it has been shown for
    /// [`canvas::STILL_SECS`] with the view unmoved.
    pub reused: Option<f64>,
    /// Set in the web build, which has no render worker: while the view changes on a document whose
    /// last render took more than 8 ms, the canvas draws the frame shown reprojected and renders
    /// once the view has been still for [`canvas::STILL_SECS`].
    pub waits_for_still: bool,
    /// The cache key the canvas last asked for and when it first asked for it (seconds, egui's
    /// input time), kept while [`Self::waits_for_still`] is set.
    pub still: Option<(CacheKey, f64)>,
    /// The low-resolution frame that covers the parts of the canvas the frame shown leaves
    /// uncovered while it is drawn for a view it was not rendered for ([`canvas::Fill`]).
    pub fill: Option<canvas::Fill>,
    /// The thread that renders [`Self::fill`], beside [`Self::worker`]. None on wasm, which has no
    /// fill.
    pub fill_worker: Option<render_worker::Worker>,
    pub worker: Option<render_worker::Worker>,
    pub worker_started: bool,
    /// The slices as laid out for (document uid, revision): the canvas draws them every frame.
    pub slices: Option<SliceCache>,
    /// The print tiling's pages for (document uid, revision) (View → Show Print Tiling).
    pub print_tiling: Option<PrintTilingCache>,
    /// The gap lines View → Show Live Paint Gaps draws for (document uid, revision).
    pub live_paint_gaps: Option<LivePaintGapsCache>,
    /// Type → Find Font's list of the document's fonts and whether the document misses one, for a
    /// version of the document's content and of the available fonts: the dialog reads them every
    /// frame.
    pub find_font: Option<FindFontCache>,
    /// The marks Type → Show Hidden Characters drew on each text object in the last frame. A frame
    /// that does not draw them drops them ([`canvas::show`]).
    pub hidden_chars: canvas::HiddenChars,
    /// The words Edit › Spelling › Auto Spell Check underlined on each text object in the last
    /// frame; dropped while it is off.
    pub spelling: canvas::SpellingMarks,
    /// The selected objects' outlines, anchors and handles as drawn in the last frame. A frame that
    /// does not draw them drops them ([`canvas::show`]).
    pub selection_marks: canvas::SelectionMarks,
    /// The meshes the tools' shared paths (a long Blob Brush or Eraser drag's settled outline) were
    /// drawn with in the last frame. A frame that does not draw them drops them ([`canvas::show`]).
    pub shared_paths: canvas::SharedPaths,
    /// [`VectorcraftApp::selection_box`] for (document uid, revision, Use Preview Bounds and
    /// Measure Type by Glyph Bounds).
    pub selection_box: Option<((u64, u64, vectorcraft_effects::preview::Measure), Option<vectorcraft_doc::OrientedBox>)>,
    /// [`VectorcraftApp::selection_bounds`] for (document uid, revision).
    pub selection_bounds: Option<((u64, u64), Option<vectorcraft_geom::Rect>)>,
    /// [`VectorcraftApp::content_bounds`] for (document uid, revision).
    pub content_bounds: Option<((u64, u64), Option<vectorcraft_geom::Rect>)>,
    /// Whether the 3D and Materials panel's Export 3D Object button is available (a selected
    /// object has a 3D effect that `assets.add3D` exports) for (document uid, revision).
    pub three_d_exportable: Option<((u64, u64), bool)>,
    /// The Transform panel's Align to Pixel Grid state (on, off or mixed, and whether every
    /// selected object can be aligned) for (document uid, revision).
    pub pixel_aligned: Option<((u64, u64), Option<PixelAligned>)>,
    /// The symbol instances in the selection and its groups, for (document uid, revision): the
    /// Properties and Symbols panels read them every frame.
    pub symbol_instances: Option<((u64, u64), SymbolInstances)>,
    /// [`CacheKey::plugins`] by document uid, with the plug-in revision
    /// ([`vectorcraft_render::effects::plugin_revision`]) it was last checked at.
    pub plugin_keys: std::collections::HashMap<u64, (u64, u64)>,
    /// [`CacheKey::placed`] by document uid ([`canvas::placed_key`]).
    pub placed_keys: std::collections::HashMap<u64, PlacedKey>,
    /// The tools' cursors as OS cursor bitmaps.
    pub cursors: cursors::Images,
    /// The view the canvas last drew and when it last changed (egui's input time): while it moves,
    /// the canvas doesn't hit-test the art under the pointer ([`canvas::hover_wait`], #2452).
    pub view_moved: Option<(canvas::ViewKey, f64)>,
    /// The cursor the active tool last gave over the canvas, with the tool and the modifier keys
    /// it was for: kept while the view moves.
    pub hover_cursor: Option<(&'static str, vectorcraft_tools::Mods, vectorcraft_tools::Cursor)>,
    /// In Presentation Mode, the canvas rect the active artboard was last fitted to: it is fitted
    /// again when the rect changes (the window going full screen).
    pub presented: Option<egui::Rect>,
    /// The canvas a document's opening view was fitted to, with the view the fit gave. The window's
    /// layout can still move the canvas's edges over the next frames, so until the view is moved or
    /// the user acts, the opening view is fitted again when the canvas changes ([`canvas::show`]).
    pub opening_fit: Option<canvas::ViewKey>,
    /// Window › Arrange › Tile: the renders the panes of the other documents draw.
    pub panes: canvas::Panes,
    /// Window › Arrange › Float in Window: what the floating document windows keep between frames.
    pub windows: crate::doc_windows::Windows,
}

/// [`CanvasCache::placed_keys`]: a document's [`CacheKey::placed`] and the placed-document bitmap
/// generations ([`vectorcraft_render::placed_document::generation`]) it was checked at.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct PlacedKey {
    /// The last generation the document was checked at.
    pub checked: u64,
    /// The document's [`CacheKey::placed`]: the generation of its first check, or of the last check
    /// that found a newly made bitmap the document may show.
    pub key: u64,
    /// The value before it last changed, and the last generation that value was checked at: no
    /// bitmap of a placed document the document shows was made between the two.
    pub before: (u64, u64),
}

/// [`CanvasCache::slices`]: the layout of the slices of (document uid, revision).
pub type SliceCache = ((u64, u64), std::sync::Arc<Vec<vectorcraft_doc::SliceArea>>);

/// [`CanvasCache::live_paint_gaps`]: the gap lines of the selected Live Paint groups of (document
/// uid, revision).
pub type LivePaintGapsCache = ((u64, u64), std::sync::Arc<Vec<vectorcraft_tools::Overlay>>);

/// [`CanvasCache::find_font`]: the document's fonts as `text.fonts` lists them, and whether
/// [`vectorcraft_engine::cmd::fontfiles::missing_fonts`] finds any (`None` until it is asked).
#[derive(Clone, Debug)]
pub struct FindFontCache {
    /// The document and the content and fonts the answers were worked out from.
    pub key: FontsKey,
    pub fonts: std::sync::Arc<Vec<serde_json::Value>>,
    pub missing: Option<bool>,
}

/// A document's uid, a version of its content (compared by identity) and the font database's
/// generation ([`vectorcraft_text::FontDb::generation`]). The weak reference keeps the version's
/// allocation, so a later version is never placed at the same address while the key is kept.
#[derive(Clone, Debug)]
pub struct FontsKey {
    pub uid: u64,
    pub doc: std::sync::Weak<vectorcraft_doc::Document>,
    pub generation: u64,
}

impl PartialEq for FontsKey {
    fn eq(&self, other: &Self) -> bool {
        self.uid == other.uid && self.generation == other.generation && std::sync::Weak::ptr_eq(&self.doc, &other.doc)
    }
}

/// [`CanvasCache::pixel_aligned`]: the selected objects' Align to Pixel Grid, on or off when they
/// agree and `None` when they differ, with whether every one can be aligned.
pub type PixelAligned = (Option<bool>, bool);

/// [`CanvasCache::symbol_instances`]: the symbol instances in the selection and its groups.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct SymbolInstances {
    /// Each instance's id, in the order the selection's subtrees list them.
    pub ids: Vec<vectorcraft_doc::NodeId>,
    /// One of them has appearance changes of its own (`symbol_overrides`).
    pub changed: bool,
    /// One of them has a scale, rotation or shear that Reset Transformation would change; measured
    /// on the first read.
    pub transformed: Option<bool>,
}

/// [`CanvasCache::print_tiling`]: the print tiling of (document uid, revision).
pub type PrintTilingCache = ((u64, u64), std::sync::Arc<Vec<vectorcraft_pdf::TilingPage>>);

#[derive(Clone, Debug, PartialEq)]
pub struct CacheKey {
    pub doc: usize,
    pub revision: u64,
    /// An interaction is in progress ([`vectorcraft_render::RenderOptions::interacting`]).
    pub interacting: bool,
    /// A tool's drag on the canvas is in progress ([`vectorcraft_render::RenderOptions::dragging`]).
    pub dragging: bool,
    pub zoom: f64,
    pub cx: f64,
    pub cy: f64,
    pub w: u32,
    pub h: u32,
    pub outline: bool,
    pub trim: bool,
    /// View › Hide Template: template layers left out ([`vectorcraft_render::RenderOptions::hide_templates`]).
    pub templates: bool,
    /// View › Show/Hide Guides: whether guide paths are visible in the artwork raster.
    pub guides: bool,
    pub ppp: f32,
    pub hidden: Vec<u64>,
    pub rot: f64,
    /// General › Anti-aliased Artwork.
    pub anti_alias: bool,
    /// Type › Highlight Alternate Glyphs (#394).
    pub highlight_alternate_glyphs: bool,
    /// Type › Highlight Substituted Fonts.
    pub highlight_substituted_fonts: bool,
    /// A [`vectorcraft_render::placed_document::generation`] that changes when the frame may show a
    /// newly made bitmap of a placed document ([`canvas::placed_key`]). Bitmaps of files that only
    /// other documents hold leave it as it is, unless more bitmaps were made since the last check
    /// than are recorded and the document shows a placed document.
    pub placed: u64,
    /// The plug-in revision ([`vectorcraft_render::effects::plugin_revision`]) of the last plug-in
    /// install or removal, or of the last time deferred plug-in effect stages gave results, after
    /// which the document drew a plug-in effect ([`vectorcraft_render::draws_plugin_effects`]): a
    /// frame drawn before it draws those effects as the plug-ins installed then did. 0 until then.
    pub plugins: u64,
    /// View › Pixel Preview: the document pixels rendered (x0, y0, x1, y1), one per point, shown
    /// with hard edges. None: the art is rendered for the screen.
    pub pixel: Option<[i64; 4]>,
    /// Images sampled smoothly ([`vectorcraft_render::RenderOptions::smooth_images`]).
    pub smooth_images: bool,
    /// Isolation mode's group or layer ([`vectorcraft_render::RenderOptions::isolated`]).
    pub isolated: Option<u64>,
    /// Appearance of Black › On Screen › Display All Blacks as Rich Black
    /// ([`vectorcraft_render::RenderOptions::rich_black`]).
    pub rich_black: bool,
    /// Colour and style of guide paths ([`vectorcraft_render::RenderOptions::guide`]).
    pub guide: ([u8; 3], bool),
    /// The Image Trace objects drawn with their source image
    /// ([`vectorcraft_render::RenderOptions::trace_source`]).
    pub trace_source: Option<std::sync::Arc<Vec<vectorcraft_doc::NodeId>>>,
    /// The display profile the frame is converted into ([`crate::display_profile::DisplayProfile::key`]),
    /// 0 for none.
    pub display: u64,
    /// The paper under the art that translucent art is blended with before the conversion
    /// ([`crate::display_profile::convert_over`]). None without a display profile, and in Outline.
    pub backdrop: Option<std::sync::Arc<crate::display_profile::Backdrop>>,
}

impl CacheKey {
    /// [`Self::backdrop`] with the zoom its grid's cells are measured at, as
    /// [`crate::display_profile::convert_over`] takes it.
    pub(crate) fn over(&self) -> Option<(&crate::display_profile::Backdrop, f64)> {
        self.backdrop.as_deref().map(|b| (b, self.zoom))
    }
}

#[derive(Clone, Copy, Debug, Default)]
pub struct Perf {
    pub frame_ms: f64,
    pub render_ms: f64,
    pub fps: f64,
}