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 "context"
7 "maps"
8 "slices"
9
10 "github.com/bnema/neferwl/internal/ports"
11)
12
13// MaxUnsent bounds reports waiting for wayland; past it the oldest flip is
14// merged into the next one.
15const MaxUnsent = 16
16
17// Queue holds reports wayland has not read yet. Its owner goroutine is the
18// only user. The Seen and ChildReads maps of a queued report are immutable
19// snapshots: wayland may still read an earlier report on another goroutine,
20// so a snapshot is replaced by a clone when its content changes and an
21// unchanged report allocates nothing.
22type Queue struct {
23 unsent []ports.OutputPresented
24 seen, reads map[ports.WindowID]uint64
25}
26
27// Push queues r. Its Seen and ChildReads may be the caller's scratch maps:
28// the queue keeps snapshots. A flip-less report replaces the newest queued
29// one when that one has no flip, or keeps its flip; flips are merged only
30// past MaxUnsent. Push reports whether a flip was merged that way.
31func (q *Queue) Push(r ports.OutputPresented) (merged bool) {
32 if !maps.Equal(q.seen, r.Seen) {
33 q.seen = maps.Clone(r.Seen)
34 }
35 if !maps.Equal(q.reads, r.ChildReads) {
36 q.reads = maps.Clone(r.ChildReads)
37 }
38 r.Seen, r.ChildReads = q.seen, q.reads
39 if n := len(q.unsent); n > 0 && (r.Flip == nil || q.unsent[n-1].Flip == nil) {
40 if r.Flip == nil {
41 r.Flip = q.unsent[n-1].Flip
42 }
43 q.unsent[n-1] = r
44 return false
45 }
46 q.unsent = append(q.unsent, r)
47 if len(q.unsent) <= MaxUnsent {
48 return false
49 }
50 if old, next := q.unsent[0].Flip, q.unsent[1].Flip; next != nil && old != nil {
51 mergeFlip(old, next)
52 }
53 q.unsent = slices.Delete(q.unsent, 0, 1)
54 return true
55}
56
57// mergeFlip folds old into next: next counts it as merged and shows what
58// old showed, so feedbacks waiting on old are answered (as discarded).
59func mergeFlip(old, next *ports.FlipInfo) {
60 next.Merged += 1 + old.Merged
61 // A snapshot can be shared with previously sent flips. Clone only when
62 // a merge actually needs to add an absent window.
63 cloned := false
64 for id, seq := range old.Shows {
65 if _, ok := next.Shows[id]; ok {
66 continue
67 }
68 if !cloned {
69 next.Shows = maps.Clone(next.Shows)
70 cloned = true
71 }
72 if next.Shows == nil {
73 next.Shows = make(map[ports.WindowID]uint64)
74 }
75 next.Shows[id] = seq
76 }
77}
78
79// Flush sends queued reports without blocking.
80func (q *Queue) Flush(ch chan<- ports.OutputPresented) {
81 for len(q.unsent) > 0 {
82 select {
83 case ch <- q.unsent[0]:
84 q.unsent = slices.Delete(q.unsent, 0, 1)
85 default:
86 return
87 }
88 }
89}
90
91// Drain sends every queued report, blocking until wayland reads them or ctx
92// ends.
93func (q *Queue) Drain(ctx context.Context, ch chan<- ports.OutputPresented) error {
94 for len(q.unsent) > 0 {
95 select {
96 case ch <- q.unsent[0]:
97 q.unsent = slices.Delete(q.unsent, 0, 1)
98 case <-ctx.Done():
99 return ctx.Err()
100 }
101 }
102 return nil
103}
104
105// Len is the number of queued reports.
106func (q *Queue) Len() int { return len(q.unsent) }
107
108// Unsent returns the queued reports, oldest first. Callers must not modify
109// them.
110func (q *Queue) Unsent() []ports.OutputPresented { return q.unsent }
111
112// Seen is the Seen snapshot of the last pushed report.
113func (q *Queue) Seen() map[ports.WindowID]uint64 { return q.seen }
114
115// Reset drops queued reports, e.g. when a security epoch makes them stale.
116// Snapshots stay: they may be shared with reports already sent.
117func (q *Queue) Reset() {
118 clear(q.unsent)
119 q.unsent = q.unsent[:0]
120}