6c4913b1fd17d28412c8b32611410b20383eb32f
[grits] / src / gtkgl.c
1 /*
2  * Copyright (C) 2009-2011 Andy Spencer <andy753421@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <gtk/gtk.h>
19
20 /***************************
21  * GtkGlExt implementation *
22  ***************************/
23 #if defined(SYS_GTKGLEXT)
24 #include <gtk/gtkgl.h>
25 void gtk_gl_enable(GtkWidget *widget)
26 {
27         GdkGLConfig *glconfig = gdk_gl_config_new_by_mode(
28                         GDK_GL_MODE_RGBA   | GDK_GL_MODE_DEPTH |
29                         GDK_GL_MODE_ALPHA  | GDK_GL_MODE_DOUBLE);
30         gtk_widget_set_gl_capability(widget,
31                         glconfig, NULL, TRUE, GDK_GL_RGBA_TYPE);
32 }
33
34 void gtk_gl_begin(GtkWidget *widget)
35 {
36         GdkGLContext  *glcontext  = gtk_widget_get_gl_context(widget);
37         GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(widget);
38         gdk_gl_drawable_gl_begin(gldrawable, glcontext);
39 }
40
41 void gtk_gl_end(GtkWidget *widget)
42 {
43         GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(widget);
44         gdk_gl_drawable_swap_buffers(gldrawable);
45         gdk_gl_drawable_gl_end(gldrawable);
46 }
47
48 void gtk_gl_disable(GtkWidget *widget)
49 {
50 }
51
52
53 /**********************
54  * X11 implementation *
55  **********************/
56 #elif defined(SYS_X11)
57 #include <GL/glx.h>
58 #include <gdk/gdkx.h>
59 static gboolean gtk_gl_errors;
60 static int gtk_gl_handler(Display *xdisplay, XErrorEvent *xerror)
61 {
62         gtk_gl_errors = TRUE;
63         return 0;
64 }
65 static GLXContext gtk_gl_create_context(Display *xdisplay, XVisualInfo *xvinfo,
66                 GLXContext shared, Bool direct)
67 {
68         gtk_gl_errors = FALSE;
69         XSync(xdisplay, False);
70         void *handler = XSetErrorHandler(gtk_gl_handler);
71         GLXContext context = glXCreateContext(xdisplay, xvinfo, shared, direct);
72         XSync(xdisplay, False);
73         XSetErrorHandler(handler);
74
75         if (gtk_gl_errors)
76                 return 0;
77         return context;
78 }
79 void gtk_gl_enable(GtkWidget *widget)
80 {
81         g_debug("GtkGl: enable");
82         GdkScreen *screen   = gdk_screen_get_default();
83         Display   *xdisplay = GDK_SCREEN_XDISPLAY(screen);
84         gint       nscreen  = GDK_SCREEN_XNUMBER(screen);
85
86         /* Create context */
87         int attribs[] = {GLX_RGBA,
88                          GLX_RED_SIZE,    1,
89                          GLX_GREEN_SIZE,  1,
90                          GLX_BLUE_SIZE,   1,
91                          GLX_ALPHA_SIZE,  1,
92                          GLX_DOUBLEBUFFER,
93                          GLX_DEPTH_SIZE,  1,
94                          None};
95
96         XVisualInfo *xvinfo  = glXChooseVisual(xdisplay, nscreen, attribs);
97         if (!xvinfo)
98                 g_error("GtkGl: enable - unable to get valid OpenGL Visual");
99         GLXContext context = 0;
100         if (!context)
101                 context = gtk_gl_create_context(xdisplay, xvinfo, NULL, True);
102         if (!context)
103                 context = gtk_gl_create_context(xdisplay, xvinfo, NULL, False);
104         if (!context)
105                 g_error("Unable to create OpenGL context,"
106                         "possible graphics driver problem?");
107         g_debug("GtkGl: direct rendering = %d\n", glXIsDirect(xdisplay, context));
108
109         g_object_set_data(G_OBJECT(widget), "glcontext", context);
110
111         /* Fix up colormap */
112         GdkVisual   *visual = gdk_x11_screen_lookup_visual(screen, xvinfo->visualid);
113         GdkColormap *cmap   = gdk_colormap_new(visual, FALSE);
114         gtk_widget_set_colormap(widget, cmap);
115         g_object_unref(cmap);
116         XFree(xvinfo);
117
118         /* Disable GTK double buffering */
119         gtk_widget_set_double_buffered(widget, FALSE);
120 }
121
122 void gtk_gl_begin(GtkWidget *widget)
123 {
124         g_debug("GtkGl: begin");
125         Display   *xdisplay = GDK_SCREEN_XDISPLAY(gtk_widget_get_screen(widget));
126         Window     xwindow  = GDK_WINDOW_XID(gtk_widget_get_window(widget));
127         GLXContext context  = g_object_get_data(G_OBJECT(widget), "glcontext");
128         glXMakeCurrent(xdisplay, xwindow, context);
129 }
130
131 void gtk_gl_end(GtkWidget *widget)
132 {
133         g_debug("GtkGl: end");
134         Display   *xdisplay = GDK_SCREEN_XDISPLAY(gtk_widget_get_screen(widget));
135         Window     xwindow  = GDK_WINDOW_XID(gtk_widget_get_window(widget));
136         glXSwapBuffers(xdisplay, xwindow);
137 }
138
139 void gtk_gl_disable(GtkWidget *widget)
140 {
141         g_debug("GtkGl: disable");
142         Display   *xdisplay = GDK_SCREEN_XDISPLAY(gtk_widget_get_screen(widget));
143         GLXContext context  = g_object_get_data(G_OBJECT(widget), "glcontext");
144         glXDestroyContext(xdisplay, context);
145 }
146
147
148 /************************
149  * Win32 implementation *
150  ************************/
151 #elif defined(SYS_WIN)
152 #include <windows.h>
153 #include <gdk/gdkwin32.h>
154 #include <GL/gl.h>
155
156 /* Windows doens't define OpenGL extensions */
157 static void APIENTRY (*glMultiTexCoord2dvPtr)(int target, const double *v);
158 static void APIENTRY (*glActiveTexturePtr)(int texture);
159
160 void APIENTRY glMultiTexCoord2dv(int target, const double *v)
161 {
162         glMultiTexCoord2dvPtr(target, v);
163 }
164
165 void APIENTRY glActiveTexture(int texture)
166 {
167         glActiveTexturePtr(texture);
168 }
169
170 static void init_extensions(void)
171 {
172         static gboolean init_done = FALSE;
173         if (init_done)
174                 return;
175         init_done = TRUE;
176
177         g_debug("GtkGl: init_extensions");
178         const guchar *exts = NULL;
179         if (!(exts = glGetString(GL_EXTENSIONS)))
180                 g_error("GtkGl: Unable to query extensions");
181         if (!(glMultiTexCoord2dvPtr = (void*)wglGetProcAddress("glMultiTexCoord2dvARB")))
182                 g_error("GtkGl: Unable to load glMultiTexCoord2dv extension:\n%s", exts);
183         if (!(glActiveTexturePtr    = (void*)wglGetProcAddress("glActiveTextureARB")))
184                 g_error("GtkGl: Unable to load glActiveTexture extension\n%s", exts);
185         g_debug("GtkGl: extensions - glMultiTexCoord2dvPtr=%p glActiveTexturePtr=%p",
186                         glMultiTexCoord2dvPtr, glActiveTexturePtr);
187 }
188
189 /* gtkgl implementation */
190 static void on_realize(GtkWidget *widget, gpointer _)
191 {
192         g_debug("GtkGl: on_realize");
193         gdk_window_ensure_native(gtk_widget_get_window(widget));
194         gtk_widget_set_double_buffered(widget, FALSE);
195
196         HWND  hwnd = GDK_WINDOW_HWND(gtk_widget_get_window(widget));
197         HDC   hDC  = GetDC(hwnd);
198
199         PIXELFORMATDESCRIPTOR pfd = {
200                 .nSize       = sizeof(pfd),
201                 .nVersion    = 1,
202                 .dwFlags     = PFD_DRAW_TO_WINDOW
203                              | PFD_SUPPORT_OPENGL
204                              | PFD_DOUBLEBUFFER,
205                 //.dwFlags     = PFD_SUPPORT_OPENGL
206                 //             | PFD_DRAW_TO_WINDOW,
207                 .iPixelType  = PFD_TYPE_RGBA,
208                 .cColorBits  = 24,
209                 .cAlphaBits  = 8,
210                 .cDepthBits  = 32,
211                 .iLayerType  = PFD_MAIN_PLANE,
212         };
213         int pf = ChoosePixelFormat(hDC, &pfd);
214         if (pf == 0)
215                 g_error("GtkGl: ChoosePixelFormat failed");
216         if (!SetPixelFormat(hDC, pf, &pfd))
217                 g_error("GtkGl: SetPixelFormat failed");
218         HGLRC hRC = wglCreateContext(hDC);
219         if (hRC == NULL)
220                 g_error("GtkGl: wglCreateContext failed");
221         g_object_set_data(G_OBJECT(widget), "glcontext", hRC);
222 }
223
224 void gtk_gl_enable(GtkWidget *widget)
225 {
226         g_debug("GtkGl: enable");
227         g_signal_connect(widget, "realize", G_CALLBACK(on_realize), NULL);
228 }
229
230 void gtk_gl_begin(GtkWidget *widget)
231 {
232         g_debug("GtkGl: begin");
233         HWND  hwnd = GDK_WINDOW_HWND(gtk_widget_get_window(widget));
234         HDC   hDC  = GetDC(hwnd);
235         HGLRC hRC  = g_object_get_data(G_OBJECT(widget), "glcontext");
236         if (!wglMakeCurrent(hDC, hRC))
237                 g_error("GtkGl: wglMakeCurrent failed");
238         init_extensions();
239 }
240
241 void gtk_gl_end(GtkWidget *widget)
242 {
243         g_debug("GtkGl: end");
244         HWND  hwnd = GDK_WINDOW_HWND(gtk_widget_get_window(widget));
245         HDC   hDC  = GetDC(hwnd);
246         if (!SwapBuffers(hDC))
247                 g_error("GtkGl: SwapBuffers failed");
248 }
249
250 void gtk_gl_disable(GtkWidget *widget)
251 {
252         g_debug("GtkGl: disable");
253         HGLRC hRC = g_object_get_data(G_OBJECT(widget), "glcontext");
254         wglDeleteContext(hRC);
255 }
256
257
258 /**************************
259  * Mac OSX implementation *
260  **************************/
261 #elif defined(SYS_MAC)
262 #include <gdk/gdkquartz.h>
263 void gtk_gl_enable(GtkWidget *widget)
264 {
265         g_debug("GtkGl: enable");
266
267         /* Create context */
268         NSOpenGLPixelFormatAttribute attribs[] = {
269                 NSOpenGLPFAColorSize, 24,
270                 NSOpenGLPFAAlphaSize, 8,
271                 NSOpenGLPFADepthSize, 1,
272                 NSOpenGLPFADoubleBuffer,
273                 0
274         };
275         NSOpenGLPixelFormat *pix = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
276         NSOpenGLContext     *ctx = [[NSOpenGLContext     alloc] initWithFormat:pix shareContext:nil];
277
278         /* Attach to widget */
279         gtk_widget_set_double_buffered(widget, FALSE);
280
281         /* Save context */
282         g_object_set_data(G_OBJECT(widget), "glcontext", ctx);
283 }
284
285 void gtk_gl_begin(GtkWidget *widget)
286 {
287         g_debug("GtkGl: begin");
288         GtkAllocation alloc;
289         gdk_window_ensure_native(gtk_widget_get_window(widget));
290         gtk_widget_get_allocation(widget, &alloc);
291         gtk_widget_translate_coordinates(widget, gtk_widget_get_toplevel(widget),
292                 0, 0, &alloc.x, &alloc.y);
293
294         NSOpenGLContext *ctx  = g_object_get_data(G_OBJECT(widget), "glcontext");
295         GdkWindow       *win  = gtk_widget_get_window(widget);
296         NSView          *view = gdk_quartz_window_get_nsview(win);
297         NSRect           rect = NSMakeRect(alloc.x, alloc.y, alloc.width, alloc.height);
298
299         [ctx  setView:view];
300         [ctx  makeCurrentContext];
301         [ctx  update];
302         [view setFrame:rect];
303         [view setWantsBestResolutionOpenGLSurface:YES];
304 }
305
306 void gtk_gl_end(GtkWidget *widget)
307 {
308         g_debug("GtkGl: end");
309         NSOpenGLContext *ctx = g_object_get_data(G_OBJECT(widget), "glcontext");
310         [ctx flushBuffer];
311 }
312
313 void gtk_gl_disable(GtkWidget *widget)
314 {
315         g_debug("GtkGl: disable");
316 }
317
318
319 /****************************
320  * Undefined implementation *
321  ****************************/
322 #else
323 #warning "Unimplemented GtkGl"
324 void gtk_gl_enable(GtkWidget *widget) { }
325 void gtk_gl_begin(GtkWidget *widget) { }
326 void gtk_gl_end(GtkWidget *widget) { }
327 void gtk_gl_disable(GtkWidget *widget) { }
328 #endif