57d0c1a502a0f801cc6e866e5db979c7a01fa42a
[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
12 # This hack makes drm initialize the fbcon, setting up the default connector
13 card = pykms.Card()
14 card = 0
16 card = pykms.Card()
17 res = pykms.ResourceManager(card)
18 conn = res.reserve_connector()
19 crtc = res.reserve_crtc(conn)
20 plane = res.reserve_overlay_plane(crtc, fmt)
22 mode = conn.get_default_mode()
24 NUM_BUFS = 5
26 fbs = []
27 for i in range(NUM_BUFS):
28     fb = pykms.DumbFramebuffer(card, w, h, fmt)
29     fbs.append(fb)
31 vidpath = pykms.VideoDevice.get_capture_devices()[0]
33 vid = pykms.VideoDevice(vidpath)
34 cap = vid.capture_streamer
35 cap.set_port(0)
36 cap.set_format(fmt, w, h)
37 cap.set_queue_size(NUM_BUFS)
39 for fb in fbs:
40     cap.queue(fb)
42 cap.stream_on()
45 def readvid(conn, mask):
46     fb = cap.dequeue()
48     if card.has_atomic:
49         plane.set_props({
50             "FB_ID": fb.id,
51             "CRTC_ID": crtc.id,
52             "SRC_W": fb.width << 16,
53             "SRC_H": fb.height << 16,
54             "CRTC_W": fb.width,
55             "CRTC_H": fb.height,
56         })
57     else:
58         crtc.set_plane(plane, fb, 0, 0, fb.width, fb.height,
59             0, 0, fb.width, fb.height)
61     cap.queue(fb)
63 def readkey(conn, mask):
64     #print("KEY EVENT");
65     sys.stdin.readline()
66     exit(0)
68 sel = selectors.DefaultSelector()
69 sel.register(cap.fd, selectors.EVENT_READ, readvid)
70 sel.register(sys.stdin, selectors.EVENT_READ, readkey)
72 while True:
73     events = sel.select()
74     for key, mask in events:
75         callback = key.data
76         callback(key.fileobj, mask)