[{"id":1181,"web_url":"https://patchwork.libcamera.org/comment/1181/","msgid":"<20190402102438.GJ4805@pendragon.ideasonboard.com>","date":"2019-04-02T10:24:38","subject":"Re: [libcamera-devel] [PATCH v5 13/19] libcamera: ipu3: Implement\n\tmemory handling","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Jacopo,\n\nThank you for the patch.\n\nOn Tue, Mar 26, 2019 at 09:38:56AM +0100, Jacopo Mondi wrote:\n> Implement buffer allocation and relase in IPU3 pipeline handlers.\n\ns/relase/release/\n\n> As the pipeline handler supports a single stream, provide two internal\n> buffer pools for 'viewfinder' and 'stat' video devices, and export\n> the 'output' video device buffers to the Stream's pool.\n\nI would add \"This works around the fact that the ImgU requires buffers\nto be queued on all its outputs, even when they are not in use.\"\n\n> Share buffers between the CIO2 output and the ImgU input video devices,\n> as the output of the former should immediately be provided to the\n> latter for further processing.\n> \n> Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>\n> ---\n>  src/libcamera/pipeline/ipu3/ipu3.cpp | 174 ++++++++++++++++++++++++---\n>  1 file changed, 160 insertions(+), 14 deletions(-)\n> \n> diff --git a/src/libcamera/pipeline/ipu3/ipu3.cpp b/src/libcamera/pipeline/ipu3/ipu3.cpp\n> index 63b84706b9b2..d3519bb1d536 100644\n> --- a/src/libcamera/pipeline/ipu3/ipu3.cpp\n> +++ b/src/libcamera/pipeline/ipu3/ipu3.cpp\n> @@ -64,6 +64,7 @@ public:\n>  \t\tV4L2Device *dev;\n>  \t\tunsigned int pad;\n>  \t\tstd::string name;\n> +\t\tBufferPool *pool;\n>  \t};\n>  \n>  \tImgUDevice()\n> @@ -94,6 +95,10 @@ public:\n>  \t{\n>  \t\treturn outputMap[id].name;\n>  \t}\n> +\tBufferPool *outputPool(enum OutputId id)\n> +\t{\n> +\t\treturn outputMap[id].pool;\n> +\t}\n>  \n>  \tint init(MediaDevice *media, unsigned int index);\n>  \tint configureInput(const StreamConfiguration &config,\n> @@ -101,6 +106,11 @@ public:\n>  \tint configureOutput(enum OutputId id,\n>  \t\t\t    const StreamConfiguration &config);\n>  \n> +\tint importBuffers(BufferPool *pool);\n> +\tint exportBuffers(enum OutputId id, BufferPool *pool);\n> +\tint exportBuffers(enum OutputId id, unsigned int count);\n> +\tvoid freeBuffers();\n> +\n>  \tunsigned int index_;\n>  \tstd::string name_;\n>  \tMediaDevice *media_;\n> @@ -114,11 +124,16 @@ public:\n>  \n>  \t/* ImgU output map: associate an output id with its descriptor. */\n>  \tstd::map<enum OutputId, struct outputDesc> outputMap;\n> +\n> +\tBufferPool vfPool;\n> +\tBufferPool statPool;\n>  };\n>  \n>  class CIO2Device\n>  {\n>  public:\n> +\tstatic constexpr unsigned int CIO2_BUFFER_COUNT = 4;\n> +\n>  \tCIO2Device()\n>  \t\t: output_(nullptr), csi2_(nullptr), sensor_(nullptr)\n>  \t{\n> @@ -136,12 +151,17 @@ public:\n>  \tint configure(const StreamConfiguration &config,\n>  \t\t      V4L2SubdeviceFormat *format);\n>  \n> +\tBufferPool *exportBuffers();\n> +\tvoid freeBuffers();\n> +\n>  \tV4L2Device *output_;\n>  \tV4L2Subdevice *csi2_;\n>  \tV4L2Subdevice *sensor_;\n>  \n>  \t/* Maximum sizes and the mbus code used to produce them. */\n>  \tstd::pair<unsigned int, SizeRange> maxSizes_;\n> +\n> +\tBufferPool pool_;\n>  };\n>  \n>  class PipelineHandlerIPU3 : public PipelineHandler\n> @@ -301,18 +321,39 @@ int PipelineHandlerIPU3::configureStreams(Camera *camera,\n>  \n>  int PipelineHandlerIPU3::allocateBuffers(Camera *camera, Stream *stream)\n>  {\n> -\tconst StreamConfiguration &cfg = stream->configuration();\n>  \tIPU3CameraData *data = cameraData(camera);\n> -\tV4L2Device *cio2 = data->cio2_.output_;\n> +\tCIO2Device *cio2 = &data->cio2_;\n> +\tImgUDevice *imgu = data->imgu_;\n> +\tint ret;\n>  \n> -\tif (!cfg.bufferCount)\n> -\t\treturn -EINVAL;\n> +\t/* Share buffers between CIO2 output and ImgU input. */\n> +\tBufferPool *pool = cio2->exportBuffers();\n> +\tif (!pool)\n> +\t\treturn -ENOMEM;\n>  \n> -\tint ret = cio2->exportBuffers(&stream->bufferPool());\n> -\tif (ret) {\n> -\t\tLOG(IPU3, Error) << \"Failed to request memory\";\n> +\tret = imgu->importBuffers(pool);\n> +\tif (ret)\n\nPlease print error an message in ImgUDevice::importBuffers or this error\npath will be silent.\n\n> +\t\treturn ret;\n> +\n> +\t/* Export ImgU output buffers to the stream's pool. */\n> +\tret = imgu->exportBuffers(ImgUDevice::MAIN_OUTPUT,\n> +\t\t\t\t  &stream->bufferPool());\n> +\tif (ret)\n> +\t\treturn ret;\n> +\n> +\t/*\n> +\t * Reserve memory in viewfinder and stat output devices. Use the\n> +\t * same number of buffers as the ones requested for the output\n> +\t * stream.\n> +\t */\n> +\tunsigned int bufferCount = stream->bufferPool().count();\n> +\tret = imgu->exportBuffers(ImgUDevice::SECONDARY_OUTPUT, bufferCount);\n> +\tif (ret)\n> +\t\treturn ret;\n> +\n> +\tret = imgu->exportBuffers(ImgUDevice::STAT, bufferCount);\n> +\tif (ret)\n>  \t\treturn ret;\n> -\t}\n>  \n>  \treturn 0;\n>  }\n> @@ -320,13 +361,9 @@ int PipelineHandlerIPU3::allocateBuffers(Camera *camera, Stream *stream)\n>  int PipelineHandlerIPU3::freeBuffers(Camera *camera, Stream *stream)\n>  {\n>  \tIPU3CameraData *data = cameraData(camera);\n> -\tV4L2Device *cio2 = data->cio2_.output_;\n>  \n> -\tint ret = cio2->releaseBuffers();\n> -\tif (ret) {\n> -\t\tLOG(IPU3, Error) << \"Failed to release memory\";\n> -\t\treturn ret;\n> -\t}\n> +\tdata->cio2_.freeBuffers();\n> +\tdata->imgu_->freeBuffers();\n>  \n>  \treturn 0;\n>  }\n> @@ -592,6 +629,7 @@ int ImgUDevice::init(MediaDevice *media, unsigned int index)\n>  \tdesc.dev = viewfinder_;\n>  \tdesc.pad = PAD_VF;\n>  \tdesc.name = \"viewfinder\";\n> +\tdesc.pool = &vfPool;\n>  \toutputMap[SECONDARY_OUTPUT] = desc;\n>  \n>  \tstat_ = V4L2Device::fromEntityName(media, name_ + \" 3a stat\");\n> @@ -603,6 +641,7 @@ int ImgUDevice::init(MediaDevice *media, unsigned int index)\n>  \tdesc.dev = stat_;\n>  \tdesc.pad = PAD_STAT;\n>  \tdesc.name = \"stat\";\n> +\tdesc.pool = &statPool;\n>  \toutputMap[STAT] = desc;\n>  \n>  \treturn 0;\n> @@ -714,6 +753,86 @@ int ImgUDevice::configureOutput(enum OutputId id,\n>  \treturn 0;\n>  }\n>  \n> +/**\n> + * \\brief Import buffers from CIO2 device into the ImgU\n> + * \\param[in] pool The buffer pool to import\n> + *\n> + * \\return 0 on success or a negative error code otherwise\n> + */\n> +int ImgUDevice::importBuffers(BufferPool *pool)\n> +{\n> +\treturn input_->importBuffers(pool);\n> +}\n> +\n> +/**\n> + * \\brief Export buffers from output \\a id to the provided \\a pool\n> + * \\param[in] id The ImgU output identifier\n> + * \\param[in] pool The buffer pool where to export buffers\n> + *\n> + * Export memory buffers reserved in the video device memory associated with\n> + * output \\a id to the buffer pool provided as argument.\n> + *\n> + * \\return 0 on success or a negative error code otherwise\n> + */\n> +int ImgUDevice::exportBuffers(enum OutputId id, BufferPool *pool)\n> +{\n> +\tV4L2Device *output = outputDevice(id);\n> +\tconst std::string &name = outputName(id);\n\nThe second variable could be inlined, the first probably as well.\n\n> +\n> +\tint ret = output->exportBuffers(pool);\n> +\tif (ret) {\n> +\t\tLOG(IPU3, Error) << \"Failed to reserve ImgU \"\n> +\t\t\t\t << name << \" buffers\";\n> +\t\treturn ret;\n> +\t}\n> +\n> +\treturn 0;\n> +}\n> +\n> +/**\n> + * \\brief Export buffers from output \\a id to the an internal buffer pool\n> + * \\param[in] id The ImgU output identifier\n> + * \\param[in] count The number of buffers to reserve\n> + *\n> + * Export memory buffers reserved in the video device memory associated with\n> + * output \\a id to the output internal buffer pool.\n> + *\n> + * \\return 0 on success or a negative error code otherwise\n> + */\n> +int ImgUDevice::exportBuffers(enum OutputId id, unsigned int count)\n> +{\n> +\tBufferPool *pool = outputPool(id);\n> +\n> +\t/* Internal buffer pools needs memory to be allocated first. */\n> +\tpool->createBuffers(count);\n> +\n> +\treturn exportBuffers(id, pool);\n> +}\n\nI don't like this function too much. I think the could would end up\nbeing easier to follow if you called createBuffers() in the caller, and\nalways used the first variant of exportBuffers().\n\n> +\n> +/**\n> + * \\brief Release buffers of the ImgU devices\n\n\"Release buffers for all the ImgU video devices\" ?\n\n> + */\n> +void ImgUDevice::freeBuffers()\n> +{\n> +\tint ret;\n> +\n> +\tret = output_->releaseBuffers();\n> +\tif (ret)\n> +\t\tLOG(IPU3, Error) << \"Failed to release ImgU output buffers\";\n> +\n> +\tret = stat_->releaseBuffers();\n> +\tif (ret)\n> +\t\tLOG(IPU3, Error) << \"Failed to release ImgU stat buffers\";\n> +\n> +\tret = viewfinder_->releaseBuffers();\n> +\tif (ret)\n> +\t\tLOG(IPU3, Error) << \"Failed to release ImgU viewfinder buffers\";\n> +\n> +\tret = input_->releaseBuffers();\n> +\tif (ret)\n> +\t\tLOG(IPU3, Error) << \"Failed to release ImgU input buffers\";\n> +}\n> +\n>  /*------------------------------------------------------------------------------\n>   * CIO2 Device\n>   */\n> @@ -880,6 +999,33 @@ int CIO2Device::configure(const StreamConfiguration &config,\n>  \treturn 0;\n>  }\n>  \n> +/**\n> + * \\brief Reserve CIO2 memory buffers and export them in a BufferPool\n\nMaybe s/Reserve/Allocate/ ?\n\n> + *\n> + * Allocate memory buffers in the CIO2 video device and export them to\n> + * a buffer pool that will be later imported by the ImgU device.\n\n\"that can then be imported by another device\" to keep the CIO2Device and\nImgUDevice self-contained ?\n\n> + *\n> + * \\return The buffer pool with export buffers on success or nullptr otherwise\n> + */\n> +BufferPool *CIO2Device::exportBuffers()\n> +{\n> +\tpool_.createBuffers(CIO2_BUFFER_COUNT);\n> +\n> +\tint ret = output_->exportBuffers(&pool_);\n> +\tif (ret) {\n> +\t\tLOG(IPU3, Error) << \"Failed to export CIO2 buffers\";\n> +\t\treturn nullptr;\n> +\t}\n> +\n> +\treturn &pool_;\n> +}\n> +\n> +void CIO2Device::freeBuffers()\n> +{\n> +\tif (output_->releaseBuffers())\n> +\t\tLOG(IPU3, Error) << \"Failed to release CIO2 buffers\";\n> +}\n> +\n>  REGISTER_PIPELINE_HANDLER(PipelineHandlerIPU3);\n>  \n>  } /* namespace libcamera */","headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 8C1AC600FB\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  2 Apr 2019 12:24:49 +0200 (CEST)","from pendragon.ideasonboard.com (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id E5E1A2F9;\n\tTue,  2 Apr 2019 12:24:48 +0200 (CEST)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1554200689;\n\tbh=oldBv4Mzvx+d7iivcbsHj40Ou2FWbwhBtX/paX0m7U0=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=KDHwGTVt9GNUPyGdiVUDcSmGJ3kBAR0pWYImyQgVI4V1dpsh9Jn7Z6t7/Oa8EIjQ5\n\tRAfiOBbeU7FyZMusIRybEbhTBOIaC0sLd2n96Qkbw4OJKaqqOZOHUNxKzPU840Kb5r\n\t5K3c7fDL1YJGZmx5LJZp5CCUyatr/58E18mFwVyY=","Date":"Tue, 2 Apr 2019 13:24:38 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Jacopo Mondi <jacopo@jmondi.org>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20190402102438.GJ4805@pendragon.ideasonboard.com>","References":"<20190326083902.26121-1-jacopo@jmondi.org>\n\t<20190326083902.26121-14-jacopo@jmondi.org>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20190326083902.26121-14-jacopo@jmondi.org>","User-Agent":"Mutt/1.10.1 (2018-07-13)","Subject":"Re: [libcamera-devel] [PATCH v5 13/19] libcamera: ipu3: Implement\n\tmemory handling","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.23","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>","X-List-Received-Date":"Tue, 02 Apr 2019 10:24:49 -0000"}}]