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.
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"
22 void GL_BINDING_CALL MarshalClearDepthToClearDepthf(GLclampd depth) {
23 glClearDepthf(static_cast<GLclampf>(depth));
26 void GL_BINDING_CALL MarshalDepthRangeToDepthRangef(GLclampd z_near,
28 glDepthRangef(static_cast<GLclampf>(z_near), static_cast<GLclampf>(z_far));
31 base::NativeLibrary LoadLibrary(const base::FilePath& filename) {
33 base::NativeLibrary library = base::LoadNativeLibrary(filename, &error);
35 DVLOG(1) << "Failed to load " << filename.MaybeAsASCII() << ": " << error;
41 base::NativeLibrary LoadLibrary(const char* filename) {
42 return LoadLibrary(base::FilePath(filename));
47 void GetAllowedGLImplementations(std::vector<GLImplementation>* impls) {
48 impls->push_back(kGLImplementationEGLGLES2);
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());
57 switch (implementation) {
58 case kGLImplementationEGLGLES2: {
59 base::NativeLibrary gles_library = LoadLibrary("libGLESv2.so");
61 LOG(ERROR) << "Failed to load libGLESv2.so.";
64 base::NativeLibrary egl_library = LoadLibrary("libEGL.so");
66 LOG(ERROR) << "Failed to load libEGL.so.";
67 base::UnloadNativeLibrary(gles_library);
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);
82 SetGLGetProcAddressProc(get_proc_address);
83 AddGLNativeLibrary(egl_library);
84 AddGLNativeLibrary(gles_library);
85 SetGLImplementation(kGLImplementationEGLGLES2);
87 InitializeStaticGLBindingsGL();
88 InitializeStaticGLBindingsEGL();
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;
96 case kGLImplementationMockGL: {
97 SetGLImplementation(kGLImplementationMockGL);
98 InitializeStaticGLBindingsGL();
102 NOTIMPLEMENTED() << "InitializeStaticGLBindings on Android";
109 bool InitializeDynamicGLBindings(GLImplementation implementation,
110 GLContext* context) {
111 switch (implementation) {
112 case kGLImplementationEGLGLES2:
113 InitializeDynamicGLBindingsGL(context);
114 InitializeDynamicGLBindingsEGL(context);
116 case kGLImplementationMockGL:
118 scoped_refptr<GLContextStubWithExtensions> mock_context(
119 new GLContextStubWithExtensions());
120 mock_context->SetGLVersionString("opengl es 3.0");
121 InitializeDynamicGLBindingsGL(mock_context.get());
123 InitializeDynamicGLBindingsGL(context);
126 NOTREACHED() << "InitializeDynamicGLBindings on Android";
133 void InitializeDebugGLBindings() {
134 InitializeDebugGLBindingsEGL();
135 InitializeDebugGLBindingsGL();
138 void ClearGLBindings() {
139 ClearGLBindingsEGL();
141 SetGLImplementation(kGLImplementationNone);
143 UnloadGLNativeLibraries();
146 bool GetGLWindowSystemBindingInfo(GLWindowSystemBindingInfo* info) {
147 switch (GetGLImplementation()) {
148 case kGLImplementationEGLGLES2:
149 return GetGLWindowSystemBindingInfoEGL(info);