use std::sync::Arc;
use cranpose_render_common::{
scene_store::{ItemRef, LayerRef},
space::{EyeView, SeenSurface, SpaceTree},
};
use super::{
LayerMotion, LayerScene, LayerSceneRecycler, MeshChild, MeshChildren, SceneDraw, SeenDraw,
WalkContext, WalkSettings, collect_child, collect_inline, collect_isolated_child, push_content,
};
use crate::scene::ContentContext;
fn window_scene<'a>(
layer: LayerRef<'a>,
context: &WalkContext<'_>,
) -> Option<(Arc<SceneDraw>, EyeView<'a>)> {
let tree = SpaceTree::of(layer)?;
let bounds = layer.local_bounds();
let view = EyeView::of(&tree, bounds.size());
let scene = SceneDraw {
camera: view.camera,
light: view.seen[0].light,
rect: bounds.translate(context.offset.x, context.offset.y),
clip: context.visual_clip,
seen: view
.seen
.iter()
.map(|seen| SeenDraw {
through: seen.through,
light: seen.light,
door: seen.door,
})
.collect(),
multisample: tree.cells[0].scene.multisample,
};
Some((Arc::new(scene), view))
}
#[cold]
#[inline(never)]
pub(super) fn collect_space_children(
layer: LayerRef<'_>,
motion: &mut LayerMotion,
(context, content): (&WalkContext<'_>, &ContentContext<'_>),
out: &mut LayerScene,
recycler: &mut LayerSceneRecycler,
) -> bool {
let mut pending = (!context.settings.in_space)
.then(|| window_scene(layer, context))
.flatten();
let in_space = context.settings.in_space || pending.is_some();
let settings = WalkSettings {
in_space,
..*context.settings
};
let context = WalkContext {
settings: &settings,
cull: None,
..*context
};
let content_offset = layer.content_offset();
let mut has_pixel_sensitive_subtree =
context.settings.wants_pixel_sensitive && layer.own_draw_facts().pixel_sensitive;
for child in layer.items() {
has_pixel_sensitive_subtree |= match child {
ItemRef::Layer(child_layer) => {
if let Some((draw, view)) = pending.take_if(|_| child_layer.has_space()) {
collect_seen_surfaces(&draw, view, motion, &context, out, recycler);
}
!(in_space && child_layer.graphics_layer().space.is_some())
&& collect_child(child_layer, motion, &context, out, recycler)
}
ItemRef::Leaf(inline) => collect_inline(inline, content_offset, &context, out),
ItemRef::Draw(_) | ItemRef::Text(_) | ItemRef::Run(_) | ItemRef::Shape(_) => {
push_content(out, child, content);
false
}
};
}
has_pixel_sensitive_subtree
}
fn collect_seen_surfaces(
draw: &Arc<SceneDraw>,
view: EyeView<'_>,
motion: &mut LayerMotion,
context: &WalkContext<'_>,
out: &mut LayerScene,
recycler: &mut LayerSceneRecycler,
) {
let mesh_child = |seen: SeenSurface<'_>| {
Arc::new(MeshChild {
scene: Arc::clone(draw),
surface: seen.surface,
content: seen.content,
seen: seen.seen,
})
};
let mut surfaces = view.surfaces.into_iter().peekable();
while let Some(owner) = surfaces.next() {
let layer = owner.layer;
let mut meshes = MeshChildren::new();
meshes.push(mesh_child(owner));
while let Some(more) = surfaces.next_if(|next| next.mesh != 0) {
meshes.push(mesh_child(more));
}
collect_isolated_child(layer, motion, context, meshes, out, recycler);
}
}