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 | |
Emil Velikov | 0744894 | 2016-07-04 15:34:24 +0100 | [diff] [blame] | 37 | #include "weston-egl-ext.h" |
Krzysztof Konopko | e338ced | 2016-09-15 13:01:49 +0200 | [diff] [blame] | 38 | #endif |
Jonny Lamb | 51a7ae5 | 2015-03-20 15:26:51 +0100 | [diff] [blame] | 39 | |
| 40 | #ifdef __cplusplus |
| 41 | extern "C" { |
| 42 | #endif |
| 43 | |
Jonny Lamb | 0e2ab36 | 2015-03-24 13:12:07 +0100 | [diff] [blame] | 44 | #ifdef ENABLE_EGL |
| 45 | |
Emil Velikov | f0c3a1c | 2016-07-04 15:34:18 +0100 | [diff] [blame] | 46 | static bool |
| 47 | weston_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 Lamb | 759fbf4 | 2015-03-24 13:12:08 +0100 | [diff] [blame] | 74 | static inline void * |
| 75 | weston_platform_get_egl_proc_address(const char *address) |
Jonny Lamb | 51a7ae5 | 2015-03-20 15:26:51 +0100 | [diff] [blame] | 76 | { |
Jonny Lamb | 759fbf4 | 2015-03-24 13:12:08 +0100 | [diff] [blame] | 77 | const char *extensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS); |
Jonny Lamb | 51a7ae5 | 2015-03-20 15:26:51 +0100 | [diff] [blame] | 78 | |
Emil Velikov | d0fcdc9 | 2016-07-04 15:34:19 +0100 | [diff] [blame] | 79 | if (extensions && |
| 80 | (weston_check_egl_extension(extensions, "EGL_EXT_platform_wayland") || |
| 81 | weston_check_egl_extension(extensions, "EGL_KHR_platform_wayland"))) { |
Jonny Lamb | 759fbf4 | 2015-03-24 13:12:08 +0100 | [diff] [blame] | 82 | return (void *) eglGetProcAddress(address); |
Jonny Lamb | 51a7ae5 | 2015-03-20 15:26:51 +0100 | [diff] [blame] | 83 | } |
Jonny Lamb | 759fbf4 | 2015-03-24 13:12:08 +0100 | [diff] [blame] | 84 | |
| 85 | return NULL; |
Jonny Lamb | 51a7ae5 | 2015-03-20 15:26:51 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static inline EGLDisplay |
| 89 | weston_platform_get_egl_display(EGLenum platform, void *native_display, |
| 90 | const EGLint *attrib_list) |
| 91 | { |
Jonny Lamb | 759fbf4 | 2015-03-24 13:12:08 +0100 | [diff] [blame] | 92 | static PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display = NULL; |
Jonny Lamb | 51a7ae5 | 2015-03-20 15:26:51 +0100 | [diff] [blame] | 93 | |
Jonny Lamb | 759fbf4 | 2015-03-24 13:12:08 +0100 | [diff] [blame] | 94 | if (!get_platform_display) { |
Matthias Treydte | cd9424e | 2016-01-29 17:02:15 +0100 | [diff] [blame] | 95 | get_platform_display = (PFNEGLGETPLATFORMDISPLAYEXTPROC) |
| 96 | weston_platform_get_egl_proc_address( |
| 97 | "eglGetPlatformDisplayEXT"); |
Jonny Lamb | 759fbf4 | 2015-03-24 13:12:08 +0100 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | if (get_platform_display) |
| 101 | return get_platform_display(platform, |
| 102 | native_display, attrib_list); |
Jonny Lamb | 0e2ab36 | 2015-03-24 13:12:07 +0100 | [diff] [blame] | 103 | |
| 104 | return eglGetDisplay((EGLNativeDisplayType) native_display); |
Jonny Lamb | 51a7ae5 | 2015-03-20 15:26:51 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Jonny Lamb | 4bdcb57 | 2015-03-20 15:26:53 +0100 | [diff] [blame] | 107 | static inline EGLSurface |
Jonny Lamb | abff883 | 2015-03-24 13:12:09 +0100 | [diff] [blame] | 108 | weston_platform_create_egl_surface(EGLDisplay dpy, EGLConfig config, |
| 109 | void *native_window, |
| 110 | const EGLint *attrib_list) |
Jonny Lamb | 4bdcb57 | 2015-03-20 15:26:53 +0100 | [diff] [blame] | 111 | { |
Jonny Lamb | 759fbf4 | 2015-03-24 13:12:08 +0100 | [diff] [blame] | 112 | static PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC |
| 113 | create_platform_window = NULL; |
Jonny Lamb | 4bdcb57 | 2015-03-20 15:26:53 +0100 | [diff] [blame] | 114 | |
Jonny Lamb | 759fbf4 | 2015-03-24 13:12:08 +0100 | [diff] [blame] | 115 | if (!create_platform_window) { |
Matthias Treydte | cd9424e | 2016-01-29 17:02:15 +0100 | [diff] [blame] | 116 | create_platform_window = (PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC) |
| 117 | weston_platform_get_egl_proc_address( |
| 118 | "eglCreatePlatformWindowSurfaceEXT"); |
Jonny Lamb | 759fbf4 | 2015-03-24 13:12:08 +0100 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | if (create_platform_window) |
| 122 | return create_platform_window(dpy, config, |
| 123 | native_window, |
| 124 | attrib_list); |
Jonny Lamb | 4bdcb57 | 2015-03-20 15:26:53 +0100 | [diff] [blame] | 125 | |
| 126 | return eglCreateWindowSurface(dpy, config, |
| 127 | (EGLNativeWindowType) native_window, |
| 128 | attrib_list); |
| 129 | } |
| 130 | |
Emil Velikov | 3612be2 | 2016-11-14 17:08:12 +0000 | [diff] [blame] | 131 | static inline EGLBoolean |
| 132 | weston_platform_destroy_egl_surface(EGLDisplay display, |
| 133 | EGLSurface surface) |
| 134 | { |
| 135 | return eglDestroySurface(display, surface); |
| 136 | } |
| 137 | |
Jonny Lamb | 0e2ab36 | 2015-03-24 13:12:07 +0100 | [diff] [blame] | 138 | #else /* ENABLE_EGL */ |
| 139 | |
| 140 | static inline void * |
| 141 | weston_platform_get_egl_display(void *platform, void *native_display, |
| 142 | const int *attrib_list) |
| 143 | { |
| 144 | return NULL; |
| 145 | } |
| 146 | |
| 147 | static inline void * |
Jonny Lamb | abff883 | 2015-03-24 13:12:09 +0100 | [diff] [blame] | 148 | weston_platform_create_egl_surface(void *dpy, void *config, |
| 149 | void *native_window, |
| 150 | const int *attrib_list) |
Jonny Lamb | 0e2ab36 | 2015-03-24 13:12:07 +0100 | [diff] [blame] | 151 | { |
| 152 | return NULL; |
| 153 | } |
Emil Velikov | 3612be2 | 2016-11-14 17:08:12 +0000 | [diff] [blame] | 154 | |
| 155 | static inline unsigned int |
| 156 | weston_platform_destroy_egl_surface(void *display, |
| 157 | void *surface) |
| 158 | { |
| 159 | return 1; |
| 160 | } |
Jonny Lamb | 0e2ab36 | 2015-03-24 13:12:07 +0100 | [diff] [blame] | 161 | #endif /* ENABLE_EGL */ |
| 162 | |
Jonny Lamb | 51a7ae5 | 2015-03-20 15:26:51 +0100 | [diff] [blame] | 163 | #ifdef __cplusplus |
| 164 | } |
| 165 | #endif |
| 166 | |
| 167 | #endif /* WESTON_PLATFORM_H */ |