summaryrefslogtreecommitdiff
path: root/plugins/gtkui/gtkglext-gtk3/gtk/gtkglwidget.c
blob: 4bb372bbfb76e29e2694b4a7600429e66477da9d (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
/* GtkGLExt - OpenGL Extension to GTK+
 * Copyright (C) 2002-2004  Naofumi Yasufuku
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#include "gtkglprivate.h"
#include "gtkglwidget.h"

#include <GL/gl.h>

typedef struct
{
  GdkGLConfig *glconfig;
  GdkGLContext *share_list;
  gboolean direct;
  int render_type;

  GdkGLContext *glcontext;

  gulong unrealize_handler;

  guint is_realized : 1;

} GLWidgetPrivate;

static const gchar quark_gl_private_string[] = "gtk-gl-widget-private";
static GQuark quark_gl_private = 0;

gboolean _gtk_gl_widget_install_toplevel_visual = FALSE;

static void     gtk_gl_widget_realize            (GtkWidget         *widget,
                                                  GLWidgetPrivate   *private);
static gboolean gtk_gl_widget_configure_event    (GtkWidget         *widget,
                                                  GdkEventConfigure *event,
                                                  GLWidgetPrivate   *private);
static void     gtk_gl_widget_size_allocate      (GtkWidget         *widget,
                                                  GtkAllocation     *allocation,
                                                  GLWidgetPrivate   *private);
static void     gtk_gl_widget_unrealize          (GtkWidget         *widget,
                                                  GLWidgetPrivate   *private);
static void     gtk_gl_widget_parent_set         (GtkWidget         *widget,
                                                  GObject           *old_parent,
                                                  GdkVisual         *visual);
static void     gtk_gl_widget_style_set          (GtkWidget         *widget,
                                                  GtkStyle          *previous_style,
                                                  gpointer           user_data);

static void     gl_widget_private_destroy        (GLWidgetPrivate   *private);

/*
 * Signal handlers.
 */

static void
gtk_gl_widget_realize (GtkWidget       *widget,
                       GLWidgetPrivate *private)
{
  GdkWindow *window;
  GdkGLWindow *glwindow;

  GTK_GL_NOTE_FUNC_PRIVATE ();

  /*
   * Set OpenGL-capability to widget->window, then connect some signal
   * handlers.
   */

  window = gtk_widget_get_window (widget);
  if (!gdk_window_is_gl_capable (window))
    {
      glwindow = gdk_window_set_gl_capability (window,
                                               private->glconfig,
                                               NULL);
      if (glwindow == NULL)
        {
          g_warning ("cannot set OpenGL-capability to widget->window\n");
          return;
        }

      /* Connect "unrealize" signal handler. */
      if (private->unrealize_handler == 0)
        private->unrealize_handler = g_signal_connect (G_OBJECT (widget), "unrealize",
                                                       G_CALLBACK (gtk_gl_widget_unrealize),
                                                       private);
    }

  private->is_realized = TRUE;
}

static gboolean
gtk_gl_widget_configure_event (GtkWidget         *widget,
                               GdkEventConfigure *event,
                               GLWidgetPrivate   *private)
{
  GTK_GL_NOTE_FUNC_PRIVATE ();

  if (!private->is_realized)
    {
      /* Realize if OpenGL-capable window is not realized yet. */
      gtk_gl_widget_realize (widget, private);
    }

  return FALSE;
}

static void
gtk_gl_widget_size_allocate (GtkWidget       *widget,
                             GtkAllocation   *allocation,
                             GLWidgetPrivate *private)
{
  GdkGLDrawable *gldrawable;

  GTK_GL_NOTE_FUNC_PRIVATE ();

  /*
   * Synchronize OpenGL and window resizing request streams.
   */

  if (gtk_widget_get_realized (widget) && private->is_realized)
    {
      gldrawable = gdk_window_get_gl_drawable (gtk_widget_get_window (widget));
      gdk_gl_drawable_wait_gdk (gldrawable);
    }
}

static void
gtk_gl_widget_unrealize (GtkWidget       *widget,
                         GLWidgetPrivate *private)
{
  GTK_GL_NOTE_FUNC_PRIVATE ();

  /*
   * Destroy OpenGL rendering context.
   */

  if (private->glcontext != NULL)
    {
      g_object_unref (private->glcontext);
      private->glcontext = NULL;
    }

  /*
   * Remove OpenGL-capability from widget->window.
   */

  if (gtk_widget_get_realized (widget))
    gdk_window_unset_gl_capability (gtk_widget_get_window (widget));

  private->is_realized = FALSE;
}

static void
gtk_gl_widget_parent_set (GtkWidget *widget,
                          GObject   *old_parent,
                          GdkVisual *visual)
{
  GtkWidget *toplevel;

  GTK_GL_NOTE_FUNC_PRIVATE ();

  /*
   * Try to install visual to the top-level window.
   */

  toplevel = gtk_widget_get_toplevel (widget);
  if (gtk_widget_is_toplevel (toplevel) && !gtk_widget_get_realized (toplevel))
    {
      GTK_GL_NOTE (MISC,
        g_message (" - Install visual to the top-level window."));

      gtk_widget_set_visual (toplevel, visual);
    }
}

static void
gtk_gl_widget_style_set (GtkWidget *widget,
                         GtkStyle  *previous_style,
                         gpointer   user_data)
{
  GdkWindow *window;

  GTK_GL_NOTE_FUNC_PRIVATE ();

  /*
   * Set a background of "None" on window to avoid AIX X server crash.
   */

  if (gtk_widget_get_realized (widget))
    {
      window = gtk_widget_get_window (widget);

      GTK_GL_NOTE (MISC,
        g_message (" - window->bg_pixmap = %p",
                   (void*)gdk_window_get_background_pattern(window)));

      gdk_window_set_background_pattern (window, NULL);

      GTK_GL_NOTE (MISC,
        g_message (" - window->bg_pixmap = %p",
                   (void*)gdk_window_get_background_pattern(window)));
    }
}

static void
gl_widget_private_destroy (GLWidgetPrivate *private)
{
  GTK_GL_NOTE_FUNC_PRIVATE ();

  g_object_unref (G_OBJECT (private->glconfig));

  if (private->share_list != NULL)
    g_object_unref (G_OBJECT (private->share_list));

  g_free (private);
}

/**
 * gtk_widget_set_gl_capability:
 * @widget: the #GtkWidget to be used as the rendering area.
 * @glconfig: a #GdkGLConfig.
 * @share_list: (allow-none): the #GdkGLContext with which to share display lists and texture
 *              objects. NULL indicates that no sharing is to take place.
 * @direct: whether rendering is to be done with a direct connection to
 *          the graphics system.
 * @render_type: GDK_GL_RGBA_TYPE.
 *
 * Set the OpenGL-capability to the @widget.
 * This function prepares the widget for its use with OpenGL.
 *
 * Return value: TRUE if it is successful, FALSE otherwise.
 **/
gboolean
gtk_widget_set_gl_capability (GtkWidget    *widget,
                              GdkGLConfig  *glconfig,
                              GdkGLContext *share_list,
                              gboolean      direct,
                              int           render_type)
{
  GdkVisual *visual;
  GLWidgetPrivate *private;

  GTK_GL_NOTE_FUNC ();

  g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
  g_return_val_if_fail (gtk_widget_get_has_window (widget), FALSE);
  g_return_val_if_fail (!gtk_widget_get_realized (widget), FALSE);
  g_return_val_if_fail (GDK_IS_GL_CONFIG (glconfig), FALSE);

  /*
   * Init quark.
   */

  if (quark_gl_private == 0)
    quark_gl_private = g_quark_from_static_string (quark_gl_private_string);

  /*
   * Already OpenGL-capable?
   */

  if (gtk_widget_is_gl_capable (widget))
    return TRUE;

  /*
   * Set OpenGL-capable colormap.
   */

  visual = gdk_gl_config_get_visual (glconfig);

  gtk_widget_set_visual (widget, visual);

  /* Install visual to the top-level window. */
  if (_gtk_gl_widget_install_toplevel_visual)
    {
      /*
       * If window manager doesn't watch the WM_COLORMAP_WINDOWS property on
       * the top-level window, we have to set OpenGL window's colormap to the
       * top-level window, especially in color index mode (color index mode
       * uses own private colormap). This is achieved by setting the window's
       * visual.
       */

      /* Signal handler to set colormap to the top-level window. */
      g_signal_connect (G_OBJECT (widget), "parent_set",
                        G_CALLBACK (gtk_gl_widget_parent_set),
                        visual);

      /* If given widget has the top-level window, visual is set here. */
      gtk_gl_widget_parent_set (widget, NULL, visual);
    }

  /*
   * Disable backing store feature of the widget.
   */

  gtk_widget_set_double_buffered (widget, FALSE);

  /*
   * "style_set" signal handler to set a background of "None" on window.
   * (relates AIX X server crash)
   */

  g_signal_connect_after (G_OBJECT (widget), "style_set",
                          G_CALLBACK (gtk_gl_widget_style_set),
                          NULL);

  /*
   * Set GL widget's private data.
   */

  private = g_new (GLWidgetPrivate, 1);

  private->glconfig = glconfig;
  g_object_ref (G_OBJECT (private->glconfig));

  if (share_list != NULL && GDK_IS_GL_CONTEXT (share_list))
    {
      private->share_list = share_list;
      g_object_ref (G_OBJECT (private->share_list));
    }
  else
    {
      private->share_list = NULL;
    }

  private->direct = direct;
  private->render_type = render_type;

  private->glcontext = NULL;

  private->unrealize_handler = 0;

  private->is_realized = FALSE;

  g_object_set_qdata_full (G_OBJECT (widget), quark_gl_private, private,
                           (GDestroyNotify) gl_widget_private_destroy);

  /*
   * Connect signal handlers to realize OpenGL-capable widget.
   */

  g_signal_connect (G_OBJECT (widget), "realize",
                    G_CALLBACK (gtk_gl_widget_realize),
                    private);

  /* gtk_drawing_area sends configure_event when it is realized. */
  g_signal_connect (G_OBJECT (widget), "configure_event",
                    G_CALLBACK (gtk_gl_widget_configure_event),
                    private);

  /*
   * Connect "size_allocate" signal handler to synchronize OpenGL and
   * window resizing request streams.
   */

  g_signal_connect (G_OBJECT (widget), "size_allocate",
                    G_CALLBACK (gtk_gl_widget_size_allocate),
                    private);

  return TRUE;
}

/**
 * gtk_widget_is_gl_capable:
 * @widget: a #GtkWidget.
 *
 * Returns whether the @widget is OpenGL-capable.
 *
 * Return value: TRUE if the @widget is OpenGL-capable, FALSE otherwise.
 **/
gboolean
gtk_widget_is_gl_capable (GtkWidget *widget)
{
  g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);

  return (g_object_get_qdata (G_OBJECT (widget), quark_gl_private) != NULL);
}

/**
 * gtk_widget_get_gl_config:
 * @widget: a #GtkWidget.
 *
 * Returns the #GdkGLConfig referred by the @widget.
 *
 * Return value: the #GdkGLConfig.
 **/
GdkGLConfig *
gtk_widget_get_gl_config (GtkWidget *widget)
{
  GLWidgetPrivate *private;

  g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);

  private = g_object_get_qdata (G_OBJECT (widget), quark_gl_private);
  if (private == NULL)
    return NULL;

  return private->glconfig;
}

/**
 * gtk_widget_create_gl_context:
 * @widget: a #GtkWidget.
 * @share_list: (allow-none): the #GdkGLContext with which to share display lists and texture
 *              objects. NULL indicates that no sharing is to take place.
 * @direct: whether rendering is to be done with a direct connection to
 *          the graphics system.
 * @render_type: GDK_GL_RGBA_TYPE.
 *
 * Creates a new #GdkGLContext with the appropriate #GdkGLDrawable
 * for this widget. The GL context must be freed when you're
 * finished with it. See also gtk_widget_get_gl_context().
 *
 * Return value: the new #GdkGLContext.
 **/
GdkGLContext *
gtk_widget_create_gl_context (GtkWidget    *widget,
                              GdkGLContext *share_list,
                              gboolean      direct,
                              int           render_type)
{
  GdkGLDrawable *gldrawable;
  GdkGLContext *glcontext;

  GTK_GL_NOTE_FUNC ();

  g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
  g_return_val_if_fail (gtk_widget_get_realized (widget), NULL);

  gldrawable = gdk_window_get_gl_drawable (gtk_widget_get_window (widget));
  if (gldrawable == NULL)
    return NULL;

  /*
   * Create OpenGL rendering context.
   */

  glcontext = gdk_gl_context_new (gldrawable,
                                  share_list,
                                  direct,
                                  render_type);
  if (glcontext == NULL)
    {
      g_warning ("cannot create GdkGLContext\n");
      return NULL;
    }

  return glcontext;
}

/**
 * gtk_widget_get_gl_context:
 * @widget: a #GtkWidget.
 *
 * Returns the #GdkGLContext with the appropriate #GdkGLDrawable
 * for this widget. Unlike the GL context returned by
 * gtk_widget_create_gl_context(),  this context is owned by the widget.
 *
 * #GdkGLContext is needed for the function gdk_gl_drawable_begin,
 * or for sharing display lists (see gtk_widget_set_gl_capability()).
 *
 * Return value: the #GdkGLContext.
 **/
GdkGLContext *
gtk_widget_get_gl_context (GtkWidget *widget)
{
  GLWidgetPrivate *private;

  g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
  g_return_val_if_fail (gtk_widget_get_realized (widget), NULL);

  private = g_object_get_qdata (G_OBJECT (widget), quark_gl_private);
  if (private == NULL)
    return NULL;

  if (private->glcontext == NULL)
    private->glcontext = gtk_widget_create_gl_context (widget,
                                                       private->share_list,
                                                       private->direct,
                                                       private->render_type);

  return private->glcontext;
}

/**
 * gtk_widget_get_gl_window:
 * @widget: a #GtkWidget.
 *
 * Returns the #GdkGLWindow owned by the @widget.
 *
 * Return value: the #GdkGLWindow.
 **/
GdkGLWindow *
gtk_widget_get_gl_window (GtkWidget *widget)
{
  g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
  g_return_val_if_fail (gtk_widget_get_realized (widget), NULL);

  return gdk_window_get_gl_window (gtk_widget_get_window (widget));
}

gboolean
gtk_widget_begin_gl(GtkWidget *widget)
{
  GdkGLContext *glcontext;
  GdkGLWindow  *glwindow;

  g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);

  glcontext = gtk_widget_get_gl_context (widget);
  glwindow  = gtk_widget_get_gl_window (widget);

  return gdk_gl_context_make_current(glcontext, GDK_GL_DRAWABLE (glwindow), GDK_GL_DRAWABLE (glwindow));
}

void
gtk_widget_end_gl(GtkWidget *widget, gboolean do_swap)
{
  GdkGLDrawable *gldrawable;

  g_return_if_fail (GTK_IS_WIDGET (widget));

  gldrawable = GDK_GL_DRAWABLE (gtk_widget_get_gl_window (widget));

  if (do_swap)
    {
      if (gdk_gl_drawable_is_double_buffered (gldrawable))
        gdk_gl_drawable_swap_buffers (gldrawable);
      else
        glFlush ();
    }

  gdk_gl_context_release_current();
}