xref: /aosp_15_r20/frameworks/native/services/surfaceflinger/FrontEnd/readme.md (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker# SurfaceFlinger FrontEnd
2*38e8c45fSAndroid Build Coastguard Worker
3*38e8c45fSAndroid Build Coastguard WorkerSurfaceFlinger FrontEnd implements the client APIs that describe how buffers should be
4*38e8c45fSAndroid Build Coastguard Workercomposited on the screen. Layers are used to capture how the buffer should be composited
5*38e8c45fSAndroid Build Coastguard Workerand each buffer is associated with a Layer. Transactions contain an atomic set of changes
6*38e8c45fSAndroid Build Coastguard Workerto one or more of these layers. The FrontEnd consumes these transactions, maintains the
7*38e8c45fSAndroid Build Coastguard Workerlayer lifecycle, and provides a snapshot to the composition engine every frame that
8*38e8c45fSAndroid Build Coastguard Workerdescribes how a set of buffers should be composited.
9*38e8c45fSAndroid Build Coastguard Worker
10*38e8c45fSAndroid Build Coastguard Worker
11*38e8c45fSAndroid Build Coastguard Worker
12*38e8c45fSAndroid Build Coastguard Worker## Layers
13*38e8c45fSAndroid Build Coastguard WorkerLayers are used to describe how a buffer should be placed on the display relative to other
14*38e8c45fSAndroid Build Coastguard Workerbuffers. They are represented as a hierarchy, similar to a scene graph. Child layers can
15*38e8c45fSAndroid Build Coastguard Workerinherit some properties from their parents, which allows higher-level system components to
16*38e8c45fSAndroid Build Coastguard Workermaintain policies at different levels without needing to understand the entire hierarchy.
17*38e8c45fSAndroid Build Coastguard WorkerThis allows control to be delegated to different parts of the system - such as SystemServer,
18*38e8c45fSAndroid Build Coastguard WorkerSysUI and Apps.
19*38e8c45fSAndroid Build Coastguard Worker
20*38e8c45fSAndroid Build Coastguard Worker### Layer Drawing Order
21*38e8c45fSAndroid Build Coastguard WorkerLayers are drawn based on an inorder traversal, treating relative parents as
22*38e8c45fSAndroid Build Coastguard Workerdirect parents. Negative z-values place layers below their parent, while
23*38e8c45fSAndroid Build Coastguard Workernon-negative values place them above. Layers with the same z-value are drawn
24*38e8c45fSAndroid Build Coastguard Workerin creation order (newer on top).  However, relying on creation order for
25*38e8c45fSAndroid Build Coastguard Workerz-ordering is discouraged; use unique z-values whenever possible for better
26*38e8c45fSAndroid Build Coastguard Workercontrol.
27*38e8c45fSAndroid Build Coastguard Worker
28*38e8c45fSAndroid Build Coastguard WorkerTraversal pseudo code:
29*38e8c45fSAndroid Build Coastguard Worker```
30*38e8c45fSAndroid Build Coastguard Workerfn traverseBottomToTop(root):
31*38e8c45fSAndroid Build Coastguard Worker  for each child node including relative children,
32*38e8c45fSAndroid Build Coastguard Worker    sorted by z then layer id, with z less than 0:
33*38e8c45fSAndroid Build Coastguard Worker          traverseBottomToTop(childNode)
34*38e8c45fSAndroid Build Coastguard Worker
35*38e8c45fSAndroid Build Coastguard Worker  visit(root)
36*38e8c45fSAndroid Build Coastguard Worker
37*38e8c45fSAndroid Build Coastguard Worker  for each child node including relative children,
38*38e8c45fSAndroid Build Coastguard Worker    sorted by z then layer id, with z greater than
39*38e8c45fSAndroid Build Coastguard Worker    or equal to 0:
40*38e8c45fSAndroid Build Coastguard Worker          traverseBottomToTop(childNode)
41*38e8c45fSAndroid Build Coastguard Worker```
42*38e8c45fSAndroid Build Coastguard Worker
43*38e8c45fSAndroid Build Coastguard Worker### Layer Lifecycle
44*38e8c45fSAndroid Build Coastguard WorkerLayer is created by a client. The client receives a strong binder reference to the layer
45*38e8c45fSAndroid Build Coastguard Workerhandle, which will keep the layer alive as long as the client holds the reference. The
46*38e8c45fSAndroid Build Coastguard Workerlayer can also be kept alive if the layer has a parent, since the parent will hold a
47*38e8c45fSAndroid Build Coastguard Workerstrong reference to the children. If the layer is not reachable but its handle is alive,
48*38e8c45fSAndroid Build Coastguard Workerthe layer will be offscreen and its resources will not be freed. Clients must explicitly
49*38e8c45fSAndroid Build Coastguard Workerrelease all references to the handle as soon as it's done with the layer. It's strongly
50*38e8c45fSAndroid Build Coastguard Workerrecommended to explicitly release the layer in Java and not rely on the GC.
51*38e8c45fSAndroid Build Coastguard Worker
52*38e8c45fSAndroid Build Coastguard Worker
53*38e8c45fSAndroid Build Coastguard Worker
54*38e8c45fSAndroid Build Coastguard Worker## Transactions
55*38e8c45fSAndroid Build Coastguard WorkerTransactions contain a group of changes to one or more layers that are applied together.
56*38e8c45fSAndroid Build Coastguard WorkerTransactions can be merged to apply a set of changes atomically. Merges are associative,
57*38e8c45fSAndroid Build Coastguard Workermeaning how you group the merges does not matter, but they are not commutative, meaning
58*38e8c45fSAndroid Build Coastguard Workerthat the order in which you merge them does.
59*38e8c45fSAndroid Build Coastguard WorkerFor example:
60*38e8c45fSAndroid Build Coastguard Worker
61*38e8c45fSAndroid Build Coastguard Worker`Transaction a; a.setAlpha(sc, 2);`
62*38e8c45fSAndroid Build Coastguard Worker
63*38e8c45fSAndroid Build Coastguard Worker`Transaction b; b.setAlpha(sc, 4);`
64*38e8c45fSAndroid Build Coastguard Worker
65*38e8c45fSAndroid Build Coastguard Worker`a.merge(b)` is not the same as `b.merge(a)`
66*38e8c45fSAndroid Build Coastguard Worker
67*38e8c45fSAndroid Build Coastguard Worker<p>
68*38e8c45fSAndroid Build Coastguard Worker
69*38e8c45fSAndroid Build Coastguard Worker`Transaction c; c.setAlpha(sc, 6);`
70*38e8c45fSAndroid Build Coastguard Worker
71*38e8c45fSAndroid Build Coastguard Worker`a.merge(b).merge(c)` is the same as `b.merge(c); a.merge(b);`
72*38e8c45fSAndroid Build Coastguard Worker
73*38e8c45fSAndroid Build Coastguard WorkerTransactions are queued in SurfaceFlinger per ApplyToken so order is only guaranteed for
74*38e8c45fSAndroid Build Coastguard WorkerTransactions with the same applyToken. By default each process and each buffer producer
75*38e8c45fSAndroid Build Coastguard Workerprovides a unique ApplyToken. This prevents clients from affecting one another, and possibly
76*38e8c45fSAndroid Build Coastguard Workerslowing each other down.
77*38e8c45fSAndroid Build Coastguard Worker
78*38e8c45fSAndroid Build Coastguard Worker
79*38e8c45fSAndroid Build Coastguard Worker
80*38e8c45fSAndroid Build Coastguard Worker## Architecture
81*38e8c45fSAndroid Build Coastguard WorkerSurfaceFlinger FrontEnd intends to optimize for predictability and performance because state
82*38e8c45fSAndroid Build Coastguard Workergeneration is on the hotpath. Simple buffer updates should be as fast as possible, and they
83*38e8c45fSAndroid Build Coastguard Workershould be consistently fast. This means avoiding contention (e.g., locks) and context
84*38e8c45fSAndroid Build Coastguard Workerswitching. We also want to avoid doing anything that does not contribute to putting a pixel
85*38e8c45fSAndroid Build Coastguard Workeron the display.
86*38e8c45fSAndroid Build Coastguard Worker
87*38e8c45fSAndroid Build Coastguard WorkerThe pipeline can be broken down into five stages:
88*38e8c45fSAndroid Build Coastguard Worker- Queue and filter transactions that are ready to be committed.
89*38e8c45fSAndroid Build Coastguard Worker- Handle layer lifecycles and update server-side state per layer.
90*38e8c45fSAndroid Build Coastguard Worker- Generate and/or update the traversal trees.
91*38e8c45fSAndroid Build Coastguard Worker- Generate a z-ordered list of snapshots.
92*38e8c45fSAndroid Build Coastguard Worker- Emit callbacks back to clients
93*38e8c45fSAndroid Build Coastguard Worker
94*38e8c45fSAndroid Build Coastguard Worker
95*38e8c45fSAndroid Build Coastguard Worker### TransactionHandler
96*38e8c45fSAndroid Build Coastguard WorkerTransactionHandler is responsible for queuing and filtering transactions that are ready to
97*38e8c45fSAndroid Build Coastguard Workerbe applied. On commit, we filter the transactions that are ready. We provide an interface
98*38e8c45fSAndroid Build Coastguard Workerfor other components to apply their own filter to determine if a transaction is ready to be
99*38e8c45fSAndroid Build Coastguard Workerapplied.
100*38e8c45fSAndroid Build Coastguard Worker
101*38e8c45fSAndroid Build Coastguard Worker
102*38e8c45fSAndroid Build Coastguard Worker### LayerLifecycleManager
103*38e8c45fSAndroid Build Coastguard WorkerRequestedLayerState is a simple data class that stores the server side layer state.
104*38e8c45fSAndroid Build Coastguard WorkerTransactions are merged into this state, similar to how transactions can be merged on the
105*38e8c45fSAndroid Build Coastguard Workerclient side. The states can always be reconstructed from LayerCreationArgs and a list of
106*38e8c45fSAndroid Build Coastguard Workertransactions. LayerLifecycleManager keeps track of Layer handle lifecycle and the layer
107*38e8c45fSAndroid Build Coastguard Workerlifecycle itself. It consumes a list of transactions and generates a list of server side
108*38e8c45fSAndroid Build Coastguard Workerstates and change flags. Other components can register to listen to layer lifecycles.
109*38e8c45fSAndroid Build Coastguard Worker
110*38e8c45fSAndroid Build Coastguard Worker
111*38e8c45fSAndroid Build Coastguard Worker### LayerHierarchyBuilder
112*38e8c45fSAndroid Build Coastguard WorkerLayerHierarchyBuilder consumes a list of RequestedLayerStates to generate a LayerHierarchy.
113*38e8c45fSAndroid Build Coastguard WorkerThe hierarchy provides functions for breadth-first traversal and z-order traversal of the
114*38e8c45fSAndroid Build Coastguard Workerentire tree or a subtree. Internally, the hierarchy is represented by a graph. Mirrored
115*38e8c45fSAndroid Build Coastguard Workerlayers are represented by the same node in the graph with multiple parents. This allows us
116*38e8c45fSAndroid Build Coastguard Workerto implement mirroring without cloning Layers and maintaining complex hierarchies.
117*38e8c45fSAndroid Build Coastguard Worker
118*38e8c45fSAndroid Build Coastguard Worker
119*38e8c45fSAndroid Build Coastguard Worker### LayerSnapshotBuilder
120*38e8c45fSAndroid Build Coastguard WorkerLayerSnapshotBuilder consumes a LayerHierarchy along with a list of RequestedLayerStates to
121*38e8c45fSAndroid Build Coastguard Workergenerate a flattened z-ordered list of LayerSnapshots. LayerSnapshots contain all the data
122*38e8c45fSAndroid Build Coastguard Workerrequired for CompositionEngine and RenderEngine. It has no dependencies to FrontEnd, or the
123*38e8c45fSAndroid Build Coastguard WorkerLayerHierarchy used to create them. They can be cloned and consumed freely. Other consumers
124*38e8c45fSAndroid Build Coastguard Workerlike WindowInfo listeners (input and accessibility) also updated from these snapshots.
125*38e8c45fSAndroid Build Coastguard Worker
126*38e8c45fSAndroid Build Coastguard WorkerChange flags are used to efficiently traverse this hierarchy where possible. This allows us
127*38e8c45fSAndroid Build Coastguard Workerto support short circuiting parts of the hierarchy, partial hierarchy updates and fast paths
128*38e8c45fSAndroid Build Coastguard Workerfor buffer updates.
129*38e8c45fSAndroid Build Coastguard Worker
130*38e8c45fSAndroid Build Coastguard Worker
131*38e8c45fSAndroid Build Coastguard WorkerWhile they can be cloned, the current implementation moves the snapshot from FrontEnd to
132*38e8c45fSAndroid Build Coastguard WorkerCompositionEngine to avoid needless work in the hotpath. For snapshot consumers not critical
133*38e8c45fSAndroid Build Coastguard Workerto composition, the goal is to clone the snapshots and consume them on a background thread.
134