Patch Detail
Show a patch.
GET /api/patches/12358/?format=api
{ "id": 12358, "url": "https://patchwork.libcamera.org/api/patches/12358/?format=api", "web_url": "https://patchwork.libcamera.org/patch/12358/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/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": "<20210521154227.60186-9-jacopo@jmondi.org>", "date": "2021-05-21T15:42:27", "name": "[libcamera-devel,v3,8/8] android: Implement flush() camera operation", "commit_ref": null, "pull_url": null, "state": "superseded", "archived": false, "hash": "1b75f4e7ce43c89748d89d868bf83e2cce7c723d", "submitter": { "id": 3, "url": "https://patchwork.libcamera.org/api/people/3/?format=api", "name": "Jacopo Mondi", "email": "jacopo@jmondi.org" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/12358/mbox/", "series": [ { "id": 2045, "url": "https://patchwork.libcamera.org/api/series/2045/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=2045", "date": "2021-05-21T15:42:19", "name": "Implement flush() camera operation", "version": 3, "mbox": "https://patchwork.libcamera.org/series/2045/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/12358/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/12358/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 D2E9AC31FF\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 21 May 2021 15:41:58 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 8E87D68920;\n\tFri, 21 May 2021 17:41:58 +0200 (CEST)", "from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net\n\t[217.70.183.201])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 449616891B\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 21 May 2021 17:41:53 +0200 (CEST)", "(Authenticated sender: jacopo@jmondi.org)\n\tby relay8-d.mail.gandi.net (Postfix) with ESMTPSA id B66911BF215;\n\tFri, 21 May 2021 15:41:52 +0000 (UTC)" ], "From": "Jacopo Mondi <jacopo@jmondi.org>", "To": "libcamera-devel@lists.libcamera.org", "Date": "Fri, 21 May 2021 17:42:27 +0200", "Message-Id": "<20210521154227.60186-9-jacopo@jmondi.org>", "X-Mailer": "git-send-email 2.31.1", "In-Reply-To": "<20210521154227.60186-1-jacopo@jmondi.org>", "References": "<20210521154227.60186-1-jacopo@jmondi.org>", "MIME-Version": "1.0", "Content-Transfer-Encoding": "8bit", "Subject": "[libcamera-devel] [PATCH v3 8/8] android: Implement flush() camera\n\toperation", "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": "Implement the flush() camera operation in the CameraDevice class\nand make it available to the camera framework by implementing the\noperation wrapper in camera_ops.cpp.\n\nThe flush() implementation stops the Camera and the worker thread and\nwaits for all in-flight requests to be returned. Stopping the Camera\nforces all Requests already queued to be returned immediately in error\nstate. As flush() has to wait until all of them have been returned, make it\nwait on a newly introduced condition variable which is notified by the\nrequest completion handler when the queue of pending requests has been\nexhausted.\n\nAs flush() can race with processCaptureRequest() protect the requests\nqueueing by introducing a new CameraState::CameraFlushing state that\nprocessCaptureRequest() inspects before queuing the Request to the\nCamera. If flush() has been called while processCaptureRequest() was\nexecuting, return the current Request immediately in error state.\n\nProtect potentially concurrent calls to close() and configureStreams()\nby inspecting the CameraState, and force a wait for any flush() call\nto complete before proceeding.\n\nSigned-off-by: Jacopo Mondi <jacopo@jmondi.org>\n---\n src/android/camera_device.cpp | 90 +++++++++++++++++++++++++++++++++--\n src/android/camera_device.h | 9 +++-\n src/android/camera_ops.cpp | 8 +++-\n 3 files changed, 100 insertions(+), 7 deletions(-)", "diff": "diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp\nindex 3fce14035718..899afaa49439 100644\n--- a/src/android/camera_device.cpp\n+++ b/src/android/camera_device.cpp\n@@ -750,16 +750,65 @@ int CameraDevice::open(const hw_module_t *hardwareModule)\n \n void CameraDevice::close()\n {\n-\tstreams_.clear();\n+\tMutexLocker cameraLock(cameraMutex_);\n+\tif (state_ == CameraFlushing) {\n+\t\tflushed_.wait(cameraLock, [&] { return state_ != CameraStopped; });\n+\t\tcamera_->release();\n \n+\t\treturn;\n+\t}\n+\n+\tstreams_.clear();\n \tstop();\n \n \tcamera_->release();\n }\n \n-void CameraDevice::stop()\n+/*\n+ * Flush is similar to stop() but sets the camera state to 'flushing' and wait\n+ * until all the in-flight requests have been returned before setting the\n+ * camera state to stopped.\n+ *\n+ * Once flushing is done it unlocks concurrent calls to camera close() and\n+ * configureStreams().\n+ */\n+void CameraDevice::flush()\n {\n+\t{\n+\t\tMutexLocker cameraLock(cameraMutex_);\n+\n+\t\tif (state_ != CameraRunning)\n+\t\t\treturn;\n+\n+\t\tworker_.stop();\n+\t\tcamera_->stop();\n+\t\tstate_ = CameraFlushing;\n+\t}\n+\n+\t/*\n+\t * Now wait for all the in-flight requests to be completed before\n+\t * continuing. Stopping the Camera guarantees that all in-flight\n+\t * requests are completed in error state.\n+\t */\n+\t{\n+\t\tMutexLocker requestsLock(requestsMutex_);\n+\t\tflushing_.wait(requestsLock, [&] { return descriptors_.empty(); });\n+\t}\n+\n+\t/*\n+\t * Set state to stopped and unlock close() or configureStreams() that\n+\t * might be waiting for flush to be completed.\n+\t */\n \tMutexLocker cameraLock(cameraMutex_);\n+\tstate_ = CameraStopped;\n+\tflushed_.notify_one();\n+}\n+\n+/* Calls to stop() must be protected by cameraMutex_ being held by the caller. */\n+void CameraDevice::stop()\n+{\n+\tASSERT(state_ != CameraFlushing);\n+\n \tif (state_ == CameraStopped)\n \t\treturn;\n \n@@ -1581,8 +1630,18 @@ PixelFormat CameraDevice::toPixelFormat(int format) const\n */\n int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list)\n {\n-\t/* Before any configuration attempt, stop the camera. */\n-\tstop();\n+\t{\n+\t\t/*\n+\t\t * If a flush is in progress, wait for it to complete and to\n+\t\t * stop the camera, otherwise before any new configuration\n+\t\t * attempt we have to stop the camera explictely.\n+\t\t */\n+\t\tMutexLocker cameraLock(cameraMutex_);\n+\t\tif (state_ == CameraFlushing)\n+\t\t\tflushed_.wait(cameraLock, [&] { return state_ != CameraStopped; });\n+\t\telse\n+\t\t\tstop();\n+\t}\n \n \tif (stream_list->num_streams == 0) {\n \t\tLOG(HAL, Error) << \"No streams in configuration\";\n@@ -1950,6 +2009,25 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques\n \tif (ret)\n \t\treturn ret;\n \n+\t/*\n+\t * Just before queuing the request, make sure flush() has not\n+\t * been called after this function has been executed. In that\n+\t * case, immediately return the request with errors.\n+\t */\n+\tMutexLocker cameraLock(cameraMutex_);\n+\tif (state_ == CameraFlushing || state_ == CameraStopped) {\n+\t\tfor (camera3_stream_buffer_t &buffer : descriptor.buffers_) {\n+\t\t\tbuffer.status = CAMERA3_BUFFER_STATUS_ERROR;\n+\t\t\tbuffer.release_fence = buffer.acquire_fence;\n+\t\t}\n+\n+\t\tnotifyError(descriptor.frameNumber_,\n+\t\t\t descriptor.buffers_[0].stream,\n+\t\t\t CAMERA3_MSG_ERROR_REQUEST);\n+\n+\t\treturn 0;\n+\t}\n+\n \tworker_.queueRequest(descriptor.request_.get());\n \n \t{\n@@ -1979,6 +2057,10 @@ void CameraDevice::requestComplete(Request *request)\n \t\t\treturn;\n \t\t}\n \n+\t\t/* Release flush if all the pending requests have been completed. */\n+\t\tif (descriptors_.empty())\n+\t\t\tflushing_.notify_one();\n+\n \t\tnode = descriptors_.extract(it);\n \t}\n \tCamera3RequestDescriptor &descriptor = node.mapped();\ndiff --git a/src/android/camera_device.h b/src/android/camera_device.h\nindex 7cf8e8370387..e1b3bf7d30f2 100644\n--- a/src/android/camera_device.h\n+++ b/src/android/camera_device.h\n@@ -7,6 +7,7 @@\n #ifndef __ANDROID_CAMERA_DEVICE_H__\n #define __ANDROID_CAMERA_DEVICE_H__\n \n+#include <condition_variable>\n #include <map>\n #include <memory>\n #include <mutex>\n@@ -42,6 +43,7 @@ public:\n \n \tint open(const hw_module_t *hardwareModule);\n \tvoid close();\n+\tvoid flush();\n \n \tunsigned int id() const { return id_; }\n \tcamera3_device_t *camera3Device() { return &camera3Device_; }\n@@ -92,6 +94,7 @@ private:\n \tenum State {\n \t\tCameraStopped,\n \t\tCameraRunning,\n+\t\tCameraFlushing,\n \t};\n \n \tvoid stop();\n@@ -120,8 +123,9 @@ private:\n \n \tCameraWorker worker_;\n \n-\tlibcamera::Mutex cameraMutex_; /* Protects access to the camera state. */\n+\tlibcamera::Mutex cameraMutex_; /* Protects the camera state and flushed_. */\n \tState state_;\n+\tstd::condition_variable flushed_;\n \n \tstd::shared_ptr<libcamera::Camera> camera_;\n \tstd::unique_ptr<libcamera::CameraConfiguration> config_;\n@@ -134,8 +138,9 @@ private:\n \tstd::map<int, libcamera::PixelFormat> formatsMap_;\n \tstd::vector<CameraStream> streams_;\n \n-\tlibcamera::Mutex requestsMutex_; /* Protects descriptors_. */\n+\tlibcamera::Mutex requestsMutex_; /* Protects descriptors_ and flushing_. */\n \tstd::map<uint64_t, Camera3RequestDescriptor> descriptors_;\n+\tstd::condition_variable flushing_;\n \n \tstd::string maker_;\n \tstd::string model_;\ndiff --git a/src/android/camera_ops.cpp b/src/android/camera_ops.cpp\nindex 696e80436821..8a3cfa175ff5 100644\n--- a/src/android/camera_ops.cpp\n+++ b/src/android/camera_ops.cpp\n@@ -66,8 +66,14 @@ static void hal_dev_dump([[maybe_unused]] const struct camera3_device *dev,\n {\n }\n \n-static int hal_dev_flush([[maybe_unused]] const struct camera3_device *dev)\n+static int hal_dev_flush(const struct camera3_device *dev)\n {\n+\tif (!dev)\n+\t\treturn -EINVAL;\n+\n+\tCameraDevice *camera = reinterpret_cast<CameraDevice *>(dev->priv);\n+\tcamera->flush();\n+\n \treturn 0;\n }\n \n", "prefixes": [ "libcamera-devel", "v3", "8/8" ] }