4 * Copyright Red Hat, Inc. 2013-2014
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"
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
,
32 uint32_t width
, height
;
33 uint32_t pixels
, *data
;
35 if (g
->parent_obj
.use_virgl_renderer
) {
36 data
= virgl_renderer_get_cursor_data(resource_id
, &width
, &height
);
41 if (width
!= s
->current_cursor
->width
||
42 height
!= s
->current_cursor
->height
) {
47 pixels
= s
->current_cursor
->width
* s
->current_cursor
->height
;
48 memcpy(s
->current_cursor
->data
, data
, pixels
* sizeof(uint32_t));
52 virtio_gpu_update_cursor_data(g
, s
, resource_id
);
55 static void virtio_gpu_gl_process_cmd(VirtIOGPU
*g
,
56 struct virtio_gpu_ctrl_command
*cmd
)
58 if (g
->parent_obj
.use_virgl_renderer
) {
59 virtio_gpu_virgl_process_cmd(g
, cmd
);
62 virtio_gpu_simple_process_cmd(g
, cmd
);
65 static void virtio_gpu_gl_flushed(VirtIOGPUBase
*b
)
67 VirtIOGPU
*g
= VIRTIO_GPU(b
);
69 if (g
->renderer_reset
) {
70 g
->renderer_reset
= false;
71 virtio_gpu_virgl_reset(g
);
73 virtio_gpu_process_cmdq(g
);
76 static void virtio_gpu_gl_handle_ctrl(VirtIODevice
*vdev
, VirtQueue
*vq
)
78 VirtIOGPU
*g
= VIRTIO_GPU(vdev
);
79 struct virtio_gpu_ctrl_command
*cmd
;
81 if (!virtio_queue_ready(vq
)) {
85 if (!g
->renderer_inited
&& g
->parent_obj
.use_virgl_renderer
) {
86 virtio_gpu_virgl_init(g
);
87 g
->renderer_inited
= true;
90 cmd
= virtqueue_pop(vq
, sizeof(struct virtio_gpu_ctrl_command
));
94 cmd
->finished
= false;
95 QTAILQ_INSERT_TAIL(&g
->cmdq
, cmd
, next
);
96 cmd
= virtqueue_pop(vq
, sizeof(struct virtio_gpu_ctrl_command
));
99 virtio_gpu_process_cmdq(g
);
101 if (g
->parent_obj
.use_virgl_renderer
) {
102 virtio_gpu_virgl_fence_poll(g
);
106 static void virtio_gpu_gl_reset(VirtIODevice
*vdev
)
108 VirtIOGPU
*g
= VIRTIO_GPU(vdev
);
110 virtio_gpu_reset(vdev
);
112 if (g
->parent_obj
.use_virgl_renderer
) {
113 if (g
->parent_obj
.renderer_blocked
) {
114 g
->renderer_reset
= true;
116 virtio_gpu_virgl_reset(g
);
118 g
->parent_obj
.use_virgl_renderer
= false;
122 static void virtio_gpu_gl_device_realize(DeviceState
*qdev
, Error
**errp
)
124 VirtIOGPU
*g
= VIRTIO_GPU(qdev
);
126 #if defined(HOST_WORDS_BIGENDIAN)
127 error_setg(errp
, "virgl is not supported on bigendian platforms");
131 if (!display_opengl
) {
132 error_setg(errp
, "opengl is not available");
136 g
->parent_obj
.conf
.flags
|= (1 << VIRTIO_GPU_FLAG_VIRGL_ENABLED
);
137 VIRTIO_GPU_BASE(g
)->virtio_config
.num_capsets
=
138 virtio_gpu_virgl_get_num_capsets(g
);
140 virtio_gpu_device_realize(qdev
, errp
);
143 static Property virtio_gpu_gl_properties
[] = {
144 DEFINE_PROP_BIT("stats", VirtIOGPU
, parent_obj
.conf
.flags
,
145 VIRTIO_GPU_FLAG_STATS_ENABLED
, false),
146 DEFINE_PROP_END_OF_LIST(),
149 static void virtio_gpu_gl_class_init(ObjectClass
*klass
, void *data
)
151 DeviceClass
*dc
= DEVICE_CLASS(klass
);
152 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
153 VirtIOGPUBaseClass
*vbc
= VIRTIO_GPU_BASE_CLASS(klass
);
154 VirtIOGPUClass
*vgc
= VIRTIO_GPU_CLASS(klass
);
156 vbc
->gl_flushed
= virtio_gpu_gl_flushed
;
157 vgc
->handle_ctrl
= virtio_gpu_gl_handle_ctrl
;
158 vgc
->process_cmd
= virtio_gpu_gl_process_cmd
;
159 vgc
->update_cursor_data
= virtio_gpu_gl_update_cursor_data
;
161 vdc
->realize
= virtio_gpu_gl_device_realize
;
162 vdc
->reset
= virtio_gpu_gl_reset
;
163 device_class_set_props(dc
, virtio_gpu_gl_properties
);
166 static const TypeInfo virtio_gpu_gl_info
= {
167 .name
= TYPE_VIRTIO_GPU_GL
,
168 .parent
= TYPE_VIRTIO_GPU
,
169 .instance_size
= sizeof(VirtIOGPUGL
),
170 .class_init
= virtio_gpu_gl_class_init
,
173 static void virtio_register_types(void)
175 type_register_static(&virtio_gpu_gl_info
);
178 type_init(virtio_register_types
)