blob: e01d9ab0c7bf33c87fc85ec2742e8df33cedf563 [file] [log] [blame]
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +01001/*
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
Peter Maydelle16f4c82016-01-29 17:49:51 +000028#include "qemu/osdep.h"
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +010029#include "ui/console.h"
30#include "ui/input.h"
31#include "ui/sdl2.h"
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +010032
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +010033static void sdl2_set_scanout_mode(struct sdl2_console *scon, bool scanout)
34{
35 if (scon->scanout_mode == scanout) {
36 return;
37 }
38
39 scon->scanout_mode = scanout;
40 if (!scon->scanout_mode) {
Gerd Hoffmann371c4ef2017-06-14 10:41:48 +020041 egl_fb_destroy(&scon->guest_fb);
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +010042 if (scon->surface) {
43 surface_gl_destroy_texture(scon->gls, scon->surface);
44 surface_gl_create_texture(scon->gls, scon->surface);
45 }
46 }
47}
48
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +010049static void sdl2_gl_render_surface(struct sdl2_console *scon)
50{
51 int ww, wh;
52
53 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +010054 sdl2_set_scanout_mode(scon, false);
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +010055
56 SDL_GetWindowSize(scon->real_window, &ww, &wh);
57 surface_gl_setup_viewport(scon->gls, scon->surface, ww, wh);
58
59 surface_gl_render_texture(scon->gls, scon->surface);
60 SDL_GL_SwapWindow(scon->real_window);
61}
62
63void sdl2_gl_update(DisplayChangeListener *dcl,
64 int x, int y, int w, int h)
65{
66 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
67
68 assert(scon->opengl);
69
Marc-André Lureaub3a654d2023-05-11 11:42:17 +040070 if (!scon->real_window) {
71 return;
72 }
73
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +010074 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
75 surface_gl_update_texture(scon->gls, scon->surface, x, y, w, h);
76 scon->updates++;
77}
78
79void sdl2_gl_switch(DisplayChangeListener *dcl,
80 DisplaySurface *new_surface)
81{
82 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
83 DisplaySurface *old_surface = scon->surface;
84
85 assert(scon->opengl);
86
87 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
88 surface_gl_destroy_texture(scon->gls, scon->surface);
89
90 scon->surface = new_surface;
91
Gerd Hoffmann9badf122024-06-05 15:14:42 +020092 if (surface_is_placeholder(new_surface) && qemu_console_get_index(dcl->con)) {
Gerd Hoffmann46e19e12017-10-10 15:54:49 +020093 qemu_gl_fini_shader(scon->gls);
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +010094 scon->gls = NULL;
95 sdl2_window_destroy(scon);
96 return;
97 }
98
99 if (!scon->real_window) {
100 sdl2_window_create(scon);
Gerd Hoffmann46e19e12017-10-10 15:54:49 +0200101 scon->gls = qemu_gl_init_shader();
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +0100102 } else if (old_surface &&
103 ((surface_width(old_surface) != surface_width(new_surface)) ||
104 (surface_height(old_surface) != surface_height(new_surface)))) {
105 sdl2_window_resize(scon);
106 }
107
108 surface_gl_create_texture(scon->gls, scon->surface);
109}
110
111void sdl2_gl_refresh(DisplayChangeListener *dcl)
112{
113 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
114
115 assert(scon->opengl);
116
117 graphic_hw_update(dcl->con);
Akihiko Odakic821a582021-02-25 19:13:15 +0900118 if (scon->updates && scon->real_window) {
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +0100119 scon->updates = 0;
120 sdl2_gl_render_surface(scon);
121 }
122 sdl2_poll_events(scon);
123}
124
125void sdl2_gl_redraw(struct sdl2_console *scon)
126{
127 assert(scon->opengl);
128
Tao Wu77f60fb2018-07-26 15:59:00 -0700129 if (scon->scanout_mode) {
130 /* sdl2_gl_scanout_flush actually only care about
131 * the first argument. */
132 return sdl2_gl_scanout_flush(&scon->dcl, 0, 0, 0, 0);
133 }
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +0100134 if (scon->surface) {
135 sdl2_gl_render_surface(scon);
136 }
137}
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +0100138
Marc-André Lureau5e79d512021-10-09 23:48:46 +0400139QEMUGLContext sdl2_gl_create_context(DisplayGLCtx *dgc,
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +0100140 QEMUGLParams *params)
141{
Marc-André Lureau5e79d512021-10-09 23:48:46 +0400142 struct sdl2_console *scon = container_of(dgc, struct sdl2_console, dgc);
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +0100143 SDL_GLContext ctx;
144
145 assert(scon->opengl);
146
147 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
148
149 SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
Markus Armbruster154fd4d2024-09-04 13:18:25 +0200150 if (scon->opts->gl == DISPLAY_GL_MODE_ON ||
151 scon->opts->gl == DISPLAY_GL_MODE_CORE) {
Elie Tournier4867e472018-04-13 14:58:42 +0100152 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
153 SDL_GL_CONTEXT_PROFILE_CORE);
Markus Armbruster154fd4d2024-09-04 13:18:25 +0200154 } else if (scon->opts->gl == DISPLAY_GL_MODE_ES) {
Elie Tournier4867e472018-04-13 14:58:42 +0100155 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
156 SDL_GL_CONTEXT_PROFILE_ES);
157 }
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +0100158 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, params->major_ver);
159 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, params->minor_ver);
160
161 ctx = SDL_GL_CreateContext(scon->real_window);
Elie Tournier4867e472018-04-13 14:58:42 +0100162
163 /* If SDL fail to create a GL context and we use the "on" flag,
164 * then try to fallback to GLES.
165 */
Markus Armbruster154fd4d2024-09-04 13:18:25 +0200166 if (!ctx && scon->opts->gl == DISPLAY_GL_MODE_ON) {
Elie Tournier4867e472018-04-13 14:58:42 +0100167 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
168 SDL_GL_CONTEXT_PROFILE_ES);
169 ctx = SDL_GL_CreateContext(scon->real_window);
170 }
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +0100171 return (QEMUGLContext)ctx;
172}
173
Marc-André Lureau5e79d512021-10-09 23:48:46 +0400174void sdl2_gl_destroy_context(DisplayGLCtx *dgc, QEMUGLContext ctx)
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +0100175{
176 SDL_GLContext sdlctx = (SDL_GLContext)ctx;
177
178 SDL_GL_DeleteContext(sdlctx);
179}
180
Marc-André Lureau5e79d512021-10-09 23:48:46 +0400181int sdl2_gl_make_context_current(DisplayGLCtx *dgc,
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +0100182 QEMUGLContext ctx)
183{
Marc-André Lureau5e79d512021-10-09 23:48:46 +0400184 struct sdl2_console *scon = container_of(dgc, struct sdl2_console, dgc);
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +0100185 SDL_GLContext sdlctx = (SDL_GLContext)ctx;
186
187 assert(scon->opengl);
188
189 return SDL_GL_MakeCurrent(scon->real_window, sdlctx);
190}
191
Gerd Hoffmanndb6cdfb2017-02-21 10:37:20 +0100192void sdl2_gl_scanout_disable(DisplayChangeListener *dcl)
193{
194 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
195
196 assert(scon->opengl);
197 scon->w = 0;
198 scon->h = 0;
Gerd Hoffmanndb6cdfb2017-02-21 10:37:20 +0100199 sdl2_set_scanout_mode(scon, false);
200}
201
Gerd Hoffmannf4c36bd2017-02-21 10:37:16 +0100202void sdl2_gl_scanout_texture(DisplayChangeListener *dcl,
203 uint32_t backing_id,
204 bool backing_y_0_top,
205 uint32_t backing_width,
206 uint32_t backing_height,
207 uint32_t x, uint32_t y,
Marc-André Lureaubf41ab62023-06-06 15:56:56 +0400208 uint32_t w, uint32_t h,
209 void *d3d_tex2d)
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +0100210{
211 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
212
213 assert(scon->opengl);
214 scon->x = x;
215 scon->y = y;
216 scon->w = w;
217 scon->h = h;
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +0100218 scon->y0_top = backing_y_0_top;
219
220 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
221
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +0100222 sdl2_set_scanout_mode(scon, true);
Gerd Hoffmann74083f92017-09-27 13:50:31 +0200223 egl_fb_setup_for_tex(&scon->guest_fb, backing_width, backing_height,
224 backing_id, false);
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +0100225}
226
227void sdl2_gl_scanout_flush(DisplayChangeListener *dcl,
228 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
229{
230 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
Gerd Hoffmann371c4ef2017-06-14 10:41:48 +0200231 int ww, wh;
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +0100232
233 assert(scon->opengl);
234 if (!scon->scanout_mode) {
235 return;
236 }
Gerd Hoffmann371c4ef2017-06-14 10:41:48 +0200237 if (!scon->guest_fb.framebuffer) {
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +0100238 return;
239 }
240
241 SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
242
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +0100243 SDL_GetWindowSize(scon->real_window, &ww, &wh);
Gerd Hoffmann371c4ef2017-06-14 10:41:48 +0200244 egl_fb_setup_default(&scon->win_fb, ww, wh);
245 egl_fb_blit(&scon->win_fb, &scon->guest_fb, !scon->y0_top);
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +0100246
247 SDL_GL_SwapWindow(scon->real_window);
248}