From patchwork Wed Nov 27 09:25:54 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: 22115 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 1563DC3213 for ; Wed, 27 Nov 2024 09:26:54 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id C8D07660C2; Wed, 27 Nov 2024 10:26:53 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="UmKjDph6"; dkim-atps=neutral Received: from mail-ot1-x32c.google.com (mail-ot1-x32c.google.com [IPv6:2607:f8b0:4864:20::32c]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 366F9660BF for ; Wed, 27 Nov 2024 10:26:45 +0100 (CET) Received: by mail-ot1-x32c.google.com with SMTP id 46e09a7af769-71d554dedc6so1265692a34.1 for ; Wed, 27 Nov 2024 01:26:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1732699604; x=1733304404; 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=kAnvO/uH9Pu56foWQBN126UILV1EQphGGewh74RvqyA=; b=UmKjDph6ERLdz627fzUkmT78sMMZ9meLpNAUzER4wH8WNhwYlzsaVQgcpmZkMpw5JS wuG1QY+erYOdt5mskt4jRV/icqm/ciFzBdvJQ4ydosHjdbATUoKy6Np7bZg6m5msHul8 XE601k+KPKHqLpMKHL6yJ0xPN21J9YmzbAZhI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732699604; x=1733304404; 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=kAnvO/uH9Pu56foWQBN126UILV1EQphGGewh74RvqyA=; b=eEoSicBijKLrG8bB1D2goZ/RvdiUqDZpj4SsjsYvSH1FsqNUcoZsKv6wOlQJO8WTN1 NrwARhV4H1pJpEVtsweoQtx7D5aQVORWQcTvfHDWbBbRXWJLoyZwjZfaVuEqdaPrU2TQ ivrFVu0UJbzkyRlp2pQSyOcGMEV5CIJjhEKixFc102MfecIR9H10St8AEqsDvj1x4Y2f mEG2dQpyraUDog0/vMyNrlurI0r8C0y4kfWJKW2HXvpXzdEnrNrjWpKZg07WsROEdr9t s480NT5mwvZ3c7Pnh4q4Fz73z5LzwyaJKAz3W3PmKaKF2BKKo4CR7Ngzmt0q4Vv5zV05 xCCw== X-Gm-Message-State: AOJu0Ywt1aeE+72Xf1A0Je2oMLYqJPl/o4y991CRWPOs37Jfvvu3EvgR NDAkJK0jRv9rWtPVz9HYHozMoJgBVsQhkZGrTMTQBUR5dombWRsKsORJtv82+I7swPajEKacPCY = X-Gm-Gg: ASbGncsMqFwSPW8QBoGwQ8vu5ALpvNcfaIfcWGMO/c5i5yjMNLD9W7y3wEjmwR1MGhw 2YH7YbLpt2TMQefyNAf1QDmv1W90Q2umTgjlhnq/kCnB9YroKw5sVkN5SeZRmQX2oU+qFX+7POy OxvbL7dEQODLWGgZ8u8nDvHLfx/Z4VwNo8fDPndE/hMEV6T7jlmShR2ROgI9g6h72ZOa77lwHQf 1Ats8rNWnoF3OPAPa8WisPb78TnxVCARiMO0dhLcW0pgqjjg6XKGWVxPIeN/DcRcYsVWhB8weRE DHPfKpGVI2N138Roix1XK5ukmiw5JqprwT0zi36PvdIZK1nEbqvfOw== X-Google-Smtp-Source: AGHT+IG1POox6Chy0wKAmoDBhSATg6WuLQ0htCvG79teD0c8dDEKe60LndYub7JwDianEcpKTyKzzQ== X-Received: by 2002:a05:6830:258a:b0:71d:5a17:75c6 with SMTP id 46e09a7af769-71d65d2a90fmr2240309a34.29.1732699603649; Wed, 27 Nov 2024 01:26:43 -0800 (PST) Received: from chenghaoyang-low.c.googlers.com.com (27.247.221.35.bc.googleusercontent.com. [35.221.247.27]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7fbcbfc41f9sm8693027a12.8.2024.11.27.01.26.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Nov 2024 01:26:43 -0800 (PST) From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Harvey Yang , Han-Lin Chen Subject: [PATCH v2 4/9] android: Migrate StreamBuffer::internalBuffer to Camera3RequestDescriptor Date: Wed, 27 Nov 2024 09:25:54 +0000 Message-ID: <20241127092632.3145984-5-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog In-Reply-To: <20241127092632.3145984-1-chenghaoyang@chromium.org> References: <20241127092632.3145984-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" Previously there's a potential issue in StreamBuffer::internalBuffer's lifetime, when more than one streams depend on the same internal buffer. This patch fixes the issue by returning the buffer when the whole Camera3RequestDescriptor is destructed. Signed-off-by: Han-Lin Chen Co-developed-by: Harvey Yang Signed-off-by: Harvey Yang --- src/android/camera_device.cpp | 63 +++++++++++++++++++--------------- src/android/camera_request.cpp | 13 ++++--- src/android/camera_request.h | 3 +- 3 files changed, 46 insertions(+), 33 deletions(-) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index 11613fac9..62f724041 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -967,9 +967,10 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques * to a libcamera stream. Streams of type Mapped will be handled later. * * Collect the CameraStream associated to each requested capture stream. - * Since requestedStreams is an std:map<>, no duplications can happen. + * Since requestedDirectBuffers is an std:map<>, no duplications can + * happen. */ - std::map requestedBuffers; + std::map requestedDirectBuffers; for (const auto &[i, buffer] : utils::enumerate(descriptor->buffers_)) { CameraStream *cameraStream = buffer.stream; camera3_stream_t *camera3Stream = cameraStream->camera3Stream(); @@ -1009,7 +1010,7 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques frameBuffer = buffer.frameBuffer.get(); acquireFence = std::move(buffer.fence); - requestedBuffers[cameraStream] = frameBuffer; + requestedDirectBuffers[cameraStream] = frameBuffer; LOG(HAL, Debug) << ss.str() << " (direct)"; break; @@ -1018,14 +1019,17 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques * Get the frame buffer from the CameraStream internal * buffer pool. * - * The buffer has to be returned to the CameraStream - * once it has been processed. + * The buffer will be returned to the CameraStream when + * the request is destructed. */ frameBuffer = cameraStream->getBuffer(); - buffer.internalBuffer = frameBuffer; buffer.srcBuffer = frameBuffer; - requestedBuffers[cameraStream] = frameBuffer; + /* + * Track the allocated internal buffers, which will be + * recycled when the descriptor destroyed. + * */ + descriptor->internalBuffers_[cameraStream] = frameBuffer; LOG(HAL, Debug) << ss.str() << " (internal)"; descriptor->pendingStreamsToProcess_.insert( @@ -1079,24 +1083,41 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques * post-processing. No need to recycle the buffer since it's * owned by Android. */ - auto iterDirectBuffer = requestedBuffers.find(sourceStream); - if (iterDirectBuffer != requestedBuffers.end()) { + auto iterDirectBuffer = requestedDirectBuffers.find(sourceStream); + if (iterDirectBuffer != requestedDirectBuffers.end()) { buffer.srcBuffer = iterDirectBuffer->second; continue; } /* - * If that's not the case, we need to add a buffer to the request - * for this stream. + * If that's not the case, we use an internal buffer allocated + * from the source stream. + * + * If an internal buffer has been requested for the source + * stream before, we should reuse it. + */ + auto iterInternalBuffer = descriptor->internalBuffers_.find(sourceStream); + if (iterInternalBuffer != descriptor->internalBuffers_.end()) { + buffer.srcBuffer = iterInternalBuffer->second; + continue; + } + + /* + * Otherwise, we need to create an internal buffer to the + * request for the source stream. Get the frame buffer from the + * source stream's internal buffer pool. + * + * The buffer will be returned to the CameraStream when the + * request is destructed. */ - FrameBuffer *frameBuffer = cameraStream->getBuffer(); - buffer.internalBuffer = frameBuffer; + FrameBuffer *frameBuffer = sourceStream->getBuffer(); buffer.srcBuffer = frameBuffer; descriptor->request_->addBuffer(sourceStream->stream(), frameBuffer, nullptr); - requestedBuffers[sourceStream] = frameBuffer; + /* Track the allocated internal buffer. */ + descriptor->internalBuffers_[sourceStream] = frameBuffer; } /* @@ -1256,13 +1277,6 @@ void CameraDevice::requestComplete(Request *request) if (ret) { setBufferStatus(*buffer, StreamBuffer::Status::Error); descriptor->pendingStreamsToProcess_.erase(stream); - - /* - * If the framebuffer is internal to CameraStream return - * it back now that we're done processing it. - */ - if (buffer->internalBuffer) - stream->putBuffer(buffer->internalBuffer); } } @@ -1381,13 +1395,6 @@ void CameraDevice::streamProcessingComplete(StreamBuffer *streamBuffer, { setBufferStatus(*streamBuffer, status); - /* - * If the framebuffer is internal to CameraStream return it back now - * that we're done processing it. - */ - if (streamBuffer->internalBuffer) - streamBuffer->stream->putBuffer(streamBuffer->internalBuffer); - Camera3RequestDescriptor *request = streamBuffer->request; { diff --git a/src/android/camera_request.cpp b/src/android/camera_request.cpp index 52a3ac1f7..a9240a83c 100644 --- a/src/android/camera_request.cpp +++ b/src/android/camera_request.cpp @@ -10,6 +10,7 @@ #include #include "camera_buffer.h" +#include "camera_stream.h" using namespace libcamera; @@ -138,7 +139,14 @@ Camera3RequestDescriptor::Camera3RequestDescriptor( request_ = camera->createRequest(reinterpret_cast(this)); } -Camera3RequestDescriptor::~Camera3RequestDescriptor() = default; +Camera3RequestDescriptor::~Camera3RequestDescriptor() +{ + /* + * Recycle the allocated internal buffer back to its source stream. + */ + for (auto &[sourceStream, frameBuffer] : internalBuffers_) + sourceStream->putBuffer(frameBuffer); +} /** * \class StreamBuffer @@ -166,9 +174,6 @@ Camera3RequestDescriptor::~Camera3RequestDescriptor() = default; * \var StreamBuffer::status * \brief Track the status of the buffer * - * \var StreamBuffer::internalBuffer - * \brief Pointer to a buffer internally handled by CameraStream (if any) - * * \var StreamBuffer::srcBuffer * \brief Pointer to the source frame buffer used for post-processing * diff --git a/src/android/camera_request.h b/src/android/camera_request.h index 335f1985d..6b2a00795 100644 --- a/src/android/camera_request.h +++ b/src/android/camera_request.h @@ -49,7 +49,6 @@ public: std::unique_ptr frameBuffer; libcamera::UniqueFD fence; Status status = Status::Success; - libcamera::FrameBuffer *internalBuffer = nullptr; const libcamera::FrameBuffer *srcBuffer = nullptr; std::unique_ptr dstBuffer; Camera3RequestDescriptor *request; @@ -85,6 +84,8 @@ public: std::unique_ptr request_; std::unique_ptr resultMetadata_; + std::map internalBuffers_; + bool complete_ = false; Status status_ = Status::Success;