[{"id":11427,"web_url":"https://patchwork.libcamera.org/comment/11427/","msgid":"<20200718153344.GD555842@oden.dyn.berto.se>","date":"2020-07-18T15:33:44","subject":"Re: [libcamera-devel] [PATCH v3 06/10] libcamera: pipeline:\n\traspberrypi: Rework stream buffer logic for zero-copy","submitter":{"id":5,"url":"https://patchwork.libcamera.org/api/people/5/","name":"Niklas Söderlund","email":"niklas.soderlund@ragnatech.se"},"content":"Hi Naushir,\n\nThanks for your work.\n\nOn 2020-07-17 09:54:06 +0100, Naushir Patuck wrote:\n> Stop using v4l2_videodevice::allocateBuffer() for internal buffers and\n> instead export/import all buffers. This allows the pipeline to return\n> any stream buffer requested by the application as zero-copy.\n> \n> Advertise the Unicam Image stream as the RAW capture stream now.\n> \n> The RPiStream object now maintains a new list of buffers that are\n> available to queue into a device. This is needed to distinguish between\n> FrameBuffers allocated for internal use vs externally provided buffers.\n> When a Request comes in, if a buffer is not provided for an exported\n> stream, we re-use a buffer from this list.\n> \n> Signed-off-by: Naushir Patuck <naush@raspberrypi.com>\n\nI really like this patch. I'm no expert on the raspberrypi pipeline so I \nthink it needs a second review of someone who knows it better then me.  \nBut save a small nit bellow I see nothing that is odd, so with the nit \nfixed,\n\nReviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n\n> ---\n>  .../pipeline/raspberrypi/raspberrypi.cpp      | 228 ++++++++++--------\n>  .../pipeline/raspberrypi/rpi_stream.cpp       | 122 +++++++---\n>  .../pipeline/raspberrypi/rpi_stream.h         |  30 ++-\n>  3 files changed, 226 insertions(+), 154 deletions(-)\n> \n> diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> index ca8434a3..f63bf497 100644\n> --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> @@ -199,7 +199,8 @@ private:\n>  \tvoid checkRequestCompleted();\n>  \tvoid tryRunPipeline();\n>  \tvoid tryFlushQueues();\n> -\tFrameBuffer *updateQueue(std::queue<FrameBuffer *> &q, uint64_t timestamp, V4L2VideoDevice *dev);\n> +\tFrameBuffer *updateQueue(std::queue<FrameBuffer *> &q, uint64_t timestamp,\n> +\t\t\t\t RPi::RPiStream *stream);\n>  \n>  \tunsigned int ispOutputCount_;\n>  };\n> @@ -520,8 +521,15 @@ int PipelineHandlerRPi::configure(Camera *camera, CameraConfiguration *config)\n>  \t\tStreamConfiguration &cfg = config->at(i);\n>  \n>  \t\tif (isRaw(cfg.pixelFormat)) {\n> -\t\t\tcfg.setStream(&data->isp_[Isp::Input]);\n> -\t\t\tdata->isp_[Isp::Input].setExternal(true);\n> +\t\t\tcfg.setStream(&data->unicam_[Unicam::Image]);\n> +\t\t\t/*\n> +\t\t\t * We must set both Unicam streams as external, even\n> +\t\t\t * though the application may only request RAW frames.\n> +\t\t\t * This is because we match timestamps on both streams\n> +\t\t\t * to synchronise buffers.\n> +\t\t\t */\n> +\t\t\tdata->unicam_[Unicam::Image].setExternal(true);\n> +\t\t\tdata->unicam_[Unicam::Embedded].setExternal(true);\n>  \t\t\tcontinue;\n>  \t\t}\n>  \n> @@ -624,7 +632,7 @@ int PipelineHandlerRPi::exportFrameBuffers(Camera *camera, Stream *stream,\n>  \tunsigned int count = stream->configuration().bufferCount;\n>  \tint ret = s->dev()->exportBuffers(count, buffers);\n>  \n> -\ts->setExternalBuffers(buffers);\n> +\ts->setExportedBuffers(buffers);\n>  \n>  \treturn ret;\n>  }\n> @@ -724,14 +732,23 @@ int PipelineHandlerRPi::queueRequestDevice(Camera *camera, Request *request)\n>  \tif (data->state_ == RPiCameraData::State::Stopped)\n>  \t\treturn -EINVAL;\n>  \n> -\t/* Ensure all external streams have associated buffers! */\n> -\tfor (auto &stream : data->isp_) {\n> -\t\tif (!stream.isExternal())\n> -\t\t\tcontinue;\n> +\tLOG(RPI, Debug) << \"queueRequestDevice: New request.\";\n>  \n> -\t\tif (!request->findBuffer(&stream)) {\n> -\t\t\tLOG(RPI, Error) << \"Attempt to queue request with invalid stream.\";\n> -\t\t\treturn -ENOENT;\n> +\t/* Push all buffers supplied in the Request to the respective streams. */\n> +\tfor (auto stream : data->streams_) {\n> +\t\tif (stream->isExternal()) {\n> +\t\t\tFrameBuffer *buffer = request->findBuffer(stream);\n> +\t\t\t/*\n> +\t\t\t * A nullptr in buffer means that we should queue an internally\n> +\t\t\t * allocated buffer.\n> +\t\t\t *\n> +\t\t\t * The below queueBuffer() call will do nothing if there are not\n> +\t\t\t * enough internal buffers allocated, but this will be handled by\n> +\t\t\t * queuing the request for buffers in the RPiStream object.\n> +\t\t\t */\n> +\t\t\tint ret = stream->queueBuffer(buffer);\n> +\t\t\tif (ret)\n> +\t\t\t\treturn ret;\n>  \t\t}\n>  \t}\n>  \n> @@ -820,12 +837,10 @@ bool PipelineHandlerRPi::match(DeviceEnumerator *enumerator)\n>  \t/* Initialize the camera properties. */\n>  \tdata->properties_ = data->sensor_->properties();\n>  \n> -\t/*\n> -\t * List the available output streams.\n> -\t * Currently cannot do Unicam streams!\n> -\t */\n> +\t/*List the available streams an application may request. */\n>  \tstd::set<Stream *> streams;\n> -\tstreams.insert(&data->isp_[Isp::Input]);\n> +\tstreams.insert(&data->unicam_[Unicam::Image]);\n> +\tstreams.insert(&data->unicam_[Unicam::Embedded]);\n>  \tstreams.insert(&data->isp_[Isp::Output0]);\n>  \tstreams.insert(&data->isp_[Isp::Output1]);\n>  \tstreams.insert(&data->isp_[Isp::Stats]);\n> @@ -843,9 +858,28 @@ int PipelineHandlerRPi::queueAllBuffers(Camera *camera)\n>  \tint ret;\n>  \n>  \tfor (auto const stream : data->streams_) {\n> -\t\tret = stream->queueBuffers();\n> -\t\tif (ret < 0)\n> -\t\t\treturn ret;\n> +\t\tif (!stream->isExternal()) {\n> +\t\t\tret = stream->queueAllBuffers();\n> +\t\t\tif (ret < 0)\n> +\t\t\t\treturn ret;\n> +\t\t} else {\n> +\t\t\t/*\n> +\t\t\t * For external streams, we must queue up a set of internal\n> +\t\t\t * buffers to handle the number of drop frames requested by\n> +\t\t\t * the IPA. This is done by passing nullptr in queueBuffer().\n> +\t\t\t *\n> +\t\t\t * The below queueBuffer() call will do nothing if there\n> +\t\t\t * are not enough internal buffers allocated, but this will\n> +\t\t\t * be handled by queuing the request for buffers in the\n> +\t\t\t * RPiStream object.\n> +\t\t\t */\n> +\t\t\tunsigned int i;\n> +\t\t\tfor (i = 0; i < data->dropFrameCount_; i++) {\n> +\t\t\t\tint ret = stream->queueBuffer(nullptr);\n> +\t\t\t\tif (ret)\n> +\t\t\t\t\treturn ret;\n> +\t\t\t}\n> +\t\t}\n>  \t}\n>  \n>  \treturn 0;\n> @@ -859,7 +893,8 @@ int PipelineHandlerRPi::prepareBuffers(Camera *camera)\n>  \t/*\n>  \t * Decide how many internal buffers to allocate. For now, simply look\n>  \t * at how many external buffers will be provided. Will need to improve\n> -\t * this logic.\n> +\t * this logic. However, we really must have all stream allocate the same\n> +\t * number of buffers to simplify error handling in queueRequestDevice().\n>  \t */\n>  \tunsigned int maxBuffers = 0;\n>  \tfor (const Stream *s : camera->streams())\n> @@ -867,33 +902,9 @@ int PipelineHandlerRPi::prepareBuffers(Camera *camera)\n>  \t\t\tmaxBuffers = std::max(maxBuffers, s->configuration().bufferCount);\n>  \n>  \tfor (auto const stream : data->streams_) {\n> -\t\tif (stream->isExternal() || stream->isImporter()) {\n> -\t\t\t/*\n> -\t\t\t * If a stream is marked as external reserve memory to\n> -\t\t\t * prepare to import as many buffers are requested in\n> -\t\t\t * the stream configuration.\n> -\t\t\t *\n> -\t\t\t * If a stream is an internal stream with importer\n> -\t\t\t * role, reserve as many buffers as possible.\n> -\t\t\t */\n> -\t\t\tunsigned int count = stream->isExternal()\n> -\t\t\t\t\t\t     ? stream->configuration().bufferCount\n> -\t\t\t\t\t\t     : maxBuffers;\n> -\t\t\tret = stream->importBuffers(count);\n> -\t\t\tif (ret < 0)\n> -\t\t\t\treturn ret;\n> -\t\t} else {\n> -\t\t\t/*\n> -\t\t\t * If the stream is an internal exporter allocate and\n> -\t\t\t * export as many buffers as possible to its internal\n> -\t\t\t * pool.\n> -\t\t\t */\n> -\t\t\tret = stream->allocateBuffers(maxBuffers);\n> -\t\t\tif (ret < 0) {\n> -\t\t\t\tfreeBuffers(camera);\n> -\t\t\t\treturn ret;\n> -\t\t\t}\n> -\t\t}\n> +\t\tret = stream->prepareBuffers(maxBuffers);\n> +\t\tif (ret < 0)\n> +\t\t\treturn ret;\n>  \t}\n>  \n>  \t/*\n> @@ -901,7 +912,7 @@ int PipelineHandlerRPi::prepareBuffers(Camera *camera)\n>  \t * the IPA and RPI_IPA_EVENT_SIGNAL_ISP_PREPARE event.\n>  \t */\n>  \tcount = 0;\n> -\tfor (auto const &b : *data->unicam_[Unicam::Image].getBuffers()) {\n> +\tfor (auto const &b : data->unicam_[Unicam::Image].getBuffers()) {\n>  \t\tb->setCookie(count++);\n>  \t}\n>  \n> @@ -910,14 +921,14 @@ int PipelineHandlerRPi::prepareBuffers(Camera *camera)\n>  \t * the IPA.\n>  \t */\n>  \tcount = 0;\n> -\tfor (auto const &b : *data->isp_[Isp::Stats].getBuffers()) {\n> +\tfor (auto const &b : data->isp_[Isp::Stats].getBuffers()) {\n>  \t\tb->setCookie(count++);\n>  \t\tdata->ipaBuffers_.push_back({ .id = RPiIpaMask::STATS | b->cookie(),\n>  \t\t\t\t\t      .planes = b->planes() });\n>  \t}\n>  \n>  \tcount = 0;\n> -\tfor (auto const &b : *data->unicam_[Unicam::Embedded].getBuffers()) {\n> +\tfor (auto const &b : data->unicam_[Unicam::Embedded].getBuffers()) {\n>  \t\tb->setCookie(count++);\n>  \t\tdata->ipaBuffers_.push_back({ .id = RPiIpaMask::EMBEDDED_DATA | b->cookie(),\n>  \t\t\t\t\t      .planes = b->planes() });\n> @@ -1089,7 +1100,7 @@ void RPiCameraData::queueFrameAction(unsigned int frame, const IPAOperationData\n>  \tswitch (action.operation) {\n>  \tcase RPI_IPA_ACTION_STATS_METADATA_COMPLETE: {\n>  \t\tunsigned int bufferId = action.data[0];\n> -\t\tFrameBuffer *buffer = isp_[Isp::Stats].getBuffers()->at(bufferId).get();\n> +\t\tFrameBuffer *buffer = isp_[Isp::Stats].getBuffers().at(bufferId);\n>  \n>  \t\thandleStreamBuffer(buffer, &isp_[Isp::Stats]);\n>  \t\t/* Fill the Request metadata buffer with what the IPA has provided */\n> @@ -1100,19 +1111,19 @@ void RPiCameraData::queueFrameAction(unsigned int frame, const IPAOperationData\n>  \n>  \tcase RPI_IPA_ACTION_EMBEDDED_COMPLETE: {\n>  \t\tunsigned int bufferId = action.data[0];\n> -\t\tFrameBuffer *buffer = unicam_[Unicam::Embedded].getBuffers()->at(bufferId).get();\n> +\t\tFrameBuffer *buffer = unicam_[Unicam::Embedded].getBuffers().at(bufferId);\n>  \t\thandleStreamBuffer(buffer, &unicam_[Unicam::Embedded]);\n>  \t\tbreak;\n>  \t}\n>  \n>  \tcase RPI_IPA_ACTION_RUN_ISP: {\n>  \t\tunsigned int bufferId = action.data[0];\n> -\t\tFrameBuffer *buffer = unicam_[Unicam::Image].getBuffers()->at(bufferId).get();\n> +\t\tFrameBuffer *buffer = unicam_[Unicam::Image].getBuffers().at(bufferId);\n>  \n>  \t\tLOG(RPI, Debug) << \"Input re-queue to ISP, buffer id \" << buffer->cookie()\n>  \t\t\t\t<< \", timestamp: \" << buffer->metadata().timestamp;\n>  \n> -\t\tisp_[Isp::Input].dev()->queueBuffer(buffer);\n> +\t\tisp_[Isp::Input].queueBuffer(buffer);\n>  \t\tispOutputCount_ = 0;\n>  \t\tbreak;\n>  \t}\n> @@ -1213,22 +1224,27 @@ void RPiCameraData::ispOutputDequeue(FrameBuffer *buffer)\n>  \t\t\t<< \", buffer id \" << buffer->cookie()\n>  \t\t\t<< \", timestamp: \" << buffer->metadata().timestamp;\n>  \n> -\thandleStreamBuffer(buffer, stream);\n> -\n>  \t/*\n> -\t * Increment the number of ISP outputs generated.\n> -\t * This is needed to track dropped frames.\n> +\t * ISP statistics buffer must not be re-queued or sent back to the\n> +\t * application until after the IPA signals so.\n>  \t */\n> -\tispOutputCount_++;\n> -\n> -\t/* If this is a stats output, hand it to the IPA now. */\n>  \tif (stream == &isp_[Isp::Stats]) {\n>  \t\tIPAOperationData op;\n>  \t\top.operation = RPI_IPA_EVENT_SIGNAL_STAT_READY;\n>  \t\top.data = { RPiIpaMask::STATS | buffer->cookie() };\n>  \t\tipa_->processEvent(op);\n> +\t} else {\n> +\t\t/* Any other ISP output can be handed back to the application now. */\n> +\t\thandleStreamBuffer(buffer, stream);\n>  \t}\n>  \n> +\t/*\n> +\t * Increment the number of ISP outputs generated.\n> +\t * This is needed to track dropped frames.\n> +\t */\n> +\tispOutputCount_++;\n> +\n> +\n>  \thandleState();\n>  }\n>  \n> @@ -1241,8 +1257,12 @@ void RPiCameraData::clearIncompleteRequests()\n>  \t */\n>  \tfor (auto const request : requestQueue_) {\n>  \t\tfor (auto const stream : streams_) {\n> -\t\t\tif (stream->isExternal())\n> -\t\t\t\tstream->dev()->queueBuffer(request->findBuffer(stream));\n> +\t\t\tif (!stream->isExternal())\n> +\t\t\t\tcontinue;\n> +\n> +\t\t\tFrameBuffer *buffer = request->findBuffer(stream);\n> +\t\t\tif (buffer)\n> +\t\t\t\tstream->queueBuffer(buffer);\n>  \t\t}\n>  \t}\n>  \n> @@ -1271,7 +1291,7 @@ void RPiCameraData::clearIncompleteRequests()\n>  \t\t\t * Has the buffer already been handed back to the\n>  \t\t\t * request? If not, do so now.\n>  \t\t\t */\n> -\t\t\tif (buffer->request())\n> +\t\t\tif (buffer && buffer->request())\n>  \t\t\t\tpipe_->completeBuffer(camera_, request, buffer);\n>  \t\t}\n>  \n> @@ -1283,30 +1303,24 @@ void RPiCameraData::clearIncompleteRequests()\n>  void RPiCameraData::handleStreamBuffer(FrameBuffer *buffer, RPi::RPiStream *stream)\n>  {\n>  \tif (stream->isExternal()) {\n> -\t\tif (!dropFrameCount_) {\n> -\t\t\tRequest *request = buffer->request();\n> +\t\tRequest *request = requestQueue_.front();\n> +\n> +\t\tif (!dropFrameCount_ && request->findBuffer(stream) == buffer) {\n> +\t\t\t/*\n> +\t\t\t * Tag the buffer as completed, returning it to the\n> +\t\t\t * application.\n> +\t\t\t */\n>  \t\t\tpipe_->completeBuffer(camera_, request, buffer);\n> +\t\t} else {\n> +\t\t\t/*\n> +\t\t\t * This buffer was not part of the Request, so we can\n> +\t\t\t * recycle it.\n> +\t\t\t */\n> +\t\t\tstream->returnBuffer(buffer);\n>  \t\t}\n>  \t} else {\n> -\t\t/* Special handling for RAW buffer Requests.\n> -\t\t *\n> -\t\t * The ISP input stream is alway an import stream, but if the\n> -\t\t * current Request has been made for a buffer on the stream,\n> -\t\t * simply memcpy to the Request buffer and requeue back to the\n> -\t\t * device.\n> -\t\t */\n> -\t\tif (stream == &unicam_[Unicam::Image] && !dropFrameCount_) {\n> -\t\t\tconst Stream *rawStream = static_cast<const Stream *>(&isp_[Isp::Input]);\n> -\t\t\tRequest *request = requestQueue_.front();\n> -\t\t\tFrameBuffer *raw = request->findBuffer(const_cast<Stream *>(rawStream));\n> -\t\t\tif (raw) {\n> -\t\t\t\traw->copyFrom(buffer);\n> -\t\t\t\tpipe_->completeBuffer(camera_, request, raw);\n> -\t\t\t}\n> -\t\t}\n> -\n> -\t\t/* Simply requeue the buffer. */\n> -\t\tstream->dev()->queueBuffer(buffer);\n> +\t\t/* Simply re-queue the buffer to the requested stream. */\n> +\t\tstream->queueBuffer(buffer);\n>  \t}\n>  }\n>  \n> @@ -1390,7 +1404,7 @@ void RPiCameraData::tryRunPipeline()\n>  \t * current bayer buffer will be removed and re-queued to the driver.\n>  \t */\n>  \tembeddedBuffer = updateQueue(embeddedQueue_, bayerBuffer->metadata().timestamp,\n> -\t\t\t\t     unicam_[Unicam::Embedded].dev());\n> +\t\t\t\t     &unicam_[Unicam::Embedded]);\n>  \n>  \tif (!embeddedBuffer) {\n>  \t\tLOG(RPI, Debug) << \"Could not find matching embedded buffer\";\n> @@ -1409,7 +1423,7 @@ void RPiCameraData::tryRunPipeline()\n>  \n>  \t\tembeddedBuffer = embeddedQueue_.front();\n>  \t\tbayerBuffer = updateQueue(bayerQueue_, embeddedBuffer->metadata().timestamp,\n> -\t\t\t\t\t  unicam_[Unicam::Image].dev());\n> +\t\t\t\t\t  &unicam_[Unicam::Image]);\n>  \n>  \t\tif (!bayerBuffer) {\n>  \t\t\tLOG(RPI, Debug) << \"Could not find matching bayer buffer - ending.\";\n> @@ -1417,11 +1431,7 @@ void RPiCameraData::tryRunPipeline()\n>  \t\t}\n>  \t}\n>  \n> -\t/*\n> -\t * Take the first request from the queue and action the IPA.\n> -\t * Unicam buffers for the request have already been queued as they come\n> -\t * in.\n> -\t */\n> +\t/* Take the first request from the queue and action the IPA. */\n>  \tRequest *request = requestQueue_.front();\n>  \n>  \t/*\n> @@ -1433,12 +1443,6 @@ void RPiCameraData::tryRunPipeline()\n>  \top.controls = { request->controls() };\n>  \tipa_->processEvent(op);\n>  \n> -\t/* Queue up any ISP buffers passed into the request. */\n> -\tfor (auto &stream : isp_) {\n> -\t\tif (stream.isExternal())\n> -\t\t\tstream.dev()->queueBuffer(request->findBuffer(&stream));\n> -\t}\n> -\n>  \t/* Ready to use the buffers, pop them off the queue. */\n>  \tbayerQueue_.pop();\n>  \tembeddedQueue_.pop();\n> @@ -1468,32 +1472,42 @@ void RPiCameraData::tryFlushQueues()\n>  \t * and give a chance for the hardware to return to lock-step. We do have\n>  \t * to drop all interim frames.\n>  \t */\n> -\tif (unicam_[Unicam::Image].getBuffers()->size() == bayerQueue_.size() &&\n> -\t    unicam_[Unicam::Embedded].getBuffers()->size() == embeddedQueue_.size()) {\n> +\tif (unicam_[Unicam::Image].getBuffers().size() == bayerQueue_.size() &&\n> +\t    unicam_[Unicam::Embedded].getBuffers().size() == embeddedQueue_.size()) {\n> +\t\t/* This cannot happen when Unicam streams are external. */\n> +\t\tassert(!unicam_[Unicam::Image].isExternal());\n> +\n>  \t\tLOG(RPI, Warning) << \"Flushing all buffer queues!\";\n>  \n>  \t\twhile (!bayerQueue_.empty()) {\n> -\t\t\tunicam_[Unicam::Image].dev()->queueBuffer(bayerQueue_.front());\n> +\t\t\tunicam_[Unicam::Image].queueBuffer(bayerQueue_.front());\n>  \t\t\tbayerQueue_.pop();\n>  \t\t}\n>  \n>  \t\twhile (!embeddedQueue_.empty()) {\n> -\t\t\tunicam_[Unicam::Embedded].dev()->queueBuffer(embeddedQueue_.front());\n> +\t\t\tunicam_[Unicam::Embedded].queueBuffer(embeddedQueue_.front());\n>  \t\t\tembeddedQueue_.pop();\n>  \t\t}\n>  \t}\n>  }\n>  \n>  FrameBuffer *RPiCameraData::updateQueue(std::queue<FrameBuffer *> &q, uint64_t timestamp,\n> -\t\t\t\t\tV4L2VideoDevice *dev)\n> +\t\t\t\t\tRPi::RPiStream *stream)\n>  {\n> +\t/*\n> +\t * If the unicam streams are external (both have to the same), then we\n> +\t * can only return out the top buffer in the queue, and assume they have\n> +\t * been synced by queuing at the same time. We cannot drop these frames,\n> +\t * as they may have been provided externally.\n> +\t */\n>  \twhile (!q.empty()) {\n>  \t\tFrameBuffer *b = q.front();\n> -\t\tif (b->metadata().timestamp < timestamp) {\n> +\t\tif (!stream->isExternal() && b->metadata().timestamp < timestamp) {\n>  \t\t\tq.pop();\n> -\t\t\tdev->queueBuffer(b);\n> -\t\t\tLOG(RPI, Warning) << \"Dropping input frame!\";\n> -\t\t} else if (b->metadata().timestamp == timestamp) {\n> +\t\t\tstream->queueBuffer(b);\n> +\t\t\tLOG(RPI, Warning) << \"Dropping unmatched input frame in stream \"\n> +\t\t\t\t\t  << stream->name();\n> +\t\t} else if (stream->isExternal() || b->metadata().timestamp == timestamp) {\n>  \t\t\t/* The calling function will pop the item from the queue. */\n>  \t\t\treturn b;\n>  \t\t} else {\n> diff --git a/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp b/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp\n> index 57e5cf72..53a335e3 100644\n> --- a/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp\n> +++ b/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp\n> @@ -21,30 +21,20 @@ V4L2VideoDevice *RPiStream::dev() const\n>  \n>  void RPiStream::setExternal(bool external)\n>  {\n> +\t/* Import streams cannot be external. */\n> +\tassert(!external || !importOnly_);\n>  \texternal_ = external;\n>  }\n>  \n>  bool RPiStream::isExternal() const\n>  {\n> -\t/*\n> -\t * Import streams cannot be external.\n> -\t *\n> -\t * RAW capture is a special case where we simply copy the RAW\n> -\t * buffer out of the request. All other buffer handling happens\n> -\t * as if the stream is internal.\n> -\t */\n> -\treturn external_ && !importOnly_;\n> -}\n> -\n> -bool RPiStream::isImporter() const\n> -{\n> -\treturn importOnly_;\n> +\treturn external_;\n>  }\n>  \n>  void RPiStream::reset()\n>  {\n>  \texternal_ = false;\n> -\tinternalBuffers_.clear();\n> +\tclearBuffers();\n>  }\n>  \n>  std::string RPiStream::name() const\n> @@ -52,65 +42,123 @@ std::string RPiStream::name() const\n>  \treturn name_;\n>  }\n>  \n> -void RPiStream::setExternalBuffers(std::vector<std::unique_ptr<FrameBuffer>> *buffers)\n> +void RPiStream::setExportedBuffers(std::vector<std::unique_ptr<FrameBuffer>> *buffers)\n>  {\n> -\texternalBuffers_ = buffers;\n> +\tstd::transform(buffers->begin(), buffers->end(), std::back_inserter(bufferList_),\n> +\t\t       [](std::unique_ptr<FrameBuffer> &b) { return b.get(); });\n>  }\n>  \n> -const std::vector<std::unique_ptr<FrameBuffer>> *RPiStream::getBuffers() const\n> +const std::vector<FrameBuffer *> &RPiStream::getBuffers() const\n>  {\n> -\treturn external_ ? externalBuffers_ : &internalBuffers_;\n> +\treturn bufferList_;\n>  }\n>  \n>  void RPiStream::releaseBuffers()\n>  {\n>  \tdev_->releaseBuffers();\n> -\tif (!external_ && !importOnly_)\n> -\t\tinternalBuffers_.clear();\n> +\tclearBuffers();\n>  }\n>  \n> -int RPiStream::importBuffers(unsigned int count)\n> +int RPiStream::prepareBuffers(unsigned int count)\n>  {\n> +\tint ret;\n> +\n> +\tif (!importOnly_) {\n> +\t\tif (count) {\n> +\t\t\t/* Export some frame buffers for internal use. */\n> +\t\t\tret = dev_->exportBuffers(count, &internalBuffers_);\n> +\t\t\tif (ret < 0)\n> +\t\t\t\treturn ret;\n> +\n> +\t\t\t/* Add these exported buffers to the internal/external buffer list. */\n> +\t\t\tsetExportedBuffers(&internalBuffers_);\n> +\n> +\t\t\t/* Add these buffers to the queue of internal usable buffers. */\n> +\t\t\tfor (auto const &buffer : internalBuffers_)\n> +\t\t\t\tavailableBuffers_.push(buffer.get());\n> +\t\t}\n> +\n> +\t\t/* We must import all internal/external exported buffers. */\n> +\t\tcount = bufferList_.size();\n> +\t}\n> +\n>  \treturn dev_->importBuffers(count);\n>  }\n>  \n> -int RPiStream::allocateBuffers(unsigned int count)\n> +int RPiStream::queueAllBuffers()\n>  {\n> -\treturn dev_->allocateBuffers(count, &internalBuffers_);\n> -}\n> +\tint ret;\n>  \n> -int RPiStream::queueBuffers()\n> -{\n>  \tif (external_)\n>  \t\treturn 0;\n>  \n> -\tfor (auto &b : internalBuffers_) {\n> -\t\tint ret = dev_->queueBuffer(b.get());\n> -\t\tif (ret) {\n> -\t\t\tLOG(RPISTREAM, Error) << \"Failed to queue buffers for \"\n> -\t\t\t\t\t      << name_;\n> +\twhile (!availableBuffers_.empty()) {\n> +\t\tret = queueBuffer(availableBuffers_.front());\n> +\t\tif (ret < 0)\n>  \t\t\treturn ret;\n> -\t\t}\n> +\n> +\t\tavailableBuffers_.pop();\n>  \t}\n>  \n>  \treturn 0;\n>  }\n>  \n> -bool RPiStream::findFrameBuffer(FrameBuffer *buffer) const\n> +int RPiStream::queueBuffer(FrameBuffer *buffer)\n> +{\n> +\t/*\n> +\t * A nullptr buffer implies an external stream, but no external\n> +\t * buffer has been supplied. So, pick one from the availableBuffers_\n> +\t * queue.\n> +\t */\n> +\tif (!buffer) {\n> +\t\tif (availableBuffers_.empty()) {\n> +\t\t\tLOG(RPISTREAM, Warning) << \"No buffers available for \"\n> +\t\t\t\t\t\t<< name_;\n> +\t\t\treturn -EINVAL;\n> +\t\t}\n> +\n> +\t\tbuffer = availableBuffers_.front();\n> +\t\tavailableBuffers_.pop();\n> +\t}\n> +\n> +\tLOG(RPISTREAM, Debug) << \"Queuing buffer \" << buffer->cookie()\n> +\t\t\t      << \" for \" << name_;\n> +\n> +\tint ret = dev_->queueBuffer(buffer);\n> +\tif (ret) {\n> +\t\tLOG(RPISTREAM, Error) << \"Failed to queue buffer for \"\n> +\t\t\t\t      << name_;\n> +\t}\n> +\n> +\treturn ret;\n> +}\n> +\n> +void RPiStream::returnBuffer(FrameBuffer *buffer)\n>  {\n> -\tauto start = external_ ? externalBuffers_->begin() : internalBuffers_.begin();\n> -\tauto end = external_ ? externalBuffers_->end() : internalBuffers_.end();\n> +\t/* This can only be called for external streams. */\n> +\tassert(external_);\n> +\n> +\tavailableBuffers_.push(buffer);\n> +}\n>  \n> +bool RPiStream::findFrameBuffer(FrameBuffer *buffer) const\n> +{\n>  \tif (importOnly_)\n>  \t\treturn false;\n>  \n> -\tif (std::find_if(start, end,\n> -\t\t\t [buffer](std::unique_ptr<FrameBuffer> const &ref) { return ref.get() == buffer; }) != end)\n> +\tif (std::find(bufferList_.begin(), bufferList_.end(), buffer) != bufferList_.end())\n>  \t\treturn true;\n\nCant this be made simpler by ?\n\n    if (bufferList_.find(buffer) != bufferList_.end())\n\n>  \n>  \treturn false;\n>  }\n>  \n> +void RPiStream::clearBuffers()\n> +{\n> +\tavailableBuffers_ = std::queue<FrameBuffer *>{};\n> +\tinternalBuffers_.clear();\n> +\tbufferList_.clear();\n> +}\n> +\n>  } /* namespace RPi */\n>  \n>  } /* namespace libcamera */\n> diff --git a/src/libcamera/pipeline/raspberrypi/rpi_stream.h b/src/libcamera/pipeline/raspberrypi/rpi_stream.h\n> index 40fff81d..019e236d 100644\n> --- a/src/libcamera/pipeline/raspberrypi/rpi_stream.h\n> +++ b/src/libcamera/pipeline/raspberrypi/rpi_stream.h\n> @@ -38,21 +38,22 @@ public:\n>  \tV4L2VideoDevice *dev() const;\n>  \tvoid setExternal(bool external);\n>  \tbool isExternal() const;\n> -\tbool isImporter() const;\n>  \tvoid reset();\n>  \tstd::string name() const;\n> -\tvoid setExternalBuffers(std::vector<std::unique_ptr<FrameBuffer>> *buffers);\n> -\tconst std::vector<std::unique_ptr<FrameBuffer>> *getBuffers() const;\n> +\tvoid setExportedBuffers(std::vector<std::unique_ptr<FrameBuffer>> *buffers);\n> +\tconst std::vector<FrameBuffer *> &getBuffers() const;\n>  \tvoid releaseBuffers();\n> -\tint importBuffers(unsigned int count);\n> -\tint allocateBuffers(unsigned int count);\n> -\tint queueBuffers();\n> +\tint prepareBuffers(unsigned int count);\n> +\tint queueAllBuffers();\n> +\tint queueBuffer(FrameBuffer *buffer);\n> +\tvoid returnBuffer(FrameBuffer *buffer);\n>  \tbool findFrameBuffer(FrameBuffer *buffer) const;\n>  \n>  private:\n> +\tvoid clearBuffers();\n>  \t/*\n>  \t * Indicates that this stream is active externally, i.e. the buffers\n> -\t * are provided by the application.\n> +\t * might be provided by (and returned to) the application.\n>  \t */\n>  \tbool external_;\n>  \t/* Indicates that this stream only imports buffers, e.g. ISP input. */\n> @@ -61,10 +62,19 @@ private:\n>  \tstd::string name_;\n>  \t/* The actual device stream. */\n>  \tstd::unique_ptr<V4L2VideoDevice> dev_;\n> -\t/* Internally allocated framebuffers associated with this device stream. */\n> +\t/* All framebuffers associated with this device stream. */\n> +\tstd::vector<FrameBuffer *> bufferList_;\n> +\t/*\n> +\t * List of frame buffer that we can use if none have been provided by\n> +\t * the application for external streams. This is populated by the\n> +\t * buffers exported internally.\n> +\t */\n> +\tstd::queue<FrameBuffer *> availableBuffers_;\n> +\t/*\n> +\t * This is a list of buffers exported internally. Need to keep this around\n> +\t * as the stream needs to maintain ownership of these buffers.\n> +\t */\n>  \tstd::vector<std::unique_ptr<FrameBuffer>> internalBuffers_;\n> -\t/* Externally allocated framebuffers associated with this device stream. */\n> -\tstd::vector<std::unique_ptr<FrameBuffer>> *externalBuffers_;\n>  };\n>  \n>  /*\n> -- \n> 2.25.1\n> \n> _______________________________________________\n> libcamera-devel mailing list\n> libcamera-devel@lists.libcamera.org\n> https://lists.libcamera.org/listinfo/libcamera-devel","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 71972BD792\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSat, 18 Jul 2020 15:33:48 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id F1741605B2;\n\tSat, 18 Jul 2020 17:33:47 +0200 (CEST)","from mail-lj1-x232.google.com (mail-lj1-x232.google.com\n\t[IPv6:2a00:1450:4864:20::232])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 8A9D060493\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 18 Jul 2020 17:33:46 +0200 (CEST)","by mail-lj1-x232.google.com with SMTP id q7so15939761ljm.1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 18 Jul 2020 08:33:46 -0700 (PDT)","from localhost (h-209-203.A463.priv.bahnhof.se. [155.4.209.203])\n\tby smtp.gmail.com with ESMTPSA id\n\tm5sm411376lfo.47.2020.07.18.08.33.44\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tSat, 18 Jul 2020 08:33:44 -0700 (PDT)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (2048-bit key;\n\tunprotected) header.d=ragnatech-se.20150623.gappssmtp.com\n\theader.i=@ragnatech-se.20150623.gappssmtp.com\n\theader.b=\"o5OSBtO0\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=ragnatech-se.20150623.gappssmtp.com; s=20150623;\n\th=date:from:to:cc:subject:message-id:references:mime-version\n\t:content-disposition:content-transfer-encoding:in-reply-to;\n\tbh=eVxAGwe/haEVCFOvLqrjlsEbPqtmdrkzHJB+duziQig=;\n\tb=o5OSBtO0tpE8IqYLfWLHeaT+FV4oAafR073c4wLZLFftgN6DLXoINuypsdh36+2t1y\n\taoznCf58pkx3nOWvud/qsq2T7jTg3K5zgFDBJXNxsKWDWY2dXpkm2RdWnB/VOn9v5L/M\n\tNwFtgJzxsfs9np9S04QpsnywEXsBqtjXlfvLeQZN+JCO/BajS6hs+utVLFI/xIj2DwPp\n\t9Mb62zmrqhMixeJYr3aCjyv1xh0x1IgNTBtAEdyDAxsGoNg06XkmOYVm/x8NZSTkdtwi\n\trOw/+p6F86/pT1jpHBszbZv8Kb6HdkfMHNFSB6rpUrutD0eEEV0M1KqMiB+BQ7T8JNMg\n\t+m4Q==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:date:from:to:cc:subject:message-id:references\n\t:mime-version:content-disposition:content-transfer-encoding\n\t:in-reply-to;\n\tbh=eVxAGwe/haEVCFOvLqrjlsEbPqtmdrkzHJB+duziQig=;\n\tb=Ypv7myeP1/jegqM/N7JhsOItvQT8Y81i3w72pmP51DoAMKwWkYKckVWXo5S5lbgmla\n\t2c6PeEmuEjXak6/zoEZ/Xz5nOqoE6BdCONxsm5I/P69mVzneKpnX+y5zeZoLuvBouC3i\n\twCmnWiRL/r3lkoI4mzrzcYkS+9TM5a0xgptk8/0a7Zy7tRtlTmUawC5hyUhXA1klYrex\n\tR/ox0ZGKaI1awfA5DXl8kpzJdY44D2Xpe4YqLssJD3GoQdjgxrDQOaFLZsZjxpon0n/e\n\tkja6UD4KrFj1wbtqf44tDcReRC/vnm77Tv/9XdGfOi4zdXVT6tkuVog95JrNmRYvZj6Q\n\tfDwA==","X-Gm-Message-State":"AOAM530LcKOeLb9UiJ842E4OhxZi+dWRBHX1mQfmtqKRC72xwKotEi8s\n\t8RoVNkHGCPlCpS/ybZ1SrQthxw==","X-Google-Smtp-Source":"ABdhPJy6tI7AOU1+/vNZx9RBIYawAiFFY/GWbV04+9lbrTKJUHcKRgAPhdxkhT95a+YN/G74p8AXyw==","X-Received":"by 2002:a2e:9b19:: with SMTP id\n\tu25mr6112011lji.161.1595086425430; \n\tSat, 18 Jul 2020 08:33:45 -0700 (PDT)","Date":"Sat, 18 Jul 2020 17:33:44 +0200","From":"Niklas =?iso-8859-1?q?S=F6derlund?= <niklas.soderlund@ragnatech.se>","To":"Naushir Patuck <naush@raspberrypi.com>","Message-ID":"<20200718153344.GD555842@oden.dyn.berto.se>","References":"<20200717085410.732308-1-naush@raspberrypi.com>\n\t<20200717085410.732308-7-naush@raspberrypi.com>","MIME-Version":"1.0","Content-Disposition":"inline","In-Reply-To":"<20200717085410.732308-7-naush@raspberrypi.com>","Subject":"Re: [libcamera-devel] [PATCH v3 06/10] libcamera: pipeline:\n\traspberrypi: Rework stream buffer logic for zero-copy","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Cc":"libcamera-devel@lists.libcamera.org","Content-Type":"text/plain; charset=\"iso-8859-1\"","Content-Transfer-Encoding":"quoted-printable","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":11437,"web_url":"https://patchwork.libcamera.org/comment/11437/","msgid":"<CAEmqJPquGFOpbv7GNQ3ui9fESKk1T_JkoMEbr5eOs6NL-nq_bw@mail.gmail.com>","date":"2020-07-20T08:30:26","subject":"Re: [libcamera-devel] [PATCH v3 06/10] libcamera: pipeline:\n\traspberrypi: Rework stream buffer logic for zero-copy","submitter":{"id":34,"url":"https://patchwork.libcamera.org/api/people/34/","name":"Naushir Patuck","email":"naush@raspberrypi.com"},"content":"Hi Niklas,\n\nThanks for the review.\n\nOn Sat, 18 Jul 2020 at 16:33, Niklas Söderlund\n<niklas.soderlund@ragnatech.se> wrote:\n>\n> Hi Naushir,\n>\n> Thanks for your work.\n>\n> On 2020-07-17 09:54:06 +0100, Naushir Patuck wrote:\n> > Stop using v4l2_videodevice::allocateBuffer() for internal buffers and\n> > instead export/import all buffers. This allows the pipeline to return\n> > any stream buffer requested by the application as zero-copy.\n> >\n> > Advertise the Unicam Image stream as the RAW capture stream now.\n> >\n> > The RPiStream object now maintains a new list of buffers that are\n> > available to queue into a device. This is needed to distinguish between\n> > FrameBuffers allocated for internal use vs externally provided buffers.\n> > When a Request comes in, if a buffer is not provided for an exported\n> > stream, we re-use a buffer from this list.\n> >\n> > Signed-off-by: Naushir Patuck <naush@raspberrypi.com>\n>\n> I really like this patch. I'm no expert on the raspberrypi pipeline so I\n> think it needs a second review of someone who knows it better then me.\n> But save a small nit bellow I see nothing that is odd, so with the nit\n> fixed,\n>\n> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n>\n> > ---\n> >  .../pipeline/raspberrypi/raspberrypi.cpp      | 228 ++++++++++--------\n> >  .../pipeline/raspberrypi/rpi_stream.cpp       | 122 +++++++---\n> >  .../pipeline/raspberrypi/rpi_stream.h         |  30 ++-\n> >  3 files changed, 226 insertions(+), 154 deletions(-)\n> >\n> > diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> > index ca8434a3..f63bf497 100644\n> > --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> > +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> > @@ -199,7 +199,8 @@ private:\n> >       void checkRequestCompleted();\n> >       void tryRunPipeline();\n> >       void tryFlushQueues();\n> > -     FrameBuffer *updateQueue(std::queue<FrameBuffer *> &q, uint64_t timestamp, V4L2VideoDevice *dev);\n> > +     FrameBuffer *updateQueue(std::queue<FrameBuffer *> &q, uint64_t timestamp,\n> > +                              RPi::RPiStream *stream);\n> >\n> >       unsigned int ispOutputCount_;\n> >  };\n> > @@ -520,8 +521,15 @@ int PipelineHandlerRPi::configure(Camera *camera, CameraConfiguration *config)\n> >               StreamConfiguration &cfg = config->at(i);\n> >\n> >               if (isRaw(cfg.pixelFormat)) {\n> > -                     cfg.setStream(&data->isp_[Isp::Input]);\n> > -                     data->isp_[Isp::Input].setExternal(true);\n> > +                     cfg.setStream(&data->unicam_[Unicam::Image]);\n> > +                     /*\n> > +                      * We must set both Unicam streams as external, even\n> > +                      * though the application may only request RAW frames.\n> > +                      * This is because we match timestamps on both streams\n> > +                      * to synchronise buffers.\n> > +                      */\n> > +                     data->unicam_[Unicam::Image].setExternal(true);\n> > +                     data->unicam_[Unicam::Embedded].setExternal(true);\n> >                       continue;\n> >               }\n> >\n> > @@ -624,7 +632,7 @@ int PipelineHandlerRPi::exportFrameBuffers(Camera *camera, Stream *stream,\n> >       unsigned int count = stream->configuration().bufferCount;\n> >       int ret = s->dev()->exportBuffers(count, buffers);\n> >\n> > -     s->setExternalBuffers(buffers);\n> > +     s->setExportedBuffers(buffers);\n> >\n> >       return ret;\n> >  }\n> > @@ -724,14 +732,23 @@ int PipelineHandlerRPi::queueRequestDevice(Camera *camera, Request *request)\n> >       if (data->state_ == RPiCameraData::State::Stopped)\n> >               return -EINVAL;\n> >\n> > -     /* Ensure all external streams have associated buffers! */\n> > -     for (auto &stream : data->isp_) {\n> > -             if (!stream.isExternal())\n> > -                     continue;\n> > +     LOG(RPI, Debug) << \"queueRequestDevice: New request.\";\n> >\n> > -             if (!request->findBuffer(&stream)) {\n> > -                     LOG(RPI, Error) << \"Attempt to queue request with invalid stream.\";\n> > -                     return -ENOENT;\n> > +     /* Push all buffers supplied in the Request to the respective streams. */\n> > +     for (auto stream : data->streams_) {\n> > +             if (stream->isExternal()) {\n> > +                     FrameBuffer *buffer = request->findBuffer(stream);\n> > +                     /*\n> > +                      * A nullptr in buffer means that we should queue an internally\n> > +                      * allocated buffer.\n> > +                      *\n> > +                      * The below queueBuffer() call will do nothing if there are not\n> > +                      * enough internal buffers allocated, but this will be handled by\n> > +                      * queuing the request for buffers in the RPiStream object.\n> > +                      */\n> > +                     int ret = stream->queueBuffer(buffer);\n> > +                     if (ret)\n> > +                             return ret;\n> >               }\n> >       }\n> >\n> > @@ -820,12 +837,10 @@ bool PipelineHandlerRPi::match(DeviceEnumerator *enumerator)\n> >       /* Initialize the camera properties. */\n> >       data->properties_ = data->sensor_->properties();\n> >\n> > -     /*\n> > -      * List the available output streams.\n> > -      * Currently cannot do Unicam streams!\n> > -      */\n> > +     /*List the available streams an application may request. */\n> >       std::set<Stream *> streams;\n> > -     streams.insert(&data->isp_[Isp::Input]);\n> > +     streams.insert(&data->unicam_[Unicam::Image]);\n> > +     streams.insert(&data->unicam_[Unicam::Embedded]);\n> >       streams.insert(&data->isp_[Isp::Output0]);\n> >       streams.insert(&data->isp_[Isp::Output1]);\n> >       streams.insert(&data->isp_[Isp::Stats]);\n> > @@ -843,9 +858,28 @@ int PipelineHandlerRPi::queueAllBuffers(Camera *camera)\n> >       int ret;\n> >\n> >       for (auto const stream : data->streams_) {\n> > -             ret = stream->queueBuffers();\n> > -             if (ret < 0)\n> > -                     return ret;\n> > +             if (!stream->isExternal()) {\n> > +                     ret = stream->queueAllBuffers();\n> > +                     if (ret < 0)\n> > +                             return ret;\n> > +             } else {\n> > +                     /*\n> > +                      * For external streams, we must queue up a set of internal\n> > +                      * buffers to handle the number of drop frames requested by\n> > +                      * the IPA. This is done by passing nullptr in queueBuffer().\n> > +                      *\n> > +                      * The below queueBuffer() call will do nothing if there\n> > +                      * are not enough internal buffers allocated, but this will\n> > +                      * be handled by queuing the request for buffers in the\n> > +                      * RPiStream object.\n> > +                      */\n> > +                     unsigned int i;\n> > +                     for (i = 0; i < data->dropFrameCount_; i++) {\n> > +                             int ret = stream->queueBuffer(nullptr);\n> > +                             if (ret)\n> > +                                     return ret;\n> > +                     }\n> > +             }\n> >       }\n> >\n> >       return 0;\n> > @@ -859,7 +893,8 @@ int PipelineHandlerRPi::prepareBuffers(Camera *camera)\n> >       /*\n> >        * Decide how many internal buffers to allocate. For now, simply look\n> >        * at how many external buffers will be provided. Will need to improve\n> > -      * this logic.\n> > +      * this logic. However, we really must have all stream allocate the same\n> > +      * number of buffers to simplify error handling in queueRequestDevice().\n> >        */\n> >       unsigned int maxBuffers = 0;\n> >       for (const Stream *s : camera->streams())\n> > @@ -867,33 +902,9 @@ int PipelineHandlerRPi::prepareBuffers(Camera *camera)\n> >                       maxBuffers = std::max(maxBuffers, s->configuration().bufferCount);\n> >\n> >       for (auto const stream : data->streams_) {\n> > -             if (stream->isExternal() || stream->isImporter()) {\n> > -                     /*\n> > -                      * If a stream is marked as external reserve memory to\n> > -                      * prepare to import as many buffers are requested in\n> > -                      * the stream configuration.\n> > -                      *\n> > -                      * If a stream is an internal stream with importer\n> > -                      * role, reserve as many buffers as possible.\n> > -                      */\n> > -                     unsigned int count = stream->isExternal()\n> > -                                                  ? stream->configuration().bufferCount\n> > -                                                  : maxBuffers;\n> > -                     ret = stream->importBuffers(count);\n> > -                     if (ret < 0)\n> > -                             return ret;\n> > -             } else {\n> > -                     /*\n> > -                      * If the stream is an internal exporter allocate and\n> > -                      * export as many buffers as possible to its internal\n> > -                      * pool.\n> > -                      */\n> > -                     ret = stream->allocateBuffers(maxBuffers);\n> > -                     if (ret < 0) {\n> > -                             freeBuffers(camera);\n> > -                             return ret;\n> > -                     }\n> > -             }\n> > +             ret = stream->prepareBuffers(maxBuffers);\n> > +             if (ret < 0)\n> > +                     return ret;\n> >       }\n> >\n> >       /*\n> > @@ -901,7 +912,7 @@ int PipelineHandlerRPi::prepareBuffers(Camera *camera)\n> >        * the IPA and RPI_IPA_EVENT_SIGNAL_ISP_PREPARE event.\n> >        */\n> >       count = 0;\n> > -     for (auto const &b : *data->unicam_[Unicam::Image].getBuffers()) {\n> > +     for (auto const &b : data->unicam_[Unicam::Image].getBuffers()) {\n> >               b->setCookie(count++);\n> >       }\n> >\n> > @@ -910,14 +921,14 @@ int PipelineHandlerRPi::prepareBuffers(Camera *camera)\n> >        * the IPA.\n> >        */\n> >       count = 0;\n> > -     for (auto const &b : *data->isp_[Isp::Stats].getBuffers()) {\n> > +     for (auto const &b : data->isp_[Isp::Stats].getBuffers()) {\n> >               b->setCookie(count++);\n> >               data->ipaBuffers_.push_back({ .id = RPiIpaMask::STATS | b->cookie(),\n> >                                             .planes = b->planes() });\n> >       }\n> >\n> >       count = 0;\n> > -     for (auto const &b : *data->unicam_[Unicam::Embedded].getBuffers()) {\n> > +     for (auto const &b : data->unicam_[Unicam::Embedded].getBuffers()) {\n> >               b->setCookie(count++);\n> >               data->ipaBuffers_.push_back({ .id = RPiIpaMask::EMBEDDED_DATA | b->cookie(),\n> >                                             .planes = b->planes() });\n> > @@ -1089,7 +1100,7 @@ void RPiCameraData::queueFrameAction(unsigned int frame, const IPAOperationData\n> >       switch (action.operation) {\n> >       case RPI_IPA_ACTION_STATS_METADATA_COMPLETE: {\n> >               unsigned int bufferId = action.data[0];\n> > -             FrameBuffer *buffer = isp_[Isp::Stats].getBuffers()->at(bufferId).get();\n> > +             FrameBuffer *buffer = isp_[Isp::Stats].getBuffers().at(bufferId);\n> >\n> >               handleStreamBuffer(buffer, &isp_[Isp::Stats]);\n> >               /* Fill the Request metadata buffer with what the IPA has provided */\n> > @@ -1100,19 +1111,19 @@ void RPiCameraData::queueFrameAction(unsigned int frame, const IPAOperationData\n> >\n> >       case RPI_IPA_ACTION_EMBEDDED_COMPLETE: {\n> >               unsigned int bufferId = action.data[0];\n> > -             FrameBuffer *buffer = unicam_[Unicam::Embedded].getBuffers()->at(bufferId).get();\n> > +             FrameBuffer *buffer = unicam_[Unicam::Embedded].getBuffers().at(bufferId);\n> >               handleStreamBuffer(buffer, &unicam_[Unicam::Embedded]);\n> >               break;\n> >       }\n> >\n> >       case RPI_IPA_ACTION_RUN_ISP: {\n> >               unsigned int bufferId = action.data[0];\n> > -             FrameBuffer *buffer = unicam_[Unicam::Image].getBuffers()->at(bufferId).get();\n> > +             FrameBuffer *buffer = unicam_[Unicam::Image].getBuffers().at(bufferId);\n> >\n> >               LOG(RPI, Debug) << \"Input re-queue to ISP, buffer id \" << buffer->cookie()\n> >                               << \", timestamp: \" << buffer->metadata().timestamp;\n> >\n> > -             isp_[Isp::Input].dev()->queueBuffer(buffer);\n> > +             isp_[Isp::Input].queueBuffer(buffer);\n> >               ispOutputCount_ = 0;\n> >               break;\n> >       }\n> > @@ -1213,22 +1224,27 @@ void RPiCameraData::ispOutputDequeue(FrameBuffer *buffer)\n> >                       << \", buffer id \" << buffer->cookie()\n> >                       << \", timestamp: \" << buffer->metadata().timestamp;\n> >\n> > -     handleStreamBuffer(buffer, stream);\n> > -\n> >       /*\n> > -      * Increment the number of ISP outputs generated.\n> > -      * This is needed to track dropped frames.\n> > +      * ISP statistics buffer must not be re-queued or sent back to the\n> > +      * application until after the IPA signals so.\n> >        */\n> > -     ispOutputCount_++;\n> > -\n> > -     /* If this is a stats output, hand it to the IPA now. */\n> >       if (stream == &isp_[Isp::Stats]) {\n> >               IPAOperationData op;\n> >               op.operation = RPI_IPA_EVENT_SIGNAL_STAT_READY;\n> >               op.data = { RPiIpaMask::STATS | buffer->cookie() };\n> >               ipa_->processEvent(op);\n> > +     } else {\n> > +             /* Any other ISP output can be handed back to the application now. */\n> > +             handleStreamBuffer(buffer, stream);\n> >       }\n> >\n> > +     /*\n> > +      * Increment the number of ISP outputs generated.\n> > +      * This is needed to track dropped frames.\n> > +      */\n> > +     ispOutputCount_++;\n> > +\n> > +\n> >       handleState();\n> >  }\n> >\n> > @@ -1241,8 +1257,12 @@ void RPiCameraData::clearIncompleteRequests()\n> >        */\n> >       for (auto const request : requestQueue_) {\n> >               for (auto const stream : streams_) {\n> > -                     if (stream->isExternal())\n> > -                             stream->dev()->queueBuffer(request->findBuffer(stream));\n> > +                     if (!stream->isExternal())\n> > +                             continue;\n> > +\n> > +                     FrameBuffer *buffer = request->findBuffer(stream);\n> > +                     if (buffer)\n> > +                             stream->queueBuffer(buffer);\n> >               }\n> >       }\n> >\n> > @@ -1271,7 +1291,7 @@ void RPiCameraData::clearIncompleteRequests()\n> >                        * Has the buffer already been handed back to the\n> >                        * request? If not, do so now.\n> >                        */\n> > -                     if (buffer->request())\n> > +                     if (buffer && buffer->request())\n> >                               pipe_->completeBuffer(camera_, request, buffer);\n> >               }\n> >\n> > @@ -1283,30 +1303,24 @@ void RPiCameraData::clearIncompleteRequests()\n> >  void RPiCameraData::handleStreamBuffer(FrameBuffer *buffer, RPi::RPiStream *stream)\n> >  {\n> >       if (stream->isExternal()) {\n> > -             if (!dropFrameCount_) {\n> > -                     Request *request = buffer->request();\n> > +             Request *request = requestQueue_.front();\n> > +\n> > +             if (!dropFrameCount_ && request->findBuffer(stream) == buffer) {\n> > +                     /*\n> > +                      * Tag the buffer as completed, returning it to the\n> > +                      * application.\n> > +                      */\n> >                       pipe_->completeBuffer(camera_, request, buffer);\n> > +             } else {\n> > +                     /*\n> > +                      * This buffer was not part of the Request, so we can\n> > +                      * recycle it.\n> > +                      */\n> > +                     stream->returnBuffer(buffer);\n> >               }\n> >       } else {\n> > -             /* Special handling for RAW buffer Requests.\n> > -              *\n> > -              * The ISP input stream is alway an import stream, but if the\n> > -              * current Request has been made for a buffer on the stream,\n> > -              * simply memcpy to the Request buffer and requeue back to the\n> > -              * device.\n> > -              */\n> > -             if (stream == &unicam_[Unicam::Image] && !dropFrameCount_) {\n> > -                     const Stream *rawStream = static_cast<const Stream *>(&isp_[Isp::Input]);\n> > -                     Request *request = requestQueue_.front();\n> > -                     FrameBuffer *raw = request->findBuffer(const_cast<Stream *>(rawStream));\n> > -                     if (raw) {\n> > -                             raw->copyFrom(buffer);\n> > -                             pipe_->completeBuffer(camera_, request, raw);\n> > -                     }\n> > -             }\n> > -\n> > -             /* Simply requeue the buffer. */\n> > -             stream->dev()->queueBuffer(buffer);\n> > +             /* Simply re-queue the buffer to the requested stream. */\n> > +             stream->queueBuffer(buffer);\n> >       }\n> >  }\n> >\n> > @@ -1390,7 +1404,7 @@ void RPiCameraData::tryRunPipeline()\n> >        * current bayer buffer will be removed and re-queued to the driver.\n> >        */\n> >       embeddedBuffer = updateQueue(embeddedQueue_, bayerBuffer->metadata().timestamp,\n> > -                                  unicam_[Unicam::Embedded].dev());\n> > +                                  &unicam_[Unicam::Embedded]);\n> >\n> >       if (!embeddedBuffer) {\n> >               LOG(RPI, Debug) << \"Could not find matching embedded buffer\";\n> > @@ -1409,7 +1423,7 @@ void RPiCameraData::tryRunPipeline()\n> >\n> >               embeddedBuffer = embeddedQueue_.front();\n> >               bayerBuffer = updateQueue(bayerQueue_, embeddedBuffer->metadata().timestamp,\n> > -                                       unicam_[Unicam::Image].dev());\n> > +                                       &unicam_[Unicam::Image]);\n> >\n> >               if (!bayerBuffer) {\n> >                       LOG(RPI, Debug) << \"Could not find matching bayer buffer - ending.\";\n> > @@ -1417,11 +1431,7 @@ void RPiCameraData::tryRunPipeline()\n> >               }\n> >       }\n> >\n> > -     /*\n> > -      * Take the first request from the queue and action the IPA.\n> > -      * Unicam buffers for the request have already been queued as they come\n> > -      * in.\n> > -      */\n> > +     /* Take the first request from the queue and action the IPA. */\n> >       Request *request = requestQueue_.front();\n> >\n> >       /*\n> > @@ -1433,12 +1443,6 @@ void RPiCameraData::tryRunPipeline()\n> >       op.controls = { request->controls() };\n> >       ipa_->processEvent(op);\n> >\n> > -     /* Queue up any ISP buffers passed into the request. */\n> > -     for (auto &stream : isp_) {\n> > -             if (stream.isExternal())\n> > -                     stream.dev()->queueBuffer(request->findBuffer(&stream));\n> > -     }\n> > -\n> >       /* Ready to use the buffers, pop them off the queue. */\n> >       bayerQueue_.pop();\n> >       embeddedQueue_.pop();\n> > @@ -1468,32 +1472,42 @@ void RPiCameraData::tryFlushQueues()\n> >        * and give a chance for the hardware to return to lock-step. We do have\n> >        * to drop all interim frames.\n> >        */\n> > -     if (unicam_[Unicam::Image].getBuffers()->size() == bayerQueue_.size() &&\n> > -         unicam_[Unicam::Embedded].getBuffers()->size() == embeddedQueue_.size()) {\n> > +     if (unicam_[Unicam::Image].getBuffers().size() == bayerQueue_.size() &&\n> > +         unicam_[Unicam::Embedded].getBuffers().size() == embeddedQueue_.size()) {\n> > +             /* This cannot happen when Unicam streams are external. */\n> > +             assert(!unicam_[Unicam::Image].isExternal());\n> > +\n> >               LOG(RPI, Warning) << \"Flushing all buffer queues!\";\n> >\n> >               while (!bayerQueue_.empty()) {\n> > -                     unicam_[Unicam::Image].dev()->queueBuffer(bayerQueue_.front());\n> > +                     unicam_[Unicam::Image].queueBuffer(bayerQueue_.front());\n> >                       bayerQueue_.pop();\n> >               }\n> >\n> >               while (!embeddedQueue_.empty()) {\n> > -                     unicam_[Unicam::Embedded].dev()->queueBuffer(embeddedQueue_.front());\n> > +                     unicam_[Unicam::Embedded].queueBuffer(embeddedQueue_.front());\n> >                       embeddedQueue_.pop();\n> >               }\n> >       }\n> >  }\n> >\n> >  FrameBuffer *RPiCameraData::updateQueue(std::queue<FrameBuffer *> &q, uint64_t timestamp,\n> > -                                     V4L2VideoDevice *dev)\n> > +                                     RPi::RPiStream *stream)\n> >  {\n> > +     /*\n> > +      * If the unicam streams are external (both have to the same), then we\n> > +      * can only return out the top buffer in the queue, and assume they have\n> > +      * been synced by queuing at the same time. We cannot drop these frames,\n> > +      * as they may have been provided externally.\n> > +      */\n> >       while (!q.empty()) {\n> >               FrameBuffer *b = q.front();\n> > -             if (b->metadata().timestamp < timestamp) {\n> > +             if (!stream->isExternal() && b->metadata().timestamp < timestamp) {\n> >                       q.pop();\n> > -                     dev->queueBuffer(b);\n> > -                     LOG(RPI, Warning) << \"Dropping input frame!\";\n> > -             } else if (b->metadata().timestamp == timestamp) {\n> > +                     stream->queueBuffer(b);\n> > +                     LOG(RPI, Warning) << \"Dropping unmatched input frame in stream \"\n> > +                                       << stream->name();\n> > +             } else if (stream->isExternal() || b->metadata().timestamp == timestamp) {\n> >                       /* The calling function will pop the item from the queue. */\n> >                       return b;\n> >               } else {\n> > diff --git a/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp b/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp\n> > index 57e5cf72..53a335e3 100644\n> > --- a/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp\n> > +++ b/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp\n> > @@ -21,30 +21,20 @@ V4L2VideoDevice *RPiStream::dev() const\n> >\n> >  void RPiStream::setExternal(bool external)\n> >  {\n> > +     /* Import streams cannot be external. */\n> > +     assert(!external || !importOnly_);\n> >       external_ = external;\n> >  }\n> >\n> >  bool RPiStream::isExternal() const\n> >  {\n> > -     /*\n> > -      * Import streams cannot be external.\n> > -      *\n> > -      * RAW capture is a special case where we simply copy the RAW\n> > -      * buffer out of the request. All other buffer handling happens\n> > -      * as if the stream is internal.\n> > -      */\n> > -     return external_ && !importOnly_;\n> > -}\n> > -\n> > -bool RPiStream::isImporter() const\n> > -{\n> > -     return importOnly_;\n> > +     return external_;\n> >  }\n> >\n> >  void RPiStream::reset()\n> >  {\n> >       external_ = false;\n> > -     internalBuffers_.clear();\n> > +     clearBuffers();\n> >  }\n> >\n> >  std::string RPiStream::name() const\n> > @@ -52,65 +42,123 @@ std::string RPiStream::name() const\n> >       return name_;\n> >  }\n> >\n> > -void RPiStream::setExternalBuffers(std::vector<std::unique_ptr<FrameBuffer>> *buffers)\n> > +void RPiStream::setExportedBuffers(std::vector<std::unique_ptr<FrameBuffer>> *buffers)\n> >  {\n> > -     externalBuffers_ = buffers;\n> > +     std::transform(buffers->begin(), buffers->end(), std::back_inserter(bufferList_),\n> > +                    [](std::unique_ptr<FrameBuffer> &b) { return b.get(); });\n> >  }\n> >\n> > -const std::vector<std::unique_ptr<FrameBuffer>> *RPiStream::getBuffers() const\n> > +const std::vector<FrameBuffer *> &RPiStream::getBuffers() const\n> >  {\n> > -     return external_ ? externalBuffers_ : &internalBuffers_;\n> > +     return bufferList_;\n> >  }\n> >\n> >  void RPiStream::releaseBuffers()\n> >  {\n> >       dev_->releaseBuffers();\n> > -     if (!external_ && !importOnly_)\n> > -             internalBuffers_.clear();\n> > +     clearBuffers();\n> >  }\n> >\n> > -int RPiStream::importBuffers(unsigned int count)\n> > +int RPiStream::prepareBuffers(unsigned int count)\n> >  {\n> > +     int ret;\n> > +\n> > +     if (!importOnly_) {\n> > +             if (count) {\n> > +                     /* Export some frame buffers for internal use. */\n> > +                     ret = dev_->exportBuffers(count, &internalBuffers_);\n> > +                     if (ret < 0)\n> > +                             return ret;\n> > +\n> > +                     /* Add these exported buffers to the internal/external buffer list. */\n> > +                     setExportedBuffers(&internalBuffers_);\n> > +\n> > +                     /* Add these buffers to the queue of internal usable buffers. */\n> > +                     for (auto const &buffer : internalBuffers_)\n> > +                             availableBuffers_.push(buffer.get());\n> > +             }\n> > +\n> > +             /* We must import all internal/external exported buffers. */\n> > +             count = bufferList_.size();\n> > +     }\n> > +\n> >       return dev_->importBuffers(count);\n> >  }\n> >\n> > -int RPiStream::allocateBuffers(unsigned int count)\n> > +int RPiStream::queueAllBuffers()\n> >  {\n> > -     return dev_->allocateBuffers(count, &internalBuffers_);\n> > -}\n> > +     int ret;\n> >\n> > -int RPiStream::queueBuffers()\n> > -{\n> >       if (external_)\n> >               return 0;\n> >\n> > -     for (auto &b : internalBuffers_) {\n> > -             int ret = dev_->queueBuffer(b.get());\n> > -             if (ret) {\n> > -                     LOG(RPISTREAM, Error) << \"Failed to queue buffers for \"\n> > -                                           << name_;\n> > +     while (!availableBuffers_.empty()) {\n> > +             ret = queueBuffer(availableBuffers_.front());\n> > +             if (ret < 0)\n> >                       return ret;\n> > -             }\n> > +\n> > +             availableBuffers_.pop();\n> >       }\n> >\n> >       return 0;\n> >  }\n> >\n> > -bool RPiStream::findFrameBuffer(FrameBuffer *buffer) const\n> > +int RPiStream::queueBuffer(FrameBuffer *buffer)\n> > +{\n> > +     /*\n> > +      * A nullptr buffer implies an external stream, but no external\n> > +      * buffer has been supplied. So, pick one from the availableBuffers_\n> > +      * queue.\n> > +      */\n> > +     if (!buffer) {\n> > +             if (availableBuffers_.empty()) {\n> > +                     LOG(RPISTREAM, Warning) << \"No buffers available for \"\n> > +                                             << name_;\n> > +                     return -EINVAL;\n> > +             }\n> > +\n> > +             buffer = availableBuffers_.front();\n> > +             availableBuffers_.pop();\n> > +     }\n> > +\n> > +     LOG(RPISTREAM, Debug) << \"Queuing buffer \" << buffer->cookie()\n> > +                           << \" for \" << name_;\n> > +\n> > +     int ret = dev_->queueBuffer(buffer);\n> > +     if (ret) {\n> > +             LOG(RPISTREAM, Error) << \"Failed to queue buffer for \"\n> > +                                   << name_;\n> > +     }\n> > +\n> > +     return ret;\n> > +}\n> > +\n> > +void RPiStream::returnBuffer(FrameBuffer *buffer)\n> >  {\n> > -     auto start = external_ ? externalBuffers_->begin() : internalBuffers_.begin();\n> > -     auto end = external_ ? externalBuffers_->end() : internalBuffers_.end();\n> > +     /* This can only be called for external streams. */\n> > +     assert(external_);\n> > +\n> > +     availableBuffers_.push(buffer);\n> > +}\n> >\n> > +bool RPiStream::findFrameBuffer(FrameBuffer *buffer) const\n> > +{\n> >       if (importOnly_)\n> >               return false;\n> >\n> > -     if (std::find_if(start, end,\n> > -                      [buffer](std::unique_ptr<FrameBuffer> const &ref) { return ref.get() == buffer; }) != end)\n> > +     if (std::find(bufferList_.begin(), bufferList_.end(), buffer) != bufferList_.end())\n> >               return true;\n>\n> Cant this be made simpler by ?\n>\n>     if (bufferList_.find(buffer) != bufferList_.end())\n>\n\nbufferList_ is a std::vector, so in this case I must use std::find here.\n\n> >\n> >       return false;\n> >  }\n> >\n> > +void RPiStream::clearBuffers()\n> > +{\n> > +     availableBuffers_ = std::queue<FrameBuffer *>{};\n> > +     internalBuffers_.clear();\n> > +     bufferList_.clear();\n> > +}\n> > +\n> >  } /* namespace RPi */\n> >\n> >  } /* namespace libcamera */\n> > diff --git a/src/libcamera/pipeline/raspberrypi/rpi_stream.h b/src/libcamera/pipeline/raspberrypi/rpi_stream.h\n> > index 40fff81d..019e236d 100644\n> > --- a/src/libcamera/pipeline/raspberrypi/rpi_stream.h\n> > +++ b/src/libcamera/pipeline/raspberrypi/rpi_stream.h\n> > @@ -38,21 +38,22 @@ public:\n> >       V4L2VideoDevice *dev() const;\n> >       void setExternal(bool external);\n> >       bool isExternal() const;\n> > -     bool isImporter() const;\n> >       void reset();\n> >       std::string name() const;\n> > -     void setExternalBuffers(std::vector<std::unique_ptr<FrameBuffer>> *buffers);\n> > -     const std::vector<std::unique_ptr<FrameBuffer>> *getBuffers() const;\n> > +     void setExportedBuffers(std::vector<std::unique_ptr<FrameBuffer>> *buffers);\n> > +     const std::vector<FrameBuffer *> &getBuffers() const;\n> >       void releaseBuffers();\n> > -     int importBuffers(unsigned int count);\n> > -     int allocateBuffers(unsigned int count);\n> > -     int queueBuffers();\n> > +     int prepareBuffers(unsigned int count);\n> > +     int queueAllBuffers();\n> > +     int queueBuffer(FrameBuffer *buffer);\n> > +     void returnBuffer(FrameBuffer *buffer);\n> >       bool findFrameBuffer(FrameBuffer *buffer) const;\n> >\n> >  private:\n> > +     void clearBuffers();\n> >       /*\n> >        * Indicates that this stream is active externally, i.e. the buffers\n> > -      * are provided by the application.\n> > +      * might be provided by (and returned to) the application.\n> >        */\n> >       bool external_;\n> >       /* Indicates that this stream only imports buffers, e.g. ISP input. */\n> > @@ -61,10 +62,19 @@ private:\n> >       std::string name_;\n> >       /* The actual device stream. */\n> >       std::unique_ptr<V4L2VideoDevice> dev_;\n> > -     /* Internally allocated framebuffers associated with this device stream. */\n> > +     /* All framebuffers associated with this device stream. */\n> > +     std::vector<FrameBuffer *> bufferList_;\n> > +     /*\n> > +      * List of frame buffer that we can use if none have been provided by\n> > +      * the application for external streams. This is populated by the\n> > +      * buffers exported internally.\n> > +      */\n> > +     std::queue<FrameBuffer *> availableBuffers_;\n> > +     /*\n> > +      * This is a list of buffers exported internally. Need to keep this around\n> > +      * as the stream needs to maintain ownership of these buffers.\n> > +      */\n> >       std::vector<std::unique_ptr<FrameBuffer>> internalBuffers_;\n> > -     /* Externally allocated framebuffers associated with this device stream. */\n> > -     std::vector<std::unique_ptr<FrameBuffer>> *externalBuffers_;\n> >  };\n> >\n> >  /*\n> > --\n> > 2.25.1\n> >\n> > _______________________________________________\n> > libcamera-devel mailing list\n> > libcamera-devel@lists.libcamera.org\n> > https://lists.libcamera.org/listinfo/libcamera-devel\n>\n> --\n> Regards,\n> Niklas Söderlund\n\nRegards,\nNaush","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 68CAFC0109\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 20 Jul 2020 08:30:45 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id C4CF6605B7;\n\tMon, 20 Jul 2020 10:30:44 +0200 (CEST)","from mail-lj1-x22d.google.com (mail-lj1-x22d.google.com\n\t[IPv6:2a00:1450:4864:20::22d])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id CE5786039E\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 20 Jul 2020 10:30:43 +0200 (CEST)","by mail-lj1-x22d.google.com with SMTP id e4so19296082ljn.4\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 20 Jul 2020 01:30:43 -0700 (PDT)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (2048-bit key;\n\tunprotected) header.d=raspberrypi.com header.i=@raspberrypi.com\n\theader.b=\"VqJyyvNB\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google;\n\th=mime-version:references:in-reply-to:from:date:message-id:subject:to\n\t:cc:content-transfer-encoding;\n\tbh=yZo5SvoN7NRhb4DHR1uBqdFVILmufKVEEHAloQAfmA4=;\n\tb=VqJyyvNB8fU3EnC2Op4Xs4S4DV4q6cs9CkpQCS9J8qyYBcD4UyvYN6bunXCKJK/plE\n\tXvj2FTF0YDgv66vCoQlkvRLuHWmbIJXjHTz8dWpulgJK7Ws/9tPaNq6s1KlqyOH9IDsK\n\tYIVcPvunYlmhCs2/33ym340vgFTuX6+6DC4a2Fug51NSSiTnhctMRGSh9rbbsknJG+h9\n\t5aZhwmKQ5S6PBCNjCZmX3+qNO+uXSxYpu+4SIpGJOdZYrfrW2PfXNjUxKj3Aj8cI2uNO\n\t80Xe9RxGGr0j2OyhLPtFKM99FBxoylsAsdJOmXSUw4e99s++q1u8RnQzmH0NBlyrvuu8\n\tUUEA==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:mime-version:references:in-reply-to:from:date\n\t:message-id:subject:to:cc:content-transfer-encoding;\n\tbh=yZo5SvoN7NRhb4DHR1uBqdFVILmufKVEEHAloQAfmA4=;\n\tb=RkrOEBbXo1lUSJ0V5ljlmUAJPVhq51JpBL8aG9MQqBzg6wUO5rAyfOVRjvsxi8sOSh\n\tTulRJHbnlK13xtFeLXHz6gaG/fCp14oRWxHcOA067/Ux737KKy9A1DSDeAMlNkwoo3Fo\n\ttvwEL+cjlNEhg8SQbyFd1zXn/wu9fnQdAzLRF3ErtfGrQlMGk0JUT/qh5ruRz4D9np6Q\n\t4jwHRVQnXGyFY7vhi61sED0BjLLA2bDwgw1G0OcWqrsFXfI+08+F0MO22DYmnHrG19Ko\n\tsqyHxchkcf1DAJM8OqLOaZBvsmZxUdFHgM2opK5x5c27XMFBvYZpmqecsXK9nRT6h7JF\n\tkOow==","X-Gm-Message-State":"AOAM530BSdMmg2IdHBx6rwxgPNsCEZhAxBtX/Yv5pTQxZc40yVvGbLck\n\tgcsfalQqj0l3AWhovCFj1Rvzyfpim0vdBv1NsCD6pM7f5CY=","X-Google-Smtp-Source":"ABdhPJwJwjt+CQvV5YnP1yGLCVu5D3k9NHzOM+FAuCTYYk7vuI0k1d29H/gtlzzv1LPCVeOB8aAjzoS/UzyHZgCr7ng=","X-Received":"by 2002:a2e:a30f:: with SMTP id\n\tl15mr9885789lje.228.1595233842722; \n\tMon, 20 Jul 2020 01:30:42 -0700 (PDT)","MIME-Version":"1.0","References":"<20200717085410.732308-1-naush@raspberrypi.com>\n\t<20200717085410.732308-7-naush@raspberrypi.com>\n\t<20200718153344.GD555842@oden.dyn.berto.se>","In-Reply-To":"<20200718153344.GD555842@oden.dyn.berto.se>","From":"Naushir Patuck <naush@raspberrypi.com>","Date":"Mon, 20 Jul 2020 09:30:26 +0100","Message-ID":"<CAEmqJPquGFOpbv7GNQ3ui9fESKk1T_JkoMEbr5eOs6NL-nq_bw@mail.gmail.com>","To":"=?utf-8?q?Niklas_S=C3=B6derlund?= <niklas.soderlund@ragnatech.se>","Subject":"Re: [libcamera-devel] [PATCH v3 06/10] libcamera: pipeline:\n\traspberrypi: Rework stream buffer logic for zero-copy","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Cc":"libcamera-devel@lists.libcamera.org","Content-Type":"text/plain; charset=\"utf-8\"","Content-Transfer-Encoding":"base64","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]