Add debug message macros controlled by environment variable
[apps/agl-service-homescreen.git] / sample / simple-egl / include / platform.h
1 /*
2  * Copyright © 2015 Collabora, Ltd.
3  *
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:
11  *
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.
24  */
25
26 #ifndef WESTON_PLATFORM_H
27 #define WESTON_PLATFORM_H
28
29 #include <string.h>
30
31 #include <wayland-egl.h>
32 #include <EGL/egl.h>
33 #include <EGL/eglext.h>
34
35 #ifndef EGL_PLATFORM_WAYLAND_KHR
36 #define EGL_PLATFORM_WAYLAND_KHR 0x31D8
37 #endif
38
39 #ifdef  __cplusplus
40 extern "C" {
41 #endif
42
43
44 #ifndef EGL_EXT_platform_base
45 typedef EGLDisplay (*PFNEGLGETPLATFORMDISPLAYEXTPROC) (EGLenum platform,
46                                                        void *native_display,
47                                                        const EGLint *attrib_list);
48 typedef EGLSurface (*PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC) (EGLDisplay dpy,
49                                                                 EGLConfig config,
50                                                                 void *native_window,
51                                                                 const EGLint *attrib_list);
52 #endif
53
54 static inline void *
55 weston_platform_get_egl_proc_address(const char *address)
56 {
57         const char *extensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
58
59         if (extensions
60             && (strstr(extensions, "EGL_EXT_platform_wayland")
61                 || strstr(extensions, "EGL_KHR_platform_wayland"))) {
62                 return (void *) eglGetProcAddress(address);
63         }
64
65         return NULL;
66 }
67
68 static inline EGLDisplay
69 weston_platform_get_egl_display(EGLenum platform, void *native_display,
70                                 const EGLint *attrib_list)
71 {
72         static PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display = NULL;
73
74         if (!get_platform_display) {
75                 get_platform_display = (PFNEGLGETPLATFORMDISPLAYEXTPROC)
76             weston_platform_get_egl_proc_address(
77                 "eglGetPlatformDisplayEXT");
78         }
79
80         if (get_platform_display)
81                 return get_platform_display(platform,
82                                             native_display, attrib_list);
83
84         return eglGetDisplay((EGLNativeDisplayType) native_display);
85 }
86
87 static inline EGLSurface
88 weston_platform_create_egl_surface(EGLDisplay dpy, EGLConfig config,
89                                    void *native_window,
90                                    const EGLint *attrib_list)
91 {
92         static PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC
93                 create_platform_window = NULL;
94
95         if (!create_platform_window) {
96                 create_platform_window = (PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC)
97             weston_platform_get_egl_proc_address(
98                 "eglCreatePlatformWindowSurfaceEXT");
99         }
100
101         if (create_platform_window)
102                 return create_platform_window(dpy, config,
103                                               native_window,
104                                               attrib_list);
105
106         return eglCreateWindowSurface(dpy, config,
107                                       (EGLNativeWindowType) native_window,
108                                       attrib_list);
109 }
110
111 #ifdef  __cplusplus
112 }
113 #endif
114
115 #endif /* WESTON_PLATFORM_H */