Gerd Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 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 | |
Peter Maydell | e16f4c8 | 2016-01-29 17:49:51 +0000 | [diff] [blame] | 28 | #include "qemu/osdep.h" |
Gerd Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 29 | #include "ui/console.h" |
| 30 | #include "ui/input.h" |
| 31 | #include "ui/sdl2.h" |
Gerd Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 32 | |
Gerd Hoffmann | cb47dc9 | 2014-11-19 14:56:46 +0100 | [diff] [blame] | 33 | static 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 Hoffmann | 371c4ef | 2017-06-14 10:41:48 +0200 | [diff] [blame] | 41 | egl_fb_destroy(&scon->guest_fb); |
Gerd Hoffmann | cb47dc9 | 2014-11-19 14:56:46 +0100 | [diff] [blame] | 42 | 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 Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 49 | static 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 Hoffmann | cb47dc9 | 2014-11-19 14:56:46 +0100 | [diff] [blame] | 54 | sdl2_set_scanout_mode(scon, false); |
Gerd Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 55 | |
| 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 | |
| 63 | void 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é Lureau | b3a654d | 2023-05-11 11:42:17 +0400 | [diff] [blame] | 70 | if (!scon->real_window) { |
| 71 | return; |
| 72 | } |
| 73 | |
Gerd Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 74 | 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 | |
| 79 | void 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 Hoffmann | 9badf12 | 2024-06-05 15:14:42 +0200 | [diff] [blame] | 92 | if (surface_is_placeholder(new_surface) && qemu_console_get_index(dcl->con)) { |
Gerd Hoffmann | 46e19e1 | 2017-10-10 15:54:49 +0200 | [diff] [blame] | 93 | qemu_gl_fini_shader(scon->gls); |
Gerd Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 94 | scon->gls = NULL; |
| 95 | sdl2_window_destroy(scon); |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | if (!scon->real_window) { |
| 100 | sdl2_window_create(scon); |
Gerd Hoffmann | 46e19e1 | 2017-10-10 15:54:49 +0200 | [diff] [blame] | 101 | scon->gls = qemu_gl_init_shader(); |
Gerd Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 102 | } 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 | |
| 111 | void 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 Odaki | c821a58 | 2021-02-25 19:13:15 +0900 | [diff] [blame] | 118 | if (scon->updates && scon->real_window) { |
Gerd Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 119 | scon->updates = 0; |
| 120 | sdl2_gl_render_surface(scon); |
| 121 | } |
| 122 | sdl2_poll_events(scon); |
| 123 | } |
| 124 | |
| 125 | void sdl2_gl_redraw(struct sdl2_console *scon) |
| 126 | { |
| 127 | assert(scon->opengl); |
| 128 | |
Tao Wu | 77f60fb | 2018-07-26 15:59:00 -0700 | [diff] [blame] | 129 | 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 Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 134 | if (scon->surface) { |
| 135 | sdl2_gl_render_surface(scon); |
| 136 | } |
| 137 | } |
Gerd Hoffmann | cb47dc9 | 2014-11-19 14:56:46 +0100 | [diff] [blame] | 138 | |
Marc-André Lureau | 5e79d51 | 2021-10-09 23:48:46 +0400 | [diff] [blame] | 139 | QEMUGLContext sdl2_gl_create_context(DisplayGLCtx *dgc, |
Gerd Hoffmann | cb47dc9 | 2014-11-19 14:56:46 +0100 | [diff] [blame] | 140 | QEMUGLParams *params) |
| 141 | { |
Marc-André Lureau | 5e79d51 | 2021-10-09 23:48:46 +0400 | [diff] [blame] | 142 | struct sdl2_console *scon = container_of(dgc, struct sdl2_console, dgc); |
Gerd Hoffmann | cb47dc9 | 2014-11-19 14:56:46 +0100 | [diff] [blame] | 143 | 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 Armbruster | 154fd4d | 2024-09-04 13:18:25 +0200 | [diff] [blame] | 150 | if (scon->opts->gl == DISPLAY_GL_MODE_ON || |
| 151 | scon->opts->gl == DISPLAY_GL_MODE_CORE) { |
Elie Tournier | 4867e47 | 2018-04-13 14:58:42 +0100 | [diff] [blame] | 152 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, |
| 153 | SDL_GL_CONTEXT_PROFILE_CORE); |
Markus Armbruster | 154fd4d | 2024-09-04 13:18:25 +0200 | [diff] [blame] | 154 | } else if (scon->opts->gl == DISPLAY_GL_MODE_ES) { |
Elie Tournier | 4867e47 | 2018-04-13 14:58:42 +0100 | [diff] [blame] | 155 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, |
| 156 | SDL_GL_CONTEXT_PROFILE_ES); |
| 157 | } |
Gerd Hoffmann | cb47dc9 | 2014-11-19 14:56:46 +0100 | [diff] [blame] | 158 | 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 Tournier | 4867e47 | 2018-04-13 14:58:42 +0100 | [diff] [blame] | 162 | |
| 163 | /* If SDL fail to create a GL context and we use the "on" flag, |
| 164 | * then try to fallback to GLES. |
| 165 | */ |
Markus Armbruster | 154fd4d | 2024-09-04 13:18:25 +0200 | [diff] [blame] | 166 | if (!ctx && scon->opts->gl == DISPLAY_GL_MODE_ON) { |
Elie Tournier | 4867e47 | 2018-04-13 14:58:42 +0100 | [diff] [blame] | 167 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, |
| 168 | SDL_GL_CONTEXT_PROFILE_ES); |
| 169 | ctx = SDL_GL_CreateContext(scon->real_window); |
| 170 | } |
Gerd Hoffmann | cb47dc9 | 2014-11-19 14:56:46 +0100 | [diff] [blame] | 171 | return (QEMUGLContext)ctx; |
| 172 | } |
| 173 | |
Marc-André Lureau | 5e79d51 | 2021-10-09 23:48:46 +0400 | [diff] [blame] | 174 | void sdl2_gl_destroy_context(DisplayGLCtx *dgc, QEMUGLContext ctx) |
Gerd Hoffmann | cb47dc9 | 2014-11-19 14:56:46 +0100 | [diff] [blame] | 175 | { |
| 176 | SDL_GLContext sdlctx = (SDL_GLContext)ctx; |
| 177 | |
| 178 | SDL_GL_DeleteContext(sdlctx); |
| 179 | } |
| 180 | |
Marc-André Lureau | 5e79d51 | 2021-10-09 23:48:46 +0400 | [diff] [blame] | 181 | int sdl2_gl_make_context_current(DisplayGLCtx *dgc, |
Gerd Hoffmann | cb47dc9 | 2014-11-19 14:56:46 +0100 | [diff] [blame] | 182 | QEMUGLContext ctx) |
| 183 | { |
Marc-André Lureau | 5e79d51 | 2021-10-09 23:48:46 +0400 | [diff] [blame] | 184 | struct sdl2_console *scon = container_of(dgc, struct sdl2_console, dgc); |
Gerd Hoffmann | cb47dc9 | 2014-11-19 14:56:46 +0100 | [diff] [blame] | 185 | SDL_GLContext sdlctx = (SDL_GLContext)ctx; |
| 186 | |
| 187 | assert(scon->opengl); |
| 188 | |
| 189 | return SDL_GL_MakeCurrent(scon->real_window, sdlctx); |
| 190 | } |
| 191 | |
Gerd Hoffmann | db6cdfb | 2017-02-21 10:37:20 +0100 | [diff] [blame] | 192 | void 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 Hoffmann | db6cdfb | 2017-02-21 10:37:20 +0100 | [diff] [blame] | 199 | sdl2_set_scanout_mode(scon, false); |
| 200 | } |
| 201 | |
Gerd Hoffmann | f4c36bd | 2017-02-21 10:37:16 +0100 | [diff] [blame] | 202 | void 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é Lureau | bf41ab6 | 2023-06-06 15:56:56 +0400 | [diff] [blame] | 208 | uint32_t w, uint32_t h, |
| 209 | void *d3d_tex2d) |
Gerd Hoffmann | cb47dc9 | 2014-11-19 14:56:46 +0100 | [diff] [blame] | 210 | { |
| 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 Hoffmann | cb47dc9 | 2014-11-19 14:56:46 +0100 | [diff] [blame] | 218 | scon->y0_top = backing_y_0_top; |
| 219 | |
| 220 | SDL_GL_MakeCurrent(scon->real_window, scon->winctx); |
| 221 | |
Gerd Hoffmann | cb47dc9 | 2014-11-19 14:56:46 +0100 | [diff] [blame] | 222 | sdl2_set_scanout_mode(scon, true); |
Gerd Hoffmann | 74083f9 | 2017-09-27 13:50:31 +0200 | [diff] [blame] | 223 | egl_fb_setup_for_tex(&scon->guest_fb, backing_width, backing_height, |
| 224 | backing_id, false); |
Gerd Hoffmann | cb47dc9 | 2014-11-19 14:56:46 +0100 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | void 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 Hoffmann | 371c4ef | 2017-06-14 10:41:48 +0200 | [diff] [blame] | 231 | int ww, wh; |
Gerd Hoffmann | cb47dc9 | 2014-11-19 14:56:46 +0100 | [diff] [blame] | 232 | |
| 233 | assert(scon->opengl); |
| 234 | if (!scon->scanout_mode) { |
| 235 | return; |
| 236 | } |
Gerd Hoffmann | 371c4ef | 2017-06-14 10:41:48 +0200 | [diff] [blame] | 237 | if (!scon->guest_fb.framebuffer) { |
Gerd Hoffmann | cb47dc9 | 2014-11-19 14:56:46 +0100 | [diff] [blame] | 238 | return; |
| 239 | } |
| 240 | |
| 241 | SDL_GL_MakeCurrent(scon->real_window, scon->winctx); |
| 242 | |
Gerd Hoffmann | cb47dc9 | 2014-11-19 14:56:46 +0100 | [diff] [blame] | 243 | SDL_GetWindowSize(scon->real_window, &ww, &wh); |
Gerd Hoffmann | 371c4ef | 2017-06-14 10:41:48 +0200 | [diff] [blame] | 244 | egl_fb_setup_default(&scon->win_fb, ww, wh); |
| 245 | egl_fb_blit(&scon->win_fb, &scon->guest_fb, !scon->y0_top); |
Gerd Hoffmann | cb47dc9 | 2014-11-19 14:56:46 +0100 | [diff] [blame] | 246 | |
| 247 | SDL_GL_SwapWindow(scon->real_window); |
| 248 | } |