From patchwork Wed Oct 9 07:41:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cheng-Hao Yang X-Patchwork-Id: 21553 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 F09F7C32DE for ; Wed, 9 Oct 2024 07:46:54 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 65DF06536A; Wed, 9 Oct 2024 09:46:52 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="K3IoTonb"; dkim-atps=neutral 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 94C1B6353B for ; Wed, 9 Oct 2024 09:46:50 +0200 (CEST) Received: by mail-pj1-x1031.google.com with SMTP id 98e67ed59e1d1-2e09f67bc39so5423308a91.1 for ; Wed, 09 Oct 2024 00:46:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1728460009; x=1729064809; darn=lists.libcamera.org; 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=tIeh9OOzrh2fB+2XUMZ2I8IIwRldeYy5fiX65sGidRY=; b=K3IoTonbKXVZVnCTps3bei1Av+yL3ND3XuYBB67RjM7JqcyNCR5AC2NI9+x6VtRFFc c2qrsUSaHmysUHHEEVCl+6RaJR5DDx+EvW56W9TXODQQJJWP9M2pc4C1W4paSOal+/0g 7lsMC1e59Kh/sd1cK+lBoR+ssCShN8vDRtuO8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728460009; x=1729064809; 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=tIeh9OOzrh2fB+2XUMZ2I8IIwRldeYy5fiX65sGidRY=; b=xBrBWaXWycfdGUMdfVOxjksOu7mSSP2uUW7533Txm4ztqDgc4tbIAbVkiN53G0RMRO UXpf9clNrXLmxH0Xdpk2CzndoomiU0IbUUtWnjGYdmz/bXNKTCRYvg9CEQ17zf9c+CO6 DOWCT7QbyylL/nx8nWwwsKrb0M7yhmp6NWBr8fbpNgO1A6L5s4NQ0wYIUZRovvsWuXzr UvAIfcp+ucJw+s5NBphgzkdX5EFXPN07twVJ2n6t/R8gbGnnk3DsAyUZMYozpyRyJ0Ep QYZs4FUj0zxEN+qtswzS9mp0ROlQwjrLR7MGdt8u5wk3jWrgBSsWcQ8//yNqZnfGFjdP wVxQ== X-Gm-Message-State: AOJu0YzaV6yqH390UcE71BUyfQ0DXYz8fUSR/T+zMsH1+6TFrif5f9A/ 4PhP7YWnD74WlWR4UbqYAbX3jecooJ58W1WPq5z3hNuvxo/bExFxL0Skax3FMZrqGapU/w5XX2o = X-Google-Smtp-Source: AGHT+IE1MbJ1uLEKY4uI/8OoZeYfydbkVINyPrZ/at025/EE0D2okHXPplA6juoqYHbAhWt5Iw423Q== X-Received: by 2002:a17:90a:46ca:b0:2d3:cd27:c480 with SMTP id 98e67ed59e1d1-2e2a25519aamr2098551a91.33.1728460008755; Wed, 09 Oct 2024 00:46:48 -0700 (PDT) Received: from chenghaoyang-low.c.googlers.com.com (199.211.81.34.bc.googleusercontent.com. [34.81.211.199]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e2a57077c2sm944414a91.17.2024.10.09.00.46.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Oct 2024 00:46:48 -0700 (PDT) From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Han-Lin Chen , Harvey Yang Subject: [PATCH 1/5] libcamera: Add mailbox template helper Date: Wed, 9 Oct 2024 07:41:19 +0000 Message-ID: <20241009074642.2965791-2-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog In-Reply-To: <20241009074642.2965791-1-chenghaoyang@chromium.org> References: <20241009074642.2965791-1-chenghaoyang@chromium.org> MIME-Version: 1.0 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: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" From: Han-Lin Chen MailBox is designed to be used to shares data, usually a frame buffer, among tasks. A Mailbox can store a data which the producer task should set into, and multiple consumer tasks could read the data from it. The data could be set only once, i.e., there should be only one producer. All tasks shares the Mailbox with share_ptr and the data it contains should be recycled when all tasks are finished. Signed-off-by: Han-Lin Chen Co-developed-by: Harvey Yang Signed-off-by: Harvey Yang --- include/libcamera/internal/mailbox.h | 75 ++++++++++++++++++++++++++ include/libcamera/internal/meson.build | 1 + src/libcamera/mailbox.cpp | 60 +++++++++++++++++++++ src/libcamera/meson.build | 1 + 4 files changed, 137 insertions(+) create mode 100644 include/libcamera/internal/mailbox.h create mode 100644 src/libcamera/mailbox.cpp diff --git a/include/libcamera/internal/mailbox.h b/include/libcamera/internal/mailbox.h new file mode 100644 index 000000000..4c76eade9 --- /dev/null +++ b/include/libcamera/internal/mailbox.h @@ -0,0 +1,75 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2024, Google Inc. + * + * mailbox.h - Template class for generic mailbox + */ + +#pragma once + +#include +#include +#include + +#include + +namespace libcamera { + +template +class MailBox +{ +public: + using Recycler = std::function; + + MailBox() + : valid_(false) {} + ~MailBox() + { + if (valid_ && recycler_) + recycler_(item_); + } + + void put(const T &item, Recycler recycler) + { + ASSERT(!valid_); + + valid_ = true; + recycler_ = recycler; + item_ = item; + } + + const T &get() + { + ASSERT(valid_); + return item_; + } + + bool valid() { return valid_; } + +private: + T item_; + bool valid_; + std::function recycler_; +}; + +template +using SharedMailBox = std::shared_ptr>; + +template +SharedMailBox makeMailBox() +{ + return std::make_shared>(); +} + +template +std::vector> makeMailBoxVector(const unsigned int count) +{ + std::vector> mailBoxes; + mailBoxes.resize(count); + for (unsigned int i = 0; i < count; i++) + mailBoxes[i] = makeMailBox(); + + return mailBoxes; +} + +} /* namespace libcamera */ diff --git a/include/libcamera/internal/meson.build b/include/libcamera/internal/meson.build index 1c5eef9ca..4c1228b95 100644 --- a/include/libcamera/internal/meson.build +++ b/include/libcamera/internal/meson.build @@ -27,6 +27,7 @@ libcamera_internal_headers = files([ 'ipa_proxy.h', 'ipc_pipe.h', 'ipc_unixsocket.h', + 'mailbox.h', 'mapped_framebuffer.h', 'media_device.h', 'media_object.h', diff --git a/src/libcamera/mailbox.cpp b/src/libcamera/mailbox.cpp new file mode 100644 index 000000000..7335122e3 --- /dev/null +++ b/src/libcamera/mailbox.cpp @@ -0,0 +1,60 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2024, Google Inc. + * + * mailbox.cpp - Template class for generic mailbox + */ + +/** + * \class libcamera::MailBox + * \brief MailBox of data + * + * A MailBox contains a block of data that has a single producer, and one or + * multiple consumers. It's often used as a shared_ptr (SharedMailBox) and + * being held by different tasks in pipelines. + */ + +/** + * \typedef MailBox::Recycler + * \brief A function that recycles the data when the mailbox is destructed + */ + +/** + * \fn MailBox::put(const T &item, MailBox::Recycler recycler) + * \brief Set the data as the producer. Should be called only once + * \param[in] item The data to be stored + * \param[in] recycler The function that recycles \a data when destructing the + * mailbox. Mostly used for recycling buffers. + */ + +/** + * \fn MailBox::get() + * \brief Get the data as a consumer. put() function should be called ahead + * + * \return The stored data + */ + +/** + * \fn MailBox::valid() + * \return True if put() function has been called + */ + +/** + * \typedef libcamera::SharedMailBox + * \brief A mailbox as a shared_ptr + */ + +/** + * \fn libcamera::makeMailBox() + * \brief A helper function to create a SharedMailBox + * + * \return A mailbox as a SharedMailBox + */ + +/** + * \fn libcamera::makeMailBoxVector(const unsigned int count) + * \brief A helper function to create a list of mailboxes + * \param[in] count The number of mailboxes requested + * + * \return Mailboxes as a vector + */ diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build index aa9ab0291..6c3ef5377 100644 --- a/src/libcamera/meson.build +++ b/src/libcamera/meson.build @@ -38,6 +38,7 @@ libcamera_internal_sources = files([ 'ipc_pipe.cpp', 'ipc_pipe_unixsocket.cpp', 'ipc_unixsocket.cpp', + 'mailbox.cpp', 'mapped_framebuffer.cpp', 'media_device.cpp', 'media_object.cpp',