blob: 952e62e5d068255a4a45c8d3d5e324544a6861c0 [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]5826d0502011-08-31 20:31:2752base::LazyInstance<base::Lock> g_lock(base::LINKER_INITIALIZED);
[email protected]25f06af2011-08-25 20:14:2353#endif
54
[email protected]b9363b22010-06-09 22:06:1555} // namespace anonymous
56
57bool InitializeGLBindings(GLImplementation implementation) {
[email protected]25f06af2011-08-25 20:14:2358#if (defined(TOOLKIT_VIEWS) && !defined(OS_CHROMEOS)) || defined(TOUCH_UI)
[email protected]5826d0502011-08-31 20:31:2759 base::AutoLock locked(g_lock.Get());
[email protected]25f06af2011-08-25 20:14:2360#endif
[email protected]b9363b22010-06-09 22:06:1561 // Prevent reinitialization with a different implementation. Once the gpu
62 // unit tests have initialized with kGLImplementationMock, we don't want to
63 // later switch to another GL implementation.
[email protected]30aa5c1a2010-07-14 20:47:0464 if (GetGLImplementation() != kGLImplementationNone)
[email protected]b9363b22010-06-09 22:06:1565 return true;
66
[email protected]6e318e0e2011-10-14 23:08:2067 // Allow the main thread or another to initialize these bindings
68 // after instituting restrictions on I/O. Going forward they will
69 // likely be used in the browser process on most platforms. The
70 // one-time initialization cost is small, between 2 and 5 ms.
71 base::ThreadRestrictions::ScopedAllowIO allow_io;
72
[email protected]b9363b22010-06-09 22:06:1573 switch (implementation) {
[email protected]f1b2cd02011-08-10 16:50:4474#if !defined(USE_WAYLAND)
[email protected]30aa5c1a2010-07-14 20:47:0475 case kGLImplementationOSMesaGL: {
[email protected]36a09792010-09-10 02:31:3976 FilePath module_path;
[email protected]d0498742010-09-20 20:27:0177 if (!PathService::Get(base::DIR_MODULE, &module_path)) {
78 LOG(ERROR) << "PathService::Get failed.";
[email protected]30aa5c1a2010-07-14 20:47:0479 return false;
[email protected]d0498742010-09-20 20:27:0180 }
[email protected]30aa5c1a2010-07-14 20:47:0481
[email protected]a246122752011-04-19 01:10:4882 base::NativeLibrary library = LoadLibrary(
83 module_path.Append("libosmesa.so"));
[email protected]84479322011-04-18 22:06:2284 if (!library)
[email protected]b9363b22010-06-09 22:06:1585 return false;
[email protected]0bec8e22010-06-21 22:20:0286
[email protected]30aa5c1a2010-07-14 20:47:0487 GLGetProcAddressProc get_proc_address =
88 reinterpret_cast<GLGetProcAddressProc>(
89 base::GetFunctionPointerFromNativeLibrary(
90 library, "OSMesaGetProcAddress"));
[email protected]1b2707bb2010-10-06 19:37:1691 if (!get_proc_address) {
[email protected]363b20d2011-01-14 22:54:1692 LOG(ERROR) << "OSMesaGetProcAddress not found.";
[email protected]1b2707bb2010-10-06 19:37:1693 base::UnloadNativeLibrary(library);
94 return false;
95 }
[email protected]b9363b22010-06-09 22:06:1596
[email protected]30aa5c1a2010-07-14 20:47:0497 SetGLGetProcAddressProc(get_proc_address);
98 AddGLNativeLibrary(library);
99 SetGLImplementation(kGLImplementationOSMesaGL);
[email protected]b9363b22010-06-09 22:06:15100
[email protected]30aa5c1a2010-07-14 20:47:04101 InitializeGLBindingsGL();
102 InitializeGLBindingsOSMESA();
103 break;
104 }
105 case kGLImplementationDesktopGL: {
[email protected]7f4115e2011-10-26 20:09:45106#if defined(OS_OPENBSD)
107 base::NativeLibrary library = LoadLibrary("libGL.so");
108#else
[email protected]84479322011-04-18 22:06:22109 base::NativeLibrary library = LoadLibrary("libGL.so.1");
[email protected]7f4115e2011-10-26 20:09:45110#endif
[email protected]84479322011-04-18 22:06:22111 if (!library)
[email protected]30aa5c1a2010-07-14 20:47:04112 return false;
[email protected]30aa5c1a2010-07-14 20:47:04113
114 GLGetProcAddressProc get_proc_address =
115 reinterpret_cast<GLGetProcAddressProc>(
116 base::GetFunctionPointerFromNativeLibrary(
117 library, "glXGetProcAddress"));
[email protected]1b2707bb2010-10-06 19:37:16118 if (!get_proc_address) {
119 LOG(ERROR) << "glxGetProcAddress not found.";
120 base::UnloadNativeLibrary(library);
121 return false;
122 }
[email protected]30aa5c1a2010-07-14 20:47:04123
124 SetGLGetProcAddressProc(get_proc_address);
125 AddGLNativeLibrary(library);
126 SetGLImplementation(kGLImplementationDesktopGL);
[email protected]b9363b22010-06-09 22:06:15127
128 InitializeGLBindingsGL();
129 InitializeGLBindingsGLX();
130 break;
[email protected]30aa5c1a2010-07-14 20:47:04131 }
[email protected]f1b2cd02011-08-10 16:50:44132#endif // !defined(USE_WAYLAND)
[email protected]30aa5c1a2010-07-14 20:47:04133 case kGLImplementationEGLGLES2: {
[email protected]84479322011-04-18 22:06:22134 base::NativeLibrary gles_library = LoadLibrary("libGLESv2.so");
135 if (!gles_library)
[email protected]1b2707bb2010-10-06 19:37:16136 return false;
[email protected]84479322011-04-18 22:06:22137 base::NativeLibrary egl_library = LoadLibrary("libEGL.so");
[email protected]91ac84f72011-06-22 19:48:02138 if (!egl_library) {
139 base::UnloadNativeLibrary(gles_library);
[email protected]0bec8e22010-06-21 22:20:02140 return false;
[email protected]91ac84f72011-06-22 19:48:02141 }
[email protected]0bec8e22010-06-21 22:20:02142
[email protected]30aa5c1a2010-07-14 20:47:04143 GLGetProcAddressProc get_proc_address =
144 reinterpret_cast<GLGetProcAddressProc>(
145 base::GetFunctionPointerFromNativeLibrary(
146 egl_library, "eglGetProcAddress"));
[email protected]1b2707bb2010-10-06 19:37:16147 if (!get_proc_address) {
148 LOG(ERROR) << "eglGetProcAddress not found.";
[email protected]30aa5c1a2010-07-14 20:47:04149 base::UnloadNativeLibrary(egl_library);
[email protected]1b2707bb2010-10-06 19:37:16150 base::UnloadNativeLibrary(gles_library);
[email protected]0bec8e22010-06-21 22:20:02151 return false;
152 }
153
[email protected]30aa5c1a2010-07-14 20:47:04154 SetGLGetProcAddressProc(get_proc_address);
155 AddGLNativeLibrary(egl_library);
156 AddGLNativeLibrary(gles_library);
157 SetGLImplementation(kGLImplementationEGLGLES2);
[email protected]0bec8e22010-06-21 22:20:02158
159 InitializeGLBindingsGL();
160 InitializeGLBindingsEGL();
161
162 // These two functions take single precision float rather than double
163 // precision float parameters in GLES.
164 ::gfx::g_glClearDepth = MarshalClearDepthToClearDepthf;
165 ::gfx::g_glDepthRange = MarshalDepthRangeToDepthRangef;
166 break;
[email protected]30aa5c1a2010-07-14 20:47:04167 }
168 case kGLImplementationMockGL: {
169 SetGLGetProcAddressProc(GetMockGLProcAddress);
170 SetGLImplementation(kGLImplementationMockGL);
[email protected]b9363b22010-06-09 22:06:15171 InitializeGLBindingsGL();
172 break;
[email protected]30aa5c1a2010-07-14 20:47:04173 }
[email protected]b9363b22010-06-09 22:06:15174 default:
175 return false;
176 }
177
178
179 return true;
180}
181
[email protected]6494823b2011-10-27 18:30:44182bool InitializeGLExtensionBindings(GLImplementation implementation,
183 GLContext* context) {
184 switch (implementation) {
185 case kGLImplementationOSMesaGL:
186 InitializeGLExtensionBindingsGL(context);
187 InitializeGLExtensionBindingsOSMESA(context);
188 break;
189 case kGLImplementationDesktopGL:
190 InitializeGLExtensionBindingsGL(context);
191 InitializeGLExtensionBindingsGLX(context);
192 break;
193 case kGLImplementationEGLGLES2:
194 InitializeGLExtensionBindingsGL(context);
195 InitializeGLExtensionBindingsEGL(context);
196 break;
197 case kGLImplementationMockGL:
198 InitializeGLExtensionBindingsGL(context);
199 break;
200 default:
201 return false;
202 }
203
204 return true;
205}
206
[email protected]218a5a22010-11-04 19:27:49207void InitializeDebugGLBindings() {
208 InitializeDebugGLBindingsEGL();
209 InitializeDebugGLBindingsGL();
[email protected]f1b2cd02011-08-10 16:50:44210#if !defined(USE_WAYLAND)
[email protected]218a5a22010-11-04 19:27:49211 InitializeDebugGLBindingsGLX();
212 InitializeDebugGLBindingsOSMESA();
[email protected]f1b2cd02011-08-10 16:50:44213#endif
[email protected]218a5a22010-11-04 19:27:49214}
215
[email protected]b9363b22010-06-09 22:06:15216} // namespace gfx