blob: 01ee655ff11b1b5922c7af9801e3d07b74df9546 [file] [log] [blame]
[email protected]991c5682012-01-30 13:32:341// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]b9363b22010-06-09 22:06:152// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]0bec8e22010-06-21 22:20:025#include <vector>
[email protected]b9363b22010-06-09 22:06:156
[email protected]30aa5c1a2010-07-14 20:47:047#include "base/command_line.h"
[email protected]30aa5c1a2010-07-14 20:47:048#include "base/logging.h"
[email protected]6e318e0e2011-10-14 23:08:209#include "base/threading/thread_restrictions.h"
[email protected]c9e2cbbb2012-05-12 21:17:2710#include "ui/gl/gl_bindings.h"
[email protected]b97c9082012-10-25 17:21:3811#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]c9e2cbbb2012-05-12 21:17:2714#include "ui/gl/gl_implementation.h"
[email protected]31e8a4f2013-06-29 01:13:2715#include "ui/gl/gl_implementation_linux.h"
[email protected]b97c9082012-10-25 17:21:3816#include "ui/gl/gl_osmesa_api_implementation.h"
[email protected]c9e2cbbb2012-05-12 21:17:2717#include "ui/gl/gl_switches.h"
[email protected]b9363b22010-06-09 22:06:1518
19namespace gfx {
20namespace {
[email protected]0bec8e22010-06-21 22:20:0221
22// TODO(piman): it should be Desktop GL marshalling from double to float. Today
23// on native GLES, we do float->double->float.
24void GL_BINDING_CALL MarshalClearDepthToClearDepthf(GLclampd depth) {
25 glClearDepthf(static_cast<GLclampf>(depth));
26}
27
28void 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]83afcbcc2012-07-27 03:06:2733} // namespace
[email protected]b9363b22010-06-09 22:06:1534
[email protected]0f5e8882011-11-08 22:29:3835void GetAllowedGLImplementations(std::vector<GLImplementation>* impls) {
[email protected]3c4b7ef2011-11-21 22:24:4936 impls->push_back(kGLImplementationDesktopGL);
[email protected]3c4b7ef2011-11-21 22:24:4937 impls->push_back(kGLImplementationEGLGLES2);
[email protected]70518b12011-11-22 01:39:1238 impls->push_back(kGLImplementationOSMesaGL);
[email protected]0f5e8882011-11-08 22:29:3839}
40
[email protected]b9363b22010-06-09 22:06:1541bool 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]30aa5c1a2010-07-14 20:47:0445 if (GetGLImplementation() != kGLImplementationNone)
[email protected]b9363b22010-06-09 22:06:1546 return true;
47
[email protected]6e318e0e2011-10-14 23:08:2048 // 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]b9363b22010-06-09 22:06:1554 switch (implementation) {
[email protected]31e8a4f2013-06-29 01:13:2755 case kGLImplementationOSMesaGL:
56 return InitializeGLBindingsOSMesaGL();
[email protected]30aa5c1a2010-07-14 20:47:0457 case kGLImplementationDesktopGL: {
[email protected]991c5682012-01-30 13:32:3458 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]7f4115e2011-10-26 20:09:4566#if defined(OS_OPENBSD)
[email protected]991c5682012-01-30 13:32:3467 library = LoadLibrary("libGL.so");
[email protected]7f4115e2011-10-26 20:09:4568#else
[email protected]991c5682012-01-30 13:32:3469 library = LoadLibrary("libGL.so.1");
[email protected]7f4115e2011-10-26 20:09:4570#endif
[email protected]991c5682012-01-30 13:32:3471 }
72
[email protected]84479322011-04-18 22:06:2273 if (!library)
[email protected]30aa5c1a2010-07-14 20:47:0474 return false;
[email protected]30aa5c1a2010-07-14 20:47:0475
76 GLGetProcAddressProc get_proc_address =
77 reinterpret_cast<GLGetProcAddressProc>(
78 base::GetFunctionPointerFromNativeLibrary(
79 library, "glXGetProcAddress"));
[email protected]1b2707bb2010-10-06 19:37:1680 if (!get_proc_address) {
81 LOG(ERROR) << "glxGetProcAddress not found.";
82 base::UnloadNativeLibrary(library);
83 return false;
84 }
[email protected]30aa5c1a2010-07-14 20:47:0485
86 SetGLGetProcAddressProc(get_proc_address);
87 AddGLNativeLibrary(library);
88 SetGLImplementation(kGLImplementationDesktopGL);
[email protected]b9363b22010-06-09 22:06:1589
90 InitializeGLBindingsGL();
91 InitializeGLBindingsGLX();
92 break;
[email protected]30aa5c1a2010-07-14 20:47:0493 }
94 case kGLImplementationEGLGLES2: {
[email protected]3733a142012-02-08 22:05:0895 base::NativeLibrary gles_library = LoadLibrary("libGLESv2.so.2");
[email protected]84479322011-04-18 22:06:2296 if (!gles_library)
[email protected]1b2707bb2010-10-06 19:37:1697 return false;
[email protected]3733a142012-02-08 22:05:0898 base::NativeLibrary egl_library = LoadLibrary("libEGL.so.1");
[email protected]91ac84f72011-06-22 19:48:0299 if (!egl_library) {
100 base::UnloadNativeLibrary(gles_library);
[email protected]0bec8e22010-06-21 22:20:02101 return false;
[email protected]91ac84f72011-06-22 19:48:02102 }
[email protected]0bec8e22010-06-21 22:20:02103
[email protected]30aa5c1a2010-07-14 20:47:04104 GLGetProcAddressProc get_proc_address =
105 reinterpret_cast<GLGetProcAddressProc>(
106 base::GetFunctionPointerFromNativeLibrary(
107 egl_library, "eglGetProcAddress"));
[email protected]1b2707bb2010-10-06 19:37:16108 if (!get_proc_address) {
109 LOG(ERROR) << "eglGetProcAddress not found.";
[email protected]30aa5c1a2010-07-14 20:47:04110 base::UnloadNativeLibrary(egl_library);
[email protected]1b2707bb2010-10-06 19:37:16111 base::UnloadNativeLibrary(gles_library);
[email protected]0bec8e22010-06-21 22:20:02112 return false;
113 }
114
[email protected]30aa5c1a2010-07-14 20:47:04115 SetGLGetProcAddressProc(get_proc_address);
116 AddGLNativeLibrary(egl_library);
117 AddGLNativeLibrary(gles_library);
118 SetGLImplementation(kGLImplementationEGLGLES2);
[email protected]0bec8e22010-06-21 22:20:02119
120 InitializeGLBindingsGL();
121 InitializeGLBindingsEGL();
122
123 // These two functions take single precision float rather than double
124 // precision float parameters in GLES.
[email protected]b97c9082012-10-25 17:21:38125 ::gfx::g_driver_gl.fn.glClearDepthFn = MarshalClearDepthToClearDepthf;
126 ::gfx::g_driver_gl.fn.glDepthRangeFn = MarshalDepthRangeToDepthRangef;
[email protected]0bec8e22010-06-21 22:20:02127 break;
[email protected]30aa5c1a2010-07-14 20:47:04128 }
129 case kGLImplementationMockGL: {
130 SetGLGetProcAddressProc(GetMockGLProcAddress);
131 SetGLImplementation(kGLImplementationMockGL);
[email protected]b9363b22010-06-09 22:06:15132 InitializeGLBindingsGL();
133 break;
[email protected]30aa5c1a2010-07-14 20:47:04134 }
[email protected]b9363b22010-06-09 22:06:15135 default:
136 return false;
137 }
138
139
140 return true;
141}
142
[email protected]6494823b2011-10-27 18:30:44143bool 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]218a5a22010-11-04 19:27:49168void InitializeDebugGLBindings() {
169 InitializeDebugGLBindingsEGL();
170 InitializeDebugGLBindingsGL();
171 InitializeDebugGLBindingsGLX();
172 InitializeDebugGLBindingsOSMESA();
173}
174
[email protected]0f5e8882011-11-08 22:29:38175void ClearGLBindings() {
176 ClearGLBindingsEGL();
177 ClearGLBindingsGL();
[email protected]0f5e8882011-11-08 22:29:38178 ClearGLBindingsGLX();
179 ClearGLBindingsOSMESA();
[email protected]0f5e8882011-11-08 22:29:38180 SetGLImplementation(kGLImplementationNone);
181
182 UnloadGLNativeLibraries();
183}
184
[email protected]45895032013-05-30 17:06:43185bool 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]b9363b22010-06-09 22:06:15197} // namespace gfx