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