audio: make playback packet length calculation exact
[qemu/ar7.git] / ui / sdl2-gl.c
blob39cab8cde7373f3c2bd8ecdfecb601de1580928a
1 /*
2 * QEMU SDL display driver -- opengl support
4 * Copyright (c) 2014 Red Hat
6 * Authors:
7 * Gerd Hoffmann <kraxel@redhat.com>
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:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
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.
28 #include "qemu/osdep.h"
29 #include "ui/console.h"
30 #include "ui/input.h"
31 #include "ui/sdl2.h"
33 static void sdl2_set_scanout_mode(struct sdl2_console *scon, bool scanout)
35 if (scon->scanout_mode == scanout) {
36 return;
39 scon->scanout_mode = scanout;
40 if (!scon->scanout_mode) {
41 egl_fb_destroy(&scon->guest_fb);
42 if (scon->surface) {
43 surface_gl_destroy_texture(scon->gls, scon->surface);
44 surface_gl_create_texture(scon->gls, scon->surface);
49 static void sdl2_gl_render_surface(struct sdl2_console *scon)
51 int ww, wh;
53 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
54 sdl2_set_scanout_mode(scon, false);
56 SDL_GetWindowSize(scon->real_window, &ww, &wh);
57 surface_gl_setup_viewport(scon->gls, scon->surface, ww, wh);
59 surface_gl_render_texture(scon->gls, scon->surface);
60 SDL_GL_SwapWindow(scon->real_window);
63 void sdl2_gl_update(DisplayChangeListener *dcl,
64 int x, int y, int w, int h)
66 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
68 assert(scon->opengl);
70 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
71 surface_gl_update_texture(scon->gls, scon->surface, x, y, w, h);
72 scon->updates++;
75 void sdl2_gl_switch(DisplayChangeListener *dcl,
76 DisplaySurface *new_surface)
78 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
79 DisplaySurface *old_surface = scon->surface;
81 assert(scon->opengl);
83 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
84 surface_gl_destroy_texture(scon->gls, scon->surface);
86 scon->surface = new_surface;
88 if (is_placeholder(new_surface) && qemu_console_get_index(dcl->con)) {
89 qemu_gl_fini_shader(scon->gls);
90 scon->gls = NULL;
91 sdl2_window_destroy(scon);
92 return;
95 if (!scon->real_window) {
96 sdl2_window_create(scon);
97 scon->gls = qemu_gl_init_shader();
98 } else if (old_surface &&
99 ((surface_width(old_surface) != surface_width(new_surface)) ||
100 (surface_height(old_surface) != surface_height(new_surface)))) {
101 sdl2_window_resize(scon);
104 surface_gl_create_texture(scon->gls, scon->surface);
107 void sdl2_gl_refresh(DisplayChangeListener *dcl)
109 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
111 assert(scon->opengl);
113 graphic_hw_update(dcl->con);
114 if (scon->updates && scon->real_window) {
115 scon->updates = 0;
116 sdl2_gl_render_surface(scon);
118 sdl2_poll_events(scon);
121 void sdl2_gl_redraw(struct sdl2_console *scon)
123 assert(scon->opengl);
125 if (scon->scanout_mode) {
126 /* sdl2_gl_scanout_flush actually only care about
127 * the first argument. */
128 return sdl2_gl_scanout_flush(&scon->dcl, 0, 0, 0, 0);
130 if (scon->surface) {
131 sdl2_gl_render_surface(scon);
135 QEMUGLContext sdl2_gl_create_context(DisplayGLCtx *dgc,
136 QEMUGLParams *params)
138 struct sdl2_console *scon = container_of(dgc, struct sdl2_console, dgc);
139 SDL_GLContext ctx;
141 assert(scon->opengl);
143 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
145 SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
146 if (scon->opts->gl == DISPLAYGL_MODE_ON ||
147 scon->opts->gl == DISPLAYGL_MODE_CORE) {
148 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
149 SDL_GL_CONTEXT_PROFILE_CORE);
150 } else if (scon->opts->gl == DISPLAYGL_MODE_ES) {
151 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
152 SDL_GL_CONTEXT_PROFILE_ES);
154 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, params->major_ver);
155 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, params->minor_ver);
157 ctx = SDL_GL_CreateContext(scon->real_window);
159 /* If SDL fail to create a GL context and we use the "on" flag,
160 * then try to fallback to GLES.
162 if (!ctx && scon->opts->gl == DISPLAYGL_MODE_ON) {
163 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
164 SDL_GL_CONTEXT_PROFILE_ES);
165 ctx = SDL_GL_CreateContext(scon->real_window);
167 return (QEMUGLContext)ctx;
170 void sdl2_gl_destroy_context(DisplayGLCtx *dgc, QEMUGLContext ctx)
172 SDL_GLContext sdlctx = (SDL_GLContext)ctx;
174 SDL_GL_DeleteContext(sdlctx);
177 int sdl2_gl_make_context_current(DisplayGLCtx *dgc,
178 QEMUGLContext ctx)
180 struct sdl2_console *scon = container_of(dgc, struct sdl2_console, dgc);
181 SDL_GLContext sdlctx = (SDL_GLContext)ctx;
183 assert(scon->opengl);
185 return SDL_GL_MakeCurrent(scon->real_window, sdlctx);
188 void sdl2_gl_scanout_disable(DisplayChangeListener *dcl)
190 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
192 assert(scon->opengl);
193 scon->w = 0;
194 scon->h = 0;
195 sdl2_set_scanout_mode(scon, false);
198 void sdl2_gl_scanout_texture(DisplayChangeListener *dcl,
199 uint32_t backing_id,
200 bool backing_y_0_top,
201 uint32_t backing_width,
202 uint32_t backing_height,
203 uint32_t x, uint32_t y,
204 uint32_t w, uint32_t h)
206 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
208 assert(scon->opengl);
209 scon->x = x;
210 scon->y = y;
211 scon->w = w;
212 scon->h = h;
213 scon->y0_top = backing_y_0_top;
215 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
217 sdl2_set_scanout_mode(scon, true);
218 egl_fb_setup_for_tex(&scon->guest_fb, backing_width, backing_height,
219 backing_id, false);
222 void sdl2_gl_scanout_flush(DisplayChangeListener *dcl,
223 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
225 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
226 int ww, wh;
228 assert(scon->opengl);
229 if (!scon->scanout_mode) {
230 return;
232 if (!scon->guest_fb.framebuffer) {
233 return;
236 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
238 SDL_GetWindowSize(scon->real_window, &ww, &wh);
239 egl_fb_setup_default(&scon->win_fb, ww, wh);
240 egl_fb_blit(&scon->win_fb, &scon->guest_fb, !scon->y0_top);
242 SDL_GL_SwapWindow(scon->real_window);