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"
9 typedef struct egl_dpy
{
10 DisplayChangeListener dcl
;
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 QEMUGLContext
egl_create_context(DisplayChangeListener
*dcl
,
44 eglMakeCurrent(qemu_egl_display
, EGL_NO_SURFACE
, EGL_NO_SURFACE
,
46 return qemu_egl_create_context(dcl
, params
);
49 static void egl_scanout_disable(DisplayChangeListener
*dcl
)
51 egl_dpy
*edpy
= container_of(dcl
, egl_dpy
, dcl
);
53 egl_fb_destroy(&edpy
->guest_fb
);
54 egl_fb_destroy(&edpy
->blit_fb
);
57 static void egl_scanout_texture(DisplayChangeListener
*dcl
,
60 uint32_t backing_width
,
61 uint32_t backing_height
,
62 uint32_t x
, uint32_t y
,
63 uint32_t w
, uint32_t h
)
65 egl_dpy
*edpy
= container_of(dcl
, egl_dpy
, dcl
);
67 edpy
->y_0_top
= backing_y_0_top
;
69 /* source framebuffer */
70 egl_fb_setup_for_tex(&edpy
->guest_fb
,
71 backing_width
, backing_height
, backing_id
, false);
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
);
81 static void egl_scanout_dmabuf(DisplayChangeListener
*dcl
,
84 egl_dmabuf_import_texture(dmabuf
);
85 if (!dmabuf
->texture
) {
89 egl_scanout_texture(dcl
, dmabuf
->texture
,
90 false, dmabuf
->width
, dmabuf
->height
,
91 0, 0, dmabuf
->width
, dmabuf
->height
);
94 static void egl_cursor_dmabuf(DisplayChangeListener
*dcl
,
95 QemuDmaBuf
*dmabuf
, bool have_hot
,
96 uint32_t hot_x
, uint32_t hot_y
)
98 egl_dpy
*edpy
= container_of(dcl
, egl_dpy
, dcl
);
101 egl_dmabuf_import_texture(dmabuf
);
102 if (!dmabuf
->texture
) {
105 egl_fb_setup_for_tex(&edpy
->cursor_fb
, dmabuf
->width
, dmabuf
->height
,
106 dmabuf
->texture
, false);
108 egl_fb_destroy(&edpy
->cursor_fb
);
112 static void egl_cursor_position(DisplayChangeListener
*dcl
,
113 uint32_t pos_x
, uint32_t pos_y
)
115 egl_dpy
*edpy
= container_of(dcl
, egl_dpy
, dcl
);
121 static void egl_release_dmabuf(DisplayChangeListener
*dcl
,
124 egl_dmabuf_release_texture(dmabuf
);
127 static void egl_scanout_flush(DisplayChangeListener
*dcl
,
128 uint32_t x
, uint32_t y
,
129 uint32_t w
, uint32_t h
)
131 egl_dpy
*edpy
= container_of(dcl
, egl_dpy
, dcl
);
133 if (!edpy
->guest_fb
.texture
|| !edpy
->ds
) {
136 assert(surface_width(edpy
->ds
) == edpy
->guest_fb
.width
);
137 assert(surface_height(edpy
->ds
) == edpy
->guest_fb
.height
);
138 assert(surface_format(edpy
->ds
) == PIXMAN_x8r8g8b8
);
140 if (edpy
->cursor_fb
.texture
) {
141 /* have cursor -> render using textures */
142 egl_texture_blit(edpy
->gls
, &edpy
->blit_fb
, &edpy
->guest_fb
,
144 egl_texture_blend(edpy
->gls
, &edpy
->blit_fb
, &edpy
->cursor_fb
,
145 !edpy
->y_0_top
, edpy
->pos_x
, edpy
->pos_y
);
147 /* no cursor -> use simple framebuffer blit */
148 egl_fb_blit(&edpy
->blit_fb
, &edpy
->guest_fb
, edpy
->y_0_top
);
151 egl_fb_read(surface_data(edpy
->ds
), &edpy
->blit_fb
);
152 dpy_gfx_update(edpy
->dcl
.con
, x
, y
, w
, h
);
155 static const DisplayChangeListenerOps egl_ops
= {
156 .dpy_name
= "egl-headless",
157 .dpy_refresh
= egl_refresh
,
158 .dpy_gfx_update
= egl_gfx_update
,
159 .dpy_gfx_switch
= egl_gfx_switch
,
161 .dpy_gl_ctx_create
= egl_create_context
,
162 .dpy_gl_ctx_destroy
= qemu_egl_destroy_context
,
163 .dpy_gl_ctx_make_current
= qemu_egl_make_context_current
,
164 .dpy_gl_ctx_get_current
= qemu_egl_get_current_context
,
166 .dpy_gl_scanout_disable
= egl_scanout_disable
,
167 .dpy_gl_scanout_texture
= egl_scanout_texture
,
168 .dpy_gl_scanout_dmabuf
= egl_scanout_dmabuf
,
169 .dpy_gl_cursor_dmabuf
= egl_cursor_dmabuf
,
170 .dpy_gl_cursor_position
= egl_cursor_position
,
171 .dpy_gl_release_dmabuf
= egl_release_dmabuf
,
172 .dpy_gl_update
= egl_scanout_flush
,
175 static void early_egl_headless_init(DisplayOptions
*opts
)
180 static void egl_headless_init(DisplayState
*ds
, DisplayOptions
*opts
)
182 DisplayGLMode mode
= opts
->has_gl
? opts
->gl
: DISPLAYGL_MODE_ON
;
187 if (egl_rendernode_init(opts
->u
.egl_headless
.rendernode
, mode
) < 0) {
188 error_report("egl: render node init failed");
192 for (idx
= 0;; idx
++) {
193 con
= qemu_console_lookup_by_index(idx
);
194 if (!con
|| !qemu_console_is_graphic(con
)) {
198 edpy
= g_new0(egl_dpy
, 1);
200 edpy
->dcl
.ops
= &egl_ops
;
201 edpy
->gls
= qemu_gl_init_shader();
202 register_displaychangelistener(&edpy
->dcl
);
206 static QemuDisplay qemu_display_egl
= {
207 .type
= DISPLAY_TYPE_EGL_HEADLESS
,
208 .early_init
= early_egl_headless_init
,
209 .init
= egl_headless_init
,
212 static void register_egl(void)
214 qemu_display_register(&qemu_display_egl
);
217 type_init(register_egl
);