[libcamera-devel,RFC,v2,1/4] pipeline: isp: The software ISP-based pipeline handler
diff mbox series

Message ID tencent_C8182AD9B900945115E9D6DCB7A0012DAD05@qq.com
State New
Headers show
Series
  • [libcamera-devel,RFC,v2,1/4] pipeline: isp: The software ISP-based pipeline handler
Related show

Commit Message

Siyuan Fan Aug. 10, 2021, 12:17 p.m. UTC
From: Fan Siyuan <siyuan.fan@foxmail.com>

 Changes in V2:
 -fix the raw and rgb data flow based queue model in pipeline handler
 -move buffer alloc and thread to ISP class
 -Fill metadata information in dstBuffer

Signed-off-by: Fan Siyuan <siyuan.fan@foxmail.com>
---
 src/libcamera/pipeline/isp/isp.cpp | 81 ++++++++++++------------------
 1 file changed, 32 insertions(+), 49 deletions(-)

Patch
diff mbox series

diff --git a/src/libcamera/pipeline/isp/isp.cpp b/src/libcamera/pipeline/isp/isp.cpp
index e5fbd536..c6b7808c 100644
--- a/src/libcamera/pipeline/isp/isp.cpp
+++ b/src/libcamera/pipeline/isp/isp.cpp
@@ -5,9 +5,10 @@ 
  * isp.cpp - The software ISP-based pipeline handler
  */
 
-#include "isp_processing.h"
+#include "../../swisp/isp.h"
 
 #include <math.h>
+#include <queue>
 #include <stdlib.h>
 #include <sys/mman.h>
 #include <unistd.h>
@@ -17,7 +18,6 @@ 
 #include <libcamera/controls.h>
 #include <libcamera/formats.h>
 
-#include "libcamera/base/thread.h"
 #include "libcamera/internal/device_enumerator.h"
 #include "libcamera/internal/media_device.h"
 #include "libcamera/internal/pipeline_handler.h"
@@ -38,7 +38,6 @@  public:
        ~ISPCameraData()
        {
                delete video_;
-               delete rawBuffers;
        }
 
        int init();
@@ -46,16 +45,16 @@  public:
        void ISPCompleted(FrameBuffer *buffer);
 
        Stream stream_;
-       ISP isp_;
-       Thread threadISP_;
-       int width;
-       int height;
+       CPU_ISP isp_;
+       int width_;
+       int height_;
+
+       std::vector<std::unique_ptr<FrameBuffer>> rawBuffers_;
+       std::vector<FrameBuffer *> rawQueueBuffers_;
+       std::vector<FrameBuffer *> rgbQueueBuffers_;
 
        MediaDevice *media_;
        V4L2VideoDevice *video_;
-       std::vector<std::unique_ptr<FrameBuffer>> *tempCopyExportBuffers;
-       std::vector<std::unique_ptr<FrameBuffer>> *rawBuffers;
-       std::map<FrameBuffer *, FrameBuffer *>  bufferPair;
 };
 
 
@@ -158,8 +157,8 @@  int PipelineHandlerISP::configure(Camera *camera, CameraConfiguration *config)
        format.fourcc = v4l2Format;
        format.size = cfg.size;
 
-       data->width = format.size.width;
-       data->height = format.size.height;
+       data->width_ = format.size.width;
+       data->height_ = format.size.height;
 
        int ret = data->video_->setFormat(&format);
        if (ret)
@@ -176,30 +175,8 @@  int PipelineHandlerISP::exportFrameBuffers(Camera *camera, Stream *stream,
 {
        unsigned int count = stream->configuration().bufferCount;
        ISPCameraData *data = cameraData(camera);
-       
-       for (unsigned int i = 0; i < count; i++) {
-              std::string name = "frame-" + std::to_string(i);
-              const int isp_fd = memfd_create(name.c_str(), 0);
-              int ret = ftruncate(isp_fd, data->width * data->height * 3);
-              if (ret < 0) {
-                     LOG(ISP, Error)
-                            << "truncate: "
-                            << strerror(-ret);
-                     return ret;
-              }
-              FileDescriptor temp = FileDescriptor(std::move(isp_fd));
-
-              FrameBuffer::Plane rgbPlane;
-              rgbPlane.fd = std::move(temp);
-              rgbPlane.length = data->width * data->height * 3;
-
-              std::vector<FrameBuffer::Plane> planes;
-              planes.push_back(std::move(rgbPlane));
-              std::unique_ptr<FrameBuffer> buffer = std::make_unique<FrameBuffer>(std::move(planes));
-              buffers->push_back(std::move(buffer));
-       }
 
-       data->tempCopyExportBuffers = buffers;
+       count = data->isp_.exportBuffers(buffers, count, data->width_, data->height_);
 
        return count;       
 
@@ -210,15 +187,16 @@  int PipelineHandlerISP::start(Camera *camera, [[maybe_unused]] const ControlList
        ISPCameraData *data = cameraData(camera);
        unsigned int count = data->stream_.configuration().bufferCount;
 
-       data->rawBuffers = new std::vector<std::unique_ptr<FrameBuffer>>;
-       int ret = data->video_->allocateBuffers(count, data->rawBuffers);
+       int ret = data->video_->allocateBuffers(count, &data->rawBuffers_);
        if (ret < 0) {
               LOG(ISP, Error) << strerror(-ret);
               return ret;
        }
 
-       for (unsigned int i = 0; i < count; i++)
-              data->bufferPair[data->tempCopyExportBuffers->at(i).get()] = data->rawBuffers->at(i).get();
+       for (unsigned int i = 0; i < count; i++) {
+              data->rawQueueBuffers_.push_back(data->rawBuffers_[i].get());
+       }
+       
 
        ret = data->video_->streamOn();
        if (ret < 0) {
@@ -226,7 +204,7 @@  int PipelineHandlerISP::start(Camera *camera, [[maybe_unused]] const ControlList
                return ret;
        }
 
-       data->threadISP_.start();
+       data->isp_.startThreadISP();
 
        return 0;
 }
@@ -235,8 +213,11 @@  void PipelineHandlerISP::stop(Camera *camera)
 {
        ISPCameraData *data = cameraData(camera);
 
-       data->threadISP_.exit();
-       data->threadISP_.wait();
+       if (!(data->rawBuffers_.empty())) {
+              data->rawBuffers_.clear();
+       }
+
+       data->isp_.stopThreadISP();
 
        data->video_->streamOff();
        data->video_->releaseBuffers();
@@ -246,15 +227,19 @@  int PipelineHandlerISP::queueRequestDevice(Camera *camera, Request *request)
 {
        ISPCameraData *data = cameraData(camera);
        FrameBuffer *rgbBuffer = request->findBuffer(&data->stream_);
+       data->rgbQueueBuffers_.push_back(rgbBuffer);
        
        if (!rgbBuffer) {
                LOG(ISP, Error) << "Attempt to queue request with invalid stream";
                return -ENOENT;
        }
 
-       int ret = data->video_->queueBuffer(data->bufferPair[rgbBuffer]);
+       int ret = data->video_->queueBuffer(data->rawQueueBuffers_[0]);
        if (ret < 0)
                return ret;
+       FrameBuffer *temp = data->rawQueueBuffers_[0];
+       data->rawQueueBuffers_.erase(data->rawQueueBuffers_.begin());
+       data->rawQueueBuffers_.push_back(std::move(temp));
 
        return 0;
 }
@@ -297,10 +282,10 @@  void ISPCameraData::ISPCompleted(FrameBuffer *buffer)
 
 void ISPCameraData::bufferReady(FrameBuffer *buffer)
 {
-       for (std::map<FrameBuffer*, FrameBuffer*>::iterator it = bufferPair.begin(); it != bufferPair.end(); it++) {
-              if (it->second == buffer)
-                     isp_.invokeMethod(&ISP::processing, ConnectionTypeQueued, buffer, it->first, width, height);
-       }
+       LOG(ISP, Debug) << rgbQueueBuffers_[0]->planes()[0].fd.fd();
+       isp_.invokeMethod(&CPU_ISP::processing, ConnectionTypeQueued, buffer, rgbQueueBuffers_[0], width_, height_);
+       rgbQueueBuffers_.erase(rgbQueueBuffers_.begin());
+       rgbQueueBuffers_.shrink_to_fit();
        
 }
 
@@ -311,8 +296,6 @@  int ISPCameraData::init()
                return -ENODEV;
 
        video_->bufferReady.connect(this, &ISPCameraData::bufferReady);
-
-       isp_.moveToThread(&threadISP_);
        isp_.ispCompleted.connect(this, &ISPCameraData::ISPCompleted);
 
        return 0;