blob: b8a724649f8c0cfe91e10586718d78e7695acd8a [file] [log] [blame]
James Robinson646469d2014-10-03 15:33:28 -07001// 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.
4
5#include <vector>
6
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"
19
20namespace gfx {
21namespace {
22
23// TODO(piman): it should be Desktop GL marshalling from double to float. Today
24// on native GLES, we do float->double->float.
25void GL_BINDING_CALL MarshalClearDepthToClearDepthf(GLclampd depth) {
26 glClearDepthf(static_cast<GLclampf>(depth));
27}
28
29void GL_BINDING_CALL MarshalDepthRangeToDepthRangef(GLclampd z_near,
30 GLclampd z_far) {
31 glDepthRangef(static_cast<GLclampf>(z_near), static_cast<GLclampf>(z_far));
32}
33
34#if defined(OS_OPENBSD)
35const char kGLLibraryName[] = "libGL.so";
36#else
37const char kGLLibraryName[] = "libGL.so.1";
38#endif
39
40const char kGLESv2LibraryName[] = "libGLESv2.so.2";
41const char kEGLLibraryName[] = "libEGL.so.1";
42
43} // namespace
44
45void GetAllowedGLImplementations(std::vector<GLImplementation>* impls) {
46 impls->push_back(kGLImplementationDesktopGL);
47 impls->push_back(kGLImplementationEGLGLES2);
48 impls->push_back(kGLImplementationOSMesaGL);
49}
50
51bool 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());
56
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;
62
63 switch (implementation) {
64 case kGLImplementationOSMesaGL:
65 return InitializeStaticGLBindingsOSMesaGL();
66 case kGLImplementationDesktopGL: {
67 base::NativeLibrary library = NULL;
James Robinson9127e722014-12-29 14:41:55 -080068 const base::CommandLine* command_line =
69 base::CommandLine::ForCurrentProcess();
James Robinson646469d2014-10-03 15:33:28 -070070
71 if (command_line->HasSwitch(switches::kTestGLLib))
72 library = LoadLibraryAndPrintError(
73 command_line->GetSwitchValueASCII(switches::kTestGLLib).c_str());
74
75 if (!library) {
76 library = LoadLibraryAndPrintError(kGLLibraryName);
77 }
78
79 if (!library)
80 return false;
81
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;
90 }
91
92 SetGLGetProcAddressProc(get_proc_address);
93 AddGLNativeLibrary(library);
94 SetGLImplementation(kGLImplementationDesktopGL);
95
96 InitializeStaticGLBindingsGL();
97 InitializeStaticGLBindingsGLX();
98 break;
99 }
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;
110 }
111
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;
121 }
122
123 SetGLGetProcAddressProc(get_proc_address);
124 AddGLNativeLibrary(egl_library);
125 AddGLNativeLibrary(gles_library);
126 SetGLImplementation(kGLImplementationEGLGLES2);
127
128 InitializeStaticGLBindingsGL();
129 InitializeStaticGLBindingsEGL();
130
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;
136 }
137 case kGLImplementationMockGL: {
138 SetGLImplementation(kGLImplementationMockGL);
139 InitializeStaticGLBindingsGL();
140 break;
141 }
142 default:
143 return false;
144 }
145
146
147 return true;
148}
149
150bool InitializeDynamicGLBindings(GLImplementation implementation,
151 GLContext* context) {
152 switch (implementation) {
153 case kGLImplementationOSMesaGL:
Benjamin Lermancdfc88d2015-02-03 14:35:12 +0100154 case kGLImplementationDesktopGL:
James Robinson646469d2014-10-03 15:33:28 -0700155 case kGLImplementationEGLGLES2:
156 InitializeDynamicGLBindingsGL(context);
James Robinson646469d2014-10-03 15:33:28 -0700157 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;
169 }
170
171 return true;
172}
173
174void InitializeDebugGLBindings() {
175 InitializeDebugGLBindingsEGL();
176 InitializeDebugGLBindingsGL();
177 InitializeDebugGLBindingsGLX();
178 InitializeDebugGLBindingsOSMESA();
179}
180
181void ClearGLBindings() {
182 ClearGLBindingsEGL();
183 ClearGLBindingsGL();
184 ClearGLBindingsGLX();
185 ClearGLBindingsOSMESA();
186 SetGLImplementation(kGLImplementationNone);
187
188 UnloadGLNativeLibraries();
189}
190
191bool 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;
199 }
200 return false;
201}
202
203} // namespace gfx