forge
Explore
Log in

Don't you dare go Hollow

wiki: Document the redraw loop

Browse files at this commit

0078293d4c2220900a8ed988a2eee9531355b8e3Ivan MolodetskikhParent: 9728dbe
wiki/Redraw-Loop.mdadded+20 -0
@@ -0,0 +1,20 @@
1+On a TTY, only one frame can be submitted to an output at a time, and the compositor must wait until the output repaints (indicated by a VBlank) to be able to submit the next frame.
2+In niri we keep track of this via the `RedrawState` enum that you can find in an `OutputState`.
3+
4+Here's a diagram of state transitions for the `RedrawState` state machine:
5+
6+![RedrawState state transition diagram](./img/RedrawState-light.drawio.png#gh-light-mode-only)
7+![RedrawState state transition diagram](./img/RedrawState-dark.drawio.png#gh-dark-mode-only)
8+
9+`Idle` is the default state, when the output does not need to be repainted.
10+Any operation that may cause the screen to update calls `queue_redraw()`, which moves the output to a `Queued` state.
11+Then, at the end of an event loop dispatch, niri calls `redraw()` for every `Queued` output.
12+
13+If the redraw causes damage (i.e. something on the output changed), we move into the `WaitingForVBlank` state, since we cannot redraw until we receive a VBlank event.
14+However, if there's no damage, we do not return to `Idle` right away.
15+Instead, we set a timer to fire roughly at when the next VBlank would occur, and transition to a `WaitingForEstimatedVBlank` state.
16+
17+This is necessary in order to throttle frame callbacks sent to applications to at most once per output refresh cycle.
18+Without this throttling, applications can start continuously redrawing without damage (for instance, if the application window is partially off-screen, and it is only the off-screen part that changes), and eating a lot of CPU in the process.
19+
20+Then, either the estimated VBlank timer completes, and we go back to `Idle`, or maybe we call `queue_redraw()` once more and try to redraw again.
wiki/_Sidebar.mdmodified+1 -0
@@ -25,3 +25,4 @@
2525 * [Design Principles](./Design-Principles.md)
2626 * [Developing niri](./Developing-niri.md)
2727 * [Fractional Layout](./Fractional-Layout.md)
28+* [Redraw Loop](./Redraw-Loop.md)
wiki/img/RedrawState-dark.drawio.pngadded+3 -0
@@ -0,0 +1,3 @@
1+version https://git-lfs.github.com/spec/v1
2+oid sha256:d6053eb3d7dbc07ae4b03403a28819788be50ce56f1d3c109b755e50de14ad62
3+size 97565
wiki/img/RedrawState-light.drawio.pngadded+3 -0
@@ -0,0 +1,3 @@
1+version https://git-lfs.github.com/spec/v1
2+oid sha256:546c4a452f964f07a8d7e100eb6a007fec856c784857c67d214727b62b14a75d
3+size 86906