Show a patch.

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

{
    "id": 14375,
    "url": "https://patchwork.libcamera.org/api/1.1/patches/14375/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/14375/",
    "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": "<20211028073038.653786-3-hiroh@chromium.org>",
    "date": "2021-10-28T07:30:38",
    "name": "[libcamera-devel,2/2] android: Introduce PlatformFrameBufferAllocator",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": false,
    "hash": "f867185526aad33aa928c01cf43141936995d470",
    "submitter": {
        "id": 63,
        "url": "https://patchwork.libcamera.org/api/1.1/people/63/?format=api",
        "name": "Hirokazu Honda",
        "email": "hiroh@chromium.org"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/14375/mbox/",
    "series": [
        {
            "id": 2670,
            "url": "https://patchwork.libcamera.org/api/1.1/series/2670/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=2670",
            "date": "2021-10-28T07:30:36",
            "name": "Introduce PlatformFrameBufferAllocator",
            "version": 1,
            "mbox": "https://patchwork.libcamera.org/series/2670/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/14375/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/14375/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 D5247BDB1C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 28 Oct 2021 07:30:51 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 226E9600BB;\n\tThu, 28 Oct 2021 09:30:51 +0200 (CEST)",
            "from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com\n\t[IPv6:2607:f8b0:4864:20::102f])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 92AAA600B7\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 28 Oct 2021 09:30:49 +0200 (CEST)",
            "by mail-pj1-x102f.google.com with SMTP id gn3so4047587pjb.0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 28 Oct 2021 00:30:49 -0700 (PDT)",
            "from hiroh2.tok.corp.google.com\n\t([2401:fa00:8f:203:3f47:e04c:b9be:d02e])\n\tby smtp.gmail.com with ESMTPSA id\n\tb13sm6974392pjl.15.2021.10.28.00.30.46\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tThu, 28 Oct 2021 00:30:47 -0700 (PDT)"
        ],
        "Authentication-Results": "lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=chromium.org header.i=@chromium.org\n\theader.b=\"FgrLSdb+\"; dkim-atps=neutral",
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org;\n\ts=google; \n\th=from:to:cc:subject:date:message-id:in-reply-to:references\n\t:mime-version:content-transfer-encoding;\n\tbh=rqL9/JXBhdXPjOiqqMl+agf6vYe+AtvFtSW6zKN4DyM=;\n\tb=FgrLSdb+3AVLTv+yH0VzW6Ag4YMJ9czJU9bYFdJV2hSkWVY5VcReuE8FVbaR2Exqlf\n\tvgEOWdcYndVPhpK74QJuG8u6GdS/+tb+4oohvdtAD+mYe9WJBOWiIhueb/P+nXJ+FjqA\n\tB+qygQevSeXa6RY2cV+58LAkyB+OB2huZ5Thg=",
        "X-Google-DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20210112;\n\th=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\n\t:references:mime-version:content-transfer-encoding;\n\tbh=rqL9/JXBhdXPjOiqqMl+agf6vYe+AtvFtSW6zKN4DyM=;\n\tb=17DJ926H3gwBgNuYz4bZ1xFyXPtZP5l56ZJSlCqiXGjNuv7SVmgTnhx/A6d3Zi+LtX\n\t6iE3HtH3O/WBRha3ohS8cynHr7G0oIVElWRXg86JkOJceFWaJsk7BBgWwK6n7+NZ9TTy\n\tzlo7YZNZcQ5QC/uOIXoOoxjbxJTheBmK7OGoRnYBNIUPGt1Uc3i5mefzVegQI9PlWe2M\n\tcOGfwZT1+6Lj9PnPoTRH2ixWaWngKL/7j+EbfALeW8fOLA1UezwQXi6a/qUxZTaWNzDj\n\tIOSpbqy/wkAIqdRKG90zE/NLfKF3D0eq+yr3ZWkgQsbZSWLc+ssUYKMdlnZfB0Ic3Thh\n\tirnA==",
        "X-Gm-Message-State": "AOAM5311VtpINnZMbYyRi+qvwYJnhvNZiv4oMOoo/Xi60xbiBtjMq+pj\n\th6B9BytTnLVz0A8zCIh/nsBsp5y9SGAhTQ==",
        "X-Google-Smtp-Source": "ABdhPJxXmGzzLQ+8ae29FziSBMPigJFQekbKnXD8EpJ2rX/MiPkEMONd7kFcwSnv2CJDJprwCAPlDA==",
        "X-Received": "by 2002:a17:902:8548:b0:141:95ae:8160 with SMTP id\n\td8-20020a170902854800b0014195ae8160mr417450plo.69.1635406247821; \n\tThu, 28 Oct 2021 00:30:47 -0700 (PDT)",
        "From": "Hirokazu Honda <hiroh@chromium.org>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Date": "Thu, 28 Oct 2021 16:30:38 +0900",
        "Message-Id": "<20211028073038.653786-3-hiroh@chromium.org>",
        "X-Mailer": "git-send-email 2.33.1.1089.g2158813163f-goog",
        "In-Reply-To": "<20211028073038.653786-1-hiroh@chromium.org>",
        "References": "<20211028073038.653786-1-hiroh@chromium.org>",
        "MIME-Version": "1.0",
        "Content-Transfer-Encoding": "8bit",
        "Subject": "[libcamera-devel] [PATCH 2/2] android: Introduce\n\tPlatformFrameBufferAllocator",
        "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": "After we unify identical streams to one stream configuration, the\ncapture requested by a HAL client can have been resolved as\nCameraStream::Mapped. That is, a buffer to be written by a camera\nis not provided by a client. We would handle this case by\ndynamically allocating FrameBuffer.\n\nThe existing FrameBufferAllocator cannot used because it is not\nallowed to allocate a new buffer while Camera is running.\n\nThis CL introduces PlatformFrameBufferAllocator. It allocates\nFrameBuffer using cros::CameraBufferManager on ChromeOS and\ngralloc on non ChromeOS platform. The allocated FrameBuffer owns\nthe underlying buffer but must be destroyed before\nPlatformFrameBufferAllocator is destroyed.\n\nSigned-off-by: Hirokazu Honda <hiroh@chromium.org>\n---\n src/android/frame_buffer_allocator.h          |  55 +++++++\n .../mm/cros_frame_buffer_allocator.cpp        |  88 +++++++++++\n .../mm/generic_frame_buffer_allocator.cpp     | 140 ++++++++++++++++++\n src/android/mm/meson.build                    |   6 +-\n 4 files changed, 287 insertions(+), 2 deletions(-)\n create mode 100644 src/android/frame_buffer_allocator.h\n create mode 100644 src/android/mm/cros_frame_buffer_allocator.cpp\n create mode 100644 src/android/mm/generic_frame_buffer_allocator.cpp",
    "diff": "diff --git a/src/android/frame_buffer_allocator.h b/src/android/frame_buffer_allocator.h\nnew file mode 100644\nindex 00000000..e46a43b5\n--- /dev/null\n+++ b/src/android/frame_buffer_allocator.h\n@@ -0,0 +1,55 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2021, Google Inc.\n+ *\n+ * frame_buffer_allocator.h - Interface definition to allocate Frame buffer in\n+ * platform dependent way.\n+ */\n+#ifndef __ANDROID_FRAME_BUFFER_ALLOCATOR_H__\n+#define __ANDROID_FRAME_BUFFER_ALLOCATOR_H__\n+\n+#include <memory>\n+\n+#include <libcamera/base/class.h>\n+#include <libcamera/camera.h>\n+#include <libcamera/framebuffer.h>\n+#include <libcamera/geometry.h>\n+\n+class CameraDevice;\n+\n+class PlatformFrameBufferAllocator : libcamera::Extensible\n+{\n+\tLIBCAMERA_DECLARE_PRIVATE()\n+\n+public:\n+\texplicit PlatformFrameBufferAllocator(CameraDevice *const cameraDevice);\n+\t~PlatformFrameBufferAllocator();\n+\n+\t/*\n+\t * FrameBuffer owns the underlying buffer. Returns nullptr on failure.\n+\t * Note: The returned FrameBuffer needs to be destroyed before\n+\t * PlatformFrameBufferAllocator is destroyed.\n+\t */\n+\tstd::unique_ptr<libcamera::FrameBuffer> allocate(\n+\t\tint halPixelFormat, const libcamera::Size &size, uint32_t usage);\n+};\n+\n+#define PUBLIC_FRAME_BUFFER_ALLOCATOR_IMPLEMENTATION\t\t\t\\\n+PlatformFrameBufferAllocator::PlatformFrameBufferAllocator(\t\t\\\n+\tCameraDevice *const cameraDevice)\t\t\t\t\\\n+\t: Extensible(std::make_unique<Private>(this, cameraDevice))\t\\\n+{\t\t\t\t\t\t\t\t\t\\\n+}\t\t\t\t\t\t\t\t\t\\\n+PlatformFrameBufferAllocator::~PlatformFrameBufferAllocator()\t\t\\\n+{\t\t\t\t\t\t\t\t\t\\\n+}\t\t\t\t\t\t\t\t\t\\\n+\t\t\t\t\t\t\t\t\t\\\n+std::unique_ptr<libcamera::FrameBuffer>\t\t\t\t\t\\\n+PlatformFrameBufferAllocator::allocate(int halPixelFormat,\t\t\\\n+\t\t\t\t       const libcamera::Size& size,\t\\\n+\t\t\t\t       uint32_t usage)\t\t\t\\\n+{\t\t\t\t\t\t\t\t\t\\\n+\treturn _d()->allocate(halPixelFormat, size, usage);\t\t\\\n+}\n+\n+#endif /* __ANDROID_FRAME_BUFFER_ALLOCATOR_H__ */\ndiff --git a/src/android/mm/cros_frame_buffer_allocator.cpp b/src/android/mm/cros_frame_buffer_allocator.cpp\nnew file mode 100644\nindex 00000000..9d7e3c88\n--- /dev/null\n+++ b/src/android/mm/cros_frame_buffer_allocator.cpp\n@@ -0,0 +1,88 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2021, Google Inc.\n+ *\n+ * cros_frame_buffer.cpp - Allocate FrameBuffer for Chromium OS using\n+ * CameraBufferManager\n+ */\n+\n+#include <memory>\n+#include <vector>\n+\n+#include <libcamera/base/log.h>\n+\n+#include \"libcamera/internal/framebuffer.h\"\n+\n+#include \"../camera_device.h\"\n+#include \"../frame_buffer_allocator.h\"\n+#include \"cros-camera/camera_buffer_manager.h\"\n+\n+using namespace libcamera;\n+\n+LOG_DECLARE_CATEGORY(HAL)\n+\n+namespace {\n+class CrosFrameBufferData : public FrameBuffer::Private\n+{\n+\tLIBCAMERA_DECLARE_PUBLIC(FrameBuffer)\n+public:\n+\tCrosFrameBufferData(cros::ScopedBufferHandle scopedHandle)\n+\t\t: FrameBuffer::Private(), scopedHandle_(std::move(scopedHandle))\n+\t{\n+\t}\n+\n+\t~CrosFrameBufferData() override = default;\n+private:\n+\tcros::ScopedBufferHandle scopedHandle_;\n+};\n+} /* namespace */\n+\n+class PlatformFrameBufferAllocator::Private : public Extensible::Private\n+{\n+\tLIBCAMERA_DECLARE_PUBLIC(PlatformFrameBufferAllocator)\n+\n+public:\n+\tPrivate([[maybe_unused]] PlatformFrameBufferAllocator *allocator,\n+\t\t[[maybe_unused]] CameraDevice *const cameraDevice)\n+\t{\n+\t}\n+\n+\t~Private() = default;\n+\n+\tstd::unique_ptr<libcamera::FrameBuffer> allocate(\n+\t\tint halPixelFormat, const libcamera::Size &size, uint32_t usage);\n+};\n+\n+std::unique_ptr<libcamera::FrameBuffer>\n+PlatformFrameBufferAllocator::Private::allocate(\n+\tint halPixelFormat, const libcamera::Size &size, uint32_t usage)\n+{\n+\tcros::ScopedBufferHandle scopedHandle =\n+\t\tcros::CameraBufferManager::AllocateScopedBuffer(\n+\t\t\tsize.width, size.height, halPixelFormat, usage);\n+\tif (!scopedHandle) {\n+\t\tLOG(HAL, Error) << \"Failed to allocate buffer handle\";\n+\t\treturn nullptr;\n+\t}\n+\n+\tbuffer_handle_t handle = *scopedHandle;\n+\tconst size_t numPlanes = cros::CameraBufferManager::GetNumPlanes(handle);\n+\tstd::vector<FrameBuffer::Plane> planes(numPlanes);\n+\tFileDescriptor fd{ handle->data[0] };\n+\tif (!fd.isValid()) {\n+\t\tLOG(HAL, Fatal) << \"Invalid fd\";\n+\t\treturn nullptr;\n+\t}\n+\n+\tfor (auto [i, plane] : utils::enumerate(planes)) {\n+\t\tplane.fd = fd;\n+\t\tplane.offset = cros::CameraBufferManager::GetPlaneOffset(handle, i);\n+\t\tplane.length = cros::CameraBufferManager::GetPlaneSize(handle, i);\n+\t}\n+\n+\treturn std::make_unique<FrameBuffer>(\n+\t\tstd::make_unique<CrosFrameBufferData>(std::move(scopedHandle)),\n+\t\tplanes);\n+}\n+\n+PUBLIC_FRAME_BUFFER_ALLOCATOR_IMPLEMENTATION\ndiff --git a/src/android/mm/generic_frame_buffer_allocator.cpp b/src/android/mm/generic_frame_buffer_allocator.cpp\nnew file mode 100644\nindex 00000000..1bc3c405\n--- /dev/null\n+++ b/src/android/mm/generic_frame_buffer_allocator.cpp\n@@ -0,0 +1,140 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2021, Google Inc.\n+ *\n+ * generic_camera_buffer.cpp - Allocate FrameBuffer using gralloc API\n+ */\n+\n+#include <memory>\n+#include <vector>\n+\n+#include <libcamera/base/log.h>\n+\n+#include \"libcamera/internal/formats.h\"\n+#include \"libcamera/internal/framebuffer.h\"\n+\n+#include <hardware/camera3.h>\n+#include <hardware/gralloc.h>\n+#include <hardware/hardware.h>\n+\n+#include \"../camera_device.h\"\n+#include \"../frame_buffer_allocator.h\"\n+\n+using namespace libcamera;\n+\n+LOG_DECLARE_CATEGORY(HAL)\n+\n+namespace {\n+class GenericFrameBufferData : public FrameBuffer::Private\n+{\n+\tLIBCAMERA_DECLARE_PUBLIC(FrameBuffer)\n+public:\n+\tGenericFrameBufferData(struct alloc_device_t *allocDevice,\n+\t\t\t       buffer_handle_t handle)\n+\t\t: allocDevice_(allocDevice), handle_(handle)\n+\t{\n+\t\tASSERT(allocDevice_);\n+\t\tASSERT(handle_);\n+\t}\n+\t~GenericFrameBufferData()\n+\t{\n+\t\t/*\n+\t\t *  allocDevice_ is used to destroy handle_. allocDevice_ is\n+\t\t * owned by PlatformFrameBufferAllocator::Private.\n+\t\t * GenericFrameBufferData must be destroyed before it is\n+\t\t * destroyed.\n+\t\t *\n+\t\t * \\todo: Consider managing alloc_device_t with std::shared_ptr\n+\t\t * if this is difficult to maintain.\n+\t\t *\n+\t\t * Q: Thread safety against alloc_device_t is not documented.\n+\t\t * Is it no problem to call alloc/free in parallel?\n+\t\t */\n+\t\tallocDevice_->free(allocDevice_, handle_);\n+\t}\n+private:\n+\tstruct alloc_device_t *allocDevice_;\n+\tconst buffer_handle_t handle_;\n+};\n+} /* namespace */\n+\n+class PlatformFrameBufferAllocator::Private : public Extensible::Private\n+{\n+\tLIBCAMERA_DECLARE_PUBLIC(PlatformFrameBufferAllocator)\n+\n+public:\n+\tPrivate([[maybe_unused]] PlatformFrameBufferAllocator *allocator,\n+\t\tCameraDevice *const cameraDevice)\n+\t\t: cameraDevice_(cameraDevice),\n+\t\t  hardwareModule_(cameraDevice->camera3Device()->common.module),\n+\t\t  allocDevice_(nullptr)\n+\t{\n+\t\tASSERT(hardwareModule_);\n+\t}\n+\n+\t~Private() override;\n+\n+\tstd::unique_ptr<libcamera::FrameBuffer> allocate(\n+\t\tint halPixelFormat, const libcamera::Size &size, uint32_t usage);\n+\n+private:\n+\tconst CameraDevice *const cameraDevice_;\n+\tstruct hw_module_t *const hardwareModule_;\n+\tstruct alloc_device_t *allocDevice_;\n+};\n+\n+PlatformFrameBufferAllocator::Private::~Private()\n+{\n+\tif (allocDevice_)\n+\t\tgralloc_close(allocDevice_);\n+}\n+\n+std::unique_ptr<libcamera::FrameBuffer>\n+PlatformFrameBufferAllocator::Private::allocate(\n+\tint halPixelFormat, const libcamera::Size &size, uint32_t usage)\n+{\n+\tif (!allocDevice_) {\n+\t\tint ret = gralloc_open(hardwareModule_, &allocDevice_);\n+\t\tif (ret) {\n+\t\t\tLOG(HAL, Error) << \"gralloc_open() failed: \" << ret;\n+\t\t\treturn nullptr;\n+\t\t}\n+\t}\n+\n+\tint stride = 0;\n+\tbuffer_handle_t handle = nullptr;\n+\tint ret = allocDevice_->alloc(allocDevice_, size.width, size.height,\n+\t\t\t\t      halPixelFormat, usage, &handle, &stride);\n+\tif (ret) {\n+\t\tLOG(HAL, Error) << \"failed buffer allocating: \" << ret;\n+\t\treturn nullptr;\n+\t}\n+\tif (!handle) {\n+\t\tLOG(HAL, Fatal) << \"buffer_handle_t is empty on success\";\n+\t\treturn nullptr;\n+\t}\n+\n+\tconst libcamera::PixelFormat pixelFormat =\n+\t\tcameraDevice_->capabilities()->toPixelFormat(halPixelFormat);\n+\tconst auto &info = PixelFormatInfo::info(pixelFormat);\n+\tconst unsigned int numPlanes = info.numPlanes();\n+\tstd::vector<FrameBuffer::Plane> planes(numPlanes);\n+\n+\t/* This code assumes the planes are mapped consecutively. */\n+\tFileDescriptor fd{ handle->data[0] };\n+\tsize_t offset = 0;\n+\tfor (auto [i, plane] : utils::enumerate(planes)) {\n+\t\tconst size_t planeSize = info.planeSize(size.height, i, stride);\n+\n+\t\tplanes[i].fd = fd;\n+\t\tplanes[i].offset = offset;\n+\t\tplanes[i].length = planeSize;\n+\t\toffset += planeSize;\n+\t}\n+\n+\treturn std::make_unique<FrameBuffer>(\n+\t\tstd::make_unique<GenericFrameBufferData>(allocDevice_, handle),\n+\t\tplanes);\n+}\n+\n+PUBLIC_FRAME_BUFFER_ALLOCATOR_IMPLEMENTATION\ndiff --git a/src/android/mm/meson.build b/src/android/mm/meson.build\nindex eeb5cc2e..d40a3b0b 100644\n--- a/src/android/mm/meson.build\n+++ b/src/android/mm/meson.build\n@@ -2,8 +2,10 @@\n \n platform = get_option('android_platform')\n if platform == 'generic'\n-    android_hal_sources += files(['generic_camera_buffer.cpp'])\n+    android_hal_sources += files(['generic_camera_buffer.cpp',\n+                                  'generic_frame_buffer_allocator.cpp'])\n elif platform == 'cros'\n-    android_hal_sources += files(['cros_camera_buffer.cpp'])\n+    android_hal_sources += files(['cros_camera_buffer.cpp',\n+                                  'cros_frame_buffer_allocator.cpp'])\n     android_deps += [dependency('libcros_camera')]\n endif\n",
    "prefixes": [
        "libcamera-devel",
        "2/2"
    ]
}