From patchwork Mon Aug 23 13:12:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 13446 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 5DF1FBD87C for ; Mon, 23 Aug 2021 13:12:41 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 176E5688A3; Mon, 23 Aug 2021 15:12:41 +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="EvERI/W8"; dkim-atps=neutral Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id BF23C688C4 for ; Mon, 23 Aug 2021 15:12:39 +0200 (CEST) Received: by mail-pf1-x42c.google.com with SMTP id x16so15329422pfh.2 for ; Mon, 23 Aug 2021 06:12:39 -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=ZwsyyHFiY04CFGEw0wUYmKMJlrRZiA5UN0Tdo4rWxBU=; b=EvERI/W8q4o7QkXNGu/8SzpiEMwSUrDLZ9oSc0NC2kyxPTBFFBXzUu74mWcjC6ZOgG pSxsmkRm9Fh0Bv2Yxu0b7ybS+C5YeZS/8k3K9h/SUKQoQUcoKx5p6yFRLpPjFmHgEyAO vYdvdqnec0BJBdf/GZJE3q1N6sIl3K2IrN9t0= 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=ZwsyyHFiY04CFGEw0wUYmKMJlrRZiA5UN0Tdo4rWxBU=; b=OXTWikkdP9i8ZypGR1rEny25MPiH07IxDb42HfMVweUUEF0TE6U8Zc4ZRXsmcwNvxi 1Vj2x/idiDoxDDQsKEkd9POTl2qyzeAgkR0r7fxhNrZ/u6Nb6QRAYpVdKC3Ephkm3DlL 1R6CUcVGSLqwRUnHBOEOIdHJ8lEHSvU0AQi5Cnueo7JfuHCU5XrGTW6FeN154jSj9JXS SSvbsC5rpXYqe23ZR/qLJFkBTA4rOGodfnXMP3agmk8IwFG1mCIFWEn71NEVmBtrFbp5 0rQwQhWmNtBthKuN/sQjmJg+TQ0FZldNTsRK9Y07UHx/l5GXn+qs5Sy4d8s7dlsBlHbJ 03iw== X-Gm-Message-State: AOAM5304aNENyyVIrA8uEHSd/yKyGvQL3XZHugD4YR8j/ikrX9KrFUkb g3Uxeg2BPRb0yVdyIRvS3Z+q1157oBuUSA== X-Google-Smtp-Source: ABdhPJzFZoEtrEiR527Q2qIq74zanQkGXRlj8L6MspVg/Lon7lWelKdSqFcn+Y6sHkniasG/p57Etw== X-Received: by 2002:a63:d0d:: with SMTP id c13mr31926493pgl.294.1629724358022; Mon, 23 Aug 2021 06:12:38 -0700 (PDT) Received: from hiroh2.tok.corp.google.com ([2401:fa00:8f:203:184e:5d20:8fcb:dfcd]) by smtp.gmail.com with ESMTPSA id j4sm18596891pgi.6.2021.08.23.06.12.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Aug 2021 06:12:37 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Mon, 23 Aug 2021 22:12:14 +0900 Message-Id: <20210823131221.1034059-4-hiroh@chromium.org> X-Mailer: git-send-email 2.33.0.rc2.250.ged5fa647cd-goog In-Reply-To: <20210823131221.1034059-1-hiroh@chromium.org> References: <20210823131221.1034059-1-hiroh@chromium.org> MIME-Version: 1.0 Subject: [libcamera-devel] [RFC PATCH v2 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 Reviewed-by: Laurent Pinchart --- .../libcamera/internal/mapped_framebuffer.h | 4 +- src/libcamera/mapped_framebuffer.cpp | 69 ++++++++++++++++--- 2 files changed, 62 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..03425dea 100644 --- a/src/libcamera/mapped_framebuffer.cpp +++ b/src/libcamera/mapped_framebuffer.cpp @@ -7,8 +7,11 @@ #include "libcamera/internal/mapped_framebuffer.h" +#include #include +#include #include +#include #include @@ -79,6 +82,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 +131,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 +179,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,17 +190,53 @@ MappedFrameBuffer::MappedFrameBuffer(const FrameBuffer *buffer, MapFlags flags) if (flags & MapFlag::Write) mmapFlags |= PROT_WRITE; + struct MappedBufferInfo { + uint8_t *address = nullptr; + size_t mapLength = 0; + size_t dmabufLength = 0; + }; + std::map mappedBuffers; + for (const FrameBuffer::Plane &plane : buffer->planes()) { + const int fd = plane.fd.fd(); + if (mappedBuffers.find(fd) == mappedBuffers.end()) { + const size_t length = lseek(fd, 0, SEEK_END); + mappedBuffers[fd] = MappedBufferInfo{ nullptr, 0, length }; + } + + const size_t length = mappedBuffers[fd].dmabufLength; + + if (plane.offset > length || + 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; + } + size_t &mapLength = mappedBuffers[fd].mapLength; + mapLength = std::max(mapLength, + static_cast(plane.offset + plane.length)); + } + 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(); + auto &info = mappedBuffers[fd]; + if (!info.address) { + info.address = + static_cast( + mmap(nullptr, info.mapLength, mmapFlags, + MAP_SHARED, fd, 0)); + if (info.address == MAP_FAILED) { + error_ = -errno; + LOG(Buffer, Error) << "Failed to mmap plane: " + << strerror(-error_); + return; + } + + maps_.emplace_back(info.address, info.mapLength); } - maps_.emplace_back(static_cast(address), plane.length); + planes_.emplace_back(info.address + plane.offset, plane.length); } }