blob: e0ed55bc4a728cdc4e65b912a4ccb960965a5463 [file] [log] [blame]
Jonny Lamb51a7ae52015-03-20 15:26:51 +01001/*
2 * Copyright © 2015 Collabora, Ltd.
3 *
Bryce Harrington6c6164c2015-06-11 14:20:17 -07004 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
Jonny Lamb51a7ae52015-03-20 15:26:51 +010011 *
Bryce Harrington6c6164c2015-06-11 14:20:17 -070012 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
Jonny Lamb51a7ae52015-03-20 15:26:51 +010024 */
25
26#ifndef WESTON_PLATFORM_H
27#define WESTON_PLATFORM_H
28
Emil Velikov10772db2016-07-04 15:34:16 +010029#include <stdbool.h>
Jonny Lamb51a7ae52015-03-20 15:26:51 +010030#include <string.h>
31
Jonny Lamb0e2ab362015-03-24 13:12:07 +010032#ifdef ENABLE_EGL
Ahmet Acar64d78bb2015-11-13 10:25:46 -060033#include <wayland-egl.h>
Jonny Lamb51a7ae52015-03-20 15:26:51 +010034#include <EGL/egl.h>
35#include <EGL/eglext.h>
Jonny Lamb0e2ab362015-03-24 13:12:07 +010036#endif
37
38#ifndef EGL_PLATFORM_WAYLAND_KHR
39#define EGL_PLATFORM_WAYLAND_KHR 0x31D8
40#endif
Jonny Lamb51a7ae52015-03-20 15:26:51 +010041
42#ifdef __cplusplus
43extern "C" {
44#endif
45
Jonny Lamb0e2ab362015-03-24 13:12:07 +010046#ifdef ENABLE_EGL
47
48#ifndef EGL_EXT_platform_base
Manuel Bachmanne8128592015-03-28 07:05:48 +010049typedef EGLDisplay (*PFNEGLGETPLATFORMDISPLAYEXTPROC) (EGLenum platform,
50 void *native_display,
51 const EGLint *attrib_list);
52typedef EGLSurface (*PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC) (EGLDisplay dpy,
53 EGLConfig config,
54 void *native_window,
55 const EGLint *attrib_list);
Jonny Lamb0e2ab362015-03-24 13:12:07 +010056#endif
57
Emil Velikovf0c3a1c2016-07-04 15:34:18 +010058static bool
59weston_check_egl_extension(const char *extensions, const char *extension)
60{
61 size_t extlen = strlen(extension);
62 const char *end = extensions + strlen(extensions);
63
64 while (extensions < end) {
65 size_t n = 0;
66
67 /* Skip whitespaces, if any */
68 if (*extensions == ' ') {
69 extensions++;
70 continue;
71 }
72
73 n = strcspn(extensions, " ");
74
75 /* Compare strings */
76 if (n == extlen && strncmp(extension, extensions, n) == 0)
77 return true; /* Found */
78
79 extensions += n;
80 }
81
82 /* Not found */
83 return false;
84}
85
Jonny Lamb759fbf42015-03-24 13:12:08 +010086static inline void *
87weston_platform_get_egl_proc_address(const char *address)
Jonny Lamb51a7ae52015-03-20 15:26:51 +010088{
Jonny Lamb759fbf42015-03-24 13:12:08 +010089 const char *extensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
Jonny Lamb51a7ae52015-03-20 15:26:51 +010090
Emil Velikovd0fcdc92016-07-04 15:34:19 +010091 if (extensions &&
92 (weston_check_egl_extension(extensions, "EGL_EXT_platform_wayland") ||
93 weston_check_egl_extension(extensions, "EGL_KHR_platform_wayland"))) {
Jonny Lamb759fbf42015-03-24 13:12:08 +010094 return (void *) eglGetProcAddress(address);
Jonny Lamb51a7ae52015-03-20 15:26:51 +010095 }
Jonny Lamb759fbf42015-03-24 13:12:08 +010096
97 return NULL;
Jonny Lamb51a7ae52015-03-20 15:26:51 +010098}
99
100static inline EGLDisplay
101weston_platform_get_egl_display(EGLenum platform, void *native_display,
102 const EGLint *attrib_list)
103{
Jonny Lamb759fbf42015-03-24 13:12:08 +0100104 static PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display = NULL;
Jonny Lamb51a7ae52015-03-20 15:26:51 +0100105
Jonny Lamb759fbf42015-03-24 13:12:08 +0100106 if (!get_platform_display) {
Matthias Treydtecd9424e2016-01-29 17:02:15 +0100107 get_platform_display = (PFNEGLGETPLATFORMDISPLAYEXTPROC)
108 weston_platform_get_egl_proc_address(
109 "eglGetPlatformDisplayEXT");
Jonny Lamb759fbf42015-03-24 13:12:08 +0100110 }
111
112 if (get_platform_display)
113 return get_platform_display(platform,
114 native_display, attrib_list);
Jonny Lamb0e2ab362015-03-24 13:12:07 +0100115
116 return eglGetDisplay((EGLNativeDisplayType) native_display);
Jonny Lamb51a7ae52015-03-20 15:26:51 +0100117}
118
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100119static inline EGLSurface
Jonny Lambabff8832015-03-24 13:12:09 +0100120weston_platform_create_egl_surface(EGLDisplay dpy, EGLConfig config,
121 void *native_window,
122 const EGLint *attrib_list)
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100123{
Jonny Lamb759fbf42015-03-24 13:12:08 +0100124 static PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC
125 create_platform_window = NULL;
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100126
Jonny Lamb759fbf42015-03-24 13:12:08 +0100127 if (!create_platform_window) {
Matthias Treydtecd9424e2016-01-29 17:02:15 +0100128 create_platform_window = (PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC)
129 weston_platform_get_egl_proc_address(
130 "eglCreatePlatformWindowSurfaceEXT");
Jonny Lamb759fbf42015-03-24 13:12:08 +0100131 }
132
133 if (create_platform_window)
134 return create_platform_window(dpy, config,
135 native_window,
136 attrib_list);
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100137
138 return eglCreateWindowSurface(dpy, config,
139 (EGLNativeWindowType) native_window,
140 attrib_list);
141}
142
Jonny Lamb0e2ab362015-03-24 13:12:07 +0100143#else /* ENABLE_EGL */
144
145static inline void *
146weston_platform_get_egl_display(void *platform, void *native_display,
147 const int *attrib_list)
148{
149 return NULL;
150}
151
152static inline void *
Jonny Lambabff8832015-03-24 13:12:09 +0100153weston_platform_create_egl_surface(void *dpy, void *config,
154 void *native_window,
155 const int *attrib_list)
Jonny Lamb0e2ab362015-03-24 13:12:07 +0100156{
157 return NULL;
158}
159#endif /* ENABLE_EGL */
160
Jonny Lamb51a7ae52015-03-20 15:26:51 +0100161#ifdef __cplusplus
162}
163#endif
164
165#endif /* WESTON_PLATFORM_H */