2
0

virtio-gpu-gl.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #include "qemu/osdep.h"
  14. #include "qemu/iov.h"
  15. #include "qemu/module.h"
  16. #include "qemu/error-report.h"
  17. #include "qapi/error.h"
  18. #include "sysemu/sysemu.h"
  19. #include "hw/virtio/virtio.h"
  20. #include "hw/virtio/virtio-gpu.h"
  21. #include "hw/virtio/virtio-gpu-bswap.h"
  22. #include "hw/virtio/virtio-gpu-pixman.h"
  23. #include "hw/qdev-properties.h"
  24. #include <virglrenderer.h>
  25. static void virtio_gpu_gl_update_cursor_data(VirtIOGPU *g,
  26. struct virtio_gpu_scanout *s,
  27. uint32_t resource_id)
  28. {
  29. uint32_t width, height;
  30. uint32_t pixels, *data;
  31. data = virgl_renderer_get_cursor_data(resource_id, &width, &height);
  32. if (!data) {
  33. return;
  34. }
  35. if (width != s->current_cursor->width ||
  36. height != s->current_cursor->height) {
  37. free(data);
  38. return;
  39. }
  40. pixels = s->current_cursor->width * s->current_cursor->height;
  41. memcpy(s->current_cursor->data, data, pixels * sizeof(uint32_t));
  42. free(data);
  43. }
  44. static void virtio_gpu_gl_flushed(VirtIOGPUBase *b)
  45. {
  46. VirtIOGPU *g = VIRTIO_GPU(b);
  47. virtio_gpu_process_cmdq(g);
  48. }
  49. static void virtio_gpu_gl_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
  50. {
  51. VirtIOGPU *g = VIRTIO_GPU(vdev);
  52. VirtIOGPUGL *gl = VIRTIO_GPU_GL(vdev);
  53. struct virtio_gpu_ctrl_command *cmd;
  54. if (!virtio_queue_ready(vq)) {
  55. return;
  56. }
  57. if (!gl->renderer_inited) {
  58. virtio_gpu_virgl_init(g);
  59. gl->renderer_inited = true;
  60. }
  61. if (gl->renderer_reset) {
  62. gl->renderer_reset = false;
  63. virtio_gpu_virgl_reset(g);
  64. }
  65. cmd = virtqueue_pop(vq, sizeof(struct virtio_gpu_ctrl_command));
  66. while (cmd) {
  67. cmd->vq = vq;
  68. cmd->error = 0;
  69. cmd->finished = false;
  70. QTAILQ_INSERT_TAIL(&g->cmdq, cmd, next);
  71. cmd = virtqueue_pop(vq, sizeof(struct virtio_gpu_ctrl_command));
  72. }
  73. virtio_gpu_process_cmdq(g);
  74. virtio_gpu_virgl_fence_poll(g);
  75. }
  76. static void virtio_gpu_gl_reset(VirtIODevice *vdev)
  77. {
  78. VirtIOGPU *g = VIRTIO_GPU(vdev);
  79. VirtIOGPUGL *gl = VIRTIO_GPU_GL(vdev);
  80. virtio_gpu_reset(vdev);
  81. /*
  82. * GL functions must be called with the associated GL context in main
  83. * thread, and when the renderer is unblocked.
  84. */
  85. if (gl->renderer_inited && !gl->renderer_reset) {
  86. virtio_gpu_virgl_reset_scanout(g);
  87. gl->renderer_reset = true;
  88. }
  89. }
  90. static void virtio_gpu_gl_device_realize(DeviceState *qdev, Error **errp)
  91. {
  92. VirtIOGPU *g = VIRTIO_GPU(qdev);
  93. #if HOST_BIG_ENDIAN
  94. error_setg(errp, "virgl is not supported on bigendian platforms");
  95. return;
  96. #endif
  97. if (!object_resolve_path_type("", TYPE_VIRTIO_GPU_GL, NULL)) {
  98. error_setg(errp, "at most one %s device is permitted", TYPE_VIRTIO_GPU_GL);
  99. return;
  100. }
  101. if (!display_opengl) {
  102. error_setg(errp, "opengl is not available");
  103. return;
  104. }
  105. g->parent_obj.conf.flags |= (1 << VIRTIO_GPU_FLAG_VIRGL_ENABLED);
  106. VIRTIO_GPU_BASE(g)->virtio_config.num_capsets =
  107. virtio_gpu_virgl_get_num_capsets(g);
  108. virtio_gpu_device_realize(qdev, errp);
  109. }
  110. static Property virtio_gpu_gl_properties[] = {
  111. DEFINE_PROP_BIT("stats", VirtIOGPU, parent_obj.conf.flags,
  112. VIRTIO_GPU_FLAG_STATS_ENABLED, false),
  113. DEFINE_PROP_END_OF_LIST(),
  114. };
  115. static void virtio_gpu_gl_class_init(ObjectClass *klass, void *data)
  116. {
  117. DeviceClass *dc = DEVICE_CLASS(klass);
  118. VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
  119. VirtIOGPUBaseClass *vbc = VIRTIO_GPU_BASE_CLASS(klass);
  120. VirtIOGPUClass *vgc = VIRTIO_GPU_CLASS(klass);
  121. vbc->gl_flushed = virtio_gpu_gl_flushed;
  122. vgc->handle_ctrl = virtio_gpu_gl_handle_ctrl;
  123. vgc->process_cmd = virtio_gpu_virgl_process_cmd;
  124. vgc->update_cursor_data = virtio_gpu_gl_update_cursor_data;
  125. vdc->realize = virtio_gpu_gl_device_realize;
  126. vdc->reset = virtio_gpu_gl_reset;
  127. device_class_set_props(dc, virtio_gpu_gl_properties);
  128. }
  129. static const TypeInfo virtio_gpu_gl_info = {
  130. .name = TYPE_VIRTIO_GPU_GL,
  131. .parent = TYPE_VIRTIO_GPU,
  132. .instance_size = sizeof(VirtIOGPUGL),
  133. .class_init = virtio_gpu_gl_class_init,
  134. };
  135. module_obj(TYPE_VIRTIO_GPU_GL);
  136. module_kconfig(VIRTIO_GPU);
  137. static void virtio_register_types(void)
  138. {
  139. type_register_static(&virtio_gpu_gl_info);
  140. }
  141. type_init(virtio_register_types)
  142. module_dep("hw-display-virtio-gpu");