xref: /openbmc/qemu/ui/console-gl.c (revision d177892d)
1 /*
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  */
27 #include "qemu/osdep.h"
28 #include "ui/console.h"
29 #include "ui/shader.h"
30 
31 /* ---------------------------------------------------------------------- */
32 
33 bool 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 
46 void surface_gl_create_texture(QemuGLShader *gls,
47                                DisplaySurface *surface)
48 {
49     assert(gls);
50     assert(QEMU_IS_ALIGNED(surface_stride(surface), surface_bytes_per_pixel(surface)));
51 
52     switch (surface->format) {
53     case PIXMAN_BE_b8g8r8x8:
54     case PIXMAN_BE_b8g8r8a8:
55         surface->glformat = GL_BGRA_EXT;
56         surface->gltype = GL_UNSIGNED_BYTE;
57         break;
58     case PIXMAN_BE_x8r8g8b8:
59     case PIXMAN_BE_a8r8g8b8:
60         surface->glformat = GL_RGBA;
61         surface->gltype = GL_UNSIGNED_BYTE;
62         break;
63     case PIXMAN_r5g6b5:
64         surface->glformat = GL_RGB;
65         surface->gltype = GL_UNSIGNED_SHORT_5_6_5;
66         break;
67     default:
68         g_assert_not_reached();
69     }
70 
71     glGenTextures(1, &surface->texture);
72     glEnable(GL_TEXTURE_2D);
73     glBindTexture(GL_TEXTURE_2D, surface->texture);
74     glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT,
75                   surface_stride(surface) / surface_bytes_per_pixel(surface));
76     if (epoxy_is_desktop_gl()) {
77         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
78                      surface_width(surface),
79                      surface_height(surface),
80                      0, surface->glformat, surface->gltype,
81                      surface_data(surface));
82     } else {
83         glTexImage2D(GL_TEXTURE_2D, 0, surface->glformat,
84                      surface_width(surface),
85                      surface_height(surface),
86                      0, surface->glformat, surface->gltype,
87                      surface_data(surface));
88         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ONE);
89     }
90 
91     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
92     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
93 }
94 
95 void surface_gl_update_texture(QemuGLShader *gls,
96                                DisplaySurface *surface,
97                                int x, int y, int w, int h)
98 {
99     uint8_t *data = (void *)surface_data(surface);
100 
101     assert(gls);
102 
103     if (surface->texture) {
104         glBindTexture(GL_TEXTURE_2D, surface->texture);
105         glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT,
106                       surface_stride(surface)
107                       / surface_bytes_per_pixel(surface));
108         glTexSubImage2D(GL_TEXTURE_2D, 0,
109                         x, y, w, h,
110                         surface->glformat, surface->gltype,
111                         data + surface_stride(surface) * y
112                         + surface_bytes_per_pixel(surface) * x);
113     }
114 }
115 
116 void surface_gl_render_texture(QemuGLShader *gls,
117                                DisplaySurface *surface)
118 {
119     assert(gls);
120 
121     glClearColor(0.1f, 0.1f, 0.1f, 0.0f);
122     glClear(GL_COLOR_BUFFER_BIT);
123 
124     qemu_gl_run_texture_blit(gls, false);
125 }
126 
127 void surface_gl_destroy_texture(QemuGLShader *gls,
128                                 DisplaySurface *surface)
129 {
130     if (!surface || !surface->texture) {
131         return;
132     }
133     glDeleteTextures(1, &surface->texture);
134     surface->texture = 0;
135 }
136 
137 void surface_gl_setup_viewport(QemuGLShader *gls,
138                                DisplaySurface *surface,
139                                int ww, int wh)
140 {
141     int gw, gh, stripe;
142     float sw, sh;
143 
144     assert(gls);
145 
146     gw = surface_width(surface);
147     gh = surface_height(surface);
148 
149     sw = (float)ww/gw;
150     sh = (float)wh/gh;
151     if (sw < sh) {
152         stripe = wh - wh*sw/sh;
153         glViewport(0, stripe / 2, ww, wh - stripe);
154     } else {
155         stripe = ww - ww*sh/sw;
156         glViewport(stripe / 2, 0, ww - stripe, wh);
157     }
158 }
159