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_osmesa.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
));
34 #if defined(OS_OPENBSD)
35 const char kGLLibraryName
[] = "libGL.so";
37 const char kGLLibraryName
[] = "libGL.so.1";
40 const char kGLESv2LibraryName
[] = "libGLESv2.so.2";
41 const char kEGLLibraryName
[] = "libEGL.so.1";
45 void GetAllowedGLImplementations(std::vector
<GLImplementation
>* impls
) {
46 impls
->push_back(kGLImplementationDesktopGL
);
47 impls
->push_back(kGLImplementationEGLGLES2
);
48 impls
->push_back(kGLImplementationOSMesaGL
);
51 bool InitializeStaticGLBindings(GLImplementation implementation
) {
52 // Prevent reinitialization with a different implementation. Once the gpu
53 // unit tests have initialized with kGLImplementationMock, we don't want to
54 // later switch to another GL implementation.
55 DCHECK_EQ(kGLImplementationNone
, GetGLImplementation());
57 // Allow the main thread or another to initialize these bindings
58 // after instituting restrictions on I/O. Going forward they will
59 // likely be used in the browser process on most platforms. The
60 // one-time initialization cost is small, between 2 and 5 ms.
61 base::ThreadRestrictions::ScopedAllowIO allow_io
;
63 switch (implementation
) {
64 case kGLImplementationOSMesaGL
:
65 return InitializeStaticGLBindingsOSMesaGL();
66 case kGLImplementationDesktopGL
: {
67 base::NativeLibrary library
= NULL
;
68 const base::CommandLine
* command_line
=
69 base::CommandLine::ForCurrentProcess();
71 if (command_line
->HasSwitch(switches::kTestGLLib
))
72 library
= LoadLibraryAndPrintError(
73 command_line
->GetSwitchValueASCII(switches::kTestGLLib
).c_str());
76 library
= LoadLibraryAndPrintError(kGLLibraryName
);
82 GLGetProcAddressProc get_proc_address
=
83 reinterpret_cast<GLGetProcAddressProc
>(
84 base::GetFunctionPointerFromNativeLibrary(
85 library
, "glXGetProcAddress"));
86 if (!get_proc_address
) {
87 LOG(ERROR
) << "glxGetProcAddress not found.";
88 base::UnloadNativeLibrary(library
);
92 SetGLGetProcAddressProc(get_proc_address
);
93 AddGLNativeLibrary(library
);
94 SetGLImplementation(kGLImplementationDesktopGL
);
96 InitializeStaticGLBindingsGL();
97 InitializeStaticGLBindingsGLX();
100 case kGLImplementationEGLGLES2
: {
101 base::NativeLibrary gles_library
=
102 LoadLibraryAndPrintError(kGLESv2LibraryName
);
105 base::NativeLibrary egl_library
=
106 LoadLibraryAndPrintError(kEGLLibraryName
);
108 base::UnloadNativeLibrary(gles_library
);
112 GLGetProcAddressProc get_proc_address
=
113 reinterpret_cast<GLGetProcAddressProc
>(
114 base::GetFunctionPointerFromNativeLibrary(
115 egl_library
, "eglGetProcAddress"));
116 if (!get_proc_address
) {
117 LOG(ERROR
) << "eglGetProcAddress not found.";
118 base::UnloadNativeLibrary(egl_library
);
119 base::UnloadNativeLibrary(gles_library
);
123 SetGLGetProcAddressProc(get_proc_address
);
124 AddGLNativeLibrary(egl_library
);
125 AddGLNativeLibrary(gles_library
);
126 SetGLImplementation(kGLImplementationEGLGLES2
);
128 InitializeStaticGLBindingsGL();
129 InitializeStaticGLBindingsEGL();
131 // These two functions take single precision float rather than double
132 // precision float parameters in GLES.
133 ::gfx::g_driver_gl
.fn
.glClearDepthFn
= MarshalClearDepthToClearDepthf
;
134 ::gfx::g_driver_gl
.fn
.glDepthRangeFn
= MarshalDepthRangeToDepthRangef
;
137 case kGLImplementationMockGL
: {
138 SetGLImplementation(kGLImplementationMockGL
);
139 InitializeStaticGLBindingsGL();
150 bool InitializeDynamicGLBindings(GLImplementation implementation
,
151 GLContext
* context
) {
152 switch (implementation
) {
153 case kGLImplementationOSMesaGL
:
154 case kGLImplementationDesktopGL
:
155 case kGLImplementationEGLGLES2
:
156 InitializeDynamicGLBindingsGL(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
);