Merge remote-tracking branch 'remotes/mst/tags/for_upstream' into staging
[qemu/ar7.git] / ui / console-gl.c
blobcb45cf8a290d490e19564954f7e6bd22f7c8accf
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-common.h"
28 #include "ui/console.h"
29 #include "ui/shader.h"
31 #include "shader/texture-blit-vert.h"
32 #include "shader/texture-blit-frag.h"
34 struct ConsoleGLState {
35 GLint texture_blit_prog;
38 /* ---------------------------------------------------------------------- */
40 ConsoleGLState *console_gl_init_context(void)
42 ConsoleGLState *gls = g_new0(ConsoleGLState, 1);
44 gls->texture_blit_prog = qemu_gl_create_compile_link_program
45 (texture_blit_vert_src, texture_blit_frag_src);
46 if (!gls->texture_blit_prog) {
47 exit(1);
50 return gls;
53 void console_gl_fini_context(ConsoleGLState *gls)
55 if (!gls) {
56 return;
58 g_free(gls);
61 bool console_gl_check_format(DisplayChangeListener *dcl,
62 pixman_format_code_t format)
64 switch (format) {
65 case PIXMAN_BE_b8g8r8x8:
66 case PIXMAN_BE_b8g8r8a8:
67 case PIXMAN_r5g6b5:
68 return true;
69 default:
70 return false;
74 void surface_gl_create_texture(ConsoleGLState *gls,
75 DisplaySurface *surface)
77 assert(gls);
78 assert(surface_stride(surface) % surface_bytes_per_pixel(surface) == 0);
80 switch (surface->format) {
81 case PIXMAN_BE_b8g8r8x8:
82 case PIXMAN_BE_b8g8r8a8:
83 surface->glformat = GL_BGRA_EXT;
84 surface->gltype = GL_UNSIGNED_BYTE;
85 break;
86 case PIXMAN_r5g6b5:
87 surface->glformat = GL_RGB;
88 surface->gltype = GL_UNSIGNED_SHORT_5_6_5;
89 break;
90 default:
91 g_assert_not_reached();
94 glGenTextures(1, &surface->texture);
95 glEnable(GL_TEXTURE_2D);
96 glBindTexture(GL_TEXTURE_2D, surface->texture);
97 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT,
98 surface_stride(surface) / surface_bytes_per_pixel(surface));
99 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
100 surface_width(surface),
101 surface_height(surface),
102 0, surface->glformat, surface->gltype,
103 surface_data(surface));
105 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
106 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
109 void surface_gl_update_texture(ConsoleGLState *gls,
110 DisplaySurface *surface,
111 int x, int y, int w, int h)
113 uint8_t *data = (void *)surface_data(surface);
115 assert(gls);
117 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT,
118 surface_stride(surface) / surface_bytes_per_pixel(surface));
119 glTexSubImage2D(GL_TEXTURE_2D, 0,
120 x, y, w, h,
121 surface->glformat, surface->gltype,
122 data + surface_stride(surface) * y
123 + surface_bytes_per_pixel(surface) * x);
126 void surface_gl_render_texture(ConsoleGLState *gls,
127 DisplaySurface *surface)
129 assert(gls);
131 glClearColor(0.1f, 0.1f, 0.1f, 0.0f);
132 glClear(GL_COLOR_BUFFER_BIT);
134 qemu_gl_run_texture_blit(gls->texture_blit_prog);
137 void surface_gl_destroy_texture(ConsoleGLState *gls,
138 DisplaySurface *surface)
140 if (!surface || !surface->texture) {
141 return;
143 glDeleteTextures(1, &surface->texture);
144 surface->texture = 0;
147 void surface_gl_setup_viewport(ConsoleGLState *gls,
148 DisplaySurface *surface,
149 int ww, int wh)
151 int gw, gh, stripe;
152 float sw, sh;
154 assert(gls);
156 gw = surface_width(surface);
157 gh = surface_height(surface);
159 sw = (float)ww/gw;
160 sh = (float)wh/gh;
161 if (sw < sh) {
162 stripe = wh - wh*sw/sh;
163 glViewport(0, stripe / 2, ww, wh - stripe);
164 } else {
165 stripe = ww - ww*sh/sw;
166 glViewport(stripe / 2, 0, ww - stripe, wh);