[{"id":12512,"web_url":"https://patchwork.libcamera.org/comment/12512/","msgid":"<20200915011406.GW15543@pendragon.ideasonboard.com>","date":"2020-09-15T01:14:06","subject":"Re: [libcamera-devel] [PATCH v2 10/13] libcamera: pipeline: rkisp1:\n\tConfigure self path","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Niklas,\n\nThank you for the patch.\n\nOn Mon, Sep 14, 2020 at 04:21:46PM +0200, Niklas Söderlund wrote:\n> Allow for both the main and self path streams to be configured. This\n> change adds the self path as an internal stream to the pipeline handler.\n> It is not exposed as a Camera stream so it can not yet be used.\n> \n> Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n> ---\n>  src/libcamera/pipeline/rkisp1/rkisp1.cpp | 200 ++++++++++++++++-------\n>  1 file changed, 141 insertions(+), 59 deletions(-)\n> \n> diff --git a/src/libcamera/pipeline/rkisp1/rkisp1.cpp b/src/libcamera/pipeline/rkisp1/rkisp1.cpp\n> index 5aa01f87afd71183..01cd461427e3a6dc 100644\n> --- a/src/libcamera/pipeline/rkisp1/rkisp1.cpp\n> +++ b/src/libcamera/pipeline/rkisp1/rkisp1.cpp\n> @@ -135,7 +135,8 @@ public:\n>  \t\t\t V4L2VideoDevice *selfPathVideo)\n>  \t\t: CameraData(pipe), sensor_(nullptr), frame_(0),\n>  \t\t  frameInfo_(pipe), mainPathVideo_(mainPathVideo),\n> -\t\t  selfPathVideo_(selfPathVideo)\n> +\t\t  selfPathVideo_(selfPathVideo), mainPathActive_(false),\n> +\t\t  selfPathActive_(false)\n>  \t{\n>  \t}\n>  \n> @@ -147,6 +148,7 @@ public:\n>  \tint loadIPA();\n>  \n>  \tStream mainPathStream_;\n> +\tStream selfPathStream_;\n>  \tCameraSensor *sensor_;\n>  \tunsigned int frame_;\n>  \tstd::vector<IPABuffer> ipaBuffers_;\n> @@ -156,6 +158,9 @@ public:\n>  \tV4L2VideoDevice *mainPathVideo_;\n>  \tV4L2VideoDevice *selfPathVideo_;\n>  \n> +\tbool mainPathActive_;\n> +\tbool selfPathActive_;\n> +\n>  private:\n>  \tvoid queueFrameAction(unsigned int frame,\n>  \t\t\t      const IPAOperationData &action);\n> @@ -617,7 +622,6 @@ int PipelineHandlerRkISP1::configure(Camera *camera, CameraConfiguration *c)\n>  \tRkISP1CameraConfiguration *config =\n>  \t\tstatic_cast<RkISP1CameraConfiguration *>(c);\n>  \tRkISP1CameraData *data = cameraData(camera);\n> -\tStreamConfiguration &cfg = config->at(0);\n>  \tCameraSensor *sensor = data->sensor_;\n>  \tint ret;\n>  \n> @@ -659,37 +663,55 @@ int PipelineHandlerRkISP1::configure(Camera *camera, CameraConfiguration *c)\n>  \n>  \tLOG(RkISP1, Debug) << \"ISP output pad configured with \" << format.toString();\n>  \n> -\tret = mainPathResizer_->setFormat(0, &format);\n> -\tif (ret < 0)\n> -\t\treturn ret;\n> -\n> -\tLOG(RkISP1, Debug) << \"Resizer input pad configured with \" << format.toString();\n> -\n> -\tformat.size = cfg.size;\n> -\n> -\tLOG(RkISP1, Debug) << \"Configuring resizer output pad with \" << format.toString();\n> -\n> -\tret = mainPathResizer_->setFormat(1, &format);\n> -\tif (ret < 0)\n> -\t\treturn ret;\n> -\n> -\tLOG(RkISP1, Debug) << \"Resizer output pad configured with \" << format.toString();\n> -\n> -\tconst PixelFormatInfo &info = PixelFormatInfo::info(cfg.pixelFormat);\n> -\tV4L2DeviceFormat outputFormat = {};\n> -\toutputFormat.fourcc = mainPathVideo_->toV4L2PixelFormat(cfg.pixelFormat);\n> -\toutputFormat.size = cfg.size;\n> -\toutputFormat.planesCount = info.numPlanes();\n> -\n> -\tret = mainPathVideo_->setFormat(&outputFormat);\n> -\tif (ret)\n> -\t\treturn ret;\n> -\n> -\tif (outputFormat.size != cfg.size ||\n> -\t    outputFormat.fourcc != mainPathVideo_->toV4L2PixelFormat(cfg.pixelFormat)) {\n> -\t\tLOG(RkISP1, Error)\n> -\t\t\t<< \"Unable to configure capture in \" << cfg.toString();\n> -\t\treturn -EINVAL;\n> +\tdata->mainPathActive_ = false;\n> +\tdata->selfPathActive_ = false;\n> +\tfor (const StreamConfiguration &cfg : *config) {\n> +\t\tV4L2SubdeviceFormat ispFormat = format;\n> +\t\tV4L2Subdevice *resizer;\n> +\t\tV4L2VideoDevice *video;\n> +\n> +\t\tif (cfg.stream() == &data->mainPathStream_) {\n> +\t\t\tresizer = mainPathResizer_;\n> +\t\t\tvideo = mainPathVideo_;\n> +\t\t\tdata->mainPathActive_ = true;\n> +\t\t} else {\n> +\t\t\tresizer = selfPathResizer_;\n> +\t\t\tvideo = selfPathVideo_;\n> +\t\t\tdata->selfPathActive_ = true;\n> +\t\t}\n\nPretty please, could you address the request from 08/13 on top ? This is\ngetting messy. I wouldn't mind if it was addressed as part of this\nseries, but going right on top would be acceptable.\n\n> +\n> +\t\tret = resizer->setFormat(0, &ispFormat);\n> +\t\tif (ret < 0)\n> +\t\t\treturn ret;\n> +\n> +\t\tLOG(RkISP1, Debug) << \"Resizer input pad configured with \" << ispFormat.toString();\n\n\t\tLOG(RkISP1, Debug)\n\t\t\t<< \"Resizer input pad configured with \"\n\t\t\t<< ispFormat.toString();\n\nand should this print which resizer is configured ?\n\n> +\n> +\t\tispFormat.size = cfg.size;\n> +\n> +\t\tLOG(RkISP1, Debug) << \"Configuring resizer output pad with \" << ispFormat.toString();\n\nDitto.\n\n> +\n> +\t\tret = resizer->setFormat(1, &ispFormat);\n> +\t\tif (ret < 0)\n> +\t\t\treturn ret;\n> +\n> +\t\tLOG(RkISP1, Debug) << \"Resizer output pad configured with \" << ispFormat.toString();\n\nAnd here too.\n\n> +\n> +\t\tconst PixelFormatInfo &info = PixelFormatInfo::info(cfg.pixelFormat);\n> +\t\tV4L2DeviceFormat outputFormat = {};\n> +\t\toutputFormat.fourcc = video->toV4L2PixelFormat(cfg.pixelFormat);\n> +\t\toutputFormat.size = cfg.size;\n> +\t\toutputFormat.planesCount = info.numPlanes();\n> +\n> +\t\tret = video->setFormat(&outputFormat);\n> +\t\tif (ret)\n> +\t\t\treturn ret;\n> +\n> +\t\tif (outputFormat.size != cfg.size ||\n> +\t\t    outputFormat.fourcc != video->toV4L2PixelFormat(cfg.pixelFormat)) {\n> +\t\t\tLOG(RkISP1, Error)\n> +\t\t\t\t<< \"Unable to configure capture in \" << cfg.toString();\n> +\t\t\treturn -EINVAL;\n> +\t\t}\n>  \t}\n>  \n>  \tV4L2DeviceFormat paramFormat = {};\n> @@ -704,28 +726,46 @@ int PipelineHandlerRkISP1::configure(Camera *camera, CameraConfiguration *c)\n>  \tif (ret)\n>  \t\treturn ret;\n>  \n> -\tcfg.setStream(&data->mainPathStream_);\n> -\n>  \treturn 0;\n>  }\n>  \n>  int PipelineHandlerRkISP1::exportFrameBuffers([[maybe_unused]] Camera *camera, Stream *stream,\n>  \t\t\t\t\t      std::vector<std::unique_ptr<FrameBuffer>> *buffers)\n>  {\n> +\tRkISP1CameraData *data = cameraData(camera);\n>  \tunsigned int count = stream->configuration().bufferCount;\n> -\treturn mainPathVideo_->exportBuffers(count, buffers);\n> +\n> +\tif (stream == &data->mainPathStream_)\n> +\t\treturn mainPathVideo_->exportBuffers(count, buffers);\n> +\n> +\tif (stream == &data->selfPathStream_)\n\nelse if ?\n\n> +\t\treturn selfPathVideo_->exportBuffers(count, buffers);\n> +\n> +\treturn -EINVAL;\n>  }\n>  \n>  int PipelineHandlerRkISP1::allocateBuffers(Camera *camera)\n>  {\n>  \tRkISP1CameraData *data = cameraData(camera);\n> -\tunsigned int count = data->mainPathStream_.configuration().bufferCount;\n>  \tunsigned int ipaBufferId = 1;\n>  \tint ret;\n>  \n> -\tret = mainPathVideo_->importBuffers(count);\n> -\tif (ret < 0)\n> -\t\tgoto error;\n> +\tunsigned int count = std::max({\n> +\t\tdata->mainPathStream_.configuration().bufferCount,\n> +\t\tdata->selfPathStream_.configuration().bufferCount,\n> +\t});\n\nWhy max, instead of using the respective values below ?\n\n> +\n> +\tif (data->mainPathActive_) {\n> +\t\tret = mainPathVideo_->importBuffers(count);\n> +\t\tif (ret < 0)\n> +\t\t\tgoto error;\n> +\t}\n> +\n> +\tif (data->selfPathActive_) {\n> +\t\tret = selfPathVideo_->importBuffers(count);\n> +\t\tif (ret < 0)\n> +\t\t\tgoto error;\n> +\t}\n>  \n>  \tret = param_->allocateBuffers(count, &paramBuffers_);\n>  \tif (ret < 0)\n> @@ -757,6 +797,7 @@ error:\n>  \tparamBuffers_.clear();\n>  \tstatBuffers_.clear();\n>  \tmainPathVideo_->releaseBuffers();\n> +\tselfPathVideo_->releaseBuffers();\n>  \n>  \treturn ret;\n>  }\n> @@ -790,6 +831,9 @@ int PipelineHandlerRkISP1::freeBuffers(Camera *camera)\n>  \tif (mainPathVideo_->releaseBuffers())\n>  \t\tLOG(RkISP1, Error) << \"Failed to release main path buffers\";\n>  \n> +\tif (selfPathVideo_->releaseBuffers())\n> +\t\tLOG(RkISP1, Error) << \"Failed to release self path buffers\";\n> +\n>  \treturn 0;\n>  }\n>  \n> @@ -832,15 +876,47 @@ int PipelineHandlerRkISP1::start(Camera *camera)\n>  \t\treturn ret;\n>  \t}\n>  \n> -\tret = mainPathVideo_->streamOn();\n> -\tif (ret) {\n> -\t\tparam_->streamOff();\n> -\t\tstat_->streamOff();\n> -\t\tdata->ipa_->stop();\n> -\t\tfreeBuffers(camera);\n> -\n> -\t\tLOG(RkISP1, Error)\n> -\t\t\t<< \"Failed to start camera \" << camera->id();\n> +\tstd::map<unsigned int, IPAStream> streamConfig;\n> +\n> +\tif (data->mainPathActive_) {\n> +\t\tret = mainPathVideo_->streamOn();\n> +\t\tif (ret) {\n> +\t\t\tparam_->streamOff();\n> +\t\t\tstat_->streamOff();\n> +\t\t\tdata->ipa_->stop();\n> +\t\t\tfreeBuffers(camera);\n> +\n> +\t\t\tLOG(RkISP1, Error)\n> +\t\t\t\t<< \"Failed to start main path \" << camera->id();\n> +\t\t\treturn ret;\n> +\t\t}\n> +\n> +\t\tstreamConfig[0] = {\n> +\t\t\t.pixelFormat = data->mainPathStream_.configuration().pixelFormat,\n> +\t\t\t.size = data->mainPathStream_.configuration().size,\n> +\t\t};\n> +\t}\n> +\n> +\tif (data->selfPathActive_) {\n> +\t\tret = selfPathVideo_->streamOn();\n> +\t\tif (ret) {\n> +\t\t\tif (data->mainPathActive_)\n> +\t\t\t\tmainPathVideo_->streamOff();\n> +\n\nThis should stop mainPathVideo_ if it has been activated. The error\nhandling is really becoming nasty :-S Any chance it could be improved on\ntop ? Goto's are probably not a great option though as they would jump\nover variable declarations. One optio would be to add an error handler\nat the beginning of the function:\n\n\tclass ErrorHandler {\n\tpublic:\n\t\tErrorHandler()\n\t\t\t: disabled_(false)\n\t\t{\n\t\t}\n\n\t\t~ErrorHandler()\n\t\t{\n\t\t\tif (disabled_)\n\t\t\t\treturn;\n\n\t\t\tparam_->streamOff();\n\t\t\tstat_->streamOff();\n\t\t\tdata->ipa_->stop();\n\t\t\tfreeBuffers();\n\t\t}\n\n\t\tvoid disable() { disabled_ = true; }\n\n\tprivate:\n\t\tbool disabled_;\n\t};\n\n\tclass ErrorHandler eh;\n\nand call\n\n\teh.disable();\n\nat the very end, before returning (names to be bikeshedded). We could\neven make it a generic class that takes a lambda function as argument,\nand calls it in the destructor. Usage here would become\n\n\tErrorHandler eh([&]() {\n\t\tparam_->streamOff();\n\t\tstat_->streamOff();\n\t\tdata->ipa_->stop();\n\t\tfreeBuffers();\n\t});\n\nwhich doesn't look too bad I think.\n\nThis would require making the V4L2VideoDevice::streamOff(),\nIPAProxy::stop() and PipelineHandlerRkISP1::freeBuffer() functions safe\nto be called when already off, stopped of freed.\n\n> +\t\t\tparam_->streamOff();\n> +\t\t\tstat_->streamOff();\n> +\t\t\tdata->ipa_->stop();\n> +\t\t\tfreeBuffers(camera);\n> +\n> +\t\t\tLOG(RkISP1, Error)\n> +\t\t\t\t<< \"Failed to start self path \" << camera->id();\n> +\t\t\treturn ret;\n> +\t\t}\n> +\n> +\t\tstreamConfig[1] = {\n> +\t\t\t.pixelFormat = data->selfPathStream_.configuration().pixelFormat,\n> +\t\t\t.size = data->selfPathStream_.configuration().size,\n> +\t\t};\n>  \t}\n>  \n>  \tactiveCamera_ = camera;\n> @@ -855,12 +931,6 @@ int PipelineHandlerRkISP1::start(Camera *camera)\n>  \t\tret = 0;\n>  \t}\n>  \n> -\tstd::map<unsigned int, IPAStream> streamConfig;\n> -\tstreamConfig[0] = {\n> -\t\t.pixelFormat = data->mainPathStream_.configuration().pixelFormat,\n> -\t\t.size = data->mainPathStream_.configuration().size,\n> -\t};\n> -\n>  \tstd::map<unsigned int, const ControlInfoMap &> entityControls;\n>  \tentityControls.emplace(0, data->sensor_->controls());\n>  \n> @@ -876,10 +946,19 @@ void PipelineHandlerRkISP1::stop(Camera *camera)\n>  \tRkISP1CameraData *data = cameraData(camera);\n>  \tint ret;\n>  \n> -\tret = mainPathVideo_->streamOff();\n> -\tif (ret)\n> -\t\tLOG(RkISP1, Warning)\n> -\t\t\t<< \"Failed to stop camera \" << camera->id();\n> +\tif (data->selfPathActive_) {\n> +\t\tret = selfPathVideo_->streamOff();\n> +\t\tif (ret)\n> +\t\t\tLOG(RkISP1, Warning)\n> +\t\t\t\t<< \"Failed to stop self path \" << camera->id();\n\nMaybe s/path/path for/ ? Or \"Failed to stop self path (camera ...)\" ?\n\n> +\t}\n> +\n> +\tif (data->mainPathActive_) {\n> +\t\tret = mainPathVideo_->streamOff();\n> +\t\tif (ret)\n> +\t\t\tLOG(RkISP1, Warning)\n> +\t\t\t\t<< \"Failed to stop main path \" << camera->id();\n> +\t}\n>  \n>  \tret = stat_->streamOff();\n>  \tif (ret)\n> @@ -964,6 +1043,9 @@ int PipelineHandlerRkISP1::initLinks(const Camera *camera,\n>  \t\tif (cfg.stream() == &data->mainPathStream_)\n>  \t\t\tlink = media_->link(\"rkisp1_isp\", 2,\n>  \t\t\t\t\t    \"rkisp1_resizer_mainpath\", 0);\n> +\t\telse if (cfg.stream() == &data->selfPathStream_)\n> +\t\t\tlink = media_->link(\"rkisp1_isp\", 2,\n> +\t\t\t\t\t    \"rkisp1_resizer_selfpath\", 0);\n\nThis renders my alternative proposal in a previous patch moot, but you\ncan then skip the nullptr initialization of link.\n\n>  \t\telse\n>  \t\t\treturn -EINVAL;\n>","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 439C6C3B5B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 15 Sep 2020 01:14:37 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id A153262DAB;\n\tTue, 15 Sep 2020 03:14:36 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id A3E1762901\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 15 Sep 2020 03:14:34 +0200 (CEST)","from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi\n\t[62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 216B9275;\n\tTue, 15 Sep 2020 03:14:34 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"s/9AQQ6e\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1600132474;\n\tbh=eCITFf9gbirQGPn/wlySahDsZxyu80QqpWYRBO4KkiM=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=s/9AQQ6e5/zcyU+dAOVThUUJpb/JbpmEIHX2UzTPwSkiE0vfxQ0g8x1T4aWUXlDn3\n\ty2UAEd+h/WohUyNGkxcNKJ9W4hbduvaoE/hEwfP10rU09mpg+im+ka0Qy986KMJaEM\n\taNuqF7YQktWiGPA4yZLv7bhOSCyNSHhJk010GQRg=","Date":"Tue, 15 Sep 2020 04:14:06 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Niklas =?utf-8?q?S=C3=B6derlund?= <niklas.soderlund@ragnatech.se>","Message-ID":"<20200915011406.GW15543@pendragon.ideasonboard.com>","References":"<20200914142149.63857-1-niklas.soderlund@ragnatech.se>\n\t<20200914142149.63857-11-niklas.soderlund@ragnatech.se>","MIME-Version":"1.0","Content-Disposition":"inline","In-Reply-To":"<20200914142149.63857-11-niklas.soderlund@ragnatech.se>","Subject":"Re: [libcamera-devel] [PATCH v2 10/13] libcamera: pipeline: rkisp1:\n\tConfigure self path","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>"}}]