From patchwork Mon Aug 16 04:31:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 13361 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 2A2F7BD87D for ; Mon, 16 Aug 2021 04:31:54 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id DACAF68894; Mon, 16 Aug 2021 06:31:53 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="Q3jnWge3"; dkim-atps=neutral Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id AC1FB6888A for ; Mon, 16 Aug 2021 06:31:51 +0200 (CEST) Received: by mail-pl1-x62c.google.com with SMTP id u1so2262175plr.1 for ; Sun, 15 Aug 2021 21:31:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=O5yb3VMefRl6XvYP/u28Ygt231KKcloKZj/dkCFdHeM=; b=Q3jnWge34hWXrcZTv9FdKQ3zT5pvohqi8YOu1nSDkYfg3LHAMPDadycpQ3lhJShfiH nNqpVnywrVCVJHLM+CiZXEfIe/oxPEo97vaUnFy5WuJG4rM/3ZePm4tRhVBjy4pHHZdV vYuQLJKF01nMiFrxEKFdXkHb81MCUxrSVVYpQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=O5yb3VMefRl6XvYP/u28Ygt231KKcloKZj/dkCFdHeM=; b=VAPVtvVvMB/pxS3fNQM9+TjRjkhrI6U2JKTeL5MXwAW+nQYwdBUC3lAPamLsF91WEp Q7uuR9GGlDGieLnaRZsWEo+RfQk/OFxhiiPeUulVbUfPcHpcg2wImIjLLY0h6OURSe0e k3aF/13USCID+CKIMXlgkOWm6YMEPP/24Uh7XtJXKjP+1cXy9nhWeYw5WfWnfyauG2RD zezoJ1a0NYSzTIsNQ+M7i7otJ/efurPk9jBdBVWGys0vAQBiIZYN+VogiDV+7brkXx3p f3vx+UzKSYEnN+o52AZPwus7lTFCDVKCLxXxZnxZX1g62kHji7xtzewXVmOQ3cPegMGW nSIg== X-Gm-Message-State: AOAM533sJgclmgBg2hbsfs5gt5CyK1CEMrFEU0mctAEtiY0PDki1sv5e 85w5YtjD/abPspiwHi5KmlMqTwrUx2TxUQ== X-Google-Smtp-Source: ABdhPJy+hTXvL1xXiTTu/xJzzSlB0keO65Ak7wnV1SHrz6UdXpordEfYIbCKcp/6CNLnJg5bC0nnlA== X-Received: by 2002:a17:90a:940e:: with SMTP id r14mr15078550pjo.41.1629088310035; Sun, 15 Aug 2021 21:31:50 -0700 (PDT) Received: from hiroh2.tok.corp.google.com ([2401:fa00:8f:203:6f5f:1479:a6ab:4229]) by smtp.gmail.com with ESMTPSA id u10sm2767417pgj.48.2021.08.15.21.31.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Aug 2021 21:31:49 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Mon, 16 Aug 2021 13:31:31 +0900 Message-Id: <20210816043138.957984-4-hiroh@chromium.org> X-Mailer: git-send-email 2.33.0.rc1.237.g0d66db33f3-goog In-Reply-To: <20210816043138.957984-1-hiroh@chromium.org> References: <20210816043138.957984-1-hiroh@chromium.org> MIME-Version: 1.0 Subject: [libcamera-devel] [RFC PATCH 03/10] libcamera: mapped_framebuffer: Return plane begin address by MappedBuffer::maps() X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" MappedBuffer::maps() returns std::vector. Plane has the address, but the address points the beginning of the buffer containing the plane. This makes the Plane point the beginning of the plane. So MappedBuffer::maps()[i].data() returns the address of i-th plane. Signed-off-by: Hirokazu Honda --- .../libcamera/internal/mapped_framebuffer.h | 4 +- src/libcamera/mapped_framebuffer.cpp | 51 +++++++++++++++---- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/include/libcamera/internal/mapped_framebuffer.h b/include/libcamera/internal/mapped_framebuffer.h index 3401a9fc..42479541 100644 --- a/include/libcamera/internal/mapped_framebuffer.h +++ b/include/libcamera/internal/mapped_framebuffer.h @@ -30,12 +30,14 @@ public: bool isValid() const { return error_ == 0; } int error() const { return error_; } - const std::vector &maps() const { return maps_; } + /* \todo rename to planes(). */ + const std::vector &maps() const { return planes_; } protected: MappedBuffer(); int error_; + std::vector planes_; std::vector maps_; private: diff --git a/src/libcamera/mapped_framebuffer.cpp b/src/libcamera/mapped_framebuffer.cpp index 2ebe9fdb..b0ba89b0 100644 --- a/src/libcamera/mapped_framebuffer.cpp +++ b/src/libcamera/mapped_framebuffer.cpp @@ -9,6 +9,7 @@ #include #include +#include #include @@ -79,6 +80,7 @@ MappedBuffer::MappedBuffer(MappedBuffer &&other) MappedBuffer &MappedBuffer::operator=(MappedBuffer &&other) { error_ = other.error_; + planes_ = std::move(other.planes_); maps_ = std::move(other.maps_); other.error_ = -ENOENT; @@ -127,10 +129,18 @@ MappedBuffer::~MappedBuffer() */ /** - * \var MappedBuffer::maps_ + * \var MappedBuffer::planes_ * \brief Stores the internal mapped planes * * MappedBuffer derived classes shall store the mappings they create in this + * vector which points the beginning of mapped plane addresses. + */ + +/** + * \var MappedBuffer::maps_ + * \brief Stores the mapped buffer + * + * MappedBuffer derived classes shall store the mappings they create in this * vector which is parsed during destruct to unmap any memory mappings which * completed successfully. */ @@ -167,7 +177,8 @@ MappedBuffer::~MappedBuffer() */ MappedFrameBuffer::MappedFrameBuffer(const FrameBuffer *buffer, MapFlags flags) { - maps_.reserve(buffer->planes().size()); + ASSERT(!buffer->planes().empty()); + planes_.reserve(buffer->planes().size()); int mmapFlags = 0; @@ -177,18 +188,38 @@ MappedFrameBuffer::MappedFrameBuffer(const FrameBuffer *buffer, MapFlags flags) if (flags & MapFlag::Write) mmapFlags |= PROT_WRITE; + int prevFd = -1; for (const FrameBuffer::Plane &plane : buffer->planes()) { - void *address = mmap(nullptr, plane.length, mmapFlags, - MAP_SHARED, plane.fd.fd(), 0); - if (address == MAP_FAILED) { - error_ = -errno; - LOG(Buffer, Error) << "Failed to mmap plane: " - << strerror(-error_); - break; + const int fd = plane.fd.fd(); + if (prevFd != fd) { + const size_t length = lseek(fd, 0, SEEK_END); + void *address = mmap(nullptr, length, mmapFlags, + MAP_SHARED, fd, 0); + if (address == MAP_FAILED) { + error_ = -errno; + LOG(Buffer, Error) << "Failed to mmap plane: " + << strerror(-error_); + return; + } + maps_.emplace_back(static_cast(address), + length); + prevFd = fd; } - maps_.emplace_back(static_cast(address), plane.length); + const size_t length = maps_.back().size(); + if (plane.offset + plane.length > length) { + LOG(Buffer, Fatal) << "plane is out of buffer: " + << "buffer length=" << length + << ", plane offset=" << plane.offset + << ", plane length=" << plane.length; + return; + } + + uint8_t *buf = maps_.back().data(); + planes_.emplace_back(buf + plane.offset, plane.length); } + + ASSERT(maps_.size() == 1u); } } /* namespace libcamera */