1import argparse
2import copy
3import re
4import xml.etree.ElementTree as et
5
6def _bool_to_c_expr(b):
7    if b is True:
8        return 'true'
9    if b is False:
10        return 'false'
11    return b
12
13class Extension:
14    def __init__(self, name, ext_version, enable):
15        self.name = name
16        self.ext_version = int(ext_version)
17        self.enable = _bool_to_c_expr(enable)
18
19class ApiVersion:
20    def __init__(self, version, enable):
21        self.version = version
22        self.enable = _bool_to_c_expr(enable)
23
24class VkVersion:
25    def __init__(self, string):
26        split = string.split('.')
27        self.major = int(split[0])
28        self.minor = int(split[1])
29        if len(split) > 2:
30            assert len(split) == 3
31            self.patch = int(split[2])
32        else:
33            self.patch = None
34
35        # Sanity check.  The range bits are required by the definition of the
36        # VK_MAKE_VERSION macro
37        assert self.major < 1024 and self.minor < 1024
38        assert self.patch is None or self.patch < 4096
39        assert(str(self) == string)
40
41    def __str__(self):
42        ver_list = [str(self.major), str(self.minor)]
43        if self.patch is not None:
44            ver_list.append(str(self.patch))
45        return '.'.join(ver_list)
46
47    def c_vk_version(self):
48        patch = self.patch if self.patch is not None else 0
49        ver_list = [str(self.major), str(self.minor), str(patch)]
50        return 'VK_MAKE_VERSION(' + ', '.join(ver_list) + ')'
51
52    def __int_ver(self):
53        # This is just an expansion of VK_VERSION
54        patch = self.patch if self.patch is not None else 0
55        return (self.major << 22) | (self.minor << 12) | patch
56
57    def __gt__(self, other):
58        # If only one of them has a patch version, "ignore" it by making
59        # other's patch version match self.
60        if (self.patch is None) != (other.patch is None):
61            other = copy.copy(other)
62            other.patch = self.patch
63
64        return self.__int_ver() > other.__int_ver()
65
66def init_exts_from_xml(xml, extensions, platform_defines):
67    """ Walk the Vulkan XML and fill out extra extension information. """
68
69    xml = et.parse(xml)
70
71    ext_name_map = {}
72    for ext in extensions:
73        ext_name_map[ext.name] = ext
74
75    # KHR_display is missing from the list.
76    platform_defines.append('VK_USE_PLATFORM_DISPLAY_KHR')
77    for platform in xml.findall('./platforms/platform'):
78        platform_defines.append(platform.attrib['protect'])
79
80    for ext_elem in xml.findall('.extensions/extension'):
81        ext_name = ext_elem.attrib['name']
82        if ext_name not in ext_name_map:
83            continue
84
85        ext = ext_name_map[ext_name]
86        ext.type = ext_elem.attrib['type']
87
88# Mapping between extension name and the android version in which the extension
89# was whitelisted in Android CTS.
90allowed_android_version = {
91    # Allowed Instance KHR Extensions
92    "VK_KHR_surface": 26,
93    "VK_KHR_display": 26,
94    "VK_KHR_android_surface": 26,
95    "VK_KHR_mir_surface": 26,
96    "VK_KHR_wayland_surface": 26,
97    "VK_KHR_win32_surface": 26,
98    "VK_KHR_xcb_surface": 26,
99    "VK_KHR_xlib_surface": 26,
100    "VK_KHR_get_physical_device_properties2": 26,
101    "VK_KHR_get_surface_capabilities2": 26,
102    "VK_KHR_external_memory_capabilities": 28,
103    "VK_KHR_external_semaphore_capabilities": 28,
104    "VK_KHR_external_fence_capabilities": 28,
105    "VK_KHR_device_group_creation": 28,
106    "VK_KHR_get_display_properties2": 29,
107    "VK_KHR_surface_protected_capabilities": 29,
108
109    # Allowed Device KHR Extensions
110    "VK_KHR_swapchain": 26,
111    "VK_KHR_display_swapchain": 26,
112    "VK_KHR_sampler_mirror_clamp_to_edge": 26,
113    "VK_KHR_shader_draw_parameters": 26,
114    "VK_KHR_shader_float_controls": 29,
115    "VK_KHR_shader_float16_int8": 29,
116    "VK_KHR_maintenance1": 26,
117    "VK_KHR_push_descriptor": 26,
118    "VK_KHR_descriptor_update_template": 26,
119    "VK_KHR_incremental_present": 26,
120    "VK_KHR_shared_presentable_image": 26,
121    "VK_KHR_storage_buffer_storage_class": 28,
122    "VK_KHR_8bit_storage": 29,
123    "VK_KHR_16bit_storage": 28,
124    "VK_KHR_get_memory_requirements2": 28,
125    "VK_KHR_external_memory": 28,
126    "VK_KHR_external_memory_fd": 28,
127    "VK_KHR_external_memory_win32": 28,
128    "VK_KHR_external_semaphore": 28,
129    "VK_KHR_external_semaphore_fd": 28,
130    "VK_KHR_external_semaphore_win32": 28,
131    "VK_KHR_external_fence": 28,
132    "VK_KHR_external_fence_fd": 28,
133    "VK_KHR_external_fence_win32": 28,
134    "VK_KHR_win32_keyed_mutex": 28,
135    "VK_KHR_dedicated_allocation": 28,
136    "VK_KHR_variable_pointers": 28,
137    "VK_KHR_relaxed_block_layout": 28,
138    "VK_KHR_bind_memory2": 28,
139    "VK_KHR_maintenance2": 28,
140    "VK_KHR_image_format_list": 28,
141    "VK_KHR_sampler_ycbcr_conversion": 28,
142    "VK_KHR_device_group": 28,
143    "VK_KHR_multiview": 28,
144    "VK_KHR_maintenance3": 28,
145    "VK_KHR_draw_indirect_count": 28,
146    "VK_KHR_create_renderpass2": 28,
147    "VK_KHR_depth_stencil_resolve": 29,
148    "VK_KHR_driver_properties": 28,
149    "VK_KHR_swapchain_mutable_format": 29,
150    "VK_KHR_shader_atomic_int64": 29,
151    "VK_KHR_vulkan_memory_model": 29,
152
153    "VK_GOOGLE_display_timing": 26,
154    "VK_ANDROID_native_buffer": 26,
155    "VK_ANDROID_external_memory_android_hardware_buffer": 28,
156}
157
158# Extensions with these prefixes are checked in Android CTS, and thus must be
159# whitelisted per the preceding dict.
160android_extension_whitelist_prefixes = (
161    "VK_KHX",
162    "VK_KHR",
163    "VK_GOOGLE",
164    "VK_ANDROID"
165)
166
167def get_extension_condition(ext_name, condition):
168    """ If |ext_name| is an extension that Android CTS cares about, prepend
169        a condition to ensure that the extension is only enabled for Android
170        versions in which the extension is whitelisted in CTS. """
171    if not ext_name.startswith(android_extension_whitelist_prefixes):
172        return condition
173    allowed_version = allowed_android_version.get(ext_name, 9999)
174    return "(!ANDROID || ANDROID_API_LEVEL >= %d) && (%s)" % (allowed_version,
175                                                              condition)
176