use std::{borrow::Cow, sync::Arc};
use cranpose_core::{NodeId, SceneUpdate};
use cranpose_render_common::{
graph::{CachePolicy, ProjectiveTransform},
layer_composition::{layer_composite_params, local_content_layer_for},
layer_shadow::ShadowLight,
layer_transform::layer_uniform_scale,
primitive_emit::resolve_clip,
scene_store::{self, ItemRef, LayerRef},
space::SeenDoor,
};
use cranpose_ui_graphics::{
BlendMode, CompositingStrategy, GraphicsLayer, Point, Rect, RenderEffect,
space::{Camera, Light, MeshSurface},
};
use crate::{
pipeline::{layer_casts_shadow, push_layer_shadow},
scene::{
BackdropLayer, ClipRounding, CompositorScene, ContentContext, LayerRoundedClip, RunDraw,
SceneCapacityHint, SnapAnchor, clip_holds,
},
target_state::Cull,
};
mod backdrop_space;
mod layer_motion;
mod layer_scene_recycler;
mod push_content;
mod raster_scale;
mod rigid_snap;
mod rounded_clip_corners;
mod shown;
mod space_children;
use backdrop_space::{detach_flat_backdrop, with_backdrop_in_own_space};
pub(crate) use layer_motion::LayerMotion;
pub(crate) use layer_scene_recycler::LayerSceneRecycler;
use push_content::{assign_shadow_anchor, push_content};
pub(crate) use push_content::{assign_snap_anchor_since, scene_counts};
use raster_scale::RasterScale;
use rigid_snap::{layer_has_pixel_sensitive_subtree, rigid_snap_anchor};
use rounded_clip_corners::{
content_admits_rounded_clip, content_takes_rounded_clip, rounded_clip_for_layer,
};
pub(crate) use shown::{ShownArea, ShownRecord};
use shown::{ShownSpace, culled, record_drawn, record_hidden, surface_space};
use space_children::collect_space_children;
const AFFINE_TOLERANCE: f32 = 1e-4;
pub(crate) struct LayerScene {
pub(crate) scene: CompositorScene,
pub(crate) children: Vec<ChildLayer>,
has_backdrop: bool,
pub(crate) shown: Vec<ShownRecord>,
}
impl LayerScene {
pub(crate) fn new(scene: CompositorScene, children: Vec<ChildLayer>) -> Self {
let mut layer = Self {
scene,
children,
has_backdrop: false,
shown: Vec::new(),
};
layer.refresh_backdrop_summary();
layer
}
pub(crate) fn contains_backdrop(&self) -> bool {
self.has_backdrop
}
fn refresh_backdrop_summary(&mut self) {
self.has_backdrop = self
.scene
.backdrop_layers
.iter()
.any(|layer| layer.alpha != 0.0)
|| self.children.iter().any(ChildLayer::reads_backdrop);
}
}
pub(crate) struct ChildLayer {
pub(crate) z_index: usize,
pub(crate) node_id: Option<NodeId>,
pub(crate) local_bounds: Rect,
pub(crate) transform: ProjectiveTransform,
pub(crate) clip: Option<Rect>,
pub(crate) rounded_clip: Option<LayerRoundedClip>,
pub(crate) alpha: f32,
pub(crate) blend_mode: BlendMode,
pub(crate) effect: Option<RenderEffect>,
pub(crate) backdrop: Option<RenderEffect>,
pub(crate) backdrop_alpha: f32,
pub(crate) snap_anchor: Option<SnapAnchor>,
pub(crate) surface_scale: f32,
pub(crate) content_hash: u64,
pub(crate) cache_policy: CachePolicy,
pub(crate) in_place: bool,
pub(crate) content: Box<LayerScene>,
pub(crate) mesh: Option<Arc<MeshChild>>,
}
pub(crate) struct SceneDraw {
pub(crate) camera: Camera,
pub(crate) light: Option<Light>,
pub(crate) rect: Rect,
pub(crate) clip: Option<Rect>,
pub(crate) seen: Vec<SeenDraw>,
}
pub(crate) struct SeenDraw {
pub(crate) through: Option<usize>,
pub(crate) light: Option<Light>,
pub(crate) door: Option<SeenDoor>,
}
pub(crate) struct MeshChild {
pub(crate) scene: Arc<SceneDraw>,
pub(crate) surface: MeshSurface,
pub(crate) content: Rect,
pub(crate) seen: usize,
}
impl ChildLayer {
pub(crate) fn reads_backdrop(&self) -> bool {
(self.backdrop.is_some() && self.backdrop_alpha != 0.0) || self.content.contains_backdrop()
}
}
#[derive(Clone, Copy)]
struct WalkContext<'s> {
offset: Point,
visual_clip: Option<Rect>,
rounding: Option<ClipRounding>,
snap_anchor: Option<SnapAnchor>,
translated: bool,
settings: &'s WalkSettings,
cull: Option<&'s Cull<'s>>,
}
#[derive(Clone, Copy)]
struct WalkSettings {
raster_scale: RasterScale,
light: ShadowLight,
wants_pixel_sensitive: bool,
update: SceneUpdate,
in_space: bool,
shown: Option<ShownSpace>,
}
pub(crate) fn direct_translation(transform: ProjectiveTransform) -> Option<Point> {
uniform_scale_translation(transform)
.filter(|(scale, _)| (scale - 1.0).abs() <= AFFINE_TOLERANCE)
.map(|(_, translation)| translation)
}
pub(crate) fn uniform_scale_translation(transform: ProjectiveTransform) -> Option<(f32, Point)> {
let matrix = transform.matrix();
let scale = matrix[0][0];
if scale <= 0.0
|| (matrix[1][1] - scale).abs() > AFFINE_TOLERANCE
|| matrix[0][1].abs() > AFFINE_TOLERANCE
|| matrix[1][0].abs() > AFFINE_TOLERANCE
|| matrix[2][0].abs() > AFFINE_TOLERANCE
|| matrix[2][1].abs() > AFFINE_TOLERANCE
|| (matrix[2][2] - 1.0).abs() > AFFINE_TOLERANCE
{
return None;
}
Some((scale, Point::new(matrix[0][2], matrix[1][2])))
}
pub(crate) fn similarity_scale(transform: ProjectiveTransform) -> Option<f32> {
let [[a, b, _], [c, d, _], perspective] = transform.matrix();
let near = |value: f32, target: f32| (value - target).abs() <= AFFINE_TOLERANCE;
let squared = a * a + c * c;
(near(perspective[0], 0.0)
&& near(perspective[1], 0.0)
&& near(perspective[2], 1.0)
&& squared.is_normal()
&& near((b * b + d * d) / squared, 1.0)
&& near((a * b + c * d) / squared, 0.0))
.then(|| squared.sqrt())
}
pub(crate) fn in_place_content_scale(scale: f32, raster_scale: f32) -> Option<f32> {
if (scale - 1.0).abs() <= AFFINE_TOLERANCE {
Some(1.0)
} else {
((scale - raster_scale).abs() <= AFFINE_TOLERANCE).then_some(raster_scale)
}
}
fn can_draw_in_place(
layer: LayerRef<'_>,
transform: ProjectiveTransform,
raster_scale: f32,
content: &LayerScene,
) -> bool {
similarity_scale(transform)
.and_then(|scale| in_place_content_scale(scale, raster_scale))
.is_some()
&& !layer.needs_surface()
&& layer.backdrop().is_none()
&& rounded_clip_for_layer(layer).is_none()
&& content_draws_in_place(content)
}
fn content_draws_in_place(content: &LayerScene) -> bool {
let scene = &content.scene;
let whole =
|rect: Rect, clip: Option<Rect>| clip.is_none_or(|clip| clip.intersect(rect) == Some(rect));
let run_draws_alike = |run: &RunDraw| {
!run.placement.rounding_cut()
&& run
.segment_records()
.all(|segment| segment.blend == BlendMode::SrcOver)
};
scene.backdrop_layers.is_empty()
&& scene.effect_layers.is_empty()
&& scene.runs.iter().all(run_draws_alike)
&& scene.shadow_draws.iter().all(|shadow| {
!shadow.requires_surface()
&& shadow.texts.iter().all(|text| whole(text.rect, text.clip))
})
&& scene
.rrect_shadows
.iter()
.all(|shadow| whole(shadow.shadow.bounds, shadow.clip))
&& scene.images.is_empty()
&& content
.children
.iter()
.all(|child| child.in_place && child.clip.is_none())
}
#[derive(Clone, Copy)]
struct Visit<'a> {
layer: LayerRef<'a>,
graphics: &'a GraphicsLayer,
bounds: Rect,
}
impl<'a> Visit<'a> {
#[expect(
clippy::inline_always,
reason = "the walk makes a visit of every record; out of line the gauntlet frame took 4.5 M more instructions"
)]
#[inline(always)]
fn of(layer: LayerRef<'a>) -> Self {
Self {
layer,
graphics: layer.graphics_layer(),
bounds: layer.local_bounds(),
}
}
#[expect(
clippy::inline_always,
reason = "the walk reads the clip of every record it visits; out of line the gauntlet frame took 2.5 M more instructions"
)]
#[inline(always)]
fn visual_clip(&self) -> Option<Rect> {
self.layer.visual_clip_of(self.graphics, self.bounds)
}
}
#[derive(Clone, Copy)]
enum Placement {
Direct(Point),
DirectRounded(Point, f32),
Isolated,
}
fn child_placement(layer: LayerRef<'_>, raster_scale: RasterScale) -> Placement {
let translation = match layer.placement() {
scene_store::Placement::At(origin) => Some(origin),
scene_store::Placement::By(transform) => direct_translation(transform),
};
let Some(translation) = translation else {
return Placement::Isolated;
};
if layer.needs_surface() {
return Placement::Isolated;
}
match rounded_clip_for_layer(layer) {
Some(clip) if !content_admits_rounded_clip(layer, clip, raster_scale) => {
content_takes_rounded_clip(layer, clip, raster_scale)
.map_or(Placement::Isolated, |radius| {
Placement::DirectRounded(translation, radius)
})
}
_ => Placement::Direct(translation),
}
}
fn rounding_within(layer_clip: Option<Rect>, context: &WalkContext<'_>) -> Option<ClipRounding> {
let keeps = match (layer_clip, context.visual_clip) {
(None, _) => true,
(Some(own), Some(rounded)) => clip_holds(own, rounded),
(Some(_), None) => false,
};
context.rounding.filter(|_| keeps)
}
pub(crate) fn collect_root(
root: LayerRef<'_>,
(update, shown): (SceneUpdate, Option<&mut Vec<ShownRecord>>),
cull: Option<&Cull<'_>>,
motion: &mut LayerMotion,
capacity: SceneCapacityHint,
root_scale: f32,
recycler: &mut LayerSceneRecycler,
) -> Box<LayerScene> {
let mut out = recycler.take(capacity);
let shown_space = shown.is_some().then_some(ShownSpace::ROOT);
if let Some(shown) = shown {
shown.clear();
out.shown = std::mem::take(shown);
}
let settings = WalkSettings {
raster_scale: RasterScale::Exact(root_scale),
light: ShadowLight::for_window(
root.local_bounds().width,
root.local_bounds().height,
1.0,
root_scale,
),
wants_pixel_sensitive: false,
update,
in_space: false,
shown: shown_space,
};
let context = WalkContext {
offset: Point::default(),
visual_clip: None,
rounding: None,
snap_anchor: None,
translated: false,
settings: &settings,
cull,
};
collect_child(root, motion, &context, &mut out, recycler);
out.scene.flush_loose();
out.refresh_backdrop_summary();
match cull {
Some(_) => motion.end_culled_frame(root.store()),
None => motion.end_frame(),
}
out
}
pub(crate) fn collect_overlay(
root: LayerRef<'_>,
(root_scale, shown): (f32, Option<&mut Vec<ShownRecord>>),
recycler: &mut LayerSceneRecycler,
) -> Box<LayerScene> {
collect_root(
root,
(root.store().update(), shown),
None,
&mut LayerMotion::default(),
SceneCapacityHint::default(),
root_scale,
recycler,
)
}
#[inline(never)]
fn push_backdrop_layer(
layer: LayerRef<'_>,
offset: Point,
context: &WalkContext<'_>,
scene: &mut CompositorScene,
) {
let Some(effect) = layer.backdrop() else {
return;
};
let local_layer = local_content_layer_for(layer.graphics_layer());
let rect = layer.local_bounds().translate(offset.x, offset.y);
let clip = resolve_clip(
context.visual_clip,
layer
.visual_clip_rect()
.map(|clip| clip.translate(offset.x, offset.y)),
);
let rounded_clip = rounded_clip_for_layer(layer).map(|clip| LayerRoundedClip {
rect: clip.rect.translate(offset.x, offset.y),
radii: clip.radii,
});
let snap_anchor = context
.snap_anchor
.or_else(|| rigid_snap_anchor(rect, &local_layer));
scene.push_backdrop_layer(BackdropLayer {
node_id: layer.node_id(),
alpha: local_layer.alpha,
rect,
clip,
reach: context.visual_clip,
rounded_clip,
snap_anchor,
effect: effect.clone(),
z_index: 0,
});
}
fn light_in_layer(layer: LayerRef<'_>, context: &WalkContext<'_>) -> ShadowLight {
let to_parent = layer
.transform_to_parent()
.then(ProjectiveTransform::translation(
context.offset.x,
context.offset.y,
));
let scale = uniform_scale_translation(to_parent).map_or_else(
|| layer_uniform_scale(layer.graphics_layer()),
|(scale, _)| scale,
);
let origin = to_parent.map_point(Point::default());
context.settings.light.in_space(origin.x, origin.y, scale)
}
fn content_key(layer: LayerRef<'_>, update: SceneUpdate) -> u64 {
const CHURN_SALT: u64 = 0x6a09_e667_f3bc_c909;
if layer.content_churns(update) {
let number = u64::from(update.since(SceneUpdate::default()));
return number.wrapping_mul(0x9e37_79b9_7f4a_7c15) ^ CHURN_SALT;
}
layer.target_content_hash()
}
fn surface_content(
visit: &Visit<'_>,
motion: &mut LayerMotion,
(settings, translated): (&WalkSettings, bool),
shown: &mut Vec<ShownRecord>,
recycler: &mut LayerSceneRecycler,
) -> (Box<LayerScene>, bool) {
let context = WalkContext {
offset: Point::default(),
visual_clip: None,
rounding: None,
snap_anchor: None,
translated,
settings,
cull: None,
};
let mut content = recycler.take(SceneCapacityHint::default());
std::mem::swap(&mut content.shown, shown);
let has_pixel_sensitive_subtree = collect_into(visit, motion, &context, &mut content, recycler);
std::mem::swap(&mut content.shown, shown);
content.scene.flush_loose();
content.refresh_backdrop_summary();
(content, has_pixel_sensitive_subtree)
}
fn isolated_child(
layer: LayerRef<'_>,
motion: &mut LayerMotion,
context: &WalkContext<'_>,
(mesh, shown): (Option<Arc<MeshChild>>, Option<ShownSpace>),
parent: &mut LayerScene,
recycler: &mut LayerSceneRecycler,
) -> (ChildLayer, bool) {
let visit = Visit::of(layer);
let graphics = visit.graphics;
let local_layer = local_content_layer_for(graphics);
let content_hash = content_key(layer, context.settings.update);
let nominal_scale = layer_uniform_scale(graphics);
let retained_scale = if layer.cache_policy() == CachePolicy::Auto {
motion.retained_scale(layer.node_id(), nominal_scale, content_hash)
} else {
nominal_scale
};
let mut raster_scale = context
.settings
.raster_scale
.within(nominal_scale, retained_scale);
if graphics.backdrop_effect.is_some()
&& uniform_scale_translation(layer.transform_to_parent()).is_none()
{
raster_scale = raster_scale.within(nominal_scale, retained_scale);
}
let content_settings = WalkSettings {
raster_scale,
light: light_in_layer(layer, context),
wants_pixel_sensitive: context.settings.wants_pixel_sensitive
|| (!context.translated && context.snap_anchor.is_none()),
update: context.settings.update,
in_space: context.settings.in_space || mesh.is_some(),
shown,
};
let translated = context.translated || layer.translated_content_context();
let (content, has_pixel_sensitive_subtree) = surface_content(
&visit,
motion,
(&content_settings, translated),
&mut parent.shown,
recycler,
);
let transform = layer
.transform_to_parent()
.then(ProjectiveTransform::translation(
context.offset.x,
context.offset.y,
));
let parent_bounds = transform.bounds_for_rect(visit.bounds);
let rigid = context.translated || has_pixel_sensitive_subtree;
let snap_anchor = context.snap_anchor.or_else(|| {
rigid
.then(|| rigid_snap_anchor(parent_bounds, &local_layer))
.flatten()
});
let alpha = if graphics.compositing_strategy == CompositingStrategy::ModulateAlpha {
1.0
} else {
GraphicsLayer::composite_alpha_8bit(graphics.alpha)
};
let cacheable = layer.cache_policy() == CachePolicy::Auto && !content.contains_backdrop();
let surface_scale = if cacheable {
retained_scale
} else {
nominal_scale
};
motion.record_scale(layer, surface_scale, content_hash);
let in_place = mesh.is_none() && can_draw_in_place(layer, transform, surface_scale, &content);
(
ChildLayer {
z_index: parent.scene.next_z(),
node_id: layer.node_id(),
local_bounds: visit.bounds,
transform,
clip: context.visual_clip,
rounded_clip: rounded_clip_for_layer(layer),
alpha,
blend_mode: graphics.blend_mode,
effect: graphics.render_effect.clone(),
backdrop: graphics.backdrop_effect.clone(),
backdrop_alpha: local_layer.alpha,
snap_anchor,
surface_scale,
content_hash,
cache_policy: layer.cache_policy(),
in_place,
content,
mesh,
},
has_pixel_sensitive_subtree,
)
}
fn collect_into(
visit: &Visit<'_>,
motion: &mut LayerMotion,
context: &WalkContext<'_>,
out: &mut LayerScene,
recycler: &mut LayerSceneRecycler,
) -> bool {
let layer = visit.layer;
let local_layer = local_content_layer_for(visit.graphics);
let layer_bounds = visit.bounds.translate(context.offset.x, context.offset.y);
let layer_clip = visit
.visual_clip()
.map(|clip| clip.translate(context.offset.x, context.offset.y));
let visual_clip = resolve_clip(context.visual_clip, layer_clip);
if visual_clip.is_some_and(|clip| clip.is_empty()) {
return context.settings.wants_pixel_sensitive && layer_has_pixel_sensitive_subtree(layer);
}
let rounding = rounding_within(layer_clip, context);
let translated = context.translated || layer.translated_content_context();
let allow_rigid_snap = translated || !layer.motion_context_animated();
let boundary_anchor = boundary_anchor(layer, context, (layer_bounds, &local_layer));
let translated_anchor = context.snap_anchor.or(boundary_anchor);
let layer_anchor = translated_anchor.or_else(|| {
(allow_rigid_snap && content_needs_rigid_snap(layer, translated))
.then(|| rigid_snap_anchor(layer_bounds, &local_layer))
.flatten()
});
let content = ContentContext {
layer_bounds: layer
.node_rect()
.translate(context.offset.x, context.offset.y),
local_layer: &local_layer,
visual_clip,
rounding,
anchor: layer_anchor,
motion_context_animated: layer.motion_context_animated() || translated,
};
let child_context = WalkContext {
offset: context.offset,
visual_clip,
rounding,
snap_anchor: translated_anchor,
translated,
settings: context.settings,
cull: context.cull,
};
if context.settings.in_space || visit.graphics.space.is_some() {
return collect_space_children(layer, motion, (&child_context, &content), out, recycler);
}
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) => {
collect_child(child_layer, motion, &child_context, out, recycler)
}
ItemRef::Leaf(inline) => collect_inline(inline, content_offset, &child_context, out),
ItemRef::Draw(_) | ItemRef::Text(_) | ItemRef::Run(_) | ItemRef::Shape(_) => {
push_content(out, child, &content);
false
}
};
}
has_pixel_sensitive_subtree
}
fn boundary_anchor(
layer: LayerRef<'_>,
context: &WalkContext<'_>,
(layer_bounds, local_layer): (Rect, &Cow<'_, GraphicsLayer>),
) -> Option<SnapAnchor> {
if context.translated || !layer.translated_content_context() {
return None;
}
rigid_snap_anchor(
layer_bounds.translate(
layer.translated_content_offset().x,
layer.translated_content_offset().y,
),
local_layer,
)
}
fn collect_child(
child: LayerRef<'_>,
motion: &mut LayerMotion,
context: &WalkContext<'_>,
out: &mut LayerScene,
recycler: &mut LayerSceneRecycler,
) -> bool {
let Some(cull) = culled(child, context.cull) else {
return false;
};
let visit = Visit::of(child);
let composite = layer_composite_params(visit.graphics);
if composite.is_some_and(|(alpha, blend)| alpha == 0.0 && blend == BlendMode::SrcOver) {
record_hidden(child, context, out);
return context.settings.wants_pixel_sensitive && layer_has_pixel_sensitive_subtree(child);
}
let (translation, radius) = match child_placement(child, context.settings.raster_scale) {
Placement::Direct(translation) => (translation, None),
Placement::DirectRounded(translation, radius) => (translation, Some(radius)),
Placement::Isolated => {
return collect_isolated_child(child, motion, context, None, out, recycler);
}
};
let child_offset = Point::new(
context.offset.x + translation.x,
context.offset.y + translation.y,
);
let child_bounds = visit.bounds.translate(child_offset.x, child_offset.y);
if clipped_away(&visit, child_bounds, context.visual_clip) {
record_hidden(child, context, out);
return context.settings.wants_pixel_sensitive && layer_has_pixel_sensitive_subtree(child);
}
let child_anchor = context.snap_anchor.or_else(|| {
context
.translated
.then(|| rigid_snap_anchor(child_bounds, &local_content_layer_for(visit.graphics)))
.flatten()
});
let shadows_from = out.scene.rrect_shadows.len();
if layer_casts_shadow(visit.graphics) {
push_direct_layer_shadow(
child,
(child_offset, child_bounds),
child_anchor,
context,
&mut out.scene,
);
}
record_drawn(child, context, (false, shadows_from), out);
let (visual_clip, rounding) = match radius {
Some(radius) => {
let clip = visit
.visual_clip()
.map(|clip| clip.translate(child_offset.x, child_offset.y));
(
resolve_clip(context.visual_clip, clip),
clip.map(|rect| ClipRounding { rect, radius }),
)
}
None => (context.visual_clip, context.rounding),
};
let child_context = WalkContext {
offset: child_offset,
visual_clip,
rounding,
snap_anchor: child_anchor,
cull,
..*context
};
if visit.graphics.backdrop_effect.is_some() {
push_backdrop_layer(child, child_offset, &child_context, &mut out.scene);
}
collect_into(&visit, motion, &child_context, out, recycler)
}
fn collect_inline(
inline: LayerRef<'_>,
parent_content_offset: Point,
context: &WalkContext<'_>,
out: &mut LayerScene,
) -> bool {
if culled(inline, context.cull).is_none() {
return false;
}
let placement = inline.origin_in_parent();
let origin = Point::new(
placement.x + parent_content_offset.x,
placement.y + parent_content_offset.y,
);
let offset = Point::new(context.offset.x + origin.x, context.offset.y + origin.y);
let bounds = inline.node_rect().translate(offset.x, offset.y);
if context
.visual_clip
.is_some_and(|clip| outside_clip(bounds, clip) && inline.draws_within_bounds())
{
record_hidden(inline, context, out);
return context.settings.wants_pixel_sensitive && inline.own_draw_facts().pixel_sensitive;
}
record_drawn(inline, context, (false, out.scene.rrect_shadows.len()), out);
let translated = context.translated;
let rigid = || rigid_snap_anchor(bounds, &GraphicsLayer::DEFAULT);
let inherited = context
.snap_anchor
.or_else(|| translated.then(rigid).flatten());
let anchor = inherited.or_else(|| {
((translated || !inline.motion_context_animated())
&& content_needs_rigid_snap(inline, translated))
.then(rigid)
.flatten()
});
let content = ContentContext {
layer_bounds: bounds,
local_layer: &GraphicsLayer::DEFAULT,
visual_clip: context.visual_clip,
rounding: context.rounding,
anchor,
motion_context_animated: inline.motion_context_animated() || translated,
};
for node in inline.items() {
push_content(out, node, &content);
}
context.settings.wants_pixel_sensitive && inline.own_draw_facts().pixel_sensitive
}
#[inline(never)]
fn push_direct_layer_shadow(
child: LayerRef<'_>,
(child_offset, child_bounds): (Point, Rect),
child_anchor: Option<SnapAnchor>,
context: &WalkContext<'_>,
scene: &mut CompositorScene,
) {
let shadow_clip = resolve_clip(
context.visual_clip,
child
.shadow_clip()
.map(|clip| clip.translate(child_offset.x, child_offset.y)),
);
let shadows_before = scene.rrect_shadows.len();
push_layer_shadow(
scene,
child.graphics_layer(),
child_bounds,
child_bounds,
shadow_clip,
context.settings.light,
);
assign_shadow_anchor(scene, shadows_before, child_anchor);
}
#[inline(never)]
fn collect_isolated_child(
child: LayerRef<'_>,
motion: &mut LayerMotion,
context: &WalkContext<'_>,
mesh: Option<Arc<MeshChild>>,
out: &mut LayerScene,
recycler: &mut LayerSceneRecycler,
) -> bool {
let transform = child
.transform_to_parent()
.then(ProjectiveTransform::translation(
context.offset.x,
context.offset.y,
));
let child_bounds = transform.bounds_for_rect(child.local_bounds());
let shadow_clip = resolve_clip(
context.visual_clip,
child
.shadow_clip()
.map(|clip| transform.bounds_for_rect(clip)),
);
let shadows_before = out.scene.rrect_shadows.len();
push_layer_shadow(
&mut out.scene,
child.graphics_layer(),
child.local_bounds(),
child_bounds,
shadow_clip,
context.settings.light,
);
let shown = surface_space(child, transform, context);
let (isolated, has_pixel_sensitive_subtree) =
isolated_child(child, motion, context, (mesh, shown), out, recycler);
let mut isolated = with_backdrop_in_own_space(isolated, recycler);
assign_shadow_anchor(&mut out.scene, shadows_before, isolated.snap_anchor);
detach_flat_backdrop(&mut isolated, &mut out.scene);
out.children.push(isolated);
out.scene.next_z += 1;
record_drawn(child, context, (shown.is_none(), shadows_before), out);
direct_translation(child.transform_to_parent()).is_some() && has_pixel_sensitive_subtree
}
fn clipped_away(child: &Visit<'_>, bounds: Rect, clip: Option<Rect>) -> bool {
clip.is_some_and(|clip| {
outside_clip(bounds, clip)
&& child.graphics.shadow_elevation <= 0.0
&& child.layer.draws_within_bounds()
})
}
fn outside_clip(bounds: Rect, clip: Rect) -> bool {
let slack = cranpose_render_common::graph::CONTAINED_DRAW_SLACK;
Rect {
x: bounds.x - slack,
y: bounds.y - slack,
width: bounds.width + slack * 2.0,
height: bounds.height + slack * 2.0,
}
.intersect(clip)
.is_none()
}
fn content_needs_rigid_snap(layer: LayerRef<'_>, translated: bool) -> bool {
let facts = layer.own_draw_facts();
facts.pixel_sensitive || (translated && facts.drawn)
}
#[cfg(test)]
#[path = "tests/collect_tests.rs"]
mod tests;