xref: /openbmc/qemu/ui/egl-headless.c (revision 14e996ef)
1 #include "qemu/osdep.h"
2 #include "qemu/module.h"
3 #include "sysemu/sysemu.h"
4 #include "ui/console.h"
5 #include "ui/egl-helpers.h"
6 #include "ui/egl-context.h"
7 #include "ui/shader.h"
8 
9 typedef struct egl_dpy {
10     DisplayChangeListener dcl;
11     DisplaySurface *ds;
12     QemuGLShader *gls;
13     egl_fb guest_fb;
14     egl_fb cursor_fb;
15     egl_fb blit_fb;
16     bool y_0_top;
17     uint32_t pos_x;
18     uint32_t pos_y;
19 } egl_dpy;
20 
21 /* ------------------------------------------------------------------ */
22 
23 static void egl_refresh(DisplayChangeListener *dcl)
24 {
25     graphic_hw_update(dcl->con);
26 }
27 
28 static void egl_gfx_update(DisplayChangeListener *dcl,
29                            int x, int y, int w, int h)
30 {
31 }
32 
33 static 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 
41 static QEMUGLContext egl_create_context(DisplayChangeListener *dcl,
42                                         QEMUGLParams *params)
43 {
44     eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
45                    qemu_egl_rn_ctx);
46     return qemu_egl_create_context(dcl, params);
47 }
48 
49 static void egl_scanout_disable(DisplayChangeListener *dcl)
50 {
51     egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
52 
53     egl_fb_destroy(&edpy->guest_fb);
54     egl_fb_destroy(&edpy->blit_fb);
55 }
56 
57 static 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 
67     edpy->y_0_top = backing_y_0_top;
68 
69     /* source framebuffer */
70     egl_fb_setup_for_tex(&edpy->guest_fb,
71                          backing_width, backing_height, backing_id, false);
72 
73     /* dest framebuffer */
74     if (edpy->blit_fb.width  != backing_width ||
75         edpy->blit_fb.height != backing_height) {
76         egl_fb_destroy(&edpy->blit_fb);
77         egl_fb_setup_new_tex(&edpy->blit_fb, backing_width, backing_height);
78     }
79 }
80 
81 static 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 
94 static void egl_cursor_dmabuf(DisplayChangeListener *dcl,
95                               QemuDmaBuf *dmabuf, bool have_hot,
96                               uint32_t hot_x, uint32_t hot_y)
97 {
98     egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
99 
100     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);
109     }
110 }
111 
112 static 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 
121 static void egl_release_dmabuf(DisplayChangeListener *dcl,
122                                QemuDmaBuf *dmabuf)
123 {
124     egl_dmabuf_release_texture(dmabuf);
125 }
126 
127 static 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);
132 
133     if (!edpy->guest_fb.texture || !edpy->ds) {
134         return;
135     }
136     assert(surface_format(edpy->ds) == PIXMAN_x8r8g8b8);
137 
138     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,
143                           !edpy->y_0_top, edpy->pos_x, edpy->pos_y,
144                           1.0, 1.0);
145     } else {
146         /* no cursor -> use simple framebuffer blit */
147         egl_fb_blit(&edpy->blit_fb, &edpy->guest_fb, edpy->y_0_top);
148     }
149 
150     egl_fb_read(edpy->ds, &edpy->blit_fb);
151     dpy_gfx_update(edpy->dcl.con, x, y, w, h);
152 }
153 
154 static 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 
160     .dpy_gl_ctx_create       = egl_create_context,
161     .dpy_gl_ctx_destroy      = qemu_egl_destroy_context,
162     .dpy_gl_ctx_make_current = qemu_egl_make_context_current,
163 
164     .dpy_gl_scanout_disable  = egl_scanout_disable,
165     .dpy_gl_scanout_texture  = egl_scanout_texture,
166     .dpy_gl_scanout_dmabuf   = egl_scanout_dmabuf,
167     .dpy_gl_cursor_dmabuf    = egl_cursor_dmabuf,
168     .dpy_gl_cursor_position  = egl_cursor_position,
169     .dpy_gl_release_dmabuf   = egl_release_dmabuf,
170     .dpy_gl_update           = egl_scanout_flush,
171 };
172 
173 static void early_egl_headless_init(DisplayOptions *opts)
174 {
175     display_opengl = 1;
176 }
177 
178 static void egl_headless_init(DisplayState *ds, DisplayOptions *opts)
179 {
180     DisplayGLMode mode = opts->has_gl ? opts->gl : DISPLAYGL_MODE_ON;
181     QemuConsole *con;
182     egl_dpy *edpy;
183     int idx;
184 
185     if (egl_rendernode_init(opts->u.egl_headless.rendernode, mode) < 0) {
186         error_report("egl: render node init failed");
187         exit(1);
188     }
189 
190     for (idx = 0;; idx++) {
191         con = qemu_console_lookup_by_index(idx);
192         if (!con || !qemu_console_is_graphic(con)) {
193             break;
194         }
195 
196         edpy = g_new0(egl_dpy, 1);
197         edpy->dcl.con = con;
198         edpy->dcl.ops = &egl_ops;
199         edpy->gls = qemu_gl_init_shader();
200         register_displaychangelistener(&edpy->dcl);
201     }
202 }
203 
204 static QemuDisplay qemu_display_egl = {
205     .type       = DISPLAY_TYPE_EGL_HEADLESS,
206     .early_init = early_egl_headless_init,
207     .init       = egl_headless_init,
208 };
209 
210 static void register_egl(void)
211 {
212     qemu_display_register(&qemu_display_egl);
213 }
214 
215 type_init(register_egl);
216