libdrm: NOTE! Default branch is now main
[libdrm.git] / gen_table_fourcc.py
blob4e8bc4096f5ac86bd5277008957cfebc83a6a283
1 #!/usr/bin/env python3
3 # Copyright 2021 Collabora, Ltd.
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice (including the
14 # next paragraph) shall be included in all copies or substantial
15 # portions of the Software.
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 # SOFTWARE.
26 # Helper script that reads drm_fourcc.h and writes a static table with the
27 # simpler format token modifiers
29 import sys
30 import re
32 filename = sys.argv[1]
33 towrite = sys.argv[2]
35 fm_re = {
36 'intel': r'^#define I915_FORMAT_MOD_(\w+)',
37 'others': r'^#define DRM_FORMAT_MOD_((?:ARM|SAMSUNG|QCOM|VIVANTE|NVIDIA|BROADCOM|ALLWINNER)\w+)\s',
38 'vendors': r'^#define DRM_FORMAT_MOD_VENDOR_(\w+)'
41 def print_fm_intel(f, f_mod):
42 f.write(' {{ DRM_MODIFIER_INTEL({}, {}) }},\n'.format(f_mod, f_mod))
44 # generic write func
45 def print_fm(f, vendor, mod, f_name):
46 f.write(' {{ DRM_MODIFIER({}, {}, {}) }},\n'.format(vendor, mod, f_name))
48 with open(filename, "r") as f:
49 data = f.read()
50 for k, v in fm_re.items():
51 fm_re[k] = re.findall(v, data, flags=re.M)
53 with open(towrite, "w") as f:
54 f.write('''\
55 /* AUTOMATICALLY GENERATED by gen_table_fourcc.py. You should modify
56 that script instead of adding here entries manually! */
57 static const struct drmFormatModifierInfo drm_format_modifier_table[] = {
58 ''')
59 f.write(' { DRM_MODIFIER_INVALID(NONE, INVALID_MODIFIER) },\n')
60 f.write(' { DRM_MODIFIER_LINEAR(NONE, LINEAR) },\n')
62 for entry in fm_re['intel']:
63 print_fm_intel(f, entry)
65 for entry in fm_re['others']:
66 (vendor, mod) = entry.split('_', 1)
67 if vendor == 'ARM' and (mod == 'TYPE_AFBC' or mod == 'TYPE_MISC'):
68 continue
69 print_fm(f, vendor, mod, mod)
71 f.write('''\
73 ''')
75 f.write('''\
76 static const struct drmFormatModifierVendorInfo drm_format_modifier_vendor_table[] = {
77 ''')
79 for entry in fm_re['vendors']:
80 f.write(" {{ DRM_FORMAT_MOD_VENDOR_{}, \"{}\" }},\n".format(entry, entry))
82 f.write('''\
84 ''')