Debian Codegraph @

gdk/wayland/gdkglcontext-wayland.c

raw
1 /* GDK - The GIMP Drawing Kit
2 *
3 * gdkglcontext-wayland.c: Wayland specific OpenGL wrappers
4 *
5 * Copyright © 2014 Emmanuele Bassi
6 * Copyright © 2014 Alexander Larsson
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "config.h"
23
24 #include "gdkglcontext-wayland.h"
25 #include "gdkdisplay-wayland.h"
26
27 #include "gdkwaylanddisplay.h"
28 #include "gdkwaylandglcontext.h"
29 #include "gdkwaylandwindow.h"
30 #include "gdkprivate-wayland.h"
31
32 #include "gdkinternals.h"
33
34 #include "gdkintl.h"
35
36 G_DEFINE_TYPE (GdkWaylandGLContext, gdk_wayland_gl_context, GDK_TYPE_GL_CONTEXT)
37
38 static void gdk_x11_gl_context_dispose (GObject *gobject);
39
40 void
41 gdk_wayland_window_invalidate_for_new_frame (GdkWindow *window,
42 cairo_region_t *update_area)
43 {
44 cairo_rectangle_int_t window_rect;
45 GdkDisplay *display = gdk_window_get_display (window);
46 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);
47 GdkWaylandGLContext *context_wayland;
48 int buffer_age;
49 gboolean invalidate_all;
50 EGLSurface egl_surface;
51
52 /* Minimal update is ok if we're not drawing with gl */
53 if (window->gl_paint_context == NULL)
54 return;
55
56 context_wayland = GDK_WAYLAND_GL_CONTEXT (window->gl_paint_context);
57 buffer_age = 0;
58
59 egl_surface = gdk_wayland_window_get_egl_surface (window->impl_window,
60 context_wayland->egl_config);
61
62 if (display_wayland->have_egl_buffer_age)
63 {
64 gdk_gl_context_make_current (window->gl_paint_context);
65 eglQuerySurface (display_wayland->egl_display, egl_surface,
66 EGL_BUFFER_AGE_EXT, &buffer_age);
67 }
68
69 invalidate_all = FALSE;
70 if (buffer_age == 0 || buffer_age >= 4)
71 invalidate_all = TRUE;
72 else
73 {
74 if (buffer_age >= 2)
75 {
76 if (window->old_updated_area[0])
77 cairo_region_union (update_area, window->old_updated_area[0]);
78 else
79 invalidate_all = TRUE;
80 }
81 if (buffer_age >= 3)
82 {
83 if (window->old_updated_area[1])
84 cairo_region_union (update_area, window->old_updated_area[1]);
85 else
86 invalidate_all = TRUE;
87 }
88 }
89
90 if (invalidate_all)
91 {
92 window_rect.x = 0;
93 window_rect.y = 0;
94 window_rect.width = gdk_window_get_width (window);
95 window_rect.height = gdk_window_get_height (window);
96
97 /* If nothing else is known, repaint everything so that the back
98 * buffer is fully up-to-date for the swapbuffer
99 */
100 cairo_region_union_rectangle (update_area, &window_rect);
101 }
102 }
103
104 #define N_EGL_ATTRS 16
105
106 static gboolean
107 gdk_wayland_gl_context_realize (GdkGLContext *context,
108 GError **error)
109 {
110 GdkWaylandGLContext *context_wayland = GDK_WAYLAND_GL_CONTEXT (context);
111 GdkDisplay *display = gdk_gl_context_get_display (context);
112 GdkGLContext *share = gdk_gl_context_get_shared_context (context);
113 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);
114 EGLContext ctx;
115 EGLint context_attribs[N_EGL_ATTRS];
116 int major, minor, flags;
117 gboolean debug_bit, forward_bit, legacy_bit, use_es;
118 int i = 0;
119
120 gdk_gl_context_get_required_version (context, &major, &minor);
121 debug_bit = gdk_gl_context_get_debug_enabled (context);
122 forward_bit = gdk_gl_context_get_forward_compatible (context);
123 legacy_bit = (_gdk_gl_flags & GDK_GL_LEGACY) != 0 ||
124 (share != NULL && gdk_gl_context_is_legacy (share));
125 use_es = (_gdk_gl_flags & GDK_GL_GLES) != 0 ||
126 (share != NULL && gdk_gl_context_get_use_es (share));
127
128 flags = 0;
129
130 if (debug_bit)
131 flags |= EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR;
132 if (forward_bit)
133 flags |= EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR;
134
135 if (!use_es)
136 {
137 eglBindAPI (EGL_OPENGL_API);
138
139 /* We want a core profile, unless in legacy mode */
140 context_attribs[i++] = EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR;
141 context_attribs[i++] = legacy_bit
142 ? EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR
143 : EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR;
144
145 /* Specify the version */
146 context_attribs[i++] = EGL_CONTEXT_MAJOR_VERSION_KHR;
147 context_attribs[i++] = legacy_bit ? 3 : major;
148 context_attribs[i++] = EGL_CONTEXT_MINOR_VERSION_KHR;
149 context_attribs[i++] = legacy_bit ? 0 : minor;
150 }
151 else
152 {
153 eglBindAPI (EGL_OPENGL_ES_API);
154
155 context_attribs[i++] = EGL_CONTEXT_CLIENT_VERSION;
156 if (major == 3)
157 context_attribs[i++] = 3;
158 else
159 context_attribs[i++] = 2;
160 }
161
162 /* Specify the flags */
163 context_attribs[i++] = EGL_CONTEXT_FLAGS_KHR;
164 context_attribs[i++] = flags;
165
166 context_attribs[i++] = EGL_NONE;
167 g_assert (i < N_EGL_ATTRS);
168
169 GDK_NOTE (OPENGL, g_message ("Creating EGL context version %d.%d (debug:%s, forward:%s, legacy:%s, es:%s)",
170 major, minor,
171 debug_bit ? "yes" : "no",
172 forward_bit ? "yes" : "no",
173 legacy_bit ? "yes" : "no",
174 use_es ? "yes" : "no"));
175
176 ctx = eglCreateContext (display_wayland->egl_display,
177 context_wayland->egl_config,
178 share != NULL ? GDK_WAYLAND_GL_CONTEXT (share)->egl_context
179 : EGL_NO_CONTEXT,
180 context_attribs);
181
182 /* If context creation failed without the legacy bit, let's try again with it */
183 if (ctx == NULL && !legacy_bit)
184 {
185 /* Ensure that re-ordering does not break the offsets */
186 g_assert (context_attribs[0] == EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR);
187 context_attribs[1] = EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR;
188 context_attribs[3] = 3;
189 context_attribs[5] = 0;
190
191 eglBindAPI (EGL_OPENGL_API);
192
193 legacy_bit = TRUE;
194 use_es = FALSE;
195
196 GDK_NOTE (OPENGL, g_message ("eglCreateContext failed, switching to legacy"));
197 ctx = eglCreateContext (display_wayland->egl_display,
198 context_wayland->egl_config,
199 share != NULL ? GDK_WAYLAND_GL_CONTEXT (share)->egl_context
200 : EGL_NO_CONTEXT,
201 context_attribs);
202 }
203
204 if (ctx == NULL)
205 {
206 g_set_error_literal (error, GDK_GL_ERROR,
207 GDK_GL_ERROR_NOT_AVAILABLE,
208 _("Unable to create a GL context"));
209 return FALSE;
210 }
211
212 GDK_NOTE (OPENGL, g_message ("Created EGL context[%p]", ctx));
213
214 context_wayland->egl_context = ctx;
215
216 gdk_gl_context_set_is_legacy (context, legacy_bit);
217 gdk_gl_context_set_use_es (context, use_es);
218
219 return TRUE;
220 }
221
222 static void
223 gdk_wayland_gl_context_end_frame (GdkGLContext *context,
224 cairo_region_t *painted,
225 cairo_region_t *damage)
226 {
227 GdkWindow *window = gdk_gl_context_get_window (context);
228 GdkDisplay *display = gdk_window_get_display (window);
229 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);
230 GdkWaylandGLContext *context_wayland = GDK_WAYLAND_GL_CONTEXT (context);
231 EGLSurface egl_surface;
232
233 gdk_gl_context_make_current (context);
234
235 egl_surface = gdk_wayland_window_get_egl_surface (window->impl_window,
236 context_wayland->egl_config);
237
238 if (display_wayland->have_egl_swap_buffers_with_damage)
239 {
240 int i, j, n_rects = cairo_region_num_rectangles (damage);
241 EGLint *rects = g_new (EGLint, n_rects * 4);
242 cairo_rectangle_int_t rect;
243 int window_height = gdk_window_get_height (window);
244
245 for (i = 0, j = 0; i < n_rects; i++)
246 {
247 cairo_region_get_rectangle (damage, i, &rect);
248 rects[j++] = rect.x;
249 rects[j++] = window_height - rect.height - rect.y;
250 rects[j++] = rect.width;
251 rects[j++] = rect.height;
252 }
253 eglSwapBuffersWithDamageEXT (display_wayland->egl_display, egl_surface, rects, n_rects);
254 g_free (rects);
255 }
256 else
257 eglSwapBuffers (display_wayland->egl_display, egl_surface);
258 }
259
260 static void
261 gdk_wayland_gl_context_class_init (GdkWaylandGLContextClass *klass)
262 {
263 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
264 GdkGLContextClass *context_class = GDK_GL_CONTEXT_CLASS (klass);
265
266 gobject_class->dispose = gdk_x11_gl_context_dispose;
267
268 context_class->realize = gdk_wayland_gl_context_realize;
269 context_class->end_frame = gdk_wayland_gl_context_end_frame;
270 }
271
272 static void
273 gdk_wayland_gl_context_init (GdkWaylandGLContext *self)
274 {
275 }
276
277 static EGLDisplay
278 gdk_wayland_get_display (GdkWaylandDisplay *display_wayland)
279 {
280 EGLDisplay dpy = NULL;
281
282 if (epoxy_has_egl_extension (NULL, "EGL_KHR_platform_base"))
283 {
284 PFNEGLGETPLATFORMDISPLAYPROC getPlatformDisplay =
285 (void *) eglGetProcAddress ("eglGetPlatformDisplay");
286
287 if (getPlatformDisplay)
288 dpy = getPlatformDisplay (EGL_PLATFORM_WAYLAND_EXT,
289 display_wayland->wl_display,
290 NULL);
291 if (dpy)
292 return dpy;
293 }
294
295 if (epoxy_has_egl_extension (NULL, "EGL_EXT_platform_base"))
296 {
297 PFNEGLGETPLATFORMDISPLAYEXTPROC getPlatformDisplay =
298 (void *) eglGetProcAddress ("eglGetPlatformDisplayEXT");
299
300 if (getPlatformDisplay)
301 dpy = getPlatformDisplay (EGL_PLATFORM_WAYLAND_EXT,
302 display_wayland->wl_display,
303 NULL);
304 if (dpy)
305 return dpy;
306 }
307
308 return eglGetDisplay ((EGLNativeDisplayType) display_wayland->wl_display);
309 }
310
311 gboolean
312 gdk_wayland_display_init_gl (GdkDisplay *display,
313 GdkGLContext *share)
314 {
315 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);
316 EGLint major, minor;
317 EGLDisplay dpy;
318 gboolean use_es;
319
320 if (display_wayland->have_egl)
321 return TRUE;
322
323 dpy = gdk_wayland_get_display (display_wayland);
324
325 if (dpy == NULL)
326 return FALSE;
327
328 if (!eglInitialize (dpy, &major, &minor))
329 return FALSE;
330
331 use_es = (_gdk_gl_flags & GDK_GL_GLES) != 0 ||
332 (share != NULL && gdk_gl_context_get_use_es (share));
333 if (!eglBindAPI (use_es ? EGL_OPENGL_ES_API : EGL_OPENGL_API))
334 return FALSE;
335
336 display_wayland->egl_display = dpy;
337 display_wayland->egl_major_version = major;
338 display_wayland->egl_minor_version = minor;
339
340 display_wayland->have_egl = TRUE;
341
342 display_wayland->have_egl_khr_create_context =
343 epoxy_has_egl_extension (dpy, "EGL_KHR_create_context");
344
345 display_wayland->have_egl_buffer_age =
346 epoxy_has_egl_extension (dpy, "EGL_EXT_buffer_age");
347
348 display_wayland->have_egl_swap_buffers_with_damage =
349 epoxy_has_egl_extension (dpy, "EGL_EXT_swap_buffers_with_damage");
350
351 display_wayland->have_egl_surfaceless_context =
352 epoxy_has_egl_extension (dpy, "EGL_KHR_surfaceless_context");
353
354 GDK_NOTE (OPENGL,
355 g_message ("EGL API version %d.%d found\n"
356 " - Vendor: %s\n"
357 " - Version: %s\n"
358 " - Client APIs: %s\n"
359 " - Extensions:\n"
360 "\t%s",
361 display_wayland->egl_major_version,
362 display_wayland->egl_minor_version,
363 eglQueryString (dpy, EGL_VENDOR),
364 eglQueryString (dpy, EGL_VERSION),
365 eglQueryString (dpy, EGL_CLIENT_APIS),
366 eglQueryString (dpy, EGL_EXTENSIONS)));
367
368 return TRUE;
369 }
370
371 #define MAX_EGL_ATTRS 30
372
373 static gboolean
374 find_eglconfig_for_window (GdkWindow *window,
375 EGLConfig *egl_config_out,
376 EGLint *min_swap_interval_out,
377 GError **error)
378 {
379 GdkDisplay *display = gdk_window_get_display (window);
380 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);
381 GdkVisual *visual = gdk_window_get_visual (window);
382 EGLint attrs[MAX_EGL_ATTRS];
383 EGLint count;
384 EGLConfig *configs, chosen_config;
385 gboolean use_rgba;
386
387 int i = 0;
388
389 attrs[i++] = EGL_SURFACE_TYPE;
390 attrs[i++] = EGL_WINDOW_BIT;
391
392 attrs[i++] = EGL_COLOR_BUFFER_TYPE;
393 attrs[i++] = EGL_RGB_BUFFER;
394
395 attrs[i++] = EGL_RED_SIZE;
396 attrs[i++] = 8;
397 attrs[i++] = EGL_GREEN_SIZE;
398 attrs[i++] = 8;
399 attrs[i++] = EGL_BLUE_SIZE;
400 attrs[i++] = 8;
401
402 use_rgba = (visual == gdk_screen_get_rgba_visual (gdk_display_get_default_screen (display)));
403
404 if (use_rgba)
405 {
406 attrs[i++] = EGL_ALPHA_SIZE;
407 attrs[i++] = 8;
408 }
409 else
410 {
411 attrs[i++] = EGL_ALPHA_SIZE;
412 attrs[i++] = 0;
413 }
414
415 attrs[i++] = EGL_NONE;
416 g_assert (i < MAX_EGL_ATTRS);
417
418 if (!eglChooseConfig (display_wayland->egl_display, attrs, NULL, 0, &count) || count < 1)
419 {
420 g_set_error_literal (error, GDK_GL_ERROR,
421 GDK_GL_ERROR_UNSUPPORTED_FORMAT,
422 _("No available configurations for the given pixel format"));
423 return FALSE;
424 }
425
426 configs = g_new (EGLConfig, count);
427
428 if (!eglChooseConfig (display_wayland->egl_display, attrs, configs, count, &count) || count < 1)
429 {
430 g_set_error_literal (error, GDK_GL_ERROR,
431 GDK_GL_ERROR_UNSUPPORTED_FORMAT,
432 _("No available configurations for the given pixel format"));
433 return FALSE;
434 }
435
436 /* Pick first valid configuration i guess? */
437 chosen_config = configs[0];
438
439 if (!eglGetConfigAttrib (display_wayland->egl_display, chosen_config,
440 EGL_MIN_SWAP_INTERVAL, min_swap_interval_out))
441 {
442 g_set_error_literal (error, GDK_GL_ERROR,
443 GDK_GL_ERROR_NOT_AVAILABLE,
444 "Could not retrieve the minimum swap interval");
445 g_free (configs);
446 return FALSE;
447 }
448
449 if (egl_config_out != NULL)
450 *egl_config_out = chosen_config;
451
452 g_free (configs);
453
454 return TRUE;
455 }
456
457 GdkGLContext *
458 gdk_wayland_window_create_gl_context (GdkWindow *window,
459 gboolean attached,
460 GdkGLContext *share,
461 GError **error)
462 {
463 GdkDisplay *display = gdk_window_get_display (window);
464 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);
465 GdkWaylandGLContext *context;
466 EGLConfig config;
467
468 if (!gdk_wayland_display_init_gl (display, share))
469 {
470 g_set_error_literal (error, GDK_GL_ERROR,
471 GDK_GL_ERROR_NOT_AVAILABLE,
472 _("No GL implementation is available"));
473 return NULL;
474 }
475
476 if (!display_wayland->have_egl_khr_create_context)
477 {
478 g_set_error_literal (error, GDK_GL_ERROR,
479 GDK_GL_ERROR_UNSUPPORTED_PROFILE,
480 _("Core GL is not available on EGL implementation"));
481 return NULL;
482 }
483
484 if (!find_eglconfig_for_window (window, &config,
485 &display_wayland->egl_min_swap_interval,
486 error))
487 return NULL;
488
489 context = g_object_new (GDK_TYPE_WAYLAND_GL_CONTEXT,
490 "display", display,
491 "window", window,
492 "shared-context", share,
493 NULL);
494
495 context->egl_config = config;
496 context->is_attached = attached;
497
498 return GDK_GL_CONTEXT (context);
499 }
500
501 static void
502 gdk_x11_gl_context_dispose (GObject *gobject)
503 {
504 GdkWaylandGLContext *context_wayland = GDK_WAYLAND_GL_CONTEXT (gobject);
505
506 if (context_wayland->egl_context != NULL)
507 {
508 GdkGLContext *context = GDK_GL_CONTEXT (gobject);
509 GdkWindow *window = gdk_gl_context_get_window (context);
510 GdkDisplay *display = gdk_window_get_display (window);
511 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);
512
513 if (eglGetCurrentContext () == context_wayland->egl_context)
514 eglMakeCurrent(display_wayland->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
515 EGL_NO_CONTEXT);
516
517 GDK_NOTE (OPENGL, g_message ("Destroying EGL context"));
518
519 eglDestroyContext (display_wayland->egl_display,
520 context_wayland->egl_context);
521 context_wayland->egl_context = NULL;
522 }
523
524 G_OBJECT_CLASS (gdk_wayland_gl_context_parent_class)->dispose (gobject);
525 }
526
527 gboolean
528 gdk_wayland_display_is_gl_context_current (GdkDisplay *display,
529 GdkGLContext *context)
530 {
531 GdkWaylandGLContext *context_wayland = GDK_WAYLAND_GL_CONTEXT (context);
532
533 return context_wayland->egl_context == eglGetCurrentContext ();
534 }
535
536 gboolean
537 gdk_wayland_display_make_gl_context_current (GdkDisplay *display,
538 GdkGLContext *context)
539 {
540 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);
541 GdkWaylandGLContext *context_wayland;
542 GdkWindow *window;
543 EGLSurface egl_surface;
544
545 if (context == NULL)
546 {
547 eglMakeCurrent(display_wayland->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
548 EGL_NO_CONTEXT);
549 return TRUE;
550 }
551
552 context_wayland = GDK_WAYLAND_GL_CONTEXT (context);
553 window = gdk_gl_context_get_window (context);
554
555 if (context_wayland->is_attached)
556 egl_surface = gdk_wayland_window_get_egl_surface (window->impl_window, context_wayland->egl_config);
557 else
558 {
559 if (display_wayland->have_egl_surfaceless_context)
560 egl_surface = EGL_NO_SURFACE;
561 else
562 egl_surface = gdk_wayland_window_get_dummy_egl_surface (window->impl_window,
563 context_wayland->egl_config);
564 }
565
566 if (!eglMakeCurrent (display_wayland->egl_display, egl_surface,
567 egl_surface, context_wayland->egl_context))
568 {
569 g_warning ("eglMakeCurrent failed");
570 return FALSE;
571 }
572
573 if (display_wayland->egl_min_swap_interval == 0)
574 eglSwapInterval (display_wayland->egl_display, 0);
575 else
576 g_debug ("Can't disable GL swap interval");
577
578 return TRUE;
579 }