1#!/usr/bin/python3 2 3import sys 4import pykms 5import selectors 6 7bar_width = 20 8bar_speed = 8 9 10class 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 20 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 26 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)) 32 33 self.flips = 0 34 self.frames = frame 35 self.time = time 36 37 if self.front_buf == 0: 38 fb = self.fb2 39 else: 40 fb = self.fb1 41 42 self.front_buf = self.front_buf ^ 1 43 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); 47 48 self.bar_xpos = new_xpos 49 50 pykms.draw_color_bar(fb, old_xpos, new_xpos, bar_width) 51 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) 58 59if len(sys.argv) > 1: 60 conn_name = sys.argv[1] 61else: 62 conn_name = '' 63 64card = pykms.Card() 65res = pykms.ResourceManager(card) 66conn = res.reserve_connector(conn_name) 67crtc = res.reserve_crtc(conn) 68mode = conn.get_default_mode() 69 70fliphandler = FlipHandler() 71 72crtc.set_mode(conn, fliphandler.fb1, mode) 73 74fliphandler.handle_page_flip(0, 0) 75 76def 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) 81 82 83def readkey(fileobj, mask): 84 #print("KEY EVENT"); 85 sys.stdin.readline() 86 exit(0) 87 88sel = selectors.DefaultSelector() 89sel.register(card.fd, selectors.EVENT_READ, readdrm) 90sel.register(sys.stdin, selectors.EVENT_READ, readkey) 91 92while True: 93 events = sel.select() 94 for key, mask in events: 95 callback = key.data 96 callback(key.fileobj, mask) 97