Ian Romanick | 0f34f3e | 2005-01-07 03:23:59 +0000 | [diff] [blame] | 1 | #!/usr/bin/python2 |
| 2 | |
| 3 | # (C) Copyright IBM Corporation 2004, 2005 |
| 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | # copy of this software and associated documentation files (the "Software"), |
| 8 | # to deal in the Software without restriction, including without limitation |
| 9 | # on the rights to use, copy, modify, merge, publish, distribute, sub |
| 10 | # license, and/or sell copies of the Software, and to permit persons to whom |
| 11 | # the Software is furnished to do so, subject to the following conditions: |
| 12 | # |
| 13 | # The above copyright notice and this permission notice (including the next |
| 14 | # paragraph) shall be included in all copies or substantial portions of the |
| 15 | # Software. |
| 16 | # |
| 17 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
| 20 | # IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 | # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 23 | # IN THE SOFTWARE. |
| 24 | # |
| 25 | # Authors: |
| 26 | # Ian Romanick <idr@us.ibm.com> |
| 27 | |
| 28 | from xml.sax import saxutils |
| 29 | from xml.sax import make_parser |
| 30 | from xml.sax.handler import feature_namespaces |
| 31 | |
| 32 | import gl_XML |
| 33 | import glX_XML |
| 34 | import license |
| 35 | import sys, getopt |
| 36 | |
| 37 | |
| 38 | class glXDocItemFactory(glX_XML.glXItemFactory): |
| 39 | """Factory to create GLX protocol documentation oriented objects derived from glItem.""" |
| 40 | |
| 41 | def create(self, context, name, attrs): |
| 42 | if name == "param": |
| 43 | return glXDocParameter(context, name, attrs) |
| 44 | else: |
| 45 | return glX_XML.glXItemFactory.create(self, context, name, attrs) |
| 46 | |
| 47 | class glXDocParameter(gl_XML.glParameter): |
| 48 | def __init__(self, context, name, attrs): |
| 49 | self.order = 1; |
| 50 | gl_XML.glParameter.__init__(self, context, name, attrs); |
| 51 | |
| 52 | |
| 53 | def packet_type(self): |
| 54 | """Get the type string for the packet header |
| 55 | |
| 56 | GLX protocol documentation uses type names like CARD32, |
| 57 | FLOAT64, LISTofCARD8, and ENUM. This function converts the |
| 58 | type of the parameter to one of these names.""" |
| 59 | |
| 60 | list_of = "" |
| 61 | if self.is_array(): |
| 62 | list_of = "LISTof" |
| 63 | |
| 64 | if self.p_type.glx_name == "": |
| 65 | type_name = "CARD8" |
| 66 | else: |
| 67 | type_name = self.p_type.glx_name |
| 68 | |
| 69 | return "%s%s" % (list_of, type_name) |
| 70 | |
| 71 | def packet_size(self): |
| 72 | p = None |
| 73 | s = self.size() |
| 74 | if s == 0: |
| 75 | a_prod = "n" |
| 76 | b_prod = self.p_type.size |
| 77 | |
Ian Romanick | 3fec8c2 | 2005-02-02 00:54:45 +0000 | [diff] [blame^] | 78 | if not self.count_parameter_list and self.counter: |
Ian Romanick | 0f34f3e | 2005-01-07 03:23:59 +0000 | [diff] [blame] | 79 | a_prod = self.counter |
Ian Romanick | 3fec8c2 | 2005-02-02 00:54:45 +0000 | [diff] [blame^] | 80 | elif self.count_parameter_list and not self.counter or self.is_output: |
Ian Romanick | 0f34f3e | 2005-01-07 03:23:59 +0000 | [diff] [blame] | 81 | pass |
Ian Romanick | 3fec8c2 | 2005-02-02 00:54:45 +0000 | [diff] [blame^] | 82 | elif self.count_parameter_list and self.counter: |
Ian Romanick | 0f34f3e | 2005-01-07 03:23:59 +0000 | [diff] [blame] | 83 | b_prod = self.counter |
| 84 | else: |
| 85 | raise RuntimeError("Parameter '%s' to function '%s' has size 0." % (self.name, self.context.name)) |
| 86 | |
| 87 | ss = "%s*%s" % (a_prod, b_prod) |
| 88 | |
| 89 | return [ss, p] |
| 90 | else: |
| 91 | if s % 4 != 0: |
| 92 | p = "p" |
| 93 | |
| 94 | return [str(s), p] |
| 95 | |
| 96 | class PrintGlxProtoText(glX_XML.GlxProto): |
| 97 | def __init__(self): |
| 98 | glX_XML.GlxProto.__init__(self) |
| 99 | self.factory = glXDocItemFactory() |
| 100 | self.last_category = "" |
| 101 | self.license = "" |
| 102 | |
| 103 | def printHeader(self): |
| 104 | return |
| 105 | |
| 106 | def body_size(self, f): |
| 107 | # At some point, refactor this function and |
| 108 | # glXFunction::command_payload_length. |
| 109 | |
| 110 | size = 0; |
| 111 | size_str = "" |
| 112 | pad_str = "" |
| 113 | plus = "" |
| 114 | for p in f.parameterIterator(1, 2): |
| 115 | [s, pad] = p.packet_size() |
| 116 | try: |
| 117 | size += int(s) |
| 118 | except Exception,e: |
| 119 | size_str += "%s%s" % (plus, s) |
| 120 | plus = "+" |
| 121 | |
| 122 | if pad != None: |
| 123 | pad_str = pad |
| 124 | |
| 125 | return [size, size_str, pad_str] |
| 126 | |
| 127 | def print_render_header(self, f): |
| 128 | [size, size_str, pad_str] = self.body_size(f) |
| 129 | size += 4; |
| 130 | |
| 131 | if size_str == "": |
| 132 | s = "%u" % ((size + 3) & ~3) |
| 133 | elif pad_str != "": |
| 134 | s = "%u+%s+%s" % (size, size_str, pad_str) |
| 135 | else: |
| 136 | s = "%u+%s" % (size, size_str) |
| 137 | |
| 138 | print ' 2 %-15s rendering command length' % (s) |
| 139 | print ' 2 %-4u rendering command opcode' % (f.glx_rop) |
| 140 | return |
| 141 | |
| 142 | |
| 143 | def print_single_header(self, f): |
| 144 | [size, size_str, pad_str] = self.body_size(f) |
| 145 | size = ((size + 3) / 4) + 2; |
| 146 | |
| 147 | if f.glx_vendorpriv != 0: |
| 148 | size += 1 |
| 149 | |
| 150 | print ' 1 CARD8 opcode (X assigned)' |
| 151 | print ' 1 %-4u GLX opcode (%s)' % (f.opcode_real_value(), f.opcode_real_name()) |
| 152 | |
| 153 | if size_str == "": |
| 154 | s = "%u" % (size) |
| 155 | elif pad_str != "": |
| 156 | s = "%u+((%s+%s)/4)" % (size, size_str, pad_str) |
| 157 | else: |
| 158 | s = "%u+((%s)/4)" % (size, size_str) |
| 159 | |
| 160 | print ' 2 %-15s request length' % (s) |
| 161 | |
| 162 | if f.glx_vendorpriv != 0: |
| 163 | print ' 4 %-4u vendor specific opcode' % (f.opcode_value()) |
| 164 | |
| 165 | print ' 4 GLX_CONTEXT_TAG context tag' |
| 166 | |
| 167 | return |
| 168 | |
| 169 | def print_reply(self, f): |
| 170 | print ' =>' |
| 171 | print ' 1 1 reply' |
| 172 | print ' 1 unused' |
| 173 | print ' 2 CARD16 sequence number' |
| 174 | |
| 175 | if f.output == None: |
| 176 | print ' 4 0 reply length' |
| 177 | elif f.reply_always_array: |
| 178 | print ' 4 m reply length' |
| 179 | else: |
| 180 | print ' 4 m reply length, m = (n == 1 ? 0 : n)' |
| 181 | |
| 182 | |
| 183 | unused = 24 |
| 184 | if f.fn_return_type != 'void': |
| 185 | print ' 4 %-15s return value' % (f.fn_return_type) |
| 186 | unused -= 4 |
| 187 | elif f.output != None: |
| 188 | print ' 4 unused' |
| 189 | unused -= 4 |
| 190 | |
| 191 | if f.output != None: |
| 192 | print ' 4 CARD32 n' |
| 193 | unused -= 4 |
| 194 | |
| 195 | if f.output != None: |
| 196 | if not f.reply_always_array: |
| 197 | print '' |
| 198 | print ' if (n = 1) this follows:' |
| 199 | print '' |
| 200 | print ' 4 CARD32 %s' % (f.output.name) |
| 201 | print ' %-2u unused' % (unused - 4) |
| 202 | print '' |
| 203 | print ' otherwise this follows:' |
| 204 | print '' |
| 205 | |
| 206 | print ' %-2u unused' % (unused) |
| 207 | p = f.output |
| 208 | [s, pad] = p.packet_size() |
| 209 | print ' %-8s %-15s %s' % (s, p.packet_type(), p.name) |
| 210 | if pad != None: |
| 211 | try: |
| 212 | bytes = int(s) |
| 213 | bytes = 4 - (bytes & 3) |
| 214 | print ' %-8u %-15s unused' % (bytes, "") |
| 215 | except Exception,e: |
| 216 | print ' %-8s %-15s unused, %s=pad(%s)' % (pad, "", pad, s) |
| 217 | else: |
| 218 | print ' %-2u unused' % (unused) |
| 219 | |
| 220 | |
| 221 | def print_body(self, f): |
| 222 | for p in f.parameterIterator(1, 2): |
| 223 | [s, pad] = p.packet_size() |
| 224 | print ' %-8s %-15s %s' % (s, p.packet_type(), p.name) |
| 225 | if pad != None: |
| 226 | try: |
| 227 | bytes = int(s) |
| 228 | bytes = 4 - (bytes & 3) |
| 229 | print ' %-8u %-15s unused' % (bytes, "") |
| 230 | except Exception,e: |
| 231 | print ' %-8s %-15s unused, %s=pad(%s)' % (pad, "", pad, s) |
| 232 | |
| 233 | def printFunction(self, f): |
| 234 | # At some point this should be expanded to support pixel |
| 235 | # functions, but I'm not going to lose any sleep over it now. |
| 236 | |
Ian Romanick | 3fec8c2 | 2005-02-02 00:54:45 +0000 | [diff] [blame^] | 237 | if f.client_handcode or f.server_handcode or f.vectorequiv or f.image: |
Ian Romanick | 0f34f3e | 2005-01-07 03:23:59 +0000 | [diff] [blame] | 238 | return |
| 239 | |
| 240 | print ' %s' % (f.name) |
| 241 | |
| 242 | if f.glx_rop != 0: |
| 243 | self.print_render_header(f) |
| 244 | else: |
| 245 | self.print_single_header(f) |
| 246 | |
| 247 | self.print_body(f) |
| 248 | |
| 249 | if f.needs_reply(): |
| 250 | self.print_reply(f) |
| 251 | |
| 252 | print '' |
| 253 | return |
| 254 | |
| 255 | |
| 256 | if __name__ == '__main__': |
| 257 | file_name = "gl_API.xml" |
| 258 | |
| 259 | try: |
| 260 | (args, trail) = getopt.getopt(sys.argv[1:], "f:") |
| 261 | except Exception,e: |
| 262 | show_usage() |
| 263 | |
| 264 | for (arg,val) in args: |
| 265 | if arg == "-f": |
| 266 | file_name = val |
| 267 | |
| 268 | dh = PrintGlxProtoText() |
| 269 | |
| 270 | parser = make_parser() |
| 271 | parser.setFeature(feature_namespaces, 0) |
| 272 | parser.setContentHandler(dh) |
| 273 | |
| 274 | f = open(file_name) |
| 275 | |
| 276 | dh.printHeader() |
| 277 | parser.parse(f) |
| 278 | dh.printFooter() |