summaryrefslogtreecommitdiff
path: root/gdk/x11/gdkglcontext-x11.c
blob: 2e0dc5efd1f9c01b256bb1a7dc0572f8daf6862f (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
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
/* GDK - The GIMP Drawing Kit
 *
 * gdkglcontext-x11.c: X11 specific OpenGL wrappers
 * 
 * Copyright © 2014  Emmanuele Bassi
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "gdkglcontext-x11.h"
#include "gdkdisplay-x11.h"
#include "gdkscreen-x11.h"

#include "gdkx11display.h"
#include "gdkx11glcontext.h"
#include "gdkx11screen.h"
#include "gdkx11window.h"
#include "gdkx11visual.h"

#include "gdkinternals.h"

#include "gdkintl.h"

#include <GL/glx.h>

G_DEFINE_TYPE (GdkX11GLContext, gdk_x11_gl_context, GDK_TYPE_GL_CONTEXT)

typedef struct {
  GLXDrawable drawable;

  GdkDisplay *display;
  GdkGLContext *context;
  GdkWindow *window;

  guint32 last_frame_counter;
} DrawableInfo;

static void
drawable_info_free (gpointer data_)
{
  DrawableInfo *data = data_;

  glXDestroyWindow (gdk_x11_display_get_xdisplay (data->display), data->drawable);

  g_slice_free (DrawableInfo, data);
}

static DrawableInfo *
get_glx_drawable_info (GdkWindow *window)
{
  return g_object_get_data (G_OBJECT (window), "-gdk-x11-window-glx-info");
}

static void
set_glx_drawable_info (GdkWindow    *window,
                       DrawableInfo *info)
{
  g_object_set_data_full (G_OBJECT (window), "-gdk-x11-window-glx-info",
                          info,
                          drawable_info_free);
}

static void
gdk_x11_gl_context_set_window (GdkGLContext *context,
                               GdkWindow    *window)
{
  GdkX11GLContext *context_x11 = GDK_X11_GL_CONTEXT (context);
  GdkDisplay *display = gdk_gl_context_get_display (context);
  DrawableInfo *info;

  if (window == NULL)
    {
      gdk_x11_display_make_gl_context_current (display, context, NULL);
      return;
    }

  /* we need to make sure that the GdkWindow is backed by
   * an actual native surface
   */
  gdk_window_ensure_native (window);

  /* GLX < 1.3 accepts X11 drawables, so there's no need to
   * go through the creation of a GLX drawable
   */
  if (GDK_X11_DISPLAY (display)->glx_version < 13)
    return;

  info = get_glx_drawable_info (window);
  if (info != NULL)
    return;

  gdk_x11_display_error_trap_push (display);

  info = g_slice_new (DrawableInfo);
  info->window = window;
  info->context = context;
  info->display = display;
  info->drawable = glXCreateWindow (gdk_x11_display_get_xdisplay (display),
                                    context_x11->glx_config,
                                    gdk_x11_window_get_xid (window),
                                    NULL);
  info->last_frame_counter = 0;

  gdk_x11_display_error_trap_pop_ignored (display);

  set_glx_drawable_info (window, info);
}

static void
gdk_x11_gl_context_update_viewport (GdkGLContext *context,
                                    GdkWindow    *window,
                                    int           width,
                                    int           height)
{
  GDK_NOTE (OPENGL, g_print ("Updating viewport to { 0, 0, %d, %d }\n", width, height));

  glViewport (0, 0, width, height);
}

static void
maybe_wait_for_vblank (GdkDisplay  *display,
                       GLXDrawable  drawable)
{
  GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
  Display *dpy = gdk_x11_display_get_xdisplay (display);

  if (display_x11->has_glx_sync_control)
    {
      gint64 ust, msc, sbc;

      glXGetSyncValuesOML (dpy, drawable, &ust, &msc, &sbc);
      glXWaitForMscOML (dpy, drawable,
                        0, 2, (msc + 1) % 2,
                        &ust, &msc, &sbc);
    }
  else if (display_x11->has_glx_video_sync)
    {
      guint32 current_count;

      glXGetVideoSyncSGI (&current_count);
      glXWaitVideoSyncSGI (2, (current_count + 1) % 2, &current_count);
    }
}

static void
gdk_x11_gl_context_flush_buffer (GdkGLContext *context)
{
  GdkDisplay *display = gdk_gl_context_get_display (context);
  GdkWindow *window = gdk_gl_context_get_window (context);
  Display *dpy = gdk_x11_display_get_xdisplay (display);
  GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
  DrawableInfo *info;
  GLXDrawable drawable;

  if (window == NULL)
    return;

  info = get_glx_drawable_info (window);
  if (info != NULL && info->drawable != None)
    drawable = info->drawable;
  else
    drawable = gdk_x11_window_get_xid (window);

  GDK_NOTE (OPENGL,
            g_print ("Flushing GLX buffers for %s drawable %lu (window: %lu)\n",
                     drawable == info->drawable ? "GLX" : "X11",
                     (unsigned long) drawable,
                     (unsigned long) gdk_x11_window_get_xid (window)));

  /* if we are going to wait for the vertical refresh manually
   * we need to flush pending redraws, and we also need to wait
   * for that to finish, otherwise we are going to tear.
   *
   * obviously, this condition should not be hit if we have
   * GLX_SGI_swap_control, and we ask the driver to do the right
   * thing.
   */
  {
    guint32 end_frame_counter = 0;
    gboolean has_counter = display_x11->has_glx_video_sync;
    gboolean can_wait = display_x11->has_glx_video_sync ||
                        display_x11->has_glx_sync_control;

    if (display_x11->has_glx_video_sync)
      glXGetVideoSyncSGI (&end_frame_counter);

    if (!display_x11->has_glx_swap_interval)
      {
        glFinish ();

        if (has_counter && can_wait)
          {
            guint32 last_counter = info != NULL ? info->last_frame_counter : 0;

            if (last_counter == end_frame_counter)
              maybe_wait_for_vblank (display, drawable);
          }
        else if (can_wait)
          maybe_wait_for_vblank (display, drawable);
      }
  }

  glXSwapBuffers (dpy, drawable);

  if (info != NULL && display_x11->has_glx_video_sync)
    glXGetVideoSyncSGI (&info->last_frame_counter);
}

static void
gdk_x11_gl_context_class_init (GdkX11GLContextClass *klass)
{
  GdkGLContextClass *context_class = GDK_GL_CONTEXT_CLASS (klass);

  context_class->set_window = gdk_x11_gl_context_set_window;
  context_class->update_viewport = gdk_x11_gl_context_update_viewport;
  context_class->flush_buffer = gdk_x11_gl_context_flush_buffer;
}

static void
gdk_x11_gl_context_init (GdkX11GLContext *self)
{
}

gboolean
gdk_x11_display_init_gl (GdkDisplay *display)
{
  GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
  GdkScreen *screen;
  Display *dpy;
  int error_base, event_base;
  int screen_num;

  if (display_x11->have_glx)
    return TRUE;

  dpy = gdk_x11_display_get_xdisplay (display);

  if (!glXQueryExtension (dpy, &error_base, &event_base))
    return FALSE;

  screen = gdk_display_get_default_screen (display);
  screen_num = GDK_X11_SCREEN (screen)->screen_num;

  display_x11->have_glx = TRUE;

  display_x11->glx_version = epoxy_glx_version (dpy, screen_num);
  display_x11->glx_error_base = error_base;
  display_x11->glx_event_base = event_base;

  display_x11->has_glx_create_context =
    epoxy_has_glx_extension (dpy, screen_num, "GLX_ARB_create_context_profile");
  display_x11->has_glx_swap_interval =
    epoxy_has_glx_extension (dpy, screen_num, "GLX_SGI_swap_control");
  display_x11->has_glx_texture_from_pixmap =
    epoxy_has_glx_extension (dpy, screen_num, "GLX_EXT_texture_from_pixmap");
  display_x11->has_glx_video_sync =
    epoxy_has_glx_extension (dpy, screen_num, "GLX_SGI_video_sync");
  display_x11->has_glx_buffer_age =
    epoxy_has_glx_extension (dpy, screen_num, "GLX_EXT_buffer_age");
  display_x11->has_glx_sync_control =
    epoxy_has_glx_extension (dpy, screen_num, "GLX_OML_sync_control");

  GDK_NOTE (OPENGL,
            g_print ("GLX version %d.%d found\n"
                     " - Vendor: %s\n"
                     " - Checked extensions:\n"
                     "\t* GLX_ARB_create_context_profile: %s\n"
                     "\t* GLX_SGI_swap_control: %s\n"
                     "\t* GLX_EXT_texture_from_pixmap: %s\n"
                     "\t* GLX_SGI_video_sync: %s\n"
                     "\t* GLX_EXT_buffer_age: %s\n"
                     "\t* GLX_OML_sync_control: %s\n",
                     display_x11->glx_version / 10,
                     display_x11->glx_version % 10,
                     glXGetClientString (dpy, GLX_VENDOR),
                     display_x11->has_glx_create_context ? "yes" : "no",
                     display_x11->has_glx_swap_interval ? "yes" : "no",
                     display_x11->has_glx_texture_from_pixmap ? "yes" : "no",
                     display_x11->has_glx_video_sync ? "yes" : "no",
                     display_x11->has_glx_buffer_age ? "yes" : "no",
                     display_x11->has_glx_sync_control ? "yes" : "no"));

  return TRUE;
}

#define MAX_GLX_ATTRS   30

static void
get_glx_attributes_for_pixel_format (GdkDisplay       *display,
                                     GdkGLPixelFormat *format,
                                     int              *attrs)
{
  GdkX11Display *display_x11;
  int i = 0;

  attrs[i++] = GLX_DRAWABLE_TYPE;
  attrs[i++] = GLX_WINDOW_BIT;

  attrs[i++] = GLX_RENDER_TYPE;
  attrs[i++] = GLX_RGBA_BIT;

  if (format->double_buffer)
    {
      attrs[i++] = GLX_DOUBLEBUFFER;
      attrs[i++] = GL_TRUE;
    }

  if (format->stereo)
    {
      attrs[i++] = GLX_STEREO;
      attrs[i++] = GL_TRUE;
    }

  if (format->color_size < 0)
    {
      attrs[i++] = GLX_RED_SIZE;
      attrs[i++] = 1;
      attrs[i++] = GLX_GREEN_SIZE;
      attrs[i++] = 1;
      attrs[i++] = GLX_BLUE_SIZE;
      attrs[i++] = 1;
    }
  else
    {
      attrs[i++] = GLX_RED_SIZE;
      attrs[i++] = format->color_size;
      attrs[i++] = GLX_GREEN_SIZE;
      attrs[i++] = format->color_size;
      attrs[i++] = GLX_BLUE_SIZE;
      attrs[i++] = format->color_size;
    }

  if (format->alpha_size < 0)
    {
      attrs[i++] = GLX_ALPHA_SIZE;
      attrs[i++] = 1;
    }
  else if (format->alpha_size == 0)
    {
      attrs[i++] = GLX_ALPHA_SIZE;
      attrs[i++] = GLX_DONT_CARE;
    }
  else
    {
      attrs[i++] = GLX_ALPHA_SIZE;
      attrs[i++] = format->alpha_size;
    }

  if (format->depth_size < 0)
    {
      attrs[i++] = GLX_DEPTH_SIZE;
      attrs[i++] = 1;
    }
  else
    {
      attrs[i++] = GLX_DEPTH_SIZE;
      attrs[i++] = format->depth_size;
    }

  if (format->stencil_size < 0)
    {
      attrs[i++] = GLX_STENCIL_SIZE;
      attrs[i++] = GLX_DONT_CARE;
    }
  else
    {
      attrs[i++] = GLX_STENCIL_SIZE;
      attrs[i++] = format->stencil_size;
    }

  if (format->accum_size > 0)
    {
      attrs[i++] = GLX_ACCUM_RED_SIZE;
      attrs[i++] = format->accum_size;
      attrs[i++] = GLX_ACCUM_GREEN_SIZE;
      attrs[i++] = format->accum_size;
      attrs[i++] = GLX_ACCUM_BLUE_SIZE;
      attrs[i++] = format->accum_size;
      attrs[i++] = GLX_ACCUM_ALPHA_SIZE;
      attrs[i++] = format->accum_size;
    }

  display_x11 = GDK_X11_DISPLAY (display);
  if (display_x11->glx_version >= 14 && format->multi_sample)
    {
      attrs[i++] = GLX_SAMPLE_BUFFERS;
      attrs[i++] = format->sample_buffers > 0 ? format->sample_buffers : 1;

      attrs[i++] = GLX_SAMPLES;
      attrs[i++] = format->samples > 0 ? format->samples : 1;
    }

  attrs[i++] = None;

  g_assert (i < MAX_GLX_ATTRS);
}

static gboolean
find_fbconfig_for_pixel_format (GdkDisplay        *display,
                                GdkGLPixelFormat  *format,
                                GLXFBConfig       *fb_config_out,
                                XVisualInfo      **visinfo_out,
                                GError           **error)
{
  static int attrs[MAX_GLX_ATTRS];

  Display *dpy = gdk_x11_display_get_xdisplay (display);
  GLXFBConfig *configs;
  int n_configs, i;
  gboolean use_rgba;
  gboolean retval = FALSE;

  get_glx_attributes_for_pixel_format (display, format, attrs);

  use_rgba = format->alpha_size != 0;

  configs = glXChooseFBConfig (dpy, DefaultScreen (dpy), attrs, &n_configs);
  if (configs == NULL || n_configs == 0)
    {
      g_set_error_literal (error, GDK_GL_PIXEL_FORMAT_ERROR,
                           GDK_GL_PIXEL_FORMAT_ERROR_NOT_AVAILABLE,
                           _("No available configurations for the given pixel format"));
      return FALSE;
    }

  /* if we don't care about an alpha channel, then the first
   * valid configuration is the one we give back
   */
  if (!use_rgba)
    {
      if (fb_config_out != NULL)
        *fb_config_out = configs[0];

      if (visinfo_out != NULL)
        *visinfo_out = glXGetVisualFromFBConfig (dpy, configs[0]);

      retval = TRUE;
      goto out;
    }

  for (i = 0; i < n_configs; i++)
    {
      XVisualInfo *visinfo;
      unsigned long mask;

      visinfo = glXGetVisualFromFBConfig (dpy, configs[i]);
      if (visinfo == NULL)
        continue;

      mask = visinfo->red_mask | visinfo->green_mask | visinfo->blue_mask;
      if (visinfo->depth == 32 && mask != 0xffffffff)
        {
          if (fb_config_out != NULL)
            *fb_config_out = configs[i];

          if (visinfo_out != NULL)
            *visinfo_out = visinfo;

          retval = TRUE;
          goto out;
        }

      XFree (visinfo);
    }
    
  g_set_error (error, GDK_GL_PIXEL_FORMAT_ERROR,
               GDK_GL_PIXEL_FORMAT_ERROR_NOT_AVAILABLE,
               _("No available configurations for the given RGBA pixel format"));

out:
  XFree (configs);

  return retval;
}

static void
update_pixel_format (GdkDisplay       *display,
                     GdkGLPixelFormat *format,
                     GLXFBConfig       config)
{
  Display *dpy = gdk_x11_display_get_xdisplay (display);
  int value = 0;

  glXGetFBConfigAttrib (dpy, config, GLX_DOUBLEBUFFER, &format->double_buffer);

  glXGetFBConfigAttrib (dpy, config, GLX_RED_SIZE, &value);
  format->color_size = value;
  glXGetFBConfigAttrib (dpy, config, GLX_GREEN_SIZE, &value);
  format->color_size = MIN (format->color_size, value);
  glXGetFBConfigAttrib (dpy, config, GLX_BLUE_SIZE, &value);
  format->color_size = MIN (format->color_size, value);

  glXGetFBConfigAttrib (dpy, config, GLX_ALPHA_SIZE, &format->alpha_size);

  glXGetFBConfigAttrib (dpy, config, GLX_AUX_BUFFERS, &format->aux_buffers);
  glXGetFBConfigAttrib (dpy, config, GLX_DEPTH_SIZE, &format->depth_size);
  glXGetFBConfigAttrib (dpy, config, GLX_STENCIL_SIZE, &format->stencil_size);

  glXGetFBConfigAttrib (dpy, config, GLX_ACCUM_RED_SIZE, &value);
  format->accum_size = value;
  glXGetFBConfigAttrib (dpy, config, GLX_ACCUM_GREEN_SIZE, &value);
  format->accum_size = MIN (format->accum_size, value);
  glXGetFBConfigAttrib (dpy, config, GLX_ACCUM_BLUE_SIZE, &value);
  format->accum_size = MIN (format->accum_size, value);
  glXGetFBConfigAttrib (dpy, config, GLX_ACCUM_ALPHA_SIZE, &value);
  format->accum_size = MIN (format->accum_size, value);
}

static GLXContext
create_gl3_context (GdkDisplay   *display,
                    GLXFBConfig   config,
                    GdkGLContext *share)
{
  static const int attrib_list[] = {
    GLX_CONTEXT_PROFILE_MASK_ARB,  GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
    GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
    GLX_CONTEXT_MAJOR_VERSION_ARB, 2,
    None,
  };

  GdkX11GLContext *context_x11 = NULL;

  if (share != NULL)
    context_x11 = GDK_X11_GL_CONTEXT (share);

  return glXCreateContextAttribsARB (gdk_x11_display_get_xdisplay (display),
                                     config,
                                     context_x11 != NULL ? context_x11->glx_context : NULL,
                                     True,
                                     attrib_list);
}

static GLXContext
create_gl_context (GdkDisplay   *display,
                   GLXFBConfig   config,
                   GdkGLContext *share)
{
  GdkX11GLContext *context_x11 = NULL;

  if (share != NULL)
    context_x11 = GDK_X11_GL_CONTEXT (share);

  return glXCreateNewContext (gdk_x11_display_get_xdisplay (display),
                              config,
                              GLX_RGBA_TYPE,
                              context_x11 != NULL ? context_x11->glx_context : NULL,
                              True);
}

GdkGLContext *
gdk_x11_display_create_gl_context (GdkDisplay        *display,
                                   GdkGLPixelFormat  *format,
                                   GdkGLContext      *share,
                                   GError           **error)
{
  GdkGLPixelFormat *valid_format;
  GdkX11GLContext *context;
  GdkVisual *gdk_visual;
  GLXFBConfig config;
  GLXContext glx_context;
  Window dummy_xwin;
  GLXWindow dummy_glx;
  GLXWindow dummy_drawable;
  gboolean is_direct;
  XVisualInfo *xvisinfo;
  XSetWindowAttributes attrs;
  unsigned long mask;
  Display *dpy;

  if (!gdk_x11_display_validate_gl_pixel_format (display, format, NULL, error))
    return NULL;

  /* if validation succeeded, then we don't need to check for the
   * result here: we know the pixel format has a valid GLXFBConfig
   */
  find_fbconfig_for_pixel_format (display, format, &config, &xvisinfo, NULL);

  dpy = gdk_x11_display_get_xdisplay (display);

  /* we check for the GLX_ARB_create_context_profile extension
   * while validating the PixelFormat.
   */
  if (format->profile == GDK_GL_PIXEL_FORMAT_PROFILE_3_2_CORE)
    glx_context = create_gl3_context (display, config, share);
  else
    {
      /* GDK_GL_PIXEL_FORMAT_PROFILE_DEFAULT is currently
       * equivalent to the LEGACY profile
       */
      glx_context = create_gl_context (display, config, share);
    }

  if (glx_context == NULL)
    {
      g_set_error_literal (error, GDK_GL_PIXEL_FORMAT_ERROR,
                           GDK_GL_PIXEL_FORMAT_ERROR_NOT_AVAILABLE,
                           _("Unable to create a GL context"));
      return NULL;
    }

  is_direct = glXIsDirect (dpy, glx_context);

  gdk_x11_display_error_trap_push (display);

  /* create a dummy window; this is needed because GLX does not allow
   * us to query the context until it's bound to a drawable; we simply
   * create a small OR window, put it off screen, and never map it. in
   * order to keep the GL machinery in a sane state, we always make
   * the dummy window the current drawable if the user unsets the
   * GdkWindow bound to the GdkGLContext.
   */
  attrs.override_redirect = True;
  attrs.colormap = XCreateColormap (dpy, DefaultRootWindow (dpy), xvisinfo->visual, AllocNone);
  attrs.border_pixel = 0;
  mask = CWOverrideRedirect | CWColormap | CWBorderPixel;

  dummy_xwin = XCreateWindow (dpy, DefaultRootWindow (dpy),
                              -100, -100, 1, 1,
                              0,
                              xvisinfo->depth,
                              CopyFromParent,
                              xvisinfo->visual,
                              mask,
                              &attrs);

  /* GLX API introduced in 1.3 expects GLX drawables */
  if (GDK_X11_DISPLAY (display)->glx_version >= 13)
    dummy_glx = glXCreateWindow (dpy, config, dummy_xwin, NULL);
  else
    dummy_glx = None;

  dummy_drawable = dummy_glx != None
                 ? dummy_glx
                 : dummy_xwin;

  glXMakeContextCurrent (dpy, dummy_drawable, dummy_drawable, glx_context);

  gdk_visual = gdk_x11_screen_lookup_visual (gdk_display_get_default_screen (display),
                                             xvisinfo->visualid);

  XFree (xvisinfo);

  if (gdk_x11_display_error_trap_pop (display))
    {
      g_set_error_literal (error, GDK_GL_PIXEL_FORMAT_ERROR,
                           GDK_GL_PIXEL_FORMAT_ERROR_NOT_AVAILABLE,
                           _("Unable to create a GL context"));

      glXDestroyContext (dpy, glx_context);

      if (dummy_xwin)
        XDestroyWindow (dpy, dummy_xwin);
      if (dummy_glx)
        glXDestroyWindow (dpy, dummy_glx);

      return NULL;
    }

  GDK_NOTE (OPENGL,
            g_print ("Created GLX context[%p], %s, dummy drawable: %lu\n",
                     glx_context,
                     is_direct ? "direct" : "indirect",
                     (unsigned long) dummy_xwin));

  /* the GdkGLContext holds a reference on the pixel format
   * that is used to create it, not the one that the user
   * passed; this allows the user to query the pixel format
   * attributes
   */
  valid_format = g_object_new (GDK_TYPE_GL_PIXEL_FORMAT, NULL);
  update_pixel_format (display, valid_format, config);

  context = g_object_new (GDK_X11_TYPE_GL_CONTEXT,
                          "display", display,
                          "pixel-format", valid_format,
                          "visual", gdk_visual,
                          NULL);

  context->glx_config = config;
  context->glx_context = glx_context;
  context->dummy_drawable = dummy_xwin;
  context->dummy_glx_drawable = dummy_glx;
  context->current_drawable = dummy_drawable;
  context->is_direct = is_direct;

  return GDK_GL_CONTEXT (context);
}

void
gdk_x11_display_destroy_gl_context (GdkDisplay   *display,
                                    GdkGLContext *context)
{
  GdkX11GLContext *context_x11 = GDK_X11_GL_CONTEXT (context);
  Display *dpy = gdk_x11_display_get_xdisplay (display);

  if (context_x11->glx_context != NULL)
    {
      if (glXGetCurrentContext () == context_x11->glx_context)
        glXMakeContextCurrent (dpy, None, None, NULL);

      GDK_NOTE (OPENGL, g_print ("Destroying GLX context\n"));
      glXDestroyContext (dpy, context_x11->glx_context);
      context_x11->glx_context = NULL;
    }

  if (context_x11->dummy_glx_drawable)
    {
      GDK_NOTE (OPENGL, g_print ("Destroying dummy GLX drawable\n"));
      glXDestroyWindow (dpy, context_x11->dummy_glx_drawable);
      context_x11->dummy_glx_drawable = None;
    }

  if (context_x11->dummy_drawable)
    {
      GDK_NOTE (OPENGL, g_print ("Destroying dummy drawable\n"));
      XDestroyWindow (dpy, context_x11->dummy_drawable);
      context_x11->dummy_drawable = None;
    }
}

gboolean
gdk_x11_display_make_gl_context_current (GdkDisplay   *display,
                                         GdkGLContext *context,
                                         GdkWindow    *window)
{
  GdkX11GLContext *context_x11 = GDK_X11_GL_CONTEXT (context);
  GLXDrawable drawable = None;

  if (context_x11->glx_context == NULL)
    return FALSE;

  if (window == NULL)
    {
      /* we re-bind our dummy drawable, so that the context
       * can still be used for queries
       */
      drawable = context_x11->dummy_glx_drawable != None
               ? context_x11->dummy_glx_drawable
               : context_x11->dummy_drawable;
    }
  else
    {
      DrawableInfo *info = get_glx_drawable_info (window);

      if (info != NULL && info->drawable != None)
        drawable = info->drawable;
      else
        drawable = gdk_x11_window_get_xid (window);
    }

  if (G_UNLIKELY (drawable == None))
    return FALSE;

  GDK_NOTE (OPENGL,
            g_print ("Making GLX context current to drawable %lu (dummy: %s)\n",
                     (unsigned long) drawable,
                     drawable == context_x11->dummy_drawable ? "yes" : "no"));

  if (drawable == context_x11->current_drawable)
    return TRUE;

  gdk_x11_display_error_trap_push (display);

  glXMakeContextCurrent (gdk_x11_display_get_xdisplay (display),
                         drawable, drawable,
                         context_x11->glx_context);

  if (GDK_X11_DISPLAY (display)->has_glx_swap_interval)
    {
      if (gdk_gl_context_get_swap_interval (context))
        glXSwapIntervalSGI (1);
      else
        glXSwapIntervalSGI (0);
    }

  XSync (gdk_x11_display_get_xdisplay (display), False);

  if (gdk_x11_display_error_trap_pop (display))
    {
      g_critical ("X Error received while calling glXMakeContextCurrent()");
      return FALSE;
    }

  context_x11->current_drawable = drawable;

  return TRUE;
}

gboolean
gdk_x11_display_validate_gl_pixel_format (GdkDisplay        *display,
                                          GdkGLPixelFormat  *format,
                                          GdkGLPixelFormat **validated_format,
                                          GError           **error)
{
  GLXFBConfig config;

  if (!gdk_x11_display_init_gl (display))
    {
      g_set_error_literal (error, GDK_GL_PIXEL_FORMAT_ERROR,
                           GDK_GL_PIXEL_FORMAT_ERROR_NOT_AVAILABLE,
                           _("No GL implementation is available"));
      return FALSE;
    }

  if (format->profile == GDK_GL_PIXEL_FORMAT_PROFILE_3_2_CORE)
    {
      if (!GDK_X11_DISPLAY (display)->has_glx_create_context)
        {
          g_set_error_literal (error, GDK_GL_PIXEL_FORMAT_ERROR,
                               GDK_GL_PIXEL_FORMAT_ERROR_NOT_AVAILABLE,
                               _("The GLX_ARB_create_context_profile extension "
                                 "needed to create 3.2 core profiles is not "
                                 "available"));
          return FALSE;
        }
    }

  if (!find_fbconfig_for_pixel_format (display, format, &config, NULL, error))
    return FALSE;

  if (validated_format != NULL)
    {
      GdkGLPixelFormat *valid = g_object_new (GDK_TYPE_GL_PIXEL_FORMAT, NULL);

      /* update the pixel format with the values of the
       * configuration we found
       */
      update_pixel_format (display, valid, config);

      *validated_format = valid;
    }

  return TRUE;
}

/**
 * gdk_x11_display_get_glx_version:
 * @display: a #GdkDisplay
 * @major: (out): return location for the GLX major version
 * @minor: (out): return location for the GLX minor version
 *
 * Retrieves the version of the GLX implementation.
 *
 * Returns: %TRUE if GLX is available
 *
 * Since: 3.14
 */
gboolean
gdk_x11_display_get_glx_version (GdkDisplay *display,
                                 int        *major,
                                 int        *minor)
{
  g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);

  if (!GDK_IS_X11_DISPLAY (display))
    return FALSE;

  if (!gdk_x11_display_init_gl (display))
    return FALSE;

  if (major != NULL)
    *major = GDK_X11_DISPLAY (display)->glx_version / 10;
  if (minor != NULL)
    *minor = GDK_X11_DISPLAY (display)->glx_version % 10;

  return TRUE;
}