1use std::sync::Arc;
4
5use waft_embedder::{Canvas, Clip, Matrix4, Offset, Path, RRect, Rect};
6use waft_foundation::{App, Handle};
7use waft_painting::{ClipContext, inverse_transform_rect};
8
9use crate::layer::{
10 AnyContainerLayer, AnyLayer, ClipPathLayer, ClipRRectLayer, ClipRectLayer, CompositionCallback,
11 ContainerLayer, ErasedLayer, LayerHandle, OffsetLayerMixin, OpacityLayer, PictureLayer,
12 TransformLayer,
13};
14use crate::object::AnyRenderObject;
15
16pub struct PaintingContext {
32 container_layer: AnyContainerLayer,
33 pub estimated_bounds: Rect,
41 canvas: Option<Canvas>,
42 is_complex_hint: bool,
43 will_change_hint: bool,
44}
45
46impl PaintingContext {
47 pub fn new(container_layer: AnyContainerLayer, estimated_bounds: Rect) -> PaintingContext {
52 PaintingContext {
53 container_layer,
54 estimated_bounds,
55 canvas: None,
56 is_complex_hint: false,
57 will_change_hint: false,
58 }
59 }
60
61 pub fn repaint_composited_child(app: &mut App, child: AnyRenderObject) {
68 debug_assert!(child.needs_paint(app));
69 Self::repaint_composited_child_inner(app, child, false, None);
70 }
71
72 fn repaint_composited_child_inner(
73 app: &mut App,
74 child: AnyRenderObject,
75 debug_also_painted_parent: bool,
76 child_context: Option<PaintingContext>,
77 ) {
78 debug_assert!(child.is_repaint_boundary(app));
79 let child_layer = if let Some(child_layer) =
80 child.layer(app).and_then(|layer| layer.as_offset_layer())
81 {
82 debug_assert!(debug_also_painted_parent || child_layer.as_layer().attached(app));
83 let debug_old_offset = if cfg!(debug_assertions) {
84 Some(child_layer.offset(app))
85 } else {
86 None
87 };
88 child_layer.as_container_layer().remove_all_children(app);
89 let updated_layer = child.update_composited_layer(app, Some(child_layer));
90 debug_assert_eq!(
91 updated_layer, child_layer,
92 "{child:?} created a new layer instance {updated_layer:?} instead of reusing the \
93 existing layer {child_layer:?}. See the documentation of RenderObject.updateCompositedLayer \
94 for more information on how to correctly implement this method."
95 );
96 debug_assert_eq!(debug_old_offset, Some(updated_layer.offset(app)));
97 child_layer
98 } else {
99 debug_assert!(debug_also_painted_parent);
100 debug_assert!(child.layer(app).is_none());
101
102 let layer = child.update_composited_layer(app, None);
107 LayerHandle::set_layer(
108 app,
109 |app| &mut child.data_mut(app).layer,
110 Some(layer.as_container_layer()),
111 );
112 layer
113 };
114 child.set_needs_composited_layer_update(app, false);
115 debug_assert_eq!(Some(child_layer.as_container_layer()), child.layer(app));
116 debug_assert!(
117 child
118 .layer(app)
119 .is_some_and(|layer| layer.as_offset_layer().is_some())
120 );
121
122 let mut child_context = child_context.unwrap_or_else(|| {
123 PaintingContext::new(child_layer.as_container_layer(), child.paint_bounds(app))
124 });
125 child.paint_with_context(app, &mut child_context, Offset::ZERO);
126
127 debug_assert_eq!(Some(child_layer.as_container_layer()), child.layer(app));
130 child_context.stop_recording_if_needed(app);
131 }
132
133 pub fn update_layer_properties(app: &mut App, child: AnyRenderObject) {
140 debug_assert!(child.is_repaint_boundary(app) && child.was_repaint_boundary(app));
141 debug_assert!(!child.needs_paint(app));
142 debug_assert!(child.layer(app).is_some());
143
144 let child_layer = child
145 .layer(app)
146 .expect("checked")
147 .as_offset_layer()
148 .expect("a repaint boundary's layer is an OffsetLayer");
149 let debug_old_offset = if cfg!(debug_assertions) {
150 Some(child_layer.offset(app))
151 } else {
152 None
153 };
154 let updated_layer = child.update_composited_layer(app, Some(child_layer));
155 debug_assert_eq!(
156 updated_layer, child_layer,
157 "{child:?} created a new layer instance {updated_layer:?} instead of reusing the \
158 existing layer {child_layer:?}. See the documentation of RenderObject.updateCompositedLayer \
159 for more information on how to correctly implement this method."
160 );
161 debug_assert_eq!(debug_old_offset, Some(updated_layer.offset(app)));
162 child.set_needs_composited_layer_update(app, false);
163 }
164
165 pub fn paint_child(&mut self, app: &mut App, child: AnyRenderObject, offset: Offset) {
171 if child.is_repaint_boundary(app) {
172 self.stop_recording_if_needed(app);
173 self.composite_child(app, child, offset);
174 } else if child.was_repaint_boundary(app) {
175 debug_assert!(
176 child
177 .layer(app)
178 .is_some_and(|layer| layer.as_offset_layer().is_some())
179 );
180 LayerHandle::set_layer(app, |app| &mut child.data_mut(app).layer, None);
181 child.paint_with_context(app, self, offset);
182 } else {
183 child.paint_with_context(app, self, offset);
184 }
185 }
186
187 fn composite_child(&mut self, app: &mut App, child: AnyRenderObject, offset: Offset) {
188 debug_assert!(!self.is_recording());
189 debug_assert!(child.is_repaint_boundary(app));
190
191 if child.needs_paint(app) || !child.was_repaint_boundary(app) {
192 Self::repaint_composited_child_inner(app, child, true, None);
193 } else if child.needs_composited_layer_update(app) {
194 Self::update_layer_properties(app, child);
195 }
196 debug_assert!(
197 child
198 .layer(app)
199 .is_some_and(|layer| layer.as_offset_layer().is_some())
200 );
201 let child_offset_layer = child
202 .layer(app)
203 .expect("checked")
204 .as_offset_layer()
205 .expect("a repaint boundary's layer is an OffsetLayer");
206 child_offset_layer.set_offset(app, offset);
207 self.append_layer(app, child_offset_layer.as_layer());
208 }
209
210 fn append_layer(&mut self, app: &mut App, layer: AnyLayer) {
217 debug_assert!(!self.is_recording());
218 layer.remove(app);
219 self.container_layer.append(app, layer);
220 }
221
222 fn is_recording(&self) -> bool {
223 self.canvas.is_some()
224 }
225
226 fn start_recording(&mut self) {
227 debug_assert!(!self.is_recording());
228 self.canvas = Some(Canvas::new());
229 self.is_complex_hint = false;
230 self.will_change_hint = false;
231 }
232
233 pub fn add_composition_callback(
236 &self,
237 app: &mut App,
238 callback: CompositionCallback,
239 ) -> Box<dyn FnOnce(&mut App)> {
240 self.container_layer
241 .as_layer()
242 .add_composition_callback(app, callback)
243 }
244
245 pub fn stop_recording_if_needed(&mut self, app: &mut App) {
256 if !self.is_recording() {
257 return;
258 }
259 let canvas = self.canvas.take().expect("recording");
260 let picture = PictureLayer::new(app, self.estimated_bounds);
261 picture.set_picture(app, Some(Arc::new(canvas.build())));
262 picture.set_is_complex_hint(app, self.is_complex_hint);
263 picture.set_will_change_hint(app, self.will_change_hint);
264 self.is_complex_hint = false;
265 self.will_change_hint = false;
266 self.container_layer.append(app, picture.as_layer());
267 }
268
269 pub fn set_is_complex_hint(&mut self) {
272 if self.canvas.is_none() {
273 self.start_recording();
274 }
275 self.is_complex_hint = true;
276 }
277
278 pub fn set_will_change_hint(&mut self) {
280 if self.canvas.is_none() {
281 self.start_recording();
282 }
283 self.will_change_hint = true;
284 }
285
286 pub fn add_layer(&mut self, app: &mut App, layer: AnyLayer) {
291 self.stop_recording_if_needed(app);
292 self.append_layer(app, layer);
293 }
294
295 pub fn push_layer(
316 &mut self,
317 app: &mut App,
318 child_layer: AnyContainerLayer,
319 painter: impl FnOnce(&mut App, &mut PaintingContext, Offset),
320 offset: Offset,
321 child_paint_bounds: Option<Rect>,
322 ) {
323 if child_layer.has_children(app) {
326 child_layer.remove_all_children(app);
327 }
328 self.stop_recording_if_needed(app);
329 self.append_layer(app, child_layer.as_layer());
330 let mut child_context = self.create_child_context(
331 child_layer,
332 child_paint_bounds.unwrap_or(self.estimated_bounds),
333 );
334
335 painter(app, &mut child_context, offset);
336 child_context.stop_recording_if_needed(app);
337 }
338
339 fn create_child_context(
343 &self,
344 child_layer: AnyContainerLayer,
345 bounds: Rect,
346 ) -> PaintingContext {
347 PaintingContext::new(child_layer, bounds)
348 }
349
350 #[allow(clippy::too_many_arguments)]
369 pub fn push_clip_rect(
370 &mut self,
371 app: &mut App,
372 needs_compositing: bool,
373 offset: Offset,
374 clip_rect: Rect,
375 painter: impl FnOnce(&mut App, &mut PaintingContext, Offset),
376 clip_behavior: Clip,
377 old_layer: Option<Handle<ClipRectLayer>>,
378 ) -> Option<Handle<ClipRectLayer>> {
379 if clip_behavior == Clip::None {
380 painter(app, self, offset);
381 return None;
382 }
383 let offset_clip_rect = clip_rect.shift(offset);
384 if needs_compositing {
385 let layer = old_layer.unwrap_or_else(|| ClipRectLayer::new(app));
386 layer.set_clip_rect(app, Some(offset_clip_rect));
387 layer.set_clip_behavior(app, clip_behavior);
388 self.push_layer(
389 app,
390 layer.as_container_layer(),
391 painter,
392 offset,
393 Some(offset_clip_rect),
394 );
395 Some(layer)
396 } else {
397 self.clip_rect_and_paint(
398 offset_clip_rect,
399 clip_behavior,
400 offset_clip_rect,
401 |context| painter(app, context, offset),
402 );
403 None
404 }
405 }
406
407 #[allow(clippy::too_many_arguments)]
409 pub fn push_clip_rrect(
410 &mut self,
411 app: &mut App,
412 needs_compositing: bool,
413 offset: Offset,
414 bounds: Rect,
415 clip_rrect: RRect,
416 painter: impl FnOnce(&mut App, &mut PaintingContext, Offset),
417 clip_behavior: Clip,
418 old_layer: Option<Handle<ClipRRectLayer>>,
419 ) -> Option<Handle<ClipRRectLayer>> {
420 if clip_behavior == Clip::None {
421 painter(app, self, offset);
422 return None;
423 }
424 let offset_bounds = bounds.shift(offset);
425 let offset_clip_rrect = clip_rrect.shift(offset);
426 if needs_compositing {
427 let layer = old_layer.unwrap_or_else(|| ClipRRectLayer::new(app));
428 layer.set_clip_rrect(app, Some(offset_clip_rrect));
429 layer.set_clip_behavior(app, clip_behavior);
430 self.push_layer(
431 app,
432 layer.as_container_layer(),
433 painter,
434 offset,
435 Some(offset_bounds),
436 );
437 Some(layer)
438 } else {
439 self.clip_rrect_and_paint(offset_clip_rrect, clip_behavior, offset_bounds, |context| {
440 painter(app, context, offset)
441 });
442 None
443 }
444 }
445
446 #[allow(clippy::too_many_arguments)]
448 pub fn push_clip_path(
449 &mut self,
450 app: &mut App,
451 needs_compositing: bool,
452 offset: Offset,
453 bounds: Rect,
454 clip_path: Path,
455 painter: impl FnOnce(&mut App, &mut PaintingContext, Offset),
456 clip_behavior: Clip,
457 old_layer: Option<Handle<ClipPathLayer>>,
458 ) -> Option<Handle<ClipPathLayer>> {
459 if clip_behavior == Clip::None {
460 painter(app, self, offset);
461 return None;
462 }
463 let offset_bounds = bounds.shift(offset);
464 let offset_clip_path = clip_path.shift(offset);
465 if needs_compositing {
466 let layer = old_layer.unwrap_or_else(|| ClipPathLayer::new(app));
467 layer.set_clip_path(app, Some(offset_clip_path));
468 layer.set_clip_behavior(app, clip_behavior);
469 self.push_layer(
470 app,
471 layer.as_container_layer(),
472 painter,
473 offset,
474 Some(offset_bounds),
475 );
476 Some(layer)
477 } else {
478 self.clip_path_and_paint(&offset_clip_path, clip_behavior, offset_bounds, |context| {
479 painter(app, context, offset)
480 });
481 None
482 }
483 }
484
485 pub fn push_transform(
494 &mut self,
495 app: &mut App,
496 needs_compositing: bool,
497 offset: Offset,
498 transform: Matrix4,
499 painter: impl FnOnce(&mut App, &mut PaintingContext, Offset),
500 old_layer: Option<Handle<TransformLayer>>,
501 ) -> Option<Handle<TransformLayer>> {
502 let (dx, dy) = (offset.dx() as f32, offset.dy() as f32);
503 let effective_transform = Matrix4::translation(dx, dy)
504 .then(&transform)
505 .then(&Matrix4::translation(-dx, -dy));
506 if needs_compositing {
507 let layer = old_layer.unwrap_or_else(|| TransformLayer::new(app));
508 layer.set_transform(app, effective_transform);
509 let child_paint_bounds =
510 inverse_transform_rect(effective_transform, self.estimated_bounds);
511 self.push_layer(
512 app,
513 layer.as_container_layer(),
514 painter,
515 offset,
516 Some(child_paint_bounds),
517 );
518 Some(layer)
519 } else {
520 self.canvas().save();
521 self.canvas().concat(&effective_transform);
522 painter(app, self, offset);
523 self.canvas().restore();
524 None
525 }
526 }
527
528 pub fn push_opacity(
537 &mut self,
538 app: &mut App,
539 offset: Offset,
540 alpha: i32,
541 painter: impl FnOnce(&mut App, &mut PaintingContext, Offset),
542 old_layer: Option<Handle<OpacityLayer>>,
543 ) -> Handle<OpacityLayer> {
544 let layer = old_layer.unwrap_or_else(|| OpacityLayer::new(app));
545 layer.set_alpha(app, alpha);
546 layer.set_offset(app, offset);
547 self.push_layer(app, layer.as_container_layer(), painter, Offset::ZERO, None);
548 layer
549 }
550}
551
552impl ClipContext for PaintingContext {
553 fn canvas(&mut self) -> &mut Canvas {
559 if self.canvas.is_none() {
560 self.start_recording();
561 }
562 self.canvas.as_mut().expect("recording")
563 }
564}
565
566#[cfg(test)]
567mod tests {
568 use std::cell::Cell;
569 use std::rc::Rc;
570 use waft_foundation::AppCell;
571
572 use waft_embedder::{Color, Effect, EngineLayer, Layer, Paint, SceneBuilder, Size};
573
574 use super::*;
575 use crate::box_::{AnyRenderBox, BoxConstraints, RenderBox, RenderBoxData};
576 use crate::layer::{AnyOffsetLayer, ContainerLayer, ErasedLayer, OpacityLayer};
577 use crate::object::{RenderHandle, RenderObject, RenderObjectData};
578 use waft_painting::AlignmentGeometry;
579
580 use crate::pipeline_owner::PipelineOwner;
581 use crate::testing::{EnginePhase, layout_with, pump_frame, test_pipeline_owner};
582
583 struct Leaf {
586 render_object: RenderObjectData,
587 render_box: RenderBoxData,
588 repaint_boundary: bool,
589 alpha: i32,
590 paints: Rc<Cell<u32>>,
591 }
592
593 impl RenderObject for Leaf {
594 crate::render_object_accessors!();
595
596 fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
597 let size = self.constraints(app).smallest();
598 self.set_size(app, size);
599 }
600
601 fn is_repaint_boundary(self: RenderHandle<Self>, app: &App) -> bool {
602 self.get(app).repaint_boundary
603 }
604
605 fn update_composited_layer(
606 self: RenderHandle<Self>,
607 app: &mut App,
608 old_layer: Option<AnyOffsetLayer>,
609 ) -> AnyOffsetLayer {
610 debug_assert!(self.is_repaint_boundary(app));
611 let layer = match old_layer {
612 Some(old) => app
613 .handle::<OpacityLayer>(old.id())
614 .expect("Leaf reuses an OpacityLayer"),
615 None => OpacityLayer::new(app),
616 };
617 layer.set_alpha(app, self.get(app).alpha);
618 layer.as_offset_layer()
619 }
620
621 fn paint(
622 self: RenderHandle<Self>,
623 app: &mut App,
624 context: &mut PaintingContext,
625 offset: Offset,
626 ) {
627 let paints = Rc::clone(&self.get(app).paints);
628 paints.set(paints.get() + 1);
629 let paint = Paint {
630 color: Color::from_argb(255, 0, 0, 0).into(),
631 ..Paint::default()
632 };
633 context.canvas().draw_rect(offset & self.size(app), &paint);
634 }
635 }
636
637 impl RenderBox for Leaf {
638 crate::render_box_accessors!();
639
640 fn paint_bounds(self: RenderHandle<Self>, app: &App) -> Rect {
642 self.render_box_data(app)
643 .size
644 .map_or(Rect::ZERO, |size| Offset::ZERO & size)
645 }
646 }
647
648 struct Parent {
650 render_object: RenderObjectData,
651 render_box: RenderBoxData,
652 child: Option<AnyRenderBox>,
653 paints_child: bool,
654 paints: Rc<Cell<u32>>,
655 }
656
657 impl RenderObject for Parent {
658 crate::render_object_accessors!();
659
660 fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
661 if let Some(child) = self.get(app).child {
662 child.layout(app, BoxConstraints::tight(Size::new(50.0, 50.0)), false);
663 }
664 self.set_size(app, Size::new(100.0, 100.0));
665 }
666
667 fn is_repaint_boundary(self: RenderHandle<Self>, _app: &App) -> bool {
668 true
669 }
670
671 fn visit_children(
672 self: RenderHandle<Self>,
673 app: &App,
674 visitor: &mut dyn FnMut(AnyRenderObject),
675 ) {
676 if let Some(child) = self.get(app).child {
677 visitor(child.as_object());
678 }
679 }
680
681 fn paint(
682 self: RenderHandle<Self>,
683 app: &mut App,
684 context: &mut PaintingContext,
685 offset: Offset,
686 ) {
687 let paints = Rc::clone(&self.get(app).paints);
688 paints.set(paints.get() + 1);
689 if let Some(child) = self.get(app).child
690 && self.get(app).paints_child
691 {
692 context.paint_child(app, child.as_object(), offset);
693 }
694 }
695 }
696
697 impl RenderBox for Parent {
698 crate::render_box_accessors!();
699 }
700
701 struct Tree {
702 parent: RenderHandle<Parent>,
703 leaf: RenderHandle<Leaf>,
704 parent_paints: Rc<Cell<u32>>,
705 leaf_paints: Rc<Cell<u32>>,
706 }
707
708 fn tree(app: &mut App, leaf_is_boundary: bool) -> Tree {
710 let leaf_paints = Rc::new(Cell::new(0));
711 let parent_paints = Rc::new(Cell::new(0));
712 let leaf = RenderHandle::new_box(
713 app,
714 Leaf {
715 render_object: RenderObjectData::new(),
716 render_box: RenderBoxData::new(),
717 repaint_boundary: leaf_is_boundary,
718 alpha: 255,
719 paints: Rc::clone(&leaf_paints),
720 },
721 );
722 let parent = RenderHandle::new_box(
723 app,
724 Parent {
725 render_object: RenderObjectData::new(),
726 render_box: RenderBoxData::new(),
727 child: Some(leaf.as_box()),
728 paints_child: true,
729 paints: Rc::clone(&parent_paints),
730 },
731 );
732 parent.adopt_child(app, leaf.as_object());
733 layout_with(
734 app,
735 parent.as_box(),
736 Some(BoxConstraints::tight(Size::new(100.0, 100.0))),
737 AlignmentGeometry::CENTER,
738 EnginePhase::Layout,
739 );
740 Tree {
741 parent,
742 leaf,
743 parent_paints,
744 leaf_paints,
745 }
746 }
747
748 fn paints(tree: &Tree) -> (u32, u32) {
749 (tree.parent_paints.get(), tree.leaf_paints.get())
750 }
751
752 fn leaf_layer_attached(app: &App, tree: &Tree) -> bool {
753 tree.leaf
754 .as_object()
755 .debug_layer(app)
756 .expect("leaf has a layer")
757 .as_layer()
758 .attached(app)
759 }
760
761 #[test]
763 fn nodes_needing_paint_updated_with_paint_changes() {
764 let cell = AppCell::new();
765 let mut app = cell.borrow_mut();
766 let owner = PipelineOwner::new(&mut app, None, None);
767 let node = RenderHandle::new_box(
768 &mut app,
769 Leaf {
770 render_object: RenderObjectData::new(),
771 render_box: RenderBoxData::new(),
772 repaint_boundary: true,
773 alpha: 255,
774 paints: Rc::new(Cell::new(0)),
775 },
776 );
777 node.as_object().attach(&mut app, owner.handle());
778 assert!(owner.nodes_needing_paint(&app).is_empty());
779
780 node.mark_needs_paint(&mut app);
781 let root = OpacityLayer::new(&mut app);
782 root.as_layer().attach(&mut app, node.as_object().id());
783 node.as_object()
784 .schedule_initial_paint(&mut app, root.as_container_layer());
785 assert!(owner.nodes_needing_paint(&app).contains(&node.as_object()));
786
787 owner.flush_compositing_bits(&mut app);
788 owner.flush_paint(&mut app);
789 assert!(owner.nodes_needing_paint(&app).is_empty());
790 }
791
792 #[test]
793 fn child_boundary_repaints_without_its_parent() {
794 let cell = AppCell::new();
795 let mut app = cell.borrow_mut();
796 let tree = tree(&mut app, true);
797 pump_frame(&mut app, EnginePhase::Paint);
798 assert_eq!(paints(&tree), (1, 1));
799 assert!(leaf_layer_attached(&app, &tree));
800 let parent_layer = tree.parent.as_object().debug_layer(&app).expect("parent");
801 let leaf_layer = tree.leaf.as_object().debug_layer(&app).expect("leaf");
802 assert_eq!(leaf_layer.as_layer().parent(&app), Some(parent_layer));
803
804 tree.leaf.mark_needs_paint(&mut app);
805 assert_eq!(
806 test_pipeline_owner(&mut app).nodes_needing_paint(&app),
807 vec![tree.leaf.as_object()]
808 );
809 pump_frame(&mut app, EnginePhase::Paint);
810 assert_eq!(paints(&tree), (1, 2));
811 let parent_layer = tree.parent.as_object().debug_layer(&app).expect("parent");
812 let leaf_layer = tree.leaf.as_object().debug_layer(&app).expect("leaf");
813 assert_eq!(leaf_layer.as_layer().parent(&app), Some(parent_layer));
814 }
815
816 #[test]
817 fn composited_layer_update_does_not_repaint() {
818 let cell = AppCell::new();
819 let mut app = cell.borrow_mut();
820 let tree = tree(&mut app, true);
821 pump_frame(&mut app, EnginePhase::Paint);
822
823 tree.leaf.get_mut(&mut app).alpha = 128;
824 tree.leaf.mark_needs_composited_layer_update(&mut app);
825 assert!(
826 tree.leaf
827 .as_object()
828 .debug_needs_composited_layer_update(&app)
829 );
830 pump_frame(&mut app, EnginePhase::Paint);
831 assert_eq!(paints(&tree), (1, 1));
832 let layer = tree.leaf.as_object().debug_layer(&app).expect("layer");
833 let opacity = app
834 .handle::<OpacityLayer>(layer.id())
835 .expect("OpacityLayer");
836 assert_eq!(opacity.alpha(&app), Some(128));
837 }
838
839 #[test]
841 fn unpainted_boundary_is_detached_and_skipped() {
842 let cell = AppCell::new();
843 let mut app = cell.borrow_mut();
844 let tree = tree(&mut app, true);
845 pump_frame(&mut app, EnginePhase::Paint);
846 assert!(leaf_layer_attached(&app, &tree));
847
848 tree.parent.get_mut(&mut app).paints_child = false;
849 tree.parent.mark_needs_paint(&mut app);
850 pump_frame(&mut app, EnginePhase::Paint);
851 assert_eq!(paints(&tree), (2, 1));
852 assert!(!leaf_layer_attached(&app, &tree));
853
854 tree.leaf.mark_needs_paint(&mut app);
855 pump_frame(&mut app, EnginePhase::Paint);
856 assert_eq!(
857 paints(&tree),
858 (2, 1),
859 "a detached boundary is not repainted"
860 );
861 assert!(tree.leaf.as_object().debug_needs_paint(&app));
862
863 tree.parent.get_mut(&mut app).paints_child = true;
864 tree.parent.mark_needs_paint(&mut app);
865 pump_frame(&mut app, EnginePhase::Paint);
866 assert_eq!(paints(&tree), (3, 2));
867 assert!(leaf_layer_attached(&app, &tree));
868 }
869
870 #[test]
871 fn non_boundary_child_paints_into_the_parent_picture() {
872 let cell = AppCell::new();
873 let mut app = cell.borrow_mut();
874 let tree = tree(&mut app, false);
875 pump_frame(&mut app, EnginePhase::Paint);
876 assert_eq!(paints(&tree), (1, 1));
877 assert!(tree.leaf.as_object().debug_layer(&app).is_none());
878
879 tree.leaf.mark_needs_paint(&mut app);
880 assert_eq!(
881 test_pipeline_owner(&mut app).nodes_needing_paint(&app),
882 vec![tree.parent.as_object()]
883 );
884 pump_frame(&mut app, EnginePhase::Paint);
885 assert_eq!(paints(&tree), (2, 2));
886 }
887
888 #[test]
889 fn composing_the_root_draws_the_child_boundary_through_its_layer() {
890 let cell = AppCell::new();
891 let mut app = cell.borrow_mut();
892 let tree = tree(&mut app, true);
893 tree.leaf.get_mut(&mut app).alpha = 128;
894 pump_frame(&mut app, EnginePhase::Paint);
895
896 let layer = tree.parent.as_object().layer(&app).expect("painted");
897 let scene = layer.build_scene(&mut app, SceneBuilder::new());
898 let parent = only_container(&scene.root.children);
899 let child = only_container(&parent.children);
900 assert!(
901 matches!(child.effect, Effect::Opacity { alpha: 128, .. }),
902 "the child's OpacityLayer: {:?}",
903 child.effect
904 );
905 assert!(
906 matches!(child.children.as_slice(), [Layer::DisplayList(_)]),
907 "the child's picture: {:?}",
908 child.children
909 );
910 }
911
912 fn only_container(children: &[Layer]) -> &EngineLayer {
914 let mut containers = children.iter().filter_map(|child| match child {
915 Layer::Container(container) => Some(container),
916 _ => None,
917 });
918 let container = containers.next().expect("a container");
919 assert!(containers.next().is_none(), "one container: {children:?}");
920 container
921 }
922}