From patchwork Mon Sep 6 22:56:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 13693 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 B35E9BE175 for ; Mon, 6 Sep 2021 22:57:50 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 6888F69168; Tue, 7 Sep 2021 00:57:50 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="IYy/keTp"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id B7C9A6918E for ; Tue, 7 Sep 2021 00:57:09 +0200 (CEST) Received: from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 52530891; Tue, 7 Sep 2021 00:57:09 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1630969029; bh=FQyMhKOgdpFBah23uV5iA8J43CwjXu3uyYsE0tW5buY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IYy/keTpUyxqEEzGXzorhpTFKLExh4wPHG3RjADb4IAR8ZhImtZo65mHK4zl7yhMc W2HuVjn9HY8bbEC2GosWcZijKmMyEMZgYTSZkPFVMHTYyOmGl+z0HBjsfa8zuRBjeF /s6xUBq2mvOtAktzydjhjoTdAARD175poWAsTtvs= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Tue, 7 Sep 2021 01:56:33 +0300 Message-Id: <20210906225636.14683-27-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210906225420.13275-1-laurent.pinchart@ideasonboard.com> References: <20210906225420.13275-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 27/30] qcam: Use Image class to access pixel data 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" Replace the manual implementation of frame buffer mapping with the Image class to improve code sharing. The ViewFinder API is updated to take an Image pointer in the render() function to prepare for multi-planar buffer support. Signed-off-by: Laurent Pinchart Reviewed-by: Jean-Michel Hautbois Reviewed-by: Kieran Bingham --- src/qcam/main_window.cpp | 31 ++++++++----------------------- src/qcam/main_window.h | 4 ++-- src/qcam/meson.build | 1 + src/qcam/viewfinder.h | 6 +++--- src/qcam/viewfinder_gl.cpp | 7 ++++--- src/qcam/viewfinder_gl.h | 2 +- src/qcam/viewfinder_qt.cpp | 6 +++--- src/qcam/viewfinder_qt.h | 2 +- 8 files changed, 23 insertions(+), 36 deletions(-) diff --git a/src/qcam/main_window.cpp b/src/qcam/main_window.cpp index 0a00b1001570..168dd5ce30e3 100644 --- a/src/qcam/main_window.cpp +++ b/src/qcam/main_window.cpp @@ -7,10 +7,9 @@ #include "main_window.h" +#include #include #include -#include -#include #include #include @@ -29,6 +28,7 @@ #include #include +#include "../cam/image.h" #include "dng_writer.h" #ifndef QT_NO_OPENGL #include "viewfinder_gl.h" @@ -473,15 +473,10 @@ int MainWindow::startCapture() for (const std::unique_ptr &buffer : allocator_->buffers(stream)) { /* Map memory buffers and cache the mappings. */ - const FrameBuffer::Plane &plane = buffer->planes().front(); - size_t length = lseek(plane.fd.fd(), 0, SEEK_END); - void *memory = mmap(NULL, length, PROT_READ, MAP_SHARED, - plane.fd.fd(), 0); - - mappedBuffers_[buffer.get()] = { static_cast(memory), - plane.length }; - planeData_[buffer.get()] = { static_cast(memory) + plane.offset, - plane.length }; + std::unique_ptr image = + Image::fromFrameBuffer(buffer.get(), Image::MapMode::ReadOnly); + assert(image != nullptr); + mappedBuffers_[buffer.get()] = std::move(image); /* Store buffers on the free list. */ freeBuffers_[stream].enqueue(buffer.get()); @@ -543,12 +538,7 @@ error_disconnect: error: requests_.clear(); - for (auto &iter : mappedBuffers_) { - const Span &buffer = iter.second; - munmap(buffer.data(), buffer.size()); - } mappedBuffers_.clear(); - planeData_.clear(); freeBuffers_.clear(); @@ -580,12 +570,7 @@ void MainWindow::stopCapture() camera_->requestCompleted.disconnect(this); - for (auto &iter : mappedBuffers_) { - const Span &buffer = iter.second; - munmap(buffer.data(), buffer.size()); - } mappedBuffers_.clear(); - planeData_.clear(); requests_.clear(); freeQueue_.clear(); @@ -682,7 +667,7 @@ void MainWindow::processRaw(FrameBuffer *buffer, "DNG Files (*.dng)"); if (!filename.isEmpty()) { - uint8_t *memory = planeData_[buffer].data(); + uint8_t *memory = mappedBuffers_[buffer]->data(0).data(); DNGWriter::write(filename.toStdString().c_str(), camera_.get(), rawStream_->configuration(), metadata, buffer, memory); @@ -766,7 +751,7 @@ void MainWindow::processViewfinder(FrameBuffer *buffer) << "fps:" << Qt::fixed << qSetRealNumberPrecision(2) << fps; /* Render the frame on the viewfinder. */ - viewfinder_->render(buffer, planeData_[buffer]); + viewfinder_->render(buffer, mappedBuffers_[buffer].get()); } void MainWindow::queueRequest(FrameBuffer *buffer) diff --git a/src/qcam/main_window.h b/src/qcam/main_window.h index 28244bca58b2..a16bea09eadc 100644 --- a/src/qcam/main_window.h +++ b/src/qcam/main_window.h @@ -34,6 +34,7 @@ using namespace libcamera; class QAction; class QComboBox; +class Image; class HotplugEvent; enum { @@ -106,8 +107,7 @@ private: FrameBufferAllocator *allocator_; std::unique_ptr config_; - std::map> mappedBuffers_; - std::map> planeData_; + std::map> mappedBuffers_; /* Capture state, buffers queue and statistics */ bool isCapturing_; diff --git a/src/qcam/meson.build b/src/qcam/meson.build index 7d3621c93d41..c46f463130cd 100644 --- a/src/qcam/meson.build +++ b/src/qcam/meson.build @@ -15,6 +15,7 @@ endif qcam_enabled = true qcam_sources = files([ + '../cam/image.cpp', '../cam/options.cpp', '../cam/stream_options.cpp', 'format_converter.cpp', diff --git a/src/qcam/viewfinder.h b/src/qcam/viewfinder.h index 42d40f1f33f0..fb462835fb5f 100644 --- a/src/qcam/viewfinder.h +++ b/src/qcam/viewfinder.h @@ -11,11 +11,11 @@ #include #include -#include - #include #include +class Image; + class ViewFinder { public: @@ -24,7 +24,7 @@ public: virtual const QList &nativeFormats() const = 0; virtual int setFormat(const libcamera::PixelFormat &format, const QSize &size) = 0; - virtual void render(libcamera::FrameBuffer *buffer, libcamera::Span mem) = 0; + virtual void render(libcamera::FrameBuffer *buffer, Image *image) = 0; virtual void stop() = 0; virtual QImage getCurrentImage() = 0; diff --git a/src/qcam/viewfinder_gl.cpp b/src/qcam/viewfinder_gl.cpp index d2ef036974f4..87e4fe03cb8d 100644 --- a/src/qcam/viewfinder_gl.cpp +++ b/src/qcam/viewfinder_gl.cpp @@ -13,6 +13,8 @@ #include +#include "../cam/image.h" + static const QList supportedFormats{ /* YUV - packed (single plane) */ libcamera::formats::UYVY, @@ -110,8 +112,7 @@ QImage ViewFinderGL::getCurrentImage() return grabFramebuffer(); } -void ViewFinderGL::render(libcamera::FrameBuffer *buffer, - libcamera::Span mem) +void ViewFinderGL::render(libcamera::FrameBuffer *buffer, Image *image) { if (buffer->planes().size() != 1) { qWarning() << "Multi-planar buffers are not supported"; @@ -121,7 +122,7 @@ void ViewFinderGL::render(libcamera::FrameBuffer *buffer, if (buffer_) renderComplete(buffer_); - data_ = mem.data(); + data_ = image->data(0).data(); /* * \todo Get the stride from the buffer instead of computing it naively */ diff --git a/src/qcam/viewfinder_gl.h b/src/qcam/viewfinder_gl.h index 3334549e0be4..7cd8ef3316b9 100644 --- a/src/qcam/viewfinder_gl.h +++ b/src/qcam/viewfinder_gl.h @@ -39,7 +39,7 @@ public: const QList &nativeFormats() const override; int setFormat(const libcamera::PixelFormat &format, const QSize &size) override; - void render(libcamera::FrameBuffer *buffer, libcamera::Span mem) override; + void render(libcamera::FrameBuffer *buffer, Image *image) override; void stop() override; QImage getCurrentImage() override; diff --git a/src/qcam/viewfinder_qt.cpp b/src/qcam/viewfinder_qt.cpp index a0bf99b0b522..fef6d53eef5e 100644 --- a/src/qcam/viewfinder_qt.cpp +++ b/src/qcam/viewfinder_qt.cpp @@ -19,6 +19,7 @@ #include +#include "../cam/image.h" #include "format_converter.h" static const QMap nativeFormats @@ -78,15 +79,14 @@ int ViewFinderQt::setFormat(const libcamera::PixelFormat &format, return 0; } -void ViewFinderQt::render(libcamera::FrameBuffer *buffer, - libcamera::Span mem) +void ViewFinderQt::render(libcamera::FrameBuffer *buffer, Image *image) { if (buffer->planes().size() != 1) { qWarning() << "Multi-planar buffers are not supported"; return; } - unsigned char *memory = mem.data(); + unsigned char *memory = image->data(0).data(); size_t size = buffer->metadata().planes()[0].bytesused; { diff --git a/src/qcam/viewfinder_qt.h b/src/qcam/viewfinder_qt.h index 1a569b9cee6e..6b48ef48a7d1 100644 --- a/src/qcam/viewfinder_qt.h +++ b/src/qcam/viewfinder_qt.h @@ -32,7 +32,7 @@ public: const QList &nativeFormats() const override; int setFormat(const libcamera::PixelFormat &format, const QSize &size) override; - void render(libcamera::FrameBuffer *buffer, libcamera::Span mem) override; + void render(libcamera::FrameBuffer *buffer, Image *image) override; void stop() override; QImage getCurrentImage() override;