2
0

sdl2-gl.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * QEMU SDL display driver -- opengl support
  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/input.h"
  31. #include "ui/sdl2.h"
  32. #include "sysemu/sysemu.h"
  33. #include <epoxy/gl.h>
  34. static void sdl2_set_scanout_mode(struct sdl2_console *scon, bool scanout)
  35. {
  36. if (scon->scanout_mode == scanout) {
  37. return;
  38. }
  39. scon->scanout_mode = scanout;
  40. if (!scon->scanout_mode) {
  41. if (scon->fbo_id) {
  42. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
  43. GL_COLOR_ATTACHMENT0_EXT,
  44. GL_TEXTURE_2D, 0, 0);
  45. glDeleteFramebuffers(1, &scon->fbo_id);
  46. glBindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
  47. scon->fbo_id = 0;
  48. }
  49. if (scon->surface) {
  50. surface_gl_destroy_texture(scon->gls, scon->surface);
  51. surface_gl_create_texture(scon->gls, scon->surface);
  52. }
  53. }
  54. }
  55. static void sdl2_gl_render_surface(struct sdl2_console *scon)
  56. {
  57. int ww, wh;
  58. SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
  59. sdl2_set_scanout_mode(scon, false);
  60. SDL_GetWindowSize(scon->real_window, &ww, &wh);
  61. surface_gl_setup_viewport(scon->gls, scon->surface, ww, wh);
  62. surface_gl_render_texture(scon->gls, scon->surface);
  63. SDL_GL_SwapWindow(scon->real_window);
  64. }
  65. void sdl2_gl_update(DisplayChangeListener *dcl,
  66. int x, int y, int w, int h)
  67. {
  68. struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
  69. assert(scon->opengl);
  70. SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
  71. surface_gl_update_texture(scon->gls, scon->surface, x, y, w, h);
  72. scon->updates++;
  73. }
  74. void sdl2_gl_switch(DisplayChangeListener *dcl,
  75. DisplaySurface *new_surface)
  76. {
  77. struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
  78. DisplaySurface *old_surface = scon->surface;
  79. assert(scon->opengl);
  80. SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
  81. surface_gl_destroy_texture(scon->gls, scon->surface);
  82. scon->surface = new_surface;
  83. if (!new_surface) {
  84. console_gl_fini_context(scon->gls);
  85. scon->gls = NULL;
  86. sdl2_window_destroy(scon);
  87. return;
  88. }
  89. if (!scon->real_window) {
  90. sdl2_window_create(scon);
  91. scon->gls = console_gl_init_context();
  92. } else if (old_surface &&
  93. ((surface_width(old_surface) != surface_width(new_surface)) ||
  94. (surface_height(old_surface) != surface_height(new_surface)))) {
  95. sdl2_window_resize(scon);
  96. }
  97. surface_gl_create_texture(scon->gls, scon->surface);
  98. }
  99. void sdl2_gl_refresh(DisplayChangeListener *dcl)
  100. {
  101. struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
  102. assert(scon->opengl);
  103. graphic_hw_update(dcl->con);
  104. if (scon->updates && scon->surface) {
  105. scon->updates = 0;
  106. sdl2_gl_render_surface(scon);
  107. }
  108. sdl2_poll_events(scon);
  109. }
  110. void sdl2_gl_redraw(struct sdl2_console *scon)
  111. {
  112. assert(scon->opengl);
  113. if (scon->surface) {
  114. sdl2_gl_render_surface(scon);
  115. }
  116. }
  117. QEMUGLContext sdl2_gl_create_context(DisplayChangeListener *dcl,
  118. QEMUGLParams *params)
  119. {
  120. struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
  121. SDL_GLContext ctx;
  122. assert(scon->opengl);
  123. SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
  124. SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
  125. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
  126. SDL_GL_CONTEXT_PROFILE_CORE);
  127. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, params->major_ver);
  128. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, params->minor_ver);
  129. ctx = SDL_GL_CreateContext(scon->real_window);
  130. return (QEMUGLContext)ctx;
  131. }
  132. void sdl2_gl_destroy_context(DisplayChangeListener *dcl, QEMUGLContext ctx)
  133. {
  134. SDL_GLContext sdlctx = (SDL_GLContext)ctx;
  135. SDL_GL_DeleteContext(sdlctx);
  136. }
  137. int sdl2_gl_make_context_current(DisplayChangeListener *dcl,
  138. QEMUGLContext ctx)
  139. {
  140. struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
  141. SDL_GLContext sdlctx = (SDL_GLContext)ctx;
  142. assert(scon->opengl);
  143. return SDL_GL_MakeCurrent(scon->real_window, sdlctx);
  144. }
  145. QEMUGLContext sdl2_gl_get_current_context(DisplayChangeListener *dcl)
  146. {
  147. SDL_GLContext sdlctx;
  148. sdlctx = SDL_GL_GetCurrentContext();
  149. return (QEMUGLContext)sdlctx;
  150. }
  151. void sdl2_gl_scanout_disable(DisplayChangeListener *dcl)
  152. {
  153. struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
  154. assert(scon->opengl);
  155. scon->w = 0;
  156. scon->h = 0;
  157. scon->tex_id = 0;
  158. sdl2_set_scanout_mode(scon, false);
  159. }
  160. void sdl2_gl_scanout_texture(DisplayChangeListener *dcl,
  161. uint32_t backing_id,
  162. bool backing_y_0_top,
  163. uint32_t backing_width,
  164. uint32_t backing_height,
  165. uint32_t x, uint32_t y,
  166. uint32_t w, uint32_t h)
  167. {
  168. struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
  169. assert(scon->opengl);
  170. scon->x = x;
  171. scon->y = y;
  172. scon->w = w;
  173. scon->h = h;
  174. scon->tex_id = backing_id;
  175. scon->y0_top = backing_y_0_top;
  176. SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
  177. sdl2_set_scanout_mode(scon, true);
  178. if (!scon->fbo_id) {
  179. glGenFramebuffers(1, &scon->fbo_id);
  180. }
  181. glBindFramebuffer(GL_FRAMEBUFFER_EXT, scon->fbo_id);
  182. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
  183. GL_TEXTURE_2D, scon->tex_id, 0);
  184. }
  185. void sdl2_gl_scanout_flush(DisplayChangeListener *dcl,
  186. uint32_t x, uint32_t y, uint32_t w, uint32_t h)
  187. {
  188. struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
  189. int ww, wh, y1, y2;
  190. assert(scon->opengl);
  191. if (!scon->scanout_mode) {
  192. return;
  193. }
  194. if (!scon->fbo_id) {
  195. return;
  196. }
  197. SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
  198. glBindFramebuffer(GL_READ_FRAMEBUFFER, scon->fbo_id);
  199. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
  200. SDL_GetWindowSize(scon->real_window, &ww, &wh);
  201. glViewport(0, 0, ww, wh);
  202. y1 = scon->y0_top ? 0 : scon->h;
  203. y2 = scon->y0_top ? scon->h : 0;
  204. glBlitFramebuffer(0, y1, scon->w, y2,
  205. 0, 0, ww, wh,
  206. GL_COLOR_BUFFER_BIT, GL_NEAREST);
  207. glBindFramebuffer(GL_FRAMEBUFFER_EXT, scon->fbo_id);
  208. SDL_GL_SwapWindow(scon->real_window);
  209. }