Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(25)

Side by Side Diff: ui/gl/generate_bindings.py

Issue 2629633003: Refactor GL bindings so there is no global GLApi or DriverGL. (Closed)
Patch Set: rebase Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/gl/BUILD.gn ('k') | ui/gl/gl_api_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 """code generator for GL/GLES extension wrangler.""" 6 """code generator for GL/GLES extension wrangler."""
7 7
8 import optparse 8 import optparse
9 import os 9 import os
10 import collections 10 import collections
(...skipping 2538 matching lines...) Expand 10 before | Expand all | Expand 10 after
2549 """ 2549 """
2550 2550
2551 #include <string> 2551 #include <string>
2552 2552
2553 %s 2553 %s
2554 2554
2555 namespace gl { 2555 namespace gl {
2556 """ % includes_string) 2556 """ % includes_string)
2557 2557
2558 file.write('\n') 2558 file.write('\n')
2559 file.write('static bool g_debugBindingsInitialized;\n') 2559 if set_name != 'gl':
2560 file.write('Driver%s g_driver_%s;\n' % (set_name.upper(), set_name.lower())) 2560 file.write('Driver%s g_driver_%s;\n' % (set_name.upper(), set_name.lower()))
2561 file.write('\n') 2561 file.write('\n')
2562 2562
2563 # Write stub functions that take the place of some functions before a context 2563 # Write stub functions that take the place of some functions before a context
2564 # is initialized. This is done to provide clear asserts on debug build and to 2564 # is initialized. This is done to provide clear asserts on debug build and to
2565 # avoid crashing in case of a bug on release build. 2565 # avoid crashing in case of a bug on release build.
2566 file.write('\n') 2566 file.write('\n')
2567 num_dynamic = 0 2567 num_dynamic = 0
2568 for func in functions: 2568 for func in functions:
2569 static_binding = GetStaticBinding(func) 2569 static_binding = GetStaticBinding(func)
2570 if static_binding: 2570 if static_binding:
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
2643 if 'client_extensions' in func['versions'][0]: 2643 if 'client_extensions' in func['versions'][0]:
2644 assert len(func['versions']) == 1 2644 assert len(func['versions']) == 1
2645 return True 2645 return True
2646 return False 2646 return False
2647 2647
2648 file.write("}\n\n"); 2648 file.write("}\n\n");
2649 2649
2650 if set_name == 'gl': 2650 if set_name == 'gl':
2651 file.write("""\ 2651 file.write("""\
2652 void DriverGL::InitializeDynamicBindings( 2652 void DriverGL::InitializeDynamicBindings(
2653 GLContext* context) { 2653 const GLVersionInfo* ver, const std::string& context_extensions) {
2654 DCHECK(context && context->IsCurrent(NULL)); 2654 std::string extensions = context_extensions + " ";
2655 const GLVersionInfo* ver = context->GetVersionInfo();
2656 ALLOW_UNUSED_LOCAL(ver);
2657 std::string extensions = context->GetExtensions() + " ";
2658 ALLOW_UNUSED_LOCAL(extensions); 2655 ALLOW_UNUSED_LOCAL(extensions);
2659 2656
2660 """) 2657 """)
2661 elif set_name == 'egl': 2658 elif set_name == 'egl':
2662 file.write("""\ 2659 file.write("""\
2663 void DriverEGL::InitializeClientExtensionBindings() { 2660 void DriverEGL::InitializeClientExtensionBindings() {
2664 std::string client_extensions(GetClientExtensions()); 2661 std::string client_extensions(GetClientExtensions());
2665 client_extensions += " "; 2662 client_extensions += " ";
2666 ALLOW_UNUSED_LOCAL(client_extensions); 2663 ALLOW_UNUSED_LOCAL(client_extensions);
2667 2664
(...skipping 10 matching lines...) Expand all
2678 def OutputExtensionBindings(extension_var, extensions, extension_funcs): 2675 def OutputExtensionBindings(extension_var, extensions, extension_funcs):
2679 # Extra space at the end of the extension name is intentional, 2676 # Extra space at the end of the extension name is intentional,
2680 # it is used as a separator 2677 # it is used as a separator
2681 for extension in extensions: 2678 for extension in extensions:
2682 file.write(' ext.b_%s = %s.find("%s ") != std::string::npos;\n' % 2679 file.write(' ext.b_%s = %s.find("%s ") != std::string::npos;\n' %
2683 (extension, extension_var, extension)) 2680 (extension, extension_var, extension))
2684 2681
2685 for func in extension_funcs: 2682 for func in extension_funcs:
2686 if not 'static_binding' in func: 2683 if not 'static_binding' in func:
2687 file.write('\n') 2684 file.write('\n')
2688 file.write(' debug_fn.%sFn = 0;\n' % func['known_as'])
2689 WriteConditionalFuncBinding(file, func) 2685 WriteConditionalFuncBinding(file, func)
2690 2686
2691 OutputExtensionBindings( 2687 OutputExtensionBindings(
2692 'client_extensions', 2688 'client_extensions',
2693 sorted(used_client_extensions), 2689 sorted(used_client_extensions),
2694 [ f for f in functions if IsClientExtensionFunc(f) ]) 2690 [ f for f in functions if IsClientExtensionFunc(f) ])
2695 2691
2696 if set_name == 'egl': 2692 if set_name == 'egl':
2697 file.write("""\ 2693 file.write("""\
2698 } 2694 }
2699 2695
2700 void DriverEGL::InitializeExtensionBindings() { 2696 void DriverEGL::InitializeExtensionBindings() {
2701 std::string extensions(GetPlatformExtensions()); 2697 std::string extensions(GetPlatformExtensions());
2702 extensions += " "; 2698 extensions += " ";
2703 ALLOW_UNUSED_LOCAL(extensions); 2699 ALLOW_UNUSED_LOCAL(extensions);
2704 2700
2705 """) 2701 """)
2706 2702
2707 OutputExtensionBindings( 2703 OutputExtensionBindings(
2708 'extensions', 2704 'extensions',
2709 sorted(used_extensions), 2705 sorted(used_extensions),
2710 [ f for f in functions if not IsClientExtensionFunc(f) ]) 2706 [ f for f in functions if not IsClientExtensionFunc(f) ])
2707 file.write('}\n')
2711 2708
2712 # Some new function pointers have been added, so update them in debug bindings 2709 # Write function to clear all function pointers.
2713 file.write('\n') 2710 file.write('\n')
2714 file.write(' if (g_debugBindingsInitialized)\n') 2711 file.write("""void Driver%s::ClearBindings() {
2715 file.write(' InitializeDebugBindings();\n') 2712 memset(this, 0, sizeof(*this));
2716 file.write('}\n') 2713 }
2717 file.write('\n') 2714 """ % set_name.upper())
2718 2715
2719 # Write logging wrappers for each function. 2716 def MakeArgNames(arguments):
2720 file.write('extern "C" {\n') 2717 argument_names = re.sub(
2718 r'(const )?[a-zA-Z0-9_]+\** ([a-zA-Z0-9_]+)', r'\2', arguments)
2719 argument_names = re.sub(
2720 r'(const )?[a-zA-Z0-9_]+\** ([a-zA-Z0-9_]+)', r'\2', argument_names)
2721 if argument_names == 'void' or argument_names == '':
2722 argument_names = ''
2723 return argument_names
2724
2725 # Write GLApiBase functions
2726 for func in functions:
2727 function_name = func['known_as']
2728 return_type = func['return_type']
2729 arguments = func['arguments']
2730 file.write('\n')
2731 file.write('%s %sApiBase::%sFn(%s) {\n' %
2732 (return_type, set_name.upper(), function_name, arguments))
2733 argument_names = MakeArgNames(arguments)
2734 if return_type == 'void':
2735 file.write(' driver_->fn.%sFn(%s);\n' %
2736 (function_name, argument_names))
2737 else:
2738 file.write(' return driver_->fn.%sFn(%s);\n' %
2739 (function_name, argument_names))
2740 file.write('}\n')
2741
2742 # Write TraceGLApi functions
2743 for func in functions:
2744 function_name = func['known_as']
2745 return_type = func['return_type']
2746 arguments = func['arguments']
2747 file.write('\n')
2748 file.write('%s Trace%sApi::%sFn(%s) {\n' %
2749 (return_type, set_name.upper(), function_name, arguments))
2750 argument_names = MakeArgNames(arguments)
2751 file.write(' TRACE_EVENT_BINARY_EFFICIENT0("gpu", "TraceGLAPI::%s")\n' %
2752 function_name)
2753 if return_type == 'void':
2754 file.write(' %s_api_->%sFn(%s);\n' %
2755 (set_name.lower(), function_name, argument_names))
2756 else:
2757 file.write(' return %s_api_->%sFn(%s);\n' %
2758 (set_name.lower(), function_name, argument_names))
2759 file.write('}\n')
2760
2761 # Write DebugGLApi functions
2721 for func in functions: 2762 for func in functions:
2722 return_type = func['return_type'] 2763 return_type = func['return_type']
2723 arguments = func['arguments'] 2764 arguments = func['arguments']
2724 file.write('\n') 2765 file.write('\n')
2725 file.write('static %s GL_BINDING_CALL Debug_%s(%s) {\n' % 2766 file.write('%s Debug%sApi::%sFn(%s) {\n' %
2726 (return_type, func['known_as'], arguments)) 2767 (return_type, set_name.upper(), func['known_as'], arguments))
2727 argument_names = re.sub( 2768 argument_names = re.sub(
2728 r'(const )?[a-zA-Z0-9_]+\** ([a-zA-Z0-9_]+)', r'\2', arguments) 2769 r'(const )?[a-zA-Z0-9_]+\** ([a-zA-Z0-9_]+)', r'\2', arguments)
2729 argument_names = re.sub( 2770 argument_names = re.sub(
2730 r'(const )?[a-zA-Z0-9_]+\** ([a-zA-Z0-9_]+)', r'\2', argument_names) 2771 r'(const )?[a-zA-Z0-9_]+\** ([a-zA-Z0-9_]+)', r'\2', argument_names)
2731 log_argument_names = re.sub( 2772 log_argument_names = re.sub(
2732 r'const char\* ([a-zA-Z0-9_]+)', r'CONSTCHAR_\1', arguments) 2773 r'const char\* ([a-zA-Z0-9_]+)', r'CONSTCHAR_\1', arguments)
2733 log_argument_names = re.sub( 2774 log_argument_names = re.sub(
2734 r'(const )?[a-zA-Z0-9_]+\* ([a-zA-Z0-9_]+)', 2775 r'(const )?[a-zA-Z0-9_]+\* ([a-zA-Z0-9_]+)',
2735 r'CONSTVOID_\2', log_argument_names) 2776 r'CONSTVOID_\2', log_argument_names)
2736 log_argument_names = re.sub( 2777 log_argument_names = re.sub(
(...skipping 20 matching lines...) Expand all
2757 log_argument_names = log_argument_names.replace(',', ' << ", " <<') 2798 log_argument_names = log_argument_names.replace(',', ' << ", " <<')
2758 if argument_names == 'void' or argument_names == '': 2799 if argument_names == 'void' or argument_names == '':
2759 argument_names = '' 2800 argument_names = ''
2760 log_argument_names = '' 2801 log_argument_names = ''
2761 else: 2802 else:
2762 log_argument_names = " << " + log_argument_names 2803 log_argument_names = " << " + log_argument_names
2763 function_name = func['known_as'] 2804 function_name = func['known_as']
2764 if return_type == 'void': 2805 if return_type == 'void':
2765 file.write(' GL_SERVICE_LOG("%s" << "(" %s << ")");\n' % 2806 file.write(' GL_SERVICE_LOG("%s" << "(" %s << ")");\n' %
2766 (function_name, log_argument_names)) 2807 (function_name, log_argument_names))
2767 file.write(' DCHECK(g_driver_%s.debug_fn.%sFn != nullptr);\n' % 2808 file.write(' %s_api_->%sFn(%s);\n' %
2768 (set_name.lower(), function_name))
2769 file.write(' g_driver_%s.debug_fn.%sFn(%s);\n' %
2770 (set_name.lower(), function_name, argument_names)) 2809 (set_name.lower(), function_name, argument_names))
2771 if 'logging_code' in func: 2810 if 'logging_code' in func:
2772 file.write("%s\n" % func['logging_code']) 2811 file.write("%s\n" % func['logging_code'])
2773 if options.generate_dchecks and set_name == 'gl': 2812 if options.generate_dchecks and set_name == 'gl':
2774 file.write(' {\n') 2813 file.write(' {\n')
2775 file.write(' GLenum error = g_driver_gl.debug_fn.glGetErrorFn();\n') 2814 file.write(' GLenum error = %s_api_->glGetErrorFn();\n'
2815 % set_name.lower())
2776 file.write(' DCHECK(error == 0);\n') 2816 file.write(' DCHECK(error == 0);\n')
2777 file.write(' }\n') 2817 file.write(' }\n')
2778 else: 2818 else:
2779 file.write(' GL_SERVICE_LOG("%s" << "(" %s << ")");\n' % 2819 file.write(' GL_SERVICE_LOG("%s" << "(" %s << ")");\n' %
2780 (function_name, log_argument_names)) 2820 (function_name, log_argument_names))
2781 file.write(' DCHECK(g_driver_%s.debug_fn.%sFn != nullptr);\n' % 2821 file.write(' %s result = %s_api_->%sFn(%s);\n' %
2782 (set_name.lower(), function_name))
2783 file.write(' %s result = g_driver_%s.debug_fn.%sFn(%s);\n' %
2784 (return_type, set_name.lower(), function_name, argument_names)) 2822 (return_type, set_name.lower(), function_name, argument_names))
2785 if 'logging_code' in func: 2823 if 'logging_code' in func:
2786 file.write("%s\n" % func['logging_code']) 2824 file.write("%s\n" % func['logging_code'])
2787 else: 2825 else:
2788 file.write(' GL_SERVICE_LOG("GL_RESULT: " << result);\n') 2826 file.write(' GL_SERVICE_LOG("GL_RESULT: " << result);\n')
2789 if options.generate_dchecks and set_name == 'gl': 2827 if options.generate_dchecks and set_name == 'gl':
2790 file.write(' {\n') 2828 file.write(' {\n')
2791 file.write(' GLenum _error = g_driver_gl.debug_fn.glGetErrorFn();\n') 2829 file.write(' GLenum _error = %s_api_->glGetErrorFn();\n' %
2830 set_name.lower())
2792 file.write(' DCHECK(_error == 0);\n') 2831 file.write(' DCHECK(_error == 0);\n')
2793 file.write(' }\n') 2832 file.write(' }\n')
2794 file.write(' return result;\n') 2833 file.write(' return result;\n')
2795 file.write('}\n') 2834 file.write('}\n')
2796 file.write('} // extern "C"\n')
2797
2798 # Write function to initialize the debug function pointers.
2799 file.write('\n')
2800 file.write('void Driver%s::InitializeDebugBindings() {\n' %
2801 set_name.upper())
2802 for func in functions:
2803 first_name = func['known_as']
2804 file.write(' if (!debug_fn.%sFn) {\n' % first_name)
2805 file.write(' debug_fn.%sFn = fn.%sFn;\n' % (first_name, first_name))
2806 file.write(' fn.%sFn = Debug_%s;\n' % (first_name, first_name))
2807 file.write(' }\n')
2808 file.write(' g_debugBindingsInitialized = true;\n')
2809 file.write('}\n')
2810
2811 # Write function to clear all function pointers.
2812 file.write('\n')
2813 file.write("""void Driver%s::ClearBindings() {
2814 memset(this, 0, sizeof(*this));
2815 }
2816 """ % set_name.upper())
2817
2818 def MakeArgNames(arguments):
2819 argument_names = re.sub(
2820 r'(const )?[a-zA-Z0-9_]+\** ([a-zA-Z0-9_]+)', r'\2', arguments)
2821 argument_names = re.sub(
2822 r'(const )?[a-zA-Z0-9_]+\** ([a-zA-Z0-9_]+)', r'\2', argument_names)
2823 if argument_names == 'void' or argument_names == '':
2824 argument_names = ''
2825 return argument_names
2826
2827 # Write GLApiBase functions
2828 for func in functions:
2829 function_name = func['known_as']
2830 return_type = func['return_type']
2831 arguments = func['arguments']
2832 file.write('\n')
2833 file.write('%s %sApiBase::%sFn(%s) {\n' %
2834 (return_type, set_name.upper(), function_name, arguments))
2835 argument_names = MakeArgNames(arguments)
2836 if return_type == 'void':
2837 file.write(' driver_->fn.%sFn(%s);\n' %
2838 (function_name, argument_names))
2839 else:
2840 file.write(' return driver_->fn.%sFn(%s);\n' %
2841 (function_name, argument_names))
2842 file.write('}\n')
2843
2844 # Write TraceGLApi functions
2845 for func in functions:
2846 function_name = func['known_as']
2847 return_type = func['return_type']
2848 arguments = func['arguments']
2849 file.write('\n')
2850 file.write('%s Trace%sApi::%sFn(%s) {\n' %
2851 (return_type, set_name.upper(), function_name, arguments))
2852 argument_names = MakeArgNames(arguments)
2853 file.write(' TRACE_EVENT_BINARY_EFFICIENT0("gpu", "TraceGLAPI::%s")\n' %
2854 function_name)
2855 if return_type == 'void':
2856 file.write(' %s_api_->%sFn(%s);\n' %
2857 (set_name.lower(), function_name, argument_names))
2858 else:
2859 file.write(' return %s_api_->%sFn(%s);\n' %
2860 (set_name.lower(), function_name, argument_names))
2861 file.write('}\n')
2862 2835
2863 # Write NoContextGLApi functions 2836 # Write NoContextGLApi functions
2864 if set_name.upper() == "GL": 2837 if set_name.upper() == "GL":
2865 for func in functions: 2838 for func in functions:
2866 function_name = func['known_as'] 2839 function_name = func['known_as']
2867 return_type = func['return_type'] 2840 return_type = func['return_type']
2868 arguments = func['arguments'] 2841 arguments = func['arguments']
2869 file.write('\n') 2842 file.write('\n')
2870 file.write('%s NoContextGLApi::%sFn(%s) {\n' % 2843 file.write('%s NoContextGLApi::%sFn(%s) {\n' %
2871 (return_type, function_name, arguments)) 2844 (return_type, function_name, arguments))
(...skipping 513 matching lines...) Expand 10 before | Expand all | Expand 10 after
3385 header_file = open( 3358 header_file = open(
3386 os.path.join(directory, 'gl_stub_autogen_gl.cc'), 'wb') 3359 os.path.join(directory, 'gl_stub_autogen_gl.cc'), 'wb')
3387 GenerateStubSource(header_file, GL_FUNCTIONS) 3360 GenerateStubSource(header_file, GL_FUNCTIONS)
3388 header_file.close() 3361 header_file.close()
3389 ClangFormat(header_file.name) 3362 ClangFormat(header_file.name)
3390 return 0 3363 return 0
3391 3364
3392 3365
3393 if __name__ == '__main__': 3366 if __name__ == '__main__':
3394 sys.exit(main(sys.argv[1:])) 3367 sys.exit(main(sys.argv[1:]))
OLDNEW
« no previous file with comments | « ui/gl/BUILD.gn ('k') | ui/gl/gl_api_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698