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 "ui/gl/gl_implementation.h"
10 #include "base/at_exit.h"
11 #include "base/command_line.h"
12 #include "base/logging.h"
13 #include "base/strings/string_split.h"
14 #include "base/strings/string_util.h"
15 #include "ui/gl/gl_bindings.h"
16 #include "ui/gl/gl_gl_api_implementation.h"
24 GLImplementation implementation
;
25 } kGLImplementationNamePairs
[] = {
26 { kGLImplementationDesktopName
, kGLImplementationDesktopGL
},
27 { kGLImplementationOSMesaName
, kGLImplementationOSMesaGL
},
28 #if defined(OS_MACOSX)
29 { kGLImplementationAppleName
, kGLImplementationAppleGL
},
31 { kGLImplementationEGLName
, kGLImplementationEGLGLES2
},
32 { kGLImplementationMockName
, kGLImplementationMockGL
}
35 typedef std::vector
<base::NativeLibrary
> LibraryArray
;
37 GLImplementation g_gl_implementation
= kGLImplementationNone
;
38 LibraryArray
* g_libraries
;
39 GLGetProcAddressProc g_get_proc_address
;
41 void CleanupNativeLibraries(void* unused
) {
43 // We do not call base::UnloadNativeLibrary() for these libraries as
44 // unloading libGL without closing X display is not allowed. See
45 // crbug.com/250813 for details.
53 base::ThreadLocalPointer
<GLApi
>* g_current_gl_context_tls
= NULL
;
54 OSMESAApi
* g_current_osmesa_context
;
58 EGLApi
* g_current_egl_context
;
59 WGLApi
* g_current_wgl_context
;
61 #elif defined(USE_X11)
63 EGLApi
* g_current_egl_context
;
64 GLXApi
* g_current_glx_context
;
66 #elif defined(USE_OZONE)
68 EGLApi
* g_current_egl_context
;
70 #elif defined(OS_ANDROID)
72 EGLApi
* g_current_egl_context
;
76 GLImplementation
GetNamedGLImplementation(const std::string
& name
) {
77 for (size_t i
= 0; i
< arraysize(kGLImplementationNamePairs
); ++i
) {
78 if (name
== kGLImplementationNamePairs
[i
].name
)
79 return kGLImplementationNamePairs
[i
].implementation
;
82 return kGLImplementationNone
;
85 const char* GetGLImplementationName(GLImplementation implementation
) {
86 for (size_t i
= 0; i
< arraysize(kGLImplementationNamePairs
); ++i
) {
87 if (implementation
== kGLImplementationNamePairs
[i
].implementation
)
88 return kGLImplementationNamePairs
[i
].name
;
94 void SetGLImplementation(GLImplementation implementation
) {
95 g_gl_implementation
= implementation
;
98 GLImplementation
GetGLImplementation() {
99 return g_gl_implementation
;
102 bool HasDesktopGLFeatures() {
103 return kGLImplementationDesktopGL
== g_gl_implementation
||
104 kGLImplementationDesktopGLCoreProfile
== g_gl_implementation
||
105 kGLImplementationOSMesaGL
== g_gl_implementation
||
106 kGLImplementationAppleGL
== g_gl_implementation
;
109 void AddGLNativeLibrary(base::NativeLibrary library
) {
113 g_libraries
= new LibraryArray
;
114 base::AtExitManager::RegisterCallback(CleanupNativeLibraries
, NULL
);
117 g_libraries
->push_back(library
);
120 void UnloadGLNativeLibraries() {
121 CleanupNativeLibraries(NULL
);
124 void SetGLGetProcAddressProc(GLGetProcAddressProc proc
) {
126 g_get_proc_address
= proc
;
129 void* GetGLProcAddress(const char* name
) {
130 DCHECK(g_gl_implementation
!= kGLImplementationNone
);
133 for (size_t i
= 0; i
< g_libraries
->size(); ++i
) {
134 void* proc
= base::GetFunctionPointerFromNativeLibrary((*g_libraries
)[i
],
140 if (g_get_proc_address
) {
141 void* proc
= g_get_proc_address(name
);
149 void InitializeNullDrawGLBindings() {
150 // This is platform independent, so it does not need to live in a platform
151 // specific implementation file.
152 InitializeNullDrawGLBindingsGL();
155 bool HasInitializedNullDrawGLBindings() {
156 return HasInitializedNullDrawGLBindingsGL();
159 std::string
FilterGLExtensionList(
160 const char* extensions
,
161 const std::vector
<std::string
>& disabled_extensions
) {
162 if (extensions
== NULL
)
165 std::vector
<std::string
> extension_vec
;
166 base::SplitString(extensions
, ' ', &extension_vec
);
168 auto is_disabled
= [&disabled_extensions
](const std::string
& ext
) {
169 return std::find(disabled_extensions
.begin(), disabled_extensions
.end(),
170 ext
) != disabled_extensions
.end();
173 std::remove_if(extension_vec
.begin(), extension_vec
.end(), is_disabled
),
174 extension_vec
.end());
176 return base::JoinString(extension_vec
, " ");
179 DisableNullDrawGLBindings::DisableNullDrawGLBindings() {
180 initial_enabled_
= SetNullDrawGLBindingsEnabledGL(false);
183 DisableNullDrawGLBindings::~DisableNullDrawGLBindings() {
184 SetNullDrawGLBindingsEnabledGL(initial_enabled_
);
187 GLWindowSystemBindingInfo::GLWindowSystemBindingInfo()
188 : direct_rendering(true) {}