]> git.xonotic.org Git - xonotic/netradiant.git/blob - libs/gtkutil/glwidget.cpp
gtk3: call ui::process() after gtk_gl_area_queue_render()
[xonotic/netradiant.git] / libs / gtkutil / glwidget.cpp
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant 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
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 // OpenGL widget based on GtkGLExt
23
24 #include "glwidget.h"
25
26 #include "igl.h"
27
28 void (*GLWidget_sharedContextCreated)() = 0;
29
30 void (*GLWidget_sharedContextDestroyed)() = 0;
31
32 unsigned int g_context_count = 0;
33
34 ui::GLArea g_shared{ui::null};
35
36 static void _glwidget_context_created(ui::GLArea self, void *data)
37 {
38     if (++g_context_count == 1) {
39         g_shared = self;
40         g_object_ref(g_shared._handle);
41
42         glwidget_make_current(g_shared);
43         GlobalOpenGL().contextValid = true;
44
45         GLWidget_sharedContextCreated();
46     }
47 }
48
49 static void _glwidget_context_destroyed(ui::GLArea self, void *data)
50 {
51     if (--g_context_count == 0) {
52         GlobalOpenGL().contextValid = false;
53
54         GLWidget_sharedContextDestroyed();
55
56         g_shared.unref();
57         g_shared = ui::GLArea(ui::null);
58     }
59 }
60
61 void glwidget_destroy_context(ui::GLArea self)
62 {
63 }
64
65 void glwidget_create_context(ui::GLArea self)
66 {
67 }
68
69 #if GTK_TARGET == 3
70
71 #include <gtk/gtk.h>
72
73 static GdkGLContext *glwidget_context_created(ui::GLArea self)
74 {
75     _glwidget_context_created(self, nullptr);
76     return gtk_gl_area_get_context(self);
77 }
78
79 ui::GLArea glwidget_new(bool zbuffer)
80 {
81     auto self = ui::GLArea::from(GTK_GL_AREA(gtk_gl_area_new()));
82     gtk_gl_area_set_has_depth_buffer(self, zbuffer);
83
84     self.connect("realize", G_CALLBACK(glwidget_context_created), nullptr);
85     return self;
86 }
87
88 bool glwidget_make_current(ui::GLArea self)
89 {
90 //    if (!g_context_count) {
91 //        glwidget_context_created(self);
92 //    }
93     gtk_gl_area_make_current(self);
94     auto valid = GlobalOpenGL().contextValid;
95     return true;
96 }
97
98 void glwidget_swap_buffers(ui::GLArea self)
99 {
100     g_assert(GTK_IS_GL_AREA(self));
101     gtk_gl_area_queue_render(self);
102
103     // Process the redraw, why is it needed?
104     ui::process();
105 }
106
107 #endif
108
109 #if GTK_TARGET == 2
110
111 #include <gtk/gtk.h>
112 #include <gtk/gtkglwidget.h>
113
114 #include "pointer.h"
115
116 struct config_t {
117     const char *name;
118     int *attribs;
119 };
120 typedef const config_t *configs_iterator;
121
122 static int config_rgba32[] = {
123         GDK_GL_RGBA,
124         GDK_GL_DOUBLEBUFFER,
125         GDK_GL_BUFFER_SIZE, 24,
126         GDK_GL_ATTRIB_LIST_NONE,
127 };
128
129 static int config_rgba[] = {
130         GDK_GL_RGBA,
131         GDK_GL_DOUBLEBUFFER,
132         GDK_GL_BUFFER_SIZE, 16,
133         GDK_GL_ATTRIB_LIST_NONE,
134 };
135
136 static const config_t configs[] = {
137         {
138                 "colour-buffer = 32bpp, depth-buffer = none",
139                 config_rgba32,
140         },
141         {
142                 "colour-buffer = 16bpp, depth-buffer = none",
143                 config_rgba,
144         }
145 };
146
147 static GdkGLConfig *glconfig_new()
148 {
149     for (configs_iterator i = configs, end = configs + 2; i != end; ++i) {
150         if (auto glconfig = gdk_gl_config_new(i->attribs)) {
151             globalOutputStream() << "OpenGL window configuration: " << i->name << "\n";
152             return glconfig;
153         }
154     }
155     globalOutputStream() << "OpenGL window configuration: colour-buffer = auto, depth-buffer = none\n";
156     return gdk_gl_config_new_by_mode((GdkGLConfigMode) (GDK_GL_MODE_RGBA | GDK_GL_MODE_DOUBLE));
157 }
158
159 static int config_rgba32_depth32[] = {
160         GDK_GL_RGBA,
161         GDK_GL_DOUBLEBUFFER,
162         GDK_GL_BUFFER_SIZE,
163         24,
164         GDK_GL_DEPTH_SIZE,
165         32,
166         GDK_GL_ATTRIB_LIST_NONE,
167 };
168
169 static int config_rgba32_depth24[] = {
170         GDK_GL_RGBA,
171         GDK_GL_DOUBLEBUFFER,
172         GDK_GL_BUFFER_SIZE, 24,
173         GDK_GL_DEPTH_SIZE, 24,
174         GDK_GL_ATTRIB_LIST_NONE,
175 };
176
177 static int config_rgba32_depth16[] = {
178         GDK_GL_RGBA,
179         GDK_GL_DOUBLEBUFFER,
180         GDK_GL_BUFFER_SIZE, 24,
181         GDK_GL_DEPTH_SIZE, 16,
182         GDK_GL_ATTRIB_LIST_NONE,
183 };
184
185 static int config_rgba32_depth[] = {
186         GDK_GL_RGBA,
187         GDK_GL_DOUBLEBUFFER,
188         GDK_GL_BUFFER_SIZE, 24,
189         GDK_GL_DEPTH_SIZE, 1,
190         GDK_GL_ATTRIB_LIST_NONE,
191 };
192
193 static int config_rgba_depth16[] = {
194         GDK_GL_RGBA,
195         GDK_GL_DOUBLEBUFFER,
196         GDK_GL_BUFFER_SIZE, 16,
197         GDK_GL_DEPTH_SIZE, 16,
198         GDK_GL_ATTRIB_LIST_NONE,
199 };
200
201 static int config_rgba_depth[] = {
202         GDK_GL_RGBA,
203         GDK_GL_DOUBLEBUFFER,
204         GDK_GL_BUFFER_SIZE, 16,
205         GDK_GL_DEPTH_SIZE, 1,
206         GDK_GL_ATTRIB_LIST_NONE,
207 };
208
209 static const config_t configs_with_depth[] =
210         {
211                 {
212                         "colour-buffer = 32bpp, depth-buffer = 32bpp",
213                         config_rgba32_depth32,
214                 },
215                 {
216                         "colour-buffer = 32bpp, depth-buffer = 24bpp",
217                         config_rgba32_depth24,
218                 },
219                 {
220                         "colour-buffer = 32bpp, depth-buffer = 16bpp",
221                         config_rgba32_depth16,
222                 },
223                 {
224                         "colour-buffer = 32bpp, depth-buffer = auto",
225                         config_rgba32_depth,
226                 },
227                 {
228                         "colour-buffer = 16bpp, depth-buffer = 16bpp",
229                         config_rgba_depth16,
230                 },
231                 {
232                         "colour-buffer = auto, depth-buffer = auto",
233                         config_rgba_depth,
234                 },
235         };
236
237 static GdkGLConfig *glconfig_new_with_depth()
238 {
239     for (configs_iterator i = configs_with_depth, end = configs_with_depth + 6; i != end; ++i) {
240         if (auto glconfig = gdk_gl_config_new(i->attribs)) {
241             globalOutputStream() << "OpenGL window configuration: " << i->name << "\n";
242             return glconfig;
243         }
244     }
245     globalOutputStream() << "OpenGL window configuration: colour-buffer = auto, depth-buffer = auto (fallback)\n";
246     return gdk_gl_config_new_by_mode((GdkGLConfigMode) (GDK_GL_MODE_RGBA | GDK_GL_MODE_DOUBLE | GDK_GL_MODE_DEPTH));
247 }
248
249 static int glwidget_context_created(ui::GLArea self, void *data)
250 {
251     _glwidget_context_created(self, data);
252     return false;
253 }
254
255 int glwidget_context_destroyed(ui::GLArea self, void *data)
256 {
257     _glwidget_context_destroyed(self, data);
258     return false;
259 }
260
261 static bool glwidget_enable_gl(ui::GLArea self, ui::Widget root, gpointer data)
262 {
263     if (!root && !gtk_widget_is_gl_capable(self)) {
264         const auto zbuffer = g_object_get_data(G_OBJECT(self), "zbuffer");
265         GdkGLConfig *glconfig = zbuffer ? glconfig_new_with_depth() : glconfig_new();
266         ASSERT_MESSAGE(glconfig, "failed to create OpenGL config");
267
268         const auto share_list = g_shared ? gtk_widget_get_gl_context(g_shared) : nullptr;
269         gtk_widget_set_gl_capability(self, glconfig, share_list, true, GDK_GL_RGBA_TYPE);
270
271         gtk_widget_realize(self);
272         if (!g_shared) {
273             g_shared = self;
274         }
275         // free glconfig?
276     }
277     return false;
278 }
279
280 ui::GLArea glwidget_new(bool zbuffer)
281 {
282     auto self = ui::GLArea::from(gtk_drawing_area_new());
283
284     g_object_set_data(G_OBJECT(self), "zbuffer", gint_to_pointer(zbuffer));
285
286     self.connect("hierarchy-changed", G_CALLBACK(glwidget_enable_gl), 0);
287
288     self.connect("realize", G_CALLBACK(glwidget_context_created), 0);
289     self.connect("unrealize", G_CALLBACK(glwidget_context_destroyed), 0);
290
291     return self;
292 }
293
294 void glwidget_swap_buffers(ui::GLArea self)
295 {
296     GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(self);
297     gdk_gl_drawable_swap_buffers(gldrawable);
298 }
299
300 bool glwidget_make_current(ui::GLArea self)
301 {
302     GdkGLContext *glcontext = gtk_widget_get_gl_context(self);
303     GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(self);
304     return gdk_gl_drawable_gl_begin(gldrawable, glcontext);
305 }
306
307 #endif