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 28 #include "qemu/osdep.h" 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 35 #include <epoxy/gl.h> 36 37 static void sdl2_set_scanout_mode(struct sdl2_console *scon, bool scanout) 38 { 39 if (scon->scanout_mode == scanout) { 40 return; 41 } 42 43 scon->scanout_mode = scanout; 44 if (!scon->scanout_mode) { 45 egl_fb_destroy(&scon->guest_fb); 46 if (scon->surface) { 47 surface_gl_destroy_texture(scon->gls, scon->surface); 48 surface_gl_create_texture(scon->gls, scon->surface); 49 } 50 } 51 } 52 53 static void sdl2_gl_render_surface(struct sdl2_console *scon) 54 { 55 int ww, wh; 56 57 SDL_GL_MakeCurrent(scon->real_window, scon->winctx); 58 sdl2_set_scanout_mode(scon, false); 59 60 SDL_GetWindowSize(scon->real_window, &ww, &wh); 61 surface_gl_setup_viewport(scon->gls, scon->surface, ww, wh); 62 63 surface_gl_render_texture(scon->gls, scon->surface); 64 SDL_GL_SwapWindow(scon->real_window); 65 } 66 67 void sdl2_gl_update(DisplayChangeListener *dcl, 68 int x, int y, int w, int h) 69 { 70 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl); 71 72 assert(scon->opengl); 73 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 92 if (!new_surface) { 93 console_gl_fini_context(scon->gls); 94 scon->gls = NULL; 95 sdl2_window_destroy(scon); 96 return; 97 } 98 99 if (!scon->real_window) { 100 sdl2_window_create(scon); 101 scon->gls = console_gl_init_context(); 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); 118 if (scon->updates && scon->surface) { 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 129 if (scon->surface) { 130 sdl2_gl_render_surface(scon); 131 } 132 } 133 134 QEMUGLContext sdl2_gl_create_context(DisplayChangeListener *dcl, 135 QEMUGLParams *params) 136 { 137 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl); 138 SDL_GLContext ctx; 139 140 assert(scon->opengl); 141 142 SDL_GL_MakeCurrent(scon->real_window, scon->winctx); 143 144 SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1); 145 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, 146 SDL_GL_CONTEXT_PROFILE_CORE); 147 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, params->major_ver); 148 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, params->minor_ver); 149 150 ctx = SDL_GL_CreateContext(scon->real_window); 151 return (QEMUGLContext)ctx; 152 } 153 154 void sdl2_gl_destroy_context(DisplayChangeListener *dcl, QEMUGLContext ctx) 155 { 156 SDL_GLContext sdlctx = (SDL_GLContext)ctx; 157 158 SDL_GL_DeleteContext(sdlctx); 159 } 160 161 int sdl2_gl_make_context_current(DisplayChangeListener *dcl, 162 QEMUGLContext ctx) 163 { 164 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl); 165 SDL_GLContext sdlctx = (SDL_GLContext)ctx; 166 167 assert(scon->opengl); 168 169 return SDL_GL_MakeCurrent(scon->real_window, sdlctx); 170 } 171 172 QEMUGLContext sdl2_gl_get_current_context(DisplayChangeListener *dcl) 173 { 174 SDL_GLContext sdlctx; 175 176 sdlctx = SDL_GL_GetCurrentContext(); 177 return (QEMUGLContext)sdlctx; 178 } 179 180 void sdl2_gl_scanout_disable(DisplayChangeListener *dcl) 181 { 182 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl); 183 184 assert(scon->opengl); 185 scon->w = 0; 186 scon->h = 0; 187 sdl2_set_scanout_mode(scon, false); 188 } 189 190 void sdl2_gl_scanout_texture(DisplayChangeListener *dcl, 191 uint32_t backing_id, 192 bool backing_y_0_top, 193 uint32_t backing_width, 194 uint32_t backing_height, 195 uint32_t x, uint32_t y, 196 uint32_t w, uint32_t h) 197 { 198 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl); 199 200 assert(scon->opengl); 201 scon->x = x; 202 scon->y = y; 203 scon->w = w; 204 scon->h = h; 205 scon->y0_top = backing_y_0_top; 206 207 SDL_GL_MakeCurrent(scon->real_window, scon->winctx); 208 209 sdl2_set_scanout_mode(scon, true); 210 egl_fb_setup_for_tex(&scon->guest_fb, backing_width, backing_height, 211 backing_id, false); 212 } 213 214 void sdl2_gl_scanout_flush(DisplayChangeListener *dcl, 215 uint32_t x, uint32_t y, uint32_t w, uint32_t h) 216 { 217 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl); 218 int ww, wh; 219 220 assert(scon->opengl); 221 if (!scon->scanout_mode) { 222 return; 223 } 224 if (!scon->guest_fb.framebuffer) { 225 return; 226 } 227 228 SDL_GL_MakeCurrent(scon->real_window, scon->winctx); 229 230 SDL_GetWindowSize(scon->real_window, &ww, &wh); 231 egl_fb_setup_default(&scon->win_fb, ww, wh); 232 egl_fb_blit(&scon->win_fb, &scon->guest_fb, !scon->y0_top); 233 234 SDL_GL_SwapWindow(scon->real_window); 235 } 236