hwcontext_drm: make dependency on Linux kernel headers optional

This was introduced in version 4.6. And may not exist all without an
optional package. So to prevent a hard dependency on needing the Linux
kernel headers to compile, make this optional.

Also ignore the status of the ioctl, since it may fail on older kernels
which don't support it. It's okay to ignore as its not fatal and any
serious errors will be caught later by the mmap call.
This commit is contained in:
Lynne 2020-12-30 22:19:16 +01:00
parent 2f20e35892
commit 53c56585a6
No known key found for this signature in database
GPG Key ID: A2FEA5F03F034464
2 changed files with 32 additions and 20 deletions

4
configure vendored
View File

@ -2125,6 +2125,7 @@ HEADERS_LIST="
ES2_gl_h
gsm_h
io_h
linux_dma_buf_h
linux_perf_event_h
machine_ioctl_bt848_h
machine_ioctl_meteor_h
@ -6151,6 +6152,9 @@ check_headers dxgidebug.h
check_headers dxva.h
check_headers dxva2api.h -D_WIN32_WINNT=0x0600
check_headers io.h
enabled libdrm &&
check_headers linux/dma-buf.h
check_headers linux/perf_event.h
check_headers libcrystalhd/libcrystalhd_if.h
check_headers malloc.h

View File

@ -16,11 +16,19 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
/* This was introduced in version 4.6. And may not exist all without an
* optional package. So to prevent a hard dependency on needing the Linux
* kernel headers to compile, make this optional. */
#if HAVE_LINUX_DMA_BUF_H
#include <linux/dma-buf.h>
#include <sys/ioctl.h>
#endif
#include <drm.h>
#include <xf86drm.h>
@ -97,14 +105,12 @@ static void drm_unmap_frame(AVHWFramesContext *hwfc,
HWMapDescriptor *hwmap)
{
DRMMapping *map = hwmap->priv;
struct dma_buf_sync sync = { .flags = DMA_BUF_SYNC_END | map->sync_flags };
int i, ret;
for (i = 0; i < map->nb_regions; i++) {
ret = ioctl(map->object[i], DMA_BUF_IOCTL_SYNC, &sync);
if (ret)
av_log(hwfc, AV_LOG_ERROR, "Failed to issue ioctl sync to DRM object "
"%d: %d.\n", map->object[i], errno);
for (int i = 0; i < map->nb_regions; i++) {
#if HAVE_LINUX_DMA_BUF_H
struct dma_buf_sync sync = { .flags = DMA_BUF_SYNC_END | map->sync_flags };
ioctl(map->object[i], DMA_BUF_IOCTL_SYNC, &sync);
#endif
munmap(map->address[i], map->length[i]);
}
@ -115,7 +121,9 @@ static int drm_map_frame(AVHWFramesContext *hwfc,
AVFrame *dst, const AVFrame *src, int flags)
{
const AVDRMFrameDescriptor *desc = (AVDRMFrameDescriptor*)src->data[0];
#if HAVE_LINUX_DMA_BUF_H
struct dma_buf_sync sync_start = { 0 };
#endif
DRMMapping *map;
int err, i, p, plane;
int mmap_prot;
@ -126,16 +134,18 @@ static int drm_map_frame(AVHWFramesContext *hwfc,
return AVERROR(ENOMEM);
mmap_prot = 0;
if (flags & AV_HWFRAME_MAP_READ) {
if (flags & AV_HWFRAME_MAP_READ)
mmap_prot |= PROT_READ;
map->sync_flags |= DMA_BUF_SYNC_READ;
}
if (flags & AV_HWFRAME_MAP_WRITE) {
if (flags & AV_HWFRAME_MAP_WRITE)
mmap_prot |= PROT_WRITE;
map->sync_flags |= DMA_BUF_SYNC_WRITE;
}
#if HAVE_LINUX_DMA_BUF_H
if (flags & AV_HWFRAME_MAP_READ)
map->sync_flags |= DMA_BUF_SYNC_READ;
if (flags & AV_HWFRAME_MAP_WRITE)
map->sync_flags |= DMA_BUF_SYNC_WRITE;
sync_start.flags = DMA_BUF_SYNC_START | map->sync_flags;
#endif
av_assert0(desc->nb_objects <= AV_DRM_MAX_PLANES);
for (i = 0; i < desc->nb_objects; i++) {
@ -152,13 +162,11 @@ static int drm_map_frame(AVHWFramesContext *hwfc,
map->length[i] = desc->objects[i].size;
map->object[i] = desc->objects[i].fd;
err = ioctl(desc->objects[i].fd, DMA_BUF_IOCTL_SYNC, &sync_start);
if (err) {
err = AVERROR(errno);
av_log(hwfc, AV_LOG_ERROR, "Failed to issue ioctl sync to DRM object "
"%d: %d.\n", desc->objects[i].fd, errno);
goto fail;
}
#if HAVE_LINUX_DMA_BUF_H
/* We're not checking for errors here because the kernel may not
* support the ioctl, in which case its okay to carry on */
ioctl(desc->objects[i].fd, DMA_BUF_IOCTL_SYNC, &sync_start);
#endif
}
map->nb_regions = i;