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_implementation_osmesa.h"
17 #include "ui/gl/gl_osmesa_api_implementation.h"
23 void GL_BINDING_CALL
MarshalClearDepthToClearDepthf(GLclampd depth
) {
24 glClearDepthf(static_cast<GLclampf
>(depth
));
27 void GL_BINDING_CALL
MarshalDepthRangeToDepthRangef(GLclampd z_near
,
29 glDepthRangef(static_cast<GLclampf
>(z_near
), static_cast<GLclampf
>(z_far
));
34 void GetAllowedGLImplementations(std::vector
<GLImplementation
>* impls
) {
35 impls
->push_back(kGLImplementationEGLGLES2
);
36 impls
->push_back(kGLImplementationOSMesaGL
);
39 bool InitializeStaticGLBindings(GLImplementation implementation
) {
40 // Prevent reinitialization with a different implementation. Once the gpu
41 // unit tests have initialized with kGLImplementationMock, we don't want to
42 // later switch to another GL implementation.
43 DCHECK_EQ(kGLImplementationNone
, GetGLImplementation());
45 switch (implementation
) {
46 case kGLImplementationEGLGLES2
: {
47 base::NativeLibrary gles_library
=
48 LoadLibraryAndPrintError("libGLESv2.so");
51 base::NativeLibrary egl_library
= LoadLibraryAndPrintError("libEGL.so");
53 base::UnloadNativeLibrary(gles_library
);
57 GLGetProcAddressProc get_proc_address
=
58 reinterpret_cast<GLGetProcAddressProc
>(
59 base::GetFunctionPointerFromNativeLibrary(
60 egl_library
, "eglGetProcAddress"));
61 if (!get_proc_address
) {
62 LOG(ERROR
) << "eglGetProcAddress not found.";
63 base::UnloadNativeLibrary(egl_library
);
64 base::UnloadNativeLibrary(gles_library
);
68 SetGLGetProcAddressProc(get_proc_address
);
69 AddGLNativeLibrary(egl_library
);
70 AddGLNativeLibrary(gles_library
);
71 SetGLImplementation(kGLImplementationEGLGLES2
);
73 InitializeStaticGLBindingsGL();
74 InitializeStaticGLBindingsEGL();
76 // These two functions take single precision float rather than double
77 // precision float parameters in GLES.
78 ::gfx::g_driver_gl
.fn
.glClearDepthFn
= MarshalClearDepthToClearDepthf
;
79 ::gfx::g_driver_gl
.fn
.glDepthRangeFn
= MarshalDepthRangeToDepthRangef
;
82 case kGLImplementationOSMesaGL
:
83 InitializeStaticGLBindingsOSMesaGL();
85 case kGLImplementationMockGL
: {
86 SetGLImplementation(kGLImplementationMockGL
);
87 InitializeStaticGLBindingsGL();
91 NOTIMPLEMENTED() << "InitializeStaticGLBindings on Android";
98 bool InitializeDynamicGLBindings(GLImplementation implementation
,
100 switch (implementation
) {
101 case kGLImplementationEGLGLES2
:
102 InitializeDynamicGLBindingsGL(context
);
103 InitializeDynamicGLBindingsEGL(context
);
105 case kGLImplementationOSMesaGL
:
106 InitializeDynamicGLBindingsGL(context
);
107 InitializeDynamicGLBindingsOSMESA(context
);
109 case kGLImplementationMockGL
:
111 scoped_refptr
<GLContextStubWithExtensions
> mock_context(
112 new GLContextStubWithExtensions());
113 mock_context
->SetGLVersionString("opengl es 3.0");
114 InitializeDynamicGLBindingsGL(mock_context
.get());
116 InitializeDynamicGLBindingsGL(context
);
119 NOTREACHED() << "InitializeDynamicGLBindings on Android";
126 void InitializeDebugGLBindings() {
127 InitializeDebugGLBindingsEGL();
128 InitializeDebugGLBindingsGL();
129 InitializeDebugGLBindingsOSMESA();
132 void ClearGLBindings() {
133 ClearGLBindingsEGL();
135 ClearGLBindingsOSMESA();
136 SetGLImplementation(kGLImplementationNone
);
138 UnloadGLNativeLibraries();
141 bool GetGLWindowSystemBindingInfo(GLWindowSystemBindingInfo
* info
) {
142 switch (GetGLImplementation()) {
143 case kGLImplementationEGLGLES2
:
144 return GetGLWindowSystemBindingInfoEGL(info
);