1package drm
2
3import (
4 "testing"
5
6 "github.com/bnema/neferwl/internal/ports"
7 "github.com/stretchr/testify/require"
8)
9
10// A flip of an animating scene tells core the next frame is due; the flip
11// of any other frame does not.
12func TestCompletedSendsFrameOnlyForAnimatingScene(t *testing.T) {
13 o, _, commits := testOutput(t)
14 o.cursor = nil
15 frames := make(chan ports.OutputFrame, 2)
16 o.Frames = frames
17 seen := map[ports.WindowID]uint64{}
18
19 require.NoError(t, o.commitFrame(70, nil, false, false, pendingFrame{}))
20 require.True(t, o.completed(eventOf((*commits)[0]), seen))
21 require.Empty(t, frames, "a settled scene wakes nobody")
22
23 require.NoError(t, o.commitFrame(70, nil, false, false, pendingFrame{animating: true}))
24 require.True(t, o.completed(eventOf((*commits)[1]), seen))
25 require.Equal(t, []ports.OutputFrame{{Output: "DP-1"}}, drain(frames))
26
27}
28
29// A frames channel core has not read stays full: the owner never waits.
30func TestCompletedFramesNeverBlock(t *testing.T) {
31 o, _, commits := testOutput(t)
32 o.cursor = nil
33 frames := make(chan ports.OutputFrame, 1)
34 o.Frames = frames
35 seen := map[ports.WindowID]uint64{}
36 for i := range 3 {
37 require.NoError(t, o.commitFrame(70, nil, false, false, pendingFrame{animating: true}))
38 require.True(t, o.completed(eventOf((*commits)[i]), seen))
39 }
40 require.Len(t, frames, 1)
41}
42
43// An output without a frames channel (tests, nobody to wake) just flips.
44func TestCompletedWithoutFramesChannel(t *testing.T) {
45 o, _, commits := testOutput(t)
46 o.cursor = nil
47 require.NoError(t, o.commitFrame(70, nil, false, false, pendingFrame{animating: true}))
48 require.True(t, o.completed(eventOf((*commits)[0]), map[ports.WindowID]uint64{}))
49}
50
51func drain(ch chan ports.OutputFrame) []ports.OutputFrame {
52 var out []ports.OutputFrame
53 for len(ch) > 0 {
54 out = append(out, <-ch)
55 }
56 return out
57}
58
59// The frame send of an animating flip allocates nothing, full channel or not.
60func TestAnimatingFrameSendAllocations(t *testing.T) {
61 o, _, _ := testOutput(t)
62 frames := make(chan ports.OutputFrame, 1)
63 o.Frames = frames
64 if n := testing.AllocsPerRun(100, func() {
65 o.wakeCore() // fills the channel, or finds it full
66 select {
67 case <-frames:
68 default:
69 }
70 o.wakeCore()
71 }); n != 0 {
72 t.Fatalf("wakeCore: %v allocations", n)
73 }
74}