Skip to content

Commit 13ad814

Browse files
0HyperCubeKeavon
andauthored
Add a complexity limit on displaying layer thumbnails to improve performance (#2828)
* Skip complex layer thumbnails * Set Raster<GPU> to have usize::MAX render complexity * Code review --------- Co-authored-by: Keavon Chambers <[email protected]>
1 parent ceffcfd commit 13ad814

File tree

4 files changed

+77
-2
lines changed

4 files changed

+77
-2
lines changed

editor/src/node_graph_executor/runtime.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,18 @@ impl NodeRuntime {
328328
return;
329329
}
330330

331+
// Skip thumbnails if the layer is too complex (for performance)
332+
if graphic_element.render_complexity() > 1000 {
333+
let old = thumbnail_renders.insert(parent_network_node_id, Vec::new());
334+
if old.is_none_or(|v| !v.is_empty()) {
335+
responses.push_back(FrontendMessage::UpdateNodeThumbnail {
336+
id: parent_network_node_id,
337+
value: "<svg viewBox=\"0 0 10 10\"><title>Dense thumbnail omitted for performance</title><line x1=\"0\" y1=\"10\" x2=\"10\" y2=\"0\" stroke=\"red\" /></svg>".to_string(),
338+
});
339+
}
340+
return;
341+
}
342+
331343
let bounds = graphic_element.bounding_box(DAffine2::IDENTITY, true);
332344

333345
// Render the thumbnail from a `GraphicElement` into an SVG string

node-graph/gcore/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub mod ops;
2222
pub mod raster;
2323
pub mod raster_types;
2424
pub mod registry;
25+
pub mod render_complexity;
2526
pub mod structural;
2627
pub mod text;
2728
pub mod transform;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use crate::instances::Instances;
2+
use crate::raster_types::{CPU, GPU, Raster};
3+
use crate::vector::VectorData;
4+
use crate::{Artboard, Color, GraphicElement};
5+
use glam::DVec2;
6+
7+
pub trait RenderComplexity {
8+
fn render_complexity(&self) -> usize {
9+
0
10+
}
11+
}
12+
13+
impl<T: RenderComplexity> RenderComplexity for Instances<T> {
14+
fn render_complexity(&self) -> usize {
15+
self.instance_ref_iter().map(|instance| instance.instance.render_complexity()).fold(0, usize::saturating_add)
16+
}
17+
}
18+
19+
impl RenderComplexity for Artboard {
20+
fn render_complexity(&self) -> usize {
21+
self.graphic_group.render_complexity()
22+
}
23+
}
24+
25+
impl RenderComplexity for GraphicElement {
26+
fn render_complexity(&self) -> usize {
27+
match self {
28+
Self::GraphicGroup(instances) => instances.render_complexity(),
29+
Self::VectorData(instances) => instances.render_complexity(),
30+
Self::RasterDataCPU(instances) => instances.render_complexity(),
31+
Self::RasterDataGPU(instances) => instances.render_complexity(),
32+
}
33+
}
34+
}
35+
36+
impl RenderComplexity for VectorData {
37+
fn render_complexity(&self) -> usize {
38+
self.segment_domain.ids().len()
39+
}
40+
}
41+
42+
impl RenderComplexity for Raster<CPU> {
43+
fn render_complexity(&self) -> usize {
44+
(self.width * self.height / 500) as usize
45+
}
46+
}
47+
48+
impl RenderComplexity for Raster<GPU> {
49+
fn render_complexity(&self) -> usize {
50+
// GPU textures currently can't have a thumbnail
51+
usize::MAX
52+
}
53+
}
54+
55+
impl RenderComplexity for String {}
56+
impl RenderComplexity for bool {}
57+
impl RenderComplexity for f32 {}
58+
impl RenderComplexity for f64 {}
59+
impl RenderComplexity for DVec2 {}
60+
impl RenderComplexity for Option<Color> {}
61+
impl RenderComplexity for Vec<Color> {}

node-graph/gsvg-renderer/src/renderer.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use graphene_core::instances::Instance;
1010
use graphene_core::math::quad::Quad;
1111
use graphene_core::raster::Image;
1212
use graphene_core::raster_types::{CPU, GPU, RasterDataTable};
13+
use graphene_core::render_complexity::RenderComplexity;
1314
use graphene_core::transform::{Footprint, Transform};
1415
use graphene_core::uuid::{NodeId, generate_uuid};
1516
use graphene_core::vector::VectorDataTable;
@@ -204,7 +205,7 @@ pub struct RenderMetadata {
204205
}
205206

206207
// TODO: Rename to "Graphical"
207-
pub trait GraphicElementRendered: BoundingBox {
208+
pub trait GraphicElementRendered: BoundingBox + RenderComplexity {
208209
fn render_svg(&self, render: &mut SvgRender, render_params: &RenderParams);
209210

210211
#[cfg(feature = "vello")]
@@ -1151,7 +1152,7 @@ impl GraphicElementRendered for GraphicElement {
11511152
}
11521153

11531154
/// Used to stop rust complaining about upstream traits adding display implementations to `Option<Color>`. This would not be an issue as we control that crate.
1154-
trait Primitive: std::fmt::Display + BoundingBox {}
1155+
trait Primitive: std::fmt::Display + BoundingBox + RenderComplexity {}
11551156
impl Primitive for String {}
11561157
impl Primitive for bool {}
11571158
impl Primitive for f32 {}

0 commit comments

Comments
 (0)