xref: /aosp_15_r20/external/libkmsxx/py/tests/rottest.py (revision f0687c8a10b3e371dbe09214db6664e37c283cca)
1#!/usr/bin/python3
2
3import pykms
4from enum import Enum
5
6import termios, sys, os, tty
7
8card = pykms.OmapCard()
9
10res = pykms.ResourceManager(card)
11conn = res.reserve_connector()
12crtc = res.reserve_crtc(conn)
13mode = conn.get_default_mode()
14modeb = mode.to_blob(card)
15rootplane = res.reserve_primary_plane(crtc, pykms.PixelFormat.XRGB8888)
16plane = res.reserve_overlay_plane(crtc, pykms.PixelFormat.NV12)
17
18card.disable_planes()
19
20req = pykms.AtomicReq(card)
21
22req.add(conn, "CRTC_ID", crtc.id)
23
24req.add(crtc, {"ACTIVE": 1,
25		"MODE_ID": modeb.id})
26
27# This enables the root plane
28
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#		"zpos": 0})
43
44req.commit_sync(allow_modeset = True)
45
46def show_rot_plane(crtc, plane, fb, rot, x_scale, y_scale):
47
48	crtc_w = int(fb_w * x_scale)
49	crtc_h = int(fb_h * y_scale)
50
51	if (rot & pykms.Rotation.ROTATE_90) or (rot & pykms.Rotation.ROTATE_270):
52		tmp = crtc_w
53		crtc_w = crtc_h
54		crtc_h = tmp
55
56	crtc_x = int(mode.hdisplay / 2 - crtc_w / 2)
57	crtc_y = int(mode.vdisplay / 2 - crtc_h / 2)
58
59	req = pykms.AtomicReq(card)
60
61	src_x = 0
62	src_y = 0
63	src_w = fb_w - src_x
64	src_h = fb_h - src_y
65
66	print("SRC {},{}-{}x{}  DST {},{}-{}x{}".format(
67		src_x, src_y, src_w, src_h,
68		crtc_x, crtc_y, crtc_w, crtc_h))
69
70	angle_str = pykms.Rotation(rot & pykms.Rotation.ROTATE_MASK).name
71	reflect_x_str = "REFLECT_X" if rot & pykms.Rotation.REFLECT_X else ""
72	reflect_y_str = "REFLECT_Y" if rot & pykms.Rotation.REFLECT_Y else ""
73
74	print("{} {} {}".format(angle_str, reflect_x_str, reflect_y_str))
75
76	sys.stdout.flush()
77
78	req.add(plane, {"FB_ID": fb.id,
79			"CRTC_ID": crtc.id,
80			"SRC_X": src_x << 16,
81			"SRC_Y": src_y << 16,
82			"SRC_W": src_w << 16,
83			"SRC_H": src_h << 16,
84			"CRTC_X": crtc_x,
85			"CRTC_Y": crtc_y,
86			"CRTC_W": crtc_w,
87			"CRTC_H": crtc_h,
88			"rotation": rot,
89			"zpos": 2})
90
91	req.commit_sync(allow_modeset = True)
92
93
94fb_w = 480
95fb_h = 150
96x_scale = 1
97y_scale = 1
98
99fb = pykms.OmapFramebuffer(card, fb_w, fb_h, "NV12", flags = pykms.OmapFramebuffer.Tiled);
100#fb = pykms.DumbFramebuffer(card, fb_w, fb_h, "NV12")
101pykms.draw_test_pattern(fb);
102
103def even(i):
104	return i & ~1
105
106pykms.draw_text(fb, even((fb_w // 2) - (8 * 3) // 2), 4, "TOP", pykms.white)
107pykms.draw_text(fb, even((fb_w // 2) - (8 * 6) // 2), fb_h - 8 - 4, "BOTTOM", pykms.white)
108pykms.draw_text(fb, 4, even(((fb_h // 2) - 4)), "L", pykms.white)
109pykms.draw_text(fb, fb_w - 8 - 4, even(((fb_h // 2) - 4)), "R", pykms.white)
110
111rots = [ pykms.Rotation.ROTATE_0, pykms.Rotation.ROTATE_90, pykms.Rotation.ROTATE_180, pykms.Rotation.ROTATE_270 ]
112cursors = [ "A", "D", "B", "C" ]
113
114print("Use the cursor keys, x and y to change rotation. Press q to quit.")
115
116fd = sys.stdin.fileno()
117oldterm = termios.tcgetattr(fd)
118tty.setcbreak(fd)
119
120try:
121	esc_seq = 0
122
123	current_rot = pykms.Rotation.ROTATE_0
124
125	show_rot_plane(crtc, plane, fb, current_rot, x_scale, y_scale)
126
127	while True:
128		c = sys.stdin.read(1)
129		#print("Got character {}".format(repr(c)))
130
131		changed = False
132		handled = False
133
134		if esc_seq == 0:
135			if c == "\x1b":
136				esc_seq = 1
137				handled = True
138		elif esc_seq == 1:
139			if c == "[":
140				esc_seq = 2
141				handled = True
142			else:
143				esc_seq = 0
144		elif esc_seq == 2:
145			esc_seq = 0
146
147			if c in cursors:
148				handled = True
149
150				rot = rots[cursors.index(c)]
151
152				current_rot &= ~pykms.Rotation.ROTATE_MASK
153				current_rot |= rot
154
155				changed = True
156
157		if not handled:
158			if c == "q":
159				break
160			elif c == "x":
161				current_rot ^= pykms.Rotation.REFLECT_X
162				changed = True
163			elif c == "y":
164				current_rot ^= pykms.Rotation.REFLECT_Y
165				changed = True
166
167		if changed:
168			show_rot_plane(crtc, plane, fb, current_rot, x_scale, y_scale)
169
170finally:
171	termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
172