From patchwork Mon Aug 23 13:12:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 13444 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 CACC4BD87C for ; Mon, 23 Aug 2021 13:12:37 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 785AD688D2; Mon, 23 Aug 2021 15:12:37 +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="Ut7SSl78"; dkim-atps=neutral Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 7391F688A2 for ; Mon, 23 Aug 2021 15:12:35 +0200 (CEST) Received: by mail-pf1-x431.google.com with SMTP id m26so15320318pff.3 for ; Mon, 23 Aug 2021 06:12:35 -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=9UJviNb4DpnhQj5osTY6f0kvGeRQy+OdwckPD5N+q80=; b=Ut7SSl78ctnG1noGbqNg58wzBLc5ae+C/rkmQ8XMDvUH0eE/RcZMa2QIfU+i8CT6XG +p+Y3Frp+wPYOtOnC1nFMRig198pimul5BYMjnCRLTdxhFwfvIWOVGPs/JKW42yEePxt 5lfU/ObOselqSIwWD+vDF+ePf+4iRbw3eBlxc= 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=9UJviNb4DpnhQj5osTY6f0kvGeRQy+OdwckPD5N+q80=; b=YzV4/uDFshlHVRrQC5mfKCkbKi0lwcKKiTA1PqBh4VlhDgU3gotcQu83GXKjZOS/vY VrtUjEF5edeBUEvHz7jmh94fUvFo2PaoKhwAnspH+m8TkysBqML703ZeEAXSnkcQgK26 blgCmSY+OFtEL3Mhl2N8epGoAUB8mqhsSVIrTz2V44KCq0jEX161diMRtei9vFJWgmTx W7/cVN3VL+tPECcHGBILVqMAHFZHPUdWyQjZHNIx0htL7nc4WuoA5XvOFGkda3qNa8PR Hy1v4SFQUu8VtjbQg263x8M6u/d0IyoStxH7Bmf6PKHKb/kAGgnL+LoHGiKiSsOAp2cG qSkQ== X-Gm-Message-State: AOAM533OSbUSzqoeI8YMt2rejN/ai011BTNABL5DGF9lawiDHpZ5gKgf o9MSYKRCpldSFcOhpckGgtJZXbtUNiiXbQ== X-Google-Smtp-Source: ABdhPJwYLchTSp3iD14b1Zfh6iztidyRVDwGk9tcSv0N1HKuA5YZJZ5fe6KLBOTgxwvfndgae6x6FA== X-Received: by 2002:a05:6a00:190c:b0:3e2:911c:b7ca with SMTP id y12-20020a056a00190c00b003e2911cb7camr32584189pfi.5.1629724353659; Mon, 23 Aug 2021 06:12:33 -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.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Aug 2021 06:12:33 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Mon, 23 Aug 2021 22:12:12 +0900 Message-Id: <20210823131221.1034059-2-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 01/10] libcamera: framebuffer: Add offset to FrameBuffer::Plane 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" This adds offset to FrameBuffer::Plane. It enables representing frame buffers that store planes in the same dmabuf at different offsets, as for instance required by the V4L2 NV12 pixel format. Signed-off-by: Hirokazu Honda Reviewed-by: Laurent Pinchart --- include/libcamera/framebuffer.h | 1 + src/android/mm/generic_camera_buffer.cpp | 2 -- src/libcamera/framebuffer.cpp | 27 ++++++++++++++---------- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/include/libcamera/framebuffer.h b/include/libcamera/framebuffer.h index 28307890..5de3c744 100644 --- a/include/libcamera/framebuffer.h +++ b/include/libcamera/framebuffer.h @@ -42,6 +42,7 @@ class FrameBuffer final : public Extensible public: struct Plane { FileDescriptor fd; + unsigned int offset; unsigned int length; }; diff --git a/src/android/mm/generic_camera_buffer.cpp b/src/android/mm/generic_camera_buffer.cpp index b296b3e3..73fbd97a 100644 --- a/src/android/mm/generic_camera_buffer.cpp +++ b/src/android/mm/generic_camera_buffer.cpp @@ -52,8 +52,6 @@ private: int fd_; int flags_; std::vector planeInfo_; - /* \todo remove planes_ is added to MappedBuffer. */ - std::vector> planes_; }; CameraBuffer::Private::Private([[maybe_unused]] CameraBuffer *cameraBuffer, diff --git a/src/libcamera/framebuffer.cpp b/src/libcamera/framebuffer.cpp index b401c50e..e9467aa0 100644 --- a/src/libcamera/framebuffer.cpp +++ b/src/libcamera/framebuffer.cpp @@ -131,7 +131,7 @@ FrameBuffer::Private::Private() * * The static information describes the memory planes that make a frame. The * planes are specified when creating the FrameBuffer and are expressed as a set - * of dmabuf file descriptors and length. + * of dmabuf file descriptors, offset and length. * * The dynamic information is grouped in a FrameMetadata instance. It is updated * during the processing of a queued capture request, and is valid from the @@ -151,18 +151,18 @@ FrameBuffer::Private::Private() * * Planar pixel formats use multiple memory regions to store the different * colour components of a frame. The Plane structure describes such a memory - * region by a dmabuf file descriptor and a length. A FrameBuffer then - * contains one or multiple planes, depending on the pixel format of the - * frames it is meant to store. + * region by a dmabuf file descriptor, an offset within the dmabuf and a length. + * A FrameBuffer then contains one or multiple planes, depending on the pixel + * format of the frames it is meant to store. * - * To support DMA access, planes are associated with dmabuf objects represented - * by FileDescriptor handles. The Plane class doesn't handle mapping of the - * memory to the CPU, but applications and IPAs may use the dmabuf file - * descriptors to map the plane memory with mmap() and access its contents. + * The offset identifies the location of the plane data from the start of the + * memory referenced by the dmabuf file descriptor. Multiple planes may be + * stored in the same dmabuf, in which case they will reference the same dmabuf + * and different offsets. No two planes may overlap, as specified by their + * offset and length. * - * \todo Once we have a Kernel API which can express offsets within a plane - * this structure shall be extended to contain this information. See commit - * 83148ce8be55e for initial documentation of this feature. + * \todo Specify how an application shall decide whether to use a single or + * multiple dmabufs, based on the camera requirements. */ /** @@ -170,6 +170,11 @@ FrameBuffer::Private::Private() * \brief The dmabuf file descriptor */ +/** + * \var FrameBuffer::Plane::offset + * \brief The plane offset in bytes +*/ + /** * \var FrameBuffer::Plane::length * \brief The plane length in bytes From patchwork Mon Aug 23 13:12:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 13445 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 BB9A1BD87C for ; Mon, 23 Aug 2021 13:12:39 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 79D10688E5; Mon, 23 Aug 2021 15:12:39 +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="RASyjUv1"; dkim-atps=neutral Received: from mail-pg1-x52f.google.com (mail-pg1-x52f.google.com [IPv6:2607:f8b0:4864:20::52f]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 38C3268892 for ; Mon, 23 Aug 2021 15:12:37 +0200 (CEST) Received: by mail-pg1-x52f.google.com with SMTP id e7so16617252pgk.2 for ; Mon, 23 Aug 2021 06:12:37 -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=dA3NswfHOdIMk4OjM0SyDPL0RUS/V4KcyBeK+nmoKVg=; b=RASyjUv1jP3B3By56raG7xNoz7ieeMvSfA/PctIqaaI93BMfBqmi0s6Xp/s2nVdtmI 65xcf1V9P5bONudQY0NnPGdG2Dc/fmNYdBeoqo5++YtyA+0SLkF7ldlQ9m8gjL8vNZYf TUOjvtWLtLNV8+Bty+dJlsM09NSkOYUtLILeE= 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=dA3NswfHOdIMk4OjM0SyDPL0RUS/V4KcyBeK+nmoKVg=; b=hkj2Yjx/52rgDXMHZPq1ecEsbGLvp+63cNU4c0PQsUYPJkBoP9PysnbesehH38HoMD 0UBCNEpOIFdmZD5YaiCvk7kaBofM8KA5/BlkTa5ZJGm7eRWzk3C6ydpaqC+hs61fO9W/ Zkpj+EEnGspOXejRQa+IF3wXv8T6HDPwYcC6D9AFB6/Y4YrEGj3EbG7TLeOCqmLqTSFH dA5gTrLpBpnke9vFGtiVZjMJuPwv8lcBkGVuhYsxuDi6BWwkhX4TqTlLuTZV85u2PWVo CxFydcDAomv0NLMV2cU4YIEhEcdw2BMml3Ws745ZobmINmMtp+ZLO1P1E6VlcAqI9iJL yKgg== X-Gm-Message-State: AOAM531jYBotx3UH9giqx9zCBy4CuPoSTeJX07jE3716rGX1ByXZz2qy IWxNVT6GRpY9iwCO+0kI1afYAG4agT/s5w== X-Google-Smtp-Source: ABdhPJx6DVyRE2wSoZxr5tyIs9/nC2E4Dj1U7o1d3K3efLxmXTgyWTtcTDq/xQrrJPwbzkrVziDECw== X-Received: by 2002:a62:1b92:0:b0:3eb:3f92:724 with SMTP id b140-20020a621b92000000b003eb3f920724mr2026550pfb.3.1629724355547; Mon, 23 Aug 2021 06:12:35 -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.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Aug 2021 06:12:35 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Mon, 23 Aug 2021 22:12:13 +0900 Message-Id: <20210823131221.1034059-3-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 02/10] libcamera: ipa_data_serializer: Modify (de)serialization for offset 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" The offset variable is added to FrameBuffer::Plane. This modifies the serialization and deserialization code for the offset variable. Signed-off-by: Hirokazu Honda Reviewed-by: Laurent Pinchart --- src/libcamera/ipa_data_serializer.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/libcamera/ipa_data_serializer.cpp b/src/libcamera/ipa_data_serializer.cpp index 5b183c70..8d1ae4d2 100644 --- a/src/libcamera/ipa_data_serializer.cpp +++ b/src/libcamera/ipa_data_serializer.cpp @@ -569,6 +569,7 @@ FileDescriptor IPADataSerializer::deserialize(const std::vector< * FrameBuffer::Plane is serialized as: * * 4 byte - FileDescriptor + * 4 bytes - uint32_t Offset * 4 bytes - uint32_t Length */ template<> @@ -586,6 +587,7 @@ IPADataSerializer::serialize(const FrameBuffer::Plane &data, dataVec.insert(dataVec.end(), fdBuf.begin(), fdBuf.end()); fdsVec.insert(fdsVec.end(), fdFds.begin(), fdFds.end()); + appendPOD(dataVec, data.offset); appendPOD(dataVec, data.length); return { dataVec, fdsVec }; @@ -603,7 +605,8 @@ IPADataSerializer::deserialize(std::vector::const_i ret.fd = IPADataSerializer::deserialize(dataBegin, dataBegin + 4, fdsBegin, fdsBegin + 1); - ret.length = readPOD(dataBegin, 4, dataEnd); + ret.offset = readPOD(dataBegin, 1, dataEnd); + ret.length = readPOD(dataBegin, 5, dataEnd); return ret; } 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); } } From patchwork Mon Aug 23 13:12:15 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 13447 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 D8CACBD87C for ; Mon, 23 Aug 2021 13:12:43 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 832EF688A6; Mon, 23 Aug 2021 15:12:43 +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="EqhpK8Lh"; dkim-atps=neutral Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id A1B83688D2 for ; Mon, 23 Aug 2021 15:12:41 +0200 (CEST) Received: by mail-pf1-x42e.google.com with SMTP id x16so15329511pfh.2 for ; Mon, 23 Aug 2021 06:12:41 -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=ub6vFbEA76Qz1icj8AASyaVTq9tX0ZQYDwgHf6xWUDM=; b=EqhpK8LhfNmZi6ZAzIpD1MvHBeyLaqLnjX9TFBfxmBcZq7UBKcErFWptbh4fA1tSq1 3vS+qN3g/9EkSpTlkM0XeIUTk5wDvDBzZS8qsGTDdP/Uqmub6aJOQszHUeqqmSiR2f6E v1zMHlsLXBQk+WFqWC23EnZtwC1Cx1BTXtl2A= 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=ub6vFbEA76Qz1icj8AASyaVTq9tX0ZQYDwgHf6xWUDM=; b=JPK5qN7nfKML7/OI73DxTPEcqea1JJ89JQHM0iH6JRbsMTuYUQNocgqxPHQfrbK52w fYeOIGgMSxs1vyNFrUL5R4g12TdUmIWlZeSp0yFkCKaTPFk0m8yocYDTjn7kHNGRQq95 fPWqbDGVxrJnQUEf5UGQqtqrXm5gJ7YxyQuEiXclkqSNKYMZQaPVUQNsivleFjIyyZ9W YQ11UO+nqnBNot3BcTaOhJhjRsMM4G7TbjSeLXKSaj1nNtQO6E33BPydy4zUzs2jkXkU HZbElHmK8noKwnCaFTTQDdNlVUL/87+n69vu3bMAlh9uz9Fu+deZmKj/fsLjKkyMsjUA MV8Q== X-Gm-Message-State: AOAM532bxQGRZkw8kE6sa8Gd25V2OH8p3uolKDKEIGyB72QoAlHOoh+g +15/9fJwJMX7IE3+e6d/eW3Zt35BM6u2fQ== X-Google-Smtp-Source: ABdhPJxY6yyNkTLgPD6xGJlIziq4Cs9IxQSM4sQNBDJZzikgUcYRBrL9QWRKRK65wJilwzg31/zfXg== X-Received: by 2002:a62:3301:0:b0:3eb:2fee:87cc with SMTP id z1-20020a623301000000b003eb2fee87ccmr5217633pfz.62.1629724359970; Mon, 23 Aug 2021 06:12:39 -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.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Aug 2021 06:12:39 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Mon, 23 Aug 2021 22:12:15 +0900 Message-Id: <20210823131221.1034059-5-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 04/10] cam: file_sink: Use offset in mapping FrameBuffer 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" This fixes the way of mapping FrameBuffer in FrameSink by using offset. Signed-off-by: Hirokazu Honda Reviewed-by: Laurent Pinchart --- src/cam/file_sink.cpp | 22 +++++++++++++++++----- src/cam/file_sink.h | 1 + 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/cam/file_sink.cpp b/src/cam/file_sink.cpp index 2d30694a..26a60058 100644 --- a/src/cam/file_sink.cpp +++ b/src/cam/file_sink.cpp @@ -51,12 +51,24 @@ int FileSink::configure(const libcamera::CameraConfiguration &config) void FileSink::mapBuffer(FrameBuffer *buffer) { + /* \todo use MappedFrameBuffer. */ for (const FrameBuffer::Plane &plane : buffer->planes()) { - void *memory = mmap(NULL, plane.length, PROT_READ, MAP_SHARED, - plane.fd.fd(), 0); + const int fd = plane.fd.fd(); + if (mappedBuffers_.find(fd) == mappedBuffers_.end()) { + /** + * \todo Should we try to only map the portions of the + * dmabuf that are used by planes ? + */ + size_t length = lseek(fd, 0, SEEK_END); + void *memory = mmap(NULL, plane.length, PROT_READ, + MAP_SHARED, plane.fd.fd(), 0); + mappedBuffers_[plane.fd.fd()] = + std::make_pair(memory, length); - mappedBuffers_[plane.fd.fd()] = - std::make_pair(memory, plane.length); + } + + void *memory = mappedBuffers_[fd].first; + planeData_[&plane] = static_cast(memory) + plane.offset; } } @@ -102,7 +114,7 @@ void FileSink::writeBuffer(const Stream *stream, FrameBuffer *buffer) const FrameBuffer::Plane &plane = buffer->planes()[i]; const FrameMetadata::Plane &meta = buffer->metadata().planes[i]; - void *data = mappedBuffers_[plane.fd.fd()].first; + uint8_t *data = planeData_[&plane]; unsigned int length = std::min(meta.bytesused, plane.length); if (meta.bytesused > plane.length) diff --git a/src/cam/file_sink.h b/src/cam/file_sink.h index c3eb230a..c12325d9 100644 --- a/src/cam/file_sink.h +++ b/src/cam/file_sink.h @@ -33,6 +33,7 @@ private: std::map streamNames_; std::string pattern_; std::map> mappedBuffers_; + std::map planeData_; }; #endif /* __CAM_FILE_SINK_H__ */ From patchwork Mon Aug 23 13:12:16 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 13448 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 9D96BBD87C for ; Mon, 23 Aug 2021 13:12:45 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 65F44688D2; Mon, 23 Aug 2021 15:12:45 +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="gz6wmSwq"; 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 6A51D688A3 for ; Mon, 23 Aug 2021 15:12:43 +0200 (CEST) Received: by mail-pf1-x42c.google.com with SMTP id t13so15331087pfl.6 for ; Mon, 23 Aug 2021 06:12:43 -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=WBeECzFwDoKtSJgAd7bs4ycayuxrXnEKLo1XxSC+/qI=; b=gz6wmSwqQ1UCLjtELzAPepIrOGRtxN1M01/9bTzCb20KcYynoF0U5J0kuIxnoTPTG0 bS99Y2V6B8dF7ygXJiB8AKbCpu4rTUM5NTRq6SpTIqLmqcdUeCPKjOhNx7fDjlxFQNw8 hMxH2gWQYVmHQhQVUs3OrtK+r3ryQnuMvCmnE= 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=WBeECzFwDoKtSJgAd7bs4ycayuxrXnEKLo1XxSC+/qI=; b=s+KFv0bVz08XMok/75MyzH/b2zy0ZTOYWSC6l9xElmUA0OKxXxaSjkR2fHI0Ao1EsH XBjmQayxpAeuvYAhdjEFu6n8ZiZmPs8xMA+OifsJIBQJdwb26JDQE/5lbB8ui8+mcbcU GCi49RhN63iKyVACYwbBGJU5b9jj0U01QALmnNGD4pYVAwkLDRfYmCwG4FKWQeqh1drF L6F5gkyoSJvLki57VMPdRplEkb2K0lJAoy0v0mDbGbiqrKPhJ++bNwqbfXrK719dTfOg SQJqfQPxOSYuwq/A2NhxIfjI9qS1zGcJhfz18oUdRdN1Z6fVNCJNotTmIGDW9Dkz4JT6 utXQ== X-Gm-Message-State: AOAM530Df7mZIPSr7ReC55sN0DGCFcHLVZa4cshNf24hGqOAXu85dNJM JuCGwod5WmybUsDt5pscLQpETBzp57aUQQ== X-Google-Smtp-Source: ABdhPJzlT/JaKRe58XnO8LQj4bjnhAY/3pfN70HJRl1kJoLcI7ocoz4zH5qq6tN2nKRflv/w4qtB8w== X-Received: by 2002:a63:d607:: with SMTP id q7mr32144408pgg.268.1629724361779; Mon, 23 Aug 2021 06:12:41 -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.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Aug 2021 06:12:41 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Mon, 23 Aug 2021 22:12:16 +0900 Message-Id: <20210823131221.1034059-6-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 05/10] ipa: rkisp1: Use offset in mapping IPABuffer 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" IPABuffer is represented by FrameBuffer. FrameBuffer::Plane has now an offset. This uses the offset variable to map the IPABuffer. Signed-off-by: Hirokazu Honda Reviewed-by: Laurent Pinchart --- .../libcamera/internal/mapped_framebuffer.h | 1 + src/ipa/rkisp1/rkisp1.cpp | 31 +++++++------------ src/libcamera/mapped_framebuffer.cpp | 7 +++++ 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/include/libcamera/internal/mapped_framebuffer.h b/include/libcamera/internal/mapped_framebuffer.h index 42479541..ee0583d0 100644 --- a/include/libcamera/internal/mapped_framebuffer.h +++ b/include/libcamera/internal/mapped_framebuffer.h @@ -55,6 +55,7 @@ public: using MapFlags = Flags; + MappedFrameBuffer(); MappedFrameBuffer(const FrameBuffer *buffer, MapFlags flags); }; diff --git a/src/ipa/rkisp1/rkisp1.cpp b/src/ipa/rkisp1/rkisp1.cpp index 06fb9640..54cf2885 100644 --- a/src/ipa/rkisp1/rkisp1.cpp +++ b/src/ipa/rkisp1/rkisp1.cpp @@ -10,7 +10,6 @@ #include #include #include -#include #include #include @@ -24,6 +23,8 @@ #include #include +#include + namespace libcamera { LOG_DEFINE_CATEGORY(IPARkISP1) @@ -54,7 +55,7 @@ private: void metadataReady(unsigned int frame, unsigned int aeState); std::map buffers_; - std::map buffersMemory_; + std::map mappedBuffers_; ControlInfoMap ctrls_; @@ -160,21 +161,10 @@ void IPARkISP1::mapBuffers(const std::vector &buffers) std::forward_as_tuple(buffer.planes)); const FrameBuffer &fb = elem.first->second; - /* - * \todo Provide a helper to mmap() buffers (possibly exposed - * to applications). - */ - buffersMemory_[buffer.id] = mmap(NULL, - fb.planes()[0].length, - PROT_READ | PROT_WRITE, - MAP_SHARED, - fb.planes()[0].fd.fd(), - 0); - - if (buffersMemory_[buffer.id] == MAP_FAILED) { - int ret = -errno; + mappedBuffers_[buffer.id] = MappedFrameBuffer(&fb, MappedFrameBuffer::MapFlag::ReadWrite); + if (!mappedBuffers_[buffer.id].isValid()) { LOG(IPARkISP1, Fatal) << "Failed to mmap buffer: " - << strerror(-ret); + << strerror(mappedBuffers_[buffer.id].error()); } } } @@ -186,8 +176,7 @@ void IPARkISP1::unmapBuffers(const std::vector &ids) if (fb == buffers_.end()) continue; - munmap(buffersMemory_[id], fb->second.planes()[0].length); - buffersMemory_.erase(id); + mappedBuffers_.erase(id); buffers_.erase(id); } } @@ -200,7 +189,8 @@ void IPARkISP1::processEvent(const RkISP1Event &event) unsigned int bufferId = event.bufferId; const rkisp1_stat_buffer *stats = - static_cast(buffersMemory_[bufferId]); + reinterpret_cast( + mappedBuffers_[bufferId].maps()[0].data()); updateStatistics(frame, stats); break; @@ -210,7 +200,8 @@ void IPARkISP1::processEvent(const RkISP1Event &event) unsigned int bufferId = event.bufferId; rkisp1_params_cfg *params = - static_cast(buffersMemory_[bufferId]); + reinterpret_cast( + mappedBuffers_[bufferId].maps()[0].data()); queueRequest(frame, params, event.controls); break; diff --git a/src/libcamera/mapped_framebuffer.cpp b/src/libcamera/mapped_framebuffer.cpp index 03425dea..34d9564d 100644 --- a/src/libcamera/mapped_framebuffer.cpp +++ b/src/libcamera/mapped_framebuffer.cpp @@ -168,6 +168,13 @@ MappedBuffer::~MappedBuffer() * \brief A bitwise combination of MappedFrameBuffer::MapFlag values */ +/** + * \brief Construct an empty MappedFrameBuffer + */ +MappedFrameBuffer::MappedFrameBuffer() + : MappedBuffer() +{ +} /** * \brief Map all planes of a FrameBuffer * \param[in] buffer FrameBuffer to be mapped From patchwork Mon Aug 23 13:12:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 13449 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 13496BD87C for ; Mon, 23 Aug 2021 13:12:47 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id BB6966891A; Mon, 23 Aug 2021 15:12:46 +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="g7cclLPo"; dkim-atps=neutral Received: from mail-pf1-x433.google.com (mail-pf1-x433.google.com [IPv6:2607:f8b0:4864:20::433]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id A000568918 for ; Mon, 23 Aug 2021 15:12:45 +0200 (CEST) Received: by mail-pf1-x433.google.com with SMTP id y11so15319164pfl.13 for ; Mon, 23 Aug 2021 06:12:45 -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=D+2/XZSMz8xxue+lGeQLRASpDn2Szc61kzLG/Z4POt0=; b=g7cclLPomXzVGrExzEjHWx1TWn+SSW2Aq0BeN5atqtSsg0r2v53IDzOn9JAt4fFSrh 9+WaU+Y509G98IV2RHZKtzsiXlH3wwUBoecxo3vAYIhH5xY9oVcOGbGell8GmliwJgac aSNb+wzPbJY9ETxRT6NP7Jze/B59KKDacz6Hg= 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=D+2/XZSMz8xxue+lGeQLRASpDn2Szc61kzLG/Z4POt0=; b=KZ8Vni+iq0syU9JgiRtCJNc11gzcb9bc4MkNtxdXyzxLCOQATGWYmHlg24eq8rKNL1 u41rKp9q29EKaZgcXQI7c2EpcTa7uIHIAmbV9lmD+own9lplYTIiUsXY2xEPZ9jQh2Q+ EXqXYfMG5GPrzrEs/SuuI2F/Cy2L0TKloCqBvrpIMovzANqSCRydcsv2QvflDwO+OBJR Dx8UgFbY0oAflRxtafSLTa7CwWfY0WC+ccC8y7a3qba9naiVACdtr/4P22+ezw0xpO6b l1J2C23frKWXb8wnHiBDYQvM/gj/HDYE9ing3fmb76JyCzdJrJxg9AGqg6bpr3HBbc8S ZTtA== X-Gm-Message-State: AOAM530M9CY89ks6yXTATXUzuIAcTKhovwQsxqBZU+MwazXO4sobvddo lpZwkWg4FVV6a/g+oeB3GN8SF04f0SXwVg== X-Google-Smtp-Source: ABdhPJzidXNiHBtPAz92qAvlFjUqwSUnNF05NP/tQcpLJmeWg6+pr45t+CurtCdzjZ2+0TV49k6EQg== X-Received: by 2002:a63:4a18:: with SMTP id x24mr28136404pga.209.1629724363520; Mon, 23 Aug 2021 06:12:43 -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.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Aug 2021 06:12:43 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Mon, 23 Aug 2021 22:12:17 +0900 Message-Id: <20210823131221.1034059-7-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 06/10] qcam: main_window: Use offset mapping FrameBuffer 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" FrameBuffer::Plane has offset info now. This uses the offset in mapping FrameBuffer in MainWindow. Signed-off-by: Hirokazu Honda Reviewed-by: Laurent Pinchart --- src/qcam/main_window.cpp | 16 +++++++++++----- src/qcam/main_window.h | 1 + 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/qcam/main_window.cpp b/src/qcam/main_window.cpp index 39d034de..7fdec4a8 100644 --- a/src/qcam/main_window.cpp +++ b/src/qcam/main_window.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -472,9 +473,12 @@ int MainWindow::startCapture() for (const std::unique_ptr &buffer : allocator_->buffers(stream)) { /* Map memory buffers and cache the mappings. */ const FrameBuffer::Plane &plane = buffer->planes().front(); - void *memory = mmap(NULL, plane.length, PROT_READ, MAP_SHARED, + size_t length = lseek(plane.fd.fd(), 0, SEEK_END); + void *memory = mmap(NULL, length, PROT_READ, MAP_SHARED, plane.fd.fd(), 0); - mappedBuffers_[buffer.get()] = { memory, plane.length }; + mappedBuffers_[buffer.get()] = { memory, length }; + planeData_[buffer.get()] = { static_cast(memory) + plane.offset, + plane.length }; /* Store buffers on the free list. */ freeBuffers_[stream].enqueue(buffer.get()); @@ -541,6 +545,7 @@ error: munmap(buffer.memory, buffer.size); } mappedBuffers_.clear(); + planeData_.clear(); freeBuffers_.clear(); @@ -577,6 +582,7 @@ void MainWindow::stopCapture() munmap(buffer.memory, buffer.size); } mappedBuffers_.clear(); + planeData_.clear(); requests_.clear(); freeQueue_.clear(); @@ -673,10 +679,10 @@ void MainWindow::processRaw(FrameBuffer *buffer, "DNG Files (*.dng)"); if (!filename.isEmpty()) { - const MappedBuffer &mapped = mappedBuffers_[buffer]; + void *memory = planeData_[buffer].data(); DNGWriter::write(filename.toStdString().c_str(), camera_.get(), rawStream_->configuration(), metadata, buffer, - mapped.memory); + memory); } #endif @@ -753,7 +759,7 @@ void MainWindow::processViewfinder(FrameBuffer *buffer) << "fps:" << Qt::fixed << qSetRealNumberPrecision(2) << fps; /* Render the frame on the viewfinder. */ - viewfinder_->render(buffer, &mappedBuffers_[buffer]); + // HACK HACK HACK viewfinder_->render(buffer, &planeData_[buffer]); } void MainWindow::queueRequest(FrameBuffer *buffer) diff --git a/src/qcam/main_window.h b/src/qcam/main_window.h index 85d56ce4..f35cce36 100644 --- a/src/qcam/main_window.h +++ b/src/qcam/main_window.h @@ -107,6 +107,7 @@ private: std::unique_ptr config_; std::map mappedBuffers_; + std::map> planeData_; /* Capture state, buffers queue and statistics */ bool isCapturing_; From patchwork Mon Aug 23 13:12:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 13450 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 9412CBD87C for ; Mon, 23 Aug 2021 13:12:49 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 5A46D688CA; Mon, 23 Aug 2021 15:12:49 +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="eJ9u8zLd"; dkim-atps=neutral Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 2F64768920 for ; Mon, 23 Aug 2021 15:12:47 +0200 (CEST) Received: by mail-pf1-x429.google.com with SMTP id w68so15314259pfd.0 for ; Mon, 23 Aug 2021 06:12:47 -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=9PbGK38tT8uYeY88oZKwqMNsbfemhDMMeb9o2l6F2uA=; b=eJ9u8zLdh4twOwWYRCb+XHAVIZcsqGKgi9nGtola0aPwTuXTrAVGxfN2zx/m2cuFqv cGjiVO2iBFGdCWLeq3y5wnmFTDMdTzjCaVIcElTvltShQDj3BxvtdU7CxAT9PFCJmJYd en4k3IxyNVpqUjOkjykEZ2t5Arxbvqovql4tc= 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=9PbGK38tT8uYeY88oZKwqMNsbfemhDMMeb9o2l6F2uA=; b=eqQyK5OSRFPuHIfFVrEhWxKg3Z+sleVgNT7DxuL54eo4c2R7F2Zk/n51x7h3RCk5p5 eerWQv3h8TWbpYoQ96Vi4brfxhOnVXmYRQLbLrzJl3YfOA+Pd8T4xXoUZvUYYnADTWWP DBuHLDVOic+KMTDueHQ6r+mPB/2NuGH6b/1vMIVnQAOt/LAbqIohiJbIzUd7NormfU9L sLX78/PTGv3fle0g5fvCWKHlORs8TWsJwCGWXGFwreqUD8M+PnmCVkWjRkFoX/PJT9zt IDMoIDBN93gDQY7y6QvwQRlfwyB0y4nBWiyfgBgoN6UoE/REB4nRMtniWNa8c+4hgOw5 IE7A== X-Gm-Message-State: AOAM533JSuMd2cuEq6qHwwzqIXq1j1IvFxUtV/rntSZsNiFlADfGLN4K GV2+SiVVa5UQb2CsMyXgF+KdDN0ehpBNkg== X-Google-Smtp-Source: ABdhPJzIrOFv7D/k+mBv7p7+1oHyknh6O9vX2hCknQHRVDLE2sJAIOEKJ4HfxHS3Hq/QAk2ipGCnbw== X-Received: by 2002:a05:6a00:805:b0:3e2:f682:1b32 with SMTP id m5-20020a056a00080500b003e2f6821b32mr26740531pfk.60.1629724365492; Mon, 23 Aug 2021 06:12:45 -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.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Aug 2021 06:12:45 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Mon, 23 Aug 2021 22:12:18 +0900 Message-Id: <20210823131221.1034059-8-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 07/10] gstreamer: gstlibcameraallocator: Use offset in creating a buffer 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" The plane length is the length of the plane size. The buffer length to be allocated for a plane is the offset and the length of FrameBuffer::Plane. Signed-off-by: Hirokazu Honda Reviewed-by: Nicolas Dufresne --- src/gstreamer/gstlibcameraallocator.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/gstreamer/gstlibcameraallocator.cpp b/src/gstreamer/gstlibcameraallocator.cpp index 7bd8ba2d..60ead273 100644 --- a/src/gstreamer/gstlibcameraallocator.cpp +++ b/src/gstreamer/gstlibcameraallocator.cpp @@ -52,8 +52,10 @@ FrameWrap::FrameWrap(GstAllocator *allocator, FrameBuffer *buffer, outstandingPlanes_(0) { for (const FrameBuffer::Plane &plane : buffer->planes()) { - GstMemory *mem = gst_fd_allocator_alloc(allocator, plane.fd.fd(), plane.length, + GstMemory *mem = gst_fd_allocator_alloc(allocator, plane.fd.fd(), + plane.offset + plane.length, GST_FD_MEMORY_FLAG_DONT_CLOSE); + gst_memory_resize(mem, plane.offset, plane.length); gst_mini_object_set_qdata(GST_MINI_OBJECT(mem), getQuark(), this, nullptr); GST_MINI_OBJECT(mem)->dispose = gst_libcamera_allocator_release; g_object_unref(mem->allocator); From patchwork Mon Aug 23 13:12:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 13451 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 E8748BD87C for ; Mon, 23 Aug 2021 13:12:52 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id AE0C4688CA; Mon, 23 Aug 2021 15:12:52 +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="oQtRZi4d"; dkim-atps=neutral Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id CE245688A2 for ; Mon, 23 Aug 2021 15:12:50 +0200 (CEST) Received: by mail-pj1-x102a.google.com with SMTP id oc2-20020a17090b1c0200b00179e56772d6so8740143pjb.4 for ; Mon, 23 Aug 2021 06:12:50 -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=VMr5JtxGWOv7zUWIkxNhnnNOPhxrXVqs3DGz83bed2o=; b=oQtRZi4dzIa+jp/glNpWyFwy8GQ91ucY/Z976CgvNCp8Ms1gqXwk+oJMQFJuPUqB4w As8p5pQEYMvnrVhSdkqbdhWIFASUVHQr9cC5pk2X+NTTja5wLp4IGMUVCNw4m4ZRMZGO ZhtrmC5KtZ09oAay3AgYcVxpA/pG7cao2gGGk= 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=VMr5JtxGWOv7zUWIkxNhnnNOPhxrXVqs3DGz83bed2o=; b=U+hDxNS4WSjVcLFSlYpaN8iv2Q68sfSeRZ9f8wtc3iC1lQ7b2tZiyMzY1fsCWF7V/T kIkhI6ElCeUJqP1cEciBUEFESFR8t4BN8w/lcBfjb3BabfZyaFDzI2aCgzJWPOt9Rclx Vdj8WNXhs3ar6V67LBd2hoxMhbdLJrOIOaBBOr4HJhIYtKYlB3B5Y3yXhE9aL2bsjpaQ vwOkWBj8R32dmRfzCjHabbmfdoK9WA3A6vFtW+sW5y6ls/4X4mG0v0RV7V2oYsV+PbWJ g1sLnF9FKohFrpqdtRkCrhs7t6B+djMn2sFQjIA9mM+sIdfGFKhAIPSgTMFIEKUj9stP hRLg== X-Gm-Message-State: AOAM530+HzaJpvEVC/rEPttWRyPM+/U7QRzmPllAlpR4ePJoIAgVd/xQ abrAcPL/PkljJyUn4WTcLKBOMG59Re3FCA== X-Google-Smtp-Source: ABdhPJyPsSab1RGWFDgFHWDr4ZYputxNeETibeqf6j9Ppl3aA+NwX9rKFdladJPpi5JS6+lXWWa6fw== X-Received: by 2002:a17:90a:708c:: with SMTP id g12mr20365493pjk.13.1629724368327; Mon, 23 Aug 2021 06:12:48 -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.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Aug 2021 06:12:47 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Mon, 23 Aug 2021 22:12:19 +0900 Message-Id: <20210823131221.1034059-9-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 08/10] libcamera: v4l2_videodevice: Create color-format planes in createBuffer() 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" V4L2VideDevice::createBuffer() creates the same number of FrameBuffer::Planes as V4L2 format planes. Therefore, if the v4l2 format single is single-planar format, the created number of FrameBuffer::Planes is 1. It should rather create the same number of FrameBuffer::Planes as the color format planes. Signed-off-by: Hirokazu Honda --- include/libcamera/internal/v4l2_videodevice.h | 9 +- src/libcamera/v4l2_videodevice.cpp | 147 ++++++++++++++---- test/v4l2_videodevice/buffer_cache.cpp | 6 +- 3 files changed, 128 insertions(+), 34 deletions(-) diff --git a/include/libcamera/internal/v4l2_videodevice.h b/include/libcamera/internal/v4l2_videodevice.h index e767ec84..69bb964a 100644 --- a/include/libcamera/internal/v4l2_videodevice.h +++ b/include/libcamera/internal/v4l2_videodevice.h @@ -114,8 +114,9 @@ struct V4L2Capability final : v4l2_capability { class V4L2BufferCache { public: - V4L2BufferCache(unsigned int numEntries); - V4L2BufferCache(const std::vector> &buffers); + V4L2BufferCache(unsigned int numEntries, unsigned int numPlanes); + V4L2BufferCache(const std::vector> &buffers, + unsigned int numPlanes); ~V4L2BufferCache(); int get(const FrameBuffer &buffer); @@ -126,7 +127,8 @@ private: { public: Entry(); - Entry(bool free, uint64_t lastUsed, const FrameBuffer &buffer); + Entry(bool free, uint64_t lastUsed, unsigned int numPlanes, + const FrameBuffer &buffer); bool operator==(const FrameBuffer &buffer) const; @@ -149,6 +151,7 @@ private: std::atomic lastUsedCounter_; std::vector cache_; + unsigned int numPlanes_; /* \todo Expose the miss counter through an instrumentation API. */ unsigned int missCounter_; }; diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp index ce60dff6..42b66bd3 100644 --- a/src/libcamera/v4l2_videodevice.cpp +++ b/src/libcamera/v4l2_videodevice.cpp @@ -25,6 +25,7 @@ #include +#include "libcamera/internal/formats.h" #include "libcamera/internal/media_device.h" #include "libcamera/internal/media_object.h" @@ -158,12 +159,13 @@ LOG_DECLARE_CATEGORY(V4L2) * \brief Create an empty cache with \a numEntries entries * \param[in] numEntries Number of entries to reserve in the cache * - * Create a cache with \a numEntries entries all marked as unused. The entries - * will be populated as the cache is used. This is typically used to implement - * buffer import, with buffers added to the cache as they are queued. + * Create a cache with \a numEntries entries all marked as unused. The entry is + * for \a numPlanes planes V4L2 buffer. The entries will be populated as the + * cache is used. This is typically used to implement buffer import, with + * buffers added to the cache as they are queued. */ -V4L2BufferCache::V4L2BufferCache(unsigned int numEntries) - : lastUsedCounter_(1), missCounter_(0) +V4L2BufferCache::V4L2BufferCache(unsigned int numEntries, unsigned int numPlanes) + : lastUsedCounter_(1), numPlanes_(numPlanes), missCounter_(0) { cache_.resize(numEntries); } @@ -172,16 +174,18 @@ V4L2BufferCache::V4L2BufferCache(unsigned int numEntries) * \brief Create a pre-populated cache * \param[in] buffers Array of buffers to pre-populated with * - * Create a cache pre-populated with \a buffers. This is typically used to - * implement buffer export, with all buffers added to the cache when they are - * allocated. + * Create a cache pre-populated with \a buffers. The entry is for \a numPlanes + * planes V4L2 buffer. This is typically used to implement buffer export, with + * all buffers added to the cache when they are allocated. */ -V4L2BufferCache::V4L2BufferCache(const std::vector> &buffers) - : lastUsedCounter_(1), missCounter_(0) +V4L2BufferCache::V4L2BufferCache(const std::vector> &buffers, + unsigned int numPlanes) + : lastUsedCounter_(1), numPlanes_(numPlanes), missCounter_(0) { for (const std::unique_ptr &buffer : buffers) cache_.emplace_back(true, lastUsedCounter_.fetch_add(1, std::memory_order_acq_rel), + numPlanes_, *buffer.get()); } @@ -237,6 +241,7 @@ int V4L2BufferCache::get(const FrameBuffer &buffer) cache_[use] = Entry(false, lastUsedCounter_.fetch_add(1, std::memory_order_acq_rel), + numPlanes_, buffer); return use; @@ -257,24 +262,47 @@ V4L2BufferCache::Entry::Entry() { } -V4L2BufferCache::Entry::Entry(bool free, uint64_t lastUsed, const FrameBuffer &buffer) +V4L2BufferCache::Entry::Entry(bool free, uint64_t lastUsed, + unsigned int numPlanes, const FrameBuffer &buffer) : free_(free), lastUsed_(lastUsed) { - for (const FrameBuffer::Plane &plane : buffer.planes()) - planes_.emplace_back(plane); + ASSERT(numPlanes <= buffer.planes().size()); + unsigned int length = 0; + for (const FrameBuffer::Plane &plane : buffer.planes()) { + ASSERT(plane.offset == length); + length += plane.length; + + if (planes_.size() < numPlanes) + planes_.emplace_back(plane); + } + + if (numPlanes == 1) { + planes_[0].length = length; + } } bool V4L2BufferCache::Entry::operator==(const FrameBuffer &buffer) const { const std::vector &planes = buffer.planes(); - if (planes_.size() != planes.size()) + if (planes_.size() != planes.size() || planes_.size() == 1) return false; - - for (unsigned int i = 0; i < planes.size(); i++) - if (planes_[i].fd != planes[i].fd.fd() || - planes_[i].length != planes[i].length) + if (planes_.size() == 1) { + unsigned int length = 0; + for (unsigned int i = 0; i < planes.size(); i++) { + if (planes_[0].fd != planes[i].fd.fd()) + return false; + length += planes[i].length; + } + if (length != planes_[0].length) return false; + } else { + for (unsigned int i = 0; i < planes.size(); i++) + if (planes_[i].fd != planes[i].fd.fd() || + planes_[i].length != planes[i].length) + return false; + } + return true; } @@ -1152,8 +1180,13 @@ int V4L2VideoDevice::requestBuffers(unsigned int count, * successful return the driver's internal buffer management is initialized in * MMAP mode, and the video device is ready to accept queueBuffer() calls. * - * The number of planes and the plane sizes for the allocation are determined - * by the currently active format on the device as set by setFormat(). + * The number of planes and their offsets and sizes are determined by the + * currently active format on the device as set by setFormat(). They do not map + * to the V4L2 buffer planes, but to colour planes of the pixel format. For + * instance, if the active format is formats::NV12, the allocated FrameBuffer + * instances will have two planes, for the luma and chroma components, + * regardless of whether the device uses V4L2_PIX_FMT_NV12 or + * V4L2_PIX_FMT_NV12M. * * Buffers allocated with this function shall later be free with * releaseBuffers(). If buffers have already been allocated with @@ -1167,11 +1200,19 @@ int V4L2VideoDevice::requestBuffers(unsigned int count, int V4L2VideoDevice::allocateBuffers(unsigned int count, std::vector> *buffers) { - int ret = createBuffers(count, buffers); + V4L2DeviceFormat format{}; + int ret = getFormat(&format); + if (ret < 0) { + LOG(V4L2, Error) + << "Failed to get format: " << strerror(-ret); + return ret; + } + + ret = createBuffers(count, buffers); if (ret < 0) return ret; - cache_ = new V4L2BufferCache(*buffers); + cache_ = new V4L2BufferCache(*buffers, format.planesCount); memoryType_ = V4L2_MEMORY_MMAP; return ret; @@ -1190,8 +1231,13 @@ int V4L2VideoDevice::allocateBuffers(unsigned int count, * usable with any V4L2 video device in DMABUF mode, or with other dmabuf * importers. * - * The number of planes and the plane sizes for the allocation are determined - * by the currently active format on the device as set by setFormat(). + * The number of planes and their offsets and sizes are determined by the + * currently active format on the device as set by setFormat(). They do not map + * to the V4L2 buffer planes, but to colour planes of the pixel format. For + * instance, if the active format is formats::NV12, the allocated FrameBuffer + * instances will have two planes, for the luma and chroma components, + * regardless of whether the device uses V4L2_PIX_FMT_NV12 or + * V4L2_PIX_FMT_NV12M. * * Multiple independent sets of buffers can be allocated with multiple calls to * this function. Device-specific limitations may apply regarding the minimum @@ -1283,12 +1329,49 @@ std::unique_ptr V4L2VideoDevice::createBuffer(unsigned int index) FrameBuffer::Plane plane; plane.fd = std::move(fd); - plane.length = multiPlanar ? - buf.m.planes[nplane].length : buf.length; + /* + * V4L2 API doesn't provide dmabuf offset information of plane. + * Set 0 as a placeholder offset. + * \todo Set the right offset once V4L2 API provides a way. + */ + plane.offset = 0; + plane.length = multiPlanar ? buf.m.planes[nplane].length : buf.length; planes.push_back(std::move(plane)); } + /* + * Get V4L2DeviceFormat of the frame buffer. If it is V4L2 single-planar + * format, overwrite FrameBuffer::Plane to make it color format planes. + * */ + V4L2DeviceFormat format{}; + ret = getFormat(&format); + if (ret < 0) { + LOG(V4L2, Error) + << "Failed to get format: " << strerror(-ret); + return nullptr; + } + + const auto &info = PixelFormatInfo::info(format.fourcc); + if (info.isValid() && info.numPlanes() != numPlanes) { + ASSERT(numPlanes == 1u); + const size_t numColorPlanes = info.numPlanes(); + planes.resize(numColorPlanes); + const FileDescriptor &fd = planes[0].fd; + size_t offset = 0; + for (size_t i = 0; i < numColorPlanes; ++i) { + planes[i].fd = fd; + planes[i].offset = offset; + + const unsigned int vertSubSample = + info.planes[i].verticalSubSampling; + planes[i].length = + info.stride(format.size.width, i, 1u) * + ((format.size.height + vertSubSample - 1) / vertSubSample); + offset += planes[i].length; + } + } + return std::make_unique(std::move(planes)); } @@ -1342,11 +1425,19 @@ int V4L2VideoDevice::importBuffers(unsigned int count) memoryType_ = V4L2_MEMORY_DMABUF; - int ret = requestBuffers(count, V4L2_MEMORY_DMABUF); + V4L2DeviceFormat format{}; + int ret = getFormat(&format); + if (ret < 0) { + LOG(V4L2, Error) + << "Failed to get format: " << strerror(-ret); + return ret; + } + + ret = requestBuffers(count, V4L2_MEMORY_DMABUF); if (ret) return ret; - cache_ = new V4L2BufferCache(count); + cache_ = new V4L2BufferCache(count, format.planesCount); LOG(V4L2, Debug) << "Prepared to import " << count << " buffers"; diff --git a/test/v4l2_videodevice/buffer_cache.cpp b/test/v4l2_videodevice/buffer_cache.cpp index b3f2bec1..48f748bc 100644 --- a/test/v4l2_videodevice/buffer_cache.cpp +++ b/test/v4l2_videodevice/buffer_cache.cpp @@ -166,7 +166,7 @@ public: * Test cache of same size as there are buffers, the cache is * created from a list of buffers and will be pre-populated. */ - V4L2BufferCache cacheFromBuffers(buffers); + V4L2BufferCache cacheFromBuffers(buffers, 1u); if (testSequential(&cacheFromBuffers, buffers) != TestPass) return TestFail; @@ -181,7 +181,7 @@ public: * Test cache of same size as there are buffers, the cache is * not pre-populated. */ - V4L2BufferCache cacheFromNumbers(numBuffers); + V4L2BufferCache cacheFromNumbers(numBuffers, 1u); if (testSequential(&cacheFromNumbers, buffers) != TestPass) return TestFail; @@ -196,7 +196,7 @@ public: * Test cache half the size of number of buffers used, the cache * is not pre-populated. */ - V4L2BufferCache cacheHalf(numBuffers / 2); + V4L2BufferCache cacheHalf(numBuffers / 2, 1u); if (testRandom(&cacheHalf, buffers) != TestPass) return TestFail; From patchwork Mon Aug 23 13:12:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 13452 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 456ECBD87C for ; Mon, 23 Aug 2021 13:12:55 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 0C767688D2; Mon, 23 Aug 2021 15:12:55 +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="EZqGupbA"; dkim-atps=neutral Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 821C86890E for ; Mon, 23 Aug 2021 15:12:53 +0200 (CEST) Received: by mail-pl1-x632.google.com with SMTP id x15so696955plg.10 for ; Mon, 23 Aug 2021 06:12:53 -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=/9J16zTMj+xkiz3SwiR2BYYmrKOcbO95+Sg/9KpSF18=; b=EZqGupbASLja44QyjTyBDa4ro8KFc/eJsUMAF66IIe6Dbc36lKecpHu9GTl+0INzsN 6JI6pBtdHo7kLqu4wPUkdfBDjL5c3cLUkXEafu7t+27mv1P0i8Rb8Ury+A+enEhu2ICB xWKtsRhfvlTgmkcTZGgFaDV5mSfphYCHGOo7o= 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=/9J16zTMj+xkiz3SwiR2BYYmrKOcbO95+Sg/9KpSF18=; b=DRbZ32pKqIn+dRzs4WS2KC6wDawlOMODhCNFlGATxZRpCRoPha5bJtehA1jzyQ/eC5 UfOFcZcUqEK2v5LTJZoyUlMABJagUD4SeYCVwVtCcRtCtraVo6nn0ALdIS8/ocP/UilS qlA0GmFcL0olstvVlYxNs5Y0D+vEMCQX8Qeo84d5UVt9E62tkp35D9cxtPgCF3LWNm4p r3re5X3KULS2qXzErDAJ0hZTwVdlxfqdixtBvrYfM+GNCCct5RpkJm497tQ/vQK8SzsD gJ6l49DjFElhG6JfOEXZAu9YAjhaL5q0QH9alg+dV06OCSeA3jYAP6oMgegDBtOD3NOF w2RQ== X-Gm-Message-State: AOAM5309HR/VEeQwFgpWTfviZ814vurBRZEYGDGCstXgqxJz9f23NhDZ RtUgGnwJV34ScbEevIRLnSBKaiGwxs7MUQ== X-Google-Smtp-Source: ABdhPJxHaWs+vTAGZxRFjUSM/NMVYXbJwOTXGyNc+7Iwx2A7wHDVub6RPCM6zMV/Ww73lk9MTgRSRw== X-Received: by 2002:a17:902:768b:b029:12d:306e:9f68 with SMTP id m11-20020a170902768bb029012d306e9f68mr28563964pll.13.1629724371752; Mon, 23 Aug 2021 06:12:51 -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.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Aug 2021 06:12:50 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Mon, 23 Aug 2021 22:12:20 +0900 Message-Id: <20210823131221.1034059-10-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 09/10] android: camera_device: Fill offset and right length in CreateFrameBuffer() 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" CameraDevice::CreateFrameBuffer() fills the length of the buffer to each FrameBuffer::Plane::length. It should rather be the length of plane. This also changes CreateFrameBuffer() to fill offset of FrameBuffer::Plane. Signed-off-by: Hirokazu Honda Reviewed-by: Laurent Pinchart --- src/android/camera_device.cpp | 47 ++++++++++++++++++++--------------- src/android/camera_device.h | 6 ++++- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index a69b687a..6663817d 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -12,6 +12,7 @@ #include #include +#include #include #include @@ -744,31 +745,35 @@ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list) return 0; } -FrameBuffer *CameraDevice::createFrameBuffer(const buffer_handle_t camera3buffer) +FrameBuffer *CameraDevice::createFrameBuffer(const buffer_handle_t camera3buffer, + libcamera::PixelFormat pixelFormat, + const libcamera::Size &size) { - std::vector planes; + FileDescriptor fd; + /* This assumes all the planes are in the same buffer. */ for (int i = 0; i < camera3buffer->numFds; i++) { - /* Skip unused planes. */ - if (camera3buffer->data[i] == -1) + if (camera3buffer->data[i] != -1) { + fd = FileDescriptor(camera3buffer->data[i]); break; - - FrameBuffer::Plane plane; - plane.fd = FileDescriptor(camera3buffer->data[i]); - if (!plane.fd.isValid()) { - LOG(HAL, Error) << "Failed to obtain FileDescriptor (" - << camera3buffer->data[i] << ") " - << " on plane " << i; - return nullptr; } + } - off_t length = lseek(plane.fd.fd(), 0, SEEK_END); - if (length == -1) { - LOG(HAL, Error) << "Failed to query plane length"; - return nullptr; - } + if (!fd.isValid()) { + LOG(HAL, Fatal) << "No valid fd"; + return nullptr; + } + + CameraBuffer buf(camera3buffer, pixelFormat, size, PROT_READ); + if (!buf.isValid()) { + LOG(HAL, Fatal) << "Failed mapping buffer"; + return nullptr; + } - plane.length = length; - planes.push_back(std::move(plane)); + std::vector planes(buf.numPlanes()); + for (size_t i = 0; i < buf.numPlanes(); ++i) { + planes[i].fd = fd; + planes[i].offset = buf.offset(i); + planes[i].length = buf.size(i); } return new FrameBuffer(std::move(planes)); @@ -976,7 +981,9 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques * associate it with the Camera3RequestDescriptor for * lifetime management only. */ - buffer = createFrameBuffer(*camera3Buffer.buffer); + buffer = createFrameBuffer(*camera3Buffer.buffer, + cameraStream->configuration().pixelFormat, + cameraStream->configuration().size); descriptor.frameBuffers_.emplace_back(buffer); LOG(HAL, Debug) << ss.str() << " (direct)"; break; diff --git a/src/android/camera_device.h b/src/android/camera_device.h index dd9aebba..a5576927 100644 --- a/src/android/camera_device.h +++ b/src/android/camera_device.h @@ -21,6 +21,8 @@ #include #include +#include +#include #include #include @@ -91,7 +93,9 @@ private: void stop(); - libcamera::FrameBuffer *createFrameBuffer(const buffer_handle_t camera3buffer); + libcamera::FrameBuffer *createFrameBuffer(const buffer_handle_t camera3buffer, + libcamera::PixelFormat pixelFormat, + const libcamera::Size &size); void abortRequest(camera3_capture_request_t *request); bool isValidRequest(camera3_capture_request_t *request) const; void notifyShutter(uint32_t frameNumber, uint64_t timestamp); From patchwork Mon Aug 23 13:12:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 13453 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 AA5F1BD87C for ; Mon, 23 Aug 2021 13:12:58 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 626A9688D2; Mon, 23 Aug 2021 15:12:58 +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="Zi9OgJrV"; dkim-atps=neutral Received: from mail-pf1-x430.google.com (mail-pf1-x430.google.com [IPv6:2607:f8b0:4864:20::430]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id E4E58688A2 for ; Mon, 23 Aug 2021 15:12:56 +0200 (CEST) Received: by mail-pf1-x430.google.com with SMTP id t42so12751526pfg.12 for ; Mon, 23 Aug 2021 06:12:56 -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=zOZ0vNaLjlI91E9tIB7rhvpt4DtZ3fL2NQ9YL4SxK5A=; b=Zi9OgJrVjJKEW2+DKe7MbMdZq3iFSzhL5VZSr7jYkWzoDJhXt2tSMa7Nezzrps2zES VZtYqqSzP5gTvS/6QQ80xVLrYuQ8vhP3ALIjoDSve4aCXbeYD7Nq/oAFUyWanNveyJQ2 h8zXy6tMGDEuiq+QYsngevF5xwHo703emENVA= 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=zOZ0vNaLjlI91E9tIB7rhvpt4DtZ3fL2NQ9YL4SxK5A=; b=O2ts8VHSwRi++M/GnWlghjRrMVq7tc7UFrnZILGXuMfq40LOB9LopXPeTW5dpah7+A SQnIO4+oQL2z5DugVzX2Ki63wuFTfnC2uG/Aq6PAauLnETB64h1zXPw8mVzd/EQZ0F+l ykvzLOSmCoYITC1vwr/5JFtje+YT82AE2XBMM7iaCd/E3h1J/5BCHzel/kZXpU1SBEiM la4KOLA6kiZ2Byc3uTbiR1PypgUfhj9zVNThOygFmTWPlMMXZ7ciV+iLt7XIBR1duQE4 Xc/tw4pr5508exPhRmSBd08bw+mKObbJiyMrxG87pKfhVmfGjY4ZR0t5aqrFjl1lzceT EbDQ== X-Gm-Message-State: AOAM5322qxPzHpCbbxNRD8si9lLjsLa9RMiN6dWTtV+iTpTbenzjCzTl FbHZWSybFHv5SypOrLeOMlW3cSM+b61o7g== X-Google-Smtp-Source: ABdhPJyRkeL5U2J546bEx7XWaSYn81Vn0YBB0FjbBffgJRno8L9Ovas1Hvfh0BlzNViZgd4adR+/7A== X-Received: by 2002:a63:8948:: with SMTP id v69mr32128430pgd.132.1629724375275; Mon, 23 Aug 2021 06:12:55 -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.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Aug 2021 06:12:53 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Mon, 23 Aug 2021 22:12:21 +0900 Message-Id: <20210823131221.1034059-11-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 10/10] libcamera: framebuffer: Add assertion to detect offset is unfilled 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" The offset variable is introduced to FrameBuffer::Plane. In order to detect that the plane is used while the offset is not set, this adds the assertion to FrameBuffer::planes(). It should be removed in the future. Signed-off-by: Hirokazu Honda Reviewed-by: Laurent Pinchart --- include/libcamera/framebuffer.h | 13 +++++++++++-- src/libcamera/framebuffer.cpp | 4 ++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/include/libcamera/framebuffer.h b/include/libcamera/framebuffer.h index 5de3c744..d5aeff00 100644 --- a/include/libcamera/framebuffer.h +++ b/include/libcamera/framebuffer.h @@ -7,6 +7,8 @@ #ifndef __LIBCAMERA_FRAMEBUFFER_H__ #define __LIBCAMERA_FRAMEBUFFER_H__ +#include +#include #include #include @@ -41,14 +43,21 @@ class FrameBuffer final : public Extensible public: struct Plane { + static constexpr unsigned int kInvalidOffset = std::numeric_limits::max(); FileDescriptor fd; - unsigned int offset; + unsigned int offset = kInvalidOffset; unsigned int length; }; FrameBuffer(const std::vector &planes, unsigned int cookie = 0); - const std::vector &planes() const { return planes_; } + const std::vector &planes() const + { + /* \todo Remove the assertions after sufficient testing */ + for (const auto &plane : planes_) + assert(plane.offset != Plane::kInvalidOffset); + return planes_; + } Request *request() const; const FrameMetadata &metadata() const { return metadata_; } diff --git a/src/libcamera/framebuffer.cpp b/src/libcamera/framebuffer.cpp index e9467aa0..5f612ecd 100644 --- a/src/libcamera/framebuffer.cpp +++ b/src/libcamera/framebuffer.cpp @@ -165,6 +165,10 @@ FrameBuffer::Private::Private() * multiple dmabufs, based on the camera requirements. */ +/** + * \var FrameBuffer::Plane::kInvalidOffset + * \brief The invalid offset value + */ /** * \var FrameBuffer::Plane::fd * \brief The dmabuf file descriptor