qmp: Don't reuse qmp visitor after grabbing output
[qemu/ar7.git] / ui / console-gl.c
blob74b1bed6ee4eac83087c7d8a424d2f150be59828
1 /*
2 * QEMU graphical console -- opengl helper bits
4 * Copyright (c) 2014 Red Hat
6 * Authors:
7 * Gerd Hoffmann <kraxel@redhat.com>
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:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
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.
27 #include "qemu/osdep.h"
28 #include "qemu-common.h"
29 #include "ui/console.h"
30 #include "ui/shader.h"
32 #include "shader/texture-blit-vert.h"
33 #include "shader/texture-blit-frag.h"
35 struct ConsoleGLState {
36 GLint texture_blit_prog;
37 GLint texture_blit_vao;
40 /* ---------------------------------------------------------------------- */
42 ConsoleGLState *console_gl_init_context(void)
44 ConsoleGLState *gls = g_new0(ConsoleGLState, 1);
46 gls->texture_blit_prog = qemu_gl_create_compile_link_program
47 (texture_blit_vert_src, texture_blit_frag_src);
48 if (!gls->texture_blit_prog) {
49 exit(1);
52 gls->texture_blit_vao =
53 qemu_gl_init_texture_blit(gls->texture_blit_prog);
55 return gls;
58 void console_gl_fini_context(ConsoleGLState *gls)
60 if (!gls) {
61 return;
63 g_free(gls);
66 bool console_gl_check_format(DisplayChangeListener *dcl,
67 pixman_format_code_t format)
69 switch (format) {
70 case PIXMAN_BE_b8g8r8x8:
71 case PIXMAN_BE_b8g8r8a8:
72 case PIXMAN_r5g6b5:
73 return true;
74 default:
75 return false;
79 void surface_gl_create_texture(ConsoleGLState *gls,
80 DisplaySurface *surface)
82 assert(gls);
83 assert(surface_stride(surface) % surface_bytes_per_pixel(surface) == 0);
85 switch (surface->format) {
86 case PIXMAN_BE_b8g8r8x8:
87 case PIXMAN_BE_b8g8r8a8:
88 surface->glformat = GL_BGRA_EXT;
89 surface->gltype = GL_UNSIGNED_BYTE;
90 break;
91 case PIXMAN_r5g6b5:
92 surface->glformat = GL_RGB;
93 surface->gltype = GL_UNSIGNED_SHORT_5_6_5;
94 break;
95 default:
96 g_assert_not_reached();
99 glGenTextures(1, &surface->texture);
100 glEnable(GL_TEXTURE_2D);
101 glBindTexture(GL_TEXTURE_2D, surface->texture);
102 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT,
103 surface_stride(surface) / surface_bytes_per_pixel(surface));
104 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
105 surface_width(surface),
106 surface_height(surface),
107 0, surface->glformat, surface->gltype,
108 surface_data(surface));
110 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
111 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
114 void surface_gl_update_texture(ConsoleGLState *gls,
115 DisplaySurface *surface,
116 int x, int y, int w, int h)
118 uint8_t *data = (void *)surface_data(surface);
120 assert(gls);
122 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT,
123 surface_stride(surface) / surface_bytes_per_pixel(surface));
124 glTexSubImage2D(GL_TEXTURE_2D, 0,
125 x, y, w, h,
126 surface->glformat, surface->gltype,
127 data + surface_stride(surface) * y
128 + surface_bytes_per_pixel(surface) * x);
131 void surface_gl_render_texture(ConsoleGLState *gls,
132 DisplaySurface *surface)
134 assert(gls);
136 glClearColor(0.1f, 0.1f, 0.1f, 0.0f);
137 glClear(GL_COLOR_BUFFER_BIT);
139 qemu_gl_run_texture_blit(gls->texture_blit_prog,
140 gls->texture_blit_vao);
143 void surface_gl_destroy_texture(ConsoleGLState *gls,
144 DisplaySurface *surface)
146 if (!surface || !surface->texture) {
147 return;
149 glDeleteTextures(1, &surface->texture);
150 surface->texture = 0;
153 void surface_gl_setup_viewport(ConsoleGLState *gls,
154 DisplaySurface *surface,
155 int ww, int wh)
157 int gw, gh, stripe;
158 float sw, sh;
160 assert(gls);
162 gw = surface_width(surface);
163 gh = surface_height(surface);
165 sw = (float)ww/gw;
166 sh = (float)wh/gh;
167 if (sw < sh) {
168 stripe = wh - wh*sw/sh;
169 glViewport(0, stripe / 2, ww, wh - stripe);
170 } else {
171 stripe = ww - ww*sh/sw;
172 glViewport(stripe / 2, 0, ww - stripe, wh);