Merge remote-tracking branch 'remotes/armbru/tags/pull-build-2019-07-02-v2' into...
[qemu/ar7.git] / ui / sdl2-gl.c
blob7a37e7c995a59509e58bc6c6f4c0d63bbe87c6bc
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"
32 #include "sysemu/sysemu.h"
34 static void sdl2_set_scanout_mode(struct sdl2_console *scon, bool scanout)
36 if (scon->scanout_mode == scanout) {
37 return;
40 scon->scanout_mode = scanout;
41 if (!scon->scanout_mode) {
42 egl_fb_destroy(&scon->guest_fb);
43 if (scon->surface) {
44 surface_gl_destroy_texture(scon->gls, scon->surface);
45 surface_gl_create_texture(scon->gls, scon->surface);
50 static void sdl2_gl_render_surface(struct sdl2_console *scon)
52 int ww, wh;
54 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
55 sdl2_set_scanout_mode(scon, false);
57 SDL_GetWindowSize(scon->real_window, &ww, &wh);
58 surface_gl_setup_viewport(scon->gls, scon->surface, ww, wh);
60 surface_gl_render_texture(scon->gls, scon->surface);
61 SDL_GL_SwapWindow(scon->real_window);
64 void sdl2_gl_update(DisplayChangeListener *dcl,
65 int x, int y, int w, int h)
67 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
69 assert(scon->opengl);
71 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
72 surface_gl_update_texture(scon->gls, scon->surface, x, y, w, h);
73 scon->updates++;
76 void sdl2_gl_switch(DisplayChangeListener *dcl,
77 DisplaySurface *new_surface)
79 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
80 DisplaySurface *old_surface = scon->surface;
82 assert(scon->opengl);
84 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
85 surface_gl_destroy_texture(scon->gls, scon->surface);
87 scon->surface = new_surface;
89 if (!new_surface) {
90 qemu_gl_fini_shader(scon->gls);
91 scon->gls = NULL;
92 sdl2_window_destroy(scon);
93 return;
96 if (!scon->real_window) {
97 sdl2_window_create(scon);
98 scon->gls = qemu_gl_init_shader();
99 } else if (old_surface &&
100 ((surface_width(old_surface) != surface_width(new_surface)) ||
101 (surface_height(old_surface) != surface_height(new_surface)))) {
102 sdl2_window_resize(scon);
105 surface_gl_create_texture(scon->gls, scon->surface);
108 void sdl2_gl_refresh(DisplayChangeListener *dcl)
110 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
112 assert(scon->opengl);
114 graphic_hw_update(dcl->con);
115 if (scon->updates && scon->surface) {
116 scon->updates = 0;
117 sdl2_gl_render_surface(scon);
119 sdl2_poll_events(scon);
122 void sdl2_gl_redraw(struct sdl2_console *scon)
124 assert(scon->opengl);
126 if (scon->scanout_mode) {
127 /* sdl2_gl_scanout_flush actually only care about
128 * the first argument. */
129 return sdl2_gl_scanout_flush(&scon->dcl, 0, 0, 0, 0);
131 if (scon->surface) {
132 sdl2_gl_render_surface(scon);
136 QEMUGLContext sdl2_gl_create_context(DisplayChangeListener *dcl,
137 QEMUGLParams *params)
139 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
140 SDL_GLContext ctx;
142 assert(scon->opengl);
144 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
146 SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
147 if (scon->opts->gl == DISPLAYGL_MODE_ON ||
148 scon->opts->gl == DISPLAYGL_MODE_CORE) {
149 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
150 SDL_GL_CONTEXT_PROFILE_CORE);
151 } else if (scon->opts->gl == DISPLAYGL_MODE_ES) {
152 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
153 SDL_GL_CONTEXT_PROFILE_ES);
155 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, params->major_ver);
156 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, params->minor_ver);
158 ctx = SDL_GL_CreateContext(scon->real_window);
160 /* If SDL fail to create a GL context and we use the "on" flag,
161 * then try to fallback to GLES.
163 if (!ctx && scon->opts->gl == DISPLAYGL_MODE_ON) {
164 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
165 SDL_GL_CONTEXT_PROFILE_ES);
166 ctx = SDL_GL_CreateContext(scon->real_window);
168 return (QEMUGLContext)ctx;
171 void sdl2_gl_destroy_context(DisplayChangeListener *dcl, QEMUGLContext ctx)
173 SDL_GLContext sdlctx = (SDL_GLContext)ctx;
175 SDL_GL_DeleteContext(sdlctx);
178 int sdl2_gl_make_context_current(DisplayChangeListener *dcl,
179 QEMUGLContext ctx)
181 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
182 SDL_GLContext sdlctx = (SDL_GLContext)ctx;
184 assert(scon->opengl);
186 return SDL_GL_MakeCurrent(scon->real_window, sdlctx);
189 QEMUGLContext sdl2_gl_get_current_context(DisplayChangeListener *dcl)
191 SDL_GLContext sdlctx;
193 sdlctx = SDL_GL_GetCurrentContext();
194 return (QEMUGLContext)sdlctx;
197 void sdl2_gl_scanout_disable(DisplayChangeListener *dcl)
199 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
201 assert(scon->opengl);
202 scon->w = 0;
203 scon->h = 0;
204 sdl2_set_scanout_mode(scon, false);
207 void sdl2_gl_scanout_texture(DisplayChangeListener *dcl,
208 uint32_t backing_id,
209 bool backing_y_0_top,
210 uint32_t backing_width,
211 uint32_t backing_height,
212 uint32_t x, uint32_t y,
213 uint32_t w, uint32_t h)
215 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
217 assert(scon->opengl);
218 scon->x = x;
219 scon->y = y;
220 scon->w = w;
221 scon->h = h;
222 scon->y0_top = backing_y_0_top;
224 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
226 sdl2_set_scanout_mode(scon, true);
227 egl_fb_setup_for_tex(&scon->guest_fb, backing_width, backing_height,
228 backing_id, false);
231 void sdl2_gl_scanout_flush(DisplayChangeListener *dcl,
232 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
234 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
235 int ww, wh;
237 assert(scon->opengl);
238 if (!scon->scanout_mode) {
239 return;
241 if (!scon->guest_fb.framebuffer) {
242 return;
245 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
247 SDL_GetWindowSize(scon->real_window, &ww, &wh);
248 egl_fb_setup_default(&scon->win_fb, ww, wh);
249 egl_fb_blit(&scon->win_fb, &scon->guest_fb, !scon->y0_top);
251 SDL_GL_SwapWindow(scon->real_window);