]>
Commit | Line | Data |
---|---|---|
0b71a5d5 GH |
1 | /* |
2 | * QEMU SDL display driver -- opengl support | |
3 | * | |
4 | * Copyright (c) 2014 Red Hat | |
5 | * | |
6 | * Authors: | |
7 | * Gerd Hoffmann <[email protected]> | |
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 "ui/console.h" |
30 | #include "ui/input.h" | |
31 | #include "ui/sdl2.h" | |
32 | #include "sysemu/sysemu.h" | |
33 | ||
cb47dc9a GH |
34 | static void sdl2_set_scanout_mode(struct sdl2_console *scon, bool scanout) |
35 | { | |
36 | if (scon->scanout_mode == scanout) { | |
37 | return; | |
38 | } | |
39 | ||
40 | scon->scanout_mode = scanout; | |
41 | if (!scon->scanout_mode) { | |
371c4ef6 | 42 | egl_fb_destroy(&scon->guest_fb); |
cb47dc9a GH |
43 | if (scon->surface) { |
44 | surface_gl_destroy_texture(scon->gls, scon->surface); | |
45 | surface_gl_create_texture(scon->gls, scon->surface); | |
46 | } | |
47 | } | |
48 | } | |
49 | ||
0b71a5d5 GH |
50 | static void sdl2_gl_render_surface(struct sdl2_console *scon) |
51 | { | |
52 | int ww, wh; | |
53 | ||
54 | SDL_GL_MakeCurrent(scon->real_window, scon->winctx); | |
cb47dc9a | 55 | sdl2_set_scanout_mode(scon, false); |
0b71a5d5 GH |
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 | SDL_GL_MakeCurrent(scon->real_window, scon->winctx); | |
72 | surface_gl_update_texture(scon->gls, scon->surface, x, y, w, h); | |
73 | scon->updates++; | |
74 | } | |
75 | ||
76 | void sdl2_gl_switch(DisplayChangeListener *dcl, | |
77 | DisplaySurface *new_surface) | |
78 | { | |
79 | struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl); | |
80 | DisplaySurface *old_surface = scon->surface; | |
81 | ||
82 | assert(scon->opengl); | |
83 | ||
84 | SDL_GL_MakeCurrent(scon->real_window, scon->winctx); | |
85 | surface_gl_destroy_texture(scon->gls, scon->surface); | |
86 | ||
87 | scon->surface = new_surface; | |
88 | ||
89 | if (!new_surface) { | |
46e19e14 | 90 | qemu_gl_fini_shader(scon->gls); |
0b71a5d5 GH |
91 | scon->gls = NULL; |
92 | sdl2_window_destroy(scon); | |
93 | return; | |
94 | } | |
95 | ||
96 | if (!scon->real_window) { | |
97 | sdl2_window_create(scon); | |
46e19e14 | 98 | scon->gls = qemu_gl_init_shader(); |
0b71a5d5 GH |
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); | |
103 | } | |
104 | ||
105 | surface_gl_create_texture(scon->gls, scon->surface); | |
106 | } | |
107 | ||
108 | void sdl2_gl_refresh(DisplayChangeListener *dcl) | |
109 | { | |
110 | struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl); | |
111 | ||
112 | assert(scon->opengl); | |
113 | ||
114 | graphic_hw_update(dcl->con); | |
115 | if (scon->updates && scon->surface) { | |
116 | scon->updates = 0; | |
117 | sdl2_gl_render_surface(scon); | |
118 | } | |
119 | sdl2_poll_events(scon); | |
120 | } | |
121 | ||
122 | void sdl2_gl_redraw(struct sdl2_console *scon) | |
123 | { | |
124 | assert(scon->opengl); | |
125 | ||
77f60fb4 TW |
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); | |
130 | } | |
0b71a5d5 GH |
131 | if (scon->surface) { |
132 | sdl2_gl_render_surface(scon); | |
133 | } | |
134 | } | |
cb47dc9a GH |
135 | |
136 | QEMUGLContext sdl2_gl_create_context(DisplayChangeListener *dcl, | |
137 | QEMUGLParams *params) | |
138 | { | |
139 | struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl); | |
140 | SDL_GLContext ctx; | |
141 | ||
142 | assert(scon->opengl); | |
143 | ||
144 | SDL_GL_MakeCurrent(scon->real_window, scon->winctx); | |
145 | ||
146 | SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1); | |
4867e47c ET |
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); | |
154 | } | |
cb47dc9a GH |
155 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, params->major_ver); |
156 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, params->minor_ver); | |
157 | ||
158 | ctx = SDL_GL_CreateContext(scon->real_window); | |
4867e47c ET |
159 | |
160 | /* If SDL fail to create a GL context and we use the "on" flag, | |
161 | * then try to fallback to GLES. | |
162 | */ | |
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); | |
167 | } | |
cb47dc9a GH |
168 | return (QEMUGLContext)ctx; |
169 | } | |
170 | ||
171 | void sdl2_gl_destroy_context(DisplayChangeListener *dcl, QEMUGLContext ctx) | |
172 | { | |
173 | SDL_GLContext sdlctx = (SDL_GLContext)ctx; | |
174 | ||
175 | SDL_GL_DeleteContext(sdlctx); | |
176 | } | |
177 | ||
178 | int sdl2_gl_make_context_current(DisplayChangeListener *dcl, | |
179 | QEMUGLContext ctx) | |
180 | { | |
181 | struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl); | |
182 | SDL_GLContext sdlctx = (SDL_GLContext)ctx; | |
183 | ||
184 | assert(scon->opengl); | |
185 | ||
186 | return SDL_GL_MakeCurrent(scon->real_window, sdlctx); | |
187 | } | |
188 | ||
189 | QEMUGLContext sdl2_gl_get_current_context(DisplayChangeListener *dcl) | |
190 | { | |
191 | SDL_GLContext sdlctx; | |
192 | ||
193 | sdlctx = SDL_GL_GetCurrentContext(); | |
194 | return (QEMUGLContext)sdlctx; | |
195 | } | |
196 | ||
db6cdfbe GH |
197 | void sdl2_gl_scanout_disable(DisplayChangeListener *dcl) |
198 | { | |
199 | struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl); | |
200 | ||
201 | assert(scon->opengl); | |
202 | scon->w = 0; | |
203 | scon->h = 0; | |
db6cdfbe GH |
204 | sdl2_set_scanout_mode(scon, false); |
205 | } | |
206 | ||
f4c36bda GH |
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) | |
cb47dc9a GH |
214 | { |
215 | struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl); | |
216 | ||
217 | assert(scon->opengl); | |
218 | scon->x = x; | |
219 | scon->y = y; | |
220 | scon->w = w; | |
221 | scon->h = h; | |
cb47dc9a GH |
222 | scon->y0_top = backing_y_0_top; |
223 | ||
224 | SDL_GL_MakeCurrent(scon->real_window, scon->winctx); | |
225 | ||
cb47dc9a | 226 | sdl2_set_scanout_mode(scon, true); |
74083f9c GH |
227 | egl_fb_setup_for_tex(&scon->guest_fb, backing_width, backing_height, |
228 | backing_id, false); | |
cb47dc9a GH |
229 | } |
230 | ||
231 | void sdl2_gl_scanout_flush(DisplayChangeListener *dcl, | |
232 | uint32_t x, uint32_t y, uint32_t w, uint32_t h) | |
233 | { | |
234 | struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl); | |
371c4ef6 | 235 | int ww, wh; |
cb47dc9a GH |
236 | |
237 | assert(scon->opengl); | |
238 | if (!scon->scanout_mode) { | |
239 | return; | |
240 | } | |
371c4ef6 | 241 | if (!scon->guest_fb.framebuffer) { |
cb47dc9a GH |
242 | return; |
243 | } | |
244 | ||
245 | SDL_GL_MakeCurrent(scon->real_window, scon->winctx); | |
246 | ||
cb47dc9a | 247 | SDL_GetWindowSize(scon->real_window, &ww, &wh); |
371c4ef6 GH |
248 | egl_fb_setup_default(&scon->win_fb, ww, wh); |
249 | egl_fb_blit(&scon->win_fb, &scon->guest_fb, !scon->y0_top); | |
cb47dc9a GH |
250 | |
251 | SDL_GL_SwapWindow(scon->real_window); | |
252 | } |