OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gl/gl_implementation.h" | 5 #include <vector> |
6 | 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/path_service.h" |
| 10 #include "base/threading/thread_restrictions.h" |
| 11 #include "build/build_config.h" |
| 12 #include "ui/gl/gl_bindings.h" |
7 #include "ui/gl/gl_context_stub_with_extensions.h" | 13 #include "ui/gl/gl_context_stub_with_extensions.h" |
8 #include "ui/gl/gl_egl_api_implementation.h" | 14 #include "ui/gl/gl_egl_api_implementation.h" |
9 #include "ui/gl/gl_gl_api_implementation.h" | 15 #include "ui/gl/gl_gl_api_implementation.h" |
10 #include "ui/gl/gl_glx_api_implementation.h" | 16 #include "ui/gl/gl_glx_api_implementation.h" |
| 17 #include "ui/gl/gl_implementation.h" |
11 #include "ui/gl/gl_implementation_osmesa.h" | 18 #include "ui/gl/gl_implementation_osmesa.h" |
12 #include "ui/gl/gl_osmesa_api_implementation.h" | 19 #include "ui/gl/gl_osmesa_api_implementation.h" |
| 20 #include "ui/gl/gl_switches.h" |
13 | 21 |
14 namespace gl { | 22 namespace gl { |
| 23 namespace { |
| 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 |
| 36 #if defined(OS_OPENBSD) |
| 37 const char kGLLibraryName[] = "libGL.so"; |
| 38 #else |
| 39 const char kGLLibraryName[] = "libGL.so.1"; |
| 40 #endif |
| 41 |
| 42 const char kGLESv2LibraryName[] = "libGLESv2.so.2"; |
| 43 const char kEGLLibraryName[] = "libEGL.so.1"; |
| 44 |
| 45 const char kGLESv2ANGLELibraryName[] = "libGLESv2.so"; |
| 46 const char kEGLANGLELibraryName[] = "libEGL.so"; |
| 47 |
| 48 } // namespace |
15 | 49 |
16 void GetAllowedGLImplementations(std::vector<GLImplementation>* impls) { | 50 void GetAllowedGLImplementations(std::vector<GLImplementation>* impls) { |
17 impls->push_back(kGLImplementationDesktopGL); | 51 impls->push_back(kGLImplementationDesktopGL); |
18 impls->push_back(kGLImplementationEGLGLES2); | 52 impls->push_back(kGLImplementationEGLGLES2); |
19 impls->push_back(kGLImplementationOSMesaGL); | 53 impls->push_back(kGLImplementationOSMesaGL); |
20 } | 54 } |
21 | 55 |
| 56 bool InitializeStaticGLBindings(GLImplementation implementation) { |
| 57 // Prevent reinitialization with a different implementation. Once the gpu |
| 58 // unit tests have initialized with kGLImplementationMock, we don't want to |
| 59 // later switch to another GL implementation. |
| 60 DCHECK_EQ(kGLImplementationNone, GetGLImplementation()); |
| 61 |
| 62 // Allow the main thread or another to initialize these bindings |
| 63 // after instituting restrictions on I/O. Going forward they will |
| 64 // likely be used in the browser process on most platforms. The |
| 65 // one-time initialization cost is small, between 2 and 5 ms. |
| 66 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 67 |
| 68 switch (implementation) { |
| 69 case kGLImplementationOSMesaGL: |
| 70 return InitializeStaticGLBindingsOSMesaGL(); |
| 71 case kGLImplementationDesktopGL: { |
| 72 base::NativeLibrary library = NULL; |
| 73 const base::CommandLine* command_line = |
| 74 base::CommandLine::ForCurrentProcess(); |
| 75 |
| 76 if (command_line->HasSwitch(switches::kTestGLLib)) |
| 77 library = LoadLibraryAndPrintError( |
| 78 command_line->GetSwitchValueASCII(switches::kTestGLLib).c_str()); |
| 79 |
| 80 if (!library) { |
| 81 library = LoadLibraryAndPrintError(kGLLibraryName); |
| 82 } |
| 83 |
| 84 if (!library) |
| 85 return false; |
| 86 |
| 87 GLGetProcAddressProc get_proc_address = |
| 88 reinterpret_cast<GLGetProcAddressProc>( |
| 89 base::GetFunctionPointerFromNativeLibrary( |
| 90 library, "glXGetProcAddress")); |
| 91 if (!get_proc_address) { |
| 92 LOG(ERROR) << "glxGetProcAddress not found."; |
| 93 base::UnloadNativeLibrary(library); |
| 94 return false; |
| 95 } |
| 96 |
| 97 SetGLGetProcAddressProc(get_proc_address); |
| 98 AddGLNativeLibrary(library); |
| 99 SetGLImplementation(kGLImplementationDesktopGL); |
| 100 |
| 101 InitializeStaticGLBindingsGL(); |
| 102 InitializeStaticGLBindingsGLX(); |
| 103 break; |
| 104 } |
| 105 case kGLImplementationEGLGLES2: { |
| 106 base::FilePath glesv2_path(kGLESv2LibraryName); |
| 107 base::FilePath egl_path(kEGLLibraryName); |
| 108 |
| 109 const base::CommandLine* command_line = |
| 110 base::CommandLine::ForCurrentProcess(); |
| 111 if (command_line->GetSwitchValueASCII(switches::kUseGL) == |
| 112 kGLImplementationANGLEName) { |
| 113 base::FilePath module_path; |
| 114 if (!PathService::Get(base::DIR_MODULE, &module_path)) |
| 115 return false; |
| 116 |
| 117 glesv2_path = module_path.Append(kGLESv2ANGLELibraryName); |
| 118 egl_path = module_path.Append(kEGLANGLELibraryName); |
| 119 } |
| 120 |
| 121 base::NativeLibrary gles_library = LoadLibraryAndPrintError(glesv2_path); |
| 122 if (!gles_library) |
| 123 return false; |
| 124 base::NativeLibrary egl_library = LoadLibraryAndPrintError(egl_path); |
| 125 if (!egl_library) { |
| 126 base::UnloadNativeLibrary(gles_library); |
| 127 return false; |
| 128 } |
| 129 |
| 130 GLGetProcAddressProc get_proc_address = |
| 131 reinterpret_cast<GLGetProcAddressProc>( |
| 132 base::GetFunctionPointerFromNativeLibrary( |
| 133 egl_library, "eglGetProcAddress")); |
| 134 if (!get_proc_address) { |
| 135 LOG(ERROR) << "eglGetProcAddress not found."; |
| 136 base::UnloadNativeLibrary(egl_library); |
| 137 base::UnloadNativeLibrary(gles_library); |
| 138 return false; |
| 139 } |
| 140 |
| 141 SetGLGetProcAddressProc(get_proc_address); |
| 142 AddGLNativeLibrary(egl_library); |
| 143 AddGLNativeLibrary(gles_library); |
| 144 SetGLImplementation(kGLImplementationEGLGLES2); |
| 145 |
| 146 InitializeStaticGLBindingsGL(); |
| 147 InitializeStaticGLBindingsEGL(); |
| 148 |
| 149 // These two functions take single precision float rather than double |
| 150 // precision float parameters in GLES. |
| 151 ::gl::g_driver_gl.fn.glClearDepthFn = MarshalClearDepthToClearDepthf; |
| 152 ::gl::g_driver_gl.fn.glDepthRangeFn = MarshalDepthRangeToDepthRangef; |
| 153 break; |
| 154 } |
| 155 case kGLImplementationMockGL: { |
| 156 SetGLImplementation(kGLImplementationMockGL); |
| 157 InitializeStaticGLBindingsGL(); |
| 158 break; |
| 159 } |
| 160 default: |
| 161 return false; |
| 162 } |
| 163 |
| 164 |
| 165 return true; |
| 166 } |
| 167 |
22 bool InitializeDynamicGLBindings(GLImplementation implementation, | 168 bool InitializeDynamicGLBindings(GLImplementation implementation, |
23 GLContext* context) { | 169 GLContext* context) { |
24 switch (implementation) { | 170 switch (implementation) { |
25 case kGLImplementationOSMesaGL: | 171 case kGLImplementationOSMesaGL: |
26 case kGLImplementationDesktopGL: | 172 case kGLImplementationDesktopGL: |
27 case kGLImplementationEGLGLES2: | 173 case kGLImplementationEGLGLES2: |
28 InitializeDynamicGLBindingsGL(context); | 174 InitializeDynamicGLBindingsGL(context); |
29 break; | 175 break; |
30 case kGLImplementationMockGL: | 176 case kGLImplementationMockGL: |
31 if (!context) { | 177 if (!context) { |
32 scoped_refptr<GLContextStubWithExtensions> mock_context( | 178 scoped_refptr<GLContextStubWithExtensions> mock_context( |
33 new GLContextStubWithExtensions()); | 179 new GLContextStubWithExtensions()); |
34 mock_context->SetGLVersionString("3.0"); | 180 mock_context->SetGLVersionString("3.0"); |
35 InitializeDynamicGLBindingsGL(mock_context.get()); | 181 InitializeDynamicGLBindingsGL(mock_context.get()); |
36 } else { | 182 } else |
37 InitializeDynamicGLBindingsGL(context); | 183 InitializeDynamicGLBindingsGL(context); |
38 } | |
39 break; | 184 break; |
40 default: | 185 default: |
41 return false; | 186 return false; |
42 } | 187 } |
43 | 188 |
44 return true; | 189 return true; |
45 } | 190 } |
46 | 191 |
| 192 void InitializeDebugGLBindings() { |
| 193 InitializeDebugGLBindingsEGL(); |
| 194 InitializeDebugGLBindingsGL(); |
| 195 InitializeDebugGLBindingsGLX(); |
| 196 InitializeDebugGLBindingsOSMESA(); |
| 197 } |
| 198 |
| 199 void ClearGLBindings() { |
| 200 ClearGLBindingsEGL(); |
| 201 ClearGLBindingsGL(); |
| 202 ClearGLBindingsGLX(); |
| 203 ClearGLBindingsOSMESA(); |
| 204 SetGLImplementation(kGLImplementationNone); |
| 205 |
| 206 UnloadGLNativeLibraries(); |
| 207 } |
| 208 |
47 bool GetGLWindowSystemBindingInfo(GLWindowSystemBindingInfo* info) { | 209 bool GetGLWindowSystemBindingInfo(GLWindowSystemBindingInfo* info) { |
48 switch (GetGLImplementation()) { | 210 switch (GetGLImplementation()) { |
49 case kGLImplementationDesktopGL: | 211 case kGLImplementationDesktopGL: |
50 return GetGLWindowSystemBindingInfoGLX(info); | 212 return GetGLWindowSystemBindingInfoGLX(info); |
51 case kGLImplementationEGLGLES2: | 213 case kGLImplementationEGLGLES2: |
52 return GetGLWindowSystemBindingInfoEGL(info); | 214 return GetGLWindowSystemBindingInfoEGL(info); |
53 default: | 215 default: |
54 return false; | 216 return false; |
55 } | 217 } |
56 return false; | 218 return false; |
57 } | 219 } |
58 | 220 |
59 } // namespace gl | 221 } // namespace gl |
OLD | NEW |