modeset-plane-test.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #define _GNU_SOURCE
  2. #include <errno.h>
  3. #include <fcntl.h>
  4. #include <stdbool.h>
  5. #include <stdint.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <sys/mman.h>
  10. #include <time.h>
  11. #include <unistd.h>
  12. #include <xf86drm.h>
  13. #include <xf86drmMode.h>
  14. struct buffer_object {
  15. uint32_t width;
  16. uint32_t height;
  17. uint32_t pitch;
  18. uint32_t handle;
  19. uint32_t size;
  20. uint8_t *vaddr;
  21. uint32_t fb_id;
  22. };
  23. struct buffer_object buf;
  24. static int modeset_create_fb(int fd, struct buffer_object *bo)
  25. {
  26. struct drm_mode_create_dumb create = {};
  27. struct drm_mode_map_dumb map = {};
  28. create.width = bo->width;
  29. create.height = bo->height;
  30. create.bpp = 32;
  31. drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &create);
  32. bo->pitch = create.pitch;
  33. bo->size = create.size;
  34. bo->handle = create.handle;
  35. // {24, 32}
  36. drmModeAddFB(fd, bo->width, bo->height, 32, 32, bo->pitch,
  37. bo->handle, &bo->fb_id);
  38. map.handle = create.handle;
  39. drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &map);
  40. bo->vaddr = mmap(0, create.size, PROT_READ | PROT_WRITE,
  41. MAP_SHARED, fd, map.offset);
  42. memset(bo->vaddr, 0x55, bo->size);
  43. return 0;
  44. }
  45. static void modeset_destroy_fb(int fd, struct buffer_object *bo)
  46. {
  47. struct drm_mode_destroy_dumb destroy = {};
  48. drmModeRmFB(fd, bo->fb_id);
  49. munmap(bo->vaddr, bo->size);
  50. destroy.handle = bo->handle;
  51. drmIoctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy);
  52. }
  53. int main(int argc, char **argv)
  54. {
  55. int fd;
  56. drmModeConnector *conn;
  57. drmModeRes *res;
  58. drmModePlaneRes *plane_res;
  59. uint32_t conn_id;
  60. uint32_t crtc_id;
  61. uint32_t plane_id;
  62. fd = open("/dev/dri/card0", O_RDWR | O_CLOEXEC);
  63. res = drmModeGetResources(fd);
  64. crtc_id = res->crtcs[0];
  65. conn_id = res->connectors[0];
  66. drmSetClientCap(fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
  67. plane_res = drmModeGetPlaneResources(fd);
  68. plane_id = plane_res->planes[0];
  69. conn = drmModeGetConnector(fd, conn_id);
  70. buf.width = conn->modes[0].hdisplay;
  71. buf.height = conn->modes[0].vdisplay;
  72. modeset_create_fb(fd, &buf);
  73. drmModeSetCrtc(fd, crtc_id, buf.fb_id,
  74. 0, 0, &conn_id, 1, &conn->modes[0]);
  75. getchar();
  76. drmModeSetPlane(fd, plane_id, crtc_id, buf.fb_id, 0,
  77. 50, 50, 320, 320,
  78. 100, 150, 320 << 16, 320 << 16);
  79. getchar();
  80. modeset_destroy_fb(fd, &buf);
  81. drmModeFreeConnector(conn);
  82. drmModeFreePlaneResources(plane_res);
  83. drmModeFreeResources(res);
  84. close(fd);
  85. return 0;
  86. }