From 1f60cd291fe7882cefb2ba75f2dd045f03c737d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A1s=20P=C5=91cze?= Date: Fri, 8 Aug 2025 10:44:55 +0200 Subject: [PATCH] spa: libcamera: source: keep `libcamera::FrameBufferAllocator` Instantiate it once and keep it instead of always dynamically allocating it when the camera is acquired. --- spa/plugins/libcamera/libcamera-source.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/spa/plugins/libcamera/libcamera-source.cpp b/spa/plugins/libcamera/libcamera-source.cpp index 485bbfb05..19cd0f8ff 100644 --- a/spa/plugins/libcamera/libcamera-source.cpp +++ b/spa/plugins/libcamera/libcamera-source.cpp @@ -152,7 +152,7 @@ struct impl { std::shared_ptr camera; const std::unique_ptr config; - FrameBufferAllocator *allocator = nullptr; + FrameBufferAllocator allocator; std::vector> requestPool; spa_ringbuffer completed_requests_rb = SPA_RINGBUFFER_INIT(); std::array completed_requests; @@ -212,7 +212,7 @@ int spa_libcamera_open(struct impl *impl) if (int res = impl->camera->acquire(); res < 0) return res; - impl->allocator = new FrameBufferAllocator(impl->camera); + spa_assert(!impl->allocator.allocated()); const ControlInfoMap &controls = impl->camera->controls(); setup_initial_controls(controls, impl->initial_controls); @@ -230,8 +230,8 @@ int spa_libcamera_close(struct impl *impl) return 0; spa_log_info(impl->log, "close camera %s", impl->camera->id().c_str()); - delete impl->allocator; - impl->allocator = nullptr; + + spa_assert(!impl->allocator.allocated()); impl->camera->release(); @@ -270,7 +270,7 @@ int spa_libcamera_buffer_recycle(struct impl *impl, struct port *port, uint32_t void freeBuffers(struct impl *impl, struct port *port) { impl->requestPool.clear(); - std::ignore = impl->allocator->free(port->streamConfig.stream()); + std::ignore = impl->allocator.free(port->streamConfig.stream()); } [[nodiscard]] @@ -298,10 +298,10 @@ int allocBuffers(struct impl *impl, struct port *port, unsigned int count) if (!impl->requestPool.empty()) return -EBUSY; - if ((res = impl->allocator->allocate(stream)) < 0) + if ((res = impl->allocator.allocate(stream)) < 0) return res; - const auto& bufs = impl->allocator->buffers(stream); + const auto& bufs = impl->allocator.buffers(stream); if (bufs.empty() || bufs.size() != count) { res = -ENOBUFS; goto err; @@ -1183,7 +1183,7 @@ spa_libcamera_alloc_buffers(struct impl *impl, struct port *port, Stream *stream = impl->config->at(0).stream(); const std::vector> &bufs = - impl->allocator->buffers(stream); + impl->allocator.buffers(stream); if (n_buffers > 0) { if (bufs.size() != n_buffers) @@ -2171,7 +2171,8 @@ impl::impl(spa_log *log, spa_loop *data_loop, spa_system *system, out_ports{{this}}, manager(std::move(manager)), camera(std::move(camera)), - config(std::move(config)) + config(std::move(config)), + allocator(this->camera) { libcamera_log_topic_init(log);