diff options
author | Mikhail Burakov <mburakov@mailbox.org> | 2023-04-12 13:24:40 +0200 |
---|---|---|
committer | Mikhail Burakov <mburakov@mailbox.org> | 2023-04-12 13:24:40 +0200 |
commit | 2efa0a9600fa41bd4a3438962c4a7f59f6ccacf6 (patch) | |
tree | 4051a712c52fb467055991dbf5c668ae79fc9930 /encode.c | |
parent | 0b3848db08cf78c6c5396f7b03a32d6074a0c48b (diff) |
Do not dup dmabuf file descriptors in gpu code
Diffstat (limited to 'encode.c')
-rw-r--r-- | encode.c | 23 |
1 files changed, 20 insertions, 3 deletions
@@ -17,6 +17,7 @@ #include "encode.h" +#include <assert.h> #include <drm_fourcc.h> #include <errno.h> #include <libavcodec/avcodec.h> @@ -154,10 +155,17 @@ rollback_encode_context: static struct GpuFrame* PrimeToGpuFrame( struct GpuContext* gpu_context, const VADRMPRIMESurfaceDescriptor* prime) { - struct GpuFramePlane planes[4]; + struct GpuFramePlane planes[] = { + {.dmabuf_fd = -1}, + {.dmabuf_fd = -1}, + {.dmabuf_fd = -1}, + {.dmabuf_fd = -1}, + }; + static_assert(LENGTH(planes) == LENGTH(prime->layers[0].object_index), + "Suspicious VADRMPRIMESurfaceDescriptor structure"); + for (size_t i = 0; i < prime->layers[0].num_planes; i++) { uint32_t object_index = prime->layers[0].object_index[i]; - if (prime->objects[object_index].fd == -1) break; planes[i] = (struct GpuFramePlane){ .dmabuf_fd = prime->objects[object_index].fd, .pitch = prime->layers[0].pitch[i], @@ -165,11 +173,20 @@ static struct GpuFrame* PrimeToGpuFrame( .modifier = prime->objects[object_index].drm_format_modifier, }; } + struct GpuFrame* gpu_frame = GpuContextCreateFrame(gpu_context, prime->width, prime->height, prime->fourcc, prime->layers[0].num_planes, planes); - for (size_t i = prime->num_objects; i; i--) close(prime->objects[i - 1].fd); + if (!gpu_frame) { + LOG("Failed to create gpu frame"); + goto release_planes; + } return gpu_frame; + +release_planes: + CloseUniqueFds((int[]){planes[0].dmabuf_fd, planes[1].dmabuf_fd, + planes[2].dmabuf_fd, planes[3].dmabuf_fd}); + return NULL; } const struct GpuFrame* EncodeContextGetFrame( |