sdl2-gl.c 7.7 KB

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