我知道在DRM渲染中,实际的像素数据保存在无效映射数据中,如下所示:
struct drm_mode_map_dumb arg;
memset(&arg, 0, sizeof(arg));
arg.handle = bo->handle;
ret = drmIoctl(bo->fd, DRM_IOCTL_MODE_MAP_DUMB, &arg);
void* map;
map = mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED, bo->fd, arg.offset);
我想知道如何获取交换缓冲区后GBM中保存的实际像素数据?
struct gbm_bo *bo;
eglSwapBuffers(egl->display, egl->surface);
bo = gbm_surface_lock_front_buffer(gbm->surface);
// How can I get the pixel data from bo ?
这是一个示例(在我的例子中,它要求缓冲区对象具有线性修饰符)。
int getDMAFDFromBO(int drmFd, struct gbm_bo *bo)
{
struct drm_prime_handle prime_handle;
memset(&prime_handle, 0, sizeof(prime_handle));
prime_handle.handle = gbm_bo_get_handle(bo).u32;
prime_handle.flags = DRM_CLOEXEC | DRM_RDWR;
prime_handle.fd = -1;
if (ioctl(drmFd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &prime_handle) != 0)
goto fail;
if (prime_handle.fd < 0)
goto fail;
// Set read and write permissions on the file descriptor
if (fcntl(prime_handle.fd, F_SETFL, fcntl(prime_handle.fd, F_GETFL) | O_RDWR) == -1)
{
close(prime_handle.fd);
goto fail;
}
printf("Got BO DMA fd using DRM_IOCTL_PRIME_HANDLE_TO_FD.\n");
return prime_handle.fd;
fail:
prime_handle.fd = gbm_bo_get_fd(bo);
if (prime_handle.fd >= 0)
{
printf("Got BO DMA fd using gbm_bo_get_fd().\n");
return prime_handle.fd;
}
printf("Failed to get fd for handle %u: %s\n", prime_handle.handle, strerror(errno));
return -1;
}
int main()
{
// Open DRM device, create gbm_bo, get size, stride etc...
int dmaFd = getDMAFDFromBO(drmFd, bo);
void **gbmBoMapData = NULL; // Needed for unmapping if gbm_bo_map is used
void *map = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, dmaFd, 0);
if (map == MAP_FAILED)
{
map = gbm_bo_map(bo, 0, 0, width, height, GBM_BO_TRANSFER_READ, &stride, gbmBoMapData);
if (!map)
{
printf("Failed to map DMA fd.\n");
return 1;
}
}
// ...
return 0;
}