cam.py: fix initial modeset
[android/external-libkmsxx.git] / py / tests / cam.py
1 #!/usr/bin/python3
3 import sys
4 import selectors
5 import pykms
7 w = 640
8 h = 480
9 fmt = pykms.PixelFormat.YUYV
11 card = pykms.Card()
12 res = pykms.ResourceManager(card)
13 conn = res.reserve_connector()
14 crtc = res.reserve_crtc(conn)
15 plane = res.reserve_overlay_plane(crtc, fmt)
17 mode = conn.get_default_mode()
18 modeb = mode.to_blob(card)
20 req = pykms.AtomicReq(card)
21 req.add(conn, "CRTC_ID", crtc.id)
22 req.add(crtc, {"ACTIVE": 1,
23         "MODE_ID": modeb.id})
24 req.commit_sync(allow_modeset = True)
26 NUM_BUFS = 5
28 fbs = []
29 for i in range(NUM_BUFS):
30     fb = pykms.DumbFramebuffer(card, w, h, fmt)
31     fbs.append(fb)
33 vidpath = pykms.VideoDevice.get_capture_devices()[0]
35 vid = pykms.VideoDevice(vidpath)
36 cap = vid.capture_streamer
37 cap.set_port(0)
38 cap.set_format(fmt, w, h)
39 cap.set_queue_size(NUM_BUFS)
41 for fb in fbs:
42     cap.queue(fb)
44 cap.stream_on()
47 def readvid(conn, mask):
48     fb = cap.dequeue()
50     if card.has_atomic:
51         plane.set_props({
52             "FB_ID": fb.id,
53             "CRTC_ID": crtc.id,
54             "SRC_W": fb.width << 16,
55             "SRC_H": fb.height << 16,
56             "CRTC_W": fb.width,
57             "CRTC_H": fb.height,
58         })
59     else:
60         crtc.set_plane(plane, fb, 0, 0, fb.width, fb.height,
61             0, 0, fb.width, fb.height)
63     cap.queue(fb)
65 def readkey(conn, mask):
66     #print("KEY EVENT");
67     sys.stdin.readline()
68     exit(0)
70 sel = selectors.DefaultSelector()
71 sel.register(cap.fd, selectors.EVENT_READ, readvid)
72 sel.register(sys.stdin, selectors.EVENT_READ, readkey)
74 while True:
75     events = sel.select()
76     for key, mask in events:
77         callback = key.data
78         callback(key.fileobj, mask)