summaryrefslogtreecommitdiffstats
path: root/ui/gfx/gl/gl_surface_glx.cc
blob: ed366422616453b549d1cb121ece107ae01148d3 (plain)
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
// 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 <X11/Xatom.h>
}

#include <map>

#include "ui/gfx/gl/gl_surface_glx.h"

#include "base/basictypes.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "third_party/mesa/MesaLib/include/GL/osmesa.h"
#include "ui/base/x/x11_util.h"
#include "ui/gfx/gl/gl_bindings.h"
#include "ui/gfx/gl/gl_implementation.h"

namespace {

static Display* g_display;
typedef std::map<gfx::PluginWindowHandle, XID> XIDMapping;
static XIDMapping glx_windows_destroyed_;
static const char kGLX_WINDOWPropertyName[] = "GLX_WINDOW";

}  // namespace

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);
  }

};

XID GetGLX_WINDOWProperty(XID window) {
  Atom a = XInternAtom(g_display, kGLX_WINDOWPropertyName, False);
  Atom actual_type;
  int actual_format;
  unsigned long nitems;
  unsigned long bytes_after;
  unsigned char* prop;

  if (XGetWindowProperty(g_display, window, a, 0, 1, False, XA_WINDOW,
                         &actual_type, &actual_format, &nitems,
                         &bytes_after, &prop) == Success && actual_type) {
    scoped_ptr_malloc<unsigned char, ScopedPtrXFree> prop_scoped(prop);
    return *reinterpret_cast<XID*>(prop);
  } else {
    return 0;
  }
}

void SetGLX_WINDOWProperty(XID window, XID glx_window) {
  Atom a = XInternAtom(g_display, kGLX_WINDOWPropertyName, False);
  XChangeProperty(g_display, window, a, XA_WINDOW, 32, PropModeReplace,
                  reinterpret_cast<unsigned char*>(&glx_window), 1);
}

void CollectGarbage() {
  for (XIDMapping::iterator iter = glx_windows_destroyed_.begin();
       iter != glx_windows_destroyed_.end(); ) {
    XID glx_window = GetGLX_WINDOWProperty(iter->second);
    if (glx_window != iter->first) {
      glXDestroyWindow(g_display, iter->first);
      glx_windows_destroyed_.erase(iter++);
    } else {
      iter++;
    }
  }
}

}  // namespace anonymous

GLSurfaceGLX::GLSurfaceGLX() {
}

GLSurfaceGLX::~GLSurfaceGLX() {
}

bool GLSurfaceGLX::InitializeOneOff() {
  static bool initialized = false;
  if (initialized)
    return true;

  g_display = XOpenDisplay(NULL);
  if (!g_display) {
    LOG(ERROR) << "XOpenDisplay failed.";
    return false;
  }

  int major, minor;
  if (!glXQueryVersion(g_display, &major, &minor)) {
    LOG(ERROR) << "glxQueryVersion failed";
    return false;
  }

  if (major == 1 && minor < 3) {
    LOG(ERROR) << "GLX 1.3 or later is required.";
    return false;
  }

  initialized = true;
  return true;
}

Display* GLSurfaceGLX::GetDisplay() {
  return g_display;
}

NativeViewGLSurfaceGLX::NativeViewGLSurfaceGLX(gfx::PluginWindowHandle window)
  : window_(window),
    config_(NULL),
    glx_window_(0) {
}

NativeViewGLSurfaceGLX::~NativeViewGLSurfaceGLX() {
  Destroy();
}

bool NativeViewGLSurfaceGLX::Initialize() {
  DCHECK(!glx_window_);

  XWindowAttributes attributes;
  XGetWindowAttributes(g_display, window_, &attributes);

  XVisualInfo visual_template = {0};
  visual_template.visualid = XVisualIDFromVisual(attributes.visual);

  int num_visual_infos;
  scoped_ptr_malloc<XVisualInfo, ScopedPtrXFree> visual_infos(
      XGetVisualInfo(g_display,
                     VisualIDMask,
                     &visual_template,
                     &num_visual_infos));

  if (!num_visual_infos)
    return false;

  if (glXGetFBConfigFromVisualSGIX) {
    config_ = glXGetFBConfigFromVisualSGIX(g_display, visual_infos.get());
    if (!config_) {
      LOG(ERROR) << "glXGetFBConfigFromVisualSGIX failed.";
    }
  }

  if (!config_) {
    int config_id;
    if (glXGetConfig(g_display,
                     visual_infos.get(),
                     GLX_FBCONFIG_ID,
                     &config_id)) {
      LOG(ERROR) << "glXGetConfig failed.";
      return false;
    }

    const int config_attributes[] = {
      GLX_FBCONFIG_ID, config_id,
      0
    };

    int num_elements = 0;
    scoped_ptr_malloc<GLXFBConfig, ScopedPtrXFree> configs(
        glXChooseFBConfig(g_display,
                          DefaultScreen(g_display),
                          config_attributes,
                          &num_elements));
    if (!configs.get()) {
      LOG(ERROR) << "glXChooseFBConfig failed.";
      return false;
    }
    if (!num_elements) {
      LOG(ERROR) << "glXChooseFBConfig returned 0 elements.";
      return false;
    }

    config_ = configs.get()[0];
  }

  // Some X servers do not allow recreating the GLX window after a previous GLX
  // window for the same X window was destroyed.  To work around this, we attach
  // a GLX_WINDOW property to the X window that stores the XID of the GLX
  // window.  In the destructor we do not call glXDestroyWindow right away;
  // instead we add the XID of the deleted window to the destroyed windows list.
  //
  // CollectGarbage call walks through the destroyed windows list and checks
  // that corresponding X windows still exist and refer to the correct GLX
  // window.  If an X window does not exist, it must have been deleted by the
  // browser process.  If an X window does exist but the property does not exist
  // or does not match, X server must have recycled the XID.  In these two cases
  // the GLX window is orphaned and needs to be deleted.
  glx_window_ = GetGLX_WINDOWProperty(window_);
  if (glx_window_)
    glx_windows_destroyed_.erase(glx_window_);
  else {
    glx_window_ = glXCreateWindow(g_display,
                                  static_cast<GLXFBConfig>(config_),
                                  window_,
                                  NULL);
    SetGLX_WINDOWProperty(window_, glx_window_);
  }

  if (!glx_window_) {
    Destroy();
    LOG(ERROR) << "glXCreateWindow failed.";
    return false;
  }

  CollectGarbage();

  return true;
}

void NativeViewGLSurfaceGLX::Destroy() {
  if (glx_window_) {
    glx_windows_destroyed_[glx_window_] = window_;
    glx_window_ = 0;
  }

  config_ = NULL;
}

bool NativeViewGLSurfaceGLX::IsOffscreen() {
  return false;
}

bool NativeViewGLSurfaceGLX::SwapBuffers() {
  glXSwapBuffers(g_display, glx_window_);
  return true;
}

gfx::Size NativeViewGLSurfaceGLX::GetSize() {
  XWindowAttributes attributes;
  XGetWindowAttributes(g_display, window_, &attributes);
  return gfx::Size(attributes.width, attributes.height);
}

void* NativeViewGLSurfaceGLX::GetHandle() {
  return reinterpret_cast<void*>(glx_window_);
}

void* NativeViewGLSurfaceGLX::GetConfig() {
  return config_;
}

PbufferGLSurfaceGLX::PbufferGLSurfaceGLX(const gfx::Size& size)
  : size_(size),
    pbuffer_(0),
    config_(NULL) {
}

PbufferGLSurfaceGLX::~PbufferGLSurfaceGLX() {
  Destroy();
}

bool PbufferGLSurfaceGLX::Initialize() {
  DCHECK(!pbuffer_);

  static const int config_attributes[] = {
    GLX_BUFFER_SIZE, 32,
    GLX_ALPHA_SIZE, 8,
    GLX_BLUE_SIZE, 8,
    GLX_GREEN_SIZE, 8,
    GLX_RED_SIZE, 8,
    GLX_DEPTH_SIZE, 16,  // TODO(apatrick): support optional depth buffer
    GLX_STENCIL_SIZE, 8,
    GLX_RENDER_TYPE, GLX_RGBA_BIT,
    GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT,
    GLX_DOUBLEBUFFER, False,
    0
  };

  int num_elements = 0;
  scoped_ptr_malloc<GLXFBConfig, ScopedPtrXFree> configs(
      glXChooseFBConfig(g_display,
                        DefaultScreen(g_display),
                        config_attributes,
                        &num_elements));
  if (!configs.get()) {
    LOG(ERROR) << "glXChooseFBConfig failed.";
    return false;
  }
  if (!num_elements) {
    LOG(ERROR) << "glXChooseFBConfig returned 0 elements.";
    return false;
  }

  config_ = configs.get()[0];

  const int pbuffer_attributes[] = {
    GLX_PBUFFER_WIDTH, size_.width(),
    GLX_PBUFFER_HEIGHT, size_.height(),
    0
  };
  pbuffer_ = glXCreatePbuffer(g_display,
                              static_cast<GLXFBConfig>(config_),
                              pbuffer_attributes);
  if (!pbuffer_) {
    Destroy();
    LOG(ERROR) << "glXCreatePbuffer failed.";
    return false;
  }

  return true;
}

void PbufferGLSurfaceGLX::Destroy() {
  if (pbuffer_) {
    glXDestroyPbuffer(g_display, pbuffer_);
    pbuffer_ = 0;
  }

  config_ = NULL;
}

bool PbufferGLSurfaceGLX::IsOffscreen() {
  return true;
}

bool PbufferGLSurfaceGLX::SwapBuffers() {
  NOTREACHED() << "Attempted to call SwapBuffers on a pbuffer.";
  return false;
}

gfx::Size PbufferGLSurfaceGLX::GetSize() {
  return size_;
}

void* PbufferGLSurfaceGLX::GetHandle() {
  return reinterpret_cast<void*>(pbuffer_);
}

void* PbufferGLSurfaceGLX::GetConfig() {
  return config_;
}

}  // namespace gfx