egl-headless.c 6.9 KB

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