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
;
91 case PIXMAN_BE_x8r8g8b8
:
92 case PIXMAN_BE_a8r8g8b8
:
93 surface
->glformat
= GL_RGBA
;
94 surface
->gltype
= GL_UNSIGNED_BYTE
;
97 surface
->glformat
= GL_RGB
;
98 surface
->gltype
= GL_UNSIGNED_SHORT_5_6_5
;
101 g_assert_not_reached();
104 glGenTextures(1, &surface
->texture
);
105 glEnable(GL_TEXTURE_2D
);
106 glBindTexture(GL_TEXTURE_2D
, surface
->texture
);
107 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT
,
108 surface_stride(surface
) / surface_bytes_per_pixel(surface
));
109 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGB
,
110 surface_width(surface
),
111 surface_height(surface
),
112 0, surface
->glformat
, surface
->gltype
,
113 surface_data(surface
));
115 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
116 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
119 void surface_gl_update_texture(ConsoleGLState
*gls
,
120 DisplaySurface
*surface
,
121 int x
, int y
, int w
, int h
)
123 uint8_t *data
= (void *)surface_data(surface
);
127 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT
,
128 surface_stride(surface
) / surface_bytes_per_pixel(surface
));
129 glTexSubImage2D(GL_TEXTURE_2D
, 0,
131 surface
->glformat
, surface
->gltype
,
132 data
+ surface_stride(surface
) * y
133 + surface_bytes_per_pixel(surface
) * x
);
136 void surface_gl_render_texture(ConsoleGLState
*gls
,
137 DisplaySurface
*surface
)
141 glClearColor(0.1f
, 0.1f
, 0.1f
, 0.0f
);
142 glClear(GL_COLOR_BUFFER_BIT
);
144 qemu_gl_run_texture_blit(gls
->texture_blit_prog
,
145 gls
->texture_blit_vao
);
148 void surface_gl_destroy_texture(ConsoleGLState
*gls
,
149 DisplaySurface
*surface
)
151 if (!surface
|| !surface
->texture
) {
154 glDeleteTextures(1, &surface
->texture
);
155 surface
->texture
= 0;
158 void surface_gl_setup_viewport(ConsoleGLState
*gls
,
159 DisplaySurface
*surface
,
167 gw
= surface_width(surface
);
168 gh
= surface_height(surface
);
173 stripe
= wh
- wh
*sw
/sh
;
174 glViewport(0, stripe
/ 2, ww
, wh
- stripe
);
176 stripe
= ww
- ww
*sh
/sw
;
177 glViewport(stripe
/ 2, 0, ww
- stripe
, wh
);