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.
9 #include "ui/gl/gl_context_glx.h"
11 #include "base/debug/trace_event.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "third_party/mesa/src/include/GL/osmesa.h"
15 #include "ui/gl/GL/glextchromium.h"
16 #include "ui/gl/gl_bindings.h"
17 #include "ui/gl/gl_implementation.h"
18 #include "ui/gl/gl_surface_glx.h"
22 GLContextGLX::GLContextGLX(GLShareGroup* share_group)
23 : GLContextReal(share_group),
28 XDisplay* GLContextGLX::display() {
32 bool GLContextGLX::Initialize(
33 GLSurface* compatible_surface, GpuPreference gpu_preference) {
34 display_ = static_cast<XDisplay*>(compatible_surface->GetDisplay());
36 GLXContext share_handle = static_cast<GLXContext>(
37 share_group() ? share_group()->GetHandle() : NULL);
39 if (GLSurfaceGLX::IsCreateContextSupported()) {
40 DVLOG(1) << "GLX_ARB_create_context supported.";
41 std::vector<int> attribs;
42 if (GLSurfaceGLX::IsCreateContextRobustnessSupported()) {
43 DVLOG(1) << "GLX_ARB_create_context_robustness supported.";
44 attribs.push_back(GLX_CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB);
45 attribs.push_back(GLX_LOSE_CONTEXT_ON_RESET_ARB);
48 context_ = glXCreateContextAttribsARB(
50 static_cast<GLXFBConfig>(compatible_surface->GetConfig()),
55 LOG(ERROR) << "Failed to create GL context with "
56 << "glXCreateContextAttribsARB.";
60 DVLOG(1) << "GLX_ARB_create_context not supported.";
61 context_ = glXCreateNewContext(
63 static_cast<GLXFBConfig>(compatible_surface->GetConfig()),
68 LOG(ERROR) << "Failed to create GL context with glXCreateNewContext.";
73 DVLOG(1) << " Successfully allocated "
74 << (compatible_surface->IsOffscreen() ?
75 "offscreen" : "onscreen")
76 << " GL context with LOSE_CONTEXT_ON_RESET_ARB";
78 DVLOG(1) << (compatible_surface->IsOffscreen() ? "Offscreen" : "Onscreen")
80 << (glXIsDirect(display_,
81 static_cast<GLXContext>(context_))
82 ? "direct" : "indirect")
88 void GLContextGLX::Destroy() {
90 glXDestroyContext(display_,
91 static_cast<GLXContext>(context_));
96 bool GLContextGLX::MakeCurrent(GLSurface* surface) {
98 if (IsCurrent(surface))
101 ScopedReleaseCurrent release_current;
102 TRACE_EVENT0("gpu", "GLContextGLX::MakeCurrent");
103 if (!glXMakeContextCurrent(
105 reinterpret_cast<GLXDrawable>(surface->GetHandle()),
106 reinterpret_cast<GLXDrawable>(surface->GetHandle()),
107 static_cast<GLXContext>(context_))) {
108 LOG(ERROR) << "Couldn't make context current with X drawable.";
113 // Set this as soon as the context is current, since we might call into GL.
117 if (!InitializeDynamicBindings()) {
122 if (!surface->OnMakeCurrent(this)) {
123 LOG(ERROR) << "Could not make current.";
128 release_current.Cancel();
132 void GLContextGLX::ReleaseCurrent(GLSurface* surface) {
133 if (!IsCurrent(surface))
137 if (!glXMakeContextCurrent(display_, 0, 0, 0))
138 LOG(ERROR) << "glXMakeCurrent failed in ReleaseCurrent";
141 bool GLContextGLX::IsCurrent(GLSurface* surface) {
142 bool native_context_is_current =
143 glXGetCurrentContext() == static_cast<GLXContext>(context_);
145 // If our context is current then our notion of which GLContext is
146 // current must be correct. On the other hand, third-party code
147 // using OpenGL might change the current context.
148 DCHECK(!native_context_is_current || (GetRealCurrent() == this));
150 if (!native_context_is_current)
154 if (glXGetCurrentDrawable() !=
155 reinterpret_cast<GLXDrawable>(surface->GetHandle())) {
163 void* GLContextGLX::GetHandle() {
167 void GLContextGLX::SetSwapInterval(int interval) {
168 DCHECK(IsCurrent(NULL));
169 if (HasExtension("GLX_EXT_swap_control") &&
170 g_driver_glx.fn.glXSwapIntervalEXTFn) {
173 glXGetCurrentDrawable(),
175 } else if (HasExtension("GLX_MESA_swap_control") &&
176 g_driver_glx.fn.glXSwapIntervalMESAFn) {
177 glXSwapIntervalMESA(interval);
181 "Could not disable vsync: driver does not "
182 "support GLX_EXT_swap_control";
186 std::string GLContextGLX::GetExtensions() {
187 DCHECK(IsCurrent(NULL));
188 const char* extensions = GLSurfaceGLX::GetGLXExtensions();
190 return GLContext::GetExtensions() + " " + extensions;
193 return GLContext::GetExtensions();
196 bool GLContextGLX::GetTotalGpuMemory(size_t* bytes) {
199 if (HasExtension("GL_NVX_gpu_memory_info")) {
201 glGetIntegerv(GL_GPU_MEMORY_INFO_DEDICATED_VIDMEM_NVX, &kbytes);
202 *bytes = 1024*kbytes;
208 bool GLContextGLX::WasAllocatedUsingRobustnessExtension() {
209 return GLSurfaceGLX::IsCreateContextRobustnessSupported();
212 GLContextGLX::~GLContextGLX() {