blob: 7c530c2825be5803839e0370b4ad975fe803567d [file] [log] [blame]
Frediano Ziglio6250dff2016-12-08 10:45:39 +00001/*
2 * Copyright (C) 2015-2016 Gerd Hoffmann <kraxel@redhat.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 */
Peter Maydelle16f4c82016-01-29 17:49:51 +000017#include "qemu/osdep.h"
Marc-André Lureaub1d38032018-07-13 15:09:06 +020018#include "qemu/drm.h"
Cole Robinson38a55bd2016-05-18 12:40:49 -040019#include "qemu/error-report.h"
Gerd Hoffmann86c05222017-10-10 15:54:51 +020020#include "ui/console.h"
Gerd Hoffmann7ced9e92015-01-06 15:40:00 +010021#include "ui/egl-helpers.h"
22
23EGLDisplay *qemu_egl_display;
24EGLConfig qemu_egl_config;
Gerd Hoffmann54d208f2018-06-18 13:21:41 +020025DisplayGLMode qemu_egl_mode;
Gerd Hoffmann7ced9e92015-01-06 15:40:00 +010026
Gerd Hoffmann6fafc262017-06-14 10:41:46 +020027/* ------------------------------------------------------------------ */
28
Gerd Hoffmann74083f92017-09-27 13:50:31 +020029static void egl_fb_delete_texture(egl_fb *fb)
30{
31 if (!fb->delete_texture) {
32 return;
33 }
34
35 glDeleteTextures(1, &fb->texture);
36 fb->delete_texture = false;
37}
38
Gerd Hoffmann6fafc262017-06-14 10:41:46 +020039void egl_fb_destroy(egl_fb *fb)
40{
41 if (!fb->framebuffer) {
42 return;
43 }
44
Gerd Hoffmann74083f92017-09-27 13:50:31 +020045 egl_fb_delete_texture(fb);
Gerd Hoffmann6fafc262017-06-14 10:41:46 +020046 glDeleteFramebuffers(1, &fb->framebuffer);
47
48 fb->width = 0;
49 fb->height = 0;
50 fb->texture = 0;
51 fb->framebuffer = 0;
52}
53
54void egl_fb_setup_default(egl_fb *fb, int width, int height)
55{
56 fb->width = width;
57 fb->height = height;
58 fb->framebuffer = 0; /* default framebuffer */
59}
60
Gerd Hoffmann74083f92017-09-27 13:50:31 +020061void egl_fb_setup_for_tex(egl_fb *fb, int width, int height,
62 GLuint texture, bool delete)
Gerd Hoffmann6fafc262017-06-14 10:41:46 +020063{
Gerd Hoffmann74083f92017-09-27 13:50:31 +020064 egl_fb_delete_texture(fb);
65
Gerd Hoffmann6fafc262017-06-14 10:41:46 +020066 fb->width = width;
67 fb->height = height;
68 fb->texture = texture;
Gerd Hoffmann74083f92017-09-27 13:50:31 +020069 fb->delete_texture = delete;
Gerd Hoffmann6fafc262017-06-14 10:41:46 +020070 if (!fb->framebuffer) {
71 glGenFramebuffers(1, &fb->framebuffer);
72 }
73
74 glBindFramebuffer(GL_FRAMEBUFFER_EXT, fb->framebuffer);
75 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
76 GL_TEXTURE_2D, fb->texture, 0);
77}
78
Gerd Hoffmann74083f92017-09-27 13:50:31 +020079void egl_fb_setup_new_tex(egl_fb *fb, int width, int height)
Gerd Hoffmann6fafc262017-06-14 10:41:46 +020080{
81 GLuint texture;
82
83 glGenTextures(1, &texture);
84 glBindTexture(GL_TEXTURE_2D, texture);
Gerd Hoffmann41126212018-02-20 12:04:33 +010085 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height,
Gerd Hoffmann6fafc262017-06-14 10:41:46 +020086 0, GL_BGRA, GL_UNSIGNED_BYTE, 0);
87
Gerd Hoffmann74083f92017-09-27 13:50:31 +020088 egl_fb_setup_for_tex(fb, width, height, texture, true);
Gerd Hoffmann6fafc262017-06-14 10:41:46 +020089}
90
91void egl_fb_blit(egl_fb *dst, egl_fb *src, bool flip)
92{
93 GLuint y1, y2;
94
95 glBindFramebuffer(GL_READ_FRAMEBUFFER, src->framebuffer);
96 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, dst->framebuffer);
97 glViewport(0, 0, dst->width, dst->height);
98 y1 = flip ? src->height : 0;
99 y2 = flip ? 0 : src->height;
100 glBlitFramebuffer(0, y1, src->width, y2,
101 0, 0, dst->width, dst->height,
102 GL_COLOR_BUFFER_BIT, GL_LINEAR);
103}
104
Gerd Hoffmannd2329232019-09-09 09:39:11 +0200105void egl_fb_read(DisplaySurface *dst, egl_fb *src)
Gerd Hoffmann6fafc262017-06-14 10:41:46 +0200106{
107 glBindFramebuffer(GL_READ_FRAMEBUFFER, src->framebuffer);
108 glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
Gerd Hoffmannd2329232019-09-09 09:39:11 +0200109 glReadPixels(0, 0, surface_width(dst), surface_height(dst),
110 GL_BGRA, GL_UNSIGNED_BYTE, surface_data(dst));
Gerd Hoffmann6fafc262017-06-14 10:41:46 +0200111}
112
Gerd Hoffmann0eb50c22017-10-10 15:54:52 +0200113void egl_texture_blit(QemuGLShader *gls, egl_fb *dst, egl_fb *src, bool flip)
114{
115 glBindFramebuffer(GL_FRAMEBUFFER_EXT, dst->framebuffer);
116 glViewport(0, 0, dst->width, dst->height);
117 glEnable(GL_TEXTURE_2D);
118 glBindTexture(GL_TEXTURE_2D, src->texture);
119 qemu_gl_run_texture_blit(gls, flip);
120}
121
122void egl_texture_blend(QemuGLShader *gls, egl_fb *dst, egl_fb *src, bool flip,
Chen Zhang051a0cd2019-01-25 15:47:23 +0800123 int x, int y, double scale_x, double scale_y)
Gerd Hoffmann0eb50c22017-10-10 15:54:52 +0200124{
125 glBindFramebuffer(GL_FRAMEBUFFER_EXT, dst->framebuffer);
Chen Zhang051a0cd2019-01-25 15:47:23 +0800126 int w = scale_x * src->width;
127 int h = scale_y * src->height;
Gerd Hoffmann0eb50c22017-10-10 15:54:52 +0200128 if (flip) {
Chen Zhang051a0cd2019-01-25 15:47:23 +0800129 glViewport(x, y, w, h);
Gerd Hoffmann0eb50c22017-10-10 15:54:52 +0200130 } else {
Chen Zhang051a0cd2019-01-25 15:47:23 +0800131 glViewport(x, dst->height - h - y, w, h);
Gerd Hoffmann0eb50c22017-10-10 15:54:52 +0200132 }
133 glEnable(GL_TEXTURE_2D);
134 glBindTexture(GL_TEXTURE_2D, src->texture);
135 glEnable(GL_BLEND);
136 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
137 qemu_gl_run_texture_blit(gls, flip);
138 glDisable(GL_BLEND);
139}
140
Gerd Hoffmann7ced9e92015-01-06 15:40:00 +0100141/* ---------------------------------------------------------------------- */
142
Gerd Hoffmann1e316592015-10-12 12:03:49 +0200143#ifdef CONFIG_OPENGL_DMABUF
144
145int qemu_egl_rn_fd;
146struct gbm_device *qemu_egl_rn_gbm_dev;
147EGLContext qemu_egl_rn_ctx;
148
Gerd Hoffmann54d208f2018-06-18 13:21:41 +0200149int egl_rendernode_init(const char *rendernode, DisplayGLMode mode)
Gerd Hoffmann1e316592015-10-12 12:03:49 +0200150{
151 qemu_egl_rn_fd = -1;
Gerd Hoffmann151c8e62017-05-05 12:40:59 +0200152 int rc;
Gerd Hoffmann1e316592015-10-12 12:03:49 +0200153
Marc-André Lureaub1d38032018-07-13 15:09:06 +0200154 qemu_egl_rn_fd = qemu_drm_rendernode_open(rendernode);
Gerd Hoffmann1e316592015-10-12 12:03:49 +0200155 if (qemu_egl_rn_fd == -1) {
Cole Robinson38a55bd2016-05-18 12:40:49 -0400156 error_report("egl: no drm render node available");
Gerd Hoffmann1e316592015-10-12 12:03:49 +0200157 goto err;
158 }
159
160 qemu_egl_rn_gbm_dev = gbm_create_device(qemu_egl_rn_fd);
161 if (!qemu_egl_rn_gbm_dev) {
Cole Robinson38a55bd2016-05-18 12:40:49 -0400162 error_report("egl: gbm_create_device failed");
Gerd Hoffmann1e316592015-10-12 12:03:49 +0200163 goto err;
164 }
165
Gerd Hoffmann54d208f2018-06-18 13:21:41 +0200166 rc = qemu_egl_init_dpy_mesa((EGLNativeDisplayType)qemu_egl_rn_gbm_dev,
167 mode);
Gerd Hoffmann151c8e62017-05-05 12:40:59 +0200168 if (rc != 0) {
169 /* qemu_egl_init_dpy_mesa reports error */
170 goto err;
171 }
Gerd Hoffmann1e316592015-10-12 12:03:49 +0200172
173 if (!epoxy_has_egl_extension(qemu_egl_display,
174 "EGL_KHR_surfaceless_context")) {
Cole Robinson38a55bd2016-05-18 12:40:49 -0400175 error_report("egl: EGL_KHR_surfaceless_context not supported");
Gerd Hoffmann1e316592015-10-12 12:03:49 +0200176 goto err;
177 }
178 if (!epoxy_has_egl_extension(qemu_egl_display,
179 "EGL_MESA_image_dma_buf_export")) {
Cole Robinson38a55bd2016-05-18 12:40:49 -0400180 error_report("egl: EGL_MESA_image_dma_buf_export not supported");
Gerd Hoffmann1e316592015-10-12 12:03:49 +0200181 goto err;
182 }
183
184 qemu_egl_rn_ctx = qemu_egl_init_ctx();
185 if (!qemu_egl_rn_ctx) {
Cole Robinson38a55bd2016-05-18 12:40:49 -0400186 error_report("egl: egl_init_ctx failed");
Gerd Hoffmann1e316592015-10-12 12:03:49 +0200187 goto err;
188 }
189
190 return 0;
191
192err:
193 if (qemu_egl_rn_gbm_dev) {
194 gbm_device_destroy(qemu_egl_rn_gbm_dev);
195 }
196 if (qemu_egl_rn_fd != -1) {
197 close(qemu_egl_rn_fd);
198 }
199
200 return -1;
201}
202
Gerd Hoffmann5fc1fb62019-05-29 09:21:43 +0200203int egl_get_fd_for_texture(uint32_t tex_id, EGLint *stride, EGLint *fourcc,
204 EGLuint64KHR *modifier)
Gerd Hoffmann1e316592015-10-12 12:03:49 +0200205{
206 EGLImageKHR image;
207 EGLint num_planes, fd;
208
209 image = eglCreateImageKHR(qemu_egl_display, eglGetCurrentContext(),
210 EGL_GL_TEXTURE_2D_KHR,
211 (EGLClientBuffer)(unsigned long)tex_id,
212 NULL);
213 if (!image) {
214 return -1;
215 }
216
217 eglExportDMABUFImageQueryMESA(qemu_egl_display, image, fourcc,
Gerd Hoffmann5fc1fb62019-05-29 09:21:43 +0200218 &num_planes, modifier);
Gerd Hoffmann1e316592015-10-12 12:03:49 +0200219 if (num_planes != 1) {
220 eglDestroyImageKHR(qemu_egl_display, image);
221 return -1;
222 }
223 eglExportDMABUFImageMESA(qemu_egl_display, image, &fd, stride, NULL);
224 eglDestroyImageKHR(qemu_egl_display, image);
225
226 return fd;
227}
228
Gerd Hoffmann86c05222017-10-10 15:54:51 +0200229void egl_dmabuf_import_texture(QemuDmaBuf *dmabuf)
230{
231 EGLImageKHR image = EGL_NO_IMAGE_KHR;
Gerd Hoffmann15ee0d92019-05-29 09:21:44 +0200232 EGLint attrs[64];
233 int i = 0;
Gerd Hoffmann86c05222017-10-10 15:54:51 +0200234
235 if (dmabuf->texture != 0) {
236 return;
237 }
238
Gerd Hoffmann15ee0d92019-05-29 09:21:44 +0200239 attrs[i++] = EGL_WIDTH;
240 attrs[i++] = dmabuf->width;
241 attrs[i++] = EGL_HEIGHT;
242 attrs[i++] = dmabuf->height;
243 attrs[i++] = EGL_LINUX_DRM_FOURCC_EXT;
244 attrs[i++] = dmabuf->fourcc;
245
246 attrs[i++] = EGL_DMA_BUF_PLANE0_FD_EXT;
247 attrs[i++] = dmabuf->fd;
248 attrs[i++] = EGL_DMA_BUF_PLANE0_PITCH_EXT;
249 attrs[i++] = dmabuf->stride;
250 attrs[i++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT;
251 attrs[i++] = 0;
252#ifdef EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT
253 if (dmabuf->modifier) {
254 attrs[i++] = EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT;
255 attrs[i++] = (dmabuf->modifier >> 0) & 0xffffffff;
256 attrs[i++] = EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT;
257 attrs[i++] = (dmabuf->modifier >> 32) & 0xffffffff;
258 }
259#endif
260 attrs[i++] = EGL_NONE;
261
Gerd Hoffmann86c05222017-10-10 15:54:51 +0200262 image = eglCreateImageKHR(qemu_egl_display,
263 EGL_NO_CONTEXT,
264 EGL_LINUX_DMA_BUF_EXT,
265 NULL, attrs);
266 if (image == EGL_NO_IMAGE_KHR) {
267 error_report("eglCreateImageKHR failed");
268 return;
269 }
270
271 glGenTextures(1, &dmabuf->texture);
272 glBindTexture(GL_TEXTURE_2D, dmabuf->texture);
273 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
274 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
275
276 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
277 eglDestroyImageKHR(qemu_egl_display, image);
278}
279
280void egl_dmabuf_release_texture(QemuDmaBuf *dmabuf)
281{
282 if (dmabuf->texture == 0) {
283 return;
284 }
285
286 glDeleteTextures(1, &dmabuf->texture);
287 dmabuf->texture = 0;
288}
289
Gerd Hoffmann1e316592015-10-12 12:03:49 +0200290#endif /* CONFIG_OPENGL_DMABUF */
291
292/* ---------------------------------------------------------------------- */
293
Alexander Kanavinfbd57c72019-01-16 12:37:51 +0100294EGLSurface qemu_egl_init_surface_x11(EGLContext ectx, EGLNativeWindowType win)
Gerd Hoffmann7ced9e92015-01-06 15:40:00 +0100295{
296 EGLSurface esurface;
297 EGLBoolean b;
298
Gerd Hoffmann7ced9e92015-01-06 15:40:00 +0100299 esurface = eglCreateWindowSurface(qemu_egl_display,
300 qemu_egl_config,
Alexander Kanavinfbd57c72019-01-16 12:37:51 +0100301 win, NULL);
Gerd Hoffmann7ced9e92015-01-06 15:40:00 +0100302 if (esurface == EGL_NO_SURFACE) {
Cole Robinson38a55bd2016-05-18 12:40:49 -0400303 error_report("egl: eglCreateWindowSurface failed");
Gerd Hoffmann7ced9e92015-01-06 15:40:00 +0100304 return NULL;
305 }
306
307 b = eglMakeCurrent(qemu_egl_display, esurface, esurface, ectx);
308 if (b == EGL_FALSE) {
Cole Robinson38a55bd2016-05-18 12:40:49 -0400309 error_report("egl: eglMakeCurrent failed");
Gerd Hoffmann7ced9e92015-01-06 15:40:00 +0100310 return NULL;
311 }
312
313 return esurface;
314}
315
316/* ---------------------------------------------------------------------- */
317
Gerd Hoffmann8bce03e2017-03-20 09:04:02 +0100318/*
319 * Taken from glamor_egl.h from the Xorg xserver, which is MIT licensed
320 *
321 * Create an EGLDisplay from a native display type. This is a little quirky
322 * for a few reasons.
323 *
324 * 1: GetPlatformDisplayEXT and GetPlatformDisplay are the API you want to
325 * use, but have different function signatures in the third argument; this
326 * happens not to matter for us, at the moment, but it means epoxy won't alias
327 * them together.
328 *
329 * 2: epoxy 1.3 and earlier don't understand EGL client extensions, which
330 * means you can't call "eglGetPlatformDisplayEXT" directly, as the resolver
331 * will crash.
332 *
333 * 3: You can't tell whether you have EGL 1.5 at this point, because
334 * eglQueryString(EGL_VERSION) is a property of the display, which we don't
335 * have yet. So you have to query for extensions no matter what. Fortunately
336 * epoxy_has_egl_extension _does_ let you query for client extensions, so
337 * we don't have to write our own extension string parsing.
338 *
339 * 4. There is no EGL_KHR_platform_base to complement the EXT one, thus one
340 * needs to know EGL 1.5 is supported in order to use the eglGetPlatformDisplay
341 * function pointer.
342 * We can workaround this (circular dependency) by probing for the EGL 1.5
343 * platform extensions (EGL_KHR_platform_gbm and friends) yet it doesn't seem
344 * like mesa will be able to advertise these (even though it can do EGL 1.5).
345 */
Gerd Hoffmanne1913db2017-05-05 12:40:58 +0200346static EGLDisplay qemu_egl_get_display(EGLNativeDisplayType native,
347 EGLenum platform)
Gerd Hoffmann8bce03e2017-03-20 09:04:02 +0100348{
349 EGLDisplay dpy = EGL_NO_DISPLAY;
350
Gerd Hoffmann8bce03e2017-03-20 09:04:02 +0100351 /* In practise any EGL 1.5 implementation would support the EXT extension */
352 if (epoxy_has_egl_extension(NULL, "EGL_EXT_platform_base")) {
353 PFNEGLGETPLATFORMDISPLAYEXTPROC getPlatformDisplayEXT =
354 (void *) eglGetProcAddress("eglGetPlatformDisplayEXT");
Gerd Hoffmanne1913db2017-05-05 12:40:58 +0200355 if (getPlatformDisplayEXT && platform != 0) {
356 dpy = getPlatformDisplayEXT(platform, native, NULL);
Gerd Hoffmann8bce03e2017-03-20 09:04:02 +0100357 }
358 }
Gerd Hoffmann8bce03e2017-03-20 09:04:02 +0100359
360 if (dpy == EGL_NO_DISPLAY) {
361 /* fallback */
362 dpy = eglGetDisplay(native);
363 }
364 return dpy;
365}
366
Gerd Hoffmanne1913db2017-05-05 12:40:58 +0200367static int qemu_egl_init_dpy(EGLNativeDisplayType dpy,
Gerd Hoffmann54d208f2018-06-18 13:21:41 +0200368 EGLenum platform,
369 DisplayGLMode mode)
Gerd Hoffmann7ced9e92015-01-06 15:40:00 +0100370{
Gerd Hoffmann54d208f2018-06-18 13:21:41 +0200371 static const EGLint conf_att_core[] = {
Gerd Hoffmann7ced9e92015-01-06 15:40:00 +0100372 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
373 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
374 EGL_RED_SIZE, 5,
375 EGL_GREEN_SIZE, 5,
376 EGL_BLUE_SIZE, 5,
377 EGL_ALPHA_SIZE, 0,
378 EGL_NONE,
379 };
Gerd Hoffmann54d208f2018-06-18 13:21:41 +0200380 static const EGLint conf_att_gles[] = {
381 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
382 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
383 EGL_RED_SIZE, 5,
384 EGL_GREEN_SIZE, 5,
385 EGL_BLUE_SIZE, 5,
386 EGL_ALPHA_SIZE, 0,
387 EGL_NONE,
388 };
Gerd Hoffmann7ced9e92015-01-06 15:40:00 +0100389 EGLint major, minor;
390 EGLBoolean b;
391 EGLint n;
Gerd Hoffmann54d208f2018-06-18 13:21:41 +0200392 bool gles = (mode == DISPLAYGL_MODE_ES);
Gerd Hoffmann7ced9e92015-01-06 15:40:00 +0100393
Gerd Hoffmanne1913db2017-05-05 12:40:58 +0200394 qemu_egl_display = qemu_egl_get_display(dpy, platform);
Gerd Hoffmann7ced9e92015-01-06 15:40:00 +0100395 if (qemu_egl_display == EGL_NO_DISPLAY) {
Cole Robinson38a55bd2016-05-18 12:40:49 -0400396 error_report("egl: eglGetDisplay failed");
Gerd Hoffmann7ced9e92015-01-06 15:40:00 +0100397 return -1;
398 }
399
Gerd Hoffmann7ced9e92015-01-06 15:40:00 +0100400 b = eglInitialize(qemu_egl_display, &major, &minor);
401 if (b == EGL_FALSE) {
Cole Robinson38a55bd2016-05-18 12:40:49 -0400402 error_report("egl: eglInitialize failed");
Gerd Hoffmann7ced9e92015-01-06 15:40:00 +0100403 return -1;
404 }
405
Gerd Hoffmann54d208f2018-06-18 13:21:41 +0200406 b = eglBindAPI(gles ? EGL_OPENGL_ES_API : EGL_OPENGL_API);
Gerd Hoffmann7ced9e92015-01-06 15:40:00 +0100407 if (b == EGL_FALSE) {
Gerd Hoffmann54d208f2018-06-18 13:21:41 +0200408 error_report("egl: eglBindAPI failed (%s mode)",
409 gles ? "gles" : "core");
Gerd Hoffmann7ced9e92015-01-06 15:40:00 +0100410 return -1;
411 }
412
Gerd Hoffmann54d208f2018-06-18 13:21:41 +0200413 b = eglChooseConfig(qemu_egl_display,
414 gles ? conf_att_gles : conf_att_core,
Gerd Hoffmann7ced9e92015-01-06 15:40:00 +0100415 &qemu_egl_config, 1, &n);
416 if (b == EGL_FALSE || n != 1) {
Gerd Hoffmann54d208f2018-06-18 13:21:41 +0200417 error_report("egl: eglChooseConfig failed (%s mode)",
418 gles ? "gles" : "core");
Gerd Hoffmann7ced9e92015-01-06 15:40:00 +0100419 return -1;
420 }
Gerd Hoffmann54d208f2018-06-18 13:21:41 +0200421
422 qemu_egl_mode = gles ? DISPLAYGL_MODE_ES : DISPLAYGL_MODE_CORE;
Gerd Hoffmann7ced9e92015-01-06 15:40:00 +0100423 return 0;
424}
425
Gerd Hoffmann54d208f2018-06-18 13:21:41 +0200426int qemu_egl_init_dpy_x11(EGLNativeDisplayType dpy, DisplayGLMode mode)
Gerd Hoffmanne1913db2017-05-05 12:40:58 +0200427{
428#ifdef EGL_KHR_platform_x11
Gerd Hoffmann54d208f2018-06-18 13:21:41 +0200429 return qemu_egl_init_dpy(dpy, EGL_PLATFORM_X11_KHR, mode);
Gerd Hoffmanne1913db2017-05-05 12:40:58 +0200430#else
Gerd Hoffmann54d208f2018-06-18 13:21:41 +0200431 return qemu_egl_init_dpy(dpy, 0, mode);
Gerd Hoffmanne1913db2017-05-05 12:40:58 +0200432#endif
433}
434
Gerd Hoffmann54d208f2018-06-18 13:21:41 +0200435int qemu_egl_init_dpy_mesa(EGLNativeDisplayType dpy, DisplayGLMode mode)
Gerd Hoffmanne1913db2017-05-05 12:40:58 +0200436{
437#ifdef EGL_MESA_platform_gbm
Gerd Hoffmann54d208f2018-06-18 13:21:41 +0200438 return qemu_egl_init_dpy(dpy, EGL_PLATFORM_GBM_MESA, mode);
Gerd Hoffmanne1913db2017-05-05 12:40:58 +0200439#else
Gerd Hoffmann54d208f2018-06-18 13:21:41 +0200440 return qemu_egl_init_dpy(dpy, 0, mode);
Gerd Hoffmanne1913db2017-05-05 12:40:58 +0200441#endif
442}
443
Gerd Hoffmann7ced9e92015-01-06 15:40:00 +0100444EGLContext qemu_egl_init_ctx(void)
445{
Gerd Hoffmann54d208f2018-06-18 13:21:41 +0200446 static const EGLint ctx_att_core[] = {
Gerd Hoffmannbc8c9462017-05-05 12:41:00 +0200447 EGL_CONTEXT_OPENGL_PROFILE_MASK, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT,
Gerd Hoffmann7ced9e92015-01-06 15:40:00 +0100448 EGL_NONE
449 };
Gerd Hoffmann54d208f2018-06-18 13:21:41 +0200450 static const EGLint ctx_att_gles[] = {
451 EGL_CONTEXT_CLIENT_VERSION, 2,
452 EGL_NONE
453 };
454 bool gles = (qemu_egl_mode == DISPLAYGL_MODE_ES);
Gerd Hoffmann7ced9e92015-01-06 15:40:00 +0100455 EGLContext ectx;
456 EGLBoolean b;
457
Gerd Hoffmann7ced9e92015-01-06 15:40:00 +0100458 ectx = eglCreateContext(qemu_egl_display, qemu_egl_config, EGL_NO_CONTEXT,
Gerd Hoffmann54d208f2018-06-18 13:21:41 +0200459 gles ? ctx_att_gles : ctx_att_core);
Gerd Hoffmann7ced9e92015-01-06 15:40:00 +0100460 if (ectx == EGL_NO_CONTEXT) {
Cole Robinson38a55bd2016-05-18 12:40:49 -0400461 error_report("egl: eglCreateContext failed");
Gerd Hoffmann7ced9e92015-01-06 15:40:00 +0100462 return NULL;
463 }
464
465 b = eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, ectx);
466 if (b == EGL_FALSE) {
Cole Robinson38a55bd2016-05-18 12:40:49 -0400467 error_report("egl: eglMakeCurrent failed");
Gerd Hoffmann7ced9e92015-01-06 15:40:00 +0100468 return NULL;
469 }
470
471 return ectx;
472}