12 #include <linux/dma-buf.h>
19 int check_vgem(int fd)
21 drm_version_t version = { 0 };
28 ret = ioctl(fd, DRM_IOCTL_VERSION, &version);
32 return strcmp(name, "vgem");
38 const char *drmstr = "/dev/dri/card";
41 for (i = 0; i < 16; i++) {
44 sprintf(name, "%s%u", drmstr, i);
46 fd = open(name, O_RDWR);
61 int import_vgem_fd(int vgem_fd, int dma_buf_fd, uint32_t *handle)
63 struct drm_prime_handle import_handle = { 0 };
66 import_handle.fd = dma_buf_fd;
67 import_handle.flags = 0;
68 import_handle.handle = 0;
70 ret = ioctl(vgem_fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &import_handle);
72 *handle = import_handle.handle;
76 void close_handle(int vgem_fd, uint32_t handle)
78 struct drm_gem_close close = { 0 };
80 close.handle = handle;
81 ioctl(vgem_fd, DRM_IOCTL_GEM_CLOSE, &close);
87 struct ion_buffer_info info;
89 struct dma_buf_sync sync = { 0 };
91 info.heap_type = ION_HEAP_TYPE_SYSTEM;
92 info.heap_size = 4096;
93 info.flag_type = ION_FLAG_CACHED;
95 ret = ion_export_buffer_fd(&info);
97 printf("ion buffer alloc failed\n");
101 vgem_fd = open_vgem();
104 printf("Failed to open vgem\n");
108 ret = import_vgem_fd(vgem_fd, info.buffd, &handle);
111 printf("Failed to import buffer\n");
115 sync.flags = DMA_BUF_SYNC_START | DMA_BUF_SYNC_RW;
116 ret = ioctl(info.buffd, DMA_BUF_IOCTL_SYNC, &sync);
118 printf("sync start failed %d\n", errno);
120 memset(info.buffer, 0xff, 4096);
122 sync.flags = DMA_BUF_SYNC_END | DMA_BUF_SYNC_RW;
123 ret = ioctl(info.buffd, DMA_BUF_IOCTL_SYNC, &sync);
125 printf("sync end failed %d\n", errno);
127 close_handle(vgem_fd, handle);
133 ion_close_buffer_fd(&info);