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