From patchwork Tue Dec 10 14:24:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cheng-Hao Yang X-Patchwork-Id: 22275 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 DD16FC32DC for ; Tue, 10 Dec 2024 14:26:18 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 81B4A67E96; Tue, 10 Dec 2024 15:26:18 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="QemRdkVS"; dkim-atps=neutral Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 4618C67E88 for ; Tue, 10 Dec 2024 15:26:14 +0100 (CET) Received: by mail-pj1-x1036.google.com with SMTP id 98e67ed59e1d1-2ee51f8c47dso4136362a91.1 for ; Tue, 10 Dec 2024 06:26:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1733840772; x=1734445572; darn=lists.libcamera.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=QsaKE+pnPxyxFfHOwhaGtd2pcOwdYEqO6rU+o+44mQk=; b=QemRdkVSxr8G9kcjQQ7KWIeHiUlwAuEinwBmJfJ/ItRzGsUmF9NADlljaquNjwF1g7 v5bWk0Yckfad3NYUDx+x5TqaIwWYCA7qcnXXNbtgtB8UT/S+PnOBUc4iCdLiRKqwuGAd IoHjAPxC+LaEGuViKrziyoEWMaxzM2E6HelGw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733840772; x=1734445572; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QsaKE+pnPxyxFfHOwhaGtd2pcOwdYEqO6rU+o+44mQk=; b=awI1UodWgNSUifOEAuLwgcFx+keEKUi9cP9cKzCkzgz0162+jxp04flYIikzl+mie8 QjsP6FetK41aathUOV9HDk1bLb58Ncve6u7YGZpNUgPkkZPqSGf5B4ou/1FsPPVNTn9Z wS46wZu0ZxGFTmSdN+UvbbNaJNCX+mMTZyDVResbhzEKw3nKs3bukV1kDFzbIt0tW5o+ /Fc0c/raYeYMZ5sprdiIcMnmHKa8l6M2ekxswJL592K6JlkFufg6hoMz2RGgs4bt46oI 9/0i2Ghytho8XNmRIEZEoom3zZcxxu7j1jW231SA0yJiyT3HjTc2s3O3dMi5HWNOUXRN pJkg== X-Gm-Message-State: AOJu0YzcTttrTDPLj3E7dWYQjVsw7+HTp0SKCzosQ9sgNqH91DyeHwWk 2KFhZtEfULs6l0CR9HXXsyQculZKFYtv4p0thjbWMZI4+3tuxp8Mc0uaPa0SNFN9rqzhNbz7s4k = X-Gm-Gg: ASbGncv+NDrjZ8OMiyH+7RvK5JE7aWIWI/PFMoMMPiAtV/j52C04Pc+Bs65XxsL/miS nH4N101vI+cm39lyw+a2MghXEEI2qtk4zEWU6y2eP+3LkNAzQ3Yvok0A591rdr0KO7JuLtzJ8Wa PEgAHVVJWvI8rCy3M1wsorVYRnUZzv0pdNfBUCdxsTf1WpTctXPmvPW3eFgW2k085efxdLEJztT bZLzd3Ka/92ImvL2IzOAAUtfEC26Oqt0wG1n7Ny0B7DP1i600yG9o+jZ+Mf+OCMDGi+h4Hq9XH/ mgnquPBefsn+0Ldtfm6h0NLaaiuC9Z8AVCbXCLFfuh8HYDnuTyMVQXWeuBuS X-Google-Smtp-Source: AGHT+IE3YA0vbFyY+16obTdgKt7fZmBsAyX0Dut7K7I8LkoLktIsyBQnza30udAzvhms3Juelm7GCw== X-Received: by 2002:a17:90b:35d2:b0:2ee:f80c:6889 with SMTP id 98e67ed59e1d1-2efcf26e386mr7414361a91.33.1733840772468; Tue, 10 Dec 2024 06:26:12 -0800 (PST) Received: from chenghaoyang-low.c.googlers.com.com (112.212.229.35.bc.googleusercontent.com. [35.229.212.112]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2ef26ff97f2sm11812053a91.11.2024.12.10.06.26.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Dec 2024 06:26:12 -0800 (PST) From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Harvey Yang , Han-Lin Chen Subject: [PATCH v4 7/7] android: Remove Camera3RequestDescriptor::streamsProcessMutex_ Date: Tue, 10 Dec 2024 14:24:00 +0000 Message-ID: <20241210142557.2886315-8-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog In-Reply-To: <20241210142557.2886315-1-chenghaoyang@chromium.org> References: <20241210142557.2886315-1-chenghaoyang@chromium.org> MIME-Version: 1.0 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 mutex was needed when CameraStream's worker thread posts a result back to CameraDevice. We can simplify it by calling CameraDevice's function on libcamera::Camera's owner thread. With this delegation, `Camera3RequestDescriptor::pendingStreamsToProcess_` will be firstly setup in the application's thread in processCaptureRequest(), and the rest accesses will be done in libcamera::CameraManager's thread. Signed-off-by: Han-Lin Chen Co-developed-by: Harvey Yang Signed-off-by: Harvey Yang --- src/android/camera_device.cpp | 33 +++++++++++++++++---------------- src/android/camera_device.h | 6 ++++-- src/android/camera_request.h | 4 +--- src/android/camera_stream.cpp | 4 ++-- 4 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index a95114c8d..cb819938a 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -989,8 +989,6 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques FrameBuffer *frameBuffer = nullptr; UniqueFD acquireFence; - MutexLocker lock(descriptor->streamsProcessMutex_); - switch (cameraStream->type()) { case CameraStream::Type::Mapped: /* Mapped streams will be handled in the next loop. */ @@ -1066,7 +1064,6 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques << cameraStream->configuration().pixelFormat << "]" << " (mapped)"; - MutexLocker lock(descriptor->streamsProcessMutex_); descriptor->pendingStreamsToProcess_.insert({ cameraStream, &buffer }); /* @@ -1252,9 +1249,6 @@ void CameraDevice::requestComplete(Request *request) descriptor->resultMetadata_ = std::make_unique(0, 0); } - /* Handle post-processing. */ - MutexLocker locker(descriptor->streamsProcessMutex_); - /* * Queue all the post-processing streams request at once. The completion * slot streamProcessingComplete() can only execute when we are out @@ -1288,10 +1282,8 @@ void CameraDevice::requestComplete(Request *request) } } - if (descriptor->pendingStreamsToProcess_.empty()) { - locker.unlock(); + if (descriptor->pendingStreamsToProcess_.empty()) completeDescriptor(descriptor); - } } /** @@ -1398,6 +1390,19 @@ void CameraDevice::setBufferStatus(StreamBuffer &streamBuffer, } } +void CameraDevice::streamProcessingCompleteDelegate(StreamBuffer *streamBuffer, + StreamBuffer::Status status) +{ + /* + * Delegate the callback to the camera manager thread to simplify race condition. + */ + auto *method = new BoundMethodMember{ + this, camera_.get(), &CameraDevice::streamProcessingComplete, ConnectionTypeQueued + }; + + method->activate(streamBuffer, status); +} + /** * \brief Handle post-processing completion of a stream in a capture request * \param[in] streamBuffer The StreamBuffer for which processing is complete @@ -1418,13 +1423,9 @@ void CameraDevice::streamProcessingComplete(StreamBuffer *streamBuffer, Camera3RequestDescriptor *request = streamBuffer->request; - { - MutexLocker locker(request->streamsProcessMutex_); - - request->pendingStreamsToProcess_.erase(streamBuffer->stream); - if (!request->pendingStreamsToProcess_.empty()) - return; - } + request->pendingStreamsToProcess_.erase(streamBuffer->stream); + if (!request->pendingStreamsToProcess_.empty()) + return; completeDescriptor(streamBuffer->request); } diff --git a/src/android/camera_device.h b/src/android/camera_device.h index 699aa8f17..69d163d76 100644 --- a/src/android/camera_device.h +++ b/src/android/camera_device.h @@ -65,8 +65,8 @@ public: int configureStreams(camera3_stream_configuration_t *stream_list); int processCaptureRequest(camera3_capture_request_t *request); void requestComplete(libcamera::Request *request); - void streamProcessingComplete(StreamBuffer *bufferStream, - StreamBuffer::Status status); + void streamProcessingCompleteDelegate(StreamBuffer *bufferStream, + StreamBuffer::Status status); protected: std::string logPrefix() const override; @@ -96,6 +96,8 @@ private: int processControls(Camera3RequestDescriptor *descriptor); void completeDescriptor(Camera3RequestDescriptor *descriptor) LIBCAMERA_TSA_EXCLUDES(descriptorsMutex_); + void streamProcessingComplete(StreamBuffer *bufferStream, + StreamBuffer::Status status); void sendCaptureResults() LIBCAMERA_TSA_REQUIRES(descriptorsMutex_); void sendCaptureResult(Camera3RequestDescriptor *request) const; void setBufferStatus(StreamBuffer &buffer, diff --git a/src/android/camera_request.h b/src/android/camera_request.h index 6b2a00795..bd75d4595 100644 --- a/src/android/camera_request.h +++ b/src/android/camera_request.h @@ -66,9 +66,7 @@ public: }; /* Keeps track of streams requiring post-processing. */ - std::map pendingStreamsToProcess_ - LIBCAMERA_TSA_GUARDED_BY(streamsProcessMutex_); - libcamera::Mutex streamsProcessMutex_; + std::map pendingStreamsToProcess_; Camera3RequestDescriptor(libcamera::Camera *camera, const camera3_capture_request_t *camera3Request); diff --git a/src/android/camera_stream.cpp b/src/android/camera_stream.cpp index 53f292d4b..7837fd7aa 100644 --- a/src/android/camera_stream.cpp +++ b/src/android/camera_stream.cpp @@ -121,8 +121,8 @@ int CameraStream::configure() else bufferStatus = StreamBuffer::Status::Error; - cameraDevice_->streamProcessingComplete(streamBuffer, - bufferStatus); + cameraDevice_->streamProcessingCompleteDelegate(streamBuffer, + bufferStatus); }); worker_->start();