xref: /qemu/hw/display/virtio-gpu-gl.c (revision c4b8ffcb)
1 /*
2  * Virtio GPU Device
3  *
4  * Copyright Red Hat, Inc. 2013-2014
5  *
6  * Authors:
7  *     Dave Airlie <airlied@redhat.com>
8  *     Gerd Hoffmann <kraxel@redhat.com>
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 
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"
25 
26 #include <virglrenderer.h>
27 
28 static void virtio_gpu_gl_update_cursor_data(VirtIOGPU *g,
29                                              struct virtio_gpu_scanout *s,
30                                              uint32_t resource_id)
31 {
32     uint32_t width, height;
33     uint32_t pixels, *data;
34 
35     data = virgl_renderer_get_cursor_data(resource_id, &width, &height);
36     if (!data) {
37         return;
38     }
39 
40     if (width != s->current_cursor->width ||
41         height != s->current_cursor->height) {
42         free(data);
43         return;
44     }
45 
46     pixels = s->current_cursor->width * s->current_cursor->height;
47     memcpy(s->current_cursor->data, data, pixels * sizeof(uint32_t));
48     free(data);
49 }
50 
51 static void virtio_gpu_gl_flushed(VirtIOGPUBase *b)
52 {
53     VirtIOGPU *g = VIRTIO_GPU(b);
54 
55     virtio_gpu_process_cmdq(g);
56 }
57 
58 static void virtio_gpu_gl_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
59 {
60     VirtIOGPU *g = VIRTIO_GPU(vdev);
61     VirtIOGPUGL *gl = VIRTIO_GPU_GL(vdev);
62     struct virtio_gpu_ctrl_command *cmd;
63 
64     if (!virtio_queue_ready(vq)) {
65         return;
66     }
67 
68     if (!gl->renderer_inited) {
69         virtio_gpu_virgl_init(g);
70         gl->renderer_inited = true;
71     }
72     if (gl->renderer_reset) {
73         gl->renderer_reset = false;
74         virtio_gpu_virgl_reset(g);
75     }
76 
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));
84     }
85 
86     virtio_gpu_process_cmdq(g);
87     virtio_gpu_virgl_fence_poll(g);
88 }
89 
90 static void virtio_gpu_gl_reset(VirtIODevice *vdev)
91 {
92     VirtIOGPU *g = VIRTIO_GPU(vdev);
93     VirtIOGPUGL *gl = VIRTIO_GPU_GL(vdev);
94 
95     virtio_gpu_reset(vdev);
96 
97     /*
98      * GL functions must be called with the associated GL context in main
99      * thread, and when the renderer is unblocked.
100      */
101     if (gl->renderer_inited && !gl->renderer_reset) {
102         virtio_gpu_virgl_reset_scanout(g);
103         gl->renderer_reset = true;
104     }
105 }
106 
107 static void virtio_gpu_gl_device_realize(DeviceState *qdev, Error **errp)
108 {
109     VirtIOGPU *g = VIRTIO_GPU(qdev);
110 
111 #if HOST_BIG_ENDIAN
112     error_setg(errp, "virgl is not supported on bigendian platforms");
113     return;
114 #endif
115 
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;
119     }
120 
121     if (!display_opengl) {
122         error_setg(errp, "opengl is not available");
123         return;
124     }
125 
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);
129 
130     virtio_gpu_device_realize(qdev, errp);
131 }
132 
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(),
137 };
138 
139 static void virtio_gpu_gl_class_init(ObjectClass *klass, void *data)
140 {
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);
145 
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;
150 
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);
154 }
155 
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,
161 };
162 module_obj(TYPE_VIRTIO_GPU_GL);
163 module_kconfig(VIRTIO_GPU);
164 
165 static void virtio_register_types(void)
166 {
167     type_register_static(&virtio_gpu_gl_info);
168 }
169 
170 type_init(virtio_register_types)
171 
172 module_dep("hw-display-virtio-gpu");
173