Jesse Hall | 3703f7f | 2014-05-13 21:52:56 -0700 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2014 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 | from __future__ import print_function |
| 18 | from operator import itemgetter |
| 19 | import collections |
| 20 | import os.path |
| 21 | import re |
| 22 | import sys |
| 23 | |
| 24 | |
| 25 | # Avoid endlessly adding to the path if this module is imported multiple |
| 26 | # times, e.g. in an interactive session |
| 27 | regpath = os.path.join(sys.path[0], "registry") |
| 28 | if sys.path[1] != regpath: |
| 29 | sys.path.insert(1, regpath) |
| 30 | import reg |
| 31 | |
| 32 | |
| 33 | def nonestr(s): |
| 34 | return s if s else "" |
| 35 | |
| 36 | |
| 37 | def parseTypedName(elem): |
| 38 | type = [nonestr(elem.text)] |
| 39 | name = None |
| 40 | for subelem in elem: |
| 41 | text = nonestr(subelem.text) |
| 42 | tail = nonestr(subelem.tail) |
| 43 | if subelem.tag == 'name': |
| 44 | name = text |
| 45 | break |
| 46 | else: |
| 47 | type.extend([text, tail]) |
| 48 | return (''.join(type).strip(), name) |
| 49 | |
| 50 | |
| 51 | # Format a list of (type, name) tuples as a C-style parameter list |
| 52 | def fmtParams(params): |
| 53 | if not params: |
| 54 | return 'void' |
| 55 | return ', '.join(['%s %s' % (p[0], p[1]) for p in params]) |
| 56 | |
| 57 | # Format a list of (type, name) tuples as a C-style argument list |
| 58 | def fmtArgs(params): |
| 59 | return ', '.join(p[1] for p in params) |
| 60 | |
| 61 | # Format a list of (type, name) tuples as comma-separated '"type", name' |
| 62 | def fmtTypeNameList(params): |
| 63 | return ', '.join(['"%s", %s' % (p[0], p[1]) for p in params]) |
| 64 | |
| 65 | |
| 66 | def overrideSymbolName(sym): |
| 67 | # The wrapper intercepts glGetString and (sometimes) calls the generated |
| 68 | # __glGetString thunk which dispatches to the driver's glGetString |
| 69 | if sym == 'glGetString': |
| 70 | return '__glGetString' |
| 71 | else: |
| 72 | return sym |
| 73 | |
| 74 | |
| 75 | # Generate API trampoline templates: |
| 76 | # <rtype> API_ENTRY(<name>)(<params>) { |
| 77 | # CALL_GL_API(<name>, <args>); |
| 78 | # // or |
| 79 | # CALL_GL_API_RETURN(<name>, <args>); |
| 80 | # } |
| 81 | class TrampolineGen(reg.OutputGenerator): |
| 82 | def __init__(self): |
| 83 | reg.OutputGenerator.__init__(self, sys.stderr, sys.stderr, None) |
| 84 | |
| 85 | def genCmd(self, cmd, name): |
| 86 | reg.OutputGenerator.genCmd(self, cmd, name) |
| 87 | |
| 88 | rtype, fname = parseTypedName(cmd.elem.find('proto')) |
| 89 | params = [parseTypedName(p) for p in cmd.elem.findall('param')] |
| 90 | |
| 91 | call = 'CALL_GL_API' if rtype == 'void' else 'CALL_GL_API_RETURN' |
| 92 | print('%s API_ENTRY(%s)(%s) {\n' |
| 93 | ' %s(%s%s%s);\n' |
| 94 | '}' |
| 95 | % (rtype, overrideSymbolName(fname), fmtParams(params), |
| 96 | call, fname, |
| 97 | ', ' if len(params) > 0 else '', |
| 98 | fmtArgs(params)), |
| 99 | file=self.outFile) |
| 100 | |
| 101 | |
| 102 | |
| 103 | # Collect all API prototypes across all families, remove duplicates, |
| 104 | # emit to entries.in and trace.in files. |
| 105 | class ApiGenerator(reg.OutputGenerator): |
| 106 | def __init__(self): |
| 107 | reg.OutputGenerator.__init__(self, sys.stderr, sys.stderr, None) |
| 108 | self.cmds = [] |
| 109 | self.enums = collections.OrderedDict() |
| 110 | |
| 111 | def genCmd(self, cmd, name): |
| 112 | reg.OutputGenerator.genCmd(self, cmd, name) |
| 113 | rtype, fname = parseTypedName(cmd.elem.find('proto')) |
| 114 | params = [parseTypedName(p) for p in cmd.elem.findall('param')] |
| 115 | self.cmds.append({'rtype': rtype, 'name': fname, 'params': params}) |
| 116 | |
| 117 | def genEnum(self, enuminfo, name): |
| 118 | reg.OutputGenerator.genEnum(self, enuminfo, name) |
| 119 | value = enuminfo.elem.get('value') |
| 120 | |
| 121 | # Skip bitmask enums. Pattern matches: |
| 122 | # - GL_DEPTH_BUFFER_BIT |
| 123 | # - GL_MAP_INVALIDATE_BUFFER_BIT_EXT |
| 124 | # - GL_COLOR_BUFFER_BIT1_QCOM |
| 125 | # but not |
| 126 | # - GL_DEPTH_BITS |
| 127 | # - GL_QUERY_COUNTER_BITS_EXT |
| 128 | # |
| 129 | # TODO: Assuming a naming pattern and using a regex is what the |
| 130 | # old glenumsgen script did. But the registry XML knows which enums are |
| 131 | # parts of bitmask groups, so we should just use that. I'm not sure how |
| 132 | # to get the information out though, and it's not critical right now, |
| 133 | # so leaving for later. |
| 134 | if re.search('_BIT($|\d*_)', name): |
| 135 | return |
| 136 | |
| 137 | # Skip non-hex values (GL_TRUE, GL_FALSE, header guard junk) |
| 138 | if not re.search('0x[0-9A-Fa-f]+', value): |
| 139 | return |
| 140 | |
| 141 | # Append 'u' or 'ull' type suffix if present |
| 142 | type = enuminfo.elem.get('type') |
| 143 | if type and type != 'i': |
| 144 | value += type |
| 145 | |
| 146 | if value not in self.enums: |
| 147 | self.enums[value] = name |
| 148 | |
| 149 | def finish(self): |
| 150 | # sort by function name, remove duplicates |
| 151 | self.cmds.sort(key=itemgetter('name')) |
| 152 | cmds = [] |
| 153 | for cmd in self.cmds: |
| 154 | if len(cmds) == 0 or cmd != cmds[-1]: |
| 155 | cmds.append(cmd) |
| 156 | self.cmds = cmds |
| 157 | |
| 158 | # Write entries.in |
| 159 | def writeEntries(self, outfile): |
| 160 | for cmd in self.cmds: |
| 161 | print('GL_ENTRY(%s, %s, %s)' |
| 162 | % (cmd['rtype'], cmd['name'], fmtParams(cmd['params'])), |
| 163 | file=outfile) |
| 164 | |
| 165 | # Write traces.in |
| 166 | def writeTrace(self, outfile): |
| 167 | for cmd in self.cmds: |
| 168 | if cmd['rtype'] == 'void': |
| 169 | ret = '_VOID(' |
| 170 | else: |
| 171 | ret = '(%s, ' % cmd['rtype'] |
| 172 | |
| 173 | params = cmd['params'] |
| 174 | if len(params) > 0: |
| 175 | typeNameList = ', ' + fmtTypeNameList(params) |
| 176 | else: |
| 177 | typeNameList = '' |
| 178 | |
| 179 | print('TRACE_GL%s%s, (%s), (%s), %d%s)' |
| 180 | % (ret, cmd['name'], |
| 181 | fmtParams(params), fmtArgs(params), |
| 182 | len(params), typeNameList), |
| 183 | file=outfile) |
| 184 | |
| 185 | # Write enums.in |
| 186 | def writeEnums(self, outfile): |
| 187 | for enum in self.enums.iteritems(): |
| 188 | print('GL_ENUM(%s,%s)' % (enum[0], enum[1]), file=outfile) |
| 189 | |
| 190 | |
| 191 | if __name__ == '__main__': |
| 192 | registry = reg.Registry() |
| 193 | registry.loadFile('registry/gl.xml') |
| 194 | |
| 195 | registry.setGenerator(TrampolineGen()) |
| 196 | TRAMPOLINE_OPTIONS = [ |
| 197 | reg.GeneratorOptions( |
| 198 | apiname = 'gles1', |
| 199 | profile = 'common', |
| 200 | filename = '../../libs/GLES_CM/gl_api.in'), |
| 201 | reg.GeneratorOptions( |
| 202 | apiname = 'gles1', |
| 203 | profile = 'common', |
| 204 | emitversions = None, |
| 205 | defaultExtensions = 'gles1', |
| 206 | filename = '../../libs/GLES_CM/glext_api.in'), |
| 207 | reg.GeneratorOptions( |
| 208 | apiname = 'gles2', |
| 209 | versions = '(2|3)\.0', |
| 210 | profile = 'common', |
| 211 | filename = '../../libs/GLES2/gl2_api.in'), |
| 212 | reg.GeneratorOptions( |
| 213 | apiname = 'gles2', |
| 214 | versions = '(2|3)\.0', |
| 215 | profile = 'common', |
| 216 | emitversions = None, |
| 217 | defaultExtensions = 'gles2', |
| 218 | filename = '../../libs/GLES2/gl2ext_api.in')] |
| 219 | for opts in TRAMPOLINE_OPTIONS: |
| 220 | registry.apiGen(opts) |
| 221 | |
| 222 | apigen = ApiGenerator() |
| 223 | registry.setGenerator(apigen) |
| 224 | API_OPTIONS = [ |
| 225 | # Generate non-extension versions of each API first, then extensions, |
| 226 | # so that if an extension enum was later standardized, we see the non- |
| 227 | # suffixed version first. |
| 228 | reg.GeneratorOptions( |
| 229 | apiname = 'gles1', |
| 230 | profile = 'common'), |
| 231 | reg.GeneratorOptions( |
| 232 | apiname = 'gles2', |
| 233 | versions = '2\.0|3\.0', |
| 234 | profile = 'common'), |
| 235 | reg.GeneratorOptions( |
| 236 | apiname = 'gles1', |
| 237 | profile = 'common', |
| 238 | emitversions = None, |
| 239 | defaultExtensions = 'gles1'), |
| 240 | reg.GeneratorOptions( |
| 241 | apiname = 'gles2', |
| 242 | versions = '2\.0|3\.0', |
| 243 | profile = 'common', |
| 244 | emitversions = None, |
| 245 | defaultExtensions = 'gles2')] |
| 246 | for opts in API_OPTIONS: |
| 247 | registry.apiGen(opts) |
| 248 | apigen.finish() |
| 249 | with open('../../libs/entries.in', 'w') as f: |
| 250 | apigen.writeEntries(f) |
| 251 | with open('../../libs/trace.in', 'w') as f: |
| 252 | apigen.writeTrace(f) |
| 253 | with open('../../libs/enums.in', 'w') as f: |
| 254 | apigen.writeEnums(f) |