console-gl.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "ui/console.h"
  29. #include "ui/shader.h"
  30. /* ---------------------------------------------------------------------- */
  31. bool console_gl_check_format(DisplayChangeListener *dcl,
  32. pixman_format_code_t format)
  33. {
  34. switch (format) {
  35. case PIXMAN_BE_b8g8r8x8:
  36. case PIXMAN_BE_b8g8r8a8:
  37. case PIXMAN_r5g6b5:
  38. return true;
  39. default:
  40. return false;
  41. }
  42. }
  43. void surface_gl_create_texture(QemuGLShader *gls,
  44. DisplaySurface *surface)
  45. {
  46. assert(gls);
  47. assert(QEMU_IS_ALIGNED(surface_stride(surface), surface_bytes_per_pixel(surface)));
  48. switch (surface->format) {
  49. case PIXMAN_BE_b8g8r8x8:
  50. case PIXMAN_BE_b8g8r8a8:
  51. surface->glformat = GL_BGRA_EXT;
  52. surface->gltype = GL_UNSIGNED_BYTE;
  53. break;
  54. case PIXMAN_BE_x8r8g8b8:
  55. case PIXMAN_BE_a8r8g8b8:
  56. surface->glformat = GL_RGBA;
  57. surface->gltype = GL_UNSIGNED_BYTE;
  58. break;
  59. case PIXMAN_r5g6b5:
  60. surface->glformat = GL_RGB;
  61. surface->gltype = GL_UNSIGNED_SHORT_5_6_5;
  62. break;
  63. default:
  64. g_assert_not_reached();
  65. }
  66. glGenTextures(1, &surface->texture);
  67. glEnable(GL_TEXTURE_2D);
  68. glBindTexture(GL_TEXTURE_2D, surface->texture);
  69. glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT,
  70. surface_stride(surface) / surface_bytes_per_pixel(surface));
  71. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
  72. surface_width(surface),
  73. surface_height(surface),
  74. 0, surface->glformat, surface->gltype,
  75. surface_data(surface));
  76. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  77. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  78. }
  79. void surface_gl_update_texture(QemuGLShader *gls,
  80. DisplaySurface *surface,
  81. int x, int y, int w, int h)
  82. {
  83. uint8_t *data = (void *)surface_data(surface);
  84. assert(gls);
  85. if (surface->texture) {
  86. glBindTexture(GL_TEXTURE_2D, surface->texture);
  87. glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT,
  88. surface_stride(surface)
  89. / surface_bytes_per_pixel(surface));
  90. glTexSubImage2D(GL_TEXTURE_2D, 0,
  91. x, y, w, h,
  92. surface->glformat, surface->gltype,
  93. data + surface_stride(surface) * y
  94. + surface_bytes_per_pixel(surface) * x);
  95. }
  96. }
  97. void surface_gl_render_texture(QemuGLShader *gls,
  98. DisplaySurface *surface)
  99. {
  100. assert(gls);
  101. glClearColor(0.1f, 0.1f, 0.1f, 0.0f);
  102. glClear(GL_COLOR_BUFFER_BIT);
  103. qemu_gl_run_texture_blit(gls, false);
  104. }
  105. void surface_gl_destroy_texture(QemuGLShader *gls,
  106. DisplaySurface *surface)
  107. {
  108. if (!surface || !surface->texture) {
  109. return;
  110. }
  111. glDeleteTextures(1, &surface->texture);
  112. surface->texture = 0;
  113. }
  114. void surface_gl_setup_viewport(QemuGLShader *gls,
  115. DisplaySurface *surface,
  116. int ww, int wh)
  117. {
  118. int gw, gh, stripe;
  119. float sw, sh;
  120. assert(gls);
  121. gw = surface_width(surface);
  122. gh = surface_height(surface);
  123. sw = (float)ww/gw;
  124. sh = (float)wh/gh;
  125. if (sw < sh) {
  126. stripe = wh - wh*sw/sh;
  127. glViewport(0, stripe / 2, ww, wh - stripe);
  128. } else {
  129. stripe = ww - ww*sh/sw;
  130. glViewport(stripe / 2, 0, ww - stripe, wh);
  131. }
  132. }