Jonny Lamb | 51a7ae5 | 2015-03-20 15:26:51 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2015 Collabora, Ltd. |
| 3 | * |
Bryce Harrington | 6c6164c | 2015-06-11 14:20:17 -0700 | [diff] [blame] | 4 | * 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 Lamb | 51a7ae5 | 2015-03-20 15:26:51 +0100 | [diff] [blame] | 11 | * |
Bryce Harrington | 6c6164c | 2015-06-11 14:20:17 -0700 | [diff] [blame] | 12 | * 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 Lamb | 51a7ae5 | 2015-03-20 15:26:51 +0100 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | #ifndef WESTON_PLATFORM_H |
| 27 | #define WESTON_PLATFORM_H |
| 28 | |
Emil Velikov | 10772db | 2016-07-04 15:34:16 +0100 | [diff] [blame] | 29 | #include <stdbool.h> |
Jonny Lamb | 51a7ae5 | 2015-03-20 15:26:51 +0100 | [diff] [blame] | 30 | #include <string.h> |
| 31 | |
Jonny Lamb | 0e2ab36 | 2015-03-24 13:12:07 +0100 | [diff] [blame] | 32 | #ifdef ENABLE_EGL |
Ahmet Acar | 64d78bb | 2015-11-13 10:25:46 -0600 | [diff] [blame] | 33 | #include <wayland-egl.h> |
Jonny Lamb | 51a7ae5 | 2015-03-20 15:26:51 +0100 | [diff] [blame] | 34 | #include <EGL/egl.h> |
| 35 | #include <EGL/eglext.h> |
Jonny Lamb | 0e2ab36 | 2015-03-24 13:12:07 +0100 | [diff] [blame] | 36 | #endif |
| 37 | |
| 38 | #ifndef EGL_PLATFORM_WAYLAND_KHR |
| 39 | #define EGL_PLATFORM_WAYLAND_KHR 0x31D8 |
| 40 | #endif |
Jonny Lamb | 51a7ae5 | 2015-03-20 15:26:51 +0100 | [diff] [blame] | 41 | |
| 42 | #ifdef __cplusplus |
| 43 | extern "C" { |
| 44 | #endif |
| 45 | |
Jonny Lamb | 0e2ab36 | 2015-03-24 13:12:07 +0100 | [diff] [blame] | 46 | #ifdef ENABLE_EGL |
| 47 | |
| 48 | #ifndef EGL_EXT_platform_base |
Manuel Bachmann | e812859 | 2015-03-28 07:05:48 +0100 | [diff] [blame] | 49 | typedef EGLDisplay (*PFNEGLGETPLATFORMDISPLAYEXTPROC) (EGLenum platform, |
| 50 | void *native_display, |
| 51 | const EGLint *attrib_list); |
| 52 | typedef EGLSurface (*PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC) (EGLDisplay dpy, |
| 53 | EGLConfig config, |
| 54 | void *native_window, |
| 55 | const EGLint *attrib_list); |
Jonny Lamb | 0e2ab36 | 2015-03-24 13:12:07 +0100 | [diff] [blame] | 56 | #endif |
| 57 | |
Emil Velikov | f0c3a1c | 2016-07-04 15:34:18 +0100 | [diff] [blame] | 58 | static bool |
| 59 | weston_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 Lamb | 759fbf4 | 2015-03-24 13:12:08 +0100 | [diff] [blame] | 86 | static inline void * |
| 87 | weston_platform_get_egl_proc_address(const char *address) |
Jonny Lamb | 51a7ae5 | 2015-03-20 15:26:51 +0100 | [diff] [blame] | 88 | { |
Jonny Lamb | 759fbf4 | 2015-03-24 13:12:08 +0100 | [diff] [blame] | 89 | const char *extensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS); |
Jonny Lamb | 51a7ae5 | 2015-03-20 15:26:51 +0100 | [diff] [blame] | 90 | |
Emil Velikov | d0fcdc9 | 2016-07-04 15:34:19 +0100 | [diff] [blame^] | 91 | if (extensions && |
| 92 | (weston_check_egl_extension(extensions, "EGL_EXT_platform_wayland") || |
| 93 | weston_check_egl_extension(extensions, "EGL_KHR_platform_wayland"))) { |
Jonny Lamb | 759fbf4 | 2015-03-24 13:12:08 +0100 | [diff] [blame] | 94 | return (void *) eglGetProcAddress(address); |
Jonny Lamb | 51a7ae5 | 2015-03-20 15:26:51 +0100 | [diff] [blame] | 95 | } |
Jonny Lamb | 759fbf4 | 2015-03-24 13:12:08 +0100 | [diff] [blame] | 96 | |
| 97 | return NULL; |
Jonny Lamb | 51a7ae5 | 2015-03-20 15:26:51 +0100 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static inline EGLDisplay |
| 101 | weston_platform_get_egl_display(EGLenum platform, void *native_display, |
| 102 | const EGLint *attrib_list) |
| 103 | { |
Jonny Lamb | 759fbf4 | 2015-03-24 13:12:08 +0100 | [diff] [blame] | 104 | static PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display = NULL; |
Jonny Lamb | 51a7ae5 | 2015-03-20 15:26:51 +0100 | [diff] [blame] | 105 | |
Jonny Lamb | 759fbf4 | 2015-03-24 13:12:08 +0100 | [diff] [blame] | 106 | if (!get_platform_display) { |
Matthias Treydte | cd9424e | 2016-01-29 17:02:15 +0100 | [diff] [blame] | 107 | get_platform_display = (PFNEGLGETPLATFORMDISPLAYEXTPROC) |
| 108 | weston_platform_get_egl_proc_address( |
| 109 | "eglGetPlatformDisplayEXT"); |
Jonny Lamb | 759fbf4 | 2015-03-24 13:12:08 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | if (get_platform_display) |
| 113 | return get_platform_display(platform, |
| 114 | native_display, attrib_list); |
Jonny Lamb | 0e2ab36 | 2015-03-24 13:12:07 +0100 | [diff] [blame] | 115 | |
| 116 | return eglGetDisplay((EGLNativeDisplayType) native_display); |
Jonny Lamb | 51a7ae5 | 2015-03-20 15:26:51 +0100 | [diff] [blame] | 117 | } |
| 118 | |
Jonny Lamb | 4bdcb57 | 2015-03-20 15:26:53 +0100 | [diff] [blame] | 119 | static inline EGLSurface |
Jonny Lamb | abff883 | 2015-03-24 13:12:09 +0100 | [diff] [blame] | 120 | weston_platform_create_egl_surface(EGLDisplay dpy, EGLConfig config, |
| 121 | void *native_window, |
| 122 | const EGLint *attrib_list) |
Jonny Lamb | 4bdcb57 | 2015-03-20 15:26:53 +0100 | [diff] [blame] | 123 | { |
Jonny Lamb | 759fbf4 | 2015-03-24 13:12:08 +0100 | [diff] [blame] | 124 | static PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC |
| 125 | create_platform_window = NULL; |
Jonny Lamb | 4bdcb57 | 2015-03-20 15:26:53 +0100 | [diff] [blame] | 126 | |
Jonny Lamb | 759fbf4 | 2015-03-24 13:12:08 +0100 | [diff] [blame] | 127 | if (!create_platform_window) { |
Matthias Treydte | cd9424e | 2016-01-29 17:02:15 +0100 | [diff] [blame] | 128 | create_platform_window = (PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC) |
| 129 | weston_platform_get_egl_proc_address( |
| 130 | "eglCreatePlatformWindowSurfaceEXT"); |
Jonny Lamb | 759fbf4 | 2015-03-24 13:12:08 +0100 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | if (create_platform_window) |
| 134 | return create_platform_window(dpy, config, |
| 135 | native_window, |
| 136 | attrib_list); |
Jonny Lamb | 4bdcb57 | 2015-03-20 15:26:53 +0100 | [diff] [blame] | 137 | |
| 138 | return eglCreateWindowSurface(dpy, config, |
| 139 | (EGLNativeWindowType) native_window, |
| 140 | attrib_list); |
| 141 | } |
| 142 | |
Jonny Lamb | 0e2ab36 | 2015-03-24 13:12:07 +0100 | [diff] [blame] | 143 | #else /* ENABLE_EGL */ |
| 144 | |
| 145 | static inline void * |
| 146 | weston_platform_get_egl_display(void *platform, void *native_display, |
| 147 | const int *attrib_list) |
| 148 | { |
| 149 | return NULL; |
| 150 | } |
| 151 | |
| 152 | static inline void * |
Jonny Lamb | abff883 | 2015-03-24 13:12:09 +0100 | [diff] [blame] | 153 | weston_platform_create_egl_surface(void *dpy, void *config, |
| 154 | void *native_window, |
| 155 | const int *attrib_list) |
Jonny Lamb | 0e2ab36 | 2015-03-24 13:12:07 +0100 | [diff] [blame] | 156 | { |
| 157 | return NULL; |
| 158 | } |
| 159 | #endif /* ENABLE_EGL */ |
| 160 | |
Jonny Lamb | 51a7ae5 | 2015-03-20 15:26:51 +0100 | [diff] [blame] | 161 | #ifdef __cplusplus |
| 162 | } |
| 163 | #endif |
| 164 | |
| 165 | #endif /* WESTON_PLATFORM_H */ |