[email protected] | 991c568 | 2012-01-30 13:32:34 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 5 | #include <vector> |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 6 | |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 7 | #include "base/command_line.h" |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 8 | #include "base/logging.h" |
[email protected] | 6e318e0e | 2011-10-14 23:08:20 | [diff] [blame] | 9 | #include "base/threading/thread_restrictions.h" |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 10 | #include "ui/gl/gl_bindings.h" |
[email protected] | b97c908 | 2012-10-25 17:21:38 | [diff] [blame] | 11 | #include "ui/gl/gl_egl_api_implementation.h" |
12 | #include "ui/gl/gl_gl_api_implementation.h" | ||||
13 | #include "ui/gl/gl_glx_api_implementation.h" | ||||
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 14 | #include "ui/gl/gl_implementation.h" |
[email protected] | 31e8a4f | 2013-06-29 01:13:27 | [diff] [blame] | 15 | #include "ui/gl/gl_implementation_linux.h" |
[email protected] | b97c908 | 2012-10-25 17:21:38 | [diff] [blame] | 16 | #include "ui/gl/gl_osmesa_api_implementation.h" |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 17 | #include "ui/gl/gl_switches.h" |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 18 | |
19 | namespace gfx { | ||||
20 | namespace { | ||||
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 21 | |
22 | // TODO(piman): it should be Desktop GL marshalling from double to float. Today | ||||
23 | // on native GLES, we do float->double->float. | ||||
24 | void GL_BINDING_CALL MarshalClearDepthToClearDepthf(GLclampd depth) { | ||||
25 | glClearDepthf(static_cast<GLclampf>(depth)); | ||||
26 | } | ||||
27 | |||||
28 | void GL_BINDING_CALL MarshalDepthRangeToDepthRangef(GLclampd z_near, | ||||
29 | GLclampd z_far) { | ||||
30 | glDepthRangef(static_cast<GLclampf>(z_near), static_cast<GLclampf>(z_far)); | ||||
31 | } | ||||
32 | |||||
[email protected] | 83afcbcc | 2012-07-27 03:06:27 | [diff] [blame] | 33 | } // namespace |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 34 | |
[email protected] | 0f5e888 | 2011-11-08 22:29:38 | [diff] [blame] | 35 | void GetAllowedGLImplementations(std::vector<GLImplementation>* impls) { |
[email protected] | 3c4b7ef | 2011-11-21 22:24:49 | [diff] [blame] | 36 | impls->push_back(kGLImplementationDesktopGL); |
[email protected] | 3c4b7ef | 2011-11-21 22:24:49 | [diff] [blame] | 37 | impls->push_back(kGLImplementationEGLGLES2); |
[email protected] | 70518b1 | 2011-11-22 01:39:12 | [diff] [blame] | 38 | impls->push_back(kGLImplementationOSMesaGL); |
[email protected] | 0f5e888 | 2011-11-08 22:29:38 | [diff] [blame] | 39 | } |
40 | |||||
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 41 | bool InitializeGLBindings(GLImplementation implementation) { |
42 | // Prevent reinitialization with a different implementation. Once the gpu | ||||
43 | // unit tests have initialized with kGLImplementationMock, we don't want to | ||||
44 | // later switch to another GL implementation. | ||||
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 45 | if (GetGLImplementation() != kGLImplementationNone) |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 46 | return true; |
47 | |||||
[email protected] | 6e318e0e | 2011-10-14 23:08:20 | [diff] [blame] | 48 | // Allow the main thread or another to initialize these bindings |
49 | // after instituting restrictions on I/O. Going forward they will | ||||
50 | // likely be used in the browser process on most platforms. The | ||||
51 | // one-time initialization cost is small, between 2 and 5 ms. | ||||
52 | base::ThreadRestrictions::ScopedAllowIO allow_io; | ||||
53 | |||||
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 54 | switch (implementation) { |
[email protected] | 31e8a4f | 2013-06-29 01:13:27 | [diff] [blame] | 55 | case kGLImplementationOSMesaGL: |
56 | return InitializeGLBindingsOSMesaGL(); | ||||
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 57 | case kGLImplementationDesktopGL: { |
[email protected] | 991c568 | 2012-01-30 13:32:34 | [diff] [blame] | 58 | base::NativeLibrary library = NULL; |
59 | const CommandLine* command_line = CommandLine::ForCurrentProcess(); | ||||
60 | |||||
61 | if (command_line->HasSwitch(switches::kTestGLLib)) | ||||
62 | library = LoadLibrary(command_line->GetSwitchValueASCII( | ||||
63 | switches::kTestGLLib).c_str()); | ||||
64 | |||||
65 | if (!library) { | ||||
[email protected] | 7f4115e | 2011-10-26 20:09:45 | [diff] [blame] | 66 | #if defined(OS_OPENBSD) |
[email protected] | 991c568 | 2012-01-30 13:32:34 | [diff] [blame] | 67 | library = LoadLibrary("libGL.so"); |
[email protected] | 7f4115e | 2011-10-26 20:09:45 | [diff] [blame] | 68 | #else |
[email protected] | 991c568 | 2012-01-30 13:32:34 | [diff] [blame] | 69 | library = LoadLibrary("libGL.so.1"); |
[email protected] | 7f4115e | 2011-10-26 20:09:45 | [diff] [blame] | 70 | #endif |
[email protected] | 991c568 | 2012-01-30 13:32:34 | [diff] [blame] | 71 | } |
72 | |||||
[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 73 | if (!library) |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 74 | return false; |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 75 | |
76 | GLGetProcAddressProc get_proc_address = | ||||
77 | reinterpret_cast<GLGetProcAddressProc>( | ||||
78 | base::GetFunctionPointerFromNativeLibrary( | ||||
79 | library, "glXGetProcAddress")); | ||||
[email protected] | 1b2707bb | 2010-10-06 19:37:16 | [diff] [blame] | 80 | if (!get_proc_address) { |
81 | LOG(ERROR) << "glxGetProcAddress not found."; | ||||
82 | base::UnloadNativeLibrary(library); | ||||
83 | return false; | ||||
84 | } | ||||
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 85 | |
86 | SetGLGetProcAddressProc(get_proc_address); | ||||
87 | AddGLNativeLibrary(library); | ||||
88 | SetGLImplementation(kGLImplementationDesktopGL); | ||||
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 89 | |
90 | InitializeGLBindingsGL(); | ||||
91 | InitializeGLBindingsGLX(); | ||||
92 | break; | ||||
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 93 | } |
94 | case kGLImplementationEGLGLES2: { | ||||
[email protected] | 3733a14 | 2012-02-08 22:05:08 | [diff] [blame] | 95 | base::NativeLibrary gles_library = LoadLibrary("libGLESv2.so.2"); |
[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 96 | if (!gles_library) |
[email protected] | 1b2707bb | 2010-10-06 19:37:16 | [diff] [blame] | 97 | return false; |
[email protected] | 3733a14 | 2012-02-08 22:05:08 | [diff] [blame] | 98 | base::NativeLibrary egl_library = LoadLibrary("libEGL.so.1"); |
[email protected] | 91ac84f7 | 2011-06-22 19:48:02 | [diff] [blame] | 99 | if (!egl_library) { |
100 | base::UnloadNativeLibrary(gles_library); | ||||
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 101 | return false; |
[email protected] | 91ac84f7 | 2011-06-22 19:48:02 | [diff] [blame] | 102 | } |
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 103 | |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 104 | GLGetProcAddressProc get_proc_address = |
105 | reinterpret_cast<GLGetProcAddressProc>( | ||||
106 | base::GetFunctionPointerFromNativeLibrary( | ||||
107 | egl_library, "eglGetProcAddress")); | ||||
[email protected] | 1b2707bb | 2010-10-06 19:37:16 | [diff] [blame] | 108 | if (!get_proc_address) { |
109 | LOG(ERROR) << "eglGetProcAddress not found."; | ||||
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 110 | base::UnloadNativeLibrary(egl_library); |
[email protected] | 1b2707bb | 2010-10-06 19:37:16 | [diff] [blame] | 111 | base::UnloadNativeLibrary(gles_library); |
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 112 | return false; |
113 | } | ||||
114 | |||||
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 115 | SetGLGetProcAddressProc(get_proc_address); |
116 | AddGLNativeLibrary(egl_library); | ||||
117 | AddGLNativeLibrary(gles_library); | ||||
118 | SetGLImplementation(kGLImplementationEGLGLES2); | ||||
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 119 | |
120 | InitializeGLBindingsGL(); | ||||
121 | InitializeGLBindingsEGL(); | ||||
122 | |||||
123 | // These two functions take single precision float rather than double | ||||
124 | // precision float parameters in GLES. | ||||
[email protected] | b97c908 | 2012-10-25 17:21:38 | [diff] [blame] | 125 | ::gfx::g_driver_gl.fn.glClearDepthFn = MarshalClearDepthToClearDepthf; |
126 | ::gfx::g_driver_gl.fn.glDepthRangeFn = MarshalDepthRangeToDepthRangef; | ||||
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 127 | break; |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 128 | } |
129 | case kGLImplementationMockGL: { | ||||
130 | SetGLGetProcAddressProc(GetMockGLProcAddress); | ||||
131 | SetGLImplementation(kGLImplementationMockGL); | ||||
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 132 | InitializeGLBindingsGL(); |
133 | break; | ||||
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 134 | } |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 135 | default: |
136 | return false; | ||||
137 | } | ||||
138 | |||||
139 | |||||
140 | return true; | ||||
141 | } | ||||
142 | |||||
[email protected] | 6494823b | 2011-10-27 18:30:44 | [diff] [blame] | 143 | bool InitializeGLExtensionBindings(GLImplementation implementation, |
144 | GLContext* context) { | ||||
145 | switch (implementation) { | ||||
146 | case kGLImplementationOSMesaGL: | ||||
147 | InitializeGLExtensionBindingsGL(context); | ||||
148 | InitializeGLExtensionBindingsOSMESA(context); | ||||
149 | break; | ||||
150 | case kGLImplementationDesktopGL: | ||||
151 | InitializeGLExtensionBindingsGL(context); | ||||
152 | InitializeGLExtensionBindingsGLX(context); | ||||
153 | break; | ||||
154 | case kGLImplementationEGLGLES2: | ||||
155 | InitializeGLExtensionBindingsGL(context); | ||||
156 | InitializeGLExtensionBindingsEGL(context); | ||||
157 | break; | ||||
158 | case kGLImplementationMockGL: | ||||
159 | InitializeGLExtensionBindingsGL(context); | ||||
160 | break; | ||||
161 | default: | ||||
162 | return false; | ||||
163 | } | ||||
164 | |||||
165 | return true; | ||||
166 | } | ||||
167 | |||||
[email protected] | 218a5a2 | 2010-11-04 19:27:49 | [diff] [blame] | 168 | void InitializeDebugGLBindings() { |
169 | InitializeDebugGLBindingsEGL(); | ||||
170 | InitializeDebugGLBindingsGL(); | ||||
171 | InitializeDebugGLBindingsGLX(); | ||||
172 | InitializeDebugGLBindingsOSMESA(); | ||||
173 | } | ||||
174 | |||||
[email protected] | 0f5e888 | 2011-11-08 22:29:38 | [diff] [blame] | 175 | void ClearGLBindings() { |
176 | ClearGLBindingsEGL(); | ||||
177 | ClearGLBindingsGL(); | ||||
[email protected] | 0f5e888 | 2011-11-08 22:29:38 | [diff] [blame] | 178 | ClearGLBindingsGLX(); |
179 | ClearGLBindingsOSMESA(); | ||||
[email protected] | 0f5e888 | 2011-11-08 22:29:38 | [diff] [blame] | 180 | SetGLImplementation(kGLImplementationNone); |
181 | |||||
182 | UnloadGLNativeLibraries(); | ||||
183 | } | ||||
184 | |||||
[email protected] | 4589503 | 2013-05-30 17:06:43 | [diff] [blame] | 185 | bool GetGLWindowSystemBindingInfo(GLWindowSystemBindingInfo* info) { |
186 | switch (GetGLImplementation()) { | ||||
187 | case kGLImplementationDesktopGL: | ||||
188 | return GetGLWindowSystemBindingInfoGLX(info); | ||||
189 | case kGLImplementationEGLGLES2: | ||||
190 | return GetGLWindowSystemBindingInfoEGL(info); | ||||
191 | default: | ||||
192 | return false; | ||||
193 | } | ||||
194 | return false; | ||||
195 | } | ||||
196 | |||||
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 197 | } // namespace gfx |