1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
extern "C" {
#include <X11/Xlib.h>
}
#include "ui/gfx/gl/gl_context_glx.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "third_party/mesa/MesaLib/include/GL/osmesa.h"
#include "ui/gfx/gl/gl_bindings.h"
#include "ui/gfx/gl/gl_implementation.h"
#include "ui/gfx/gl/gl_surface_glx.h"
namespace gfx {
namespace {
// scoped_ptr functor for XFree(). Use as follows:
// scoped_ptr_malloc<XVisualInfo, ScopedPtrXFree> foo(...);
// where "XVisualInfo" is any X type that is freed with XFree.
class ScopedPtrXFree {
public:
void operator()(void* x) const {
::XFree(x);
}
};
bool IsCompositingWindowManagerActive(Display* display) {
// The X macro "None" has been undefined by gl_bindings.h.
const int kNone = 0;
static Atom net_wm_cm_s0 = kNone;
if (net_wm_cm_s0 == kNone) {
net_wm_cm_s0 = XInternAtom(display, "_NET_WM_CM_S0", True);
}
if (net_wm_cm_s0 == kNone) {
return false;
}
return XGetSelectionOwner(display, net_wm_cm_s0) != kNone;
}
} // namespace anonymous
GLContextGLX::GLContextGLX(GLShareGroup* share_group)
: GLContext(share_group),
context_(NULL) {
}
GLContextGLX::~GLContextGLX() {
Destroy();
}
bool GLContextGLX::Initialize(GLSurface* compatible_surface) {
GLSurfaceGLX* surface_glx = static_cast<GLSurfaceGLX*>(compatible_surface);
GLXFBConfig config = static_cast<GLXFBConfig>(surface_glx->GetConfig());
GLXContext share_handle = static_cast<GLXContext>(
share_group() ? share_group()->GetHandle() : NULL);
// The means by which the context is created depends on whether the drawable
// type works reliably with GLX 1.3. If it does not then fall back to GLX 1.2.
if (config) {
context_ = glXCreateNewContext(
GLSurfaceGLX::GetDisplay(),
static_cast<GLXFBConfig>(surface_glx->GetConfig()),
GLX_RGBA_TYPE,
share_handle,
True);
} else {
Display* display = GLSurfaceGLX::GetDisplay();
// Get the visuals for the X drawable.
XWindowAttributes attributes;
XGetWindowAttributes(
display,
reinterpret_cast<GLXDrawable>(surface_glx->GetHandle()),
&attributes);
XVisualInfo visual_info_template;
visual_info_template.visualid = XVisualIDFromVisual(attributes.visual);
int visual_info_count = 0;
scoped_ptr_malloc<XVisualInfo, ScopedPtrXFree> visual_info_list(
XGetVisualInfo(display, VisualIDMask,
&visual_info_template,
&visual_info_count));
DCHECK(visual_info_list.get());
if (visual_info_count == 0) {
LOG(ERROR) << "No visual info for visual ID.";
return false;
}
// Attempt to create a context with each visual in turn until one works.
context_ = glXCreateContext(
display,
visual_info_list.get(),
share_handle,
True);
}
if (!context_) {
LOG(ERROR) << "Couldn't create GL context.";
Destroy();
return false;
}
return true;
}
void GLContextGLX::Destroy() {
if (context_) {
glXDestroyContext(GLSurfaceGLX::GetDisplay(),
static_cast<GLXContext>(context_));
context_ = NULL;
}
}
bool GLContextGLX::MakeCurrent(GLSurface* surface) {
DCHECK(context_);
if (IsCurrent(surface))
return true;
GLSurfaceGLX* surface_glx = static_cast<GLSurfaceGLX*>(surface);
if (!glXMakeCurrent(
GLSurfaceGLX::GetDisplay(),
reinterpret_cast<GLXDrawable>(surface->GetHandle()),
static_cast<GLXContext>(context_))) {
LOG(ERROR) << "Couldn't make context current with X drawable.";
Destroy();
return false;
}
return true;
}
void GLContextGLX::ReleaseCurrent(GLSurface* surface) {
if (!IsCurrent(surface))
return;
glXMakeContextCurrent(GLSurfaceGLX::GetDisplay(), 0, 0, NULL);
}
bool GLContextGLX::IsCurrent(GLSurface* surface) {
if (glXGetCurrentContext() != static_cast<GLXContext>(context_))
return false;
if (surface) {
if (glXGetCurrentDrawable() !=
reinterpret_cast<GLXDrawable>(surface->GetHandle())) {
return false;
}
}
return true;
}
void* GLContextGLX::GetHandle() {
return context_;
}
void GLContextGLX::SetSwapInterval(int interval) {
DCHECK(IsCurrent(NULL));
if (HasExtension("GLX_EXT_swap_control") && glXSwapIntervalEXT) {
// Only enable vsync if we aren't using a compositing window
// manager. At the moment, compositing window managers don't
// respect this setting anyway (tearing still occurs) and it
// dramatically increases latency.
if (interval == 1 &&
IsCompositingWindowManagerActive(GLSurfaceGLX::GetDisplay())) {
LOG(INFO) <<
"Forcing vsync off because compositing window manager was detected.";
interval = 0;
}
glXSwapIntervalEXT(
GLSurfaceGLX::GetDisplay(),
glXGetCurrentDrawable(),
interval);
} else {
if(interval == 0)
LOG(WARNING) <<
"Could not disable vsync: driver does not "
"support GLX_EXT_swap_control";
}
}
std::string GLContextGLX::GetExtensions() {
DCHECK(IsCurrent(NULL));
const char* extensions = glXQueryExtensionsString(
GLSurfaceGLX::GetDisplay(),
0);
if (extensions) {
return GLContext::GetExtensions() + " " + extensions;
}
return GLContext::GetExtensions();
}
} // namespace gfx
|