virtgpu_plane.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (C) 2015 Red Hat, Inc.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial
  15. * portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "virtgpu_drv.h"
  26. #include <drm/drm_plane_helper.h>
  27. #include <drm/drm_atomic_helper.h>
  28. static const uint32_t virtio_gpu_formats[] = {
  29. DRM_FORMAT_XRGB8888,
  30. DRM_FORMAT_ARGB8888,
  31. DRM_FORMAT_BGRX8888,
  32. DRM_FORMAT_BGRA8888,
  33. DRM_FORMAT_RGBX8888,
  34. DRM_FORMAT_RGBA8888,
  35. DRM_FORMAT_XBGR8888,
  36. DRM_FORMAT_ABGR8888,
  37. };
  38. static void virtio_gpu_plane_destroy(struct drm_plane *plane)
  39. {
  40. kfree(plane);
  41. }
  42. static const struct drm_plane_funcs virtio_gpu_plane_funcs = {
  43. .update_plane = drm_atomic_helper_update_plane,
  44. .disable_plane = drm_atomic_helper_disable_plane,
  45. .destroy = virtio_gpu_plane_destroy,
  46. .reset = drm_atomic_helper_plane_reset,
  47. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  48. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  49. };
  50. static int virtio_gpu_plane_atomic_check(struct drm_plane *plane,
  51. struct drm_plane_state *state)
  52. {
  53. return 0;
  54. }
  55. static void virtio_gpu_plane_atomic_update(struct drm_plane *plane,
  56. struct drm_plane_state *old_state)
  57. {
  58. struct drm_device *dev = plane->dev;
  59. struct virtio_gpu_device *vgdev = dev->dev_private;
  60. struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(plane->crtc);
  61. struct virtio_gpu_framebuffer *vgfb;
  62. struct virtio_gpu_object *bo;
  63. uint32_t handle;
  64. if (plane->state->fb) {
  65. vgfb = to_virtio_gpu_framebuffer(plane->state->fb);
  66. bo = gem_to_virtio_gpu_obj(vgfb->obj);
  67. handle = bo->hw_res_handle;
  68. if (bo->dumb) {
  69. virtio_gpu_cmd_transfer_to_host_2d
  70. (vgdev, handle, 0,
  71. cpu_to_le32(plane->state->crtc_w),
  72. cpu_to_le32(plane->state->crtc_h),
  73. plane->state->crtc_x, plane->state->crtc_y, NULL);
  74. }
  75. } else {
  76. handle = 0;
  77. }
  78. DRM_DEBUG("handle 0x%x, crtc %dx%d+%d+%d\n", handle,
  79. plane->state->crtc_w, plane->state->crtc_h,
  80. plane->state->crtc_x, plane->state->crtc_y);
  81. virtio_gpu_cmd_set_scanout(vgdev, output->index, handle,
  82. plane->state->crtc_w,
  83. plane->state->crtc_h,
  84. plane->state->crtc_x,
  85. plane->state->crtc_y);
  86. virtio_gpu_cmd_resource_flush(vgdev, handle,
  87. plane->state->crtc_x,
  88. plane->state->crtc_y,
  89. plane->state->crtc_w,
  90. plane->state->crtc_h);
  91. }
  92. static const struct drm_plane_helper_funcs virtio_gpu_plane_helper_funcs = {
  93. .atomic_check = virtio_gpu_plane_atomic_check,
  94. .atomic_update = virtio_gpu_plane_atomic_update,
  95. };
  96. struct drm_plane *virtio_gpu_plane_init(struct virtio_gpu_device *vgdev,
  97. int index)
  98. {
  99. struct drm_device *dev = vgdev->ddev;
  100. struct drm_plane *plane;
  101. int ret;
  102. plane = kzalloc(sizeof(*plane), GFP_KERNEL);
  103. if (!plane)
  104. return ERR_PTR(-ENOMEM);
  105. ret = drm_universal_plane_init(dev, plane, 1 << index,
  106. &virtio_gpu_plane_funcs,
  107. virtio_gpu_formats,
  108. ARRAY_SIZE(virtio_gpu_formats),
  109. DRM_PLANE_TYPE_PRIMARY, NULL);
  110. if (ret)
  111. goto err_plane_init;
  112. drm_plane_helper_add(plane, &virtio_gpu_plane_helper_funcs);
  113. return plane;
  114. err_plane_init:
  115. kfree(plane);
  116. return ERR_PTR(ret);
  117. }