Skip to content

Commit

Permalink
chore: Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
marc2332 committed Aug 14, 2024
1 parent d17bb7c commit 2961956
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 99 deletions.
36 changes: 17 additions & 19 deletions crates/common/src/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,13 @@ use std::sync::{
MutexGuard,
};

use freya_engine::prelude::Canvas;
use freya_native_core::NodeId;
use itertools::sorted;
use rustc_hash::{
FxHashMap,
FxHashSet,
};
use torin::prelude::{
Area,
AreaModel,
Size2D,
};
use torin::prelude::Area;

use crate::Layers;

Expand Down Expand Up @@ -55,11 +50,10 @@ impl Compositor {
pub fn run(
&self,
dirty_nodes: &CompositorDirtyNodes,
canvas: &Canvas,
get_affected: impl Fn(NodeId, bool) -> Vec<NodeId>,
get_area: impl Fn(NodeId) -> Option<Area>,
layers: &Layers,
) -> (Layers, Area) {
) -> (Layers, Option<Area>) {
let mut dirty_nodes = dirty_nodes.get();
let (mut invalidated_nodes, mut dirty_nodes) = {
(
Expand Down Expand Up @@ -87,26 +81,29 @@ impl Compositor {
}

let rendering_layers = Layers::default();
let dirty_area = Area::from_size(Size2D::new(999., 999.));
let mut dirty_area: Option<Area> = None;

let full_render = self.full_render.load(Ordering::Relaxed);

let run_check = |layer: i16, nodes: &[NodeId]| {
let mut run_check = |layer: i16, nodes: &[NodeId]| {
for node_id in nodes {
let Some(area) = get_area(*node_id) else {
continue;
};
let is_invalidated = full_render || invalidated_nodes.contains(node_id);
let is_area_invalidated = is_invalidated
|| invalidated_nodes.iter().any(|invalid_node| {
let Some(invalid_node_area) = get_area(*invalid_node) else {
return false;
};
invalid_node_area.area_intersects(&area)
});

if is_area_invalidated {
let is_area_invalidated = dirty_area
.map(|dirty_area| dirty_area.intersects(&area))
.unwrap_or_default();

if is_invalidated || is_area_invalidated {
rendering_layers.insert_node_in_layer(*node_id, layer);
if is_invalidated {
if let Some(dirty_area) = &mut dirty_area {
*dirty_area = dirty_area.union(&area);
} else {
dirty_area = Some(area)
}
}
}
}
};
Expand All @@ -122,6 +119,7 @@ impl Compositor {
}

self.full_render.store(false, Ordering::Relaxed);

(rendering_layers, dirty_area)
}

Expand Down
8 changes: 2 additions & 6 deletions crates/common/src/layers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,14 @@ impl Layers {
layer.push(node_id);
}

pub fn remove_node_from_layer(&self, node_id: NodeId, layer_n: i16) -> bool {
pub fn remove_node_from_layer(&self, node_id: NodeId, layer_n: i16) {
let mut layers = self.layers.lock().unwrap();
let layer = layers.get_mut(&layer_n).unwrap();
layer.retain(|id| *id != node_id);

let layer_is_empty = layer.is_empty();

if layer_is_empty {
if layer.is_empty() {
layers.remove(&layer_n);
}

layer_is_empty
}

pub fn layers(&self) -> MutexGuard<FxHashMap<i16, Vec<NodeId>>> {
Expand Down
17 changes: 2 additions & 15 deletions crates/core/src/dom/doms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::sync::{

use dioxus_core::VirtualDom;
use freya_common::{
Compositor,
CompositorDirtyNodes,
Layers,
ParagraphElements,
Expand Down Expand Up @@ -167,12 +166,7 @@ impl FreyaDOM {
}

/// Create the initial DOM from the given Mutations
pub fn init_dom(
&mut self,
vdom: &mut VirtualDom,
scale_factor: f32,
compositor: &mut Compositor,
) {
pub fn init_dom(&mut self, vdom: &mut VirtualDom, scale_factor: f32) {
// Build the RealDOM
vdom.rebuild(&mut MutationsWriter {
native_writer: self
Expand All @@ -183,7 +177,6 @@ impl FreyaDOM {
paragraphs: &self.paragraphs,
scale_factor,
compositor_dirty_nodes: &self.compositor_dirty_nodes,
compositor,
});

let mut ctx = SendAnyMap::new();
Expand All @@ -196,12 +189,7 @@ impl FreyaDOM {
}

/// Process the given mutations from the [`VirtualDOM`](dioxus_core::VirtualDom).
pub fn render_mutations(
&mut self,
vdom: &mut VirtualDom,
scale_factor: f32,
compositor: &mut Compositor,
) -> (bool, bool) {
pub fn render_mutations(&mut self, vdom: &mut VirtualDom, scale_factor: f32) -> (bool, bool) {
// Update the RealDOM
vdom.render_immediate(&mut MutationsWriter {
native_writer: self
Expand All @@ -212,7 +200,6 @@ impl FreyaDOM {
paragraphs: &self.paragraphs,
scale_factor,
compositor_dirty_nodes: &self.compositor_dirty_nodes,
compositor,
});

// Update the Nodes states
Expand Down
9 changes: 1 addition & 8 deletions crates/core/src/dom/mutations_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use dioxus_core::{
WriteMutations,
};
use freya_common::{
Compositor,
CompositorDirtyNodes,
Layers,
ParagraphElements,
Expand Down Expand Up @@ -32,7 +31,6 @@ pub struct MutationsWriter<'a> {
pub paragraphs: &'a ParagraphElements,
pub scale_factor: f32,
pub compositor_dirty_nodes: &'a CompositorDirtyNodes,
pub compositor: &'a mut Compositor,
}

impl<'a> MutationsWriter<'a> {
Expand Down Expand Up @@ -74,19 +72,14 @@ impl<'a> MutationsWriter<'a> {
}

// Remove from layers
let layer_was_removed = self
.layers
self.layers
.remove_node_from_layer(node_id, layer_state.layer);

// Remove from paragraph elements
if let Some(cursor_ref) = cursor_state.cursor_ref.as_ref() {
self.paragraphs
.remove_paragraph(node_id, &cursor_ref.text_id);
}

if layer_was_removed {
//self.compositor.remove_layer(layer_state.layer);
}
}
}
}
Expand Down
34 changes: 14 additions & 20 deletions crates/core/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use freya_engine::prelude::{
Canvas,
ClipOp,
Color,
IPoint,
Rect,
SamplingOptions,
Surface,
Expand Down Expand Up @@ -36,7 +35,6 @@ pub fn process_render(

let (dirty_layers, dirty_area) = compositor.run(
compositor_dirty_nodes,
dirty_canvas,
|node, try_traverse_children| {
let node = rdom.get(node);
if let Some(node) = node {
Expand Down Expand Up @@ -65,28 +63,27 @@ pub fn process_render(
layers,
);

let layers = layers.layers();
let dirty_layers = dirty_layers.layers();

dirty_canvas.save();

dirty_canvas.clip_rect(
Rect::new(
dirty_area.min_x(),
dirty_area.min_y(),
dirty_area.max_x(),
dirty_area.max_y(),
),
Some(ClipOp::Intersect),
false,
);
dirty_canvas.clear(Color::TRANSPARENT);
if let Some(dirty_area) = dirty_area {
dirty_canvas.clip_rect(
Rect::new(
dirty_area.min_x(),
dirty_area.min_y(),
dirty_area.max_x(),
dirty_area.max_y(),
),
Some(ClipOp::Intersect),
false,
);
dirty_canvas.clear(Color::WHITE);
}

let mut painted = Vec::new();

// Render the dirty nodes
for layer in sorted(dirty_layers.keys().copied().collect::<Vec<i16>>()) {
let nodes = layers.get(&layer).unwrap();
for (_, nodes) in sorted(dirty_layers.iter()) {
'elements: for node_id in nodes {
let node = rdom.get(*node_id).unwrap();
let node_viewports = node.get::<ViewportState>().unwrap();
Expand All @@ -108,9 +105,6 @@ pub fn process_render(
}
}

println!("{painted:?}");

dirty_canvas.restore();
canvas.clear(Color::TRANSPARENT);
dirty_surface.draw(canvas, (0, 0), SamplingOptions::default(), None);
}
13 changes: 5 additions & 8 deletions crates/renderer/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,21 +151,18 @@ impl Application {

self.provide_vdom_contexts(app_state);

self.sdom
.get_mut()
.init_dom(&mut self.vdom, scale_factor, &mut self.compositor);
self.sdom.get_mut().init_dom(&mut self.vdom, scale_factor);
self.plugins.send(PluginEvent::FinishedUpdatingDOM);
}

/// Update the RealDOM, layout and others with the latest changes from the VirtualDOM
pub fn render_mutations(&mut self, scale_factor: f32) -> (bool, bool) {
self.plugins.send(PluginEvent::StartedUpdatingDOM);

let (repaint, relayout) = self.sdom.get_mut().render_mutations(
&mut self.vdom,
scale_factor,
&mut self.compositor,
);
let (repaint, relayout) = self
.sdom
.get_mut()
.render_mutations(&mut self.vdom, scale_factor);

self.plugins.send(PluginEvent::FinishedUpdatingDOM);

Expand Down
10 changes: 6 additions & 4 deletions crates/renderer/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,10 +416,12 @@ impl<'a, State: Clone> ApplicationHandler<EventMessage> for DesktopRenderer<'a,
*surface =
create_surface(window, *fb_info, gr_context, *num_samples, *stencil_size);

*dirty_surface = surface.new_surface_with_dimensions((
size.width.try_into().expect("Could not convert width"),
size.height.try_into().expect("Could not convert height"),
)).unwrap();
*dirty_surface = surface
.new_surface_with_dimensions((
size.width.try_into().expect("Could not convert width"),
size.height.try_into().expect("Could not convert height"),
))
.unwrap();

gl_surface.resize(
gl_context,
Expand Down
16 changes: 6 additions & 10 deletions crates/testing/src/test_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,7 @@ impl TestingHandler {
self.provide_vdom_contexts();
let sdom = self.utils.sdom();
let mut fdom = sdom.get();
fdom.init_dom(
&mut self.vdom,
SCALE_FACTOR as f32,
&mut Compositor::default(),
);
fdom.init_dom(&mut self.vdom, SCALE_FACTOR as f32);
}

/// Get a mutable reference to the current [`TestingConfig`].
Expand Down Expand Up @@ -189,11 +185,11 @@ impl TestingHandler {
.await
.ok();

let (must_repaint, must_relayout) = self.utils.sdom().get_mut().render_mutations(
&mut self.vdom,
SCALE_FACTOR as f32,
&mut Compositor::default(),
);
let (must_repaint, must_relayout) = self
.utils
.sdom()
.get_mut()
.render_mutations(&mut self.vdom, SCALE_FACTOR as f32);

self.wait_for_work(self.config.size());

Expand Down
9 changes: 0 additions & 9 deletions crates/torin/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ pub trait AreaModel {
);

fn adjust_size(&mut self, node: &Node);

fn area_intersects(&self, other: &Self) -> bool;
}

impl AreaModel for Area {
Expand Down Expand Up @@ -179,13 +177,6 @@ impl AreaModel for Area {
self.size.height *= p.get() / 100.;
}
}

fn area_intersects(&self, other: &Self) -> bool {
(self.min_x() <= other.max_x() && (self.max_x() <= other.min_x()))
|| (self.max_x() <= other.min_x() && (self.min_x() <= other.max_x()))
|| (self.min_y() <= other.max_y() && (self.max_y() <= other.min_y()))
|| (self.max_y() <= other.min_y() && (self.min_y() <= other.max_y()))
}
}

pub fn get_align_axis(
Expand Down

0 comments on commit 2961956

Please sign in to comment.