Merge remote-tracking branch 'remotes/dgilbert-gitlab/tags/pull-migration-20210726a...
[qemu/armbru.git] / hw / display / virtio-gpu-gl.c
blob6cc4313b1af2c3fac0011d6b39aa671e92b51499
1 /*
2 * Virtio GPU Device
4 * Copyright Red Hat, Inc. 2013-2014
6 * Authors:
7 * Dave Airlie <airlied@redhat.com>
8 * Gerd Hoffmann <kraxel@redhat.com>
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.
14 #include "qemu/osdep.h"
15 #include "qemu/iov.h"
16 #include "qemu/module.h"
17 #include "qemu/error-report.h"
18 #include "qapi/error.h"
19 #include "sysemu/sysemu.h"
20 #include "hw/virtio/virtio.h"
21 #include "hw/virtio/virtio-gpu.h"
22 #include "hw/virtio/virtio-gpu-bswap.h"
23 #include "hw/virtio/virtio-gpu-pixman.h"
24 #include "hw/qdev-properties.h"
26 #include <virglrenderer.h>
28 static void virtio_gpu_gl_update_cursor_data(VirtIOGPU *g,
29 struct virtio_gpu_scanout *s,
30 uint32_t resource_id)
32 uint32_t width, height;
33 uint32_t pixels, *data;
35 data = virgl_renderer_get_cursor_data(resource_id, &width, &height);
36 if (!data) {
37 return;
40 if (width != s->current_cursor->width ||
41 height != s->current_cursor->height) {
42 free(data);
43 return;
46 pixels = s->current_cursor->width * s->current_cursor->height;
47 memcpy(s->current_cursor->data, data, pixels * sizeof(uint32_t));
48 free(data);
51 static void virtio_gpu_gl_flushed(VirtIOGPUBase *b)
53 VirtIOGPU *g = VIRTIO_GPU(b);
55 virtio_gpu_process_cmdq(g);
58 static void virtio_gpu_gl_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
60 VirtIOGPU *g = VIRTIO_GPU(vdev);
61 VirtIOGPUGL *gl = VIRTIO_GPU_GL(vdev);
62 struct virtio_gpu_ctrl_command *cmd;
64 if (!virtio_queue_ready(vq)) {
65 return;
68 if (!gl->renderer_inited) {
69 virtio_gpu_virgl_init(g);
70 gl->renderer_inited = true;
72 if (gl->renderer_reset) {
73 gl->renderer_reset = false;
74 virtio_gpu_virgl_reset(g);
77 cmd = virtqueue_pop(vq, sizeof(struct virtio_gpu_ctrl_command));
78 while (cmd) {
79 cmd->vq = vq;
80 cmd->error = 0;
81 cmd->finished = false;
82 QTAILQ_INSERT_TAIL(&g->cmdq, cmd, next);
83 cmd = virtqueue_pop(vq, sizeof(struct virtio_gpu_ctrl_command));
86 virtio_gpu_process_cmdq(g);
87 virtio_gpu_virgl_fence_poll(g);
90 static void virtio_gpu_gl_reset(VirtIODevice *vdev)
92 VirtIOGPU *g = VIRTIO_GPU(vdev);
93 VirtIOGPUGL *gl = VIRTIO_GPU_GL(vdev);
95 virtio_gpu_reset(vdev);
98 * GL functions must be called with the associated GL context in main
99 * thread, and when the renderer is unblocked.
101 if (gl->renderer_inited && !gl->renderer_reset) {
102 virtio_gpu_virgl_reset_scanout(g);
103 gl->renderer_reset = true;
107 static void virtio_gpu_gl_device_realize(DeviceState *qdev, Error **errp)
109 VirtIOGPU *g = VIRTIO_GPU(qdev);
111 #if defined(HOST_WORDS_BIGENDIAN)
112 error_setg(errp, "virgl is not supported on bigendian platforms");
113 return;
114 #endif
116 if (!object_resolve_path_type("", TYPE_VIRTIO_GPU_GL, NULL)) {
117 error_setg(errp, "at most one %s device is permitted", TYPE_VIRTIO_GPU_GL);
118 return;
121 if (!display_opengl) {
122 error_setg(errp, "opengl is not available");
123 return;
126 g->parent_obj.conf.flags |= (1 << VIRTIO_GPU_FLAG_VIRGL_ENABLED);
127 VIRTIO_GPU_BASE(g)->virtio_config.num_capsets =
128 virtio_gpu_virgl_get_num_capsets(g);
130 virtio_gpu_device_realize(qdev, errp);
133 static Property virtio_gpu_gl_properties[] = {
134 DEFINE_PROP_BIT("stats", VirtIOGPU, parent_obj.conf.flags,
135 VIRTIO_GPU_FLAG_STATS_ENABLED, false),
136 DEFINE_PROP_END_OF_LIST(),
139 static void virtio_gpu_gl_class_init(ObjectClass *klass, void *data)
141 DeviceClass *dc = DEVICE_CLASS(klass);
142 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
143 VirtIOGPUBaseClass *vbc = VIRTIO_GPU_BASE_CLASS(klass);
144 VirtIOGPUClass *vgc = VIRTIO_GPU_CLASS(klass);
146 vbc->gl_flushed = virtio_gpu_gl_flushed;
147 vgc->handle_ctrl = virtio_gpu_gl_handle_ctrl;
148 vgc->process_cmd = virtio_gpu_virgl_process_cmd;
149 vgc->update_cursor_data = virtio_gpu_gl_update_cursor_data;
151 vdc->realize = virtio_gpu_gl_device_realize;
152 vdc->reset = virtio_gpu_gl_reset;
153 device_class_set_props(dc, virtio_gpu_gl_properties);
156 static const TypeInfo virtio_gpu_gl_info = {
157 .name = TYPE_VIRTIO_GPU_GL,
158 .parent = TYPE_VIRTIO_GPU,
159 .instance_size = sizeof(VirtIOGPUGL),
160 .class_init = virtio_gpu_gl_class_init,
162 module_obj(TYPE_VIRTIO_GPU_GL);
164 static void virtio_register_types(void)
166 type_register_static(&virtio_gpu_gl_info);
169 type_init(virtio_register_types)
171 module_dep("hw-display-virtio-gpu");