blob: 7a30fd9777654931c55e0b66bbdfa7c98480831d [file] [log] [blame]
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +02001#include "qemu/osdep.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +02002#include "qemu/module.h"
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +02003#include "sysemu/sysemu.h"
4#include "ui/console.h"
5#include "ui/egl-helpers.h"
6#include "ui/egl-context.h"
Gerd Hoffmanna3517912017-10-10 15:54:53 +02007#include "ui/shader.h"
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +02008
9typedef struct egl_dpy {
10 DisplayChangeListener dcl;
11 DisplaySurface *ds;
Gerd Hoffmanna3517912017-10-10 15:54:53 +020012 QemuGLShader *gls;
Gerd Hoffmannd8dc67e2017-06-14 10:41:47 +020013 egl_fb guest_fb;
Gerd Hoffmanna3517912017-10-10 15:54:53 +020014 egl_fb cursor_fb;
Gerd Hoffmannd8dc67e2017-06-14 10:41:47 +020015 egl_fb blit_fb;
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +020016 bool y_0_top;
Gerd Hoffmanna3517912017-10-10 15:54:53 +020017 uint32_t pos_x;
18 uint32_t pos_y;
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +020019} egl_dpy;
20
Gerd Hoffmannd8dc67e2017-06-14 10:41:47 +020021/* ------------------------------------------------------------------ */
22
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +020023static void egl_refresh(DisplayChangeListener *dcl)
24{
25 graphic_hw_update(dcl->con);
26}
27
28static void egl_gfx_update(DisplayChangeListener *dcl,
29 int x, int y, int w, int h)
30{
31}
32
33static void egl_gfx_switch(DisplayChangeListener *dcl,
34 struct DisplaySurface *new_surface)
35{
36 egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
37
38 edpy->ds = new_surface;
39}
40
Marc-André Lureau5e79d512021-10-09 23:48:46 +040041static QEMUGLContext egl_create_context(DisplayGLCtx *dgc,
Gerd Hoffmann952e5d52018-11-29 13:35:02 +010042 QEMUGLParams *params)
43{
44 eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
45 qemu_egl_rn_ctx);
Marc-André Lureau5e79d512021-10-09 23:48:46 +040046 return qemu_egl_create_context(dgc, params);
Gerd Hoffmann952e5d52018-11-29 13:35:02 +010047}
48
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +020049static void egl_scanout_disable(DisplayChangeListener *dcl)
50{
51 egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
52
Gerd Hoffmannd8dc67e2017-06-14 10:41:47 +020053 egl_fb_destroy(&edpy->guest_fb);
54 egl_fb_destroy(&edpy->blit_fb);
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +020055}
56
57static void egl_scanout_texture(DisplayChangeListener *dcl,
58 uint32_t backing_id,
59 bool backing_y_0_top,
60 uint32_t backing_width,
61 uint32_t backing_height,
62 uint32_t x, uint32_t y,
63 uint32_t w, uint32_t h)
64{
65 egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
66
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +020067 edpy->y_0_top = backing_y_0_top;
68
69 /* source framebuffer */
Gerd Hoffmann74083f92017-09-27 13:50:31 +020070 egl_fb_setup_for_tex(&edpy->guest_fb,
71 backing_width, backing_height, backing_id, false);
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +020072
73 /* dest framebuffer */
Gerd Hoffmannd8dc67e2017-06-14 10:41:47 +020074 if (edpy->blit_fb.width != backing_width ||
75 edpy->blit_fb.height != backing_height) {
76 egl_fb_destroy(&edpy->blit_fb);
Gerd Hoffmann74083f92017-09-27 13:50:31 +020077 egl_fb_setup_new_tex(&edpy->blit_fb, backing_width, backing_height);
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +020078 }
79}
80
Gerd Hoffmanna3517912017-10-10 15:54:53 +020081static void egl_scanout_dmabuf(DisplayChangeListener *dcl,
82 QemuDmaBuf *dmabuf)
83{
84 egl_dmabuf_import_texture(dmabuf);
85 if (!dmabuf->texture) {
86 return;
87 }
88
89 egl_scanout_texture(dcl, dmabuf->texture,
90 false, dmabuf->width, dmabuf->height,
91 0, 0, dmabuf->width, dmabuf->height);
92}
93
94static void egl_cursor_dmabuf(DisplayChangeListener *dcl,
Gerd Hoffmann6e1f2cb2018-02-20 12:04:31 +010095 QemuDmaBuf *dmabuf, bool have_hot,
96 uint32_t hot_x, uint32_t hot_y)
Gerd Hoffmanna3517912017-10-10 15:54:53 +020097{
98 egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
99
Gerd Hoffmannb0916922018-02-20 12:04:32 +0100100 if (dmabuf) {
101 egl_dmabuf_import_texture(dmabuf);
102 if (!dmabuf->texture) {
103 return;
104 }
105 egl_fb_setup_for_tex(&edpy->cursor_fb, dmabuf->width, dmabuf->height,
106 dmabuf->texture, false);
107 } else {
108 egl_fb_destroy(&edpy->cursor_fb);
Gerd Hoffmanna3517912017-10-10 15:54:53 +0200109 }
Gerd Hoffmanna3517912017-10-10 15:54:53 +0200110}
111
Gerd Hoffmann6e1f2cb2018-02-20 12:04:31 +0100112static void egl_cursor_position(DisplayChangeListener *dcl,
113 uint32_t pos_x, uint32_t pos_y)
114{
115 egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
116
117 edpy->pos_x = pos_x;
118 edpy->pos_y = pos_y;
119}
120
Gerd Hoffmanna3517912017-10-10 15:54:53 +0200121static void egl_release_dmabuf(DisplayChangeListener *dcl,
122 QemuDmaBuf *dmabuf)
123{
124 egl_dmabuf_release_texture(dmabuf);
125}
126
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +0200127static void egl_scanout_flush(DisplayChangeListener *dcl,
128 uint32_t x, uint32_t y,
129 uint32_t w, uint32_t h)
130{
131 egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +0200132
Gerd Hoffmannd8dc67e2017-06-14 10:41:47 +0200133 if (!edpy->guest_fb.texture || !edpy->ds) {
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +0200134 return;
135 }
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +0200136 assert(surface_format(edpy->ds) == PIXMAN_x8r8g8b8);
137
Gerd Hoffmanna3517912017-10-10 15:54:53 +0200138 if (edpy->cursor_fb.texture) {
139 /* have cursor -> render using textures */
140 egl_texture_blit(edpy->gls, &edpy->blit_fb, &edpy->guest_fb,
141 !edpy->y_0_top);
142 egl_texture_blend(edpy->gls, &edpy->blit_fb, &edpy->cursor_fb,
Chen Zhang051a0cd2019-01-25 15:47:23 +0800143 !edpy->y_0_top, edpy->pos_x, edpy->pos_y,
144 1.0, 1.0);
Gerd Hoffmanna3517912017-10-10 15:54:53 +0200145 } else {
146 /* no cursor -> use simple framebuffer blit */
147 egl_fb_blit(&edpy->blit_fb, &edpy->guest_fb, edpy->y_0_top);
148 }
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +0200149
Gerd Hoffmannd2329232019-09-09 09:39:11 +0200150 egl_fb_read(edpy->ds, &edpy->blit_fb);
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +0200151 dpy_gfx_update(edpy->dcl.con, x, y, w, h);
152}
153
154static const DisplayChangeListenerOps egl_ops = {
155 .dpy_name = "egl-headless",
156 .dpy_refresh = egl_refresh,
157 .dpy_gfx_update = egl_gfx_update,
158 .dpy_gfx_switch = egl_gfx_switch,
159
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +0200160 .dpy_gl_scanout_disable = egl_scanout_disable,
161 .dpy_gl_scanout_texture = egl_scanout_texture,
Gerd Hoffmanna3517912017-10-10 15:54:53 +0200162 .dpy_gl_scanout_dmabuf = egl_scanout_dmabuf,
163 .dpy_gl_cursor_dmabuf = egl_cursor_dmabuf,
Gerd Hoffmann6e1f2cb2018-02-20 12:04:31 +0100164 .dpy_gl_cursor_position = egl_cursor_position,
Gerd Hoffmanna3517912017-10-10 15:54:53 +0200165 .dpy_gl_release_dmabuf = egl_release_dmabuf,
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +0200166 .dpy_gl_update = egl_scanout_flush,
167};
168
Marc-André Lureaua62c4a12022-02-16 19:33:37 +0400169static bool
170egl_is_compatible_dcl(DisplayGLCtx *dgc,
171 DisplayChangeListener *dcl)
172{
Marc-André Lureaucd19c252022-02-16 19:42:40 +0400173 if (!dcl->ops->dpy_gl_update) {
174 /*
175 * egl-headless is compatible with all 2d listeners, as it blits the GL
176 * updates on the 2d console surface.
177 */
178 return true;
179 }
180
Marc-André Lureaua62c4a12022-02-16 19:33:37 +0400181 return dcl->ops == &egl_ops;
182}
183
Marc-André Lureau5e79d512021-10-09 23:48:46 +0400184static const DisplayGLCtxOps eglctx_ops = {
Marc-André Lureaua62c4a12022-02-16 19:33:37 +0400185 .dpy_gl_ctx_is_compatible_dcl = egl_is_compatible_dcl,
Marc-André Lureau5e79d512021-10-09 23:48:46 +0400186 .dpy_gl_ctx_create = egl_create_context,
187 .dpy_gl_ctx_destroy = qemu_egl_destroy_context,
188 .dpy_gl_ctx_make_current = qemu_egl_make_context_current,
189};
190
Gerd Hoffmann16ab0a72018-03-01 11:05:39 +0100191static void early_egl_headless_init(DisplayOptions *opts)
192{
193 display_opengl = 1;
194}
195
196static void egl_headless_init(DisplayState *ds, DisplayOptions *opts)
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +0200197{
Gerd Hoffmann54d208f2018-06-18 13:21:41 +0200198 DisplayGLMode mode = opts->has_gl ? opts->gl : DISPLAYGL_MODE_ON;
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +0200199 QemuConsole *con;
200 egl_dpy *edpy;
201 int idx;
202
Erik Skultety91e61942018-11-16 11:14:43 +0100203 if (egl_rendernode_init(opts->u.egl_headless.rendernode, mode) < 0) {
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +0200204 error_report("egl: render node init failed");
205 exit(1);
206 }
207
208 for (idx = 0;; idx++) {
Marc-André Lureau5e79d512021-10-09 23:48:46 +0400209 DisplayGLCtx *ctx;
210
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +0200211 con = qemu_console_lookup_by_index(idx);
212 if (!con || !qemu_console_is_graphic(con)) {
213 break;
214 }
215
216 edpy = g_new0(egl_dpy, 1);
217 edpy->dcl.con = con;
218 edpy->dcl.ops = &egl_ops;
Gerd Hoffmanna3517912017-10-10 15:54:53 +0200219 edpy->gls = qemu_gl_init_shader();
Marc-André Lureau5e79d512021-10-09 23:48:46 +0400220 ctx = g_new0(DisplayGLCtx, 1);
221 ctx->ops = &eglctx_ops;
222 qemu_console_set_display_gl_ctx(con, ctx);
Gerd Hoffmannbb1599b2017-05-05 12:41:01 +0200223 register_displaychangelistener(&edpy->dcl);
224 }
225}
Gerd Hoffmann16ab0a72018-03-01 11:05:39 +0100226
227static QemuDisplay qemu_display_egl = {
228 .type = DISPLAY_TYPE_EGL_HEADLESS,
229 .early_init = early_egl_headless_init,
230 .init = egl_headless_init,
231};
232
233static void register_egl(void)
234{
235 qemu_display_register(&qemu_display_egl);
236}
237
238type_init(register_egl);
Gerd Hoffmannb36ae1c2021-06-24 12:38:13 +0200239
Gerd Hoffmannb36ae1c2021-06-24 12:38:13 +0200240module_dep("ui-opengl");