Patch Detail
Show a patch.
GET /api/1.1/patches/14191/?format=api
{ "id": 14191, "url": "https://patchwork.libcamera.org/api/1.1/patches/14191/?format=api", "web_url": "https://patchwork.libcamera.org/patch/14191/", "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": "<20211020104212.121743-4-umang.jain@ideasonboard.com>", "date": "2021-10-20T10:42:11", "name": "[libcamera-devel,v5,3/4] android: post_processor: Make post processing async", "commit_ref": null, "pull_url": null, "state": "superseded", "archived": false, "hash": "b6fbf3116fa052e173fcf2579cb077027a4c6f14", "submitter": { "id": 86, "url": "https://patchwork.libcamera.org/api/1.1/people/86/?format=api", "name": "Umang Jain", "email": "umang.jain@ideasonboard.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/14191/mbox/", "series": [ { "id": 2640, "url": "https://patchwork.libcamera.org/api/1.1/series/2640/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=2640", "date": "2021-10-20T10:42:08", "name": "Async Post Processor", "version": 5, "mbox": "https://patchwork.libcamera.org/series/2640/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/14191/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/14191/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 7BF09BF415\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 20 Oct 2021 10:42:27 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 3902E68F5E;\n\tWed, 20 Oct 2021 12:42:27 +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 26F0868F59\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 20 Oct 2021 12:42:25 +0200 (CEST)", "from perceval.ideasonboard.com (unknown [103.251.226.185])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 284DA3F6;\n\tWed, 20 Oct 2021 12:42:23 +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=\"HS27UytA\"; dkim-atps=neutral", "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1634726544;\n\tbh=ogsSJjRL0Tw4mCch/KO7enP8OtimsXDMgdkD3ke7dZc=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=HS27UytAgOXJv8LvpNsf0MFsl5aRTWrTpIM5S/nWLdcRsjJtVBUcMR7jrah4a1x4v\n\tNKbak4SWxsUCkJhv66nCk6SEbCfxR57yPH+PyaZGzAtbgX+tg4/6hrhcG316mhypwZ\n\tOH8oPhS8/6vVjRNXL6oDczBJhyr32N549YNdYSEs=", "From": "Umang Jain <umang.jain@ideasonboard.com>", "To": "libcamera-devel@lists.libcamera.org", "Date": "Wed, 20 Oct 2021 16:12:11 +0530", "Message-Id": "<20211020104212.121743-4-umang.jain@ideasonboard.com>", "X-Mailer": "git-send-email 2.31.1", "In-Reply-To": "<20211020104212.121743-1-umang.jain@ideasonboard.com>", "References": "<20211020104212.121743-1-umang.jain@ideasonboard.com>", "MIME-Version": "1.0", "Content-Transfer-Encoding": "8bit", "Subject": "[libcamera-devel] [PATCH v5 3/4] android: post_processor: Make post\n\tprocessing async", "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": "Introduce a dedicated worker class derived from libcamera::Thread.\nThe worker class maintains a queue for post-processing requests\nand waits for a post-processing request to become available.\nIt will process them as per FIFO before de-queuing it from the\nqueue.\n\nTo get access to the source and destination buffers in the worker\nthread, we also need to save a pointer to them in the\nCamera3RequestDescriptor.\n\nThis patch also implements a flush() for the PostProcessorWorker\nclass which is responsible to purge post-processing requests\nqueued up while a camera is stopping/flushing. It is hooked with\nCameraStream::flush(), which isn't used currently but will be\nused when we handle flush/stop scnearios in greater detail\nsubsequently (in a different patchset).\n\nThe libcamera request completion handler CameraDevice::requestComplete()\nassumes that the request that has just completed is at the front of the\nqueue. Now that the post-processor runs asynchronously, this isn't true\nanymore, a request being post-processed will stay in the queue and a new\nlibcamera request may complete. Remove that assumption, and use the\nrequest cookie to obtain the Camera3RequestDescriptor.\n\nSigned-off-by: Umang Jain <umang.jain@ideasonboard.com>\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n src/android/camera_device.cpp | 46 +++++----------\n src/android/camera_request.h | 4 ++\n src/android/camera_stream.cpp | 107 +++++++++++++++++++++++++++++++---\n src/android/camera_stream.h | 38 +++++++++++-\n 4 files changed, 156 insertions(+), 39 deletions(-)", "diff": "diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp\nindex 541c2c81..b14416ce 100644\n--- a/src/android/camera_device.cpp\n+++ b/src/android/camera_device.cpp\n@@ -1020,29 +1020,8 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques\n \n void CameraDevice::requestComplete(Request *request)\n {\n-\tCamera3RequestDescriptor *descriptor;\n-\t{\n-\t\tMutexLocker descriptorsLock(descriptorsMutex_);\n-\t\tASSERT(!descriptors_.empty());\n-\t\tdescriptor = descriptors_.front().get();\n-\t}\n-\n-\tif (descriptor->request_->cookie() != request->cookie()) {\n-\t\t/*\n-\t\t * \\todo Clarify if the Camera has to be closed on\n-\t\t * ERROR_DEVICE.\n-\t\t */\n-\t\tLOG(HAL, Error)\n-\t\t\t<< \"Out-of-order completion for request \"\n-\t\t\t<< utils::hex(request->cookie());\n-\n-\t\tMutexLocker descriptorsLock(descriptorsMutex_);\n-\t\tdescriptors_.pop();\n-\n-\t\tnotifyError(0, nullptr, CAMERA3_MSG_ERROR_DEVICE);\n-\n-\t\treturn;\n-\t}\n+\tCamera3RequestDescriptor *descriptor =\n+\t\treinterpret_cast<Camera3RequestDescriptor *>(request->cookie());\n \n \t/*\n \t * Prepare the capture result for the Android camera stack.\n@@ -1120,12 +1099,7 @@ void CameraDevice::requestComplete(Request *request)\n \tbool needsPostProcessing = false;\n \tCamera3RequestDescriptor::Status processingStatus =\n \t\tCamera3RequestDescriptor::Status::Pending;\n-\t/*\n-\t * \\todo Apply streamsProcessMutex_ when post-processing is adapted to run\n-\t * asynchronously. If we introduce the streamsProcessMutex_ right away, the\n-\t * lock will be held forever since it is synchronous at this point\n-\t * (see streamProcessingComplete).\n-\t */\n+\n \tfor (auto &buffer : descriptor->buffers_) {\n \t\tCameraStream *stream = buffer.stream;\n \n@@ -1145,7 +1119,13 @@ void CameraDevice::requestComplete(Request *request)\n \t\t\tbuffer.internalBuffer = src;\n \n \t\tneedsPostProcessing = true;\n-\t\tint ret = stream->process(*src, buffer, descriptor);\n+\n+\t\tint ret;\n+\t\t{\n+\t\t\tMutexLocker locker(descriptor->streamsProcessMutex_);\n+\t\t\tret = stream->process(*src, buffer, descriptor);\n+\t\t}\n+\n \t\tif (ret) {\n \t\t\tsetBufferStatus(stream, buffer, descriptor,\n \t\t\t\t\tCamera3RequestDescriptor::Status::Error);\n@@ -1156,6 +1136,12 @@ void CameraDevice::requestComplete(Request *request)\n \tif (needsPostProcessing) {\n \t\tif (processingStatus == Camera3RequestDescriptor::Status::Error) {\n \t\t\tdescriptor->status_ = processingStatus;\n+\t\t\t/*\n+\t\t\t * \\todo This is problematic when some streams are\n+\t\t\t * queued successfully, but some fail to get queued.\n+\t\t\t * We might end up with use-after-free situation for\n+\t\t\t * descriptor in streamProcessingComplete().\n+\t\t\t */\n \t\t\tsendCaptureResults();\n \t\t}\n \ndiff --git a/src/android/camera_request.h b/src/android/camera_request.h\nindex 3a2774e0..85274414 100644\n--- a/src/android/camera_request.h\n+++ b/src/android/camera_request.h\n@@ -18,6 +18,7 @@\n \n #include <hardware/camera3.h>\n \n+#include \"camera_buffer.h\"\n #include \"camera_metadata.h\"\n #include \"camera_worker.h\"\n \n@@ -39,6 +40,9 @@ public:\n \t\tint fence;\n \t\tStatus status;\n \t\tlibcamera::FrameBuffer *internalBuffer = nullptr;\n+\t\tstd::unique_ptr<CameraBuffer> destBuffer;\n+\t\tconst libcamera::FrameBuffer *srcBuffer;\n+\t\tCamera3RequestDescriptor *request = nullptr;\n \t};\n \n \tCamera3RequestDescriptor(libcamera::Camera *camera,\ndiff --git a/src/android/camera_stream.cpp b/src/android/camera_stream.cpp\nindex b3cb06a2..a29ce33b 100644\n--- a/src/android/camera_stream.cpp\n+++ b/src/android/camera_stream.cpp\n@@ -99,6 +99,7 @@ int CameraStream::configure()\n \t\tif (ret)\n \t\t\treturn ret;\n \n+\t\tworker_ = std::make_unique<PostProcessorWorker>(postProcessor_.get());\n \t\tpostProcessor_->processComplete.connect(\n \t\t\tthis, [&](Camera3RequestDescriptor *request, PostProcessor::Status status) {\n \t\t\t\tCamera3RequestDescriptor::Status bufferStatus =\n@@ -110,6 +111,7 @@ int CameraStream::configure()\n \t\t\t\tcameraDevice_->streamProcessingComplete(this, request,\n \t\t\t\t\t\t\t\t\tbufferStatus);\n \t\t\t});\n+\t\tworker_->start();\n \t}\n \n \tif (type_ == Type::Internal) {\n@@ -179,23 +181,31 @@ int CameraStream::process(const FrameBuffer &source,\n \tif (!postProcessor_)\n \t\treturn 0;\n \n-\t/*\n-\t * \\todo Buffer mapping and processing should be moved to a\n-\t * separate thread.\n-\t */\n \tconst StreamConfiguration &output = configuration();\n-\tCameraBuffer destBuffer(*dest.camera3Buffer, output.pixelFormat,\n-\t\t\t\toutput.size, PROT_READ | PROT_WRITE);\n-\tif (!destBuffer.isValid()) {\n+\tdest.destBuffer = std::make_unique<CameraBuffer>(\n+\t\t*dest.camera3Buffer, output.pixelFormat, output.size, PROT_READ | PROT_WRITE);\n+\tif (!dest.destBuffer->isValid()) {\n \t\tLOG(HAL, Error) << \"Failed to create destination buffer\";\n \t\treturn -EINVAL;\n \t}\n \n-\tpostProcessor_->process(source, &destBuffer, request);\n+\tdest.srcBuffer = &source;\n+\tdest.request = request;\n+\n+\t/* Push the postProcessor request to the worker queue. */\n+\tworker_->queueRequest(&dest);\n \n \treturn 0;\n }\n \n+void CameraStream::flush()\n+{\n+\tif (!postProcessor_)\n+\t\treturn;\n+\n+\tworker_->flush();\n+}\n+\n FrameBuffer *CameraStream::getBuffer()\n {\n \tif (!allocator_)\n@@ -223,3 +233,84 @@ void CameraStream::putBuffer(FrameBuffer *buffer)\n \n \tbuffers_.push_back(buffer);\n }\n+\n+CameraStream::PostProcessorWorker::PostProcessorWorker(PostProcessor *postProcessor)\n+\t: postProcessor_(postProcessor)\n+{\n+}\n+\n+CameraStream::PostProcessorWorker::~PostProcessorWorker()\n+{\n+\t{\n+\t\tlibcamera::MutexLocker lock(mutex_);\n+\t\tstate_ = State::Stopped;\n+\t}\n+\n+\tcv_.notify_one();\n+\twait();\n+}\n+\n+void CameraStream::PostProcessorWorker::start()\n+{\n+\t{\n+\t\tlibcamera::MutexLocker lock(mutex_);\n+\t\tstate_ = State::Running;\n+\t}\n+\n+\tThread::start();\n+}\n+\n+void CameraStream::PostProcessorWorker::queueRequest(Camera3RequestDescriptor::StreamBuffer *dest)\n+{\n+\t{\n+\t\tMutexLocker lock(mutex_);\n+\t\tASSERT(state_ == State::Running);\n+\t\trequests_.push(dest);\n+\t}\n+\n+\tcv_.notify_one();\n+}\n+\n+void CameraStream::PostProcessorWorker::run()\n+{\n+\tMutexLocker locker(mutex_);\n+\n+\twhile (1) {\n+\t\tcv_.wait(locker, [&] {\n+\t\t\treturn state_ != State::Running || !requests_.empty();\n+\t\t});\n+\n+\t\tif (state_ != State::Running)\n+\t\t\tbreak;\n+\n+\t\tCamera3RequestDescriptor::StreamBuffer *stream = requests_.front();\n+\t\trequests_.pop();\n+\t\tlocker.unlock();\n+\n+\t\tpostProcessor_->process(*stream->srcBuffer, stream->destBuffer.get(),\n+\t\t\t\t\tstream->request);\n+\n+\t\tlocker.lock();\n+\t}\n+\n+\tif (state_ == State::Flushing) {\n+\t\twhile (!requests_.empty()) {\n+\t\t\tpostProcessor_->processComplete.emit(\n+\t\t\t\trequests_.front()->request,\n+\t\t\t\tPostProcessor::Status::Error);\n+\t\t\trequests_.pop();\n+\t\t}\n+\n+\t\tstate_ = State::Stopped;\n+\t\tlocker.unlock();\n+\t}\n+}\n+\n+void CameraStream::PostProcessorWorker::flush()\n+{\n+\tlibcamera::MutexLocker lock(mutex_);\n+\tstate_ = State::Flushing;\n+\tlock.unlock();\n+\n+\tcv_.notify_one();\n+}\ndiff --git a/src/android/camera_stream.h b/src/android/camera_stream.h\nindex f242336e..64e32c77 100644\n--- a/src/android/camera_stream.h\n+++ b/src/android/camera_stream.h\n@@ -7,12 +7,16 @@\n #ifndef __ANDROID_CAMERA_STREAM_H__\n #define __ANDROID_CAMERA_STREAM_H__\n \n+#include <condition_variable>\n #include <memory>\n #include <mutex>\n+#include <queue>\n #include <vector>\n \n #include <hardware/camera3.h>\n \n+#include <libcamera/base/thread.h>\n+\n #include <libcamera/camera.h>\n #include <libcamera/framebuffer.h>\n #include <libcamera/framebuffer_allocator.h>\n@@ -20,9 +24,9 @@\n #include <libcamera/pixel_format.h>\n \n #include \"camera_request.h\"\n+#include \"post_processor.h\"\n \n class CameraDevice;\n-class PostProcessor;\n \n class CameraStream\n {\n@@ -126,8 +130,38 @@ public:\n \t\t Camera3RequestDescriptor *request);\n \tlibcamera::FrameBuffer *getBuffer();\n \tvoid putBuffer(libcamera::FrameBuffer *buffer);\n+\tvoid flush();\n \n private:\n+\tclass PostProcessorWorker : public libcamera::Thread\n+\t{\n+\tpublic:\n+\t\tenum class State {\n+\t\t\tStopped,\n+\t\t\tRunning,\n+\t\t\tFlushing,\n+\t\t};\n+\n+\t\tPostProcessorWorker(PostProcessor *postProcessor);\n+\t\t~PostProcessorWorker();\n+\n+\t\tvoid start();\n+\t\tvoid queueRequest(Camera3RequestDescriptor::StreamBuffer *request);\n+\t\tvoid flush();\n+\n+\tprotected:\n+\t\tvoid run() override;\n+\n+\tprivate:\n+\t\tPostProcessor *postProcessor_;\n+\n+\t\tlibcamera::Mutex mutex_;\n+\t\tstd::condition_variable cv_;\n+\n+\t\tstd::queue<Camera3RequestDescriptor::StreamBuffer *> requests_;\n+\t\tState state_;\n+\t};\n+\n \tint waitFence(int fence);\n \n \tCameraDevice *const cameraDevice_;\n@@ -144,6 +178,8 @@ private:\n \t */\n \tstd::unique_ptr<std::mutex> mutex_;\n \tstd::unique_ptr<PostProcessor> postProcessor_;\n+\n+\tstd::unique_ptr<PostProcessorWorker> worker_;\n };\n \n #endif /* __ANDROID_CAMERA_STREAM__ */\n", "prefixes": [ "libcamera-devel", "v5", "3/4" ] }