Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ui / gl / gl_implementation_android.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 "base/base_paths.h"
6 #include "base/command_line.h"
7 #include "base/files/file_path.h"
8 #include "base/logging.h"
9 #include "base/native_library.h"
10 #include "base/path_service.h"
11 #include "ui/gl/gl_bindings.h"
12 #include "ui/gl/gl_context_stub_with_extensions.h"
13 #include "ui/gl/gl_egl_api_implementation.h"
14 #include "ui/gl/gl_gl_api_implementation.h"
15 #include "ui/gl/gl_implementation.h"
16 #include "ui/gl/gl_osmesa_api_implementation.h"
17
18 namespace gfx {
19
20 namespace {
21
22 void GL_BINDING_CALL MarshalClearDepthToClearDepthf(GLclampd depth) {
23   glClearDepthf(static_cast<GLclampf>(depth));
24 }
25
26 void GL_BINDING_CALL MarshalDepthRangeToDepthRangef(GLclampd z_near,
27                                                     GLclampd z_far) {
28   glDepthRangef(static_cast<GLclampf>(z_near), static_cast<GLclampf>(z_far));
29 }
30
31 base::NativeLibrary LoadLibrary(const base::FilePath& filename) {
32   std::string error;
33   base::NativeLibrary library = base::LoadNativeLibrary(filename, &error);
34   if (!library) {
35     DVLOG(1) << "Failed to load " << filename.MaybeAsASCII() << ": " << error;
36     return NULL;
37   }
38   return library;
39 }
40
41 base::NativeLibrary LoadLibrary(const char* filename) {
42   return LoadLibrary(base::FilePath(filename));
43 }
44
45 }  // namespace
46
47 void GetAllowedGLImplementations(std::vector<GLImplementation>* impls) {
48   impls->push_back(kGLImplementationEGLGLES2);
49 }
50
51 bool InitializeStaticGLBindings(GLImplementation implementation) {
52   // Prevent reinitialization with a different implementation. Once the gpu
53   // unit tests have initialized with kGLImplementationMock, we don't want to
54   // later switch to another GL implementation.
55   DCHECK_EQ(kGLImplementationNone, GetGLImplementation());
56
57   switch (implementation) {
58     case kGLImplementationEGLGLES2: {
59       base::NativeLibrary gles_library = LoadLibrary("libGLESv2.so");
60       if (!gles_library) {
61         LOG(ERROR) << "Failed to load libGLESv2.so.";
62         return false;
63       }
64       base::NativeLibrary egl_library = LoadLibrary("libEGL.so");
65       if (!egl_library) {
66         LOG(ERROR) << "Failed to load libEGL.so.";
67         base::UnloadNativeLibrary(gles_library);
68         return false;
69       }
70
71       GLGetProcAddressProc get_proc_address =
72           reinterpret_cast<GLGetProcAddressProc>(
73               base::GetFunctionPointerFromNativeLibrary(
74                   egl_library, "eglGetProcAddress"));
75       if (!get_proc_address) {
76         LOG(ERROR) << "eglGetProcAddress not found.";
77         base::UnloadNativeLibrary(egl_library);
78         base::UnloadNativeLibrary(gles_library);
79         return false;
80       }
81
82       SetGLGetProcAddressProc(get_proc_address);
83       AddGLNativeLibrary(egl_library);
84       AddGLNativeLibrary(gles_library);
85       SetGLImplementation(kGLImplementationEGLGLES2);
86
87       InitializeStaticGLBindingsGL();
88       InitializeStaticGLBindingsEGL();
89
90       // These two functions take single precision float rather than double
91       // precision float parameters in GLES.
92       ::gfx::g_driver_gl.fn.glClearDepthFn = MarshalClearDepthToClearDepthf;
93       ::gfx::g_driver_gl.fn.glDepthRangeFn = MarshalDepthRangeToDepthRangef;
94       break;
95     }
96     case kGLImplementationMockGL: {
97       SetGLImplementation(kGLImplementationMockGL);
98       InitializeStaticGLBindingsGL();
99       break;
100     }
101     default:
102       NOTIMPLEMENTED() << "InitializeStaticGLBindings on Android";
103       return false;
104   }
105
106   return true;
107 }
108
109 bool InitializeDynamicGLBindings(GLImplementation implementation,
110                                  GLContext* context) {
111   switch (implementation) {
112     case kGLImplementationEGLGLES2:
113       InitializeDynamicGLBindingsGL(context);
114       InitializeDynamicGLBindingsEGL(context);
115       break;
116     case kGLImplementationMockGL:
117       if (!context) {
118         scoped_refptr<GLContextStubWithExtensions> mock_context(
119             new GLContextStubWithExtensions());
120         mock_context->SetGLVersionString("opengl es 3.0");
121         InitializeDynamicGLBindingsGL(mock_context.get());
122       } else
123         InitializeDynamicGLBindingsGL(context);
124       break;
125     default:
126       NOTREACHED() << "InitializeDynamicGLBindings on Android";
127       return false;
128   }
129
130   return true;
131 }
132
133 void InitializeDebugGLBindings() {
134   InitializeDebugGLBindingsEGL();
135   InitializeDebugGLBindingsGL();
136 }
137
138 void ClearGLBindings() {
139   ClearGLBindingsEGL();
140   ClearGLBindingsGL();
141   SetGLImplementation(kGLImplementationNone);
142
143   UnloadGLNativeLibraries();
144 }
145
146 bool GetGLWindowSystemBindingInfo(GLWindowSystemBindingInfo* info) {
147   switch (GetGLImplementation()) {
148     case kGLImplementationEGLGLES2:
149       return GetGLWindowSystemBindingInfoEGL(info);
150     default:
151       return false;
152   }
153   return false;
154 }
155
156 }  // namespace gfx