2
0

sdl2-gl.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 "ui/console.h"
  29. #include "ui/input.h"
  30. #include "ui/sdl2.h"
  31. static void sdl2_set_scanout_mode(struct sdl2_console *scon, bool scanout)
  32. {
  33. if (scon->scanout_mode == scanout) {
  34. return;
  35. }
  36. scon->scanout_mode = scanout;
  37. if (!scon->scanout_mode) {
  38. egl_fb_destroy(&scon->guest_fb);
  39. if (scon->surface) {
  40. surface_gl_destroy_texture(scon->gls, scon->surface);
  41. surface_gl_create_texture(scon->gls, scon->surface);
  42. }
  43. }
  44. }
  45. static void sdl2_gl_render_surface(struct sdl2_console *scon)
  46. {
  47. int ww, wh;
  48. SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
  49. sdl2_set_scanout_mode(scon, false);
  50. SDL_GetWindowSize(scon->real_window, &ww, &wh);
  51. surface_gl_setup_viewport(scon->gls, scon->surface, ww, wh);
  52. surface_gl_render_texture(scon->gls, scon->surface);
  53. SDL_GL_SwapWindow(scon->real_window);
  54. }
  55. void sdl2_gl_update(DisplayChangeListener *dcl,
  56. int x, int y, int w, int h)
  57. {
  58. struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
  59. assert(scon->opengl);
  60. SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
  61. surface_gl_update_texture(scon->gls, scon->surface, x, y, w, h);
  62. scon->updates++;
  63. }
  64. void sdl2_gl_switch(DisplayChangeListener *dcl,
  65. DisplaySurface *new_surface)
  66. {
  67. struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
  68. DisplaySurface *old_surface = scon->surface;
  69. assert(scon->opengl);
  70. SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
  71. surface_gl_destroy_texture(scon->gls, scon->surface);
  72. scon->surface = new_surface;
  73. if (!new_surface) {
  74. qemu_gl_fini_shader(scon->gls);
  75. scon->gls = NULL;
  76. sdl2_window_destroy(scon);
  77. return;
  78. }
  79. if (!scon->real_window) {
  80. sdl2_window_create(scon);
  81. scon->gls = qemu_gl_init_shader();
  82. } else if (old_surface &&
  83. ((surface_width(old_surface) != surface_width(new_surface)) ||
  84. (surface_height(old_surface) != surface_height(new_surface)))) {
  85. sdl2_window_resize(scon);
  86. }
  87. surface_gl_create_texture(scon->gls, scon->surface);
  88. }
  89. void sdl2_gl_refresh(DisplayChangeListener *dcl)
  90. {
  91. struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
  92. assert(scon->opengl);
  93. graphic_hw_update(dcl->con);
  94. if (scon->updates && scon->surface) {
  95. scon->updates = 0;
  96. sdl2_gl_render_surface(scon);
  97. }
  98. sdl2_poll_events(scon);
  99. }
  100. void sdl2_gl_redraw(struct sdl2_console *scon)
  101. {
  102. assert(scon->opengl);
  103. if (scon->scanout_mode) {
  104. /* sdl2_gl_scanout_flush actually only care about
  105. * the first argument. */
  106. return sdl2_gl_scanout_flush(&scon->dcl, 0, 0, 0, 0);
  107. }
  108. if (scon->surface) {
  109. sdl2_gl_render_surface(scon);
  110. }
  111. }
  112. QEMUGLContext sdl2_gl_create_context(DisplayChangeListener *dcl,
  113. QEMUGLParams *params)
  114. {
  115. struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
  116. SDL_GLContext ctx;
  117. assert(scon->opengl);
  118. SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
  119. SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
  120. if (scon->opts->gl == DISPLAYGL_MODE_ON ||
  121. scon->opts->gl == DISPLAYGL_MODE_CORE) {
  122. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
  123. SDL_GL_CONTEXT_PROFILE_CORE);
  124. } else if (scon->opts->gl == DISPLAYGL_MODE_ES) {
  125. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
  126. SDL_GL_CONTEXT_PROFILE_ES);
  127. }
  128. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, params->major_ver);
  129. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, params->minor_ver);
  130. ctx = SDL_GL_CreateContext(scon->real_window);
  131. /* If SDL fail to create a GL context and we use the "on" flag,
  132. * then try to fallback to GLES.
  133. */
  134. if (!ctx && scon->opts->gl == DISPLAYGL_MODE_ON) {
  135. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
  136. SDL_GL_CONTEXT_PROFILE_ES);
  137. ctx = SDL_GL_CreateContext(scon->real_window);
  138. }
  139. return (QEMUGLContext)ctx;
  140. }
  141. void sdl2_gl_destroy_context(DisplayChangeListener *dcl, QEMUGLContext ctx)
  142. {
  143. SDL_GLContext sdlctx = (SDL_GLContext)ctx;
  144. SDL_GL_DeleteContext(sdlctx);
  145. }
  146. int sdl2_gl_make_context_current(DisplayChangeListener *dcl,
  147. QEMUGLContext ctx)
  148. {
  149. struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
  150. SDL_GLContext sdlctx = (SDL_GLContext)ctx;
  151. assert(scon->opengl);
  152. return SDL_GL_MakeCurrent(scon->real_window, sdlctx);
  153. }
  154. QEMUGLContext sdl2_gl_get_current_context(DisplayChangeListener *dcl)
  155. {
  156. SDL_GLContext sdlctx;
  157. sdlctx = SDL_GL_GetCurrentContext();
  158. return (QEMUGLContext)sdlctx;
  159. }
  160. void sdl2_gl_scanout_disable(DisplayChangeListener *dcl)
  161. {
  162. struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
  163. assert(scon->opengl);
  164. scon->w = 0;
  165. scon->h = 0;
  166. sdl2_set_scanout_mode(scon, false);
  167. }
  168. void sdl2_gl_scanout_texture(DisplayChangeListener *dcl,
  169. uint32_t backing_id,
  170. bool backing_y_0_top,
  171. uint32_t backing_width,
  172. uint32_t backing_height,
  173. uint32_t x, uint32_t y,
  174. uint32_t w, uint32_t h)
  175. {
  176. struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
  177. assert(scon->opengl);
  178. scon->x = x;
  179. scon->y = y;
  180. scon->w = w;
  181. scon->h = h;
  182. scon->y0_top = backing_y_0_top;
  183. SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
  184. sdl2_set_scanout_mode(scon, true);
  185. egl_fb_setup_for_tex(&scon->guest_fb, backing_width, backing_height,
  186. backing_id, false);
  187. }
  188. void sdl2_gl_scanout_flush(DisplayChangeListener *dcl,
  189. uint32_t x, uint32_t y, uint32_t w, uint32_t h)
  190. {
  191. struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
  192. int ww, wh;
  193. assert(scon->opengl);
  194. if (!scon->scanout_mode) {
  195. return;
  196. }
  197. if (!scon->guest_fb.framebuffer) {
  198. return;
  199. }
  200. SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
  201. SDL_GetWindowSize(scon->real_window, &ww, &wh);
  202. egl_fb_setup_default(&scon->win_fb, ww, wh);
  203. egl_fb_blit(&scon->win_fb, &scon->guest_fb, !scon->y0_top);
  204. SDL_GL_SwapWindow(scon->real_window);
  205. }