]> git.proxmox.com Git - mirror_qemu.git/blob - ui/egl-headless.c
e4177206f2dab6fb3c3c000a44be54788da0127e
[mirror_qemu.git] / ui / egl-headless.c
1 #include "qemu/osdep.h"
2 #include "qemu/error-report.h"
3 #include "qemu/module.h"
4 #include "qapi/error.h"
5 #include "ui/console.h"
6 #include "ui/egl-helpers.h"
7 #include "ui/egl-context.h"
8 #include "ui/shader.h"
9
10 typedef struct egl_dpy {
11 DisplayChangeListener dcl;
12 DisplaySurface *ds;
13 QemuGLShader *gls;
14 egl_fb guest_fb;
15 egl_fb cursor_fb;
16 egl_fb blit_fb;
17 bool y_0_top;
18 uint32_t pos_x;
19 uint32_t pos_y;
20 } egl_dpy;
21
22 /* ------------------------------------------------------------------ */
23
24 static void egl_refresh(DisplayChangeListener *dcl)
25 {
26 graphic_hw_update(dcl->con);
27 }
28
29 static void egl_gfx_update(DisplayChangeListener *dcl,
30 int x, int y, int w, int h)
31 {
32 }
33
34 static 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
42 static QEMUGLContext egl_create_context(DisplayGLCtx *dgc,
43 QEMUGLParams *params)
44 {
45 eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
46 qemu_egl_rn_ctx);
47 return qemu_egl_create_context(dgc, params);
48 }
49
50 static void egl_scanout_disable(DisplayChangeListener *dcl)
51 {
52 egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
53
54 egl_fb_destroy(&edpy->guest_fb);
55 egl_fb_destroy(&edpy->blit_fb);
56 }
57
58 static 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,
64 uint32_t w, uint32_t h)
65 {
66 egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
67
68 edpy->y_0_top = backing_y_0_top;
69
70 /* source framebuffer */
71 egl_fb_setup_for_tex(&edpy->guest_fb,
72 backing_width, backing_height, backing_id, false);
73
74 /* dest framebuffer */
75 if (edpy->blit_fb.width != backing_width ||
76 edpy->blit_fb.height != backing_height) {
77 egl_fb_destroy(&edpy->blit_fb);
78 egl_fb_setup_new_tex(&edpy->blit_fb, backing_width, backing_height);
79 }
80 }
81
82 #ifdef CONFIG_GBM
83
84 static void egl_scanout_dmabuf(DisplayChangeListener *dcl,
85 QemuDmaBuf *dmabuf)
86 {
87 egl_dmabuf_import_texture(dmabuf);
88 if (!dmabuf->texture) {
89 return;
90 }
91
92 egl_scanout_texture(dcl, dmabuf->texture,
93 false, dmabuf->width, dmabuf->height,
94 0, 0, dmabuf->width, dmabuf->height);
95 }
96
97 static void egl_cursor_dmabuf(DisplayChangeListener *dcl,
98 QemuDmaBuf *dmabuf, bool have_hot,
99 uint32_t hot_x, uint32_t hot_y)
100 {
101 egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
102
103 if (dmabuf) {
104 egl_dmabuf_import_texture(dmabuf);
105 if (!dmabuf->texture) {
106 return;
107 }
108 egl_fb_setup_for_tex(&edpy->cursor_fb, dmabuf->width, dmabuf->height,
109 dmabuf->texture, false);
110 } else {
111 egl_fb_destroy(&edpy->cursor_fb);
112 }
113 }
114
115 static void egl_release_dmabuf(DisplayChangeListener *dcl,
116 QemuDmaBuf *dmabuf)
117 {
118 egl_dmabuf_release_texture(dmabuf);
119 }
120
121 #endif
122
123 static void egl_cursor_position(DisplayChangeListener *dcl,
124 uint32_t pos_x, uint32_t pos_y)
125 {
126 egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
127
128 edpy->pos_x = pos_x;
129 edpy->pos_y = pos_y;
130 }
131
132 static void egl_scanout_flush(DisplayChangeListener *dcl,
133 uint32_t x, uint32_t y,
134 uint32_t w, uint32_t h)
135 {
136 egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
137
138 if (!edpy->guest_fb.texture || !edpy->ds) {
139 return;
140 }
141 assert(surface_format(edpy->ds) == PIXMAN_x8r8g8b8);
142
143 if (edpy->cursor_fb.texture) {
144 /* have cursor -> render using textures */
145 egl_texture_blit(edpy->gls, &edpy->blit_fb, &edpy->guest_fb,
146 !edpy->y_0_top);
147 egl_texture_blend(edpy->gls, &edpy->blit_fb, &edpy->cursor_fb,
148 !edpy->y_0_top, edpy->pos_x, edpy->pos_y,
149 1.0, 1.0);
150 } else {
151 /* no cursor -> use simple framebuffer blit */
152 egl_fb_blit(&edpy->blit_fb, &edpy->guest_fb, edpy->y_0_top);
153 }
154
155 egl_fb_read(edpy->ds, &edpy->blit_fb);
156 dpy_gfx_update(edpy->dcl.con, x, y, w, h);
157 }
158
159 static const DisplayChangeListenerOps egl_ops = {
160 .dpy_name = "egl-headless",
161 .dpy_refresh = egl_refresh,
162 .dpy_gfx_update = egl_gfx_update,
163 .dpy_gfx_switch = egl_gfx_switch,
164
165 .dpy_gl_scanout_disable = egl_scanout_disable,
166 .dpy_gl_scanout_texture = egl_scanout_texture,
167 #ifdef CONFIG_GBM
168 .dpy_gl_scanout_dmabuf = egl_scanout_dmabuf,
169 .dpy_gl_cursor_dmabuf = egl_cursor_dmabuf,
170 .dpy_gl_release_dmabuf = egl_release_dmabuf,
171 #endif
172 .dpy_gl_cursor_position = egl_cursor_position,
173 .dpy_gl_update = egl_scanout_flush,
174 };
175
176 static bool
177 egl_is_compatible_dcl(DisplayGLCtx *dgc,
178 DisplayChangeListener *dcl)
179 {
180 if (!dcl->ops->dpy_gl_update) {
181 /*
182 * egl-headless is compatible with all 2d listeners, as it blits the GL
183 * updates on the 2d console surface.
184 */
185 return true;
186 }
187
188 return dcl->ops == &egl_ops;
189 }
190
191 static const DisplayGLCtxOps eglctx_ops = {
192 .dpy_gl_ctx_is_compatible_dcl = egl_is_compatible_dcl,
193 .dpy_gl_ctx_create = egl_create_context,
194 .dpy_gl_ctx_destroy = qemu_egl_destroy_context,
195 .dpy_gl_ctx_make_current = qemu_egl_make_context_current,
196 };
197
198 static void early_egl_headless_init(DisplayOptions *opts)
199 {
200 DisplayGLMode mode = DISPLAYGL_MODE_ON;
201
202 if (opts->has_gl) {
203 mode = opts->gl;
204 }
205
206 egl_init(opts->u.egl_headless.rendernode, mode, &error_fatal);
207 }
208
209 static void egl_headless_init(DisplayState *ds, DisplayOptions *opts)
210 {
211 QemuConsole *con;
212 egl_dpy *edpy;
213 int idx;
214
215 for (idx = 0;; idx++) {
216 DisplayGLCtx *ctx;
217
218 con = qemu_console_lookup_by_index(idx);
219 if (!con || !qemu_console_is_graphic(con)) {
220 break;
221 }
222
223 edpy = g_new0(egl_dpy, 1);
224 edpy->dcl.con = con;
225 edpy->dcl.ops = &egl_ops;
226 edpy->gls = qemu_gl_init_shader();
227 ctx = g_new0(DisplayGLCtx, 1);
228 ctx->ops = &eglctx_ops;
229 qemu_console_set_display_gl_ctx(con, ctx);
230 register_displaychangelistener(&edpy->dcl);
231 }
232 }
233
234 static QemuDisplay qemu_display_egl = {
235 .type = DISPLAY_TYPE_EGL_HEADLESS,
236 .early_init = early_egl_headless_init,
237 .init = egl_headless_init,
238 };
239
240 static void register_egl(void)
241 {
242 qemu_display_register(&qemu_display_egl);
243 }
244
245 type_init(register_egl);
246
247 module_dep("ui-opengl");