master
c 320 lines 9.59 KB
Raw
1 /*
2 * QEMU SDL display driver -- opengl support
3 *
4 * Copyright (c) 2014 Red Hat
5 *
6 * Authors:
7 * Gerd Hoffmann <kraxel@redhat.com>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 */
27
28 #include "qemu/osdep.h"
29 #include "qemu/main-loop.h"
30 #include "qemu/error-report.h"
31 #include "ui/console.h"
32 #include "ui/input.h"
33 #include "ui/sdl2.h"
34
35 static void sdl2_set_scanout_mode(struct sdl2_console *scon, bool scanout)
36 {
37 if (scon->scanout_mode == scanout) {
38 return;
39 }
40
41 scon->scanout_mode = scanout;
42 if (!scon->scanout_mode) {
43 egl_fb_destroy(&scon->guest_fb);
44 if (scon->surface) {
45 surface_gl_destroy_texture(scon->gls, scon->surface);
46 surface_gl_create_texture(scon->gls, scon->surface);
47 }
48 }
49 }
50
51 static void sdl2_gl_render_surface(struct sdl2_console *scon)
52 {
53 int ww, wh;
54
55 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
56
57 SDL_GetWindowSize(scon->real_window, &ww, &wh);
58 surface_gl_setup_viewport(scon->gls, scon->surface, ww, wh);
59
60 surface_gl_render_texture(scon->gls, scon->surface);
61 SDL_GL_SwapWindow(scon->real_window);
62 }
63
64 void sdl2_gl_update(DisplayChangeListener *dcl,
65 int x, int y, int w, int h)
66 {
67 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
68
69 assert(scon->opengl);
70
71 if (!scon->real_window) {
72 return;
73 }
74
75 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
76 surface_gl_update_texture(scon->gls, scon->surface, x, y, w, h);
77 scon->updates++;
78 }
79
80 void sdl2_gl_switch(DisplayChangeListener *dcl,
81 DisplaySurface *new_surface)
82 {
83 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
84 DisplaySurface *old_surface = scon->surface;
85
86 assert(scon->opengl);
87
88 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
89 surface_gl_destroy_texture(scon->gls, scon->surface);
90
91 scon->surface = new_surface;
92
93 if (surface_is_placeholder(new_surface) && qemu_console_get_index(dcl->con)) {
94 qemu_gl_fini_shader(scon->gls);
95 scon->gls = NULL;
96 sdl2_window_destroy(scon);
97 return;
98 }
99
100 if (!scon->real_window) {
101 sdl2_window_create(scon);
102 scon->gls = qemu_gl_init_shader();
103 } else if (old_surface &&
104 ((surface_width(old_surface) != surface_width(new_surface)) ||
105 (surface_height(old_surface) != surface_height(new_surface)))) {
106 sdl2_window_resize(scon);
107 }
108
109 surface_gl_create_texture(scon->gls, scon->surface);
110 }
111
112 void sdl2_gl_refresh(DisplayChangeListener *dcl)
113 {
114 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
115
116 assert(scon->opengl);
117
118 qemu_console_hw_update(dcl->con);
119 if (scon->updates && scon->real_window) {
120 scon->updates = 0;
121 sdl2_gl_render_surface(scon);
122 }
123 sdl2_poll_events(scon);
124 }
125
126 void sdl2_gl_redraw(struct sdl2_console *scon)
127 {
128 assert(scon->opengl);
129
130 if (scon->scanout_mode) {
131 /* sdl2_gl_scanout_flush actually only care about
132 * the first argument. */
133 return sdl2_gl_scanout_flush(&scon->dcl, 0, 0, 0, 0);
134 }
135 if (scon->surface) {
136 sdl2_gl_render_surface(scon);
137 }
138 }
139
140 QEMUGLContext sdl2_gl_create_context(DisplayGLCtx *dgc,
141 QEMUGLParams *params)
142 {
143 struct sdl2_console *scon = container_of(dgc, struct sdl2_console, dgc);
144 SDL_GLContext ctx, current_ctx;
145
146 assert(scon->opengl);
147
148 current_ctx = SDL_GL_GetCurrentContext();
149
150 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
151
152 SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
153 if (scon->opts->gl == DISPLAY_GL_MODE_ON ||
154 scon->opts->gl == DISPLAY_GL_MODE_CORE) {
155 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
156 SDL_GL_CONTEXT_PROFILE_CORE);
157 } else if (scon->opts->gl == DISPLAY_GL_MODE_ES) {
158 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
159 SDL_GL_CONTEXT_PROFILE_ES);
160 }
161 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, params->major_ver);
162 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, params->minor_ver);
163
164 ctx = SDL_GL_CreateContext(scon->real_window);
165
166 /* If SDL fail to create a GL context and we use the "on" flag,
167 * then try to fallback to GLES.
168 */
169 if (!ctx && scon->opts->gl == DISPLAY_GL_MODE_ON) {
170 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
171 SDL_GL_CONTEXT_PROFILE_ES);
172 ctx = SDL_GL_CreateContext(scon->real_window);
173 }
174
175 SDL_GL_MakeCurrent(scon->real_window, current_ctx);
176
177 return (QEMUGLContext)ctx;
178 }
179
180 void sdl2_gl_destroy_context(DisplayGLCtx *dgc, QEMUGLContext ctx)
181 {
182 SDL_GLContext sdlctx = (SDL_GLContext)ctx;
183
184 SDL_GL_DeleteContext(sdlctx);
185 }
186
187 int sdl2_gl_make_context_current(DisplayGLCtx *dgc,
188 QEMUGLContext ctx)
189 {
190 struct sdl2_console *scon = container_of(dgc, struct sdl2_console, dgc);
191 SDL_GLContext sdlctx = (SDL_GLContext)ctx;
192
193 assert(scon->opengl);
194
195 return SDL_GL_MakeCurrent(scon->real_window, sdlctx);
196 }
197
198 void sdl2_gl_scanout_disable(DisplayChangeListener *dcl)
199 {
200 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
201
202 assert(scon->opengl);
203 scon->w = 0;
204 scon->h = 0;
205 sdl2_set_scanout_mode(scon, false);
206 }
207
208 void sdl2_gl_scanout_texture(DisplayChangeListener *dcl,
209 uint32_t backing_id,
210 bool backing_y_0_top,
211 uint32_t backing_width,
212 uint32_t backing_height,
213 uint32_t x, uint32_t y,
214 uint32_t w, uint32_t h,
215 void *d3d_tex2d)
216 {
217 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
218
219 assert(scon->opengl);
220 scon->x = x;
221 scon->y = y;
222 scon->w = w;
223 scon->h = h;
224 scon->y0_top = backing_y_0_top;
225
226 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
227
228 sdl2_set_scanout_mode(scon, true);
229 egl_fb_setup_for_tex(&scon->guest_fb, backing_width, backing_height,
230 backing_id, false);
231 }
232
233 void sdl2_gl_scanout_flush(DisplayChangeListener *dcl,
234 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
235 {
236 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
237 int ww, wh;
238
239 assert(scon->opengl);
240 if (!scon->scanout_mode) {
241 return;
242 }
243 if (!scon->guest_fb.framebuffer) {
244 return;
245 }
246
247 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
248
249 SDL_GetWindowSize(scon->real_window, &ww, &wh);
250 egl_fb_setup_default(&scon->win_fb, ww, wh, 0, 0);
251 egl_fb_blit(&scon->win_fb, &scon->guest_fb, !scon->y0_top);
252
253 SDL_GL_SwapWindow(scon->real_window);
254 }
255
256 #ifdef CONFIG_GBM
257 void sdl2_gl_scanout_dmabuf(DisplayChangeListener *dcl,
258 QemuDmaBuf *dmabuf)
259 {
260 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
261 const int *fds;
262
263 assert(scon->opengl);
264 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
265
266 egl_dmabuf_import_texture(dmabuf);
267 if (!qemu_dmabuf_get_texture(dmabuf)) {
268 fds = qemu_dmabuf_get_fds(dmabuf, NULL);
269 error_report("%s: failed fd=%d", __func__, fds ? fds[0] : -1);
270 return;
271 }
272
273 sdl2_gl_scanout_texture(dcl, qemu_dmabuf_get_texture(dmabuf), false,
274 qemu_dmabuf_get_width(dmabuf),
275 qemu_dmabuf_get_height(dmabuf),
276 0, 0,
277 qemu_dmabuf_get_width(dmabuf),
278 qemu_dmabuf_get_height(dmabuf),
279 NULL);
280
281 if (qemu_dmabuf_get_allow_fences(dmabuf)) {
282 scon->guest_fb.dmabuf = dmabuf;
283 }
284 }
285
286 void sdl2_gl_release_dmabuf(DisplayChangeListener *dcl,
287 QemuDmaBuf *dmabuf)
288 {
289 egl_dmabuf_release_texture(dmabuf);
290 }
291
292 bool sdl2_gl_has_dmabuf(DisplayChangeListener *dcl)
293 {
294 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
295
296 return scon->has_dmabuf;
297 }
298 #endif
299
300 void sdl2_gl_console_init(struct sdl2_console *scon)
301 {
302 bool hidden = scon->hidden;
303
304 scon->hidden = true;
305 scon->surface = qemu_create_displaysurface(1, 1);
306 sdl2_window_create(scon);
307
308 /*
309 * QEMU checks whether console supports dma-buf before switching
310 * to the console. To break this chicken-egg problem we pre-check
311 * dma-buf availability beforehand using a dummy SDL window.
312 */
313 scon->has_dmabuf = qemu_egl_has_dmabuf();
314
315 sdl2_window_destroy(scon);
316 qemu_free_displaysurface(scon->surface);
317
318 scon->surface = NULL;
319 scon->hidden = hidden;
320 }