From patchwork Mon Apr 26 08:38:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 12110 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 EB437BDC99 for ; Mon, 26 Apr 2021 08:38:41 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id AE2B2688B6; Mon, 26 Apr 2021 10:38:41 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="iw/S7OvC"; dkim-atps=neutral Received: from mail-pj1-x102c.google.com (mail-pj1-x102c.google.com [IPv6:2607:f8b0:4864:20::102c]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 02614605BE for ; Mon, 26 Apr 2021 10:38:39 +0200 (CEST) Received: by mail-pj1-x102c.google.com with SMTP id j6-20020a17090adc86b02900cbfe6f2c96so4858657pjv.1 for ; Mon, 26 Apr 2021 01:38:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=4zlrEa4ha9TdBM0OP0ylkX7d3YyewkL0ZRW5RuTbEl4=; b=iw/S7OvC0lVn6MD3L0B/Y57TT4dGYDGxq/EyCO9MpQQswgLAtaiT6Lj4ci4bcMZA/f r8zFt0kiWiTP+QJdxYR7relu9O9S6OlMCeIvZxerCXYEpFpROE4leneH1SIpes7sinpT jyhp2wAdxrSKLQPDFzfpbgIckeGVjdatiLw/g= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=4zlrEa4ha9TdBM0OP0ylkX7d3YyewkL0ZRW5RuTbEl4=; b=JUJKrLxU7QghJr1c7/MIiA7Z+XHzBpijTb9PGQ5BrsgY1RF9JiyTA7xqOBVAhQ7G3X ONtE5gXYlfeL4+KZsC7njF+Pr/VfTPpUovVtSNafHwIDifyYoPGOYiiUW9Id1eI4XIEB ea0vKI1Hq6weN4WaUDgX6C2kwd8bZh2eAoE9RHJbwK+UNKJPQWwZB7z/7Hjei69PHlWo fKLOfvtPOpukLDKG/i85eti3jOKKEgXXCCVwTciLnH6ezIvEIOD7cvVlg45NmhwExXSA zYKrXHP33emPdtY7VijRQpebnLbVqlWi2A86f7Lk8JzY0Bpd21I8um0V4yNMWRvzTNxu nyaw== X-Gm-Message-State: AOAM532mKN4QxRHq9FtppGambnasA7PoHJ+Chor5V7vjIjhcsKKlYzlC 6MLVJ+BH9DEVMTsdEL0v5dhbWrAm+DZVmg== X-Google-Smtp-Source: ABdhPJxIFGC84LDul1+MTQdgLlhCPyYy9N2dFEofIKHGcasRHuo1VG+6OddqACwO1BgXuI4P1/10gQ== X-Received: by 2002:a17:90a:6585:: with SMTP id k5mr19612685pjj.40.1619426317458; Mon, 26 Apr 2021 01:38:37 -0700 (PDT) Received: from hiroh2.tok.corp.google.com ([2401:fa00:8f:2:a407:b8c2:d8b:a449]) by smtp.gmail.com with ESMTPSA id e18sm4873728pgr.8.2021.04.26.01.38.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Apr 2021 01:38:37 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Mon, 26 Apr 2021 17:38:28 +0900 Message-Id: <20210426083830.2965095-2-hiroh@chromium.org> X-Mailer: git-send-email 2.31.1.498.g6c1eba8ee3d-goog In-Reply-To: <20210426083830.2965095-1-hiroh@chromium.org> References: <20210426083830.2965095-1-hiroh@chromium.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 1/3] android: CameraDevice: Separate CaptureRequest from Camera3RequestDescriptor 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" Camera3RequestDescriptors can be destroyed in stop(). Since Camera3RequestDescriptor owns CaptureRequest, the CaptureRequest should not be used anymore. But requestComplete() cannot check if the associated descriptor with a request is valid without calling CaptureRequest::cookie(). This resolves the problem by separating CaptureRequest lifetime from Camera3RequestDescriptor. Thus CaptureRequest always is destroyed in requestComplete(). This guarantees that a request is valid until requestComplete() is called. Signed-off-by: Hirokazu Honda --- src/android/camera_device.cpp | 27 +++++++++++++++------------ src/android/camera_device.h | 9 +++++---- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index a71aee2f..b3def04b 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -361,7 +361,8 @@ bool validateCropRotate(const camera3_stream_configuration_t &streamList) */ CameraDevice::Camera3RequestDescriptor::Camera3RequestDescriptor( - Camera *camera, const camera3_capture_request_t *camera3Request) + const camera3_capture_request_t *camera3Request, CaptureRequest *request) + : request_(request) { frameNumber_ = camera3Request->frame_number; @@ -379,12 +380,6 @@ CameraDevice::Camera3RequestDescriptor::Camera3RequestDescriptor( /* Clone the controls associated with the camera3 request. */ settings_ = CameraMetadata(camera3Request->settings); - - /* - * Create the CaptureRequest, stored as a unique_ptr<> to tie its - * lifetime to the descriptor. - */ - request_ = std::make_unique(camera); } /* @@ -1930,7 +1925,8 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques * The descriptor and the associated memory reserved here are freed * at request complete time. */ - Camera3RequestDescriptor descriptor(camera_.get(), camera3Request); + auto request = std::make_unique(camera_.get()); + Camera3RequestDescriptor descriptor(camera3Request, request.get()); /* * \todo The Android request model is incremental, settings passed in @@ -2013,11 +2009,12 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques if (ret) return ret; - worker_.queueRequest(descriptor.request_.get()); + worker_.queueRequest(descriptor.request_); { std::scoped_lock lock(mutex_); descriptors_[descriptor.request_->cookie()] = std::move(descriptor); + requests_[request->cookie()] = std::move(request); } return 0; @@ -2030,15 +2027,21 @@ void CameraDevice::requestComplete(Request *request) std::unique_ptr resultMetadata; decltype(descriptors_)::node_type node; + std::unique_ptr captureRequest; { std::scoped_lock lock(mutex_); - auto it = descriptors_.find(request->cookie()); - if (it == descriptors_.end()) { + auto requestIt = requests_.find(request->cookie()); + if (requestIt == requests_.end()) { LOG(HAL, Fatal) << "Unknown request: " << request->cookie(); - status = CAMERA3_BUFFER_STATUS_ERROR; return; } + captureRequest = std::move(requestIt->second); + requests_.erase(requestIt); + + auto it = descriptors_.find(request->cookie()); + if (it == descriptors_.end()) + return; node = descriptors_.extract(it); } diff --git a/src/android/camera_device.h b/src/android/camera_device.h index c63e8e21..95d77890 100644 --- a/src/android/camera_device.h +++ b/src/android/camera_device.h @@ -72,15 +72,15 @@ private: struct Camera3RequestDescriptor { Camera3RequestDescriptor() = default; ~Camera3RequestDescriptor() = default; - Camera3RequestDescriptor(libcamera::Camera *camera, - const camera3_capture_request_t *camera3Request); + Camera3RequestDescriptor(const camera3_capture_request_t *camera3Request, + CaptureRequest *request); Camera3RequestDescriptor &operator=(Camera3RequestDescriptor &&) = default; uint32_t frameNumber_ = 0; std::vector buffers_; std::vector> frameBuffers_; CameraMetadata settings_; - std::unique_ptr request_; + CaptureRequest *request_ = nullptr; }; struct Camera3StreamConfiguration { @@ -127,8 +127,9 @@ private: std::map formatsMap_; std::vector streams_; - std::mutex mutex_; /* Protect descriptors_ */ + std::mutex mutex_; /* Protect descriptors_ and requests_. */ std::map descriptors_; + std::map> requests_; std::string maker_; std::string model_; From patchwork Mon Apr 26 08:38:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 12111 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 8B09CBDC99 for ; Mon, 26 Apr 2021 08:38:43 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 12C8F688AF; Mon, 26 Apr 2021 10:38:43 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="EJQz4C8/"; dkim-atps=neutral Received: from mail-pg1-x531.google.com (mail-pg1-x531.google.com [IPv6:2607:f8b0:4864:20::531]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 926AE688AF for ; Mon, 26 Apr 2021 10:38:41 +0200 (CEST) Received: by mail-pg1-x531.google.com with SMTP id s22so18495899pgk.6 for ; Mon, 26 Apr 2021 01:38:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=xpJ1iUDfDDXHcnvCiqwegzXaebN0Vojm3qpeP8L5ZPg=; b=EJQz4C8/VgpVE4wvb+udZIbNTeIOIA69lWHdsusBGqcFOzKt/1neW3MP4ULJJdOOkU CG7s2vYLG/nB8GKyJy8CYpogxZQoo8Rq5GjeyviAJlDyANBBu7KMGuQDaLcwBWaNjS7Y m7VCotglQKt+unKDANl70ENQHob818gXfNU/0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=xpJ1iUDfDDXHcnvCiqwegzXaebN0Vojm3qpeP8L5ZPg=; b=DMuZseBzhtN6FWy7CaLEt2uXpQ9XtNyH5kXWwc+FL8xqD72xo/WPHPuLPXOvSyiWSc gjCgIoCjAgGDjuhByEWMJI6ZZyF5nPqN8SvcmstZsVxe4XhwnwNs0Rk081WvsO1A2TK5 nJ5eDM876gYmMJZcSmT1PSTvgHTHUZ0EtfATGj+Ad2NeZdQcCJM1z2zeKoRc9JDHCWGc m8DlqdxdUVhwnjs3iIxzvycYnDPqwHx3sySCEfQH0VwfxUM9ju/jL9gCazQ4hgu0kiys 81lQI8ilKC+L/xcgMhpwus73AqFMgvJ4K613g7dsyA/jiGDLlO64DfircTlhVj65K2vD mgug== X-Gm-Message-State: AOAM531eg2ZG63VC0n4+LBI0+Bdyl3lGHh86irpr/LDgko5+LGEyn+at 55PB1p7XXAo/RS4TCChjydkQzMzzArFLBA== X-Google-Smtp-Source: ABdhPJzQcTvXzQ2r0aau+zOgI/YMS56qZEW2JZZHzMsjYTnQUGk8ZpJoK/CqcGjMz3xk0TJ+ShtYjw== X-Received: by 2002:a63:8948:: with SMTP id v69mr15891684pgd.264.1619426318878; Mon, 26 Apr 2021 01:38:38 -0700 (PDT) Received: from hiroh2.tok.corp.google.com ([2401:fa00:8f:2:a407:b8c2:d8b:a449]) by smtp.gmail.com with ESMTPSA id e18sm4873728pgr.8.2021.04.26.01.38.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Apr 2021 01:38:38 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Mon, 26 Apr 2021 17:38:29 +0900 Message-Id: <20210426083830.2965095-3-hiroh@chromium.org> X-Mailer: git-send-email 2.31.1.498.g6c1eba8ee3d-goog In-Reply-To: <20210426083830.2965095-1-hiroh@chromium.org> References: <20210426083830.2965095-1-hiroh@chromium.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 2/3] android: CameraDevice: Prevent race due to concurrent HAL calls 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" HAL API functions might be called by different threads in Android Camera HAL3 API, while process_capture_request() must be called by a single thread. Furthermore, close() and flush() can be called any time. Therefore, races to variables can occur among the HAL API functions. This prevents the races by enclosing HAL calls by mutex. It might not be the best in terms of the performance, but the simplicity is good as a first step and also further development. Signed-off-by: Hirokazu Honda --- src/android/camera_device.cpp | 59 ++++++++++++++++++++--------------- src/android/camera_device.h | 7 +++-- 2 files changed, 39 insertions(+), 27 deletions(-) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index b3def04b..96c8d4a9 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -743,7 +743,7 @@ int CameraDevice::open(const hw_module_t *hardwareModule) void CameraDevice::close() { - streams_.clear(); + std::scoped_lock halLock(halMutex_); stop(); @@ -758,12 +758,17 @@ void CameraDevice::stop() worker_.stop(); camera_->stop(); - descriptors_.clear(); + { + std::scoped_lock lock(mutex_); + descriptors_.clear(); + } + running_ = false; } void CameraDevice::setCallbacks(const camera3_callback_ops_t *callbacks) { + std::scoped_lock halLock(halMutex_); callbacks_ = callbacks; } @@ -1643,8 +1648,7 @@ PixelFormat CameraDevice::toPixelFormat(int format) const */ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list) { - /* Before any configuration attempt, stop the camera. */ - stop(); + std::scoped_lock halLock(halMutex_); if (stream_list->num_streams == 0) { LOG(HAL, Error) << "No streams in configuration"; @@ -1656,6 +1660,9 @@ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list) return -EINVAL; #endif + /* Before any configuration attempt, stop the camera. */ + stop(); + /* * Generate an empty configuration, and construct a StreamConfiguration * for each camera3_stream to add to it. @@ -1671,6 +1678,8 @@ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list) * ensure the required entries are available without further * reallocation. */ + + std::scoped_lock lock(mutex_); streams_.clear(); streams_.reserve(stream_list->num_streams); @@ -1907,6 +1916,8 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques if (!isValidRequest(camera3Request)) return -EINVAL; + std::scoped_lock halLock(halMutex_); + /* Start the camera if that's the first request we handle. */ if (!running_) { worker_.start(); @@ -2022,29 +2033,26 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques void CameraDevice::requestComplete(Request *request) { + std::scoped_lock lock(mutex_); + const Request::BufferMap &buffers = request->buffers(); camera3_buffer_status status = CAMERA3_BUFFER_STATUS_OK; std::unique_ptr resultMetadata; - decltype(descriptors_)::node_type node; - std::unique_ptr captureRequest; - { - std::scoped_lock lock(mutex_); - auto requestIt = requests_.find(request->cookie()); - if (requestIt == requests_.end()) { - LOG(HAL, Fatal) - << "Unknown request: " << request->cookie(); - return; - } - captureRequest = std::move(requestIt->second); - requests_.erase(requestIt); + auto requestIt = requests_.find(request->cookie()); + if (requestIt == requests_.end()) { + LOG(HAL, Fatal) + << "Unknown request: " << request->cookie(); + return; + } + auto captureRequest = std::move(requestIt->second); + requests_.erase(requestIt); - auto it = descriptors_.find(request->cookie()); - if (it == descriptors_.end()) - return; + auto it = descriptors_.find(request->cookie()); + if (it == descriptors_.end()) + return; - node = descriptors_.extract(it); - } + auto node = descriptors_.extract(it); Camera3RequestDescriptor &descriptor = node.mapped(); if (request->status() != Request::RequestComplete) { @@ -2149,11 +2157,12 @@ void CameraDevice::notifyError(uint32_t frameNumber, camera3_stream_t *stream) camera3_notify_msg_t notify = {}; /* - * \todo Report and identify the stream number or configuration to - * clarify the stream that failed. + * \todo Report and identify the stream number or configuration + * to clarify the stream that failed. */ - LOG(HAL, Error) << "Error occurred on frame " << frameNumber << " (" - << toPixelFormat(stream->format).toString() << ")"; + LOG(HAL, Error) + << "Error occurred on frame " << descriptor.frameNumber_ << " (" + << toPixelFormat(descriptor.buffers_[0].stream->format).toString() << ")"; notify.type = CAMERA3_MSG_ERROR; notify.message.error.error_stream = stream; diff --git a/src/android/camera_device.h b/src/android/camera_device.h index 95d77890..325fb967 100644 --- a/src/android/camera_device.h +++ b/src/android/camera_device.h @@ -125,9 +125,12 @@ private: std::vector streamConfigurations_; std::map formatsMap_; - std::vector streams_; - std::mutex mutex_; /* Protect descriptors_ and requests_. */ + /* Avoid races by concurrent Camera HAL API calls. */ + std::mutex halMutex_; + + std::mutex mutex_; /* Protect streams_, descriptors_ and requests_. */ + std::vector streams_; std::map descriptors_; std::map> requests_; From patchwork Mon Apr 26 08:38:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 12112 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 30AA3BDC99 for ; Mon, 26 Apr 2021 08:38:44 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id CF390688B9; Mon, 26 Apr 2021 10:38:43 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="JbWUTsO+"; dkim-atps=neutral Received: from mail-pl1-x630.google.com (mail-pl1-x630.google.com [IPv6:2607:f8b0:4864:20::630]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id E2F5A688B9 for ; Mon, 26 Apr 2021 10:38:41 +0200 (CEST) Received: by mail-pl1-x630.google.com with SMTP id t3so1252786plz.1 for ; Mon, 26 Apr 2021 01:38:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=hB4U1qnLM8I7fOVF5tDADuTX+WeJdZ8FPLjDox9q8+Q=; b=JbWUTsO+LTYR9abJRbOnmVrfdfOVGsjwZG3iL0wRYptQLwCJGTi1SGGh8pc7rWNf2f VazEIQZfPAIGcnH99IaQEefW0RZPCNSIpGXqEmx3qluoUIq7QeG/m6AY0DhL2cBMLolN igFI1gaK8EgigOAVOKucoffAYxWJm6iqNjfmI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=hB4U1qnLM8I7fOVF5tDADuTX+WeJdZ8FPLjDox9q8+Q=; b=Ky+dAz82jvYGq6vIx/Xv0KGi5xfziFEMw/Y0hqg9KFd3eaL5NsmUEbSn+SiGi+oB5w ongXRY+i5PnL7DqPZ45lB/N4OuYZOvGOqQ6oK2igSY7KGXVIjWqLWw9/AQbuYdpcjz9k 2utAxOLct8A/7lFD3M0ETgu6Q3/0qWUBu+GlQ7pak6/g8xVmIeLhWqQTPHO2EL5FtObP cMSQ1wc5jx6J5XRIMmxDWMpDGn3B9+kUTdD/AFikniBSjGdzSKoQ43/IKN5BQ+1+vYRk RvywYLWzmVHQo4SQ2eTTDZGgefr6h/N1L5L9ZZcrXbCeslFcnxQdoJCshrCpzQuBzfKv K8Rw== X-Gm-Message-State: AOAM532u4c1HpjQvdhTdvd7bcrwWOsZ/6mgOfAVtZIGEhxGdWe5kccSl Xf4+lsEYcmxs5NhhS7GzIySilkAkqohXeg== X-Google-Smtp-Source: ABdhPJxzDgE0FXq3jyW489rPYW+aVAIdSzSYzbLyRTrgm57h9hJuEISZ/55J0xzwPwjwLUQ+ERrCzA== X-Received: by 2002:a17:90b:88d:: with SMTP id bj13mr13815365pjb.192.1619426320361; Mon, 26 Apr 2021 01:38:40 -0700 (PDT) Received: from hiroh2.tok.corp.google.com ([2401:fa00:8f:2:a407:b8c2:d8b:a449]) by smtp.gmail.com with ESMTPSA id e18sm4873728pgr.8.2021.04.26.01.38.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Apr 2021 01:38:39 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Mon, 26 Apr 2021 17:38:30 +0900 Message-Id: <20210426083830.2965095-4-hiroh@chromium.org> X-Mailer: git-send-email 2.31.1.498.g6c1eba8ee3d-goog In-Reply-To: <20210426083830.2965095-1-hiroh@chromium.org> References: <20210426083830.2965095-1-hiroh@chromium.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 3/3] android: CameraDevice: Implement HAL3 API flush 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 implements flush(). It is mostly the same as close() though the canceled events are reported with error messages. Signed-off-by: Hirokazu Honda --- src/android/camera_device.cpp | 28 ++++++++++++++++++++++++++++ src/android/camera_device.h | 1 + src/android/camera_ops.cpp | 8 ++++++-- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index 96c8d4a9..fb89195e 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -750,6 +750,15 @@ void CameraDevice::close() camera_->release(); } +int CameraDevice::flush() +{ + std::scoped_lock halLock(halMutex_); + + stop(); + + return 0; +} + void CameraDevice::stop() { if (!running_) @@ -760,6 +769,25 @@ void CameraDevice::stop() { std::scoped_lock lock(mutex_); + + for (auto &[cookie, descriptor] : descriptors_) { + LOG(HAL, Debug) << "request is canceled: " << cookie; + + camera3_capture_result_t captureResult = {}; + captureResult.frame_number = descriptor.frameNumber_; + captureResult.num_output_buffers = descriptor.buffers_.size(); + for (camera3_stream_buffer_t &buffer : descriptor.buffers_) { + buffer.acquire_fence = -1; + buffer.release_fence = -1; + buffer.status = CAMERA3_BUFFER_STATUS_ERROR; + } + captureResult.output_buffers = descriptor.buffers_.data(); + + notifyError(descriptor.frameNumber_, + descriptor.buffers_[0].stream); + callbacks_->process_capture_result(callbacks_, &captureResult); + } + descriptors_.clear(); } diff --git a/src/android/camera_device.h b/src/android/camera_device.h index 325fb967..fa652c3c 100644 --- a/src/android/camera_device.h +++ b/src/android/camera_device.h @@ -41,6 +41,7 @@ public: int open(const hw_module_t *hardwareModule); void close(); + int flush(); unsigned int id() const { return id_; } camera3_device_t *camera3Device() { return &camera3Device_; } diff --git a/src/android/camera_ops.cpp b/src/android/camera_ops.cpp index 696e8043..01458403 100644 --- a/src/android/camera_ops.cpp +++ b/src/android/camera_ops.cpp @@ -66,9 +66,13 @@ static void hal_dev_dump([[maybe_unused]] const struct camera3_device *dev, { } -static int hal_dev_flush([[maybe_unused]] const struct camera3_device *dev) +static int hal_dev_flush(const struct camera3_device *dev) { - return 0; + if (!dev) + return -EINVAL; + + CameraDevice *camera = reinterpret_cast(dev->priv); + return camera->flush(); } int hal_dev_close(hw_device_t *hw_device)