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 if (GetGLImplementation() != kGLImplementationNone
)
49 // Allow the main thread or another to initialize these bindings
50 // after instituting restrictions on I/O. Going forward they will
51 // likely be used in the browser process on most platforms. The
52 // one-time initialization cost is small, between 2 and 5 ms.
53 base::ThreadRestrictions::ScopedAllowIO allow_io
;
55 switch (implementation
) {
56 case kGLImplementationOSMesaGL
:
57 return InitializeStaticGLBindingsOSMesaGL();
58 case kGLImplementationDesktopGL
: {
59 base::NativeLibrary library
= NULL
;
60 const CommandLine
* command_line
= CommandLine::ForCurrentProcess();
62 if (command_line
->HasSwitch(switches::kTestGLLib
))
63 library
= LoadLibrary(command_line
->GetSwitchValueASCII(
64 switches::kTestGLLib
).c_str());
67 #if defined(OS_OPENBSD)
68 library
= LoadLibrary("libGL.so");
70 library
= LoadLibrary("libGL.so.1");
77 GLGetProcAddressProc get_proc_address
=
78 reinterpret_cast<GLGetProcAddressProc
>(
79 base::GetFunctionPointerFromNativeLibrary(
80 library
, "glXGetProcAddress"));
81 if (!get_proc_address
) {
82 LOG(ERROR
) << "glxGetProcAddress not found.";
83 base::UnloadNativeLibrary(library
);
87 SetGLGetProcAddressProc(get_proc_address
);
88 AddGLNativeLibrary(library
);
89 SetGLImplementation(kGLImplementationDesktopGL
);
91 InitializeStaticGLBindingsGL();
92 InitializeStaticGLBindingsGLX();
95 case kGLImplementationEGLGLES2
: {
96 base::NativeLibrary gles_library
= LoadLibrary("libGLESv2.so.2");
99 base::NativeLibrary egl_library
= LoadLibrary("libEGL.so.1");
101 base::UnloadNativeLibrary(gles_library
);
105 GLGetProcAddressProc get_proc_address
=
106 reinterpret_cast<GLGetProcAddressProc
>(
107 base::GetFunctionPointerFromNativeLibrary(
108 egl_library
, "eglGetProcAddress"));
109 if (!get_proc_address
) {
110 LOG(ERROR
) << "eglGetProcAddress not found.";
111 base::UnloadNativeLibrary(egl_library
);
112 base::UnloadNativeLibrary(gles_library
);
116 SetGLGetProcAddressProc(get_proc_address
);
117 AddGLNativeLibrary(egl_library
);
118 AddGLNativeLibrary(gles_library
);
119 SetGLImplementation(kGLImplementationEGLGLES2
);
121 InitializeStaticGLBindingsGL();
122 InitializeStaticGLBindingsEGL();
124 // These two functions take single precision float rather than double
125 // precision float parameters in GLES.
126 ::gfx::g_driver_gl
.fn
.glClearDepthFn
= MarshalClearDepthToClearDepthf
;
127 ::gfx::g_driver_gl
.fn
.glDepthRangeFn
= MarshalDepthRangeToDepthRangef
;
130 case kGLImplementationMockGL
: {
131 SetGLImplementation(kGLImplementationMockGL
);
132 InitializeStaticGLBindingsGL();
143 bool InitializeDynamicGLBindings(GLImplementation implementation
,
144 GLContext
* context
) {
145 switch (implementation
) {
146 case kGLImplementationOSMesaGL
:
147 InitializeDynamicGLBindingsGL(context
);
148 InitializeDynamicGLBindingsOSMESA(context
);
150 case kGLImplementationDesktopGL
:
151 InitializeDynamicGLBindingsGL(context
);
152 InitializeDynamicGLBindingsGLX(context
);
154 case kGLImplementationEGLGLES2
:
155 InitializeDynamicGLBindingsGL(context
);
156 InitializeDynamicGLBindingsEGL(context
);
158 case kGLImplementationMockGL
:
160 scoped_refptr
<GLContextStubWithExtensions
> mock_context(
161 new GLContextStubWithExtensions());
162 mock_context
->SetGLVersionString("3.0");
163 InitializeDynamicGLBindingsGL(mock_context
.get());
165 InitializeDynamicGLBindingsGL(context
);
174 void InitializeDebugGLBindings() {
175 InitializeDebugGLBindingsEGL();
176 InitializeDebugGLBindingsGL();
177 InitializeDebugGLBindingsGLX();
178 InitializeDebugGLBindingsOSMESA();
181 void ClearGLBindings() {
182 ClearGLBindingsEGL();
184 ClearGLBindingsGLX();
185 ClearGLBindingsOSMESA();
186 SetGLImplementation(kGLImplementationNone
);
188 UnloadGLNativeLibraries();
191 bool GetGLWindowSystemBindingInfo(GLWindowSystemBindingInfo
* info
) {
192 switch (GetGLImplementation()) {
193 case kGLImplementationDesktopGL
:
194 return GetGLWindowSystemBindingInfoGLX(info
);
195 case kGLImplementationEGLGLES2
:
196 return GetGLWindowSystemBindingInfoEGL(info
);