blob: eed0a15d9b746d0bf187d80359e9ee5d1d1886e6 [file] [log] [blame]
[email protected]5ae0b282011-03-28 19:24:491// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]b9363b22010-06-09 22:06:152// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]0bec8e22010-06-21 22:20:025#include <vector>
[email protected]b9363b22010-06-09 22:06:156
[email protected]30aa5c1a2010-07-14 20:47:047#include "base/base_paths.h"
8#include "base/command_line.h"
9#include "base/file_path.h"
[email protected]5826d0502011-08-31 20:31:2710#include "base/lazy_instance.h"
[email protected]30aa5c1a2010-07-14 20:47:0411#include "base/logging.h"
12#include "base/native_library.h"
13#include "base/path_service.h"
[email protected]25f06af2011-08-25 20:14:2314#include "base/synchronization/lock.h"
[email protected]6e318e0e2011-10-14 23:08:2015#include "base/threading/thread_restrictions.h"
[email protected]5ae0b282011-03-28 19:24:4916#include "ui/gfx/gl/gl_bindings.h"
17#include "ui/gfx/gl/gl_implementation.h"
[email protected]b9363b22010-06-09 22:06:1518
19namespace gfx {
20namespace {
[email protected]0bec8e22010-06-21 22:20:0221
22// TODO(piman): it should be Desktop GL marshalling from double to float. Today
23// on native GLES, we do float->double->float.
24void GL_BINDING_CALL MarshalClearDepthToClearDepthf(GLclampd depth) {
25 glClearDepthf(static_cast<GLclampf>(depth));
26}
27
28void GL_BINDING_CALL MarshalDepthRangeToDepthRangef(GLclampd z_near,
29 GLclampd z_far) {
30 glDepthRangef(static_cast<GLclampf>(z_near), static_cast<GLclampf>(z_far));
31}
32
[email protected]84479322011-04-18 22:06:2233// Load a library, printing an error message on failure.
[email protected]a246122752011-04-19 01:10:4834base::NativeLibrary LoadLibrary(const FilePath& filename) {
[email protected]84479322011-04-18 22:06:2235 std::string error;
[email protected]a246122752011-04-19 01:10:4836 base::NativeLibrary library = base::LoadNativeLibrary(filename,
[email protected]84479322011-04-18 22:06:2237 &error);
38 if (!library) {
[email protected]a246122752011-04-19 01:10:4839 VLOG(1) << "Failed to load " << filename.MaybeAsASCII() << ": " << error;
[email protected]84479322011-04-18 22:06:2240 return NULL;
41 }
42 return library;
43}
44
[email protected]a246122752011-04-19 01:10:4845base::NativeLibrary LoadLibrary(const char* filename) {
46 return LoadLibrary(FilePath(filename));
47}
48
[email protected]25f06af2011-08-25 20:14:2349// TODO(backer): Find a more principled (less heavy handed) way to prevent a
50// race in the bindings initialization.
51#if (defined(TOOLKIT_VIEWS) && !defined(OS_CHROMEOS)) || defined(TOUCH_UI)
[email protected]8cdcca3a42011-11-01 20:27:3752base::LazyInstance<base::Lock,
53 base::LeakyLazyInstanceTraits<base::Lock> >
54 g_lock(base::LINKER_INITIALIZED);
[email protected]25f06af2011-08-25 20:14:2355#endif
56
[email protected]b9363b22010-06-09 22:06:1557} // namespace anonymous
58
[email protected]0f5e8882011-11-08 22:29:3859void GetAllowedGLImplementations(std::vector<GLImplementation>* impls) {
60 impls->push_back(kGLImplementationDesktopGL);
61 impls->push_back(kGLImplementationEGLGLES2);
62 impls->push_back(kGLImplementationOSMesaGL);
63}
64
[email protected]b9363b22010-06-09 22:06:1565bool InitializeGLBindings(GLImplementation implementation) {
[email protected]25f06af2011-08-25 20:14:2366#if (defined(TOOLKIT_VIEWS) && !defined(OS_CHROMEOS)) || defined(TOUCH_UI)
[email protected]5826d0502011-08-31 20:31:2767 base::AutoLock locked(g_lock.Get());
[email protected]25f06af2011-08-25 20:14:2368#endif
[email protected]b9363b22010-06-09 22:06:1569 // Prevent reinitialization with a different implementation. Once the gpu
70 // unit tests have initialized with kGLImplementationMock, we don't want to
71 // later switch to another GL implementation.
[email protected]30aa5c1a2010-07-14 20:47:0472 if (GetGLImplementation() != kGLImplementationNone)
[email protected]b9363b22010-06-09 22:06:1573 return true;
74
[email protected]6e318e0e2011-10-14 23:08:2075 // Allow the main thread or another to initialize these bindings
76 // after instituting restrictions on I/O. Going forward they will
77 // likely be used in the browser process on most platforms. The
78 // one-time initialization cost is small, between 2 and 5 ms.
79 base::ThreadRestrictions::ScopedAllowIO allow_io;
80
[email protected]b9363b22010-06-09 22:06:1581 switch (implementation) {
[email protected]f1b2cd02011-08-10 16:50:4482#if !defined(USE_WAYLAND)
[email protected]30aa5c1a2010-07-14 20:47:0483 case kGLImplementationOSMesaGL: {
[email protected]36a09792010-09-10 02:31:3984 FilePath module_path;
[email protected]d0498742010-09-20 20:27:0185 if (!PathService::Get(base::DIR_MODULE, &module_path)) {
86 LOG(ERROR) << "PathService::Get failed.";
[email protected]30aa5c1a2010-07-14 20:47:0487 return false;
[email protected]d0498742010-09-20 20:27:0188 }
[email protected]30aa5c1a2010-07-14 20:47:0489
[email protected]a246122752011-04-19 01:10:4890 base::NativeLibrary library = LoadLibrary(
91 module_path.Append("libosmesa.so"));
[email protected]84479322011-04-18 22:06:2292 if (!library)
[email protected]b9363b22010-06-09 22:06:1593 return false;
[email protected]0bec8e22010-06-21 22:20:0294
[email protected]30aa5c1a2010-07-14 20:47:0495 GLGetProcAddressProc get_proc_address =
96 reinterpret_cast<GLGetProcAddressProc>(
97 base::GetFunctionPointerFromNativeLibrary(
98 library, "OSMesaGetProcAddress"));
[email protected]1b2707bb2010-10-06 19:37:1699 if (!get_proc_address) {
[email protected]363b20d2011-01-14 22:54:16100 LOG(ERROR) << "OSMesaGetProcAddress not found.";
[email protected]1b2707bb2010-10-06 19:37:16101 base::UnloadNativeLibrary(library);
102 return false;
103 }
[email protected]b9363b22010-06-09 22:06:15104
[email protected]30aa5c1a2010-07-14 20:47:04105 SetGLGetProcAddressProc(get_proc_address);
106 AddGLNativeLibrary(library);
107 SetGLImplementation(kGLImplementationOSMesaGL);
[email protected]b9363b22010-06-09 22:06:15108
[email protected]30aa5c1a2010-07-14 20:47:04109 InitializeGLBindingsGL();
110 InitializeGLBindingsOSMESA();
111 break;
112 }
113 case kGLImplementationDesktopGL: {
[email protected]7f4115e2011-10-26 20:09:45114#if defined(OS_OPENBSD)
115 base::NativeLibrary library = LoadLibrary("libGL.so");
116#else
[email protected]84479322011-04-18 22:06:22117 base::NativeLibrary library = LoadLibrary("libGL.so.1");
[email protected]7f4115e2011-10-26 20:09:45118#endif
[email protected]84479322011-04-18 22:06:22119 if (!library)
[email protected]30aa5c1a2010-07-14 20:47:04120 return false;
[email protected]30aa5c1a2010-07-14 20:47:04121
122 GLGetProcAddressProc get_proc_address =
123 reinterpret_cast<GLGetProcAddressProc>(
124 base::GetFunctionPointerFromNativeLibrary(
125 library, "glXGetProcAddress"));
[email protected]1b2707bb2010-10-06 19:37:16126 if (!get_proc_address) {
127 LOG(ERROR) << "glxGetProcAddress not found.";
128 base::UnloadNativeLibrary(library);
129 return false;
130 }
[email protected]30aa5c1a2010-07-14 20:47:04131
132 SetGLGetProcAddressProc(get_proc_address);
133 AddGLNativeLibrary(library);
134 SetGLImplementation(kGLImplementationDesktopGL);
[email protected]b9363b22010-06-09 22:06:15135
136 InitializeGLBindingsGL();
137 InitializeGLBindingsGLX();
138 break;
[email protected]30aa5c1a2010-07-14 20:47:04139 }
[email protected]f1b2cd02011-08-10 16:50:44140#endif // !defined(USE_WAYLAND)
[email protected]30aa5c1a2010-07-14 20:47:04141 case kGLImplementationEGLGLES2: {
[email protected]84479322011-04-18 22:06:22142 base::NativeLibrary gles_library = LoadLibrary("libGLESv2.so");
143 if (!gles_library)
[email protected]1b2707bb2010-10-06 19:37:16144 return false;
[email protected]84479322011-04-18 22:06:22145 base::NativeLibrary egl_library = LoadLibrary("libEGL.so");
[email protected]91ac84f72011-06-22 19:48:02146 if (!egl_library) {
147 base::UnloadNativeLibrary(gles_library);
[email protected]0bec8e22010-06-21 22:20:02148 return false;
[email protected]91ac84f72011-06-22 19:48:02149 }
[email protected]0bec8e22010-06-21 22:20:02150
[email protected]30aa5c1a2010-07-14 20:47:04151 GLGetProcAddressProc get_proc_address =
152 reinterpret_cast<GLGetProcAddressProc>(
153 base::GetFunctionPointerFromNativeLibrary(
154 egl_library, "eglGetProcAddress"));
[email protected]1b2707bb2010-10-06 19:37:16155 if (!get_proc_address) {
156 LOG(ERROR) << "eglGetProcAddress not found.";
[email protected]30aa5c1a2010-07-14 20:47:04157 base::UnloadNativeLibrary(egl_library);
[email protected]1b2707bb2010-10-06 19:37:16158 base::UnloadNativeLibrary(gles_library);
[email protected]0bec8e22010-06-21 22:20:02159 return false;
160 }
161
[email protected]30aa5c1a2010-07-14 20:47:04162 SetGLGetProcAddressProc(get_proc_address);
163 AddGLNativeLibrary(egl_library);
164 AddGLNativeLibrary(gles_library);
165 SetGLImplementation(kGLImplementationEGLGLES2);
[email protected]0bec8e22010-06-21 22:20:02166
167 InitializeGLBindingsGL();
168 InitializeGLBindingsEGL();
169
170 // These two functions take single precision float rather than double
171 // precision float parameters in GLES.
172 ::gfx::g_glClearDepth = MarshalClearDepthToClearDepthf;
173 ::gfx::g_glDepthRange = MarshalDepthRangeToDepthRangef;
174 break;
[email protected]30aa5c1a2010-07-14 20:47:04175 }
176 case kGLImplementationMockGL: {
177 SetGLGetProcAddressProc(GetMockGLProcAddress);
178 SetGLImplementation(kGLImplementationMockGL);
[email protected]b9363b22010-06-09 22:06:15179 InitializeGLBindingsGL();
180 break;
[email protected]30aa5c1a2010-07-14 20:47:04181 }
[email protected]b9363b22010-06-09 22:06:15182 default:
183 return false;
184 }
185
186
187 return true;
188}
189
[email protected]6494823b2011-10-27 18:30:44190bool InitializeGLExtensionBindings(GLImplementation implementation,
191 GLContext* context) {
192 switch (implementation) {
193 case kGLImplementationOSMesaGL:
194 InitializeGLExtensionBindingsGL(context);
195 InitializeGLExtensionBindingsOSMESA(context);
196 break;
197 case kGLImplementationDesktopGL:
198 InitializeGLExtensionBindingsGL(context);
199 InitializeGLExtensionBindingsGLX(context);
200 break;
201 case kGLImplementationEGLGLES2:
202 InitializeGLExtensionBindingsGL(context);
203 InitializeGLExtensionBindingsEGL(context);
204 break;
205 case kGLImplementationMockGL:
206 InitializeGLExtensionBindingsGL(context);
207 break;
208 default:
209 return false;
210 }
211
212 return true;
213}
214
[email protected]218a5a22010-11-04 19:27:49215void InitializeDebugGLBindings() {
216 InitializeDebugGLBindingsEGL();
217 InitializeDebugGLBindingsGL();
[email protected]f1b2cd02011-08-10 16:50:44218#if !defined(USE_WAYLAND)
[email protected]218a5a22010-11-04 19:27:49219 InitializeDebugGLBindingsGLX();
220 InitializeDebugGLBindingsOSMESA();
[email protected]f1b2cd02011-08-10 16:50:44221#endif
[email protected]218a5a22010-11-04 19:27:49222}
223
[email protected]0f5e8882011-11-08 22:29:38224void ClearGLBindings() {
225 ClearGLBindingsEGL();
226 ClearGLBindingsGL();
227#if !defined(USE_WAYLAND)
228 ClearGLBindingsGLX();
229 ClearGLBindingsOSMESA();
230#endif
231 SetGLImplementation(kGLImplementationNone);
232
233 UnloadGLNativeLibraries();
234}
235
[email protected]b9363b22010-06-09 22:06:15236} // namespace gfx