blob: 103b954017fe8d603001dc1b948344bfb4aad022 [file] [log] [blame]
Gerd Hoffmanncd2bc882015-01-09 11:40:23 +01001/*
2 * QEMU graphical console -- opengl helper bits
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 */
Peter Maydelle16f4c82016-01-29 17:49:51 +000027#include "qemu/osdep.h"
Gerd Hoffmanncd2bc882015-01-09 11:40:23 +010028#include "ui/console.h"
29#include "ui/shader.h"
30
Gerd Hoffmanncd2bc882015-01-09 11:40:23 +010031/* ---------------------------------------------------------------------- */
32
Gerd Hoffmanncd2bc882015-01-09 11:40:23 +010033bool console_gl_check_format(DisplayChangeListener *dcl,
34 pixman_format_code_t format)
35{
36 switch (format) {
37 case PIXMAN_BE_b8g8r8x8:
38 case PIXMAN_BE_b8g8r8a8:
39 case PIXMAN_r5g6b5:
40 return true;
41 default:
42 return false;
43 }
44}
45
Gerd Hoffmann46e19e12017-10-10 15:54:49 +020046void surface_gl_create_texture(QemuGLShader *gls,
Gerd Hoffmanncd2bc882015-01-09 11:40:23 +010047 DisplaySurface *surface)
48{
49 assert(gls);
Philippe Mathieu-Daudé2e9a8562017-07-18 03:09:44 -030050 assert(QEMU_IS_ALIGNED(surface_stride(surface), surface_bytes_per_pixel(surface)));
Gerd Hoffmanncd2bc882015-01-09 11:40:23 +010051
Marc-André Lureau6cdcf882022-02-15 00:13:36 +040052 if (surface->texture) {
53 return;
54 }
55
Marc-André Lureauff174c62023-08-30 13:38:21 +040056 switch (surface_format(surface)) {
Gerd Hoffmanncd2bc882015-01-09 11:40:23 +010057 case PIXMAN_BE_b8g8r8x8:
58 case PIXMAN_BE_b8g8r8a8:
59 surface->glformat = GL_BGRA_EXT;
60 surface->gltype = GL_UNSIGNED_BYTE;
61 break;
Thomas Huth2c2311c2016-06-06 22:01:01 +020062 case PIXMAN_BE_x8r8g8b8:
63 case PIXMAN_BE_a8r8g8b8:
64 surface->glformat = GL_RGBA;
65 surface->gltype = GL_UNSIGNED_BYTE;
66 break;
Gerd Hoffmanncd2bc882015-01-09 11:40:23 +010067 case PIXMAN_r5g6b5:
68 surface->glformat = GL_RGB;
69 surface->gltype = GL_UNSIGNED_SHORT_5_6_5;
70 break;
71 default:
72 g_assert_not_reached();
73 }
74
75 glGenTextures(1, &surface->texture);
76 glEnable(GL_TEXTURE_2D);
77 glBindTexture(GL_TEXTURE_2D, surface->texture);
78 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT,
79 surface_stride(surface) / surface_bytes_per_pixel(surface));
Akihiko Odaki14c235e2021-02-19 18:48:03 +090080 if (epoxy_is_desktop_gl()) {
81 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
82 surface_width(surface),
83 surface_height(surface),
84 0, surface->glformat, surface->gltype,
85 surface_data(surface));
86 } else {
87 glTexImage2D(GL_TEXTURE_2D, 0, surface->glformat,
88 surface_width(surface),
89 surface_height(surface),
90 0, surface->glformat, surface->gltype,
91 surface_data(surface));
92 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ONE);
93 }
Gerd Hoffmanncd2bc882015-01-09 11:40:23 +010094
95 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
96 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
97}
98
Gerd Hoffmann46e19e12017-10-10 15:54:49 +020099void surface_gl_update_texture(QemuGLShader *gls,
Gerd Hoffmanncd2bc882015-01-09 11:40:23 +0100100 DisplaySurface *surface,
101 int x, int y, int w, int h)
102{
103 uint8_t *data = (void *)surface_data(surface);
104
105 assert(gls);
106
HOU Qimingbfafa472019-05-07 11:05:01 +0300107 if (surface->texture) {
108 glBindTexture(GL_TEXTURE_2D, surface->texture);
109 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT,
110 surface_stride(surface)
111 / surface_bytes_per_pixel(surface));
112 glTexSubImage2D(GL_TEXTURE_2D, 0,
113 x, y, w, h,
114 surface->glformat, surface->gltype,
115 data + surface_stride(surface) * y
116 + surface_bytes_per_pixel(surface) * x);
117 }
Gerd Hoffmanncd2bc882015-01-09 11:40:23 +0100118}
119
Gerd Hoffmann46e19e12017-10-10 15:54:49 +0200120void surface_gl_render_texture(QemuGLShader *gls,
Gerd Hoffmanncd2bc882015-01-09 11:40:23 +0100121 DisplaySurface *surface)
122{
123 assert(gls);
124
125 glClearColor(0.1f, 0.1f, 0.1f, 0.0f);
126 glClear(GL_COLOR_BUFFER_BIT);
127
Gerd Hoffmann2e1d70b2017-10-10 15:54:50 +0200128 qemu_gl_run_texture_blit(gls, false);
Gerd Hoffmanncd2bc882015-01-09 11:40:23 +0100129}
130
Gerd Hoffmann46e19e12017-10-10 15:54:49 +0200131void surface_gl_destroy_texture(QemuGLShader *gls,
Gerd Hoffmanncd2bc882015-01-09 11:40:23 +0100132 DisplaySurface *surface)
133{
134 if (!surface || !surface->texture) {
135 return;
136 }
137 glDeleteTextures(1, &surface->texture);
138 surface->texture = 0;
139}
140
Gerd Hoffmann46e19e12017-10-10 15:54:49 +0200141void surface_gl_setup_viewport(QemuGLShader *gls,
Gerd Hoffmanncd2bc882015-01-09 11:40:23 +0100142 DisplaySurface *surface,
143 int ww, int wh)
144{
145 int gw, gh, stripe;
146 float sw, sh;
147
148 assert(gls);
149
150 gw = surface_width(surface);
151 gh = surface_height(surface);
152
153 sw = (float)ww/gw;
154 sh = (float)wh/gh;
155 if (sw < sh) {
156 stripe = wh - wh*sw/sh;
157 glViewport(0, stripe / 2, ww, wh - stripe);
158 } else {
159 stripe = ww - ww*sh/sw;
160 glViewport(stripe / 2, 0, ww - stripe, wh);
161 }
162}