[email protected] | 5ae0b28 | 2011-03-28 19:24:49 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 5 | #include <vector> |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 6 | |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 7 | #include "base/base_paths.h" |
| 8 | #include "base/command_line.h" |
| 9 | #include "base/file_path.h" |
[email protected] | 5826d050 | 2011-08-31 20:31:27 | [diff] [blame] | 10 | #include "base/lazy_instance.h" |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 11 | #include "base/logging.h" |
| 12 | #include "base/native_library.h" |
| 13 | #include "base/path_service.h" |
[email protected] | 25f06af | 2011-08-25 20:14:23 | [diff] [blame] | 14 | #include "base/synchronization/lock.h" |
[email protected] | 6e318e0e | 2011-10-14 23:08:20 | [diff] [blame^] | 15 | #include "base/threading/thread_restrictions.h" |
[email protected] | 5ae0b28 | 2011-03-28 19:24:49 | [diff] [blame] | 16 | #include "ui/gfx/gl/gl_bindings.h" |
| 17 | #include "ui/gfx/gl/gl_implementation.h" |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 18 | |
| 19 | namespace gfx { |
| 20 | namespace { |
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 21 | |
| 22 | // TODO(piman): it should be Desktop GL marshalling from double to float. Today |
| 23 | // on native GLES, we do float->double->float. |
| 24 | void GL_BINDING_CALL MarshalClearDepthToClearDepthf(GLclampd depth) { |
| 25 | glClearDepthf(static_cast<GLclampf>(depth)); |
| 26 | } |
| 27 | |
| 28 | void 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] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 33 | // Load a library, printing an error message on failure. |
[email protected] | a24612275 | 2011-04-19 01:10:48 | [diff] [blame] | 34 | base::NativeLibrary LoadLibrary(const FilePath& filename) { |
[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 35 | std::string error; |
[email protected] | a24612275 | 2011-04-19 01:10:48 | [diff] [blame] | 36 | base::NativeLibrary library = base::LoadNativeLibrary(filename, |
[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 37 | &error); |
| 38 | if (!library) { |
[email protected] | a24612275 | 2011-04-19 01:10:48 | [diff] [blame] | 39 | VLOG(1) << "Failed to load " << filename.MaybeAsASCII() << ": " << error; |
[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 40 | return NULL; |
| 41 | } |
| 42 | return library; |
| 43 | } |
| 44 | |
[email protected] | a24612275 | 2011-04-19 01:10:48 | [diff] [blame] | 45 | base::NativeLibrary LoadLibrary(const char* filename) { |
| 46 | return LoadLibrary(FilePath(filename)); |
| 47 | } |
| 48 | |
[email protected] | 25f06af | 2011-08-25 20:14:23 | [diff] [blame] | 49 | // 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] | 5826d050 | 2011-08-31 20:31:27 | [diff] [blame] | 52 | base::LazyInstance<base::Lock> g_lock(base::LINKER_INITIALIZED); |
[email protected] | 25f06af | 2011-08-25 20:14:23 | [diff] [blame] | 53 | #endif |
| 54 | |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 55 | } // namespace anonymous |
| 56 | |
| 57 | bool InitializeGLBindings(GLImplementation implementation) { |
[email protected] | 25f06af | 2011-08-25 20:14:23 | [diff] [blame] | 58 | #if (defined(TOOLKIT_VIEWS) && !defined(OS_CHROMEOS)) || defined(TOUCH_UI) |
[email protected] | 5826d050 | 2011-08-31 20:31:27 | [diff] [blame] | 59 | base::AutoLock locked(g_lock.Get()); |
[email protected] | 25f06af | 2011-08-25 20:14:23 | [diff] [blame] | 60 | #endif |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 61 | // 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] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 64 | if (GetGLImplementation() != kGLImplementationNone) |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 65 | return true; |
| 66 | |
[email protected] | 6e318e0e | 2011-10-14 23:08:20 | [diff] [blame^] | 67 | // 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] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 73 | switch (implementation) { |
[email protected] | f1b2cd0 | 2011-08-10 16:50:44 | [diff] [blame] | 74 | #if !defined(USE_WAYLAND) |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 75 | case kGLImplementationOSMesaGL: { |
[email protected] | 36a0979 | 2010-09-10 02:31:39 | [diff] [blame] | 76 | FilePath module_path; |
[email protected] | d049874 | 2010-09-20 20:27:01 | [diff] [blame] | 77 | if (!PathService::Get(base::DIR_MODULE, &module_path)) { |
| 78 | LOG(ERROR) << "PathService::Get failed."; |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 79 | return false; |
[email protected] | d049874 | 2010-09-20 20:27:01 | [diff] [blame] | 80 | } |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 81 | |
[email protected] | a24612275 | 2011-04-19 01:10:48 | [diff] [blame] | 82 | base::NativeLibrary library = LoadLibrary( |
| 83 | module_path.Append("libosmesa.so")); |
[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 84 | if (!library) |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 85 | return false; |
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 86 | |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 87 | GLGetProcAddressProc get_proc_address = |
| 88 | reinterpret_cast<GLGetProcAddressProc>( |
| 89 | base::GetFunctionPointerFromNativeLibrary( |
| 90 | library, "OSMesaGetProcAddress")); |
[email protected] | 1b2707bb | 2010-10-06 19:37:16 | [diff] [blame] | 91 | if (!get_proc_address) { |
[email protected] | 363b20d | 2011-01-14 22:54:16 | [diff] [blame] | 92 | LOG(ERROR) << "OSMesaGetProcAddress not found."; |
[email protected] | 1b2707bb | 2010-10-06 19:37:16 | [diff] [blame] | 93 | base::UnloadNativeLibrary(library); |
| 94 | return false; |
| 95 | } |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 96 | |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 97 | SetGLGetProcAddressProc(get_proc_address); |
| 98 | AddGLNativeLibrary(library); |
| 99 | SetGLImplementation(kGLImplementationOSMesaGL); |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 100 | |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 101 | InitializeGLBindingsGL(); |
| 102 | InitializeGLBindingsOSMESA(); |
| 103 | break; |
| 104 | } |
| 105 | case kGLImplementationDesktopGL: { |
[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 106 | base::NativeLibrary library = LoadLibrary("libGL.so.1"); |
| 107 | if (!library) |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 108 | return false; |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 109 | |
| 110 | GLGetProcAddressProc get_proc_address = |
| 111 | reinterpret_cast<GLGetProcAddressProc>( |
| 112 | base::GetFunctionPointerFromNativeLibrary( |
| 113 | library, "glXGetProcAddress")); |
[email protected] | 1b2707bb | 2010-10-06 19:37:16 | [diff] [blame] | 114 | if (!get_proc_address) { |
| 115 | LOG(ERROR) << "glxGetProcAddress not found."; |
| 116 | base::UnloadNativeLibrary(library); |
| 117 | return false; |
| 118 | } |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 119 | |
| 120 | SetGLGetProcAddressProc(get_proc_address); |
| 121 | AddGLNativeLibrary(library); |
| 122 | SetGLImplementation(kGLImplementationDesktopGL); |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 123 | |
| 124 | InitializeGLBindingsGL(); |
| 125 | InitializeGLBindingsGLX(); |
| 126 | break; |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 127 | } |
[email protected] | f1b2cd0 | 2011-08-10 16:50:44 | [diff] [blame] | 128 | #endif // !defined(USE_WAYLAND) |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 129 | case kGLImplementationEGLGLES2: { |
[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 130 | base::NativeLibrary gles_library = LoadLibrary("libGLESv2.so"); |
| 131 | if (!gles_library) |
[email protected] | 1b2707bb | 2010-10-06 19:37:16 | [diff] [blame] | 132 | return false; |
[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 133 | base::NativeLibrary egl_library = LoadLibrary("libEGL.so"); |
[email protected] | 91ac84f7 | 2011-06-22 19:48:02 | [diff] [blame] | 134 | if (!egl_library) { |
| 135 | base::UnloadNativeLibrary(gles_library); |
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 136 | return false; |
[email protected] | 91ac84f7 | 2011-06-22 19:48:02 | [diff] [blame] | 137 | } |
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 138 | |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 139 | GLGetProcAddressProc get_proc_address = |
| 140 | reinterpret_cast<GLGetProcAddressProc>( |
| 141 | base::GetFunctionPointerFromNativeLibrary( |
| 142 | egl_library, "eglGetProcAddress")); |
[email protected] | 1b2707bb | 2010-10-06 19:37:16 | [diff] [blame] | 143 | if (!get_proc_address) { |
| 144 | LOG(ERROR) << "eglGetProcAddress not found."; |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 145 | base::UnloadNativeLibrary(egl_library); |
[email protected] | 1b2707bb | 2010-10-06 19:37:16 | [diff] [blame] | 146 | base::UnloadNativeLibrary(gles_library); |
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 147 | return false; |
| 148 | } |
| 149 | |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 150 | SetGLGetProcAddressProc(get_proc_address); |
| 151 | AddGLNativeLibrary(egl_library); |
| 152 | AddGLNativeLibrary(gles_library); |
| 153 | SetGLImplementation(kGLImplementationEGLGLES2); |
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 154 | |
| 155 | InitializeGLBindingsGL(); |
| 156 | InitializeGLBindingsEGL(); |
| 157 | |
| 158 | // These two functions take single precision float rather than double |
| 159 | // precision float parameters in GLES. |
| 160 | ::gfx::g_glClearDepth = MarshalClearDepthToClearDepthf; |
| 161 | ::gfx::g_glDepthRange = MarshalDepthRangeToDepthRangef; |
| 162 | break; |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 163 | } |
| 164 | case kGLImplementationMockGL: { |
| 165 | SetGLGetProcAddressProc(GetMockGLProcAddress); |
| 166 | SetGLImplementation(kGLImplementationMockGL); |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 167 | InitializeGLBindingsGL(); |
| 168 | break; |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 169 | } |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 170 | default: |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | |
| 175 | return true; |
| 176 | } |
| 177 | |
[email protected] | 218a5a2 | 2010-11-04 19:27:49 | [diff] [blame] | 178 | void InitializeDebugGLBindings() { |
| 179 | InitializeDebugGLBindingsEGL(); |
| 180 | InitializeDebugGLBindingsGL(); |
[email protected] | f1b2cd0 | 2011-08-10 16:50:44 | [diff] [blame] | 181 | #if !defined(USE_WAYLAND) |
[email protected] | 218a5a2 | 2010-11-04 19:27:49 | [diff] [blame] | 182 | InitializeDebugGLBindingsGLX(); |
| 183 | InitializeDebugGLBindingsOSMESA(); |
[email protected] | f1b2cd0 | 2011-08-10 16:50:44 | [diff] [blame] | 184 | #endif |
[email protected] | 218a5a2 | 2010-11-04 19:27:49 | [diff] [blame] | 185 | } |
| 186 | |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 187 | } // namespace gfx |