waft_embedder/composition.rs
1//! Native content the OS compositor shows beside the framework's picture: the capability a host
2//! with a compositor hands out, and the composition layer a producer feeds from a thread of its
3//! own. The Core Animation hosts' `composition` module, in `waft-embedder-apple`, says
4//! why it is shaped as it is. A producer that paces by the screen's refreshes waits on the
5//! platform's [`VsyncWaiter`](crate::VsyncWaiter).
6//!
7//! A composition layer is a node of the compositor's tree, as Core Animation's `CALayer`,
8//! DirectComposition's visual and Android's `SurfaceControl` are: it shows buffers presented to
9//! it, or a layer the system draws itself, and holds children beneath it. It is made on its own
10//! ([`Compositor::make_layer`]) and adopted by whoever places it, as `addSublayer` decides a
11//! `CALayer`'s parent: a view's frame, whose [`Scene`](crate::Scene) places it through its
12//! [`slices`](crate::slices), or another layer, whose [`CompositionLayer::present`] places its
13//! children in its buffer's pixels, beneath its buffer. Each frame names every child it shows, as
14//! a frame-based compositor takes them: Chromium's `SubmitCompositorFrame` lists the surfaces a
15//! frame embeds, and Flutter's embedder hands its compositor each frame's layers. The host lands
16//! a frame in the one transaction its compositor has for it (a `CATransaction`, a parent's
17//! `wl_surface.commit`, `IDCompositionDevice::Commit`, a `SurfaceControl.Transaction`). A
18//! program whose picture is itself a layer in another app's window shows its own layers as that
19//! picture's children, and the placement of the picture moves, clips and scales the whole group
20//! as one. What a layer shows can be read back ([`CompositionLayer::current_picture`]): the raw
21//! buffer presented to it, or the lent frame of the video or camera on it, so a tool that takes a
22//! picture of a window draws its layers beneath the picture above them as the compositor shows
23//! them.
24//!
25//! The invariant: a layer is on screen only where its parent's last frame placed it, it has one
26//! parent at a time, and the host, not the framework, owns the native object behind it.
27
28use std::any::Any;
29use std::sync::Arc;
30
31use crate::{GpuId, HardwareBuffer, HardwareBufferFormat, Placement, VideoFrame};
32
33/// The host's compositor, as an app reaches it.
34pub trait Compositor: Send + Sync {
35 /// A layer placed nowhere yet, fed by a producer from any thread, and shown wherever the one
36 /// that places it says: a view's scenes, or another layer's presents. `opaque` says whether
37 /// its buffers carry no alpha the compositor must blend; a present that gives it children,
38 /// which show through the clear pixels of its buffer, has that buffer blended all the same.
39 /// The layer goes when its last handle does: its producer's, and those of the frames that
40 /// place it.
41 fn make_layer(&self, opaque: bool) -> Arc<dyn CompositionLayer>;
42
43 /// The buffers its layers show: what a producer that makes its own buffers makes, so that
44 /// none is refused or copied between GPUs. It answers as the compositor was made, and does not
45 /// change after.
46 fn accepted_buffers(&self) -> AcceptedBuffers;
47}
48
49/// What a compositor shows a layer's pictures from ([`Compositor::accepted_buffers`]): GPU memory
50/// it imports, and shared memory it maps. A producer makes buffers of a format both it and the
51/// compositor take, never of the compositor's list alone.
52#[derive(Clone, Debug, Default, PartialEq, Eq)]
53pub struct AcceptedBuffers {
54 /// The GPU memory it imports, one entry per GPU, in its order of preference: the first is the
55 /// GPU it composites on, where a producer's buffers show with no copy. Empty where it imports
56 /// none.
57 pub gpu_memory: Vec<GpuMemory>,
58 /// Whether it also shows pictures from shared memory, which the producer writes with the CPU:
59 /// Wayland's `wl_shm`. No other system Waft runs on has it.
60 pub shared_memory: bool,
61}
62
63impl AcceptedBuffers {
64 /// The GPU it composites on, where its system names it.
65 pub fn gpu(&self) -> Option<GpuId> {
66 self.gpu_memory.first().and_then(|memory| memory.gpu)
67 }
68}
69
70/// The formats a compositor imports from one GPU's memory.
71#[derive(Clone, Debug, PartialEq, Eq)]
72pub struct GpuMemory {
73 /// The GPU the buffers are made on; `None` where the system names none (macOS, iOS and
74 /// Android, whose compositors take memory of any of the machine's GPUs).
75 pub gpu: Option<GpuId>,
76 /// The formats, in the compositor's order of preference.
77 pub formats: Vec<AcceptedFormat>,
78}
79
80/// One format a compositor imports.
81#[derive(Clone, Debug, PartialEq, Eq)]
82pub struct AcceptedFormat {
83 pub format: HardwareBufferFormat,
84 /// The DRM format modifiers it imports the format in, Linux's alone, in its order of
85 /// preference: `DRM_FORMAT_MOD_INVALID` alone means the driver's implicit layout. Empty
86 /// elsewhere, where the system's allocator picks the layout and the compositor takes it.
87 pub modifiers: Vec<u64>,
88}
89
90/// One layer of the compositor's tree. Every method may be called from any thread.
91///
92/// The handle is the layer's identity: a frame places the `Arc` itself, so only a layer that
93/// exists can be placed, and it lives while anything shows it. A host gets its own type back
94/// from a placed layer by downcasting through `Any`.
95pub trait CompositionLayer: Any + Send + Sync {
96 /// Shows `buffer`, which fills the layer, and `children` beneath it, the bottom-most first,
97 /// from the compositor's next frame on, in one step, in place of what the layer showed: the
98 /// buffers presented before, or a layer the system draws into, such as a player's, whose
99 /// picture goes nowhere then. A layer shows one source at a time, the last given it. Each
100 /// child lies where its placement says in the buffer's pixels, as a scene places a layer in
101 /// its view's, so the buffer's own placement moves, clips and scales it with it. A child the
102 /// last present showed and this one does not name is let go; a layer another parent holds is
103 /// not shown here, and is reported.
104 ///
105 /// Returns once the host holds the buffer, not once it is on screen. `released` runs, on any
106 /// thread, when the compositor has finished with the buffer and the producer may draw into
107 /// it again, as Wayland's `wl_buffer.release`; at once for a buffer another host handed out,
108 /// which is not shown. On Apple platforms the host learns that from `IOSurfaceIsInUse` at the
109 /// layer's next present, or when the layer goes, so a producer
110 /// there never waits for a buffer to come back: it draws into one it knows is free, or a new
111 /// one. Core Animation takes no fence, so a producer that draws the buffer on the GPU
112 /// presents it once that work is scheduled (`waitUntilScheduled`), as Chromium does before
113 /// it hands Core Animation a surface.
114 fn present(
115 &self,
116 buffer: Arc<dyn HardwareBuffer>,
117 children: Vec<(Arc<dyn CompositionLayer>, Placement)>,
118 released: Box<dyn FnOnce() + Send + Sync>,
119 );
120
121 /// What the layer shows now: the last buffer presented, or the current frame of the video or
122 /// camera it shows. `None` while it shows nothing, and while it shows a layer the system draws
123 /// into that the host does not read, such as one an app shows on it itself.
124 fn current_picture(&self) -> Option<LayerPicture>;
125}
126
127/// What a composition layer shows now ([`CompositionLayer::current_picture`]), as raw memory or a
128/// lent frame, as the app holds either.
129pub enum LayerPicture {
130 /// The buffer presented to the layer last, which fills it.
131 Buffer(Arc<dyn HardwareBuffer>),
132 /// The frame of the video or the camera shown on the layer, lent: held while the picture is
133 /// kept, since its pool hands its memory out again once nothing holds it. The system shows it
134 /// whole, keeping its shape.
135 Frame(Arc<dyn VideoFrame>),
136}