blob: ee8762e3b43b9c3db040c8f882c398d2134a5984 [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"""
Adithya Srinivasan8dce9d72019-07-11 14:26:04 -070070 genfile = os.path.join(os.path.dirname(__file__),'..','nulldrv','null_driver_gen.h')
Adithya Srinivasan6a9b16e2019-07-10 17:49:49 -070071 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')
Adithya Srinivasan8dce9d72019-07-11 14:26:04 -070088 f.close()
89 gencom.runClangFormat(genfile)
Adithya Srinivasan6a9b16e2019-07-10 17:49:49 -070090
91def null_driver_gencpp():
92 header = """#include <algorithm>
93
94#include "null_driver_gen.h"
95
96using namespace null_driver;
97
98namespace {
99
100struct NameProc {
101 const char* name;
102 PFN_vkVoidFunction proc;
103};
104
105PFN_vkVoidFunction Lookup(const char* name,
106 const NameProc* begin,
107 const NameProc* end) {
108 const auto& entry = std::lower_bound(
109 begin, end, name,
110 [](const NameProc& e, const char* n) { return strcmp(e.name, n) < 0; });
111 if (entry == end || strcmp(entry->name, name) != 0)
112 return nullptr;
113 return entry->proc;
114}
115
116template <size_t N>
117PFN_vkVoidFunction Lookup(const char* name, const NameProc (&procs)[N]) {
118 return Lookup(name, procs, procs + N);
119}
120
121const NameProc kGlobalProcs[] = {
122"""
Adithya Srinivasan8dce9d72019-07-11 14:26:04 -0700123 genfile = os.path.join(os.path.dirname(__file__),'..','nulldrv','null_driver_gen.cpp')
Adithya Srinivasan6a9b16e2019-07-10 17:49:49 -0700124 with open(genfile, 'w') as f:
125 f.write (copyright)
126 f.write (gencom.warning)
127 f.write (header)
128 gencom.clang_off(f,1)
129
130 sortedCommandsList = sorted(gencom.allCommandsList)
131 for cmds in sortedCommandsList:
132 if isDriverFunction(cmds) and gencom.getDispatchTableType(cmds) == 'Global':
133 f.write (gencom.clang_off_spaces + '{\"' + cmds + '\", reinterpret_cast<PFN_vkVoidFunction>(static_cast<PFN_' + cmds + '>(' + cmds[2:] + '))},\n')
134 gencom.clang_on(f,1)
135 f.write ('};\n\n')
136
137 f.write ('const NameProc kInstanceProcs[] = {\n')
138 gencom.clang_off(f,1)
139 for cmds in sortedCommandsList:
140 if isDriverFunction(cmds):
141 f.write (gencom.clang_off_spaces + '{\"' + cmds + '\", reinterpret_cast<PFN_vkVoidFunction>(static_cast<PFN_' + cmds + '>(' + cmds[2:] + '))},\n')
142 gencom.clang_on(f,1)
143 f.write ('};\n\n} // namespace\n\n')
144
145 f.write ("""namespace null_driver {
146
147PFN_vkVoidFunction GetGlobalProcAddr(const char* name) {
148 return Lookup(name, kGlobalProcs);
149}
150
151PFN_vkVoidFunction GetInstanceProcAddr(const char* name) {
152 return Lookup(name, kInstanceProcs);
153}
154
155} // namespace null_driver\n""")
Adithya Srinivasan8dce9d72019-07-11 14:26:04 -0700156 f.close()
157 gencom.runClangFormat(genfile)
Adithya Srinivasan6a9b16e2019-07-10 17:49:49 -0700158