From patchwork Fri Sep 10 07:06:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Umang Jain X-Patchwork-Id: 13804 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 53416BDC71 for ; Fri, 10 Sep 2021 07:07:02 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 248BE69177; Fri, 10 Sep 2021 09:07:02 +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="coRZ+SeZ"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 90BD069169 for ; Fri, 10 Sep 2021 09:06:58 +0200 (CEST) Received: from perceval.ideasonboard.com (unknown [103.251.226.149]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 8F6F6BC0; Fri, 10 Sep 2021 09:06:57 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1631257618; bh=OqW2lZGCcnuofLg+JxPbMd736XyeEBFLOPyj2TNi1wA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=coRZ+SeZyQrRcxXa726pBAyCt3dyoj728WCg2o/jjCGsnjy5G6o6Ctmouj/CDGp6M dVMlL3qS2RH6amAiUX36bxCRfEe5vZQNoedIpr7AKWF4X51PTFFsQi1ZTm5Q5iZpg+ p1XWVqNLUkDFm6y/uUeqbEiid6fFglWLSNsEZqSk= From: Umang Jain To: libcamera-devel@lists.libcamera.org Date: Fri, 10 Sep 2021 12:36:35 +0530 Message-Id: <20210910070638.467294-7-umang.jain@ideasonboard.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210910070638.467294-1-umang.jain@ideasonboard.com> References: <20210910070638.467294-1-umang.jain@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 6/9] android: camera_device: Add a queue for sending capture results 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" When a camera capture request completes, the next step is to send the capture results to the framework via process_capture_results(). All the capture associated result metadata is prepared and populated. If any post-processing is required, it will also take place in the same thread (which can be blocking for a subtaintial amount of time) before the results can be sent back to the framework. Subsequent patches will move the post-processing to run in a separate thread. In order to do so, there is few bits of groundwork which this patch entails. Mainly, we need to preserve the order of sending the capture results back in which they were queued by the framework to the HAL (i.e. the order is sequential). Hence, we need to add a queue in which capture results can be queued with context. As per this patch, the post-processor still runs synchronously as before, but it will queue up the current capture results with context. Later down the line, the capture results are dequeud in order and sent back to the framework via sendQueuedCaptureResults(). If the queue is empty or unused, the current behaviour is to skip the queue and send capture results directly. The context is preserved using Camera3RequestDescriptor utility structure. It has been expanded accordingly to address the needs of the functionality. --- Check if we can drop unique_ptr to camera3descriptor --- Signed-off-by: Umang Jain --- src/android/camera_device.cpp | 106 ++++++++++++++++++++++++++++++---- src/android/camera_device.h | 22 +++++++ src/android/camera_stream.cpp | 2 + 3 files changed, 119 insertions(+), 11 deletions(-) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index 84549d04..7f04d225 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -240,6 +240,8 @@ Camera3RequestDescriptor::Camera3RequestDescriptor( /* Clone the controls associated with the camera3 request. */ settings_ = CameraMetadata(camera3Request->settings); + internalBuffer_ = nullptr; + /* * Create the CaptureRequest, stored as a unique_ptr<> to tie its * lifetime to the descriptor. @@ -989,6 +991,7 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques * once it has been processed. */ buffer = cameraStream->getBuffer(); + descriptor.internalBuffer_ = buffer; LOG(HAL, Debug) << ss.str() << " (internal)"; break; } @@ -1148,26 +1151,107 @@ void CameraDevice::requestComplete(Request *request) continue; } + + /* + * Save the current context of capture result and queue the + * descriptor before processing the camera stream. + * + * When the processing completes, the descriptor will be + * dequeued and capture results will be sent to the framework. + */ + descriptor.status_ = Camera3RequestDescriptor::Pending; + descriptor.resultMetadata_ = std::move(resultMetadata); + descriptor.captureResult_ = captureResult; + + std::unique_ptr reqDescriptor = + std::make_unique(); + *reqDescriptor = std::move(descriptor); + queuedDescriptor_.push_back(std::move(reqDescriptor)); + + Camera3RequestDescriptor *currentDescriptor = queuedDescriptor_.back().get(); int ret = cameraStream->process(src, *buffer.buffer, - descriptor.settings_, - resultMetadata.get(), - &descriptor); + currentDescriptor->settings_, + currentDescriptor->resultMetadata_.get(), + currentDescriptor); + return; + } + + if (queuedDescriptor_.empty()) { + captureResult.result = resultMetadata->get(); + callbacks_->process_capture_result(callbacks_, &captureResult); + } else { + /* + * Previous capture results waiting to be sent to framework + * hence, queue the current capture results as well. After that, + * check if any results are ready to be sent back to the + * framework. + */ + descriptor.status_ = Camera3RequestDescriptor::Succeeded; + descriptor.resultMetadata_ = std::move(resultMetadata); + descriptor.captureResult_ = captureResult; + std::unique_ptr reqDescriptor = + std::make_unique(); + *reqDescriptor = std::move(descriptor); + queuedDescriptor_.push_back(std::move(reqDescriptor)); + + sendQueuedCaptureResults(); + } +} + +void CameraDevice::streamProcessingComplete(CameraStream *cameraStream, + CameraStream::ProcessStatus status, + const Camera3RequestDescriptor *context) +{ + for (auto &d : queuedDescriptor_) { + if (d.get() != context) + continue; + /* * Return the FrameBuffer to the CameraStream now that we're * done processing it. */ if (cameraStream->type() == CameraStream::Type::Internal) - cameraStream->putBuffer(src); - - if (ret) { - buffer.status = CAMERA3_BUFFER_STATUS_ERROR; - notifyError(descriptor.frameNumber_, buffer.stream, - CAMERA3_MSG_ERROR_BUFFER); + cameraStream->putBuffer(d->internalBuffer_); + + if (status == CameraStream::ProcessStatus::Succeeded) { + d->status_ = Camera3RequestDescriptor::Succeeded; + } else { + d->status_ = Camera3RequestDescriptor::Failed; + + d->captureResult_.partial_result = 0; + for (camera3_stream_buffer_t &buffer : d->buffers_) { + CameraStream *cs = static_cast(buffer.stream->priv); + + if (cs->camera3Stream().format != HAL_PIXEL_FORMAT_BLOB) + continue; + buffer.status = CAMERA3_BUFFER_STATUS_ERROR; + notifyError(d->frameNumber_, buffer.stream, + CAMERA3_MSG_ERROR_BUFFER); + } } + break; } - captureResult.result = resultMetadata->get(); - callbacks_->process_capture_result(callbacks_, &captureResult); + /* + * Send back capture results to the framework by inspecting the queue. + * The framework can defer queueing further requests to the HAL (via + * process_capture_request) unless until it receives the capture results + * for already queued requests. + */ + sendQueuedCaptureResults(); +} + +void CameraDevice::sendQueuedCaptureResults() +{ + while (!queuedDescriptor_.empty()) { + std::unique_ptr &d = queuedDescriptor_.front(); + if (d->status_ == Camera3RequestDescriptor::Pending) + return; + + d->captureResult_.result = d->resultMetadata_->get(); + callbacks_->process_capture_result(callbacks_, &(d->captureResult_)); + queuedDescriptor_.pop_front(); + } } std::string CameraDevice::logPrefix() const diff --git a/src/android/camera_device.h b/src/android/camera_device.h index b59ac3e7..e7318358 100644 --- a/src/android/camera_device.h +++ b/src/android/camera_device.h @@ -7,6 +7,7 @@ #ifndef __ANDROID_CAMERA_DEVICE_H__ #define __ANDROID_CAMERA_DEVICE_H__ +#include #include #include #include @@ -46,6 +47,20 @@ struct Camera3RequestDescriptor { std::vector> frameBuffers_; CameraMetadata settings_; std::unique_ptr request_; + + /* + * Below are utility placeholders used when a capture result + * needs to be queued before completion via process_capture_result() + */ + enum completionStatus { + Pending, + Succeeded, + Failed, + }; + std::unique_ptr resultMetadata_; + camera3_capture_result_t captureResult_; + libcamera::FrameBuffer *internalBuffer_; + completionStatus status_; }; class CameraDevice : protected libcamera::Loggable @@ -78,6 +93,9 @@ public: int processCaptureRequest(camera3_capture_request_t *request); void requestComplete(libcamera::Request *request); + void streamProcessingComplete(CameraStream *stream, + CameraStream::ProcessStatus status, + const Camera3RequestDescriptor *context); protected: std::string logPrefix() const override; @@ -106,6 +124,8 @@ private: std::unique_ptr getResultMetadata( const Camera3RequestDescriptor &descriptor) const; + void sendQueuedCaptureResults(); + unsigned int id_; camera3_device_t camera3Device_; @@ -126,6 +146,8 @@ private: libcamera::Mutex descriptorsMutex_; /* Protects descriptors_. */ std::map descriptors_; + std::deque> queuedDescriptor_; + std::string maker_; std::string model_; diff --git a/src/android/camera_stream.cpp b/src/android/camera_stream.cpp index 996779c4..c7d874b2 100644 --- a/src/android/camera_stream.cpp +++ b/src/android/camera_stream.cpp @@ -134,6 +134,8 @@ void CameraStream::postProcessingComplete(PostProcessor::Status status, processStatus = ProcessStatus::Succeeded; else processStatus = ProcessStatus::Failed; + + cameraDevice_->streamProcessingComplete(this, processStatus, context); } FrameBuffer *CameraStream::getBuffer()