gtk-egl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * GTK UI -- egl opengl code.
  3. *
  4. * Note that gtk 3.16+ (released 2015-03-23) has a GtkGLArea widget,
  5. * which is GtkDrawingArea like widget with opengl rendering support.
  6. *
  7. * This code handles opengl support on older gtk versions, using egl
  8. * to get a opengl context for the X11 window.
  9. *
  10. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  11. * See the COPYING file in the top-level directory.
  12. */
  13. #include "qemu/osdep.h"
  14. #include "qemu/main-loop.h"
  15. #include "trace.h"
  16. #include "ui/console.h"
  17. #include "ui/gtk.h"
  18. #include "ui/egl-helpers.h"
  19. #include "ui/shader.h"
  20. #include "sysemu/sysemu.h"
  21. static void gtk_egl_set_scanout_mode(VirtualConsole *vc, bool scanout)
  22. {
  23. if (vc->gfx.scanout_mode == scanout) {
  24. return;
  25. }
  26. vc->gfx.scanout_mode = scanout;
  27. if (!vc->gfx.scanout_mode) {
  28. egl_fb_destroy(&vc->gfx.guest_fb);
  29. if (vc->gfx.surface) {
  30. surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
  31. surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
  32. }
  33. }
  34. }
  35. /** DisplayState Callbacks (opengl version) **/
  36. void gd_egl_init(VirtualConsole *vc)
  37. {
  38. GdkWindow *gdk_window = gtk_widget_get_window(vc->gfx.drawing_area);
  39. if (!gdk_window) {
  40. return;
  41. }
  42. Window x11_window = gdk_x11_window_get_xid(gdk_window);
  43. if (!x11_window) {
  44. return;
  45. }
  46. vc->gfx.ectx = qemu_egl_init_ctx();
  47. vc->gfx.esurface = qemu_egl_init_surface
  48. (vc->gfx.ectx, (EGLNativeWindowType)x11_window);
  49. assert(vc->gfx.esurface);
  50. }
  51. void gd_egl_draw(VirtualConsole *vc)
  52. {
  53. GdkWindow *window;
  54. #ifdef CONFIG_GBM
  55. QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf;
  56. #endif
  57. int ww, wh;
  58. if (!vc->gfx.gls) {
  59. return;
  60. }
  61. window = gtk_widget_get_window(vc->gfx.drawing_area);
  62. ww = gdk_window_get_width(window);
  63. wh = gdk_window_get_height(window);
  64. if (vc->gfx.scanout_mode) {
  65. #ifdef CONFIG_GBM
  66. if (dmabuf) {
  67. if (!dmabuf->draw_submitted) {
  68. return;
  69. } else {
  70. dmabuf->draw_submitted = false;
  71. }
  72. }
  73. #endif
  74. gd_egl_scanout_flush(&vc->gfx.dcl, 0, 0, vc->gfx.w, vc->gfx.h);
  75. vc->gfx.scale_x = (double)ww / vc->gfx.w;
  76. vc->gfx.scale_y = (double)wh / vc->gfx.h;
  77. glFlush();
  78. #ifdef CONFIG_GBM
  79. if (dmabuf) {
  80. egl_dmabuf_create_fence(dmabuf);
  81. if (dmabuf->fence_fd > 0) {
  82. qemu_set_fd_handler(dmabuf->fence_fd, gd_hw_gl_flushed, NULL, vc);
  83. return;
  84. }
  85. graphic_hw_gl_block(vc->gfx.dcl.con, false);
  86. }
  87. #endif
  88. } else {
  89. if (!vc->gfx.ds) {
  90. return;
  91. }
  92. eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
  93. vc->gfx.esurface, vc->gfx.ectx);
  94. surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, ww, wh);
  95. surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds);
  96. eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
  97. vc->gfx.scale_x = (double)ww / surface_width(vc->gfx.ds);
  98. vc->gfx.scale_y = (double)wh / surface_height(vc->gfx.ds);
  99. glFlush();
  100. }
  101. }
  102. void gd_egl_update(DisplayChangeListener *dcl,
  103. int x, int y, int w, int h)
  104. {
  105. VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
  106. if (!vc->gfx.gls || !vc->gfx.ds) {
  107. return;
  108. }
  109. eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
  110. vc->gfx.esurface, vc->gfx.ectx);
  111. surface_gl_update_texture(vc->gfx.gls, vc->gfx.ds, x, y, w, h);
  112. vc->gfx.glupdates++;
  113. }
  114. void gd_egl_refresh(DisplayChangeListener *dcl)
  115. {
  116. VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
  117. gd_update_monitor_refresh_rate(
  118. vc, vc->window ? vc->window : vc->gfx.drawing_area);
  119. if (!vc->gfx.esurface) {
  120. gd_egl_init(vc);
  121. if (!vc->gfx.esurface) {
  122. return;
  123. }
  124. vc->gfx.gls = qemu_gl_init_shader();
  125. if (vc->gfx.ds) {
  126. surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
  127. surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
  128. }
  129. #ifdef CONFIG_GBM
  130. if (vc->gfx.guest_fb.dmabuf) {
  131. egl_dmabuf_release_texture(vc->gfx.guest_fb.dmabuf);
  132. gd_egl_scanout_dmabuf(dcl, vc->gfx.guest_fb.dmabuf);
  133. }
  134. #endif
  135. }
  136. graphic_hw_update(dcl->con);
  137. if (vc->gfx.glupdates) {
  138. vc->gfx.glupdates = 0;
  139. gtk_egl_set_scanout_mode(vc, false);
  140. gd_egl_draw(vc);
  141. }
  142. }
  143. void gd_egl_switch(DisplayChangeListener *dcl,
  144. DisplaySurface *surface)
  145. {
  146. VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
  147. bool resized = true;
  148. trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
  149. if (vc->gfx.ds &&
  150. surface_width(vc->gfx.ds) == surface_width(surface) &&
  151. surface_height(vc->gfx.ds) == surface_height(surface)) {
  152. resized = false;
  153. }
  154. eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
  155. vc->gfx.esurface, vc->gfx.ectx);
  156. surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
  157. vc->gfx.ds = surface;
  158. if (vc->gfx.gls) {
  159. surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
  160. }
  161. if (resized) {
  162. gd_update_windowsize(vc);
  163. }
  164. eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
  165. EGL_NO_CONTEXT);
  166. }
  167. QEMUGLContext gd_egl_create_context(DisplayGLCtx *dgc,
  168. QEMUGLParams *params)
  169. {
  170. VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc);
  171. eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
  172. vc->gfx.esurface, vc->gfx.ectx);
  173. return qemu_egl_create_context(dgc, params);
  174. }
  175. void gd_egl_scanout_disable(DisplayChangeListener *dcl)
  176. {
  177. VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
  178. vc->gfx.w = 0;
  179. vc->gfx.h = 0;
  180. gtk_egl_set_scanout_mode(vc, false);
  181. }
  182. static void gd_egl_scanout_borrowed_texture(VirtualConsole *vc,
  183. uint32_t backing_id,
  184. bool backing_y_0_top,
  185. uint32_t backing_width,
  186. uint32_t backing_height,
  187. uint32_t x, uint32_t y,
  188. uint32_t w, uint32_t h)
  189. {
  190. vc->gfx.x = x;
  191. vc->gfx.y = y;
  192. vc->gfx.w = w;
  193. vc->gfx.h = h;
  194. vc->gfx.y0_top = backing_y_0_top;
  195. eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
  196. vc->gfx.esurface, vc->gfx.ectx);
  197. gtk_egl_set_scanout_mode(vc, true);
  198. egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height,
  199. backing_id, false);
  200. }
  201. void gd_egl_scanout_texture(DisplayChangeListener *dcl, uint32_t backing_id,
  202. DisplayGLTextureBorrower backing_borrow,
  203. uint32_t x, uint32_t y,
  204. uint32_t w, uint32_t h)
  205. {
  206. VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
  207. bool backing_y_0_top;
  208. uint32_t backing_width;
  209. uint32_t backing_height;
  210. GLuint backing_texture = backing_borrow(backing_id, &backing_y_0_top,
  211. &backing_width, &backing_height);
  212. gd_egl_scanout_borrowed_texture(vc, backing_texture, backing_y_0_top,
  213. backing_width, backing_height,
  214. x, y, w, h);
  215. }
  216. void gd_egl_scanout_dmabuf(DisplayChangeListener *dcl, QemuDmaBuf *dmabuf)
  217. {
  218. #ifdef CONFIG_GBM
  219. VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
  220. eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
  221. vc->gfx.esurface, vc->gfx.ectx);
  222. egl_dmabuf_import_texture(dmabuf);
  223. if (!dmabuf->texture) {
  224. return;
  225. }
  226. gd_egl_scanout_borrowed_texture(vc, dmabuf->texture,
  227. false, dmabuf->width, dmabuf->height,
  228. 0, 0, dmabuf->width, dmabuf->height);
  229. if (dmabuf->allow_fences) {
  230. vc->gfx.guest_fb.dmabuf = dmabuf;
  231. }
  232. #endif
  233. }
  234. void gd_egl_cursor_dmabuf(DisplayChangeListener *dcl,
  235. QemuDmaBuf *dmabuf, bool have_hot,
  236. uint32_t hot_x, uint32_t hot_y)
  237. {
  238. #ifdef CONFIG_GBM
  239. VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
  240. if (dmabuf) {
  241. egl_dmabuf_import_texture(dmabuf);
  242. if (!dmabuf->texture) {
  243. return;
  244. }
  245. egl_fb_setup_for_tex(&vc->gfx.cursor_fb, dmabuf->width, dmabuf->height,
  246. dmabuf->texture, false);
  247. } else {
  248. egl_fb_destroy(&vc->gfx.cursor_fb);
  249. }
  250. #endif
  251. }
  252. void gd_egl_cursor_position(DisplayChangeListener *dcl,
  253. uint32_t pos_x, uint32_t pos_y)
  254. {
  255. VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
  256. vc->gfx.cursor_x = pos_x * vc->gfx.scale_x;
  257. vc->gfx.cursor_y = pos_y * vc->gfx.scale_y;
  258. }
  259. void gd_egl_scanout_flush(DisplayChangeListener *dcl,
  260. uint32_t x, uint32_t y, uint32_t w, uint32_t h)
  261. {
  262. VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
  263. GdkWindow *window;
  264. int ww, wh;
  265. if (!vc->gfx.scanout_mode) {
  266. return;
  267. }
  268. if (!vc->gfx.guest_fb.framebuffer) {
  269. return;
  270. }
  271. eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
  272. vc->gfx.esurface, vc->gfx.ectx);
  273. window = gtk_widget_get_window(vc->gfx.drawing_area);
  274. ww = gdk_window_get_width(window);
  275. wh = gdk_window_get_height(window);
  276. egl_fb_setup_default(&vc->gfx.win_fb, ww, wh);
  277. if (vc->gfx.cursor_fb.texture) {
  278. egl_texture_blit(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.guest_fb,
  279. vc->gfx.y0_top, false);
  280. egl_texture_blend(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.cursor_fb,
  281. vc->gfx.y0_top, false,
  282. vc->gfx.cursor_x, vc->gfx.cursor_y,
  283. vc->gfx.scale_x, vc->gfx.scale_y);
  284. } else {
  285. egl_fb_blit(&vc->gfx.win_fb, &vc->gfx.guest_fb, !vc->gfx.y0_top);
  286. }
  287. #ifdef CONFIG_GBM
  288. if (vc->gfx.guest_fb.dmabuf) {
  289. egl_dmabuf_create_sync(vc->gfx.guest_fb.dmabuf);
  290. }
  291. #endif
  292. eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
  293. }
  294. void gd_egl_flush(DisplayChangeListener *dcl,
  295. uint32_t x, uint32_t y, uint32_t w, uint32_t h)
  296. {
  297. VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
  298. GtkWidget *area = vc->gfx.drawing_area;
  299. if (vc->gfx.guest_fb.dmabuf && !vc->gfx.guest_fb.dmabuf->draw_submitted) {
  300. graphic_hw_gl_block(vc->gfx.dcl.con, true);
  301. vc->gfx.guest_fb.dmabuf->draw_submitted = true;
  302. gtk_widget_queue_draw_area(area, x, y, w, h);
  303. return;
  304. }
  305. gd_egl_scanout_flush(&vc->gfx.dcl, x, y, w, h);
  306. }
  307. void gtk_egl_init(DisplayGLMode mode)
  308. {
  309. GdkDisplay *gdk_display = gdk_display_get_default();
  310. Display *x11_display = gdk_x11_display_get_xdisplay(gdk_display);
  311. if (qemu_egl_init_dpy_x11(x11_display, mode) < 0) {
  312. return;
  313. }
  314. display_opengl = 1;
  315. }
  316. int gd_egl_make_current(DisplayGLCtx *dgc,
  317. QEMUGLContext ctx)
  318. {
  319. VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc);
  320. return eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
  321. vc->gfx.esurface, ctx);
  322. }