gen_table_fourcc.py 2.8 KB

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