| 1 |
/* GDK - The GIMP Drawing Kit |
| 2 |
* |
| 3 |
* gdkglcontext-x11.c: X11 specific OpenGL wrappers |
| 4 |
* |
| 5 |
* Copyright © 2014 Emmanuele Bassi |
| 6 |
* |
| 7 |
* This library is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU Library General Public |
| 9 |
* License as published by the Free Software Foundation; either |
| 10 |
* version 2 of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This library is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
* Library General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Library General Public |
| 18 |
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include "gdkglcontext-x11.h" |
| 24 |
#include "gdkdisplay-x11.h" |
| 25 |
#include "gdkscreen-x11.h" |
| 26 |
|
| 27 |
#include "gdkx11display.h" |
| 28 |
#include "gdkx11glcontext.h" |
| 29 |
#include "gdkx11screen.h" |
| 30 |
#include "gdkx11window.h" |
| 31 |
#include "gdkx11visual.h" |
| 32 |
#include "gdkvisualprivate.h" |
| 33 |
#include "gdkx11property.h" |
| 34 |
#include <X11/Xatom.h> |
| 35 |
|
| 36 |
#include "gdkinternals.h" |
| 37 |
|
| 38 |
#include "gdkintl.h" |
| 39 |
|
| 40 |
#include <cairo/cairo-xlib.h> |
| 41 |
|
| 42 |
#include <epoxy/glx.h> |
| 43 |
|
| 44 |
G_DEFINE_TYPE (GdkX11GLContext, gdk_x11_gl_context, GDK_TYPE_GL_CONTEXT) |
| 45 |
|
| 46 |
typedef struct { |
| 47 |
GdkDisplay *display; |
| 48 |
|
| 49 |
GLXDrawable glx_drawable; |
| 50 |
|
| 51 |
Window dummy_xwin; |
| 52 |
GLXWindow dummy_glx; |
| 53 |
|
| 54 |
guint32 last_frame_counter; |
| 55 |
} DrawableInfo; |
| 56 |
|
| 57 |
static void |
| 58 |
drawable_info_free (gpointer data_) |
| 59 |
{ |
| 60 |
DrawableInfo *data = data_; |
| 61 |
Display *dpy; |
| 62 |
|
| 63 |
gdk_x11_display_error_trap_push (data->display); |
| 64 |
|
| 65 |
dpy = gdk_x11_display_get_xdisplay (data->display); |
| 66 |
|
| 67 |
if (data->glx_drawable) |
| 68 |
glXDestroyWindow (dpy, data->glx_drawable); |
| 69 |
|
| 70 |
if (data->dummy_glx) |
| 71 |
glXDestroyWindow (dpy, data->dummy_glx); |
| 72 |
|
| 73 |
if (data->dummy_xwin) |
| 74 |
XDestroyWindow (dpy, data->dummy_xwin); |
| 75 |
|
| 76 |
gdk_x11_display_error_trap_pop_ignored (data->display); |
| 77 |
|
| 78 |
g_slice_free (DrawableInfo, data); |
| 79 |
} |
| 80 |
|
| 81 |
static DrawableInfo * |
| 82 |
get_glx_drawable_info (GdkWindow *window) |
| 83 |
{ |
| 84 |
return g_object_get_data (G_OBJECT (window), "-gdk-x11-window-glx-info"); |
| 85 |
} |
| 86 |
|
| 87 |
static void |
| 88 |
set_glx_drawable_info (GdkWindow *window, |
| 89 |
DrawableInfo *info) |
| 90 |
{ |
| 91 |
g_object_set_data_full (G_OBJECT (window), "-gdk-x11-window-glx-info", |
| 92 |
info, |
| 93 |
drawable_info_free); |
| 94 |
} |
| 95 |
|
| 96 |
static void |
| 97 |
maybe_wait_for_vblank (GdkDisplay *display, |
| 98 |
GLXDrawable drawable) |
| 99 |
{ |
| 100 |
GdkX11Display *display_x11 = GDK_X11_DISPLAY (display); |
| 101 |
Display *dpy = gdk_x11_display_get_xdisplay (display); |
| 102 |
|
| 103 |
if (display_x11->has_glx_sync_control) |
| 104 |
{ |
| 105 |
gint64 ust, msc, sbc; |
| 106 |
|
| 107 |
glXGetSyncValuesOML (dpy, drawable, &ust, &msc, &sbc); |
| 108 |
glXWaitForMscOML (dpy, drawable, |
| 109 |
0, 2, (msc + 1) % 2, |
| 110 |
&ust, &msc, &sbc); |
| 111 |
} |
| 112 |
else if (display_x11->has_glx_video_sync) |
| 113 |
{ |
| 114 |
guint32 current_count; |
| 115 |
|
| 116 |
glXGetVideoSyncSGI (¤t_count); |
| 117 |
glXWaitVideoSyncSGI (2, (current_count + 1) % 2, ¤t_count); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
void |
| 122 |
gdk_x11_window_invalidate_for_new_frame (GdkWindow *window, |
| 123 |
cairo_region_t *update_area) |
| 124 |
{ |
| 125 |
cairo_rectangle_int_t window_rect; |
| 126 |
GdkDisplay *display = gdk_window_get_display (window); |
| 127 |
GdkX11Display *display_x11 = GDK_X11_DISPLAY (display); |
| 128 |
Display *dpy = gdk_x11_display_get_xdisplay (display); |
| 129 |
GdkX11GLContext *context_x11; |
| 130 |
unsigned int buffer_age; |
| 131 |
gboolean invalidate_all; |
| 132 |
|
| 133 |
/* Minimal update is ok if we're not drawing with gl */ |
| 134 |
if (window->gl_paint_context == NULL) |
| 135 |
return; |
| 136 |
|
| 137 |
context_x11 = GDK_X11_GL_CONTEXT (window->gl_paint_context); |
| 138 |
|
| 139 |
buffer_age = 0; |
| 140 |
|
| 141 |
context_x11->do_blit_swap = FALSE; |
| 142 |
|
| 143 |
if (display_x11->has_glx_buffer_age) |
| 144 |
{ |
| 145 |
gdk_gl_context_make_current (window->gl_paint_context); |
| 146 |
glXQueryDrawable(dpy, context_x11->drawable, |
| 147 |
GLX_BACK_BUFFER_AGE_EXT, &buffer_age); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
invalidate_all = FALSE; |
| 152 |
if (buffer_age >= 4) |
| 153 |
{ |
| 154 |
cairo_rectangle_int_t whole_window = { 0, 0, gdk_window_get_width (window), gdk_window_get_height (window) }; |
| 155 |
|
| 156 |
if (gdk_gl_context_has_framebuffer_blit (window->gl_paint_context) && |
| 157 |
cairo_region_contains_rectangle (update_area, &whole_window) != CAIRO_REGION_OVERLAP_IN) |
| 158 |
{ |
| 159 |
context_x11->do_blit_swap = TRUE; |
| 160 |
} |
| 161 |
else |
| 162 |
invalidate_all = TRUE; |
| 163 |
} |
| 164 |
else |
| 165 |
{ |
| 166 |
if (buffer_age == 0) |
| 167 |
{ |
| 168 |
invalidate_all = TRUE; |
| 169 |
} |
| 170 |
if (buffer_age >= 2) |
| 171 |
{ |
| 172 |
if (window->old_updated_area[0]) |
| 173 |
cairo_region_union (update_area, window->old_updated_area[0]); |
| 174 |
else |
| 175 |
invalidate_all = TRUE; |
| 176 |
} |
| 177 |
if (buffer_age >= 3) |
| 178 |
{ |
| 179 |
if (window->old_updated_area[1]) |
| 180 |
cairo_region_union (update_area, window->old_updated_area[1]); |
| 181 |
else |
| 182 |
invalidate_all = TRUE; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
if (invalidate_all) |
| 187 |
{ |
| 188 |
window_rect.x = 0; |
| 189 |
window_rect.y = 0; |
| 190 |
window_rect.width = gdk_window_get_width (window); |
| 191 |
window_rect.height = gdk_window_get_height (window); |
| 192 |
|
| 193 |
/* If nothing else is known, repaint everything so that the back |
| 194 |
buffer is fully up-to-date for the swapbuffer */ |
| 195 |
cairo_region_union_rectangle (update_area, &window_rect); |
| 196 |
} |
| 197 |
|
| 198 |
} |
| 199 |
|
| 200 |
static void |
| 201 |
gdk_gl_blit_region (GdkWindow *window, cairo_region_t *region) |
| 202 |
{ |
| 203 |
int n_rects, i; |
| 204 |
int scale = gdk_window_get_scale_factor (window); |
| 205 |
int wh = gdk_window_get_height (window); |
| 206 |
cairo_rectangle_int_t rect; |
| 207 |
|
| 208 |
n_rects = cairo_region_num_rectangles (region); |
| 209 |
for (i = 0; i < n_rects; i++) |
| 210 |
{ |
| 211 |
cairo_region_get_rectangle (region, i, &rect); |
| 212 |
glScissor (rect.x * scale, (wh - rect.y - rect.height) * scale, rect.width * scale, rect.height * scale); |
| 213 |
glBlitFramebuffer (rect.x * scale, (wh - rect.y - rect.height) * scale, (rect.x + rect.width) * scale, (wh - rect.y) * scale, |
| 214 |
rect.x * scale, (wh - rect.y - rect.height) * scale, (rect.x + rect.width) * scale, (wh - rect.y) * scale, |
| 215 |
GL_COLOR_BUFFER_BIT, GL_NEAREST); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
static void |
| 220 |
gdk_x11_gl_context_end_frame (GdkGLContext *context, |
| 221 |
cairo_region_t *painted, |
| 222 |
cairo_region_t *damage) |
| 223 |
{ |
| 224 |
GdkX11GLContext *context_x11 = GDK_X11_GL_CONTEXT (context); |
| 225 |
GdkWindow *window = gdk_gl_context_get_window (context); |
| 226 |
GdkDisplay *display = gdk_gl_context_get_display (context); |
| 227 |
Display *dpy = gdk_x11_display_get_xdisplay (display); |
| 228 |
GdkX11Display *display_x11 = GDK_X11_DISPLAY (display); |
| 229 |
DrawableInfo *info; |
| 230 |
GLXDrawable drawable; |
| 231 |
|
| 232 |
gdk_gl_context_make_current (context); |
| 233 |
|
| 234 |
info = get_glx_drawable_info (window); |
| 235 |
|
| 236 |
drawable = context_x11->drawable; |
| 237 |
|
| 238 |
GDK_NOTE (OPENGL, |
| 239 |
g_message ("Flushing GLX buffers for drawable %lu (window: %lu), frame sync: %s", |
| 240 |
(unsigned long) drawable, |
| 241 |
(unsigned long) gdk_x11_window_get_xid (window), |
| 242 |
context_x11->do_frame_sync ? "yes" : "no")); |
| 243 |
|
| 244 |
/* if we are going to wait for the vertical refresh manually |
| 245 |
* we need to flush pending redraws, and we also need to wait |
| 246 |
* for that to finish, otherwise we are going to tear. |
| 247 |
* |
| 248 |
* obviously, this condition should not be hit if we have |
| 249 |
* GLX_SGI_swap_control, and we ask the driver to do the right |
| 250 |
* thing. |
| 251 |
*/ |
| 252 |
if (context_x11->do_frame_sync) |
| 253 |
{ |
| 254 |
guint32 end_frame_counter = 0; |
| 255 |
gboolean has_counter = display_x11->has_glx_video_sync; |
| 256 |
gboolean can_wait = display_x11->has_glx_video_sync || display_x11->has_glx_sync_control; |
| 257 |
|
| 258 |
if (display_x11->has_glx_video_sync) |
| 259 |
glXGetVideoSyncSGI (&end_frame_counter); |
| 260 |
|
| 261 |
if (context_x11->do_frame_sync && !display_x11->has_glx_swap_interval) |
| 262 |
{ |
| 263 |
glFinish (); |
| 264 |
|
| 265 |
if (has_counter && can_wait) |
| 266 |
{ |
| 267 |
guint32 last_counter = info != NULL ? info->last_frame_counter : 0; |
| 268 |
|
| 269 |
if (last_counter == end_frame_counter) |
| 270 |
maybe_wait_for_vblank (display, drawable); |
| 271 |
} |
| 272 |
else if (can_wait) |
| 273 |
maybe_wait_for_vblank (display, drawable); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
if (context_x11->do_blit_swap) |
| 278 |
{ |
| 279 |
glDrawBuffer(GL_FRONT); |
| 280 |
glReadBuffer(GL_BACK); |
| 281 |
gdk_gl_blit_region (window, painted); |
| 282 |
glDrawBuffer(GL_BACK); |
| 283 |
glFlush(); |
| 284 |
|
| 285 |
if (gdk_gl_context_has_frame_terminator (context)) |
| 286 |
glFrameTerminatorGREMEDY (); |
| 287 |
} |
| 288 |
else |
| 289 |
glXSwapBuffers (dpy, drawable); |
| 290 |
|
| 291 |
if (context_x11->do_frame_sync && info != NULL && display_x11->has_glx_video_sync) |
| 292 |
glXGetVideoSyncSGI (&info->last_frame_counter); |
| 293 |
} |
| 294 |
|
| 295 |
typedef struct { |
| 296 |
Display *display; |
| 297 |
GLXDrawable drawable; |
| 298 |
gboolean y_inverted; |
| 299 |
} GdkGLXPixmap; |
| 300 |
|
| 301 |
static void |
| 302 |
glx_pixmap_destroy (void *data) |
| 303 |
{ |
| 304 |
GdkGLXPixmap *glx_pixmap = data; |
| 305 |
|
| 306 |
glXDestroyPixmap (glx_pixmap->display, glx_pixmap->drawable); |
| 307 |
|
| 308 |
g_slice_free (GdkGLXPixmap, glx_pixmap); |
| 309 |
} |
| 310 |
|
| 311 |
static GdkGLXPixmap * |
| 312 |
glx_pixmap_get (cairo_surface_t *surface, guint texture_target) |
| 313 |
{ |
| 314 |
Display *display = cairo_xlib_surface_get_display (surface); |
| 315 |
Screen *screen = cairo_xlib_surface_get_screen (surface); |
| 316 |
Visual *visual = cairo_xlib_surface_get_visual (surface); |
| 317 |
GdkGLXPixmap *glx_pixmap; |
| 318 |
GLXFBConfig *fbconfigs, config; |
| 319 |
int nfbconfigs; |
| 320 |
XVisualInfo *visinfo; |
| 321 |
VisualID visualid; |
| 322 |
int i, value; |
| 323 |
gboolean y_inverted; |
| 324 |
gboolean with_alpha; |
| 325 |
guint target = 0; |
| 326 |
guint format = 0; |
| 327 |
int pixmap_attributes[] = { |
| 328 |
GLX_TEXTURE_TARGET_EXT, 0, |
| 329 |
GLX_TEXTURE_FORMAT_EXT, 0, |
| 330 |
None |
| 331 |
}; |
| 332 |
|
| 333 |
if (visual == NULL) |
| 334 |
return NULL; |
| 335 |
|
| 336 |
with_alpha = cairo_surface_get_content (surface) == CAIRO_CONTENT_COLOR_ALPHA; |
| 337 |
|
| 338 |
y_inverted = FALSE; |
| 339 |
fbconfigs = glXGetFBConfigs (display, XScreenNumberOfScreen (screen), &nfbconfigs); |
| 340 |
for (i = 0; i < nfbconfigs; i++) |
| 341 |
{ |
| 342 |
visinfo = glXGetVisualFromFBConfig (display, fbconfigs[i]); |
| 343 |
if (!visinfo) |
| 344 |
continue; |
| 345 |
|
| 346 |
visualid = visinfo->visualid; |
| 347 |
XFree (visinfo); |
| 348 |
|
| 349 |
if (visualid != XVisualIDFromVisual (visual)) |
| 350 |
continue; |
| 351 |
|
| 352 |
glXGetFBConfigAttrib (display, fbconfigs[i], GLX_DRAWABLE_TYPE, &value); |
| 353 |
if (!(value & GLX_PIXMAP_BIT)) |
| 354 |
continue; |
| 355 |
|
| 356 |
glXGetFBConfigAttrib (display, fbconfigs[i], |
| 357 |
GLX_BIND_TO_TEXTURE_TARGETS_EXT, |
| 358 |
&value); |
| 359 |
if (texture_target == GL_TEXTURE_2D) |
| 360 |
{ |
| 361 |
if (value & GLX_TEXTURE_2D_BIT_EXT) |
| 362 |
target = GLX_TEXTURE_2D_EXT; |
| 363 |
else |
| 364 |
continue; |
| 365 |
} |
| 366 |
else if (texture_target == GL_TEXTURE_RECTANGLE_ARB) |
| 367 |
{ |
| 368 |
if (value & GLX_TEXTURE_RECTANGLE_BIT_EXT) |
| 369 |
target = GLX_TEXTURE_RECTANGLE_EXT; |
| 370 |
else |
| 371 |
continue; |
| 372 |
} |
| 373 |
else |
| 374 |
continue; |
| 375 |
|
| 376 |
if (!with_alpha) |
| 377 |
{ |
| 378 |
glXGetFBConfigAttrib (display, fbconfigs[i], |
| 379 |
GLX_BIND_TO_TEXTURE_RGB_EXT, |
| 380 |
&value); |
| 381 |
if (!value) |
| 382 |
continue; |
| 383 |
|
| 384 |
format = GLX_TEXTURE_FORMAT_RGB_EXT; |
| 385 |
} |
| 386 |
else |
| 387 |
{ |
| 388 |
glXGetFBConfigAttrib (display, fbconfigs[i], |
| 389 |
GLX_BIND_TO_TEXTURE_RGBA_EXT, |
| 390 |
&value); |
| 391 |
if (!value) |
| 392 |
continue; |
| 393 |
|
| 394 |
format = GLX_TEXTURE_FORMAT_RGBA_EXT; |
| 395 |
} |
| 396 |
|
| 397 |
glXGetFBConfigAttrib (display, fbconfigs[i], |
| 398 |
GLX_Y_INVERTED_EXT, |
| 399 |
&value); |
| 400 |
if (value == TRUE) |
| 401 |
y_inverted = TRUE; |
| 402 |
|
| 403 |
config = fbconfigs[i]; |
| 404 |
break; |
| 405 |
} |
| 406 |
|
| 407 |
XFree (fbconfigs); |
| 408 |
|
| 409 |
if (i == nfbconfigs) |
| 410 |
return NULL; |
| 411 |
|
| 412 |
pixmap_attributes[1] = target; |
| 413 |
pixmap_attributes[3] = format; |
| 414 |
|
| 415 |
glx_pixmap = g_slice_new0 (GdkGLXPixmap); |
| 416 |
glx_pixmap->y_inverted = y_inverted; |
| 417 |
glx_pixmap->display = display; |
| 418 |
glx_pixmap->drawable = glXCreatePixmap (display, config, |
| 419 |
cairo_xlib_surface_get_drawable (surface), |
| 420 |
pixmap_attributes); |
| 421 |
|
| 422 |
return glx_pixmap; |
| 423 |
} |
| 424 |
|
| 425 |
static gboolean |
| 426 |
gdk_x11_gl_context_texture_from_surface (GdkGLContext *paint_context, |
| 427 |
cairo_surface_t *surface, |
| 428 |
cairo_region_t *region) |
| 429 |
{ |
| 430 |
GdkGLXPixmap *glx_pixmap; |
| 431 |
double device_x_offset, device_y_offset; |
| 432 |
cairo_rectangle_int_t rect; |
| 433 |
int n_rects, i; |
| 434 |
GdkWindow *window; |
| 435 |
int unscaled_window_height; |
| 436 |
int window_scale; |
| 437 |
unsigned int texture_id; |
| 438 |
gboolean use_texture_rectangle; |
| 439 |
guint target; |
| 440 |
double sx, sy; |
| 441 |
float uscale, vscale; |
| 442 |
GdkTexturedQuad *quads; |
| 443 |
GdkX11Display *display_x11; |
| 444 |
|
| 445 |
display_x11 = GDK_X11_DISPLAY (gdk_gl_context_get_display (paint_context)); |
| 446 |
if (!display_x11->has_glx_texture_from_pixmap) |
| 447 |
return FALSE; |
| 448 |
|
| 449 |
if (cairo_surface_get_type (surface) != CAIRO_SURFACE_TYPE_XLIB) |
| 450 |
return FALSE; |
| 451 |
|
| 452 |
use_texture_rectangle = gdk_gl_context_use_texture_rectangle (paint_context); |
| 453 |
if (use_texture_rectangle) |
| 454 |
target = GL_TEXTURE_RECTANGLE_ARB; |
| 455 |
else |
| 456 |
target = GL_TEXTURE_2D; |
| 457 |
|
| 458 |
glx_pixmap = glx_pixmap_get (surface, target); |
| 459 |
if (glx_pixmap == NULL) |
| 460 |
return FALSE; |
| 461 |
|
| 462 |
GDK_NOTE (OPENGL, g_message ("Using GLX_EXT_texture_from_pixmap to draw surface")); |
| 463 |
|
| 464 |
window = gdk_gl_context_get_window (paint_context)->impl_window; |
| 465 |
window_scale = gdk_window_get_scale_factor (window); |
| 466 |
gdk_window_get_unscaled_size (window, NULL, &unscaled_window_height); |
| 467 |
|
| 468 |
sx = sy = 1; |
| 469 |
cairo_surface_get_device_scale (window->current_paint.surface, &sx, &sy); |
| 470 |
cairo_surface_get_device_offset (surface, &device_x_offset, &device_y_offset); |
| 471 |
|
| 472 |
/* Ensure all the X stuff are synced before we read it back via texture-from-pixmap */ |
| 473 |
glXWaitX(); |
| 474 |
|
| 475 |
glGenTextures (1, &texture_id); |
| 476 |
glBindTexture (target, texture_id); |
| 477 |
|
| 478 |
glTexParameteri (target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 479 |
glTexParameteri (target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 480 |
glTexParameteri (target, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 481 |
glTexParameteri (target, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 482 |
|
| 483 |
glXBindTexImageEXT (glx_pixmap->display, glx_pixmap->drawable, |
| 484 |
GLX_FRONT_LEFT_EXT, NULL); |
| 485 |
|
| 486 |
glEnable (GL_SCISSOR_TEST); |
| 487 |
|
| 488 |
n_rects = cairo_region_num_rectangles (region); |
| 489 |
quads = g_new (GdkTexturedQuad, n_rects); |
| 490 |
|
| 491 |
#define FLIP_Y(_y) (unscaled_window_height - (_y)) |
| 492 |
|
| 493 |
cairo_region_get_extents (region, &rect); |
| 494 |
glScissor (rect.x * window_scale, FLIP_Y((rect.y + rect.height) * window_scale), |
| 495 |
rect.width * window_scale, rect.height * window_scale); |
| 496 |
|
| 497 |
for (i = 0; i < n_rects; i++) |
| 498 |
{ |
| 499 |
int src_x, src_y, src_height, src_width; |
| 500 |
|
| 501 |
cairo_region_get_rectangle (region, i, &rect); |
| 502 |
|
| 503 |
src_x = rect.x * sx + device_x_offset; |
| 504 |
src_y = rect.y * sy + device_y_offset; |
| 505 |
src_width = rect.width * sx; |
| 506 |
src_height = rect.height * sy; |
| 507 |
|
| 508 |
if (use_texture_rectangle) |
| 509 |
{ |
| 510 |
uscale = 1.0; |
| 511 |
vscale = 1.0; |
| 512 |
} |
| 513 |
else |
| 514 |
{ |
| 515 |
uscale = 1.0 / cairo_xlib_surface_get_width (surface); |
| 516 |
vscale = 1.0 / cairo_xlib_surface_get_height (surface); |
| 517 |
} |
| 518 |
|
| 519 |
{ |
| 520 |
GdkTexturedQuad quad = { |
| 521 |
rect.x * window_scale, FLIP_Y(rect.y * window_scale), |
| 522 |
(rect.x + rect.width) * window_scale, FLIP_Y((rect.y + rect.height) * window_scale), |
| 523 |
uscale * src_x, vscale * src_y, |
| 524 |
uscale * (src_x + src_width), vscale * (src_y + src_height), |
| 525 |
}; |
| 526 |
|
| 527 |
quads[i] = quad; |
| 528 |
} |
| 529 |
} |
| 530 |
|
| 531 |
#undef FLIP_Y |
| 532 |
|
| 533 |
gdk_gl_texture_quads (paint_context, target, n_rects, quads, FALSE); |
| 534 |
g_free (quads); |
| 535 |
|
| 536 |
glDisable (GL_SCISSOR_TEST); |
| 537 |
|
| 538 |
glXReleaseTexImageEXT (glx_pixmap->display, glx_pixmap->drawable, |
| 539 |
GLX_FRONT_LEFT_EXT); |
| 540 |
|
| 541 |
glDeleteTextures (1, &texture_id); |
| 542 |
|
| 543 |
glx_pixmap_destroy(glx_pixmap); |
| 544 |
|
| 545 |
return TRUE; |
| 546 |
} |
| 547 |
|
| 548 |
static XVisualInfo * |
| 549 |
find_xvisinfo_for_fbconfig (GdkDisplay *display, |
| 550 |
GLXFBConfig config) |
| 551 |
{ |
| 552 |
Display *dpy = gdk_x11_display_get_xdisplay (display); |
| 553 |
|
| 554 |
return glXGetVisualFromFBConfig (dpy, config); |
| 555 |
} |
| 556 |
|
| 557 |
static GLXContext |
| 558 |
create_gl3_context (GdkDisplay *display, |
| 559 |
GLXFBConfig config, |
| 560 |
GdkGLContext *share, |
| 561 |
int profile, |
| 562 |
int flags, |
| 563 |
int major, |
| 564 |
int minor) |
| 565 |
{ |
| 566 |
int attrib_list[] = { |
| 567 |
GLX_CONTEXT_PROFILE_MASK_ARB, profile, |
| 568 |
GLX_CONTEXT_MAJOR_VERSION_ARB, major, |
| 569 |
GLX_CONTEXT_MINOR_VERSION_ARB, minor, |
| 570 |
GLX_CONTEXT_FLAGS_ARB, flags, |
| 571 |
None, |
| 572 |
}; |
| 573 |
GLXContext res; |
| 574 |
|
| 575 |
GdkX11GLContext *share_x11 = NULL; |
| 576 |
|
| 577 |
if (share != NULL) |
| 578 |
share_x11 = GDK_X11_GL_CONTEXT (share); |
| 579 |
|
| 580 |
gdk_x11_display_error_trap_push (display); |
| 581 |
|
| 582 |
res = glXCreateContextAttribsARB (gdk_x11_display_get_xdisplay (display), |
| 583 |
config, |
| 584 |
share_x11 != NULL ? share_x11->glx_context : NULL, |
| 585 |
True, |
| 586 |
attrib_list); |
| 587 |
|
| 588 |
if (gdk_x11_display_error_trap_pop (display)) |
| 589 |
return NULL; |
| 590 |
|
| 591 |
return res; |
| 592 |
} |
| 593 |
|
| 594 |
static GLXContext |
| 595 |
create_legacy_context (GdkDisplay *display, |
| 596 |
GLXFBConfig config, |
| 597 |
GdkGLContext *share) |
| 598 |
{ |
| 599 |
GdkX11GLContext *share_x11 = NULL; |
| 600 |
GLXContext res; |
| 601 |
|
| 602 |
if (share != NULL) |
| 603 |
share_x11 = GDK_X11_GL_CONTEXT (share); |
| 604 |
|
| 605 |
gdk_x11_display_error_trap_push (display); |
| 606 |
|
| 607 |
res = glXCreateNewContext (gdk_x11_display_get_xdisplay (display), |
| 608 |
config, |
| 609 |
GLX_RGBA_TYPE, |
| 610 |
share_x11 != NULL ? share_x11->glx_context : NULL, |
| 611 |
TRUE); |
| 612 |
|
| 613 |
if (gdk_x11_display_error_trap_pop (display)) |
| 614 |
return NULL; |
| 615 |
|
| 616 |
return res; |
| 617 |
} |
| 618 |
|
| 619 |
static gboolean |
| 620 |
gdk_x11_gl_context_realize (GdkGLContext *context, |
| 621 |
GError **error) |
| 622 |
{ |
| 623 |
GdkX11Display *display_x11; |
| 624 |
GdkDisplay *display; |
| 625 |
GdkX11GLContext *context_x11; |
| 626 |
GLXWindow drawable; |
| 627 |
XVisualInfo *xvisinfo; |
| 628 |
Display *dpy; |
| 629 |
DrawableInfo *info; |
| 630 |
GdkGLContext *share; |
| 631 |
GdkWindow *window; |
| 632 |
gboolean debug_bit, compat_bit, legacy_bit, es_bit; |
| 633 |
int major, minor, flags; |
| 634 |
|
| 635 |
window = gdk_gl_context_get_window (context); |
| 636 |
display = gdk_window_get_display (window); |
| 637 |
dpy = gdk_x11_display_get_xdisplay (display); |
| 638 |
context_x11 = GDK_X11_GL_CONTEXT (context); |
| 639 |
display_x11 = GDK_X11_DISPLAY (display); |
| 640 |
share = gdk_gl_context_get_shared_context (context); |
| 641 |
|
| 642 |
gdk_gl_context_get_required_version (context, &major, &minor); |
| 643 |
debug_bit = gdk_gl_context_get_debug_enabled (context); |
| 644 |
compat_bit = gdk_gl_context_get_forward_compatible (context); |
| 645 |
|
| 646 |
/* If there is no glXCreateContextAttribsARB() then we default to legacy */ |
| 647 |
legacy_bit = !display_x11->has_glx_create_context || |
| 648 |
(_gdk_gl_flags & GDK_GL_LEGACY) != 0; |
| 649 |
|
| 650 |
es_bit = ((_gdk_gl_flags & GDK_GL_GLES) != 0 || |
| 651 |
(share != NULL && gdk_gl_context_get_use_es (share))) && |
| 652 |
(display_x11->has_glx_create_context && display_x11->has_glx_create_es2_context); |
| 653 |
|
| 654 |
/* We cannot share legacy contexts with core profile ones, so the |
| 655 |
* shared context is the one that decides if we're going to create |
| 656 |
* a legacy context or not. |
| 657 |
*/ |
| 658 |
if (share != NULL && gdk_gl_context_is_legacy (share)) |
| 659 |
legacy_bit = TRUE; |
| 660 |
|
| 661 |
flags = 0; |
| 662 |
if (debug_bit) |
| 663 |
flags |= GLX_CONTEXT_DEBUG_BIT_ARB; |
| 664 |
if (compat_bit) |
| 665 |
flags |= GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB; |
| 666 |
|
| 667 |
GDK_NOTE (OPENGL, |
| 668 |
g_message ("Creating GLX context (version:%d.%d, debug:%s, forward:%s, legacy:%s, es:%s)", |
| 669 |
major, minor, |
| 670 |
debug_bit ? "yes" : "no", |
| 671 |
compat_bit ? "yes" : "no", |
| 672 |
legacy_bit ? "yes" : "no", |
| 673 |
es_bit ? "yes" : "no")); |
| 674 |
|
| 675 |
/* If we have access to GLX_ARB_create_context_profile then we can ask for |
| 676 |
* a compatibility profile; if we don't, then we have to fall back to the |
| 677 |
* old GLX 1.3 API. |
| 678 |
*/ |
| 679 |
if (legacy_bit && !GDK_X11_DISPLAY (display)->has_glx_create_context) |
| 680 |
{ |
| 681 |
GDK_NOTE (OPENGL, g_message ("Creating legacy GL context on request")); |
| 682 |
context_x11->glx_context = create_legacy_context (display, context_x11->glx_config, share); |
| 683 |
} |
| 684 |
else |
| 685 |
{ |
| 686 |
int profile; |
| 687 |
|
| 688 |
if (es_bit) |
| 689 |
profile = GLX_CONTEXT_ES2_PROFILE_BIT_EXT; |
| 690 |
else |
| 691 |
profile = legacy_bit ? GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB |
| 692 |
: GLX_CONTEXT_CORE_PROFILE_BIT_ARB; |
| 693 |
|
| 694 |
/* We need to tweak the version, otherwise we may end up requesting |
| 695 |
* a compatibility context with a minimum version of 3.2, which is |
| 696 |
* an error |
| 697 |
*/ |
| 698 |
if (legacy_bit) |
| 699 |
{ |
| 700 |
major = 3; |
| 701 |
minor = 0; |
| 702 |
} |
| 703 |
|
| 704 |
GDK_NOTE (OPENGL, g_message ("Creating GL3 context")); |
| 705 |
context_x11->glx_context = create_gl3_context (display, |
| 706 |
context_x11->glx_config, |
| 707 |
share, |
| 708 |
profile, flags, major, minor); |
| 709 |
|
| 710 |
/* Fall back to legacy in case the GL3 context creation failed */ |
| 711 |
if (context_x11->glx_context == NULL) |
| 712 |
{ |
| 713 |
GDK_NOTE (OPENGL, g_message ("Creating fallback legacy context")); |
| 714 |
context_x11->glx_context = create_legacy_context (display, context_x11->glx_config, share); |
| 715 |
legacy_bit = TRUE; |
| 716 |
es_bit = FALSE; |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
if (context_x11->glx_context == NULL) |
| 721 |
{ |
| 722 |
g_set_error_literal (error, GDK_GL_ERROR, |
| 723 |
GDK_GL_ERROR_NOT_AVAILABLE, |
| 724 |
_("Unable to create a GL context")); |
| 725 |
return FALSE; |
| 726 |
} |
| 727 |
|
| 728 |
/* Ensure that any other context is created with a legacy bit set */ |
| 729 |
gdk_gl_context_set_is_legacy (context, legacy_bit); |
| 730 |
|
| 731 |
/* Ensure that any other context is created with a ES bit set */ |
| 732 |
gdk_gl_context_set_use_es (context, es_bit); |
| 733 |
|
| 734 |
xvisinfo = find_xvisinfo_for_fbconfig (display, context_x11->glx_config); |
| 735 |
|
| 736 |
info = get_glx_drawable_info (window->impl_window); |
| 737 |
if (info == NULL) |
| 738 |
{ |
| 739 |
XSetWindowAttributes attrs; |
| 740 |
unsigned long mask; |
| 741 |
|
| 742 |
gdk_x11_display_error_trap_push (display); |
| 743 |
|
| 744 |
info = g_slice_new0 (DrawableInfo); |
| 745 |
info->display = display; |
| 746 |
info->last_frame_counter = 0; |
| 747 |
|
| 748 |
attrs.override_redirect = True; |
| 749 |
attrs.colormap = XCreateColormap (dpy, DefaultRootWindow (dpy), xvisinfo->visual, AllocNone); |
| 750 |
attrs.border_pixel = 0; |
| 751 |
mask = CWOverrideRedirect | CWColormap | CWBorderPixel; |
| 752 |
info->dummy_xwin = XCreateWindow (dpy, DefaultRootWindow (dpy), |
| 753 |
-100, -100, 1, 1, |
| 754 |
0, |
| 755 |
xvisinfo->depth, |
| 756 |
CopyFromParent, |
| 757 |
xvisinfo->visual, |
| 758 |
mask, |
| 759 |
&attrs); |
| 760 |
XMapWindow(dpy, info->dummy_xwin); |
| 761 |
|
| 762 |
if (GDK_X11_DISPLAY (display)->glx_version >= 13) |
| 763 |
{ |
| 764 |
info->glx_drawable = glXCreateWindow (dpy, context_x11->glx_config, |
| 765 |
gdk_x11_window_get_xid (window->impl_window), |
| 766 |
NULL); |
| 767 |
info->dummy_glx = glXCreateWindow (dpy, context_x11->glx_config, info->dummy_xwin, NULL); |
| 768 |
} |
| 769 |
|
| 770 |
if (gdk_x11_display_error_trap_pop (display)) |
| 771 |
{ |
| 772 |
g_set_error_literal (error, GDK_GL_ERROR, |
| 773 |
GDK_GL_ERROR_NOT_AVAILABLE, |
| 774 |
_("Unable to create a GL context")); |
| 775 |
|
| 776 |
XFree (xvisinfo); |
| 777 |
drawable_info_free (info); |
| 778 |
glXDestroyContext (dpy, context_x11->glx_context); |
| 779 |
context_x11->glx_context = NULL; |
| 780 |
|
| 781 |
return FALSE; |
| 782 |
} |
| 783 |
|
| 784 |
set_glx_drawable_info (window->impl_window, info); |
| 785 |
} |
| 786 |
|
| 787 |
XFree (xvisinfo); |
| 788 |
|
| 789 |
if (context_x11->is_attached) |
| 790 |
drawable = info->glx_drawable ? info->glx_drawable : gdk_x11_window_get_xid (window->impl_window); |
| 791 |
else |
| 792 |
drawable = info->dummy_glx ? info->dummy_glx : info->dummy_xwin; |
| 793 |
|
| 794 |
context_x11->is_direct = glXIsDirect (dpy, context_x11->glx_context); |
| 795 |
context_x11->drawable = drawable; |
| 796 |
|
| 797 |
GDK_NOTE (OPENGL, |
| 798 |
g_message ("Realized GLX context[%p], %s", |
| 799 |
context_x11->glx_context, |
| 800 |
context_x11->is_direct ? "direct" : "indirect")); |
| 801 |
|
| 802 |
return TRUE; |
| 803 |
} |
| 804 |
|
| 805 |
static void |
| 806 |
gdk_x11_gl_context_dispose (GObject *gobject) |
| 807 |
{ |
| 808 |
GdkX11GLContext *context_x11 = GDK_X11_GL_CONTEXT (gobject); |
| 809 |
|
| 810 |
if (context_x11->glx_context != NULL) |
| 811 |
{ |
| 812 |
GdkGLContext *context = GDK_GL_CONTEXT (gobject); |
| 813 |
GdkDisplay *display = gdk_gl_context_get_display (context); |
| 814 |
Display *dpy = gdk_x11_display_get_xdisplay (display); |
| 815 |
|
| 816 |
if (glXGetCurrentContext () == context_x11->glx_context) |
| 817 |
glXMakeContextCurrent (dpy, None, None, NULL); |
| 818 |
|
| 819 |
GDK_NOTE (OPENGL, g_message ("Destroying GLX context")); |
| 820 |
glXDestroyContext (dpy, context_x11->glx_context); |
| 821 |
context_x11->glx_context = NULL; |
| 822 |
} |
| 823 |
|
| 824 |
G_OBJECT_CLASS (gdk_x11_gl_context_parent_class)->dispose (gobject); |
| 825 |
} |
| 826 |
|
| 827 |
static void |
| 828 |
gdk_x11_gl_context_class_init (GdkX11GLContextClass *klass) |
| 829 |
{ |
| 830 |
GdkGLContextClass *context_class = GDK_GL_CONTEXT_CLASS (klass); |
| 831 |
GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
| 832 |
|
| 833 |
context_class->realize = gdk_x11_gl_context_realize; |
| 834 |
context_class->end_frame = gdk_x11_gl_context_end_frame; |
| 835 |
context_class->texture_from_surface = gdk_x11_gl_context_texture_from_surface; |
| 836 |
|
| 837 |
gobject_class->dispose = gdk_x11_gl_context_dispose; |
| 838 |
} |
| 839 |
|
| 840 |
static void |
| 841 |
gdk_x11_gl_context_init (GdkX11GLContext *self) |
| 842 |
{ |
| 843 |
self->do_frame_sync = TRUE; |
| 844 |
} |
| 845 |
|
| 846 |
gboolean |
| 847 |
gdk_x11_screen_init_gl (GdkScreen *screen) |
| 848 |
{ |
| 849 |
GdkDisplay *display = gdk_screen_get_display (screen); |
| 850 |
GdkX11Display *display_x11 = GDK_X11_DISPLAY (display); |
| 851 |
Display *dpy; |
| 852 |
int error_base, event_base; |
| 853 |
int screen_num; |
| 854 |
|
| 855 |
if (display_x11->have_glx) |
| 856 |
return TRUE; |
| 857 |
|
| 858 |
if (_gdk_gl_flags & GDK_GL_DISABLE) |
| 859 |
return FALSE; |
| 860 |
|
| 861 |
dpy = gdk_x11_display_get_xdisplay (display); |
| 862 |
|
| 863 |
if (!epoxy_has_glx (dpy)) |
| 864 |
return FALSE; |
| 865 |
|
| 866 |
if (!glXQueryExtension (dpy, &error_base, &event_base)) |
| 867 |
return FALSE; |
| 868 |
|
| 869 |
screen_num = GDK_X11_SCREEN (screen)->screen_num; |
| 870 |
|
| 871 |
display_x11->have_glx = TRUE; |
| 872 |
|
| 873 |
display_x11->glx_version = epoxy_glx_version (dpy, screen_num); |
| 874 |
display_x11->glx_error_base = error_base; |
| 875 |
display_x11->glx_event_base = event_base; |
| 876 |
|
| 877 |
display_x11->has_glx_create_context = |
| 878 |
epoxy_has_glx_extension (dpy, screen_num, "GLX_ARB_create_context_profile"); |
| 879 |
display_x11->has_glx_create_es2_context = |
| 880 |
epoxy_has_glx_extension (dpy, screen_num, "GLX_EXT_create_context_es2_profile"); |
| 881 |
display_x11->has_glx_swap_interval = |
| 882 |
epoxy_has_glx_extension (dpy, screen_num, "GLX_SGI_swap_control"); |
| 883 |
display_x11->has_glx_texture_from_pixmap = |
| 884 |
epoxy_has_glx_extension (dpy, screen_num, "GLX_EXT_texture_from_pixmap"); |
| 885 |
display_x11->has_glx_video_sync = |
| 886 |
epoxy_has_glx_extension (dpy, screen_num, "GLX_SGI_video_sync"); |
| 887 |
display_x11->has_glx_buffer_age = |
| 888 |
epoxy_has_glx_extension (dpy, screen_num, "GLX_EXT_buffer_age"); |
| 889 |
display_x11->has_glx_sync_control = |
| 890 |
epoxy_has_glx_extension (dpy, screen_num, "GLX_OML_sync_control"); |
| 891 |
display_x11->has_glx_multisample = |
| 892 |
epoxy_has_glx_extension (dpy, screen_num, "GLX_ARB_multisample"); |
| 893 |
display_x11->has_glx_visual_rating = |
| 894 |
epoxy_has_glx_extension (dpy, screen_num, "GLX_EXT_visual_rating"); |
| 895 |
|
| 896 |
GDK_NOTE (OPENGL, |
| 897 |
g_message ("GLX version %d.%d found\n" |
| 898 |
" - Vendor: %s\n" |
| 899 |
" - Checked extensions:\n" |
| 900 |
"\t* GLX_ARB_create_context_profile: %s\n" |
| 901 |
"\t* GLX_EXT_create_context_es2_profile: %s\n" |
| 902 |
"\t* GLX_SGI_swap_control: %s\n" |
| 903 |
"\t* GLX_EXT_texture_from_pixmap: %s\n" |
| 904 |
"\t* GLX_SGI_video_sync: %s\n" |
| 905 |
"\t* GLX_EXT_buffer_age: %s\n" |
| 906 |
"\t* GLX_OML_sync_control: %s", |
| 907 |
display_x11->glx_version / 10, |
| 908 |
display_x11->glx_version % 10, |
| 909 |
glXGetClientString (dpy, GLX_VENDOR), |
| 910 |
display_x11->has_glx_create_context ? "yes" : "no", |
| 911 |
display_x11->has_glx_create_es2_context ? "yes" : "no", |
| 912 |
display_x11->has_glx_swap_interval ? "yes" : "no", |
| 913 |
display_x11->has_glx_texture_from_pixmap ? "yes" : "no", |
| 914 |
display_x11->has_glx_video_sync ? "yes" : "no", |
| 915 |
display_x11->has_glx_buffer_age ? "yes" : "no", |
| 916 |
display_x11->has_glx_sync_control ? "yes" : "no")); |
| 917 |
|
| 918 |
return TRUE; |
| 919 |
} |
| 920 |
|
| 921 |
#define MAX_GLX_ATTRS 30 |
| 922 |
|
| 923 |
static gboolean |
| 924 |
find_fbconfig_for_visual (GdkDisplay *display, |
| 925 |
GdkVisual *visual, |
| 926 |
GLXFBConfig *fb_config_out, |
| 927 |
GError **error) |
| 928 |
{ |
| 929 |
static int attrs[MAX_GLX_ATTRS]; |
| 930 |
Display *dpy = gdk_x11_display_get_xdisplay (display); |
| 931 |
GLXFBConfig *configs; |
| 932 |
int n_configs, i; |
| 933 |
gboolean use_rgba; |
| 934 |
gboolean retval = FALSE; |
| 935 |
VisualID xvisual_id = XVisualIDFromVisual(gdk_x11_visual_get_xvisual (visual)); |
| 936 |
|
| 937 |
i = 0; |
| 938 |
attrs[i++] = GLX_DRAWABLE_TYPE; |
| 939 |
attrs[i++] = GLX_WINDOW_BIT; |
| 940 |
|
| 941 |
attrs[i++] = GLX_RENDER_TYPE; |
| 942 |
attrs[i++] = GLX_RGBA_BIT; |
| 943 |
|
| 944 |
attrs[i++] = GLX_DOUBLEBUFFER; |
| 945 |
attrs[i++] = GL_TRUE; |
| 946 |
|
| 947 |
attrs[i++] = GLX_RED_SIZE; |
| 948 |
attrs[i++] = 1; |
| 949 |
attrs[i++] = GLX_GREEN_SIZE; |
| 950 |
attrs[i++] = 1; |
| 951 |
attrs[i++] = GLX_BLUE_SIZE; |
| 952 |
attrs[i++] = 1; |
| 953 |
|
| 954 |
use_rgba = (visual == gdk_screen_get_rgba_visual (gdk_display_get_default_screen (display))); |
| 955 |
if (use_rgba) |
| 956 |
{ |
| 957 |
attrs[i++] = GLX_ALPHA_SIZE; |
| 958 |
attrs[i++] = 1; |
| 959 |
} |
| 960 |
else |
| 961 |
{ |
| 962 |
attrs[i++] = GLX_ALPHA_SIZE; |
| 963 |
attrs[i++] = GLX_DONT_CARE; |
| 964 |
} |
| 965 |
|
| 966 |
attrs[i++] = None; |
| 967 |
|
| 968 |
g_assert (i < MAX_GLX_ATTRS); |
| 969 |
|
| 970 |
configs = glXChooseFBConfig (dpy, DefaultScreen (dpy), attrs, &n_configs); |
| 971 |
if (configs == NULL || n_configs == 0) |
| 972 |
{ |
| 973 |
g_set_error_literal (error, GDK_GL_ERROR, |
| 974 |
GDK_GL_ERROR_UNSUPPORTED_FORMAT, |
| 975 |
_("No available configurations for the given pixel format")); |
| 976 |
return FALSE; |
| 977 |
} |
| 978 |
|
| 979 |
for (i = 0; i < n_configs; i++) |
| 980 |
{ |
| 981 |
XVisualInfo *visinfo; |
| 982 |
|
| 983 |
visinfo = glXGetVisualFromFBConfig (dpy, configs[i]); |
| 984 |
if (visinfo == NULL) |
| 985 |
continue; |
| 986 |
|
| 987 |
if (visinfo->visualid != xvisual_id) |
| 988 |
{ |
| 989 |
XFree (visinfo); |
| 990 |
continue; |
| 991 |
} |
| 992 |
|
| 993 |
if (fb_config_out != NULL) |
| 994 |
*fb_config_out = configs[i]; |
| 995 |
|
| 996 |
XFree (visinfo); |
| 997 |
retval = TRUE; |
| 998 |
goto out; |
| 999 |
} |
| 1000 |
|
| 1001 |
g_set_error (error, GDK_GL_ERROR, |
| 1002 |
GDK_GL_ERROR_UNSUPPORTED_FORMAT, |
| 1003 |
_("No available configurations for the given RGBA pixel format")); |
| 1004 |
|
| 1005 |
out: |
| 1006 |
XFree (configs); |
| 1007 |
|
| 1008 |
return retval; |
| 1009 |
} |
| 1010 |
|
| 1011 |
struct glvisualinfo { |
| 1012 |
int supports_gl; |
| 1013 |
int double_buffer; |
| 1014 |
int stereo; |
| 1015 |
int alpha_size; |
| 1016 |
int depth_size; |
| 1017 |
int stencil_size; |
| 1018 |
int num_multisample; |
| 1019 |
int visual_caveat; |
| 1020 |
}; |
| 1021 |
|
| 1022 |
static gboolean |
| 1023 |
visual_compatible (const GdkVisual *a, const GdkVisual *b) |
| 1024 |
{ |
| 1025 |
return a->type == b->type && |
| 1026 |
a->depth == b->depth && |
| 1027 |
a->red_mask == b->red_mask && |
| 1028 |
a->green_mask == b->green_mask && |
| 1029 |
a->blue_mask == b->blue_mask && |
| 1030 |
a->colormap_size == b->colormap_size && |
| 1031 |
a->bits_per_rgb == b->bits_per_rgb; |
| 1032 |
} |
| 1033 |
|
| 1034 |
static gboolean |
| 1035 |
visual_is_rgba (const GdkVisual *visual) |
| 1036 |
{ |
| 1037 |
return |
| 1038 |
visual->depth == 32 && |
| 1039 |
visual->red_mask == 0xff0000 && |
| 1040 |
visual->green_mask == 0x00ff00 && |
| 1041 |
visual->blue_mask == 0x0000ff; |
| 1042 |
} |
| 1043 |
|
| 1044 |
/* This picks a compatible (as in has the same X visual details) visual |
| 1045 |
that has "better" characteristics on the GL side */ |
| 1046 |
static GdkVisual * |
| 1047 |
pick_better_visual_for_gl (GdkX11Screen *x11_screen, |
| 1048 |
struct glvisualinfo *gl_info, |
| 1049 |
GdkVisual *compatible) |
| 1050 |
{ |
| 1051 |
GdkVisual *visual; |
| 1052 |
int i; |
| 1053 |
gboolean want_alpha = visual_is_rgba (compatible); |
| 1054 |
|
| 1055 |
/* First look for "perfect match", i.e: |
| 1056 |
* supports gl |
| 1057 |
* double buffer |
| 1058 |
* alpha iff visual is an rgba visual |
| 1059 |
* no unnecessary stuff |
| 1060 |
*/ |
| 1061 |
for (i = 0; i < x11_screen->nvisuals; i++) |
| 1062 |
{ |
| 1063 |
visual = x11_screen->visuals[i]; |
| 1064 |
if (visual_compatible (visual, compatible) && |
| 1065 |
gl_info[i].supports_gl && |
| 1066 |
gl_info[i].double_buffer && |
| 1067 |
!gl_info[i].stereo && |
| 1068 |
(want_alpha ? (gl_info[i].alpha_size > 0) : (gl_info[i].alpha_size == 0)) && |
| 1069 |
(gl_info[i].depth_size == 0) && |
| 1070 |
(gl_info[i].stencil_size == 0) && |
| 1071 |
(gl_info[i].num_multisample == 0) && |
| 1072 |
(gl_info[i].visual_caveat == GLX_NONE_EXT)) |
| 1073 |
return visual; |
| 1074 |
} |
| 1075 |
|
| 1076 |
if (!want_alpha) |
| 1077 |
{ |
| 1078 |
/* Next, allow alpha even if we don't want it: */ |
| 1079 |
for (i = 0; i < x11_screen->nvisuals; i++) |
| 1080 |
{ |
| 1081 |
visual = x11_screen->visuals[i]; |
| 1082 |
if (visual_compatible (visual, compatible) && |
| 1083 |
gl_info[i].supports_gl && |
| 1084 |
gl_info[i].double_buffer && |
| 1085 |
!gl_info[i].stereo && |
| 1086 |
(gl_info[i].depth_size == 0) && |
| 1087 |
(gl_info[i].stencil_size == 0) && |
| 1088 |
(gl_info[i].num_multisample == 0) && |
| 1089 |
(gl_info[i].visual_caveat == GLX_NONE_EXT)) |
| 1090 |
return visual; |
| 1091 |
} |
| 1092 |
} |
| 1093 |
|
| 1094 |
/* Next, allow depth and stencil buffers: */ |
| 1095 |
for (i = 0; i < x11_screen->nvisuals; i++) |
| 1096 |
{ |
| 1097 |
visual = x11_screen->visuals[i]; |
| 1098 |
if (visual_compatible (visual, compatible) && |
| 1099 |
gl_info[i].supports_gl && |
| 1100 |
gl_info[i].double_buffer && |
| 1101 |
!gl_info[i].stereo && |
| 1102 |
(gl_info[i].num_multisample == 0) && |
| 1103 |
(gl_info[i].visual_caveat == GLX_NONE_EXT)) |
| 1104 |
return visual; |
| 1105 |
} |
| 1106 |
|
| 1107 |
/* Next, allow multisample: */ |
| 1108 |
for (i = 0; i < x11_screen->nvisuals; i++) |
| 1109 |
{ |
| 1110 |
visual = x11_screen->visuals[i]; |
| 1111 |
if (visual_compatible (visual, compatible) && |
| 1112 |
gl_info[i].supports_gl && |
| 1113 |
gl_info[i].double_buffer && |
| 1114 |
!gl_info[i].stereo && |
| 1115 |
(gl_info[i].visual_caveat == GLX_NONE_EXT)) |
| 1116 |
return visual; |
| 1117 |
} |
| 1118 |
|
| 1119 |
return compatible; |
| 1120 |
} |
| 1121 |
|
| 1122 |
static gboolean |
| 1123 |
get_cached_gl_visuals (GdkDisplay *display, int *system, int *rgba) |
| 1124 |
{ |
| 1125 |
gboolean found; |
| 1126 |
Atom type_return; |
| 1127 |
gint format_return; |
| 1128 |
gulong nitems_return; |
| 1129 |
gulong bytes_after_return; |
| 1130 |
guchar *data = NULL; |
| 1131 |
Display *dpy; |
| 1132 |
|
| 1133 |
dpy = gdk_x11_display_get_xdisplay (display); |
| 1134 |
|
| 1135 |
found = FALSE; |
| 1136 |
|
| 1137 |
gdk_x11_display_error_trap_push (display); |
| 1138 |
if (XGetWindowProperty (dpy, DefaultRootWindow (dpy), |
| 1139 |
gdk_x11_get_xatom_by_name_for_display (display, "GDK_VISUALS"), |
| 1140 |
0, 2, False, XA_INTEGER, &type_return, |
| 1141 |
&format_return, &nitems_return, |
| 1142 |
&bytes_after_return, &data) == Success) |
| 1143 |
{ |
| 1144 |
if (type_return == XA_INTEGER && |
| 1145 |
format_return == 32 && |
| 1146 |
nitems_return == 2 && |
| 1147 |
data != NULL) |
| 1148 |
{ |
| 1149 |
long *visuals = (long *) data; |
| 1150 |
|
| 1151 |
*system = (int)visuals[0]; |
| 1152 |
*rgba = (int)visuals[1]; |
| 1153 |
found = TRUE; |
| 1154 |
} |
| 1155 |
} |
| 1156 |
gdk_x11_display_error_trap_pop_ignored (display); |
| 1157 |
|
| 1158 |
if (data) |
| 1159 |
XFree (data); |
| 1160 |
|
| 1161 |
return found; |
| 1162 |
} |
| 1163 |
|
| 1164 |
static void |
| 1165 |
save_cached_gl_visuals (GdkDisplay *display, int system, int rgba) |
| 1166 |
{ |
| 1167 |
long visualdata[2]; |
| 1168 |
Display *dpy; |
| 1169 |
|
| 1170 |
dpy = gdk_x11_display_get_xdisplay (display); |
| 1171 |
|
| 1172 |
visualdata[0] = system; |
| 1173 |
visualdata[1] = rgba; |
| 1174 |
|
| 1175 |
gdk_x11_display_error_trap_push (display); |
| 1176 |
XChangeProperty (dpy, DefaultRootWindow (dpy), |
| 1177 |
gdk_x11_get_xatom_by_name_for_display (display, "GDK_VISUALS"), |
| 1178 |
XA_INTEGER, 32, PropModeReplace, |
| 1179 |
(unsigned char *)visualdata, 2); |
| 1180 |
gdk_x11_display_error_trap_pop_ignored (display); |
| 1181 |
} |
| 1182 |
|
| 1183 |
void |
| 1184 |
_gdk_x11_screen_update_visuals_for_gl (GdkScreen *screen) |
| 1185 |
{ |
| 1186 |
GdkX11Screen *x11_screen; |
| 1187 |
GdkDisplay *display; |
| 1188 |
GdkX11Display *display_x11; |
| 1189 |
Display *dpy; |
| 1190 |
struct glvisualinfo *gl_info; |
| 1191 |
int i; |
| 1192 |
int system_visual_id, rgba_visual_id; |
| 1193 |
|
| 1194 |
x11_screen = GDK_X11_SCREEN (screen); |
| 1195 |
display = x11_screen->display; |
| 1196 |
display_x11 = GDK_X11_DISPLAY (display); |
| 1197 |
dpy = gdk_x11_display_get_xdisplay (display); |
| 1198 |
|
| 1199 |
/* We save the default visuals as a property on the root window to avoid |
| 1200 |
having to initialize GL each time, as it may not be used later. */ |
| 1201 |
if (get_cached_gl_visuals (display, &system_visual_id, &rgba_visual_id)) |
| 1202 |
{ |
| 1203 |
for (i = 0; i < x11_screen->nvisuals; i++) |
| 1204 |
{ |
| 1205 |
GdkVisual *visual = x11_screen->visuals[i]; |
| 1206 |
int visual_id = gdk_x11_visual_get_xvisual (visual)->visualid; |
| 1207 |
|
| 1208 |
if (visual_id == system_visual_id) |
| 1209 |
x11_screen->system_visual = visual; |
| 1210 |
if (visual_id == rgba_visual_id) |
| 1211 |
x11_screen->rgba_visual = visual; |
| 1212 |
} |
| 1213 |
|
| 1214 |
return; |
| 1215 |
} |
| 1216 |
|
| 1217 |
if (!gdk_x11_screen_init_gl (screen)) |
| 1218 |
return; |
| 1219 |
|
| 1220 |
gl_info = g_new0 (struct glvisualinfo, x11_screen->nvisuals); |
| 1221 |
|
| 1222 |
for (i = 0; i < x11_screen->nvisuals; i++) |
| 1223 |
{ |
| 1224 |
XVisualInfo *visual_list; |
| 1225 |
XVisualInfo visual_template; |
| 1226 |
int nxvisuals; |
| 1227 |
|
| 1228 |
visual_template.screen = x11_screen->screen_num; |
| 1229 |
visual_template.visualid = gdk_x11_visual_get_xvisual (x11_screen->visuals[i])->visualid; |
| 1230 |
visual_list = XGetVisualInfo (x11_screen->xdisplay, VisualIDMask| VisualScreenMask, &visual_template, &nxvisuals); |
| 1231 |
|
| 1232 |
if (visual_list == NULL) |
| 1233 |
continue; |
| 1234 |
|
| 1235 |
glXGetConfig (dpy, &visual_list[0], GLX_USE_GL, &gl_info[i].supports_gl); |
| 1236 |
glXGetConfig (dpy, &visual_list[0], GLX_DOUBLEBUFFER, &gl_info[i].double_buffer); |
| 1237 |
glXGetConfig (dpy, &visual_list[0], GLX_STEREO, &gl_info[i].stereo); |
| 1238 |
glXGetConfig (dpy, &visual_list[0], GLX_ALPHA_SIZE, &gl_info[i].alpha_size); |
| 1239 |
glXGetConfig (dpy, &visual_list[0], GLX_DEPTH_SIZE, &gl_info[i].depth_size); |
| 1240 |
glXGetConfig (dpy, &visual_list[0], GLX_STENCIL_SIZE, &gl_info[i].stencil_size); |
| 1241 |
|
| 1242 |
if (display_x11->has_glx_multisample) |
| 1243 |
glXGetConfig(dpy, &visual_list[0], GLX_SAMPLE_BUFFERS_ARB, &gl_info[i].num_multisample); |
| 1244 |
|
| 1245 |
if (display_x11->has_glx_visual_rating) |
| 1246 |
glXGetConfig(dpy, &visual_list[0], GLX_VISUAL_CAVEAT_EXT, &gl_info[i].visual_caveat); |
| 1247 |
else |
| 1248 |
gl_info[i].visual_caveat = GLX_NONE_EXT; |
| 1249 |
|
| 1250 |
XFree (visual_list); |
| 1251 |
} |
| 1252 |
|
| 1253 |
x11_screen->system_visual = pick_better_visual_for_gl (x11_screen, gl_info, x11_screen->system_visual); |
| 1254 |
if (x11_screen->rgba_visual) |
| 1255 |
x11_screen->rgba_visual = pick_better_visual_for_gl (x11_screen, gl_info, x11_screen->rgba_visual); |
| 1256 |
|
| 1257 |
g_free (gl_info); |
| 1258 |
|
| 1259 |
save_cached_gl_visuals (display, |
| 1260 |
gdk_x11_visual_get_xvisual (x11_screen->system_visual)->visualid, |
| 1261 |
x11_screen->rgba_visual ? gdk_x11_visual_get_xvisual (x11_screen->rgba_visual)->visualid : 0); |
| 1262 |
} |
| 1263 |
|
| 1264 |
GdkGLContext * |
| 1265 |
gdk_x11_window_create_gl_context (GdkWindow *window, |
| 1266 |
gboolean attached, |
| 1267 |
GdkGLContext *share, |
| 1268 |
GError **error) |
| 1269 |
{ |
| 1270 |
GdkDisplay *display; |
| 1271 |
GdkX11GLContext *context; |
| 1272 |
GdkVisual *visual; |
| 1273 |
GLXFBConfig config; |
| 1274 |
|
| 1275 |
display = gdk_window_get_display (window); |
| 1276 |
|
| 1277 |
if (!gdk_x11_screen_init_gl (gdk_window_get_screen (window))) |
| 1278 |
{ |
| 1279 |
g_set_error_literal (error, GDK_GL_ERROR, |
| 1280 |
GDK_GL_ERROR_NOT_AVAILABLE, |
| 1281 |
_("No GL implementation is available")); |
| 1282 |
return NULL; |
| 1283 |
} |
| 1284 |
|
| 1285 |
visual = gdk_window_get_visual (window); |
| 1286 |
if (!find_fbconfig_for_visual (display, visual, &config, error)) |
| 1287 |
return NULL; |
| 1288 |
|
| 1289 |
context = g_object_new (GDK_TYPE_X11_GL_CONTEXT, |
| 1290 |
"display", display, |
| 1291 |
"window", window, |
| 1292 |
"shared-context", share, |
| 1293 |
NULL); |
| 1294 |
|
| 1295 |
context->glx_config = config; |
| 1296 |
context->is_attached = attached; |
| 1297 |
|
| 1298 |
return GDK_GL_CONTEXT (context); |
| 1299 |
} |
| 1300 |
|
| 1301 |
gboolean |
| 1302 |
gdk_x11_display_is_gl_context_current (GdkDisplay *display, |
| 1303 |
GdkGLContext *context) |
| 1304 |
{ |
| 1305 |
GdkX11GLContext *context_x11 = GDK_X11_GL_CONTEXT (context); |
| 1306 |
|
| 1307 |
return context_x11->glx_context == glXGetCurrentContext (); |
| 1308 |
} |
| 1309 |
|
| 1310 |
gboolean |
| 1311 |
gdk_x11_display_make_gl_context_current (GdkDisplay *display, |
| 1312 |
GdkGLContext *context) |
| 1313 |
{ |
| 1314 |
GdkX11GLContext *context_x11; |
| 1315 |
Display *dpy = gdk_x11_display_get_xdisplay (display); |
| 1316 |
GdkWindow *window; |
| 1317 |
GdkScreen *screen; |
| 1318 |
gboolean do_frame_sync = FALSE; |
| 1319 |
|
| 1320 |
if (context == NULL) |
| 1321 |
{ |
| 1322 |
glXMakeContextCurrent (dpy, None, None, NULL); |
| 1323 |
return TRUE; |
| 1324 |
} |
| 1325 |
|
| 1326 |
context_x11 = GDK_X11_GL_CONTEXT (context); |
| 1327 |
if (context_x11->glx_context == NULL) |
| 1328 |
{ |
| 1329 |
g_critical ("No GLX context associated to the GdkGLContext; you must " |
| 1330 |
"call gdk_gl_context_realize() first."); |
| 1331 |
return FALSE; |
| 1332 |
} |
| 1333 |
|
| 1334 |
GDK_NOTE (OPENGL, |
| 1335 |
g_message ("Making GLX context current to drawable %lu", |
| 1336 |
(unsigned long) context_x11->drawable)); |
| 1337 |
|
| 1338 |
if (!glXMakeContextCurrent (dpy, context_x11->drawable, context_x11->drawable, |
| 1339 |
context_x11->glx_context)) |
| 1340 |
{ |
| 1341 |
GDK_NOTE (OPENGL, |
| 1342 |
g_message ("Making GLX context current failed")); |
| 1343 |
return FALSE; |
| 1344 |
} |
| 1345 |
|
| 1346 |
if (context_x11->is_attached && GDK_X11_DISPLAY (display)->has_glx_swap_interval) |
| 1347 |
{ |
| 1348 |
window = gdk_gl_context_get_window (context); |
| 1349 |
|
| 1350 |
/* If the WM is compositing there is no particular need to delay |
| 1351 |
* the swap when drawing on the offscreen, rendering to the screen |
| 1352 |
* happens later anyway, and its up to the compositor to sync that |
| 1353 |
* to the vblank. */ |
| 1354 |
screen = gdk_window_get_screen (window); |
| 1355 |
do_frame_sync = ! gdk_screen_is_composited (screen); |
| 1356 |
|
| 1357 |
if (do_frame_sync != context_x11->do_frame_sync) |
| 1358 |
{ |
| 1359 |
context_x11->do_frame_sync = do_frame_sync; |
| 1360 |
|
| 1361 |
if (do_frame_sync) |
| 1362 |
glXSwapIntervalSGI (1); |
| 1363 |
else |
| 1364 |
glXSwapIntervalSGI (0); |
| 1365 |
} |
| 1366 |
} |
| 1367 |
|
| 1368 |
return TRUE; |
| 1369 |
} |
| 1370 |
|
| 1371 |
/** |
| 1372 |
* gdk_x11_display_get_glx_version: |
| 1373 |
* @display: a #GdkDisplay |
| 1374 |
* @major: (out): return location for the GLX major version |
| 1375 |
* @minor: (out): return location for the GLX minor version |
| 1376 |
* |
| 1377 |
* Retrieves the version of the GLX implementation. |
| 1378 |
* |
| 1379 |
* Returns: %TRUE if GLX is available |
| 1380 |
* |
| 1381 |
* Since: 3.16 |
| 1382 |
*/ |
| 1383 |
gboolean |
| 1384 |
gdk_x11_display_get_glx_version (GdkDisplay *display, |
| 1385 |
gint *major, |
| 1386 |
gint *minor) |
| 1387 |
{ |
| 1388 |
g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE); |
| 1389 |
|
| 1390 |
if (!GDK_IS_X11_DISPLAY (display)) |
| 1391 |
return FALSE; |
| 1392 |
|
| 1393 |
if (!gdk_x11_screen_init_gl (gdk_display_get_default_screen (display))) |
| 1394 |
return FALSE; |
| 1395 |
|
| 1396 |
if (major != NULL) |
| 1397 |
*major = GDK_X11_DISPLAY (display)->glx_version / 10; |
| 1398 |
if (minor != NULL) |
| 1399 |
*minor = GDK_X11_DISPLAY (display)->glx_version % 10; |
| 1400 |
|
| 1401 |
return TRUE; |
| 1402 |
} |