spapr_iommu: Move table allocation to helpers
[qemu.git] / ui / egl-helpers.c
blob22835c06262b57f5df441666d1ec8a67dda453c5
1 #include "qemu/osdep.h"
2 #include <glob.h>
3 #include <dirent.h>
5 #include "ui/egl-helpers.h"
7 EGLDisplay *qemu_egl_display;
8 EGLConfig qemu_egl_config;
10 /* ---------------------------------------------------------------------- */
12 static bool egl_gles;
13 static int egl_debug;
15 #define egl_dbg(_x ...) \
16 do { \
17 if (egl_debug) { \
18 fprintf(stderr, "egl: " _x); \
19 } \
20 } while (0);
22 /* ---------------------------------------------------------------------- */
24 #ifdef CONFIG_OPENGL_DMABUF
26 int qemu_egl_rn_fd;
27 struct gbm_device *qemu_egl_rn_gbm_dev;
28 EGLContext qemu_egl_rn_ctx;
30 int qemu_egl_rendernode_open(void)
32 DIR *dir;
33 struct dirent *e;
34 int r, fd;
35 char *p;
37 dir = opendir("/dev/dri");
38 if (!dir) {
39 return -1;
42 fd = -1;
43 while ((e = readdir(dir))) {
44 if (e->d_type != DT_CHR) {
45 continue;
48 if (strncmp(e->d_name, "renderD", 7)) {
49 continue;
52 p = g_strdup_printf("/dev/dri/%s", e->d_name);
54 r = open(p, O_RDWR | O_CLOEXEC | O_NOCTTY | O_NONBLOCK);
55 if (r < 0) {
56 g_free(p);
57 continue;
59 fd = r;
60 g_free(p);
61 break;
64 closedir(dir);
65 if (fd < 0) {
66 return -1;
68 return fd;
71 int egl_rendernode_init(void)
73 qemu_egl_rn_fd = -1;
75 qemu_egl_rn_fd = qemu_egl_rendernode_open();
76 if (qemu_egl_rn_fd == -1) {
77 fprintf(stderr, "egl: no drm render node available\n");
78 goto err;
81 qemu_egl_rn_gbm_dev = gbm_create_device(qemu_egl_rn_fd);
82 if (!qemu_egl_rn_gbm_dev) {
83 fprintf(stderr, "egl: gbm_create_device failed\n");
84 goto err;
87 qemu_egl_init_dpy((EGLNativeDisplayType)qemu_egl_rn_gbm_dev, false, false);
89 if (!epoxy_has_egl_extension(qemu_egl_display,
90 "EGL_KHR_surfaceless_context")) {
91 fprintf(stderr, "egl: EGL_KHR_surfaceless_context not supported\n");
92 goto err;
94 if (!epoxy_has_egl_extension(qemu_egl_display,
95 "EGL_MESA_image_dma_buf_export")) {
96 fprintf(stderr, "egl: EGL_MESA_image_dma_buf_export not supported\n");
97 goto err;
100 qemu_egl_rn_ctx = qemu_egl_init_ctx();
101 if (!qemu_egl_rn_ctx) {
102 fprintf(stderr, "egl: egl_init_ctx failed\n");
103 goto err;
106 return 0;
108 err:
109 if (qemu_egl_rn_gbm_dev) {
110 gbm_device_destroy(qemu_egl_rn_gbm_dev);
112 if (qemu_egl_rn_fd != -1) {
113 close(qemu_egl_rn_fd);
116 return -1;
119 int egl_get_fd_for_texture(uint32_t tex_id, EGLint *stride, EGLint *fourcc)
121 EGLImageKHR image;
122 EGLint num_planes, fd;
124 image = eglCreateImageKHR(qemu_egl_display, eglGetCurrentContext(),
125 EGL_GL_TEXTURE_2D_KHR,
126 (EGLClientBuffer)(unsigned long)tex_id,
127 NULL);
128 if (!image) {
129 return -1;
132 eglExportDMABUFImageQueryMESA(qemu_egl_display, image, fourcc,
133 &num_planes, NULL);
134 if (num_planes != 1) {
135 eglDestroyImageKHR(qemu_egl_display, image);
136 return -1;
138 eglExportDMABUFImageMESA(qemu_egl_display, image, &fd, stride, NULL);
139 eglDestroyImageKHR(qemu_egl_display, image);
141 return fd;
144 #endif /* CONFIG_OPENGL_DMABUF */
146 /* ---------------------------------------------------------------------- */
148 EGLSurface qemu_egl_init_surface_x11(EGLContext ectx, Window win)
150 EGLSurface esurface;
151 EGLBoolean b;
153 egl_dbg("eglCreateWindowSurface (x11 win id 0x%lx) ...\n",
154 (unsigned long) win);
155 esurface = eglCreateWindowSurface(qemu_egl_display,
156 qemu_egl_config,
157 (EGLNativeWindowType)win, NULL);
158 if (esurface == EGL_NO_SURFACE) {
159 fprintf(stderr, "egl: eglCreateWindowSurface failed\n");
160 return NULL;
163 b = eglMakeCurrent(qemu_egl_display, esurface, esurface, ectx);
164 if (b == EGL_FALSE) {
165 fprintf(stderr, "egl: eglMakeCurrent failed\n");
166 return NULL;
169 return esurface;
172 /* ---------------------------------------------------------------------- */
174 int qemu_egl_init_dpy(EGLNativeDisplayType dpy, bool gles, bool debug)
176 static const EGLint conf_att_gl[] = {
177 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
178 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
179 EGL_RED_SIZE, 5,
180 EGL_GREEN_SIZE, 5,
181 EGL_BLUE_SIZE, 5,
182 EGL_ALPHA_SIZE, 0,
183 EGL_NONE,
185 static const EGLint conf_att_gles[] = {
186 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
187 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
188 EGL_RED_SIZE, 5,
189 EGL_GREEN_SIZE, 5,
190 EGL_BLUE_SIZE, 5,
191 EGL_ALPHA_SIZE, 0,
192 EGL_NONE,
194 EGLint major, minor;
195 EGLBoolean b;
196 EGLint n;
198 if (debug) {
199 egl_debug = 1;
200 setenv("EGL_LOG_LEVEL", "debug", true);
201 setenv("LIBGL_DEBUG", "verbose", true);
204 egl_dbg("eglGetDisplay (dpy %p) ...\n", dpy);
205 qemu_egl_display = eglGetDisplay(dpy);
206 if (qemu_egl_display == EGL_NO_DISPLAY) {
207 fprintf(stderr, "egl: eglGetDisplay failed\n");
208 return -1;
211 egl_dbg("eglInitialize ...\n");
212 b = eglInitialize(qemu_egl_display, &major, &minor);
213 if (b == EGL_FALSE) {
214 fprintf(stderr, "egl: eglInitialize failed\n");
215 return -1;
218 egl_dbg("eglBindAPI ...\n");
219 b = eglBindAPI(gles ? EGL_OPENGL_ES_API : EGL_OPENGL_API);
220 if (b == EGL_FALSE) {
221 fprintf(stderr, "egl: eglBindAPI failed\n");
222 return -1;
225 egl_dbg("eglChooseConfig ...\n");
226 b = eglChooseConfig(qemu_egl_display,
227 gles ? conf_att_gles : conf_att_gl,
228 &qemu_egl_config, 1, &n);
229 if (b == EGL_FALSE || n != 1) {
230 fprintf(stderr, "egl: eglChooseConfig failed\n");
231 return -1;
234 egl_gles = gles;
235 return 0;
238 EGLContext qemu_egl_init_ctx(void)
240 static const EGLint ctx_att_gl[] = {
241 EGL_NONE
243 static const EGLint ctx_att_gles[] = {
244 EGL_CONTEXT_CLIENT_VERSION, 2,
245 EGL_NONE
248 EGLContext ectx;
249 EGLBoolean b;
251 egl_dbg("eglCreateContext ...\n");
252 ectx = eglCreateContext(qemu_egl_display, qemu_egl_config, EGL_NO_CONTEXT,
253 egl_gles ? ctx_att_gles : ctx_att_gl);
254 if (ectx == EGL_NO_CONTEXT) {
255 fprintf(stderr, "egl: eglCreateContext failed\n");
256 return NULL;
259 b = eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, ectx);
260 if (b == EGL_FALSE) {
261 fprintf(stderr, "egl: eglMakeCurrent failed\n");
262 return NULL;
265 return ectx;