[{"id":31544,"web_url":"https://patchwork.libcamera.org/comment/31544/","msgid":"<CAEB1ahuuGQP6MOAnMM_1UOi6KSOkYscOi3EOAPofBbzMrCrTOg@mail.gmail.com>","date":"2024-10-03T08:06:28","subject":"Re: [PATCH v14 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 and Jacopo,\n\nOn Mon, Sep 30, 2024 at 2:33 PM Harvey Yang <chenghaoyang@chromium.org> wrote:\n>\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> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\n> ---\n>  .../libcamera/internal/dma_buf_allocator.h    | 13 +++++\n>  src/libcamera/dma_buf_allocator.cpp           | 57 +++++++++++++++++++\n>  2 files changed, 70 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..f8ed3de2 100644\n> --- a/src/libcamera/dma_buf_allocator.cpp\n> +++ b/src/libcamera/dma_buf_allocator.cpp\n> @@ -22,6 +22,9 @@\n>\n>  #include <libcamera/base/log.h>\n>  #include <libcamera/base/memfd.h>\n> +#include <libcamera/base/shared_fd.h>\n> +\n> +#include <libcamera/framebuffer.h>\n>\n>  /**\n>   * \\file dma_buf_allocator.cpp\n> @@ -205,4 +208,58 @@ 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> + * Planes in a FrameBuffer are allocated with a single dma buf.\n> + * \\todo Add the option to allocate each plane with a dma buf respectively.\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> +               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\nMaybe it's a bit late:\nDo you think I should rename `frameSizes` as `planeSizes`,\nand `bufferSize` as `frameSize`?\n\nBR,\nHarvey\n\n> +\n> +       SharedFD fd(alloc(name.c_str(), bufferSize));\n> +       if (!fd.isValid())\n> +               return nullptr;\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.1.824.gd892dcdcdd-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 7CCF2C3257\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu,  3 Oct 2024 08:06:44 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 25AAF63527;\n\tThu,  3 Oct 2024 10:06:43 +0200 (CEST)","from mail-lj1-x230.google.com (mail-lj1-x230.google.com\n\t[IPv6:2a00:1450:4864:20::230])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 4E6B66351F\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu,  3 Oct 2024 10:06:41 +0200 (CEST)","by mail-lj1-x230.google.com with SMTP id\n\t38308e7fff4ca-2fad784e304so8370341fa.2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 03 Oct 2024 01:06:41 -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=\"lzK7iamS\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=chromium.org; s=google; t=1727942800; x=1728547600;\n\tdarn=lists.libcamera.org; \n\th=content-transfer-encoding:cc:to:subject:message-id:date:from\n\t:in-reply-to:references:mime-version:from:to:cc:subject:date\n\t:message-id:reply-to;\n\tbh=vJm2GZfM3NB0xWx1H9VMv8v5o43NmGRUJz7yQp/5Fhc=;\n\tb=lzK7iamS4xn26dQJluheHcKDCWFiM1zAkE8hZ1SiVmwfIqgPDGyBqDkyr62YmoDQHN\n\tPi898xlg5AAWaE5HAYSXxzJPlTODYnM7Dtazqg+9V24eIAkp0ZqPbqxJZnxXoiB1MWJN\n\tdvbqP3VArMEf0dJNOZLVTBP3/aPKJMCdnRh4I=","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20230601; t=1727942800; x=1728547600;\n\th=content-transfer-encoding:cc:to:subject:message-id:date:from\n\t:in-reply-to:references:mime-version:x-gm-message-state:from:to:cc\n\t:subject:date:message-id:reply-to;\n\tbh=vJm2GZfM3NB0xWx1H9VMv8v5o43NmGRUJz7yQp/5Fhc=;\n\tb=TVRSQrC8x43jWbrssIpIaf8Dd51XpW1Vfvfd0raKGWE9h5Es01YePrKZg63K28wMex\n\t4E6S/Zj5i7iQ0EYj0gXIZsYRAPJU+dWVyMWX2lbJ2r6aYojLMCsGOGlYpX5Vby6WliV1\n\t+8YXe4lDnkFuQiTP3VVm3Cp9ueDWpaK0giRcEEEZl1IHvBNq8VKpaeylkASbAmYQsldN\n\t09HMlaVlkyRC5KAPn1QeoVH7nzcoC/0XpfZHwxXUpamDkHMnlCeynqVMpTvNoNMlaObt\n\t77qCf2ylXZxwhHtBgc4lKHoprrzchMpyXUQnXsvLKrzDDSMAr0U+xojO4t+ce/L2Kpqe\n\tMYdQ==","X-Gm-Message-State":"AOJu0YxSRW7Y879s08XU+vXRrRxST8cBH2ttM6+y24eVasl91tjKRyVH\n\t0bC8Eb0kgvN+Y/om+uZLfDYYv6Es9dbzqzNC0IxMrCwaaWo4rMXFXvPmsc12SDLIPEM3dlg5w/a\n\t5uzTGrquHjilM+/yXdW/qgyWfa4Yt+WZMKs4YWNGQT3ziMsc=","X-Google-Smtp-Source":"AGHT+IGqXws4AVAOQby+tyQwPN5zMHM93k2S7n2sTYPFQWGFLeLstd0AJPrilLIB/iwYhrASSliYbDDDPvr+8tX/bAI=","X-Received":"by 2002:a2e:be83:0:b0:2fa:cc12:67de with SMTP id\n\t38308e7fff4ca-2fae107dc81mr37346731fa.32.1727942799788;\n\tThu, 03 Oct 2024 01:06:39 -0700 (PDT)","MIME-Version":"1.0","References":"<20240930063342.3014837-1-chenghaoyang@google.com>\n\t<20240930063342.3014837-2-chenghaoyang@google.com>","In-Reply-To":"<20240930063342.3014837-2-chenghaoyang@google.com>","From":"Cheng-Hao Yang <chenghaoyang@chromium.org>","Date":"Thu, 3 Oct 2024 16:06:28 +0800","Message-ID":"<CAEB1ahuuGQP6MOAnMM_1UOi6KSOkYscOi3EOAPofBbzMrCrTOg@mail.gmail.com>","Subject":"Re: [PATCH v14 1/7] libcamera: add DmaBufAllocator::exportBuffers()","To":"libcamera-devel@lists.libcamera.org, \n\tKieran Bingham <kieran.bingham@ideasonboard.com>, \n\tJacopo Mondi <jacopo.mondi@ideasonboard.com>","Cc":"Harvey Yang <chenghaoyang@google.com>","Content-Type":"text/plain; charset=\"UTF-8\"","Content-Transfer-Encoding":"quoted-printable","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":31545,"web_url":"https://patchwork.libcamera.org/comment/31545/","msgid":"<172794373569.1619946.13242011887045606838@ping.linuxembedded.co.uk>","date":"2024-10-03T08:22:15","subject":"Re: [PATCH v14 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 Cheng-Hao Yang (2024-10-03 09:06:28)\n> Hi Kieran and Jacopo,\n> \n> On Mon, Sep 30, 2024 at 2:33 PM Harvey Yang <chenghaoyang@chromium.org> wrote:\n> >\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> > Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> > Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\n> > ---\n> >  .../libcamera/internal/dma_buf_allocator.h    | 13 +++++\n> >  src/libcamera/dma_buf_allocator.cpp           | 57 +++++++++++++++++++\n> >  2 files changed, 70 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..f8ed3de2 100644\n> > --- a/src/libcamera/dma_buf_allocator.cpp\n> > +++ b/src/libcamera/dma_buf_allocator.cpp\n> > @@ -22,6 +22,9 @@\n> >\n> >  #include <libcamera/base/log.h>\n> >  #include <libcamera/base/memfd.h>\n> > +#include <libcamera/base/shared_fd.h>\n> > +\n> > +#include <libcamera/framebuffer.h>\n> >\n> >  /**\n> >   * \\file dma_buf_allocator.cpp\n> > @@ -205,4 +208,58 @@ 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> > + * Planes in a FrameBuffer are allocated with a single dma buf.\n> > + * \\todo Add the option to allocate each plane with a dma buf respectively.\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> > +               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> Maybe it's a bit late:\n> Do you think I should rename `frameSizes` as `planeSizes`,\n> and `bufferSize` as `frameSize`?\n\nplane sizes would probably be better, but I don't object either way.\n\nI've just checked CI - and I see the unit tests are passing again, so\nI'll see if I can find some time to re-test this later tonight if no one\nelse beats me to it.\n\nThis small rename could be handled when applying if this series is close\nto merge.\n\n> \n> BR,\n> Harvey\n> \n> > +\n> > +       SharedFD fd(alloc(name.c_str(), bufferSize));\n> > +       if (!fd.isValid())\n> > +               return nullptr;\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.1.824.gd892dcdcdd-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 7B1CAC3257\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu,  3 Oct 2024 08:22:22 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 5E8E263525;\n\tThu,  3 Oct 2024 10:22:21 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 8507A62C93\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu,  3 Oct 2024 10:22:19 +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 4FA7A4C7;\n\tThu,  3 Oct 2024 10:20:46 +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=\"YPgd/S7l\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1727943646;\n\tbh=giXu5nJoUiCSxmvjloCKkB9cbRny0pExiySCZJAuQaA=;\n\th=In-Reply-To:References:Subject:From:Cc:To:Date:From;\n\tb=YPgd/S7l0y3gV1x7WhP9V0sBDsc7GYNc3xZg+tYLcLMbJ1ZddedWW933YnWHz1XHU\n\tuQFjeE/L98M/mZcgM0oMJ319DpGOUUisNUkyPO1Ltq0wA34J5EWScNhYPjvex75Y7T\n\tea1vZYv6Qh3SiLhMakZ7ruP4cTM0hTNnZhE9jq7E=","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<CAEB1ahuuGQP6MOAnMM_1UOi6KSOkYscOi3EOAPofBbzMrCrTOg@mail.gmail.com>","References":"<20240930063342.3014837-1-chenghaoyang@google.com>\n\t<20240930063342.3014837-2-chenghaoyang@google.com>\n\t<CAEB1ahuuGQP6MOAnMM_1UOi6KSOkYscOi3EOAPofBbzMrCrTOg@mail.gmail.com>","Subject":"Re: [PATCH v14 1/7] libcamera: add DmaBufAllocator::exportBuffers()","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Cc":"Harvey Yang <chenghaoyang@google.com>","To":"Cheng-Hao Yang <chenghaoyang@chromium.org>,\n\tJacopo Mondi <jacopo.mondi@ideasonboard.com>,\n\tlibcamera-devel@lists.libcamera.org","Date":"Thu, 03 Oct 2024 09:22:15 +0100","Message-ID":"<172794373569.1619946.13242011887045606838@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>"}}]