summaryrefslogtreecommitdiff
path: root/encode.c
diff options
context:
space:
mode:
authorMikhail Burakov <mburakov@mailbox.org>2023-04-12 13:24:40 +0200
committerMikhail Burakov <mburakov@mailbox.org>2023-04-12 13:24:40 +0200
commit2efa0a9600fa41bd4a3438962c4a7f59f6ccacf6 (patch)
tree4051a712c52fb467055991dbf5c668ae79fc9930 /encode.c
parent0b3848db08cf78c6c5396f7b03a32d6074a0c48b (diff)
Do not dup dmabuf file descriptors in gpu code
Diffstat (limited to 'encode.c')
-rw-r--r--encode.c23
1 files changed, 20 insertions, 3 deletions
diff --git a/encode.c b/encode.c
index 5dc9cdd..cbf9294 100644
--- a/encode.c
+++ b/encode.c
@@ -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(