| 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 <epoxy/gl.h> |
| 29 | #include "qemu/error-report.h" |
| 30 | #include "ui/console.h" |
| 31 | #include "ui/shader.h" |
| 32 | |
| 33 | /* ---------------------------------------------------------------------- */ |
| 34 | |
| 35 | static bool map_format(pixman_format_code_t format, |
| 36 | GLenum *glformat, GLenum *gltype) |
| 37 | { |
| 38 | switch (format) { |
| 39 | case PIXMAN_BE_b8g8r8x8: |
| 40 | case PIXMAN_BE_b8g8r8a8: |
| 41 | *glformat = GL_BGRA_EXT; |
| 42 | *gltype = GL_UNSIGNED_BYTE; |
| 43 | return true; |
| 44 | case PIXMAN_BE_x8r8g8b8: |
| 45 | case PIXMAN_BE_a8r8g8b8: |
| 46 | *glformat = GL_RGBA; |
| 47 | *gltype = GL_UNSIGNED_BYTE; |
| 48 | return true; |
| 49 | case PIXMAN_r5g6b5: |
| 50 | *glformat = GL_RGB; |
| 51 | *gltype = GL_UNSIGNED_SHORT_5_6_5; |
| 52 | return true; |
| 53 | default: |
| 54 | return false; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | bool console_gl_check_format(DisplayChangeListener *dcl, |
| 59 | pixman_format_code_t format) |
| 60 | { |
| 61 | GLenum glformat; |
| 62 | GLenum gltype; |
| 63 | |
| 64 | return map_format(format, &glformat, &gltype); |
| 65 | } |
| 66 | |
| 67 | void surface_gl_create_texture(QemuGLShader *gls, |
| 68 | DisplaySurface *surface) |
| 69 | { |
| 70 | GLenum glformat; |
| 71 | GLenum gltype; |
| 72 | |
| 73 | assert(gls); |
| 74 | assert(QEMU_IS_ALIGNED(surface_stride(surface), surface_bytes_per_pixel(surface))); |
| 75 | |
| 76 | if (surface->texture) { |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | assert(map_format(surface_format(surface), &glformat, &gltype)); |
| 81 | glGenTextures(1, &surface->texture); |
| 82 | glEnable(GL_TEXTURE_2D); |
| 83 | glBindTexture(GL_TEXTURE_2D, surface->texture); |
| 84 | glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, |
| 85 | surface_stride(surface) / surface_bytes_per_pixel(surface)); |
| 86 | if (epoxy_is_desktop_gl()) { |
| 87 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, |
| 88 | surface_width(surface), surface_height(surface), 0, |
| 89 | glformat, gltype, surface_data(surface)); |
| 90 | } else { |
| 91 | glTexImage2D(GL_TEXTURE_2D, 0, glformat, |
| 92 | surface_width(surface), surface_height(surface), 0, |
| 93 | glformat, gltype, surface_data(surface)); |
| 94 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ONE); |
| 95 | } |
| 96 | |
| 97 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 98 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 99 | } |
| 100 | |
| 101 | bool surface_gl_create_texture_from_fd(DisplaySurface *surface, |
| 102 | int fd, GLuint *texture, |
| 103 | GLuint *mem_obj) |
| 104 | { |
| 105 | unsigned long size = surface_stride(surface) * surface_height(surface); |
| 106 | GLenum err = glGetError(); |
| 107 | *texture = 0; |
| 108 | *mem_obj = 0; |
| 109 | |
| 110 | if (!epoxy_has_gl_extension("GL_EXT_memory_object") || |
| 111 | !epoxy_has_gl_extension("GL_EXT_memory_object_fd")) { |
| 112 | error_report("spice: required OpenGL extensions not supported: " |
| 113 | "GL_EXT_memory_object and GL_EXT_memory_object_fd"); |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | #ifdef GL_EXT_memory_object_fd |
| 118 | glCreateMemoryObjectsEXT(1, mem_obj); |
| 119 | glImportMemoryFdEXT(*mem_obj, size, GL_HANDLE_TYPE_OPAQUE_FD_EXT, fd); |
| 120 | |
| 121 | err = glGetError(); |
| 122 | if (err != GL_NO_ERROR) { |
| 123 | error_report("spice: cannot import memory object from fd"); |
| 124 | goto cleanup_mem; |
| 125 | } |
| 126 | |
| 127 | glGenTextures(1, texture); |
| 128 | glBindTexture(GL_TEXTURE_2D, *texture); |
| 129 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_TILING_EXT, GL_LINEAR_TILING_EXT); |
| 130 | glTexStorageMem2DEXT(GL_TEXTURE_2D, 1, GL_RGBA8, surface_width(surface), |
| 131 | surface_height(surface), *mem_obj, 0); |
| 132 | err = glGetError(); |
| 133 | if (err != GL_NO_ERROR) { |
| 134 | error_report("spice: cannot create texture from memory object"); |
| 135 | goto cleanup_tex_and_mem; |
| 136 | } |
| 137 | return true; |
| 138 | |
| 139 | cleanup_tex_and_mem: |
| 140 | glDeleteTextures(1, texture); |
| 141 | cleanup_mem: |
| 142 | glDeleteMemoryObjectsEXT(1, mem_obj); |
| 143 | |
| 144 | #endif |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | void surface_gl_update_texture(QemuGLShader *gls, |
| 149 | DisplaySurface *surface, |
| 150 | int x, int y, int w, int h) |
| 151 | { |
| 152 | uint8_t *data = (void *)surface_data(surface); |
| 153 | GLenum glformat; |
| 154 | GLenum gltype; |
| 155 | |
| 156 | assert(gls); |
| 157 | assert(map_format(surface_format(surface), &glformat, &gltype)); |
| 158 | |
| 159 | if (surface->texture) { |
| 160 | glBindTexture(GL_TEXTURE_2D, surface->texture); |
| 161 | glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, |
| 162 | surface_stride(surface) |
| 163 | / surface_bytes_per_pixel(surface)); |
| 164 | glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, glformat, gltype, |
| 165 | data + surface_stride(surface) * y |
| 166 | + surface_bytes_per_pixel(surface) * x); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | void surface_gl_render_texture(QemuGLShader *gls, |
| 171 | DisplaySurface *surface) |
| 172 | { |
| 173 | assert(gls); |
| 174 | |
| 175 | glClearColor(0.1f, 0.1f, 0.1f, 0.0f); |
| 176 | glClear(GL_COLOR_BUFFER_BIT); |
| 177 | |
| 178 | qemu_gl_run_texture_blit(gls, false); |
| 179 | } |
| 180 | |
| 181 | void surface_gl_destroy_texture(QemuGLShader *gls, |
| 182 | DisplaySurface *surface) |
| 183 | { |
| 184 | if (!surface || !surface->texture) { |
| 185 | return; |
| 186 | } |
| 187 | glDeleteTextures(1, &surface->texture); |
| 188 | surface->texture = 0; |
| 189 | } |
| 190 | |
| 191 | void surface_gl_setup_viewport(QemuGLShader *gls, |
| 192 | DisplaySurface *surface, |
| 193 | int ww, int wh) |
| 194 | { |
| 195 | int gw, gh, stripe; |
| 196 | float sw, sh; |
| 197 | |
| 198 | assert(gls); |
| 199 | |
| 200 | gw = surface_width(surface); |
| 201 | gh = surface_height(surface); |
| 202 | |
| 203 | sw = (float)ww/gw; |
| 204 | sh = (float)wh/gh; |
| 205 | if (sw < sh) { |
| 206 | stripe = wh - wh*sw/sh; |
| 207 | glViewport(0, stripe / 2, ww, wh - stripe); |
| 208 | } else { |
| 209 | stripe = ww - ww*sh/sw; |
| 210 | glViewport(stripe / 2, 0, ww - stripe, wh); |
| 211 | } |
| 212 | } |