Show a patch.

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

{
    "id": 8434,
    "url": "https://patchwork.libcamera.org/api/1.1/patches/8434/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/8434/",
    "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": "<20200625223900.1282164-10-niklas.soderlund@ragnatech.se>",
    "date": "2020-06-25T22:39:00",
    "name": "[libcamera-devel,v4,9/9] libcamera: ipu3: Allow zero-copy RAW stream capture",
    "commit_ref": null,
    "pull_url": null,
    "state": "accepted",
    "archived": false,
    "hash": "808a7f10394820acd8e252384bec771cb7f182d2",
    "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/8434/mbox/",
    "series": [
        {
            "id": 1042,
            "url": "https://patchwork.libcamera.org/api/1.1/series/1042/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=1042",
            "date": "2020-06-25T22:38:51",
            "name": "libcamera: ipu3: Allow zero-copy RAW stream",
            "version": 4,
            "mbox": "https://patchwork.libcamera.org/series/1042/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/8434/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/8434/checks/",
    "tags": {},
    "headers": {
        "Return-Path": "<libcamera-devel-bounces@lists.libcamera.org>",
        "X-Original-To": "parsemail@patchwork.libcamera.org",
        "Delivered-To": "parsemail@patchwork.libcamera.org",
        "Received": [
            "from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 36246C2E65\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 25 Jun 2020 22:39:27 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id C5C6760BEC;\n\tFri, 26 Jun 2020 00:39:26 +0200 (CEST)",
            "from bin-mail-out-06.binero.net (bin-mail-out-06.binero.net\n\t[195.74.38.229])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id C377760AF1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 26 Jun 2020 00:39:25 +0200 (CEST)",
            "from bismarck.berto.se (p4fca2eca.dip0.t-ipconnect.de\n\t[79.202.46.202]) by bin-vsp-out-01.atm.binero.net (Halon) with ESMTPA\n\tid b66c5fad-b734-11ea-933e-005056917a89;\n\tFri, 26 Jun 2020 00:39:21 +0200 (CEST)"
        ],
        "X-Halon-ID": "b66c5fad-b734-11ea-933e-005056917a89",
        "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": "Fri, 26 Jun 2020 00:39:00 +0200",
        "Message-Id": "<20200625223900.1282164-10-niklas.soderlund@ragnatech.se>",
        "X-Mailer": "git-send-email 2.27.0",
        "In-Reply-To": "<20200625223900.1282164-1-niklas.soderlund@ragnatech.se>",
        "References": "<20200625223900.1282164-1-niklas.soderlund@ragnatech.se>",
        "MIME-Version": "1.0",
        "Subject": "[libcamera-devel] [PATCH v4 9/9] libcamera: ipu3: Allow zero-copy\n\tRAW stream capture",
        "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>",
        "Content-Type": "text/plain; charset=\"utf-8\"",
        "Content-Transfer-Encoding": "base64",
        "Errors-To": "libcamera-devel-bounces@lists.libcamera.org",
        "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"
    },
    "content": "With the refactored CIO2 interface it's now easy to add zero-copy for\nbuffers in the RAW stream. Use the internally allocated buffers inside\nthe CIO2Device if no buffer for the RAW stream is provided by the\napplication, or use the application-provided buffer if any.\n\nSigned-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\nReviewed-by: Jacopo Mondi <jacopo@jmondi.org>\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n* Changes since v3\n- Update comment.\n\n* Changes since v2\n- Add todo of potential common need code.\n\n* Changes since v1\n- Update comments.\n- Use Request::findBuffer() instead of own logic.\n---\n src/libcamera/pipeline/ipu3/cio2.cpp | 58 ++++++++++++++---------\n src/libcamera/pipeline/ipu3/cio2.h   |  7 ++-\n src/libcamera/pipeline/ipu3/ipu3.cpp | 69 +++++++++-------------------\n 3 files changed, 61 insertions(+), 73 deletions(-)",
    "diff": "diff --git a/src/libcamera/pipeline/ipu3/cio2.cpp b/src/libcamera/pipeline/ipu3/cio2.cpp\nindex d698cd80cba212a1..885e2ca0d9677364 100644\n--- a/src/libcamera/pipeline/ipu3/cio2.cpp\n+++ b/src/libcamera/pipeline/ipu3/cio2.cpp\n@@ -213,31 +213,16 @@ int CIO2Device::exportBuffers(unsigned int count,\n \treturn output_->exportBuffers(count, buffers);\n }\n \n-FrameBuffer *CIO2Device::getBuffer()\n-{\n-\tif (availableBuffers_.empty()) {\n-\t\tLOG(IPU3, Error) << \"CIO2 buffer underrun\";\n-\t\treturn nullptr;\n-\t}\n-\n-\tFrameBuffer *buffer = availableBuffers_.front();\n-\n-\tavailableBuffers_.pop();\n-\n-\treturn buffer;\n-}\n-\n-void CIO2Device::putBuffer(FrameBuffer *buffer)\n-{\n-\tavailableBuffers_.push(buffer);\n-}\n-\n int CIO2Device::start()\n {\n-\tint ret = output_->allocateBuffers(CIO2_BUFFER_COUNT, &buffers_);\n+\tint ret = output_->exportBuffers(CIO2_BUFFER_COUNT, &buffers_);\n \tif (ret < 0)\n \t\treturn ret;\n \n+\tret = output_->importBuffers(CIO2_BUFFER_COUNT);\n+\tif (ret)\n+\t\tLOG(IPU3, Error) << \"Failed to import CIO2 buffers\";\n+\n \tfor (std::unique_ptr<FrameBuffer> &buffer : buffers_)\n \t\tavailableBuffers_.push(buffer.get());\n \n@@ -257,11 +242,42 @@ int CIO2Device::stop()\n \treturn ret;\n }\n \n-int CIO2Device::queueBuffer(FrameBuffer *buffer)\n+int CIO2Device::queueBuffer(Request *request, FrameBuffer *rawBuffer)\n {\n+\tFrameBuffer *buffer = rawBuffer;\n+\n+\t/* If no buffer is provided in the request, use an internal one. */\n+\tif (!buffer) {\n+\t\tif (availableBuffers_.empty()) {\n+\t\t\tLOG(IPU3, Error) << \"CIO2 buffer underrun\";\n+\t\t\treturn -EINVAL;\n+\t\t}\n+\n+\t\tbuffer = availableBuffers_.front();\n+\t\tavailableBuffers_.pop();\n+\t}\n+\n+\tbuffer->setRequest(request);\n+\n \treturn output_->queueBuffer(buffer);\n }\n \n+void CIO2Device::tryReturnBuffer(FrameBuffer *buffer)\n+{\n+\t/*\n+\t * \\todo Once more pipelines deal with buffers that may be allocated\n+\t * internally or externally this pattern might become a common need. At\n+\t * that point this check should be moved to something clever in\n+\t * FrameBuffer.\n+\t */\n+\tfor (const std::unique_ptr<FrameBuffer> &buf : buffers_) {\n+\t\tif (buf.get() == buffer) {\n+\t\t\tavailableBuffers_.push(buffer);\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+}\n+\n void CIO2Device::freeBuffers()\n {\n \t/* The default std::queue constructor is explicit with gcc 5 and 6. */\ndiff --git a/src/libcamera/pipeline/ipu3/cio2.h b/src/libcamera/pipeline/ipu3/cio2.h\nindex 47c6f010eae6a64d..dc764b101f112f05 100644\n--- a/src/libcamera/pipeline/ipu3/cio2.h\n+++ b/src/libcamera/pipeline/ipu3/cio2.h\n@@ -18,6 +18,7 @@ namespace libcamera {\n class CameraSensor;\n class FrameBuffer;\n class MediaDevice;\n+class Request;\n class V4L2DeviceFormat;\n class V4L2Subdevice;\n class V4L2VideoDevice;\n@@ -40,15 +41,13 @@ public:\n \tint exportBuffers(unsigned int count,\n \t\t\t  std::vector<std::unique_ptr<FrameBuffer>> *buffers);\n \n-\tFrameBuffer *getBuffer();\n-\tvoid putBuffer(FrameBuffer *buffer);\n-\n \tint start();\n \tint stop();\n \n \tCameraSensor *sensor() { return sensor_; }\n \n-\tint queueBuffer(FrameBuffer *buffer);\n+\tint queueBuffer(Request *request, FrameBuffer *rawBuffer);\n+\tvoid tryReturnBuffer(FrameBuffer *buffer);\n \tSignal<FrameBuffer *> bufferReady;\n \n private:\ndiff --git a/src/libcamera/pipeline/ipu3/ipu3.cpp b/src/libcamera/pipeline/ipu3/ipu3.cpp\nindex 4818027e8db1f7a3..6c43169eb0915965 100644\n--- a/src/libcamera/pipeline/ipu3/ipu3.cpp\n+++ b/src/libcamera/pipeline/ipu3/ipu3.cpp\n@@ -122,7 +122,6 @@ public:\n \t}\n \n \tvoid imguOutputBufferReady(FrameBuffer *buffer);\n-\tvoid imguInputBufferReady(FrameBuffer *buffer);\n \tvoid cio2BufferReady(FrameBuffer *buffer);\n \n \tCIO2Device cio2_;\n@@ -737,25 +736,24 @@ void PipelineHandlerIPU3::stop(Camera *camera)\n int PipelineHandlerIPU3::queueRequestDevice(Camera *camera, Request *request)\n {\n \tIPU3CameraData *data = cameraData(camera);\n-\tFrameBuffer *buffer;\n \tint error = 0;\n \n-\t/* Get a CIO2 buffer, associate it with the request and queue it. */\n-\tbuffer = data->cio2_.getBuffer();\n-\tif (!buffer)\n-\t\treturn -EINVAL;\n-\n-\tbuffer->setRequest(request);\n-\tdata->cio2_.queueBuffer(buffer);\n+\t/*\n+\t * Queue a buffer on the CIO2, using the raw stream buffer provided in\n+\t * the request, if any, or a CIO2 internal buffer otherwise.\n+\t */\n+\tFrameBuffer *rawBuffer = request->findBuffer(&data->rawStream_);\n+\terror = data->cio2_.queueBuffer(request, rawBuffer);\n+\tif (error)\n+\t\treturn error;\n \n+\t/* Queue all buffers from the request aimed for the ImgU. */\n \tfor (auto it : request->buffers()) {\n \t\tIPU3Stream *stream = static_cast<IPU3Stream *>(it.first);\n-\t\tbuffer = it.second;\n-\n-\t\t/* Skip raw streams, they are copied from the CIO2 buffer. */\n \t\tif (stream->raw_)\n \t\t\tcontinue;\n \n+\t\tFrameBuffer *buffer = it.second;\n \t\tint ret = stream->device_->dev->queueBuffer(buffer);\n \t\tif (ret < 0)\n \t\t\terror = ret;\n@@ -885,8 +883,8 @@ int PipelineHandlerIPU3::registerCameras()\n \t\t */\n \t\tdata->cio2_.bufferReady.connect(data.get(),\n \t\t\t\t\t&IPU3CameraData::cio2BufferReady);\n-\t\tdata->imgu_->input_->bufferReady.connect(data.get(),\n-\t\t\t\t\t&IPU3CameraData::imguInputBufferReady);\n+\t\tdata->imgu_->input_->bufferReady.connect(&data->cio2_,\n+\t\t\t\t\t&CIO2Device::tryReturnBuffer);\n \t\tdata->imgu_->output_.dev->bufferReady.connect(data.get(),\n \t\t\t\t\t&IPU3CameraData::imguOutputBufferReady);\n \t\tdata->imgu_->viewfinder_.dev->bufferReady.connect(data.get(),\n@@ -915,22 +913,6 @@ int PipelineHandlerIPU3::registerCameras()\n  * Buffer Ready slots\n  */\n \n-/**\n- * \\brief Handle buffers completion at the ImgU input\n- * \\param[in] buffer The completed buffer\n- *\n- * Buffers completed from the ImgU input are immediately queued back to the\n- * CIO2 unit to continue frame capture.\n- */\n-void IPU3CameraData::imguInputBufferReady(FrameBuffer *buffer)\n-{\n-\t/* \\todo Handle buffer failures when state is set to BufferError. */\n-\tif (buffer->metadata().status == FrameMetadata::FrameCancelled)\n-\t\treturn;\n-\n-\tcio2_.putBuffer(buffer);\n-}\n-\n /**\n  * \\brief Handle buffers completion at the ImgU output\n  * \\param[in] buffer The completed buffer\n@@ -963,27 +945,18 @@ void IPU3CameraData::cio2BufferReady(FrameBuffer *buffer)\n \t\treturn;\n \n \tRequest *request = buffer->request();\n-\tFrameBuffer *raw = request->findBuffer(&rawStream_);\n \n-\tif (!raw) {\n-\t\t/* No RAW buffers present, just queue to IMGU. */\n-\t\timgu_->input_->queueBuffer(buffer);\n-\t\treturn;\n-\t}\n-\n-\t/* RAW buffers present, special care is needed. */\n-\tif (request->buffers().size() > 1)\n-\t\timgu_->input_->queueBuffer(buffer);\n-\n-\tif (raw->copyFrom(buffer))\n-\t\tLOG(IPU3, Debug) << \"Copy of FrameBuffer failed\";\n-\n-\tpipe_->completeBuffer(camera_, request, raw);\n-\n-\tif (request->buffers().size() == 1) {\n-\t\tcio2_.putBuffer(buffer);\n+\t/*\n+\t * If the request contains a buffer for the RAW stream only, complete it\n+\t * now as there's no need for ImgU processing.\n+\t */\n+\tif (request->findBuffer(&rawStream_) &&\n+\t    pipe_->completeBuffer(camera_, request, buffer)) {\n \t\tpipe_->completeRequest(camera_, request);\n+\t\treturn;\n \t}\n+\n+\timgu_->input_->queueBuffer(buffer);\n }\n \n /* -----------------------------------------------------------------------------\n",
    "prefixes": [
        "libcamera-devel",
        "v4",
        "9/9"
    ]
}