blob: fcbaf393a59cd09a69f0084d9a717f097aa34d01 [file] [log] [blame]
Adithya Srinivasan6a9b16e2019-07-10 17:49:49 -07001#!/usr/bin/env python3
2#
3# Copyright 2019 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17# This script provides the functions for generating the null driver
18# framework directly from the vulkan registry (vk.xml).
19
20import generator_common as gencom
21import os
22
23copyright = """/*
24 * Copyright 2015 The Android Open Source Project
25 *
26 * Licensed under the Apache License, Version 2.0 (the "License");
27 * you may not use this file except in compliance with the License.
28 * You may obtain a copy of the License at
29 *
30 * http://www.apache.org/licenses/LICENSE-2.0
31 *
32 * Unless required by applicable law or agreed to in writing, software
33 * distributed under the License is distributed on an "AS IS" BASIS,
34 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35 * See the License for the specific language governing permissions and
36 * limitations under the License.
37 */
38
39"""
40
41def isDriverExtension(extensionName):
42 switchCase = {
43 'VK_ANDROID_native_buffer' : True,
44 'VK_EXT_debug_report' : True,
45 'VK_KHR_get_physical_device_properties2' : True
46 }
47
48 if extensionName in switchCase:
49 return switchCase[extensionName]
50 return False
51
52def isDriverFunction(functionName):
53 if functionName in gencom.extensionsDict:
54 return isDriverExtension(gencom.extensionsDict[functionName])
55 return True
56
57def null_driver_genh():
58 header = """#ifndef NULLDRV_NULL_DRIVER_H
59#define NULLDRV_NULL_DRIVER_H 1
60
61#include <vulkan/vk_android_native_buffer.h>
62#include <vulkan/vulkan.h>
63
64namespace null_driver {
65
66PFN_vkVoidFunction GetGlobalProcAddr(const char* name);
67PFN_vkVoidFunction GetInstanceProcAddr(const char* name);
68
69"""
70 genfile = os.path.join(os.path.dirname(__file__),'..','nulldrv','null_driver_gen2.h')
71 with open(genfile, 'w') as f:
72 f.write (copyright)
73 f.write (gencom.warning)
74 f.write (header)
75 gencom.clang_off(f,0)
76
77 for cmds in gencom.allCommandsList:
78 if isDriverFunction(cmds):
79 paramList = [''.join(i) for i in gencom.paramDict[cmds]]
80 f.write ('VKAPI_ATTR ' + gencom.returnTypeDict[cmds] + ' ' + cmds[2:] + '(' +', '.join(paramList) + ');\n')
81 f.write ("""VKAPI_ATTR VkResult GetSwapchainGrallocUsageANDROID(VkDevice device, VkFormat format, VkImageUsageFlags imageUsage, int* grallocUsage);
82VKAPI_ATTR VkResult AcquireImageANDROID(VkDevice device, VkImage image, int nativeFenceFd, VkSemaphore semaphore, VkFence fence);
83VKAPI_ATTR VkResult QueueSignalReleaseImageANDROID(VkQueue queue, uint32_t waitSemaphoreCount, const VkSemaphore* pWaitSemaphores, VkImage image, int* pNativeFenceFd);\n""")
84 gencom.clang_on(f,0)
85
86 f.write ('\n} // namespace null_driver\n')
87 f.write ('\n#endif // NULLDRV_NULL_DRIVER_H\n')
88
89def null_driver_gencpp():
90 header = """#include <algorithm>
91
92#include "null_driver_gen.h"
93
94using namespace null_driver;
95
96namespace {
97
98struct NameProc {
99 const char* name;
100 PFN_vkVoidFunction proc;
101};
102
103PFN_vkVoidFunction Lookup(const char* name,
104 const NameProc* begin,
105 const NameProc* end) {
106 const auto& entry = std::lower_bound(
107 begin, end, name,
108 [](const NameProc& e, const char* n) { return strcmp(e.name, n) < 0; });
109 if (entry == end || strcmp(entry->name, name) != 0)
110 return nullptr;
111 return entry->proc;
112}
113
114template <size_t N>
115PFN_vkVoidFunction Lookup(const char* name, const NameProc (&procs)[N]) {
116 return Lookup(name, procs, procs + N);
117}
118
119const NameProc kGlobalProcs[] = {
120"""
121 genfile = os.path.join(os.path.dirname(__file__),'..','nulldrv','null_driver_gen2.cpp')
122 with open(genfile, 'w') as f:
123 f.write (copyright)
124 f.write (gencom.warning)
125 f.write (header)
126 gencom.clang_off(f,1)
127
128 sortedCommandsList = sorted(gencom.allCommandsList)
129 for cmds in sortedCommandsList:
130 if isDriverFunction(cmds) and gencom.getDispatchTableType(cmds) == 'Global':
131 f.write (gencom.clang_off_spaces + '{\"' + cmds + '\", reinterpret_cast<PFN_vkVoidFunction>(static_cast<PFN_' + cmds + '>(' + cmds[2:] + '))},\n')
132 gencom.clang_on(f,1)
133 f.write ('};\n\n')
134
135 f.write ('const NameProc kInstanceProcs[] = {\n')
136 gencom.clang_off(f,1)
137 for cmds in sortedCommandsList:
138 if isDriverFunction(cmds):
139 f.write (gencom.clang_off_spaces + '{\"' + cmds + '\", reinterpret_cast<PFN_vkVoidFunction>(static_cast<PFN_' + cmds + '>(' + cmds[2:] + '))},\n')
140 gencom.clang_on(f,1)
141 f.write ('};\n\n} // namespace\n\n')
142
143 f.write ("""namespace null_driver {
144
145PFN_vkVoidFunction GetGlobalProcAddr(const char* name) {
146 return Lookup(name, kGlobalProcs);
147}
148
149PFN_vkVoidFunction GetInstanceProcAddr(const char* name) {
150 return Lookup(name, kInstanceProcs);
151}
152
153} // namespace null_driver\n""")
154