Debian Codegraph @

gdk/win32/gdkglcontext-win32.c

raw
1 /* GDK - The GIMP Drawing Kit
2 *
3 * gdkglcontext-win32.c: Win32 specific OpenGL wrappers
4 *
5 * Copyright © 2014 Emmanuele Bassi
6 * Copyright © 2014 Alexander Larsson
7 * Copyright © 2014 Chun-wei Fan
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "config.h"
24
25 #include "gdkprivate-win32.h"
26 #include "gdkwindow-win32.h"
27 #include "gdkglcontext-win32.h"
28 #include "gdkdisplay-win32.h"
29
30 #include "gdkwin32display.h"
31 #include "gdkwin32glcontext.h"
32 #include "gdkwin32misc.h"
33 #include "gdkwin32screen.h"
34 #include "gdkwin32window.h"
35
36 #include "gdkglcontext.h"
37 #include "gdkwindow.h"
38 #include "gdkinternals.h"
39 #include "gdkintl.h"
40
41 #include <cairo.h>
42 #include <epoxy/wgl.h>
43
44 #ifdef GDK_WIN32_ENABLE_EGL
45 # include <epoxy/egl.h>
46 #endif
47
48 struct _GdkWin32GLContext
49 {
50 GdkGLContext parent_instance;
51
52 HDC gl_hdc;
53 guint need_alpha_bits : 1;
54 guint is_attached : 1;
55 guint do_frame_sync : 1;
56 guint do_blit_swap : 1;
57 };
58
59 struct _GdkWin32GLContextClass
60 {
61 GdkGLContextClass parent_class;
62 };
63
64 G_DEFINE_TYPE (GdkWin32GLContext, gdk_win32_gl_context, GDK_TYPE_GL_CONTEXT)
65
66 static void
67 gdk_win32_gl_context_cleanup (GdkGLContext *context)
68 {
69 GdkWindow *window = gdk_gl_context_get_window (context);
70
71 if (window != NULL)
72 {
73 GdkWindowImplWin32 *impl = NULL;
74
75 ReleaseDC (GDK_WINDOW_HWND (window), GDK_WIN32_GL_CONTEXT(context)->gl_hdc);
76 impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
77
78 if (impl->suppress_layered > 0)
79 impl->suppress_layered--;
80
81 /* If we don't have any window that forces layered windows off,
82 * trigger update_style_bits() to enable layered windows again
83 */
84 if (impl->suppress_layered == 0)
85 _gdk_win32_window_update_style_bits (window);
86 }
87 }
88
89 static void
90 gdk_gl_blit_region (GdkWindow *window,
91 cairo_region_t *region,
92 gboolean use_intel_workaround)
93 {
94 int n_rects, i, j;
95 int scale = gdk_window_get_scale_factor (window);
96 int wh = gdk_window_get_height (window);
97 cairo_rectangle_int_t rect;
98 int retries = 0;
99
100 if (use_intel_workaround)
101 retries = 1;
102
103 n_rects = cairo_region_num_rectangles (region);
104
105 for (i = 0; i <= retries; i ++)
106 {
107 for (j = 0; j < n_rects; j++)
108 {
109 cairo_region_get_rectangle (region, j, &rect);
110 glScissor (rect.x * scale, (wh - rect.y - rect.height) * scale, rect.width * scale, rect.height * scale);
111 glBlitFramebuffer (rect.x * scale, (wh - rect.y - rect.height) * scale, (rect.x + rect.width) * scale, (wh - rect.y) * scale,
112 rect.x * scale, (wh - rect.y - rect.height) * scale, (rect.x + rect.width) * scale, (wh - rect.y) * scale,
113 GL_COLOR_BUFFER_BIT, GL_NEAREST);
114 }
115
116 if (retries > 0 && i < retries)
117 glFlush ();
118 }
119 }
120
121 /* Section on WGL usage */
122 struct _GdkWin32GLContextWGL
123 {
124 GdkWin32GLContext parent_instance;
125
126 HGLRC wgl_context;
127 };
128
129 typedef struct _GdkWin32GLContextClass GdkWin32GLContextWGLClass;
130 typedef struct _GdkWin32GLContextWGL GdkWin32GLContextWGL;
131
132 G_DEFINE_TYPE (GdkWin32GLContextWGL, gdk_win32_gl_context_wgl, GDK_TYPE_WIN32_GL_CONTEXT)
133
134 #define GDK_TYPE_WIN32_GL_CONTEXT_WGL (gdk_win32_gl_context_wgl_get_type())
135 #define GDK_WIN32_GL_CONTEXT_WGL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GDK_TYPE_WIN32_GL_CONTEXT_WGL, GdkWin32GLContextWGL))
136 #define GDK_IS_WIN32_GL_CONTEXT_WGL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GDK_TYPE_WIN32_GL_CONTEXT_WGL))
137
138 typedef struct
139 {
140 ATOM wc_atom;
141 HWND hwnd;
142 HDC hdc;
143 HGLRC hglrc;
144 gboolean inited;
145 } GdkWGLDummy;
146
147 static void
148 destroy_dummy_gl_context (GdkWGLDummy dummy)
149 {
150 if (dummy.hglrc != NULL)
151 {
152 wglDeleteContext (dummy.hglrc);
153 dummy.hglrc = NULL;
154 }
155 if (dummy.hdc != NULL)
156 {
157 DeleteDC (dummy.hdc);
158 dummy.hdc = NULL;
159 }
160 if (dummy.hwnd != NULL)
161 {
162 DestroyWindow (dummy.hwnd);
163 dummy.hwnd = NULL;
164 }
165 if (dummy.wc_atom != 0)
166 {
167 UnregisterClass (MAKEINTATOM (dummy.wc_atom), GetModuleHandle (NULL));
168 dummy.wc_atom = 0;
169 }
170 dummy.inited = FALSE;
171 }
172
173 /* Yup, we need to create a dummy window for the dummy WGL context */
174 static void
175 get_dummy_window_hwnd (GdkWGLDummy *dummy)
176 {
177 WNDCLASSEX dummy_wc;
178
179 memset (&dummy_wc, 0, sizeof (WNDCLASSEX));
180
181 dummy_wc.cbSize = sizeof( WNDCLASSEX );
182 dummy_wc.style = CS_OWNDC;
183 dummy_wc.lpfnWndProc = (WNDPROC) DefWindowProc;
184 dummy_wc.cbClsExtra = 0;
185 dummy_wc.cbWndExtra = 0;
186 dummy_wc.hInstance = GetModuleHandle( NULL );
187 dummy_wc.hIcon = 0;
188 dummy_wc.hCursor = NULL;
189 dummy_wc.hbrBackground = 0;
190 dummy_wc.lpszMenuName = 0;
191 dummy_wc.lpszClassName = "dummy";
192 dummy_wc.hIconSm = 0;
193
194 dummy->wc_atom = RegisterClassEx (&dummy_wc);
195
196 dummy->hwnd =
197 CreateWindowEx (WS_EX_APPWINDOW,
198 MAKEINTATOM (dummy->wc_atom),
199 "",
200 WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
201 0,
202 0,
203 0,
204 0,
205 NULL,
206 NULL,
207 GetModuleHandle (NULL),
208 NULL);
209 }
210
211 static gint
212 gdk_init_dummy_context (GdkWGLDummy *dummy,
213 const gboolean need_alpha_bits);
214
215 #define PIXEL_ATTRIBUTES 19
216
217 static gint
218 get_wgl_pfd (HDC hdc,
219 const gboolean need_alpha_bits,
220 PIXELFORMATDESCRIPTOR *pfd,
221 GdkWin32Display *display)
222 {
223 gint best_pf = 0;
224
225 pfd->nSize = sizeof (PIXELFORMATDESCRIPTOR);
226
227 if (display != NULL && display->hasWglARBPixelFormat)
228 {
229 GdkWGLDummy dummy;
230 UINT num_formats;
231 gint colorbits = GetDeviceCaps (hdc, BITSPIXEL);
232 guint extra_fields = 1;
233 gint i = 0;
234 int pixelAttribs[PIXEL_ATTRIBUTES];
235 int alpha_idx = 0;
236
237 /* Save up the HDC and HGLRC that we are currently using, to restore back to it when we are done here */
238 HDC hdc_current = wglGetCurrentDC ();
239 HGLRC hglrc_current = wglGetCurrentContext ();
240
241 if (display->hasWglARBmultisample)
242 {
243 /* 2 pairs of values needed for multisampling/AA support */
244 extra_fields += 2 * 2;
245 }
246
247 /* Update PIXEL_ATTRIBUTES above if any groups are added here! */
248 /* one group contains a value pair for both pixelAttribs and pixelAttribsNoAlpha */
249 pixelAttribs[i] = WGL_DRAW_TO_WINDOW_ARB;
250 pixelAttribs[i++] = GL_TRUE;
251
252 pixelAttribs[i++] = WGL_SUPPORT_OPENGL_ARB;
253 pixelAttribs[i++] = GL_TRUE;
254
255 pixelAttribs[i++] = WGL_DOUBLE_BUFFER_ARB;
256 pixelAttribs[i++] = GL_TRUE;
257
258 pixelAttribs[i++] = WGL_ACCELERATION_ARB;
259 pixelAttribs[i++] = WGL_FULL_ACCELERATION_ARB;
260
261 pixelAttribs[i++] = WGL_PIXEL_TYPE_ARB;
262 pixelAttribs[i++] = WGL_TYPE_RGBA_ARB;
263
264 pixelAttribs[i++] = WGL_COLOR_BITS_ARB;
265 pixelAttribs[i++] = colorbits;
266
267 /* end of "Update PIXEL_ATTRIBUTES above if any groups are added here!" */
268
269 if (display->hasWglARBmultisample)
270 {
271 pixelAttribs[i++] = WGL_SAMPLE_BUFFERS_ARB;
272 pixelAttribs[i++] = 1;
273
274 pixelAttribs[i++] = WGL_SAMPLES_ARB;
275 pixelAttribs[i++] = 8;
276 }
277
278 pixelAttribs[i++] = WGL_ALPHA_BITS_ARB;
279
280 /* track the spot where the alpha bits are, so that we can clear it if needed */
281 alpha_idx = i;
282
283 pixelAttribs[i++] = 8;
284 pixelAttribs[i++] = 0; /* end of pixelAttribs */
285
286 memset (&dummy, 0, sizeof (GdkWGLDummy));
287
288 /* acquire and cache dummy Window (HWND & HDC) and
289 * dummy GL Context, we need it for wglChoosePixelFormatARB()
290 */
291 best_pf = gdk_init_dummy_context (&dummy, need_alpha_bits);
292
293 if (best_pf == 0 || !wglMakeCurrent (dummy.hdc, dummy.hglrc))
294 {
295 wglMakeCurrent (hdc_current, hglrc_current);
296 return 0;
297 }
298
299 wglChoosePixelFormatARB (hdc,
300 pixelAttribs,
301 NULL,
302 1,
303 &best_pf,
304 &num_formats);
305
306 if (best_pf == 0)
307 {
308 if (!need_alpha_bits)
309 {
310 pixelAttribs[alpha_idx] = 0;
311 pixelAttribs[alpha_idx + 1] = 0;
312
313 /* give another chance if need_alpha_bits is FALSE,
314 * meaning we prefer to have an alpha channel anyways
315 */
316 wglChoosePixelFormatARB (hdc,
317 pixelAttribs,
318 NULL,
319 1,
320 &best_pf,
321 &num_formats);
322
323 }
324 }
325
326 /* Go back to the HDC that we were using, since we are done with the dummy HDC and GL Context */
327 wglMakeCurrent (hdc_current, hglrc_current);
328 destroy_dummy_gl_context (dummy);
329 }
330 else
331 {
332 pfd->nVersion = 1;
333 pfd->dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER;
334 pfd->iPixelType = PFD_TYPE_RGBA;
335 pfd->cColorBits = GetDeviceCaps (hdc, BITSPIXEL);
336 pfd->cAlphaBits = 8;
337 pfd->dwLayerMask = PFD_MAIN_PLANE;
338
339 best_pf = ChoosePixelFormat (hdc, pfd);
340
341 if (best_pf == 0)
342 /* give another chance if need_alpha_bits is FALSE,
343 * meaning we prefer to have an alpha channel anyways
344 */
345 if (!need_alpha_bits)
346 {
347 pfd->cAlphaBits = 0;
348 best_pf = ChoosePixelFormat (hdc, pfd);
349 }
350 }
351
352 return best_pf;
353 }
354
355 /* in WGL, for many OpenGL items, we need a dummy WGL context, so create
356 * one and cache it for later use
357 */
358 static gint
359 gdk_init_dummy_context (GdkWGLDummy *dummy,
360 const gboolean need_alpha_bits)
361 {
362 PIXELFORMATDESCRIPTOR pfd;
363 gboolean set_pixel_format_result = FALSE;
364 gint best_idx = 0;
365
366 get_dummy_window_hwnd (dummy);
367
368 dummy->hdc = GetDC (dummy->hwnd);
369 memset (&pfd, 0, sizeof (PIXELFORMATDESCRIPTOR));
370
371 best_idx = get_wgl_pfd (dummy->hdc, need_alpha_bits, &pfd, NULL);
372
373 if (best_idx != 0)
374 set_pixel_format_result = SetPixelFormat (dummy->hdc,
375 best_idx,
376 &pfd);
377
378 if (best_idx == 0 || !set_pixel_format_result)
379 return 0;
380
381 dummy->hglrc = wglCreateContext (dummy->hdc);
382 if (dummy->hglrc == NULL)
383 return 0;
384
385 dummy->inited = TRUE;
386
387 return best_idx;
388 }
389
390 static void
391 gdk_win32_gl_context_dispose_wgl (GObject *gobject)
392 {
393 GdkGLContext *context = GDK_GL_CONTEXT (gobject);
394 GdkWin32GLContextWGL *context_wgl = GDK_WIN32_GL_CONTEXT_WGL (context);
395
396 if (context_wgl->wgl_context != NULL)
397 {
398 GdkWindow *window = gdk_gl_context_get_window (context);
399
400 if (gdk_win32_private_wglGetCurrentContext () == context_wgl->wgl_context)
401 gdk_win32_private_wglMakeCurrent (NULL, NULL);
402
403 GDK_NOTE (OPENGL, g_print ("Destroying WGL context\n"));
404
405 gdk_win32_private_wglDeleteContext (context_wgl->wgl_context);
406 context_wgl->wgl_context = NULL;
407
408 gdk_win32_gl_context_cleanup (context);
409 }
410
411 G_OBJECT_CLASS (gdk_win32_gl_context_parent_class)->dispose (gobject);
412 }
413
414 static void
415 gdk_win32_gl_context_end_frame_wgl (GdkGLContext *context,
416 cairo_region_t *painted,
417 cairo_region_t *damage)
418 {
419 GdkWin32Display *display_win32 = (GDK_WIN32_DISPLAY (gdk_gl_context_get_display (context)));
420 GdkWin32GLContext *context_win32 = GDK_WIN32_GL_CONTEXT (context);
421
422 gdk_gl_context_make_current (context);
423
424 if (context_win32->do_frame_sync)
425 {
426 glFinish ();
427
428 if (display_win32->hasWglOMLSyncControl)
429 {
430 gint64 ust, msc, sbc;
431
432 wglGetSyncValuesOML (context_win32->gl_hdc, &ust, &msc, &sbc);
433 wglWaitForMscOML (context_win32->gl_hdc,
434 0,
435 2,
436 (msc + 1) % 2,
437 &ust, &msc, &sbc);
438 }
439 }
440
441 if (context_win32->do_blit_swap)
442 {
443 GdkWindow *window = gdk_gl_context_get_window (context);
444
445 glDrawBuffer(GL_FRONT);
446 glReadBuffer(GL_BACK);
447 gdk_gl_blit_region (window, painted, display_win32->needIntelGLWorkaround);
448 glDrawBuffer(GL_BACK);
449 glFlush();
450
451 if (gdk_gl_context_has_frame_terminator (context))
452 glFrameTerminatorGREMEDY ();
453 }
454 else
455 SwapBuffers (context_win32->gl_hdc);
456 }
457
458 /*
459 * We need to check whether the OpenGL driver is capable of delvering
460 * what we need to use in GdkGLContext. This function in the future
461 * will also check for blacklisted drivers, if needed. If things fail
462 * here, we can try falling back to OpenGL/ES (libANGLE) if that is enabled.
463 */
464 static gboolean
465 gdk_win32_gl_context_wgl_check_capabilities (GdkWin32Display *display_win32,
466 GdkWGLDummy dummy)
467 {
468 if (!wglMakeCurrent (dummy.hdc, dummy.hglrc))
469 return FALSE;
470
471 display_win32->gl_version = epoxy_gl_version ();
472
473 /* We must have OpenGL/WGL 2.0 or later, or have the GL_ARB_shader_objects extension */
474 if (display_win32->gl_version < 20 && !epoxy_has_gl_extension ("GL_ARB_shader_objects"))
475 return FALSE;
476
477 return TRUE;
478 }
479
480 static void
481 gdk_win32_display_init_wgl (GdkWin32Display *display_win32,
482 const gboolean need_alpha_bits)
483 {
484 /* acquire and cache dummy Window (HWND & HDC) and
485 * dummy GL Context, it is used to query functions
486 * and used for other stuff as well
487 */
488 GdkWGLDummy dummy;
489 gint best_idx = 0;
490 gboolean proceed = TRUE;
491
492 memset (&dummy, 0, sizeof (GdkWGLDummy));
493
494 best_idx = gdk_init_dummy_context (&dummy, need_alpha_bits);
495
496 if (best_idx != 0)
497 proceed = gdk_win32_gl_context_wgl_check_capabilities (display_win32, dummy);
498
499 if (best_idx != 0 && proceed)
500 {
501 display_win32->hasWglARBCreateContext =
502 epoxy_has_wgl_extension (dummy.hdc, "WGL_ARB_create_context");
503 display_win32->hasWglEXTSwapControl =
504 epoxy_has_wgl_extension (dummy.hdc, "WGL_EXT_swap_control");
505 display_win32->hasWglOMLSyncControl =
506 epoxy_has_wgl_extension (dummy.hdc, "WGL_OML_sync_control");
507 display_win32->hasWglARBPixelFormat =
508 epoxy_has_wgl_extension (dummy.hdc, "WGL_ARB_pixel_format");
509 display_win32->hasWglARBmultisample =
510 epoxy_has_wgl_extension (dummy.hdc, "WGL_ARB_multisample");
511 display_win32->needIntelGLWorkaround =
512 (g_ascii_strcasecmp (glGetString (GL_VENDOR), "intel") == 0);
513
514 GDK_NOTE (OPENGL,
515 g_print ("WGL API version %d.%d found\n"
516 " - Vendor: %s\n"
517 " - Intel OpenGL workaround: %s\n"
518 " - Checked extensions:\n"
519 "\t* WGL_ARB_pixel_format: %s\n"
520 "\t* WGL_ARB_create_context: %s\n"
521 "\t* WGL_EXT_swap_control: %s\n"
522 "\t* WGL_OML_sync_control: %s\n"
523 "\t* WGL_ARB_multisample: %s\n",
524 display_win32->gl_version / 10,
525 display_win32->gl_version % 10,
526 glGetString (GL_VENDOR),
527 display_win32->needIntelGLWorkaround ? "yes" : "no",
528 display_win32->hasWglARBPixelFormat ? "yes" : "no",
529 display_win32->hasWglARBCreateContext ? "yes" : "no",
530 display_win32->hasWglEXTSwapControl ? "yes" : "no",
531 display_win32->hasWglOMLSyncControl ? "yes" : "no",
532 display_win32->hasWglARBmultisample ? "yes" : "no"));
533
534 display_win32->gl_type = GDK_WIN32_GL_WGL;
535 }
536 else
537 display_win32->gl_type = GDK_WIN32_GL_NONE;
538
539 /* clean up things if we indeed created our dummy WGL context */
540 if (best_idx != 0)
541 {
542 wglMakeCurrent (NULL, NULL);
543 destroy_dummy_gl_context (dummy);
544 }
545 }
546
547 /* Setup the legacy WGL context after creating it */
548 static gboolean
549 ensure_legacy_wgl_context (HDC hdc,
550 HGLRC hglrc_legacy,
551 GdkGLContext *share)
552 {
553 if (!wglMakeCurrent (hdc, hglrc_legacy))
554 return FALSE;
555
556 if (share != NULL)
557 {
558 GdkWin32GLContextWGL *context_wgl = GDK_WIN32_GL_CONTEXT_WGL (share);
559
560 return wglShareLists (hglrc_legacy, context_wgl->wgl_context);
561 }
562
563 return TRUE;
564 }
565
566 static HGLRC
567 create_wgl_context_with_attribs (HDC hdc,
568 HGLRC hglrc_base,
569 GdkGLContext *share,
570 int flags,
571 int major,
572 int minor,
573 gboolean *is_legacy)
574 {
575 HGLRC hglrc;
576 GdkWin32GLContextWGL *context_wgl;
577
578 /* if we have wglCreateContextAttribsARB(), create a
579 * context with the compatibility profile if a legacy
580 * context is requested, or when we go into fallback mode
581 */
582 int profile = *is_legacy ? WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB :
583 WGL_CONTEXT_CORE_PROFILE_BIT_ARB;
584
585 int attribs[] = {
586 WGL_CONTEXT_PROFILE_MASK_ARB, profile,
587 WGL_CONTEXT_MAJOR_VERSION_ARB, *is_legacy ? 3 : major,
588 WGL_CONTEXT_MINOR_VERSION_ARB, *is_legacy ? 0 : minor,
589 WGL_CONTEXT_FLAGS_ARB, flags,
590 0
591 };
592
593 if (share != NULL)
594 context_wgl= GDK_WIN32_GL_CONTEXT_WGL (share);
595
596 hglrc = wglCreateContextAttribsARB (hdc,
597 share != NULL ? context_wgl->wgl_context : NULL,
598 attribs);
599
600 return hglrc;
601 }
602
603 static HGLRC
604 create_wgl_context (HDC hdc,
605 GdkGLContext *share,
606 int flags,
607 int major,
608 int minor,
609 gboolean *is_legacy,
610 gboolean hasWglARBCreateContext)
611 {
612 /* We need a legacy WGL context for *all* cases */
613 HGLRC hglrc_base = wglCreateContext (hdc);
614 gboolean success = TRUE;
615
616 /* Save up the HDC and HGLRC that we are currently using, to restore back to it when we are done here */
617 HDC hdc_current = wglGetCurrentDC ();
618 HGLRC hglrc_current = wglGetCurrentContext ();
619
620 /* if we have no wglCreateContextAttribsARB(), return the legacy context when all is set */
621 if (*is_legacy && !hasWglARBCreateContext)
622 {
623 if (ensure_legacy_wgl_context (hdc, hglrc_base, share))
624 {
625 wglMakeCurrent (hdc_current, hglrc_current);
626 return hglrc_base;
627 }
628
629 success = FALSE;
630 goto gl_fail;
631 }
632 else
633 {
634 HGLRC hglrc;
635
636 if (!wglMakeCurrent (hdc, hglrc_base))
637 {
638 success = FALSE;
639 goto gl_fail;
640 }
641
642 hglrc = create_wgl_context_with_attribs (hdc,
643 hglrc_base,
644 share,
645 flags,
646 major,
647 minor,
648 is_legacy);
649
650 /* return the legacy context we have if it could be setup properly, in case the 3.0+ context creation failed */
651 if (hglrc == NULL)
652 {
653 if (!(*is_legacy))
654 {
655 /* If we aren't using a legacy context in the beginning, try again with a compatibility profile 3.0 context */
656 hglrc = create_wgl_context_with_attribs (hdc,
657 hglrc_base,
658 share,
659 flags,
660 0, 0,
661 is_legacy);
662
663 *is_legacy = TRUE;
664 }
665
666 if (hglrc == NULL)
667 {
668 if (!ensure_legacy_wgl_context (hdc, hglrc_base, share))
669 success = FALSE;
670 }
671
672 if (success)
673 GDK_NOTE (OPENGL, g_print ("Using legacy context as fallback\n"));
674 }
675
676 gl_fail:
677
678 if (!success)
679 {
680 wglMakeCurrent (NULL, NULL);
681 wglDeleteContext (hglrc_base);
682 return NULL;
683 }
684
685 wglMakeCurrent (hdc_current, hglrc_current);
686
687 if (hglrc != NULL)
688 {
689 wglDeleteContext (hglrc_base);
690 return hglrc;
691 }
692
693 return hglrc_base;
694 }
695 }
696
697 static gboolean
698 set_pixformat_for_hdc (HDC hdc,
699 gint *best_idx,
700 const gboolean need_alpha_bits,
701 GdkWin32Display *display)
702 {
703 gboolean already_checked = TRUE;
704 *best_idx = GetPixelFormat (hdc);
705
706 /* one is only allowed to call SetPixelFormat(), and so ChoosePixelFormat()
707 * one single time per window HDC
708 */
709 if (*best_idx == 0)
710 {
711 PIXELFORMATDESCRIPTOR pfd;
712 gboolean set_pixel_format_result = FALSE;
713
714 GDK_NOTE (OPENGL, g_print ("requesting pixel format...\n"));
715 already_checked = FALSE;
716 *best_idx = get_wgl_pfd (hdc, need_alpha_bits, &pfd, display);
717
718 if (*best_idx != 0)
719 set_pixel_format_result = SetPixelFormat (hdc, *best_idx, &pfd);
720
721 /* ChoosePixelFormat() or SetPixelFormat() failed, bail out */
722 if (*best_idx == 0 || !set_pixel_format_result)
723 return FALSE;
724 }
725
726 GDK_NOTE (OPENGL, g_print ("%s""requested and set pixel format: %d\n", already_checked ? "already " : "", *best_idx));
727
728 return TRUE;
729 }
730
731 static gboolean
732 gdk_win32_gl_context_realize_wgl (GdkGLContext *context,
733 GError **error)
734 {
735 GdkGLContext *share = gdk_gl_context_get_shared_context (context);
736 GdkWin32GLContext *context_win32 = GDK_WIN32_GL_CONTEXT (context);
737 GdkWindow *window = gdk_gl_context_get_window (context);
738 GdkWindowImplWin32 *impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
739 GdkWin32Display *display_win32 = GDK_WIN32_DISPLAY (gdk_window_get_display (window));
740
741 /* These are the real WGL context items that we will want to use later */
742 gboolean debug_bit, compat_bit;
743 gboolean legacy_bit = FALSE;
744 HGLRC hglrc;
745 gint pixel_format;
746
747 /* request flags and specific versions for core (3.2+) WGL context */
748 gint flags = 0;
749 gint major = 0;
750 gint minor = 0;
751
752 /*
753 * A legacy context cannot be shared with core profile ones, this means we
754 * must stick to a legacy context if the shared context is a legacy context. We
755 * also use a legacy context if we use GDK_GL=legacy or if we do not have
756 * the WGL_ARB_create_context extension
757 */
758 if ((_gdk_gl_flags & GDK_GL_LEGACY) != 0 ||
759 !display_win32->hasWglARBCreateContext ||
760 share != NULL && gdk_gl_context_is_legacy (share))
761 legacy_bit = TRUE;
762
763 /* say early enough that we are not using GLES, so that we acquire the correct versions */
764 gdk_gl_context_set_use_es (context, FALSE);
765
766 gdk_gl_context_get_required_version (context, &major, &minor);
767 debug_bit = gdk_gl_context_get_debug_enabled (context);
768 compat_bit = gdk_gl_context_get_forward_compatible (context);
769
770 if (!set_pixformat_for_hdc (context_win32->gl_hdc,
771 &pixel_format,
772 context_win32->need_alpha_bits,
773 display_win32))
774 {
775 g_set_error_literal (error, GDK_GL_ERROR,
776 GDK_GL_ERROR_UNSUPPORTED_FORMAT,
777 _("No available configurations for the given pixel format"));
778
779 return FALSE;
780 }
781
782 if (debug_bit)
783 flags |= WGL_CONTEXT_DEBUG_BIT_ARB;
784 if (compat_bit)
785 flags |= WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB;
786
787 GDK_NOTE (OPENGL,
788 g_print ("Creating %s WGL context (version:%d.%d, debug:%s, forward:%s, legacy: %s)\n",
789 compat_bit ? "core" : "compat",
790 major,
791 minor,
792 debug_bit ? "yes" : "no",
793 compat_bit ? "yes" : "no",
794 legacy_bit ? "yes" : "no"));
795
796 hglrc = create_wgl_context (context_win32->gl_hdc,
797 share,
798 flags,
799 major,
800 minor,
801 &legacy_bit,
802 display_win32->hasWglARBCreateContext);
803
804 if (hglrc == NULL)
805 {
806 g_set_error_literal (error, GDK_GL_ERROR,
807 GDK_GL_ERROR_NOT_AVAILABLE,
808 _("Unable to create a GL context"));
809 return FALSE;
810 }
811
812 GDK_NOTE (OPENGL,
813 g_print ("Created WGL context[%p], pixel_format=%d\n",
814 hglrc,
815 pixel_format));
816
817 GDK_WIN32_GL_CONTEXT_WGL (context)->wgl_context = hglrc;
818
819 /* OpenGL does not work with WS_EX_LAYERED enabled, so we need to
820 * disable WS_EX_LAYERED when we acquire a valid HGLRC
821 */
822 impl->suppress_layered++;
823
824 /* if this is the first time a GL context is acquired for the window,
825 * disable layered windows by triggering update_style_bits()
826 */
827 if (impl->suppress_layered == 1)
828 _gdk_win32_window_update_style_bits (window);
829
830 /* Ensure that any other context is created with a legacy bit set */
831 gdk_gl_context_set_is_legacy (context, legacy_bit);
832
833 return TRUE;
834 }
835
836 gboolean
837 gdk_win32_display_is_wgl_context_current (GdkDisplay *display,
838 GdkGLContext *context)
839 {
840 GdkWin32GLContextWGL *context_wgl = GDK_WIN32_GL_CONTEXT_WGL (context);
841
842 return context_wgl->wgl_context == wglGetCurrentContext ();
843 }
844
845 static gboolean
846 gdk_win32_display_make_wgl_context_current (GdkDisplay *display,
847 GdkGLContext *context)
848 {
849 GdkWin32GLContext *context_win32;
850 GdkWin32Display *display_win32 = GDK_WIN32_DISPLAY (display);
851 GdkWindow *window;
852
853 if (context == NULL)
854 {
855 gdk_win32_private_wglMakeCurrent (NULL, NULL);
856
857 return TRUE;
858 }
859
860 context_win32 = GDK_WIN32_GL_CONTEXT (context);
861 window = gdk_gl_context_get_window (context);
862
863 if (!gdk_win32_private_wglMakeCurrent (context_win32->gl_hdc,
864 GDK_WIN32_GL_CONTEXT_WGL (context)->wgl_context))
865 {
866 GDK_NOTE (OPENGL, g_print ("Making WGL context current failed\n"));
867 return FALSE;
868 }
869
870 if (context_win32->is_attached)
871 {
872 if (display_win32->hasWglEXTSwapControl)
873 {
874 /* If there is compositing there is no particular need to delay
875 * the swap when drawing on the offscreen, rendering to the screen
876 * happens later anyway, and its up to the compositor to sync that
877 * to the vblank. */
878 gboolean do_frame_sync = !gdk_screen_is_composited (gdk_window_get_screen (window));
879
880 if (do_frame_sync != context_win32->do_frame_sync)
881 {
882 context_win32->do_frame_sync = do_frame_sync;
883
884 if (do_frame_sync)
885 wglSwapIntervalEXT (1);
886 else
887 wglSwapIntervalEXT (0);
888 }
889 }
890 }
891
892 return TRUE;
893 }
894
895 static void
896 gdk_win32_gl_context_wgl_class_init (GdkWin32GLContextWGLClass *klass)
897 {
898 GdkGLContextClass *context_class = GDK_GL_CONTEXT_CLASS (klass);
899 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
900
901 context_class->end_frame = gdk_win32_gl_context_end_frame_wgl;
902 context_class->realize = gdk_win32_gl_context_realize_wgl;
903
904 gobject_class->dispose = gdk_win32_gl_context_dispose_wgl;
905 }
906
907 static void
908 gdk_win32_gl_context_wgl_init (GdkWin32GLContextWGL *wgl_context)
909 {
910 }
911
912 /* End section on WGL support */
913 /* Section on EGL/libANGLE support */
914 #ifdef GDK_WIN32_ENABLE_EGL
915
916 #ifndef EGL_PLATFORM_ANGLE_ANGLE
917 #define EGL_PLATFORM_ANGLE_ANGLE 0x3202
918 #endif
919
920 #ifndef EGL_PLATFORM_ANGLE_TYPE_ANGLE
921 #define EGL_PLATFORM_ANGLE_TYPE_ANGLE 0x3203
922 #endif
923
924 #ifndef EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE
925 #define EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE 0x3208
926 #endif
927
928 struct _GdkWin32GLContextEGL
929 {
930 GdkWin32GLContext parent_instance;
931
932 EGLContext egl_context;
933 };
934
935 #define GDK_TYPE_WIN32_GL_CONTEXT_EGL (gdk_win32_gl_context_egl_get_type())
936 #define GDK_WIN32_GL_CONTEXT_EGL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GDK_TYPE_WIN32_GL_CONTEXT_EGL, GdkWin32GLContextEGL))
937 #define GDK_IS_WIN32_GL_CONTEXT_EGL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GDK_TYPE_WIN32_GL_CONTEXT_EGL))
938
939 typedef struct _GdkWin32GLContextClass GdkWin32GLContextEGLClass;
940 typedef struct _GdkWin32GLContextEGL GdkWin32GLContextEGL;
941
942 G_DEFINE_TYPE (GdkWin32GLContextEGL, gdk_win32_gl_context_egl, GDK_TYPE_WIN32_GL_CONTEXT)
943
944 static EGLDisplay
945 gdk_win32_get_egl_display (GdkWin32Display *display)
946 {
947 EGLDisplay disp;
948 gboolean success = FALSE;
949
950 if (epoxy_has_egl_extension (NULL, "EGL_EXT_platform_base"))
951 {
952 PFNEGLGETPLATFORMDISPLAYEXTPROC getPlatformDisplay = (void *) eglGetProcAddress ("eglGetPlatformDisplayEXT");
953 if (getPlatformDisplay)
954 {
955 EGLint disp_attr[] = {EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE, EGL_NONE};
956
957 disp = getPlatformDisplay (EGL_PLATFORM_ANGLE_ANGLE, display->hdc_egl_temp, disp_attr);
958
959 if (disp != EGL_NO_DISPLAY)
960 return disp;
961 }
962 }
963
964 return eglGetDisplay (display->hdc_egl_temp);
965 }
966
967 static void
968 gdk_win32_gl_context_dispose_egl (GObject *gobject)
969 {
970 GdkGLContext *context = GDK_GL_CONTEXT (gobject);
971 GdkWin32GLContextEGL *context_egl = GDK_WIN32_GL_CONTEXT_EGL (context);
972
973 if (context_egl->egl_context != EGL_NO_CONTEXT)
974 {
975 GdkWin32Display *display_win32 = GDK_WIN32_DISPLAY (gdk_gl_context_get_display (context));
976
977 if (eglGetCurrentContext () == context_egl->egl_context)
978 eglMakeCurrent(display_win32->egl_disp, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
979
980 GDK_NOTE (OPENGL, g_message ("Destroying EGL (ANGLE) context"));
981
982 eglDestroyContext (display_win32->egl_disp, context_egl->egl_context);
983 context_egl->egl_context = EGL_NO_CONTEXT;
984
985 gdk_win32_gl_context_cleanup (context);
986 }
987
988 G_OBJECT_CLASS (gdk_win32_gl_context_parent_class)->dispose (gobject);
989 }
990
991 static gboolean
992 get_is_egl_force_redraw (GdkWindow *window)
993 {
994 /* We only need to call gdk_window_invalidate_rect () if necessary */
995 if (window->gl_paint_context != NULL && gdk_gl_context_get_use_es (window->gl_paint_context))
996 {
997 GdkWindowImplWin32 *impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
998
999 return impl->egl_force_redraw_all;
1000 }
1001
1002 return FALSE;
1003 }
1004
1005 static void
1006 reset_egl_force_redraw (GdkWindow *window)
1007 {
1008 if (window->gl_paint_context != NULL && gdk_gl_context_get_use_es (window->gl_paint_context))
1009 {
1010 GdkWindowImplWin32 *impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
1011
1012 if (impl->egl_force_redraw_all)
1013 impl->egl_force_redraw_all = FALSE;
1014 }
1015 }
1016
1017 static void
1018 gdk_win32_gl_context_end_frame_egl (GdkGLContext *context,
1019 cairo_region_t *painted,
1020 cairo_region_t *damage)
1021 {
1022 GdkWindow *window = gdk_gl_context_get_window (context);
1023 GdkWin32Display *display_win32 = (GDK_WIN32_DISPLAY (gdk_gl_context_get_display (context)));
1024 GdkWin32GLContext *context_win32 = GDK_WIN32_GL_CONTEXT (context);
1025
1026 EGLSurface egl_surface = _gdk_win32_window_get_egl_surface (window, display_win32->egl_config, FALSE);
1027 gboolean force_egl_redraw_all = get_is_egl_force_redraw (window);
1028
1029 gdk_gl_context_make_current (context);
1030
1031 if (context_win32->do_blit_swap && !force_egl_redraw_all)
1032 gdk_gl_blit_region (window, painted, FALSE);
1033 else if (force_egl_redraw_all)
1034 {
1035 GdkRectangle rect = {0, 0, gdk_window_get_width (window), gdk_window_get_height (window)};
1036
1037 /* We need to do gdk_window_invalidate_rect() so that we don't get glitches after maximizing or
1038 * restoring or using aerosnap
1039 */
1040 gdk_window_invalidate_rect (window, &rect, TRUE);
1041 reset_egl_force_redraw (window);
1042 }
1043
1044 eglSwapBuffers (display_win32->egl_disp, egl_surface);
1045 }
1046
1047 static void
1048 gdk_win32_display_init_egl (GdkWin32Display *display_win32)
1049 {
1050 EGLDisplay egl_disp;
1051
1052 if (display_win32->gl_type == GDK_WIN32_GL_NONE)
1053 GDK_NOTE (OPENGL, g_message ("Falling back to GLES..."));
1054
1055 if (!epoxy_has_egl ())
1056 {
1057 GDK_NOTE (OPENGL, g_message ("EGL/GLES support not present in this system"));
1058 return;
1059 }
1060
1061 egl_disp = gdk_win32_get_egl_display (display_win32);
1062
1063 if (egl_disp == EGL_NO_DISPLAY ||
1064 !eglInitialize (egl_disp, NULL, NULL))
1065 {
1066 if (egl_disp != EGL_NO_DISPLAY)
1067 {
1068 eglTerminate (egl_disp);
1069 egl_disp = EGL_NO_DISPLAY;
1070 }
1071
1072 display_win32->gl_type = GDK_WIN32_GL_NONE;
1073 return;
1074 }
1075
1076 display_win32->egl_disp = egl_disp;
1077 display_win32->gl_version = epoxy_egl_version (egl_disp);
1078
1079 eglBindAPI(EGL_OPENGL_ES_API);
1080
1081 display_win32->hasEglSurfacelessContext =
1082 epoxy_has_egl_extension (egl_disp, "EGL_KHR_surfaceless_context");
1083
1084 GDK_NOTE (OPENGL,
1085 g_print ("EGL API version %d.%d found\n"
1086 " - Vendor: %s\n"
1087 " - Checked extensions:\n"
1088 "\t* EGL_KHR_surfaceless_context: %s\n",
1089 display_win32->gl_version / 10,
1090 display_win32->gl_version % 10,
1091 eglQueryString (display_win32->egl_disp, EGL_VENDOR),
1092 display_win32->hasEglSurfacelessContext ? "yes" : "no"));
1093
1094 display_win32->gl_type = GDK_WIN32_GL_EGL;
1095 }
1096
1097 #define MAX_EGL_ATTRS 30
1098
1099 static gboolean
1100 find_eglconfig_for_window (GdkWin32Display *display_win32,
1101 gboolean need_alpha_bits,
1102 GError **error)
1103 {
1104 EGLint attrs[MAX_EGL_ATTRS];
1105 EGLint count;
1106 EGLConfig *configs, chosen_config;
1107
1108 int i = 0;
1109
1110 EGLDisplay egl_disp;
1111
1112 if (display_win32->egl_min_swap_interval != 0 &&
1113 display_win32->egl_config != NULL)
1114 return TRUE;
1115
1116 egl_disp = display_win32->egl_disp;
1117
1118 attrs[i++] = EGL_CONFORMANT;
1119 attrs[i++] = EGL_OPENGL_ES2_BIT;
1120 attrs[i++] = EGL_SURFACE_TYPE;
1121 attrs[i++] = EGL_WINDOW_BIT;
1122
1123 attrs[i++] = EGL_COLOR_BUFFER_TYPE;
1124 attrs[i++] = EGL_RGB_BUFFER;
1125
1126 attrs[i++] = EGL_RED_SIZE;
1127 attrs[i++] = 1;
1128 attrs[i++] = EGL_GREEN_SIZE;
1129 attrs[i++] = 1;
1130 attrs[i++] = EGL_BLUE_SIZE;
1131 attrs[i++] = 1;
1132
1133 if (need_alpha_bits)
1134 {
1135 attrs[i++] = EGL_ALPHA_SIZE;
1136 attrs[i++] = 1;
1137 }
1138 else
1139 {
1140 attrs[i++] = EGL_ALPHA_SIZE;
1141 attrs[i++] = EGL_DONT_CARE;
1142 }
1143
1144 attrs[i++] = EGL_NONE;
1145 g_assert (i < MAX_EGL_ATTRS);
1146
1147 if (!eglChooseConfig (display_win32->egl_disp, attrs, NULL, 0, &count) || count < 1)
1148 {
1149 g_set_error_literal (error, GDK_GL_ERROR,
1150 GDK_GL_ERROR_UNSUPPORTED_FORMAT,
1151 _("No available configurations for the given pixel format"));
1152 return FALSE;
1153 }
1154
1155 configs = g_new (EGLConfig, count);
1156
1157 if (!eglChooseConfig (display_win32->egl_disp, attrs, configs, count, &count) || count < 1)
1158 {
1159 g_set_error_literal (error, GDK_GL_ERROR,
1160 GDK_GL_ERROR_UNSUPPORTED_FORMAT,
1161 _("No available configurations for the given pixel format"));
1162 return FALSE;
1163 }
1164
1165 /* Pick first valid configuration i guess? */
1166 chosen_config = configs[0];
1167
1168 if (!eglGetConfigAttrib (display_win32->egl_disp, chosen_config,
1169 EGL_MIN_SWAP_INTERVAL, &display_win32->egl_min_swap_interval))
1170 {
1171 g_set_error_literal (error, GDK_GL_ERROR,
1172 GDK_GL_ERROR_NOT_AVAILABLE,
1173 "Could not retrieve the minimum swap interval");
1174 g_free (configs);
1175 return FALSE;
1176 }
1177
1178 display_win32->egl_config = chosen_config;
1179
1180 g_free (configs);
1181
1182 return TRUE;
1183 }
1184
1185 #define N_EGL_ATTRS 16
1186
1187 static EGLContext
1188 create_egl_context (EGLDisplay display,
1189 EGLConfig config,
1190 GdkGLContext *share,
1191 int flags,
1192 int major,
1193 int minor)
1194 {
1195 EGLContext ctx;
1196 EGLint context_attribs[N_EGL_ATTRS];
1197 int i = 0;
1198
1199 /*
1200 * ANGLE does not support the GL_OES_vertex_array_object extension, so we need to use ES3 directly
1201 * if we do not request for ES3 or later
1202 */
1203 if (major < 3)
1204 {
1205 context_attribs[i++] = EGL_CONTEXT_CLIENT_VERSION;
1206 context_attribs[i++] = 3;
1207 }
1208
1209 /* Specify the flags */
1210 context_attribs[i++] = EGL_CONTEXT_FLAGS_KHR;
1211 context_attribs[i++] = flags;
1212
1213 context_attribs[i++] = EGL_NONE;
1214 g_assert (i < N_EGL_ATTRS);
1215
1216 ctx = eglCreateContext (display,
1217 config,
1218 share != NULL ? GDK_WIN32_GL_CONTEXT_EGL (share)->egl_context
1219 : EGL_NO_CONTEXT,
1220 context_attribs);
1221
1222 return ctx;
1223 }
1224
1225 static gboolean
1226 gdk_win32_gl_context_realize_egl (GdkGLContext *context,
1227 GError **error)
1228 {
1229 GdkGLContext *share = gdk_gl_context_get_shared_context (context);
1230 GdkWin32GLContext *context_win32 = GDK_WIN32_GL_CONTEXT (context);
1231 GdkWindow *window = gdk_gl_context_get_window (context);
1232 GdkWindowImplWin32 *impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
1233 GdkWin32Display *display_win32 = GDK_WIN32_DISPLAY (gdk_window_get_display (window));
1234
1235 /* These are the real EGL context items that we will want to use later */
1236 EGLContext ctx;
1237 gboolean debug_bit, compat_bit;
1238 gboolean legacy_bit = FALSE;
1239
1240 /* request flags and specific versions for EGL context */
1241 gint flags = 0;
1242 gint major = 0;
1243 gint minor = 0;
1244
1245 /* we are using GLES, so set early so that we get the correct GLES versions to request */
1246 gdk_gl_context_set_use_es (context, TRUE);
1247
1248 gdk_gl_context_get_required_version (context, &major, &minor);
1249 debug_bit = gdk_gl_context_get_debug_enabled (context);
1250 compat_bit = gdk_gl_context_get_forward_compatible (context);
1251
1252 if (debug_bit)
1253 flags |= EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR;
1254 if (compat_bit)
1255 flags |= EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR;
1256
1257 GDK_NOTE (OPENGL, g_message ("Creating EGL context version %d.%d (debug:%s, forward:%s)",
1258 major, minor,
1259 debug_bit ? "yes" : "no",
1260 compat_bit ? "yes" : "no"));
1261
1262 ctx = create_egl_context (display_win32->egl_disp,
1263 display_win32->egl_config,
1264 share,
1265 flags,
1266 major,
1267 minor);
1268
1269 if (ctx == EGL_NO_CONTEXT)
1270 {
1271 g_set_error_literal (error, GDK_GL_ERROR,
1272 GDK_GL_ERROR_NOT_AVAILABLE,
1273 _("Unable to create a GL context"));
1274
1275 return FALSE;
1276 }
1277
1278 GDK_NOTE (OPENGL,
1279 g_print ("Created EGL context[%p]\n",
1280 ctx));
1281
1282 GDK_WIN32_GL_CONTEXT_EGL (context)->egl_context = ctx;
1283
1284 /* OpenGL does not work with WS_EX_LAYERED enabled, so we need to
1285 * disable WS_EX_LAYERED when we acquire a valid HGLRC
1286 */
1287 impl->suppress_layered++;
1288
1289 /* if this is the first time a GL context is acquired for the window,
1290 * disable layered windows by triggering update_style_bits()
1291 */
1292 if (impl->suppress_layered == 1)
1293 _gdk_win32_window_update_style_bits (window);
1294
1295 /* Ensure that any other context is created with a legacy bit set */
1296 gdk_gl_context_set_is_legacy (context, legacy_bit);
1297
1298 return TRUE;
1299 }
1300
1301 void
1302 gdk_win32_window_invalidate_egl_framebuffer (GdkWindow *window)
1303 {
1304 /* If we are using ANGLE, we need to force redraw of the whole Window and its child windows
1305 * as we need to re-acquire the EGL surfaces that we rendered to upload to Cairo explicitly,
1306 * using gdk_window_invalidate_rect (), when we maximize or restore or use aerosnap
1307 */
1308 if (window->gl_paint_context != NULL && gdk_gl_context_get_use_es (window->gl_paint_context))
1309 {
1310 GdkWindowImplWin32 *impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
1311
1312 impl->egl_force_redraw_all = TRUE;
1313 }
1314 }
1315
1316 #ifdef GDK_WIN32_ENABLE_EGL
1317 gboolean
1318 gdk_win32_display_is_egl_context_current (GdkDisplay *display,
1319 GdkGLContext *context)
1320 {
1321 GdkWin32GLContextEGL *context_egl = GDK_WIN32_GL_CONTEXT_EGL (context);
1322
1323 return context_egl->egl_context == eglGetCurrentContext ();
1324 }
1325 #else
1326 #define gdk_win32_display_is_egl_context_current(disp,ctx) FALSE
1327 #endif
1328
1329 static gboolean
1330 gdk_win32_display_make_egl_context_current (GdkDisplay *display,
1331 GdkGLContext *context)
1332 {
1333 GdkWin32GLContext *context_win32;
1334 GdkWin32Display *display_win32 = GDK_WIN32_DISPLAY (display);
1335 GdkWindow *window;
1336 EGLSurface egl_surface;
1337
1338 if (context == NULL)
1339 {
1340 eglMakeCurrent (display_win32->egl_disp, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
1341
1342 return TRUE;
1343 }
1344
1345 context_win32 = GDK_WIN32_GL_CONTEXT (context);
1346 window = gdk_gl_context_get_window (context);
1347
1348 if (context_win32->is_attached || !display_win32->hasEglSurfacelessContext)
1349 egl_surface = _gdk_win32_window_get_egl_surface (window,
1350 display_win32->egl_config,
1351 !context_win32->is_attached);
1352 else
1353 egl_surface = EGL_NO_SURFACE;
1354
1355 if (!eglMakeCurrent (display_win32->egl_disp,
1356 egl_surface,
1357 egl_surface,
1358 GDK_WIN32_GL_CONTEXT_EGL (context)->egl_context))
1359 {
1360 g_warning ("eglMakeCurrent failed");
1361 return FALSE;
1362 }
1363
1364 if (display_win32->egl_min_swap_interval == 0)
1365 eglSwapInterval (display_win32->egl_disp, 0);
1366 else
1367 g_debug ("Can't disable GL swap interval");
1368
1369 return TRUE;
1370 }
1371
1372 static void
1373 gdk_win32_gl_context_egl_class_init (GdkWin32GLContextEGLClass *klass)
1374 {
1375 GdkGLContextClass *context_class = GDK_GL_CONTEXT_CLASS (klass);
1376 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1377
1378 context_class->end_frame = gdk_win32_gl_context_end_frame_egl;
1379 context_class->realize = gdk_win32_gl_context_realize_egl;
1380
1381 gobject_class->dispose = gdk_win32_gl_context_dispose_egl;
1382 }
1383
1384 static void
1385 gdk_win32_gl_context_egl_init (GdkWin32GLContextEGL *egl_context)
1386 {
1387 }
1388 #else /* GDK_WIN32_ENABLE_EGL */
1389 /* define EGL stuff as no-op macros or functions if it is not enabled */
1390
1391 static void
1392 gdk_win32_display_init_egl (GdkWin32Display *display_win32)
1393 {
1394 GDK_NOTE (OPENGL, g_message ("Cannot %s GLES contexts: no GLES support",
1395 display_win32->gl_type == GDK_WIN32_GL_PENDING ?
1396 "create" : "fallback to"));
1397 display_win32->gl_type = GDK_WIN32_GL_NONE;
1398 }
1399
1400 #define gdk_win32_display_make_egl_context_current(disp,ctx) FALSE
1401
1402 void
1403 gdk_win32_window_invalidate_egl_framebuffer (GdkWindow *window)
1404 {
1405 }
1406
1407 static gboolean
1408 find_eglconfig_for_window (GdkWin32Display *display_win32,
1409 gboolean need_alpha_bits,
1410 GError **error)
1411 {
1412 return FALSE;
1413 }
1414 #endif /* !GDK_WIN32_ENABLE_EGL */
1415
1416 static void
1417 gdk_win32_gl_context_class_init (GdkWin32GLContextClass *klass)
1418 {
1419 GdkGLContextClass *context_class = GDK_GL_CONTEXT_CLASS (klass);
1420 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1421 }
1422
1423 static void
1424 gdk_win32_gl_context_init (GdkWin32GLContext *self)
1425 {
1426 }
1427
1428 void
1429 gdk_win32_window_invalidate_for_new_frame (GdkWindow *window,
1430 cairo_region_t *update_area)
1431 {
1432 cairo_rectangle_int_t window_rect;
1433 gboolean invalidate_all = FALSE;
1434 GdkWin32GLContext *context_win32;
1435 cairo_rectangle_int_t whole_window = { 0, 0, gdk_window_get_width (window), gdk_window_get_height (window) };
1436
1437 /* Minimal update is ok if we're not drawing with gl */
1438 if (window->gl_paint_context == NULL)
1439 return;
1440
1441 context_win32 = GDK_WIN32_GL_CONTEXT (window->gl_paint_context);
1442 context_win32->do_blit_swap = FALSE;
1443
1444 if (gdk_gl_context_has_framebuffer_blit (window->gl_paint_context) &&
1445 cairo_region_contains_rectangle (update_area, &whole_window) != CAIRO_REGION_OVERLAP_IN)
1446 {
1447 context_win32->do_blit_swap = TRUE;
1448 }
1449 else
1450 invalidate_all = TRUE;
1451
1452 if (invalidate_all)
1453 {
1454 window_rect.x = 0;
1455 window_rect.y = 0;
1456 window_rect.width = gdk_window_get_width (window);
1457 window_rect.height = gdk_window_get_height (window);
1458
1459 /* If nothing else is known, repaint everything so that the back
1460 buffer is fully up-to-date for the swapbuffer */
1461 cairo_region_union_rectangle (update_area, &window_rect);
1462 }
1463 }
1464
1465 static void
1466 gdk_win32_display_init_gl (GdkDisplay *display,
1467 GdkGLContext *share,
1468 const gboolean need_alpha_bits)
1469 {
1470 GdkWin32Display *display_win32 = GDK_WIN32_DISPLAY (display);
1471 gint best_idx = 0;
1472 gboolean disable_wgl = FALSE;
1473
1474 if (display_win32->gl_type == GDK_WIN32_GL_WGL ||
1475 display_win32->gl_type == GDK_WIN32_GL_EGL)
1476 return;
1477
1478 /*
1479 * We must disable WGL if we are using GDK_GL=gles or if the
1480 * existing shared GLContext is a GLES context
1481 */
1482 disable_wgl = ((_gdk_gl_flags & GDK_GL_GLES) != 0) ||
1483 (share != NULL && gdk_gl_context_get_use_es (share));
1484
1485 if (!disable_wgl)
1486 gdk_win32_display_init_wgl (display_win32, need_alpha_bits);
1487
1488 if (display_win32->gl_type == GDK_WIN32_GL_PENDING ||
1489 display_win32->gl_type == GDK_WIN32_GL_NONE)
1490 gdk_win32_display_init_egl (display_win32);
1491 }
1492
1493 GdkGLContext *
1494 gdk_win32_window_create_gl_context (GdkWindow *window,
1495 gboolean attached,
1496 GdkGLContext *share,
1497 GError **error)
1498 {
1499 GdkDisplay *display = gdk_window_get_display (window);
1500 GdkWin32Display *display_win32 = GDK_WIN32_DISPLAY (gdk_window_get_display (window));
1501 GdkWin32GLContext *context_win32 = NULL;
1502 GdkVisual *visual = gdk_window_get_visual (window);
1503
1504 gboolean need_alpha_bits = (visual == gdk_screen_get_rgba_visual (gdk_display_get_default_screen (display)));
1505
1506 /* Acquire and store up the Windows-specific HWND and HDC */
1507 HDC hdc = GetDC (GDK_WINDOW_HWND (window));
1508
1509 /* display_win32->hdc_egl_temp should *not* be destroyed here! It is destroyed at dispose()! */
1510 display_win32->hdc_egl_temp = hdc;
1511 gdk_win32_display_init_gl (display,
1512 share,
1513 need_alpha_bits);
1514
1515 if (display_win32->gl_type == GDK_WIN32_GL_NONE)
1516 {
1517 g_set_error_literal (error, GDK_GL_ERROR,
1518 GDK_GL_ERROR_NOT_AVAILABLE,
1519 _("No GL implementation is available"));
1520 return NULL;
1521 }
1522
1523 if (display_win32->gl_type == GDK_WIN32_GL_EGL &&
1524 !find_eglconfig_for_window (display_win32, need_alpha_bits, error))
1525 {
1526 display_win32->gl_type = GDK_WIN32_GL_NONE;
1527 return NULL;
1528 }
1529
1530 if (display_win32->gl_type == GDK_WIN32_GL_WGL)
1531 context_win32 = GDK_WIN32_GL_CONTEXT (g_object_new (GDK_TYPE_WIN32_GL_CONTEXT_WGL,
1532 "display", display,
1533 "window", window,
1534 "shared-context", share,
1535 NULL));
1536 #ifdef GDK_WIN32_ENABLE_EGL
1537 else if (display_win32->gl_type == GDK_WIN32_GL_EGL)
1538 context_win32 = GDK_WIN32_GL_CONTEXT (g_object_new (GDK_TYPE_WIN32_GL_CONTEXT_EGL,
1539 "display", display,
1540 "window", window,
1541 "shared-context", share,
1542 NULL));
1543 #endif
1544
1545 context_win32->need_alpha_bits = need_alpha_bits;
1546 context_win32->gl_hdc = hdc;
1547 context_win32->is_attached = attached;
1548
1549 return GDK_GL_CONTEXT (context_win32);
1550 }
1551
1552 gboolean
1553 gdk_win32_display_is_gl_context_current (GdkDisplay *display,
1554 GdkGLContext *context)
1555 {
1556 GdkWin32Display *display_win32 = GDK_WIN32_DISPLAY (display);
1557
1558 if (display_win32->gl_type == GDK_WIN32_GL_WGL)
1559 return gdk_win32_display_is_wgl_context_current (display, context);
1560
1561 if (display_win32->gl_type == GDK_WIN32_GL_EGL)
1562 return gdk_win32_display_is_egl_context_current (display, context);
1563
1564 g_assert_not_reached ();
1565 return TRUE;
1566 }
1567
1568 gboolean
1569 gdk_win32_display_make_gl_context_current (GdkDisplay *display,
1570 GdkGLContext *context)
1571 {
1572 GdkWin32Display *display_win32 = GDK_WIN32_DISPLAY (display);
1573
1574 if (display_win32->gl_type == GDK_WIN32_GL_WGL)
1575 return gdk_win32_display_make_wgl_context_current (display, context);
1576
1577 if (display_win32->gl_type == GDK_WIN32_GL_EGL)
1578 return gdk_win32_display_make_egl_context_current (display, context);
1579
1580 g_assert_not_reached ();
1581 return FALSE;
1582 }
1583
1584 /**
1585 * gdk_win32_display_get_wgl_version:
1586 * @display: a #GdkDisplay
1587 * @major: (out): return location for the WGL major version
1588 * @minor: (out): return location for the WGL minor version
1589 *
1590 * Retrieves the version of the WGL implementation.
1591 *
1592 * Returns: %TRUE if WGL is available
1593 *
1594 * Since: 3.16
1595 */
1596 gboolean
1597 gdk_win32_display_get_wgl_version (GdkDisplay *display,
1598 gint *major,
1599 gint *minor)
1600 {
1601 GdkWin32Display *display_win32 = NULL;
1602 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
1603
1604 if (!GDK_IS_WIN32_DISPLAY (display))
1605 return FALSE;
1606
1607 display_win32 = GDK_WIN32_DISPLAY (display);
1608
1609 if (display_win32->gl_type == GDK_WIN32_GL_PENDING)
1610 gdk_win32_display_init_gl (display, NULL, FALSE);
1611
1612 if (display_win32->gl_type == GDK_WIN32_GL_NONE)
1613 return FALSE;
1614
1615 if (major != NULL)
1616 *major = GDK_WIN32_DISPLAY (display)->gl_version / 10;
1617 if (minor != NULL)
1618 *minor = GDK_WIN32_DISPLAY (display)->gl_version % 10;
1619
1620 return TRUE;
1621 }