]> git.proxmox.com Git - mirror_qemu.git/blame - ui/sdl2-gl.c
sdl: Move DisplayOptions global to sdl2_console
[mirror_qemu.git] / ui / sdl2-gl.c
CommitLineData
0b71a5d5
GH
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
e16f4c87 28#include "qemu/osdep.h"
0b71a5d5
GH
29#include "qemu-common.h"
30#include "ui/console.h"
31#include "ui/input.h"
32#include "ui/sdl2.h"
33#include "sysemu/sysemu.h"
34
cb47dc9a
GH
35static 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) {
371c4ef6 43 egl_fb_destroy(&scon->guest_fb);
cb47dc9a
GH
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
0b71a5d5
GH
51static void sdl2_gl_render_surface(struct sdl2_console *scon)
52{
53 int ww, wh;
54
55 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
cb47dc9a 56 sdl2_set_scanout_mode(scon, false);
0b71a5d5
GH
57
58 SDL_GetWindowSize(scon->real_window, &ww, &wh);
59 surface_gl_setup_viewport(scon->gls, scon->surface, ww, wh);
60
61 surface_gl_render_texture(scon->gls, scon->surface);
62 SDL_GL_SwapWindow(scon->real_window);
63}
64
65void sdl2_gl_update(DisplayChangeListener *dcl,
66 int x, int y, int w, int h)
67{
68 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
69
70 assert(scon->opengl);
71
72 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
73 surface_gl_update_texture(scon->gls, scon->surface, x, y, w, h);
74 scon->updates++;
75}
76
77void sdl2_gl_switch(DisplayChangeListener *dcl,
78 DisplaySurface *new_surface)
79{
80 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
81 DisplaySurface *old_surface = scon->surface;
82
83 assert(scon->opengl);
84
85 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
86 surface_gl_destroy_texture(scon->gls, scon->surface);
87
88 scon->surface = new_surface;
89
90 if (!new_surface) {
46e19e14 91 qemu_gl_fini_shader(scon->gls);
0b71a5d5
GH
92 scon->gls = NULL;
93 sdl2_window_destroy(scon);
94 return;
95 }
96
97 if (!scon->real_window) {
98 sdl2_window_create(scon);
46e19e14 99 scon->gls = qemu_gl_init_shader();
0b71a5d5
GH
100 } else if (old_surface &&
101 ((surface_width(old_surface) != surface_width(new_surface)) ||
102 (surface_height(old_surface) != surface_height(new_surface)))) {
103 sdl2_window_resize(scon);
104 }
105
106 surface_gl_create_texture(scon->gls, scon->surface);
107}
108
109void sdl2_gl_refresh(DisplayChangeListener *dcl)
110{
111 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
112
113 assert(scon->opengl);
114
115 graphic_hw_update(dcl->con);
116 if (scon->updates && scon->surface) {
117 scon->updates = 0;
118 sdl2_gl_render_surface(scon);
119 }
120 sdl2_poll_events(scon);
121}
122
123void sdl2_gl_redraw(struct sdl2_console *scon)
124{
125 assert(scon->opengl);
126
127 if (scon->surface) {
128 sdl2_gl_render_surface(scon);
129 }
130}
cb47dc9a
GH
131
132QEMUGLContext sdl2_gl_create_context(DisplayChangeListener *dcl,
133 QEMUGLParams *params)
134{
135 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
136 SDL_GLContext ctx;
137
138 assert(scon->opengl);
139
140 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
141
142 SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
143 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
144 SDL_GL_CONTEXT_PROFILE_CORE);
145 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, params->major_ver);
146 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, params->minor_ver);
147
148 ctx = SDL_GL_CreateContext(scon->real_window);
149 return (QEMUGLContext)ctx;
150}
151
152void sdl2_gl_destroy_context(DisplayChangeListener *dcl, QEMUGLContext ctx)
153{
154 SDL_GLContext sdlctx = (SDL_GLContext)ctx;
155
156 SDL_GL_DeleteContext(sdlctx);
157}
158
159int sdl2_gl_make_context_current(DisplayChangeListener *dcl,
160 QEMUGLContext ctx)
161{
162 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
163 SDL_GLContext sdlctx = (SDL_GLContext)ctx;
164
165 assert(scon->opengl);
166
167 return SDL_GL_MakeCurrent(scon->real_window, sdlctx);
168}
169
170QEMUGLContext sdl2_gl_get_current_context(DisplayChangeListener *dcl)
171{
172 SDL_GLContext sdlctx;
173
174 sdlctx = SDL_GL_GetCurrentContext();
175 return (QEMUGLContext)sdlctx;
176}
177
db6cdfbe
GH
178void sdl2_gl_scanout_disable(DisplayChangeListener *dcl)
179{
180 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
181
182 assert(scon->opengl);
183 scon->w = 0;
184 scon->h = 0;
db6cdfbe
GH
185 sdl2_set_scanout_mode(scon, false);
186}
187
f4c36bda
GH
188void sdl2_gl_scanout_texture(DisplayChangeListener *dcl,
189 uint32_t backing_id,
190 bool backing_y_0_top,
191 uint32_t backing_width,
192 uint32_t backing_height,
193 uint32_t x, uint32_t y,
194 uint32_t w, uint32_t h)
cb47dc9a
GH
195{
196 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
197
198 assert(scon->opengl);
199 scon->x = x;
200 scon->y = y;
201 scon->w = w;
202 scon->h = h;
cb47dc9a
GH
203 scon->y0_top = backing_y_0_top;
204
205 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
206
cb47dc9a 207 sdl2_set_scanout_mode(scon, true);
74083f9c
GH
208 egl_fb_setup_for_tex(&scon->guest_fb, backing_width, backing_height,
209 backing_id, false);
cb47dc9a
GH
210}
211
212void sdl2_gl_scanout_flush(DisplayChangeListener *dcl,
213 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
214{
215 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
371c4ef6 216 int ww, wh;
cb47dc9a
GH
217
218 assert(scon->opengl);
219 if (!scon->scanout_mode) {
220 return;
221 }
371c4ef6 222 if (!scon->guest_fb.framebuffer) {
cb47dc9a
GH
223 return;
224 }
225
226 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
227
cb47dc9a 228 SDL_GetWindowSize(scon->real_window, &ww, &wh);
371c4ef6
GH
229 egl_fb_setup_default(&scon->win_fb, ww, wh);
230 egl_fb_blit(&scon->win_fb, &scon->guest_fb, !scon->y0_top);
cb47dc9a
GH
231
232 SDL_GL_SwapWindow(scon->real_window);
233}