]>
Commit | Line | Data |
---|---|---|
bb1599b6 GH |
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" | |
a3517917 | 7 | #include "ui/shader.h" |
bb1599b6 GH |
8 | |
9 | typedef struct egl_dpy { | |
10 | DisplayChangeListener dcl; | |
11 | DisplaySurface *ds; | |
a3517917 | 12 | QemuGLShader *gls; |
d8dc67e1 | 13 | egl_fb guest_fb; |
a3517917 | 14 | egl_fb cursor_fb; |
d8dc67e1 | 15 | egl_fb blit_fb; |
bb1599b6 | 16 | bool y_0_top; |
a3517917 GH |
17 | uint32_t pos_x; |
18 | uint32_t pos_y; | |
bb1599b6 GH |
19 | } egl_dpy; |
20 | ||
d8dc67e1 GH |
21 | /* ------------------------------------------------------------------ */ |
22 | ||
bb1599b6 GH |
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 void egl_scanout_disable(DisplayChangeListener *dcl) | |
42 | { | |
43 | egl_dpy *edpy = container_of(dcl, egl_dpy, dcl); | |
44 | ||
d8dc67e1 GH |
45 | egl_fb_destroy(&edpy->guest_fb); |
46 | egl_fb_destroy(&edpy->blit_fb); | |
bb1599b6 GH |
47 | } |
48 | ||
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) | |
56 | { | |
57 | egl_dpy *edpy = container_of(dcl, egl_dpy, dcl); | |
58 | ||
bb1599b6 GH |
59 | edpy->y_0_top = backing_y_0_top; |
60 | ||
61 | /* source framebuffer */ | |
74083f9c GH |
62 | egl_fb_setup_for_tex(&edpy->guest_fb, |
63 | backing_width, backing_height, backing_id, false); | |
bb1599b6 GH |
64 | |
65 | /* dest framebuffer */ | |
d8dc67e1 GH |
66 | if (edpy->blit_fb.width != backing_width || |
67 | edpy->blit_fb.height != backing_height) { | |
68 | egl_fb_destroy(&edpy->blit_fb); | |
74083f9c | 69 | egl_fb_setup_new_tex(&edpy->blit_fb, backing_width, backing_height); |
bb1599b6 GH |
70 | } |
71 | } | |
72 | ||
a3517917 GH |
73 | static void egl_scanout_dmabuf(DisplayChangeListener *dcl, |
74 | QemuDmaBuf *dmabuf) | |
75 | { | |
76 | egl_dmabuf_import_texture(dmabuf); | |
77 | if (!dmabuf->texture) { | |
78 | return; | |
79 | } | |
80 | ||
81 | egl_scanout_texture(dcl, dmabuf->texture, | |
82 | false, dmabuf->width, dmabuf->height, | |
83 | 0, 0, dmabuf->width, dmabuf->height); | |
84 | } | |
85 | ||
86 | static void egl_cursor_dmabuf(DisplayChangeListener *dcl, | |
87 | QemuDmaBuf *dmabuf, | |
88 | uint32_t pos_x, uint32_t pos_y) | |
89 | { | |
90 | egl_dpy *edpy = container_of(dcl, egl_dpy, dcl); | |
91 | ||
92 | edpy->pos_x = pos_x; | |
93 | edpy->pos_y = pos_y; | |
94 | ||
95 | egl_dmabuf_import_texture(dmabuf); | |
96 | if (!dmabuf->texture) { | |
97 | return; | |
98 | } | |
99 | ||
100 | egl_fb_setup_for_tex(&edpy->cursor_fb, dmabuf->width, dmabuf->height, | |
101 | dmabuf->texture, false); | |
102 | } | |
103 | ||
104 | static void egl_release_dmabuf(DisplayChangeListener *dcl, | |
105 | QemuDmaBuf *dmabuf) | |
106 | { | |
107 | egl_dmabuf_release_texture(dmabuf); | |
108 | } | |
109 | ||
bb1599b6 GH |
110 | static void egl_scanout_flush(DisplayChangeListener *dcl, |
111 | uint32_t x, uint32_t y, | |
112 | uint32_t w, uint32_t h) | |
113 | { | |
114 | egl_dpy *edpy = container_of(dcl, egl_dpy, dcl); | |
bb1599b6 | 115 | |
d8dc67e1 | 116 | if (!edpy->guest_fb.texture || !edpy->ds) { |
bb1599b6 GH |
117 | return; |
118 | } | |
d8dc67e1 GH |
119 | assert(surface_width(edpy->ds) == edpy->guest_fb.width); |
120 | assert(surface_height(edpy->ds) == edpy->guest_fb.height); | |
bb1599b6 GH |
121 | assert(surface_format(edpy->ds) == PIXMAN_x8r8g8b8); |
122 | ||
a3517917 GH |
123 | if (edpy->cursor_fb.texture) { |
124 | /* have cursor -> render using textures */ | |
125 | egl_texture_blit(edpy->gls, &edpy->blit_fb, &edpy->guest_fb, | |
126 | !edpy->y_0_top); | |
127 | egl_texture_blend(edpy->gls, &edpy->blit_fb, &edpy->cursor_fb, | |
128 | !edpy->y_0_top, edpy->pos_x, edpy->pos_y); | |
129 | } else { | |
130 | /* no cursor -> use simple framebuffer blit */ | |
131 | egl_fb_blit(&edpy->blit_fb, &edpy->guest_fb, edpy->y_0_top); | |
132 | } | |
d8dc67e1 | 133 | |
a3517917 | 134 | egl_fb_read(surface_data(edpy->ds), &edpy->blit_fb); |
bb1599b6 GH |
135 | dpy_gfx_update(edpy->dcl.con, x, y, w, h); |
136 | } | |
137 | ||
138 | static const DisplayChangeListenerOps egl_ops = { | |
139 | .dpy_name = "egl-headless", | |
140 | .dpy_refresh = egl_refresh, | |
141 | .dpy_gfx_update = egl_gfx_update, | |
142 | .dpy_gfx_switch = egl_gfx_switch, | |
143 | ||
144 | .dpy_gl_ctx_create = qemu_egl_create_context, | |
145 | .dpy_gl_ctx_destroy = qemu_egl_destroy_context, | |
146 | .dpy_gl_ctx_make_current = qemu_egl_make_context_current, | |
147 | .dpy_gl_ctx_get_current = qemu_egl_get_current_context, | |
148 | ||
149 | .dpy_gl_scanout_disable = egl_scanout_disable, | |
150 | .dpy_gl_scanout_texture = egl_scanout_texture, | |
a3517917 GH |
151 | .dpy_gl_scanout_dmabuf = egl_scanout_dmabuf, |
152 | .dpy_gl_cursor_dmabuf = egl_cursor_dmabuf, | |
153 | .dpy_gl_release_dmabuf = egl_release_dmabuf, | |
bb1599b6 GH |
154 | .dpy_gl_update = egl_scanout_flush, |
155 | }; | |
156 | ||
157 | void egl_headless_init(void) | |
158 | { | |
159 | QemuConsole *con; | |
160 | egl_dpy *edpy; | |
161 | int idx; | |
162 | ||
163 | if (egl_rendernode_init(NULL) < 0) { | |
164 | error_report("egl: render node init failed"); | |
165 | exit(1); | |
166 | } | |
167 | ||
168 | for (idx = 0;; idx++) { | |
169 | con = qemu_console_lookup_by_index(idx); | |
170 | if (!con || !qemu_console_is_graphic(con)) { | |
171 | break; | |
172 | } | |
173 | ||
174 | edpy = g_new0(egl_dpy, 1); | |
175 | edpy->dcl.con = con; | |
176 | edpy->dcl.ops = &egl_ops; | |
a3517917 | 177 | edpy->gls = qemu_gl_init_shader(); |
bb1599b6 GH |
178 | register_displaychangelistener(&edpy->dcl); |
179 | } | |
180 | } |