xref: /qemu/ui/egl-helpers.c (revision 20daa90a)
1 /*
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  */
17 #include "qemu/osdep.h"
18 #include <glob.h>
19 #include <dirent.h>
20 
21 #include "qemu/error-report.h"
22 #include "ui/egl-helpers.h"
23 
24 EGLDisplay *qemu_egl_display;
25 EGLConfig qemu_egl_config;
26 
27 /* ---------------------------------------------------------------------- */
28 
29 static bool egl_gles;
30 static int egl_debug;
31 
32 #define egl_dbg(_x ...)                          \
33     do {                                         \
34         if (egl_debug) {                         \
35             fprintf(stderr, "egl: " _x);         \
36         }                                        \
37     } while (0);
38 
39 /* ---------------------------------------------------------------------- */
40 
41 #ifdef CONFIG_OPENGL_DMABUF
42 
43 int qemu_egl_rn_fd;
44 struct gbm_device *qemu_egl_rn_gbm_dev;
45 EGLContext qemu_egl_rn_ctx;
46 
47 int qemu_egl_rendernode_open(void)
48 {
49     DIR *dir;
50     struct dirent *e;
51     int r, fd;
52     char *p;
53 
54     dir = opendir("/dev/dri");
55     if (!dir) {
56         return -1;
57     }
58 
59     fd = -1;
60     while ((e = readdir(dir))) {
61         if (e->d_type != DT_CHR) {
62             continue;
63         }
64 
65         if (strncmp(e->d_name, "renderD", 7)) {
66             continue;
67         }
68 
69         p = g_strdup_printf("/dev/dri/%s", e->d_name);
70 
71         r = open(p, O_RDWR | O_CLOEXEC | O_NOCTTY | O_NONBLOCK);
72         if (r < 0) {
73             g_free(p);
74             continue;
75         }
76         fd = r;
77         g_free(p);
78         break;
79     }
80 
81     closedir(dir);
82     if (fd < 0) {
83         return -1;
84     }
85     return fd;
86 }
87 
88 int egl_rendernode_init(void)
89 {
90     qemu_egl_rn_fd = -1;
91 
92     qemu_egl_rn_fd = qemu_egl_rendernode_open();
93     if (qemu_egl_rn_fd == -1) {
94         error_report("egl: no drm render node available");
95         goto err;
96     }
97 
98     qemu_egl_rn_gbm_dev = gbm_create_device(qemu_egl_rn_fd);
99     if (!qemu_egl_rn_gbm_dev) {
100         error_report("egl: gbm_create_device failed");
101         goto err;
102     }
103 
104     qemu_egl_init_dpy((EGLNativeDisplayType)qemu_egl_rn_gbm_dev, false, false);
105 
106     if (!epoxy_has_egl_extension(qemu_egl_display,
107                                  "EGL_KHR_surfaceless_context")) {
108         error_report("egl: EGL_KHR_surfaceless_context not supported");
109         goto err;
110     }
111     if (!epoxy_has_egl_extension(qemu_egl_display,
112                                  "EGL_MESA_image_dma_buf_export")) {
113         error_report("egl: EGL_MESA_image_dma_buf_export not supported");
114         goto err;
115     }
116 
117     qemu_egl_rn_ctx = qemu_egl_init_ctx();
118     if (!qemu_egl_rn_ctx) {
119         error_report("egl: egl_init_ctx failed");
120         goto err;
121     }
122 
123     return 0;
124 
125 err:
126     if (qemu_egl_rn_gbm_dev) {
127         gbm_device_destroy(qemu_egl_rn_gbm_dev);
128     }
129     if (qemu_egl_rn_fd != -1) {
130         close(qemu_egl_rn_fd);
131     }
132 
133     return -1;
134 }
135 
136 int egl_get_fd_for_texture(uint32_t tex_id, EGLint *stride, EGLint *fourcc)
137 {
138     EGLImageKHR image;
139     EGLint num_planes, fd;
140 
141     image = eglCreateImageKHR(qemu_egl_display, eglGetCurrentContext(),
142                               EGL_GL_TEXTURE_2D_KHR,
143                               (EGLClientBuffer)(unsigned long)tex_id,
144                               NULL);
145     if (!image) {
146         return -1;
147     }
148 
149     eglExportDMABUFImageQueryMESA(qemu_egl_display, image, fourcc,
150                                   &num_planes, NULL);
151     if (num_planes != 1) {
152         eglDestroyImageKHR(qemu_egl_display, image);
153         return -1;
154     }
155     eglExportDMABUFImageMESA(qemu_egl_display, image, &fd, stride, NULL);
156     eglDestroyImageKHR(qemu_egl_display, image);
157 
158     return fd;
159 }
160 
161 #endif /* CONFIG_OPENGL_DMABUF */
162 
163 /* ---------------------------------------------------------------------- */
164 
165 EGLSurface qemu_egl_init_surface_x11(EGLContext ectx, Window win)
166 {
167     EGLSurface esurface;
168     EGLBoolean b;
169 
170     egl_dbg("eglCreateWindowSurface (x11 win id 0x%lx) ...\n",
171             (unsigned long) win);
172     esurface = eglCreateWindowSurface(qemu_egl_display,
173                                       qemu_egl_config,
174                                       (EGLNativeWindowType)win, NULL);
175     if (esurface == EGL_NO_SURFACE) {
176         error_report("egl: eglCreateWindowSurface failed");
177         return NULL;
178     }
179 
180     b = eglMakeCurrent(qemu_egl_display, esurface, esurface, ectx);
181     if (b == EGL_FALSE) {
182         error_report("egl: eglMakeCurrent failed");
183         return NULL;
184     }
185 
186     return esurface;
187 }
188 
189 /* ---------------------------------------------------------------------- */
190 
191 int qemu_egl_init_dpy(EGLNativeDisplayType dpy, bool gles, bool debug)
192 {
193     static const EGLint conf_att_gl[] = {
194         EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
195         EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
196         EGL_RED_SIZE,   5,
197         EGL_GREEN_SIZE, 5,
198         EGL_BLUE_SIZE,  5,
199         EGL_ALPHA_SIZE, 0,
200         EGL_NONE,
201     };
202     static const EGLint conf_att_gles[] = {
203         EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
204         EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
205         EGL_RED_SIZE,   5,
206         EGL_GREEN_SIZE, 5,
207         EGL_BLUE_SIZE,  5,
208         EGL_ALPHA_SIZE, 0,
209         EGL_NONE,
210     };
211     EGLint major, minor;
212     EGLBoolean b;
213     EGLint n;
214 
215     if (debug) {
216         egl_debug = 1;
217         setenv("EGL_LOG_LEVEL", "debug", true);
218         setenv("LIBGL_DEBUG", "verbose", true);
219     }
220 
221     egl_dbg("eglGetDisplay (dpy %p) ...\n", dpy);
222     qemu_egl_display = eglGetDisplay(dpy);
223     if (qemu_egl_display == EGL_NO_DISPLAY) {
224         error_report("egl: eglGetDisplay failed");
225         return -1;
226     }
227 
228     egl_dbg("eglInitialize ...\n");
229     b = eglInitialize(qemu_egl_display, &major, &minor);
230     if (b == EGL_FALSE) {
231         error_report("egl: eglInitialize failed");
232         return -1;
233     }
234 
235     egl_dbg("eglBindAPI ...\n");
236     b = eglBindAPI(gles ? EGL_OPENGL_ES_API : EGL_OPENGL_API);
237     if (b == EGL_FALSE) {
238         error_report("egl: eglBindAPI failed");
239         return -1;
240     }
241 
242     egl_dbg("eglChooseConfig ...\n");
243     b = eglChooseConfig(qemu_egl_display,
244                         gles ? conf_att_gles : conf_att_gl,
245                         &qemu_egl_config, 1, &n);
246     if (b == EGL_FALSE || n != 1) {
247         error_report("egl: eglChooseConfig failed");
248         return -1;
249     }
250 
251     egl_gles = gles;
252     return 0;
253 }
254 
255 EGLContext qemu_egl_init_ctx(void)
256 {
257     static const EGLint ctx_att_gl[] = {
258         EGL_NONE
259     };
260     static const EGLint ctx_att_gles[] = {
261         EGL_CONTEXT_CLIENT_VERSION, 2,
262         EGL_NONE
263     };
264 
265     EGLContext ectx;
266     EGLBoolean b;
267 
268     egl_dbg("eglCreateContext ...\n");
269     ectx = eglCreateContext(qemu_egl_display, qemu_egl_config, EGL_NO_CONTEXT,
270                             egl_gles ? ctx_att_gles : ctx_att_gl);
271     if (ectx == EGL_NO_CONTEXT) {
272         error_report("egl: eglCreateContext failed");
273         return NULL;
274     }
275 
276     b = eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, ectx);
277     if (b == EGL_FALSE) {
278         error_report("egl: eglMakeCurrent failed");
279         return NULL;
280     }
281 
282     return ectx;
283 }
284