From patchwork Tue Oct 1 08:10:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 21446 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 4C8D4C3257 for ; Tue, 1 Oct 2024 08:18:35 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id DFD1F6351C; Tue, 1 Oct 2024 10:18:34 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="NKIH6+KU"; dkim-atps=neutral Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 429F36351A for ; Tue, 1 Oct 2024 10:18:32 +0200 (CEST) Received: by mail-pf1-x429.google.com with SMTP id d2e1a72fcca58-7198de684a7so3615195b3a.2 for ; Tue, 01 Oct 2024 01:18:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1727770710; x=1728375510; 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=YbYwf988nJOwEwXgyU/GP/nmEOf36C/2R8QZli4loEE=; b=NKIH6+KULjWMXXlmC8MC65QFUZyetoCMEDoAA5LXK5zXjo0B10X/zlAtL6GEVBqkC2 y8w45uKenBPUsWBKSPuQPTzKVXL7pY90zqwrkgjBMBjWmkp76sHq/9J6kXrVlrxzQ9/6 rwZYi/j73lS4XKAt+semWhHLyP/jA6/1RxxPM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727770710; x=1728375510; 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=YbYwf988nJOwEwXgyU/GP/nmEOf36C/2R8QZli4loEE=; b=PTJh25RtHbASletYLR8j+AVulFAE3oTDgmuVGKn2+Cg8yFwTe1+k8WL4HHEzEBfjzt xobcF/D0DwtV+IMoJ8MCml+iGNHQNFiYLkMplrH4Hmdv7H7RsRyabTbKXflT/SAO29nP hmVmdcoHRQmC91zdu9/aKZVUGEMibax+LtdoVLqkfDppKe/BFvdsMfr5xXccKO6Y/zCf IJHOiFgmajWXsqhkprWa5HNAmRRzv8nnylZLyvPOPvFhaFIn6XDNOtcKCWk1iRoDJnjI +KDlk+nLk84RLcgBj/gsfddpBNBPSAM0HC55s/y0chdt261eah+1Qst5e+xAnijHDoDw 9RAw== X-Gm-Message-State: AOJu0Yzr15IuhQHp7p99be3/Gf9DxzMBRJrt6RCIvyOvYsC+5dKqhwhb TnsQEnKkpqCSVMHdybINJssN8jHivrL4NlCOq1OtvedYCROKvefH2RbbjWK2Rw7QgnxGNzlFR3Y = X-Google-Smtp-Source: AGHT+IEuwfJrQvVJ4e3gvMd0z2rc1T7LxStprb4Nz23ypZ170jVassoJuYZUBSihGOe3vVwwg4NQrw== X-Received: by 2002:a05:6a00:194f:b0:70e:a524:6490 with SMTP id d2e1a72fcca58-71b25f0ab85mr20411882b3a.1.1727770709970; Tue, 01 Oct 2024 01:18:29 -0700 (PDT) Received: from chenghaoyang-low.c.googlers.com.com (208.158.221.35.bc.googleusercontent.com. [35.221.158.208]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7e6db17c3b8sm7941186a12.0.2024.10.01.01.18.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Oct 2024 01:18:29 -0700 (PDT) From: Harvey Yang X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Han-Lin Chen , Hanlin Chen , Harvey Yang Subject: [PATCH 1/1] libcamera: Add mailbox template helper Date: Tue, 1 Oct 2024 08:10:08 +0000 Message-ID: <20241001081824.960009-2-chenghaoyang@google.com> X-Mailer: git-send-email 2.46.1.824.gd892dcdcdd-goog In-Reply-To: <20241001081824.960009-1-chenghaoyang@google.com> References: <20241001081824.960009-1-chenghaoyang@google.com> 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: Hanlin Chen Co-developed-by: Harvey Yang --- include/libcamera/internal/mailbox.h | 50 +++++++++++++ include/libcamera/internal/meson.build | 1 + src/libcamera/mailbox.cpp | 97 ++++++++++++++++++++++++++ src/libcamera/meson.build | 1 + 4 files changed, 149 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 00000000..fb27f5bb --- /dev/null +++ b/include/libcamera/internal/mailbox.h @@ -0,0 +1,50 @@ +/* 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 + +namespace libcamera { + +template +class MailBox +{ +public: + using Recycler = std::function; + + MailBox() + : valid_(false) {} + ~MailBox(); + + void put(const T &item, Recycler recycler); + + const T &get(); + + 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); + +} /* namespace libcamera */ diff --git a/include/libcamera/internal/meson.build b/include/libcamera/internal/meson.build index 1c5eef9c..4c1228b9 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 00000000..4d129224 --- /dev/null +++ b/src/libcamera/mailbox.cpp @@ -0,0 +1,97 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2024, Google Inc. + * + * mailbox.cpp - Template class for generic mailbox + */ + +#include "libcamera/internal/mailbox.h" + +#include + +namespace libcamera { + +/** + * \class 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 + */ + +template +MailBox::~MailBox() +{ + if (valid_ && recycler_) + recycler_(item_); +} + +/** + * \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. + */ +template +void MailBox::put(const T &item, MailBox::Recycler recycler) +{ + ASSERT(!valid_); + + valid_ = true; + recycler_ = recycler; + item_ = item; +} + +/** + * \brief Get the data as a consumer. put() function should be called ahead + * + * \return The stored data + */ +template +const T &MailBox::get() +{ + ASSERT(valid_); + return item_; +} + +/** + * \fn MailBox::valid() + * \return True if put() function has been called + */ + +/** + * \typedef SharedMailBox + * \brief A mailbox as a shared_ptr + */ + +/** + * \fn makeMailBox() + * \brief A helper function to create a SharedMailBox + * + * \return A mailbox as a SharedMailBox + */ + +/** + * \brief A helper function to create a list of mailboxes + * \param[in] count The number of mailboxes requested + * + * \return Mailboxes as a vector + */ +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/src/libcamera/meson.build b/src/libcamera/meson.build index aa9ab029..6c3ef537 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',