1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "base/threading/thread_restrictions.h"
10 #include "ui/gl/gl_bindings.h"
11 #include "ui/gl/gl_context_stub_with_extensions.h"
12 #include "ui/gl/gl_egl_api_implementation.h"
13 #include "ui/gl/gl_gl_api_implementation.h"
14 #include "ui/gl/gl_glx_api_implementation.h"
15 #include "ui/gl/gl_implementation.h"
16 #include "ui/gl/gl_implementation_linux.h"
17 #include "ui/gl/gl_osmesa_api_implementation.h"
18 #include "ui/gl/gl_switches.h"
23 // TODO(piman): it should be Desktop GL marshalling from double to float. Today
24 // on native GLES, we do float->double->float.
25 void GL_BINDING_CALL MarshalClearDepthToClearDepthf(GLclampd depth) {
26 glClearDepthf(static_cast<GLclampf>(depth));
29 void GL_BINDING_CALL MarshalDepthRangeToDepthRangef(GLclampd z_near,
31 glDepthRangef(static_cast<GLclampf>(z_near), static_cast<GLclampf>(z_far));
36 void GetAllowedGLImplementations(std::vector<GLImplementation>* impls) {
37 impls->push_back(kGLImplementationDesktopGL);
38 impls->push_back(kGLImplementationEGLGLES2);
39 impls->push_back(kGLImplementationOSMesaGL);
42 bool InitializeStaticGLBindings(GLImplementation implementation) {
43 // Prevent reinitialization with a different implementation. Once the gpu
44 // unit tests have initialized with kGLImplementationMock, we don't want to
45 // later switch to another GL implementation.
46 DCHECK_EQ(kGLImplementationNone, GetGLImplementation());
48 // Allow the main thread or another to initialize these bindings
49 // after instituting restrictions on I/O. Going forward they will
50 // likely be used in the browser process on most platforms. The
51 // one-time initialization cost is small, between 2 and 5 ms.
52 base::ThreadRestrictions::ScopedAllowIO allow_io;
54 switch (implementation) {
55 case kGLImplementationOSMesaGL:
56 return InitializeStaticGLBindingsOSMesaGL();
57 case kGLImplementationDesktopGL: {
58 base::NativeLibrary library = NULL;
59 const CommandLine* command_line = CommandLine::ForCurrentProcess();
61 if (command_line->HasSwitch(switches::kTestGLLib))
62 library = LoadLibrary(command_line->GetSwitchValueASCII(
63 switches::kTestGLLib).c_str());
66 #if defined(OS_OPENBSD)
67 library = LoadLibrary("libGL.so");
69 library = LoadLibrary("libGL.so.1");
76 GLGetProcAddressProc get_proc_address =
77 reinterpret_cast<GLGetProcAddressProc>(
78 base::GetFunctionPointerFromNativeLibrary(
79 library, "glXGetProcAddress"));
80 if (!get_proc_address) {
81 LOG(ERROR) << "glxGetProcAddress not found.";
82 base::UnloadNativeLibrary(library);
86 SetGLGetProcAddressProc(get_proc_address);
87 AddGLNativeLibrary(library);
88 SetGLImplementation(kGLImplementationDesktopGL);
90 InitializeStaticGLBindingsGL();
91 InitializeStaticGLBindingsGLX();
94 case kGLImplementationEGLGLES2: {
95 base::NativeLibrary gles_library = LoadLibrary("libGLESv2.so.2");
98 base::NativeLibrary egl_library = LoadLibrary("libEGL.so.1");
100 base::UnloadNativeLibrary(gles_library);
104 GLGetProcAddressProc get_proc_address =
105 reinterpret_cast<GLGetProcAddressProc>(
106 base::GetFunctionPointerFromNativeLibrary(
107 egl_library, "eglGetProcAddress"));
108 if (!get_proc_address) {
109 LOG(ERROR) << "eglGetProcAddress not found.";
110 base::UnloadNativeLibrary(egl_library);
111 base::UnloadNativeLibrary(gles_library);
115 SetGLGetProcAddressProc(get_proc_address);
116 AddGLNativeLibrary(egl_library);
117 AddGLNativeLibrary(gles_library);
118 SetGLImplementation(kGLImplementationEGLGLES2);
120 InitializeStaticGLBindingsGL();
121 InitializeStaticGLBindingsEGL();
123 // These two functions take single precision float rather than double
124 // precision float parameters in GLES.
125 ::gfx::g_driver_gl.fn.glClearDepthFn = MarshalClearDepthToClearDepthf;
126 ::gfx::g_driver_gl.fn.glDepthRangeFn = MarshalDepthRangeToDepthRangef;
129 case kGLImplementationMockGL: {
130 SetGLImplementation(kGLImplementationMockGL);
131 InitializeStaticGLBindingsGL();
142 bool InitializeDynamicGLBindings(GLImplementation implementation,
143 GLContext* context) {
144 switch (implementation) {
145 case kGLImplementationOSMesaGL:
146 InitializeDynamicGLBindingsGL(context);
147 InitializeDynamicGLBindingsOSMESA(context);
149 case kGLImplementationDesktopGL:
150 InitializeDynamicGLBindingsGL(context);
151 InitializeDynamicGLBindingsGLX(context);
153 case kGLImplementationEGLGLES2:
154 InitializeDynamicGLBindingsGL(context);
155 InitializeDynamicGLBindingsEGL(context);
157 case kGLImplementationMockGL:
159 scoped_refptr<GLContextStubWithExtensions> mock_context(
160 new GLContextStubWithExtensions());
161 mock_context->SetGLVersionString("3.0");
162 InitializeDynamicGLBindingsGL(mock_context.get());
164 InitializeDynamicGLBindingsGL(context);
173 void InitializeDebugGLBindings() {
174 InitializeDebugGLBindingsEGL();
175 InitializeDebugGLBindingsGL();
176 InitializeDebugGLBindingsGLX();
177 InitializeDebugGLBindingsOSMESA();
180 void ClearGLBindings() {
181 ClearGLBindingsEGL();
183 ClearGLBindingsGLX();
184 ClearGLBindingsOSMESA();
185 SetGLImplementation(kGLImplementationNone);
187 UnloadGLNativeLibraries();
190 bool GetGLWindowSystemBindingInfo(GLWindowSystemBindingInfo* info) {
191 switch (GetGLImplementation()) {
192 case kGLImplementationDesktopGL:
193 return GetGLWindowSystemBindingInfoGLX(info);
194 case kGLImplementationEGLGLES2:
195 return GetGLWindowSystemBindingInfoEGL(info);