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 "qemu-common.h" |
| 30 | #include "ui/console.h" |
| 31 | #include "ui/input.h" |
| 32 | #include "ui/sdl2.h" |
| 33 | #include "sysemu/sysemu.h" |
| 34 | |
Gerd Hoffmann | cb47dc9 | 2014-11-19 14:56:46 +0100 | [diff] [blame] | 35 | static 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) { |
Gerd Hoffmann | 371c4ef | 2017-06-14 10:41:48 +0200 | [diff] [blame] | 43 | egl_fb_destroy(&scon->guest_fb); |
Gerd Hoffmann | cb47dc9 | 2014-11-19 14:56:46 +0100 | [diff] [blame] | 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 | |
Gerd Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 51 | static void sdl2_gl_render_surface(struct sdl2_console *scon) |
| 52 | { |
| 53 | int ww, wh; |
| 54 | |
| 55 | SDL_GL_MakeCurrent(scon->real_window, scon->winctx); |
Gerd Hoffmann | cb47dc9 | 2014-11-19 14:56:46 +0100 | [diff] [blame] | 56 | sdl2_set_scanout_mode(scon, false); |
Gerd Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 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 | |
| 65 | void 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 | |
| 77 | void 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) { |
Gerd Hoffmann | 46e19e1 | 2017-10-10 15:54:49 +0200 | [diff] [blame] | 91 | qemu_gl_fini_shader(scon->gls); |
Gerd Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 92 | scon->gls = NULL; |
| 93 | sdl2_window_destroy(scon); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | if (!scon->real_window) { |
| 98 | sdl2_window_create(scon); |
Gerd Hoffmann | 46e19e1 | 2017-10-10 15:54:49 +0200 | [diff] [blame] | 99 | scon->gls = qemu_gl_init_shader(); |
Gerd Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 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 | |
| 109 | void 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 | |
| 123 | void 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 | } |
Gerd Hoffmann | cb47dc9 | 2014-11-19 14:56:46 +0100 | [diff] [blame] | 131 | |
| 132 | QEMUGLContext 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); |
Elie Tournier | 4867e47 | 2018-04-13 14:58:42 +0100 | [diff] [blame] | 143 | if (scon->opts->gl == DISPLAYGL_MODE_ON || |
| 144 | scon->opts->gl == DISPLAYGL_MODE_CORE) { |
| 145 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, |
| 146 | SDL_GL_CONTEXT_PROFILE_CORE); |
| 147 | } else if (scon->opts->gl == DISPLAYGL_MODE_ES) { |
| 148 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, |
| 149 | SDL_GL_CONTEXT_PROFILE_ES); |
| 150 | } |
Gerd Hoffmann | cb47dc9 | 2014-11-19 14:56:46 +0100 | [diff] [blame] | 151 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, params->major_ver); |
| 152 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, params->minor_ver); |
| 153 | |
| 154 | ctx = SDL_GL_CreateContext(scon->real_window); |
Elie Tournier | 4867e47 | 2018-04-13 14:58:42 +0100 | [diff] [blame] | 155 | |
| 156 | /* If SDL fail to create a GL context and we use the "on" flag, |
| 157 | * then try to fallback to GLES. |
| 158 | */ |
| 159 | if (!ctx && scon->opts->gl == DISPLAYGL_MODE_ON) { |
| 160 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, |
| 161 | SDL_GL_CONTEXT_PROFILE_ES); |
| 162 | ctx = SDL_GL_CreateContext(scon->real_window); |
| 163 | } |
Gerd Hoffmann | cb47dc9 | 2014-11-19 14:56:46 +0100 | [diff] [blame] | 164 | return (QEMUGLContext)ctx; |
| 165 | } |
| 166 | |
| 167 | void sdl2_gl_destroy_context(DisplayChangeListener *dcl, QEMUGLContext ctx) |
| 168 | { |
| 169 | SDL_GLContext sdlctx = (SDL_GLContext)ctx; |
| 170 | |
| 171 | SDL_GL_DeleteContext(sdlctx); |
| 172 | } |
| 173 | |
| 174 | int sdl2_gl_make_context_current(DisplayChangeListener *dcl, |
| 175 | QEMUGLContext ctx) |
| 176 | { |
| 177 | struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl); |
| 178 | SDL_GLContext sdlctx = (SDL_GLContext)ctx; |
| 179 | |
| 180 | assert(scon->opengl); |
| 181 | |
| 182 | return SDL_GL_MakeCurrent(scon->real_window, sdlctx); |
| 183 | } |
| 184 | |
| 185 | QEMUGLContext sdl2_gl_get_current_context(DisplayChangeListener *dcl) |
| 186 | { |
| 187 | SDL_GLContext sdlctx; |
| 188 | |
| 189 | sdlctx = SDL_GL_GetCurrentContext(); |
| 190 | return (QEMUGLContext)sdlctx; |
| 191 | } |
| 192 | |
Gerd Hoffmann | db6cdfb | 2017-02-21 10:37:20 +0100 | [diff] [blame] | 193 | void sdl2_gl_scanout_disable(DisplayChangeListener *dcl) |
| 194 | { |
| 195 | struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl); |
| 196 | |
| 197 | assert(scon->opengl); |
| 198 | scon->w = 0; |
| 199 | scon->h = 0; |
Gerd Hoffmann | db6cdfb | 2017-02-21 10:37:20 +0100 | [diff] [blame] | 200 | sdl2_set_scanout_mode(scon, false); |
| 201 | } |
| 202 | |
Gerd Hoffmann | f4c36bd | 2017-02-21 10:37:16 +0100 | [diff] [blame] | 203 | void sdl2_gl_scanout_texture(DisplayChangeListener *dcl, |
| 204 | uint32_t backing_id, |
| 205 | bool backing_y_0_top, |
| 206 | uint32_t backing_width, |
| 207 | uint32_t backing_height, |
| 208 | uint32_t x, uint32_t y, |
| 209 | uint32_t w, uint32_t h) |
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 | } |