blob: 40d9bf2abc5d789a733383739213dd88b9f90f74 [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
Emil Velikov07448942016-07-04 15:34:24 +010037#include "weston-egl-ext.h"
Krzysztof Konopkoe338ced2016-09-15 13:01:49 +020038#endif
Jonny Lamb51a7ae52015-03-20 15:26:51 +010039
40#ifdef __cplusplus
41extern "C" {
42#endif
43
Jonny Lamb0e2ab362015-03-24 13:12:07 +010044#ifdef ENABLE_EGL
45
Emil Velikovf0c3a1c2016-07-04 15:34:18 +010046static bool
47weston_check_egl_extension(const char *extensions, const char *extension)
48{
49 size_t extlen = strlen(extension);
50 const char *end = extensions + strlen(extensions);
51
52 while (extensions < end) {
53 size_t n = 0;
54
55 /* Skip whitespaces, if any */
56 if (*extensions == ' ') {
57 extensions++;
58 continue;
59 }
60
61 n = strcspn(extensions, " ");
62
63 /* Compare strings */
64 if (n == extlen && strncmp(extension, extensions, n) == 0)
65 return true; /* Found */
66
67 extensions += n;
68 }
69
70 /* Not found */
71 return false;
72}
73
Jonny Lamb759fbf42015-03-24 13:12:08 +010074static inline void *
75weston_platform_get_egl_proc_address(const char *address)
Jonny Lamb51a7ae52015-03-20 15:26:51 +010076{
Jonny Lamb759fbf42015-03-24 13:12:08 +010077 const char *extensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
Jonny Lamb51a7ae52015-03-20 15:26:51 +010078
Emil Velikovd0fcdc92016-07-04 15:34:19 +010079 if (extensions &&
80 (weston_check_egl_extension(extensions, "EGL_EXT_platform_wayland") ||
81 weston_check_egl_extension(extensions, "EGL_KHR_platform_wayland"))) {
Jonny Lamb759fbf42015-03-24 13:12:08 +010082 return (void *) eglGetProcAddress(address);
Jonny Lamb51a7ae52015-03-20 15:26:51 +010083 }
Jonny Lamb759fbf42015-03-24 13:12:08 +010084
85 return NULL;
Jonny Lamb51a7ae52015-03-20 15:26:51 +010086}
87
88static inline EGLDisplay
89weston_platform_get_egl_display(EGLenum platform, void *native_display,
90 const EGLint *attrib_list)
91{
Jonny Lamb759fbf42015-03-24 13:12:08 +010092 static PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display = NULL;
Jonny Lamb51a7ae52015-03-20 15:26:51 +010093
Jonny Lamb759fbf42015-03-24 13:12:08 +010094 if (!get_platform_display) {
Matthias Treydtecd9424e2016-01-29 17:02:15 +010095 get_platform_display = (PFNEGLGETPLATFORMDISPLAYEXTPROC)
96 weston_platform_get_egl_proc_address(
97 "eglGetPlatformDisplayEXT");
Jonny Lamb759fbf42015-03-24 13:12:08 +010098 }
99
100 if (get_platform_display)
101 return get_platform_display(platform,
102 native_display, attrib_list);
Jonny Lamb0e2ab362015-03-24 13:12:07 +0100103
104 return eglGetDisplay((EGLNativeDisplayType) native_display);
Jonny Lamb51a7ae52015-03-20 15:26:51 +0100105}
106
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100107static inline EGLSurface
Jonny Lambabff8832015-03-24 13:12:09 +0100108weston_platform_create_egl_surface(EGLDisplay dpy, EGLConfig config,
109 void *native_window,
110 const EGLint *attrib_list)
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100111{
Jonny Lamb759fbf42015-03-24 13:12:08 +0100112 static PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC
113 create_platform_window = NULL;
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100114
Jonny Lamb759fbf42015-03-24 13:12:08 +0100115 if (!create_platform_window) {
Matthias Treydtecd9424e2016-01-29 17:02:15 +0100116 create_platform_window = (PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC)
117 weston_platform_get_egl_proc_address(
118 "eglCreatePlatformWindowSurfaceEXT");
Jonny Lamb759fbf42015-03-24 13:12:08 +0100119 }
120
121 if (create_platform_window)
122 return create_platform_window(dpy, config,
123 native_window,
124 attrib_list);
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100125
126 return eglCreateWindowSurface(dpy, config,
127 (EGLNativeWindowType) native_window,
128 attrib_list);
129}
130
Emil Velikov3612be22016-11-14 17:08:12 +0000131static inline EGLBoolean
132weston_platform_destroy_egl_surface(EGLDisplay display,
133 EGLSurface surface)
134{
135 return eglDestroySurface(display, surface);
136}
137
Jonny Lamb0e2ab362015-03-24 13:12:07 +0100138#else /* ENABLE_EGL */
139
140static inline void *
141weston_platform_get_egl_display(void *platform, void *native_display,
142 const int *attrib_list)
143{
144 return NULL;
145}
146
147static inline void *
Jonny Lambabff8832015-03-24 13:12:09 +0100148weston_platform_create_egl_surface(void *dpy, void *config,
149 void *native_window,
150 const int *attrib_list)
Jonny Lamb0e2ab362015-03-24 13:12:07 +0100151{
152 return NULL;
153}
Emil Velikov3612be22016-11-14 17:08:12 +0000154
155static inline unsigned int
156weston_platform_destroy_egl_surface(void *display,
157 void *surface)
158{
159 return 1;
160}
Jonny Lamb0e2ab362015-03-24 13:12:07 +0100161#endif /* ENABLE_EGL */
162
Jonny Lamb51a7ae52015-03-20 15:26:51 +0100163#ifdef __cplusplus
164}
165#endif
166
167#endif /* WESTON_PLATFORM_H */