1package wayland
2
3import (
4 "testing"
5
6 "github.com/bnema/go-wayland-bindings/server/linuxdmabuf"
7 "github.com/bnema/neferwl/internal/ports"
8 "golang.org/x/sys/unix"
9)
10
11func TestYUVParams(t *testing.T) {
12 for _, tc := range []struct {
13 name string
14 format uint32
15 two bool
16 modMismatch bool
17 short bool
18 errCode uint32
19 }{
20 {name: "NV12", format: fourccNV12, two: true},
21 {name: "P010", format: fourccP010, two: true},
22 {name: "missing plane", format: fourccNV12, errCode: uint32(linuxdmabuf.ZwpLinuxBufferParamsV1ErrorIncomplete)},
23 {name: "extra plane", format: fourccNV12, two: true, errCode: uint32(linuxdmabuf.ZwpLinuxBufferParamsV1ErrorIncomplete)},
24 {name: "modifier mismatch", format: fourccNV12, two: true, modMismatch: true, errCode: uint32(linuxdmabuf.ZwpLinuxBufferParamsV1ErrorInvalidFormat)},
25 {name: "P010 short chroma", format: fourccP010, two: true, short: true, errCode: uint32(linuxdmabuf.ZwpLinuxBufferParamsV1ErrorOutOfBounds)},
26 } {
27 t.Run(tc.name, func(t *testing.T) {
28 s, _, _, dir := dmabufServer(t, ports.DMABufSupport{Formats: []ports.DMABufFormat{{Format: fourccNV12}, {Format: fourccP010}}})
29 c := protocolClient(t, s, dir)
30 dm := bindVersion(t, c, "zwp_linux_dmabuf_v1", 4)
31 p := c.AllocateID()
32 requestProtocol(t, c, dm, linuxdmabuf.ZwpLinuxDmabufV1RequestCreateParams, p)
33 proxy := ¶msProxy{result: make(chan uint16, 2)}
34 proxy.SetID(p)
35 registerWireProxy(c, proxy)
36 fd, err := unix.MemfdCreate("yuv-params", 0)
37 if err != nil {
38 t.Fatal(err)
39 }
40 defer unix.Close(fd)
41 if err := unix.Ftruncate(fd, 8192); err != nil {
42 t.Fatal(err)
43 }
44 add := func(idx, offset, stride, mod uint32) {
45 t.Helper()
46 if err := wireRequest(c, p, uint16(linuxdmabuf.ZwpLinuxBufferParamsV1RequestAdd), []int{fd}, idx, offset, stride, uint32(0), mod); err != nil {
47 t.Fatal(err)
48 }
49 }
50 stride := uint32(64)
51 if tc.format == fourccP010 {
52 stride = 128
53 }
54 add(0, 0, stride, 0)
55 if tc.two {
56 mod := uint32(0)
57 if tc.modMismatch {
58 mod = 1
59 }
60 offset := uint32(1024)
61 if tc.short {
62 offset = 8180
63 }
64 add(1, offset, stride, mod)
65 }
66 if tc.name == "extra plane" {
67 add(2, 4096, 64, 0)
68 }
69 if tc.modMismatch {
70 expectProtocolError(t, c, p, tc.errCode)
71 return
72 }
73 requestProtocol(t, c, p, linuxdmabuf.ZwpLinuxBufferParamsV1RequestCreate, int32(64), int32(16), tc.format, uint32(0))
74 if tc.errCode != 0 {
75 expectProtocolError(t, c, p, tc.errCode)
76 return
77 }
78 if err := c.Roundtrip(); err != nil {
79 t.Fatal(err)
80 }
81 if op := <-proxy.result; op != uint16(linuxdmabuf.ZwpLinuxBufferParamsV1EventCreated) {
82 t.Fatalf("created event %d", op)
83 }
84 })
85 }
86}