From patchwork Mon May 22 08:35:08 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: 18645 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 1E4DDC31E9 for ; Mon, 22 May 2023 08:36:01 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id E0F8E62865; Mon, 22 May 2023 10:36:00 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1684744560; bh=0MRzzNK7YdDOow/alZSUpvn6S0XNOZGqFTYfaZmht7Q=; 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=ceph6V0j3ZB5Uj5NN/DWQkJdOKXSkoY8CrCz2GmycpOTT2J5IV9OgkSI+NBWh9AUH Jt0S7VqPWeYrcb3a0uNnlBzQULE82aoQWpHMc9IpeINfBcSE+8jNk4QNNMS+i/OjgU WCGhNaLHHTDiK1OuXZimEmsQCJ7HJrjO33XVzKX7SGO6yYMpIyUwWXW8lYQYUg53C4 uUAJNk5l6lEUyDZWXUM4O6Cmb/8KRFZpOKlV5aQAR7EZBRysrssdgJkHy47EJIW+g3 R1jCEXNkJQpQldn2sYJ/uBICPPlW5UIQNfc/H1+2h6gNrm8smOxIgUl2ekZoAvn/IY dRNnVWpeUWsHw== 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 55FDE627DC for ; Mon, 22 May 2023 10:35:56 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="Ujkpgv9T"; dkim-atps=neutral Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-1ae875bf125so18966145ad.1 for ; Mon, 22 May 2023 01:35:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1684744555; x=1687336555; 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=+HAMbjUgljEg+SmXq+kpb1omhD43ul+/Dp5+qYCcWhE=; b=Ujkpgv9TlHTrh0RLXWBZ/g+AFH5Cx3g9BE0qnPS743yMJLadkzW9ldq7HG6URAImqD 1Gn1cHfS8Tc7uEC5P8IxlcYYPE7DPXicSBON/aIFdgQG8tzzOYbNyxaJ85VS3RdGay6R HTm4tTG09D43Mjn+Y9Zvu8H0JS35GJbbZV5K8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684744555; x=1687336555; 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=+HAMbjUgljEg+SmXq+kpb1omhD43ul+/Dp5+qYCcWhE=; b=PbAfys0MhIot66qcjl4i9AAF7OwYN3T83biyIcy9zVOd5QDLvADSpJgcy9GwsI9v/k KrrMvwuKJNlM1R/11dAj7OtdlXhTXDMfuklIuCX0waXQ+jflrffDSyXWQMhvQH5FTYQp W3ONBKkNkdnPuHkcHRxU0Ka4RXDPne12cXlFRZROi2BuMqJ34tTmBFRusVJcN5PO2rff rZlokqDIM0eNdsTXeeV6vV7wsDIawGiRzgTg6DnCsMCaiTazbV8/yv/FsMctpeE3PhkD ODX0pveUn2mmq/Hay7OivGEl10yWoZtKkF3JPirMIwNvdAYolR/qmXMnX+GX+O5xsr64 hh3w== X-Gm-Message-State: AC+VfDwcKQ9h5oBMWRT5K1mZu581ZnlKy4+1jLTgJ5hp7siN3+lKuxvc BCF5YEVJfJv63QBvmEXjArtAXeFXyhqhKsTPNCg= X-Google-Smtp-Source: ACHHUZ7aNPY/xYWoAJ2jN6H6QWE0J1AAZS8YAu4J7MeSaGOoNpQE37e1JqaKWYH7bjUiOeGR6PBO4Q== X-Received: by 2002:a17:902:e889:b0:1ac:753a:7933 with SMTP id w9-20020a170902e88900b001ac753a7933mr11982573plg.39.1684744554666; Mon, 22 May 2023 01:35:54 -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 h1-20020a170902f54100b001a980a23804sm4321253plf.4.2023.05.22.01.35.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 May 2023 01:35:54 -0700 (PDT) X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Date: Mon, 22 May 2023 08:35:08 +0000 Message-ID: <20230522083546.2465448-4-chenghaoyang@google.com> X-Mailer: git-send-email 2.40.1.698.g37aff9b760-goog In-Reply-To: <20230522083546.2465448-1-chenghaoyang@google.com> References: <20230522083546.2465448-1-chenghaoyang@google.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v6 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 --- include/libcamera/internal/heap_allocator.h | 9 +++ src/libcamera/heap_allocator.cpp | 62 +++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/include/libcamera/internal/heap_allocator.h b/include/libcamera/internal/heap_allocator.h index 92d4488a..92beaaa4 100644 --- a/include/libcamera/internal/heap_allocator.h +++ b/include/libcamera/internal/heap_allocator.h @@ -9,12 +9,16 @@ #pragma once #include +#include #include namespace libcamera { +class Camera; +class FrameBuffer; class Heap; +class Stream; class HeapAllocator { @@ -24,7 +28,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 8f99f590..e99cdbe5 100644 --- a/src/libcamera/heap_allocator.cpp +++ b/src/libcamera/heap_allocator.cpp @@ -22,6 +22,12 @@ #include +#include +#include +#include + +#include "libcamera/internal/formats.h" + namespace libcamera { /* @@ -227,4 +233,60 @@ 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; + + /** \todo We don't support allocating buffers with a dedicated fd per plane */ + 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 */