From patchwork Fri Apr 9 04:32:05 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 11876 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 996D7BD16B for ; Fri, 9 Apr 2021 04:32:20 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id E854C687FE; Fri, 9 Apr 2021 06:32:19 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="WVE1s0Hk"; dkim-atps=neutral Received: from mail-pg1-x52c.google.com (mail-pg1-x52c.google.com [IPv6:2607:f8b0:4864:20::52c]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 65C5A6879F for ; Fri, 9 Apr 2021 06:32:18 +0200 (CEST) Received: by mail-pg1-x52c.google.com with SMTP id f29so2943970pgm.8 for ; Thu, 08 Apr 2021 21:32:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=VUGAIDjTkjuUhahNCh14V0cenxuZqjvdUJFw7LfwF1E=; b=WVE1s0Hk8a3Wdb6GKXvk7onpUWcf2W3qdAmGBanCrWMOsJEziuihgJtK2cFg/RnMY8 lIBWrojr151gtw6NtZWsC6mcKO/5Pg97vv5t1N5qnSDVzaQs/GO/1wDs01JbquRZ3fOC NvN4PDahvtydh1zRwLWX9aWAkcVCu/UOo9hhU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=VUGAIDjTkjuUhahNCh14V0cenxuZqjvdUJFw7LfwF1E=; b=DfL5Hr7jAkaqvVMlHetbd91ExqNhxbD9U5cz5RvIBgyBzml4WAt6ghW7A8XZdsG6QT 2lD21dQPzgJ4PS8Gq9+6u7TzPhYDIWRHiIcSaBm+9Hz2+9brXPAoBTwzxaZyqjl102my vrz9uapkU3oTKGU1AxBEBXnV+ha0SE18EhCPAOA06Hlxrsv5BWVjX0NhFJNaA2txytKy UdsmpOGVH34mnlKvhReFeHWWG0+sWgWPKhMlidTv5n+rXRe/uUOdJKWhxq4HrD5q39gs XYjrAPi8YulmYqszahx0qBPSEouq4AMstIAN2lDrDBkn6BOXgLuaGNxH/Lvbk0kQ8RRY JKhg== X-Gm-Message-State: AOAM532DftIZaKAaw5jLt4/1b8pQ/W5++xRtFn6l+5LKWdk3ZdngnCl+ 0mBqZCjL9Kt0wHJO/mGXALTEWf+4TO10aA== X-Google-Smtp-Source: ABdhPJwPuthi3o/kdXblhPm/LEkrQWGyJzUjY9XZnvD6kmSGOONZkZP2uG9mNgvW5pYUcLNZ9N+GzQ== X-Received: by 2002:a65:4383:: with SMTP id m3mr11334327pgp.57.1617942736863; Thu, 08 Apr 2021 21:32:16 -0700 (PDT) Received: from hiroh2.tok.corp.google.com ([2401:fa00:8f:2:7567:510e:1b47:ce92]) by smtp.gmail.com with ESMTPSA id il6sm737275pjb.56.2021.04.08.21.32.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Apr 2021 21:32:16 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Fri, 9 Apr 2021 13:32:05 +0900 Message-Id: <20210409043208.1823330-3-hiroh@chromium.org> X-Mailer: git-send-email 2.31.1.295.g9ea45b61b8-goog In-Reply-To: <20210409043208.1823330-1-hiroh@chromium.org> References: <20210409043208.1823330-1-hiroh@chromium.org> MIME-Version: 1.0 Subject: [libcamera-devel] [RFC PATCH 2/5] libcamera: V4L2Subdevice: Add getter/setter function for test pattern mode 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: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" The supported test pattern modes can be obtained by calling VIDIOC_QUERYMENU to a camera sensor device. This implements the getter and setter functions for the test pattern mode in V4L2SubDevice. Signed-off-by: Hirokazu Honda --- include/libcamera/internal/v4l2_subdevice.h | 5 + src/libcamera/v4l2_subdevice.cpp | 104 ++++++++++++++++++++ 2 files changed, 109 insertions(+) diff --git a/include/libcamera/internal/v4l2_subdevice.h b/include/libcamera/internal/v4l2_subdevice.h index d2b9ca55..f2f5d3f6 100644 --- a/include/libcamera/internal/v4l2_subdevice.h +++ b/include/libcamera/internal/v4l2_subdevice.h @@ -60,6 +60,9 @@ public: int setFormat(unsigned int pad, V4L2SubdeviceFormat *format, Whence whence = ActiveFormat); + std::vector getAvailableTestPatternModes(); + int setTestPatternMode(int32_t testPatternMode); + static std::unique_ptr fromEntityName(const MediaDevice *media, const std::string &entity); @@ -74,6 +77,8 @@ private: unsigned int code); const MediaEntity *entity_; + + std::map testPatternModeMap_; }; } /* namespace libcamera */ diff --git a/src/libcamera/v4l2_subdevice.cpp b/src/libcamera/v4l2_subdevice.cpp index 721ff5a9..130e9c4d 100644 --- a/src/libcamera/v4l2_subdevice.cpp +++ b/src/libcamera/v4l2_subdevice.cpp @@ -24,6 +24,7 @@ #include "libcamera/internal/media_object.h" #include "libcamera/internal/utils.h" +#include "android/metadata/system/camera_metadata_tags.h" /** * \file v4l2_subdevice.h * \brief V4L2 Subdevice API @@ -523,4 +524,107 @@ std::vector V4L2Subdevice::enumPadSizes(unsigned int pad, return sizes; } +/** + * \var V4L2Subdevice::testPatternModeMap_ + * \brief The map from controls::SensorTestPatternMode to an index value to be + * used for V4L2_CID_TEST_PATTERN. + * + * The map is constructed in getAvailableTestPatternModes() and referred in + * setTestPatternMode() to find a value associated with the requested test + * pattern mode. + */ +/** + * \fn V4L2Subdevice::getAvailableTestPatternModes() + * \brief Retrieve the available test pattern modes. + * + * \return The available control::SensorTestPatternMode values. + */ +std::vector V4L2Subdevice::getAvailableTestPatternModes() +{ + std::vector patternModes; + if (!testPatternModeMap_.empty()) { + for (const auto &mode : testPatternModeMap_) + patternModes.push_back(mode.first); + return patternModes; + } + + v4l2_queryctrl ctrl; + memset(&ctrl, 0, sizeof(ctrl)); + ctrl.id = V4L2_CID_TEST_PATTERN; + int ret = ioctl(VIDIOC_QUERYCTRL, &ctrl); + if (ret < 0) { + LOG(V4L2, Error) << "Unable to get test pattern mode :" + << strerror(-ret); + return {}; + } + + v4l2_querymenu menu; + memset(&menu, 0, sizeof(menu)); + menu.id = ctrl.id; + for (menu.index = ctrl.minimum; + static_cast(menu.index) <= ctrl.maximum; menu.index++) { + if (ioctl(VIDIOC_QUERYMENU, &menu) != 0) { + continue; + } + + const std::string modeName(reinterpret_cast(menu.name)); + std::optional androidTestPatternMode; + /* + * ov13858 and ov5670. + * No corresponding value for "Vertical Color Bar Type 3" and + * "Vertical Color Bar Type 4". + */ + if (modeName == "Disabled") { + androidTestPatternMode = + ANDROID_SENSOR_TEST_PATTERN_MODE_OFF; + } else if (modeName == "Vertical Color Bar Type 1") { + androidTestPatternMode = + ANDROID_SENSOR_TEST_PATTERN_MODE_COLOR_BARS; + } else if (modeName == "Vertical Color Bar Type 2") { + androidTestPatternMode = + ANDROID_SENSOR_TEST_PATTERN_MODE_COLOR_BARS_FADE_TO_GRAY; + } + + if (androidTestPatternMode) { + testPatternModeMap_[*androidTestPatternMode] = menu.index; + patternModes.push_back(*androidTestPatternMode); + } else { + LOG(V4L2, Debug) << "Skip test pattern mode: " + << modeName; + } + } + + return patternModes; +} + +/** + * \fn V4L2Subdevice::getAvailableTestPatternModes() + * \brief Set the test pattern mode. + * + * \return 0 on success or a negative error code otherwise. + */ +int V4L2Subdevice::setTestPatternMode(int32_t testPatternMode) +{ + auto it = testPatternModeMap_.find(testPatternMode); + if (it != testPatternModeMap_.end()) { + LOG(V4L2, Error) << "Unsupported test pattern mode: " + << testPatternMode; + + return -EINVAL; + } + + v4l2_control ctrl; + memset(&ctrl, 0, sizeof(ctrl)); + ctrl.id = V4L2_CID_TEST_PATTERN; + ctrl.value = it->second; + int ret = ioctl(VIDIOC_S_CTRL, &ctrl); + if (ret < 0) { + LOG(V4L2, Error) << "Unable to set test pattern mode: " + << strerror(-ret); + + return -EINVAL; + } + + return 0; +} } /* namespace libcamera */