From 475665d615eb3f139462080115f03d1484a376e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A1s=20P=C5=91cze?= Date: Thu, 7 Aug 2025 15:22:42 +0200 Subject: [PATCH] spa: libcamera: source: persistent requests <-> buffer association Currently only a single stream is used. This makes it easy to associate each request with the appropriate frame buffer when it is created. In turn, this makes reusing requests a bit simpler and a bit more efficient. So do that: add buffers to requests when they are allocated in `allocBuffers()`. --- spa/plugins/libcamera/libcamera-source.cpp | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/spa/plugins/libcamera/libcamera-source.cpp b/spa/plugins/libcamera/libcamera-source.cpp index ae3e9a9b3..9feb761ad 100644 --- a/spa/plugins/libcamera/libcamera-source.cpp +++ b/spa/plugins/libcamera/libcamera-source.cpp @@ -257,13 +257,6 @@ int spa_libcamera_buffer_recycle(struct impl *impl, struct port *port, uint32_t return -EINVAL; } Request *request = impl->requestPool[buffer_id].get(); - Stream *stream = port->streamConfig.stream(); - FrameBuffer *buffer = impl->allocator->buffers(stream)[buffer_id].get(); - if ((res = request->addBuffer(stream, buffer)) < 0) { - spa_log_warn(impl->log, "can't add buffer %u for request: %s", - buffer_id, spa_strerror(res)); - return -ENOMEM; - } if (!impl->active) { impl->pendingRequests.push_back(request); return 0; @@ -325,6 +318,11 @@ int allocBuffers(struct impl *impl, struct port *port, unsigned int count) res = -ENOMEM; goto err; } + + res = request->addBuffer(stream, bufs[i].get()); + if (res < 0) + goto err; + impl->requestPool.push_back(std::move(request)); } @@ -1252,7 +1250,7 @@ void impl::requestComplete(libcamera::Request *request) if ((request->status() == Request::RequestCancelled)) { spa_log_debug(impl->log, "Request was cancelled"); - request->reuse(); + request->reuse(libcamera::Request::ReuseFlag::ReuseBuffers); SPA_FLAG_SET(b->flags, BUFFER_FLAG_OUTSTANDING); spa_libcamera_buffer_recycle(impl, port, b->id); return; @@ -1299,7 +1297,7 @@ void impl::requestComplete(libcamera::Request *request) b->h->pts = fmd.timestamp; b->h->dts_offset = 0; } - request->reuse(); + request->reuse(libcamera::Request::ReuseFlag::ReuseBuffers); spa_ringbuffer_get_write_index(&port->ring, &index); port->ring_ids[index & MASK_BUFFERS] = buffer_id; @@ -1384,7 +1382,7 @@ int spa_libcamera_stream_off(struct impl *impl) if (!impl->active) { for (std::unique_ptr &req : impl->requestPool) - req->reuse(); + req->reuse(libcamera::Request::ReuseFlag::ReuseBuffers); return 0; }