{"id":8833,"url":"https://patchwork.libcamera.org/api/1.1/patches/8833/?format=json","web_url":"https://patchwork.libcamera.org/patch/8833/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20200715140703.566298-11-naush@raspberrypi.com>","date":"2020-07-15T14:07:03","name":"[libcamera-devel,v2,10/10] libcamera: pipeline: raspberrypi: Handle any externally allocated FrameBuffer","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"e9f2c0bf39a30d1ec0af799aada0d4f7f651ed49","submitter":{"id":34,"url":"https://patchwork.libcamera.org/api/1.1/people/34/?format=json","name":"Naushir Patuck","email":"naush@raspberrypi.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/8833/mbox/","series":[{"id":1111,"url":"https://patchwork.libcamera.org/api/1.1/series/1111/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1111","date":"2020-07-15T14:06:53","name":"Zero-copy RAW stream work","version":2,"mbox":"https://patchwork.libcamera.org/series/1111/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/8833/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/8833/checks/","tags":{},"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 8962DBD790\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 15 Jul 2020 14:07:29 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 4D52F60BF0;\n\tWed, 15 Jul 2020 16:07:29 +0200 (CEST)","from mail-wm1-x336.google.com (mail-wm1-x336.google.com\n\t[IPv6:2a00:1450:4864:20::336])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 7BBCF60BEA\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 15 Jul 2020 16:07:27 +0200 (CEST)","by mail-wm1-x336.google.com with SMTP id l2so6002744wmf.0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 15 Jul 2020 07:07:27 -0700 (PDT)","from naushir-VirtualBox.patuck.local ([88.97.76.4])\n\tby smtp.gmail.com with ESMTPSA id\n\tl67sm4195339wml.13.2020.07.15.07.07.24\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tWed, 15 Jul 2020 07:07:25 -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=\"Rm1m7xWy\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google;\n\th=from:to:cc:subject:date:message-id:in-reply-to:references\n\t:mime-version:content-transfer-encoding;\n\tbh=6zW1E+HopE1BSrWRdHQY+/uzS0skXS5tqcgLuihqK6M=;\n\tb=Rm1m7xWycW/1y6hTb//8eN8NvRATLZAcq0b+Fp1j6tgDuwGiIkdGIEvx9L8MHATfpi\n\tP3t+gt+YJBmCoANBAFE2vKtXyK3iscddPEJQTBeYkL3j4eOenY7N8ePFdp3LdN9xTCIs\n\tjhXvE6PvCcCkjPy1yxL1XoIAerhK0YWVgG+maOtR0q7w3o1HDnMIhZ2jopZ47WxfpjX2\n\tS1c6a/KVI76fnIuJi+QroO/bdajpljoJw4VleDlNB0FiYl6p/5+5DT0jsgZgG1SP8VSi\n\tbAcTvuZqnRrsk6/TyXtJIhYqzUuJjlfDpsNPwXfPsL/Grpbp7zb7k8HJesJoV49i2cvS\n\tO+sA==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\n\t:references:mime-version:content-transfer-encoding;\n\tbh=6zW1E+HopE1BSrWRdHQY+/uzS0skXS5tqcgLuihqK6M=;\n\tb=dITEiWQXZRYfj2Tk7V/U3jwYxwVnAb8DcjolEuT98x+FVZ6YfWB+4aGrEskFLPuQ/G\n\tEKwMu7YkIW3jdzX2TNL+bgOwxXFID3/12vD1NzuqNNvmIzP0VqVEXiLD7PeIAuJFTmKN\n\tPf3YzO7lGR5IRVh7PbjJo9nP2G5aU0PDqqCLKF+A4cmhlnVBInbzgnHgKYJ9n9c6d/Ye\n\tPabuR2QqRAQNNI/Z8NVzVoBjnqJY/qohpTlfQd2+g3ojsWVkUdV2M67/A56O2nazGk35\n\tnKyjfmAOQ0DU2+TePO9RD30H9NxTJLnRZwquU58v4/G4VGx9R1yrxh9Ec7N5wZbCyThi\n\tgOuQ==","X-Gm-Message-State":"AOAM532DPKMK3I73vps6qwZb5uXyKnvZDWm2UCg7ZuxSoypHSdu7+EOy\n\t8rm19WQit6xQw5cjfHKkKc19MocEUSE=","X-Google-Smtp-Source":"ABdhPJyOCV2s9wkKE/tLHILo1yMThiQwPlSHTv0dKtUtARUja/GuGl4UMELH32wE2nCRR5ExMPaXNg==","X-Received":"by 2002:a1c:2905:: with SMTP id p5mr9318955wmp.91.1594822046632; \n\tWed, 15 Jul 2020 07:07:26 -0700 (PDT)","From":"Naushir Patuck <naush@raspberrypi.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Wed, 15 Jul 2020 15:07:03 +0100","Message-Id":"<20200715140703.566298-11-naush@raspberrypi.com>","X-Mailer":"git-send-email 2.25.1","In-Reply-To":"<20200715140703.566298-1-naush@raspberrypi.com>","References":"<20200715140703.566298-1-naush@raspberrypi.com>","MIME-Version":"1.0","Subject":"[libcamera-devel] [PATCH v2 10/10] libcamera: pipeline:\n\traspberrypi: Handle any externally allocated FrameBuffer","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>","Content-Type":"text/plain; charset=\"us-ascii\"","Content-Transfer-Encoding":"7bit","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Handle the case where a FrameBuffer that has been externally allocated\n(i.e. not through the v4l2 video device) is passed into a Request.\n\nWe must store the buffer pointer in our internal buffer list to identify\nwhen used, as well as mmap the buffer in the IPA if needed.\n\nSigned-off-by: Naushir Patuck <naush@raspberrypi.com>\n---\n .../pipeline/raspberrypi/raspberrypi.cpp      | 90 +++++++++++--------\n .../pipeline/raspberrypi/rpi_stream.cpp       |  5 ++\n .../pipeline/raspberrypi/rpi_stream.h         |  1 +\n 3 files changed, 61 insertions(+), 35 deletions(-)","diff":"diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\nindex fc6fbfc3..a32ec687 100644\n--- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n+++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n@@ -172,8 +172,8 @@ public:\n \tRPi::RPiDevice<Isp, 4> isp_;\n \t/* The vector below is just for convenience when iterating over all streams. */\n \tstd::vector<RPi::RPiStream *> streams_;\n-\t/* Buffers passed to the IPA. */\n-\tstd::vector<IPABuffer> ipaBuffers_;\n+\t/* Stores the ids of the buffers mapped in the IPA. */\n+\tstd::vector<unsigned int> ipaBufferIds_;\n \n \t/* VCSM allocation helper. */\n \t::RPi::Vcsm vcsm_;\n@@ -193,7 +193,6 @@ public:\n \tstd::queue<FrameBuffer *> bayerQueue_;\n \tstd::queue<FrameBuffer *> embeddedQueue_;\n \tstd::deque<Request *> requestQueue_;\n-\n \tunsigned int dropFrameCount_;\n \n private:\n@@ -246,6 +245,8 @@ private:\n \tint queueAllBuffers(Camera *camera);\n \tint prepareBuffers(Camera *camera);\n \tvoid freeBuffers(Camera *camera);\n+\tvoid mapBuffers(Camera *camera, const std::vector<FrameBuffer *> &buffers,\n+\t\t\tunsigned int startId);\n \n \tMediaDevice *unicam_;\n \tMediaDevice *isp_;\n@@ -730,18 +731,36 @@ int PipelineHandlerRPi::queueRequestDevice(Camera *camera, Request *request)\n \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\tif (!stream->isExternal())\n+\t\t\tcontinue;\n+\n+\t\tFrameBuffer *buffer = request->findBuffer(stream);\n+\t\tif (stream->getBufferIndex(buffer) == -1) {\n \t\t\t/*\n-\t\t\t * If we have been asked to drop frames by the IPA, passing\n-\t\t\t * in a nullptr here will queue an internally allocated buffer.\n-\t\t\t * The Request buffer will be stored in another queue to be\n-\t\t\t * queued to the device - in sync with the Request.\n+\t\t\t * This buffer is not recognised, so it must have been allocated\n+\t\t\t * outside the v4l2 device. Store it in the stream buffer list\n+\t\t\t * so we can track it.\n \t\t\t */\n-\t\t\tint ret = stream->queueBuffer(data->dropFrameCount_ ? nullptr : buffer);\n-\t\t\tif (ret)\n-\t\t\t\treturn ret;\n+\t\t\tstream->setExternalBuffer(buffer);\n+\n+\t\t\t/* Also get the IPA to mmap it if needed. */\n+\t\t\tif (stream == &data->unicam_[Unicam::Embedded]) {\n+\t\t\t\tmapBuffers(camera, { buffer },\n+\t\t\t\t\t   RPiIpaMask::EMBEDDED_DATA | (stream->getBuffers().size() - 1));\n+\t\t\t} else if (stream == &data->isp_[Isp::Stats]) {\n+\t\t\t\tmapBuffers(camera, { buffer },\n+\t\t\t\t\t   RPiIpaMask::STATS | (stream->getBuffers().size() - 1));\n+\t\t\t}\n \t\t}\n+\t\t/*\n+\t\t * If we have been asked to drop frames by the IPA, passing\n+\t\t * in a nullptr here will queue an internally allocated buffer.\n+\t\t * The Request buffer will be stored in another queue to be\n+\t\t * queued to the device - in sync with the Request.\n+\t\t */\n+\t\tint ret = stream->queueBuffer(data->dropFrameCount_ ? nullptr : buffer);\n+\t\tif (ret)\n+\t\t\treturn ret;\n \t}\n \n \t/* Push the request to the back of the queue. */\n@@ -919,7 +938,6 @@ int PipelineHandlerRPi::queueAllBuffers(Camera *camera)\n int PipelineHandlerRPi::prepareBuffers(Camera *camera)\n {\n \tRPiCameraData *data = cameraData(camera);\n-\tunsigned int index;\n \tint ret;\n \n \t/*\n@@ -939,42 +957,44 @@ int PipelineHandlerRPi::prepareBuffers(Camera *camera)\n \t\t\treturn ret;\n \t}\n \n+\t/*\n+\t * Pass the stats and embedded data buffers to the IPA. No other\n+\t * buffers need to be passed.\n+\t */\n+\tmapBuffers(camera, data->isp_[Isp::Stats].getBuffers(), RPiIpaMask::STATS);\n+\tmapBuffers(camera, data->unicam_[Unicam::Embedded].getBuffers(), RPiIpaMask::EMBEDDED_DATA);\n+\n+\treturn 0;\n+}\n+\n+void PipelineHandlerRPi::mapBuffers(Camera *camera, const std::vector<FrameBuffer *> &buffers,\n+\t\t\t\t    unsigned int startId)\n+{\n+\tRPiCameraData *data = cameraData(camera);\n+\tstd::vector<IPABuffer> ipaBuffers;\n+\tunsigned int id = startId;\n \t/*\n \t * Link the FrameBuffers with the index of their position in the vector\n-\t * stored in the RPi stream object.\n+\t * stored in the RPi stream object - along with an identifer mask.\n \t *\n \t * This will allow us to identify buffers passed between the pipeline\n \t * handler and the IPA.\n \t */\n-\tindex = 0;\n-\tfor (auto const &b : data->isp_[Isp::Stats].getBuffers()) {\n-\t\tdata->ipaBuffers_.push_back({ .id = RPiIpaMask::STATS | index,\n-\t\t\t\t\t      .planes = b->planes() });\n-\t\tindex++;\n+\tfor (auto const &b : buffers) {\n+\t\tipaBuffers.push_back({ .id = id, .planes = b->planes() });\n+\t\tdata->ipaBufferIds_.push_back(id);\n+\t\tid++;\n \t}\n \n-\tindex = 0;\n-\tfor (auto const &b : data->unicam_[Unicam::Embedded].getBuffers()) {\n-\t\tdata->ipaBuffers_.push_back({ .id = RPiIpaMask::EMBEDDED_DATA | index,\n-\t\t\t\t\t      .planes = b->planes() });\n-\t\tindex++;\n-\t}\n-\n-\tdata->ipa_->mapBuffers(data->ipaBuffers_);\n-\n-\treturn 0;\n+\tdata->ipa_->mapBuffers(ipaBuffers);\n }\n \n void PipelineHandlerRPi::freeBuffers(Camera *camera)\n {\n \tRPiCameraData *data = cameraData(camera);\n \n-\tstd::vector<unsigned int> ids;\n-\tfor (IPABuffer &ipabuf : data->ipaBuffers_)\n-\t\tids.push_back(ipabuf.id);\n-\n-\tdata->ipa_->unmapBuffers(ids);\n-\tdata->ipaBuffers_.clear();\n+\tdata->ipa_->unmapBuffers(data->ipaBufferIds_);\n+\tdata->ipaBufferIds_.clear();\n \n \tfor (auto const stream : data->streams_)\n \t\tstream->releaseBuffers();\ndiff --git a/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp b/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp\nindex 97f87ad7..862a094a 100644\n--- a/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp\n+++ b/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp\n@@ -48,6 +48,11 @@ void RPiStream::setExternalBuffers(std::vector<std::unique_ptr<FrameBuffer>> *bu\n \t\t       [](std::unique_ptr<FrameBuffer> &b) { return b.get(); });\n }\n \n+void RPiStream::setExternalBuffer(FrameBuffer *buffer)\n+{\n+\tbufferList_.push_back(buffer);\n+}\n+\n const std::vector<FrameBuffer *> &RPiStream::getBuffers() const\n {\n \treturn bufferList_;\ndiff --git a/src/libcamera/pipeline/raspberrypi/rpi_stream.h b/src/libcamera/pipeline/raspberrypi/rpi_stream.h\nindex 16b90fac..266441df 100644\n--- a/src/libcamera/pipeline/raspberrypi/rpi_stream.h\n+++ b/src/libcamera/pipeline/raspberrypi/rpi_stream.h\n@@ -41,6 +41,7 @@ public:\n \tvoid reset();\n \tstd::string name() const;\n \tvoid setExternalBuffers(std::vector<std::unique_ptr<FrameBuffer>> *buffers);\n+\tvoid setExternalBuffer(FrameBuffer *buffer);\n \tconst std::vector<FrameBuffer *> &getBuffers() const;\n \tvoid releaseBuffers();\n \tint prepareBuffers(unsigned int count);\n","prefixes":["libcamera-devel","v2","10/10"]}