[{"id":23232,"web_url":"https://patchwork.libcamera.org/comment/23232/","msgid":"<YpSM4YJxcFbgWnD7@pendragon.ideasonboard.com>","date":"2022-05-30T09:22:41","subject":"Re: [libcamera-devel] [PATCH v3 24/30] py: MappedFrameBuffer:\n\tSupport non-contextmanager use","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:41PM +0300, Tomi Valkeinen wrote:\n> Implement non-contextmanager use to MappedFrameBuffer so that we can\n> either:\n> \n> with MappedFrameBuffer(fb) as mfb:\n> \t...\n> \n> or\n> \n> mfb = MappedFrameBuffer(fb)\n> mfb.mmap()\n> ...\n> mfb.munmap()\n> \n> While at it, improve the error handling a bit.\n> \n> Note that the mmap() returns self. In other words, one can do this:\n> \n> mfb = MappedFrameBuffer(fb).mmap()\n> ...\n> mfb.munmap()\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/libcamera/utils/MappedFrameBuffer.py | 22 ++++++++++++++++++++-\n>  1 file changed, 21 insertions(+), 1 deletion(-)\n> \n> diff --git a/src/py/libcamera/utils/MappedFrameBuffer.py b/src/py/libcamera/utils/MappedFrameBuffer.py\n> index 69315e76..4b20f904 100644\n> --- a/src/py/libcamera/utils/MappedFrameBuffer.py\n> +++ b/src/py/libcamera/utils/MappedFrameBuffer.py\n> @@ -10,8 +10,19 @@ class MappedFrameBuffer:\n>      \"\"\"\n>      def __init__(self, fb: libcamera.FrameBuffer):\n>          self.__fb = fb\n> +        self.__planes = ()\n> +        self.__maps = ()\n>  \n>      def __enter__(self):\n> +        return self.mmap()\n> +\n> +    def __exit__(self, exc_type, exc_value, exc_traceback):\n> +        self.munmap()\n> +\n> +    def mmap(self):\n> +        if self.__planes:\n> +            raise RuntimeError('MappedFrameBuffer already mmapped')\n> +\n>          import os\n>          import mmap\n>  \n> @@ -68,14 +79,23 @@ class MappedFrameBuffer:\n>  \n>          return self\n>  \n> -    def __exit__(self, exc_type, exc_value, exc_traceback):\n> +    def munmap(self):\n> +        if not self.__planes:\n> +            raise RuntimeError('MappedFrameBuffer not mmapped')\n> +\n>          for p in self.__planes:\n>              p.release()\n>  \n>          for mm in self.__maps:\n>              mm.close()\n>  \n> +        self.__planes = ()\n> +        self.__maps = ()\n> +\n>      @property\n>      def planes(self) -> Tuple[memoryview, ...]:\n>          \"\"\"memoryviews for the planes\"\"\"\n> +        if not self.__planes:\n> +            raise RuntimeError('MappedFrameBuffer not mmapped')\n> +\n>          return self.__planes","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 208D7BD160\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 30 May 2022 09:22:46 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id C5CAC65633;\n\tMon, 30 May 2022 11:22:45 +0200 (CEST)","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 E21FA6040B\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 30 May 2022 11:22:43 +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 93DFD6BD;\n\tMon, 30 May 2022 11:22:43 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1653902565;\n\tbh=h2bLWezCb6Xlaoz9cW3VhmjxK5s/+dtUCH+3X/tGZ1o=;\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=byXrgokSkhW2IEGg/YlOQ72SPIfl+TlFUftKSEheDtSAx1zGspgIGhXlCijje3KPk\n\ticnt6HAaHhdS+/WCXkHavBsWtDCbB7NubmpPQGIEP77E0vGI3xbs4tGs0nE2G6apkP\n\tg36ZVAg0ovBJrxY146Hvwe3pXeQB5te/BA8GK1tNzxbXjeH6a+krBfeThAmvPx722V\n\tl/Z80P3o1mRNskKWhI1AlqA/CKduUS4t+GeaX4WUf8AdEImcyzJfpVGfKe8llrruDX\n\tjWA8cOkPj/WksuHoBCww/mJnlSluPXv2CSkdFC3I2wPwY0w/JTAqSWQ+Ql6ySeCY8n\n\twD7INbKW9gjHg==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1653902563;\n\tbh=h2bLWezCb6Xlaoz9cW3VhmjxK5s/+dtUCH+3X/tGZ1o=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=chY2TLzniHdugTtwZ9F7xabz5GiS/iSN3gvnp0a0fvoa8p3EstDF/KSUheGsxYfAY\n\tviPhkkJMCDSqH7RHXYpmI8faCaALZdLwJYidhngGTqeaWlzhXFpU/oGF5Qz2MP/Ikk\n\tcnDJk44VPqgnHt8YstW+rvGUKYHo4lDo9FW8l6dQ="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"chY2TLzn\"; dkim-atps=neutral","Date":"Mon, 30 May 2022 12:22:41 +0300","To":"Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>","Message-ID":"<YpSM4YJxcFbgWnD7@pendragon.ideasonboard.com>","References":"<20220527144447.94891-1-tomi.valkeinen@ideasonboard.com>\n\t<20220527144447.94891-25-tomi.valkeinen@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20220527144447.94891-25-tomi.valkeinen@ideasonboard.com>","Subject":"Re: [libcamera-devel] [PATCH v3 24/30] py: MappedFrameBuffer:\n\tSupport non-contextmanager use","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>"}}]