2 // https://www.khronos.org/registry/EGL/extensions/MESA/EGL_MESA_platform_gbm.txt
15 #include <EGL/eglext.h>
16 #include <GLES2/gl2.h>
19 struct gbm_device
*gbm
;
24 struct my_display dpy
;
29 struct my_config config
;
30 struct gbm_surface
*gbm
;
34 PFNEGLGETPLATFORMDISPLAYEXTPROC getPlatformDisplayEXT
;
35 PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC createPlatformWindowSurfaceEXT
;
38 check_extensions(void)
40 const char *client_extensions
= eglQueryString(EGL_NO_DISPLAY
, EGL_EXTENSIONS
);
42 if (!client_extensions
) {
45 if (!strstr(client_extensions
, "EGL_MESA_platform_gbm")) {
49 if (!strstr(client_extensions
, "EGL_EXT_platform_base")) {
53 getPlatformDisplayEXT
=
54 (void *) eglGetProcAddress("eglGetPlatformDisplayEXT");
55 createPlatformWindowSurfaceEXT
=
56 (void *) eglGetProcAddress("eglCreatePlatformWindowSurfaceEXT");
60 static struct my_display
63 struct my_display dpy
;
66 int fd
= open("/dev/dri/card0", O_RDWR
| FD_CLOEXEC
);
70 dpy
.gbm
= gbm_create_device(fd
);
74 dpy
.egl
= getPlatformDisplayEXT(EGL_PLATFORM_GBM_MESA
, dpy
.gbm
, NULL
);
76 if (dpy
.egl
== EGL_NO_DISPLAY
)
79 if (eglInitialize(dpy
.egl
, &major
, &minor
))
80 printf ("EGL %d.%d\n", major
, minor
);
87 static struct my_config
88 get_config(struct my_display dpy
)
90 struct my_config config
= {
94 EGLint egl_config_attribs
[] = {
96 EGL_DEPTH_SIZE
, EGL_DONT_CARE
,
97 EGL_STENCIL_SIZE
, EGL_DONT_CARE
,
98 EGL_RENDERABLE_TYPE
, EGL_OPENGL_ES2_BIT
,
99 EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
,
104 if (!eglGetConfigs(dpy
.egl
, NULL
, 0, &num_configs
))
107 EGLConfig
*configs
= malloc(num_configs
* sizeof(EGLConfig
));
108 if (!eglChooseConfig(dpy
.egl
, egl_config_attribs
,
109 configs
, num_configs
, &num_configs
)) {
112 if (num_configs
== 0)
115 for (int i
= 0; i
< num_configs
; ++i
) {
117 struct gbm_format_name_desc desc
;
119 if (!eglGetConfigAttrib(dpy
.egl
, configs
[i
],
120 EGL_NATIVE_VISUAL_ID
, &gbm_format
)) {
124 printf ("found gbm_format: %s\n", gbm_format_get_name (gbm_format
, &desc
));
125 if (gbm_format
== GBM_FORMAT_ARGB8888
) {
126 config
.egl
= configs
[i
];
132 // no egl config matching gbm format
136 static struct my_window
137 get_window(struct my_config config
)
139 struct my_window window
= {
143 window
.gbm
= gbm_surface_create(config
.dpy
.gbm
,
146 GBM_BO_USE_RENDERING
);
150 window
.egl
= createPlatformWindowSurfaceEXT(config
.dpy
.egl
,
154 if (window
.egl
== EGL_NO_SURFACE
)
165 struct my_display dpy
= get_display();
166 struct my_config config
= get_config(dpy
);
167 struct my_window window
= get_window(config
);
170 context
= eglCreateContext(dpy
.egl
, config
.egl
, EGL_NO_CONTEXT
, NULL
);
171 eglMakeCurrent(dpy
.egl
, window
.egl
, window
.egl
, context
);
173 /* just so we have some gles symbols too */
174 glClearColor(1.0, 1.0, 1.0, 1.0);
175 glClear(GL_COLOR_BUFFER_BIT
);