Merge remote-tracking branch 'remotes/xtensa/tags/20180918-xtensa' into staging
[qemu.git] / ui / egl-headless.c
blob42a41310b0ac092f885e4c7054c9a47e423166e4
1 #include "qemu/osdep.h"
2 #include "qemu-common.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"
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;
21 /* ------------------------------------------------------------------ */
23 static void egl_refresh(DisplayChangeListener *dcl)
25 graphic_hw_update(dcl->con);
28 static void egl_gfx_update(DisplayChangeListener *dcl,
29 int x, int y, int w, int h)
33 static void egl_gfx_switch(DisplayChangeListener *dcl,
34 struct DisplaySurface *new_surface)
36 egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
38 edpy->ds = new_surface;
41 static void egl_scanout_disable(DisplayChangeListener *dcl)
43 egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
45 egl_fb_destroy(&edpy->guest_fb);
46 egl_fb_destroy(&edpy->blit_fb);
49 static void egl_scanout_texture(DisplayChangeListener *dcl,
50 uint32_t backing_id,
51 bool backing_y_0_top,
52 uint32_t backing_width,
53 uint32_t backing_height,
54 uint32_t x, uint32_t y,
55 uint32_t w, uint32_t h)
57 egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
59 edpy->y_0_top = backing_y_0_top;
61 /* source framebuffer */
62 egl_fb_setup_for_tex(&edpy->guest_fb,
63 backing_width, backing_height, backing_id, false);
65 /* dest framebuffer */
66 if (edpy->blit_fb.width != backing_width ||
67 edpy->blit_fb.height != backing_height) {
68 egl_fb_destroy(&edpy->blit_fb);
69 egl_fb_setup_new_tex(&edpy->blit_fb, backing_width, backing_height);
73 static void egl_scanout_dmabuf(DisplayChangeListener *dcl,
74 QemuDmaBuf *dmabuf)
76 egl_dmabuf_import_texture(dmabuf);
77 if (!dmabuf->texture) {
78 return;
81 egl_scanout_texture(dcl, dmabuf->texture,
82 false, dmabuf->width, dmabuf->height,
83 0, 0, dmabuf->width, dmabuf->height);
86 static void egl_cursor_dmabuf(DisplayChangeListener *dcl,
87 QemuDmaBuf *dmabuf, bool have_hot,
88 uint32_t hot_x, uint32_t hot_y)
90 egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
92 if (dmabuf) {
93 egl_dmabuf_import_texture(dmabuf);
94 if (!dmabuf->texture) {
95 return;
97 egl_fb_setup_for_tex(&edpy->cursor_fb, dmabuf->width, dmabuf->height,
98 dmabuf->texture, false);
99 } else {
100 egl_fb_destroy(&edpy->cursor_fb);
104 static void egl_cursor_position(DisplayChangeListener *dcl,
105 uint32_t pos_x, uint32_t pos_y)
107 egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
109 edpy->pos_x = pos_x;
110 edpy->pos_y = pos_y;
113 static void egl_release_dmabuf(DisplayChangeListener *dcl,
114 QemuDmaBuf *dmabuf)
116 egl_dmabuf_release_texture(dmabuf);
119 static void egl_scanout_flush(DisplayChangeListener *dcl,
120 uint32_t x, uint32_t y,
121 uint32_t w, uint32_t h)
123 egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
125 if (!edpy->guest_fb.texture || !edpy->ds) {
126 return;
128 assert(surface_width(edpy->ds) == edpy->guest_fb.width);
129 assert(surface_height(edpy->ds) == edpy->guest_fb.height);
130 assert(surface_format(edpy->ds) == PIXMAN_x8r8g8b8);
132 if (edpy->cursor_fb.texture) {
133 /* have cursor -> render using textures */
134 egl_texture_blit(edpy->gls, &edpy->blit_fb, &edpy->guest_fb,
135 !edpy->y_0_top);
136 egl_texture_blend(edpy->gls, &edpy->blit_fb, &edpy->cursor_fb,
137 !edpy->y_0_top, edpy->pos_x, edpy->pos_y);
138 } else {
139 /* no cursor -> use simple framebuffer blit */
140 egl_fb_blit(&edpy->blit_fb, &edpy->guest_fb, edpy->y_0_top);
143 egl_fb_read(surface_data(edpy->ds), &edpy->blit_fb);
144 dpy_gfx_update(edpy->dcl.con, x, y, w, h);
147 static const DisplayChangeListenerOps egl_ops = {
148 .dpy_name = "egl-headless",
149 .dpy_refresh = egl_refresh,
150 .dpy_gfx_update = egl_gfx_update,
151 .dpy_gfx_switch = egl_gfx_switch,
153 .dpy_gl_ctx_create = qemu_egl_create_context,
154 .dpy_gl_ctx_destroy = qemu_egl_destroy_context,
155 .dpy_gl_ctx_make_current = qemu_egl_make_context_current,
156 .dpy_gl_ctx_get_current = qemu_egl_get_current_context,
158 .dpy_gl_scanout_disable = egl_scanout_disable,
159 .dpy_gl_scanout_texture = egl_scanout_texture,
160 .dpy_gl_scanout_dmabuf = egl_scanout_dmabuf,
161 .dpy_gl_cursor_dmabuf = egl_cursor_dmabuf,
162 .dpy_gl_cursor_position = egl_cursor_position,
163 .dpy_gl_release_dmabuf = egl_release_dmabuf,
164 .dpy_gl_update = egl_scanout_flush,
167 static void early_egl_headless_init(DisplayOptions *opts)
169 display_opengl = 1;
172 static void egl_headless_init(DisplayState *ds, DisplayOptions *opts)
174 DisplayGLMode mode = opts->has_gl ? opts->gl : DISPLAYGL_MODE_ON;
175 QemuConsole *con;
176 egl_dpy *edpy;
177 int idx;
179 if (egl_rendernode_init(NULL, mode) < 0) {
180 error_report("egl: render node init failed");
181 exit(1);
184 for (idx = 0;; idx++) {
185 con = qemu_console_lookup_by_index(idx);
186 if (!con || !qemu_console_is_graphic(con)) {
187 break;
190 edpy = g_new0(egl_dpy, 1);
191 edpy->dcl.con = con;
192 edpy->dcl.ops = &egl_ops;
193 edpy->gls = qemu_gl_init_shader();
194 register_displaychangelistener(&edpy->dcl);
198 static QemuDisplay qemu_display_egl = {
199 .type = DISPLAY_TYPE_EGL_HEADLESS,
200 .early_init = early_egl_headless_init,
201 .init = egl_headless_init,
204 static void register_egl(void)
206 qemu_display_register(&qemu_display_egl);
209 type_init(register_egl);