py: Reorganize source directory
[android/external-libkmsxx.git] / py / tests / cam.py
1 #!/usr/bin/python3
3 import sys
4 import selectors
5 import pykms
6 from helpers import *
9 w = 640
10 h = 480
11 fmt = pykms.PixelFormat.YUYV
14 # This hack makes drm initialize the fbcon, setting up the default connector
15 card = pykms.Card()
16 card = 0
18 card = pykms.Card()
19 res = pykms.ResourceManager(card)
20 conn = res.reserve_connector()
21 crtc = res.reserve_crtc(conn)
22 plane = res.reserve_overlay_plane(crtc, fmt)
24 mode = conn.get_default_mode()
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         set_props(plane, {
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)