From patchwork Tue Mar 28 09:55:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 18488 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 D4733C329C for ; Tue, 28 Mar 2023 09:55:45 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 8EC766275E; Tue, 28 Mar 2023 11:55:44 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1679997344; bh=rKkP6APJoA9gPyVjCqIRpYkSaFXNsNkeFUkaXQOrw5A=; 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=hIaOq6Nzy8sBj8Jab8NxHU4b4rSy1sXjc8vD8x6A8vDz6GuQ7VD3CNzzLOrTWfG7a 8gt0OMdQgh1rn2p2Ft1gtXoLhW0vf6ClPVgwN8EYATVb3axauW9hjL8i+b60aiox3F 6rfnJ0E6dcK7H1T7mZEJ54rQINo/IKs1BDK4W6zzxADEvgpLVJXv7AhJ/g8p+GBHUW C5n1BkfXJVpMY/I7+KD9RMS6GzqfRANgQmZpcEmizZzKFWJ6iCLY2PjoMAS50hYYwF xN/tsipGI6moa+Fn1wqizgN0foyEwELKj/GmTu0QW+kmWP8YW0NeZxpAt71VkOcdyT LuD/u3GV//iRg== Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 351246273D for ; Tue, 28 Mar 2023 11:55:42 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="WuXRtnJd"; dkim-atps=neutral Received: by mail-pj1-x1031.google.com with SMTP id d13so10330041pjh.0 for ; Tue, 28 Mar 2023 02:55:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1679997340; 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=AnZvnxHR0i8wKScqjrDBwNbGDayWUHwb6jiPkaTvz0s=; b=WuXRtnJdkWQE388DhJlwPb2HMuu4Ojd0UtJO4Zi4sPHobgT8qekCJKxiwEPF8w3kmw h9AaLN+G+BvjqGfsA6Fx4u6IIbb18dYyorfurMPan47j9tx/wqX96mrd7DAqYvsqbF3r DlzRyqzLg4qOhwQordUEEy4Y1oBYBD0xE3vMQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679997340; 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=AnZvnxHR0i8wKScqjrDBwNbGDayWUHwb6jiPkaTvz0s=; b=4S7jxACPLJ5tKk+1pS8KpY35OolOxaIZQoUA6eSOQHr/L8znBHgf8aWh7kxNiKf4t0 g/1RLKTs2HxpgbsPVQuOG/jqG6LRx5Kkgi5SStYew8/uJLOd8lgnVAUUjBo2gdTQbmQn YaliYwwwbmqrps21ae4hVM+dhVZ9DOh09sWZZLrgDbEFj3blbnTFU72+5RhDjTc7FX4P wpaJDCfosv8P+7sRicvYV7m9DmhcqGlxZ/mtdn+WJw+5gi8mvd03wkaroywzMucg6tdF maD7xragTnc29ZnKqwa8W1J6vwqg1zd2F3xu1dbLj1UiHLAv5ZS2kcbmoAWroK/ZvJv/ +JUQ== X-Gm-Message-State: AO0yUKVw59+hwthOo1RQFQPQ7E/dVgM28ABCOF+kkGJjPripPhukWARl pJ0ZPIQNPGlyyNAJi2lrnIZCXuwnP/lsT61rB6DqNg== X-Google-Smtp-Source: AK7set9R3fnmT4Ti0KtDADvAI7yNjueZeEEZ2DHW/pw9FSTJpxs+pFOKay9p2EZ2m0jSevSPnkDbzA== X-Received: by 2002:a05:6a20:7187:b0:d9:f086:e756 with SMTP id s7-20020a056a20718700b000d9f086e756mr13977925pzb.39.1679997340440; Tue, 28 Mar 2023 02:55:40 -0700 (PDT) Received: from harveyyang.localdomain (1-163-26-93.dynamic-ip.hinet.net. [1.163.26.93]) by smtp.gmail.com with ESMTPSA id v21-20020aa78515000000b006259beddb63sm20241488pfn.44.2023.03.28.02.55.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 02:55:40 -0700 (PDT) X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Date: Tue, 28 Mar 2023 17:55:34 +0800 Message-Id: <20230328095534.3584-4-harveyycyang@gmail.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230328095534.3584-1-harveyycyang@gmail.com> References: <20230328095534.3584-1-harveyycyang@gmail.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 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: 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/heap_allocator.h | 11 ++++++ src/libcamera/heap_allocator.cpp | 56 ++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/include/libcamera/heap_allocator.h b/include/libcamera/heap_allocator.h index cd7ed1a3..076f0951 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,11 @@ namespace libcamera { +class Camera; +class Stream; +class FrameBuffer; +class PixelFormatInfo; + class HeapAllocator { public: @@ -23,7 +29,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(Stream *stream); + std::unique_ptr heap_; }; diff --git a/src/libcamera/heap_allocator.cpp b/src/libcamera/heap_allocator.cpp index 179c2c21..33e9eaca 100644 --- a/src/libcamera/heap_allocator.cpp +++ b/src/libcamera/heap_allocator.cpp @@ -17,7 +17,11 @@ #include +#include #include +#include +#include +#include #include namespace libcamera { @@ -43,4 +47,56 @@ 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; + + int num_page = (stream->configuration().frameSize + getpagesize() - 1) / getpagesize(); + + UniqueFD fd = alloc("Buffer", num_page * getpagesize()); + if (!fd.isValid()) + return nullptr; + + auto info = PixelFormatInfo::info(stream->configuration().pixelFormat); + SharedFD shared_fd(std::move(fd)); + unsigned int offset = 0; + for (long unsigned int 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 */