[{"id":23234,"web_url":"https://patchwork.libcamera.org/comment/23234/","msgid":"<YpSNwwtnfQOY6XW1@pendragon.ideasonboard.com>","date":"2022-05-30T09:26:27","subject":"Re: [libcamera-devel] [PATCH v3 26/30] py: cam: cam_qt: mmap the\n\tfbs only once","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Tomi,\n\nThank you for the patch.\n\nOn Fri, May 27, 2022 at 05:44:43PM +0300, Tomi Valkeinen wrote:\n> Instead of doing an mmap and munmap every time a Request is complete,\n> mmap all the buffers once at the start of the program.\n> \n> Signed-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> ---\n>  src/py/cam/cam_qt.py | 39 +++++++++++++++++++++++++--------------\n>  1 file changed, 25 insertions(+), 14 deletions(-)\n> \n> diff --git a/src/py/cam/cam_qt.py b/src/py/cam/cam_qt.py\n> index b6412bdf..c1723b44 100644\n> --- a/src/py/cam/cam_qt.py\n> +++ b/src/py/cam/cam_qt.py\n> @@ -52,6 +52,16 @@ class QtRenderer:\n>  \n>          self.windows = windows\n>  \n> +        buf_mmap_map = {}\n> +\n> +        for ctx in self.contexts:\n> +            for stream in ctx.streams:\n> +                for buf in ctx.allocator.buffers(stream):\n> +                    mfb = libcamera.utils.MappedFrameBuffer(buf).mmap()\n> +                    buf_mmap_map[buf] = mfb\n> +\n> +        self.buf_mmap_map = buf_mmap_map\n> +\n>      def run(self):\n>          camnotif = QtCore.QSocketNotifier(self.cm.event_fd, QtCore.QSocketNotifier.Read)\n>          camnotif.activated.connect(lambda _: self.readcam())\n> @@ -81,7 +91,9 @@ class QtRenderer:\n>          for stream, fb in buffers.items():\n>              wnd = next(wnd for wnd in self.windows if wnd.stream == stream)\n>  \n> -            wnd.handle_request(stream, fb)\n> +            mfb = self.buf_mmap_map[fb]\n> +\n> +            wnd.handle_request(stream, mfb)\n>  \n>          self.state.request_processed(ctx, req)\n>  \n> @@ -145,26 +157,25 @@ class MainWindow(QtWidgets.QWidget):\n>  \n>          controlsLayout.addStretch()\n>  \n> -    def buf_to_qpixmap(self, stream, fb):\n> -        with libcamera.utils.MappedFrameBuffer(fb) as mfb:\n> -            cfg = stream.configuration\n> +    def buf_to_qpixmap(self, stream, mfb):\n> +        cfg = stream.configuration\n>  \n> -            if cfg.pixel_format == libcam.formats.MJPEG:\n> -                pix = QtGui.QPixmap(cfg.size.width, cfg.size.height)\n> -                pix.loadFromData(mfb.planes[0])\n> -            else:\n> -                rgb = mfb_to_rgb(mfb, cfg)\n> -                if rgb is None:\n> -                    raise Exception('Format not supported: ' + cfg.pixel_format)\n> +        if cfg.pixel_format == libcam.formats.MJPEG:\n> +            pix = QtGui.QPixmap(cfg.size.width, cfg.size.height)\n> +            pix.loadFromData(mfb.planes[0])\n> +        else:\n> +            rgb = mfb_to_rgb(mfb, cfg)\n> +            if rgb is None:\n> +                raise Exception('Format not supported: ' + cfg.pixel_format)\n>  \n> -                pix = rgb_to_pix(rgb)\n> +            pix = rgb_to_pix(rgb)\n>  \n>          return pix\n>  \n> -    def handle_request(self, stream, fb):\n> +    def handle_request(self, stream, mfb):\n>          ctx = self.ctx\n>  \n> -        pix = self.buf_to_qpixmap(stream, fb)\n> +        pix = self.buf_to_qpixmap(stream, mfb)\n>          self.label.setPixmap(pix)\n>  \n>          self.frameLabel.setText('Queued: {}\\nDone: {}\\nFps: {:.2f}'","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 4D5D6BD161\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 30 May 2022 09:26:32 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id A0F7065635;\n\tMon, 30 May 2022 11:26:31 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id B18E76040B\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 30 May 2022 11:26:30 +0200 (CEST)","from pendragon.ideasonboard.com\n\t(lmontsouris-659-1-41-236.w92-154.abo.wanadoo.fr [92.154.76.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 0A6FC6BD;\n\tMon, 30 May 2022 11:26:30 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1653902791;\n\tbh=0ho8Fyc3jO13vAdsBHuUUkUpSxh9AHkuE6gp4HfJP2M=;\n\th=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=4BMrS9CShJ1goNWEmQJuuhEQache1KUx5UaWF+4HrDunZA/3h0zaLM85HiLKeHXcr\n\tmZhnmeevDTM0jiVHWyKwv2V3D6h7XM1uspIB9T08AL6Z5AE5com7hRHURTGdRJeCFZ\n\tWwKARjjNFj84UD5gK5xCXdFAHpoUqkb5VMwzSpsHIhjPq3Nt9xTlSllLj97kycPVrv\n\tcTzg5X75DeVVLaNRjMD904s/PUPNXF+XCtFImKPZzRm/6ASE0/aCHTypFbu5svhnCj\n\t+49yyy3HRs4VdUMEY5R45W2MEaLR1/8d9Iqf7nw58ctsBocy3ej3HUUPkgUAbaUOn+\n\tIrpnqCfez8ktg==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1653902790;\n\tbh=0ho8Fyc3jO13vAdsBHuUUkUpSxh9AHkuE6gp4HfJP2M=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=bpj8RPjhUgsieNgJRZwe2/8OrLCGpfC0EYp1ujOKKDTO/h7RNKLq7Bod0JcKbHLVj\n\tybc7YqGtL1vCUrDs1pe8SPGqeBjLG7NvA3guWts3/O1Z6CXYqmro2CG5ODONqZWIMY\n\tMj7OnBTL324TWJYXclGG30oXJnIhvsHGxzjtNoAk="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"bpj8RPjh\"; dkim-atps=neutral","Date":"Mon, 30 May 2022 12:26:27 +0300","To":"Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>","Message-ID":"<YpSNwwtnfQOY6XW1@pendragon.ideasonboard.com>","References":"<20220527144447.94891-1-tomi.valkeinen@ideasonboard.com>\n\t<20220527144447.94891-27-tomi.valkeinen@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20220527144447.94891-27-tomi.valkeinen@ideasonboard.com>","Subject":"Re: [libcamera-devel] [PATCH v3 26/30] py: cam: cam_qt: mmap the\n\tfbs only once","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>","From":"Laurent Pinchart via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]