From patchwork Tue May 16 08:03:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cheng-Hao Yang X-Patchwork-Id: 18638 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 B53C6C3285 for ; Tue, 16 May 2023 08:05:12 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id A8A8D6286E; Tue, 16 May 2023 10:05:11 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1684224311; bh=txUIkZ3u9xSb2SNYvo9IkUSeGXC5nQtPTUZA9q/8tfI=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=CFIT9I6PtnZC3jIBvKk7rxHACrD+/GuLQOw/Jj0L7iueu2h+/e9dDv7Cggp588NkY DpnRpMaz5Rhx6OsH0QeCRMqG2l2ra8oao8p7cZ7s98VYaMpGBxOYgV6GtrusR65S+P e6TDlcf822uJ0LNBAzmty7QS/CwSU/o+J3eymX5JcRE6x0ATbAfnQO2v6xSOx0EZQx xsXXmRONNZutGQPTDI4e3y6HYsGQGzLvJ2LxLescSxR+GOyYibsbNdJX9xMxt7H6qn XmFY4rHj6WyjlzXe6nJazTSx5v+l0tfCxfj6iLTZHafOnQ4CoZ/MTK0AvoZQCjfwka J4UY5NmSRgI+A== 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 37EAB62861 for ; Tue, 16 May 2023 10:05:10 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="P1WUp9z1"; dkim-atps=neutral Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-1aaea3909d1so128993285ad.2 for ; Tue, 16 May 2023 01:05:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1684224308; x=1686816308; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ApWxcQmz7KVlqjwqFr0G5ziyjKDyN2vbRBPSrxD8FOk=; b=P1WUp9z18A4UXqUTxw3ZE98c7BtHVNb7F/sVqJYUsE+lo+BggN+PkalO08aOl7Mn7+ kiPlvnUni+5cXNT2WlmhnRE9J5puCS1G0XMQVDf2W/mDbnNXoudkWsJXSdqWMY45jwzt hojZ2KQtxmX+/A9o8siOgXgrqWEaROqnUK0nA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684224308; x=1686816308; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ApWxcQmz7KVlqjwqFr0G5ziyjKDyN2vbRBPSrxD8FOk=; b=UKh44KyuriSVCPN1SCzl85EVF5JHd1lmKR/jV4jKzIIYnYZd3OOKA4O4VQiqsGfPry FNM6odGJPbRPtv7xbuYBtWQb22KlSBuXDZPm978E5LZiZg2meEzCi9UIDN1H2t31zs2o r38kXOZpL28vljiB/U85Q+EHHp+wNRxLfjU0GQPcpjN1uisPybXJ2A2OC7diUuL0ygpe YXhq6rVcZp5SS0ggle+s/H68mKdE+Gx0lTUy6g+2KTXMqjJjGQ5lp7MCZ+7wql2EMMle 7kPdgChu5JoTbxqckGNn+OxoN+7tHryfKobg+QP2WM+BCzOHHyXFlsuLxp71lQJmQcO0 r9gw== X-Gm-Message-State: AC+VfDweRA9QXKwkzFjgWuzy0YW7P3odTo0f5pWCJiG/kWRv6HaMn6mP yEnFM02daAIuHgEvD3uG1MA/52hxsTrf9CSx38oYTQ== X-Google-Smtp-Source: ACHHUZ7+ZsXKe4OzadC7O6u390DSP/aHvVlwNvAbndQGjgYDTL8aTcMtQC1RxUlDtUNioymZIkMZcg== X-Received: by 2002:a17:902:f548:b0:1ad:cb4b:1d50 with SMTP id h8-20020a170902f54800b001adcb4b1d50mr22055162plf.43.1684224308636; Tue, 16 May 2023 01:05:08 -0700 (PDT) Received: from chenghaoyang-low.c.googlers.com.com (27.247.221.35.bc.googleusercontent.com. [35.221.247.27]) by smtp.gmail.com with ESMTPSA id kd14-20020a17090b348e00b00252df595c01sm996881pjb.52.2023.05.16.01.05.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 May 2023 01:05:08 -0700 (PDT) X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Date: Tue, 16 May 2023 08:03:19 +0000 Message-ID: <20230516080459.711347-4-chenghaoyang@google.com> X-Mailer: git-send-email 2.40.1.606.ga4b1b128d6-goog In-Reply-To: <20230516080459.711347-1-chenghaoyang@google.com> References: <20230516080459.711347-1-chenghaoyang@google.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v5 3/3] libcamera: Add exportFrameBuffers in HeapAllocator 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: , X-Patchwork-Original-From: Harvey Yang via libcamera-devel From: Cheng-Hao Yang Reply-To: Harvey Yang Cc: Cheng-Hao Yang Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" From: Cheng-Hao Yang Add a helper function exportFrameBuffers in HeapAllocator to make it easier to use. Signed-off-by: Harvey Yang Reviewed-by: Jacopo Mondi --- include/libcamera/internal/heap_allocator.h | 9 +++ src/libcamera/heap_allocator.cpp | 61 +++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/include/libcamera/internal/heap_allocator.h b/include/libcamera/internal/heap_allocator.h index d061fdce..b6488fbc 100644 --- a/include/libcamera/internal/heap_allocator.h +++ b/include/libcamera/internal/heap_allocator.h @@ -8,12 +8,16 @@ #pragma once #include +#include #include namespace libcamera { +class Camera; +class FrameBuffer; class Heap; +class Stream; class HeapAllocator { @@ -23,7 +27,12 @@ public: bool isValid() const; UniqueFD alloc(const char *name, std::size_t size); + int exportFrameBuffers(Camera *camera, Stream *stream, + std::vector> *buffers); + private: + std::unique_ptr createBuffer(Stream *stream); + std::unique_ptr heap_; }; diff --git a/src/libcamera/heap_allocator.cpp b/src/libcamera/heap_allocator.cpp index 682a7e01..ce3a815d 100644 --- a/src/libcamera/heap_allocator.cpp +++ b/src/libcamera/heap_allocator.cpp @@ -21,6 +21,12 @@ #include +#include +#include +#include + +#include "libcamera/internal/formats.h" + namespace libcamera { /* @@ -231,4 +237,59 @@ UniqueFD HeapAllocator::alloc(const char *name, std::size_t size) return heap_->alloc(name, size); } +int HeapAllocator::exportFrameBuffers([[maybe_unused]] Camera *camera, Stream *stream, + std::vector> *buffers) +{ + unsigned int count = stream->configuration().bufferCount; + + /** \todo Support multiplanar allocations */ + + for (unsigned i = 0; i < count; ++i) { + std::unique_ptr buffer = createBuffer(stream); + if (!buffer) { + LOG(HeapAllocator, Error) << "Unable to create buffer"; + + buffers->clear(); + return -EINVAL; + } + + buffers->push_back(std::move(buffer)); + } + + return count; +} + +std::unique_ptr HeapAllocator::createBuffer(Stream *stream) +{ + std::vector planes; + + unsigned int frameSize = stream->configuration().frameSize; + int pageSize = getpagesize(); + // Allocation size need to be a direct multiple of |pageSize|. + unsigned int numPage = (frameSize + pageSize - 1) / pageSize; + + UniqueFD fd = alloc("FrameBuffer", numPage * pageSize); + if (!fd.isValid()) + return nullptr; + + auto info = PixelFormatInfo::info(stream->configuration().pixelFormat); + SharedFD shared_fd(std::move(fd)); + unsigned int offset = 0; + for (size_t i = 0; i < info.planes.size(); ++i) { + unsigned int planeSize = info.planeSize(stream->configuration().size, i); + if (planeSize == 0) + continue; + + FrameBuffer::Plane plane; + plane.fd = shared_fd; + plane.offset = offset; + plane.length = planeSize; + planes.push_back(std::move(plane)); + + offset += planeSize; + } + + return std::make_unique(planes); +} + } /* namespace libcamera */