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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
// Copyright (c) 2010 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.
// This file implements the ViewGLContext and PbufferGLContext classes.
#include <dlfcn.h>
#include <GL/glew.h>
#include <GL/glxew.h>
#include <GL/glx.h>
#include <GL/osmew.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include "app/x11_util.h"
#include "base/logging.h"
#include "base/scoped_ptr.h"
#include "app/gfx/gl/gl_context.h"
#include "app/gfx/gl/gl_context_osmesa.h"
namespace gfx {
typedef GLXContext GLContextHandle;
typedef GLXPbuffer PbufferHandle;
// This class is a wrapper around a GL context that renders directly to a
// window.
class ViewGLContext : public GLContext {
public:
explicit ViewGLContext(gfx::PluginWindowHandle window)
: window_(window),
context_(NULL) {
DCHECK(window);
}
// Initializes the GL context.
bool Initialize(bool multisampled);
virtual void Destroy();
virtual bool MakeCurrent();
virtual bool IsCurrent();
virtual bool IsOffscreen();
virtual void SwapBuffers();
virtual gfx::Size GetSize();
virtual void* GetHandle();
private:
gfx::PluginWindowHandle window_;
GLContextHandle context_;
DISALLOW_COPY_AND_ASSIGN(ViewGLContext);
};
// This class is a wrapper around a GL context used for offscreen rendering.
// It is initially backed by a 1x1 pbuffer. Use it to create an FBO to do useful
// rendering.
class PbufferGLContext : public GLContext {
public:
explicit PbufferGLContext()
: context_(NULL),
pbuffer_(0) {
}
// Initializes the GL context.
bool Initialize(void* shared_handle);
virtual void Destroy();
virtual bool MakeCurrent();
virtual bool IsCurrent();
virtual bool IsOffscreen();
virtual void SwapBuffers();
virtual gfx::Size GetSize();
virtual void* GetHandle();
private:
GLContextHandle context_;
PbufferHandle pbuffer_;
DISALLOW_COPY_AND_ASSIGN(PbufferGLContext);
};
// 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);
}
};
// Some versions of NVIDIA's GL libGL.so include a broken version of
// dlopen/dlsym, and so linking it into chrome breaks it. So we dynamically
// load it, and use glew to dynamically resolve symbols.
// See http://code.google.com/p/chromium/issues/detail?id=16800
static bool InitializeOneOff() {
static bool initialized = false;
if (initialized)
return true;
osmewInit();
if (!OSMesaCreateContext) {
void* handle = dlopen("libGL.so.1", RTLD_LAZY | RTLD_GLOBAL);
if (!handle) {
LOG(ERROR) << "Could not find libGL.so.1";
return false;
}
// Initializes context-independent parts of GLEW
if (glxewInit() != GLEW_OK) {
LOG(ERROR) << "glxewInit failed";
return false;
}
// glxewContextInit really only needs a display connection to
// complete, and we don't want to have to create an OpenGL context
// just to get access to GLX 1.3 entry points to create pbuffers.
// We therefore added a glxewContextInitWithDisplay entry point.
Display* display = x11_util::GetXDisplay();
if (glxewContextInitWithDisplay(display) != GLEW_OK) {
LOG(ERROR) << "glxewContextInit failed";
return false;
}
}
initialized = true;
return true;
}
bool ViewGLContext::Initialize(bool multisampled) {
if (multisampled) {
DLOG(WARNING) << "Multisampling not implemented.";
}
Display* display = x11_util::GetXDisplay();
XWindowAttributes attributes;
XGetWindowAttributes(display, window_, &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());
DCHECK_GT(visual_info_count, 0);
context_ = NULL;
for (int i = 0; i < visual_info_count; ++i) {
context_ = glXCreateContext(display, visual_info_list.get() + i, 0, True);
if (context_)
break;
}
if (!context_) {
DLOG(ERROR) << "Couldn't create GL context.";
return false;
}
if (!MakeCurrent()) {
Destroy();
DLOG(ERROR) << "Couldn't make context current for initialization.";
return false;
}
if (!InitializeGLEW()) {
Destroy();
return false;
}
if (!InitializeCommon()) {
Destroy();
return false;
}
return true;
}
void ViewGLContext::Destroy() {
Display* display = x11_util::GetXDisplay();
Bool result = glXMakeCurrent(display, 0, 0);
// glXMakeCurrent isn't supposed to fail when unsetting the context, unless
// we have pending draws on an invalid window - which shouldn't be the case
// here.
DCHECK(result);
if (context_) {
glXDestroyContext(display, context_);
context_ = NULL;
}
}
bool ViewGLContext::MakeCurrent() {
if (IsCurrent()) {
return true;
}
Display* display = x11_util::GetXDisplay();
if (glXMakeCurrent(display, window_, context_) != True) {
glXDestroyContext(display, context_);
context_ = 0;
DLOG(ERROR) << "Couldn't make context current.";
return false;
}
return true;
}
bool ViewGLContext::IsCurrent() {
return glXGetCurrentDrawable() == window_ &&
glXGetCurrentContext() == context_;
}
bool ViewGLContext::IsOffscreen() {
return false;
}
void ViewGLContext::SwapBuffers() {
Display* display = x11_util::GetXDisplay();
glXSwapBuffers(display, window_);
}
gfx::Size ViewGLContext::GetSize() {
XWindowAttributes attributes;
Display* display = x11_util::GetXDisplay();
XGetWindowAttributes(display, window_, &attributes);
return gfx::Size(attributes.width, attributes.height);
}
void* ViewGLContext::GetHandle() {
return context_;
}
GLContext* GLContext::CreateViewGLContext(gfx::PluginWindowHandle window,
bool multisampled) {
if (!InitializeOneOff())
return NULL;
if (OSMesaCreateContext) {
// TODO(apatrick): Support OSMesa rendering to a window on Linux.
NOTREACHED() << "OSMesa rendering to a window is not yet implemented.";
return NULL;
} else {
scoped_ptr<ViewGLContext> context(new ViewGLContext(window));
if (!context->Initialize(multisampled))
return NULL;
return context.release();
}
}
bool PbufferGLContext::Initialize(void* shared_handle) {
if (!glXChooseFBConfig ||
!glXCreateNewContext ||
!glXCreatePbuffer ||
!glXDestroyPbuffer) {
DLOG(ERROR) << "Pbuffer support not available.";
return false;
}
static const int config_attributes[] = {
GLX_DRAWABLE_TYPE,
GLX_PBUFFER_BIT,
GLX_RENDER_TYPE,
GLX_RGBA_BIT,
GLX_DOUBLEBUFFER,
0,
0
};
Display* display = x11_util::GetXDisplay();
int nelements = 0;
// TODO(kbr): figure out whether hardcoding screen to 0 is sufficient.
scoped_ptr_malloc<GLXFBConfig, ScopedPtrXFree> config(
glXChooseFBConfig(display, 0, config_attributes, &nelements));
if (!config.get()) {
DLOG(ERROR) << "glXChooseFBConfig failed.";
return false;
}
if (!nelements) {
DLOG(ERROR) << "glXChooseFBConfig returned 0 elements.";
return false;
}
context_ = glXCreateNewContext(display,
config.get()[0],
GLX_RGBA_TYPE,
static_cast<GLContextHandle>(shared_handle),
True);
if (!context_) {
DLOG(ERROR) << "glXCreateNewContext failed.";
return false;
}
static const int pbuffer_attributes[] = {
GLX_PBUFFER_WIDTH,
1,
GLX_PBUFFER_HEIGHT,
1,
0
};
pbuffer_ = glXCreatePbuffer(display,
config.get()[0], pbuffer_attributes);
if (!pbuffer_) {
Destroy();
DLOG(ERROR) << "glXCreatePbuffer failed.";
return false;
}
if (!MakeCurrent()) {
Destroy();
DLOG(ERROR) << "Couldn't make context current for initialization.";
return false;
}
if (!InitializeGLEW()) {
Destroy();
return false;
}
if (!InitializeCommon()) {
Destroy();
return false;
}
return true;
}
void PbufferGLContext::Destroy() {
Display* display = x11_util::GetXDisplay();
Bool result = glXMakeCurrent(display, 0, 0);
// glXMakeCurrent isn't supposed to fail when unsetting the context, unless
// we have pending draws on an invalid window - which shouldn't be the case
// here.
DCHECK(result);
if (context_) {
glXDestroyContext(display, context_);
context_ = NULL;
}
if (pbuffer_) {
glXDestroyPbuffer(display, pbuffer_);
pbuffer_ = 0;
}
}
bool PbufferGLContext::MakeCurrent() {
if (IsCurrent()) {
return true;
}
Display* display = x11_util::GetXDisplay();
if (glXMakeCurrent(display, pbuffer_, context_) != True) {
glXDestroyContext(display, context_);
context_ = NULL;
DLOG(ERROR) << "Couldn't make context current.";
return false;
}
return true;
}
bool PbufferGLContext::IsCurrent() {
return glXGetCurrentDrawable() == pbuffer_ &&
glXGetCurrentContext() == context_;
}
bool PbufferGLContext::IsOffscreen() {
return true;
}
void PbufferGLContext::SwapBuffers() {
NOTREACHED() << "Attempted to call SwapBuffers on a pbuffer.";
}
gfx::Size PbufferGLContext::GetSize() {
NOTREACHED() << "Should not be requesting size of this pbuffer.";
return gfx::Size(1, 1);
}
void* PbufferGLContext::GetHandle() {
return context_;
}
GLContext* GLContext::CreateOffscreenGLContext(void* shared_handle) {
if (!InitializeOneOff())
return NULL;
if (OSMesaCreateContext) {
scoped_ptr<OSMesaGLContext> context(new OSMesaGLContext);
if (!context->Initialize(shared_handle))
return NULL;
return context.release();
} else {
scoped_ptr<PbufferGLContext> context(new PbufferGLContext);
if (!context->Initialize(shared_handle))
return NULL;
return context.release();
}
}
} // namespace gfx
|