Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ParagraphCache alternative #996

Draft
wants to merge 21 commits into
base: main
Choose a base branch
from
Draft
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
9 changes: 7 additions & 2 deletions crates/common/src/layout.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::ops::Div;
use std::{
cell::RefCell,
ops::Div,
rc::Rc,
};

use freya_engine::prelude::Paragraph;
use torin::geometry::{
Expand Down Expand Up @@ -27,7 +31,8 @@ pub enum CursorLayoutResponse {
TextSelection { from: usize, to: usize, id: usize },
}

pub struct CachedParagraph(pub Paragraph, pub f32);
#[derive(Clone)]
pub struct CachedParagraph(pub Rc<RefCell<Paragraph>>);

/// # Safety
/// Skia `Paragraph` are neither Sync or Send, but in order to store them in the Associated
Expand Down
1 change: 1 addition & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ tracing = { workspace = true }
uuid = { workspace = true }
itertools = "0.13.0"
smallvec = { workspace = true }
indexmap = "2.6.0"

[dev-dependencies]
dioxus = { workspace = true }
Expand Down
37 changes: 26 additions & 11 deletions crates/core/src/dom/doms.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
use std::sync::{
Arc,
Mutex,
MutexGuard,
use std::{
cell::{
RefCell,
RefMut,
},
sync::{
Arc,
Mutex,
MutexGuard,
},
};

use dioxus_core::VirtualDom;
Expand Down Expand Up @@ -39,11 +45,14 @@ use freya_node_state::{
use torin::prelude::*;

use super::mutations_writer::MutationsWriter;
use crate::prelude::{
CompositorCache,
CompositorDirtyArea,
ParagraphElement,
TextGroupMeasurement,
use crate::{
prelude::{
CompositorCache,
CompositorDirtyArea,
ParagraphElement,
TextGroupMeasurement,
},
render::ParagraphCache,
};

pub type DioxusDOM = RealDom<CustomAttributeValues>;
Expand Down Expand Up @@ -130,10 +139,11 @@ pub struct FreyaDOM {
compositor_cache: Arc<Mutex<CompositorCache>>,
accessibility_dirty_nodes: Arc<Mutex<AccessibilityDirtyNodes>>,
accessibility_generator: Arc<AccessibilityGenerator>,
paragraph_cache: RefCell<ParagraphCache>,
}

impl Default for FreyaDOM {
fn default() -> Self {
impl FreyaDOM {
pub fn new(paragraph_cache_max_size: usize) -> Self {
let mut rdom = RealDom::<CustomAttributeValues>::new([
CursorState::to_type_erased(),
FontStyleState::to_type_erased(),
Expand All @@ -157,6 +167,7 @@ impl Default for FreyaDOM {
compositor_cache: Arc::default(),
accessibility_dirty_nodes: Arc::default(),
accessibility_generator: Arc::default(),
paragraph_cache: RefCell::new(ParagraphCache::new(paragraph_cache_max_size)),
}
}
}
Expand Down Expand Up @@ -194,6 +205,10 @@ impl FreyaDOM {
&self.accessibility_generator
}

pub fn paragraph_cache(&self) -> RefMut<ParagraphCache> {
self.paragraph_cache.borrow_mut()
}

/// Create the initial DOM from the given Mutations
pub fn init_dom(&mut self, vdom: &mut VirtualDom, scale_factor: f32) {
// Build the RealDOM
Expand Down
6 changes: 5 additions & 1 deletion crates/core/src/elements/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use freya_node_state::{
};

use super::utils::ElementUtils;
use crate::dom::DioxusNode;
use crate::{
dom::DioxusNode,
render::ParagraphCache,
};

pub struct ImageElement;

Expand All @@ -20,6 +23,7 @@ impl ElementUtils for ImageElement {
_font_manager: &FontMgr,
_default_fonts: &[String],
_scale_factor: f32,
_paragraph_cache: &mut ParagraphCache,
) {
let area = layout_node.visible_area();
let node_style = node_ref.get::<StyleState>().unwrap();
Expand Down
16 changes: 10 additions & 6 deletions crates/core/src/elements/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ use torin::prelude::{
use super::utils::ElementUtils;
use crate::{
prelude::DioxusNode,
render::align_main_align_paragraph,
render::{
align_main_align_paragraph,
ParagraphCache,
},
};

pub struct LabelElement;
Expand All @@ -28,6 +31,7 @@ impl ElementUtils for LabelElement {
_font_manager: &FontMgr,
_default_fonts: &[String],
_scale_factor: f32,
_paragraph_cache: &mut ParagraphCache,
) {
let paragraph = &layout_node
.data
Expand All @@ -39,9 +43,9 @@ impl ElementUtils for LabelElement {
let area = layout_node.visible_area();

let x = area.min_x();
let y = area.min_y() + align_main_align_paragraph(node_ref, &area, paragraph);
let y = area.min_y() + align_main_align_paragraph(node_ref, &area, &paragraph.borrow());

paragraph.paint(canvas, (x, y));
paragraph.borrow().paint(canvas, (x, y));
}

#[inline]
Expand All @@ -57,15 +61,15 @@ impl ElementUtils for LabelElement {
node_ref: &DioxusNode,
scale_factor: f32,
) -> Area {
let paragraph_font_height = &layout_node
let paragraph = &layout_node
.data
.as_ref()
.unwrap()
.get::<CachedParagraph>()
.unwrap()
.1;
.0;
let mut area = layout_node.visible_area();
area.size.height = area.size.height.max(*paragraph_font_height);
area.size.height = area.size.height.max(paragraph.borrow().height());

let font_style = node_ref.get::<FontStyleState>().unwrap();

Expand Down
21 changes: 12 additions & 9 deletions crates/core/src/elements/paragraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use crate::{
create_paragraph,
draw_cursor,
draw_cursor_highlights,
ParagraphCache,
ParagraphData,
},
};
Expand Down Expand Up @@ -65,14 +66,14 @@ impl ParagraphElement {
return;
}

let y = align_main_align_paragraph(node, &layout_node.area, paragraph);
let y = align_main_align_paragraph(node, &layout_node.area, &paragraph.borrow());

if let Some(cursor_reference) = &cursor_state.cursor_ref {
if let Some(cursor_position) = text_measurement.cursor_position {
let position = CursorPoint::new(cursor_position.x, cursor_position.y - y as f64);

// Calculate the new cursor position
let char_position = paragraph.get_glyph_position_at_coordinate(
let char_position = paragraph.borrow().get_glyph_position_at_coordinate(
position.mul(scale_factor).to_i32().to_tuple(),
);

Expand All @@ -91,11 +92,11 @@ impl ParagraphElement {
let dist_position = CursorPoint::new(dist.x, dist.y - y as f64);

// Calculate the start of the highlighting
let origin_char = paragraph.get_glyph_position_at_coordinate(
let origin_char = paragraph.borrow().get_glyph_position_at_coordinate(
origin_position.mul(scale_factor).to_i32().to_tuple(),
);
// Calculate the end of the highlighting
let dist_char = paragraph.get_glyph_position_at_coordinate(
let dist_char = paragraph.borrow().get_glyph_position_at_coordinate(
dist_position.mul(scale_factor).to_i32().to_tuple(),
);

Expand All @@ -122,6 +123,7 @@ impl ElementUtils for ParagraphElement {
_font_manager: &FontMgr,
default_fonts: &[String],
scale_factor: f32,
paragraph_cache: &mut ParagraphCache,
) {
let area = layout_node.visible_area();
let node_cursor_state = &*node_ref.get::<CursorState>().unwrap();
Expand All @@ -147,8 +149,9 @@ impl ElementUtils for ParagraphElement {
true,
default_fonts,
scale_factor,
paragraph_cache,
);
paint(&paragraph);
paint(&paragraph.0.borrow());
} else {
let paragraph = &layout_node
.data
Expand All @@ -157,7 +160,7 @@ impl ElementUtils for ParagraphElement {
.get::<CachedParagraph>()
.unwrap()
.0;
paint(paragraph);
paint(&paragraph.borrow());
};
}

Expand All @@ -184,15 +187,15 @@ impl ElementUtils for ParagraphElement {
node_ref: &DioxusNode,
scale_factor: f32,
) -> Area {
let paragraph_font_height = &layout_node
let paragraph = &layout_node
.data
.as_ref()
.unwrap()
.get::<CachedParagraph>()
.unwrap()
.1;
.0;
let mut area = layout_node.visible_area();
area.size.height = area.size.height.max(*paragraph_font_height);
area.size.height = area.size.height.max(paragraph.borrow().height());

// Iterate over all the text spans inside this paragraph and if any of them
// has a shadow at all, apply this shadow to the general paragraph.
Expand Down
2 changes: 2 additions & 0 deletions crates/core/src/elements/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::{
render_border,
render_shadow,
BorderShape,
ParagraphCache,
},
};

Expand Down Expand Up @@ -89,6 +90,7 @@ impl ElementUtils for RectElement {
_font_manager: &FontMgr,
_default_fonts: &[String],
scale_factor: f32,
_paragraph_cache: &mut ParagraphCache,
) {
let node_style = &*node_ref.get::<StyleState>().unwrap();

Expand Down
6 changes: 5 additions & 1 deletion crates/core/src/elements/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use freya_node_state::{
use torin::prelude::LayoutNode;

use super::utils::ElementUtils;
use crate::dom::DioxusNode;
use crate::{
dom::DioxusNode,
render::ParagraphCache,
};

pub struct SvgElement;

Expand All @@ -21,6 +24,7 @@ impl ElementUtils for SvgElement {
font_manager: &FontMgr,
_default_fonts: &[String],
_scale_factor: f32,
_paragraph_cache: &mut ParagraphCache,
) {
let area = layout_node.visible_area();
let node_style = &*node_ref.get::<StyleState>().unwrap();
Expand Down
12 changes: 11 additions & 1 deletion crates/core/src/elements/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ use torin::{
};

use super::*;
use crate::dom::DioxusNode;
use crate::{
dom::DioxusNode,
render::ParagraphCache,
};

pub trait ElementUtils {
fn is_point_inside_area(
Expand Down Expand Up @@ -55,6 +58,7 @@ pub trait ElementUtils {
font_manager: &FontMgr,
default_fonts: &[String],
scale_factor: f32,
paragraph_cache: &mut ParagraphCache,
);

fn element_drawing_area(
Expand Down Expand Up @@ -198,6 +202,7 @@ impl ElementUtils for ElementWithUtils {
font_manager: &FontMgr,
default_fonts: &[String],
scale_factor: f32,
paragraph_cache: &mut ParagraphCache,
) {
match self {
Self::Rect(el) => el.render(
Expand All @@ -208,6 +213,7 @@ impl ElementUtils for ElementWithUtils {
font_manager,
default_fonts,
scale_factor,
paragraph_cache,
),
Self::Svg(el) => el.render(
layout_node,
Expand All @@ -217,6 +223,7 @@ impl ElementUtils for ElementWithUtils {
font_manager,
default_fonts,
scale_factor,
paragraph_cache,
),
Self::Paragraph(el) => el.render(
layout_node,
Expand All @@ -226,6 +233,7 @@ impl ElementUtils for ElementWithUtils {
font_manager,
default_fonts,
scale_factor,
paragraph_cache,
),
Self::Image(el) => el.render(
layout_node,
Expand All @@ -235,6 +243,7 @@ impl ElementUtils for ElementWithUtils {
font_manager,
default_fonts,
scale_factor,
paragraph_cache,
),
Self::Label(el) => el.render(
layout_node,
Expand All @@ -244,6 +253,7 @@ impl ElementUtils for ElementWithUtils {
font_manager,
default_fonts,
scale_factor,
paragraph_cache,
),
}
}
Expand Down
10 changes: 9 additions & 1 deletion crates/core/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ pub fn process_layout(
) {
{
let rdom = fdom.rdom();
let mut paragraph_cache = fdom.paragraph_cache();
let mut dom_adapter = DioxusDOMAdapter::new(rdom, scale_factor);
let skia_measurer = SkiaMeasurer::new(rdom, font_collection, default_fonts, scale_factor);

let mut layout = fdom.layout();

Expand Down Expand Up @@ -54,7 +54,15 @@ pub fn process_layout(
buffer.extend(node.child_ids());
}
}

let root_id = fdom.rdom().root_id();
let skia_measurer = SkiaMeasurer::new(
rdom,
font_collection,
default_fonts,
scale_factor,
&mut paragraph_cache,
);

// Measure the layout
layout.measure(root_id, area, &mut Some(skia_measurer), &mut dom_adapter);
Expand Down
2 changes: 2 additions & 0 deletions crates/core/src/render/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
pub mod compositor;
pub mod paragraph_cache;
pub mod pipeline;
pub mod skia_measurer;
pub mod utils;
mod wireframe_renderer;

pub use compositor::*;
pub use paragraph_cache::*;
pub use pipeline::*;
pub use skia_measurer::*;
pub use utils::*;
Loading
Loading