py: drop the fancy event handling
[android/external-libkmsxx.git] / py / tests / db.py
1 #!/usr/bin/python3
3 import sys
4 import pykms
5 import selectors
7 bar_width = 20
8 bar_speed = 8
10 class FlipHandler():
11     def __init__(self):
12         super().__init__()
13         self.bar_xpos = 0
14         self.front_buf = 0
15         self.fb1 = pykms.DumbFramebuffer(card, mode.hdisplay, mode.vdisplay, "XR24");
16         self.fb2 = pykms.DumbFramebuffer(card, mode.hdisplay, mode.vdisplay, "XR24");
17         self.flips = 0
18         self.frames = 0
19         self.time = 0
21     def handle_page_flip(self, frame, time):
22         self.flips += 1
23         if self.time == 0:
24             self.frames = frame
25             self.time = time
27         time_delta = time - self.time
28         if time_delta >= 5:
29             frame_delta = frame - self.frames
30             print("Frame rate: %f (%u/%u frames in %f s)" %
31                   (frame_delta / time_delta, self.flips, frame_delta, time_delta))
33             self.flips = 0
34             self.frames = frame
35             self.time = time
37         if self.front_buf == 0:
38             fb = self.fb2
39         else:
40             fb = self.fb1
42         self.front_buf = self.front_buf ^ 1
44         current_xpos = self.bar_xpos;
45         old_xpos = (current_xpos + (fb.width - bar_width - bar_speed)) % (fb.width - bar_width);
46         new_xpos = (current_xpos + bar_speed) % (fb.width - bar_width);
48         self.bar_xpos = new_xpos
50         pykms.draw_color_bar(fb, old_xpos, new_xpos, bar_width)
52         if card.has_atomic:
53             ctx = pykms.AtomicReq(card)
54             ctx.add(crtc.primary_plane, "FB_ID", fb.id)
55             ctx.commit()
56         else:
57             crtc.page_flip(fb)
59 if len(sys.argv) > 1:
60     conn_name = sys.argv[1]
61 else:
62     conn_name = ''
64 card = pykms.Card()
65 res = pykms.ResourceManager(card)
66 conn = res.reserve_connector(conn_name)
67 crtc = res.reserve_crtc(conn)
68 mode = conn.get_default_mode()
70 fliphandler = FlipHandler()
72 crtc.set_mode(conn, fliphandler.fb1, mode)
74 fliphandler.handle_page_flip(0, 0)
76 def readdrm(fileobj, mask):
77     #print("EVENT");
78     for ev in card.read_events():
79         if ev.type == pykms.DrmEventType.FLIP_COMPLETE:
80             fliphandler.handle_page_flip(ev.seq, ev.time)
83 def readkey(fileobj, mask):
84     #print("KEY EVENT");
85     sys.stdin.readline()
86     exit(0)
88 sel = selectors.DefaultSelector()
89 sel.register(card.fd, selectors.EVENT_READ, readdrm)
90 sel.register(sys.stdin, selectors.EVENT_READ, readkey)
92 while True:
93     events = sel.select()
94     for key, mask in events:
95         callback = key.data
96         callback(key.fileobj, mask)