{"id":13972,"url":"https://patchwork.libcamera.org/api/1.1/patches/13972/?format=json","web_url":"https://patchwork.libcamera.org/patch/13972/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20210928162536.227475-4-umang.jain@ideasonboard.com>","date":"2021-09-28T16:25:36","name":"[libcamera-devel,v2,3/3] android: camera_device: Send capture results by inspecting the queue","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"4acb508c98e2329bd861451d0befde25ab7727c9","submitter":{"id":86,"url":"https://patchwork.libcamera.org/api/1.1/people/86/?format=json","name":"Umang Jain","email":"umang.jain@ideasonboard.com"},"delegate":{"id":12,"url":"https://patchwork.libcamera.org/api/1.1/users/12/?format=json","username":"uajain","first_name":"Umang","last_name":"Jain","email":"umang.jain@ideasonboard.com"},"mbox":"https://patchwork.libcamera.org/patch/13972/mbox/","series":[{"id":2583,"url":"https://patchwork.libcamera.org/api/1.1/series/2583/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2583","date":"2021-09-28T16:25:33","name":"Camera3RequestDescriptors std::map => std::queue","version":2,"mbox":"https://patchwork.libcamera.org/series/2583/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/13972/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/13972/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 EC4FDC3243\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 28 Sep 2021 16:26:22 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id ACA1869191;\n\tTue, 28 Sep 2021 18:26:22 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 1E6A469188\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 28 Sep 2021 18:26:22 +0200 (CEST)","from perceval.ideasonboard.com (unknown\n\t[IPv6:2405:204:8500:b152:4879:ab55:a9fb:2b15])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 3FC683F1;\n\tTue, 28 Sep 2021 18:26:06 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"X+tELMke\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1632846381;\n\tbh=wHxoNWgcPVtkASppd4qr+E6KCcn4LDJ/b6FMWSNmLQg=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=X+tELMkeztq0Qdy2kIgRAikVEYfwjXAmcYpOS4MyEVsZG6qzM9DxNE+J+bj2MW9Bs\n\tqYj24kY4fVgVkSSrXnTnEttyHWRE1wc0q3O8xgTHf5rXpHeozAbJ6MgP0XhxMC+ZLB\n\tnMZMKRsVKl5V4NIu8pFdsBw8RfrXCj8xleVUW4z8=","From":"Umang Jain <umang.jain@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Tue, 28 Sep 2021 21:55:36 +0530","Message-Id":"<20210928162536.227475-4-umang.jain@ideasonboard.com>","X-Mailer":"git-send-email 2.31.1","In-Reply-To":"<20210928162536.227475-1-umang.jain@ideasonboard.com>","References":"<20210928162536.227475-1-umang.jain@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v2 3/3] android: camera_device: Send\n\tcapture results by inspecting the queue","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":"There is a possibility that an out-of-order completion of capture\nrequest happens by calling process_capture_result() directly on error\npaths. The framework expects that errors should be notified as soon as\npossible, but the request completion order should remain intact.\nAn existing instance of this is abortRequest(), which sends the capture\nresults on flushing state, without considering order-of-completion.\n\nSince we have a queue of Camera3RequestDescriptor tracking each\ncapture request placed by framework to libcamera HAL, we should be only\nsending back capture results from a single location, by inspecting\nthe queue. As per the patch, this now happens in\nCameraDevice::sendCaptureResults().\n\nEach descriptor is now equipped with its own status to denote whether\nthe capture request is complete and ready to send back to the framework\nor needs to be waited upon. This ensures that the order of completion is\nrespected for the requests.\n\nSigned-off-by: Umang Jain <umang.jain@ideasonboard.com>\n---\n src/android/camera_device.cpp | 44 +++++++++++++++++++++++++----------\n src/android/camera_device.h   | 15 +++++++++++-\n 2 files changed, 46 insertions(+), 13 deletions(-)","diff":"diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp\nindex a3b8a549..83030366 100644\n--- a/src/android/camera_device.cpp\n+++ b/src/android/camera_device.cpp\n@@ -861,11 +861,12 @@ int CameraDevice::processControls(Camera3RequestDescriptor *descriptor)\n \treturn 0;\n }\n \n-void CameraDevice::abortRequest(camera3_capture_request_t *request)\n+void CameraDevice::abortRequest(Camera3RequestDescriptor *descriptor,\n+\t\t\t\tcamera3_capture_request_t *request)\n {\n \tnotifyError(request->frame_number, nullptr, CAMERA3_MSG_ERROR_REQUEST);\n \n-\tcamera3_capture_result_t result = {};\n+\tcamera3_capture_result_t &result = descriptor->captureResult_;\n \tresult.num_output_buffers = request->num_output_buffers;\n \tresult.frame_number = request->frame_number;\n \tresult.partial_result = 0;\n@@ -879,7 +880,7 @@ void CameraDevice::abortRequest(camera3_capture_request_t *request)\n \t}\n \tresult.output_buffers = resultBuffers.data();\n \n-\tcallbacks_->process_capture_result(callbacks_, &result);\n+\tdescriptor->status_ = Camera3RequestDescriptor::Status::Error;\n }\n \n bool CameraDevice::isValidRequest(camera3_capture_request_t *camera3Request) const\n@@ -1052,13 +1053,19 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques\n \t\treturn ret;\n \n \t/*\n-\t * If flush is in progress abort the request. If the camera has been\n-\t * stopped we have to re-start it to be able to process the request.\n+\t * If flush is in progress abort the request and push the descriptor to\n+\t * the queue. If the camera has been stopped we have to re-start it to\n+\t * be able to process the request.\n \t */\n \tMutexLocker stateLock(stateMutex_);\n \n \tif (state_ == State::Flushing) {\n-\t\tabortRequest(camera3Request);\n+\t\tabortRequest(descriptors_.back().get(), camera3Request);\n+\t\t{\n+\t\t\tMutexLocker descriptorsLock(descriptorsMutex_);\n+\t\t\tdescriptors_.push(std::move(descriptor));\n+\t\t}\n+\t\tsendCaptureResults();\n \t\treturn 0;\n \t}\n \n@@ -1116,7 +1123,7 @@ void CameraDevice::requestComplete(Request *request)\n \t * The buffer status is set to OK and later changed to ERROR if\n \t * post-processing/compression fails.\n \t */\n-\tcamera3_capture_result_t captureResult = {};\n+\tcamera3_capture_result_t &captureResult = descriptor->captureResult_;\n \tcaptureResult.frame_number = descriptor->frameNumber_;\n \tcaptureResult.num_output_buffers = descriptor->buffers_.size();\n \tfor (camera3_stream_buffer_t &buffer : descriptor->buffers_) {\n@@ -1166,9 +1173,9 @@ void CameraDevice::requestComplete(Request *request)\n \t\t\tbuffer.status = CAMERA3_BUFFER_STATUS_ERROR;\n \t\t}\n \n-\t\tcallbacks_->process_capture_result(callbacks_, &captureResult);\n+\t\tdescriptor->status_ = Camera3RequestDescriptor::Status::Error;\n+\t\tsendCaptureResults();\n \n-\t\tdescriptors_.pop();\n \t\treturn;\n \t}\n \n@@ -1234,10 +1241,23 @@ void CameraDevice::requestComplete(Request *request)\n \t}\n \n \tcaptureResult.result = resultMetadata->get();\n-\tcallbacks_->process_capture_result(callbacks_, &captureResult);\n+\tdescriptor->status_ = Camera3RequestDescriptor::Status::Success;\n+\tsendCaptureResults();\n+}\n \n-\tMutexLocker descriptorsLock(descriptorsMutex_);\n-\tdescriptors_.pop();\n+void CameraDevice::sendCaptureResults()\n+{\n+\tMutexLocker lock(descriptorsMutex_);\n+\twhile (!descriptors_.empty() && !descriptors_.front()->isPending()) {\n+\t\tstd::unique_ptr<Camera3RequestDescriptor> descriptor =\n+\t\t\tstd::move(descriptors_.front());\n+\t\tdescriptors_.pop();\n+\n+\t\tlock.unlock();\n+\t\tcallbacks_->process_capture_result(callbacks_,\n+\t\t\t\t\t\t   &descriptor->captureResult_);\n+\t\tlock.lock();\n+\t}\n }\n \n std::string CameraDevice::logPrefix() const\ndiff --git a/src/android/camera_device.h b/src/android/camera_device.h\nindex 9ec510d5..dbfa7431 100644\n--- a/src/android/camera_device.h\n+++ b/src/android/camera_device.h\n@@ -74,17 +74,28 @@ private:\n \tCameraDevice(unsigned int id, std::shared_ptr<libcamera::Camera> camera);\n \n \tstruct Camera3RequestDescriptor {\n+\t\tenum class Status {\n+\t\t\tPending,\n+\t\t\tSuccess,\n+\t\t\tError,\n+\t\t};\n+\n \t\tCamera3RequestDescriptor() = default;\n \t\t~Camera3RequestDescriptor() = default;\n \t\tCamera3RequestDescriptor(libcamera::Camera *camera,\n \t\t\t\t\t const camera3_capture_request_t *camera3Request);\n \t\tCamera3RequestDescriptor &operator=(Camera3RequestDescriptor &&) = default;\n \n+\t\tbool isPending() const { return status_ == Status::Pending; }\n+\n \t\tuint32_t frameNumber_ = 0;\n \t\tstd::vector<camera3_stream_buffer_t> buffers_;\n \t\tstd::vector<std::unique_ptr<libcamera::FrameBuffer>> frameBuffers_;\n \t\tCameraMetadata settings_;\n \t\tstd::unique_ptr<CaptureRequest> request_;\n+\n+\t\tcamera3_capture_result_t captureResult_ = {};\n+\t\tStatus status_ = Status::Pending;\n \t};\n \n \tenum class State {\n@@ -99,12 +110,14 @@ private:\n \tcreateFrameBuffer(const buffer_handle_t camera3buffer,\n \t\t\t  libcamera::PixelFormat pixelFormat,\n \t\t\t  const libcamera::Size &size);\n-\tvoid abortRequest(camera3_capture_request_t *request);\n+\tvoid abortRequest(Camera3RequestDescriptor *descriptor,\n+\t\t\t  camera3_capture_request_t *request);\n \tbool isValidRequest(camera3_capture_request_t *request) const;\n \tvoid notifyShutter(uint32_t frameNumber, uint64_t timestamp);\n \tvoid notifyError(uint32_t frameNumber, camera3_stream_t *stream,\n \t\t\t camera3_error_msg_code code);\n \tint processControls(Camera3RequestDescriptor *descriptor);\n+\tvoid sendCaptureResults();\n \tstd::unique_ptr<CameraMetadata> getResultMetadata(\n \t\tconst Camera3RequestDescriptor &descriptor) const;\n \n","prefixes":["libcamera-devel","v2","3/3"]}