console-gl.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. if (surface->texture) {
  49. return;
  50. }
  51. switch (surface_format(surface)) {
  52. case PIXMAN_BE_b8g8r8x8:
  53. case PIXMAN_BE_b8g8r8a8:
  54. surface->glformat = GL_RGBA;
  55. surface->gltype = GL_UNSIGNED_BYTE;
  56. surface->glswapped = true;
  57. break;
  58. case PIXMAN_BE_x8r8g8b8:
  59. case PIXMAN_BE_a8r8g8b8:
  60. surface->glformat = GL_RGBA;
  61. surface->gltype = GL_UNSIGNED_BYTE;
  62. break;
  63. case PIXMAN_r5g6b5:
  64. surface->glformat = GL_RGB;
  65. surface->gltype = GL_UNSIGNED_SHORT_5_6_5;
  66. break;
  67. default:
  68. g_assert_not_reached();
  69. }
  70. glGenTextures(1, &surface->texture);
  71. glEnable(GL_TEXTURE_2D);
  72. glBindTexture(GL_TEXTURE_2D, surface->texture);
  73. glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT,
  74. surface_stride(surface) / surface_bytes_per_pixel(surface));
  75. if (epoxy_is_desktop_gl()) {
  76. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
  77. surface_width(surface),
  78. surface_height(surface),
  79. 0, surface->glformat, surface->gltype,
  80. surface_data(surface));
  81. } else {
  82. glTexImage2D(GL_TEXTURE_2D, 0, surface->glformat,
  83. surface_width(surface),
  84. surface_height(surface),
  85. 0, surface->glformat, surface->gltype,
  86. surface_data(surface));
  87. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ONE);
  88. }
  89. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  90. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  91. }
  92. void surface_gl_update_texture(QemuGLShader *gls,
  93. DisplaySurface *surface,
  94. int x, int y, int w, int h)
  95. {
  96. uint8_t *data = (void *)surface_data(surface);
  97. assert(gls);
  98. if (surface->texture) {
  99. glBindTexture(GL_TEXTURE_2D, surface->texture);
  100. glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT,
  101. surface_stride(surface)
  102. / surface_bytes_per_pixel(surface));
  103. glTexSubImage2D(GL_TEXTURE_2D, 0,
  104. x, y, w, h,
  105. surface->glformat, surface->gltype,
  106. data + surface_stride(surface) * y
  107. + surface_bytes_per_pixel(surface) * x);
  108. }
  109. }
  110. void surface_gl_render_texture(QemuGLShader *gls,
  111. DisplaySurface *surface)
  112. {
  113. assert(gls);
  114. glClearColor(0.1f, 0.1f, 0.1f, 0.0f);
  115. glClear(GL_COLOR_BUFFER_BIT);
  116. qemu_gl_run_texture_blit(gls, false, false);
  117. }
  118. void surface_gl_destroy_texture(QemuGLShader *gls,
  119. DisplaySurface *surface)
  120. {
  121. if (!surface || !surface->texture) {
  122. return;
  123. }
  124. glDeleteTextures(1, &surface->texture);
  125. surface->texture = 0;
  126. }
  127. void surface_gl_setup_viewport(QemuGLShader *gls,
  128. DisplaySurface *surface,
  129. int ww, int wh)
  130. {
  131. int gw, gh, stripe;
  132. float sw, sh;
  133. assert(gls);
  134. gw = surface_width(surface);
  135. gh = surface_height(surface);
  136. sw = (float)ww/gw;
  137. sh = (float)wh/gh;
  138. if (sw < sh) {
  139. stripe = wh - wh*sw/sh;
  140. glViewport(0, stripe / 2, ww, wh - stripe);
  141. } else {
  142. stripe = ww - ww*sh/sw;
  143. glViewport(stripe / 2, 0, ww - stripe, wh);
  144. }
  145. }