2014-11-11 18:54:45 +03:00
|
|
|
/*
|
|
|
|
* QEMU SDL display driver -- opengl support
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 Red Hat
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Gerd Hoffmann <kraxel@redhat.com>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2016-01-29 20:49:51 +03:00
|
|
|
#include "qemu/osdep.h"
|
2014-11-11 18:54:45 +03:00
|
|
|
#include "ui/console.h"
|
|
|
|
#include "ui/input.h"
|
|
|
|
#include "ui/sdl2.h"
|
|
|
|
|
2014-11-19 16:56:46 +03:00
|
|
|
static void sdl2_set_scanout_mode(struct sdl2_console *scon, bool scanout)
|
|
|
|
{
|
|
|
|
if (scon->scanout_mode == scanout) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
scon->scanout_mode = scanout;
|
|
|
|
if (!scon->scanout_mode) {
|
2017-06-14 11:41:48 +03:00
|
|
|
egl_fb_destroy(&scon->guest_fb);
|
2014-11-19 16:56:46 +03:00
|
|
|
if (scon->surface) {
|
|
|
|
surface_gl_destroy_texture(scon->gls, scon->surface);
|
|
|
|
surface_gl_create_texture(scon->gls, scon->surface);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-11 18:54:45 +03:00
|
|
|
static void sdl2_gl_render_surface(struct sdl2_console *scon)
|
|
|
|
{
|
|
|
|
int ww, wh;
|
|
|
|
|
|
|
|
SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
|
2014-11-19 16:56:46 +03:00
|
|
|
sdl2_set_scanout_mode(scon, false);
|
2014-11-11 18:54:45 +03:00
|
|
|
|
|
|
|
SDL_GetWindowSize(scon->real_window, &ww, &wh);
|
|
|
|
surface_gl_setup_viewport(scon->gls, scon->surface, ww, wh);
|
|
|
|
|
|
|
|
surface_gl_render_texture(scon->gls, scon->surface);
|
|
|
|
SDL_GL_SwapWindow(scon->real_window);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sdl2_gl_update(DisplayChangeListener *dcl,
|
|
|
|
int x, int y, int w, int h)
|
|
|
|
{
|
|
|
|
struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
|
|
|
|
|
|
|
|
assert(scon->opengl);
|
|
|
|
|
2023-05-11 10:42:17 +03:00
|
|
|
if (!scon->real_window) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-11 18:54:45 +03:00
|
|
|
SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
|
|
|
|
surface_gl_update_texture(scon->gls, scon->surface, x, y, w, h);
|
|
|
|
scon->updates++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sdl2_gl_switch(DisplayChangeListener *dcl,
|
|
|
|
DisplaySurface *new_surface)
|
|
|
|
{
|
|
|
|
struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
|
|
|
|
DisplaySurface *old_surface = scon->surface;
|
|
|
|
|
|
|
|
assert(scon->opengl);
|
|
|
|
|
|
|
|
SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
|
|
|
|
surface_gl_destroy_texture(scon->gls, scon->surface);
|
|
|
|
|
|
|
|
scon->surface = new_surface;
|
|
|
|
|
2024-06-05 16:14:42 +03:00
|
|
|
if (surface_is_placeholder(new_surface) && qemu_console_get_index(dcl->con)) {
|
2017-10-10 16:54:49 +03:00
|
|
|
qemu_gl_fini_shader(scon->gls);
|
2014-11-11 18:54:45 +03:00
|
|
|
scon->gls = NULL;
|
|
|
|
sdl2_window_destroy(scon);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!scon->real_window) {
|
|
|
|
sdl2_window_create(scon);
|
2017-10-10 16:54:49 +03:00
|
|
|
scon->gls = qemu_gl_init_shader();
|
2014-11-11 18:54:45 +03:00
|
|
|
} else if (old_surface &&
|
|
|
|
((surface_width(old_surface) != surface_width(new_surface)) ||
|
|
|
|
(surface_height(old_surface) != surface_height(new_surface)))) {
|
|
|
|
sdl2_window_resize(scon);
|
|
|
|
}
|
|
|
|
|
|
|
|
surface_gl_create_texture(scon->gls, scon->surface);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sdl2_gl_refresh(DisplayChangeListener *dcl)
|
|
|
|
{
|
|
|
|
struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
|
|
|
|
|
|
|
|
assert(scon->opengl);
|
|
|
|
|
|
|
|
graphic_hw_update(dcl->con);
|
2021-02-25 13:13:15 +03:00
|
|
|
if (scon->updates && scon->real_window) {
|
2014-11-11 18:54:45 +03:00
|
|
|
scon->updates = 0;
|
|
|
|
sdl2_gl_render_surface(scon);
|
|
|
|
}
|
|
|
|
sdl2_poll_events(scon);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sdl2_gl_redraw(struct sdl2_console *scon)
|
|
|
|
{
|
|
|
|
assert(scon->opengl);
|
|
|
|
|
2018-07-27 01:59:00 +03:00
|
|
|
if (scon->scanout_mode) {
|
|
|
|
/* sdl2_gl_scanout_flush actually only care about
|
|
|
|
* the first argument. */
|
|
|
|
return sdl2_gl_scanout_flush(&scon->dcl, 0, 0, 0, 0);
|
|
|
|
}
|
2014-11-11 18:54:45 +03:00
|
|
|
if (scon->surface) {
|
|
|
|
sdl2_gl_render_surface(scon);
|
|
|
|
}
|
|
|
|
}
|
2014-11-19 16:56:46 +03:00
|
|
|
|
2021-10-09 22:48:46 +03:00
|
|
|
QEMUGLContext sdl2_gl_create_context(DisplayGLCtx *dgc,
|
2014-11-19 16:56:46 +03:00
|
|
|
QEMUGLParams *params)
|
|
|
|
{
|
2021-10-09 22:48:46 +03:00
|
|
|
struct sdl2_console *scon = container_of(dgc, struct sdl2_console, dgc);
|
2014-11-19 16:56:46 +03:00
|
|
|
SDL_GLContext ctx;
|
|
|
|
|
|
|
|
assert(scon->opengl);
|
|
|
|
|
|
|
|
SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
|
|
|
|
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
|
2024-09-04 14:18:25 +03:00
|
|
|
if (scon->opts->gl == DISPLAY_GL_MODE_ON ||
|
|
|
|
scon->opts->gl == DISPLAY_GL_MODE_CORE) {
|
2018-04-13 16:58:42 +03:00
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
|
|
|
|
SDL_GL_CONTEXT_PROFILE_CORE);
|
2024-09-04 14:18:25 +03:00
|
|
|
} else if (scon->opts->gl == DISPLAY_GL_MODE_ES) {
|
2018-04-13 16:58:42 +03:00
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
|
|
|
|
SDL_GL_CONTEXT_PROFILE_ES);
|
|
|
|
}
|
2014-11-19 16:56:46 +03:00
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, params->major_ver);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, params->minor_ver);
|
|
|
|
|
|
|
|
ctx = SDL_GL_CreateContext(scon->real_window);
|
2018-04-13 16:58:42 +03:00
|
|
|
|
|
|
|
/* If SDL fail to create a GL context and we use the "on" flag,
|
|
|
|
* then try to fallback to GLES.
|
|
|
|
*/
|
2024-09-04 14:18:25 +03:00
|
|
|
if (!ctx && scon->opts->gl == DISPLAY_GL_MODE_ON) {
|
2018-04-13 16:58:42 +03:00
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
|
|
|
|
SDL_GL_CONTEXT_PROFILE_ES);
|
|
|
|
ctx = SDL_GL_CreateContext(scon->real_window);
|
|
|
|
}
|
2014-11-19 16:56:46 +03:00
|
|
|
return (QEMUGLContext)ctx;
|
|
|
|
}
|
|
|
|
|
2021-10-09 22:48:46 +03:00
|
|
|
void sdl2_gl_destroy_context(DisplayGLCtx *dgc, QEMUGLContext ctx)
|
2014-11-19 16:56:46 +03:00
|
|
|
{
|
|
|
|
SDL_GLContext sdlctx = (SDL_GLContext)ctx;
|
|
|
|
|
|
|
|
SDL_GL_DeleteContext(sdlctx);
|
|
|
|
}
|
|
|
|
|
2021-10-09 22:48:46 +03:00
|
|
|
int sdl2_gl_make_context_current(DisplayGLCtx *dgc,
|
2014-11-19 16:56:46 +03:00
|
|
|
QEMUGLContext ctx)
|
|
|
|
{
|
2021-10-09 22:48:46 +03:00
|
|
|
struct sdl2_console *scon = container_of(dgc, struct sdl2_console, dgc);
|
2014-11-19 16:56:46 +03:00
|
|
|
SDL_GLContext sdlctx = (SDL_GLContext)ctx;
|
|
|
|
|
|
|
|
assert(scon->opengl);
|
|
|
|
|
|
|
|
return SDL_GL_MakeCurrent(scon->real_window, sdlctx);
|
|
|
|
}
|
|
|
|
|
2017-02-21 12:37:20 +03:00
|
|
|
void sdl2_gl_scanout_disable(DisplayChangeListener *dcl)
|
|
|
|
{
|
|
|
|
struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
|
|
|
|
|
|
|
|
assert(scon->opengl);
|
|
|
|
scon->w = 0;
|
|
|
|
scon->h = 0;
|
|
|
|
sdl2_set_scanout_mode(scon, false);
|
|
|
|
}
|
|
|
|
|
2017-02-21 12:37:16 +03:00
|
|
|
void sdl2_gl_scanout_texture(DisplayChangeListener *dcl,
|
|
|
|
uint32_t backing_id,
|
|
|
|
bool backing_y_0_top,
|
|
|
|
uint32_t backing_width,
|
|
|
|
uint32_t backing_height,
|
|
|
|
uint32_t x, uint32_t y,
|
2023-06-06 14:56:56 +03:00
|
|
|
uint32_t w, uint32_t h,
|
|
|
|
void *d3d_tex2d)
|
2014-11-19 16:56:46 +03:00
|
|
|
{
|
|
|
|
struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
|
|
|
|
|
|
|
|
assert(scon->opengl);
|
|
|
|
scon->x = x;
|
|
|
|
scon->y = y;
|
|
|
|
scon->w = w;
|
|
|
|
scon->h = h;
|
|
|
|
scon->y0_top = backing_y_0_top;
|
|
|
|
|
|
|
|
SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
|
|
|
|
|
|
|
|
sdl2_set_scanout_mode(scon, true);
|
2017-09-27 14:50:31 +03:00
|
|
|
egl_fb_setup_for_tex(&scon->guest_fb, backing_width, backing_height,
|
|
|
|
backing_id, false);
|
2014-11-19 16:56:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void sdl2_gl_scanout_flush(DisplayChangeListener *dcl,
|
|
|
|
uint32_t x, uint32_t y, uint32_t w, uint32_t h)
|
|
|
|
{
|
|
|
|
struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
|
2017-06-14 11:41:48 +03:00
|
|
|
int ww, wh;
|
2014-11-19 16:56:46 +03:00
|
|
|
|
|
|
|
assert(scon->opengl);
|
|
|
|
if (!scon->scanout_mode) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-14 11:41:48 +03:00
|
|
|
if (!scon->guest_fb.framebuffer) {
|
2014-11-19 16:56:46 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
|
|
|
|
|
|
|
|
SDL_GetWindowSize(scon->real_window, &ww, &wh);
|
2017-06-14 11:41:48 +03:00
|
|
|
egl_fb_setup_default(&scon->win_fb, ww, wh);
|
|
|
|
egl_fb_blit(&scon->win_fb, &scon->guest_fb, !scon->y0_top);
|
2014-11-19 16:56:46 +03:00
|
|
|
|
|
|
|
SDL_GL_SwapWindow(scon->real_window);
|
|
|
|
}
|