From patchwork Wed Jun 29 07:04:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomi Valkeinen X-Patchwork-Id: 16409 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 20986BE173 for ; Wed, 29 Jun 2022 07:04:50 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 11ED065648; Wed, 29 Jun 2022 09:04:47 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1656486287; bh=Pa5/wJaKKF4mEpNi/1FHR/wV42PbNXVxJ/ZFRVOKfAQ=; 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=B9C9MiCTiQyPl4pfTqKBbShZ2BTH5pJf2UBxsGPo3F++oKO2M/vSqGVVq9yioa2bK N5qbnaVwJnziyp3H08vMFufqb87N3+oZEZ6kvlGzD97sj0i5HOZf9gozAyO2Umb1JN IA/rl6zfVZZAmPOYITwqBvy2OHkTwLq2WoQCuMGxODgUUJkrcIwz2TASdTzTizmDHu zlfZnZQdy2GWVt8FRBmT3XK3bnoNyFpj7HBxpSWKyz4FK67cIYA2N1xRQrT72UuLxh WshikOyI7ZQNEBB7mt2lfAiXWwwINXUKhY4AelvrzQ47rZGZ4TmCxqT8zsKNuxytPy fK2aRtraKWmFw== 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 740A065635 for ; Wed, 29 Jun 2022 09:04:41 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="t2j6apth"; dkim-atps=neutral Received: from deskari.lan (91-158-154-79.elisa-laajakaista.fi [91.158.154.79]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id E56BF4A8; Wed, 29 Jun 2022 09:04:40 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1656486281; bh=Pa5/wJaKKF4mEpNi/1FHR/wV42PbNXVxJ/ZFRVOKfAQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=t2j6apthdQv39A51Zri0YY0XRvtSeEJ6HR8qM5TqsN2UlimsVQfbRGrYT8LZi+g/Y z7CTNa0b3PlTlWdXuyUJcjgNY0KsidCbGopkbDLm+u6b6jskh0II4UZwRfJNm/jGvE r0xX1lWT3pNQSll77ZcN9lqkLNlubLSeOBWGf4gU= To: libcamera-devel@lists.libcamera.org, David Plowman , Kieran Bingham , Laurent Pinchart , Jacopo Mondi Date: Wed, 29 Jun 2022 10:04:05 +0300 Message-Id: <20220629070416.17550-4-tomi.valkeinen@ideasonboard.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220629070416.17550-1-tomi.valkeinen@ideasonboard.com> References: <20220629070416.17550-1-tomi.valkeinen@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 03/14] py: Move ControlValue helpers to py_helpers.cpp 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" Clean up the py_main.cpp a bit by moving the ControlValue helpers to a separate file. Signed-off-by: Tomi Valkeinen Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- src/py/libcamera/meson.build | 1 + src/py/libcamera/py_helpers.cpp | 97 +++++++++++++++++++++++++++++++++ src/py/libcamera/py_helpers.h | 13 +++++ src/py/libcamera/py_main.cpp | 83 +--------------------------- 4 files changed, 113 insertions(+), 81 deletions(-) create mode 100644 src/py/libcamera/py_helpers.cpp create mode 100644 src/py/libcamera/py_helpers.h diff --git a/src/py/libcamera/meson.build b/src/py/libcamera/meson.build index eb884538..04578bac 100644 --- a/src/py/libcamera/meson.build +++ b/src/py/libcamera/meson.build @@ -15,6 +15,7 @@ pybind11_dep = pybind11_proj.get_variable('pybind11_dep') pycamera_sources = files([ 'py_enums.cpp', 'py_geometry.cpp', + 'py_helpers.cpp', 'py_main.cpp', ]) diff --git a/src/py/libcamera/py_helpers.cpp b/src/py/libcamera/py_helpers.cpp new file mode 100644 index 00000000..45aecce9 --- /dev/null +++ b/src/py/libcamera/py_helpers.cpp @@ -0,0 +1,97 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2022, Tomi Valkeinen + */ + +#include "py_helpers.h" + +#include + +#include +#include +#include + +namespace py = pybind11; + +using namespace libcamera; + +template +static py::object valueOrTuple(const ControlValue &cv) +{ + if (cv.isArray()) { + const T *v = reinterpret_cast(cv.data().data()); + auto t = py::tuple(cv.numElements()); + + for (size_t i = 0; i < cv.numElements(); ++i) + t[i] = v[i]; + + return std::move(t); + } + + return py::cast(cv.get()); +} + +py::object controlValueToPy(const ControlValue &cv) +{ + switch (cv.type()) { + case ControlTypeBool: + return valueOrTuple(cv); + case ControlTypeByte: + return valueOrTuple(cv); + case ControlTypeInteger32: + return valueOrTuple(cv); + case ControlTypeInteger64: + return valueOrTuple(cv); + case ControlTypeFloat: + return valueOrTuple(cv); + case ControlTypeString: + return py::cast(cv.get()); + case ControlTypeRectangle: { + const Rectangle *v = reinterpret_cast(cv.data().data()); + return py::cast(v); + } + case ControlTypeSize: { + const Size *v = reinterpret_cast(cv.data().data()); + return py::cast(v); + } + case ControlTypeNone: + default: + throw std::runtime_error("Unsupported ControlValue type"); + } +} + +template +static ControlValue controlValueMaybeArray(const py::object &ob) +{ + if (py::isinstance(ob) || py::isinstance(ob)) { + std::vector vec = ob.cast>(); + return ControlValue(Span(vec)); + } + + return ControlValue(ob.cast()); +} + +ControlValue pyToControlValue(const py::object &ob, ControlType type) +{ + switch (type) { + case ControlTypeBool: + return ControlValue(ob.cast()); + case ControlTypeByte: + return controlValueMaybeArray(ob); + case ControlTypeInteger32: + return controlValueMaybeArray(ob); + case ControlTypeInteger64: + return controlValueMaybeArray(ob); + case ControlTypeFloat: + return controlValueMaybeArray(ob); + case ControlTypeString: + return ControlValue(ob.cast()); + case ControlTypeRectangle: + return ControlValue(ob.cast()); + case ControlTypeSize: + return ControlValue(ob.cast()); + case ControlTypeNone: + default: + throw std::runtime_error("Control type not implemented"); + } +} diff --git a/src/py/libcamera/py_helpers.h b/src/py/libcamera/py_helpers.h new file mode 100644 index 00000000..cd31e2cc --- /dev/null +++ b/src/py/libcamera/py_helpers.h @@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2022, Tomi Valkeinen + */ + +#pragma once + +#include + +#include + +pybind11::object controlValueToPy(const libcamera::ControlValue &cv); +libcamera::ControlValue pyToControlValue(const pybind11::object &ob, libcamera::ControlType type); diff --git a/src/py/libcamera/py_main.cpp b/src/py/libcamera/py_main.cpp index 4b698f77..e652837f 100644 --- a/src/py/libcamera/py_main.cpp +++ b/src/py/libcamera/py_main.cpp @@ -21,6 +21,8 @@ #include #include +#include "py_helpers.h" + namespace py = pybind11; using namespace libcamera; @@ -31,87 +33,6 @@ LOG_DEFINE_CATEGORY(Python) } -template -static py::object valueOrTuple(const ControlValue &cv) -{ - if (cv.isArray()) { - const T *v = reinterpret_cast(cv.data().data()); - auto t = py::tuple(cv.numElements()); - - for (size_t i = 0; i < cv.numElements(); ++i) - t[i] = v[i]; - - return std::move(t); - } - - return py::cast(cv.get()); -} - -static py::object controlValueToPy(const ControlValue &cv) -{ - switch (cv.type()) { - case ControlTypeBool: - return valueOrTuple(cv); - case ControlTypeByte: - return valueOrTuple(cv); - case ControlTypeInteger32: - return valueOrTuple(cv); - case ControlTypeInteger64: - return valueOrTuple(cv); - case ControlTypeFloat: - return valueOrTuple(cv); - case ControlTypeString: - return py::cast(cv.get()); - case ControlTypeRectangle: { - const Rectangle *v = reinterpret_cast(cv.data().data()); - return py::cast(v); - } - case ControlTypeSize: { - const Size *v = reinterpret_cast(cv.data().data()); - return py::cast(v); - } - case ControlTypeNone: - default: - throw std::runtime_error("Unsupported ControlValue type"); - } -} - -template -static ControlValue controlValueMaybeArray(const py::object &ob) -{ - if (py::isinstance(ob) || py::isinstance(ob)) { - std::vector vec = ob.cast>(); - return ControlValue(Span(vec)); - } - - return ControlValue(ob.cast()); -} - -static ControlValue pyToControlValue(const py::object &ob, ControlType type) -{ - switch (type) { - case ControlTypeBool: - return ControlValue(ob.cast()); - case ControlTypeByte: - return controlValueMaybeArray(ob); - case ControlTypeInteger32: - return controlValueMaybeArray(ob); - case ControlTypeInteger64: - return controlValueMaybeArray(ob); - case ControlTypeFloat: - return controlValueMaybeArray(ob); - case ControlTypeString: - return ControlValue(ob.cast()); - case ControlTypeRectangle: - return ControlValue(ob.cast()); - case ControlTypeSize: - return ControlValue(ob.cast()); - case ControlTypeNone: - default: - throw std::runtime_error("Control type not implemented"); - } -} - static std::weak_ptr gCameraManager; static int gEventfd; static std::mutex gReqlistMutex;