py: add rottest.py
[android/external-libkmsxx.git] / py / tests / rottest.py
1 #!/usr/bin/python3
3 import pykms
4 from enum import Enum
6 import termios, sys, os, tty
8 card = pykms.OmapCard()
10 res = pykms.ResourceManager(card)
11 conn = res.reserve_connector()
12 crtc = res.reserve_crtc(conn)
13 mode = conn.get_default_mode()
14 modeb = mode.to_blob(card)
15 rootplane = res.reserve_primary_plane(crtc, pykms.PixelFormat.XRGB8888)
16 plane = res.reserve_overlay_plane(crtc, pykms.PixelFormat.NV12)
18 card.disable_planes()
20 req = pykms.AtomicReq(card)
22 req.add(conn, "CRTC_ID", crtc.id)
24 req.add(crtc, {"ACTIVE": 1,
25                 "MODE_ID": modeb.id})
27 # This enables the root plane
29 #rootfb = pykms.OmapFramebuffer(card, mode.hdisplay, mode.vdisplay, "XR24");
30 #pykms.draw_test_pattern(rootfb);
31 #
32 #req.add(rootplane, {"FB_ID": rootfb.id,
33 #               "CRTC_ID": crtc.id,
34 #               "SRC_X": 0 << 16,
35 #               "SRC_Y": 0 << 16,
36 #               "SRC_W": mode.hdisplay << 16,
37 #               "SRC_H": mode.vdisplay << 16,
38 #               "CRTC_X": 0,
39 #               "CRTC_Y": 0,
40 #               "CRTC_W": mode.hdisplay,
41 #               "CRTC_H": mode.vdisplay,
42 #               "zorder": 0})
44 req.commit_sync(allow_modeset = True)
46 class Rotation(int, Enum):
47         ROTATE_0 = 1 << 0
48         ROTATE_90 = 1 << 1
49         ROTATE_180 = 1 << 2
50         ROTATE_270 = 1 << 3
51         ROTATE_MASK = ROTATE_0 | ROTATE_90 | ROTATE_180 | ROTATE_270
52         REFLECT_X = 1 << 4
53         REFLECT_Y = 1 << 5
54         REFLECT_MASK = REFLECT_X | REFLECT_Y
57 def show_rot_plane(crtc, plane, fb, rot, x_scale, y_scale):
59         crtc_w = int(fb_w * x_scale)
60         crtc_h = int(fb_h * y_scale)
62         if (rot & Rotation.ROTATE_90) or (rot & Rotation.ROTATE_270):
63                 tmp = crtc_w
64                 crtc_w = crtc_h
65                 crtc_h = tmp
67         crtc_x = int(mode.hdisplay / 2 - crtc_w / 2)
68         crtc_y = int(mode.vdisplay / 2 - crtc_h / 2)
70         req = pykms.AtomicReq(card)
72         src_x = 0
73         src_y = 0
74         src_w = fb_w - src_x
75         src_h = fb_h - src_y
77         print("SRC {},{}-{}x{}  DST {},{}-{}x{}".format(
78                 src_x, src_y, src_w, src_h,
79                 crtc_x, crtc_y, crtc_w, crtc_h))
81         angle_str = Rotation(rot & Rotation.ROTATE_MASK).name
82         reflect_x_str = "REFLECT_X" if rot & Rotation.REFLECT_X else ""
83         reflect_y_str = "REFLECT_Y" if rot & Rotation.REFLECT_Y else ""
85         print("{} {} {}".format(angle_str, reflect_x_str, reflect_y_str))
87         sys.stdout.flush()
89         req.add(plane, {"FB_ID": fb.id,
90                         "CRTC_ID": crtc.id,
91                         "SRC_X": src_x << 16,
92                         "SRC_Y": src_y << 16,
93                         "SRC_W": src_w << 16,
94                         "SRC_H": src_h << 16,
95                         "CRTC_X": crtc_x,
96                         "CRTC_Y": crtc_y,
97                         "CRTC_W": crtc_w,
98                         "CRTC_H": crtc_h,
99                         "rotation": rot,
100                         "zorder": 2})
102         req.commit_sync(allow_modeset = True)
105 fb_w = 480
106 fb_h = 150
107 x_scale = 1
108 y_scale = 1
110 fb = pykms.OmapFramebuffer(card, fb_w, fb_h, "NV12", tiled = True);
111 #fb = pykms.DumbFramebuffer(card, fb_w, fb_h, "NV12")
112 pykms.draw_test_pattern(fb);
114 def even(i):
115         return i & ~1
117 pykms.draw_text(fb, even((fb_w // 2) - (8 * 3) // 2), 4, "TOP", pykms.white)
118 pykms.draw_text(fb, even((fb_w // 2) - (8 * 6) // 2), fb_h - 8 - 4, "BOTTOM", pykms.white)
119 pykms.draw_text(fb, 4, even(((fb_h // 2) - 4)), "L", pykms.white)
120 pykms.draw_text(fb, fb_w - 8 - 4, even(((fb_h // 2) - 4)), "R", pykms.white)
122 rots = [ Rotation.ROTATE_0, Rotation.ROTATE_90, Rotation.ROTATE_180, Rotation.ROTATE_270 ]
123 cursors = [ "A", "D", "B", "C" ]
125 print("Use the cursor keys, x and y to change rotation. Press q to quit.")
127 fd = sys.stdin.fileno()
128 oldterm = termios.tcgetattr(fd)
129 tty.setcbreak(fd)
131 try:
132         esc_seq = 0
134         current_rot = Rotation.ROTATE_0
136         show_rot_plane(crtc, plane, fb, current_rot, x_scale, y_scale)
138         while True:
139                 c = sys.stdin.read(1)
140                 #print("Got character {}".format(repr(c)))
142                 changed = False
143                 handled = False
145                 if esc_seq == 0:
146                         if c == "\x1b":
147                                 esc_seq = 1
148                                 handled = True
149                 elif esc_seq == 1:
150                         if c == "[":
151                                 esc_seq = 2
152                                 handled = True
153                         else:
154                                 esc_seq = 0
155                 elif esc_seq == 2:
156                         esc_seq = 0
158                         if c in cursors:
159                                 handled = True
161                                 rot = rots[cursors.index(c)]
163                                 current_rot &= ~Rotation.ROTATE_MASK
164                                 current_rot |= rot
166                                 changed = True
168                 if not handled:
169                         if c == "q":
170                                 break
171                         elif c == "x":
172                                 current_rot ^= Rotation.REFLECT_X
173                                 changed = True
174                         elif c == "y":
175                                 current_rot ^= Rotation.REFLECT_Y
176                                 changed = True
178                 if changed:
179                         show_rot_plane(crtc, plane, fb, current_rot, x_scale, y_scale)
181 finally:
182         termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)