qemu/ui/console-gl.c
Vivek Kasireddy 2103690b1a ui/spice: Create a new texture with linear layout when gl=on is specified
Since most encoders/decoders (invoked by Spice) may not work properly
with tiled memory associated with a texture, we need to create another
texture that has linear memory layout and use that instead.

Note that, there does not seem to be a direct way to indicate to the
GL implementation that a texture's backing memory needs to be linear.
Instead, we have to do it in a roundabout way where we need to first
create a tiled texture and import that as a memory object to create
a new texture that has a linear memory layout.

Cc: Gerd Hoffmann <kraxel@redhat.com>
Cc: Marc-André Lureau <marcandre.lureau@redhat.com>
Cc: Dmitry Osipenko <dmitry.osipenko@collabora.com>
Cc: Frediano Ziglio <freddy77@gmail.com>
Cc: Dongwon Kim <dongwon.kim@intel.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Co-developed-by: Michael Scherle <michael.scherle@rz.uni-freiburg.de>
Signed-off-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
Reviewed-by: Dmitry Osipenko <dmitry.osipenko@collabora.com>
Message-Id: <20250617043546.1022779-7-vivek.kasireddy@intel.com>
2025-07-15 10:22:33 +04:00

217 lines
6.8 KiB
C

/*
* QEMU graphical console -- opengl helper bits
*
* 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 "qemu/error-report.h"
#include "ui/console.h"
#include "ui/shader.h"
/* ---------------------------------------------------------------------- */
bool console_gl_check_format(DisplayChangeListener *dcl,
pixman_format_code_t format)
{
switch (format) {
case PIXMAN_BE_b8g8r8x8:
case PIXMAN_BE_b8g8r8a8:
case PIXMAN_r5g6b5:
return true;
default:
return false;
}
}
void surface_gl_create_texture(QemuGLShader *gls,
DisplaySurface *surface)
{
assert(gls);
assert(QEMU_IS_ALIGNED(surface_stride(surface), surface_bytes_per_pixel(surface)));
if (surface->texture) {
return;
}
switch (surface_format(surface)) {
case PIXMAN_BE_b8g8r8x8:
case PIXMAN_BE_b8g8r8a8:
surface->glformat = GL_BGRA_EXT;
surface->gltype = GL_UNSIGNED_BYTE;
break;
case PIXMAN_BE_x8r8g8b8:
case PIXMAN_BE_a8r8g8b8:
surface->glformat = GL_RGBA;
surface->gltype = GL_UNSIGNED_BYTE;
break;
case PIXMAN_r5g6b5:
surface->glformat = GL_RGB;
surface->gltype = GL_UNSIGNED_SHORT_5_6_5;
break;
default:
g_assert_not_reached();
}
glGenTextures(1, &surface->texture);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, surface->texture);
glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT,
surface_stride(surface) / surface_bytes_per_pixel(surface));
if (epoxy_is_desktop_gl()) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
surface_width(surface),
surface_height(surface),
0, surface->glformat, surface->gltype,
surface_data(surface));
} else {
glTexImage2D(GL_TEXTURE_2D, 0, surface->glformat,
surface_width(surface),
surface_height(surface),
0, surface->glformat, surface->gltype,
surface_data(surface));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ONE);
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
bool surface_gl_create_texture_from_fd(DisplaySurface *surface,
int fd, GLuint *texture,
GLuint *mem_obj)
{
unsigned long size = surface_stride(surface) * surface_height(surface);
GLenum err = glGetError();
*texture = 0;
*mem_obj = 0;
if (!epoxy_has_gl_extension("GL_EXT_memory_object") ||
!epoxy_has_gl_extension("GL_EXT_memory_object_fd")) {
error_report("spice: required OpenGL extensions not supported: "
"GL_EXT_memory_object and GL_EXT_memory_object_fd");
return false;
}
#ifdef GL_EXT_memory_object_fd
glCreateMemoryObjectsEXT(1, mem_obj);
glImportMemoryFdEXT(*mem_obj, size, GL_HANDLE_TYPE_OPAQUE_FD_EXT, fd);
err = glGetError();
if (err != GL_NO_ERROR) {
error_report("spice: cannot import memory object from fd");
goto cleanup_mem;
}
glGenTextures(1, texture);
glBindTexture(GL_TEXTURE_2D, *texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_TILING_EXT, GL_LINEAR_TILING_EXT);
glTexStorageMem2DEXT(GL_TEXTURE_2D, 1, GL_RGBA8, surface_width(surface),
surface_height(surface), *mem_obj, 0);
err = glGetError();
if (err != GL_NO_ERROR) {
error_report("spice: cannot create texture from memory object");
goto cleanup_tex_and_mem;
}
return true;
cleanup_tex_and_mem:
glDeleteTextures(1, texture);
cleanup_mem:
glDeleteMemoryObjectsEXT(1, mem_obj);
#endif
return false;
}
void surface_gl_update_texture(QemuGLShader *gls,
DisplaySurface *surface,
int x, int y, int w, int h)
{
uint8_t *data = (void *)surface_data(surface);
assert(gls);
if (surface->texture) {
glBindTexture(GL_TEXTURE_2D, surface->texture);
glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT,
surface_stride(surface)
/ surface_bytes_per_pixel(surface));
glTexSubImage2D(GL_TEXTURE_2D, 0,
x, y, w, h,
surface->glformat, surface->gltype,
data + surface_stride(surface) * y
+ surface_bytes_per_pixel(surface) * x);
}
}
void surface_gl_render_texture(QemuGLShader *gls,
DisplaySurface *surface)
{
assert(gls);
glClearColor(0.1f, 0.1f, 0.1f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
qemu_gl_run_texture_blit(gls, false);
}
void surface_gl_destroy_texture(QemuGLShader *gls,
DisplaySurface *surface)
{
if (!surface || !surface->texture) {
return;
}
glDeleteTextures(1, &surface->texture);
surface->texture = 0;
#ifdef GL_EXT_memory_object_fd
if (surface->mem_obj) {
glDeleteMemoryObjectsEXT(1, &surface->mem_obj);
surface->mem_obj = 0;
}
#endif
}
void surface_gl_setup_viewport(QemuGLShader *gls,
DisplaySurface *surface,
int ww, int wh)
{
int gw, gh, stripe;
float sw, sh;
assert(gls);
gw = surface_width(surface);
gh = surface_height(surface);
sw = (float)ww/gw;
sh = (float)wh/gh;
if (sw < sh) {
stripe = wh - wh*sw/sh;
glViewport(0, stripe / 2, ww, wh - stripe);
} else {
stripe = ww - ww*sh/sw;
glViewport(stripe / 2, 0, ww - stripe, wh);
}
}