From patchwork Mon May 30 14:27:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomi Valkeinen X-Patchwork-Id: 16115 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id E84B6C3272 for ; Mon, 30 May 2022 14:27:51 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 864526564D; Mon, 30 May 2022 16:27:51 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1653920871; bh=UkzTO1W+wg3gwxN5XqnlVlliRzafRc+gxBtG4GGaOsU=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=K0bAJSy/9T83aftfJgfvp2F1JBZ8XcY1Rou3f6Q3clcSTdzTOqulQ/Zrj5/E4IDxm YVoeB35x+tuEHOszjVWyRSX6kwkOoyUYHc1+VXkFATrv+sSenr65h6fBqR+YJbtsWm Of4KpTvgB3xerEyQiirIOImeS2dcAua6wZyzooMOsSM0mBRiwHrUIpE5Zw92B4hC+t DotcW13ihk6twEwlZ4yeATNUpkVkB3gletzjkLAZlBSHrUuCOxI/mgsje/XupKrTxm bBnen4kZQPrcQilHL/7L+2rsb6sLK+outgdkGxu7m9hEsuWBHlYHKHA6Two8oTvFAg tcvsoL14E6m7Q== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 5E22560411 for ; Mon, 30 May 2022 16:27:43 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="OlVcfBir"; dkim-atps=neutral Received: from deskari.lan (91-156-85-209.elisa-laajakaista.fi [91.156.85.209]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id BE0821287; Mon, 30 May 2022 16:27:42 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1653920863; bh=UkzTO1W+wg3gwxN5XqnlVlliRzafRc+gxBtG4GGaOsU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OlVcfBir3IORTor3L/JhhasXzh+BrpL3dUrLcD/RF2ZwaL+kx03V5vp5le8Gf2+uj tro5IQPnNJjxz4EFW8vC/xE9H1dNxQgGsRUUO3kh1p6uPmlMpEkN8+tvvNxyFXFKf1 +sAMZIoQrVpoFv3G622jD0Yzr1/+jOa+pMp7K3Q0= To: libcamera-devel@lists.libcamera.org, David Plowman , Kieran Bingham , Laurent Pinchart , Jacopo Mondi Date: Mon, 30 May 2022 17:27:14 +0300 Message-Id: <20220530142722.57618-9-tomi.valkeinen@ideasonboard.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220530142722.57618-1-tomi.valkeinen@ideasonboard.com> References: <20220530142722.57618-1-tomi.valkeinen@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 08/16] py: MappedFrameBuffer: Support non-contextmanager use X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tomi Valkeinen via libcamera-devel From: Tomi Valkeinen Reply-To: Tomi Valkeinen Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Implement non-contextmanager use to MappedFrameBuffer so that we can either: with MappedFrameBuffer(fb) as mfb: ... or mfb = MappedFrameBuffer(fb) mfb.mmap() ... mfb.munmap() While at it, improve the error handling a bit. Note that the mmap() returns self. In other words, one can do this: mfb = MappedFrameBuffer(fb).mmap() ... mfb.munmap() Signed-off-by: Tomi Valkeinen Reviewed-by: Laurent Pinchart --- src/py/libcamera/utils/MappedFrameBuffer.py | 22 ++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/py/libcamera/utils/MappedFrameBuffer.py b/src/py/libcamera/utils/MappedFrameBuffer.py index a8502d51..c300a6d7 100644 --- a/src/py/libcamera/utils/MappedFrameBuffer.py +++ b/src/py/libcamera/utils/MappedFrameBuffer.py @@ -10,8 +10,19 @@ class MappedFrameBuffer: """ def __init__(self, fb: libcamera.FrameBuffer): self.__fb = fb + self.__planes = () + self.__maps = () def __enter__(self): + return self.mmap() + + def __exit__(self, exc_type, exc_value, exc_traceback): + self.munmap() + + def mmap(self): + if self.__planes: + raise RuntimeError('MappedFrameBuffer already mmapped') + import os import mmap @@ -68,14 +79,23 @@ class MappedFrameBuffer: return self - def __exit__(self, exc_type, exc_value, exc_traceback): + def munmap(self): + if not self.__planes: + raise RuntimeError('MappedFrameBuffer not mmapped') + for p in self.__planes: p.release() for mm in self.__maps: mm.close() + self.__planes = () + self.__maps = () + @property def planes(self) -> Tuple[memoryview, ...]: """memoryviews for the planes""" + if not self.__planes: + raise RuntimeError('MappedFrameBuffer not mmapped') + return self.__planes