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 <vector> | |
6 | |
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" | |
13 #include "base/threading/thread_restrictions.h" | |
14 #include "ui/gfx/gl/gl_bindings.h" | |
15 #include "ui/gfx/gl/gl_implementation.h" | |
16 #include "ui/gfx/gl/gl_switches.h" | |
17 | |
18 namespace gfx { | |
19 namespace { | |
20 | |
21 // TODO(piman): it should be Desktop GL marshalling from double to float. Today | |
22 // on native GLES, we do float->double->float. | |
23 void GL_BINDING_CALL MarshalClearDepthToClearDepthf(GLclampd depth) { | |
24 glClearDepthf(static_cast<GLclampf>(depth)); | |
25 } | |
26 | |
27 void GL_BINDING_CALL MarshalDepthRangeToDepthRangef(GLclampd z_near, | |
28 GLclampd z_far) { | |
29 glDepthRangef(static_cast<GLclampf>(z_near), static_cast<GLclampf>(z_far)); | |
30 } | |
31 | |
32 // Load a library, printing an error message on failure. | |
33 base::NativeLibrary LoadLibrary(const FilePath& filename) { | |
34 std::string error; | |
35 base::NativeLibrary library = base::LoadNativeLibrary(filename, | |
36 &error); | |
37 if (!library) { | |
38 DVLOG(1) << "Failed to load " << filename.MaybeAsASCII() << ": " << error; | |
39 return NULL; | |
40 } | |
41 return library; | |
42 } | |
43 | |
44 base::NativeLibrary LoadLibrary(const char* filename) { | |
45 return LoadLibrary(FilePath(filename)); | |
46 } | |
47 | |
48 } // namespace anonymous | |
49 | |
50 void GetAllowedGLImplementations(std::vector<GLImplementation>* impls) { | |
51 impls->push_back(kGLImplementationDesktopGL); | |
52 impls->push_back(kGLImplementationEGLGLES2); | |
53 impls->push_back(kGLImplementationOSMesaGL); | |
54 } | |
55 | |
56 bool InitializeGLBindings(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 if (GetGLImplementation() != kGLImplementationNone) | |
61 return true; | |
62 | |
63 // Allow the main thread or another to initialize these bindings | |
64 // after instituting restrictions on I/O. Going forward they will | |
65 // likely be used in the browser process on most platforms. The | |
66 // one-time initialization cost is small, between 2 and 5 ms. | |
67 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
68 | |
69 switch (implementation) { | |
70 case kGLImplementationOSMesaGL: { | |
71 FilePath module_path; | |
72 if (!PathService::Get(base::DIR_MODULE, &module_path)) { | |
73 LOG(ERROR) << "PathService::Get failed."; | |
74 return false; | |
75 } | |
76 | |
77 base::NativeLibrary library = LoadLibrary( | |
78 module_path.Append("libosmesa.so")); | |
79 if (!library) | |
80 return false; | |
81 | |
82 GLGetProcAddressProc get_proc_address = | |
83 reinterpret_cast<GLGetProcAddressProc>( | |
84 base::GetFunctionPointerFromNativeLibrary( | |
85 library, "OSMesaGetProcAddress")); | |
86 if (!get_proc_address) { | |
87 LOG(ERROR) << "OSMesaGetProcAddress not found."; | |
88 base::UnloadNativeLibrary(library); | |
89 return false; | |
90 } | |
91 | |
92 SetGLGetProcAddressProc(get_proc_address); | |
93 AddGLNativeLibrary(library); | |
94 SetGLImplementation(kGLImplementationOSMesaGL); | |
95 | |
96 InitializeGLBindingsGL(); | |
97 InitializeGLBindingsOSMESA(); | |
98 break; | |
99 } | |
100 case kGLImplementationDesktopGL: { | |
101 base::NativeLibrary library = NULL; | |
102 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
103 | |
104 if (command_line->HasSwitch(switches::kTestGLLib)) | |
105 library = LoadLibrary(command_line->GetSwitchValueASCII( | |
106 switches::kTestGLLib).c_str()); | |
107 | |
108 if (!library) { | |
109 #if defined(OS_OPENBSD) | |
110 library = LoadLibrary("libGL.so"); | |
111 #else | |
112 library = LoadLibrary("libGL.so.1"); | |
113 #endif | |
114 } | |
115 | |
116 if (!library) | |
117 return false; | |
118 | |
119 GLGetProcAddressProc get_proc_address = | |
120 reinterpret_cast<GLGetProcAddressProc>( | |
121 base::GetFunctionPointerFromNativeLibrary( | |
122 library, "glXGetProcAddress")); | |
123 if (!get_proc_address) { | |
124 LOG(ERROR) << "glxGetProcAddress not found."; | |
125 base::UnloadNativeLibrary(library); | |
126 return false; | |
127 } | |
128 | |
129 SetGLGetProcAddressProc(get_proc_address); | |
130 AddGLNativeLibrary(library); | |
131 SetGLImplementation(kGLImplementationDesktopGL); | |
132 | |
133 InitializeGLBindingsGL(); | |
134 InitializeGLBindingsGLX(); | |
135 break; | |
136 } | |
137 case kGLImplementationEGLGLES2: { | |
138 base::NativeLibrary gles_library = LoadLibrary("libGLESv2.so.2"); | |
139 if (!gles_library) | |
140 return false; | |
141 base::NativeLibrary egl_library = LoadLibrary("libEGL.so.1"); | |
142 if (!egl_library) { | |
143 base::UnloadNativeLibrary(gles_library); | |
144 return false; | |
145 } | |
146 | |
147 GLGetProcAddressProc get_proc_address = | |
148 reinterpret_cast<GLGetProcAddressProc>( | |
149 base::GetFunctionPointerFromNativeLibrary( | |
150 egl_library, "eglGetProcAddress")); | |
151 if (!get_proc_address) { | |
152 LOG(ERROR) << "eglGetProcAddress not found."; | |
153 base::UnloadNativeLibrary(egl_library); | |
154 base::UnloadNativeLibrary(gles_library); | |
155 return false; | |
156 } | |
157 | |
158 SetGLGetProcAddressProc(get_proc_address); | |
159 AddGLNativeLibrary(egl_library); | |
160 AddGLNativeLibrary(gles_library); | |
161 SetGLImplementation(kGLImplementationEGLGLES2); | |
162 | |
163 InitializeGLBindingsGL(); | |
164 InitializeGLBindingsEGL(); | |
165 | |
166 // These two functions take single precision float rather than double | |
167 // precision float parameters in GLES. | |
168 ::gfx::g_glClearDepth = MarshalClearDepthToClearDepthf; | |
169 ::gfx::g_glDepthRange = MarshalDepthRangeToDepthRangef; | |
170 break; | |
171 } | |
172 case kGLImplementationMockGL: { | |
173 SetGLGetProcAddressProc(GetMockGLProcAddress); | |
174 SetGLImplementation(kGLImplementationMockGL); | |
175 InitializeGLBindingsGL(); | |
176 break; | |
177 } | |
178 default: | |
179 return false; | |
180 } | |
181 | |
182 | |
183 return true; | |
184 } | |
185 | |
186 bool InitializeGLExtensionBindings(GLImplementation implementation, | |
187 GLContext* context) { | |
188 switch (implementation) { | |
189 case kGLImplementationOSMesaGL: | |
190 InitializeGLExtensionBindingsGL(context); | |
191 InitializeGLExtensionBindingsOSMESA(context); | |
192 break; | |
193 case kGLImplementationDesktopGL: | |
194 InitializeGLExtensionBindingsGL(context); | |
195 InitializeGLExtensionBindingsGLX(context); | |
196 break; | |
197 case kGLImplementationEGLGLES2: | |
198 InitializeGLExtensionBindingsGL(context); | |
199 InitializeGLExtensionBindingsEGL(context); | |
200 break; | |
201 case kGLImplementationMockGL: | |
202 InitializeGLExtensionBindingsGL(context); | |
203 break; | |
204 default: | |
205 return false; | |
206 } | |
207 | |
208 return true; | |
209 } | |
210 | |
211 void InitializeDebugGLBindings() { | |
212 InitializeDebugGLBindingsEGL(); | |
213 InitializeDebugGLBindingsGL(); | |
214 InitializeDebugGLBindingsGLX(); | |
215 InitializeDebugGLBindingsOSMESA(); | |
216 } | |
217 | |
218 void ClearGLBindings() { | |
219 ClearGLBindingsEGL(); | |
220 ClearGLBindingsGL(); | |
221 ClearGLBindingsGLX(); | |
222 ClearGLBindingsOSMESA(); | |
223 SetGLImplementation(kGLImplementationNone); | |
224 | |
225 UnloadGLNativeLibraries(); | |
226 } | |
227 | |
228 } // namespace gfx | |
OLD | NEW |