glthread: don't prefix variable_data with const
[mesa.git] / src / mapi / glapi / gen / gl_marshal.py
index a50d773e27e69c9647110347e1e062d325cb763e..9d5d5ad9e2718fe1efc38a5d849648991aaf0ec1 100644 (file)
@@ -1,4 +1,3 @@
-#!/usr/bin/env python
 
 # Copyright (C) 2012 Intel Corporation
 #
@@ -21,6 +20,8 @@
 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 # IN THE SOFTWARE.
 
+from __future__ import print_function
+
 import contextlib
 import getopt
 import gl_XML
@@ -43,9 +44,9 @@ current_indent = 0
 
 def out(str):
     if str:
-        print ' '*current_indent + str
+        print(' '*current_indent + str)
     else:
-        print ''
+        print('')
 
 
 @contextlib.contextmanager
@@ -65,17 +66,15 @@ class PrintCode(gl_XML.gl_print_base):
             'Copyright (C) 2012 Intel Corporation', 'INTEL CORPORATION')
 
     def printRealHeader(self):
-        print header
-        print '#include <X11/Xlib-xcb.h>'
-        print
-        print 'static _X_INLINE int safe_mul(int a, int b)'
-        print '{'
-        print '    if (a < 0 || b < 0) return -1;'
-        print '    if (a == 0 || b == 0) return 0;'
-        print '    if (a > INT_MAX / b) return -1;'
-        print '    return a * b;'
-        print '}'
-        print
+        print(header)
+        print('static inline int safe_mul(int a, int b)')
+        print('{')
+        print('    if (a < 0 || b < 0) return -1;')
+        print('    if (a == 0 || b == 0) return 0;')
+        print('    if (a > INT_MAX / b) return -1;')
+        print('    return a * b;')
+        print('}')
+        print()
 
     def printRealFooter(self):
         pass
@@ -89,7 +88,7 @@ class PrintCode(gl_XML.gl_print_base):
             out('return {0};'.format(call))
 
     def print_sync_dispatch(self, func):
-        out('_mesa_glthread_finish(ctx);')
+        out('debug_print_sync_fallback("{0}");'.format(func.name))
         self.print_sync_call(func)
 
     def print_sync_body(self, func):
@@ -175,18 +174,26 @@ class PrintCode(gl_XML.gl_print_base):
         with indent():
             for p in func.fixed_params:
                 if p.count:
-                    out('const {0} * {1} = cmd->{1};'.format(
-                            p.get_base_type_string(), p.name))
+                    p_decl = '{0} * {1} = cmd->{1};'.format(
+                            p.get_base_type_string(), p.name)
                 else:
-                    out('const {0} {1} = cmd->{1};'.format(
-                            p.type_string(), p.name))
+                    p_decl = '{0} {1} = cmd->{1};'.format(
+                            p.type_string(), p.name)
+
+                if not p_decl.startswith('const '):
+                    # Declare all local function variables as const, even if
+                    # the original parameter is not const.
+                    p_decl = 'const ' + p_decl
+
+                out(p_decl)
+
             if func.variable_params:
                 for p in func.variable_params:
-                    out('const {0} * {1};'.format(
+                    out('{0} * {1};'.format(
                             p.get_base_type_string(), p.name))
                 out('const char *variable_data = (const char *) (cmd + 1);')
                 for p in func.variable_params:
-                    out('{0} = (const {1} *) variable_data;'.format(
+                    out('{0} = ({1} *) variable_data;'.format(
                             p.name, p.get_base_type_string()))
 
                     if p.img_null_flag:
@@ -202,7 +209,7 @@ class PrintCode(gl_XML.gl_print_base):
             self.print_sync_call(func)
         out('}')
 
-    def validate_count_or_return(self, func):
+    def validate_count_or_fallback(self, func):
         # Check that any counts for variable-length arguments might be < 0, in
         # which case the command alloc or the memcpy would blow up before we
         # get to the validation in Mesa core.
@@ -210,12 +217,14 @@ class PrintCode(gl_XML.gl_print_base):
             if p.is_variable_length():
                 out('if (unlikely({0} < 0)) {{'.format(p.size_string()))
                 with indent():
-                    out('_mesa_glthread_finish(ctx);')
-                    out('_mesa_error(ctx, GL_INVALID_VALUE, "{0}({1} < 0)");'.format(func.name, p.size_string()))
-                    out('return;')
+                    out('goto fallback_to_sync;')
                 out('}')
+                return True
+        return False
+
 
     def print_async_marshal(self, func):
+        need_fallback_sync = False
         out('static void GLAPIENTRY')
         out('_mesa_marshal_{0}({1})'.format(
                 func.name, func.get_parameter_string()))
@@ -234,28 +243,34 @@ class PrintCode(gl_XML.gl_print_base):
 
             out('debug_print_marshal("{0}");'.format(func.name))
 
-            self.validate_count_or_return(func)
+            need_fallback_sync = self.validate_count_or_fallback(func)
 
             if func.marshal_fail:
                 out('if ({0}) {{'.format(func.marshal_fail))
                 with indent():
-                    out('_mesa_glthread_destroy(ctx);')
+                    out('_mesa_glthread_finish(ctx);')
+                    out('_mesa_glthread_restore_dispatch(ctx, __func__);')
                     self.print_sync_dispatch(func)
                     out('return;')
                 out('}')
 
-            out('if (cmd_size <= MARSHAL_MAX_CMD_SIZE) {')
+        if len(func.variable_params) > 0:
             with indent():
-                self.print_async_dispatch(func)
-            out('} else {')
+                out('if (cmd_size <= MARSHAL_MAX_CMD_SIZE) {')
+                with indent():
+                    self.print_async_dispatch(func)
+                    out('return;')
+                out('}')
+            out('')
+            if need_fallback_sync:
+                out('fallback_to_sync:')
             with indent():
+                out('_mesa_glthread_finish(ctx);')
                 self.print_sync_dispatch(func)
-            out('}')
-
-            if func.marshal == 'draw':
-                out('/* We relied on all vertex and index data being in VBOs */')
-                out('assert(ctx->API == API_OPENGL_CORE);')
-
+        else:
+            with indent():
+                self.print_async_dispatch(func)
+                assert not need_fallback_sync
         out('}')
 
     def print_async_body(self, func):
@@ -333,7 +348,7 @@ class PrintCode(gl_XML.gl_print_base):
 
 
 def show_usage():
-    print 'Usage: %s [-f input_file_name]' % sys.argv[0]
+    print('Usage: %s [-f input_file_name]' % sys.argv[0])
     sys.exit(1)
 
 
@@ -342,7 +357,7 @@ if __name__ == '__main__':
 
     try:
         (args, trail) = getopt.getopt(sys.argv[1:], 'm:f:')
-    except Exception,e:
+    except Exception:
         show_usage()
 
     for (arg,val) in args: