{"id":13503,"url":"https://patchwork.libcamera.org/api/1.1/patches/13503/?format=json","web_url":"https://patchwork.libcamera.org/patch/13503/","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":"<20210826112539.170694-3-hiroh@chromium.org>","date":"2021-08-26T11:25:32","name":"[libcamera-devel,v3,2/9] libcamera: mapped_framebuffer: Return plane begin address by MappedBuffer::maps()","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"98e23b11a547f7115d2454a28c28ff431262e33f","submitter":{"id":63,"url":"https://patchwork.libcamera.org/api/1.1/people/63/?format=json","name":"Hirokazu Honda","email":"hiroh@chromium.org"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/13503/mbox/","series":[{"id":2398,"url":"https://patchwork.libcamera.org/api/1.1/series/2398/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2398","date":"2021-08-26T11:25:30","name":"Add offset to FrameBuffer::Plane","version":3,"mbox":"https://patchwork.libcamera.org/series/2398/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/13503/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/13503/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 2E019BD87C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 26 Aug 2021 11:25:54 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id E8C4D688E5;\n\tThu, 26 Aug 2021 13:25:53 +0200 (CEST)","from mail-pg1-x536.google.com (mail-pg1-x536.google.com\n\t[IPv6:2607:f8b0:4864:20::536])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 007316888F\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 26 Aug 2021 13:25:51 +0200 (CEST)","by mail-pg1-x536.google.com with SMTP id s11so2767524pgr.11\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 26 Aug 2021 04:25:50 -0700 (PDT)","from hiroh2.tok.corp.google.com\n\t([2401:fa00:8f:203:a5bc:b3dd:7208:bec1])\n\tby smtp.gmail.com with ESMTPSA id\n\ts29sm3472057pgl.38.2021.08.26.04.25.47\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tThu, 26 Aug 2021 04:25:48 -0700 (PDT)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=chromium.org header.i=@chromium.org\n\theader.b=\"ilKQd1gz\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org;\n\ts=google; \n\th=from:to:cc:subject:date:message-id:in-reply-to:references\n\t:mime-version:content-transfer-encoding;\n\tbh=cIFc40HtWtIL1OXXk5WK2FVqnWkKAedNtYJqkR0Jv9o=;\n\tb=ilKQd1gzKKrqtL2RB0XR0+1LuddO+uRvU6Yi2Cg6ol34AMpW4VYzlSkZe4Xm7Ommas\n\t3xJruwhmBZdwxALFlVgQIPPiq7scVO7LYNu7i0zMDBRVoBkhiT6vKTE6F1ii6kqi1l5V\n\tLhWhWlmrui9V/8WWHxY6Nhv3Sw2fm/pErh2cY=","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=cIFc40HtWtIL1OXXk5WK2FVqnWkKAedNtYJqkR0Jv9o=;\n\tb=OMZnRlXK3T7L7UUbR1vH3Ia66GY2o7GufTAxpsB8JzOCKZlpx3aMNqc0/v3ZUlHB6S\n\t7jBJ6wTh1H4BkpVBMDbU1OlcsrpuHXi20DaeTaF9p/iVm9x3vpkw0nNTGO7vrpqPwFK0\n\tWD0mIKK4r62jPr4MtixMCGfLHiVgl11Bwba4KcT3mlicJNJX2l2dmSKcvEeL22Ppo5iZ\n\tMjwXYV253sp+Svvkm/SiB3JRPlP6rkqoESsuX7tOLJgADlC5ESn6t1YlsW/dEWqnuy2t\n\twyhB/nPWK19heKddLLFUp6zRBL50pZdEm/pv8bOcSls+tmfWT1ZaEyVGuBILaQF/UlVj\n\thvbQ==","X-Gm-Message-State":"AOAM5323YI/828tmg0CKKSG0vc3WxWAfEk8tmaEXEq6pz6uMCseUWyIh\n\tG/rkndAFVieW1w6Zwc86JQMQHGhBEJP5gQ==","X-Google-Smtp-Source":"ABdhPJwQBxs+c58Adv7oOLpc6yXiCMztbBLJCZBzymldjl3+aYTeGaXhDKZJqe0fE20ZGuj+qPb/ig==","X-Received":"by 2002:a62:58c2:0:b0:3eb:31ee:cfb9 with SMTP id\n\tm185-20020a6258c2000000b003eb31eecfb9mr3412374pfb.31.1629977149163; \n\tThu, 26 Aug 2021 04:25:49 -0700 (PDT)","From":"Hirokazu Honda <hiroh@chromium.org>","To":"libcamera-devel@lists.libcamera.org","Date":"Thu, 26 Aug 2021 20:25:32 +0900","Message-Id":"<20210826112539.170694-3-hiroh@chromium.org>","X-Mailer":"git-send-email 2.33.0.rc2.250.ged5fa647cd-goog","In-Reply-To":"<20210826112539.170694-1-hiroh@chromium.org>","References":"<20210826112539.170694-1-hiroh@chromium.org>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v3 2/9] libcamera: mapped_framebuffer:\n\tReturn plane begin address by MappedBuffer::maps()","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>"},"content":"MappedBuffer::maps() returns std::vector<MappedBuffer::Plane>.\nPlane has the address, but the address points the beginning of the\nbuffer containing the plane.\nThis makes the Plane point the beginning of the plane. So\nMappedBuffer::maps()[i].data() returns the address of i-th plane.\n\nSigned-off-by: Hirokazu Honda <hiroh@chromium.org>\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n .../libcamera/internal/mapped_framebuffer.h   |  4 +-\n src/android/mm/generic_camera_buffer.cpp      |  2 -\n src/libcamera/mapped_framebuffer.cpp          | 71 ++++++++++++++++---\n src/libcamera/v4l2_videodevice.cpp            |  6 ++\n 4 files changed, 70 insertions(+), 13 deletions(-)","diff":"diff --git a/include/libcamera/internal/mapped_framebuffer.h b/include/libcamera/internal/mapped_framebuffer.h\nindex 3401a9fc..42479541 100644\n--- a/include/libcamera/internal/mapped_framebuffer.h\n+++ b/include/libcamera/internal/mapped_framebuffer.h\n@@ -30,12 +30,14 @@ public:\n \n \tbool isValid() const { return error_ == 0; }\n \tint error() const { return error_; }\n-\tconst std::vector<Plane> &maps() const { return maps_; }\n+\t/* \\todo rename to planes(). */\n+\tconst std::vector<Plane> &maps() const { return planes_; }\n \n protected:\n \tMappedBuffer();\n \n \tint error_;\n+\tstd::vector<Plane> planes_;\n \tstd::vector<Plane> maps_;\n \n private:\ndiff --git a/src/android/mm/generic_camera_buffer.cpp b/src/android/mm/generic_camera_buffer.cpp\nindex d4f3f22b..299a5496 100644\n--- a/src/android/mm/generic_camera_buffer.cpp\n+++ b/src/android/mm/generic_camera_buffer.cpp\n@@ -53,8 +53,6 @@ private:\n \tint flags_;\n \toff_t bufferLength_;\n \tstd::vector<PlaneInfo> planeInfo_;\n-\t/* \\todo Remove planes_ when it will be added to MappedBuffer */\n-\tstd::vector<Span<uint8_t>> planes_;\n };\n \n CameraBuffer::Private::Private([[maybe_unused]] CameraBuffer *cameraBuffer,\ndiff --git a/src/libcamera/mapped_framebuffer.cpp b/src/libcamera/mapped_framebuffer.cpp\nindex 2ebe9fdb..1dc88647 100644\n--- a/src/libcamera/mapped_framebuffer.cpp\n+++ b/src/libcamera/mapped_framebuffer.cpp\n@@ -7,8 +7,11 @@\n \n #include \"libcamera/internal/mapped_framebuffer.h\"\n \n+#include <algorithm>\n #include <errno.h>\n+#include <map>\n #include <sys/mman.h>\n+#include <unistd.h>\n \n #include <libcamera/base/log.h>\n \n@@ -79,6 +82,7 @@ MappedBuffer::MappedBuffer(MappedBuffer &&other)\n MappedBuffer &MappedBuffer::operator=(MappedBuffer &&other)\n {\n \terror_ = other.error_;\n+\tplanes_ = std::move(other.planes_);\n \tmaps_ = std::move(other.maps_);\n \tother.error_ = -ENOENT;\n \n@@ -127,10 +131,18 @@ MappedBuffer::~MappedBuffer()\n  */\n \n /**\n- * \\var MappedBuffer::maps_\n+ * \\var MappedBuffer::planes_\n  * \\brief Stores the internal mapped planes\n  *\n  * MappedBuffer derived classes shall store the mappings they create in this\n+ * vector which points the beginning of mapped plane addresses.\n+ */\n+\n+/**\n+ * \\var MappedBuffer::maps_\n+ * \\brief Stores the mapped buffer\n+ *\n+ * MappedBuffer derived classes shall store the mappings they create in this\n  * vector which is parsed during destruct to unmap any memory mappings which\n  * completed successfully.\n  */\n@@ -167,7 +179,8 @@ MappedBuffer::~MappedBuffer()\n  */\n MappedFrameBuffer::MappedFrameBuffer(const FrameBuffer *buffer, MapFlags flags)\n {\n-\tmaps_.reserve(buffer->planes().size());\n+\tASSERT(!buffer->planes().empty());\n+\tplanes_.reserve(buffer->planes().size());\n \n \tint mmapFlags = 0;\n \n@@ -177,17 +190,55 @@ MappedFrameBuffer::MappedFrameBuffer(const FrameBuffer *buffer, MapFlags flags)\n \tif (flags & MapFlag::Write)\n \t\tmmapFlags |= PROT_WRITE;\n \n+\tstruct MappedBufferInfo {\n+\t\tuint8_t *address = nullptr;\n+\t\tsize_t mapLength = 0;\n+\t\tsize_t dmabufLength = 0;\n+\t};\n+\tstd::map<int, MappedBufferInfo> mappedBuffers;\n+\n+\tfor (const FrameBuffer::Plane &plane : buffer->planes()) {\n+\t\tconst int fd = plane.fd.fd();\n+\t\tif (mappedBuffers.find(fd) == mappedBuffers.end()) {\n+\t\t\tconst size_t length = lseek(fd, 0, SEEK_END);\n+\t\t\tmappedBuffers[fd] = MappedBufferInfo{ nullptr, 0, length };\n+\t\t}\n+\n+\t\tconst size_t length = mappedBuffers[fd].dmabufLength;\n+\n+\t\tif (plane.offset > length ||\n+\t\t    plane.offset + plane.length > length) {\n+\t\t\tLOG(Buffer, Fatal) << \"plane is out of buffer: \"\n+\t\t\t\t\t   << \"buffer length=\" << length\n+\t\t\t\t\t   << \", plane offset=\" << plane.offset\n+\t\t\t\t\t   << \", plane length=\" << plane.length;\n+\t\t\treturn;\n+\t\t}\n+\t\tsize_t &mapLength = mappedBuffers[fd].mapLength;\n+\t\tmapLength = std::max(mapLength,\n+\t\t\t\t     static_cast<size_t>(plane.offset + plane.length));\n+\t}\n+\n \tfor (const FrameBuffer::Plane &plane : buffer->planes()) {\n-\t\tvoid *address = mmap(nullptr, plane.length, mmapFlags,\n-\t\t\t\t     MAP_SHARED, plane.fd.fd(), 0);\n-\t\tif (address == MAP_FAILED) {\n-\t\t\terror_ = -errno;\n-\t\t\tLOG(Buffer, Error) << \"Failed to mmap plane: \"\n-\t\t\t\t\t   << strerror(-error_);\n-\t\t\tbreak;\n+\t\tconst int fd = plane.fd.fd();\n+\t\tauto &info = mappedBuffers[fd];\n+\t\tif (!info.address) {\n+\t\t\tvoid *address =\n+\t\t\t\tstatic_cast<uint8_t *>(\n+\t\t\t\t\tmmap(nullptr, info.mapLength, mmapFlags,\n+\t\t\t\t\t     MAP_SHARED, fd, 0));\n+\t\t\tif (address == MAP_FAILED) {\n+\t\t\t\terror_ = -errno;\n+\t\t\t\tLOG(Buffer, Error) << \"Failed to mmap plane: \"\n+\t\t\t\t\t\t   << strerror(-error_);\n+\t\t\t\treturn;\n+\t\t\t}\n+\n+\t\t\tinfo.address = static_cast<uint8_t *>(address);\n+\t\t\tmaps_.emplace_back(info.address, info.mapLength);\n \t\t}\n \n-\t\tmaps_.emplace_back(static_cast<uint8_t *>(address), plane.length);\n+\t\tplanes_.emplace_back(info.address + plane.offset, plane.length);\n \t}\n }\n \ndiff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp\nindex ce60dff6..2ff25af2 100644\n--- a/src/libcamera/v4l2_videodevice.cpp\n+++ b/src/libcamera/v4l2_videodevice.cpp\n@@ -1283,6 +1283,12 @@ std::unique_ptr<FrameBuffer> V4L2VideoDevice::createBuffer(unsigned int index)\n \n \t\tFrameBuffer::Plane plane;\n \t\tplane.fd = std::move(fd);\n+\t\t/*\n+\t\t * V4L2 API doesn't provide dmabuf offset information of plane.\n+\t\t * Set 0 as a placeholder offset.\n+\t\t * \\todo Set the right offset once V4L2 API provides a way.\n+\t\t */\n+\t\tplane.offset = 0;\n \t\tplane.length = multiPlanar ?\n \t\t\tbuf.m.planes[nplane].length : buf.length;\n \n","prefixes":["libcamera-devel","v3","2/9"]}