Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ui / gl / gl_implementation_x11.cc
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/command_line.h"
8 #include "base/logging.h"
9 #include "base/threading/thread_restrictions.h"
10 #include "ui/gl/gl_bindings.h"
11 #include "ui/gl/gl_context_stub_with_extensions.h"
12 #include "ui/gl/gl_egl_api_implementation.h"
13 #include "ui/gl/gl_gl_api_implementation.h"
14 #include "ui/gl/gl_glx_api_implementation.h"
15 #include "ui/gl/gl_implementation.h"
16 #include "ui/gl/gl_implementation_linux.h"
17 #include "ui/gl/gl_osmesa_api_implementation.h"
18 #include "ui/gl/gl_switches.h"
19
20 namespace gfx {
21 namespace {
22
23 // TODO(piman): it should be Desktop GL marshalling from double to float. Today
24 // on native GLES, we do float->double->float.
25 void GL_BINDING_CALL MarshalClearDepthToClearDepthf(GLclampd depth) {
26   glClearDepthf(static_cast<GLclampf>(depth));
27 }
28
29 void GL_BINDING_CALL MarshalDepthRangeToDepthRangef(GLclampd z_near,
30                                                     GLclampd z_far) {
31   glDepthRangef(static_cast<GLclampf>(z_near), static_cast<GLclampf>(z_far));
32 }
33
34 }  // namespace
35
36 void GetAllowedGLImplementations(std::vector<GLImplementation>* impls) {
37   impls->push_back(kGLImplementationDesktopGL);
38   impls->push_back(kGLImplementationEGLGLES2);
39   impls->push_back(kGLImplementationOSMesaGL);
40 }
41
42 bool InitializeStaticGLBindings(GLImplementation implementation) {
43   // Prevent reinitialization with a different implementation. Once the gpu
44   // unit tests have initialized with kGLImplementationMock, we don't want to
45   // later switch to another GL implementation.
46   DCHECK_EQ(kGLImplementationNone, GetGLImplementation());
47
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
54   switch (implementation) {
55     case kGLImplementationOSMesaGL:
56       return InitializeStaticGLBindingsOSMesaGL();
57     case kGLImplementationDesktopGL: {
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) {
66 #if defined(OS_OPENBSD)
67         library = LoadLibrary("libGL.so");
68 #else
69         library = LoadLibrary("libGL.so.1");
70 #endif
71       }
72
73       if (!library)
74         return false;
75
76       GLGetProcAddressProc get_proc_address =
77           reinterpret_cast<GLGetProcAddressProc>(
78               base::GetFunctionPointerFromNativeLibrary(
79                   library, "glXGetProcAddress"));
80       if (!get_proc_address) {
81         LOG(ERROR) << "glxGetProcAddress not found.";
82         base::UnloadNativeLibrary(library);
83         return false;
84       }
85
86       SetGLGetProcAddressProc(get_proc_address);
87       AddGLNativeLibrary(library);
88       SetGLImplementation(kGLImplementationDesktopGL);
89
90       InitializeStaticGLBindingsGL();
91       InitializeStaticGLBindingsGLX();
92       break;
93     }
94     case kGLImplementationEGLGLES2: {
95       base::NativeLibrary gles_library = LoadLibrary("libGLESv2.so.2");
96       if (!gles_library)
97         return false;
98       base::NativeLibrary egl_library = LoadLibrary("libEGL.so.1");
99       if (!egl_library) {
100         base::UnloadNativeLibrary(gles_library);
101         return false;
102       }
103
104       GLGetProcAddressProc get_proc_address =
105           reinterpret_cast<GLGetProcAddressProc>(
106               base::GetFunctionPointerFromNativeLibrary(
107                   egl_library, "eglGetProcAddress"));
108       if (!get_proc_address) {
109         LOG(ERROR) << "eglGetProcAddress not found.";
110         base::UnloadNativeLibrary(egl_library);
111         base::UnloadNativeLibrary(gles_library);
112         return false;
113       }
114
115       SetGLGetProcAddressProc(get_proc_address);
116       AddGLNativeLibrary(egl_library);
117       AddGLNativeLibrary(gles_library);
118       SetGLImplementation(kGLImplementationEGLGLES2);
119
120       InitializeStaticGLBindingsGL();
121       InitializeStaticGLBindingsEGL();
122
123       // These two functions take single precision float rather than double
124       // precision float parameters in GLES.
125       ::gfx::g_driver_gl.fn.glClearDepthFn = MarshalClearDepthToClearDepthf;
126       ::gfx::g_driver_gl.fn.glDepthRangeFn = MarshalDepthRangeToDepthRangef;
127       break;
128     }
129     case kGLImplementationMockGL: {
130       SetGLImplementation(kGLImplementationMockGL);
131       InitializeStaticGLBindingsGL();
132       break;
133     }
134     default:
135       return false;
136   }
137
138
139   return true;
140 }
141
142 bool InitializeDynamicGLBindings(GLImplementation implementation,
143     GLContext* context) {
144   switch (implementation) {
145     case kGLImplementationOSMesaGL:
146       InitializeDynamicGLBindingsGL(context);
147       InitializeDynamicGLBindingsOSMESA(context);
148       break;
149     case kGLImplementationDesktopGL:
150       InitializeDynamicGLBindingsGL(context);
151       InitializeDynamicGLBindingsGLX(context);
152       break;
153     case kGLImplementationEGLGLES2:
154       InitializeDynamicGLBindingsGL(context);
155       InitializeDynamicGLBindingsEGL(context);
156       break;
157     case kGLImplementationMockGL:
158       if (!context) {
159         scoped_refptr<GLContextStubWithExtensions> mock_context(
160             new GLContextStubWithExtensions());
161         mock_context->SetGLVersionString("3.0");
162         InitializeDynamicGLBindingsGL(mock_context.get());
163       } else
164         InitializeDynamicGLBindingsGL(context);
165       break;
166     default:
167       return false;
168   }
169
170   return true;
171 }
172
173 void InitializeDebugGLBindings() {
174   InitializeDebugGLBindingsEGL();
175   InitializeDebugGLBindingsGL();
176   InitializeDebugGLBindingsGLX();
177   InitializeDebugGLBindingsOSMESA();
178 }
179
180 void ClearGLBindings() {
181   ClearGLBindingsEGL();
182   ClearGLBindingsGL();
183   ClearGLBindingsGLX();
184   ClearGLBindingsOSMESA();
185   SetGLImplementation(kGLImplementationNone);
186
187   UnloadGLNativeLibraries();
188 }
189
190 bool GetGLWindowSystemBindingInfo(GLWindowSystemBindingInfo* info) {
191   switch (GetGLImplementation()) {
192     case kGLImplementationDesktopGL:
193       return GetGLWindowSystemBindingInfoGLX(info);
194     case kGLImplementationEGLGLES2:
195       return GetGLWindowSystemBindingInfoEGL(info);
196     default:
197       return false;
198   }
199   return false;
200 }
201
202 }  // namespace gfx