Skip to main content

waft_embedder/scene/
builder.rs

1//! Flutter counterpart: dart:ui `SceneBuilder`, as the framework's layers call it from
2//! `add_to_scene`.
3//!
4//! It builds the engine's layer tree as `lib/ui/compositing/scene_builder.cc` does, with a stack
5//! of the containers being built: a push starts one inside the last, `pop` builds the last and
6//! adds it to the one beneath, and an add puts a leaf, or a retained container, in the last. It
7//! records nothing and computes nothing; what the containers do is replayed by whoever walks the
8//! tree. The invariant a later editor must keep: a node is built before it is shared, so `pop`
9//! hands back a container no later call changes.
10
11use std::fmt;
12use std::sync::Arc;
13
14use super::Scene;
15use super::layer::{
16    ContainerLayer, DisplayListLayer, Effect, EmbeddedLayer, EngineLayer, Layer, VideoLayer,
17};
18use crate::composition::CompositionLayer;
19use crate::geometry::{Clip, Matrix4, Offset, RRect, Rect};
20use crate::media::VideoFrameReader;
21use crate::painting::{BlendMode, ImageFilter, Path, Picture};
22
23/// Builds a [`Scene`] containing the given visuals.
24///
25/// A [`Scene`] can then be rendered using [`crate::View::present`].
26///
27/// To draw graphical operations onto a [`Scene`], first create a [`Canvas`](crate::Canvas),
28/// record into it, and add the resulting [`Picture`] with [`add_picture`](Self::add_picture).
29/// Each `push_*` starts a container that [`pop`](Self::pop) builds into an [`EngineLayer`];
30/// unbalanced pushes are a bug the builder asserts on.
31pub struct SceneBuilder {
32    /// The containers being built, the scene's root first, as the engine's `layer_stack_`:
33    /// every push starts one inside the last, and every pop builds the last into the one beneath.
34    /// `build` builds the root.
35    layer_stack: Vec<ContainerLayerBuilder>,
36}
37
38/// The [`ContainerLayer`] being built while its push is open: its effect, and the children added
39/// so far.
40struct ContainerLayerBuilder {
41    effect: Effect,
42    children: Vec<Layer>,
43}
44
45impl ContainerLayerBuilder {
46    fn new(effect: Effect) -> ContainerLayerBuilder {
47        ContainerLayerBuilder {
48            effect,
49            children: Vec::new(),
50        }
51    }
52
53    fn build(self) -> ContainerLayer {
54        ContainerLayer {
55            effect: self.effect,
56            children: self.children,
57        }
58    }
59}
60
61impl fmt::Debug for SceneBuilder {
62    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63        f.debug_struct("SceneBuilder")
64            .field("open_pushes", &(self.layer_stack.len() - 1))
65            .finish_non_exhaustive()
66    }
67}
68
69impl Default for SceneBuilder {
70    fn default() -> SceneBuilder {
71        SceneBuilder::new()
72    }
73}
74
75impl SceneBuilder {
76    /// Creates an empty [`SceneBuilder`] object.
77    pub fn new() -> SceneBuilder {
78        SceneBuilder {
79            layer_stack: vec![ContainerLayerBuilder::new(Effect::None)],
80        }
81    }
82
83    /// Pushes a transform operation onto the operation stack.
84    ///
85    /// The objects are transformed by the given matrix before rasterization.
86    pub fn push_transform(&mut self, matrix4: &Matrix4) {
87        self.push_layer(Effect::Transform(*matrix4));
88    }
89
90    /// Pushes an offset operation onto the operation stack.
91    ///
92    /// This is equivalent to [`push_transform`](Self::push_transform) with a matrix with only
93    /// translation.
94    pub fn push_offset(&mut self, dx: f64, dy: f64) {
95        self.push_layer(Effect::Transform(Matrix4::translation(
96            dx as f32, dy as f32,
97        )));
98    }
99
100    /// Pushes a rectangular clip operation onto the operation stack.
101    ///
102    /// Rasterization outside the given rectangle is discarded.
103    pub fn push_clip_rect(&mut self, rect: Rect, clip_behavior: Clip) {
104        self.push_layer(Effect::ClipRect {
105            rect,
106            clip_behavior,
107        });
108    }
109
110    /// Pushes a rounded-rectangular clip operation onto the operation stack.
111    ///
112    /// Rasterization outside the given rounded rectangle is discarded.
113    pub fn push_clip_rrect(&mut self, rrect: RRect, clip_behavior: Clip) {
114        self.push_layer(Effect::ClipRRect {
115            rrect,
116            clip_behavior,
117        });
118    }
119
120    /// Pushes a path clip operation onto the operation stack.
121    ///
122    /// Rasterization outside the given path is discarded.
123    pub fn push_clip_path(&mut self, path: &Path, clip_behavior: Clip) {
124        self.push_layer(Effect::ClipPath {
125            path: path.clone(),
126            clip_behavior,
127        });
128    }
129
130    /// Pushes an opacity operation onto the operation stack.
131    ///
132    /// The given alpha value is blended into the alpha value of the objects' rasterization. An
133    /// alpha value of 0 makes the objects entirely invisible. An alpha value of 255 has no
134    /// effect (i.e., the objects retain the current opacity).
135    pub fn push_opacity(&mut self, alpha: i32, offset: Offset) {
136        self.push_layer(Effect::Opacity {
137            alpha: alpha.clamp(0, 255) as u8,
138            offset,
139        });
140    }
141
142    /// Pushes a backdrop filter operation onto the operation stack.
143    ///
144    /// The given filter is applied to the current contents of the scene as far back as the most
145    /// recent save layer and rendered back to the scene using the indicated `blend_mode` prior to
146    /// rasterizing the child layers.
147    ///
148    /// Filters sharing a `backdrop_id` sample one backdrop, as Flutter's `BackdropKey` groups
149    /// them.
150    pub fn push_backdrop_filter(
151        &mut self,
152        filter: &ImageFilter,
153        blend_mode: BlendMode,
154        backdrop_id: Option<u64>,
155    ) {
156        self.push_layer(Effect::BackdropFilter {
157            filter: filter.clone(),
158            blend_mode,
159            backdrop_id,
160        });
161    }
162
163    /// Pushes an image filter operation onto the operation stack.
164    ///
165    /// The given filter is applied to the children's rasterization before compositing them into
166    /// the scene, the children moved by `offset` first.
167    pub fn push_image_filter(&mut self, filter: &ImageFilter, offset: Offset) {
168        self.push_layer(Effect::ImageFilter {
169            filter: filter.clone(),
170            offset,
171        });
172    }
173
174    /// Ends the effect of the most recently pushed operation.
175    ///
176    /// The container built is the [`EngineLayer`] returned, a child of the one beneath it: the
177    /// layer that pushed keeps it and hands it to [`add_retained`](Self::add_retained) while its
178    /// subtree stays unchanged. dart:ui returns the engine layer from the push; a container here
179    /// is built, and shared, at its pop.
180    ///
181    /// # Panics
182    ///
183    /// If nothing is pushed.
184    pub fn pop(&mut self) -> EngineLayer {
185        assert!(
186            self.layer_stack.len() > 1,
187            "SceneBuilder::pop without a push"
188        );
189        let built = self.layer_stack.pop().expect("a push is open").build();
190        let layer = Arc::new(built);
191        self.add_layer(Layer::Container(Arc::clone(&layer)));
192        layer
193    }
194
195    /// Places a composition layer the host composites, at `rect` in the coordinates of the
196    /// container being built: Flutter's `addPlatformView`. It draws nothing; whoever composites
197    /// the scene makes room for it at its place in paint order.
198    pub fn add_embedded_layer(&mut self, layer: Arc<dyn CompositionLayer>, rect: Rect) {
199        self.add_layer(Layer::Embedded(EmbeddedLayer { layer, rect }));
200    }
201
202    /// Draws `reader`'s video into `rect`, in the coordinates of the container being built, at its
203    /// place in paint order: what Flutter's `addTexture` does for a texture id. Whoever draws the
204    /// scene asks the reader for the frame then.
205    pub fn add_video(&mut self, reader: Arc<dyn VideoFrameReader>, rect: Rect) {
206        self.add_layer(Layer::Video(VideoLayer { reader, rect }));
207    }
208
209    /// Adds a retained [`EngineLayer`] subtree from a previous frame.
210    ///
211    /// All the engine layers that are in the subtree of the retained layer will be automatically
212    /// appended to the current engine layer tree: the same node, in the container being built.
213    pub fn add_retained(&mut self, retained_layer: &EngineLayer) {
214        self.add_layer(Layer::Container(Arc::clone(retained_layer)));
215    }
216
217    /// Adds a [`Picture`] to the scene.
218    ///
219    /// The picture is rasterized at the given `offset`. The hints are for a raster cache valo
220    /// does not have; they are accepted so a layer can pass what Flutter passes.
221    pub fn add_picture(
222        &mut self,
223        offset: Offset,
224        picture: &Arc<Picture>,
225        is_complex_hint: bool,
226        will_change_hint: bool,
227    ) {
228        let _ = (is_complex_hint, will_change_hint);
229        self.add_layer(Layer::DisplayList(DisplayListLayer {
230            offset,
231            picture: Arc::clone(picture),
232        }));
233    }
234
235    /// Finishes building the scene.
236    ///
237    /// # Panics
238    ///
239    /// In debug builds, if a pushed operation was not popped.
240    pub fn build(mut self) -> Scene {
241        debug_assert!(
242            self.layer_stack.len() == 1,
243            "SceneBuilder::build with an open push"
244        );
245        while self.layer_stack.len() > 1 {
246            self.pop();
247        }
248        let root = self.layer_stack.pop().expect("the root is being built");
249        Scene { root: root.build() }
250    }
251
252    /// Starts building a container inside the last: the engine's `PushLayer`.
253    fn push_layer(&mut self, effect: Effect) {
254        self.layer_stack.push(ContainerLayerBuilder::new(effect));
255    }
256
257    /// Adds `layer` to the container being built last: the engine's `AddLayer`.
258    fn add_layer(&mut self, layer: Layer) {
259        self.layer_stack
260            .last_mut()
261            .expect("the root is built only by build")
262            .children
263            .push(layer);
264    }
265}
266
267#[cfg(test)]
268mod tests {
269    use super::*;
270    use crate::{Canvas, Paint};
271
272    fn square() -> Arc<Picture> {
273        let mut canvas = Canvas::new();
274        canvas.draw_rect(Rect::from_ltwh(0.0, 0.0, 10.0, 10.0), &Paint::default());
275        Arc::new(canvas.build())
276    }
277
278    fn container(layer: &Layer) -> &EngineLayer {
279        match layer {
280            Layer::Container(container) => container,
281            other => panic!("not a container: {other:?}"),
282        }
283    }
284
285    #[test]
286    fn a_popped_container_is_a_node_of_its_parent() {
287        let picture = square();
288        let mut builder = SceneBuilder::new();
289        builder.push_opacity(128, Offset::new(1.0, 2.0));
290        builder.push_transform(&Matrix4::scale(2.0, 2.0));
291        builder.add_picture(Offset::new(3.0, 4.0), &picture, false, false);
292        let inner = builder.pop();
293        let outer = builder.pop();
294        let scene = builder.build();
295
296        assert_eq!(scene.root.effect, Effect::None);
297        let [only] = scene.root.children.as_slice() else {
298            panic!("one child: {:?}", scene.root.children);
299        };
300        assert!(Arc::ptr_eq(container(only), &outer));
301        assert_eq!(
302            outer.effect,
303            Effect::Opacity {
304                alpha: 128,
305                offset: Offset::new(1.0, 2.0)
306            }
307        );
308        assert!(Arc::ptr_eq(container(&outer.children[0]), &inner));
309        assert_eq!(inner.effect, Effect::Transform(Matrix4::scale(2.0, 2.0)));
310        assert_eq!(
311            inner.children,
312            [Layer::DisplayList(DisplayListLayer {
313                offset: Offset::new(3.0, 4.0),
314                picture,
315            })]
316        );
317    }
318
319    #[test]
320    fn a_retained_container_is_the_same_node_in_the_next_scene() {
321        let mut builder = SceneBuilder::new();
322        builder.push_clip_rect(Rect::from_ltwh(0.0, 0.0, 5.0, 5.0), Clip::HardEdge);
323        builder.add_picture(Offset::ZERO, &square(), false, false);
324        let retained = builder.pop();
325        let _first = builder.build();
326
327        let mut builder = SceneBuilder::new();
328        builder.push_offset(10.0, 0.0);
329        builder.add_retained(&retained);
330        builder.pop();
331        let next = builder.build();
332        let moved = container(&next.root.children[0]);
333        assert!(Arc::ptr_eq(container(&moved.children[0]), &retained));
334    }
335
336    #[test]
337    fn an_opacity_is_kept_between_none_and_all() {
338        let mut builder = SceneBuilder::new();
339        builder.push_opacity(300, Offset::ZERO);
340        let all = builder.pop();
341        builder.push_opacity(-1, Offset::ZERO);
342        let none = builder.pop();
343        let alpha = |layer: &EngineLayer| match layer.effect {
344            Effect::Opacity { alpha, .. } => alpha,
345            _ => unreachable!(),
346        };
347        assert_eq!((alpha(&all), alpha(&none)), (255, 0));
348    }
349
350    #[test]
351    fn a_composition_layer_is_a_leaf_at_its_place_in_paint_order() {
352        let layer = super::super::idle_layer();
353        let rect = Rect::from_ltwh(1.0, 2.0, 3.0, 4.0);
354        let mut builder = SceneBuilder::new();
355        builder.add_picture(Offset::ZERO, &square(), false, false);
356        builder.add_embedded_layer(Arc::clone(&layer), rect);
357        let scene = builder.build();
358        assert!(matches!(scene.root.children[0], Layer::DisplayList(_)));
359        assert_eq!(
360            scene.root.children[1],
361            Layer::Embedded(EmbeddedLayer { layer, rect })
362        );
363    }
364
365    #[test]
366    fn a_scene_without_layers_has_an_empty_root() {
367        let scene = SceneBuilder::new().build();
368        assert!(scene.root.children.is_empty());
369    }
370
371    #[test]
372    #[should_panic(expected = "open push")]
373    fn build_with_an_open_push_panics() {
374        let mut builder = SceneBuilder::new();
375        builder.push_offset(0.0, 0.0);
376        let _scene = builder.build();
377    }
378
379    #[test]
380    #[should_panic(expected = "without a push")]
381    fn pop_without_a_push_panics() {
382        SceneBuilder::new().pop();
383    }
384}