Show a patch.

GET /api/1.1/patches/3616/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 3616,
    "url": "https://patchwork.libcamera.org/api/1.1/patches/3616/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/3616/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/1.1/projects/1/?format=api",
        "name": "libcamera",
        "link_name": "libcamera",
        "list_id": "libcamera_core",
        "list_email": "libcamera-devel@lists.libcamera.org",
        "web_url": "",
        "scm_url": "",
        "webscm_url": ""
    },
    "msgid": "<20200430003604.2423018-3-niklas.soderlund@ragnatech.se>",
    "date": "2020-04-30T00:36:01",
    "name": "[libcamera-devel,RFC/PATCH,2/5] qcam: Allow for a second raw stream to be configured",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": false,
    "hash": "e5a813739aeeb5d9b28885f0c296be975608ab77",
    "submitter": {
        "id": 5,
        "url": "https://patchwork.libcamera.org/api/1.1/people/5/?format=api",
        "name": "Niklas Söderlund",
        "email": "niklas.soderlund@ragnatech.se"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/3616/mbox/",
    "series": [
        {
            "id": 855,
            "url": "https://patchwork.libcamera.org/api/1.1/series/855/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=855",
            "date": "2020-04-30T00:36:00",
            "name": "qcam: Add RAW capture support",
            "version": 1,
            "mbox": "https://patchwork.libcamera.org/series/855/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/3616/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/3616/checks/",
    "tags": {},
    "headers": {
        "Return-Path": "<niklas.soderlund@ragnatech.se>",
        "Received": [
            "from bin-mail-out-05.binero.net (bin-mail-out-05.binero.net\n\t[195.74.38.228])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 1732460AF5\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 30 Apr 2020 02:36:37 +0200 (CEST)",
            "from bismarck.berto.se (p4fca2392.dip0.t-ipconnect.de\n\t[79.202.35.146]) by bin-vsp-out-03.atm.binero.net (Halon) with ESMTPA\n\tid 86bd1769-8a7a-11ea-89d0-0050569116f7;\n\tThu, 30 Apr 2020 02:35:44 +0200 (CEST)"
        ],
        "X-Halon-ID": "86bd1769-8a7a-11ea-89d0-0050569116f7",
        "Authorized-sender": "niklas@soderlund.pp.se",
        "From": "=?utf-8?q?Niklas_S=C3=B6derlund?= <niklas.soderlund@ragnatech.se>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Date": "Thu, 30 Apr 2020 02:36:01 +0200",
        "Message-Id": "<20200430003604.2423018-3-niklas.soderlund@ragnatech.se>",
        "X-Mailer": "git-send-email 2.26.0",
        "In-Reply-To": "<20200430003604.2423018-1-niklas.soderlund@ragnatech.se>",
        "References": "<20200430003604.2423018-1-niklas.soderlund@ragnatech.se>",
        "MIME-Version": "1.0",
        "Content-Type": "text/plain; charset=UTF-8",
        "Content-Transfer-Encoding": "8bit",
        "Subject": "[libcamera-devel] [RFC/PATCH 2/5] qcam: Allow for a second raw\n\tstream to be configured",
        "X-BeenThere": "libcamera-devel@lists.libcamera.org",
        "X-Mailman-Version": "2.1.29",
        "Precedence": "list",
        "List-Id": "<libcamera-devel.lists.libcamera.org>",
        "List-Unsubscribe": "<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>",
        "List-Archive": "<https://lists.libcamera.org/pipermail/libcamera-devel/>",
        "List-Post": "<mailto:libcamera-devel@lists.libcamera.org>",
        "List-Help": "<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>",
        "List-Subscribe": "<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>",
        "X-List-Received-Date": "Thu, 30 Apr 2020 00:36:37 -0000"
    },
    "content": "Allow a second stream to be configured for raw capture. This change only\nadds support for configuring and allocating buffers for the second\nstream. Later changes are needed to queue the allocated buffers to the\ncamera when the user wish to capture a raw frame.\n\nSigned-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n---\n src/qcam/main_window.cpp | 133 +++++++++++++++++++++++++++------------\n src/qcam/main_window.h   |   8 ++-\n 2 files changed, 99 insertions(+), 42 deletions(-)",
    "diff": "diff --git a/src/qcam/main_window.cpp b/src/qcam/main_window.cpp\nindex b683c2e00d317307..e77bc01df8f3edfe 100644\n--- a/src/qcam/main_window.cpp\n+++ b/src/qcam/main_window.cpp\n@@ -278,17 +278,30 @@ void MainWindow::toggleCapture(bool start)\n int MainWindow::startCapture()\n {\n \tStreamRoles roles = StreamKeyValueParser::roles(options_[OptStream]);\n+\tstd::vector<Request *> requests;\n \tint ret;\n \n \t/* Verify roles are supported. */\n-\tif (roles.size() != 1) {\n-\t\tqWarning() << \"Only one stream supported\";\n-\t\treturn -EINVAL;\n-\t}\n-\n-\tif (roles[0] != StreamRole::Viewfinder) {\n-\t\tqWarning() << \"Only viewfinder supported\";\n-\t\treturn -EINVAL;\n+\tswitch (roles.size()) {\n+\tcase 1:\n+\t\tif (roles[0] != StreamRole::Viewfinder) {\n+\t\t\tqWarning() << \"Only viewfinder supported for single stream\";\n+\t\t\treturn -EINVAL;\n+\t\t}\n+\t\tbreak;\n+\tcase 2:\n+\t\tif (roles[0] != StreamRole::Viewfinder ||\n+\t\t    roles[1] != StreamRole::StillCaptureRaw) {\n+\t\t\tqWarning() << \"Only viewfinder + raw supported for dual streams\";\n+\t\t\treturn -EINVAL;\n+\t\t}\n+\t\tbreak;\n+\tdefault:\n+\t\tif (roles.size() != 1) {\n+\t\t\tqWarning() << \"Unsuported stream configuration\";\n+\t\t\treturn -EINVAL;\n+\t\t}\n+\t\tbreak;\n \t}\n \n \t/* Configure the camera. */\n@@ -298,17 +311,17 @@ int MainWindow::startCapture()\n \t\treturn -EINVAL;\n \t}\n \n-\tStreamConfiguration &cfg = config_->at(0);\n+\tStreamConfiguration &vfConfig = config_->at(0);\n \n \t/* Use a format supported by the viewfinder if available. */\n-\tstd::vector<PixelFormat> formats = cfg.formats().pixelformats();\n+\tstd::vector<PixelFormat> formats = vfConfig.formats().pixelformats();\n \tfor (const PixelFormat &format : viewfinder_->nativeFormats()) {\n \t\tauto match = std::find_if(formats.begin(), formats.end(),\n \t\t\t\t\t  [&](const PixelFormat &f) {\n \t\t\t\t\t\t  return f == format;\n \t\t\t\t\t  });\n \t\tif (match != formats.end()) {\n-\t\t\tcfg.pixelFormat = format;\n+\t\t\tvfConfig.pixelFormat = format;\n \t\t\tbreak;\n \t\t}\n \t}\n@@ -326,7 +339,7 @@ int MainWindow::startCapture()\n \n \tif (validation == CameraConfiguration::Adjusted)\n \t\tqInfo() << \"Stream configuration adjusted to \"\n-\t\t\t<< cfg.toString().c_str();\n+\t\t\t<< vfConfig.toString().c_str();\n \n \tret = camera_->configure(config_.get());\n \tif (ret < 0) {\n@@ -334,10 +347,16 @@ int MainWindow::startCapture()\n \t\treturn ret;\n \t}\n \n+\t/* Store stream allocation. */\n+\tvfStream_ = config_->at(0).stream();\n+\tif (config_->size() == 2)\n+\t\trawStream_ = config_->at(1).stream();\n+\telse\n+\t\trawStream_ = nullptr;\n+\n \t/* Configure the viewfinder. */\n-\tStream *stream = cfg.stream();\n-\tret = viewfinder_->setFormat(cfg.pixelFormat,\n-\t\t\t\t     QSize(cfg.size.width, cfg.size.height));\n+\tret = viewfinder_->setFormat(vfConfig.pixelFormat,\n+\t\t\t\t     QSize(vfConfig.size.width, vfConfig.size.height));\n \tif (ret < 0) {\n \t\tqInfo() << \"Failed to set viewfinder format\";\n \t\treturn ret;\n@@ -345,16 +364,33 @@ int MainWindow::startCapture()\n \n \tadjustSize();\n \n-\t/* Allocate buffers and requests. */\n+\t/* Allocate and map buffers. */\n \tallocator_ = new FrameBufferAllocator(camera_);\n-\tret = allocator_->allocate(stream);\n-\tif (ret < 0) {\n-\t\tqWarning() << \"Failed to allocate capture buffers\";\n-\t\treturn ret;\n+\tfor (StreamConfiguration &config : *config_) {\n+\t\tStream *stream = config.stream();\n+\n+\t\tret = allocator_->allocate(stream);\n+\t\tif (ret < 0) {\n+\t\t\tqWarning() << \"Failed to allocate capture buffers\";\n+\t\t\tgoto error;\n+\t\t}\n+\n+\t\tfor (const std::unique_ptr<FrameBuffer> &buffer : allocator_->buffers(stream)) {\n+\t\t\t/* Map memory buffers and cache the mappings. */\n+\t\t\tconst FrameBuffer::Plane &plane = buffer->planes().front();\n+\t\t\tvoid *memory = mmap(NULL, plane.length, PROT_READ, MAP_SHARED,\n+\t\t\t\t\t    plane.fd.fd(), 0);\n+\t\t\tmappedBuffers_[buffer.get()] = { memory, plane.length };\n+\n+\t\t\t/* Store buffers on the free list. */\n+\t\t\tfreeBuffers_[stream].enqueue(buffer.get());\n+\t\t}\n \t}\n \n-\tstd::vector<Request *> requests;\n-\tfor (const std::unique_ptr<FrameBuffer> &buffer : allocator_->buffers(stream)) {\n+\t/* Create requests and fill it with buffers from the viewfinder. */\n+\twhile (!freeBuffers_[vfStream_].isEmpty()) {\n+\t\tFrameBuffer *buffer = freeBuffers_[vfStream_].dequeue();\n+\n \t\tRequest *request = camera_->createRequest();\n \t\tif (!request) {\n \t\t\tqWarning() << \"Can't create request\";\n@@ -362,19 +398,13 @@ int MainWindow::startCapture()\n \t\t\tgoto error;\n \t\t}\n \n-\t\tret = request->addBuffer(stream, buffer.get());\n+\t\tret = request->addBuffer(vfStream_, buffer);\n \t\tif (ret < 0) {\n \t\t\tqWarning() << \"Can't set buffer for request\";\n \t\t\tgoto error;\n \t\t}\n \n \t\trequests.push_back(request);\n-\n-\t\t/* Map memory buffers and cache the mappings. */\n-\t\tconst FrameBuffer::Plane &plane = buffer->planes().front();\n-\t\tvoid *memory = mmap(NULL, plane.length, PROT_READ, MAP_SHARED,\n-\t\t\t\t    plane.fd.fd(), 0);\n-\t\tmappedBuffers_[buffer.get()] = { memory, plane.length };\n \t}\n \n \t/* Start the title timer and the camera. */\n@@ -419,6 +449,8 @@ error:\n \t}\n \tmappedBuffers_.clear();\n \n+\tfreeBuffers_.clear();\n+\n \tdelete allocator_;\n \tallocator_ = nullptr;\n \n@@ -461,7 +493,8 @@ void MainWindow::stopCapture()\n \t * but not processed yet. Clear the queue of done buffers to avoid\n \t * racing with the event handler.\n \t */\n-\tdoneQueue_.clear();\n+\tfreeBuffers_.clear();\n+\tdoneBuffers_.clear();\n \n \ttitleTimer_.stop();\n \tsetWindowTitle(title_);\n@@ -500,12 +533,9 @@ void MainWindow::requestComplete(Request *request)\n \t * are not allowed. Add the buffer to the done queue and post a\n \t * CaptureEvent for the application thread to handle.\n \t */\n-\tconst std::map<Stream *, FrameBuffer *> &buffers = request->buffers();\n-\tFrameBuffer *buffer = buffers.begin()->second;\n-\n \t{\n \t\tQMutexLocker locker(&mutex_);\n-\t\tdoneQueue_.enqueue(buffer);\n+\t\tdoneBuffers_.push_back(request->buffers());\n \t}\n \n \tQCoreApplication::postEvent(this, new CaptureEvent);\n@@ -518,16 +548,40 @@ void MainWindow::processCapture()\n \t * if stopCapture() has been called while a CaptureEvent was posted but\n \t * not processed yet. Return immediately in that case.\n \t */\n-\tFrameBuffer *buffer;\n-\n+\tstd::vector<std::map<Stream *, FrameBuffer *>> doneBuffers;\n \t{\n \t\tQMutexLocker locker(&mutex_);\n-\t\tif (doneQueue_.isEmpty())\n+\t\tif (doneBuffers_.empty())\n \t\t\treturn;\n \n-\t\tbuffer = doneQueue_.dequeue();\n+\t\tdoneBuffers = std::move(doneBuffers_);\n+\t\tdoneBuffers_.clear();\n \t}\n \n+\t/* Process buffers. */\n+\tfor (std::map<Stream *, FrameBuffer *> &buffers : doneBuffers) {\n+\t\tif (buffers.count(vfStream_))\n+\t\t\tprocessViewfinder(buffers[vfStream_]);\n+\n+\t\t/*\n+\t\t * Return buffers so they can be reused. No processing involving\n+\t\t * a buffer can happen after they are returned to the free list.\n+\t\t */\n+\t\tfor (auto &it : buffers) {\n+\t\t\tStream *stream = it.first;\n+\t\t\tFrameBuffer *buffer = it.second;\n+\n+\t\t\t/* The ViewFinder manages the viewfinder buffers. */\n+\t\t\tif (stream == vfStream_)\n+\t\t\t\tcontinue;\n+\n+\t\t\tfreeBuffers_[stream].enqueue(buffer);\n+\t\t}\n+\t}\n+}\n+\n+void MainWindow::processViewfinder(FrameBuffer *buffer)\n+{\n \tframesCaptured_++;\n \n \tconst FrameMetadata &metadata = buffer->metadata();\n@@ -554,8 +608,7 @@ void MainWindow::queueRequest(FrameBuffer *buffer)\n \t\treturn;\n \t}\n \n-\tStream *stream = config_->at(0).stream();\n-\trequest->addBuffer(stream, buffer);\n+\trequest->addBuffer(vfStream_, buffer);\n \n \tcamera_->queueRequest(request);\n }\ndiff --git a/src/qcam/main_window.h b/src/qcam/main_window.h\nindex 5e9d9b8d9c6b2d6d..c2040c0ebcd61bfa 100644\n--- a/src/qcam/main_window.h\n+++ b/src/qcam/main_window.h\n@@ -68,6 +68,7 @@ private:\n \n \tvoid requestComplete(Request *request);\n \tvoid processCapture();\n+\tvoid processViewfinder(FrameBuffer *buffer);\n \n \t/* UI elements */\n \tQToolBar *toolbar_;\n@@ -93,8 +94,11 @@ private:\n \n \t/* Capture state, buffers queue and statistics */\n \tbool isCapturing_;\n-\tQQueue<FrameBuffer *> doneQueue_;\n-\tQMutex mutex_;\t/* Protects doneQueue_ */\n+\tStream *vfStream_;\n+\tStream *rawStream_;\n+\tstd::map<Stream *, QQueue<FrameBuffer *>> freeBuffers_;\n+\tstd::vector<std::map<Stream *, FrameBuffer *>> doneBuffers_;\n+\tQMutex mutex_; /* Protects freeBuffers_ and doneBuffers_ */\n \n \tuint64_t lastBufferTime_;\n \tQElapsedTimer frameRateInterval_;\n",
    "prefixes": [
        "libcamera-devel",
        "RFC/PATCH",
        "2/5"
    ]
}