Commit | Line | Data |
---|---|---|
6250dff3 FZ |
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 | */ | |
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; | |
83 | ||
7b525508 | 84 | qemu_egl_rn_fd = qemu_egl_rendernode_open(rendernode); |
1e316598 | 85 | if (qemu_egl_rn_fd == -1) { |
38a55bdd | 86 | error_report("egl: no drm render node available"); |
1e316598 GH |
87 | goto err; |
88 | } | |
89 | ||
90 | qemu_egl_rn_gbm_dev = gbm_create_device(qemu_egl_rn_fd); | |
91 | if (!qemu_egl_rn_gbm_dev) { | |
38a55bdd | 92 | error_report("egl: gbm_create_device failed"); |
1e316598 GH |
93 | goto err; |
94 | } | |
95 | ||
9f728c79 | 96 | qemu_egl_init_dpy((EGLNativeDisplayType)qemu_egl_rn_gbm_dev); |
1e316598 GH |
97 | |
98 | if (!epoxy_has_egl_extension(qemu_egl_display, | |
99 | "EGL_KHR_surfaceless_context")) { | |
38a55bdd | 100 | error_report("egl: EGL_KHR_surfaceless_context not supported"); |
1e316598 GH |
101 | goto err; |
102 | } | |
103 | if (!epoxy_has_egl_extension(qemu_egl_display, | |
104 | "EGL_MESA_image_dma_buf_export")) { | |
38a55bdd | 105 | error_report("egl: EGL_MESA_image_dma_buf_export not supported"); |
1e316598 GH |
106 | goto err; |
107 | } | |
108 | ||
109 | qemu_egl_rn_ctx = qemu_egl_init_ctx(); | |
110 | if (!qemu_egl_rn_ctx) { | |
38a55bdd | 111 | error_report("egl: egl_init_ctx failed"); |
1e316598 GH |
112 | goto err; |
113 | } | |
114 | ||
115 | return 0; | |
116 | ||
117 | err: | |
118 | if (qemu_egl_rn_gbm_dev) { | |
119 | gbm_device_destroy(qemu_egl_rn_gbm_dev); | |
120 | } | |
121 | if (qemu_egl_rn_fd != -1) { | |
122 | close(qemu_egl_rn_fd); | |
123 | } | |
124 | ||
125 | return -1; | |
126 | } | |
127 | ||
128 | int egl_get_fd_for_texture(uint32_t tex_id, EGLint *stride, EGLint *fourcc) | |
129 | { | |
130 | EGLImageKHR image; | |
131 | EGLint num_planes, fd; | |
132 | ||
133 | image = eglCreateImageKHR(qemu_egl_display, eglGetCurrentContext(), | |
134 | EGL_GL_TEXTURE_2D_KHR, | |
135 | (EGLClientBuffer)(unsigned long)tex_id, | |
136 | NULL); | |
137 | if (!image) { | |
138 | return -1; | |
139 | } | |
140 | ||
141 | eglExportDMABUFImageQueryMESA(qemu_egl_display, image, fourcc, | |
142 | &num_planes, NULL); | |
143 | if (num_planes != 1) { | |
144 | eglDestroyImageKHR(qemu_egl_display, image); | |
145 | return -1; | |
146 | } | |
147 | eglExportDMABUFImageMESA(qemu_egl_display, image, &fd, stride, NULL); | |
148 | eglDestroyImageKHR(qemu_egl_display, image); | |
149 | ||
150 | return fd; | |
151 | } | |
152 | ||
153 | #endif /* CONFIG_OPENGL_DMABUF */ | |
154 | ||
155 | /* ---------------------------------------------------------------------- */ | |
156 | ||
7ced9e9f GH |
157 | EGLSurface qemu_egl_init_surface_x11(EGLContext ectx, Window win) |
158 | { | |
159 | EGLSurface esurface; | |
160 | EGLBoolean b; | |
161 | ||
7ced9e9f GH |
162 | esurface = eglCreateWindowSurface(qemu_egl_display, |
163 | qemu_egl_config, | |
164 | (EGLNativeWindowType)win, NULL); | |
165 | if (esurface == EGL_NO_SURFACE) { | |
38a55bdd | 166 | error_report("egl: eglCreateWindowSurface failed"); |
7ced9e9f GH |
167 | return NULL; |
168 | } | |
169 | ||
170 | b = eglMakeCurrent(qemu_egl_display, esurface, esurface, ectx); | |
171 | if (b == EGL_FALSE) { | |
38a55bdd | 172 | error_report("egl: eglMakeCurrent failed"); |
7ced9e9f GH |
173 | return NULL; |
174 | } | |
175 | ||
176 | return esurface; | |
177 | } | |
178 | ||
179 | /* ---------------------------------------------------------------------- */ | |
180 | ||
8bce03e3 GH |
181 | /* |
182 | * Taken from glamor_egl.h from the Xorg xserver, which is MIT licensed | |
183 | * | |
184 | * Create an EGLDisplay from a native display type. This is a little quirky | |
185 | * for a few reasons. | |
186 | * | |
187 | * 1: GetPlatformDisplayEXT and GetPlatformDisplay are the API you want to | |
188 | * use, but have different function signatures in the third argument; this | |
189 | * happens not to matter for us, at the moment, but it means epoxy won't alias | |
190 | * them together. | |
191 | * | |
192 | * 2: epoxy 1.3 and earlier don't understand EGL client extensions, which | |
193 | * means you can't call "eglGetPlatformDisplayEXT" directly, as the resolver | |
194 | * will crash. | |
195 | * | |
196 | * 3: You can't tell whether you have EGL 1.5 at this point, because | |
197 | * eglQueryString(EGL_VERSION) is a property of the display, which we don't | |
198 | * have yet. So you have to query for extensions no matter what. Fortunately | |
199 | * epoxy_has_egl_extension _does_ let you query for client extensions, so | |
200 | * we don't have to write our own extension string parsing. | |
201 | * | |
202 | * 4. There is no EGL_KHR_platform_base to complement the EXT one, thus one | |
203 | * needs to know EGL 1.5 is supported in order to use the eglGetPlatformDisplay | |
204 | * function pointer. | |
205 | * We can workaround this (circular dependency) by probing for the EGL 1.5 | |
206 | * platform extensions (EGL_KHR_platform_gbm and friends) yet it doesn't seem | |
207 | * like mesa will be able to advertise these (even though it can do EGL 1.5). | |
208 | */ | |
209 | static EGLDisplay qemu_egl_get_display(void *native) | |
210 | { | |
211 | EGLDisplay dpy = EGL_NO_DISPLAY; | |
212 | ||
213 | #ifdef EGL_MESA_platform_gbm | |
214 | /* In practise any EGL 1.5 implementation would support the EXT extension */ | |
215 | if (epoxy_has_egl_extension(NULL, "EGL_EXT_platform_base")) { | |
216 | PFNEGLGETPLATFORMDISPLAYEXTPROC getPlatformDisplayEXT = | |
217 | (void *) eglGetProcAddress("eglGetPlatformDisplayEXT"); | |
218 | if (getPlatformDisplayEXT) { | |
219 | dpy = getPlatformDisplayEXT(EGL_PLATFORM_GBM_MESA, native, NULL); | |
220 | } | |
221 | } | |
222 | #endif | |
223 | ||
224 | if (dpy == EGL_NO_DISPLAY) { | |
225 | /* fallback */ | |
226 | dpy = eglGetDisplay(native); | |
227 | } | |
228 | return dpy; | |
229 | } | |
230 | ||
9f728c79 | 231 | int qemu_egl_init_dpy(EGLNativeDisplayType dpy) |
7ced9e9f GH |
232 | { |
233 | static const EGLint conf_att_gl[] = { | |
234 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, | |
235 | EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, | |
236 | EGL_RED_SIZE, 5, | |
237 | EGL_GREEN_SIZE, 5, | |
238 | EGL_BLUE_SIZE, 5, | |
239 | EGL_ALPHA_SIZE, 0, | |
240 | EGL_NONE, | |
241 | }; | |
7ced9e9f GH |
242 | EGLint major, minor; |
243 | EGLBoolean b; | |
244 | EGLint n; | |
245 | ||
8bce03e3 | 246 | qemu_egl_display = qemu_egl_get_display(dpy); |
7ced9e9f | 247 | if (qemu_egl_display == EGL_NO_DISPLAY) { |
38a55bdd | 248 | error_report("egl: eglGetDisplay failed"); |
7ced9e9f GH |
249 | return -1; |
250 | } | |
251 | ||
7ced9e9f GH |
252 | b = eglInitialize(qemu_egl_display, &major, &minor); |
253 | if (b == EGL_FALSE) { | |
38a55bdd | 254 | error_report("egl: eglInitialize failed"); |
7ced9e9f GH |
255 | return -1; |
256 | } | |
257 | ||
9f728c79 | 258 | b = eglBindAPI(EGL_OPENGL_API); |
7ced9e9f | 259 | if (b == EGL_FALSE) { |
38a55bdd | 260 | error_report("egl: eglBindAPI failed"); |
7ced9e9f GH |
261 | return -1; |
262 | } | |
263 | ||
9f728c79 | 264 | b = eglChooseConfig(qemu_egl_display, conf_att_gl, |
7ced9e9f GH |
265 | &qemu_egl_config, 1, &n); |
266 | if (b == EGL_FALSE || n != 1) { | |
38a55bdd | 267 | error_report("egl: eglChooseConfig failed"); |
7ced9e9f GH |
268 | return -1; |
269 | } | |
7ced9e9f GH |
270 | return 0; |
271 | } | |
272 | ||
273 | EGLContext qemu_egl_init_ctx(void) | |
274 | { | |
275 | static const EGLint ctx_att_gl[] = { | |
276 | EGL_NONE | |
277 | }; | |
7ced9e9f GH |
278 | EGLContext ectx; |
279 | EGLBoolean b; | |
280 | ||
7ced9e9f | 281 | ectx = eglCreateContext(qemu_egl_display, qemu_egl_config, EGL_NO_CONTEXT, |
9f728c79 | 282 | ctx_att_gl); |
7ced9e9f | 283 | if (ectx == EGL_NO_CONTEXT) { |
38a55bdd | 284 | error_report("egl: eglCreateContext failed"); |
7ced9e9f GH |
285 | return NULL; |
286 | } | |
287 | ||
288 | b = eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, ectx); | |
289 | if (b == EGL_FALSE) { | |
38a55bdd | 290 | error_report("egl: eglMakeCurrent failed"); |
7ced9e9f GH |
291 | return NULL; |
292 | } | |
293 | ||
294 | return ectx; | |
295 | } |