From patchwork Fri Nov 26 03:41:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 14789 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 A1ED9BDB13 for ; Fri, 26 Nov 2021 03:42:00 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id D86856049B; Fri, 26 Nov 2021 04:41:59 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="WOUYZVwW"; dkim-atps=neutral Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 9E19B6011D for ; Fri, 26 Nov 2021 04:41:57 +0100 (CET) Received: by mail-pj1-x102a.google.com with SMTP id p18-20020a17090ad31200b001a78bb52876so8987791pju.3 for ; Thu, 25 Nov 2021 19:41:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=6QK1MyRb7UCtu8CYpX8LC3RrUZlA00Qlxu6YEFO7ek8=; b=WOUYZVwWlNSuefs3QW0fuAVPgEKJGrbRBNd6nmBMIJIWN+gRQgBnAEKRJXm35+15KQ 7y9+c/bHUi/wHPDeiFV465zThzeSeKO8+wzj+A89lSskNycrkksbKGriISnCjV4fxTD3 Eoi9YYYOrr+hSPiQcEIXdmUnuG5CP7jJn+s0w= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=6QK1MyRb7UCtu8CYpX8LC3RrUZlA00Qlxu6YEFO7ek8=; b=Xe7/FglLXgVCXargjY38xS5aTGFahtnuL9X3lvAuJFSN3Y3b0xCSjr+Cc/qMFpBDOE iKaNYuPWfx4xhmPbKX1oXd5wRGUOXV6y0b6I16G4LmngoRdm5RunRY0QJxI1wOGQUy8+ IUtbkfazxCLW37T8uv5NiPdybMnhy1VZ3c0JOt7sQOD6aNSJVhX6UI46BDPDjZcMgVBh pYVkZ4CD+lKz+23VHLgjToauP1fNJRqo5FgjHCUBhffX7/K/dThhVWwmLNSzzC9O02Ga SyTTgJwxVntFzU1JKNfx3vhf8IeyuHOvWuaOyfo4xZhtiSe3iJssxKmWlGXLFNCfhk5n vwfw== X-Gm-Message-State: AOAM5330/l4pAzLvzoB3fu9NjnhCF5rDIp5FVRWELIalnDzo7tl6a+3t 8et83LlRQUpEg7InjKHqAJn4eIfXZpVepA== X-Google-Smtp-Source: ABdhPJwMkhlkyJEQz6Bhxblp9HGvAIzyt0c/nltHGzYYOWLCIdRACddc9SaKkjgsc2ChRPgIbub7hA== X-Received: by 2002:a17:902:e885:b0:142:1500:d2ba with SMTP id w5-20020a170902e88500b001421500d2bamr35160123plg.19.1637898115630; Thu, 25 Nov 2021 19:41:55 -0800 (PST) Received: from hiroh2.tok.corp.google.com ([2401:fa00:8f:203:139c:952e:8861:d909]) by smtp.gmail.com with ESMTPSA id t4sm4500697pfj.168.2021.11.25.19.41.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Nov 2021 19:41:55 -0800 (PST) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Fri, 26 Nov 2021 12:41:49 +0900 Message-Id: <20211126034150.1843285-1-hiroh@chromium.org> X-Mailer: git-send-email 2.34.0.rc2.393.gf8c9666880-goog MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 1/2] libcamera: framebuffer: Enable attaching additional data to FrameBuffer 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" We cannot have a subclass of FrameBuffer because it is marked as final. This adds a FrameBuffer constructor with FrameBuffer::Private. So we can attach some additional resources with FrameBuffer through a customized FrameBuffer::Private class. Signed-off-by: Hirokazu Honda Reviewed-by: Jacopo Mondi Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- include/libcamera/framebuffer.h | 2 ++ include/libcamera/internal/framebuffer.h | 1 + src/libcamera/framebuffer.cpp | 24 ++++++++++++++++++++++-- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/include/libcamera/framebuffer.h b/include/libcamera/framebuffer.h index 7f2f176a..abcede31 100644 --- a/include/libcamera/framebuffer.h +++ b/include/libcamera/framebuffer.h @@ -58,6 +58,8 @@ public: }; FrameBuffer(const std::vector &planes, unsigned int cookie = 0); + FrameBuffer(std::unique_ptr d, + const std::vector &planes, unsigned int cookie = 0); const std::vector &planes() const { return planes_; } Request *request() const; diff --git a/include/libcamera/internal/framebuffer.h b/include/libcamera/internal/framebuffer.h index cd33c295..3cced5b1 100644 --- a/include/libcamera/internal/framebuffer.h +++ b/include/libcamera/internal/framebuffer.h @@ -19,6 +19,7 @@ class FrameBuffer::Private : public Extensible::Private public: Private(); + virtual ~Private(); void setRequest(Request *request) { request_ = request; } bool isContiguous() const { return isContiguous_; } diff --git a/src/libcamera/framebuffer.cpp b/src/libcamera/framebuffer.cpp index 337ea115..3a324471 100644 --- a/src/libcamera/framebuffer.cpp +++ b/src/libcamera/framebuffer.cpp @@ -116,6 +116,13 @@ FrameBuffer::Private::Private() { } +/** + * \brief FrameBuffer::Private destructor + */ +FrameBuffer::Private::~Private() +{ +} + /** * \fn FrameBuffer::Private::setRequest() * \brief Set the request this buffer belongs to @@ -213,8 +220,21 @@ FrameBuffer::Private::Private() * \param[in] cookie Cookie */ FrameBuffer::FrameBuffer(const std::vector &planes, unsigned int cookie) - : Extensible(std::make_unique()), planes_(planes), - cookie_(cookie) + : FrameBuffer(std::make_unique(), planes, cookie) +{ +} + +/** + * \brief Construct a FrameBuffer with an extensible private class and an array + * of planes + * \param[in] d The extensible private class + * \param[in] planes The frame memory planes + * \param[in] cookie Cookie + */ +FrameBuffer::FrameBuffer(std::unique_ptr d, + const std::vector &planes, + unsigned int cookie) + : Extensible(std::move(d)), planes_(planes), cookie_(cookie) { metadata_.planes_.resize(planes_.size()); From patchwork Fri Nov 26 03:41:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 14790 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 208D6BDB13 for ; Fri, 26 Nov 2021 03:42:02 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id BEDA7604F9; Fri, 26 Nov 2021 04:42:01 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="MT0WbN91"; dkim-atps=neutral Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 7A68860491 for ; Fri, 26 Nov 2021 04:41:59 +0100 (CET) Received: by mail-pl1-x631.google.com with SMTP id v19so5803401plo.7 for ; Thu, 25 Nov 2021 19:41:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=2Mg0jBRdnhiRkOK+M2xyhHMTZof0Ul8AvHdUgxBwUio=; b=MT0WbN91Dz+xughgyN+AoWBe2mhh4fTlkBhAgHQWRY4HznfkaqqRDnUufAgbvBg8Pw 7RX/IhxNFklQ1+NoWwhYe/dBMuy9uc5KPmOSk3f4toxUtyLNbnIW1ErDvAYEFRwuY1u7 wbZK/IP7K2qSMNEq4qMJ5XYj1QcunsqlSjE9k= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2Mg0jBRdnhiRkOK+M2xyhHMTZof0Ul8AvHdUgxBwUio=; b=Xuq8SVQlVBnXHFVp1d2DOFLSh9ydKYefWbl/eRGpEXHLl34JEtoMcsmDYMpzClFWJn zMjLxuusHTLToXie9OgfAg7CP0MMbJLwTjDYVj+lhtV1GhvEc6sW+HNITdbi0L9Y38Ma bCmv5xqTy0vGSZePnxWgBSMBpeBCK2TmDO9grrZPDCvfBmiIB7Md1mH2tYdBq3+e7UJ9 fQ0Cy7ioqRdV7APM5Q4VVb/Ms7Manc/CxvV1JKHWGmwd7Hko0YLXvkuoos57l5a1JtWq TE6/9ZcbofgRG6VCa15DrbuQm+RlMrJORaZe4GJcZ0nCHdFfk0tPSI9NCuwp1bo1NYQ7 lb7Q== X-Gm-Message-State: AOAM531/DJAUSti4m0uB/Q8ze5Vtupw1IaOOLn6iIjDIbmuLEi1J0SB0 AGYzKgml4Fa+ywHCHOo9ssSFsPxLD7ROhQ== X-Google-Smtp-Source: ABdhPJy/VBsjulVhEEi3tS3bUgLJIigFAV0T1o/o/FZp2600Asxu8/yTA4bEPzFgE4N7ZnWZKYDTRw== X-Received: by 2002:a17:902:6b46:b0:142:8470:862e with SMTP id g6-20020a1709026b4600b001428470862emr34761025plt.49.1637898117614; Thu, 25 Nov 2021 19:41:57 -0800 (PST) Received: from hiroh2.tok.corp.google.com ([2401:fa00:8f:203:139c:952e:8861:d909]) by smtp.gmail.com with ESMTPSA id t4sm4500697pfj.168.2021.11.25.19.41.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Nov 2021 19:41:57 -0800 (PST) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Fri, 26 Nov 2021 12:41:50 +0900 Message-Id: <20211126034150.1843285-2-hiroh@chromium.org> X-Mailer: git-send-email 2.34.0.rc2.393.gf8c9666880-goog In-Reply-To: <20211126034150.1843285-1-hiroh@chromium.org> References: <20211126034150.1843285-1-hiroh@chromium.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 2/2] android: Introduce PlatformFrameBufferAllocator 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" The existing FrameBufferAllocator is not allowed to allocate a new buffer while Camera is running. This introduces PlatformFrameBufferAllocator. It allocates FrameBuffer using cros::CameraBufferManager on ChromeOS and gralloc on non ChromeOS platform. The allocated FrameBuffer owns the underlying buffer but must be destroyed before PlatformFrameBufferAllocator is destroyed. Signed-off-by: Hirokazu Honda Reviewed-by: Jacopo Mondi Reviewed-by: Laurent Pinchart --- src/android/frame_buffer_allocator.h | 55 +++++++ .../mm/cros_frame_buffer_allocator.cpp | 88 +++++++++++ .../mm/generic_frame_buffer_allocator.cpp | 142 ++++++++++++++++++ src/android/mm/meson.build | 6 +- 4 files changed, 289 insertions(+), 2 deletions(-) create mode 100644 src/android/frame_buffer_allocator.h create mode 100644 src/android/mm/cros_frame_buffer_allocator.cpp create mode 100644 src/android/mm/generic_frame_buffer_allocator.cpp diff --git a/src/android/frame_buffer_allocator.h b/src/android/frame_buffer_allocator.h new file mode 100644 index 00000000..8fa74520 --- /dev/null +++ b/src/android/frame_buffer_allocator.h @@ -0,0 +1,55 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2021, Google Inc. + * + * frame_buffer_allocator.h - Interface definition to allocate Frame buffer in + * platform dependent way. + */ +#ifndef __ANDROID_FRAME_BUFFER_ALLOCATOR_H__ +#define __ANDROID_FRAME_BUFFER_ALLOCATOR_H__ + +#include + +#include + +#include +#include +#include + +class CameraDevice; + +class PlatformFrameBufferAllocator : libcamera::Extensible +{ + LIBCAMERA_DECLARE_PRIVATE() + +public: + explicit PlatformFrameBufferAllocator(CameraDevice *const cameraDevice); + ~PlatformFrameBufferAllocator(); + + /* + * FrameBuffer owns the underlying buffer. Returns nullptr on failure. + * Note: The returned FrameBuffer needs to be destroyed before + * PlatformFrameBufferAllocator is destroyed. + */ + std::unique_ptr allocate( + int halPixelFormat, const libcamera::Size &size, uint32_t usage); +}; + +#define PUBLIC_FRAME_BUFFER_ALLOCATOR_IMPLEMENTATION \ +PlatformFrameBufferAllocator::PlatformFrameBufferAllocator( \ + CameraDevice *const cameraDevice) \ + : Extensible(std::make_unique(this, cameraDevice)) \ +{ \ +} \ +PlatformFrameBufferAllocator::~PlatformFrameBufferAllocator() \ +{ \ +} \ +std::unique_ptr \ +PlatformFrameBufferAllocator::allocate(int halPixelFormat, \ + const libcamera::Size& size, \ + uint32_t usage) \ +{ \ + return _d()->allocate(halPixelFormat, size, usage); \ +} + +#endif /* __ANDROID_FRAME_BUFFER_ALLOCATOR_H__ */ diff --git a/src/android/mm/cros_frame_buffer_allocator.cpp b/src/android/mm/cros_frame_buffer_allocator.cpp new file mode 100644 index 00000000..47d8b77b --- /dev/null +++ b/src/android/mm/cros_frame_buffer_allocator.cpp @@ -0,0 +1,88 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2021, Google Inc. + * + * cros_frame_buffer.cpp - Allocate FrameBuffer for Chromium OS using + * CameraBufferManager + */ + +#include +#include + +#include + +#include "libcamera/internal/framebuffer.h" + +#include "../camera_device.h" +#include "../frame_buffer_allocator.h" +#include "cros-camera/camera_buffer_manager.h" + +using namespace libcamera; + +LOG_DECLARE_CATEGORY(HAL) + +namespace { + +class CrosFrameBufferData : public FrameBuffer::Private +{ + LIBCAMERA_DECLARE_PUBLIC(FrameBuffer) +public: + CrosFrameBufferData(cros::ScopedBufferHandle scopedHandle) + : FrameBuffer::Private(), scopedHandle_(std::move(scopedHandle)) + { + } + +private: + cros::ScopedBufferHandle scopedHandle_; +}; + +} /* namespace */ + +class PlatformFrameBufferAllocator::Private : public Extensible::Private +{ + LIBCAMERA_DECLARE_PUBLIC(PlatformFrameBufferAllocator) + +public: + Private([[maybe_unused]] PlatformFrameBufferAllocator *allocator, + [[maybe_unused]] CameraDevice *const cameraDevice) + { + } + + std::unique_ptr allocate( + int halPixelFormat, const libcamera::Size &size, uint32_t usage); +}; + +std::unique_ptr +PlatformFrameBufferAllocator::Private::allocate( + int halPixelFormat, const libcamera::Size &size, uint32_t usage) +{ + cros::ScopedBufferHandle scopedHandle = + cros::CameraBufferManager::AllocateScopedBuffer( + size.width, size.height, halPixelFormat, usage); + if (!scopedHandle) { + LOG(HAL, Error) << "Failed to allocate buffer handle"; + return nullptr; + } + + buffer_handle_t handle = *scopedHandle; + const size_t numPlanes = cros::CameraBufferManager::GetNumPlanes(handle); + std::vector planes(numPlanes); + FileDescriptor fd{ handle->data[0] }; + if (!fd.isValid()) { + LOG(HAL, Fatal) << "Invalid fd"; + return nullptr; + } + + /* This code assumes all the planes are located in the same buffer. */ + for (auto [i, plane] : utils::enumerate(planes)) { + plane.fd = fd; + plane.offset = cros::CameraBufferManager::GetPlaneOffset(handle, i); + plane.length = cros::CameraBufferManager::GetPlaneSize(handle, i); + } + + return std::make_unique( + std::make_unique(std::move(scopedHandle)), + planes); +} + +PUBLIC_FRAME_BUFFER_ALLOCATOR_IMPLEMENTATION diff --git a/src/android/mm/generic_frame_buffer_allocator.cpp b/src/android/mm/generic_frame_buffer_allocator.cpp new file mode 100644 index 00000000..f00fd704 --- /dev/null +++ b/src/android/mm/generic_frame_buffer_allocator.cpp @@ -0,0 +1,142 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2021, Google Inc. + * + * generic_camera_buffer.cpp - Allocate FrameBuffer using gralloc API + */ + +#include +#include + +#include + +#include "libcamera/internal/formats.h" +#include "libcamera/internal/framebuffer.h" + +#include +#include +#include + +#include "../camera_device.h" +#include "../frame_buffer_allocator.h" + +using namespace libcamera; + +LOG_DECLARE_CATEGORY(HAL) + +namespace { +class GenericFrameBufferData : public FrameBuffer::Private +{ + LIBCAMERA_DECLARE_PUBLIC(FrameBuffer) + +public: + GenericFrameBufferData(struct alloc_device_t *allocDevice, + buffer_handle_t handle) + : allocDevice_(allocDevice), handle_(handle) + { + ASSERT(allocDevice_); + ASSERT(handle_); + } + + ~GenericFrameBufferData() override + { + /* + * allocDevice_ is used to destroy handle_. allocDevice_ is + * owned by PlatformFrameBufferAllocator::Private. + * GenericFrameBufferData must be destroyed before it is + * destroyed. + * + * \todo Consider managing alloc_device_t with std::shared_ptr + * if this is difficult to maintain. + * + * \todo Thread safety against alloc_device_t is not documented. + * Is it no problem to call alloc/free in parallel? + */ + allocDevice_->free(allocDevice_, handle_); + } + +private: + struct alloc_device_t *allocDevice_; + const buffer_handle_t handle_; +}; +} /* namespace */ + +class PlatformFrameBufferAllocator::Private : public Extensible::Private +{ + LIBCAMERA_DECLARE_PUBLIC(PlatformFrameBufferAllocator) + +public: + Private([[maybe_unused]] PlatformFrameBufferAllocator *allocator, + CameraDevice *const cameraDevice) + : cameraDevice_(cameraDevice), + hardwareModule_(cameraDevice->camera3Device()->common.module), + allocDevice_(nullptr) + { + ASSERT(hardwareModule_); + } + + ~Private() override; + + std::unique_ptr allocate( + int halPixelFormat, const libcamera::Size &size, uint32_t usage); + +private: + const CameraDevice *const cameraDevice_; + struct hw_module_t *const hardwareModule_; + struct alloc_device_t *allocDevice_; +}; + +PlatformFrameBufferAllocator::Private::~Private() +{ + if (allocDevice_) + gralloc_close(allocDevice_); +} + +std::unique_ptr +PlatformFrameBufferAllocator::Private::allocate( + int halPixelFormat, const libcamera::Size &size, uint32_t usage) +{ + if (!allocDevice_) { + int ret = gralloc_open(hardwareModule_, &allocDevice_); + if (ret) { + LOG(HAL, Fatal) << "gralloc_open() failed: " << ret; + return nullptr; + } + } + + int stride = 0; + buffer_handle_t handle = nullptr; + int ret = allocDevice_->alloc(allocDevice_, size.width, size.height, + halPixelFormat, usage, &handle, &stride); + if (ret) { + LOG(HAL, Error) << "failed buffer allocating: " << ret; + return nullptr; + } + if (!handle) { + LOG(HAL, Fatal) << "buffer_handle_t is empty on success"; + return nullptr; + } + + const libcamera::PixelFormat pixelFormat = + cameraDevice_->capabilities()->toPixelFormat(halPixelFormat); + const auto &info = PixelFormatInfo::info(pixelFormat); + std::vector planes(info.numPlanes()); + + /* This code assumes the planes are mapped consecutively. */ + FileDescriptor fd{ handle->data[0] }; + size_t offset = 0; + for (auto [i, plane] : utils::enumerate(planes)) { + const size_t planeSize = info.planeSize(size.height, i, stride); + + planes[i].fd = fd; + planes[i].offset = offset; + planes[i].length = planeSize; + offset += planeSize; + } + + return std::make_unique( + std::make_unique(allocDevice_, handle), + planes); +} + +PUBLIC_FRAME_BUFFER_ALLOCATOR_IMPLEMENTATION diff --git a/src/android/mm/meson.build b/src/android/mm/meson.build index eeb5cc2e..d40a3b0b 100644 --- a/src/android/mm/meson.build +++ b/src/android/mm/meson.build @@ -2,8 +2,10 @@ platform = get_option('android_platform') if platform == 'generic' - android_hal_sources += files(['generic_camera_buffer.cpp']) + android_hal_sources += files(['generic_camera_buffer.cpp', + 'generic_frame_buffer_allocator.cpp']) elif platform == 'cros' - android_hal_sources += files(['cros_camera_buffer.cpp']) + android_hal_sources += files(['cros_camera_buffer.cpp', + 'cros_frame_buffer_allocator.cpp']) android_deps += [dependency('libcros_camera')] endif