summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHirokazu Honda <hiroh@chromium.org>2021-12-06 14:49:17 +0900
committerJacopo Mondi <jacopo@jmondi.org>2021-12-06 17:41:46 +0100
commite1b70e764fdeaaf423ec22776100be40e59bb0c3 (patch)
tree36d0d48bba2076b65efb27d7965373fe3958b2b0
parent5148c0aa7e5957628c4fe6b8b9bca9e923e608ce (diff)
libcamera: camera_sensor: Enable to set a test pattern mode
This adds a function to set a camera sensor driver a test pattern mode. CameraSensor initializes the test pattern mode by Off. Signed-off-by: Hirokazu Honda <hiroh@chromium.org> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>
-rw-r--r--include/libcamera/internal/camera_sensor.h11
-rw-r--r--src/libcamera/camera_sensor.cpp74
2 files changed, 73 insertions, 12 deletions
diff --git a/include/libcamera/internal/camera_sensor.h b/include/libcamera/internal/camera_sensor.h
index bb70ed9e..2fa11567 100644
--- a/include/libcamera/internal/camera_sensor.h
+++ b/include/libcamera/internal/camera_sensor.h
@@ -29,6 +29,8 @@ class BayerFormat;
class CameraLens;
class MediaEntity;
+struct CameraSensorProperties;
+
class CameraSensor : protected Loggable
{
public:
@@ -47,6 +49,7 @@ public:
{
return testPatternModes_;
}
+ int setTestPatternMode(controls::draft::TestPatternModeEnum mode);
V4L2SubdeviceFormat getFormat(const std::vector<unsigned int> &mbusCodes,
const Size &size) const;
@@ -75,15 +78,16 @@ private:
int validateSensorDriver();
void initVimcDefaultProperties();
void initStaticProperties();
- void initTestPatternModes(
- const std::map<controls::draft::TestPatternModeEnum, int32_t>
- &testPatternModeMap);
+ void initTestPatternModes();
int initProperties();
+ int applyTestPatternMode(controls::draft::TestPatternModeEnum mode);
const MediaEntity *entity_;
std::unique_ptr<V4L2Subdevice> subdev_;
unsigned int pad_;
+ const CameraSensorProperties *staticProps_;
+
std::string model_;
std::string id_;
@@ -91,6 +95,7 @@ private:
std::vector<unsigned int> mbusCodes_;
std::vector<Size> sizes_;
std::vector<controls::draft::TestPatternModeEnum> testPatternModes_;
+ controls::draft::TestPatternModeEnum testPatternMode_;
Size pixelArraySize_;
Rectangle activeArea_;
diff --git a/src/libcamera/camera_sensor.cpp b/src/libcamera/camera_sensor.cpp
index dc871abf..7bb39b1e 100644
--- a/src/libcamera/camera_sensor.cpp
+++ b/src/libcamera/camera_sensor.cpp
@@ -54,8 +54,8 @@ LOG_DEFINE_CATEGORY(CameraSensor)
* Once constructed the instance must be initialized with init().
*/
CameraSensor::CameraSensor(const MediaEntity *entity)
- : entity_(entity), pad_(UINT_MAX), bayerFormat_(nullptr),
- properties_(properties::properties)
+ : entity_(entity), pad_(UINT_MAX), staticProps_(nullptr),
+ bayerFormat_(nullptr), properties_(properties::properties)
{
}
@@ -161,7 +161,7 @@ int CameraSensor::init()
if (ret)
return ret;
- return 0;
+ return applyTestPatternMode(controls::draft::TestPatternModeEnum::TestPatternModeOff);
}
int CameraSensor::validateSensorDriver()
@@ -300,21 +300,30 @@ void CameraSensor::initVimcDefaultProperties()
void CameraSensor::initStaticProperties()
{
- const CameraSensorProperties *props = CameraSensorProperties::get(model_);
- if (!props)
+ staticProps_ = CameraSensorProperties::get(model_);
+ if (!staticProps_)
return;
/* Register the properties retrieved from the sensor database. */
- properties_.set(properties::UnitCellSize, props->unitCellSize);
+ properties_.set(properties::UnitCellSize, staticProps_->unitCellSize);
- initTestPatternModes(props->testPatternModes);
+ initTestPatternModes();
}
-void CameraSensor::initTestPatternModes(
- const std::map<controls::draft::TestPatternModeEnum, int32_t> &testPatternModes)
+void CameraSensor::initTestPatternModes()
{
const auto &v4l2TestPattern = controls().find(V4L2_CID_TEST_PATTERN);
if (v4l2TestPattern == controls().end()) {
+ LOG(CameraSensor, Debug) << "V4L2_CID_TEST_PATTERN is not supported";
+ return;
+ }
+
+ const auto &testPatternModes = staticProps_->testPatternModes;
+ if (testPatternModes.empty()) {
+ /*
+ * The camera sensor supports test patterns but we don't know
+ * how to map them so this should be fixed.
+ */
LOG(CameraSensor, Debug) << "No static test pattern map for \'"
<< model() << "\'";
return;
@@ -505,6 +514,53 @@ Size CameraSensor::resolution() const
*/
/**
+ * \brief Set the test pattern mode for the camera sensor
+ * \param[in] mode The test pattern mode
+ *
+ * The new \a mode is applied to the sensor if it differs from the active test
+ * pattern mode. Otherwise, this function is a no-op. Setting the same test
+ * pattern mode for every frame thus incurs no performance penalty.
+ */
+int CameraSensor::setTestPatternMode(controls::draft::TestPatternModeEnum mode)
+{
+ if (testPatternMode_ == mode)
+ return 0;
+
+ return applyTestPatternMode(mode);
+}
+
+int CameraSensor::applyTestPatternMode(controls::draft::TestPatternModeEnum mode)
+{
+ if (testPatternModes_.empty()) {
+ LOG(CameraSensor, Error)
+ << "Camera sensor does not support test pattern modes.";
+ return 0;
+ }
+
+ auto it = std::find(testPatternModes_.begin(), testPatternModes_.end(),
+ mode);
+ if (it == testPatternModes_.end()) {
+ LOG(CameraSensor, Error) << "Unsupported test pattern mode "
+ << mode;
+ return -EINVAL;
+ }
+
+ LOG(CameraSensor, Debug) << "Apply test pattern mode " << mode;
+
+ int32_t index = staticProps_->testPatternModes.at(mode);
+ ControlList ctrls{ controls() };
+ ctrls.set(V4L2_CID_TEST_PATTERN, index);
+
+ int ret = setControls(&ctrls);
+ if (ret)
+ return ret;
+
+ testPatternMode_ = mode;
+
+ return 0;
+}
+
+/**
* \brief Retrieve the best sensor format for a desired output
* \param[in] mbusCodes The list of acceptable media bus codes
* \param[in] size The desired size