[libcamera-devel,v3,05/13] pipeline: raspberrypi: Handle MandatoryRequestBuffer hints for Unicam Image
diff mbox series

Message ID 20221206135459.25521-6-naush@raspberrypi.com
State Superseded
Headers show
Series
  • Raspberry Pi: Platform configuration and buffer allocation improvements
Related show

Commit Message

Naushir Patuck Dec. 6, 2022, 1:54 p.m. UTC
Look for MandatoryRequestBuffer flag in the hints field of the Unicam Image
StreamConfiguration structure. If this flag is set, it guarantees that the
application will provide buffers for Unicam Image, so override the
minUnicamBuffers and minTotalUnicamBuffers config parameters in the following
way:

- If startup drop frames are required, allocate at least 1 internal buffer.
- If no startup drop frames are required, do not allocate any internal buffers.

All other buffer allocations remain unchanged.

Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
---
 .../pipeline/raspberrypi/raspberrypi.cpp      | 36 ++++++++++++++-----
 1 file changed, 28 insertions(+), 8 deletions(-)

Patch
diff mbox series

diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp
index c5a489fc7581..8be46962e825 100644
--- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp
+++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp
@@ -1447,12 +1447,33 @@  int PipelineHandlerRPi::queueAllBuffers(Camera *camera)
 int PipelineHandlerRPi::prepareBuffers(Camera *camera)
 {
 	RPiCameraData *data = cameraData(camera);
-	unsigned int numRawBuffers = 0;
+	unsigned int minUnicamBuffers = data->config_.minUnicamBuffers,
+		     minTotalUnicamBuffers = data->config_.minTotalUnicamBuffers,
+		     numRawBuffers = 0;
 	int ret;
 
 	for (Stream *s : camera->streams()) {
-		if (isRaw(s->configuration().pixelFormat)) {
+		if (s == &data->unicam_[Unicam::Image]) {
 			numRawBuffers = s->configuration().bufferCount;
+			/*
+			 * If the application provides a guarantees that Unicam
+			 * image buffers will always be provided for the RAW stream
+			 * in a Request, we need:
+			 * - at least 1 internal Unicam buffer to handle startup frame drops,
+			 * - no internal Unicam buffers if there are no startup frame drops.
+			 */
+			if (s->configuration().hints &
+			    StreamConfiguration::Hint::MandatoryRequestBuffer) {
+				if (data->dropFrameCount_) {
+					minUnicamBuffers = std::max(1u, minUnicamBuffers);
+					minTotalUnicamBuffers =
+						std::max(minUnicamBuffers, minTotalUnicamBuffers);
+				} else {
+					minUnicamBuffers = 0;
+					minTotalUnicamBuffers = 0;
+				}
+			}
+
 			break;
 		}
 	}
@@ -1464,7 +1485,6 @@  int PipelineHandlerRPi::prepareBuffers(Camera *camera)
 		 * For Unicam, allocate a minimum number of buffers for internal
 		 * use as we want to avoid any frame drops.
 		 */
-		const unsigned int minBuffers = data->config_.minTotalUnicamBuffers;
 		if (stream == &data->unicam_[Unicam::Image]) {
 			/*
 			 * If an application has configured a RAW stream, allocate
@@ -1472,8 +1492,8 @@  int PipelineHandlerRPi::prepareBuffers(Camera *camera)
 			 * we have at least minUnicamBuffers of internal buffers
 			 * to use to minimise frame drops.
 			 */
-			numBuffers = std::max<int>(data->config_.minUnicamBuffers,
-						   minBuffers - numRawBuffers);
+			numBuffers = std::max<int>(minUnicamBuffers,
+						   minTotalUnicamBuffers - numRawBuffers);
 		} else if (stream == &data->isp_[Isp::Input]) {
 			/*
 			 * ISP input buffers are imported from Unicam, so follow
@@ -1481,15 +1501,15 @@  int PipelineHandlerRPi::prepareBuffers(Camera *camera)
 			 * available.
 			 */
 			numBuffers = numRawBuffers +
-				     std::max<int>(data->config_.minUnicamBuffers,
-						   minBuffers - numRawBuffers);
+				     std::max<int>(minUnicamBuffers,
+						   minTotalUnicamBuffers - numRawBuffers);
 
 		} else if (stream == &data->unicam_[Unicam::Embedded]) {
 			/*
 			 * Embedded data buffers are (currently) for internal use,
 			 * so allocate the minimum required to avoid frame drops.
 			 */
-			numBuffers = minBuffers;
+			numBuffers = data->config_.minTotalUnicamBuffers;
 		} else {
 			/*
 			 * Since the ISP runs synchronous with the IPA and requests,