blob: 1f6b845500dd29dc63d74f0f75c523863a2b4d80 [file] [log] [blame]
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +02001#include "qemu/osdep.h"
Thomas Huth5feed382023-02-10 12:19:31 +01002#include "qemu/error-report.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +02003#include "qemu/module.h"
Marc-André Lureau0e1be592023-02-14 16:11:24 +04004#include "qapi/error.h"
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +02005#include "ui/console.h"
6#include "ui/egl-helpers.h"
7#include "ui/egl-context.h"
Gerd Hoffmanna3517912017-10-10 15:54:53 +02008#include "ui/shader.h"
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +02009
10typedef struct egl_dpy {
11 DisplayChangeListener dcl;
12 DisplaySurface *ds;
Gerd Hoffmanna3517912017-10-10 15:54:53 +020013 QemuGLShader *gls;
Gerd Hoffmannd8dc67e2017-06-14 10:41:47 +020014 egl_fb guest_fb;
Gerd Hoffmanna3517912017-10-10 15:54:53 +020015 egl_fb cursor_fb;
Gerd Hoffmannd8dc67e2017-06-14 10:41:47 +020016 egl_fb blit_fb;
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +020017 bool y_0_top;
Gerd Hoffmanna3517912017-10-10 15:54:53 +020018 uint32_t pos_x;
19 uint32_t pos_y;
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +020020} egl_dpy;
21
Gerd Hoffmannd8dc67e2017-06-14 10:41:47 +020022/* ------------------------------------------------------------------ */
23
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +020024static void egl_refresh(DisplayChangeListener *dcl)
25{
26 graphic_hw_update(dcl->con);
27}
28
29static void egl_gfx_update(DisplayChangeListener *dcl,
30 int x, int y, int w, int h)
31{
32}
33
34static void egl_gfx_switch(DisplayChangeListener *dcl,
35 struct DisplaySurface *new_surface)
36{
37 egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
38
39 edpy->ds = new_surface;
40}
41
Marc-André Lureau5e79d512021-10-09 23:48:46 +040042static QEMUGLContext egl_create_context(DisplayGLCtx *dgc,
Gerd Hoffmann952e5d52018-11-29 13:35:02 +010043 QEMUGLParams *params)
44{
45 eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
46 qemu_egl_rn_ctx);
Marc-André Lureau5e79d512021-10-09 23:48:46 +040047 return qemu_egl_create_context(dgc, params);
Gerd Hoffmann952e5d52018-11-29 13:35:02 +010048}
49
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +020050static void egl_scanout_disable(DisplayChangeListener *dcl)
51{
52 egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
53
Gerd Hoffmannd8dc67e2017-06-14 10:41:47 +020054 egl_fb_destroy(&edpy->guest_fb);
55 egl_fb_destroy(&edpy->blit_fb);
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +020056}
57
58static void egl_scanout_texture(DisplayChangeListener *dcl,
59 uint32_t backing_id,
60 bool backing_y_0_top,
61 uint32_t backing_width,
62 uint32_t backing_height,
63 uint32_t x, uint32_t y,
Marc-André Lureaubf41ab62023-06-06 15:56:56 +040064 uint32_t w, uint32_t h,
65 void *d3d_tex2d)
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +020066{
67 egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
68
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +020069 edpy->y_0_top = backing_y_0_top;
70
71 /* source framebuffer */
Gerd Hoffmann74083f92017-09-27 13:50:31 +020072 egl_fb_setup_for_tex(&edpy->guest_fb,
73 backing_width, backing_height, backing_id, false);
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +020074
75 /* dest framebuffer */
Gerd Hoffmannd8dc67e2017-06-14 10:41:47 +020076 if (edpy->blit_fb.width != backing_width ||
77 edpy->blit_fb.height != backing_height) {
78 egl_fb_destroy(&edpy->blit_fb);
Gerd Hoffmann74083f92017-09-27 13:50:31 +020079 egl_fb_setup_new_tex(&edpy->blit_fb, backing_width, backing_height);
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +020080 }
81}
82
Marc-André Lureau39324b42023-06-06 15:56:49 +040083#ifdef CONFIG_GBM
84
Gerd Hoffmanna3517912017-10-10 15:54:53 +020085static void egl_scanout_dmabuf(DisplayChangeListener *dcl,
86 QemuDmaBuf *dmabuf)
87{
Dongwon Kim6779a302024-05-08 10:54:00 -070088 uint32_t width, height, texture;
89
Gerd Hoffmanna3517912017-10-10 15:54:53 +020090 egl_dmabuf_import_texture(dmabuf);
Dongwon Kim6779a302024-05-08 10:54:00 -070091 texture = qemu_dmabuf_get_texture(dmabuf);
92 if (!texture) {
Gerd Hoffmanna3517912017-10-10 15:54:53 +020093 return;
94 }
95
Dongwon Kim6779a302024-05-08 10:54:00 -070096 width = qemu_dmabuf_get_width(dmabuf);
97 height = qemu_dmabuf_get_height(dmabuf);
98
99 egl_scanout_texture(dcl, texture, false, width, height, 0, 0,
100 width, height, NULL);
Gerd Hoffmanna3517912017-10-10 15:54:53 +0200101}
102
103static void egl_cursor_dmabuf(DisplayChangeListener *dcl,
Gerd Hoffmann6e1f2cb2018-02-20 12:04:31 +0100104 QemuDmaBuf *dmabuf, bool have_hot,
105 uint32_t hot_x, uint32_t hot_y)
Gerd Hoffmanna3517912017-10-10 15:54:53 +0200106{
Dongwon Kim6779a302024-05-08 10:54:00 -0700107 uint32_t width, height, texture;
Gerd Hoffmanna3517912017-10-10 15:54:53 +0200108 egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
109
Gerd Hoffmannb0916922018-02-20 12:04:32 +0100110 if (dmabuf) {
111 egl_dmabuf_import_texture(dmabuf);
Dongwon Kim6779a302024-05-08 10:54:00 -0700112 texture = qemu_dmabuf_get_texture(dmabuf);
113 if (!texture) {
Gerd Hoffmannb0916922018-02-20 12:04:32 +0100114 return;
115 }
Dongwon Kim6779a302024-05-08 10:54:00 -0700116
117 width = qemu_dmabuf_get_width(dmabuf);
118 height = qemu_dmabuf_get_height(dmabuf);
119 egl_fb_setup_for_tex(&edpy->cursor_fb, width, height, texture, false);
Gerd Hoffmannb0916922018-02-20 12:04:32 +0100120 } else {
121 egl_fb_destroy(&edpy->cursor_fb);
Gerd Hoffmanna3517912017-10-10 15:54:53 +0200122 }
Gerd Hoffmanna3517912017-10-10 15:54:53 +0200123}
124
Marc-André Lureau39324b42023-06-06 15:56:49 +0400125static void egl_release_dmabuf(DisplayChangeListener *dcl,
126 QemuDmaBuf *dmabuf)
127{
128 egl_dmabuf_release_texture(dmabuf);
129}
130
131#endif
132
Gerd Hoffmann6e1f2cb2018-02-20 12:04:31 +0100133static void egl_cursor_position(DisplayChangeListener *dcl,
134 uint32_t pos_x, uint32_t pos_y)
135{
136 egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
137
138 edpy->pos_x = pos_x;
139 edpy->pos_y = pos_y;
140}
141
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +0200142static void egl_scanout_flush(DisplayChangeListener *dcl,
143 uint32_t x, uint32_t y,
144 uint32_t w, uint32_t h)
145{
146 egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +0200147
Gerd Hoffmannd8dc67e2017-06-14 10:41:47 +0200148 if (!edpy->guest_fb.texture || !edpy->ds) {
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +0200149 return;
150 }
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +0200151 assert(surface_format(edpy->ds) == PIXMAN_x8r8g8b8);
152
Gerd Hoffmanna3517912017-10-10 15:54:53 +0200153 if (edpy->cursor_fb.texture) {
154 /* have cursor -> render using textures */
155 egl_texture_blit(edpy->gls, &edpy->blit_fb, &edpy->guest_fb,
156 !edpy->y_0_top);
157 egl_texture_blend(edpy->gls, &edpy->blit_fb, &edpy->cursor_fb,
Chen Zhang051a0cd2019-01-25 15:47:23 +0800158 !edpy->y_0_top, edpy->pos_x, edpy->pos_y,
159 1.0, 1.0);
Gerd Hoffmanna3517912017-10-10 15:54:53 +0200160 } else {
161 /* no cursor -> use simple framebuffer blit */
162 egl_fb_blit(&edpy->blit_fb, &edpy->guest_fb, edpy->y_0_top);
163 }
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +0200164
Gerd Hoffmannd2329232019-09-09 09:39:11 +0200165 egl_fb_read(edpy->ds, &edpy->blit_fb);
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +0200166 dpy_gfx_update(edpy->dcl.con, x, y, w, h);
167}
168
169static const DisplayChangeListenerOps egl_ops = {
170 .dpy_name = "egl-headless",
171 .dpy_refresh = egl_refresh,
172 .dpy_gfx_update = egl_gfx_update,
173 .dpy_gfx_switch = egl_gfx_switch,
174
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +0200175 .dpy_gl_scanout_disable = egl_scanout_disable,
176 .dpy_gl_scanout_texture = egl_scanout_texture,
Marc-André Lureau39324b42023-06-06 15:56:49 +0400177#ifdef CONFIG_GBM
Gerd Hoffmanna3517912017-10-10 15:54:53 +0200178 .dpy_gl_scanout_dmabuf = egl_scanout_dmabuf,
179 .dpy_gl_cursor_dmabuf = egl_cursor_dmabuf,
180 .dpy_gl_release_dmabuf = egl_release_dmabuf,
Marc-André Lureau39324b42023-06-06 15:56:49 +0400181#endif
182 .dpy_gl_cursor_position = egl_cursor_position,
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +0200183 .dpy_gl_update = egl_scanout_flush,
184};
185
Marc-André Lureaua62c4a12022-02-16 19:33:37 +0400186static bool
187egl_is_compatible_dcl(DisplayGLCtx *dgc,
188 DisplayChangeListener *dcl)
189{
Marc-André Lureaucd19c252022-02-16 19:42:40 +0400190 if (!dcl->ops->dpy_gl_update) {
191 /*
192 * egl-headless is compatible with all 2d listeners, as it blits the GL
193 * updates on the 2d console surface.
194 */
195 return true;
196 }
197
Marc-André Lureaua62c4a12022-02-16 19:33:37 +0400198 return dcl->ops == &egl_ops;
199}
200
Marc-André Lureau5e79d512021-10-09 23:48:46 +0400201static const DisplayGLCtxOps eglctx_ops = {
Marc-André Lureaua62c4a12022-02-16 19:33:37 +0400202 .dpy_gl_ctx_is_compatible_dcl = egl_is_compatible_dcl,
Marc-André Lureau5e79d512021-10-09 23:48:46 +0400203 .dpy_gl_ctx_create = egl_create_context,
204 .dpy_gl_ctx_destroy = qemu_egl_destroy_context,
205 .dpy_gl_ctx_make_current = qemu_egl_make_context_current,
206};
207
Gerd Hoffmann16ab0a72018-03-01 11:05:39 +0100208static void early_egl_headless_init(DisplayOptions *opts)
209{
Markus Armbruster154fd4d2024-09-04 13:18:25 +0200210 DisplayGLMode mode = DISPLAY_GL_MODE_ON;
Marc-André Lureau0e1be592023-02-14 16:11:24 +0400211
212 if (opts->has_gl) {
213 mode = opts->gl;
214 }
215
216 egl_init(opts->u.egl_headless.rendernode, mode, &error_fatal);
Gerd Hoffmann16ab0a72018-03-01 11:05:39 +0100217}
218
219static void egl_headless_init(DisplayState *ds, DisplayOptions *opts)
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +0200220{
221 QemuConsole *con;
222 egl_dpy *edpy;
223 int idx;
224
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +0200225 for (idx = 0;; idx++) {
Marc-André Lureau5e79d512021-10-09 23:48:46 +0400226 DisplayGLCtx *ctx;
227
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +0200228 con = qemu_console_lookup_by_index(idx);
229 if (!con || !qemu_console_is_graphic(con)) {
230 break;
231 }
232
233 edpy = g_new0(egl_dpy, 1);
234 edpy->dcl.con = con;
235 edpy->dcl.ops = &egl_ops;
Gerd Hoffmanna3517912017-10-10 15:54:53 +0200236 edpy->gls = qemu_gl_init_shader();
Marc-André Lureau5e79d512021-10-09 23:48:46 +0400237 ctx = g_new0(DisplayGLCtx, 1);
238 ctx->ops = &eglctx_ops;
239 qemu_console_set_display_gl_ctx(con, ctx);
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +0200240 register_displaychangelistener(&edpy->dcl);
241 }
242}
Gerd Hoffmann16ab0a72018-03-01 11:05:39 +0100243
244static QemuDisplay qemu_display_egl = {
245 .type = DISPLAY_TYPE_EGL_HEADLESS,
246 .early_init = early_egl_headless_init,
247 .init = egl_headless_init,
248};
249
250static void register_egl(void)
251{
252 qemu_display_register(&qemu_display_egl);
253}
254
255type_init(register_egl);
Gerd Hoffmannb36ae1c2021-06-24 12:38:13 +0200256
Gerd Hoffmannb36ae1c2021-06-24 12:38:13 +0200257module_dep("ui-opengl");