gtk-gl-area.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * GTK UI -- glarea opengl code.
  3. *
  4. * Requires 3.16+ (GtkGLArea widget).
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  7. * See the COPYING file in the top-level directory.
  8. */
  9. #include "qemu/osdep.h"
  10. #include "trace.h"
  11. #include "ui/console.h"
  12. #include "ui/gtk.h"
  13. #include "ui/egl-helpers.h"
  14. #include "sysemu/sysemu.h"
  15. static void gtk_gl_area_set_scanout_mode(VirtualConsole *vc, bool scanout)
  16. {
  17. if (vc->gfx.scanout_mode == scanout) {
  18. return;
  19. }
  20. vc->gfx.scanout_mode = scanout;
  21. if (!vc->gfx.scanout_mode) {
  22. egl_fb_destroy(&vc->gfx.guest_fb);
  23. if (vc->gfx.surface) {
  24. surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
  25. surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
  26. }
  27. }
  28. }
  29. /** DisplayState Callbacks (opengl version) **/
  30. void gd_gl_area_draw(VirtualConsole *vc)
  31. {
  32. int ww, wh, y1, y2;
  33. if (!vc->gfx.gls) {
  34. return;
  35. }
  36. gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
  37. ww = gtk_widget_get_allocated_width(vc->gfx.drawing_area);
  38. wh = gtk_widget_get_allocated_height(vc->gfx.drawing_area);
  39. if (vc->gfx.scanout_mode) {
  40. if (!vc->gfx.guest_fb.framebuffer) {
  41. return;
  42. }
  43. glBindFramebuffer(GL_READ_FRAMEBUFFER, vc->gfx.guest_fb.framebuffer);
  44. /* GtkGLArea sets GL_DRAW_FRAMEBUFFER for us */
  45. glViewport(0, 0, ww, wh);
  46. y1 = vc->gfx.y0_top ? 0 : vc->gfx.h;
  47. y2 = vc->gfx.y0_top ? vc->gfx.h : 0;
  48. glBlitFramebuffer(0, y1, vc->gfx.w, y2,
  49. 0, 0, ww, wh,
  50. GL_COLOR_BUFFER_BIT, GL_NEAREST);
  51. } else {
  52. if (!vc->gfx.ds) {
  53. return;
  54. }
  55. gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
  56. surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, ww, wh);
  57. surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds);
  58. }
  59. }
  60. void gd_gl_area_update(DisplayChangeListener *dcl,
  61. int x, int y, int w, int h)
  62. {
  63. VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
  64. if (!vc->gfx.gls || !vc->gfx.ds) {
  65. return;
  66. }
  67. gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
  68. surface_gl_update_texture(vc->gfx.gls, vc->gfx.ds, x, y, w, h);
  69. vc->gfx.glupdates++;
  70. }
  71. void gd_gl_area_refresh(DisplayChangeListener *dcl)
  72. {
  73. VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
  74. if (!vc->gfx.gls) {
  75. if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
  76. return;
  77. }
  78. gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
  79. vc->gfx.gls = qemu_gl_init_shader();
  80. if (vc->gfx.ds) {
  81. surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
  82. }
  83. }
  84. graphic_hw_update(dcl->con);
  85. if (vc->gfx.glupdates) {
  86. vc->gfx.glupdates = 0;
  87. gtk_gl_area_set_scanout_mode(vc, false);
  88. gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
  89. }
  90. }
  91. void gd_gl_area_switch(DisplayChangeListener *dcl,
  92. DisplaySurface *surface)
  93. {
  94. VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
  95. bool resized = true;
  96. trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
  97. if (vc->gfx.ds &&
  98. surface_width(vc->gfx.ds) == surface_width(surface) &&
  99. surface_height(vc->gfx.ds) == surface_height(surface)) {
  100. resized = false;
  101. }
  102. if (vc->gfx.gls) {
  103. gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
  104. surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
  105. surface_gl_create_texture(vc->gfx.gls, surface);
  106. }
  107. vc->gfx.ds = surface;
  108. if (resized) {
  109. gd_update_windowsize(vc);
  110. }
  111. }
  112. QEMUGLContext gd_gl_area_create_context(DisplayChangeListener *dcl,
  113. QEMUGLParams *params)
  114. {
  115. VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
  116. GdkWindow *window;
  117. GdkGLContext *ctx;
  118. GError *err = NULL;
  119. gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
  120. window = gtk_widget_get_window(vc->gfx.drawing_area);
  121. ctx = gdk_window_create_gl_context(window, &err);
  122. if (err) {
  123. g_printerr("Create gdk gl context failed: %s\n", err->message);
  124. g_error_free(err);
  125. return NULL;
  126. }
  127. gdk_gl_context_set_required_version(ctx,
  128. params->major_ver,
  129. params->minor_ver);
  130. gdk_gl_context_realize(ctx, &err);
  131. if (err) {
  132. g_printerr("Realize gdk gl context failed: %s\n", err->message);
  133. g_error_free(err);
  134. g_clear_object(&ctx);
  135. return NULL;
  136. }
  137. return ctx;
  138. }
  139. void gd_gl_area_destroy_context(DisplayChangeListener *dcl, QEMUGLContext ctx)
  140. {
  141. /* FIXME */
  142. }
  143. void gd_gl_area_scanout_texture(DisplayChangeListener *dcl,
  144. uint32_t backing_id,
  145. bool backing_y_0_top,
  146. uint32_t backing_width,
  147. uint32_t backing_height,
  148. uint32_t x, uint32_t y,
  149. uint32_t w, uint32_t h)
  150. {
  151. VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
  152. vc->gfx.x = x;
  153. vc->gfx.y = y;
  154. vc->gfx.w = w;
  155. vc->gfx.h = h;
  156. vc->gfx.y0_top = backing_y_0_top;
  157. gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
  158. if (backing_id == 0 || vc->gfx.w == 0 || vc->gfx.h == 0) {
  159. gtk_gl_area_set_scanout_mode(vc, false);
  160. return;
  161. }
  162. gtk_gl_area_set_scanout_mode(vc, true);
  163. egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height,
  164. backing_id, false);
  165. }
  166. void gd_gl_area_scanout_flush(DisplayChangeListener *dcl,
  167. uint32_t x, uint32_t y, uint32_t w, uint32_t h)
  168. {
  169. VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
  170. gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
  171. }
  172. void gtk_gl_area_init(void)
  173. {
  174. display_opengl = 1;
  175. }
  176. QEMUGLContext gd_gl_area_get_current_context(DisplayChangeListener *dcl)
  177. {
  178. return gdk_gl_context_get_current();
  179. }
  180. int gd_gl_area_make_current(DisplayChangeListener *dcl,
  181. QEMUGLContext ctx)
  182. {
  183. gdk_gl_context_make_current(ctx);
  184. return 0;
  185. }