qemu/ui/sdl2-gl.c
Akihiko Odaki c821a58ee7 ui/console: Pass placeholder surface to displays
ui/console used to accept NULL as graphic console surface, but its
semantics was inconsistent among displays:
- cocoa and gtk-egl perform NULL dereference.
- egl-headless, spice and spice-egl do nothing.
- gtk releases underlying resources.
- sdl2-2d and sdl2-gl destroys the window.
- vnc shows a message, "Display output is not active."

Fortunately, only virtio-gpu and virtio-gpu-3d assign NULL so
we can study them to figure out the desired behavior. They assign
NULL *except* for the primary display when the device is realized,
reset, or its scanout is disabled. This effectively destroys
windows for the (uninitialized) secondary displays.

To implement the consistent behavior of display device
realization/reset, this change embeds it to the operation
switching the surface. When NULL was given as a new surface when
switching, ui/console will instead passes a placeholder down
to each display listeners.

sdl destroys the window for a secondary console if its surface is a
placeholder. The other displays simply shows the placeholder.

Signed-off-by: Akihiko Odaki <akihiko.odaki@gmail.com>
Message-Id: <20210225101316.83940-2-akihiko.odaki@gmail.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2021-03-04 09:35:36 +01:00

245 lines
7.6 KiB
C

/*
* 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.
*/
#include "qemu/osdep.h"
#include "ui/console.h"
#include "ui/input.h"
#include "ui/sdl2.h"
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) {
egl_fb_destroy(&scon->guest_fb);
if (scon->surface) {
surface_gl_destroy_texture(scon->gls, scon->surface);
surface_gl_create_texture(scon->gls, scon->surface);
}
}
}
static void sdl2_gl_render_surface(struct sdl2_console *scon)
{
int ww, wh;
SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
sdl2_set_scanout_mode(scon, false);
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);
graphic_hw_gl_flushed(scon->dcl.con);
}
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);
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;
if (is_placeholder(new_surface) && qemu_console_get_index(dcl->con)) {
qemu_gl_fini_shader(scon->gls);
scon->gls = NULL;
sdl2_window_destroy(scon);
return;
}
if (!scon->real_window) {
sdl2_window_create(scon);
scon->gls = qemu_gl_init_shader();
} 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);
if (scon->updates && scon->real_window) {
scon->updates = 0;
sdl2_gl_render_surface(scon);
}
sdl2_poll_events(scon);
}
void sdl2_gl_redraw(struct sdl2_console *scon)
{
assert(scon->opengl);
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);
}
if (scon->surface) {
sdl2_gl_render_surface(scon);
}
}
QEMUGLContext sdl2_gl_create_context(DisplayChangeListener *dcl,
QEMUGLParams *params)
{
struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
SDL_GLContext ctx;
assert(scon->opengl);
SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
if (scon->opts->gl == DISPLAYGL_MODE_ON ||
scon->opts->gl == DISPLAYGL_MODE_CORE) {
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
SDL_GL_CONTEXT_PROFILE_CORE);
} else if (scon->opts->gl == DISPLAYGL_MODE_ES) {
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
SDL_GL_CONTEXT_PROFILE_ES);
}
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);
/* If SDL fail to create a GL context and we use the "on" flag,
* then try to fallback to GLES.
*/
if (!ctx && scon->opts->gl == DISPLAYGL_MODE_ON) {
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
SDL_GL_CONTEXT_PROFILE_ES);
ctx = SDL_GL_CreateContext(scon->real_window);
}
return (QEMUGLContext)ctx;
}
void sdl2_gl_destroy_context(DisplayChangeListener *dcl, QEMUGLContext ctx)
{
SDL_GLContext sdlctx = (SDL_GLContext)ctx;
SDL_GL_DeleteContext(sdlctx);
}
int sdl2_gl_make_context_current(DisplayChangeListener *dcl,
QEMUGLContext ctx)
{
struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
SDL_GLContext sdlctx = (SDL_GLContext)ctx;
assert(scon->opengl);
return SDL_GL_MakeCurrent(scon->real_window, sdlctx);
}
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);
}
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,
uint32_t w, uint32_t h)
{
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);
egl_fb_setup_for_tex(&scon->guest_fb, backing_width, backing_height,
backing_id, false);
}
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);
int ww, wh;
assert(scon->opengl);
if (!scon->scanout_mode) {
return;
}
if (!scon->guest_fb.framebuffer) {
return;
}
SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
SDL_GetWindowSize(scon->real_window, &ww, &wh);
egl_fb_setup_default(&scon->win_fb, ww, wh);
egl_fb_blit(&scon->win_fb, &scon->guest_fb, !scon->y0_top);
SDL_GL_SwapWindow(scon->real_window);
graphic_hw_gl_flushed(dcl->con);
}