OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include <d3dx9.h> | |
6 | |
7 #include <vector> | |
8 | |
9 #include "base/at_exit.h" | |
10 #include "base/base_paths.h" | |
11 #include "base/bind.h" | |
12 #include "base/command_line.h" | |
13 #include "base/file_path.h" | |
14 #include "base/logging.h" | |
15 #include "base/native_library.h" | |
16 #include "base/path_service.h" | |
17 #include "base/stringprintf.h" | |
18 #include "base/threading/thread_restrictions.h" | |
19 #include "ui/gfx/gl/gl_bindings.h" | |
20 #include "ui/gfx/gl/gl_implementation.h" | |
21 | |
22 #if defined(ENABLE_SWIFTSHADER) | |
23 #include "software_renderer.h" | |
24 #endif | |
25 | |
26 namespace gfx { | |
27 | |
28 namespace { | |
29 | |
30 typedef std::vector<base::NativeLibrary> LibraryArray; | |
31 LibraryArray* g_d3dx_libraries; | |
32 | |
33 void GL_BINDING_CALL MarshalClearDepthToClearDepthf(GLclampd depth) { | |
34 glClearDepthf(static_cast<GLclampf>(depth)); | |
35 } | |
36 | |
37 void GL_BINDING_CALL MarshalDepthRangeToDepthRangef(GLclampd z_near, | |
38 GLclampd z_far) { | |
39 glDepthRangef(static_cast<GLclampf>(z_near), static_cast<GLclampf>(z_far)); | |
40 } | |
41 | |
42 void UnloadD3DXLibraries(void* unused) { | |
43 if (g_d3dx_libraries) { | |
44 for (LibraryArray::iterator it = g_d3dx_libraries->begin(); | |
45 it != g_d3dx_libraries->end(); ++it) { | |
46 base::UnloadNativeLibrary(*it); | |
47 } | |
48 delete g_d3dx_libraries; | |
49 g_d3dx_libraries = NULL; | |
50 } | |
51 } | |
52 | |
53 bool LoadD3DXLibrary(const FilePath& module_path, | |
54 const FilePath::StringType& name) { | |
55 base::NativeLibrary library = base::LoadNativeLibrary(FilePath(name), NULL); | |
56 if (!library) { | |
57 library = base::LoadNativeLibrary(module_path.Append(name), NULL); | |
58 if (!library) { | |
59 DVLOG(1) << name << " not found."; | |
60 return false; | |
61 } | |
62 } | |
63 | |
64 if (!g_d3dx_libraries) { | |
65 g_d3dx_libraries = new LibraryArray; | |
66 base::AtExitManager::RegisterCallback(UnloadD3DXLibraries, NULL); | |
67 } | |
68 g_d3dx_libraries->push_back(library); | |
69 return true; | |
70 } | |
71 | |
72 } // namespace anonymous | |
73 | |
74 void GetAllowedGLImplementations(std::vector<GLImplementation>* impls) { | |
75 impls->push_back(kGLImplementationEGLGLES2); | |
76 impls->push_back(kGLImplementationDesktopGL); | |
77 impls->push_back(kGLImplementationOSMesaGL); | |
78 } | |
79 | |
80 bool InitializeGLBindings(GLImplementation implementation) { | |
81 // Prevent reinitialization with a different implementation. Once the gpu | |
82 // unit tests have initialized with kGLImplementationMock, we don't want to | |
83 // later switch to another GL implementation. | |
84 if (GetGLImplementation() != kGLImplementationNone) | |
85 return true; | |
86 | |
87 // Allow the main thread or another to initialize these bindings | |
88 // after instituting restrictions on I/O. Going forward they will | |
89 // likely be used in the browser process on most platforms. The | |
90 // one-time initialization cost is small, between 2 and 5 ms. | |
91 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
92 | |
93 switch (implementation) { | |
94 case kGLImplementationOSMesaGL: { | |
95 FilePath module_path; | |
96 if (!PathService::Get(base::DIR_MODULE, &module_path)) { | |
97 LOG(ERROR) << "PathService::Get failed."; | |
98 return false; | |
99 } | |
100 | |
101 base::NativeLibrary library = base::LoadNativeLibrary( | |
102 module_path.Append(L"osmesa.dll"), NULL); | |
103 if (!library) { | |
104 DVLOG(1) << "osmesa.dll not found"; | |
105 return false; | |
106 } | |
107 | |
108 GLGetProcAddressProc get_proc_address = | |
109 reinterpret_cast<GLGetProcAddressProc>( | |
110 base::GetFunctionPointerFromNativeLibrary( | |
111 library, "OSMesaGetProcAddress")); | |
112 if (!get_proc_address) { | |
113 DLOG(ERROR) << "OSMesaGetProcAddress not found."; | |
114 base::UnloadNativeLibrary(library); | |
115 return false; | |
116 } | |
117 | |
118 SetGLGetProcAddressProc(get_proc_address); | |
119 AddGLNativeLibrary(library); | |
120 SetGLImplementation(kGLImplementationOSMesaGL); | |
121 | |
122 InitializeGLBindingsGL(); | |
123 InitializeGLBindingsOSMESA(); | |
124 break; | |
125 } | |
126 case kGLImplementationEGLGLES2: { | |
127 FilePath module_path; | |
128 if (!PathService::Get(base::DIR_MODULE, &module_path)) | |
129 return false; | |
130 | |
131 // Attempt to load D3DX and its dependencies using the default search path | |
132 // and if that fails, using an absolute path. This is to ensure these DLLs | |
133 // are loaded before ANGLE is loaded in case they are not in the default | |
134 // search path. | |
135 LoadD3DXLibrary(module_path, base::StringPrintf(L"d3dcompiler_%d.dll", | |
136 D3DX_SDK_VERSION)); | |
137 LoadD3DXLibrary(module_path, base::StringPrintf(L"d3dx9_%d.dll", | |
138 D3DX_SDK_VERSION)); | |
139 | |
140 FilePath gles_path; | |
141 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
142 bool using_swift_shader = | |
143 command_line->GetSwitchValueASCII(switches::kUseGL) == "swiftshader"; | |
144 if (using_swift_shader) { | |
145 if (!command_line->HasSwitch(switches::kSwiftShaderPath)) | |
146 return false; | |
147 gles_path = | |
148 command_line->GetSwitchValuePath(switches::kSwiftShaderPath); | |
149 // Preload library | |
150 LoadLibrary(L"ddraw.dll"); | |
151 } else { | |
152 gles_path = module_path; | |
153 } | |
154 | |
155 // Load libglesv2.dll before libegl.dll because the latter is dependent on | |
156 // the former and if there is another version of libglesv2.dll in the dll | |
157 // search path, it will get loaded instead. | |
158 base::NativeLibrary gles_library = base::LoadNativeLibrary( | |
159 gles_path.Append(L"libglesv2.dll"), NULL); | |
160 if (!gles_library) { | |
161 DVLOG(1) << "libglesv2.dll not found"; | |
162 return false; | |
163 } | |
164 | |
165 // When using EGL, first try eglGetProcAddress and then Windows | |
166 // GetProcAddress on both the EGL and GLES2 DLLs. | |
167 base::NativeLibrary egl_library = base::LoadNativeLibrary( | |
168 gles_path.Append(L"libegl.dll"), NULL); | |
169 if (!egl_library) { | |
170 DVLOG(1) << "libegl.dll not found."; | |
171 base::UnloadNativeLibrary(gles_library); | |
172 return false; | |
173 } | |
174 | |
175 #if defined(ENABLE_SWIFTSHADER) | |
176 if (using_swift_shader) { | |
177 SetupSoftwareRenderer(gles_library); | |
178 } | |
179 #endif | |
180 | |
181 GLGetProcAddressProc get_proc_address = | |
182 reinterpret_cast<GLGetProcAddressProc>( | |
183 base::GetFunctionPointerFromNativeLibrary( | |
184 egl_library, "eglGetProcAddress")); | |
185 if (!get_proc_address) { | |
186 LOG(ERROR) << "eglGetProcAddress not found."; | |
187 base::UnloadNativeLibrary(egl_library); | |
188 base::UnloadNativeLibrary(gles_library); | |
189 return false; | |
190 } | |
191 | |
192 SetGLGetProcAddressProc(get_proc_address); | |
193 AddGLNativeLibrary(egl_library); | |
194 AddGLNativeLibrary(gles_library); | |
195 SetGLImplementation(kGLImplementationEGLGLES2); | |
196 | |
197 InitializeGLBindingsGL(); | |
198 InitializeGLBindingsEGL(); | |
199 | |
200 // These two functions take single precision float rather than double | |
201 // precision float parameters in GLES. | |
202 ::gfx::g_glClearDepth = MarshalClearDepthToClearDepthf; | |
203 ::gfx::g_glDepthRange = MarshalDepthRangeToDepthRangef; | |
204 break; | |
205 } | |
206 case kGLImplementationDesktopGL: { | |
207 // When using Windows OpenGL, first try wglGetProcAddress and then | |
208 // Windows GetProcAddress. | |
209 base::NativeLibrary library = base::LoadNativeLibrary( | |
210 FilePath(L"opengl32.dll"), NULL); | |
211 if (!library) { | |
212 DVLOG(1) << "opengl32.dll not found"; | |
213 return false; | |
214 } | |
215 | |
216 GLGetProcAddressProc get_proc_address = | |
217 reinterpret_cast<GLGetProcAddressProc>( | |
218 base::GetFunctionPointerFromNativeLibrary( | |
219 library, "wglGetProcAddress")); | |
220 if (!get_proc_address) { | |
221 LOG(ERROR) << "wglGetProcAddress not found."; | |
222 base::UnloadNativeLibrary(library); | |
223 return false; | |
224 } | |
225 | |
226 SetGLGetProcAddressProc(get_proc_address); | |
227 AddGLNativeLibrary(library); | |
228 SetGLImplementation(kGLImplementationDesktopGL); | |
229 | |
230 InitializeGLBindingsGL(); | |
231 InitializeGLBindingsWGL(); | |
232 break; | |
233 } | |
234 case kGLImplementationMockGL: { | |
235 SetGLGetProcAddressProc(GetMockGLProcAddress); | |
236 SetGLImplementation(kGLImplementationMockGL); | |
237 InitializeGLBindingsGL(); | |
238 break; | |
239 } | |
240 default: | |
241 return false; | |
242 } | |
243 | |
244 return true; | |
245 } | |
246 | |
247 bool InitializeGLExtensionBindings(GLImplementation implementation, | |
248 GLContext* context) { | |
249 switch (implementation) { | |
250 case kGLImplementationOSMesaGL: | |
251 InitializeGLExtensionBindingsGL(context); | |
252 InitializeGLExtensionBindingsOSMESA(context); | |
253 break; | |
254 case kGLImplementationEGLGLES2: | |
255 InitializeGLExtensionBindingsGL(context); | |
256 InitializeGLExtensionBindingsEGL(context); | |
257 break; | |
258 case kGLImplementationDesktopGL: | |
259 InitializeGLExtensionBindingsGL(context); | |
260 InitializeGLExtensionBindingsWGL(context); | |
261 break; | |
262 case kGLImplementationMockGL: | |
263 InitializeGLExtensionBindingsGL(context); | |
264 break; | |
265 default: | |
266 return false; | |
267 } | |
268 | |
269 return true; | |
270 } | |
271 | |
272 void InitializeDebugGLBindings() { | |
273 InitializeDebugGLBindingsEGL(); | |
274 InitializeDebugGLBindingsGL(); | |
275 InitializeDebugGLBindingsOSMESA(); | |
276 InitializeDebugGLBindingsWGL(); | |
277 } | |
278 | |
279 void ClearGLBindings() { | |
280 ClearGLBindingsEGL(); | |
281 ClearGLBindingsGL(); | |
282 ClearGLBindingsOSMESA(); | |
283 ClearGLBindingsWGL(); | |
284 SetGLImplementation(kGLImplementationNone); | |
285 | |
286 UnloadGLNativeLibraries(); | |
287 UnloadD3DXLibraries(NULL); | |
288 } | |
289 | |
290 } // namespace gfx | |
OLD | NEW |