[{"id":31128,"web_url":"https://patchwork.libcamera.org/comment/31128/","msgid":"<172588817726.2319503.4478256900209432750@ping.linuxembedded.co.uk>","date":"2024-09-09T13:22:57","subject":"Re: [PATCH v11 1/7] libcamera: add DmaBufAllocator::exportBuffers()","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"Quoting Harvey Yang (2024-09-07 15:28:26)\n> Add a helper function exportBuffers in DmaBufAllocator to make it easier\n> to use.\n> \n> It'll be used in Virtual Pipeline Handler and SoftwareIsp.\n> \n> Signed-off-by: Harvey Yang <chenghaoyang@chromium.org>\n> ---\n>  .../libcamera/internal/dma_buf_allocator.h    | 13 +++++\n>  src/libcamera/dma_buf_allocator.cpp           | 55 +++++++++++++++++++\n>  2 files changed, 68 insertions(+)\n> \n> diff --git a/include/libcamera/internal/dma_buf_allocator.h b/include/libcamera/internal/dma_buf_allocator.h\n> index d2a0a0d1..66d3b419 100644\n> --- a/include/libcamera/internal/dma_buf_allocator.h\n> +++ b/include/libcamera/internal/dma_buf_allocator.h\n> @@ -7,11 +7,17 @@\n>  \n>  #pragma once\n>  \n> +#include <memory>\n> +#include <string>\n> +#include <vector>\n> +\n>  #include <libcamera/base/flags.h>\n>  #include <libcamera/base/unique_fd.h>\n>  \n>  namespace libcamera {\n>  \n> +class FrameBuffer;\n> +\n>  class DmaBufAllocator\n>  {\n>  public:\n> @@ -28,7 +34,14 @@ public:\n>         bool isValid() const { return providerHandle_.isValid(); }\n>         UniqueFD alloc(const char *name, std::size_t size);\n>  \n> +       int exportBuffers(unsigned int count,\n> +                         const std::vector<unsigned int> &frameSize,\n> +                         std::vector<std::unique_ptr<FrameBuffer>> *buffers);\n> +\n>  private:\n> +       std::unique_ptr<FrameBuffer> createBuffer(\n> +               std::string name, const std::vector<unsigned int> &frameSizes);\n> +\n>         UniqueFD allocFromHeap(const char *name, std::size_t size);\n>         UniqueFD allocFromUDmaBuf(const char *name, std::size_t size);\n>         UniqueFD providerHandle_;\n> diff --git a/src/libcamera/dma_buf_allocator.cpp b/src/libcamera/dma_buf_allocator.cpp\n> index be6efb89..d2ac175f 100644\n> --- a/src/libcamera/dma_buf_allocator.cpp\n> +++ b/src/libcamera/dma_buf_allocator.cpp\n> @@ -23,6 +23,10 @@\n>  #include <libcamera/base/log.h>\n>  #include <libcamera/base/memfd.h>\n>  \n> +#include <libcamera/framebuffer.h>\n> +\n> +#include \"libcamera/internal/formats.h\"\n> +\n>  /**\n>   * \\file dma_buf_allocator.cpp\n>   * \\brief dma-buf allocator\n> @@ -205,4 +209,55 @@ UniqueFD DmaBufAllocator::alloc(const char *name, std::size_t size)\n>                 return allocFromHeap(name, size);\n>  }\n>  \n> +/**\n> + * \\brief Allocate and export buffers from the DmaBufAllocator\n> + * \\param[in] count The number of requested FrameBuffers\n> + * \\param[in] frameSizes The sizes of planes in each FrameBuffer\n> + * \\param[out] buffers Array of buffers successfully allocated\n> + *\n> + * \\return The number of allocated buffers on success or a negative error code\n> + * otherwise\n> + */\n> +int DmaBufAllocator::exportBuffers(unsigned int count,\n> +                                  const std::vector<unsigned int> &frameSizes,\n> +                                  std::vector<std::unique_ptr<FrameBuffer>> *buffers)\n> +{\n> +       for (unsigned int i = 0; i < count; ++i) {\n> +               std::unique_ptr<FrameBuffer> buffer =\n> +                       createBuffer(\"frame-\" + std::to_string(i), frameSizes);\n\nI wonder if we should find a better name for the buffer names ... but\nthis is what already happens in softisp anyway so I'm fine with this.\n\n\n\n> +               if (!buffer) {\n> +                       LOG(DmaBufAllocator, Error) << \"Unable to create buffer\";\n> +\n> +                       buffers->clear();\n> +                       return -EINVAL;\n> +               }\n> +\n> +               buffers->push_back(std::move(buffer));\n> +       }\n> +\n> +       return count;\n> +}\n> +\n> +std::unique_ptr<FrameBuffer>\n> +DmaBufAllocator::createBuffer(std::string name,\n> +                             const std::vector<unsigned int> &frameSizes)\n> +{\n> +       std::vector<FrameBuffer::Plane> planes;\n> +\n> +       unsigned int bufferSize = 0, offset = 0;\n> +       for (auto frameSize : frameSizes)\n> +               bufferSize += frameSize;\n> +\n> +       SharedFD fd(alloc(name.c_str(), bufferSize));\n> +       if (!fd.isValid())\n> +               return nullptr;\n\nSo this creates the allocation with a single dma buf ... I'm weary if\nthis will work generically - but I also think we don't need to worry\nabout this now.\n\nSome allocations might want each of the planes to be separated or have\nspecific alignments...\n\nBut without a user requiring those - I think this is fine for a first\nstep. I wonder if this should be documented somewhere, probably in the\nexportBuffers description.\n\nWith that\n\n\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\n> +\n> +       for (auto frameSize : frameSizes) {\n> +               planes.emplace_back(FrameBuffer::Plane{ fd, offset, frameSize });\n> +               offset += frameSize;\n> +       }\n> +\n> +       return std::make_unique<FrameBuffer>(planes);\n> +}\n> +\n>  } /* namespace libcamera */\n> -- \n> 2.46.0.469.g59c65b2a67-goog\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 9475BC324C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon,  9 Sep 2024 13:23:02 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 456AC634EE;\n\tMon,  9 Sep 2024 15:23:02 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 73FC1634EE\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon,  9 Sep 2024 15:23:00 +0200 (CEST)","from pendragon.ideasonboard.com\n\t(cpc89244-aztw30-2-0-cust6594.18-1.cable.virginm.net [86.31.185.195])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id B08BD3DA;\n\tMon,  9 Sep 2024 15:21:43 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"uHBJfYqN\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1725888103;\n\tbh=Nw6Pdn+w5fTCqTctS5MRMnQDBotc8OE94hZFgu9QVBE=;\n\th=In-Reply-To:References:Subject:From:Cc:To:Date:From;\n\tb=uHBJfYqNscJxOfCkb2LJftuh0FjiLk2y2NTrPK4iTqepCRGVug44LHayFb11lHC5Z\n\tO6Em0k5bLhqCPKlbz9uynVo4ljx/vG/IcATMNWwGkq0a8qAfhDWC4XTw0AB8Yt3a4Q\n\t9+guZxGhyUyXqfhJpBPtxEuvbdzYrJ057bR8Ergk=","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20240907143110.2210711-2-chenghaoyang@google.com>","References":"<20240907143110.2210711-1-chenghaoyang@google.com>\n\t<20240907143110.2210711-2-chenghaoyang@google.com>","Subject":"Re: [PATCH v11 1/7] libcamera: add DmaBufAllocator::exportBuffers()","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Cc":"Harvey Yang <chenghaoyang@google.com>,\n\tHarvey Yang <chenghaoyang@chromium.org>","To":"Harvey Yang <chenghaoyang@chromium.org>,\n\tlibcamera-devel@lists.libcamera.org","Date":"Mon, 09 Sep 2024 14:22:57 +0100","Message-ID":"<172588817726.2319503.4478256900209432750@ping.linuxembedded.co.uk>","User-Agent":"alot/0.10","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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":31151,"web_url":"https://patchwork.libcamera.org/comment/31151/","msgid":"<CAEB1ahs2fkrgSn0iDz4QbghsykE_5rfVPhutQGbjLJf3Z9DhsA@mail.gmail.com>","date":"2024-09-10T04:50:43","subject":"Re: [PATCH v11 1/7] libcamera: add DmaBufAllocator::exportBuffers()","submitter":{"id":117,"url":"https://patchwork.libcamera.org/api/people/117/","name":"Cheng-Hao Yang","email":"chenghaoyang@chromium.org"},"content":"Hi Kieran,\n\nOn Mon, Sep 9, 2024 at 9:23 PM Kieran Bingham <\nkieran.bingham@ideasonboard.com> wrote:\n\n> Quoting Harvey Yang (2024-09-07 15:28:26)\n> > Add a helper function exportBuffers in DmaBufAllocator to make it easier\n> > to use.\n> >\n> > It'll be used in Virtual Pipeline Handler and SoftwareIsp.\n> >\n> > Signed-off-by: Harvey Yang <chenghaoyang@chromium.org>\n> > ---\n> >  .../libcamera/internal/dma_buf_allocator.h    | 13 +++++\n> >  src/libcamera/dma_buf_allocator.cpp           | 55 +++++++++++++++++++\n> >  2 files changed, 68 insertions(+)\n> >\n> > diff --git a/include/libcamera/internal/dma_buf_allocator.h\n> b/include/libcamera/internal/dma_buf_allocator.h\n> > index d2a0a0d1..66d3b419 100644\n> > --- a/include/libcamera/internal/dma_buf_allocator.h\n> > +++ b/include/libcamera/internal/dma_buf_allocator.h\n> > @@ -7,11 +7,17 @@\n> >\n> >  #pragma once\n> >\n> > +#include <memory>\n> > +#include <string>\n> > +#include <vector>\n> > +\n> >  #include <libcamera/base/flags.h>\n> >  #include <libcamera/base/unique_fd.h>\n> >\n> >  namespace libcamera {\n> >\n> > +class FrameBuffer;\n> > +\n> >  class DmaBufAllocator\n> >  {\n> >  public:\n> > @@ -28,7 +34,14 @@ public:\n> >         bool isValid() const { return providerHandle_.isValid(); }\n> >         UniqueFD alloc(const char *name, std::size_t size);\n> >\n> > +       int exportBuffers(unsigned int count,\n> > +                         const std::vector<unsigned int> &frameSize,\n> > +                         std::vector<std::unique_ptr<FrameBuffer>>\n> *buffers);\n> > +\n> >  private:\n> > +       std::unique_ptr<FrameBuffer> createBuffer(\n> > +               std::string name, const std::vector<unsigned int>\n> &frameSizes);\n> > +\n> >         UniqueFD allocFromHeap(const char *name, std::size_t size);\n> >         UniqueFD allocFromUDmaBuf(const char *name, std::size_t size);\n> >         UniqueFD providerHandle_;\n> > diff --git a/src/libcamera/dma_buf_allocator.cpp\n> b/src/libcamera/dma_buf_allocator.cpp\n> > index be6efb89..d2ac175f 100644\n> > --- a/src/libcamera/dma_buf_allocator.cpp\n> > +++ b/src/libcamera/dma_buf_allocator.cpp\n> > @@ -23,6 +23,10 @@\n> >  #include <libcamera/base/log.h>\n> >  #include <libcamera/base/memfd.h>\n> >\n> > +#include <libcamera/framebuffer.h>\n> > +\n> > +#include \"libcamera/internal/formats.h\"\n> > +\n> >  /**\n> >   * \\file dma_buf_allocator.cpp\n> >   * \\brief dma-buf allocator\n> > @@ -205,4 +209,55 @@ UniqueFD DmaBufAllocator::alloc(const char *name,\n> std::size_t size)\n> >                 return allocFromHeap(name, size);\n> >  }\n> >\n> > +/**\n> > + * \\brief Allocate and export buffers from the DmaBufAllocator\n> > + * \\param[in] count The number of requested FrameBuffers\n> > + * \\param[in] frameSizes The sizes of planes in each FrameBuffer\n> > + * \\param[out] buffers Array of buffers successfully allocated\n> > + *\n> > + * \\return The number of allocated buffers on success or a negative\n> error code\n> > + * otherwise\n> > + */\n> > +int DmaBufAllocator::exportBuffers(unsigned int count,\n> > +                                  const std::vector<unsigned int>\n> &frameSizes,\n> > +\n> std::vector<std::unique_ptr<FrameBuffer>> *buffers)\n> > +{\n> > +       for (unsigned int i = 0; i < count; ++i) {\n> > +               std::unique_ptr<FrameBuffer> buffer =\n> > +                       createBuffer(\"frame-\" + std::to_string(i),\n> frameSizes);\n>\n> I wonder if we should find a better name for the buffer names ... but\n> this is what already happens in softisp anyway so I'm fine with this.\n>\n>\nI'd be glad to accept suggestions :)\n\n\n>\n>\n> > +               if (!buffer) {\n> > +                       LOG(DmaBufAllocator, Error) << \"Unable to create\n> buffer\";\n> > +\n> > +                       buffers->clear();\n> > +                       return -EINVAL;\n> > +               }\n> > +\n> > +               buffers->push_back(std::move(buffer));\n> > +       }\n> > +\n> > +       return count;\n> > +}\n> > +\n> > +std::unique_ptr<FrameBuffer>\n> > +DmaBufAllocator::createBuffer(std::string name,\n> > +                             const std::vector<unsigned int>\n> &frameSizes)\n> > +{\n> > +       std::vector<FrameBuffer::Plane> planes;\n> > +\n> > +       unsigned int bufferSize = 0, offset = 0;\n> > +       for (auto frameSize : frameSizes)\n> > +               bufferSize += frameSize;\n> > +\n> > +       SharedFD fd(alloc(name.c_str(), bufferSize));\n> > +       if (!fd.isValid())\n> > +               return nullptr;\n>\n> So this creates the allocation with a single dma buf ... I'm weary if\n> this will work generically - but I also think we don't need to worry\n> about this now.\n>\n> Some allocations might want each of the planes to be separated or have\n> specific alignments...\n>\n> But without a user requiring those - I think this is fine for a first\n> step. I wonder if this should be documented somewhere, probably in the\n> exportBuffers description.\n>\n\nTrue, some of the previous versions are actually allocating buffers with\ntheir own dma buf allocation respectively, while I thought that using a\nsingle\ndma buf is generally more preferred (Please let me know if I'm wrong :p).\n\nAdded a description and a todo in `exportBuffers`.\n\n\n>\n> With that\n>\n>\n> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n>\n> > +\n> > +       for (auto frameSize : frameSizes) {\n> > +               planes.emplace_back(FrameBuffer::Plane{ fd, offset,\n> frameSize });\n> > +               offset += frameSize;\n> > +       }\n> > +\n> > +       return std::make_unique<FrameBuffer>(planes);\n> > +}\n> > +\n> >  } /* namespace libcamera */\n> > --\n> > 2.46.0.469.g59c65b2a67-goog\n> >\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 1E57FC324C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 10 Sep 2024 04:50:58 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id C68AF618F2;\n\tTue, 10 Sep 2024 06:50:57 +0200 (CEST)","from mail-lj1-x231.google.com (mail-lj1-x231.google.com\n\t[IPv6:2a00:1450:4864:20::231])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 834B4618F2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 10 Sep 2024 06:50:55 +0200 (CEST)","by mail-lj1-x231.google.com with SMTP id\n\t38308e7fff4ca-2f762de00fbso2842731fa.2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 09 Sep 2024 21:50:55 -0700 (PDT)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=chromium.org header.i=@chromium.org\n\theader.b=\"Bl4uF9go\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=chromium.org; s=google; t=1725943855; x=1726548655;\n\tdarn=lists.libcamera.org; \n\th=cc:to:subject:message-id:date:from:in-reply-to:references\n\t:mime-version:from:to:cc:subject:date:message-id:reply-to;\n\tbh=Fx9kI+eoteRsvEb44fPANjJgYFMSFHgpUAIogdHGSys=;\n\tb=Bl4uF9goZORW9g70/XRGv0QspTxOAkkBvjGX/wzNKKSWDyrH7ZOXhMeRkbCUgow9+M\n\tmgmHKaovrETtaOKCYYgW1QM3whSV5MgzNTtdx2OPzAx6Qm1S7g/PeTFJGChrxeLlrohN\n\tHTZEuF0yDgqNieCiBGlClOwcqVEgjqHMdZ7Tk=","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20230601; t=1725943855; x=1726548655;\n\th=cc:to:subject:message-id:date:from:in-reply-to:references\n\t:mime-version:x-gm-message-state:from:to:cc:subject:date:message-id\n\t:reply-to;\n\tbh=Fx9kI+eoteRsvEb44fPANjJgYFMSFHgpUAIogdHGSys=;\n\tb=MQVNxreHIxADPmiP2T6IgaaVlfKUwjMDG6oqnt36pSCZLiEIm7NYemVKce3i5Zw+mS\n\tHOMkbS9DFVP1G6gCaC22CEL7BZUwOdmKPKy0ZepWn4o59uW4OEIbUq9q/own51uxSwgj\n\t9I566IwaU6DxcZzOooNUgw24CmqimwlkDis/aujFq9gkyJc+YbX+4TdMTgYUaELV0MsG\n\tl3/kblznC0p/KmviAVEmaHfLe/+x7wtwfMKRkWCgr9tkXAZl92CN0llBU+wmgq18ebvy\n\tZhgw9HkQC72VD1Rt9g6wU/V7kbpjsp622dJy3k6lPUMHKDtmzUtioXqUYulS2UVLHXgn\n\ty4aQ==","X-Gm-Message-State":"AOJu0Yx2B2rHQ8Iq228HIYAJtP59MmafSuma+ct7XGuqcbdWRa6jhUgl\n\tUwpDJcOMUvSyH2IRplUReNxZoMczdkZvNaJQsyy4TtHM+agB1HNPjJM7bfgSxciKHiqonsVobqJ\n\tSZW8u6WtYVvDWWqxHix/3iGwFMDJ+XGUJKqnNX/Eev3F+2lk=","X-Google-Smtp-Source":"AGHT+IGkpXOcgM6RxFG15oJJg8vl3nbgJMoFsnXc356c9/uFBhb3bxWUpiI45AMEsPxgWsxH4ajs+3h+ISsBpDT+8hA=","X-Received":"by 2002:a05:651c:550:b0:2f6:649e:bf5c with SMTP id\n\t38308e7fff4ca-2f751ed4456mr108324151fa.17.1725943854681;\n\tMon, 09 Sep 2024 21:50:54 -0700 (PDT)","MIME-Version":"1.0","References":"<20240907143110.2210711-1-chenghaoyang@google.com>\n\t<20240907143110.2210711-2-chenghaoyang@google.com>\n\t<172588817726.2319503.4478256900209432750@ping.linuxembedded.co.uk>","In-Reply-To":"<172588817726.2319503.4478256900209432750@ping.linuxembedded.co.uk>","From":"Cheng-Hao Yang <chenghaoyang@chromium.org>","Date":"Tue, 10 Sep 2024 12:50:43 +0800","Message-ID":"<CAEB1ahs2fkrgSn0iDz4QbghsykE_5rfVPhutQGbjLJf3Z9DhsA@mail.gmail.com>","Subject":"Re: [PATCH v11 1/7] libcamera: add DmaBufAllocator::exportBuffers()","To":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org,\n\tHarvey Yang <chenghaoyang@google.com>","Content-Type":"multipart/alternative; boundary=\"0000000000005c65ea0621bc9ec6\"","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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]