Add locking control to user flows, disallow screen locking during user creation.
[chromium-blink-merge.git] / ui / gl / gl_implementation_x11.cc
blob7588a37fb4252d404fabd30ba9df260e9922102c
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_linux.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 } // namespace
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)
47 return true;
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());
66 if (!library) {
67 #if defined(OS_OPENBSD)
68 library = LoadLibrary("libGL.so");
69 #else
70 library = LoadLibrary("libGL.so.1");
71 #endif
74 if (!library)
75 return false;
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);
84 return false;
87 SetGLGetProcAddressProc(get_proc_address);
88 AddGLNativeLibrary(library);
89 SetGLImplementation(kGLImplementationDesktopGL);
91 InitializeStaticGLBindingsGL();
92 InitializeStaticGLBindingsGLX();
93 break;
95 case kGLImplementationEGLGLES2: {
96 base::NativeLibrary gles_library = LoadLibrary("libGLESv2.so.2");
97 if (!gles_library)
98 return false;
99 base::NativeLibrary egl_library = LoadLibrary("libEGL.so.1");
100 if (!egl_library) {
101 base::UnloadNativeLibrary(gles_library);
102 return false;
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);
113 return false;
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;
128 break;
130 case kGLImplementationMockGL: {
131 SetGLImplementation(kGLImplementationMockGL);
132 InitializeStaticGLBindingsGL();
133 break;
135 default:
136 return false;
140 return true;
143 bool InitializeDynamicGLBindings(GLImplementation implementation,
144 GLContext* context) {
145 switch (implementation) {
146 case kGLImplementationOSMesaGL:
147 InitializeDynamicGLBindingsGL(context);
148 InitializeDynamicGLBindingsOSMESA(context);
149 break;
150 case kGLImplementationDesktopGL:
151 InitializeDynamicGLBindingsGL(context);
152 InitializeDynamicGLBindingsGLX(context);
153 break;
154 case kGLImplementationEGLGLES2:
155 InitializeDynamicGLBindingsGL(context);
156 InitializeDynamicGLBindingsEGL(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