1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
/*
* GTK UI -- glarea opengl code.
*
* Requires 3.16+ (GtkGLArea widget).
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*/
#include "qemu/osdep.h"
#include "qemu/main-loop.h"
#include "trace.h"
#include "ui/console.h"
#include "ui/gtk.h"
#include "ui/egl-helpers.h"
#include "sysemu/sysemu.h"
static void gtk_gl_area_set_scanout_mode(VirtualConsole *vc, bool scanout)
{
if (vc->gfx.scanout_mode == scanout) {
return;
}
vc->gfx.scanout_mode = scanout;
if (!vc->gfx.scanout_mode) {
gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
egl_fb_destroy(&vc->gfx.guest_fb);
if (vc->gfx.surface) {
surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
}
}
}
/** DisplayState Callbacks (opengl version) **/
void gd_gl_area_draw(VirtualConsole *vc)
{
#ifdef CONFIG_GBM
QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf;
#endif
int ww, wh, ws, y1, y2;
if (!vc->gfx.gls) {
return;
}
gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
ws = gdk_window_get_scale_factor(gtk_widget_get_window(vc->gfx.drawing_area));
ww = gtk_widget_get_allocated_width(vc->gfx.drawing_area) * ws;
wh = gtk_widget_get_allocated_height(vc->gfx.drawing_area) * ws;
if (vc->gfx.scanout_mode) {
if (!vc->gfx.guest_fb.framebuffer) {
return;
}
#ifdef CONFIG_GBM
if (dmabuf) {
if (!qemu_dmabuf_get_draw_submitted(dmabuf)) {
return;
} else {
qemu_dmabuf_set_draw_submitted(dmabuf, false);
}
}
#endif
glBindFramebuffer(GL_READ_FRAMEBUFFER, vc->gfx.guest_fb.framebuffer);
/* GtkGLArea sets GL_DRAW_FRAMEBUFFER for us */
glViewport(0, 0, ww, wh);
y1 = vc->gfx.y0_top ? 0 : vc->gfx.h;
y2 = vc->gfx.y0_top ? vc->gfx.h : 0;
glBlitFramebuffer(0, y1, vc->gfx.w, y2,
0, 0, ww, wh,
GL_COLOR_BUFFER_BIT, GL_NEAREST);
#ifdef CONFIG_GBM
if (dmabuf) {
egl_dmabuf_create_sync(dmabuf);
}
#endif
glFlush();
#ifdef CONFIG_GBM
if (dmabuf) {
int fence_fd;
egl_dmabuf_create_fence(dmabuf);
fence_fd = qemu_dmabuf_get_fence_fd(dmabuf);
if (fence_fd >= 0) {
qemu_set_fd_handler(fence_fd, gd_hw_gl_flushed, NULL, vc);
return;
}
graphic_hw_gl_block(vc->gfx.dcl.con, false);
}
#endif
} else {
if (!vc->gfx.ds) {
return;
}
gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, ww, wh);
surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds);
}
}
void gd_gl_area_update(DisplayChangeListener *dcl,
int x, int y, int w, int h)
{
VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
if (!vc->gfx.gls || !vc->gfx.ds) {
return;
}
gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
surface_gl_update_texture(vc->gfx.gls, vc->gfx.ds, x, y, w, h);
vc->gfx.glupdates++;
gdk_gl_context_clear_current();
}
void gd_gl_area_refresh(DisplayChangeListener *dcl)
{
VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
gd_update_monitor_refresh_rate(vc, vc->window ? vc->window : vc->gfx.drawing_area);
if (vc->gfx.guest_fb.dmabuf &&
qemu_dmabuf_get_draw_submitted(vc->gfx.guest_fb.dmabuf)) {
gd_gl_area_draw(vc);
return;
}
if (!vc->gfx.gls) {
if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
return;
}
gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
vc->gfx.gls = qemu_gl_init_shader();
if (vc->gfx.ds) {
surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
}
}
graphic_hw_update(dcl->con);
if (vc->gfx.glupdates) {
vc->gfx.glupdates = 0;
gtk_gl_area_set_scanout_mode(vc, false);
gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
}
}
void gd_gl_area_switch(DisplayChangeListener *dcl,
DisplaySurface *surface)
{
VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
bool resized = true;
trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
if (vc->gfx.ds &&
surface_width(vc->gfx.ds) == surface_width(surface) &&
surface_height(vc->gfx.ds) == surface_height(surface)) {
resized = false;
}
if (vc->gfx.gls) {
gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
surface_gl_create_texture(vc->gfx.gls, surface);
}
vc->gfx.ds = surface;
if (resized) {
gd_update_windowsize(vc);
}
}
static int gd_cmp_gl_context_version(int major, int minor, QEMUGLParams *params)
{
if (major > params->major_ver) {
return 1;
}
if (major < params->major_ver) {
return -1;
}
if (minor > params->minor_ver) {
return 1;
}
if (minor < params->minor_ver) {
return -1;
}
return 0;
}
QEMUGLContext gd_gl_area_create_context(DisplayGLCtx *dgc,
QEMUGLParams *params)
{
VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc);
GdkWindow *window;
GdkGLContext *ctx;
GError *err = NULL;
int major, minor;
window = gtk_widget_get_window(vc->gfx.drawing_area);
ctx = gdk_window_create_gl_context(window, &err);
if (err) {
g_printerr("Create gdk gl context failed: %s\n", err->message);
g_error_free(err);
return NULL;
}
gdk_gl_context_set_required_version(ctx,
params->major_ver,
params->minor_ver);
gdk_gl_context_realize(ctx, &err);
if (err) {
g_printerr("Realize gdk gl context failed: %s\n", err->message);
g_error_free(err);
g_clear_object(&ctx);
return NULL;
}
gdk_gl_context_make_current(ctx);
gdk_gl_context_get_version(ctx, &major, &minor);
gdk_gl_context_clear_current();
gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
if (gd_cmp_gl_context_version(major, minor, params) == -1) {
/* created ctx version < requested version */
g_clear_object(&ctx);
}
trace_gd_gl_area_create_context(ctx, params->major_ver, params->minor_ver);
return ctx;
}
void gd_gl_area_destroy_context(DisplayGLCtx *dgc, QEMUGLContext ctx)
{
GdkGLContext *current_ctx = gdk_gl_context_get_current();
trace_gd_gl_area_destroy_context(ctx, current_ctx);
if (ctx == current_ctx) {
gdk_gl_context_clear_current();
}
g_clear_object(&ctx);
}
void gd_gl_area_scanout_texture(DisplayChangeListener *dcl,
uint32_t backing_id,
bool backing_y_0_top,
uint32_t backing_width,
uint32_t backing_height,
uint32_t x, uint32_t y,
uint32_t w, uint32_t h,
void *d3d_tex2d)
{
VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
vc->gfx.x = x;
vc->gfx.y = y;
vc->gfx.w = w;
vc->gfx.h = h;
vc->gfx.y0_top = backing_y_0_top;
gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
if (backing_id == 0 || vc->gfx.w == 0 || vc->gfx.h == 0) {
gtk_gl_area_set_scanout_mode(vc, false);
return;
}
gtk_gl_area_set_scanout_mode(vc, true);
egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height,
backing_id, false);
}
void gd_gl_area_scanout_disable(DisplayChangeListener *dcl)
{
VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
gtk_gl_area_set_scanout_mode(vc, false);
}
void gd_gl_area_scanout_flush(DisplayChangeListener *dcl,
uint32_t x, uint32_t y, uint32_t w, uint32_t h)
{
VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
if (vc->gfx.guest_fb.dmabuf &&
!qemu_dmabuf_get_draw_submitted(vc->gfx.guest_fb.dmabuf)) {
graphic_hw_gl_block(vc->gfx.dcl.con, true);
qemu_dmabuf_set_draw_submitted(vc->gfx.guest_fb.dmabuf, true);
gtk_gl_area_set_scanout_mode(vc, true);
}
gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
}
void gd_gl_area_scanout_dmabuf(DisplayChangeListener *dcl,
QemuDmaBuf *dmabuf)
{
#ifdef CONFIG_GBM
VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
uint32_t x, y, width, height, backing_width, backing_height, texture;
bool y0_top;
gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
egl_dmabuf_import_texture(dmabuf);
texture = qemu_dmabuf_get_texture(dmabuf);
if (!texture) {
return;
}
x = qemu_dmabuf_get_x(dmabuf);
y = qemu_dmabuf_get_y(dmabuf);
width = qemu_dmabuf_get_width(dmabuf);
height = qemu_dmabuf_get_height(dmabuf);
backing_width = qemu_dmabuf_get_backing_width(dmabuf);
backing_height = qemu_dmabuf_get_backing_height(dmabuf);
y0_top = qemu_dmabuf_get_y0_top(dmabuf);
gd_gl_area_scanout_texture(dcl, texture, y0_top,
backing_width, backing_height,
x, y, width, height, NULL);
if (qemu_dmabuf_get_allow_fences(dmabuf)) {
vc->gfx.guest_fb.dmabuf = dmabuf;
}
#endif
}
void gtk_gl_area_init(void)
{
display_opengl = 1;
}
int gd_gl_area_make_current(DisplayGLCtx *dgc,
QEMUGLContext ctx)
{
gdk_gl_context_make_current(ctx);
return 0;
}
|