Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / gl / gl_implementation_x11.cc
blobb8a724649f8c0cfe91e10586718d78e7695acd8a
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.
5 #include <vector>
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"
20 namespace gfx {
21 namespace {
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,
30 GLclampd z_far) {
31 glDepthRangef(static_cast<GLclampf>(z_near), static_cast<GLclampf>(z_far));
34 #if defined(OS_OPENBSD)
35 const char kGLLibraryName[] = "libGL.so";
36 #else
37 const char kGLLibraryName[] = "libGL.so.1";
38 #endif
40 const char kGLESv2LibraryName[] = "libGLESv2.so.2";
41 const char kEGLLibraryName[] = "libEGL.so.1";
43 } // namespace
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());
75 if (!library) {
76 library = LoadLibraryAndPrintError(kGLLibraryName);
79 if (!library)
80 return false;
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);
89 return false;
92 SetGLGetProcAddressProc(get_proc_address);
93 AddGLNativeLibrary(library);
94 SetGLImplementation(kGLImplementationDesktopGL);
96 InitializeStaticGLBindingsGL();
97 InitializeStaticGLBindingsGLX();
98 break;
100 case kGLImplementationEGLGLES2: {
101 base::NativeLibrary gles_library =
102 LoadLibraryAndPrintError(kGLESv2LibraryName);
103 if (!gles_library)
104 return false;
105 base::NativeLibrary egl_library =
106 LoadLibraryAndPrintError(kEGLLibraryName);
107 if (!egl_library) {
108 base::UnloadNativeLibrary(gles_library);
109 return false;
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);
120 return false;
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;
135 break;
137 case kGLImplementationMockGL: {
138 SetGLImplementation(kGLImplementationMockGL);
139 InitializeStaticGLBindingsGL();
140 break;
142 default:
143 return false;
147 return true;
150 bool InitializeDynamicGLBindings(GLImplementation implementation,
151 GLContext* context) {
152 switch (implementation) {
153 case kGLImplementationOSMesaGL:
154 case kGLImplementationDesktopGL:
155 case kGLImplementationEGLGLES2:
156 InitializeDynamicGLBindingsGL(context);
157 break;
158 case kGLImplementationMockGL:
159 if (!context) {
160 scoped_refptr<GLContextStubWithExtensions> mock_context(
161 new GLContextStubWithExtensions());
162 mock_context->SetGLVersionString("3.0");
163 InitializeDynamicGLBindingsGL(mock_context.get());
164 } else
165 InitializeDynamicGLBindingsGL(context);
166 break;
167 default:
168 return false;
171 return true;
174 void InitializeDebugGLBindings() {
175 InitializeDebugGLBindingsEGL();
176 InitializeDebugGLBindingsGL();
177 InitializeDebugGLBindingsGLX();
178 InitializeDebugGLBindingsOSMESA();
181 void ClearGLBindings() {
182 ClearGLBindingsEGL();
183 ClearGLBindingsGL();
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);
197 default:
198 return false;
200 return false;
203 } // namespace gfx