[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" |
| 10 | #include "base/logging.h" |
| 11 | #include "base/native_library.h" |
| 12 | #include "base/path_service.h" |
[email protected] | 5ae0b28 | 2011-03-28 19:24:49 | [diff] [blame] | 13 | #include "ui/gfx/gl/gl_bindings.h" |
| 14 | #include "ui/gfx/gl/gl_implementation.h" |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 15 | |
| 16 | namespace gfx { |
| 17 | namespace { |
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 18 | |
| 19 | // TODO(piman): it should be Desktop GL marshalling from double to float. Today |
| 20 | // on native GLES, we do float->double->float. |
| 21 | void GL_BINDING_CALL MarshalClearDepthToClearDepthf(GLclampd depth) { |
| 22 | glClearDepthf(static_cast<GLclampf>(depth)); |
| 23 | } |
| 24 | |
| 25 | void GL_BINDING_CALL MarshalDepthRangeToDepthRangef(GLclampd z_near, |
| 26 | GLclampd z_far) { |
| 27 | glDepthRangef(static_cast<GLclampf>(z_near), static_cast<GLclampf>(z_far)); |
| 28 | } |
| 29 | |
[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 30 | // Load a library, printing an error message on failure. |
[email protected] | a24612275 | 2011-04-19 01:10:48 | [diff] [blame^] | 31 | base::NativeLibrary LoadLibrary(const FilePath& filename) { |
[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 32 | std::string error; |
[email protected] | a24612275 | 2011-04-19 01:10:48 | [diff] [blame^] | 33 | base::NativeLibrary library = base::LoadNativeLibrary(filename, |
[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 34 | &error); |
| 35 | if (!library) { |
[email protected] | a24612275 | 2011-04-19 01:10:48 | [diff] [blame^] | 36 | VLOG(1) << "Failed to load " << filename.MaybeAsASCII() << ": " << error; |
[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 37 | return NULL; |
| 38 | } |
| 39 | return library; |
| 40 | } |
| 41 | |
[email protected] | a24612275 | 2011-04-19 01:10:48 | [diff] [blame^] | 42 | base::NativeLibrary LoadLibrary(const char* filename) { |
| 43 | return LoadLibrary(FilePath(filename)); |
| 44 | } |
| 45 | |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 46 | } // namespace anonymous |
| 47 | |
| 48 | bool InitializeGLBindings(GLImplementation implementation) { |
| 49 | // Prevent reinitialization with a different implementation. Once the gpu |
| 50 | // unit tests have initialized with kGLImplementationMock, we don't want to |
| 51 | // later switch to another GL implementation. |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 52 | if (GetGLImplementation() != kGLImplementationNone) |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 53 | return true; |
| 54 | |
| 55 | switch (implementation) { |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 56 | case kGLImplementationOSMesaGL: { |
[email protected] | 36a0979 | 2010-09-10 02:31:39 | [diff] [blame] | 57 | FilePath module_path; |
[email protected] | d049874 | 2010-09-20 20:27:01 | [diff] [blame] | 58 | if (!PathService::Get(base::DIR_MODULE, &module_path)) { |
| 59 | LOG(ERROR) << "PathService::Get failed."; |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 60 | return false; |
[email protected] | d049874 | 2010-09-20 20:27:01 | [diff] [blame] | 61 | } |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 62 | |
[email protected] | a24612275 | 2011-04-19 01:10:48 | [diff] [blame^] | 63 | base::NativeLibrary library = LoadLibrary( |
| 64 | module_path.Append("libosmesa.so")); |
[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 65 | if (!library) |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 66 | return false; |
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 67 | |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 68 | GLGetProcAddressProc get_proc_address = |
| 69 | reinterpret_cast<GLGetProcAddressProc>( |
| 70 | base::GetFunctionPointerFromNativeLibrary( |
| 71 | library, "OSMesaGetProcAddress")); |
[email protected] | 1b2707bb | 2010-10-06 19:37:16 | [diff] [blame] | 72 | if (!get_proc_address) { |
[email protected] | 363b20d | 2011-01-14 22:54:16 | [diff] [blame] | 73 | LOG(ERROR) << "OSMesaGetProcAddress not found."; |
[email protected] | 1b2707bb | 2010-10-06 19:37:16 | [diff] [blame] | 74 | base::UnloadNativeLibrary(library); |
| 75 | return false; |
| 76 | } |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 77 | |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 78 | SetGLGetProcAddressProc(get_proc_address); |
| 79 | AddGLNativeLibrary(library); |
| 80 | SetGLImplementation(kGLImplementationOSMesaGL); |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 81 | |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 82 | InitializeGLBindingsGL(); |
| 83 | InitializeGLBindingsOSMESA(); |
| 84 | break; |
| 85 | } |
| 86 | case kGLImplementationDesktopGL: { |
[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 87 | base::NativeLibrary library = LoadLibrary("libGL.so.1"); |
| 88 | if (!library) |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 89 | return false; |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 90 | |
| 91 | GLGetProcAddressProc get_proc_address = |
| 92 | reinterpret_cast<GLGetProcAddressProc>( |
| 93 | base::GetFunctionPointerFromNativeLibrary( |
| 94 | library, "glXGetProcAddress")); |
[email protected] | 1b2707bb | 2010-10-06 19:37:16 | [diff] [blame] | 95 | if (!get_proc_address) { |
| 96 | LOG(ERROR) << "glxGetProcAddress not found."; |
| 97 | base::UnloadNativeLibrary(library); |
| 98 | return false; |
| 99 | } |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 100 | |
| 101 | SetGLGetProcAddressProc(get_proc_address); |
| 102 | AddGLNativeLibrary(library); |
| 103 | SetGLImplementation(kGLImplementationDesktopGL); |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 104 | |
| 105 | InitializeGLBindingsGL(); |
| 106 | InitializeGLBindingsGLX(); |
| 107 | break; |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 108 | } |
| 109 | case kGLImplementationEGLGLES2: { |
[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 110 | base::NativeLibrary gles_library = LoadLibrary("libGLESv2.so"); |
| 111 | if (!gles_library) |
[email protected] | 1b2707bb | 2010-10-06 19:37:16 | [diff] [blame] | 112 | return false; |
[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 113 | base::NativeLibrary egl_library = LoadLibrary("libEGL.so"); |
| 114 | if (!egl_library) |
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 115 | return false; |
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 116 | |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 117 | GLGetProcAddressProc get_proc_address = |
| 118 | reinterpret_cast<GLGetProcAddressProc>( |
| 119 | base::GetFunctionPointerFromNativeLibrary( |
| 120 | egl_library, "eglGetProcAddress")); |
[email protected] | 1b2707bb | 2010-10-06 19:37:16 | [diff] [blame] | 121 | if (!get_proc_address) { |
| 122 | LOG(ERROR) << "eglGetProcAddress not found."; |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 123 | base::UnloadNativeLibrary(egl_library); |
[email protected] | 1b2707bb | 2010-10-06 19:37:16 | [diff] [blame] | 124 | base::UnloadNativeLibrary(gles_library); |
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 125 | return false; |
| 126 | } |
| 127 | |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 128 | SetGLGetProcAddressProc(get_proc_address); |
| 129 | AddGLNativeLibrary(egl_library); |
| 130 | AddGLNativeLibrary(gles_library); |
| 131 | SetGLImplementation(kGLImplementationEGLGLES2); |
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 132 | |
| 133 | InitializeGLBindingsGL(); |
| 134 | InitializeGLBindingsEGL(); |
| 135 | |
| 136 | // These two functions take single precision float rather than double |
| 137 | // precision float parameters in GLES. |
| 138 | ::gfx::g_glClearDepth = MarshalClearDepthToClearDepthf; |
| 139 | ::gfx::g_glDepthRange = MarshalDepthRangeToDepthRangef; |
| 140 | break; |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 141 | } |
| 142 | case kGLImplementationMockGL: { |
| 143 | SetGLGetProcAddressProc(GetMockGLProcAddress); |
| 144 | SetGLImplementation(kGLImplementationMockGL); |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 145 | InitializeGLBindingsGL(); |
| 146 | break; |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 147 | } |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 148 | default: |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | |
| 153 | return true; |
| 154 | } |
| 155 | |
[email protected] | 218a5a2 | 2010-11-04 19:27:49 | [diff] [blame] | 156 | void InitializeDebugGLBindings() { |
| 157 | InitializeDebugGLBindingsEGL(); |
| 158 | InitializeDebugGLBindingsGL(); |
| 159 | InitializeDebugGLBindingsGLX(); |
| 160 | InitializeDebugGLBindingsOSMESA(); |
| 161 | } |
| 162 | |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 163 | } // namespace gfx |