| 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 <vector> | 5 #include "ui/gl/gl_implementation.h" |
| 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" | |
| 13 #include "ui/gl/gl_context_stub_with_extensions.h" | 7 #include "ui/gl/gl_context_stub_with_extensions.h" |
| 14 #include "ui/gl/gl_egl_api_implementation.h" | 8 #include "ui/gl/gl_egl_api_implementation.h" |
| 15 #include "ui/gl/gl_gl_api_implementation.h" | 9 #include "ui/gl/gl_gl_api_implementation.h" |
| 16 #include "ui/gl/gl_glx_api_implementation.h" | 10 #include "ui/gl/gl_glx_api_implementation.h" |
| 17 #include "ui/gl/gl_implementation.h" | |
| 18 #include "ui/gl/gl_implementation_osmesa.h" | 11 #include "ui/gl/gl_implementation_osmesa.h" |
| 19 #include "ui/gl/gl_osmesa_api_implementation.h" | 12 #include "ui/gl/gl_osmesa_api_implementation.h" |
| 20 #include "ui/gl/gl_switches.h" | |
| 21 | 13 |
| 22 namespace gl { | 14 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 | |
| 49 | 15 |
| 50 void GetAllowedGLImplementations(std::vector<GLImplementation>* impls) { | 16 void GetAllowedGLImplementations(std::vector<GLImplementation>* impls) { |
| 51 impls->push_back(kGLImplementationDesktopGL); | 17 impls->push_back(kGLImplementationDesktopGL); |
| 52 impls->push_back(kGLImplementationEGLGLES2); | 18 impls->push_back(kGLImplementationEGLGLES2); |
| 53 impls->push_back(kGLImplementationOSMesaGL); | 19 impls->push_back(kGLImplementationOSMesaGL); |
| 54 } | 20 } |
| 55 | 21 |
| 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 | |
| 168 bool InitializeDynamicGLBindings(GLImplementation implementation, | 22 bool InitializeDynamicGLBindings(GLImplementation implementation, |
| 169 GLContext* context) { | 23 GLContext* context) { |
| 170 switch (implementation) { | 24 switch (implementation) { |
| 171 case kGLImplementationOSMesaGL: | 25 case kGLImplementationOSMesaGL: |
| 172 case kGLImplementationDesktopGL: | 26 case kGLImplementationDesktopGL: |
| 173 case kGLImplementationEGLGLES2: | 27 case kGLImplementationEGLGLES2: |
| 174 InitializeDynamicGLBindingsGL(context); | 28 InitializeDynamicGLBindingsGL(context); |
| 175 break; | 29 break; |
| 176 case kGLImplementationMockGL: | 30 case kGLImplementationMockGL: |
| 177 if (!context) { | 31 if (!context) { |
| 178 scoped_refptr<GLContextStubWithExtensions> mock_context( | 32 scoped_refptr<GLContextStubWithExtensions> mock_context( |
| 179 new GLContextStubWithExtensions()); | 33 new GLContextStubWithExtensions()); |
| 180 mock_context->SetGLVersionString("3.0"); | 34 mock_context->SetGLVersionString("3.0"); |
| 181 InitializeDynamicGLBindingsGL(mock_context.get()); | 35 InitializeDynamicGLBindingsGL(mock_context.get()); |
| 182 } else | 36 } else { |
| 183 InitializeDynamicGLBindingsGL(context); | 37 InitializeDynamicGLBindingsGL(context); |
| 38 } |
| 184 break; | 39 break; |
| 185 default: | 40 default: |
| 186 return false; | 41 return false; |
| 187 } | 42 } |
| 188 | 43 |
| 189 return true; | 44 return true; |
| 190 } | 45 } |
| 191 | 46 |
| 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 | |
| 209 bool GetGLWindowSystemBindingInfo(GLWindowSystemBindingInfo* info) { | 47 bool GetGLWindowSystemBindingInfo(GLWindowSystemBindingInfo* info) { |
| 210 switch (GetGLImplementation()) { | 48 switch (GetGLImplementation()) { |
| 211 case kGLImplementationDesktopGL: | 49 case kGLImplementationDesktopGL: |
| 212 return GetGLWindowSystemBindingInfoGLX(info); | 50 return GetGLWindowSystemBindingInfoGLX(info); |
| 213 case kGLImplementationEGLGLES2: | 51 case kGLImplementationEGLGLES2: |
| 214 return GetGLWindowSystemBindingInfoEGL(info); | 52 return GetGLWindowSystemBindingInfoEGL(info); |
| 215 default: | 53 default: |
| 216 return false; | 54 return false; |
| 217 } | 55 } |
| 218 return false; | 56 return false; |
| 219 } | 57 } |
| 220 | 58 |
| 221 } // namespace gl | 59 } // namespace gl |
| OLD | NEW |