aboutsummaryrefslogtreecommitdiffstats
path: root/src/glx/windows/windowsgl.c
blob: 56849da837162f52e5d860266fed5f13bd06d589 (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
/*
 * Copyright © 2014 Jon Turney
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include "windowsgl.h"
#include "windowsgl_internal.h"

#include "glapi.h"
#include "wgl.h"

#include <dlfcn.h>
#include <assert.h>
#include <stdio.h>
#include <strings.h>

static struct _glapi_table *windows_api = NULL;

static void *
windows_get_dl_handle(void)
{
   static void *dl_handle = NULL;

   if (!dl_handle)
      dl_handle = dlopen("cygnativeGLthunk.dll", RTLD_NOW);

   return dl_handle;
}

static void
windows_glapi_create_table(void)
{
   if (windows_api)
      return;

   windows_api = _glapi_create_table_from_handle(windows_get_dl_handle(), "gl");
   assert(windows_api);
}

static void windows_glapi_set_dispatch(void)
{
   windows_glapi_create_table();
   _glapi_set_dispatch(windows_api);
}

windowsContext *
windows_create_context(int pxfi, windowsContext *shared)
{
   windowsContext *gc;

   gc = calloc(1, sizeof *gc);
   if (gc == NULL)
      return NULL;

   // create a temporary, invisible window
#define GL_TEMP_WINDOW_CLASS "glTempWndClass"
   {
      static wATOM glTempWndClass = 0;

      if (glTempWndClass == 0) {
         WNDCLASSEX wc;
         wc.cbSize = sizeof(WNDCLASSEX);
         wc.style = CS_HREDRAW | CS_VREDRAW;
         wc.lpfnWndProc = DefWindowProc;
         wc.cbClsExtra = 0;
         wc.cbWndExtra = 0;
         wc.hInstance = GetModuleHandle(NULL);
         wc.hIcon = 0;
         wc.hCursor = 0;
         wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
         wc.lpszMenuName = NULL;
         wc.lpszClassName = GL_TEMP_WINDOW_CLASS;
         wc.hIconSm = 0;
         RegisterClassEx(&wc);
      }
   }

   HWND hwnd = CreateWindowExA(0,
                               GL_TEMP_WINDOW_CLASS,
                               "glWindow",
                               0,
                               0, 0, 0, 0,
                               NULL, NULL, GetModuleHandle(NULL), NULL);
   HDC hdc = GetDC(hwnd);

   // We must set the windows pixel format before we can create a WGL context
   gc->pxfi = pxfi;
   SetPixelFormat(hdc, gc->pxfi, NULL);

   gc->ctx = wglCreateContext(hdc);

   if (shared && gc->ctx)
      wglShareLists(shared->ctx, gc->ctx);

   ReleaseDC(hwnd, hdc);
   DestroyWindow(hwnd);

   if (!gc->ctx)
   {
     free(gc);
     return NULL;
   }

   return gc;
}

windowsContext *
windows_create_context_attribs(int pxfi, windowsContext *shared, const int *attribList)
{
   windowsContext *gc;

   gc = calloc(1, sizeof *gc);
   if (gc == NULL)
      return NULL;

   // create a temporary, invisible window
#define GL_TEMP_WINDOW_CLASS "glTempWndClass"
   {
      static wATOM glTempWndClass = 0;

      if (glTempWndClass == 0) {
         WNDCLASSEX wc;
         wc.cbSize = sizeof(WNDCLASSEX);
         wc.style = CS_HREDRAW | CS_VREDRAW;
         wc.lpfnWndProc = DefWindowProc;
         wc.cbClsExtra = 0;
         wc.cbWndExtra = 0;
         wc.hInstance = GetModuleHandle(NULL);
         wc.hIcon = 0;
         wc.hCursor = 0;
         wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
         wc.lpszMenuName = NULL;
         wc.lpszClassName = GL_TEMP_WINDOW_CLASS;
         wc.hIconSm = 0;
         RegisterClassEx(&wc);
      }
   }

   HWND hwnd = CreateWindowExA(0,
                               GL_TEMP_WINDOW_CLASS,
                               "glWindow",
                               0,
                               0, 0, 0, 0,
                               NULL, NULL, GetModuleHandle(NULL), NULL);
   HDC hdc = GetDC(hwnd);
   HGLRC shareContext = NULL;
   if (shared)
      shareContext = shared->ctx;

   // We must set the windows pixel format before we can create a WGL context
   gc->pxfi = pxfi;
   SetPixelFormat(hdc, gc->pxfi, NULL);

   gc->ctx = wglCreateContextAttribsARB(hdc, shareContext, attribList);

   ReleaseDC(hwnd, hdc);
   DestroyWindow(hwnd);

   if (!gc->ctx)
   {
     free(gc);
     return NULL;
   }

   return gc;
}

void
windows_destroy_context(windowsContext *context)
{
   wglDeleteContext(context->ctx);
   context->ctx = NULL;
   free(context);
}

int windows_bind_context(windowsContext *context, windowsDrawable *draw, windowsDrawable *read)
{
   HDC drawDc = draw->callbacks->getdc(draw);

   if (!draw->pxfi)
   {
      SetPixelFormat(drawDc, context->pxfi, NULL);
      draw->pxfi = context->pxfi;
   }

   if ((read != NULL) &&  (read != draw))
   {
      /*
        If there is a separate read drawable, create a separate read DC, and
        use the wglMakeContextCurrent extension to make the context current
        drawing to one DC and reading from the other

        Should only occur when WGL_ARB_make_current_read extension is present
      */
      HDC readDc = read->callbacks->getdc(read);

      BOOL ret = wglMakeContextCurrentARB(drawDc, readDc, context->ctx);

      read->callbacks->releasedc(read, readDc);

      if (!ret) {
         printf("wglMakeContextCurrentARB error: %08x\n", GetLastError());
         return FALSE;
      }
   }
   else
   {
      /* Otherwise, just use wglMakeCurrent */
      BOOL ret = wglMakeCurrent(drawDc, context->ctx);
      if (!ret) {
         printf("wglMakeCurrent error: %08x\n", GetLastError());
         return FALSE;
      }
   }

   draw->callbacks->releasedc(draw, drawDc);

   windows_glapi_set_dispatch();

   return TRUE;
}

void windows_unbind_context(windowsContext * context)
{
   wglMakeCurrent(NULL, NULL);
}

/*
 *
 */

void
windows_swap_buffers(windowsDrawable *draw)
{
   HDC drawDc = GetDC(draw->hWnd);
   SwapBuffers(drawDc);
   ReleaseDC(draw->hWnd, drawDc);
}


typedef void (__stdcall * PFNGLADDSWAPHINTRECTWIN) (GLint x, GLint y,
                                                    GLsizei width,
                                                    GLsizei height);

static void
glAddSwapHintRectWIN(GLint x, GLint y, GLsizei width,
                            GLsizei height)
{
   PFNGLADDSWAPHINTRECTWIN proc = (PFNGLADDSWAPHINTRECTWIN)wglGetProcAddress("glAddSwapHintRectWIN");
   if (proc)
      proc(x, y, width, height);
}

void
windows_copy_subbuffer(windowsDrawable *draw,
                      int x, int y, int width, int height)
{
   glAddSwapHintRectWIN(x, y, width, height);
   windows_swap_buffers(draw);
}

/*
 * Helper function for calling a test function on an initial context
 */
static void
windows_call_with_context(void (*proc)(HDC, void *), void *args)
{
   // create window class
#define WIN_GL_TEST_WINDOW_CLASS "GLTest"
   {
      static wATOM glTestWndClass = 0;

      if (glTestWndClass == 0) {
         WNDCLASSEX wc;

         wc.cbSize = sizeof(WNDCLASSEX);
         wc.style = CS_HREDRAW | CS_VREDRAW;
         wc.lpfnWndProc = DefWindowProc;
         wc.cbClsExtra = 0;
         wc.cbWndExtra = 0;
         wc.hInstance = GetModuleHandle(NULL);
         wc.hIcon = 0;
         wc.hCursor = 0;
         wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
         wc.lpszMenuName = NULL;
         wc.lpszClassName = WIN_GL_TEST_WINDOW_CLASS;
         wc.hIconSm = 0;
         glTestWndClass = RegisterClassEx(&wc);
      }
   }

   // create an invisible window for a scratch DC
   HWND hwnd = CreateWindowExA(0,
                               WIN_GL_TEST_WINDOW_CLASS,
                               "GL Renderer Capabilities Test Window",
                               0, 0, 0, 0, 0, NULL, NULL, GetModuleHandle(NULL),
                               NULL);
   if (hwnd) {
      HDC hdc = GetDC(hwnd);

      // we must set a pixel format before we can create a context, just use the first one...
      SetPixelFormat(hdc, 1, NULL);
      HGLRC hglrc = wglCreateContext(hdc);
      wglMakeCurrent(hdc, hglrc);

      // call the test function
      proc(hdc, args);

      // clean up
      wglMakeCurrent(NULL, NULL);
      wglDeleteContext(hglrc);
      ReleaseDC(hwnd, hdc);
      DestroyWindow(hwnd);
   }
}

static void
windows_check_render_test(HDC hdc, void *args)
{
   int *result = (int *)args;

   /* Rather than play linkage games using stdcall to ensure we get
      glGetString from opengl32.dll here, use dlsym */
   void *dlhandle = windows_get_dl_handle();
   const char *(*proc)(int) = dlsym(dlhandle, "glGetString");
   const char *gl_renderer = (const char *)proc(GL_RENDERER);

   if ((!gl_renderer) || (strcasecmp(gl_renderer, "GDI Generic") == 0))
      *result = FALSE;
   else
      *result = TRUE;
}

int
windows_check_renderer(void)
{
   int result;
   windows_call_with_context(windows_check_render_test, &result);
   return result;
}

typedef struct {
   char *gl_extensions;
   char *wgl_extensions;
} windows_extensions_result;

static void
windows_extensions_test(HDC hdc, void *args)
{
   windows_extensions_result *r = (windows_extensions_result *)args;

   void *dlhandle = windows_get_dl_handle();
   const char *(*proc)(int) = dlsym(dlhandle, "glGetString");

   r->gl_extensions = strdup(proc(GL_EXTENSIONS));

   wglResolveExtensionProcs();
   r->wgl_extensions = strdup(wglGetExtensionsStringARB(hdc));
}

void
windows_extensions(char **gl_extensions, char **wgl_extensions)
{
   windows_extensions_result result;

   *gl_extensions = "";
   *wgl_extensions = "";

   windows_call_with_context(windows_extensions_test, &result);

   *gl_extensions = result.gl_extensions;
   *wgl_extensions = result.wgl_extensions;
}

void windows_setTexBuffer(windowsContext *context, int textureTarget,
                         int textureFormat, windowsDrawable *drawable)
{
   // not yet implemented
}

void windows_releaseTexBuffer(windowsContext *context, int textureTarget,
                             windowsDrawable *drawable)
{
   // not yet implemented
}