From patchwork Fri May 27 14:44:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomi Valkeinen X-Patchwork-Id: 16094 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 B123EC326E for ; Fri, 27 May 2022 14:45:36 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 70A186565D; Fri, 27 May 2022 16:45:36 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1653662736; bh=uHPpsYlvDhUb28WKb9TZp8sVFeHN/J/uHXXmuPnsmS8=; 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=d6F4UMDPLJ7tpdQzt6ABXRiE4shjH8w6JoDevXW1L0TCuBWiIXeJ5HkOwQc8pIVLt akCyw8hrLpezEt1/djYnHOaAS5oZ4CSWNazcSuuFyccMXE0eALrJsJ7C1/S70TsT4z nInRumRhlTnmD5gDIA3+Ipf36uI13l8OE3PUlOE2KbVDxLwjic/ksaUgioFtOn7yqs bhBLCDtMF3YLneP7zCLjgwde2xZhPKgmmPb6CHb7WbQZKxuggfk0JHogHBLH9+lRio lc8rrHYtt3oN+fywSzE9FK1VtaDvIsjM9GUIBeBpFL6CfWxKDU1qfi1vHk1dE2wy1M LJ1tlgVI727UA== 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 E5F306565C for ; Fri, 27 May 2022 16:45:16 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="u81QOrZg"; 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 58F2E10CA; Fri, 27 May 2022 16:45:16 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1653662716; bh=uHPpsYlvDhUb28WKb9TZp8sVFeHN/J/uHXXmuPnsmS8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=u81QOrZgvKCvTPk/CNc4Z27JH+vESqBRwWYGkmw3ohSW+OyckaoVBBAYGwdnFuJIW TB9TrfQg9gDQk66ec1UvYDq0BAziWBWknTaW5Np9Y5+qjef8KXdGyeeM+c8mw0wUnY iiJ5C0n1P0AixgmTyuLnoSMqJbFAXAOxSj1LZZvs= To: libcamera-devel@lists.libcamera.org, David Plowman , Kieran Bingham , Laurent Pinchart , Jacopo Mondi Date: Fri, 27 May 2022 17:44:41 +0300 Message-Id: <20220527144447.94891-25-tomi.valkeinen@ideasonboard.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220527144447.94891-1-tomi.valkeinen@ideasonboard.com> References: <20220527144447.94891-1-tomi.valkeinen@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 24/30] 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 69315e76..4b20f904 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