From patchwork Wed Aug 11 12:40:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 13302 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 D18EFBD87D for ; Wed, 11 Aug 2021 12:40:27 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 8233E68889; Wed, 11 Aug 2021 14:40:27 +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="hd6C0FON"; dkim-atps=neutral Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 5BD1568887 for ; Wed, 11 Aug 2021 14:40:26 +0200 (CEST) Received: by mail-pj1-x1033.google.com with SMTP id mq2-20020a17090b3802b0290178911d298bso4625321pjb.1 for ; Wed, 11 Aug 2021 05:40:26 -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=lQiq0GEAYbCg/uzaZZgmDr/dO8jHINNLWTeG67DI18o=; b=hd6C0FONK/vS5+HpPPYsf0mBhNkEnZOs8OJoiaB8gQa6oCCkczZfWDtstvpaUxR1gq znFicgImUqrbkmZegTm6T2uXqzFKeXSyDv11WTed2t6ZVYc1qJrd76ob3Hjm6tHe829J V/nmCyW3mb9tPYG1wGSq/VGBpaTLsEQVrvraU= 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=lQiq0GEAYbCg/uzaZZgmDr/dO8jHINNLWTeG67DI18o=; b=K1/1+TvjcW0Gqx5VL4dIabxun2jck3TYyPM7CCVsVfb9xjvBpeerBmcePcCIjuW0TR i976kelDKPLAtrprSphvrR+6hBHiCuOUqmmTwveBfzClG0VDmoBTduZ4lRyTgfO6ourG 73FY5klJtgK2s3pHhFHLFb52c5RAnUsv9kTJ+1B30c1w1lDGjwXPR9zneh39bzhxZuV0 zIIAGoQM2MaoZ4Rl5mtXcKZJOylfvQmGyVNC0P9iZE44KRVF6ZxEosV0dN1jI7sPV4xL c8FwzfXjN4DOkkRODyIz9oo2Fsya8KkwHexvN8OUFnwjQFtZiFrzixpX9efwugSGjhLK Yb5g== X-Gm-Message-State: AOAM530iWXJP8XNJW7gKzXi5qaT4o3ly6Py3eE65C5XwgcpFrGufzeGw sOSJvxZ0pbWh7wDizcc1U4YgNda4/9sSng== X-Google-Smtp-Source: ABdhPJxzuCIgI0O0Zdun4srfar6sya/FsJJ7LimAsUo9YpaSQDyd4MkYgMVEcnCYjBMLBN/9CLTzIA== X-Received: by 2002:a17:90b:3014:: with SMTP id hg20mr25539426pjb.140.1628685624641; Wed, 11 Aug 2021 05:40:24 -0700 (PDT) Received: from hiroh2.tok.corp.google.com ([2401:fa00:8f:203:ba11:c25e:242c:485d]) by smtp.gmail.com with ESMTPSA id p30sm15722876pfh.116.2021.08.11.05.40.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Aug 2021 05:40:24 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Wed, 11 Aug 2021 21:40:11 +0900 Message-Id: <20210811124015.2116188-2-hiroh@chromium.org> X-Mailer: git-send-email 2.32.0.605.g8dce9f2422-goog In-Reply-To: <20210811124015.2116188-1-hiroh@chromium.org> References: <20210811124015.2116188-1-hiroh@chromium.org> MIME-Version: 1.0 Subject: [libcamera-devel] [RFC PATCH 1/5] 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 | 59 +++++++++++++++---- 2 files changed, 52 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..5d5b02f1 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,46 @@ MappedFrameBuffer::MappedFrameBuffer(const FrameBuffer *buffer, MapFlags flags) if (flags & MapFlag::Write) mmapFlags |= PROT_WRITE; + size_t offset = 0; + 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_); + break; + } + maps_.emplace_back(static_cast(address), + length); + prevFd = fd; } - maps_.emplace_back(static_cast(address), plane.length); + ASSERT(!maps_.empty()); + uint8_t *buf = maps_.back().data(); + const size_t length = maps_.back().size(); + + /* + * This offset calculation assumes planes are consecutive. + * \todo remove this assumption once offset is introduced to + * FrameBuffer::Plane. + */ + planes_.emplace_back(buf + offset, plane.length); + offset += plane.length; + + if (offset > length) { + LOG(Buffer, Fatal) + << "plane length is too large: plane lengths=" + << offset << ", buffer length=" << length; + break; + } } + + ASSERT(maps_.size() == 1); } } /* namespace libcamera */