Show a patch.

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

{
    "id": 15089,
    "url": "https://patchwork.libcamera.org/api/patches/15089/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/15089/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/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": "<20211209092906.37303-6-tomi.valkeinen@ideasonboard.com>",
    "date": "2021-12-09T09:29:06",
    "name": "[libcamera-devel,RFC,v3,5/5] py: Add cam.py",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": false,
    "hash": "97dd133cf6c295d1da39ccecb701f44ce9f6b059",
    "submitter": {
        "id": 109,
        "url": "https://patchwork.libcamera.org/api/people/109/?format=api",
        "name": "Tomi Valkeinen",
        "email": "tomi.valkeinen@ideasonboard.com"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/15089/mbox/",
    "series": [
        {
            "id": 2829,
            "url": "https://patchwork.libcamera.org/api/series/2829/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=2829",
            "date": "2021-12-09T09:29:01",
            "name": "Python bindings",
            "version": 3,
            "mbox": "https://patchwork.libcamera.org/series/2829/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/15089/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/15089/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 A8B47BF415\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu,  9 Dec 2021 09:29:51 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 583806088C;\n\tThu,  9 Dec 2021 10:29:51 +0100 (CET)",
            "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 2C03160897\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu,  9 Dec 2021 10:29:45 +0100 (CET)",
            "from deskari.lan (91-156-85-209.elisa-laajakaista.fi\n\t[91.156.85.209])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id A17C9501;\n\tThu,  9 Dec 2021 10:29:44 +0100 (CET)"
        ],
        "Authentication-Results": "lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"Ra9yv/Ny\"; dkim-atps=neutral",
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1639042184;\n\tbh=mO6TgVwsQL5YnPXIbc+48g/U3x6udgxIFO3SUSBMQeg=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=Ra9yv/NyD0gFIIMmhSBVWznGk/qEadAczbRqtnDDdQBaxO28GqhmanwsIA+IbkS5a\n\t0IBqW8GDSM38SpuPj3X9SEZhCWxjU/fsaapQW+ylkZLCx3Ura5zEOMEyPhxRbEOQsV\n\tUXa5DZmAMFL+km8xbFIU/DVhrX5H7qBwrAhrkrAE=",
        "From": "Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Date": "Thu,  9 Dec 2021 11:29:06 +0200",
        "Message-Id": "<20211209092906.37303-6-tomi.valkeinen@ideasonboard.com>",
        "X-Mailer": "git-send-email 2.25.1",
        "In-Reply-To": "<20211209092906.37303-1-tomi.valkeinen@ideasonboard.com>",
        "References": "<20211209092906.37303-1-tomi.valkeinen@ideasonboard.com>",
        "MIME-Version": "1.0",
        "Content-Transfer-Encoding": "8bit",
        "Subject": "[libcamera-devel] [RFC v3 5/5] py: Add cam.py",
        "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>",
        "Errors-To": "libcamera-devel-bounces@lists.libcamera.org",
        "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"
    },
    "content": "Add cam.py, which mimics the 'cam' tool. Four rendering backends are\nadded:\n\n* null - Do nothing\n* kms - Use KMS with dmabufs\n* qt - SW render on a Qt window\n* qtgl - OpenGL render on a Qt window\n\nAll the renderers handle only a few pixel formats, and especially the GL\nrenderer is just a prototype.\n\nSigned-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>\n---\n src/py/test/cam.py        | 464 ++++++++++++++++++++++++++++++++++++++\n src/py/test/cam_kms.py    | 185 +++++++++++++++\n src/py/test/cam_null.py   |  46 ++++\n src/py/test/cam_qt.py     | 355 +++++++++++++++++++++++++++++\n src/py/test/cam_qtgl.py   | 385 +++++++++++++++++++++++++++++++\n src/py/test/gl_helpers.py |  67 ++++++\n 6 files changed, 1502 insertions(+)\n create mode 100755 src/py/test/cam.py\n create mode 100644 src/py/test/cam_kms.py\n create mode 100644 src/py/test/cam_null.py\n create mode 100644 src/py/test/cam_qt.py\n create mode 100644 src/py/test/cam_qtgl.py\n create mode 100644 src/py/test/gl_helpers.py",
    "diff": "diff --git a/src/py/test/cam.py b/src/py/test/cam.py\nnew file mode 100755\nindex 00000000..48df01cf\n--- /dev/null\n+++ b/src/py/test/cam.py\n@@ -0,0 +1,464 @@\n+#!/usr/bin/python3\n+# SPDX-License-Identifier: GPL-2.0-or-later\n+# Copyright (C) 2021, Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>\n+\n+import pycamera as pycam\n+import time\n+import binascii\n+import argparse\n+import selectors\n+import os\n+import sys\n+\n+class CustomCameraAction(argparse.Action):\n+\tdef __call__(self, parser, namespace, values, option_string=None):\n+\t\tprint(self.dest, values)\n+\n+\t\tif not \"camera\" in namespace or namespace.camera == None:\n+\t\t\tsetattr(namespace, \"camera\", [])\n+\n+\t\tprevious = namespace.camera\n+\t\tprevious.append((self.dest, values))\n+\t\tsetattr(namespace, \"camera\", previous)\n+\n+class CustomAction(argparse.Action):\n+\tdef __init__(self, option_strings, dest, **kwargs):\n+\t\tsuper().__init__(option_strings, dest, default={}, **kwargs)\n+\n+\tdef __call__(self, parser, namespace, values, option_string=None):\n+\t\tif len(namespace.camera) == 0:\n+\t\t\tprint(f\"Option {option_string} requires a --camera context\")\n+\t\t\tsys.exit(-1)\n+\n+\t\tif self.type == bool:\n+\t\t\tvalues = True\n+\n+\t\tcurrent = namespace.camera[-1]\n+\n+\t\tdata = getattr(namespace, self.dest)\n+\n+\t\tif self.nargs == \"+\":\n+\t\t\tif not current in data:\n+\t\t\t\tdata[current] = []\n+\n+\t\t\tdata[current] += values\n+\t\telse:\n+\t\t\tdata[current] = values\n+\n+\n+\n+def do_cmd_list(cm):\n+\tprint(\"Available cameras:\")\n+\n+\tfor idx,c in enumerate(cm.cameras):\n+\t\tprint(f\"{idx + 1}: {c.id}\")\n+\n+def do_cmd_list_props(ctx):\n+\tcamera = ctx[\"camera\"]\n+\n+\tprint(\"Properties for\", ctx[\"id\"])\n+\n+\tfor name, prop in camera.properties.items():\n+\t\tprint(\"\\t{}: {}\".format(name, prop))\n+\n+def do_cmd_list_controls(ctx):\n+\tcamera = ctx[\"camera\"]\n+\n+\tprint(\"Controls for\", ctx[\"id\"])\n+\n+\tfor name, prop in camera.controls.items():\n+\t\tprint(\"\\t{}: {}\".format(name, prop))\n+\n+def do_cmd_info(ctx):\n+\tcamera = ctx[\"camera\"]\n+\n+\tprint(\"Stream info for\", ctx[\"id\"])\n+\n+\troles = [pycam.StreamRole.Viewfinder]\n+\n+\tcamconfig = camera.generateConfiguration(roles)\n+\tif camconfig == None:\n+\t\traise Exception(\"Generating config failed\")\n+\n+\tfor i, stream_config in enumerate(camconfig):\n+\t\tprint(\"\\t{}: {}\".format(i, stream_config.toString()))\n+\n+\t\tformats = stream_config.formats\n+\t\tfor fmt in formats.pixelFormats:\n+\t\t\tprint(\"\\t * Pixelformat:\", fmt, formats.range(fmt))\n+\n+\t\t\tfor size in formats.sizes(fmt):\n+\t\t\t\tprint(\"\\t  -\", size)\n+\n+def acquire(ctx):\n+\tcamera = ctx[\"camera\"]\n+\n+\tcamera.acquire()\n+\n+def release(ctx):\n+\tcamera = ctx[\"camera\"]\n+\n+\tcamera.release()\n+\n+def parse_streams(ctx):\n+\tstreams = []\n+\n+\tfor stream_desc in ctx[\"opt-stream\"]:\n+\t\tstream_opts = {\"role\": pycam.StreamRole.Viewfinder}\n+\n+\t\tfor stream_opt in stream_desc.split(\",\"):\n+\t\t\tif stream_opt == 0:\n+\t\t\t\tcontinue\n+\n+\t\t\tarr = stream_opt.split(\"=\")\n+\t\t\tif len(arr) != 2:\n+\t\t\t\tprint(\"Bad stream option\", stream_opt)\n+\t\t\t\tsys.exit(-1)\n+\n+\t\t\tkey = arr[0]\n+\t\t\tvalue = arr[1]\n+\n+\t\t\tif key in [\"width\", \"height\"]:\n+\t\t\t\tvalue = int(value)\n+\t\t\telif key == \"role\":\n+\t\t\t\trolemap = {\n+\t\t\t\t\t\"still\": pycam.StreamRole.StillCapture,\n+\t\t\t\t\t\"raw\": pycam.StreamRole.Raw,\n+\t\t\t\t\t\"video\": pycam.StreamRole.VideoRecording,\n+\t\t\t\t\t\"viewfinder\": pycam.StreamRole.Viewfinder,\n+\t\t\t\t}\n+\n+\t\t\t\trole = rolemap.get(value.lower(), None)\n+\n+\t\t\t\tif role == None:\n+\t\t\t\t\tprint(\"Bad stream role\", value)\n+\t\t\t\t\tsys.exit(-1)\n+\n+\t\t\t\tvalue = role\n+\t\t\telif key == \"pixelformat\":\n+\t\t\t\tpass\n+\t\t\telse:\n+\t\t\t\tprint(\"Bad stream option key\", key)\n+\t\t\t\tsys.exit(-1)\n+\n+\t\t\tstream_opts[key] = value\n+\n+\t\tstreams.append(stream_opts)\n+\n+\treturn streams\n+\n+def configure(ctx):\n+\tcamera = ctx[\"camera\"]\n+\n+\tstreams = parse_streams(ctx)\n+\n+\troles = [opts[\"role\"] for opts in streams]\n+\n+\tcamconfig = camera.generateConfiguration(roles)\n+\tif camconfig == None:\n+\t\traise Exception(\"Generating config failed\")\n+\n+\tfor idx,stream_opts in enumerate(streams):\n+\t\tstream_config = camconfig.at(idx)\n+\n+\t\tif \"width\" in stream_opts and \"height\" in stream_opts:\n+\t\t\tstream_config.size = (stream_opts[\"width\"], stream_opts[\"height\"])\n+\n+\t\tif \"pixelformat\" in stream_opts:\n+\t\t\tstream_config.fmt = stream_opts[\"pixelformat\"]\n+\n+\tstat = camconfig.validate()\n+\n+\tif stat == pycam.ConfigurationStatus.Invalid:\n+\t\tprint(\"Camera configuration invalid\")\n+\t\texit(-1)\n+\telif stat == pycam.ConfigurationStatus.Adjusted:\n+\t\tif ctx[\"opt-strict-formats\"]:\n+\t\t\tprint(\"Adjusting camera configuration disallowed by --strict-formats argument\")\n+\t\t\texit(-1)\n+\n+\t\tprint(\"Camera configuration adjusted\")\n+\n+\tr = camera.configure(camconfig);\n+\tif r != 0:\n+\t\traise Exception(\"Configure failed\")\n+\n+\tctx[\"stream-names\"] = {}\n+\tctx[\"streams\"] = []\n+\n+\tfor idx, stream_config in enumerate(camconfig):\n+\t\tstream = stream_config.stream\n+\t\tctx[\"streams\"].append(stream)\n+\t\tctx[\"stream-names\"][stream] = \"stream\" + str(idx)\n+\t\tprint(\"{}-{}: stream config {}\".format(ctx[\"id\"], ctx[\"stream-names\"][stream], stream.configuration.toString()))\n+\n+def alloc_buffers(ctx):\n+\tcamera = ctx[\"camera\"]\n+\n+\tallocator = pycam.FrameBufferAllocator(camera);\n+\n+\tfor idx, stream in enumerate(ctx[\"streams\"]):\n+\t\tret = allocator.allocate(stream)\n+\t\tif ret < 0:\n+\t\t\tprint(\"Can't allocate buffers\")\n+\t\t\texit(-1)\n+\n+\t\tallocated = len(allocator.buffers(stream))\n+\n+\t\tprint(\"{}-{}: Allocated {} buffers\".format(ctx[\"id\"], ctx[\"stream-names\"][stream], allocated))\n+\n+\tctx[\"allocator\"] = allocator\n+\n+def create_requests(ctx):\n+\tcamera = ctx[\"camera\"]\n+\n+\tctx[\"requests\"] = []\n+\n+\t# Identify the stream with the least number of buffers\n+\tnum_bufs = min([len(ctx[\"allocator\"].buffers(stream)) for stream in ctx[\"streams\"]])\n+\n+\trequests = []\n+\n+\tfor buf_num in range(num_bufs):\n+\t\trequest = camera.createRequest()\n+\n+\t\tif request == None:\n+\t\t\tprint(\"Can't create request\")\n+\t\t\texit(-1)\n+\n+\t\tfor stream in ctx[\"streams\"]:\n+\t\t\tbuffers = ctx[\"allocator\"].buffers(stream)\n+\t\t\tbuffer = buffers[buf_num]\n+\n+\t\t\tret = request.addBuffer(stream, buffer)\n+\t\t\tif ret < 0:\n+\t\t\t\tprint(\"Can't set buffer for request\")\n+\t\t\t\texit(-1)\n+\n+\t\trequests.append(request)\n+\n+\tctx[\"requests\"] = requests\n+\n+def start(ctx):\n+\tcamera = ctx[\"camera\"]\n+\n+\tcamera.start()\n+\n+def stop(ctx):\n+\tcamera = ctx[\"camera\"]\n+\n+\tcamera.stop()\n+\n+def queue_requests(ctx):\n+\tcamera = ctx[\"camera\"]\n+\n+\tfor request in ctx[\"requests\"]:\n+\t\tcamera.queueRequest(request)\n+\t\tctx[\"reqs-queued\"] += 1\n+\n+\tdel ctx[\"requests\"]\n+\n+def capture_init(contexts):\n+\tfor ctx in contexts:\n+\t\tacquire(ctx)\n+\n+\tfor ctx in contexts:\n+\t\tconfigure(ctx)\n+\n+\tfor ctx in contexts:\n+\t\talloc_buffers(ctx)\n+\n+\tfor ctx in contexts:\n+\t\tcreate_requests(ctx)\n+\n+def capture_start(contexts):\n+\tfor ctx in contexts:\n+\t\tstart(ctx)\n+\n+\tfor ctx in contexts:\n+\t\tqueue_requests(ctx)\n+\n+# Called from renderer when there is a libcamera event\n+def event_handler(state):\n+\tcm = state[\"cm\"]\n+\tcontexts = state[\"contexts\"]\n+\n+\tdata = os.read(cm.efd, 8)\n+\n+\treqs = cm.getReadyRequests()\n+\n+\tfor req in reqs:\n+\t\tctx = next(ctx for ctx in contexts if ctx[\"camera\"] == req.camera)\n+\t\trequest_handler(state, ctx, req)\n+\n+\trunning = any(ctx[\"reqs-completed\"] < ctx[\"opt-capture\"] for ctx in contexts)\n+\treturn running\n+\n+def request_handler(state, ctx, req):\n+\tcamera = ctx[\"camera\"]\n+\n+\tif req.status != pycam.RequestStatus.Complete:\n+\t\traise Exception(\"{}: Request failed: {}\".format(ctx[\"id\"], req.status))\n+\n+\tbuffers = req.buffers\n+\n+\t# Compute the frame rate. The timestamp is arbitrarily retrieved from\n+\t# the first buffer, as all buffers should have matching timestamps.\n+\tts = buffers[next(iter(buffers))].metadata.timestamp\n+\tlast = ctx.get(\"last\", 0)\n+\tfps = 1000000000.0 / (ts - last) if (last != 0 and (ts - last) != 0) else 0\n+\tctx[\"last\"] = ts\n+\tctx[\"fps\"] = fps\n+\n+\tfor stream, fb in buffers.items():\n+\t\tstream_name = ctx[\"stream-names\"][stream]\n+\n+\t\tcrcs = []\n+\t\tif ctx[\"opt-crc\"]:\n+\t\t\twith fb.mmap(0) as b:\n+\t\t\t\tcrc = binascii.crc32(b)\n+\t\t\t\tcrcs.append(crc)\n+\n+\t\tmeta = fb.metadata\n+\n+\t\tprint(\"{:.6f} ({:.2f} fps) {}-{}: seq {}, bytes {}, CRCs {}\"\n+\t\t\t  .format(ts / 1000000000, fps,\n+\t\t\t\t\t  ctx[\"id\"], stream_name,\n+\t\t\t\t\t  meta.sequence, meta.bytesused,\n+\t\t\t\t\t  crcs))\n+\n+\t\tif ctx[\"opt-metadata\"]:\n+\t\t\treqmeta = req.metadata\n+\t\t\tfor ctrl, val in reqmeta.items():\n+\t\t\t\tprint(f\"\\t{ctrl} = {val}\")\n+\n+\t\tif ctx[\"opt-save-frames\"]:\n+\t\t\twith fb.mmap(0) as b:\n+\t\t\t\tfilename = \"frame-{}-{}-{}.data\".format(ctx[\"id\"], stream_name, ctx[\"reqs-completed\"])\n+\t\t\t\twith open(filename, \"wb\") as f:\n+\t\t\t\t\tf.write(b)\n+\n+\tstate[\"renderer\"].request_handler(ctx, req);\n+\n+\tctx[\"reqs-completed\"] += 1\n+\n+# Called from renderer when it has finished with a request\n+def request_prcessed(ctx, req):\n+\tcamera = ctx[\"camera\"]\n+\n+\tif ctx[\"reqs-queued\"] < ctx[\"opt-capture\"]:\n+\t\treq.reuse()\n+\t\tcamera.queueRequest(req)\n+\t\tctx[\"reqs-queued\"] += 1\n+\n+def capture_deinit(contexts):\n+\tfor ctx in contexts:\n+\t\tstop(ctx)\n+\n+\tfor ctx in contexts:\n+\t\trelease(ctx)\n+\n+def do_cmd_capture(state):\n+\tcapture_init(state[\"contexts\"])\n+\n+\trenderer = state[\"renderer\"]\n+\n+\trenderer.setup()\n+\n+\tcapture_start(state[\"contexts\"])\n+\n+\trenderer.run()\n+\n+\tcapture_deinit(state[\"contexts\"])\n+\n+def main():\n+\tparser = argparse.ArgumentParser()\n+\t# global options\n+\tparser.add_argument(\"-l\", \"--list\", action=\"store_true\", help=\"List all cameras\")\n+\tparser.add_argument(\"-c\", \"--camera\", type=int, action=\"extend\", nargs=1, default=[], help=\"Specify which camera to operate on, by index\")\n+\tparser.add_argument(\"-p\", \"--list-properties\", action=\"store_true\", help=\"List cameras properties\")\n+\tparser.add_argument(\"--list-controls\", action=\"store_true\", help=\"List cameras controls\")\n+\tparser.add_argument(\"-I\", \"--info\", action=\"store_true\", help=\"Display information about stream(s)\")\n+\tparser.add_argument(\"-R\", \"--renderer\", default=\"null\", help=\"Renderer (null, kms, qt, qtgl)\")\n+\n+\t# per camera options\n+\tparser.add_argument(\"-C\", \"--capture\", nargs=\"?\", type=int, const=1000000, action=CustomAction, help=\"Capture until interrupted by user or until CAPTURE frames captured\")\n+\tparser.add_argument(\"--crc\", nargs=0, type=bool, action=CustomAction, help=\"Print CRC32 for captured frames\")\n+\tparser.add_argument(\"--save-frames\", nargs=0, type=bool, action=CustomAction, help=\"Save captured frames to files\")\n+\tparser.add_argument(\"--metadata\", nargs=0, type=bool, action=CustomAction, help=\"Print the metadata for completed requests\")\n+\tparser.add_argument(\"--strict-formats\", type=bool, nargs=0, action=CustomAction, help=\"Do not allow requested stream format(s) to be adjusted\")\n+\tparser.add_argument(\"-s\", \"--stream\", nargs=\"+\", action=CustomAction)\n+\targs = parser.parse_args()\n+\n+\tcm = pycam.CameraManager.singleton()\n+\n+\tif args.list:\n+\t\tdo_cmd_list(cm)\n+\n+\tcontexts = []\n+\n+\tfor cam_idx in args.camera:\n+\t\tcamera = next((c for i,c in enumerate(cm.cameras) if i + 1 == cam_idx), None)\n+\n+\t\tif camera == None:\n+\t\t\tprint(\"Unable to find camera\", cam_idx)\n+\t\t\treturn -1\n+\n+\t\tcontexts.append({\n+\t\t\t\t\t\t\"camera\": camera,\n+\t\t\t\t\t\t\"idx\": cam_idx,\n+\t\t\t\t\t\t\"id\": \"cam\" + str(cam_idx),\n+\t\t\t\t\t\t\"reqs-queued\": 0,\n+\t\t\t\t\t\t\"reqs-completed\": 0,\n+\t\t\t\t\t\t\"opt-capture\": args.capture.get(cam_idx, False),\n+\t\t\t\t\t\t\"opt-crc\": args.crc.get(cam_idx, False),\n+\t\t\t\t\t\t\"opt-save-frames\": args.save_frames.get(cam_idx, False),\n+\t\t\t\t\t\t\"opt-metadata\": args.metadata.get(cam_idx, False),\n+\t\t\t\t\t\t\"opt-strict-formats\": args.strict_formats.get(cam_idx, False),\n+\t\t\t\t\t\t\"opt-stream\": args.stream.get(cam_idx, [\"role=viewfinder\"]),\n+\t\t\t\t\t\t})\n+\n+\tfor ctx in contexts:\n+\t\tprint(\"Using camera {} as {}\".format(ctx[\"camera\"].id, ctx[\"id\"]))\n+\n+\tfor ctx in contexts:\n+\t\tif args.list_properties:\n+\t\t\tdo_cmd_list_props(ctx)\n+\t\tif args.list_controls:\n+\t\t\tdo_cmd_list_controls(ctx)\n+\t\tif args.info:\n+\t\t\tdo_cmd_info(ctx)\n+\n+\tif args.capture:\n+\n+\t\tstate = {\n+\t\t\t\"cm\": cm,\n+\t\t\t\"contexts\": contexts,\n+\t\t\t\"event_handler\": event_handler,\n+\t\t\t\"request_prcessed\": request_prcessed,\n+\t\t}\n+\n+\t\tif args.renderer == \"null\":\n+\t\t\timport cam_null\n+\t\t\trenderer = cam_null.NullRenderer(state)\n+\t\telif args.renderer == \"kms\":\n+\t\t\timport cam_kms\n+\t\t\trenderer = cam_kms.KMSRenderer(state)\n+\t\telif args.renderer == \"qt\":\n+\t\t\timport cam_qt\n+\t\t\trenderer = cam_qt.QtRenderer(state)\n+\t\telif args.renderer == \"qtgl\":\n+\t\t\timport cam_qtgl\n+\t\t\trenderer = cam_qtgl.QtRenderer(state)\n+\t\telse:\n+\t\t\tprint(\"Bad renderer\", args.renderer)\n+\t\t\treturn -1\n+\n+\t\tstate[\"renderer\"] = renderer\n+\n+\t\tdo_cmd_capture(state)\n+\n+\treturn 0\n+\n+if __name__ == \"__main__\":\n+\tsys.exit(main())\ndiff --git a/src/py/test/cam_kms.py b/src/py/test/cam_kms.py\nnew file mode 100644\nindex 00000000..fb0e6375\n--- /dev/null\n+++ b/src/py/test/cam_kms.py\n@@ -0,0 +1,185 @@\n+# SPDX-License-Identifier: GPL-2.0-or-later\n+# Copyright (C) 2021, Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>\n+\n+import sys\n+import selectors\n+import pykms\n+\n+FMT_MAP = {\n+\t\"RGB888\": pykms.PixelFormat.RGB888,\n+\t\"YUYV\": pykms.PixelFormat.YUYV,\n+\t\"ARGB8888\": pykms.PixelFormat.ARGB8888,\n+\t\"XRGB8888\": pykms.PixelFormat.XRGB8888,\n+}\n+\n+class KMSRenderer:\n+\tdef __init__(self, state):\n+\t\tself.state = state\n+\n+\t\tself.cm = state[\"cm\"]\n+\t\tself.contexts = state[\"contexts\"]\n+\t\tself.running = False\n+\n+\t\tcard = pykms.Card()\n+\n+\t\tres = pykms.ResourceManager(card)\n+\t\tconn = res.reserve_connector()\n+\t\tcrtc = res.reserve_crtc(conn)\n+\t\tmode = conn.get_default_mode()\n+\t\tmodeb = mode.to_blob(card)\n+\n+\t\treq = pykms.AtomicReq(card)\n+\t\treq.add_connector(conn, crtc)\n+\t\treq.add_crtc(crtc, modeb)\n+\t\tr = req.commit_sync(allow_modeset = True)\n+\t\tassert(r == 0)\n+\n+\t\tself.card = card\n+\t\tself.resman = res\n+\t\tself.crtc = crtc\n+\t\tself.mode = mode\n+\n+\t\tself.bufqueue = []\n+\t\tself.current = None\n+\t\tself.next = None\n+\t\tself.cam_2_drm = {}\n+\n+\t# KMS\n+\n+\tdef close(self):\n+\t\treq = pykms.AtomicReq(self.card)\n+\t\tfor s in self.streams:\n+\t\t\treq.add_plane(s[\"plane\"], None, None, dst=(0, 0, 0, 0))\n+\t\treq.commit()\n+\n+\tdef add_plane(self, req, stream, fb):\n+\t\ts = next(s for s in self.streams if s[\"stream\"] == stream)\n+\t\tidx = s[\"idx\"]\n+\t\tplane = s[\"plane\"]\n+\n+\t\tif idx % 2 == 0:\n+\t\t\tx = 0\n+\t\telse:\n+\t\t\tx = self.mode.hdisplay - fb.width\n+\n+\t\tif idx // 2 == 0:\n+\t\t\ty = 0\n+\t\telse:\n+\t\t\ty = self.mode.vdisplay - fb.height\n+\n+\t\treq.add_plane(plane, fb, self.crtc, dst=(x, y, fb.width, fb.height))\n+\n+\tdef apply_request(self, drmreq):\n+\n+\t\tbuffers = drmreq[\"camreq\"].buffers\n+\n+\t\tfor stream, fb in buffers.items():\n+\t\t\tdrmfb = self.cam_2_drm.get(fb, None)\n+\n+\t\t\treq = pykms.AtomicReq(self.card)\n+\t\t\tself.add_plane(req, stream, drmfb)\n+\t\t\treq.commit()\n+\n+\tdef handle_page_flip(self, frame, time):\n+\t\told = self.current\n+\t\tself.current = self.next\n+\n+\t\tif len(self.bufqueue) > 0:\n+\t\t\tself.next = self.bufqueue.pop(0)\n+\t\telse:\n+\t\t\tself.next = None\n+\n+\t\tif self.next:\n+\t\t\tdrmreq = self.next\n+\n+\t\t\tself.apply_request(drmreq)\n+\n+\t\tif old:\n+\t\t\treq = old[\"camreq\"]\n+\t\t\tctx = old[\"camctx\"]\n+\t\t\tself.state[\"request_prcessed\"](ctx, req)\n+\n+\tdef queue(self, drmreq):\n+\t\tif not self.next:\n+\t\t\tself.next = drmreq\n+\t\t\tself.apply_request(drmreq)\n+\t\telse:\n+\t\t\tself.bufqueue.append(drmreq)\n+\n+\t# libcamera\n+\n+\tdef setup(self):\n+\t\tself.streams = []\n+\n+\t\tidx = 0\n+\t\tfor ctx in self.contexts:\n+\t\t\tcamera = ctx[\"camera\"]\n+\n+\t\t\tfor stream in ctx[\"streams\"]:\n+\n+\t\t\t\tcfg = stream.configuration\n+\t\t\t\tfmt = cfg.fmt\n+\t\t\t\tfmt = FMT_MAP[fmt]\n+\n+\t\t\t\tplane = self.resman.reserve_generic_plane(self.crtc, fmt)\n+\t\t\t\tassert(plane != None)\n+\n+\t\t\t\tself.streams.append({\n+\t\t\t\t\t\t\t\t\"idx\": idx,\n+\t\t\t\t\t\t\t\t\"stream\": stream,\n+\t\t\t\t\t\t\t\t\"plane\": plane,\n+\t\t\t\t\t\t\t\t\"fmt\": fmt,\n+\t\t\t\t\t\t\t\t\"size\": cfg.size,\n+\t\t\t\t\t\t\t   })\n+\n+\t\t\t\tfor fb in ctx[\"allocator\"].buffers(stream):\n+\t\t\t\t\tw, h = cfg.size\n+\t\t\t\t\tstride = cfg.stride\n+\t\t\t\t\tfd = fb.fd(0)\n+\t\t\t\t\tdrmfb = pykms.DmabufFramebuffer(self.card, w, h, fmt,\n+\t\t\t\t\t\t\t\t\t\t\t\t\t[fd], [stride], [0])\n+\t\t\t\t\tself.cam_2_drm[fb] = drmfb\n+\n+\t\t\t\tidx += 1\n+\n+\n+\tdef readdrm(self, fileobj):\n+\t\tfor ev in self.card.read_events():\n+\t\t\tif ev.type == pykms.DrmEventType.FLIP_COMPLETE:\n+\t\t\t\tself.handle_page_flip(ev.seq, ev.time)\n+\n+\tdef readcam(self, fd):\n+\t\tself.running = self.state[\"event_handler\"](self.state)\n+\n+\tdef readkey(self, fileobj):\n+\t\tsys.stdin.readline()\n+\t\tself.running = False\n+\n+\tdef run(self):\n+\t\tprint(\"Capturing...\")\n+\n+\t\tself.running = True\n+\n+\t\tsel = selectors.DefaultSelector()\n+\t\tsel.register(self.card.fd, selectors.EVENT_READ, self.readdrm)\n+\t\tsel.register(self.cm.efd, selectors.EVENT_READ, self.readcam)\n+\t\tsel.register(sys.stdin, selectors.EVENT_READ, self.readkey)\n+\n+\t\tprint(\"Press enter to exit\")\n+\n+\t\twhile self.running:\n+\t\t\tevents = sel.select()\n+\t\t\tfor key, mask in events:\n+\t\t\t\tcallback = key.data\n+\t\t\t\tcallback(key.fileobj)\n+\n+\t\tprint(\"Exiting...\")\n+\n+\tdef request_handler(self, ctx, req):\n+\n+\t\tdrmreq = {\n+\t\t\t\"camctx\": ctx,\n+\t\t\t\"camreq\": req,\n+\t\t}\n+\n+\t\tself.queue(drmreq)\ndiff --git a/src/py/test/cam_null.py b/src/py/test/cam_null.py\nnew file mode 100644\nindex 00000000..3935f5b6\n--- /dev/null\n+++ b/src/py/test/cam_null.py\n@@ -0,0 +1,46 @@\n+# SPDX-License-Identifier: GPL-2.0-or-later\n+# Copyright (C) 2021, Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>\n+\n+import sys\n+import selectors\n+\n+class NullRenderer:\n+\tdef __init__(self, state):\n+\t\tself.state = state\n+\n+\t\tself.cm = state[\"cm\"]\n+\t\tself.contexts = state[\"contexts\"]\n+\n+\t\tself.running = False\n+\n+\tdef setup(self):\n+\t\tpass\n+\n+\tdef run(self):\n+\t\tprint(\"Capturing...\")\n+\n+\t\tself.running = True\n+\n+\t\tsel = selectors.DefaultSelector()\n+\t\tsel.register(self.cm.efd, selectors.EVENT_READ, self.readcam)\n+\t\tsel.register(sys.stdin, selectors.EVENT_READ, self.readkey)\n+\n+\t\tprint(\"Press enter to exit\")\n+\n+\t\twhile self.running:\n+\t\t\tevents = sel.select()\n+\t\t\tfor key, mask in events:\n+\t\t\t\tcallback = key.data\n+\t\t\t\tcallback(key.fileobj)\n+\n+\t\tprint(\"Exiting...\")\n+\n+\tdef readcam(self, fd):\n+\t\tself.running = self.state[\"event_handler\"](self.state)\n+\n+\tdef readkey(self, fileobj):\n+\t\tsys.stdin.readline()\n+\t\tself.running = False\n+\n+\tdef request_handler(self, ctx, req):\n+\t\tself.state[\"request_prcessed\"](ctx, req)\ndiff --git a/src/py/test/cam_qt.py b/src/py/test/cam_qt.py\nnew file mode 100644\nindex 00000000..3ff12df6\n--- /dev/null\n+++ b/src/py/test/cam_qt.py\n@@ -0,0 +1,355 @@\n+# SPDX-License-Identifier: GPL-2.0-or-later\n+# Copyright (C) 2021, Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>\n+#\n+# Debayering code from PiCamera documentation\n+\n+from PyQt5 import QtCore, QtGui, QtWidgets\n+from io import BytesIO\n+from PIL import Image\n+from PIL.ImageQt import ImageQt\n+import numpy as np\n+from numpy.lib.stride_tricks import as_strided\n+import sys\n+\n+def rgb_to_pix(rgb):\n+\timg = Image.frombuffer(\"RGB\", (rgb.shape[1], rgb.shape[0]), rgb)\n+\tqim = ImageQt(img).copy()\n+\tpix = QtGui.QPixmap.fromImage(qim)\n+\treturn pix\n+\n+\n+def separate_components(data, r0, g0, g1, b0):\n+\t# Now to split the data up into its red, green, and blue components. The\n+\t# Bayer pattern of the OV5647 sensor is BGGR. In other words the first\n+\t# row contains alternating green/blue elements, the second row contains\n+\t# alternating red/green elements, and so on as illustrated below:\n+\t#\n+\t# GBGBGBGBGBGBGB\n+\t# RGRGRGRGRGRGRG\n+\t# GBGBGBGBGBGBGB\n+\t# RGRGRGRGRGRGRG\n+\t#\n+\t# Please note that if you use vflip or hflip to change the orientation\n+\t# of the capture, you must flip the Bayer pattern accordingly\n+\n+\trgb = np.zeros(data.shape + (3,), dtype=data.dtype)\n+\trgb[r0[1]::2, r0[0]::2, 0] = data[r0[1]::2, r0[0]::2] # Red\n+\trgb[g0[1]::2, g0[0]::2, 1] = data[g0[1]::2, g0[0]::2] # Green\n+\trgb[g1[1]::2, g1[0]::2, 1] = data[g1[1]::2, g1[0]::2] # Green\n+\trgb[b0[1]::2, b0[0]::2, 2] = data[b0[1]::2, b0[0]::2] # Blue\n+\n+\treturn rgb\n+\n+def demosaic(rgb, r0, g0, g1, b0):\n+\t# At this point we now have the raw Bayer data with the correct values\n+\t# and colors but the data still requires de-mosaicing and\n+\t# post-processing. If you wish to do this yourself, end the script here!\n+\t#\n+\t# Below we present a fairly naive de-mosaic method that simply\n+\t# calculates the weighted average of a pixel based on the pixels\n+\t# surrounding it. The weighting is provided b0[1] a b0[1]te representation of\n+\t# the Bayer filter which we construct first:\n+\n+\tbayer = np.zeros(rgb.shape, dtype=np.uint8)\n+\tbayer[r0[1]::2, r0[0]::2, 0] = 1 # Red\n+\tbayer[g0[1]::2, g0[0]::2, 1] = 1 # Green\n+\tbayer[g1[1]::2, g1[0]::2, 1] = 1 # Green\n+\tbayer[b0[1]::2, b0[0]::2, 2] = 1 # Blue\n+\n+\t# Allocate an array to hold our output with the same shape as the input\n+\t# data. After this we define the size of window that will be used to\n+\t# calculate each weighted average (3x3). Then we pad out the rgb and\n+\t# bayer arrays, adding blank pixels at their edges to compensate for the\n+\t# size of the window when calculating averages for edge pixels.\n+\n+\toutput = np.empty(rgb.shape, dtype=rgb.dtype)\n+\twindow = (3, 3)\n+\tborders = (window[0] - 1, window[1] - 1)\n+\tborder = (borders[0] // 2, borders[1] // 2)\n+\n+\t#rgb_pad = np.zeros((\n+\t#\trgb.shape[0] + borders[0],\n+\t#\trgb.shape[1] + borders[1],\n+\t#\trgb.shape[2]), dtype=rgb.dtype)\n+\t#rgb_pad[\n+\t#\tborder[0]:rgb_pad.shape[0] - border[0],\n+\t#\tborder[1]:rgb_pad.shape[1] - border[1],\n+\t#\t:] = rgb\n+\t#rgb = rgb_pad\n+\t#\n+\t#bayer_pad = np.zeros((\n+\t#\tbayer.shape[0] + borders[0],\n+\t#\tbayer.shape[1] + borders[1],\n+\t#\tbayer.shape[2]), dtype=bayer.dtype)\n+\t#bayer_pad[\n+\t#\tborder[0]:bayer_pad.shape[0] - border[0],\n+\t#\tborder[1]:bayer_pad.shape[1] - border[1],\n+\t#\t:] = bayer\n+\t#bayer = bayer_pad\n+\n+\t# In numpy >=1.7.0 just use np.pad (version in Raspbian is 1.6.2 at the\n+\t# time of writing...)\n+\t#\n+\trgb = np.pad(rgb, [\n+\t\t(border[0], border[0]),\n+\t\t(border[1], border[1]),\n+\t\t(0, 0),\n+\t\t], 'constant')\n+\tbayer = np.pad(bayer, [\n+\t\t(border[0], border[0]),\n+\t\t(border[1], border[1]),\n+\t\t(0, 0),\n+\t\t], 'constant')\n+\n+\t# For each plane in the RGB data, we use a nifty numpy trick\n+\t# (as_strided) to construct a view over the plane of 3x3 matrices. We do\n+\t# the same for the bayer array, then use Einstein summation on each\n+\t# (np.sum is simpler, but copies the data so it's slower), and divide\n+\t# the results to get our weighted average:\n+\n+\tfor plane in range(3):\n+\t\tp = rgb[..., plane]\n+\t\tb = bayer[..., plane]\n+\t\tpview = as_strided(p, shape=(\n+\t\t\tp.shape[0] - borders[0],\n+\t\t\tp.shape[1] - borders[1]) + window, strides=p.strides * 2)\n+\t\tbview = as_strided(b, shape=(\n+\t\t\tb.shape[0] - borders[0],\n+\t\t\tb.shape[1] - borders[1]) + window, strides=b.strides * 2)\n+\t\tpsum = np.einsum('ijkl->ij', pview)\n+\t\tbsum = np.einsum('ijkl->ij', bview)\n+\t\toutput[..., plane] = psum // bsum\n+\n+\treturn output\n+\n+\n+\n+\n+def to_rgb(fmt, size, data):\n+\tw = size[0]\n+\th = size[1]\n+\n+\tif fmt == \"YUYV\":\n+\t\t# YUV422\n+\t\tyuyv = data.reshape((h, w // 2 * 4))\n+\n+\t\t# YUV444\n+\t\tyuv = np.empty((h, w, 3), dtype=np.uint8)\n+\t\tyuv[:, :, 0] = yuyv[:, 0::2]\t\t\t\t\t# Y\n+\t\tyuv[:, :, 1] = yuyv[:, 1::4].repeat(2, axis=1)\t# U\n+\t\tyuv[:, :, 2] = yuyv[:, 3::4].repeat(2, axis=1)\t# V\n+\n+\t\tm = np.array([\n+\t\t\t[ 1.0, 1.0, 1.0],\n+\t\t\t[-0.000007154783816076815, -0.3441331386566162, 1.7720025777816772],\n+\t\t\t[ 1.4019975662231445, -0.7141380310058594 , 0.00001542569043522235]\n+\t\t])\n+\n+\t\trgb = np.dot(yuv, m)\n+\t\trgb[:, :, 0] -= 179.45477266423404\n+\t\trgb[:, :, 1] += 135.45870971679688\n+\t\trgb[:, :, 2] -= 226.8183044444304\n+\t\trgb = rgb.astype(np.uint8)\n+\n+\telif fmt == \"RGB888\":\n+\t\trgb = data.reshape((h, w, 3))\n+\t\trgb[:, :, [0, 1, 2]] = rgb[:, :, [2, 1, 0]]\n+\n+\telif fmt == \"BGR888\":\n+\t\trgb = data.reshape((h, w, 3))\n+\n+\telif fmt in [\"ARGB8888\", \"XRGB8888\"]:\n+\t\trgb = data.reshape((h, w, 4))\n+\t\trgb = np.flip(rgb, axis=2)\n+\t\t# drop alpha component\n+\t\trgb = np.delete(rgb, np.s_[0::4], axis=2)\n+\n+\telif fmt.startswith(\"S\"):\n+\t\tbayer_pattern = fmt[1:5]\n+\t\tbitspp = int(fmt[5:])\n+\n+\t\t# TODO: shifting leaves the lowest bits 0\n+\t\tif bitspp == 8:\n+\t\t\tdata = data.reshape((h, w))\n+\t\t\tdata = data.astype(np.uint16) << 8\n+\t\telif bitspp in [10, 12]:\n+\t\t\tdata = data.view(np.uint16)\n+\t\t\tdata = data.reshape((h, w))\n+\t\t\tdata = data << (16 - bitspp)\n+\t\telse:\n+\t\t\traise Exception(\"Bad bitspp:\" + str(bitspp))\n+\n+\t\tidx = bayer_pattern.find(\"R\")\n+\t\tassert(idx != -1)\n+\t\tr0 = (idx % 2, idx // 2)\n+\n+\t\tidx = bayer_pattern.find(\"G\")\n+\t\tassert(idx != -1)\n+\t\tg0 = (idx % 2, idx // 2)\n+\n+\t\tidx = bayer_pattern.find(\"G\", idx + 1)\n+\t\tassert(idx != -1)\n+\t\tg1 = (idx % 2, idx // 2)\n+\n+\t\tidx = bayer_pattern.find(\"B\")\n+\t\tassert(idx != -1)\n+\t\tb0 = (idx % 2, idx // 2)\n+\n+\t\trgb = separate_components(data, r0, g0, g1, b0)\n+\t\trgb = demosaic(rgb, r0, g0, g1, b0)\n+\t\trgb = (rgb >> 8).astype(np.uint8)\n+\n+\telse:\n+\t\trgb = None\n+\n+\treturn rgb\n+\n+\n+class QtRenderer:\n+\tdef __init__(self, state):\n+\t\tself.state = state\n+\n+\t\tself.cm = state[\"cm\"]\n+\t\tself.contexts = state[\"contexts\"]\n+\n+\tdef setup(self):\n+\t\tself.app = QtWidgets.QApplication([])\n+\n+\t\twindows = []\n+\n+\t\tfor ctx in self.contexts:\n+\t\t\tcamera = ctx[\"camera\"]\n+\n+\t\t\tfor stream in ctx[\"streams\"]:\n+\t\t\t\tfmt = stream.configuration.fmt\n+\t\t\t\tsize = stream.configuration.size\n+\n+\t\t\t\twindow = MainWindow(ctx, stream)\n+\t\t\t\twindow.setAttribute(QtCore.Qt.WA_ShowWithoutActivating)\n+\t\t\t\twindow.show()\n+\t\t\t\twindows.append(window)\n+\n+\t\tself.windows = windows\n+\n+\tdef run(self):\n+\t\tcamnotif = QtCore.QSocketNotifier(self.cm.efd, QtCore.QSocketNotifier.Read)\n+\t\tcamnotif.activated.connect(lambda x: self.readcam())\n+\n+\t\tkeynotif = QtCore.QSocketNotifier(sys.stdin.fileno(), QtCore.QSocketNotifier.Read)\n+\t\tkeynotif.activated.connect(lambda x: self.readkey())\n+\n+\t\tprint(\"Capturing...\")\n+\n+\t\tself.app.exec()\n+\n+\t\tprint(\"Exiting...\")\n+\n+\tdef readcam(self):\n+\t\trunning = self.state[\"event_handler\"](self.state)\n+\n+\t\tif not running:\n+\t\t\tself.app.quit()\n+\n+\tdef readkey(self):\n+\t\tsys.stdin.readline()\n+\t\tself.app.quit()\n+\n+\tdef request_handler(self, ctx, req):\n+\t\tbuffers = req.buffers\n+\n+\t\tfor stream, fb in buffers.items():\n+\t\t\twnd = next(wnd for wnd in self.windows if wnd.stream == stream)\n+\n+\t\t\twnd.handle_request(stream, fb)\n+\n+\t\tself.state[\"request_prcessed\"](ctx, req)\n+\n+\tdef cleanup(self):\n+\t\tfor w in self.windows:\n+\t\t\tw.close()\n+\n+\n+class MainWindow(QtWidgets.QWidget):\n+\tdef __init__(self, ctx, stream):\n+\t\tsuper().__init__()\n+\n+\t\tself.ctx = ctx\n+\t\tself.stream = stream\n+\n+\t\tself.label = QtWidgets.QLabel()\n+\n+\t\twindowLayout = QtWidgets.QHBoxLayout()\n+\t\tself.setLayout(windowLayout)\n+\n+\t\twindowLayout.addWidget(self.label)\n+\n+\t\tcontrolsLayout = QtWidgets.QVBoxLayout()\n+\t\twindowLayout.addLayout(controlsLayout)\n+\n+\t\twindowLayout.addStretch()\n+\n+\t\tgroup = QtWidgets.QGroupBox(\"Info\")\n+\t\tgroupLayout = QtWidgets.QVBoxLayout()\n+\t\tgroup.setLayout(groupLayout)\n+\t\tcontrolsLayout.addWidget(group)\n+\n+\t\tlab = QtWidgets.QLabel(ctx[\"id\"])\n+\t\tgroupLayout.addWidget(lab)\n+\n+\t\tself.frameLabel = QtWidgets.QLabel()\n+\t\tgroupLayout.addWidget(self.frameLabel)\n+\n+\n+\t\tgroup = QtWidgets.QGroupBox(\"Properties\")\n+\t\tgroupLayout = QtWidgets.QVBoxLayout()\n+\t\tgroup.setLayout(groupLayout)\n+\t\tcontrolsLayout.addWidget(group)\n+\n+\t\tcamera = ctx[\"camera\"]\n+\n+\t\tfor k, v in camera.properties.items():\n+\t\t\tlab = QtWidgets.QLabel()\n+\t\t\tlab.setText(k + \" = \" + str(v))\n+\t\t\tgroupLayout.addWidget(lab)\n+\n+\t\tgroup = QtWidgets.QGroupBox(\"Controls\")\n+\t\tgroupLayout = QtWidgets.QVBoxLayout()\n+\t\tgroup.setLayout(groupLayout)\n+\t\tcontrolsLayout.addWidget(group)\n+\n+\t\tfor k, (min, max, default) in camera.controls.items():\n+\t\t\tlab = QtWidgets.QLabel()\n+\t\t\tlab.setText(\"{} = {}/{}/{}\".format(k, min, max, default))\n+\t\t\tgroupLayout.addWidget(lab)\n+\n+\t\tcontrolsLayout.addStretch()\n+\n+\tdef buf_to_qpixmap(self, stream, fb):\n+\t\twith fb.mmap(0) as b:\n+\t\t\tcfg = stream.configuration\n+\t\t\tw, h = cfg.size\n+\t\t\tpitch = cfg.stride\n+\n+\t\t\tif cfg.fmt == \"MJPEG\":\n+\t\t\t\timg = Image.open(BytesIO(b))\n+\t\t\t\tqim = ImageQt(img).copy()\n+\t\t\t\tpix = QtGui.QPixmap.fromImage(qim)\n+\t\t\telse:\n+\t\t\t\tdata = np.array(b, dtype=np.uint8)\n+\t\t\t\trgb = to_rgb(cfg.fmt, cfg.size, data)\n+\n+\t\t\t\tif rgb is None:\n+\t\t\t\t\traise Exception(\"Format not supported: \" + cfg.fmt)\n+\n+\t\t\t\tpix = rgb_to_pix(rgb)\n+\n+\t\treturn pix\n+\n+\tdef handle_request(self, stream, fb):\n+\t\tctx = self.ctx\n+\n+\t\tpix = self.buf_to_qpixmap(stream, fb)\n+\t\tself.label.setPixmap(pix)\n+\n+\t\tself.frameLabel.setText(\"Queued: {}\\nDone: {}\\nFps: {:.2f}\"\n+\t\t\t.format(ctx[\"reqs-queued\"], ctx[\"reqs-completed\"], ctx[\"fps\"]))\ndiff --git a/src/py/test/cam_qtgl.py b/src/py/test/cam_qtgl.py\nnew file mode 100644\nindex 00000000..01168979\n--- /dev/null\n+++ b/src/py/test/cam_qtgl.py\n@@ -0,0 +1,385 @@\n+# SPDX-License-Identifier: GPL-2.0-or-later\n+# Copyright (C) 2021, Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>\n+\n+from PyQt5 import QtCore, QtWidgets\n+from PyQt5.QtCore import Qt\n+\n+import numpy as np\n+import sys\n+import os\n+os.environ[\"PYOPENGL_PLATFORM\"] = \"egl\"\n+import math\n+\n+import OpenGL\n+#OpenGL.FULL_LOGGING = True\n+\n+from OpenGL import GL as gl\n+from OpenGL.EGL.KHR.image import *\n+from OpenGL.EGL.EXT.image_dma_buf_import import *\n+from OpenGL.EGL.VERSION.EGL_1_0 import *\n+from OpenGL.EGL.VERSION.EGL_1_2 import *\n+from OpenGL.EGL.VERSION.EGL_1_3 import *\n+\n+from OpenGL.GLES2.VERSION.GLES2_2_0 import *\n+from OpenGL.GLES2.OES.EGL_image import *\n+from OpenGL.GLES2.OES.EGL_image_external import *\n+from OpenGL.GLES3.VERSION.GLES3_3_0 import *\n+\n+from OpenGL.GL import shaders\n+\n+from gl_helpers import *\n+\n+# libcamera format string -> DRM fourcc\n+FMT_MAP = {\n+\t\"RGB888\": \"RG24\",\n+\t\"XRGB8888\": \"XR24\",\n+\t\"ARGB8888\": \"AR24\",\n+\t\"YUYV\": \"YUYV\",\n+}\n+\n+class EglState:\n+\tdef __init__(self):\n+\t\tself.create_display()\n+\t\tself.choose_config()\n+\t\tself.create_context()\n+\t\tself.check_extensions()\n+\n+\tdef create_display(self):\n+\t\txdpy = getEGLNativeDisplay()\n+\t\tdpy = eglGetDisplay(xdpy)\n+\t\tself.display = dpy\n+\n+\tdef choose_config(self):\n+\t\tdpy = self.display\n+\n+\t\tmajor, minor = EGLint(), EGLint()\n+\n+\t\tb = eglInitialize(dpy, major, minor)\n+\t\tassert(b)\n+\n+\t\tprint(\"EGL {} {}\".format(\n+\t\t\t  eglQueryString(dpy, EGL_VENDOR).decode(),\n+\t\t\t  eglQueryString(dpy, EGL_VERSION).decode()))\n+\n+\t\tcheck_egl_extensions(dpy, [\"EGL_EXT_image_dma_buf_import\"])\n+\n+\t\tb = eglBindAPI(EGL_OPENGL_ES_API)\n+\t\tassert(b)\n+\n+\t\tdef print_config(dpy, cfg):\n+\n+\t\t\tdef _getconf(dpy, cfg, a):\n+\t\t\t\tvalue = ctypes.c_long()\n+\t\t\t\teglGetConfigAttrib(dpy, cfg, a, value)\n+\t\t\t\treturn value.value\n+\n+\t\t\tgetconf = lambda a: _getconf(dpy, cfg, a)\n+\n+\t\t\tprint(\"EGL Config {}: color buf {}/{}/{}/{} = {}, depth {}, stencil {}, native visualid {}, native visualtype {}\".format(\n+\t\t\t\tgetconf(EGL_CONFIG_ID),\n+\t\t\t\tgetconf(EGL_ALPHA_SIZE),\n+\t\t\t\tgetconf(EGL_RED_SIZE),\n+\t\t\t\tgetconf(EGL_GREEN_SIZE),\n+\t\t\t\tgetconf(EGL_BLUE_SIZE),\n+\t\t\t\tgetconf(EGL_BUFFER_SIZE),\n+\t\t\t\tgetconf(EGL_DEPTH_SIZE),\n+\t\t\t\tgetconf(EGL_STENCIL_SIZE),\n+\t\t\t\tgetconf(EGL_NATIVE_VISUAL_ID),\n+\t\t\t\tgetconf(EGL_NATIVE_VISUAL_TYPE)))\n+\n+\t\tif False:\n+\t\t\tnum_configs = ctypes.c_long()\n+\t\t\teglGetConfigs(dpy, None, 0, num_configs)\n+\t\t\tprint(\"{} configs\".format(num_configs.value))\n+\n+\t\t\tconfigs = (EGLConfig * num_configs.value)()\n+\t\t\teglGetConfigs(dpy, configs, num_configs.value, num_configs)\n+\t\t\tfor config_id in configs:\n+\t\t\t\tprint_config(dpy, config_id)\n+\n+\n+\t\tconfig_attribs = [\n+\t\t\tEGL_SURFACE_TYPE, EGL_WINDOW_BIT,\n+\t\t\tEGL_RED_SIZE, 8,\n+\t\t\tEGL_GREEN_SIZE, 8,\n+\t\t\tEGL_BLUE_SIZE, 8,\n+\t\t\tEGL_ALPHA_SIZE, 0,\n+\t\t\tEGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,\n+\t\t\tEGL_NONE,\n+\t\t]\n+\n+\t\tn = EGLint()\n+\t\tconfigs = (EGLConfig * 1)()\n+\t\tb = eglChooseConfig(dpy, config_attribs, configs, 1, n)\n+\t\tassert(b and n.value == 1)\n+\t\tconfig = configs[0]\n+\n+\t\tprint(\"Chosen Config:\")\n+\t\tprint_config(dpy, config)\n+\n+\t\tself.config = config\n+\n+\tdef create_context(self):\n+\t\tdpy = self.display\n+\n+\t\tcontext_attribs = [\n+\t\t\tEGL_CONTEXT_CLIENT_VERSION, 2,\n+\t\t\tEGL_NONE,\n+\t\t]\n+\n+\t\tcontext = eglCreateContext(dpy, self.config, EGL_NO_CONTEXT, context_attribs)\n+\t\tassert(context)\n+\n+\t\tb = eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, context)\n+\t\tassert(b)\n+\n+\t\tself.context = context\n+\n+\tdef check_extensions(self):\n+\t\tcheck_gl_extensions([\"GL_OES_EGL_image\"])\n+\n+\t\tassert(eglCreateImageKHR)\n+\t\tassert(eglDestroyImageKHR)\n+\t\tassert(glEGLImageTargetTexture2DOES)\n+\n+\n+class QtRenderer:\n+\tdef __init__(self, state):\n+\t\tself.state = state\n+\n+\tdef setup(self):\n+\t\tself.app = QtWidgets.QApplication([])\n+\n+\t\twindow = MainWindow(self.state)\n+\t\twindow.setAttribute(QtCore.Qt.WA_ShowWithoutActivating)\n+\t\twindow.show()\n+\n+\t\tself.window = window\n+\n+\tdef run(self):\n+\t\tcamnotif = QtCore.QSocketNotifier(self.state[\"cm\"].efd, QtCore.QSocketNotifier.Read)\n+\t\tcamnotif.activated.connect(lambda x: self.readcam())\n+\n+\t\tkeynotif = QtCore.QSocketNotifier(sys.stdin.fileno(), QtCore.QSocketNotifier.Read)\n+\t\tkeynotif.activated.connect(lambda x: self.readkey())\n+\n+\t\tprint(\"Capturing...\")\n+\n+\t\tself.app.exec()\n+\n+\t\tprint(\"Exiting...\")\n+\n+\tdef readcam(self):\n+\t\trunning = self.state[\"event_handler\"](self.state)\n+\n+\t\tif not running:\n+\t\t\tself.app.quit()\n+\n+\tdef readkey(self):\n+\t\tsys.stdin.readline()\n+\t\tself.app.quit()\n+\n+\tdef request_handler(self, ctx, req):\n+\t\tself.window.handle_request(ctx, req)\n+\n+\tdef cleanup(self):\n+\t\tself.window.close()\n+\n+\n+class MainWindow(QtWidgets.QWidget):\n+\tdef __init__(self, state):\n+\t\tsuper().__init__()\n+\n+\t\tself.setAttribute(Qt.WA_PaintOnScreen)\n+\t\tself.setAttribute(Qt.WA_NativeWindow)\n+\n+\t\tself.state = state\n+\n+\t\tself.textures = {}\n+\t\tself.reqqueue = {}\n+\t\tself.current = {}\n+\n+\t\tfor ctx in self.state[\"contexts\"]:\n+\n+\t\t\tself.reqqueue[ctx[\"idx\"]] = []\n+\t\t\tself.current[ctx[\"idx\"]] = []\n+\n+\t\t\tfor stream in ctx[\"streams\"]:\n+\t\t\t\tfmt = stream.configuration.fmt\n+\t\t\t\tsize = stream.configuration.size\n+\n+\t\t\t\tif not fmt in FMT_MAP:\n+\t\t\t\t\traise Exception(\"Unsupported pixel format: \" + str(fmt))\n+\n+\t\t\t\tself.textures[stream] = None\n+\n+\t\tnum_tiles = len(self.textures)\n+\t\tself.num_columns = math.ceil(math.sqrt(num_tiles))\n+\t\tself.num_rows = math.ceil(num_tiles / self.num_columns)\n+\n+\t\tself.egl = EglState()\n+\n+\t\tself.surface = None\n+\n+\tdef paintEngine(self):\n+\t\treturn None\n+\n+\tdef create_surface(self):\n+\t\tnative_surface = c_void_p(self.winId().__int__())\n+\t\tsurface = eglCreateWindowSurface(self.egl.display, self.egl.config,\n+\t\t\t\t\t\t\t\t\t\t native_surface, None)\n+\n+\t\tb = eglMakeCurrent(self.egl.display, self.surface, self.surface, self.egl.context)\n+\t\tassert(b)\n+\n+\t\tself.surface = surface\n+\n+\tdef init_gl(self):\n+\t\tself.create_surface()\n+\n+\t\tvertShaderSrc = \"\"\"\n+\t\t\tattribute vec2 aPosition;\n+\t\t\tvarying vec2 texcoord;\n+\n+\t\t\tvoid main()\n+\t\t\t{\n+\t\t\t\tgl_Position = vec4(aPosition * 2.0 - 1.0, 0.0, 1.0);\n+\t\t\t\ttexcoord.x = aPosition.x;\n+\t\t\t\ttexcoord.y = 1.0 - aPosition.y;\n+\t\t\t}\n+\t\t\"\"\"\n+\t\tfragShaderSrc = \"\"\"\n+\t\t\t#extension GL_OES_EGL_image_external : enable\n+\t\t\tprecision mediump float;\n+\t\t\tvarying vec2 texcoord;\n+\t\t\tuniform samplerExternalOES texture;\n+\n+\t\t\tvoid main()\n+\t\t\t{\n+\t\t\t\tgl_FragColor = texture2D(texture, texcoord);\n+\t\t\t}\n+\t\t\"\"\"\n+\n+\t\tprogram = shaders.compileProgram(\n+\t\t\tshaders.compileShader(vertShaderSrc, GL_VERTEX_SHADER),\n+\t\t\tshaders.compileShader(fragShaderSrc, GL_FRAGMENT_SHADER)\n+\t\t)\n+\n+\t\tglUseProgram(program)\n+\n+\t\tglClearColor(0.5, 0.8, 0.7, 1.0)\n+\n+\t\tvertPositions = [\n+\t\t\t 0.0,  0.0,\n+\t\t\t 1.0,  0.0,\n+\t\t\t 1.0,  1.0,\n+\t\t\t 0.0,  1.0\n+\t\t]\n+\n+\t\tinputAttrib = glGetAttribLocation(program, \"aPosition\")\n+\t\tglVertexAttribPointer(inputAttrib, 2, GL_FLOAT, GL_FALSE, 0, vertPositions)\n+\t\tglEnableVertexAttribArray(inputAttrib)\n+\n+\n+\tdef create_texture(self, stream, fb):\n+\t\tcfg = stream.configuration\n+\t\tfmt = cfg.fmt\n+\t\tfmt = str_to_fourcc(FMT_MAP[fmt])\n+\t\tw, h = cfg.size\n+\n+\t\tattribs = [\n+\t\t\tEGL_WIDTH, w,\n+\t\t\tEGL_HEIGHT, h,\n+\t\t\tEGL_LINUX_DRM_FOURCC_EXT, fmt,\n+\t\t\tEGL_DMA_BUF_PLANE0_FD_EXT, fb.fd(0),\n+\t\t\tEGL_DMA_BUF_PLANE0_OFFSET_EXT, 0,\n+\t\t\tEGL_DMA_BUF_PLANE0_PITCH_EXT, cfg.stride,\n+\t\t\tEGL_NONE,\n+\t\t]\n+\n+\t\timage = eglCreateImageKHR(self.egl.display,\n+\t\t\t\t\t\t\t\t  EGL_NO_CONTEXT,\n+\t\t\t\t\t\t\t\t  EGL_LINUX_DMA_BUF_EXT,\n+\t\t\t\t\t\t\t\t  None,\n+\t\t\t\t\t\t\t\t  attribs)\n+\t\tassert(image)\n+\n+\t\ttextures = glGenTextures(1)\n+\t\tglBindTexture(GL_TEXTURE_EXTERNAL_OES, textures)\n+\t\tglTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_LINEAR)\n+\t\tglTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR)\n+\t\tglTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)\n+\t\tglTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)\n+\t\tglEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, image)\n+\n+\t\treturn textures\n+\n+\tdef resizeEvent(self, event):\n+\t\tsize = event.size()\n+\n+\t\tprint(\"Resize\", size)\n+\n+\t\tsuper().resizeEvent(event)\n+\n+\t\tif self.surface == None:\n+\t\t\treturn\n+\n+\t\tglViewport(0, 0, size.width()//2, size.height())\n+\n+\tdef paintEvent(self, event):\n+\t\tif self.surface == None:\n+\t\t\tself.init_gl()\n+\n+\t\tfor ctx_idx, queue in self.reqqueue.items():\n+\t\t\tif len(queue) == 0:\n+\t\t\t\tcontinue\n+\n+\t\t\tctx = next(ctx for ctx in self.state[\"contexts\"] if ctx[\"idx\"] == ctx_idx)\n+\n+\t\t\tif self.current[ctx_idx]:\n+\t\t\t\told = self.current[ctx_idx]\n+\t\t\t\tself.current[ctx_idx] = None\n+\t\t\t\tself.state[\"request_prcessed\"](ctx, old)\n+\n+\t\t\tnext_req = queue.pop(0)\n+\t\t\tself.current[ctx_idx] = next_req\n+\n+\t\t\tstream, fb = next(iter(next_req.buffers.items()))\n+\n+\t\t\tself.textures[stream] = self.create_texture(stream, fb)\n+\n+\t\tself.paint_gl()\n+\n+\tdef paint_gl(self):\n+\t\tb = eglMakeCurrent(self.egl.display, self.surface, self.surface, self.egl.context)\n+\t\tassert(b)\n+\n+\t\tglClear(GL_COLOR_BUFFER_BIT)\n+\n+\t\tsize = self.size()\n+\n+\t\tfor idx,ctx in enumerate(self.state[\"contexts\"]):\n+\t\t\tfor stream in ctx[\"streams\"]:\n+\t\t\t\tif self.textures[stream] == None:\n+\t\t\t\t\tcontinue\n+\n+\t\t\t\tw = size.width() // self.num_columns\n+\t\t\t\th = size.height() // self.num_rows\n+\n+\t\t\t\tx = idx % self.num_columns\n+\t\t\t\ty = idx // self.num_columns\n+\n+\t\t\t\tx *= w\n+\t\t\t\ty *= h\n+\n+\t\t\t\tglViewport(x, y, w, h)\n+\n+\t\t\t\tglBindTexture(GL_TEXTURE_EXTERNAL_OES, self.textures[stream])\n+\t\t\t\tglDrawArrays(GL_TRIANGLE_FAN, 0, 4)\n+\n+\t\tb = eglSwapBuffers(self.egl.display, self.surface)\n+\t\tassert(b)\n+\n+\tdef handle_request(self, ctx, req):\n+\t\tself.reqqueue[ctx[\"idx\"]].append(req)\n+\t\tself.update()\ndiff --git a/src/py/test/gl_helpers.py b/src/py/test/gl_helpers.py\nnew file mode 100644\nindex 00000000..a80b03b2\n--- /dev/null\n+++ b/src/py/test/gl_helpers.py\n@@ -0,0 +1,67 @@\n+# SPDX-License-Identifier: GPL-2.0-or-later\n+# Copyright (C) 2021, Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>\n+\n+from OpenGL.EGL.VERSION.EGL_1_0 import EGLNativeDisplayType, eglGetProcAddress, eglQueryString, EGL_EXTENSIONS\n+\n+from OpenGL.raw.GLES2 import _types as _cs\n+from OpenGL.GLES2.VERSION.GLES2_2_0 import *\n+from OpenGL.GLES3.VERSION.GLES3_3_0 import *\n+from OpenGL import GL as gl\n+\n+from ctypes import c_int, c_char_p, c_void_p, cdll, POINTER, util, \\\n+\tpointer, CFUNCTYPE, c_bool\n+\n+def getEGLNativeDisplay():\n+\t_x11lib = cdll.LoadLibrary(util.find_library(\"X11\"))\n+\tXOpenDisplay = _x11lib.XOpenDisplay\n+\tXOpenDisplay.argtypes = [c_char_p]\n+\tXOpenDisplay.restype = POINTER(EGLNativeDisplayType)\n+\n+\txdpy = XOpenDisplay(None)\n+\n+# Hack. PyOpenGL doesn't seem to manage to find glEGLImageTargetTexture2DOES.\n+def getglEGLImageTargetTexture2DOES():\n+\tfuncptr = eglGetProcAddress(\"glEGLImageTargetTexture2DOES\")\n+\tprototype = CFUNCTYPE(None,_cs.GLenum,_cs.GLeglImageOES)\n+\treturn prototype(funcptr)\n+\n+glEGLImageTargetTexture2DOES = getglEGLImageTargetTexture2DOES()\n+\n+\n+def str_to_fourcc(str):\n+\tassert(len(str) == 4)\n+\tfourcc = 0\n+\tfor i,v in enumerate([ord(c) for c in str]):\n+\t\tfourcc |= v << (i * 8)\n+\treturn fourcc\n+\n+def get_gl_extensions():\n+\tn = GLint()\n+\tglGetIntegerv(GL_NUM_EXTENSIONS, n)\n+\tgl_extensions = []\n+\tfor i in range(n.value):\n+\t\tgl_extensions.append(gl.glGetStringi(GL_EXTENSIONS, i).decode())\n+\treturn gl_extensions\n+\n+def check_gl_extensions(required_extensions):\n+\textensions = get_gl_extensions()\n+\n+\tif False:\n+\t\tprint(\"GL EXTENSIONS: \", \" \".join(extensions))\n+\n+\tfor ext in required_extensions:\n+\t\tif not ext in extensions:\n+\t\t\traise Exception(ext + \" missing\")\n+\n+def get_egl_extensions(egl_display):\n+\treturn eglQueryString(egl_display, EGL_EXTENSIONS).decode().split(\" \")\n+\n+def check_egl_extensions(egl_display, required_extensions):\n+\textensions = get_egl_extensions(egl_display)\n+\n+\tif False:\n+\t\tprint(\"EGL EXTENSIONS: \", \" \".join(extensions))\n+\n+\tfor ext in required_extensions:\n+\t\tif not ext in extensions:\n+\t\t\traise Exception(ext + \" missing\")\n",
    "prefixes": [
        "libcamera-devel",
        "RFC",
        "v3",
        "5/5"
    ]
}