]>
Commit | Line | Data |
---|---|---|
6250dff3 FZ |
1 | /* |
2 | * Copyright (C) 2015-2016 Gerd Hoffmann <[email protected]> | |
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 | */ | |
e16f4c87 | 17 | #include "qemu/osdep.h" |
7ced9e9f | 18 | #include <glob.h> |
1e316598 | 19 | #include <dirent.h> |
7ced9e9f | 20 | |
38a55bdd | 21 | #include "qemu/error-report.h" |
7ced9e9f GH |
22 | #include "ui/egl-helpers.h" |
23 | ||
24 | EGLDisplay *qemu_egl_display; | |
25 | EGLConfig qemu_egl_config; | |
26 | ||
27 | /* ---------------------------------------------------------------------- */ | |
28 | ||
1e316598 GH |
29 | #ifdef CONFIG_OPENGL_DMABUF |
30 | ||
31 | int qemu_egl_rn_fd; | |
32 | struct gbm_device *qemu_egl_rn_gbm_dev; | |
33 | EGLContext qemu_egl_rn_ctx; | |
34 | ||
7b525508 | 35 | static int qemu_egl_rendernode_open(const char *rendernode) |
1e316598 GH |
36 | { |
37 | DIR *dir; | |
38 | struct dirent *e; | |
39 | int r, fd; | |
40 | char *p; | |
41 | ||
7b525508 MAL |
42 | if (rendernode) { |
43 | return open(rendernode, O_RDWR | O_CLOEXEC | O_NOCTTY | O_NONBLOCK); | |
44 | } | |
45 | ||
1e316598 GH |
46 | dir = opendir("/dev/dri"); |
47 | if (!dir) { | |
48 | return -1; | |
49 | } | |
50 | ||
51 | fd = -1; | |
52 | while ((e = readdir(dir))) { | |
53 | if (e->d_type != DT_CHR) { | |
54 | continue; | |
55 | } | |
56 | ||
57 | if (strncmp(e->d_name, "renderD", 7)) { | |
58 | continue; | |
59 | } | |
60 | ||
f454f49c | 61 | p = g_strdup_printf("/dev/dri/%s", e->d_name); |
1e316598 GH |
62 | |
63 | r = open(p, O_RDWR | O_CLOEXEC | O_NOCTTY | O_NONBLOCK); | |
64 | if (r < 0) { | |
f454f49c | 65 | g_free(p); |
1e316598 GH |
66 | continue; |
67 | } | |
68 | fd = r; | |
f454f49c | 69 | g_free(p); |
1e316598 GH |
70 | break; |
71 | } | |
72 | ||
73 | closedir(dir); | |
74 | if (fd < 0) { | |
75 | return -1; | |
76 | } | |
77 | return fd; | |
78 | } | |
79 | ||
7b525508 | 80 | int egl_rendernode_init(const char *rendernode) |
1e316598 GH |
81 | { |
82 | qemu_egl_rn_fd = -1; | |
151c8e60 | 83 | int rc; |
1e316598 | 84 | |
7b525508 | 85 | qemu_egl_rn_fd = qemu_egl_rendernode_open(rendernode); |
1e316598 | 86 | if (qemu_egl_rn_fd == -1) { |
38a55bdd | 87 | error_report("egl: no drm render node available"); |
1e316598 GH |
88 | goto err; |
89 | } | |
90 | ||
91 | qemu_egl_rn_gbm_dev = gbm_create_device(qemu_egl_rn_fd); | |
92 | if (!qemu_egl_rn_gbm_dev) { | |
38a55bdd | 93 | error_report("egl: gbm_create_device failed"); |
1e316598 GH |
94 | goto err; |
95 | } | |
96 | ||
151c8e60 GH |
97 | rc = qemu_egl_init_dpy_mesa((EGLNativeDisplayType)qemu_egl_rn_gbm_dev); |
98 | if (rc != 0) { | |
99 | /* qemu_egl_init_dpy_mesa reports error */ | |
100 | goto err; | |
101 | } | |
1e316598 GH |
102 | |
103 | if (!epoxy_has_egl_extension(qemu_egl_display, | |
104 | "EGL_KHR_surfaceless_context")) { | |
38a55bdd | 105 | error_report("egl: EGL_KHR_surfaceless_context not supported"); |
1e316598 GH |
106 | goto err; |
107 | } | |
108 | if (!epoxy_has_egl_extension(qemu_egl_display, | |
109 | "EGL_MESA_image_dma_buf_export")) { | |
38a55bdd | 110 | error_report("egl: EGL_MESA_image_dma_buf_export not supported"); |
1e316598 GH |
111 | goto err; |
112 | } | |
113 | ||
114 | qemu_egl_rn_ctx = qemu_egl_init_ctx(); | |
115 | if (!qemu_egl_rn_ctx) { | |
38a55bdd | 116 | error_report("egl: egl_init_ctx failed"); |
1e316598 GH |
117 | goto err; |
118 | } | |
119 | ||
120 | return 0; | |
121 | ||
122 | err: | |
123 | if (qemu_egl_rn_gbm_dev) { | |
124 | gbm_device_destroy(qemu_egl_rn_gbm_dev); | |
125 | } | |
126 | if (qemu_egl_rn_fd != -1) { | |
127 | close(qemu_egl_rn_fd); | |
128 | } | |
129 | ||
130 | return -1; | |
131 | } | |
132 | ||
133 | int egl_get_fd_for_texture(uint32_t tex_id, EGLint *stride, EGLint *fourcc) | |
134 | { | |
135 | EGLImageKHR image; | |
136 | EGLint num_planes, fd; | |
137 | ||
138 | image = eglCreateImageKHR(qemu_egl_display, eglGetCurrentContext(), | |
139 | EGL_GL_TEXTURE_2D_KHR, | |
140 | (EGLClientBuffer)(unsigned long)tex_id, | |
141 | NULL); | |
142 | if (!image) { | |
143 | return -1; | |
144 | } | |
145 | ||
146 | eglExportDMABUFImageQueryMESA(qemu_egl_display, image, fourcc, | |
147 | &num_planes, NULL); | |
148 | if (num_planes != 1) { | |
149 | eglDestroyImageKHR(qemu_egl_display, image); | |
150 | return -1; | |
151 | } | |
152 | eglExportDMABUFImageMESA(qemu_egl_display, image, &fd, stride, NULL); | |
153 | eglDestroyImageKHR(qemu_egl_display, image); | |
154 | ||
155 | return fd; | |
156 | } | |
157 | ||
158 | #endif /* CONFIG_OPENGL_DMABUF */ | |
159 | ||
160 | /* ---------------------------------------------------------------------- */ | |
161 | ||
7ced9e9f GH |
162 | EGLSurface qemu_egl_init_surface_x11(EGLContext ectx, Window win) |
163 | { | |
164 | EGLSurface esurface; | |
165 | EGLBoolean b; | |
166 | ||
7ced9e9f GH |
167 | esurface = eglCreateWindowSurface(qemu_egl_display, |
168 | qemu_egl_config, | |
169 | (EGLNativeWindowType)win, NULL); | |
170 | if (esurface == EGL_NO_SURFACE) { | |
38a55bdd | 171 | error_report("egl: eglCreateWindowSurface failed"); |
7ced9e9f GH |
172 | return NULL; |
173 | } | |
174 | ||
175 | b = eglMakeCurrent(qemu_egl_display, esurface, esurface, ectx); | |
176 | if (b == EGL_FALSE) { | |
38a55bdd | 177 | error_report("egl: eglMakeCurrent failed"); |
7ced9e9f GH |
178 | return NULL; |
179 | } | |
180 | ||
181 | return esurface; | |
182 | } | |
183 | ||
184 | /* ---------------------------------------------------------------------- */ | |
185 | ||
8bce03e3 GH |
186 | /* |
187 | * Taken from glamor_egl.h from the Xorg xserver, which is MIT licensed | |
188 | * | |
189 | * Create an EGLDisplay from a native display type. This is a little quirky | |
190 | * for a few reasons. | |
191 | * | |
192 | * 1: GetPlatformDisplayEXT and GetPlatformDisplay are the API you want to | |
193 | * use, but have different function signatures in the third argument; this | |
194 | * happens not to matter for us, at the moment, but it means epoxy won't alias | |
195 | * them together. | |
196 | * | |
197 | * 2: epoxy 1.3 and earlier don't understand EGL client extensions, which | |
198 | * means you can't call "eglGetPlatformDisplayEXT" directly, as the resolver | |
199 | * will crash. | |
200 | * | |
201 | * 3: You can't tell whether you have EGL 1.5 at this point, because | |
202 | * eglQueryString(EGL_VERSION) is a property of the display, which we don't | |
203 | * have yet. So you have to query for extensions no matter what. Fortunately | |
204 | * epoxy_has_egl_extension _does_ let you query for client extensions, so | |
205 | * we don't have to write our own extension string parsing. | |
206 | * | |
207 | * 4. There is no EGL_KHR_platform_base to complement the EXT one, thus one | |
208 | * needs to know EGL 1.5 is supported in order to use the eglGetPlatformDisplay | |
209 | * function pointer. | |
210 | * We can workaround this (circular dependency) by probing for the EGL 1.5 | |
211 | * platform extensions (EGL_KHR_platform_gbm and friends) yet it doesn't seem | |
212 | * like mesa will be able to advertise these (even though it can do EGL 1.5). | |
213 | */ | |
e1913dbb GH |
214 | static EGLDisplay qemu_egl_get_display(EGLNativeDisplayType native, |
215 | EGLenum platform) | |
8bce03e3 GH |
216 | { |
217 | EGLDisplay dpy = EGL_NO_DISPLAY; | |
218 | ||
8bce03e3 GH |
219 | /* In practise any EGL 1.5 implementation would support the EXT extension */ |
220 | if (epoxy_has_egl_extension(NULL, "EGL_EXT_platform_base")) { | |
221 | PFNEGLGETPLATFORMDISPLAYEXTPROC getPlatformDisplayEXT = | |
222 | (void *) eglGetProcAddress("eglGetPlatformDisplayEXT"); | |
e1913dbb GH |
223 | if (getPlatformDisplayEXT && platform != 0) { |
224 | dpy = getPlatformDisplayEXT(platform, native, NULL); | |
8bce03e3 GH |
225 | } |
226 | } | |
8bce03e3 GH |
227 | |
228 | if (dpy == EGL_NO_DISPLAY) { | |
229 | /* fallback */ | |
230 | dpy = eglGetDisplay(native); | |
231 | } | |
232 | return dpy; | |
233 | } | |
234 | ||
e1913dbb GH |
235 | static int qemu_egl_init_dpy(EGLNativeDisplayType dpy, |
236 | EGLenum platform) | |
7ced9e9f GH |
237 | { |
238 | static const EGLint conf_att_gl[] = { | |
239 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, | |
240 | EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, | |
241 | EGL_RED_SIZE, 5, | |
242 | EGL_GREEN_SIZE, 5, | |
243 | EGL_BLUE_SIZE, 5, | |
244 | EGL_ALPHA_SIZE, 0, | |
245 | EGL_NONE, | |
246 | }; | |
7ced9e9f GH |
247 | EGLint major, minor; |
248 | EGLBoolean b; | |
249 | EGLint n; | |
250 | ||
e1913dbb | 251 | qemu_egl_display = qemu_egl_get_display(dpy, platform); |
7ced9e9f | 252 | if (qemu_egl_display == EGL_NO_DISPLAY) { |
38a55bdd | 253 | error_report("egl: eglGetDisplay failed"); |
7ced9e9f GH |
254 | return -1; |
255 | } | |
256 | ||
7ced9e9f GH |
257 | b = eglInitialize(qemu_egl_display, &major, &minor); |
258 | if (b == EGL_FALSE) { | |
38a55bdd | 259 | error_report("egl: eglInitialize failed"); |
7ced9e9f GH |
260 | return -1; |
261 | } | |
262 | ||
9f728c79 | 263 | b = eglBindAPI(EGL_OPENGL_API); |
7ced9e9f | 264 | if (b == EGL_FALSE) { |
38a55bdd | 265 | error_report("egl: eglBindAPI failed"); |
7ced9e9f GH |
266 | return -1; |
267 | } | |
268 | ||
9f728c79 | 269 | b = eglChooseConfig(qemu_egl_display, conf_att_gl, |
7ced9e9f GH |
270 | &qemu_egl_config, 1, &n); |
271 | if (b == EGL_FALSE || n != 1) { | |
38a55bdd | 272 | error_report("egl: eglChooseConfig failed"); |
7ced9e9f GH |
273 | return -1; |
274 | } | |
7ced9e9f GH |
275 | return 0; |
276 | } | |
277 | ||
e1913dbb GH |
278 | int qemu_egl_init_dpy_x11(EGLNativeDisplayType dpy) |
279 | { | |
280 | #ifdef EGL_KHR_platform_x11 | |
281 | return qemu_egl_init_dpy(dpy, EGL_PLATFORM_X11_KHR); | |
282 | #else | |
283 | return qemu_egl_init_dpy(dpy, 0); | |
284 | #endif | |
285 | } | |
286 | ||
287 | int qemu_egl_init_dpy_mesa(EGLNativeDisplayType dpy) | |
288 | { | |
289 | #ifdef EGL_MESA_platform_gbm | |
290 | return qemu_egl_init_dpy(dpy, EGL_PLATFORM_GBM_MESA); | |
291 | #else | |
292 | return qemu_egl_init_dpy(dpy, 0); | |
293 | #endif | |
294 | } | |
295 | ||
7ced9e9f GH |
296 | EGLContext qemu_egl_init_ctx(void) |
297 | { | |
298 | static const EGLint ctx_att_gl[] = { | |
bc8c946f | 299 | EGL_CONTEXT_OPENGL_PROFILE_MASK, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT, |
7ced9e9f GH |
300 | EGL_NONE |
301 | }; | |
7ced9e9f GH |
302 | EGLContext ectx; |
303 | EGLBoolean b; | |
304 | ||
7ced9e9f | 305 | ectx = eglCreateContext(qemu_egl_display, qemu_egl_config, EGL_NO_CONTEXT, |
9f728c79 | 306 | ctx_att_gl); |
7ced9e9f | 307 | if (ectx == EGL_NO_CONTEXT) { |
38a55bdd | 308 | error_report("egl: eglCreateContext failed"); |
7ced9e9f GH |
309 | return NULL; |
310 | } | |
311 | ||
312 | b = eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, ectx); | |
313 | if (b == EGL_FALSE) { | |
38a55bdd | 314 | error_report("egl: eglMakeCurrent failed"); |
7ced9e9f GH |
315 | return NULL; |
316 | } | |
317 | ||
318 | return ectx; | |
319 | } |