2 * QEMU graphical console -- opengl helper bits
4 * Copyright (c) 2014 Red Hat
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
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
) {
52 gls
->texture_blit_vao
=
53 qemu_gl_init_texture_blit(gls
->texture_blit_prog
);
58 void console_gl_fini_context(ConsoleGLState
*gls
)
66 bool console_gl_check_format(DisplayChangeListener
*dcl
,
67 pixman_format_code_t format
)
70 case PIXMAN_BE_b8g8r8x8
:
71 case PIXMAN_BE_b8g8r8a8
:
79 void surface_gl_create_texture(ConsoleGLState
*gls
,
80 DisplaySurface
*surface
)
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
;
92 surface
->glformat
= GL_RGB
;
93 surface
->gltype
= GL_UNSIGNED_SHORT_5_6_5
;
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
);
122 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT
,
123 surface_stride(surface
) / surface_bytes_per_pixel(surface
));
124 glTexSubImage2D(GL_TEXTURE_2D
, 0,
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
)
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
) {
149 glDeleteTextures(1, &surface
->texture
);
150 surface
->texture
= 0;
153 void surface_gl_setup_viewport(ConsoleGLState
*gls
,
154 DisplaySurface
*surface
,
162 gw
= surface_width(surface
);
163 gh
= surface_height(surface
);
168 stripe
= wh
- wh
*sw
/sh
;
169 glViewport(0, stripe
/ 2, ww
, wh
- stripe
);
171 stripe
= ww
- ww
*sh
/sw
;
172 glViewport(stripe
/ 2, 0, ww
- stripe
, wh
);