[{"id":2186,"web_url":"https://patchwork.libcamera.org/comment/2186/","msgid":"<5ab512b7-2fe0-52c3-15e0-ec96a18509d1@ideasonboard.com>","date":"2019-07-08T09:34:50","subject":"Re: [libcamera-devel] [PATCH 9/9] [HACK] test: v4l2_videodevice:\n\tAdd buffer import test","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"Hi Jacopo,\n\nOn 04/07/2019 23:53, Jacopo Mondi wrote:\n> Test buffer importing by streaming the camera to a video output device\n> performing zero-copy memory sharing using dmabuf file descriptors.\n> \n> Not-yet-Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>\n\nFollowing through this to see how the new usages will be:\n\n> \n> ---\n> Not suitable for merge yes. More a test utility for development at the\n> moment. To be morhped into a camera test from a video device one.\n> ---\n>  test/v4l2_videodevice/buffer_import.cpp | 234 ++++++++++++++++++++++++\n>  test/v4l2_videodevice/meson.build       |   1 +\n>  2 files changed, 235 insertions(+)\n>  create mode 100644 test/v4l2_videodevice/buffer_import.cpp\n> \n> diff --git a/test/v4l2_videodevice/buffer_import.cpp b/test/v4l2_videodevice/buffer_import.cpp\n> new file mode 100644\n> index 000000000000..0a294b055af5\n> --- /dev/null\n> +++ b/test/v4l2_videodevice/buffer_import.cpp\n> @@ -0,0 +1,234 @@\n> +/* SPDX-License-Identifier: GPL-2.0-or-later */\n> +/*\n> + * Copyright (C) 2019, Google Inc.\n> + *\n> + * libcamera V4L2 API tests\n> + *\n> + * Test importing buffers exported from an output device into a camera\n> + */\n> +\n> +#include <iostream>\n> +\n> +#include <libcamera/buffer.h>\n> +#include <libcamera/camera.h>\n> +#include <libcamera/camera_manager.h>\n> +#include <libcamera/event_dispatcher.h>\n> +#include <libcamera/timer.h>\n> +\n> +#include \"v4l2_videodevice_test.h\"\n> +\n> +using namespace libcamera;\n> +using namespace std;\n> +\n> +class BufferImportTest : public V4L2VideoDeviceTest\n> +{\n> +public:\n> +\tBufferImportTest()\n> +\t\t: V4L2VideoDeviceTest(\"vivid\", \"vivid-000-vid-out\")\n> +\t{\n> +\t}\n> +\n> +protected:\n> +\tvoid cameraBufferComplete(Request *request, Buffer *buffer)\n> +\t{\n> +\t\tif (buffer->status() != Buffer::BufferSuccess)\n> +\t\t\treturn;\n> +\n> +\t\tcapture_->queueBuffer(buffer);\n> +\t}\n> +\n> +\tvoid requestComplete(Request *request, const std::map<Stream *, Buffer *> &buffers)\n> +\t{\n> +\t\tif (request->status() != Request::RequestComplete)\n> +\t\t\treturn;\n> +\n> +\t\t/* Reuse the buffers for a new request. */\n> +\t\trequest = camera_->createRequest();\n> +\t\trequest->setBuffers(buffers);\n> +\t\tcamera_->queueRequest(request);\n> +\t}\n> +\n> +\tint init()\n> +\t{\n> +\t\tconstexpr unsigned int bufferCount = 4;\n> +\n> +\t\t/* Get a camera where to capture frames from. */\n> +\t\tcm_ = CameraManager::instance();\n> +\n> +\t\tif (cm_->start()) {\n> +\t\t\tcout << \"Failed to start camera manager\" << endl;\n> +\t\t\treturn TestFail;\n> +\t\t}\n> +\n> +\t\tcamera_ = cm_->get(\"Integrated Camera: Integrated C\");\n> +\t\tif (!camera_) {\n> +\t\t\tcout << \"Can not find VIMC camera\" << endl;\n> +\t\t\treturn TestSkip;\n> +\t\t}\n> +\n> +\t\tif (camera_->acquire()) {\n> +\t\t\tcout << \"Failed to acquire the camera\" << endl;\n> +\t\t\treturn TestFail;\n> +\t\t}\n> +\n> +\t\t/*\n> +\t\t * Initialize the output device and export buffers in a pool.\n> +\t\t * The 'output' device is actually called capture_ by the base\n> +\t\t * class.\n> +\t\t */\n\nPerhaps for the test/ demo it's worth just creating the output device\nmanually then? (I know this is a dummy hack/wip, so not exactly a\nnecessity here,)\n\n> +\t\tint ret = V4L2VideoDeviceTest::init();\n> +\t\tif (ret) {\n> +\t\t\tcerr << \"Failed to initialize output device\" << endl;\n> +\t\t\treturn ret;\n> +\t\t}\n> +\n> +\t\t/*\n> +\t\t * Set a known format on the output devices, then apply it\n> +\t\t * to the camera.\n> +\t\t */\n> +\t\tV4L2DeviceFormat format = {};\n> +\t\tif (capture_->getFormat(&format)) {\n> +\t\t\tcleanup();\n> +\t\t\treturn TestFail;\n> +\t\t}\n> +\n> +\t\tformat.size.width = 640;\n> +\t\tformat.size.height = 480;\n> +\t\tformat.fourcc = V4L2_PIX_FMT_YUYV;\n> +\t\tformat.planesCount = 1;\n> +\t\tformat.planes[0].size = 640 * 480 * 2;\n> +\t\tformat.planes[0].bpl = 640 * 2;\n> +\t\tif (capture_->setFormat(&format)) {\n> +\t\t\tcleanup();\n> +\t\t\treturn TestFail;\n> +\t\t}\n> +\n> +\t\tcout << \"Output format: \" << format.toString();\n> +\n> +\t\tconfig_ = camera_->generateConfiguration({ StreamRole::VideoRecording });\n> +\t\tif (!config_ || config_->size() != 1) {\n> +\t\t\tcout << \"Failed to generate default configuration\" << endl;\n> +\t\t\tcleanup();\n> +\t\t\treturn TestFail;\n> +\t\t}\n> +\n> +\t\t/*\n> +\t\t * Configure the Stream to work with externally allocated\n> +\t\t * buffers by setting the memoryType to ExternalMemory.\n> +\t\t */\n> +\t\tStreamConfiguration &cfg = config_->at(0);\n> +\t\tcfg.size = format.size;\n> +\t\tcfg.pixelFormat = format.fourcc;\n> +\t\tcfg.memoryType = ExternalMemory;\n\nOk - so with ExternalMemory, no pool is imported into the Stream, It's\njust (arbitrary) buffers being added at each request?\n\n> +\n> +\t\tif (camera_->configure(config_.get())) {\n> +\t\t\tcout << \"Failed to set modified configuration\" << endl;\n> +\t\t\tcleanup();\n> +\t\t\treturn TestFail;\n> +\t\t}\n> +\t\tcout << \"Capture format: \" << format.toString();\n> +\n> +\t\t/*\n> +\t\t * Export the output buffers to a pool and then import\n> +\t\t * them before setting up buffers in the Camera.\n> +\t\t */\n> +\t\tpool_.createBuffers(bufferCount);\n> +\t\tret = capture_->exportBuffers(&pool_);\n\nI would have imagined we could do something like\nstream_->importBuffers(pool_); ? (which would also then set the\nExternalMemory flag, because that becomes implicit)\n\n\n> +\t\tif (ret) {\n> +\t\t\tstd::cout << \"Failed to export buffers\" << std::endl;\n> +\t\t\tcleanup();\n> +\t\t\treturn TestFail;\n> +\t\t}\n> +\n> +\t\tif (camera_->allocateBuffers()) {\n\nAnd I guess this doesn't allocateBuffers any more?\n\nAhh yes, I see an earlier patch has suggested renaming this already...\n\nI agree - setupBuffers() could be more appropriate at the moment...\n\n\n> +\t\t\tcout << \"Failed to allocate buffers\" << endl;\n> +\t\t\treturn TestFail;\n> +\t\t}\n> +\n> +\t\treturn TestPass;\n> +\t}\n> +\n> +\tint run()\n> +\t{\n> +\t\tstd::vector<Request *> requests;\n> +\t\tStreamConfiguration &cfg = config_->at(0);\n> +\t\tStream *stream = cfg.stream();\n> +\t\t/* Create one request for each output video buffer. */\n> +\t\tfor (Buffer &buffer : pool_.buffers()) {\n> +\t\t\tRequest *request = camera_->createRequest();\n> +\t\t\tif (!request) {\n> +\t\t\t\tcout << \"Failed to create request\" << endl;\n> +\t\t\t\treturn TestFail;\n> +\t\t\t}\n> +\n> +\t\t\tstd::map<Stream *, Buffer *> map = { { stream, &buffer } };\n> +\t\t\tif (request->setBuffers(map)) {\n> +\t\t\t\tcout << \"Failed to associating buffer with request\" << endl;\n\ns/associating/associate/\n\n\n> +\t\t\t\treturn TestFail;\n> +\t\t\t}\n> +\n> +\t\t\trequests.push_back(request);\n> +\t\t}\n> +\n> +\t\t/* Connect the buffer ready signals of camera and output */\n> +\t\tcamera_->bufferCompleted.connect(this,\n> +\t\t\t\t&BufferImportTest::cameraBufferComplete);\n> +\n> +\t\t/* Connect the request ready signal to re-queue requests. */\n\nWe requeue buffers in a new request, the request is not re-queued. (we\ndon't/can't re-use requests currently).\n\n> +\t\tcamera_->requestCompleted.connect(this,\n> +\t\t\t\t&BufferImportTest::requestComplete);\n> +\n> +\t\tcapture_->streamOn();\n> +\t\tif (camera_->start()) {\n> +\t\t\tcout << \"Failed to start camera\" << endl;\n> +\t\t\treturn TestFail;\n> +\t\t}\n> +\n> +\t\tfor (Request *request : requests) {\n> +\t\t\tif (camera_->queueRequest(request)) {\n> +\t\t\t\tcout << \"Failed to queue request\" << endl;\n> +\t\t\t\tcamera_->stop();\n> +\t\t\t\tcapture_->streamOff();\n> +\t\t\t\tcleanup();\n> +\t\t\t\treturn TestFail;\n> +\t\t\t}\n> +\t\t}\n> +\n> +\t\tEventDispatcher *dispatcher = CameraManager::instance()->eventDispatcher();\n> +\n> +\t\tTimer timer;\n> +\t\ttimer.start(2000);\n> +\t\twhile (timer.isRunning())\n> +\t\t\tdispatcher->processEvents();\n> +\n> +\t\tif (camera_->stop()) {\n> +\t\t\tcout << \"Failed to stop camera\" << endl;\n> +\t\t\treturn TestFail;\n> +\t\t}\n> +\n> +\t\tcapture_->streamOff();\n> +\n> +\t\treturn TestPass;\n> +\t}\n> +\n> +\tvoid cleanup()\n> +\t{\n> +\t\tcamera_->freeBuffers();\n> +\n> +\t\tif (camera_) {\n> +\t\t\tcamera_->release();\n> +\t\t\tcamera_.reset();\n> +\t\t}\n> +\n> +\t\tcm_->stop();\n> +\n> +\t\tV4L2VideoDeviceTest::cleanup();\n> +\t}\n> +\n> +private:\n> +\tCameraManager *cm_;\n> +\tstd::shared_ptr<Camera> camera_;\n> +\tstd::unique_ptr<CameraConfiguration> config_;\n> +};\n> +\n> +TEST_REGISTER(BufferImportTest);\n> diff --git a/test/v4l2_videodevice/meson.build b/test/v4l2_videodevice/meson.build\n> index 76be5e142bb6..15169abe48d3 100644\n> --- a/test/v4l2_videodevice/meson.build\n> +++ b/test/v4l2_videodevice/meson.build\n> @@ -7,6 +7,7 @@ v4l2_videodevice_tests = [\n>      [ 'stream_on_off',      'stream_on_off.cpp' ],\n>      [ 'capture_async',      'capture_async.cpp' ],\n>      [ 'buffer_sharing',     'buffer_sharing.cpp' ],\n> +    [ 'buffer_import',      'buffer_import.cpp' ],\n>  ]\n>  \n>  foreach t : v4l2_videodevice_tests\n>","headers":{"Return-Path":"<kieran.bingham@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 4479060BC8\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon,  8 Jul 2019 11:34:54 +0200 (CEST)","from [192.168.0.20]\n\t(cpc89242-aztw30-2-0-cust488.18-1.cable.virginm.net [86.31.129.233])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 7141B56A;\n\tMon,  8 Jul 2019 11:34:53 +0200 (CEST)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1562578493;\n\tbh=IM3sc/KY7NnX14Q1IcclvdavLJOExSXHxTz2mfCh4Po=;\n\th=Reply-To:Subject:To:References:From:Date:In-Reply-To:From;\n\tb=Ke/58csVa2YaZzfHvhvOHYzBkKAvjq3/tVLJzc1rsUuJOCCXVDHUYFDFya3r2e+OF\n\tuV9lbvTbnzpEx3a+i8qpWaf/+nKLn/5mjGm/Wc82tqdVN0a7oHqGOAXQbW24/Rvvcd\n\t6QamyTW4dAS2vKx0csvRYidKDB49DiK2Bb7fJNFU=","Reply-To":"kieran.bingham@ideasonboard.com","To":"Jacopo Mondi <jacopo@jmondi.org>, libcamera-devel@lists.libcamera.org","References":"<20190704225334.26170-1-jacopo@jmondi.org>\n\t<20190704225334.26170-10-jacopo@jmondi.org>","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Openpgp":"preference=signencrypt","Autocrypt":"addr=kieran.bingham@ideasonboard.com; keydata=\n\tmQINBFYE/WYBEACs1PwjMD9rgCu1hlIiUA1AXR4rv2v+BCLUq//vrX5S5bjzxKAryRf0uHat\n\tV/zwz6hiDrZuHUACDB7X8OaQcwhLaVlq6byfoBr25+hbZG7G3+5EUl9cQ7dQEdvNj6V6y/SC\n\trRanWfelwQThCHckbobWiQJfK9n7rYNcPMq9B8e9F020LFH7Kj6YmO95ewJGgLm+idg1Kb3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I\n\t2DJO5FbxABEBAAGJAiUEGAEKAA8CGwwFAlnDlGsFCQeA/gIACgkQoR5GchCkYf1yYRAAq+Yo\n\tnbf9DGdK1kTAm2RTFg+w9oOp2Xjqfhds2PAhFFvrHQg1XfQR/UF/SjeUmaOmLSczM0s6XMeO\n\tVcE77UFtJ/+hLo4PRFKm5X1Pcar6g5m4xGqa+Xfzi9tRkwC29KMCoQOag1BhHChgqYaUH3yo\n\tUzaPwT/fY75iVI+yD0ih/e6j8qYvP8pvGwMQfrmN9YB0zB39YzCSdaUaNrWGD3iCBxg6lwSO\n\tLKeRhxxfiXCIYEf3vwOsP3YMx2JkD5doseXmWBGW1U0T/oJF+DVfKB6mv5UfsTzpVhJRgee7\n\t4jkjqFq4qsUGxcvF2xtRkfHFpZDbRgRlVmiWkqDkT4qMA+4q1y/dWwshSKi/uwVZNycuLsz+\n\t+OD8xPNCsMTqeUkAKfbD8xW4LCay3r/dD2ckoxRxtMD9eOAyu5wYzo/ydIPTh1QEj9SYyvp8\n\tO0g6CpxEwyHUQtF5oh15O018z3ZLztFJKR3RD42VKVsrnNDKnoY0f4U0z7eJv2NeF8xHMuiU\n\tRCIzqxX1GVYaNkKTnb/Qja8hnYnkUzY1Lc+OtwiGmXTwYsPZjjAaDX35J/RSKAoy5wGo/YFA\n\tJxB1gWThL4kOTbsqqXj9GLcyOImkW0lJGGR3o/fV91Zh63S5TKnf2YGGGzxki+ADdxVQAm+Q\n\tsbsRB8KNNvVXBOVNwko86rQqF9drZuw=","Organization":"Ideas on Board","Message-ID":"<5ab512b7-2fe0-52c3-15e0-ec96a18509d1@ideasonboard.com>","Date":"Mon, 8 Jul 2019 10:34:50 +0100","User-Agent":"Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101\n\tThunderbird/60.7.1","MIME-Version":"1.0","In-Reply-To":"<20190704225334.26170-10-jacopo@jmondi.org>","Content-Type":"text/plain; charset=utf-8","Content-Language":"en-GB","Content-Transfer-Encoding":"8bit","Subject":"Re: [libcamera-devel] [PATCH 9/9] [HACK] test: v4l2_videodevice:\n\tAdd buffer import test","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.23","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":"Mon, 08 Jul 2019 09:34:54 -0000"}}]