From patchwork Tue Feb 14 09:37:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 18283 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 4D614C324E for ; Tue, 14 Feb 2023 09:37:50 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id EC854625E3; Tue, 14 Feb 2023 10:37:49 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1676367469; bh=crF47FA90nPFDW1PTQ1EIWr0ELfwIeXg28krgyX/O2k=; 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=akqZd28cUoGmz6AxSfh5LVZBVeuCoKgYWiCm3CqNwQHERRIrWd8wmuXczBodvA4Bd sdaXLjAysogi0/lViqKr+pauvaJ7j59VTQw2C/3J5dOG3uQSGXGE0bFfILu0OGHM2S M9rgnJleEI0dQci08zqgFKMF1N5DF69wuWd7EdfSqFyiv+1+4Qk1yZ6u/9PNQlGDWu 6HPbAdQGd1fbt9y51sbFg1U0gvLu8Blt3b4LsSA6mCmnKbSjZDEQAYusERrLsThNJp 16QHdUk03zU1h/peVLi5sIFME/4+uqx4SP92NmG3abuPDISFYRvu//69fIeEImePFP L7jyZAHh4J7YA== Received: from mail-pg1-x52a.google.com (mail-pg1-x52a.google.com [IPv6:2607:f8b0:4864:20::52a]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 483D5625FE for ; Tue, 14 Feb 2023 10:37:46 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="N3I7xe66"; dkim-atps=neutral Received: by mail-pg1-x52a.google.com with SMTP id x31so9859380pgl.6 for ; Tue, 14 Feb 2023 01:37:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; 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=5ed0e0o/PIWFeUY6+86mpSLl+MQcwNx1H9NaiJxqlCY=; b=N3I7xe66b7KQtpJ9MtLKmySf+wCvNanvQYSDfwIMk1naHoe3d5PONflV1Wz0tbj/+5 jzy61OBQBU1GBB8TFbCKmzcEhvGta/5KA2Py2okixFlFr1SZS13QK+0FqFVM6ePSypOD 8YvvOeJSdOHzhYKi9g96IV4epw84jd4D7BlMY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=5ed0e0o/PIWFeUY6+86mpSLl+MQcwNx1H9NaiJxqlCY=; b=eCP9bHB0zViE/4MoAQzr6JhI5MIJCTUECgLnXJFiY3ApnUXkvxVSKHnptPdN426EW9 5Ek+KsirKReTtI0grp1LZj5jxwD9YIUsvtJ4QC1muoaFG/vSHDLb72i3LrUBt7OC2MmZ 6eTb+A28zFWwbE2q0iWF53nx8DSiBUVz9qJtpTbf/BPnUXplJ3zlG/IW9BcZLqTRUDWi Nul2zadTw1vtXPyxFuhpXLW2KoMnPdRQZf560DCZxZSXGDJe5StbJgoSalekYHqPowdm y6tbOQC7+1S5EoeLKzxRQiVlpZYt/tc0e4bhCXvCWcaFhAC4DTo79ro20q0OYknpdTkO pXsQ== X-Gm-Message-State: AO0yUKW/UkeCGfiZDwu1OfODZt0ReqeYjafbCtBOfY9lkbLS0laMUFjE MDOEiKbJhmx7CguqW4QSNFqYnL7HYH4nwBV7 X-Google-Smtp-Source: AK7set9qEFc04dS2yPGmwulwmsCzVusXo0AqIb2Ys9peTGMwRkYG3LAEvyF9t0592/Xma8DF/XcSYQ== X-Received: by 2002:aa7:9ad8:0:b0:5a8:808a:d3ce with SMTP id x24-20020aa79ad8000000b005a8808ad3cemr1677167pfp.8.1676367464676; Tue, 14 Feb 2023 01:37:44 -0800 (PST) Received: from chenghaoyang-low.c.googlers.com.com (46.165.189.35.bc.googleusercontent.com. [35.189.165.46]) by smtp.gmail.com with ESMTPSA id z9-20020aa791c9000000b0059416691b64sm9696172pfa.19.2023.02.14.01.37.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Feb 2023 01:37:44 -0800 (PST) X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Date: Tue, 14 Feb 2023 09:37:36 +0000 Message-Id: <20230214093736.4067033-4-chenghaoyang@google.com> X-Mailer: git-send-email 2.39.1.581.gbfd45094c4-goog In-Reply-To: <20230214093736.4067033-1-chenghaoyang@google.com> References: <20230214093736.4067033-1-chenghaoyang@google.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 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: Harvey Yang Reply-To: Harvey Yang Cc: Harvey Yang , 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/heap_allocator.h | 10 +++++++ src/libcamera/heap_allocator.cpp | 44 ++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/include/libcamera/heap_allocator.h b/include/libcamera/heap_allocator.h index cd7ed1a3..138596de 100644 --- a/include/libcamera/heap_allocator.h +++ b/include/libcamera/heap_allocator.h @@ -8,6 +8,7 @@ #pragma once #include +#include #include @@ -15,6 +16,10 @@ namespace libcamera { +class Camera; +class Stream; +class FrameBuffer; + class HeapAllocator { public: @@ -23,7 +28,12 @@ public: bool isValid() const { return heap_->isValid(); } UniqueFD alloc(const char *name, std::size_t size); + int exportFrameBuffers(Camera *camera, Stream *stream, + std::vector> *buffers); + private: + std::unique_ptr createBuffer(std::size_t size); + std::unique_ptr heap_; }; diff --git a/src/libcamera/heap_allocator.cpp b/src/libcamera/heap_allocator.cpp index 179c2c21..1faa5585 100644 --- a/src/libcamera/heap_allocator.cpp +++ b/src/libcamera/heap_allocator.cpp @@ -17,7 +17,10 @@ #include +#include #include +#include +#include #include namespace libcamera { @@ -43,4 +46,45 @@ 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 */ + size_t size = stream->configuration().frameSize; + + for (unsigned i = 0; i < count; ++i) { + std::unique_ptr buffer = createBuffer(size); + 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(std::size_t size) +{ + std::vector planes; + + UniqueFD fd = alloc("Buffer", size); + if (!fd.isValid()) + return nullptr; + + FrameBuffer::Plane plane; + plane.fd = SharedFD(std::move(fd)); + plane.offset = 0; + plane.length = size; + + planes.push_back(std::move(plane)); + + return std::make_unique(planes); +} + } /* namespace libcamera */