Introduce canvas::Cache grouping
Caches with the same `Group` will share their text atlas!
This commit is contained in:
parent
24501fd73b
commit
b5b78d505e
10 changed files with 279 additions and 101 deletions
158
graphics/src/cache.rs
Normal file
158
graphics/src/cache.rs
Normal file
|
|
@ -0,0 +1,158 @@
|
||||||
|
//! Cache computations and efficiently reuse them.
|
||||||
|
use std::cell::RefCell;
|
||||||
|
use std::fmt;
|
||||||
|
use std::sync::atomic::{self, AtomicU64};
|
||||||
|
|
||||||
|
/// A simple cache that stores generated values to avoid recomputation.
|
||||||
|
///
|
||||||
|
/// Keeps track of the last generated value after clearing.
|
||||||
|
pub struct Cache<T> {
|
||||||
|
group: Group,
|
||||||
|
state: RefCell<State<T>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Cache<T> {
|
||||||
|
/// Creates a new empty [`Cache`].
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Cache {
|
||||||
|
group: Group::unique(),
|
||||||
|
state: RefCell::new(State::Empty { previous: None }),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates a new empty [`Cache`] with the given [`Group`].
|
||||||
|
///
|
||||||
|
/// Caches within the same group may reuse internal rendering storage.
|
||||||
|
///
|
||||||
|
/// You should generally group caches that are likely to change
|
||||||
|
/// together.
|
||||||
|
pub fn with_group(group: Group) -> Self {
|
||||||
|
Cache {
|
||||||
|
group,
|
||||||
|
state: RefCell::new(State::Empty { previous: None }),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the [`Group`] of the [`Cache`].
|
||||||
|
pub fn group(&self) -> Group {
|
||||||
|
self.group
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Puts the given value in the [`Cache`].
|
||||||
|
///
|
||||||
|
/// Notice that, given this is a cache, a mutable reference is not
|
||||||
|
/// necessary to call this method. You can safely update the cache in
|
||||||
|
/// rendering code.
|
||||||
|
pub fn put(&self, value: T) {
|
||||||
|
*self.state.borrow_mut() = State::Filled { current: value };
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a reference cell to the internal [`State`] of the [`Cache`].
|
||||||
|
pub fn state(&self) -> &RefCell<State<T>> {
|
||||||
|
&self.state
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Clears the [`Cache`].
|
||||||
|
pub fn clear(&self)
|
||||||
|
where
|
||||||
|
T: Clone,
|
||||||
|
{
|
||||||
|
use std::ops::Deref;
|
||||||
|
|
||||||
|
let previous = match self.state.borrow().deref() {
|
||||||
|
State::Empty { previous } => previous.clone(),
|
||||||
|
State::Filled { current } => Some(current.clone()),
|
||||||
|
};
|
||||||
|
|
||||||
|
*self.state.borrow_mut() = State::Empty { previous };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A cache group.
|
||||||
|
///
|
||||||
|
/// Caches that share the same group generally change together.
|
||||||
|
///
|
||||||
|
/// A cache group can be used to implement certain performance
|
||||||
|
/// optimizations during rendering, like batching or sharing atlases.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
|
pub struct Group(u64);
|
||||||
|
|
||||||
|
impl Group {
|
||||||
|
/// Generates a new unique cache [`Group`].
|
||||||
|
pub fn unique() -> Self {
|
||||||
|
static NEXT: AtomicU64 = AtomicU64::new(0);
|
||||||
|
|
||||||
|
Self(NEXT.fetch_add(1, atomic::Ordering::Relaxed))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> fmt::Debug for Cache<T>
|
||||||
|
where
|
||||||
|
T: fmt::Debug,
|
||||||
|
{
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
use std::ops::Deref;
|
||||||
|
|
||||||
|
let state = self.state.borrow();
|
||||||
|
|
||||||
|
match state.deref() {
|
||||||
|
State::Empty { previous } => {
|
||||||
|
write!(f, "Cache::Empty {{ previous: {previous:?} }}")
|
||||||
|
}
|
||||||
|
State::Filled { current } => {
|
||||||
|
write!(f, "Cache::Filled {{ current: {current:?} }}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Default for Cache<T> {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The state of a [`Cache`].
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub enum State<T> {
|
||||||
|
/// The [`Cache`] is empty.
|
||||||
|
Empty {
|
||||||
|
/// The previous value of the [`Cache`].
|
||||||
|
previous: Option<T>,
|
||||||
|
},
|
||||||
|
/// The [`Cache`] is filled.
|
||||||
|
Filled {
|
||||||
|
/// The current value of the [`Cache`]
|
||||||
|
current: T,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A piece of data that can be cached.
|
||||||
|
pub trait Cached: Sized {
|
||||||
|
/// The type of cache produced.
|
||||||
|
type Cache: Clone;
|
||||||
|
|
||||||
|
/// Loads the [`Cache`] into a proper instance.
|
||||||
|
///
|
||||||
|
/// [`Cache`]: Self::Cache
|
||||||
|
fn load(cache: &Self::Cache) -> Self;
|
||||||
|
|
||||||
|
/// Caches this value, producing its corresponding [`Cache`].
|
||||||
|
///
|
||||||
|
/// [`Cache`]: Self::Cache
|
||||||
|
fn cache(self, group: Group, previous: Option<Self::Cache>) -> Self::Cache;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
impl Cached for () {
|
||||||
|
type Cache = ();
|
||||||
|
|
||||||
|
fn load(_cache: &Self::Cache) -> Self {}
|
||||||
|
|
||||||
|
fn cache(
|
||||||
|
self,
|
||||||
|
_group: Group,
|
||||||
|
_previous: Option<Self::Cache>,
|
||||||
|
) -> Self::Cache {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,24 +0,0 @@
|
||||||
/// A piece of data that can be cached.
|
|
||||||
pub trait Cached: Sized {
|
|
||||||
/// The type of cache produced.
|
|
||||||
type Cache: Clone;
|
|
||||||
|
|
||||||
/// Loads the [`Cache`] into a proper instance.
|
|
||||||
///
|
|
||||||
/// [`Cache`]: Self::Cache
|
|
||||||
fn load(cache: &Self::Cache) -> Self;
|
|
||||||
|
|
||||||
/// Caches this value, producing its corresponding [`Cache`].
|
|
||||||
///
|
|
||||||
/// [`Cache`]: Self::Cache
|
|
||||||
fn cache(self, previous: Option<Self::Cache>) -> Self::Cache;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
|
||||||
impl Cached for () {
|
|
||||||
type Cache = ();
|
|
||||||
|
|
||||||
fn load(_cache: &Self::Cache) -> Self {}
|
|
||||||
|
|
||||||
fn cache(self, _previous: Option<Self::Cache>) -> Self::Cache {}
|
|
||||||
}
|
|
||||||
|
|
@ -18,8 +18,8 @@ pub use text::Text;
|
||||||
|
|
||||||
pub use crate::gradient::{self, Gradient};
|
pub use crate::gradient::{self, Gradient};
|
||||||
|
|
||||||
|
use crate::cache::Cached;
|
||||||
use crate::core::{self, Size};
|
use crate::core::{self, Size};
|
||||||
use crate::Cached;
|
|
||||||
|
|
||||||
/// A renderer capable of drawing some [`Self::Geometry`].
|
/// A renderer capable of drawing some [`Self::Geometry`].
|
||||||
pub trait Renderer: core::Renderer {
|
pub trait Renderer: core::Renderer {
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
|
use crate::cache::{self, Cached};
|
||||||
use crate::core::Size;
|
use crate::core::Size;
|
||||||
use crate::geometry::{self, Frame};
|
use crate::geometry::{self, Frame};
|
||||||
use crate::Cached;
|
|
||||||
|
|
||||||
use std::cell::RefCell;
|
pub use cache::Group;
|
||||||
|
|
||||||
/// A simple cache that stores generated geometry to avoid recomputation.
|
/// A simple cache that stores generated geometry to avoid recomputation.
|
||||||
///
|
///
|
||||||
|
|
@ -12,7 +12,13 @@ pub struct Cache<Renderer>
|
||||||
where
|
where
|
||||||
Renderer: geometry::Renderer,
|
Renderer: geometry::Renderer,
|
||||||
{
|
{
|
||||||
state: RefCell<State<Renderer::Geometry>>,
|
raw: crate::Cache<Data<<Renderer::Geometry as Cached>::Cache>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
struct Data<T> {
|
||||||
|
bounds: Size,
|
||||||
|
geometry: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Renderer> Cache<Renderer>
|
impl<Renderer> Cache<Renderer>
|
||||||
|
|
@ -22,20 +28,25 @@ where
|
||||||
/// Creates a new empty [`Cache`].
|
/// Creates a new empty [`Cache`].
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Cache {
|
Cache {
|
||||||
state: RefCell::new(State::Empty { previous: None }),
|
raw: cache::Cache::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates a new empty [`Cache`] with the given [`Group`].
|
||||||
|
///
|
||||||
|
/// Caches within the same group may reuse internal rendering storage.
|
||||||
|
///
|
||||||
|
/// You should generally group caches that are likely to change
|
||||||
|
/// together.
|
||||||
|
pub fn with_group(group: Group) -> Self {
|
||||||
|
Cache {
|
||||||
|
raw: crate::Cache::with_group(group),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Clears the [`Cache`], forcing a redraw the next time it is used.
|
/// Clears the [`Cache`], forcing a redraw the next time it is used.
|
||||||
pub fn clear(&self) {
|
pub fn clear(&self) {
|
||||||
use std::ops::Deref;
|
self.raw.clear();
|
||||||
|
|
||||||
let previous = match self.state.borrow().deref() {
|
|
||||||
State::Empty { previous } => previous.clone(),
|
|
||||||
State::Filled { geometry, .. } => Some(geometry.clone()),
|
|
||||||
};
|
|
||||||
|
|
||||||
*self.state.borrow_mut() = State::Empty { previous };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Draws geometry using the provided closure and stores it in the
|
/// Draws geometry using the provided closure and stores it in the
|
||||||
|
|
@ -56,27 +67,30 @@ where
|
||||||
) -> Renderer::Geometry {
|
) -> Renderer::Geometry {
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
|
||||||
let previous = match self.state.borrow().deref() {
|
let state = self.raw.state();
|
||||||
State::Empty { previous } => previous.clone(),
|
|
||||||
State::Filled {
|
let previous = match state.borrow().deref() {
|
||||||
bounds: cached_bounds,
|
cache::State::Empty { previous } => {
|
||||||
geometry,
|
previous.as_ref().map(|data| data.geometry.clone())
|
||||||
} => {
|
}
|
||||||
if *cached_bounds == bounds {
|
cache::State::Filled { current } => {
|
||||||
return Cached::load(geometry);
|
if current.bounds == bounds {
|
||||||
|
return Cached::load(¤t.geometry);
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(geometry.clone())
|
Some(current.geometry.clone())
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut frame = Frame::new(renderer, bounds);
|
let mut frame = Frame::new(renderer, bounds);
|
||||||
draw_fn(&mut frame);
|
draw_fn(&mut frame);
|
||||||
|
|
||||||
let geometry = frame.into_geometry().cache(previous);
|
let geometry = frame.into_geometry().cache(self.raw.group(), previous);
|
||||||
let result = Cached::load(&geometry);
|
let result = Cached::load(&geometry);
|
||||||
|
|
||||||
*self.state.borrow_mut() = State::Filled { bounds, geometry };
|
*state.borrow_mut() = cache::State::Filled {
|
||||||
|
current: Data { bounds, geometry },
|
||||||
|
};
|
||||||
|
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
@ -85,16 +99,10 @@ where
|
||||||
impl<Renderer> std::fmt::Debug for Cache<Renderer>
|
impl<Renderer> std::fmt::Debug for Cache<Renderer>
|
||||||
where
|
where
|
||||||
Renderer: geometry::Renderer,
|
Renderer: geometry::Renderer,
|
||||||
|
<Renderer::Geometry as Cached>::Cache: std::fmt::Debug,
|
||||||
{
|
{
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
let state = self.state.borrow();
|
write!(f, "{:?}", &self.raw)
|
||||||
|
|
||||||
match *state {
|
|
||||||
State::Empty { .. } => write!(f, "Cache::Empty"),
|
|
||||||
State::Filled { bounds, .. } => {
|
|
||||||
write!(f, "Cache::Filled {{ bounds: {bounds:?} }}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -106,16 +114,3 @@ where
|
||||||
Self::new()
|
Self::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum State<Geometry>
|
|
||||||
where
|
|
||||||
Geometry: Cached,
|
|
||||||
{
|
|
||||||
Empty {
|
|
||||||
previous: Option<Geometry::Cache>,
|
|
||||||
},
|
|
||||||
Filled {
|
|
||||||
bounds: Size,
|
|
||||||
geometry: Geometry::Cache,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -9,10 +9,10 @@
|
||||||
)]
|
)]
|
||||||
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
|
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
|
||||||
mod antialiasing;
|
mod antialiasing;
|
||||||
mod cached;
|
|
||||||
mod settings;
|
mod settings;
|
||||||
mod viewport;
|
mod viewport;
|
||||||
|
|
||||||
|
pub mod cache;
|
||||||
pub mod color;
|
pub mod color;
|
||||||
pub mod compositor;
|
pub mod compositor;
|
||||||
pub mod damage;
|
pub mod damage;
|
||||||
|
|
@ -27,7 +27,7 @@ pub mod text;
|
||||||
pub mod geometry;
|
pub mod geometry;
|
||||||
|
|
||||||
pub use antialiasing::Antialiasing;
|
pub use antialiasing::Antialiasing;
|
||||||
pub use cached::Cached;
|
pub use cache::Cache;
|
||||||
pub use compositor::Compositor;
|
pub use compositor::Compositor;
|
||||||
pub use error::Error;
|
pub use error::Error;
|
||||||
pub use gradient::Gradient;
|
pub use gradient::Gradient;
|
||||||
|
|
|
||||||
|
|
@ -428,8 +428,8 @@ where
|
||||||
mod geometry {
|
mod geometry {
|
||||||
use super::Renderer;
|
use super::Renderer;
|
||||||
use crate::core::{Point, Radians, Rectangle, Size, Vector};
|
use crate::core::{Point, Radians, Rectangle, Size, Vector};
|
||||||
|
use crate::graphics::cache::{self, Cached};
|
||||||
use crate::graphics::geometry::{self, Fill, Path, Stroke, Text};
|
use crate::graphics::geometry::{self, Fill, Path, Stroke, Text};
|
||||||
use crate::graphics::Cached;
|
|
||||||
|
|
||||||
impl<A, B> geometry::Renderer for Renderer<A, B>
|
impl<A, B> geometry::Renderer for Renderer<A, B>
|
||||||
where
|
where
|
||||||
|
|
@ -483,21 +483,25 @@ mod geometry {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cache(self, previous: Option<Self::Cache>) -> Self::Cache {
|
fn cache(
|
||||||
|
self,
|
||||||
|
group: cache::Group,
|
||||||
|
previous: Option<Self::Cache>,
|
||||||
|
) -> Self::Cache {
|
||||||
match (self, previous) {
|
match (self, previous) {
|
||||||
(
|
(
|
||||||
Self::Primary(geometry),
|
Self::Primary(geometry),
|
||||||
Some(Geometry::Primary(previous)),
|
Some(Geometry::Primary(previous)),
|
||||||
) => Geometry::Primary(geometry.cache(Some(previous))),
|
) => Geometry::Primary(geometry.cache(group, Some(previous))),
|
||||||
(Self::Primary(geometry), None) => {
|
(Self::Primary(geometry), None) => {
|
||||||
Geometry::Primary(geometry.cache(None))
|
Geometry::Primary(geometry.cache(group, None))
|
||||||
}
|
}
|
||||||
(
|
(
|
||||||
Self::Secondary(geometry),
|
Self::Secondary(geometry),
|
||||||
Some(Geometry::Secondary(previous)),
|
Some(Geometry::Secondary(previous)),
|
||||||
) => Geometry::Secondary(geometry.cache(Some(previous))),
|
) => Geometry::Secondary(geometry.cache(group, Some(previous))),
|
||||||
(Self::Secondary(geometry), None) => {
|
(Self::Secondary(geometry), None) => {
|
||||||
Geometry::Secondary(geometry.cache(None))
|
Geometry::Secondary(geometry.cache(group, None))
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,10 @@
|
||||||
use crate::core::text::LineHeight;
|
use crate::core::text::LineHeight;
|
||||||
use crate::core::{Pixels, Point, Radians, Rectangle, Size, Vector};
|
use crate::core::{Pixels, Point, Radians, Rectangle, Size, Vector};
|
||||||
|
use crate::graphics::cache::{self, Cached};
|
||||||
use crate::graphics::geometry::fill::{self, Fill};
|
use crate::graphics::geometry::fill::{self, Fill};
|
||||||
use crate::graphics::geometry::stroke::{self, Stroke};
|
use crate::graphics::geometry::stroke::{self, Stroke};
|
||||||
use crate::graphics::geometry::{self, Path, Style};
|
use crate::graphics::geometry::{self, Path, Style};
|
||||||
use crate::graphics::{Cached, Gradient, Text};
|
use crate::graphics::{Gradient, Text};
|
||||||
use crate::Primitive;
|
use crate::Primitive;
|
||||||
|
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
@ -32,7 +33,7 @@ impl Cached for Geometry {
|
||||||
Self::Cache(cache.clone())
|
Self::Cache(cache.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cache(self, _previous: Option<Cache>) -> Cache {
|
fn cache(self, _group: cache::Group, _previous: Option<Cache>) -> Cache {
|
||||||
match self {
|
match self {
|
||||||
Self::Live {
|
Self::Live {
|
||||||
primitives,
|
primitives,
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ use crate::core::text::LineHeight;
|
||||||
use crate::core::{
|
use crate::core::{
|
||||||
Pixels, Point, Radians, Rectangle, Size, Transformation, Vector,
|
Pixels, Point, Radians, Rectangle, Size, Transformation, Vector,
|
||||||
};
|
};
|
||||||
|
use crate::graphics::cache::{self, Cached};
|
||||||
use crate::graphics::color;
|
use crate::graphics::color;
|
||||||
use crate::graphics::geometry::fill::{self, Fill};
|
use crate::graphics::geometry::fill::{self, Fill};
|
||||||
use crate::graphics::geometry::{
|
use crate::graphics::geometry::{
|
||||||
|
|
@ -10,7 +11,7 @@ use crate::graphics::geometry::{
|
||||||
};
|
};
|
||||||
use crate::graphics::gradient::{self, Gradient};
|
use crate::graphics::gradient::{self, Gradient};
|
||||||
use crate::graphics::mesh::{self, Mesh};
|
use crate::graphics::mesh::{self, Mesh};
|
||||||
use crate::graphics::{self, Cached, Text};
|
use crate::graphics::{self, Text};
|
||||||
use crate::text;
|
use crate::text;
|
||||||
use crate::triangle;
|
use crate::triangle;
|
||||||
|
|
||||||
|
|
@ -38,7 +39,11 @@ impl Cached for Geometry {
|
||||||
Geometry::Cached(cache.clone())
|
Geometry::Cached(cache.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cache(self, previous: Option<Self::Cache>) -> Self::Cache {
|
fn cache(
|
||||||
|
self,
|
||||||
|
group: cache::Group,
|
||||||
|
previous: Option<Self::Cache>,
|
||||||
|
) -> Self::Cache {
|
||||||
match self {
|
match self {
|
||||||
Self::Live { meshes, text } => {
|
Self::Live { meshes, text } => {
|
||||||
if let Some(mut previous) = previous {
|
if let Some(mut previous) = previous {
|
||||||
|
|
@ -51,14 +56,14 @@ impl Cached for Geometry {
|
||||||
if let Some(cache) = &mut previous.text {
|
if let Some(cache) = &mut previous.text {
|
||||||
cache.update(text);
|
cache.update(text);
|
||||||
} else {
|
} else {
|
||||||
previous.text = text::Cache::new(text);
|
previous.text = text::Cache::new(group, text);
|
||||||
}
|
}
|
||||||
|
|
||||||
previous
|
previous
|
||||||
} else {
|
} else {
|
||||||
Cache {
|
Cache {
|
||||||
meshes: triangle::Cache::new(meshes),
|
meshes: triangle::Cache::new(meshes),
|
||||||
text: text::Cache::new(text),
|
text: text::Cache::new(group, text),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
use crate::core::alignment;
|
use crate::core::alignment;
|
||||||
use crate::core::{Rectangle, Size, Transformation};
|
use crate::core::{Rectangle, Size, Transformation};
|
||||||
|
use crate::graphics::cache;
|
||||||
use crate::graphics::color;
|
use crate::graphics::color;
|
||||||
use crate::graphics::text::cache::{self, Cache as BufferCache};
|
use crate::graphics::text::cache::{self as text_cache, Cache as BufferCache};
|
||||||
use crate::graphics::text::{font_system, to_color, Editor, Paragraph};
|
use crate::graphics::text::{font_system, to_color, Editor, Paragraph};
|
||||||
|
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
|
|
@ -35,6 +36,7 @@ pub enum Item {
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Cache {
|
pub struct Cache {
|
||||||
id: Id,
|
id: Id,
|
||||||
|
group: cache::Group,
|
||||||
text: Rc<[Text]>,
|
text: Rc<[Text]>,
|
||||||
version: usize,
|
version: usize,
|
||||||
}
|
}
|
||||||
|
|
@ -43,7 +45,7 @@ pub struct Cache {
|
||||||
pub struct Id(u64);
|
pub struct Id(u64);
|
||||||
|
|
||||||
impl Cache {
|
impl Cache {
|
||||||
pub fn new(text: Vec<Text>) -> Option<Self> {
|
pub fn new(group: cache::Group, text: Vec<Text>) -> Option<Self> {
|
||||||
static NEXT_ID: AtomicU64 = AtomicU64::new(0);
|
static NEXT_ID: AtomicU64 = AtomicU64::new(0);
|
||||||
|
|
||||||
if text.is_empty() {
|
if text.is_empty() {
|
||||||
|
|
@ -52,6 +54,7 @@ impl Cache {
|
||||||
|
|
||||||
Some(Self {
|
Some(Self {
|
||||||
id: Id(NEXT_ID.fetch_add(1, atomic::Ordering::Relaxed)),
|
id: Id(NEXT_ID.fetch_add(1, atomic::Ordering::Relaxed)),
|
||||||
|
group,
|
||||||
text: Rc::from(text),
|
text: Rc::from(text),
|
||||||
version: 0,
|
version: 0,
|
||||||
})
|
})
|
||||||
|
|
@ -65,29 +68,39 @@ impl Cache {
|
||||||
|
|
||||||
struct Upload {
|
struct Upload {
|
||||||
renderer: glyphon::TextRenderer,
|
renderer: glyphon::TextRenderer,
|
||||||
atlas: glyphon::TextAtlas,
|
|
||||||
buffer_cache: BufferCache,
|
buffer_cache: BufferCache,
|
||||||
transformation: Transformation,
|
transformation: Transformation,
|
||||||
version: usize,
|
version: usize,
|
||||||
text: rc::Weak<[Text]>,
|
text: rc::Weak<[Text]>,
|
||||||
|
_atlas: rc::Weak<()>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct Storage {
|
pub struct Storage {
|
||||||
|
groups: FxHashMap<cache::Group, Group>,
|
||||||
uploads: FxHashMap<Id, Upload>,
|
uploads: FxHashMap<Id, Upload>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Group {
|
||||||
|
atlas: glyphon::TextAtlas,
|
||||||
|
previous_uploads: usize,
|
||||||
|
handle: Rc<()>,
|
||||||
|
}
|
||||||
|
|
||||||
impl Storage {
|
impl Storage {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self::default()
|
Self::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get(&self, cache: &Cache) -> Option<&Upload> {
|
fn get(&self, cache: &Cache) -> Option<(&glyphon::TextAtlas, &Upload)> {
|
||||||
if cache.text.is_empty() {
|
if cache.text.is_empty() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.uploads.get(&cache.id)
|
self.groups
|
||||||
|
.get(&cache.group)
|
||||||
|
.map(|group| &group.atlas)
|
||||||
|
.zip(self.uploads.get(&cache.id))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn prepare(
|
fn prepare(
|
||||||
|
|
@ -101,6 +114,20 @@ impl Storage {
|
||||||
bounds: Rectangle,
|
bounds: Rectangle,
|
||||||
target_size: Size<u32>,
|
target_size: Size<u32>,
|
||||||
) {
|
) {
|
||||||
|
let group_count = self.groups.len();
|
||||||
|
|
||||||
|
let group = self.groups.entry(cache.group).or_insert_with(|| {
|
||||||
|
log::info!("New text atlas created (total: {})", group_count + 1);
|
||||||
|
|
||||||
|
Group {
|
||||||
|
atlas: glyphon::TextAtlas::with_color_mode(
|
||||||
|
device, queue, format, COLOR_MODE,
|
||||||
|
),
|
||||||
|
previous_uploads: 0,
|
||||||
|
handle: Rc::new(()),
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
match self.uploads.entry(cache.id) {
|
match self.uploads.entry(cache.id) {
|
||||||
hash_map::Entry::Occupied(entry) => {
|
hash_map::Entry::Occupied(entry) => {
|
||||||
let upload = entry.into_mut();
|
let upload = entry.into_mut();
|
||||||
|
|
@ -114,7 +141,7 @@ impl Storage {
|
||||||
queue,
|
queue,
|
||||||
encoder,
|
encoder,
|
||||||
&mut upload.renderer,
|
&mut upload.renderer,
|
||||||
&mut upload.atlas,
|
&mut group.atlas,
|
||||||
&mut upload.buffer_cache,
|
&mut upload.buffer_cache,
|
||||||
&cache.text,
|
&cache.text,
|
||||||
bounds,
|
bounds,
|
||||||
|
|
@ -127,16 +154,11 @@ impl Storage {
|
||||||
upload.transformation = new_transformation;
|
upload.transformation = new_transformation;
|
||||||
|
|
||||||
upload.buffer_cache.trim();
|
upload.buffer_cache.trim();
|
||||||
upload.atlas.trim();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
hash_map::Entry::Vacant(entry) => {
|
hash_map::Entry::Vacant(entry) => {
|
||||||
let mut atlas = glyphon::TextAtlas::with_color_mode(
|
|
||||||
device, queue, format, COLOR_MODE,
|
|
||||||
);
|
|
||||||
|
|
||||||
let mut renderer = glyphon::TextRenderer::new(
|
let mut renderer = glyphon::TextRenderer::new(
|
||||||
&mut atlas,
|
&mut group.atlas,
|
||||||
device,
|
device,
|
||||||
wgpu::MultisampleState::default(),
|
wgpu::MultisampleState::default(),
|
||||||
None,
|
None,
|
||||||
|
|
@ -149,7 +171,7 @@ impl Storage {
|
||||||
queue,
|
queue,
|
||||||
encoder,
|
encoder,
|
||||||
&mut renderer,
|
&mut renderer,
|
||||||
&mut atlas,
|
&mut group.atlas,
|
||||||
&mut buffer_cache,
|
&mut buffer_cache,
|
||||||
&cache.text,
|
&cache.text,
|
||||||
bounds,
|
bounds,
|
||||||
|
|
@ -159,11 +181,11 @@ impl Storage {
|
||||||
|
|
||||||
let _ = entry.insert(Upload {
|
let _ = entry.insert(Upload {
|
||||||
renderer,
|
renderer,
|
||||||
atlas,
|
|
||||||
buffer_cache,
|
buffer_cache,
|
||||||
transformation: new_transformation,
|
transformation: new_transformation,
|
||||||
version: 0,
|
version: 0,
|
||||||
text: Rc::downgrade(&cache.text),
|
text: Rc::downgrade(&cache.text),
|
||||||
|
_atlas: Rc::downgrade(&group.handle),
|
||||||
});
|
});
|
||||||
|
|
||||||
log::info!(
|
log::info!(
|
||||||
|
|
@ -178,6 +200,22 @@ impl Storage {
|
||||||
pub fn trim(&mut self) {
|
pub fn trim(&mut self) {
|
||||||
self.uploads
|
self.uploads
|
||||||
.retain(|_id, upload| upload.text.strong_count() > 0);
|
.retain(|_id, upload| upload.text.strong_count() > 0);
|
||||||
|
|
||||||
|
self.groups.retain(|_id, group| {
|
||||||
|
let uploads_alive = Rc::weak_count(&group.handle);
|
||||||
|
|
||||||
|
if uploads_alive == 0 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if uploads_alive < group.previous_uploads {
|
||||||
|
group.atlas.trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
group.previous_uploads = uploads_alive;
|
||||||
|
|
||||||
|
true
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -306,10 +344,10 @@ impl Pipeline {
|
||||||
layer_count += 1;
|
layer_count += 1;
|
||||||
}
|
}
|
||||||
Item::Cached { cache, .. } => {
|
Item::Cached { cache, .. } => {
|
||||||
if let Some(upload) = storage.get(cache) {
|
if let Some((atlas, upload)) = storage.get(cache) {
|
||||||
upload
|
upload
|
||||||
.renderer
|
.renderer
|
||||||
.render(&upload.atlas, render_pass)
|
.render(atlas, render_pass)
|
||||||
.expect("Render cached text");
|
.expect("Render cached text");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -345,7 +383,7 @@ fn prepare(
|
||||||
enum Allocation {
|
enum Allocation {
|
||||||
Paragraph(Paragraph),
|
Paragraph(Paragraph),
|
||||||
Editor(Editor),
|
Editor(Editor),
|
||||||
Cache(cache::KeyHash),
|
Cache(text_cache::KeyHash),
|
||||||
Raw(Arc<glyphon::Buffer>),
|
Raw(Arc<glyphon::Buffer>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -369,7 +407,7 @@ fn prepare(
|
||||||
} => {
|
} => {
|
||||||
let (key, _) = buffer_cache.allocate(
|
let (key, _) = buffer_cache.allocate(
|
||||||
font_system,
|
font_system,
|
||||||
cache::Key {
|
text_cache::Key {
|
||||||
content,
|
content,
|
||||||
size: f32::from(*size),
|
size: f32::from(*size),
|
||||||
line_height: f32::from(*line_height),
|
line_height: f32::from(*line_height),
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ mod program;
|
||||||
pub use event::Event;
|
pub use event::Event;
|
||||||
pub use program::Program;
|
pub use program::Program;
|
||||||
|
|
||||||
|
pub use crate::graphics::cache::Group;
|
||||||
pub use crate::graphics::geometry::{
|
pub use crate::graphics::geometry::{
|
||||||
fill, gradient, path, stroke, Fill, Gradient, LineCap, LineDash, LineJoin,
|
fill, gradient, path, stroke, Fill, Gradient, LineCap, LineDash, LineJoin,
|
||||||
Path, Stroke, Style, Text,
|
Path, Stroke, Style, Text,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue