2
0

egl-headless.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include "qemu/osdep.h"
  2. #include "qemu/module.h"
  3. #include "sysemu/sysemu.h"
  4. #include "ui/console.h"
  5. #include "ui/egl-helpers.h"
  6. #include "ui/egl-context.h"
  7. #include "ui/shader.h"
  8. typedef struct egl_dpy {
  9. DisplayChangeListener dcl;
  10. DisplaySurface *ds;
  11. QemuGLShader *gls;
  12. egl_fb guest_fb;
  13. egl_fb cursor_fb;
  14. egl_fb blit_fb;
  15. bool y_0_top;
  16. uint32_t pos_x;
  17. uint32_t pos_y;
  18. } egl_dpy;
  19. /* ------------------------------------------------------------------ */
  20. static void egl_refresh(DisplayChangeListener *dcl)
  21. {
  22. graphic_hw_update(dcl->con);
  23. }
  24. static void egl_gfx_update(DisplayChangeListener *dcl,
  25. int x, int y, int w, int h)
  26. {
  27. }
  28. static void egl_gfx_switch(DisplayChangeListener *dcl,
  29. struct DisplaySurface *new_surface)
  30. {
  31. egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
  32. edpy->ds = new_surface;
  33. }
  34. static QEMUGLContext egl_create_context(DisplayChangeListener *dcl,
  35. QEMUGLParams *params)
  36. {
  37. eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
  38. qemu_egl_rn_ctx);
  39. return qemu_egl_create_context(dcl, params);
  40. }
  41. static void egl_scanout_disable(DisplayChangeListener *dcl)
  42. {
  43. egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
  44. egl_fb_destroy(&edpy->guest_fb);
  45. egl_fb_destroy(&edpy->blit_fb);
  46. }
  47. static void egl_scanout_texture(DisplayChangeListener *dcl,
  48. uint32_t backing_id,
  49. bool backing_y_0_top,
  50. uint32_t backing_width,
  51. uint32_t backing_height,
  52. uint32_t x, uint32_t y,
  53. uint32_t w, uint32_t h)
  54. {
  55. egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
  56. edpy->y_0_top = backing_y_0_top;
  57. /* source framebuffer */
  58. egl_fb_setup_for_tex(&edpy->guest_fb,
  59. backing_width, backing_height, backing_id, false);
  60. /* dest framebuffer */
  61. if (edpy->blit_fb.width != backing_width ||
  62. edpy->blit_fb.height != backing_height) {
  63. egl_fb_destroy(&edpy->blit_fb);
  64. egl_fb_setup_new_tex(&edpy->blit_fb, backing_width, backing_height);
  65. }
  66. }
  67. static void egl_scanout_dmabuf(DisplayChangeListener *dcl,
  68. QemuDmaBuf *dmabuf)
  69. {
  70. egl_dmabuf_import_texture(dmabuf);
  71. if (!dmabuf->texture) {
  72. return;
  73. }
  74. egl_scanout_texture(dcl, dmabuf->texture,
  75. false, dmabuf->width, dmabuf->height,
  76. 0, 0, dmabuf->width, dmabuf->height);
  77. }
  78. static void egl_cursor_dmabuf(DisplayChangeListener *dcl,
  79. QemuDmaBuf *dmabuf, bool have_hot,
  80. uint32_t hot_x, uint32_t hot_y)
  81. {
  82. egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
  83. if (dmabuf) {
  84. egl_dmabuf_import_texture(dmabuf);
  85. if (!dmabuf->texture) {
  86. return;
  87. }
  88. egl_fb_setup_for_tex(&edpy->cursor_fb, dmabuf->width, dmabuf->height,
  89. dmabuf->texture, false);
  90. } else {
  91. egl_fb_destroy(&edpy->cursor_fb);
  92. }
  93. }
  94. static void egl_cursor_position(DisplayChangeListener *dcl,
  95. uint32_t pos_x, uint32_t pos_y)
  96. {
  97. egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
  98. edpy->pos_x = pos_x;
  99. edpy->pos_y = pos_y;
  100. }
  101. static void egl_release_dmabuf(DisplayChangeListener *dcl,
  102. QemuDmaBuf *dmabuf)
  103. {
  104. egl_dmabuf_release_texture(dmabuf);
  105. }
  106. static void egl_scanout_flush(DisplayChangeListener *dcl,
  107. uint32_t x, uint32_t y,
  108. uint32_t w, uint32_t h)
  109. {
  110. egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
  111. if (!edpy->guest_fb.texture || !edpy->ds) {
  112. return;
  113. }
  114. assert(surface_format(edpy->ds) == PIXMAN_x8r8g8b8);
  115. if (edpy->cursor_fb.texture) {
  116. /* have cursor -> render using textures */
  117. egl_texture_blit(edpy->gls, &edpy->blit_fb, &edpy->guest_fb,
  118. !edpy->y_0_top);
  119. egl_texture_blend(edpy->gls, &edpy->blit_fb, &edpy->cursor_fb,
  120. !edpy->y_0_top, edpy->pos_x, edpy->pos_y,
  121. 1.0, 1.0);
  122. } else {
  123. /* no cursor -> use simple framebuffer blit */
  124. egl_fb_blit(&edpy->blit_fb, &edpy->guest_fb, edpy->y_0_top);
  125. }
  126. egl_fb_read(edpy->ds, &edpy->blit_fb);
  127. dpy_gfx_update(edpy->dcl.con, x, y, w, h);
  128. }
  129. static const DisplayChangeListenerOps egl_ops = {
  130. .dpy_name = "egl-headless",
  131. .dpy_refresh = egl_refresh,
  132. .dpy_gfx_update = egl_gfx_update,
  133. .dpy_gfx_switch = egl_gfx_switch,
  134. .dpy_gl_ctx_create = egl_create_context,
  135. .dpy_gl_ctx_destroy = qemu_egl_destroy_context,
  136. .dpy_gl_ctx_make_current = qemu_egl_make_context_current,
  137. .dpy_gl_ctx_get_current = qemu_egl_get_current_context,
  138. .dpy_gl_scanout_disable = egl_scanout_disable,
  139. .dpy_gl_scanout_texture = egl_scanout_texture,
  140. .dpy_gl_scanout_dmabuf = egl_scanout_dmabuf,
  141. .dpy_gl_cursor_dmabuf = egl_cursor_dmabuf,
  142. .dpy_gl_cursor_position = egl_cursor_position,
  143. .dpy_gl_release_dmabuf = egl_release_dmabuf,
  144. .dpy_gl_update = egl_scanout_flush,
  145. };
  146. static void early_egl_headless_init(DisplayOptions *opts)
  147. {
  148. display_opengl = 1;
  149. }
  150. static void egl_headless_init(DisplayState *ds, DisplayOptions *opts)
  151. {
  152. DisplayGLMode mode = opts->has_gl ? opts->gl : DISPLAYGL_MODE_ON;
  153. QemuConsole *con;
  154. egl_dpy *edpy;
  155. int idx;
  156. if (egl_rendernode_init(opts->u.egl_headless.rendernode, mode) < 0) {
  157. error_report("egl: render node init failed");
  158. exit(1);
  159. }
  160. for (idx = 0;; idx++) {
  161. con = qemu_console_lookup_by_index(idx);
  162. if (!con || !qemu_console_is_graphic(con)) {
  163. break;
  164. }
  165. edpy = g_new0(egl_dpy, 1);
  166. edpy->dcl.con = con;
  167. edpy->dcl.ops = &egl_ops;
  168. edpy->gls = qemu_gl_init_shader();
  169. register_displaychangelistener(&edpy->dcl);
  170. }
  171. }
  172. static QemuDisplay qemu_display_egl = {
  173. .type = DISPLAY_TYPE_EGL_HEADLESS,
  174. .early_init = early_egl_headless_init,
  175. .init = egl_headless_init,
  176. };
  177. static void register_egl(void)
  178. {
  179. qemu_display_register(&qemu_display_egl);
  180. }
  181. type_init(register_egl);