queue.go RawHistoryFolder
1// Package presented queues the OutputPresented reports an output owner sends
2// to wayland: what the output read, held and flipped.
3package presented
4
5import (
6 "maps"
7
8 "github.com/bnema/neferwl/internal/ports"
9)
10
11// Queue holds the reports an output has not pushed to wayland yet. The owner
12// goroutine flushes it every loop turn, so it holds the few reports of one
13// turn; the sink alone bounds what waits for the display loop (and merges
14// flips when that stalls). Its owner goroutine is the only user. The Seen and
15// ChildReads maps of a queued report are immutable snapshots: wayland may
16// still read an earlier report on another goroutine, so a snapshot is
17// replaced by a clone when its content changes and an unchanged report
18// allocates nothing.
19type Queue struct {
20 unsent []ports.OutputPresented
21 seen, reads map[ports.WindowID]uint64
22}
23
24// Push queues r. Its Seen and ChildReads may be the caller's scratch maps:
25// the queue keeps snapshots. A flip-less report replaces the newest queued
26// one when that one has no flip, or keeps its flip; every flip is queued.
27func (q *Queue) Push(r ports.OutputPresented) {
28 if !maps.Equal(q.seen, r.Seen) {
29 q.seen = maps.Clone(r.Seen)
30 }
31 if !maps.Equal(q.reads, r.ChildReads) {
32 q.reads = maps.Clone(r.ChildReads)
33 }
34 r.Seen, r.ChildReads = q.seen, q.reads
35 if n := len(q.unsent); n > 0 && (r.Flip == nil || q.unsent[n-1].Flip == nil) {
36 if r.Flip == nil {
37 r.Flip = q.unsent[n-1].Flip
38 }
39 q.unsent[n-1] = r
40 return
41 }
42 q.unsent = append(q.unsent, r)
43}
44
45// Merge folds old into next: next counts it as merged and shows what
46// old showed, so feedbacks waiting on old are answered (as discarded).
47// Wayland's presentation sink uses it when its ring overflows.
48func Merge(old, next *ports.FlipInfo) {
49 next.Merged += 1 + old.Merged
50 // A snapshot can be shared with previously sent flips. Clone only when
51 // a merge actually needs to add an absent window.
52 cloned := false
53 for id, seq := range old.Shows {
54 if _, ok := next.Shows[id]; ok {
55 continue
56 }
57 if !cloned {
58 next.Shows = maps.Clone(next.Shows)
59 cloned = true
60 }
61 if next.Shows == nil {
62 next.Shows = make(map[ports.WindowID]uint64)
63 }
64 next.Shows[id] = seq
65 }
66}
67
68// Flush pushes every queued report to sink, oldest first, and empties the
69// queue. Push never blocks.
70func (q *Queue) Flush(sink ports.PresentationSink) {
71 for _, r := range q.unsent {
72 sink.Push(r)
73 }
74 q.Reset()
75}
76
77// Len is the number of queued reports.
78func (q *Queue) Len() int { return len(q.unsent) }
79
80// Unsent returns the queued reports, oldest first. Callers must not modify
81// them.
82func (q *Queue) Unsent() []ports.OutputPresented { return q.unsent }
83
84// Seen is the Seen snapshot of the last pushed report.
85func (q *Queue) Seen() map[ports.WindowID]uint64 { return q.seen }
86
87// Reset drops queued reports, e.g. when a security epoch makes them stale.
88// Snapshots stay: they may be shared with reports already sent.
89func (q *Queue) Reset() {
90 clear(q.unsent)
91 q.unsent = q.unsent[:0]
92}