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 "qemu-common.h" 29 #include "ui/console.h" 30 #include "ui/shader.h" 31 32 #include "shader/texture-blit-vert.h" 33 #include "shader/texture-blit-frag.h" 34 35 struct ConsoleGLState { 36 GLint texture_blit_prog; 37 GLint texture_blit_vao; 38 }; 39 40 /* ---------------------------------------------------------------------- */ 41 42 ConsoleGLState *console_gl_init_context(void) 43 { 44 ConsoleGLState *gls = g_new0(ConsoleGLState, 1); 45 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); 50 } 51 52 gls->texture_blit_vao = 53 qemu_gl_init_texture_blit(gls->texture_blit_prog); 54 55 return gls; 56 } 57 58 void console_gl_fini_context(ConsoleGLState *gls) 59 { 60 if (!gls) { 61 return; 62 } 63 g_free(gls); 64 } 65 66 bool console_gl_check_format(DisplayChangeListener *dcl, 67 pixman_format_code_t format) 68 { 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; 76 } 77 } 78 79 void surface_gl_create_texture(ConsoleGLState *gls, 80 DisplaySurface *surface) 81 { 82 assert(gls); 83 assert(surface_stride(surface) % surface_bytes_per_pixel(surface) == 0); 84 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(); 97 } 98 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)); 109 110 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 111 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 112 } 113 114 void surface_gl_update_texture(ConsoleGLState *gls, 115 DisplaySurface *surface, 116 int x, int y, int w, int h) 117 { 118 uint8_t *data = (void *)surface_data(surface); 119 120 assert(gls); 121 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); 129 } 130 131 void surface_gl_render_texture(ConsoleGLState *gls, 132 DisplaySurface *surface) 133 { 134 assert(gls); 135 136 glClearColor(0.1f, 0.1f, 0.1f, 0.0f); 137 glClear(GL_COLOR_BUFFER_BIT); 138 139 qemu_gl_run_texture_blit(gls->texture_blit_prog, 140 gls->texture_blit_vao); 141 } 142 143 void surface_gl_destroy_texture(ConsoleGLState *gls, 144 DisplaySurface *surface) 145 { 146 if (!surface || !surface->texture) { 147 return; 148 } 149 glDeleteTextures(1, &surface->texture); 150 surface->texture = 0; 151 } 152 153 void surface_gl_setup_viewport(ConsoleGLState *gls, 154 DisplaySurface *surface, 155 int ww, int wh) 156 { 157 int gw, gh, stripe; 158 float sw, sh; 159 160 assert(gls); 161 162 gw = surface_width(surface); 163 gh = surface_height(surface); 164 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); 173 } 174 } 175