dmabuf.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * SPDX-License-Identifier: GPL-2.0-or-later
  3. *
  4. * QemuDmaBuf struct and helpers used for accessing its data
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  7. * See the COPYING file in the top-level directory.
  8. */
  9. #include "qemu/osdep.h"
  10. #include "ui/dmabuf.h"
  11. struct QemuDmaBuf {
  12. int fd;
  13. uint32_t width;
  14. uint32_t height;
  15. uint32_t stride;
  16. uint32_t fourcc;
  17. uint64_t modifier;
  18. uint32_t texture;
  19. uint32_t x;
  20. uint32_t y;
  21. uint32_t backing_width;
  22. uint32_t backing_height;
  23. bool y0_top;
  24. void *sync;
  25. int fence_fd;
  26. bool allow_fences;
  27. bool draw_submitted;
  28. };
  29. QemuDmaBuf *qemu_dmabuf_new(uint32_t width, uint32_t height,
  30. uint32_t stride, uint32_t x,
  31. uint32_t y, uint32_t backing_width,
  32. uint32_t backing_height, uint32_t fourcc,
  33. uint64_t modifier, int32_t dmabuf_fd,
  34. bool allow_fences, bool y0_top) {
  35. QemuDmaBuf *dmabuf;
  36. dmabuf = g_new0(QemuDmaBuf, 1);
  37. dmabuf->width = width;
  38. dmabuf->height = height;
  39. dmabuf->stride = stride;
  40. dmabuf->x = x;
  41. dmabuf->y = y;
  42. dmabuf->backing_width = backing_width;
  43. dmabuf->backing_height = backing_height;
  44. dmabuf->fourcc = fourcc;
  45. dmabuf->modifier = modifier;
  46. dmabuf->fd = dmabuf_fd;
  47. dmabuf->allow_fences = allow_fences;
  48. dmabuf->y0_top = y0_top;
  49. dmabuf->fence_fd = -1;
  50. return dmabuf;
  51. }
  52. void qemu_dmabuf_free(QemuDmaBuf *dmabuf)
  53. {
  54. if (dmabuf == NULL) {
  55. return;
  56. }
  57. g_free(dmabuf);
  58. }
  59. int qemu_dmabuf_get_fd(QemuDmaBuf *dmabuf)
  60. {
  61. assert(dmabuf != NULL);
  62. return dmabuf->fd;
  63. }
  64. int qemu_dmabuf_dup_fd(QemuDmaBuf *dmabuf)
  65. {
  66. assert(dmabuf != NULL);
  67. if (dmabuf->fd >= 0) {
  68. return dup(dmabuf->fd);
  69. } else {
  70. return -1;
  71. }
  72. }
  73. void qemu_dmabuf_close(QemuDmaBuf *dmabuf)
  74. {
  75. assert(dmabuf != NULL);
  76. if (dmabuf->fd >= 0) {
  77. close(dmabuf->fd);
  78. dmabuf->fd = -1;
  79. }
  80. }
  81. uint32_t qemu_dmabuf_get_width(QemuDmaBuf *dmabuf)
  82. {
  83. assert(dmabuf != NULL);
  84. return dmabuf->width;
  85. }
  86. uint32_t qemu_dmabuf_get_height(QemuDmaBuf *dmabuf)
  87. {
  88. assert(dmabuf != NULL);
  89. return dmabuf->height;
  90. }
  91. uint32_t qemu_dmabuf_get_stride(QemuDmaBuf *dmabuf)
  92. {
  93. assert(dmabuf != NULL);
  94. return dmabuf->stride;
  95. }
  96. uint32_t qemu_dmabuf_get_fourcc(QemuDmaBuf *dmabuf)
  97. {
  98. assert(dmabuf != NULL);
  99. return dmabuf->fourcc;
  100. }
  101. uint64_t qemu_dmabuf_get_modifier(QemuDmaBuf *dmabuf)
  102. {
  103. assert(dmabuf != NULL);
  104. return dmabuf->modifier;
  105. }
  106. uint32_t qemu_dmabuf_get_texture(QemuDmaBuf *dmabuf)
  107. {
  108. assert(dmabuf != NULL);
  109. return dmabuf->texture;
  110. }
  111. uint32_t qemu_dmabuf_get_x(QemuDmaBuf *dmabuf)
  112. {
  113. assert(dmabuf != NULL);
  114. return dmabuf->x;
  115. }
  116. uint32_t qemu_dmabuf_get_y(QemuDmaBuf *dmabuf)
  117. {
  118. assert(dmabuf != NULL);
  119. return dmabuf->y;
  120. }
  121. uint32_t qemu_dmabuf_get_backing_width(QemuDmaBuf *dmabuf)
  122. {
  123. assert(dmabuf != NULL);
  124. return dmabuf->backing_width;
  125. }
  126. uint32_t qemu_dmabuf_get_backing_height(QemuDmaBuf *dmabuf)
  127. {
  128. assert(dmabuf != NULL);
  129. return dmabuf->backing_height;
  130. }
  131. bool qemu_dmabuf_get_y0_top(QemuDmaBuf *dmabuf)
  132. {
  133. assert(dmabuf != NULL);
  134. return dmabuf->y0_top;
  135. }
  136. void *qemu_dmabuf_get_sync(QemuDmaBuf *dmabuf)
  137. {
  138. assert(dmabuf != NULL);
  139. return dmabuf->sync;
  140. }
  141. int32_t qemu_dmabuf_get_fence_fd(QemuDmaBuf *dmabuf)
  142. {
  143. assert(dmabuf != NULL);
  144. return dmabuf->fence_fd;
  145. }
  146. bool qemu_dmabuf_get_allow_fences(QemuDmaBuf *dmabuf)
  147. {
  148. assert(dmabuf != NULL);
  149. return dmabuf->allow_fences;
  150. }
  151. bool qemu_dmabuf_get_draw_submitted(QemuDmaBuf *dmabuf)
  152. {
  153. assert(dmabuf != NULL);
  154. return dmabuf->draw_submitted;
  155. }
  156. void qemu_dmabuf_set_texture(QemuDmaBuf *dmabuf, uint32_t texture)
  157. {
  158. assert(dmabuf != NULL);
  159. dmabuf->texture = texture;
  160. }
  161. void qemu_dmabuf_set_fence_fd(QemuDmaBuf *dmabuf, int32_t fence_fd)
  162. {
  163. assert(dmabuf != NULL);
  164. dmabuf->fence_fd = fence_fd;
  165. }
  166. void qemu_dmabuf_set_sync(QemuDmaBuf *dmabuf, void *sync)
  167. {
  168. assert(dmabuf != NULL);
  169. dmabuf->sync = sync;
  170. }
  171. void qemu_dmabuf_set_draw_submitted(QemuDmaBuf *dmabuf, bool draw_submitted)
  172. {
  173. assert(dmabuf != NULL);
  174. dmabuf->draw_submitted = draw_submitted;
  175. }
  176. void qemu_dmabuf_set_fd(QemuDmaBuf *dmabuf, int32_t fd)
  177. {
  178. assert(dmabuf != NULL);
  179. dmabuf->fd = fd;
  180. }