From patchwork Mon Sep 20 17:37:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Umang Jain X-Patchwork-Id: 13874 X-Patchwork-Delegate: umang.jain@ideasonboard.com 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 0564BBF01C for ; Mon, 20 Sep 2021 17:38:23 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id AF2686918E; Mon, 20 Sep 2021 19:38:22 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="lUyrnocf"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 36B356918F for ; Mon, 20 Sep 2021 19:38:20 +0200 (CEST) Received: from perceval.ideasonboard.com (unknown [103.251.226.144]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 2FEA745E; Mon, 20 Sep 2021 19:38:19 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1632159500; bh=0TR32C93mMbOXb1KbT3dgwFoknIVPBIG8oc9sLKNYUQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lUyrnocfHkZrfkUxRVoIA9lLQLh+XRSbqwRIN9qdY250NllpJj5zv9y9+xMorn9dq fBpRb/npwk7S5rGbxB2Uf0kr9qbxALIdyFG8fQpmrtacdwx/tMNQoqNIgR9cStN7z3 LRKwj7VKC/edj9JqxUSCpSpXNr48fLWIdaNfHtQM= From: Umang Jain To: libcamera-devel@lists.libcamera.org Date: Mon, 20 Sep 2021 23:07:52 +0530 Message-Id: <20210920173752.1346190-11-umang.jain@ideasonboard.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210920173752.1346190-1-umang.jain@ideasonboard.com> References: <20210920173752.1346190-1-umang.jain@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 10/10] android: camera_stream: Run post processor in a thread 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" This commit makes the post processor run in a separate thread. To enable the move to a separate thread, he post processor needs to be inherited from libcamera::Object and use the Object::moveToThread() API to execute the move. A user defined destructor is introduced to stop the thread for cleanup. Since CameraStream is move-constructible and compiler implicitly generates its constructor, defining a destructor will prevent the compiler from adding an implicitly-declared move constructor. Therefore, we need to explicitly add a move constructor as well. Also, the destination buffer for post-processing needs to be alive and stored as a part of overall context, hence a CameraBuffer unique-pointer member is introduced in the Camera3RequestDescriptor struct. --- /* \todo Use ConnectionTypeQueued instead of ConnectionTypeBlocking */ --- Signed-off-by: Umang Jain --- src/android/camera_device.h | 2 ++ src/android/camera_stream.cpp | 29 +++++++++++++++++++++-------- src/android/camera_stream.h | 5 +++++ src/android/post_processor.h | 3 ++- 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/android/camera_device.h b/src/android/camera_device.h index 0bd570a1..e3f3fe7c 100644 --- a/src/android/camera_device.h +++ b/src/android/camera_device.h @@ -27,6 +27,7 @@ #include #include +#include "camera_buffer.h" #include "camera_capabilities.h" #include "camera_metadata.h" #include "camera_stream.h" @@ -55,6 +56,7 @@ struct Camera3RequestDescriptor { CameraMetadata settings_; std::unique_ptr request_; std::unique_ptr resultMetadata_; + std::unique_ptr destBuffer_; camera3_capture_result_t captureResult_ = {}; libcamera::FrameBuffer *internalBuffer_; diff --git a/src/android/camera_stream.cpp b/src/android/camera_stream.cpp index c18c7041..6578ce09 100644 --- a/src/android/camera_stream.cpp +++ b/src/android/camera_stream.cpp @@ -55,6 +55,9 @@ CameraStream::CameraStream(CameraDevice *const cameraDevice, * is what we instantiate here. */ postProcessor_ = std::make_unique(cameraDevice_); + thread_ = std::make_unique(); + postProcessor_->moveToThread(thread_.get()); + thread_->start(); } if (type == Type::Internal) { @@ -63,6 +66,14 @@ CameraStream::CameraStream(CameraDevice *const cameraDevice, } } +CameraStream::~CameraStream() +{ + if (thread_) { + thread_->exit(); + thread_->wait(); + } +} + const StreamConfiguration &CameraStream::configuration() const { return config_->at(index_); @@ -111,21 +122,23 @@ void CameraStream::process(const FrameBuffer *source, return; } - /* - * \todo Buffer mapping and processing should be moved to a - * separate thread. - */ const StreamConfiguration &output = configuration(); - CameraBuffer dest(camera3Dest, formats::MJPEG, output.size, - PROT_READ | PROT_WRITE); - if (!dest.isValid()) { + std::unique_ptr dest = + std::make_unique(camera3Dest, formats::MJPEG, + output.size, PROT_READ | PROT_WRITE); + + if (!dest->isValid()) { LOG(HAL, Error) << "Failed to map android blob buffer"; request->status_ = Camera3RequestDescriptor::ProcessStatus::Error; handleProcessComplete(request); return; } + request->destBuffer_ = std::move(dest); - postProcessor_->process(source, &dest, request); + /* \todo Use ConnectionTypeQueued instead of ConnectionTypeBlocking */ + postProcessor_->invokeMethod(&PostProcessor::process, + ConnectionTypeBlocking, source, + request->destBuffer_.get(), request); } void CameraStream::handleProcessComplete(Camera3RequestDescriptor *request) diff --git a/src/android/camera_stream.h b/src/android/camera_stream.h index d4ec5c25..42db72d8 100644 --- a/src/android/camera_stream.h +++ b/src/android/camera_stream.h @@ -13,6 +13,8 @@ #include +#include + #include #include #include @@ -113,6 +115,8 @@ public: CameraStream(CameraDevice *const cameraDevice, libcamera::CameraConfiguration *config, Type type, camera3_stream_t *camera3Stream, unsigned int index); + CameraStream(CameraStream &&other) = default; + ~CameraStream(); Type type() const { return type_; } const camera3_stream_t &camera3Stream() const { return *camera3Stream_; } @@ -143,6 +147,7 @@ private: */ std::unique_ptr mutex_; std::unique_ptr postProcessor_; + std::unique_ptr thread_; }; #endif /* __ANDROID_CAMERA_STREAM__ */ diff --git a/src/android/post_processor.h b/src/android/post_processor.h index 48ddd8ac..fdfd52d3 100644 --- a/src/android/post_processor.h +++ b/src/android/post_processor.h @@ -7,6 +7,7 @@ #ifndef __ANDROID_POST_PROCESSOR_H__ #define __ANDROID_POST_PROCESSOR_H__ +#include #include #include @@ -18,7 +19,7 @@ class CameraMetadata; struct Camera3RequestDescriptor; -class PostProcessor +class PostProcessor : public libcamera::Object { public: virtual ~PostProcessor() = default;