glgen.py revision 6cb7d3dc9f1ce37547646979e0625992264ea4d0
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
17from __future__ import print_function
18from operator import itemgetter
19import collections
20import os.path
21import re
22import sys
23
24
25# Avoid endlessly adding to the path if this module is imported multiple
26# times, e.g. in an interactive session
27regpath = os.path.join(sys.path[0], "registry")
28if sys.path[1] != regpath:
29    sys.path.insert(1, regpath)
30import reg
31
32
33def nonestr(s):
34    return s if s else ""
35
36
37def 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
52def 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
58def 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'
62def fmtTypeNameList(params):
63    return ', '.join(['"%s", %s' % (p[0], p[1]) for p in params])
64
65
66def 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#   }
81class 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.
105class 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
191if __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            profile             = 'common',
210            filename            = '../../libs/GLES2/gl2_api.in'),
211        reg.GeneratorOptions(
212            apiname             = 'gles2',
213            profile             = 'common',
214            emitversions        = None,
215            defaultExtensions   = 'gles2',
216            filename            = '../../libs/GLES2/gl2ext_api.in')]
217    for opts in TRAMPOLINE_OPTIONS:
218        registry.apiGen(opts)
219
220    apigen = ApiGenerator()
221    registry.setGenerator(apigen)
222    API_OPTIONS = [
223        # Generate non-extension versions of each API first, then extensions,
224        # so that if an extension enum was later standardized, we see the non-
225        # suffixed version first.
226        reg.GeneratorOptions(
227            apiname             = 'gles1',
228            profile             = 'common'),
229        reg.GeneratorOptions(
230            apiname             = 'gles2',
231            profile             = 'common'),
232        reg.GeneratorOptions(
233            apiname             = 'gles1',
234            profile             = 'common',
235            emitversions        = None,
236            defaultExtensions   = 'gles1'),
237        reg.GeneratorOptions(
238            apiname             = 'gles2',
239            profile             = 'common',
240            emitversions        = None,
241            defaultExtensions   = 'gles2')]
242    for opts in API_OPTIONS:
243        registry.apiGen(opts)
244    apigen.finish()
245    with open('../../libs/entries.in', 'w') as f:
246        apigen.writeEntries(f)
247    with open('../../libs/trace.in', 'w') as f:
248        apigen.writeTrace(f)
249    with open('../../libs/enums.in', 'w') as f:
250        apigen.writeEnums(f)
251