Show a patch.

GET /api/1.1/patches/21446/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 21446,
    "url": "https://patchwork.libcamera.org/api/1.1/patches/21446/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/21446/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/1.1/projects/1/?format=api",
        "name": "libcamera",
        "link_name": "libcamera",
        "list_id": "libcamera_core",
        "list_email": "libcamera-devel@lists.libcamera.org",
        "web_url": "",
        "scm_url": "",
        "webscm_url": ""
    },
    "msgid": "<20241001081824.960009-2-chenghaoyang@google.com>",
    "date": "2024-10-01T08:10:08",
    "name": "[1/1] libcamera: Add mailbox template helper",
    "commit_ref": null,
    "pull_url": null,
    "state": "new",
    "archived": false,
    "hash": "f296b48619c291eb56816ec1a4110d47c014a4f5",
    "submitter": {
        "id": 117,
        "url": "https://patchwork.libcamera.org/api/1.1/people/117/?format=api",
        "name": "Cheng-Hao Yang",
        "email": "chenghaoyang@chromium.org"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/21446/mbox/",
    "series": [
        {
            "id": 4643,
            "url": "https://patchwork.libcamera.org/api/1.1/series/4643/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=4643",
            "date": "2024-10-01T08:10:07",
            "name": "Add MailBox",
            "version": 1,
            "mbox": "https://patchwork.libcamera.org/series/4643/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/21446/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/21446/checks/",
    "tags": {},
    "headers": {
        "Return-Path": "<libcamera-devel-bounces@lists.libcamera.org>",
        "X-Original-To": "parsemail@patchwork.libcamera.org",
        "Delivered-To": "parsemail@patchwork.libcamera.org",
        "Received": [
            "from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 4C8D4C3257\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue,  1 Oct 2024 08:18:35 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id DFD1F6351C;\n\tTue,  1 Oct 2024 10:18:34 +0200 (CEST)",
            "from mail-pf1-x429.google.com (mail-pf1-x429.google.com\n\t[IPv6:2607:f8b0:4864:20::429])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 429F36351A\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  1 Oct 2024 10:18:32 +0200 (CEST)",
            "by mail-pf1-x429.google.com with SMTP id\n\td2e1a72fcca58-7198de684a7so3615195b3a.2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 01 Oct 2024 01:18:32 -0700 (PDT)",
            "from chenghaoyang-low.c.googlers.com.com\n\t(208.158.221.35.bc.googleusercontent.com. [35.221.158.208])\n\tby smtp.gmail.com with ESMTPSA id\n\t41be03b00d2f7-7e6db17c3b8sm7941186a12.0.2024.10.01.01.18.28\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tTue, 01 Oct 2024 01:18:29 -0700 (PDT)"
        ],
        "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=chromium.org header.i=@chromium.org\n\theader.b=\"NKIH6+KU\"; dkim-atps=neutral",
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=chromium.org; s=google; t=1727770710; x=1728375510;\n\tdarn=lists.libcamera.org; \n\th=content-transfer-encoding:mime-version:references:in-reply-to\n\t:message-id:date:subject:cc:to:from:from:to:cc:subject:date\n\t:message-id:reply-to;\n\tbh=YbYwf988nJOwEwXgyU/GP/nmEOf36C/2R8QZli4loEE=;\n\tb=NKIH6+KULjWMXXlmC8MC65QFUZyetoCMEDoAA5LXK5zXjo0B10X/zlAtL6GEVBqkC2\n\ty8w45uKenBPUsWBKSPuQPTzKVXL7pY90zqwrkgjBMBjWmkp76sHq/9J6kXrVlrxzQ9/6\n\trwZYi/j73lS4XKAt+semWhHLyP/jA6/1RxxPM=",
        "X-Google-DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20230601; t=1727770710; x=1728375510;\n\th=content-transfer-encoding:mime-version:references:in-reply-to\n\t:message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc\n\t:subject:date:message-id:reply-to;\n\tbh=YbYwf988nJOwEwXgyU/GP/nmEOf36C/2R8QZli4loEE=;\n\tb=PTJh25RtHbASletYLR8j+AVulFAE3oTDgmuVGKn2+Cg8yFwTe1+k8WL4HHEzEBfjzt\n\txobcF/D0DwtV+IMoJ8MCml+iGNHQNFiYLkMplrH4Hmdv7H7RsRyabTbKXflT/SAO29nP\n\thmVmdcoHRQmC91zdu9/aKZVUGEMibax+LtdoVLqkfDppKe/BFvdsMfr5xXccKO6Y/zCf\n\tIJHOiFgmajWXsqhkprWa5HNAmRRzv8nnylZLyvPOPvFhaFIn6XDNOtcKCWk1iRoDJnjI\n\t+KDlk+nLk84RLcgBj/gsfddpBNBPSAM0HC55s/y0chdt261eah+1Qst5e+xAnijHDoDw\n\t9RAw==",
        "X-Gm-Message-State": "AOJu0Yzr15IuhQHp7p99be3/Gf9DxzMBRJrt6RCIvyOvYsC+5dKqhwhb\n\tTnsQEnKkpqCSVMHdybINJssN8jHivrL4NlCOq1OtvedYCROKvefH2RbbjWK2Rw7QgnxGNzlFR3Y\n\t=",
        "X-Google-Smtp-Source": "AGHT+IEuwfJrQvVJ4e3gvMd0z2rc1T7LxStprb4Nz23ypZ170jVassoJuYZUBSihGOe3vVwwg4NQrw==",
        "X-Received": "by 2002:a05:6a00:194f:b0:70e:a524:6490 with SMTP id\n\td2e1a72fcca58-71b25f0ab85mr20411882b3a.1.1727770709970; \n\tTue, 01 Oct 2024 01:18:29 -0700 (PDT)",
        "From": "Harvey Yang <chenghaoyang@chromium.org>",
        "X-Google-Original-From": "Harvey Yang <chenghaoyang@google.com>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Cc": "Han-Lin Chen <hanlinchen@chromium.org>,\n\tHanlin Chen <hanlinchen@google.com>,\n\tHarvey Yang <chenghaoyang@chromium.org>",
        "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",
        "Content-Transfer-Encoding": "8bit",
        "X-BeenThere": "libcamera-devel@lists.libcamera.org",
        "X-Mailman-Version": "2.1.29",
        "Precedence": "list",
        "List-Id": "<libcamera-devel.lists.libcamera.org>",
        "List-Unsubscribe": "<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>",
        "List-Archive": "<https://lists.libcamera.org/pipermail/libcamera-devel/>",
        "List-Post": "<mailto:libcamera-devel@lists.libcamera.org>",
        "List-Help": "<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>",
        "List-Subscribe": "<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>",
        "Errors-To": "libcamera-devel-bounces@lists.libcamera.org",
        "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"
    },
    "content": "From: Han-Lin Chen <hanlinchen@chromium.org>\n\nMailBox is designed to be used to shares data, usually a frame buffer, among\ntasks. A Mailbox can store a data which the producer task should set into,\nand multiple consumer tasks could read the data from it. The data could\nbe set only once, i.e., there should be only one producer. All tasks shares\nthe Mailbox with share_ptr and the data it contains should be recycled when\nall tasks are finished.\n\nSigned-off-by: Hanlin Chen <hanlinchen@google.com>\nCo-developed-by: Harvey Yang <chenghaoyang@chromium.org>\n---\n include/libcamera/internal/mailbox.h   | 50 +++++++++++++\n include/libcamera/internal/meson.build |  1 +\n src/libcamera/mailbox.cpp              | 97 ++++++++++++++++++++++++++\n src/libcamera/meson.build              |  1 +\n 4 files changed, 149 insertions(+)\n create mode 100644 include/libcamera/internal/mailbox.h\n create mode 100644 src/libcamera/mailbox.cpp",
    "diff": "diff --git a/include/libcamera/internal/mailbox.h b/include/libcamera/internal/mailbox.h\nnew file mode 100644\nindex 00000000..fb27f5bb\n--- /dev/null\n+++ b/include/libcamera/internal/mailbox.h\n@@ -0,0 +1,50 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2024, Google Inc.\n+ *\n+ * mailbox.h - Template class for generic mailbox\n+ */\n+\n+#pragma once\n+\n+#include <functional>\n+#include <memory>\n+#include <vector>\n+\n+namespace libcamera {\n+\n+template<class T>\n+class MailBox\n+{\n+public:\n+\tusing Recycler = std::function<void(T &)>;\n+\n+\tMailBox()\n+\t\t: valid_(false) {}\n+\t~MailBox();\n+\n+\tvoid put(const T &item, Recycler recycler);\n+\n+\tconst T &get();\n+\n+\tbool valid() { return valid_; }\n+\n+private:\n+\tT item_;\n+\tbool valid_;\n+\tstd::function<void(T &)> recycler_;\n+};\n+\n+template<class T>\n+using SharedMailBox = std::shared_ptr<MailBox<T>>;\n+\n+template<class T>\n+SharedMailBox<T> makeMailBox()\n+{\n+\treturn std::make_shared<MailBox<T>>();\n+}\n+\n+template<class T>\n+std::vector<SharedMailBox<T>> makeMailBoxVector(const unsigned int count);\n+\n+} /* namespace libcamera */\ndiff --git a/include/libcamera/internal/meson.build b/include/libcamera/internal/meson.build\nindex 1c5eef9c..4c1228b9 100644\n--- a/include/libcamera/internal/meson.build\n+++ b/include/libcamera/internal/meson.build\n@@ -27,6 +27,7 @@ libcamera_internal_headers = files([\n     'ipa_proxy.h',\n     'ipc_pipe.h',\n     'ipc_unixsocket.h',\n+    'mailbox.h',\n     'mapped_framebuffer.h',\n     'media_device.h',\n     'media_object.h',\ndiff --git a/src/libcamera/mailbox.cpp b/src/libcamera/mailbox.cpp\nnew file mode 100644\nindex 00000000..4d129224\n--- /dev/null\n+++ b/src/libcamera/mailbox.cpp\n@@ -0,0 +1,97 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2024, Google Inc.\n+ *\n+ * mailbox.cpp - Template class for generic mailbox\n+ */\n+\n+#include \"libcamera/internal/mailbox.h\"\n+\n+#include <libcamera/base/log.h>\n+\n+namespace libcamera {\n+\n+/**\n+ * \\class MailBox\n+ * \\brief MailBox of data\n+ *\n+ * A MailBox contains a block of data that has a single producer, and one or\n+ * multiple consumers. It's often used as a shared_ptr (SharedMailBox) and\n+ * being held by different tasks in pipelines.\n+ */\n+\n+/**\n+ * \\typedef MailBox::Recycler\n+ * \\brief A function that recycles the data when the mailbox is destructed\n+ */\n+\n+template<class T>\n+MailBox<T>::~MailBox()\n+{\n+\tif (valid_ && recycler_)\n+\t\trecycler_(item_);\n+}\n+\n+/**\n+ * \\brief Set the data as the producer. Should be called only once\n+ * \\param[in] item The data to be stored\n+ * \\param[in] recycler The function that recycles \\a data when destructing the\n+ * mailbox. Mostly used for recycling buffers.\n+ */\n+template<class T>\n+void MailBox<T>::put(const T &item, MailBox<T>::Recycler recycler)\n+{\n+\tASSERT(!valid_);\n+\n+\tvalid_ = true;\n+\trecycler_ = recycler;\n+\titem_ = item;\n+}\n+\n+/**\n+ * \\brief Get the data as a consumer. put() function should be called ahead\n+ *\n+ * \\return The stored data\n+ */\n+template<class T>\n+const T &MailBox<T>::get()\n+{\n+\tASSERT(valid_);\n+\treturn item_;\n+}\n+\n+/**\n+ * \\fn MailBox::valid()\n+ * \\return True if put() function has been called\n+ */\n+\n+/**\n+ * \\typedef SharedMailBox\n+ * \\brief A mailbox as a shared_ptr\n+ */\n+\n+/**\n+ * \\fn makeMailBox()\n+ * \\brief A helper function to create a SharedMailBox\n+ *\n+ * \\return A mailbox as a SharedMailBox\n+ */\n+\n+/**\n+ * \\brief A helper function to create a list of mailboxes\n+ * \\param[in] count The number of mailboxes requested\n+ *\n+ * \\return Mailboxes as a vector\n+ */\n+template<class T>\n+std::vector<SharedMailBox<T>> makeMailBoxVector(const unsigned int count)\n+{\n+\tstd::vector<SharedMailBox<T>> mailBoxes;\n+\tmailBoxes.resize(count);\n+\tfor (unsigned int i = 0; i < count; i++)\n+\t\tmailBoxes[i] = makeMailBox<T>();\n+\n+\treturn mailBoxes;\n+}\n+\n+} /* namespace libcamera */\ndiff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\nindex aa9ab029..6c3ef537 100644\n--- a/src/libcamera/meson.build\n+++ b/src/libcamera/meson.build\n@@ -38,6 +38,7 @@ libcamera_internal_sources = files([\n     'ipc_pipe.cpp',\n     'ipc_pipe_unixsocket.cpp',\n     'ipc_unixsocket.cpp',\n+    'mailbox.cpp',\n     'mapped_framebuffer.cpp',\n     'media_device.cpp',\n     'media_object.cpp',\n",
    "prefixes": [
        "1/1"
    ]
}