From patchwork Wed Aug 2 06:51:45 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: 18923 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 69EF7C32B0 for ; Wed, 2 Aug 2023 06:55:06 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id F0EE0627EC; Wed, 2 Aug 2023 08:55:05 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1690959306; bh=EUpSCqw8ZvFk90XGcTGbyKhD5FT1P8Mlb4eRm3H2xTk=; 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=dri8xwThQ5K0iEYrcGK2FC8ue2A/hZp6UvFjwBT7rZEd3RVOzXuLCqqwO3JIlI5t+ GwhOjY9w+krNgI7UygM6OqF8IYwvGx62FBaF9HgfsnD8TF1fLWt0TEV3n1ijHkMjcY 7/bdKuV/pK4p/NxmIU88u0UE4dP1Zmj1QRXTugQPGMt8SbCSUNuRc4XNa5mRh1HIkL 3ogwakj125NRS38vbjW0tCjQy3Y6J74+3vgvN1szhA3yDP5uDxujp1NSKwGIR+QX8m Kz+G5DwT/HaBRApC42Zs65JA8sM2xRYhBaqaAYcFw6MVpUPZZxkb2zzvvrxIFic7Na r+Mn0Maz/VR9Q== 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 AB20F627E6 for ; Wed, 2 Aug 2023 08:55:02 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="HPoVEsPm"; dkim-atps=neutral Received: by mail-pf1-x433.google.com with SMTP id d2e1a72fcca58-686f0d66652so6121602b3a.2 for ; Tue, 01 Aug 2023 23:55:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1690959301; x=1691564101; 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=SR8gv5LwHmrnA+J0vzfvBFITtJUKF6RbopGQzNfWe00=; b=HPoVEsPm2QMjL2WA1bDtnATwF9JX5MvNS3MO1tRficDZRqVFiKtwg+nPbG9C6XcVje tv+0NqCvHHAb3SPU/fIGlX6lkfOmqhi5YOf7rg8rS+BIb+mZLQEKYt7SJRYmX0ZJIWbP mbhqfS8TzZeYLw6oStcZirwtuV80iAPg/VK6E= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690959301; x=1691564101; 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=SR8gv5LwHmrnA+J0vzfvBFITtJUKF6RbopGQzNfWe00=; b=dfDin6EqlcJISn0vmho+jzGPUeluLzregCSRh998kWgn+eg4RoJpKYc2deGG/8P1UP uvZfVDHsA1UATr7w+UtzMh9n0KatAnJSJ6rvih8hcqlLI5yZW1pr13HZ68j5Ve1Cymy9 NczNt8kk4WOROaIf/1AHds81DqCxYmRiqJRDzY2dshWm405w2sAkgrbtNvoNw1XyjOeC dlDWBHC4JuYaLP8IduKTvEMjyC1vE5q9vXSfptiAIV8JKgu/wfPSeqg/RNHm93jevI8X VmPptB9VRk+w4SOE93bB1kP2fYkNAy00zKfyxJCvP+ulphAYRvTKv/qKotoAlkrsUPtP HlTQ== X-Gm-Message-State: ABy/qLYyNMNmOmC5IhAq+HiWnSeKFXZLb5ppFT0pYiib3ZrJwAaQh24+ TlMdLyrYeRLHzaS8aqO+Hbtv+FH+/W8lfslKPMU= X-Google-Smtp-Source: APBJJlHBsefdblBYvxLdcESSOQ719nUlWQC9SiI7nSQIHHojOT2Sb8AuVMyUI2u/CeSK7d/Pn+X8ng== X-Received: by 2002:a05:6a21:6d95:b0:134:4f86:7966 with SMTP id wl21-20020a056a216d9500b001344f867966mr19325429pzb.9.1690959300640; Tue, 01 Aug 2023 23:55:00 -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 d14-20020aa7868e000000b0065980654baasm9142154pfo.130.2023.08.01.23.54.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Aug 2023 23:55:00 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Wed, 2 Aug 2023 06:51:45 +0000 Message-ID: <20230802065449.2904457-4-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.41.0.585.gd2178a4bd4-goog In-Reply-To: <20230802065449.2904457-1-chenghaoyang@chromium.org> References: <20230802065449.2904457-1-chenghaoyang@chromium.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v7 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: Harvey Yang Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Add a helper function exportFrameBuffers in HeapAllocator to make it easier to use. Signed-off-by: Harvey Yang --- include/libcamera/internal/heap_allocator.h | 10 ++++ src/libcamera/heap_allocator.cpp | 60 ++++++++++++++++++++- 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/include/libcamera/internal/heap_allocator.h b/include/libcamera/internal/heap_allocator.h index 6620fa23..064696d5 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; +struct StreamConfiguration; class HeapAllocator { @@ -25,7 +29,13 @@ public: bool isValid() const; UniqueFD alloc(const char *name, std::size_t size); + int exportFrameBuffers( + const StreamConfiguration &config, + std::vector> *buffers); + private: + std::unique_ptr createBuffer(const StreamConfiguration &config); + std::unique_ptr heap_; }; diff --git a/src/libcamera/heap_allocator.cpp b/src/libcamera/heap_allocator.cpp index 1caa1469..a1dba155 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" + /** * \file libcamera/internal/heap_allocator.h * \brief Internal heap buffer allocator support @@ -291,7 +297,59 @@ UniqueFD HeapAllocator::alloc(const char *name, std::size_t size) return {}; } - return heap_->alloc(name, size); + std::size_t pageSize = getpagesize(); + /* Allocation size needs to be a direct multiple of pageSize. */ + std::size_t numPage = (size + pageSize - 1) / pageSize; + + return heap_->alloc(name, numPage * pageSize); +} + +int HeapAllocator::exportFrameBuffers( + const StreamConfiguration &config, + std::vector> *buffers) +{ + unsigned int count = config.bufferCount; + + for (unsigned i = 0; i < count; ++i) { + std::unique_ptr buffer = createBuffer(config); + 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( + const StreamConfiguration &config) +{ + std::vector planes; + + auto info = PixelFormatInfo::info(config.pixelFormat); + for (size_t i = 0; i < info.planes.size(); ++i) { + unsigned int planeSize = info.planeSize(config.size, i); + if (planeSize == 0) + continue; + + UniqueFD fd = alloc("FrameBuffer", planeSize); + if (!fd.isValid()) + return nullptr; + + SharedFD sharedFd(std::move(fd)); + + FrameBuffer::Plane plane; + plane.fd = sharedFd; + plane.offset = 0; + plane.length = planeSize; + planes.push_back(std::move(plane)); + } + + return std::make_unique(planes); } } /* namespace libcamera */