[email protected] | 991c568 | 2012-01-30 13:32:34 | [diff] [blame] | 1 | // Copyright (c) 2012 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" |
| 10 | #include "base/logging.h" |
| 11 | #include "base/native_library.h" |
| 12 | #include "base/path_service.h" |
[email protected] | 6e318e0e | 2011-10-14 23:08:20 | [diff] [blame] | 13 | #include "base/threading/thread_restrictions.h" |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 14 | #include "ui/gl/gl_bindings.h" |
[email protected] | b97c908 | 2012-10-25 17:21:38 | [diff] [blame^] | 15 | #include "ui/gl/gl_egl_api_implementation.h" |
| 16 | #include "ui/gl/gl_gl_api_implementation.h" |
| 17 | #include "ui/gl/gl_glx_api_implementation.h" |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 18 | #include "ui/gl/gl_implementation.h" |
[email protected] | b97c908 | 2012-10-25 17:21:38 | [diff] [blame^] | 19 | #include "ui/gl/gl_osmesa_api_implementation.h" |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 20 | #include "ui/gl/gl_switches.h" |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 21 | |
| 22 | namespace gfx { |
| 23 | namespace { |
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 24 | |
| 25 | // TODO(piman): it should be Desktop GL marshalling from double to float. Today |
| 26 | // on native GLES, we do float->double->float. |
| 27 | void GL_BINDING_CALL MarshalClearDepthToClearDepthf(GLclampd depth) { |
| 28 | glClearDepthf(static_cast<GLclampf>(depth)); |
| 29 | } |
| 30 | |
| 31 | void GL_BINDING_CALL MarshalDepthRangeToDepthRangef(GLclampd z_near, |
| 32 | GLclampd z_far) { |
| 33 | glDepthRangef(static_cast<GLclampf>(z_near), static_cast<GLclampf>(z_far)); |
| 34 | } |
| 35 | |
[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 36 | // Load a library, printing an error message on failure. |
[email protected] | a24612275 | 2011-04-19 01:10:48 | [diff] [blame] | 37 | base::NativeLibrary LoadLibrary(const FilePath& filename) { |
[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 38 | std::string error; |
[email protected] | a24612275 | 2011-04-19 01:10:48 | [diff] [blame] | 39 | base::NativeLibrary library = base::LoadNativeLibrary(filename, |
[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 40 | &error); |
| 41 | if (!library) { |
[email protected] | 46a519fba | 2012-04-23 00:40:35 | [diff] [blame] | 42 | DVLOG(1) << "Failed to load " << filename.MaybeAsASCII() << ": " << error; |
[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 43 | return NULL; |
| 44 | } |
| 45 | return library; |
| 46 | } |
| 47 | |
[email protected] | a24612275 | 2011-04-19 01:10:48 | [diff] [blame] | 48 | base::NativeLibrary LoadLibrary(const char* filename) { |
| 49 | return LoadLibrary(FilePath(filename)); |
| 50 | } |
| 51 | |
[email protected] | 83afcbcc | 2012-07-27 03:06:27 | [diff] [blame] | 52 | } // namespace |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 53 | |
[email protected] | 0f5e888 | 2011-11-08 22:29:38 | [diff] [blame] | 54 | void GetAllowedGLImplementations(std::vector<GLImplementation>* impls) { |
[email protected] | 3c4b7ef | 2011-11-21 22:24:49 | [diff] [blame] | 55 | impls->push_back(kGLImplementationDesktopGL); |
[email protected] | 3c4b7ef | 2011-11-21 22:24:49 | [diff] [blame] | 56 | impls->push_back(kGLImplementationEGLGLES2); |
[email protected] | 70518b1 | 2011-11-22 01:39:12 | [diff] [blame] | 57 | impls->push_back(kGLImplementationOSMesaGL); |
[email protected] | 0f5e888 | 2011-11-08 22:29:38 | [diff] [blame] | 58 | } |
| 59 | |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 60 | bool InitializeGLBindings(GLImplementation implementation) { |
| 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] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 74 | case kGLImplementationOSMesaGL: { |
[email protected] | 36a0979 | 2010-09-10 02:31:39 | [diff] [blame] | 75 | FilePath module_path; |
[email protected] | d049874 | 2010-09-20 20:27:01 | [diff] [blame] | 76 | if (!PathService::Get(base::DIR_MODULE, &module_path)) { |
| 77 | LOG(ERROR) << "PathService::Get failed."; |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 78 | return false; |
[email protected] | d049874 | 2010-09-20 20:27:01 | [diff] [blame] | 79 | } |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 80 | |
[email protected] | a24612275 | 2011-04-19 01:10:48 | [diff] [blame] | 81 | base::NativeLibrary library = LoadLibrary( |
| 82 | module_path.Append("libosmesa.so")); |
[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 83 | if (!library) |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 84 | return false; |
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 85 | |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 86 | GLGetProcAddressProc get_proc_address = |
| 87 | reinterpret_cast<GLGetProcAddressProc>( |
| 88 | base::GetFunctionPointerFromNativeLibrary( |
| 89 | library, "OSMesaGetProcAddress")); |
[email protected] | 1b2707bb | 2010-10-06 19:37:16 | [diff] [blame] | 90 | if (!get_proc_address) { |
[email protected] | 363b20d | 2011-01-14 22:54:16 | [diff] [blame] | 91 | LOG(ERROR) << "OSMesaGetProcAddress not found."; |
[email protected] | 1b2707bb | 2010-10-06 19:37:16 | [diff] [blame] | 92 | base::UnloadNativeLibrary(library); |
| 93 | return false; |
| 94 | } |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 95 | |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 96 | SetGLGetProcAddressProc(get_proc_address); |
| 97 | AddGLNativeLibrary(library); |
| 98 | SetGLImplementation(kGLImplementationOSMesaGL); |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 99 | |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 100 | InitializeGLBindingsGL(); |
| 101 | InitializeGLBindingsOSMESA(); |
| 102 | break; |
| 103 | } |
| 104 | case kGLImplementationDesktopGL: { |
[email protected] | 991c568 | 2012-01-30 13:32:34 | [diff] [blame] | 105 | base::NativeLibrary library = NULL; |
| 106 | const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 107 | |
| 108 | if (command_line->HasSwitch(switches::kTestGLLib)) |
| 109 | library = LoadLibrary(command_line->GetSwitchValueASCII( |
| 110 | switches::kTestGLLib).c_str()); |
| 111 | |
| 112 | if (!library) { |
[email protected] | 7f4115e | 2011-10-26 20:09:45 | [diff] [blame] | 113 | #if defined(OS_OPENBSD) |
[email protected] | 991c568 | 2012-01-30 13:32:34 | [diff] [blame] | 114 | library = LoadLibrary("libGL.so"); |
[email protected] | 7f4115e | 2011-10-26 20:09:45 | [diff] [blame] | 115 | #else |
[email protected] | 991c568 | 2012-01-30 13:32:34 | [diff] [blame] | 116 | library = LoadLibrary("libGL.so.1"); |
[email protected] | 7f4115e | 2011-10-26 20:09:45 | [diff] [blame] | 117 | #endif |
[email protected] | 991c568 | 2012-01-30 13:32:34 | [diff] [blame] | 118 | } |
| 119 | |
[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 120 | if (!library) |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 121 | return false; |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 122 | |
| 123 | GLGetProcAddressProc get_proc_address = |
| 124 | reinterpret_cast<GLGetProcAddressProc>( |
| 125 | base::GetFunctionPointerFromNativeLibrary( |
| 126 | library, "glXGetProcAddress")); |
[email protected] | 1b2707bb | 2010-10-06 19:37:16 | [diff] [blame] | 127 | if (!get_proc_address) { |
| 128 | LOG(ERROR) << "glxGetProcAddress not found."; |
| 129 | base::UnloadNativeLibrary(library); |
| 130 | return false; |
| 131 | } |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 132 | |
| 133 | SetGLGetProcAddressProc(get_proc_address); |
| 134 | AddGLNativeLibrary(library); |
| 135 | SetGLImplementation(kGLImplementationDesktopGL); |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 136 | |
| 137 | InitializeGLBindingsGL(); |
| 138 | InitializeGLBindingsGLX(); |
| 139 | break; |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 140 | } |
| 141 | case kGLImplementationEGLGLES2: { |
[email protected] | 3733a14 | 2012-02-08 22:05:08 | [diff] [blame] | 142 | base::NativeLibrary gles_library = LoadLibrary("libGLESv2.so.2"); |
[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 143 | if (!gles_library) |
[email protected] | 1b2707bb | 2010-10-06 19:37:16 | [diff] [blame] | 144 | return false; |
[email protected] | 3733a14 | 2012-02-08 22:05:08 | [diff] [blame] | 145 | base::NativeLibrary egl_library = LoadLibrary("libEGL.so.1"); |
[email protected] | 91ac84f7 | 2011-06-22 19:48:02 | [diff] [blame] | 146 | if (!egl_library) { |
| 147 | base::UnloadNativeLibrary(gles_library); |
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 148 | return false; |
[email protected] | 91ac84f7 | 2011-06-22 19:48:02 | [diff] [blame] | 149 | } |
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 150 | |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 151 | GLGetProcAddressProc get_proc_address = |
| 152 | reinterpret_cast<GLGetProcAddressProc>( |
| 153 | base::GetFunctionPointerFromNativeLibrary( |
| 154 | egl_library, "eglGetProcAddress")); |
[email protected] | 1b2707bb | 2010-10-06 19:37:16 | [diff] [blame] | 155 | if (!get_proc_address) { |
| 156 | LOG(ERROR) << "eglGetProcAddress not found."; |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 157 | base::UnloadNativeLibrary(egl_library); |
[email protected] | 1b2707bb | 2010-10-06 19:37:16 | [diff] [blame] | 158 | base::UnloadNativeLibrary(gles_library); |
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 159 | return false; |
| 160 | } |
| 161 | |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 162 | SetGLGetProcAddressProc(get_proc_address); |
| 163 | AddGLNativeLibrary(egl_library); |
| 164 | AddGLNativeLibrary(gles_library); |
| 165 | SetGLImplementation(kGLImplementationEGLGLES2); |
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 166 | |
| 167 | InitializeGLBindingsGL(); |
| 168 | InitializeGLBindingsEGL(); |
| 169 | |
| 170 | // These two functions take single precision float rather than double |
| 171 | // precision float parameters in GLES. |
[email protected] | b97c908 | 2012-10-25 17:21:38 | [diff] [blame^] | 172 | ::gfx::g_driver_gl.fn.glClearDepthFn = MarshalClearDepthToClearDepthf; |
| 173 | ::gfx::g_driver_gl.fn.glDepthRangeFn = MarshalDepthRangeToDepthRangef; |
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 174 | break; |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 175 | } |
| 176 | case kGLImplementationMockGL: { |
| 177 | SetGLGetProcAddressProc(GetMockGLProcAddress); |
| 178 | SetGLImplementation(kGLImplementationMockGL); |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 179 | InitializeGLBindingsGL(); |
| 180 | break; |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 181 | } |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 182 | default: |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | |
| 187 | return true; |
| 188 | } |
| 189 | |
[email protected] | 6494823b | 2011-10-27 18:30:44 | [diff] [blame] | 190 | bool 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] | 218a5a2 | 2010-11-04 19:27:49 | [diff] [blame] | 215 | void InitializeDebugGLBindings() { |
| 216 | InitializeDebugGLBindingsEGL(); |
| 217 | InitializeDebugGLBindingsGL(); |
| 218 | InitializeDebugGLBindingsGLX(); |
| 219 | InitializeDebugGLBindingsOSMESA(); |
| 220 | } |
| 221 | |
[email protected] | 0f5e888 | 2011-11-08 22:29:38 | [diff] [blame] | 222 | void ClearGLBindings() { |
| 223 | ClearGLBindingsEGL(); |
| 224 | ClearGLBindingsGL(); |
[email protected] | 0f5e888 | 2011-11-08 22:29:38 | [diff] [blame] | 225 | ClearGLBindingsGLX(); |
| 226 | ClearGLBindingsOSMESA(); |
[email protected] | 0f5e888 | 2011-11-08 22:29:38 | [diff] [blame] | 227 | SetGLImplementation(kGLImplementationNone); |
| 228 | |
| 229 | UnloadGLNativeLibraries(); |
| 230 | } |
| 231 | |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 232 | } // namespace gfx |