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