2
0

gtk-gl-area.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. gdk_gl_context_set_required_version(ctx,
  123. params->major_ver,
  124. params->minor_ver);
  125. gdk_gl_context_realize(ctx, &err);
  126. return ctx;
  127. }
  128. void gd_gl_area_destroy_context(DisplayChangeListener *dcl, QEMUGLContext ctx)
  129. {
  130. /* FIXME */
  131. }
  132. void gd_gl_area_scanout_texture(DisplayChangeListener *dcl,
  133. uint32_t backing_id,
  134. bool backing_y_0_top,
  135. uint32_t backing_width,
  136. uint32_t backing_height,
  137. uint32_t x, uint32_t y,
  138. uint32_t w, uint32_t h)
  139. {
  140. VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
  141. vc->gfx.x = x;
  142. vc->gfx.y = y;
  143. vc->gfx.w = w;
  144. vc->gfx.h = h;
  145. vc->gfx.y0_top = backing_y_0_top;
  146. gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
  147. if (backing_id == 0 || vc->gfx.w == 0 || vc->gfx.h == 0) {
  148. gtk_gl_area_set_scanout_mode(vc, false);
  149. return;
  150. }
  151. gtk_gl_area_set_scanout_mode(vc, true);
  152. egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height,
  153. backing_id, false);
  154. }
  155. void gd_gl_area_scanout_flush(DisplayChangeListener *dcl,
  156. uint32_t x, uint32_t y, uint32_t w, uint32_t h)
  157. {
  158. VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
  159. gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
  160. }
  161. void gtk_gl_area_init(void)
  162. {
  163. display_opengl = 1;
  164. }
  165. QEMUGLContext gd_gl_area_get_current_context(DisplayChangeListener *dcl)
  166. {
  167. return gdk_gl_context_get_current();
  168. }
  169. int gd_gl_area_make_current(DisplayChangeListener *dcl,
  170. QEMUGLContext ctx)
  171. {
  172. gdk_gl_context_make_current(ctx);
  173. return 0;
  174. }