From patchwork Fri Apr 24 21:52:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 3522 Return-Path: Received: from relay3-d.mail.gandi.net (relay3-d.mail.gandi.net [217.70.183.195]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 92119603FD for ; Fri, 24 Apr 2020 23:50:07 +0200 (CEST) X-Originating-IP: 87.3.55.240 Received: from uno.homenet.telecomitalia.it (host240-55-dynamic.3-87-r.retail.telecomitalia.it [87.3.55.240]) (Authenticated sender: jacopo@jmondi.org) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id 9D33D60004; Fri, 24 Apr 2020 21:50:06 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Fri, 24 Apr 2020 23:52:55 +0200 Message-Id: <20200424215304.558317-5-jacopo@jmondi.org> X-Mailer: git-send-email 2.26.1 In-Reply-To: <20200424215304.558317-1-jacopo@jmondi.org> References: <20200424215304.558317-1-jacopo@jmondi.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 04/13] libcamera: v4l2_subdevice: Implement get selection 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-List-Received-Date: Fri, 24 Apr 2020 21:50:07 -0000 Implement get selection for the V4L2Subdevice class to support retrieving three targets: the subdevice native size, the analog crop and the active pixel area. Signed-off-by: Jacopo Mondi --- src/libcamera/include/v4l2_subdevice.h | 6 ++ src/libcamera/v4l2_subdevice.cpp | 92 ++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/src/libcamera/include/v4l2_subdevice.h b/src/libcamera/include/v4l2_subdevice.h index 4a04eadfb1f9..763ffadb61fb 100644 --- a/src/libcamera/include/v4l2_subdevice.h +++ b/src/libcamera/include/v4l2_subdevice.h @@ -46,6 +46,10 @@ public: const MediaEntity *entity() const { return entity_; } + int getNativeSize(unsigned int pad, Size *size); + int getActiveArea(unsigned int pad, Rectangle *rect); + int getCropRectangle(unsigned int pad, Rectangle *rect); + int setCrop(unsigned int pad, Rectangle *rect); int setCompose(unsigned int pad, Rectangle *rect); @@ -67,6 +71,8 @@ private: std::vector enumPadSizes(unsigned int pad, unsigned int code); + int getSelection(unsigned int pad, unsigned int target, + Rectangle *rect); int setSelection(unsigned int pad, unsigned int target, Rectangle *rect); diff --git a/src/libcamera/v4l2_subdevice.cpp b/src/libcamera/v4l2_subdevice.cpp index 5a479a96b795..4dcf8ce48754 100644 --- a/src/libcamera/v4l2_subdevice.cpp +++ b/src/libcamera/v4l2_subdevice.cpp @@ -133,6 +133,68 @@ int V4L2Subdevice::open() * \return The subdevice's associated media entity. */ +/** + * \brief Get the sub-device native size + * \param[in] pad The 0-indexed pad number the rectangle is to be applied to + * \param[out] size The sub-device native area size + * + * Retrieve the size of the sub-device native area. + * If the sub-device represent an image sensor, the native area describes + * the pixel array dimensions, including inactive and calibration pixels. + * + * \return 0 on success or a negative error code otherwise + * \retval -ENOTTY The native size information is not available + */ +int V4L2Subdevice::getNativeSize(unsigned int pad, Size *size) +{ + Rectangle rect = {}; + int ret = getSelection(pad, V4L2_SEL_TGT_NATIVE_SIZE, &rect); + if (ret) + return ret; + + size->width = rect.width; + size->height = rect.height; + + return 0; +} + +/** + * \brief Get the active area rectangle + * \param[in] pad The 0-indexed pad number the rectangle is to be applied to + * \param[out] rect The rectangle describing the sub-device active area + * + * Retrieve the rectangle describing the sub-device active area. + * If the sub-device represent an image sensor, the active area describes + * the pixel array active portion. + * + * The returned \a rect 'x' and 'y' fields represent the displacement from the + * native size rectangle top-left corner. + * + * \return 0 on success or a negative error code otherwise + * \retval -ENOTTY The active areas size information is not available + */ +int V4L2Subdevice::getActiveArea(unsigned int pad, Rectangle *rect) +{ + return getSelection(pad, V4L2_SEL_TGT_CROP_DEFAULT, rect); +} + +/** + * \brief Get the crop rectangle + * \param[in] pad The 0-indexed pad number the rectangle is to be applied to + * \param[out] rect The rectangle describing the cropped area rectangle + * + * Retrieve the rectangle representing the cropped area. If the sub-device + * represents an image sensor, the cropped area describes the pixel array + * portion from which the final image is produced from. + * + * \return 0 on success or a negative error code otherwise + * \retval -ENOTTY The crop rectangle size information is not available + */ +int V4L2Subdevice::getCropRectangle(unsigned int pad, Rectangle *rect) +{ + return getSelection(pad, V4L2_SEL_TGT_CROP, rect); +} + /** * \brief Set a crop rectangle on one of the V4L2 subdevice pads * \param[in] pad The 0-indexed pad number the rectangle is to be applied to @@ -343,6 +405,36 @@ std::vector V4L2Subdevice::enumPadSizes(unsigned int pad, return sizes; } +int V4L2Subdevice::getSelection(unsigned int pad, unsigned int target, + Rectangle *rect) +{ + struct v4l2_subdev_selection sel = {}; + + sel.which = V4L2_SUBDEV_FORMAT_ACTIVE; + sel.pad = pad; + sel.target = target; + sel.flags = 0; + + int ret = ioctl(VIDIOC_SUBDEV_G_SELECTION, &sel); + if (ret < 0 && ret != -ENOTTY) { + LOG(V4L2, Error) + << "Unable to get rectangle " << target << " on pad " + << pad << ": " << strerror(-ret); + return ret; + } + if (ret == -ENOTTY) { + LOG(V4L2, Debug) << "Selection API not available"; + return ret; + } + + rect->x = sel.r.left; + rect->y = sel.r.top; + rect->width = sel.r.width; + rect->height = sel.r.height; + + return 0; +} + int V4L2Subdevice::setSelection(unsigned int pad, unsigned int target, Rectangle *rect) {