~emersion/go-drm

go-drm/fourcc.py -rwxr-xr-x 4.8 KiB
7088ebfeSimon Ser Add support for Amlogic format modifiers a month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python3

import os
import subprocess
import sys
import tempfile

cc = os.getenv("CC", "cc")
pkg_config = os.getenv("PKG_CONFIG", "pkg-config")
gofmt = os.getenv("GOFMT", "gofmt")

version = subprocess.check_output([pkg_config, "libdrm", "--modversion"]).decode().strip()
cflags = subprocess.check_output([pkg_config, "libdrm", "--cflags-only-I"]).decode().strip().split()

found = False
for flag in cflags:
	if not flag.startswith("-I"):
		raise Exception("Expected an include dir for libdrm")
	libdrm_include = flag[2:]
	fourcc_include = libdrm_include + "/drm_fourcc.h"
	if os.path.exists(fourcc_include):
		found = True
		break
if not found:
	raise Exception("Failed to find drm_fourcc.h in libdrm -I cflags")

prefixes = ["DRM_FORMAT_", "I915_FORMAT_", "AFBC_FORMAT_MOD_", "AMLOGIC_FBC_"]

ident_list = []
with open(fourcc_include) as input_file:
	for l in input_file.readlines():
		parts = l.split()
		if len(parts) < 3 or parts[0] != "#define":
			continue
		ident = parts[1]

		found = False
		for prefix in prefixes:
			if ident.startswith(prefix):
				found = True
				break
		if not found:
			continue

		if "(" in ident:
			# Some complicated formats use a macro with parameters
			continue

		ident_list.append(ident)

idents = {}
with tempfile.TemporaryDirectory() as work_dir:
	c_file_name = work_dir + "/main.c"
	exe_file_name = work_dir + "/main"

	with open(c_file_name, "w+") as c_file:
		c_file.write('#include <inttypes.h>\n')
		c_file.write('#include <stdint.h>\n')
		c_file.write('#include <stdio.h>\n')
		c_file.write('#include "' + fourcc_include + '"\n')
		c_file.write('\n')
		c_file.write('int main(void) {\n')
		for ident in ident_list:
			c_file.write('printf("0x%" PRIX64 "\\n", (uint64_t)' + ident + ');\n')
		c_file.write('}\n')


	subprocess.check_call([cc, "-Wall", "-Wextra", "-o", exe_file_name, c_file_name] + cflags)
	output = subprocess.check_output([exe_file_name]).decode().strip()
	for (i, l) in enumerate(output.splitlines()):
		idents[ident_list[i]] = l

types = ["Format", "ModifierVendor", "Modifier", "armModifierType", "armAFBC", "amlogicFBCLayout", "amlogicFBCOption"]
words = ["none", "invalid", "linear"]
initialisms = ["AMD", "NVIDIA", "ARM"]

def to_camel_case(s):
	parts = s.split("_")
	for (i, part) in enumerate(parts):
		if not part in initialisms:
			parts[i] = part.lower().capitalize()
	return "".join(parts)

consts = {}
for (ident, val) in idents.items():
	if "_MOD_" in ident:
		t = "Modifier"
		if ident.startswith("DRM_FORMAT_MOD_"):
			ident = ident[len("DRM_FORMAT_MOD_"):]
			if ident.startswith("ARM_TYPE_"):
				t = "armModifierType"
				ident = ident[len("ARM_TYPE_"):]
			elif ident.startswith("VENDOR_"):
				t = "ModifierVendor"
				ident = ident[len("VENDOR_"):]
		elif ident.startswith("AFBC_FORMAT_MOD_"):
			t = "armAFBC"
			ident = ident[len("AFBC_FORMAT_MOD"):]
		else:
			parts = ident.split("_FORMAT_MOD_")
			assert(len(parts) == 2)
			ident = parts[0] + "_" + parts[1]
		if ident in ["NONE"]:
			continue
	elif ident.startswith("DRM_FORMAT_"):
		t = "Format"
		ident = ident[len("DRM_FORMAT_"):]
		if ident in ["BIG_ENDIAN", "RESERVED"]:
			continue
	elif ident.startswith("AMLOGIC_FBC_LAYOUT_"):
		t = "amlogicFBCLayout"
		ident = ident[len("AMLOGIC_FBC_LAYOUT_"):]
	elif ident.startswith("AMLOGIC_FBC_OPTION_"):
		t = "amlogicFBCOption"
		ident = ident[len("AMLOGIC_FBC_OPTION_"):]
	else:
		assert(False)

	name = ident
	if ident.lower() in words:
		name = ident.lower()
	elif t == "ModifierVendor":
		name = to_camel_case(ident)

	if t == "ModifierVendor" or ident.lower() in words:
		ident = to_camel_case(ident)

	if t not in consts:
		consts[t] = []
	consts[t].append({"name": name, "go_ident": t + ident, "value": val, "type": t})

# Some constants are aliases. Detect and remove these.
for t, l in consts.items():
	seen = set()
	out = []
	for cst in l:
		if cst["value"] in seen:
			continue
		seen.add(cst["value"])
		out.append(cst)
	consts[t] = out

stringers = {"Format", "ModifierVendor", "Modifier"}
with open("fourcc_generated.go", "w+") as go_file:
	go_file.write('// Code generated by fourcc.py - DO NOT EDIT\n')
	go_file.write('// libdrm ' + version + '\n')
	go_file.write('\n')
	go_file.write('package drm\n')
	for t in types:
		go_file.write('\n')
		go_file.write('const (\n')
		for cst in consts[t]:
			go_file.write('\t' + cst["go_ident"] + ' ' + cst["type"] + ' = ' + cst["value"] + '\n')
		go_file.write(')\n')
		go_file.write('\n')

		if t not in stringers:
			continue

		go_file.write('func (v ' + t + ') str() string {\n')
		go_file.write('\tswitch v {\n')
		for cst in consts[t]:
			go_file.write('\tcase ' + cst["go_ident"] + ':\n')
			go_file.write('\t\treturn "' + cst["name"] + '"\n')
		go_file.write('\tdefault:\n')
		go_file.write('\t\treturn ""\n')
		go_file.write('\t}\n')
		go_file.write('}\n')

subprocess.check_call([gofmt, "-w", "fourcc_generated.go"])