1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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"
33
34static 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) {
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);
46 }
47 }
48}
49
50static void sdl2_gl_render_surface(struct sdl2_console *scon)
51{
52 int ww, wh;
53
54 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
55 sdl2_set_scanout_mode(scon, false);
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
64void 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
76void 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) {
90 qemu_gl_fini_shader(scon->gls);
91 scon->gls = NULL;
92 sdl2_window_destroy(scon);
93 return;
94 }
95
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);
103 }
104
105 surface_gl_create_texture(scon->gls, scon->surface);
106}
107
108void 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
122void sdl2_gl_redraw(struct sdl2_console *scon)
123{
124 assert(scon->opengl);
125
126 if (scon->scanout_mode) {
127
128
129 return sdl2_gl_scanout_flush(&scon->dcl, 0, 0, 0, 0);
130 }
131 if (scon->surface) {
132 sdl2_gl_render_surface(scon);
133 }
134}
135
136QEMUGLContext 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);
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 }
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);
159
160
161
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 }
168 return (QEMUGLContext)ctx;
169}
170
171void sdl2_gl_destroy_context(DisplayChangeListener *dcl, QEMUGLContext ctx)
172{
173 SDL_GLContext sdlctx = (SDL_GLContext)ctx;
174
175 SDL_GL_DeleteContext(sdlctx);
176}
177
178int 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
189QEMUGLContext sdl2_gl_get_current_context(DisplayChangeListener *dcl)
190{
191 SDL_GLContext sdlctx;
192
193 sdlctx = SDL_GL_GetCurrentContext();
194 return (QEMUGLContext)sdlctx;
195}
196
197void 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;
204 sdl2_set_scanout_mode(scon, false);
205}
206
207void 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)
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;
222 scon->y0_top = backing_y_0_top;
223
224 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
225
226 sdl2_set_scanout_mode(scon, true);
227 egl_fb_setup_for_tex(&scon->guest_fb, backing_width, backing_height,
228 backing_id, false);
229}
230
231void 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);
235 int ww, wh;
236
237 assert(scon->opengl);
238 if (!scon->scanout_mode) {
239 return;
240 }
241 if (!scon->guest_fb.framebuffer) {
242 return;
243 }
244
245 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
246
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);
250
251 SDL_GL_SwapWindow(scon->real_window);
252}
253