Message ID | 20201215004811.602429-2-niklas.soderlund@ragnatech.se |
---|---|
State | Superseded |
Delegated to: | Niklas Söderlund |
Headers | show |
Series |
|
Related | show |
Hi Niklas, Thank you for your patch. On Tue, 15 Dec 2020 at 00:48, Niklas Söderlund < niklas.soderlund@ragnatech.se> wrote: > Some sensor controls take effect with a delay as the sensor needs time > to adjust, for example exposure. Add an optional helper DelayedControls > to help pipelines deal with such controls. > > The idea is to provide a queue of controls towards the V4L2 device and > apply individual controls with the specified delay with the aim to get > predictable and retrievable control values for any given frame. To do > this the queue of controls needs to be at least as deep as the control > with the largest delay. > > The DelayedControls needs to be informed of every start of exposure. > This can be emulated but the helper is designed to be used with this > event being provide by the kernel thru V4L2 events. > > This helper is based on StaggeredCtrl from the Raspberry Pi pipeline > handler but expands on its API. This helpers aims to replace the > Raspberry Pi implementations and mimics it behavior perfectly. > > Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> > --- > * Changes since v2 > - Drop optional argument to reset(). > - Update commit message. > - Remove usage of Mutex. > - Rename frameStart() to applyControls)_. > - Rename ControlInfo to Into. > - Rename ControlArray to ControlRingBuffer. > - Drop ControlsDelays and ControlsValues. > - Sort headers. > - Rename iterators. > - Simplify queueCount_ handeling in reset(). > - Add more warnings. > - Update documentation. > > * Changes since v2 > - Improve error logic in queue() as suggested by Jean-Michel Hautbois. > - s/fistSequence_/firstSequence_/ > > * Changes since v1 > - Correct copyright to reflect work is derived from Raspberry Pi > pipeline handler. This was always the intention and was wrong in v1. > - Rewrite large parts of the documentation. > - Join two loops to one in DelayedControls::DelayedControls() > --- > include/libcamera/internal/delayed_controls.h | 82 ++++++ > src/libcamera/delayed_controls.cpp | 252 ++++++++++++++++++ > src/libcamera/meson.build | 1 + > 3 files changed, 335 insertions(+) > create mode 100644 include/libcamera/internal/delayed_controls.h > create mode 100644 src/libcamera/delayed_controls.cpp > > diff --git a/include/libcamera/internal/delayed_controls.h > b/include/libcamera/internal/delayed_controls.h > new file mode 100644 > index 0000000000000000..1292b484ec9f53e9 > --- /dev/null > +++ b/include/libcamera/internal/delayed_controls.h > @@ -0,0 +1,82 @@ > +/* SPDX-License-Identifier: LGPL-2.1-or-later */ > +/* > + * Copyright (C) 2020, Raspberry Pi (Trading) Ltd. > + * > + * delayed_controls.h - Helper to deal with controls that are applied > with a delay > + */ > +#ifndef __LIBCAMERA_INTERNAL_DELAYED_CONTROLS_H__ > +#define __LIBCAMERA_INTERNAL_DELAYED_CONTROLS_H__ > + > +#include <stdint.h> > +#include <unordered_map> > + > +#include <libcamera/controls.h> > + > +namespace libcamera { > + > +class V4L2Device; > + > +class DelayedControls > +{ > +public: > + DelayedControls(V4L2Device *device, > + const std::unordered_map<uint32_t, unsigned int> > &delays); > + > + void reset(); > + > + bool push(const ControlList &controls); > + ControlList get(uint32_t sequence); > + > + void applyControls(uint32_t sequence); > + > +private: > + class Info > + { > + public: > + Info() > + : updated(false) > + { > + } > + > + Info(const ControlValue &v) > + : value(v), updated(true) > + { > + } > + > + ControlValue value; > + bool updated; > + }; > + > + /* \todo: Make the listSize configurable at instance creation > time. */ > + static constexpr int listSize = 16; > + class ControlRingBuffer : public std::array<Info, listSize> > + { > + public: > + Info &operator[](unsigned int index) > + { > + return std::array<Info, > listSize>::operator[](index % listSize); > + } > + > + const Info &operator[](unsigned int index) const > + { > + return std::array<Info, > listSize>::operator[](index % listSize); > + } > + }; > + > + bool queue(const ControlList &controls); > + > + V4L2Device *device_; > + std::unordered_map<const ControlId *, unsigned int> delays_; > + unsigned int maxDelay_; > + > + bool running_; > + uint32_t firstSequence_; > + > + uint32_t queueCount_; > + uint32_t writeCount_; > + std::unordered_map<const ControlId *, ControlRingBuffer> values_; > +}; > StaggeredCtrl has a mutex to protect access to state. I wasn't entirely sure that was needed. Presumably the SoF event calling applyControls() and all other public method access occur in the same thread context, so no protection is needed? Do you foresee this ever changing? + > +} /* namespace libcamera */ > + > +#endif /* __LIBCAMERA_INTERNAL_DELAYED_CONTROLS_H__ */ > diff --git a/src/libcamera/delayed_controls.cpp > b/src/libcamera/delayed_controls.cpp > new file mode 100644 > index 0000000000000000..db2e51f8c93c4755 > --- /dev/null > +++ b/src/libcamera/delayed_controls.cpp > @@ -0,0 +1,252 @@ > +/* SPDX-License-Identifier: LGPL-2.1-or-later */ > +/* > + * Copyright (C) 2020, Raspberry Pi (Trading) Ltd. > + * > + * delayed_controls.h - Helper to deal with controls that are applied > with a delay > + */ > + > +#include "libcamera/internal/delayed_controls.h" > + > +#include <libcamera/controls.h> > + > +#include "libcamera/internal/log.h" > +#include "libcamera/internal/v4l2_device.h" > + > +/** > + * \file delayed_controls.h > + * \brief Helper to deal with controls that are applied with a delay > + */ > + > +namespace libcamera { > + > +LOG_DEFINE_CATEGORY(DelayedControls) > + > +/** > + * \class DelayedControls > + * \brief Helper to deal with controls that take effect with a delay > + * > + * Some sensor controls take effect with a delay as the sensor needs time > to > + * adjust, for example exposure and focus. This is a helper class to deal > with > + * such controls and the intended users are pipeline handlers. > + * > + * The idea is to extend the concept of the buffer depth of a pipeline the > + * application needs to maintain to also cover controls. Just as with > buffer > + * depth if the application keeps the number of requests queued above the > + * control depth the controls are guaranteed to take effect for the > correct > + * request. The control depth is determined by the control with the > greatest > + * delay. > + */ > + > +/** > + * \brief Construct a DelayedControls instance > + * \param[in] device The V4L2 device the controls have to be applied to > + * \param[in] delays Map of the numerical V4L2 control ids to their > associated > + * delays (in frames) > + * > + * Only controls specified in \a delays are handled. If it's desired to > mix > + * delayed controls and controls that take effect immediately the > immediate > + * controls must be listed in the \a delays map with a delay value of 0. > + */ > +DelayedControls::DelayedControls(V4L2Device *device, > + const std::unordered_map<uint32_t, > unsigned int> &delays) > + : device_(device), maxDelay_(0) > +{ > + const ControlInfoMap &controls = device_->controls(); > + > + /* > + * Create a map of control ids to delays for controls exposed by > the > + * device. > + */ > + for (auto const &delay : delays) { > + auto it = controls.find(delay.first); > + if (it == controls.end()) { > + LOG(DelayedControls, Error) > + << "Delay request for control id " > + << utils::hex(delay.first) > + << " but control is not exposed by device " > + << device_->deviceNode(); > + continue; > + } > + > + const ControlId *id = it->first; > + > + delays_[id] = delay.second; > + > + LOG(DelayedControls, Debug) > + << "Set a delay of " << delays_[id] > + << " for " << id->name(); > + > + maxDelay_ = std::max(maxDelay_, delays_[id]); > + } > + > + reset(); > +} > + > +/** > + * \brief Reset state machine > + * > + * Resets the state machine to a starting position based on control values > + * retrieved from the device. > + */ > +void DelayedControls::reset() > +{ > + running_ = false; > + firstSequence_ = 0; > + queueCount_ = 1; > + writeCount_ = 0; > + > + /* Retrieve control as reported by the device. */ > + std::vector<uint32_t> ids; > + for (auto const &delay : delays_) > + ids.push_back(delay.first->id()); > + > + ControlList controls = device_->getControls(ids); > + > + /* Seed the control queue with the controls reported by the > device. */ > + values_.clear(); > + for (const auto &ctrl : controls) { > + const ControlId *id = > device_->controls().idmap().at(ctrl.first); > + values_[id][0] = Info(ctrl.second); > + } > I think this may have been mentioned in one of the earlier rounds - this may not be correct as all the controls available to the device may not be registered with DelayedControls. In those cases, the values_ map lookup would fail. You would need to validate that id is an available key in the map. > +} > + > +/** > + * \brief Push a set of controls on the queue > + * \param[in] controls List of controls to add to the device queue > + * > + * Push a set of controls to the control queue. This increases the > control queue > + * depth by one. > + * > + * \returns true if \a controls are accepted, or false otherwise > + */ > +bool DelayedControls::push(const ControlList &controls) > +{ > + return queue(controls); > +} > Apologies, I may have missed something obvious, but why do we have the push() and queue() methods when one calls the other? > + > +bool DelayedControls::queue(const ControlList &controls) > +{ > + /* Copy state from previous frame. */ > + for (auto &ctrl : values_) { > + Info &info = ctrl.second[queueCount_]; > + info.value = values_[ctrl.first][queueCount_ - 1].value; > + info.updated = false; > + } > + > + /* Update with new controls. */ > + const ControlIdMap &idmap = device_->controls().idmap(); > + for (const auto &control : controls) { > + const auto &it = idmap.find(control.first); > + if (it == idmap.end()) { > + LOG(DelayedControls, Warning) > + << "Unknown control " << control.first; > + return false; > + } > + > + const ControlId *id = it->second; > + > + if (delays_.find(id) == delays_.end()) > + return false; > + > + Info &info = values_[id][queueCount_]; > + > + info.value = control.second; > + info.updated = true; > + > + LOG(DelayedControls, Debug) > + << "Queuing " << id->name() > + << " to " << info.value.toString() > + << " at index " << queueCount_; > + } > + > + queueCount_++; > + > + return true; > +} > + > +/** > + * \brief Read back controls in effect at a sequence number > + * \param[in] sequence The sequence number to get controls for > + * > + * Read back what controls where in effect at a specific sequence number. > The > + * history is a ring buffer of 16 entries where new and old values > coexist. It's > + * the callers responsibility to not read too old sequence numbers that > have been > + * pushed out of the history. > + * > + * Historic values are evicted by pushing new values onto the queue using > + * push(). The max history from the current sequence number that yields > valid > + * values are thus 16 minus number of controls pushed. > + * > + * \return The controls at \a sequence number > + */ > +ControlList DelayedControls::get(uint32_t sequence) > +{ > + uint32_t adjustedSeq = sequence - firstSequence_ + 1; > + unsigned int index = std::max<int>(0, adjustedSeq - maxDelay_); > + > + ControlList out(device_->controls()); > + for (const auto &ctrl : values_) { > + const ControlId *id = ctrl.first; > + const Info &info = ctrl.second[index]; > + > + out.set(id->id(), info.value); > + > + LOG(DelayedControls, Debug) > + << "Reading " << id->name() > + << " to " << info.value.toString() > + << " at index " << index; > + } > + > + return out; > +} > + > +/** > + * \brief Inform DelayedControls of the start of a new frame > + * \param[in] sequence Sequence number of the frame that started > + * > + * Inform the state machine that a new frame has started and of its > sequence > + * number. Any user of these helpers is responsible to inform the helper > about > + * the start of any frame.This can be connected with ease to the start of > a > + * exposure (SOE) V4L2 event. > + */ > +void DelayedControls::applyControls(uint32_t sequence) > +{ > + LOG(DelayedControls, Debug) << "frame " << sequence << " started"; > + > + if (!running_) { > + firstSequence_ = sequence; > + running_ = true; > + } > + > + /* > + * Create control list peaking ahead in the value queue to ensure > + * values are set in time to satisfy the sensor delay. > + */ > + ControlList out(device_->controls()); > + for (const auto &ctrl : values_) { > + const ControlId *id = ctrl.first; > + unsigned int delayDiff = maxDelay_ - delays_[id]; > + unsigned int index = std::max<int>(0, writeCount_ - > delayDiff); > + const Info &info = ctrl.second[index]; > + > + if (info.updated) { > + out.set(id->id(), info.value); > + LOG(DelayedControls, Debug) > + << "Setting " << id->name() > + << " to " << info.value.toString() > + << " at index " << index; > + } > + } > + > + writeCount_++; > + > + while (writeCount_ >= queueCount_) { > + LOG(DelayedControls, Debug) > + << "Queue is empty, auto queue no-op."; > + queue({}); > + } > + > + device_->setControls(&out); > As mentioned previously, this will need to separate out controls that must be written immediately (e.g. VBLANK) instead of batched together, but that can be added at a later stage. Regards, Naush > +} > + > +} /* namespace libcamera */ > diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build > index 387d5d88ecae11ad..5a4bf0d7ba4fd231 100644 > --- a/src/libcamera/meson.build > +++ b/src/libcamera/meson.build > @@ -12,6 +12,7 @@ libcamera_sources = files([ > 'controls.cpp', > 'control_serializer.cpp', > 'control_validator.cpp', > + 'delayed_controls.cpp', > 'device_enumerator.cpp', > 'device_enumerator_sysfs.cpp', > 'event_dispatcher.cpp', > -- > 2.29.2 > >
Hi Naush, On Tue, Dec 15, 2020 at 01:53:12PM +0000, Naushir Patuck wrote: > On Tue, 15 Dec 2020 at 00:48, Niklas Söderlund wrote: > > > Some sensor controls take effect with a delay as the sensor needs time > > to adjust, for example exposure. Add an optional helper DelayedControls > > to help pipelines deal with such controls. > > > > The idea is to provide a queue of controls towards the V4L2 device and > > apply individual controls with the specified delay with the aim to get > > predictable and retrievable control values for any given frame. To do > > this the queue of controls needs to be at least as deep as the control > > with the largest delay. > > > > The DelayedControls needs to be informed of every start of exposure. > > This can be emulated but the helper is designed to be used with this > > event being provide by the kernel thru V4L2 events. > > > > This helper is based on StaggeredCtrl from the Raspberry Pi pipeline > > handler but expands on its API. This helpers aims to replace the > > Raspberry Pi implementations and mimics it behavior perfectly. > > > > Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> > > --- > > * Changes since v2 > > - Drop optional argument to reset(). > > - Update commit message. > > - Remove usage of Mutex. > > - Rename frameStart() to applyControls)_. > > - Rename ControlInfo to Into. > > - Rename ControlArray to ControlRingBuffer. > > - Drop ControlsDelays and ControlsValues. > > - Sort headers. > > - Rename iterators. > > - Simplify queueCount_ handeling in reset(). > > - Add more warnings. > > - Update documentation. > > > > * Changes since v2 > > - Improve error logic in queue() as suggested by Jean-Michel Hautbois. > > - s/fistSequence_/firstSequence_/ > > > > * Changes since v1 > > - Correct copyright to reflect work is derived from Raspberry Pi > > pipeline handler. This was always the intention and was wrong in v1. > > - Rewrite large parts of the documentation. > > - Join two loops to one in DelayedControls::DelayedControls() > > --- > > include/libcamera/internal/delayed_controls.h | 82 ++++++ > > src/libcamera/delayed_controls.cpp | 252 ++++++++++++++++++ > > src/libcamera/meson.build | 1 + > > 3 files changed, 335 insertions(+) > > create mode 100644 include/libcamera/internal/delayed_controls.h > > create mode 100644 src/libcamera/delayed_controls.cpp > > > > diff --git a/include/libcamera/internal/delayed_controls.h > > b/include/libcamera/internal/delayed_controls.h > > new file mode 100644 > > index 0000000000000000..1292b484ec9f53e9 > > --- /dev/null > > +++ b/include/libcamera/internal/delayed_controls.h > > @@ -0,0 +1,82 @@ > > +/* SPDX-License-Identifier: LGPL-2.1-or-later */ > > +/* > > + * Copyright (C) 2020, Raspberry Pi (Trading) Ltd. > > + * > > + * delayed_controls.h - Helper to deal with controls that are applied with a delay > > + */ > > +#ifndef __LIBCAMERA_INTERNAL_DELAYED_CONTROLS_H__ > > +#define __LIBCAMERA_INTERNAL_DELAYED_CONTROLS_H__ > > + > > +#include <stdint.h> > > +#include <unordered_map> > > + > > +#include <libcamera/controls.h> > > + > > +namespace libcamera { > > + > > +class V4L2Device; > > + > > +class DelayedControls > > +{ > > +public: > > + DelayedControls(V4L2Device *device, > > + const std::unordered_map<uint32_t, unsigned int> &delays); > > + > > + void reset(); > > + > > + bool push(const ControlList &controls); > > + ControlList get(uint32_t sequence); > > + > > + void applyControls(uint32_t sequence); > > + > > +private: > > + class Info > > + { > > + public: > > + Info() > > + : updated(false) > > + { > > + } > > + > > + Info(const ControlValue &v) > > + : value(v), updated(true) > > + { > > + } > > + > > + ControlValue value; > > + bool updated; > > + }; > > + > > + /* \todo: Make the listSize configurable at instance creation time. */ > > + static constexpr int listSize = 16; > > + class ControlRingBuffer : public std::array<Info, listSize> > > + { > > + public: > > + Info &operator[](unsigned int index) > > + { > > + return std::array<Info, listSize>::operator[](index % listSize); > > + } > > + > > + const Info &operator[](unsigned int index) const > > + { > > + return std::array<Info, listSize>::operator[](index % listSize); > > + } > > + }; > > + > > + bool queue(const ControlList &controls); > > + > > + V4L2Device *device_; > > + std::unordered_map<const ControlId *, unsigned int> delays_; > > + unsigned int maxDelay_; > > + > > + bool running_; > > + uint32_t firstSequence_; > > + > > + uint32_t queueCount_; > > + uint32_t writeCount_; > > + std::unordered_map<const ControlId *, ControlRingBuffer> values_; > > +}; > > StaggeredCtrl has a mutex to protect access to state. I wasn't entirely > sure that was needed. Presumably the SoF event calling applyControls() and > all other public method access occur in the same thread context, so no > protection is needed? Do you foresee this ever changing? Once the camera is started, the pipeline handler is event-based, with all events generated in the same thread (before the camera gets started, the configure call runs in the application thread). Right now we have a single thread that covers all pipeline handler instances, and we may move to one thread per pipeline handler in the future (or possibly a thread pool of N threads for M pipeline handlers, with N < M), but not to a model where different events would be handled in different threads. The mutex is thus unnecessary. Niklas had one in v3, and I've asked him to drop it. I'll let Niklas reply to the other questions below. > > +} /* namespace libcamera */ > > + > > +#endif /* __LIBCAMERA_INTERNAL_DELAYED_CONTROLS_H__ */ > > diff --git a/src/libcamera/delayed_controls.cpp > > b/src/libcamera/delayed_controls.cpp > > new file mode 100644 > > index 0000000000000000..db2e51f8c93c4755 > > --- /dev/null > > +++ b/src/libcamera/delayed_controls.cpp > > @@ -0,0 +1,252 @@ > > +/* SPDX-License-Identifier: LGPL-2.1-or-later */ > > +/* > > + * Copyright (C) 2020, Raspberry Pi (Trading) Ltd. > > + * > > + * delayed_controls.h - Helper to deal with controls that are applied with a delay > > + */ > > + > > +#include "libcamera/internal/delayed_controls.h" > > + > > +#include <libcamera/controls.h> > > + > > +#include "libcamera/internal/log.h" > > +#include "libcamera/internal/v4l2_device.h" > > + > > +/** > > + * \file delayed_controls.h > > + * \brief Helper to deal with controls that are applied with a delay > > + */ > > + > > +namespace libcamera { > > + > > +LOG_DEFINE_CATEGORY(DelayedControls) > > + > > +/** > > + * \class DelayedControls > > + * \brief Helper to deal with controls that take effect with a delay > > + * > > + * Some sensor controls take effect with a delay as the sensor needs time to > > + * adjust, for example exposure and focus. This is a helper class to deal with > > + * such controls and the intended users are pipeline handlers. > > + * > > + * The idea is to extend the concept of the buffer depth of a pipeline the > > + * application needs to maintain to also cover controls. Just as with buffer > > + * depth if the application keeps the number of requests queued above the > > + * control depth the controls are guaranteed to take effect for the correct > > + * request. The control depth is determined by the control with the greatest > > + * delay. > > + */ > > + > > +/** > > + * \brief Construct a DelayedControls instance > > + * \param[in] device The V4L2 device the controls have to be applied to > > + * \param[in] delays Map of the numerical V4L2 control ids to their associated > > + * delays (in frames) > > + * > > + * Only controls specified in \a delays are handled. If it's desired to mix > > + * delayed controls and controls that take effect immediately the immediate > > + * controls must be listed in the \a delays map with a delay value of 0. > > + */ > > +DelayedControls::DelayedControls(V4L2Device *device, > > + const std::unordered_map<uint32_t, unsigned int> &delays) > > + : device_(device), maxDelay_(0) > > +{ > > + const ControlInfoMap &controls = device_->controls(); > > + > > + /* > > + * Create a map of control ids to delays for controls exposed by the > > + * device. > > + */ > > + for (auto const &delay : delays) { > > + auto it = controls.find(delay.first); > > + if (it == controls.end()) { > > + LOG(DelayedControls, Error) > > + << "Delay request for control id " > > + << utils::hex(delay.first) > > + << " but control is not exposed by device " > > + << device_->deviceNode(); > > + continue; > > + } > > + > > + const ControlId *id = it->first; > > + > > + delays_[id] = delay.second; > > + > > + LOG(DelayedControls, Debug) > > + << "Set a delay of " << delays_[id] > > + << " for " << id->name(); > > + > > + maxDelay_ = std::max(maxDelay_, delays_[id]); > > + } > > + > > + reset(); > > +} > > + > > +/** > > + * \brief Reset state machine > > + * > > + * Resets the state machine to a starting position based on control values > > + * retrieved from the device. > > + */ > > +void DelayedControls::reset() > > +{ > > + running_ = false; > > + firstSequence_ = 0; > > + queueCount_ = 1; > > + writeCount_ = 0; > > + > > + /* Retrieve control as reported by the device. */ > > + std::vector<uint32_t> ids; > > + for (auto const &delay : delays_) > > + ids.push_back(delay.first->id()); > > + > > + ControlList controls = device_->getControls(ids); > > + > > + /* Seed the control queue with the controls reported by the device. */ > > + values_.clear(); > > + for (const auto &ctrl : controls) { > > + const ControlId *id = device_->controls().idmap().at(ctrl.first); > > + values_[id][0] = Info(ctrl.second); > > + } > > I think this may have been mentioned in one of the earlier rounds - this > may not be correct as all the controls available to the device may not be > registered with DelayedControls. In those cases, the values_ map lookup > would fail. You would need to validate that id is an available key in the > map. > > > +} > > + > > +/** > > + * \brief Push a set of controls on the queue > > + * \param[in] controls List of controls to add to the device queue > > + * > > + * Push a set of controls to the control queue. This increases the control queue > > + * depth by one. > > + * > > + * \returns true if \a controls are accepted, or false otherwise > > + */ > > +bool DelayedControls::push(const ControlList &controls) > > +{ > > + return queue(controls); > > +} > > Apologies, I may have missed something obvious, but why do we have the > push() and queue() methods when one calls the other? > > > + > > +bool DelayedControls::queue(const ControlList &controls) > > +{ > > + /* Copy state from previous frame. */ > > + for (auto &ctrl : values_) { > > + Info &info = ctrl.second[queueCount_]; > > + info.value = values_[ctrl.first][queueCount_ - 1].value; > > + info.updated = false; > > + } > > + > > + /* Update with new controls. */ > > + const ControlIdMap &idmap = device_->controls().idmap(); > > + for (const auto &control : controls) { > > + const auto &it = idmap.find(control.first); > > + if (it == idmap.end()) { > > + LOG(DelayedControls, Warning) > > + << "Unknown control " << control.first; > > + return false; > > + } > > + > > + const ControlId *id = it->second; > > + > > + if (delays_.find(id) == delays_.end()) > > + return false; > > + > > + Info &info = values_[id][queueCount_]; > > + > > + info.value = control.second; > > + info.updated = true; > > + > > + LOG(DelayedControls, Debug) > > + << "Queuing " << id->name() > > + << " to " << info.value.toString() > > + << " at index " << queueCount_; > > + } > > + > > + queueCount_++; > > + > > + return true; > > +} > > + > > +/** > > + * \brief Read back controls in effect at a sequence number > > + * \param[in] sequence The sequence number to get controls for > > + * > > + * Read back what controls where in effect at a specific sequence number. The > > + * history is a ring buffer of 16 entries where new and old values coexist. It's > > + * the callers responsibility to not read too old sequence numbers that have been > > + * pushed out of the history. > > + * > > + * Historic values are evicted by pushing new values onto the queue using > > + * push(). The max history from the current sequence number that yields valid > > + * values are thus 16 minus number of controls pushed. > > + * > > + * \return The controls at \a sequence number > > + */ > > +ControlList DelayedControls::get(uint32_t sequence) > > +{ > > + uint32_t adjustedSeq = sequence - firstSequence_ + 1; > > + unsigned int index = std::max<int>(0, adjustedSeq - maxDelay_); > > + > > + ControlList out(device_->controls()); > > + for (const auto &ctrl : values_) { > > + const ControlId *id = ctrl.first; > > + const Info &info = ctrl.second[index]; > > + > > + out.set(id->id(), info.value); > > + > > + LOG(DelayedControls, Debug) > > + << "Reading " << id->name() > > + << " to " << info.value.toString() > > + << " at index " << index; > > + } > > + > > + return out; > > +} > > + > > +/** > > + * \brief Inform DelayedControls of the start of a new frame > > + * \param[in] sequence Sequence number of the frame that started > > + * > > + * Inform the state machine that a new frame has started and of its sequence > > + * number. Any user of these helpers is responsible to inform the helper about > > + * the start of any frame.This can be connected with ease to the start of a > > + * exposure (SOE) V4L2 event. > > + */ > > +void DelayedControls::applyControls(uint32_t sequence) > > +{ > > + LOG(DelayedControls, Debug) << "frame " << sequence << " started"; > > + > > + if (!running_) { > > + firstSequence_ = sequence; > > + running_ = true; > > + } > > + > > + /* > > + * Create control list peaking ahead in the value queue to ensure > > + * values are set in time to satisfy the sensor delay. > > + */ > > + ControlList out(device_->controls()); > > + for (const auto &ctrl : values_) { > > + const ControlId *id = ctrl.first; > > + unsigned int delayDiff = maxDelay_ - delays_[id]; > > + unsigned int index = std::max<int>(0, writeCount_ - delayDiff); > > + const Info &info = ctrl.second[index]; > > + > > + if (info.updated) { > > + out.set(id->id(), info.value); > > + LOG(DelayedControls, Debug) > > + << "Setting " << id->name() > > + << " to " << info.value.toString() > > + << " at index " << index; > > + } > > + } > > + > > + writeCount_++; > > + > > + while (writeCount_ >= queueCount_) { > > + LOG(DelayedControls, Debug) > > + << "Queue is empty, auto queue no-op."; > > + queue({}); > > + } > > + > > + device_->setControls(&out); > > As mentioned previously, this will need to separate out controls that must > be written immediately (e.g. VBLANK) instead of batched together, but that > can be added at a later stage. > > > +} > > + > > +} /* namespace libcamera */ > > diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build > > index 387d5d88ecae11ad..5a4bf0d7ba4fd231 100644 > > --- a/src/libcamera/meson.build > > +++ b/src/libcamera/meson.build > > @@ -12,6 +12,7 @@ libcamera_sources = files([ > > 'controls.cpp', > > 'control_serializer.cpp', > > 'control_validator.cpp', > > + 'delayed_controls.cpp', > > 'device_enumerator.cpp', > > 'device_enumerator_sysfs.cpp', > > 'event_dispatcher.cpp',
Hi Niklas, On Tue, Dec 15, 2020 at 01:48:04AM +0100, Niklas Söderlund wrote: > Some sensor controls take effect with a delay as the sensor needs time > to adjust, for example exposure. Add an optional helper DelayedControls > to help pipelines deal with such controls. > > The idea is to provide a queue of controls towards the V4L2 device and > apply individual controls with the specified delay with the aim to get > predictable and retrievable control values for any given frame. To do > this the queue of controls needs to be at least as deep as the control > with the largest delay. > > The DelayedControls needs to be informed of every start of exposure. > This can be emulated but the helper is designed to be used with this > event being provide by the kernel thru V4L2 events. > > This helper is based on StaggeredCtrl from the Raspberry Pi pipeline > handler but expands on its API. This helpers aims to replace the > Raspberry Pi implementations and mimics it behavior perfectly. > > Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> I guess comments on v3 not addressed were deemed not requested for this iteration: Reviewed-by: Jacopo Mondi <jacopo@jmondi.org> Thanks j > --- > * Changes since v2 > - Drop optional argument to reset(). > - Update commit message. > - Remove usage of Mutex. > - Rename frameStart() to applyControls)_. > - Rename ControlInfo to Into. > - Rename ControlArray to ControlRingBuffer. > - Drop ControlsDelays and ControlsValues. > - Sort headers. > - Rename iterators. > - Simplify queueCount_ handeling in reset(). > - Add more warnings. > - Update documentation. > > * Changes since v2 > - Improve error logic in queue() as suggested by Jean-Michel Hautbois. > - s/fistSequence_/firstSequence_/ > > * Changes since v1 > - Correct copyright to reflect work is derived from Raspberry Pi > pipeline handler. This was always the intention and was wrong in v1. > - Rewrite large parts of the documentation. > - Join two loops to one in DelayedControls::DelayedControls() > --- > include/libcamera/internal/delayed_controls.h | 82 ++++++ > src/libcamera/delayed_controls.cpp | 252 ++++++++++++++++++ > src/libcamera/meson.build | 1 + > 3 files changed, 335 insertions(+) > create mode 100644 include/libcamera/internal/delayed_controls.h > create mode 100644 src/libcamera/delayed_controls.cpp > > diff --git a/include/libcamera/internal/delayed_controls.h b/include/libcamera/internal/delayed_controls.h > new file mode 100644 > index 0000000000000000..1292b484ec9f53e9 > --- /dev/null > +++ b/include/libcamera/internal/delayed_controls.h > @@ -0,0 +1,82 @@ > +/* SPDX-License-Identifier: LGPL-2.1-or-later */ > +/* > + * Copyright (C) 2020, Raspberry Pi (Trading) Ltd. > + * > + * delayed_controls.h - Helper to deal with controls that are applied with a delay > + */ > +#ifndef __LIBCAMERA_INTERNAL_DELAYED_CONTROLS_H__ > +#define __LIBCAMERA_INTERNAL_DELAYED_CONTROLS_H__ > + > +#include <stdint.h> > +#include <unordered_map> > + > +#include <libcamera/controls.h> > + > +namespace libcamera { > + > +class V4L2Device; > + > +class DelayedControls > +{ > +public: > + DelayedControls(V4L2Device *device, > + const std::unordered_map<uint32_t, unsigned int> &delays); > + > + void reset(); > + > + bool push(const ControlList &controls); > + ControlList get(uint32_t sequence); > + > + void applyControls(uint32_t sequence); > + > +private: > + class Info > + { > + public: > + Info() > + : updated(false) > + { > + } > + > + Info(const ControlValue &v) > + : value(v), updated(true) > + { > + } > + > + ControlValue value; > + bool updated; > + }; > + > + /* \todo: Make the listSize configurable at instance creation time. */ > + static constexpr int listSize = 16; > + class ControlRingBuffer : public std::array<Info, listSize> > + { > + public: > + Info &operator[](unsigned int index) > + { > + return std::array<Info, listSize>::operator[](index % listSize); > + } > + > + const Info &operator[](unsigned int index) const > + { > + return std::array<Info, listSize>::operator[](index % listSize); > + } > + }; > + > + bool queue(const ControlList &controls); > + > + V4L2Device *device_; > + std::unordered_map<const ControlId *, unsigned int> delays_; > + unsigned int maxDelay_; > + > + bool running_; > + uint32_t firstSequence_; > + > + uint32_t queueCount_; > + uint32_t writeCount_; > + std::unordered_map<const ControlId *, ControlRingBuffer> values_; > +}; > + > +} /* namespace libcamera */ > + > +#endif /* __LIBCAMERA_INTERNAL_DELAYED_CONTROLS_H__ */ > diff --git a/src/libcamera/delayed_controls.cpp b/src/libcamera/delayed_controls.cpp > new file mode 100644 > index 0000000000000000..db2e51f8c93c4755 > --- /dev/null > +++ b/src/libcamera/delayed_controls.cpp > @@ -0,0 +1,252 @@ > +/* SPDX-License-Identifier: LGPL-2.1-or-later */ > +/* > + * Copyright (C) 2020, Raspberry Pi (Trading) Ltd. > + * > + * delayed_controls.h - Helper to deal with controls that are applied with a delay > + */ > + > +#include "libcamera/internal/delayed_controls.h" > + > +#include <libcamera/controls.h> > + > +#include "libcamera/internal/log.h" > +#include "libcamera/internal/v4l2_device.h" > + > +/** > + * \file delayed_controls.h > + * \brief Helper to deal with controls that are applied with a delay > + */ > + > +namespace libcamera { > + > +LOG_DEFINE_CATEGORY(DelayedControls) > + > +/** > + * \class DelayedControls > + * \brief Helper to deal with controls that take effect with a delay > + * > + * Some sensor controls take effect with a delay as the sensor needs time to > + * adjust, for example exposure and focus. This is a helper class to deal with > + * such controls and the intended users are pipeline handlers. > + * > + * The idea is to extend the concept of the buffer depth of a pipeline the > + * application needs to maintain to also cover controls. Just as with buffer > + * depth if the application keeps the number of requests queued above the > + * control depth the controls are guaranteed to take effect for the correct > + * request. The control depth is determined by the control with the greatest > + * delay. > + */ > + > +/** > + * \brief Construct a DelayedControls instance > + * \param[in] device The V4L2 device the controls have to be applied to > + * \param[in] delays Map of the numerical V4L2 control ids to their associated > + * delays (in frames) > + * > + * Only controls specified in \a delays are handled. If it's desired to mix > + * delayed controls and controls that take effect immediately the immediate > + * controls must be listed in the \a delays map with a delay value of 0. > + */ > +DelayedControls::DelayedControls(V4L2Device *device, > + const std::unordered_map<uint32_t, unsigned int> &delays) > + : device_(device), maxDelay_(0) > +{ > + const ControlInfoMap &controls = device_->controls(); > + > + /* > + * Create a map of control ids to delays for controls exposed by the > + * device. > + */ > + for (auto const &delay : delays) { > + auto it = controls.find(delay.first); > + if (it == controls.end()) { > + LOG(DelayedControls, Error) > + << "Delay request for control id " > + << utils::hex(delay.first) > + << " but control is not exposed by device " > + << device_->deviceNode(); > + continue; > + } > + > + const ControlId *id = it->first; > + > + delays_[id] = delay.second; > + > + LOG(DelayedControls, Debug) > + << "Set a delay of " << delays_[id] > + << " for " << id->name(); > + > + maxDelay_ = std::max(maxDelay_, delays_[id]); > + } > + > + reset(); > +} > + > +/** > + * \brief Reset state machine > + * > + * Resets the state machine to a starting position based on control values > + * retrieved from the device. > + */ > +void DelayedControls::reset() > +{ > + running_ = false; > + firstSequence_ = 0; > + queueCount_ = 1; > + writeCount_ = 0; > + > + /* Retrieve control as reported by the device. */ > + std::vector<uint32_t> ids; > + for (auto const &delay : delays_) > + ids.push_back(delay.first->id()); > + > + ControlList controls = device_->getControls(ids); > + > + /* Seed the control queue with the controls reported by the device. */ > + values_.clear(); > + for (const auto &ctrl : controls) { > + const ControlId *id = device_->controls().idmap().at(ctrl.first); > + values_[id][0] = Info(ctrl.second); > + } > +} > + > +/** > + * \brief Push a set of controls on the queue > + * \param[in] controls List of controls to add to the device queue > + * > + * Push a set of controls to the control queue. This increases the control queue > + * depth by one. > + * > + * \returns true if \a controls are accepted, or false otherwise > + */ > +bool DelayedControls::push(const ControlList &controls) > +{ > + return queue(controls); > +} > + > +bool DelayedControls::queue(const ControlList &controls) > +{ > + /* Copy state from previous frame. */ > + for (auto &ctrl : values_) { > + Info &info = ctrl.second[queueCount_]; > + info.value = values_[ctrl.first][queueCount_ - 1].value; > + info.updated = false; > + } > + > + /* Update with new controls. */ > + const ControlIdMap &idmap = device_->controls().idmap(); > + for (const auto &control : controls) { > + const auto &it = idmap.find(control.first); > + if (it == idmap.end()) { > + LOG(DelayedControls, Warning) > + << "Unknown control " << control.first; > + return false; > + } > + > + const ControlId *id = it->second; > + > + if (delays_.find(id) == delays_.end()) > + return false; > + > + Info &info = values_[id][queueCount_]; > + > + info.value = control.second; > + info.updated = true; > + > + LOG(DelayedControls, Debug) > + << "Queuing " << id->name() > + << " to " << info.value.toString() > + << " at index " << queueCount_; > + } > + > + queueCount_++; > + > + return true; > +} > + > +/** > + * \brief Read back controls in effect at a sequence number > + * \param[in] sequence The sequence number to get controls for > + * > + * Read back what controls where in effect at a specific sequence number. The > + * history is a ring buffer of 16 entries where new and old values coexist. It's > + * the callers responsibility to not read too old sequence numbers that have been > + * pushed out of the history. > + * > + * Historic values are evicted by pushing new values onto the queue using > + * push(). The max history from the current sequence number that yields valid > + * values are thus 16 minus number of controls pushed. > + * > + * \return The controls at \a sequence number > + */ > +ControlList DelayedControls::get(uint32_t sequence) > +{ > + uint32_t adjustedSeq = sequence - firstSequence_ + 1; > + unsigned int index = std::max<int>(0, adjustedSeq - maxDelay_); > + > + ControlList out(device_->controls()); > + for (const auto &ctrl : values_) { > + const ControlId *id = ctrl.first; > + const Info &info = ctrl.second[index]; > + > + out.set(id->id(), info.value); > + > + LOG(DelayedControls, Debug) > + << "Reading " << id->name() > + << " to " << info.value.toString() > + << " at index " << index; > + } > + > + return out; > +} > + > +/** > + * \brief Inform DelayedControls of the start of a new frame > + * \param[in] sequence Sequence number of the frame that started > + * > + * Inform the state machine that a new frame has started and of its sequence > + * number. Any user of these helpers is responsible to inform the helper about > + * the start of any frame.This can be connected with ease to the start of a > + * exposure (SOE) V4L2 event. > + */ > +void DelayedControls::applyControls(uint32_t sequence) > +{ > + LOG(DelayedControls, Debug) << "frame " << sequence << " started"; > + > + if (!running_) { > + firstSequence_ = sequence; > + running_ = true; > + } > + > + /* > + * Create control list peaking ahead in the value queue to ensure > + * values are set in time to satisfy the sensor delay. > + */ > + ControlList out(device_->controls()); > + for (const auto &ctrl : values_) { > + const ControlId *id = ctrl.first; > + unsigned int delayDiff = maxDelay_ - delays_[id]; > + unsigned int index = std::max<int>(0, writeCount_ - delayDiff); > + const Info &info = ctrl.second[index]; > + > + if (info.updated) { > + out.set(id->id(), info.value); > + LOG(DelayedControls, Debug) > + << "Setting " << id->name() > + << " to " << info.value.toString() > + << " at index " << index; > + } > + } > + > + writeCount_++; > + > + while (writeCount_ >= queueCount_) { > + LOG(DelayedControls, Debug) > + << "Queue is empty, auto queue no-op."; > + queue({}); > + } > + > + device_->setControls(&out); > +} > + > +} /* namespace libcamera */ > diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build > index 387d5d88ecae11ad..5a4bf0d7ba4fd231 100644 > --- a/src/libcamera/meson.build > +++ b/src/libcamera/meson.build > @@ -12,6 +12,7 @@ libcamera_sources = files([ > 'controls.cpp', > 'control_serializer.cpp', > 'control_validator.cpp', > + 'delayed_controls.cpp', > 'device_enumerator.cpp', > 'device_enumerator_sysfs.cpp', > 'event_dispatcher.cpp', > -- > 2.29.2 > > _______________________________________________ > libcamera-devel mailing list > libcamera-devel@lists.libcamera.org > https://lists.libcamera.org/listinfo/libcamera-devel
Hi Naushir, Thanks for your comments. On 2020-12-15 13:53:12 +0000, Naushir Patuck wrote: > Hi Niklas, > > Thank you for your patch. > > On Tue, 15 Dec 2020 at 00:48, Niklas Söderlund < > niklas.soderlund@ragnatech.se> wrote: > > > Some sensor controls take effect with a delay as the sensor needs time > > to adjust, for example exposure. Add an optional helper DelayedControls > > to help pipelines deal with such controls. > > > > The idea is to provide a queue of controls towards the V4L2 device and > > apply individual controls with the specified delay with the aim to get > > predictable and retrievable control values for any given frame. To do > > this the queue of controls needs to be at least as deep as the control > > with the largest delay. > > > > The DelayedControls needs to be informed of every start of exposure. > > This can be emulated but the helper is designed to be used with this > > event being provide by the kernel thru V4L2 events. > > > > This helper is based on StaggeredCtrl from the Raspberry Pi pipeline > > handler but expands on its API. This helpers aims to replace the > > Raspberry Pi implementations and mimics it behavior perfectly. > > > > Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> > > --- > > * Changes since v2 > > - Drop optional argument to reset(). > > - Update commit message. > > - Remove usage of Mutex. > > - Rename frameStart() to applyControls)_. > > - Rename ControlInfo to Into. > > - Rename ControlArray to ControlRingBuffer. > > - Drop ControlsDelays and ControlsValues. > > - Sort headers. > > - Rename iterators. > > - Simplify queueCount_ handeling in reset(). > > - Add more warnings. > > - Update documentation. > > > > * Changes since v2 > > - Improve error logic in queue() as suggested by Jean-Michel Hautbois. > > - s/fistSequence_/firstSequence_/ > > > > * Changes since v1 > > - Correct copyright to reflect work is derived from Raspberry Pi > > pipeline handler. This was always the intention and was wrong in v1. > > - Rewrite large parts of the documentation. > > - Join two loops to one in DelayedControls::DelayedControls() > > --- > > include/libcamera/internal/delayed_controls.h | 82 ++++++ > > src/libcamera/delayed_controls.cpp | 252 ++++++++++++++++++ > > src/libcamera/meson.build | 1 + > > 3 files changed, 335 insertions(+) > > create mode 100644 include/libcamera/internal/delayed_controls.h > > create mode 100644 src/libcamera/delayed_controls.cpp > > > > diff --git a/include/libcamera/internal/delayed_controls.h > > b/include/libcamera/internal/delayed_controls.h > > new file mode 100644 > > index 0000000000000000..1292b484ec9f53e9 > > --- /dev/null > > +++ b/include/libcamera/internal/delayed_controls.h > > @@ -0,0 +1,82 @@ > > +/* SPDX-License-Identifier: LGPL-2.1-or-later */ > > +/* > > + * Copyright (C) 2020, Raspberry Pi (Trading) Ltd. > > + * > > + * delayed_controls.h - Helper to deal with controls that are applied > > with a delay > > + */ > > +#ifndef __LIBCAMERA_INTERNAL_DELAYED_CONTROLS_H__ > > +#define __LIBCAMERA_INTERNAL_DELAYED_CONTROLS_H__ > > + > > +#include <stdint.h> > > +#include <unordered_map> > > + > > +#include <libcamera/controls.h> > > + > > +namespace libcamera { > > + > > +class V4L2Device; > > + > > +class DelayedControls > > +{ > > +public: > > + DelayedControls(V4L2Device *device, > > + const std::unordered_map<uint32_t, unsigned int> > > &delays); > > + > > + void reset(); > > + > > + bool push(const ControlList &controls); > > + ControlList get(uint32_t sequence); > > + > > + void applyControls(uint32_t sequence); > > + > > +private: > > + class Info > > + { > > + public: > > + Info() > > + : updated(false) > > + { > > + } > > + > > + Info(const ControlValue &v) > > + : value(v), updated(true) > > + { > > + } > > + > > + ControlValue value; > > + bool updated; > > + }; > > + > > + /* \todo: Make the listSize configurable at instance creation > > time. */ > > + static constexpr int listSize = 16; > > + class ControlRingBuffer : public std::array<Info, listSize> > > + { > > + public: > > + Info &operator[](unsigned int index) > > + { > > + return std::array<Info, > > listSize>::operator[](index % listSize); > > + } > > + > > + const Info &operator[](unsigned int index) const > > + { > > + return std::array<Info, > > listSize>::operator[](index % listSize); > > + } > > + }; > > + > > + bool queue(const ControlList &controls); > > + > > + V4L2Device *device_; > > + std::unordered_map<const ControlId *, unsigned int> delays_; > > + unsigned int maxDelay_; > > + > > + bool running_; > > + uint32_t firstSequence_; > > + > > + uint32_t queueCount_; > > + uint32_t writeCount_; > > + std::unordered_map<const ControlId *, ControlRingBuffer> values_; > > +}; > > > > StaggeredCtrl has a mutex to protect access to state. I wasn't entirely > sure that was needed. Presumably the SoF event calling applyControls() and > all other public method access occur in the same thread context, so no > protection is needed? Do you foresee this ever changing? As Laurent already replied, the mutex is not needed in out current design. > > + > > +} /* namespace libcamera */ > > + > > +#endif /* __LIBCAMERA_INTERNAL_DELAYED_CONTROLS_H__ */ > > diff --git a/src/libcamera/delayed_controls.cpp > > b/src/libcamera/delayed_controls.cpp > > new file mode 100644 > > index 0000000000000000..db2e51f8c93c4755 > > --- /dev/null > > +++ b/src/libcamera/delayed_controls.cpp > > @@ -0,0 +1,252 @@ > > +/* SPDX-License-Identifier: LGPL-2.1-or-later */ > > +/* > > + * Copyright (C) 2020, Raspberry Pi (Trading) Ltd. > > + * > > + * delayed_controls.h - Helper to deal with controls that are applied > > with a delay > > + */ > > + > > +#include "libcamera/internal/delayed_controls.h" > > + > > +#include <libcamera/controls.h> > > + > > +#include "libcamera/internal/log.h" > > +#include "libcamera/internal/v4l2_device.h" > > + > > +/** > > + * \file delayed_controls.h > > + * \brief Helper to deal with controls that are applied with a delay > > + */ > > + > > +namespace libcamera { > > + > > +LOG_DEFINE_CATEGORY(DelayedControls) > > + > > +/** > > + * \class DelayedControls > > + * \brief Helper to deal with controls that take effect with a delay > > + * > > + * Some sensor controls take effect with a delay as the sensor needs time > > to > > + * adjust, for example exposure and focus. This is a helper class to deal > > with > > + * such controls and the intended users are pipeline handlers. > > + * > > + * The idea is to extend the concept of the buffer depth of a pipeline the > > + * application needs to maintain to also cover controls. Just as with > > buffer > > + * depth if the application keeps the number of requests queued above the > > + * control depth the controls are guaranteed to take effect for the > > correct > > + * request. The control depth is determined by the control with the > > greatest > > + * delay. > > + */ > > + > > +/** > > + * \brief Construct a DelayedControls instance > > + * \param[in] device The V4L2 device the controls have to be applied to > > + * \param[in] delays Map of the numerical V4L2 control ids to their > > associated > > + * delays (in frames) > > + * > > + * Only controls specified in \a delays are handled. If it's desired to > > mix > > + * delayed controls and controls that take effect immediately the > > immediate > > + * controls must be listed in the \a delays map with a delay value of 0. > > + */ > > +DelayedControls::DelayedControls(V4L2Device *device, > > + const std::unordered_map<uint32_t, > > unsigned int> &delays) > > + : device_(device), maxDelay_(0) > > +{ > > + const ControlInfoMap &controls = device_->controls(); > > + > > + /* > > + * Create a map of control ids to delays for controls exposed by > > the > > + * device. > > + */ > > + for (auto const &delay : delays) { > > + auto it = controls.find(delay.first); > > + if (it == controls.end()) { > > + LOG(DelayedControls, Error) > > + << "Delay request for control id " > > + << utils::hex(delay.first) > > + << " but control is not exposed by device " > > + << device_->deviceNode(); > > + continue; > > + } > > + > > + const ControlId *id = it->first; > > + > > + delays_[id] = delay.second; > > + > > + LOG(DelayedControls, Debug) > > + << "Set a delay of " << delays_[id] > > + << " for " << id->name(); > > + > > + maxDelay_ = std::max(maxDelay_, delays_[id]); > > + } > > + > > + reset(); > > +} > > + > > +/** > > + * \brief Reset state machine > > + * > > + * Resets the state machine to a starting position based on control values > > + * retrieved from the device. > > + */ > > +void DelayedControls::reset() > > +{ > > + running_ = false; > > + firstSequence_ = 0; > > + queueCount_ = 1; > > + writeCount_ = 0; > > + > > + /* Retrieve control as reported by the device. */ > > + std::vector<uint32_t> ids; > > + for (auto const &delay : delays_) > > + ids.push_back(delay.first->id()); > > + > > + ControlList controls = device_->getControls(ids); > > + > > + /* Seed the control queue with the controls reported by the > > device. */ > > + values_.clear(); > > + for (const auto &ctrl : controls) { > > + const ControlId *id = > > device_->controls().idmap().at(ctrl.first); > > + values_[id][0] = Info(ctrl.second); > > + } > > > > I think this may have been mentioned in one of the earlier rounds - this > may not be correct as all the controls available to the device may not be > registered with DelayedControls. In those cases, the values_ map lookup > would fail. You would need to validate that id is an available key in the > map. Is this not already done? The 'controls' that are iterated in this loop is constructed from the IDs in delays_ which only contains the controls with an associated delay. > > > > +} > > + > > +/** > > + * \brief Push a set of controls on the queue > > + * \param[in] controls List of controls to add to the device queue > > + * > > + * Push a set of controls to the control queue. This increases the > > control queue > > + * depth by one. > > + * > > + * \returns true if \a controls are accepted, or false otherwise > > + */ > > +bool DelayedControls::push(const ControlList &controls) > > +{ > > + return queue(controls); > > +} > > > > Apologies, I may have missed something obvious, but why do we have the > push() and queue() methods when one calls the other? Good call! This is a leftover from when there was a mutex that needed to be locked when called from the public API but not when the functionality was used internally where the mutex was already locked. Will fold these two together. > > > > + > > +bool DelayedControls::queue(const ControlList &controls) > > +{ > > + /* Copy state from previous frame. */ > > + for (auto &ctrl : values_) { > > + Info &info = ctrl.second[queueCount_]; > > + info.value = values_[ctrl.first][queueCount_ - 1].value; > > + info.updated = false; > > + } > > + > > + /* Update with new controls. */ > > + const ControlIdMap &idmap = device_->controls().idmap(); > > + for (const auto &control : controls) { > > + const auto &it = idmap.find(control.first); > > + if (it == idmap.end()) { > > + LOG(DelayedControls, Warning) > > + << "Unknown control " << control.first; > > + return false; > > + } > > + > > + const ControlId *id = it->second; > > + > > + if (delays_.find(id) == delays_.end()) > > + return false; > > + > > + Info &info = values_[id][queueCount_]; > > + > > + info.value = control.second; > > + info.updated = true; > > + > > + LOG(DelayedControls, Debug) > > + << "Queuing " << id->name() > > + << " to " << info.value.toString() > > + << " at index " << queueCount_; > > + } > > + > > + queueCount_++; > > + > > + return true; > > +} > > + > > +/** > > + * \brief Read back controls in effect at a sequence number > > + * \param[in] sequence The sequence number to get controls for > > + * > > + * Read back what controls where in effect at a specific sequence number. > > The > > + * history is a ring buffer of 16 entries where new and old values > > coexist. It's > > + * the callers responsibility to not read too old sequence numbers that > > have been > > + * pushed out of the history. > > + * > > + * Historic values are evicted by pushing new values onto the queue using > > + * push(). The max history from the current sequence number that yields > > valid > > + * values are thus 16 minus number of controls pushed. > > + * > > + * \return The controls at \a sequence number > > + */ > > +ControlList DelayedControls::get(uint32_t sequence) > > +{ > > + uint32_t adjustedSeq = sequence - firstSequence_ + 1; > > + unsigned int index = std::max<int>(0, adjustedSeq - maxDelay_); > > + > > + ControlList out(device_->controls()); > > + for (const auto &ctrl : values_) { > > + const ControlId *id = ctrl.first; > > + const Info &info = ctrl.second[index]; > > + > > + out.set(id->id(), info.value); > > + > > + LOG(DelayedControls, Debug) > > + << "Reading " << id->name() > > + << " to " << info.value.toString() > > + << " at index " << index; > > + } > > + > > + return out; > > +} > > + > > +/** > > + * \brief Inform DelayedControls of the start of a new frame > > + * \param[in] sequence Sequence number of the frame that started > > + * > > + * Inform the state machine that a new frame has started and of its > > sequence > > + * number. Any user of these helpers is responsible to inform the helper > > about > > + * the start of any frame.This can be connected with ease to the start of > > a > > + * exposure (SOE) V4L2 event. > > + */ > > +void DelayedControls::applyControls(uint32_t sequence) > > +{ > > + LOG(DelayedControls, Debug) << "frame " << sequence << " started"; > > + > > + if (!running_) { > > + firstSequence_ = sequence; > > + running_ = true; > > + } > > + > > + /* > > + * Create control list peaking ahead in the value queue to ensure > > + * values are set in time to satisfy the sensor delay. > > + */ > > + ControlList out(device_->controls()); > > + for (const auto &ctrl : values_) { > > + const ControlId *id = ctrl.first; > > + unsigned int delayDiff = maxDelay_ - delays_[id]; > > + unsigned int index = std::max<int>(0, writeCount_ - > > delayDiff); > > + const Info &info = ctrl.second[index]; > > + > > + if (info.updated) { > > + out.set(id->id(), info.value); > > + LOG(DelayedControls, Debug) > > + << "Setting " << id->name() > > + << " to " << info.value.toString() > > + << " at index " << index; > > + } > > + } > > + > > + writeCount_++; > > + > > + while (writeCount_ >= queueCount_) { > > + LOG(DelayedControls, Debug) > > + << "Queue is empty, auto queue no-op."; > > + queue({}); > > + } > > + > > + device_->setControls(&out); > > > > As mentioned previously, this will need to separate out controls that must > be written immediately (e.g. VBLANK) instead of batched together, but that > can be added at a later stage. I agree this is just the first step and I'm sure we need to evolve and rework parts of DelayedControls as we gain more capabilities. > > Regards, > Naush > > > > > +} > > + > > +} /* namespace libcamera */ > > diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build > > index 387d5d88ecae11ad..5a4bf0d7ba4fd231 100644 > > --- a/src/libcamera/meson.build > > +++ b/src/libcamera/meson.build > > @@ -12,6 +12,7 @@ libcamera_sources = files([ > > 'controls.cpp', > > 'control_serializer.cpp', > > 'control_validator.cpp', > > + 'delayed_controls.cpp', > > 'device_enumerator.cpp', > > 'device_enumerator_sysfs.cpp', > > 'event_dispatcher.cpp', > > -- > > 2.29.2 > > > >
Hi Niklas, On Fri, 8 Jan 2021 at 15:21, Niklas Söderlund <niklas.soderlund@ragnatech.se> wrote: > Hi Naushir, > > Thanks for your comments. > > On 2020-12-15 13:53:12 +0000, Naushir Patuck wrote: > > Hi Niklas, > > > > Thank you for your patch. > > > > On Tue, 15 Dec 2020 at 00:48, Niklas Söderlund < > > niklas.soderlund@ragnatech.se> wrote: > > > > > Some sensor controls take effect with a delay as the sensor needs time > > > to adjust, for example exposure. Add an optional helper DelayedControls > > > to help pipelines deal with such controls. > > > > > > The idea is to provide a queue of controls towards the V4L2 device and > > > apply individual controls with the specified delay with the aim to get > > > predictable and retrievable control values for any given frame. To do > > > this the queue of controls needs to be at least as deep as the control > > > with the largest delay. > > > > > > The DelayedControls needs to be informed of every start of exposure. > > > This can be emulated but the helper is designed to be used with this > > > event being provide by the kernel thru V4L2 events. > > > > > > This helper is based on StaggeredCtrl from the Raspberry Pi pipeline > > > handler but expands on its API. This helpers aims to replace the > > > Raspberry Pi implementations and mimics it behavior perfectly. > > > > > > Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> > > > --- > > > * Changes since v2 > > > - Drop optional argument to reset(). > > > - Update commit message. > > > - Remove usage of Mutex. > > > - Rename frameStart() to applyControls)_. > > > - Rename ControlInfo to Into. > > > - Rename ControlArray to ControlRingBuffer. > > > - Drop ControlsDelays and ControlsValues. > > > - Sort headers. > > > - Rename iterators. > > > - Simplify queueCount_ handeling in reset(). > > > - Add more warnings. > > > - Update documentation. > > > > > > * Changes since v2 > > > - Improve error logic in queue() as suggested by Jean-Michel Hautbois. > > > - s/fistSequence_/firstSequence_/ > > > > > > * Changes since v1 > > > - Correct copyright to reflect work is derived from Raspberry Pi > > > pipeline handler. This was always the intention and was wrong in v1. > > > - Rewrite large parts of the documentation. > > > - Join two loops to one in DelayedControls::DelayedControls() > > > --- > > > include/libcamera/internal/delayed_controls.h | 82 ++++++ > > > src/libcamera/delayed_controls.cpp | 252 ++++++++++++++++++ > > > src/libcamera/meson.build | 1 + > > > 3 files changed, 335 insertions(+) > > > create mode 100644 include/libcamera/internal/delayed_controls.h > > > create mode 100644 src/libcamera/delayed_controls.cpp > > > > > > diff --git a/include/libcamera/internal/delayed_controls.h > > > b/include/libcamera/internal/delayed_controls.h > > > new file mode 100644 > > > index 0000000000000000..1292b484ec9f53e9 > > > --- /dev/null > > > +++ b/include/libcamera/internal/delayed_controls.h > > > @@ -0,0 +1,82 @@ > > > +/* SPDX-License-Identifier: LGPL-2.1-or-later */ > > > +/* > > > + * Copyright (C) 2020, Raspberry Pi (Trading) Ltd. > > > + * > > > + * delayed_controls.h - Helper to deal with controls that are applied > > > with a delay > > > + */ > > > +#ifndef __LIBCAMERA_INTERNAL_DELAYED_CONTROLS_H__ > > > +#define __LIBCAMERA_INTERNAL_DELAYED_CONTROLS_H__ > > > + > > > +#include <stdint.h> > > > +#include <unordered_map> > > > + > > > +#include <libcamera/controls.h> > > > + > > > +namespace libcamera { > > > + > > > +class V4L2Device; > > > + > > > +class DelayedControls > > > +{ > > > +public: > > > + DelayedControls(V4L2Device *device, > > > + const std::unordered_map<uint32_t, unsigned > int> > > > &delays); > > > + > > > + void reset(); > > > + > > > + bool push(const ControlList &controls); > > > + ControlList get(uint32_t sequence); > > > + > > > + void applyControls(uint32_t sequence); > > > + > > > +private: > > > + class Info > > > + { > > > + public: > > > + Info() > > > + : updated(false) > > > + { > > > + } > > > + > > > + Info(const ControlValue &v) > > > + : value(v), updated(true) > > > + { > > > + } > > > + > > > + ControlValue value; > > > + bool updated; > > > + }; > > > + > > > + /* \todo: Make the listSize configurable at instance creation > > > time. */ > > > + static constexpr int listSize = 16; > > > + class ControlRingBuffer : public std::array<Info, listSize> > > > + { > > > + public: > > > + Info &operator[](unsigned int index) > > > + { > > > + return std::array<Info, > > > listSize>::operator[](index % listSize); > > > + } > > > + > > > + const Info &operator[](unsigned int index) const > > > + { > > > + return std::array<Info, > > > listSize>::operator[](index % listSize); > > > + } > > > + }; > > > + > > > + bool queue(const ControlList &controls); > > > + > > > + V4L2Device *device_; > > > + std::unordered_map<const ControlId *, unsigned int> delays_; > > > + unsigned int maxDelay_; > > > + > > > + bool running_; > > > + uint32_t firstSequence_; > > > + > > > + uint32_t queueCount_; > > > + uint32_t writeCount_; > > > + std::unordered_map<const ControlId *, ControlRingBuffer> > values_; > > > +}; > > > > > > > StaggeredCtrl has a mutex to protect access to state. I wasn't entirely > > sure that was needed. Presumably the SoF event calling applyControls() > and > > all other public method access occur in the same thread context, so no > > protection is needed? Do you foresee this ever changing? > > As Laurent already replied, the mutex is not needed in out current > design. > > > > > + > > > +} /* namespace libcamera */ > > > + > > > +#endif /* __LIBCAMERA_INTERNAL_DELAYED_CONTROLS_H__ */ > > > diff --git a/src/libcamera/delayed_controls.cpp > > > b/src/libcamera/delayed_controls.cpp > > > new file mode 100644 > > > index 0000000000000000..db2e51f8c93c4755 > > > --- /dev/null > > > +++ b/src/libcamera/delayed_controls.cpp > > > @@ -0,0 +1,252 @@ > > > +/* SPDX-License-Identifier: LGPL-2.1-or-later */ > > > +/* > > > + * Copyright (C) 2020, Raspberry Pi (Trading) Ltd. > > > + * > > > + * delayed_controls.h - Helper to deal with controls that are applied > > > with a delay > > > + */ > > > + > > > +#include "libcamera/internal/delayed_controls.h" > > > + > > > +#include <libcamera/controls.h> > > > + > > > +#include "libcamera/internal/log.h" > > > +#include "libcamera/internal/v4l2_device.h" > > > + > > > +/** > > > + * \file delayed_controls.h > > > + * \brief Helper to deal with controls that are applied with a delay > > > + */ > > > + > > > +namespace libcamera { > > > + > > > +LOG_DEFINE_CATEGORY(DelayedControls) > > > + > > > +/** > > > + * \class DelayedControls > > > + * \brief Helper to deal with controls that take effect with a delay > > > + * > > > + * Some sensor controls take effect with a delay as the sensor needs > time > > > to > > > + * adjust, for example exposure and focus. This is a helper class to > deal > > > with > > > + * such controls and the intended users are pipeline handlers. > > > + * > > > + * The idea is to extend the concept of the buffer depth of a > pipeline the > > > + * application needs to maintain to also cover controls. Just as with > > > buffer > > > + * depth if the application keeps the number of requests queued above > the > > > + * control depth the controls are guaranteed to take effect for the > > > correct > > > + * request. The control depth is determined by the control with the > > > greatest > > > + * delay. > > > + */ > > > + > > > +/** > > > + * \brief Construct a DelayedControls instance > > > + * \param[in] device The V4L2 device the controls have to be applied > to > > > + * \param[in] delays Map of the numerical V4L2 control ids to their > > > associated > > > + * delays (in frames) > > > + * > > > + * Only controls specified in \a delays are handled. If it's desired > to > > > mix > > > + * delayed controls and controls that take effect immediately the > > > immediate > > > + * controls must be listed in the \a delays map with a delay value of > 0. > > > + */ > > > +DelayedControls::DelayedControls(V4L2Device *device, > > > + const std::unordered_map<uint32_t, > > > unsigned int> &delays) > > > + : device_(device), maxDelay_(0) > > > +{ > > > + const ControlInfoMap &controls = device_->controls(); > > > + > > > + /* > > > + * Create a map of control ids to delays for controls exposed > by > > > the > > > + * device. > > > + */ > > > + for (auto const &delay : delays) { > > > + auto it = controls.find(delay.first); > > > + if (it == controls.end()) { > > > + LOG(DelayedControls, Error) > > > + << "Delay request for control id " > > > + << utils::hex(delay.first) > > > + << " but control is not exposed by > device " > > > + << device_->deviceNode(); > > > + continue; > > > + } > > > + > > > + const ControlId *id = it->first; > > > + > > > + delays_[id] = delay.second; > > > + > > > + LOG(DelayedControls, Debug) > > > + << "Set a delay of " << delays_[id] > > > + << " for " << id->name(); > > > + > > > + maxDelay_ = std::max(maxDelay_, delays_[id]); > > > + } > > > + > > > + reset(); > > > +} > > > + > > > +/** > > > + * \brief Reset state machine > > > + * > > > + * Resets the state machine to a starting position based on control > values > > > + * retrieved from the device. > > > + */ > > > +void DelayedControls::reset() > > > +{ > > > + running_ = false; > > > + firstSequence_ = 0; > > > + queueCount_ = 1; > > > + writeCount_ = 0; > > > + > > > + /* Retrieve control as reported by the device. */ > > > + std::vector<uint32_t> ids; > > > + for (auto const &delay : delays_) > > > + ids.push_back(delay.first->id()); > > > + > > > + ControlList controls = device_->getControls(ids); > > > + > > > + /* Seed the control queue with the controls reported by the > > > device. */ > > > + values_.clear(); > > > + for (const auto &ctrl : controls) { > > > + const ControlId *id = > > > device_->controls().idmap().at(ctrl.first); > > > + values_[id][0] = Info(ctrl.second); > > > + } > > > > > > > I think this may have been mentioned in one of the earlier rounds - this > > may not be correct as all the controls available to the device may not > be > > registered with DelayedControls. In those cases, the values_ map lookup > > would fail. You would need to validate that id is an available key in > the > > map. > > Is this not already done? The 'controls' that are iterated in this loop > is constructed from the IDs in delays_ which only contains the controls > with an associated delay. > Yes, I see that now. Sorry I missed that in my earlier read of the patch. With the minor fixup to merge push() and queue(), Reviewed-by: Naushir Patuck <naush@raspberrypi.com> > > > > > > > > +} > > > + > > > +/** > > > + * \brief Push a set of controls on the queue > > > + * \param[in] controls List of controls to add to the device queue > > > + * > > > + * Push a set of controls to the control queue. This increases the > > > control queue > > > + * depth by one. > > > + * > > > + * \returns true if \a controls are accepted, or false otherwise > > > + */ > > > +bool DelayedControls::push(const ControlList &controls) > > > +{ > > > + return queue(controls); > > > +} > > > > > > > Apologies, I may have missed something obvious, but why do we have the > > push() and queue() methods when one calls the other? > > Good call! This is a leftover from when there was a mutex that needed to > be locked when called from the public API but not when the functionality > was used internally where the mutex was already locked. Will fold these > two together. > > > > > > > > + > > > +bool DelayedControls::queue(const ControlList &controls) > > > +{ > > > + /* Copy state from previous frame. */ > > > + for (auto &ctrl : values_) { > > > + Info &info = ctrl.second[queueCount_]; > > > + info.value = values_[ctrl.first][queueCount_ - > 1].value; > > > + info.updated = false; > > > + } > > > + > > > + /* Update with new controls. */ > > > + const ControlIdMap &idmap = device_->controls().idmap(); > > > + for (const auto &control : controls) { > > > + const auto &it = idmap.find(control.first); > > > + if (it == idmap.end()) { > > > + LOG(DelayedControls, Warning) > > > + << "Unknown control " << control.first; > > > + return false; > > > + } > > > + > > > + const ControlId *id = it->second; > > > + > > > + if (delays_.find(id) == delays_.end()) > > > + return false; > > > + > > > + Info &info = values_[id][queueCount_]; > > > + > > > + info.value = control.second; > > > + info.updated = true; > > > + > > > + LOG(DelayedControls, Debug) > > > + << "Queuing " << id->name() > > > + << " to " << info.value.toString() > > > + << " at index " << queueCount_; > > > + } > > > + > > > + queueCount_++; > > > + > > > + return true; > > > +} > > > + > > > +/** > > > + * \brief Read back controls in effect at a sequence number > > > + * \param[in] sequence The sequence number to get controls for > > > + * > > > + * Read back what controls where in effect at a specific sequence > number. > > > The > > > + * history is a ring buffer of 16 entries where new and old values > > > coexist. It's > > > + * the callers responsibility to not read too old sequence numbers > that > > > have been > > > + * pushed out of the history. > > > + * > > > + * Historic values are evicted by pushing new values onto the queue > using > > > + * push(). The max history from the current sequence number that > yields > > > valid > > > + * values are thus 16 minus number of controls pushed. > > > + * > > > + * \return The controls at \a sequence number > > > + */ > > > +ControlList DelayedControls::get(uint32_t sequence) > > > +{ > > > + uint32_t adjustedSeq = sequence - firstSequence_ + 1; > > > + unsigned int index = std::max<int>(0, adjustedSeq - maxDelay_); > > > + > > > + ControlList out(device_->controls()); > > > + for (const auto &ctrl : values_) { > > > + const ControlId *id = ctrl.first; > > > + const Info &info = ctrl.second[index]; > > > + > > > + out.set(id->id(), info.value); > > > + > > > + LOG(DelayedControls, Debug) > > > + << "Reading " << id->name() > > > + << " to " << info.value.toString() > > > + << " at index " << index; > > > + } > > > + > > > + return out; > > > +} > > > + > > > +/** > > > + * \brief Inform DelayedControls of the start of a new frame > > > + * \param[in] sequence Sequence number of the frame that started > > > + * > > > + * Inform the state machine that a new frame has started and of its > > > sequence > > > + * number. Any user of these helpers is responsible to inform the > helper > > > about > > > + * the start of any frame.This can be connected with ease to the > start of > > > a > > > + * exposure (SOE) V4L2 event. > > > + */ > > > +void DelayedControls::applyControls(uint32_t sequence) > > > +{ > > > + LOG(DelayedControls, Debug) << "frame " << sequence << " > started"; > > > + > > > + if (!running_) { > > > + firstSequence_ = sequence; > > > + running_ = true; > > > + } > > > + > > > + /* > > > + * Create control list peaking ahead in the value queue to > ensure > > > + * values are set in time to satisfy the sensor delay. > > > + */ > > > + ControlList out(device_->controls()); > > > + for (const auto &ctrl : values_) { > > > + const ControlId *id = ctrl.first; > > > + unsigned int delayDiff = maxDelay_ - delays_[id]; > > > + unsigned int index = std::max<int>(0, writeCount_ - > > > delayDiff); > > > + const Info &info = ctrl.second[index]; > > > + > > > + if (info.updated) { > > > + out.set(id->id(), info.value); > > > + LOG(DelayedControls, Debug) > > > + << "Setting " << id->name() > > > + << " to " << info.value.toString() > > > + << " at index " << index; > > > + } > > > + } > > > + > > > + writeCount_++; > > > + > > > + while (writeCount_ >= queueCount_) { > > > + LOG(DelayedControls, Debug) > > > + << "Queue is empty, auto queue no-op."; > > > + queue({}); > > > + } > > > + > > > + device_->setControls(&out); > > > > > > > As mentioned previously, this will need to separate out controls that > must > > be written immediately (e.g. VBLANK) instead of batched together, but > that > > can be added at a later stage. > > I agree this is just the first step and I'm sure we need to evolve and > rework parts of DelayedControls as we gain more capabilities. > > > > > Regards, > > Naush > > > > > > > > > +} > > > + > > > +} /* namespace libcamera */ > > > diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build > > > index 387d5d88ecae11ad..5a4bf0d7ba4fd231 100644 > > > --- a/src/libcamera/meson.build > > > +++ b/src/libcamera/meson.build > > > @@ -12,6 +12,7 @@ libcamera_sources = files([ > > > 'controls.cpp', > > > 'control_serializer.cpp', > > > 'control_validator.cpp', > > > + 'delayed_controls.cpp', > > > 'device_enumerator.cpp', > > > 'device_enumerator_sysfs.cpp', > > > 'event_dispatcher.cpp', > > > -- > > > 2.29.2 > > > > > > > > -- > Regards, > Niklas Söderlund >
Hi Niklas, Thank you for the patch. In the subject line, s/applies/apply/ On Tue, Dec 15, 2020 at 01:48:04AM +0100, Niklas Söderlund wrote: > Some sensor controls take effect with a delay as the sensor needs time > to adjust, for example exposure. Add an optional helper DelayedControls > to help pipelines deal with such controls. > > The idea is to provide a queue of controls towards the V4L2 device and > apply individual controls with the specified delay with the aim to get > predictable and retrievable control values for any given frame. To do > this the queue of controls needs to be at least as deep as the control > with the largest delay. > > The DelayedControls needs to be informed of every start of exposure. > This can be emulated but the helper is designed to be used with this > event being provide by the kernel thru V4L2 events. s/thru/through/ > This helper is based on StaggeredCtrl from the Raspberry Pi pipeline > handler but expands on its API. This helpers aims to replace the > Raspberry Pi implementations and mimics it behavior perfectly. > > Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> > --- > * Changes since v2 > - Drop optional argument to reset(). > - Update commit message. > - Remove usage of Mutex. > - Rename frameStart() to applyControls)_. > - Rename ControlInfo to Into. > - Rename ControlArray to ControlRingBuffer. > - Drop ControlsDelays and ControlsValues. > - Sort headers. > - Rename iterators. > - Simplify queueCount_ handeling in reset(). > - Add more warnings. > - Update documentation. > > * Changes since v2 > - Improve error logic in queue() as suggested by Jean-Michel Hautbois. > - s/fistSequence_/firstSequence_/ > > * Changes since v1 > - Correct copyright to reflect work is derived from Raspberry Pi > pipeline handler. This was always the intention and was wrong in v1. > - Rewrite large parts of the documentation. > - Join two loops to one in DelayedControls::DelayedControls() > --- > include/libcamera/internal/delayed_controls.h | 82 ++++++ > src/libcamera/delayed_controls.cpp | 252 ++++++++++++++++++ > src/libcamera/meson.build | 1 + > 3 files changed, 335 insertions(+) > create mode 100644 include/libcamera/internal/delayed_controls.h > create mode 100644 src/libcamera/delayed_controls.cpp > > diff --git a/include/libcamera/internal/delayed_controls.h b/include/libcamera/internal/delayed_controls.h > new file mode 100644 > index 0000000000000000..1292b484ec9f53e9 > --- /dev/null > +++ b/include/libcamera/internal/delayed_controls.h > @@ -0,0 +1,82 @@ > +/* SPDX-License-Identifier: LGPL-2.1-or-later */ > +/* > + * Copyright (C) 2020, Raspberry Pi (Trading) Ltd. > + * > + * delayed_controls.h - Helper to deal with controls that are applied with a delay > + */ > +#ifndef __LIBCAMERA_INTERNAL_DELAYED_CONTROLS_H__ > +#define __LIBCAMERA_INTERNAL_DELAYED_CONTROLS_H__ > + > +#include <stdint.h> > +#include <unordered_map> > + > +#include <libcamera/controls.h> > + > +namespace libcamera { > + > +class V4L2Device; > + > +class DelayedControls > +{ > +public: > + DelayedControls(V4L2Device *device, > + const std::unordered_map<uint32_t, unsigned int> &delays); I've sent the following comment on v3: > I could imagine this being useful for non-V4L2 devices too. We could > instead pass a ControlInfoMap to the constructor, and move the > setControls() call to the user of this class. There's no need to do so > now (but of course if you think it's a good idea, I'm not asking to > delay such rework :-)), but could you capture this in a \todo comment > (if you agree with the idea) ? I see neither a rework of the API, nor a \todo comment. Is that an oversight, or did you disagree with the idea ? In the latter case, a reply to the review would be nice. > + > + void reset(); > + > + bool push(const ControlList &controls); > + ControlList get(uint32_t sequence); > + > + void applyControls(uint32_t sequence); > + > +private: > + class Info > + { > + public: > + Info() > + : updated(false) > + { > + } > + > + Info(const ControlValue &v) > + : value(v), updated(true) > + { > + } > + > + ControlValue value; > + bool updated; > + }; Same here, v3 had the following review comment: > You could also make this class inherit from ControlValue, to avoid the > explicit .value below. > + > + /* \todo: Make the listSize configurable at instance creation time. */ > + static constexpr int listSize = 16; > + class ControlRingBuffer : public std::array<Info, listSize> > + { > + public: > + Info &operator[](unsigned int index) > + { > + return std::array<Info, listSize>::operator[](index % listSize); > + } > + > + const Info &operator[](unsigned int index) const > + { > + return std::array<Info, listSize>::operator[](index % listSize); > + } > + }; There were further comments in the review of v3 that have also not been addressed. > + > + bool queue(const ControlList &controls); > + > + V4L2Device *device_; > + std::unordered_map<const ControlId *, unsigned int> delays_; > + unsigned int maxDelay_; > + > + bool running_; > + uint32_t firstSequence_; > + > + uint32_t queueCount_; > + uint32_t writeCount_; > + std::unordered_map<const ControlId *, ControlRingBuffer> values_; > +}; > + > +} /* namespace libcamera */ > + > +#endif /* __LIBCAMERA_INTERNAL_DELAYED_CONTROLS_H__ */ > diff --git a/src/libcamera/delayed_controls.cpp b/src/libcamera/delayed_controls.cpp > new file mode 100644 > index 0000000000000000..db2e51f8c93c4755 > --- /dev/null > +++ b/src/libcamera/delayed_controls.cpp > @@ -0,0 +1,252 @@ > +/* SPDX-License-Identifier: LGPL-2.1-or-later */ > +/* > + * Copyright (C) 2020, Raspberry Pi (Trading) Ltd. > + * > + * delayed_controls.h - Helper to deal with controls that are applied with a delay > + */ > + > +#include "libcamera/internal/delayed_controls.h" > + > +#include <libcamera/controls.h> > + > +#include "libcamera/internal/log.h" > +#include "libcamera/internal/v4l2_device.h" > + > +/** > + * \file delayed_controls.h > + * \brief Helper to deal with controls that are applied with a delay s/are applied/take effect/ (matching the documentation of the class below) > + */ > + > +namespace libcamera { > + > +LOG_DEFINE_CATEGORY(DelayedControls) > + > +/** > + * \class DelayedControls > + * \brief Helper to deal with controls that take effect with a delay > + * > + * Some sensor controls take effect with a delay as the sensor needs time to > + * adjust, for example exposure and focus. This is a helper class to deal with Focus is a different beast. Let's s/focus/analog gain/, that's a better example. > + * such controls and the intended users are pipeline handlers. > + * > + * The idea is to extend the concept of the buffer depth of a pipeline the > + * application needs to maintain to also cover controls. Just as with buffer > + * depth if the application keeps the number of requests queued above the > + * control depth the controls are guaranteed to take effect for the correct > + * request. The control depth is determined by the control with the greatest > + * delay. > + */ > + > +/** > + * \brief Construct a DelayedControls instance > + * \param[in] device The V4L2 device the controls have to be applied to > + * \param[in] delays Map of the numerical V4L2 control ids to their associated > + * delays (in frames) > + * > + * Only controls specified in \a delays are handled. If it's desired to mix > + * delayed controls and controls that take effect immediately the immediate > + * controls must be listed in the \a delays map with a delay value of 0. > + */ > +DelayedControls::DelayedControls(V4L2Device *device, > + const std::unordered_map<uint32_t, unsigned int> &delays) > + : device_(device), maxDelay_(0) > +{ > + const ControlInfoMap &controls = device_->controls(); > + > + /* > + * Create a map of control ids to delays for controls exposed by the > + * device. > + */ > + for (auto const &delay : delays) { > + auto it = controls.find(delay.first); > + if (it == controls.end()) { > + LOG(DelayedControls, Error) > + << "Delay request for control id " > + << utils::hex(delay.first) > + << " but control is not exposed by device " > + << device_->deviceNode(); > + continue; > + } > + > + const ControlId *id = it->first; > + > + delays_[id] = delay.second; > + > + LOG(DelayedControls, Debug) > + << "Set a delay of " << delays_[id] > + << " for " << id->name(); > + > + maxDelay_ = std::max(maxDelay_, delays_[id]); > + } > + > + reset(); > +} > + > +/** > + * \brief Reset state machine > + * > + * Resets the state machine to a starting position based on control values > + * retrieved from the device. This is a bit terse, and so is the \class documentation. Reading the documentation only, not the code, it would be quite difficult to use this class :-S > + */ > +void DelayedControls::reset() > +{ > + running_ = false; > + firstSequence_ = 0; > + queueCount_ = 1; > + writeCount_ = 0; > + > + /* Retrieve control as reported by the device. */ > + std::vector<uint32_t> ids; > + for (auto const &delay : delays_) > + ids.push_back(delay.first->id()); > + > + ControlList controls = device_->getControls(ids); > + > + /* Seed the control queue with the controls reported by the device. */ > + values_.clear(); > + for (const auto &ctrl : controls) { > + const ControlId *id = device_->controls().idmap().at(ctrl.first); > + values_[id][0] = Info(ctrl.second); > + } > +} > + > +/** > + * \brief Push a set of controls on the queue > + * \param[in] controls List of controls to add to the device queue > + * > + * Push a set of controls to the control queue. This increases the control queue > + * depth by one. > + * > + * \returns true if \a controls are accepted, or false otherwise > + */ > +bool DelayedControls::push(const ControlList &controls) > +{ > + return queue(controls); > +} > + > +bool DelayedControls::queue(const ControlList &controls) > +{ > + /* Copy state from previous frame. */ > + for (auto &ctrl : values_) { > + Info &info = ctrl.second[queueCount_]; > + info.value = values_[ctrl.first][queueCount_ - 1].value; > + info.updated = false; > + } > + > + /* Update with new controls. */ > + const ControlIdMap &idmap = device_->controls().idmap(); > + for (const auto &control : controls) { > + const auto &it = idmap.find(control.first); > + if (it == idmap.end()) { > + LOG(DelayedControls, Warning) > + << "Unknown control " << control.first; > + return false; > + } > + > + const ControlId *id = it->second; > + > + if (delays_.find(id) == delays_.end()) > + return false; > + > + Info &info = values_[id][queueCount_]; > + > + info.value = control.second; > + info.updated = true; > + > + LOG(DelayedControls, Debug) > + << "Queuing " << id->name() > + << " to " << info.value.toString() > + << " at index " << queueCount_; > + } > + > + queueCount_++; > + > + return true; > +} > + > +/** > + * \brief Read back controls in effect at a sequence number > + * \param[in] sequence The sequence number to get controls for > + * > + * Read back what controls where in effect at a specific sequence number. The > + * history is a ring buffer of 16 entries where new and old values coexist. It's > + * the callers responsibility to not read too old sequence numbers that have been > + * pushed out of the history. > + * > + * Historic values are evicted by pushing new values onto the queue using > + * push(). The max history from the current sequence number that yields valid > + * values are thus 16 minus number of controls pushed. > + * > + * \return The controls at \a sequence number > + */ > +ControlList DelayedControls::get(uint32_t sequence) > +{ > + uint32_t adjustedSeq = sequence - firstSequence_ + 1; > + unsigned int index = std::max<int>(0, adjustedSeq - maxDelay_); > + > + ControlList out(device_->controls()); > + for (const auto &ctrl : values_) { > + const ControlId *id = ctrl.first; > + const Info &info = ctrl.second[index]; > + > + out.set(id->id(), info.value); > + > + LOG(DelayedControls, Debug) > + << "Reading " << id->name() > + << " to " << info.value.toString() > + << " at index " << index; > + } > + > + return out; > +} > + > +/** > + * \brief Inform DelayedControls of the start of a new frame > + * \param[in] sequence Sequence number of the frame that started > + * > + * Inform the state machine that a new frame has started and of its sequence > + * number. Any user of these helpers is responsible to inform the helper about > + * the start of any frame.This can be connected with ease to the start of a > + * exposure (SOE) V4L2 event. > + */ > +void DelayedControls::applyControls(uint32_t sequence) > +{ > + LOG(DelayedControls, Debug) << "frame " << sequence << " started"; > + > + if (!running_) { > + firstSequence_ = sequence; > + running_ = true; > + } > + > + /* > + * Create control list peaking ahead in the value queue to ensure > + * values are set in time to satisfy the sensor delay. > + */ > + ControlList out(device_->controls()); > + for (const auto &ctrl : values_) { > + const ControlId *id = ctrl.first; > + unsigned int delayDiff = maxDelay_ - delays_[id]; > + unsigned int index = std::max<int>(0, writeCount_ - delayDiff); A reply to the v3 review would also be appreciated for the issue pointed our regarding this code. I still can't see the implementation being correct :-S > + const Info &info = ctrl.second[index]; > + > + if (info.updated) { > + out.set(id->id(), info.value); > + LOG(DelayedControls, Debug) > + << "Setting " << id->name() > + << " to " << info.value.toString() > + << " at index " << index; > + } > + } > + > + writeCount_++; > + > + while (writeCount_ >= queueCount_) { > + LOG(DelayedControls, Debug) > + << "Queue is empty, auto queue no-op."; > + queue({}); > + } > + > + device_->setControls(&out); > +} > + > +} /* namespace libcamera */ > diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build > index 387d5d88ecae11ad..5a4bf0d7ba4fd231 100644 > --- a/src/libcamera/meson.build > +++ b/src/libcamera/meson.build > @@ -12,6 +12,7 @@ libcamera_sources = files([ > 'controls.cpp', > 'control_serializer.cpp', > 'control_validator.cpp', > + 'delayed_controls.cpp', > 'device_enumerator.cpp', > 'device_enumerator_sysfs.cpp', > 'event_dispatcher.cpp',
Hi Laurent, Thanks for your feedback. First let me apologise for for the misunderstanding on my side regarding the comments from you in v3 that are not addressed in this version. After reading your comments in addition to offline discussions my understanding of the way forward was to, - Remove my attempt in v1 and v2 to integrate DelayedControls into CameraSensor and leave the work for pipeline handlers. The goal was to not prematurely build a CameraSensor integration which did not suite all users. This I agreed with and it is done. - Likewise my understanding was that we aimed to not make any large redesigns of the DelayedControls logic as it currently performs in lockstep with StaggeredCtrls which it is replacing. Once we had integrated it with RkISP1 replacing the awfully Timeline solution (I'm allowed to cal it names as I designed it :-P) and the IPU3 to allow a IPA base to take shape would we start to fix it up in a way that would solve problems for all pipelines. My view was that this would be done on top as I think the integration in CameraSensor and improving on the DelayedControls is a bit related to each other and that is why I "ignored" your comments when sending v4. Judging from your reaction I have misread the situation and for that I'm sorry. I never meant to ignore your comments, I only did so believing we both shared the same understanding. orz I will reply to your comments below and I will copy-paste in your comments from v3 that you have not already yourself. On 2021-01-10 18:07:16 +0200, Laurent Pinchart wrote: > Hi Niklas, > > Thank you for the patch. > > In the subject line, s/applies/apply/ > > On Tue, Dec 15, 2020 at 01:48:04AM +0100, Niklas Söderlund wrote: > > Some sensor controls take effect with a delay as the sensor needs time > > to adjust, for example exposure. Add an optional helper DelayedControls > > to help pipelines deal with such controls. > > > > The idea is to provide a queue of controls towards the V4L2 device and > > apply individual controls with the specified delay with the aim to get > > predictable and retrievable control values for any given frame. To do > > this the queue of controls needs to be at least as deep as the control > > with the largest delay. > > > > The DelayedControls needs to be informed of every start of exposure. > > This can be emulated but the helper is designed to be used with this > > event being provide by the kernel thru V4L2 events. > > s/thru/through/ > > > This helper is based on StaggeredCtrl from the Raspberry Pi pipeline > > handler but expands on its API. This helpers aims to replace the > > Raspberry Pi implementations and mimics it behavior perfectly. > > > > Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> > > --- > > * Changes since v2 > > - Drop optional argument to reset(). > > - Update commit message. > > - Remove usage of Mutex. > > - Rename frameStart() to applyControls)_. > > - Rename ControlInfo to Into. > > - Rename ControlArray to ControlRingBuffer. > > - Drop ControlsDelays and ControlsValues. > > - Sort headers. > > - Rename iterators. > > - Simplify queueCount_ handeling in reset(). > > - Add more warnings. > > - Update documentation. > > > > * Changes since v2 > > - Improve error logic in queue() as suggested by Jean-Michel Hautbois. > > - s/fistSequence_/firstSequence_/ > > > > * Changes since v1 > > - Correct copyright to reflect work is derived from Raspberry Pi > > pipeline handler. This was always the intention and was wrong in v1. > > - Rewrite large parts of the documentation. > > - Join two loops to one in DelayedControls::DelayedControls() > > --- > > include/libcamera/internal/delayed_controls.h | 82 ++++++ > > src/libcamera/delayed_controls.cpp | 252 ++++++++++++++++++ > > src/libcamera/meson.build | 1 + > > 3 files changed, 335 insertions(+) > > create mode 100644 include/libcamera/internal/delayed_controls.h > > create mode 100644 src/libcamera/delayed_controls.cpp > > > > diff --git a/include/libcamera/internal/delayed_controls.h b/include/libcamera/internal/delayed_controls.h > > new file mode 100644 > > index 0000000000000000..1292b484ec9f53e9 > > --- /dev/null > > +++ b/include/libcamera/internal/delayed_controls.h > > @@ -0,0 +1,82 @@ > > +/* SPDX-License-Identifier: LGPL-2.1-or-later */ > > +/* > > + * Copyright (C) 2020, Raspberry Pi (Trading) Ltd. > > + * > > + * delayed_controls.h - Helper to deal with controls that are applied with a delay > > + */ > > +#ifndef __LIBCAMERA_INTERNAL_DELAYED_CONTROLS_H__ > > +#define __LIBCAMERA_INTERNAL_DELAYED_CONTROLS_H__ > > + > > +#include <stdint.h> > > +#include <unordered_map> > > + > > +#include <libcamera/controls.h> > > + > > +namespace libcamera { > > + > > +class V4L2Device; > > + > > +class DelayedControls > > +{ > > +public: > > + DelayedControls(V4L2Device *device, > > + const std::unordered_map<uint32_t, unsigned int> &delays); > > I've sent the following comment on v3: > > > I could imagine this being useful for non-V4L2 devices too. We could > > instead pass a ControlInfoMap to the constructor, and move the > > setControls() call to the user of this class. There's no need to do so > > now (but of course if you think it's a good idea, I'm not asking to > > delay such rework :-)), but could you capture this in a \todo comment > > (if you agree with the idea) ? > > I see neither a rework of the API, nor a \todo comment. Is that an > oversight, or did you disagree with the idea ? In the latter case, a > reply to the review would be nice. Unluckily for my situation, this is a comment I do not agree with :-) Maybe down the road once we know the design of how we want to integrate this with CameraSensor and what type of other non-V4L2 devices this could be useful for I might be see it differently. Foe now I think we should keep it as is as, at least until we know how to integrate it with CameraSensor which I think should be the next stop done on top of this (together with integrating the sensor database). > > > + > > + void reset(); > > + > > + bool push(const ControlList &controls); > > + ControlList get(uint32_t sequence); > > + > > + void applyControls(uint32_t sequence); > > + > > +private: > > + class Info > > + { > > + public: > > + Info() > > + : updated(false) > > + { > > + } > > + > > + Info(const ControlValue &v) > > + : value(v), updated(true) > > + { > > + } > > + > > + ControlValue value; > > + bool updated; > > + }; > > Same here, v3 had the following review comment: > > > You could also make this class inherit from ControlValue, to avoid the > > explicit .value below. This one I agree with but "ignored" as to keep the deign as close to StaggerdCtrls as possible, I will do so for v5. > > > + > > + /* \todo: Make the listSize configurable at instance creation time. */ > > + static constexpr int listSize = 16; > > + class ControlRingBuffer : public std::array<Info, listSize> > > + { > > + public: > > + Info &operator[](unsigned int index) > > + { > > + return std::array<Info, listSize>::operator[](index % listSize); > > + } > > + > > + const Info &operator[](unsigned int index) const > > + { > > + return std::array<Info, listSize>::operator[](index % listSize); > > + } > > + }; > > There were further comments in the review of v3 that have also not been > addressed. Once more sorry for the misunderstanding, the comments where, > As these two types are only used once, below in the class declaration, > how about dropping the aliases ? Aliases are useful to avoid typing > types out explicitly in every location, but they have the drawback of > possibly obfuscating the code. I would then rename the above ControlInfo > to Value, as it stores a value. This is mostly done in v4, ControlInfo was renamed INfo and not Value. > > I wonder if we should bundle the delay and array in a single class, to > have a single map (now or on top). I can see pros and cons with both solutions. I don't feel strongly about either one, unless we find a good reason to rework this now I would rather do it on-top later once we have a reason to do so. > > I also wonder if we shouldn't use the integer control ID as the key, > this would simplify DelayedControls::queue() that wouldn't have to look > up the ControlId pointer from the idmap. I'm a bit debated here. Once more I can see pros and cons with both. How about I add a todo for now so we don't forget to reevaluate the design as we integrat it closer in CameraSensor? > > > + > > + bool queue(const ControlList &controls); > > + > > + V4L2Device *device_; > > + std::unordered_map<const ControlId *, unsigned int> delays_; > > + unsigned int maxDelay_; > > + > > + bool running_; > > + uint32_t firstSequence_; > > + > > + uint32_t queueCount_; > > + uint32_t writeCount_; > > + std::unordered_map<const ControlId *, ControlRingBuffer> values_; > > +}; > > + > > +} /* namespace libcamera */ > > + > > +#endif /* __LIBCAMERA_INTERNAL_DELAYED_CONTROLS_H__ */ > > diff --git a/src/libcamera/delayed_controls.cpp b/src/libcamera/delayed_controls.cpp > > new file mode 100644 > > index 0000000000000000..db2e51f8c93c4755 > > --- /dev/null > > +++ b/src/libcamera/delayed_controls.cpp > > @@ -0,0 +1,252 @@ > > +/* SPDX-License-Identifier: LGPL-2.1-or-later */ > > +/* > > + * Copyright (C) 2020, Raspberry Pi (Trading) Ltd. > > + * > > + * delayed_controls.h - Helper to deal with controls that are applied with a delay > > + */ > > + > > +#include "libcamera/internal/delayed_controls.h" > > + > > +#include <libcamera/controls.h> > > + > > +#include "libcamera/internal/log.h" > > +#include "libcamera/internal/v4l2_device.h" > > + > > +/** > > + * \file delayed_controls.h > > + * \brief Helper to deal with controls that are applied with a delay > > s/are applied/take effect/ (matching the documentation of the class > below) > > > + */ > > + > > +namespace libcamera { > > + > > +LOG_DEFINE_CATEGORY(DelayedControls) > > + > > +/** > > + * \class DelayedControls > > + * \brief Helper to deal with controls that take effect with a delay > > + * > > + * Some sensor controls take effect with a delay as the sensor needs time to > > + * adjust, for example exposure and focus. This is a helper class to deal with > > Focus is a different beast. Let's s/focus/analog gain/, that's a better > example. > > > + * such controls and the intended users are pipeline handlers. > > + * > > + * The idea is to extend the concept of the buffer depth of a pipeline the > > + * application needs to maintain to also cover controls. Just as with buffer > > + * depth if the application keeps the number of requests queued above the > > + * control depth the controls are guaranteed to take effect for the correct > > + * request. The control depth is determined by the control with the greatest > > + * delay. > > + */ > > + > > +/** > > + * \brief Construct a DelayedControls instance > > + * \param[in] device The V4L2 device the controls have to be applied to > > + * \param[in] delays Map of the numerical V4L2 control ids to their associated > > + * delays (in frames) > > + * > > + * Only controls specified in \a delays are handled. If it's desired to mix > > + * delayed controls and controls that take effect immediately the immediate > > + * controls must be listed in the \a delays map with a delay value of 0. > > + */ > > +DelayedControls::DelayedControls(V4L2Device *device, > > + const std::unordered_map<uint32_t, unsigned int> &delays) > > + : device_(device), maxDelay_(0) > > +{ > > + const ControlInfoMap &controls = device_->controls(); > > + > > + /* > > + * Create a map of control ids to delays for controls exposed by the > > + * device. > > + */ > > + for (auto const &delay : delays) { > > + auto it = controls.find(delay.first); > > + if (it == controls.end()) { > > + LOG(DelayedControls, Error) > > + << "Delay request for control id " > > + << utils::hex(delay.first) > > + << " but control is not exposed by device " > > + << device_->deviceNode(); > > + continue; > > + } > > + > > + const ControlId *id = it->first; > > + > > + delays_[id] = delay.second; > > + > > + LOG(DelayedControls, Debug) > > + << "Set a delay of " << delays_[id] > > + << " for " << id->name(); > > + > > + maxDelay_ = std::max(maxDelay_, delays_[id]); > > + } > > + > > + reset(); > > +} > > + > > +/** > > + * \brief Reset state machine > > + * > > + * Resets the state machine to a starting position based on control values > > + * retrieved from the device. > > This is a bit terse, and so is the \class documentation. Reading the > documentation only, not the code, it would be quite difficult to use > this class :-S Agreed, it's also a bit by design as much of this design is in flux right now. Once we agree on the overall design I will try to expand the documentation a bit more, we also had this comment in v3, > Once we agree on the review comments (and a new version is posted to > address them if needed), I can help with the documentation if that would > be useful. ;-P > > > + */ > > +void DelayedControls::reset() > > +{ > > + running_ = false; > > + firstSequence_ = 0; > > + queueCount_ = 1; > > + writeCount_ = 0; > > + > > + /* Retrieve control as reported by the device. */ > > + std::vector<uint32_t> ids; > > + for (auto const &delay : delays_) > > + ids.push_back(delay.first->id()); > > + > > + ControlList controls = device_->getControls(ids); > > + > > + /* Seed the control queue with the controls reported by the device. */ > > + values_.clear(); > > + for (const auto &ctrl : controls) { > > + const ControlId *id = device_->controls().idmap().at(ctrl.first); > > + values_[id][0] = Info(ctrl.second); > > + } > > +} > > + > > +/** > > + * \brief Push a set of controls on the queue > > + * \param[in] controls List of controls to add to the device queue > > + * > > + * Push a set of controls to the control queue. This increases the control queue > > + * depth by one. > > + * > > + * \returns true if \a controls are accepted, or false otherwise > > + */ > > +bool DelayedControls::push(const ControlList &controls) > > +{ > > + return queue(controls); > > +} > > + > > +bool DelayedControls::queue(const ControlList &controls) > > +{ > > + /* Copy state from previous frame. */ > > + for (auto &ctrl : values_) { > > + Info &info = ctrl.second[queueCount_]; > > + info.value = values_[ctrl.first][queueCount_ - 1].value; > > + info.updated = false; > > + } > > + > > + /* Update with new controls. */ > > + const ControlIdMap &idmap = device_->controls().idmap(); > > + for (const auto &control : controls) { > > + const auto &it = idmap.find(control.first); > > + if (it == idmap.end()) { > > + LOG(DelayedControls, Warning) > > + << "Unknown control " << control.first; > > + return false; > > + } > > + > > + const ControlId *id = it->second; > > + > > + if (delays_.find(id) == delays_.end()) > > + return false; > > + > > + Info &info = values_[id][queueCount_]; > > + > > + info.value = control.second; > > + info.updated = true; > > + > > + LOG(DelayedControls, Debug) > > + << "Queuing " << id->name() > > + << " to " << info.value.toString() > > + << " at index " << queueCount_; > > + } > > + > > + queueCount_++; > > + > > + return true; > > +} > > + > > +/** > > + * \brief Read back controls in effect at a sequence number > > + * \param[in] sequence The sequence number to get controls for > > + * > > + * Read back what controls where in effect at a specific sequence number. The > > + * history is a ring buffer of 16 entries where new and old values coexist. It's > > + * the callers responsibility to not read too old sequence numbers that have been > > + * pushed out of the history. > > + * > > + * Historic values are evicted by pushing new values onto the queue using > > + * push(). The max history from the current sequence number that yields valid > > + * values are thus 16 minus number of controls pushed. > > + * > > + * \return The controls at \a sequence number > > + */ > > +ControlList DelayedControls::get(uint32_t sequence) > > +{ > > + uint32_t adjustedSeq = sequence - firstSequence_ + 1; > > + unsigned int index = std::max<int>(0, adjustedSeq - maxDelay_); > > + > > + ControlList out(device_->controls()); > > + for (const auto &ctrl : values_) { > > + const ControlId *id = ctrl.first; > > + const Info &info = ctrl.second[index]; > > + > > + out.set(id->id(), info.value); > > + > > + LOG(DelayedControls, Debug) > > + << "Reading " << id->name() > > + << " to " << info.value.toString() > > + << " at index " << index; > > + } > > + > > + return out; > > +} > > + > > +/** > > + * \brief Inform DelayedControls of the start of a new frame > > + * \param[in] sequence Sequence number of the frame that started > > + * > > + * Inform the state machine that a new frame has started and of its sequence > > + * number. Any user of these helpers is responsible to inform the helper about > > + * the start of any frame.This can be connected with ease to the start of a > > + * exposure (SOE) V4L2 event. > > + */ > > +void DelayedControls::applyControls(uint32_t sequence) > > +{ > > + LOG(DelayedControls, Debug) << "frame " << sequence << " started"; > > + > > + if (!running_) { > > + firstSequence_ = sequence; > > + running_ = true; > > + } > > + > > + /* > > + * Create control list peaking ahead in the value queue to ensure > > + * values are set in time to satisfy the sensor delay. > > + */ > > + ControlList out(device_->controls()); > > + for (const auto &ctrl : values_) { > > + const ControlId *id = ctrl.first; > > + unsigned int delayDiff = maxDelay_ - delays_[id]; > > + unsigned int index = std::max<int>(0, writeCount_ - delayDiff); > > A reply to the v3 review would also be appreciated for the issue pointed > our regarding this code. I still can't see the implementation being > correct :-S Comment from v3: > Let's assume a sensor that only a single control, with a delay of 1 > frame. It has been pre-programmed before stream start with an initial > value X with reset(). Then, two requests are queued before the first > SOF, with values A and B. push() is called twice, with A and B. > queueCount is thus 3 (initial value set to 1 in reset(), and increased > twice by push), and the queue contains X, A and B in positions 0, 1 and > 2. > > The first frame start arrives, frameStart(0) is called. writeCount_ is > 0, maxDelay_ is 1, delays_[id] is 1. index will thus be 0, which causes > X to be written to the sensor. > > Isn't that an incorrect behaviour ? Shouldn't B be written instead, > given that the value will take effect in frame 1, which will be captured > in request B ? It may be incorrect behavior in the end. It do match how StaggerdCtrls work and that was my goal to make the transition as non-controversial as possible. I think we need to revisit this at some point when we really know what we want here. I am of course open to doing that work now but I fear the change in behavior may impact the RPi pipeline and delay this series even longer. I also don't know exactly how we want this to behave in the end so I would need input on that. > > > + const Info &info = ctrl.second[index]; > > + > > + if (info.updated) { > > + out.set(id->id(), info.value); > > + LOG(DelayedControls, Debug) > > + << "Setting " << id->name() > > + << " to " << info.value.toString() > > + << " at index " << index; > > + } > > + } > > + > > + writeCount_++; > > + > > + while (writeCount_ >= queueCount_) { > > + LOG(DelayedControls, Debug) > > + << "Queue is empty, auto queue no-op."; > > + queue({}); > > + } > > + > > + device_->setControls(&out); > > +} > > + > > +} /* namespace libcamera */ > > diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build > > index 387d5d88ecae11ad..5a4bf0d7ba4fd231 100644 > > --- a/src/libcamera/meson.build > > +++ b/src/libcamera/meson.build > > @@ -12,6 +12,7 @@ libcamera_sources = files([ > > 'controls.cpp', > > 'control_serializer.cpp', > > 'control_validator.cpp', > > + 'delayed_controls.cpp', > > 'device_enumerator.cpp', > > 'device_enumerator_sysfs.cpp', > > 'event_dispatcher.cpp', > > -- > Regards, > > Laurent Pinchart
HI Niklas and Laurent, On Sat, 16 Jan 2021 at 09:56, Niklas Söderlund < niklas.soderlund@ragnatech.se> wrote: > Hi Laurent, > > Thanks for your feedback. > > First let me apologise for for the misunderstanding on my side regarding > the comments from you in v3 that are not addressed in this version. > After reading your comments in addition to offline discussions my > understanding of the way forward was to, > > - Remove my attempt in v1 and v2 to integrate DelayedControls into > CameraSensor and leave the work for pipeline handlers. The goal was to > not prematurely build a CameraSensor integration which did not suite > all users. This I agreed with and it is done. > > - Likewise my understanding was that we aimed to not make any large > redesigns of the DelayedControls logic as it currently performs in > lockstep with StaggeredCtrls which it is replacing. Once we had > integrated it with RkISP1 replacing the awfully Timeline solution (I'm > allowed to cal it names as I designed it :-P) and the IPU3 to allow a > IPA base to take shape would we start to fix it up in a way that would > solve problems for all pipelines. > > My view was that this would be done on top as I think the integration in > CameraSensor and improving on the DelayedControls is a bit related to > each other and that is why I "ignored" your comments when sending v4. > Judging from your reaction I have misread the situation and for that I'm > sorry. I never meant to ignore your comments, I only did so believing we > both shared the same understanding. > > orz > > I will reply to your comments below and I will copy-paste in your > comments from v3 that you have not already yourself. > > On 2021-01-10 18:07:16 +0200, Laurent Pinchart wrote: > > Hi Niklas, > > > > Thank you for the patch. > > > > In the subject line, s/applies/apply/ > > > > On Tue, Dec 15, 2020 at 01:48:04AM +0100, Niklas Söderlund wrote: > > > Some sensor controls take effect with a delay as the sensor needs time > > > to adjust, for example exposure. Add an optional helper DelayedControls > > > to help pipelines deal with such controls. > > > > > > The idea is to provide a queue of controls towards the V4L2 device and > > > apply individual controls with the specified delay with the aim to get > > > predictable and retrievable control values for any given frame. To do > > > this the queue of controls needs to be at least as deep as the control > > > with the largest delay. > > > > > > The DelayedControls needs to be informed of every start of exposure. > > > This can be emulated but the helper is designed to be used with this > > > event being provide by the kernel thru V4L2 events. > > > > s/thru/through/ > > > > > This helper is based on StaggeredCtrl from the Raspberry Pi pipeline > > > handler but expands on its API. This helpers aims to replace the > > > Raspberry Pi implementations and mimics it behavior perfectly. > > > > > > Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> > > > --- > > > * Changes since v2 > > > - Drop optional argument to reset(). > > > - Update commit message. > > > - Remove usage of Mutex. > > > - Rename frameStart() to applyControls)_. > > > - Rename ControlInfo to Into. > > > - Rename ControlArray to ControlRingBuffer. > > > - Drop ControlsDelays and ControlsValues. > > > - Sort headers. > > > - Rename iterators. > > > - Simplify queueCount_ handeling in reset(). > > > - Add more warnings. > > > - Update documentation. > > > > > > * Changes since v2 > > > - Improve error logic in queue() as suggested by Jean-Michel Hautbois. > > > - s/fistSequence_/firstSequence_/ > > > > > > * Changes since v1 > > > - Correct copyright to reflect work is derived from Raspberry Pi > > > pipeline handler. This was always the intention and was wrong in v1. > > > - Rewrite large parts of the documentation. > > > - Join two loops to one in DelayedControls::DelayedControls() > > > --- > > > include/libcamera/internal/delayed_controls.h | 82 ++++++ > > > src/libcamera/delayed_controls.cpp | 252 ++++++++++++++++++ > > > src/libcamera/meson.build | 1 + > > > 3 files changed, 335 insertions(+) > > > create mode 100644 include/libcamera/internal/delayed_controls.h > > > create mode 100644 src/libcamera/delayed_controls.cpp > > > > > > diff --git a/include/libcamera/internal/delayed_controls.h > b/include/libcamera/internal/delayed_controls.h > > > new file mode 100644 > > > index 0000000000000000..1292b484ec9f53e9 > > > --- /dev/null > > > +++ b/include/libcamera/internal/delayed_controls.h > > > @@ -0,0 +1,82 @@ > > > +/* SPDX-License-Identifier: LGPL-2.1-or-later */ > > > +/* > > > + * Copyright (C) 2020, Raspberry Pi (Trading) Ltd. > > > + * > > > + * delayed_controls.h - Helper to deal with controls that are applied > with a delay > > > + */ > > > +#ifndef __LIBCAMERA_INTERNAL_DELAYED_CONTROLS_H__ > > > +#define __LIBCAMERA_INTERNAL_DELAYED_CONTROLS_H__ > > > + > > > +#include <stdint.h> > > > +#include <unordered_map> > > > + > > > +#include <libcamera/controls.h> > > > + > > > +namespace libcamera { > > > + > > > +class V4L2Device; > > > + > > > +class DelayedControls > > > +{ > > > +public: > > > + DelayedControls(V4L2Device *device, > > > + const std::unordered_map<uint32_t, unsigned int> > &delays); > > > > I've sent the following comment on v3: > > > > > I could imagine this being useful for non-V4L2 devices too. We could > > > instead pass a ControlInfoMap to the constructor, and move the > > > setControls() call to the user of this class. There's no need to do so > > > now (but of course if you think it's a good idea, I'm not asking to > > > delay such rework :-)), but could you capture this in a \todo comment > > > (if you agree with the idea) ? > > > > I see neither a rework of the API, nor a \todo comment. Is that an > > oversight, or did you disagree with the idea ? In the latter case, a > > reply to the review would be nice. > > Unluckily for my situation, this is a comment I do not agree with :-) > > Maybe down the road once we know the design of how we want to integrate > this with CameraSensor and what type of other non-V4L2 devices this > could be useful for I might be see it differently. Foe now I think we > should keep it as is as, at least until we know how to integrate it with > CameraSensor which I think should be the next stop done on top of this > (together with integrating the sensor database). > > > > > > + > > > + void reset(); > > > + > > > + bool push(const ControlList &controls); > > > + ControlList get(uint32_t sequence); > > > + > > > + void applyControls(uint32_t sequence); > > > + > > > +private: > > > + class Info > > > + { > > > + public: > > > + Info() > > > + : updated(false) > > > + { > > > + } > > > + > > > + Info(const ControlValue &v) > > > + : value(v), updated(true) > > > + { > > > + } > > > + > > > + ControlValue value; > > > + bool updated; > > > + }; > > > > Same here, v3 had the following review comment: > > > > > You could also make this class inherit from ControlValue, to avoid the > > > explicit .value below. > > This one I agree with but "ignored" as to keep the deign as close to > StaggerdCtrls as possible, I will do so for v5. > > > > > > + > > > + /* \todo: Make the listSize configurable at instance creation > time. */ > > > + static constexpr int listSize = 16; > > > + class ControlRingBuffer : public std::array<Info, listSize> > > > + { > > > + public: > > > + Info &operator[](unsigned int index) > > > + { > > > + return std::array<Info, > listSize>::operator[](index % listSize); > > > + } > > > + > > > + const Info &operator[](unsigned int index) const > > > + { > > > + return std::array<Info, > listSize>::operator[](index % listSize); > > > + } > > > + }; > > > > There were further comments in the review of v3 that have also not been > > addressed. > > Once more sorry for the misunderstanding, the comments where, > > > As these two types are only used once, below in the class declaration, > > how about dropping the aliases ? Aliases are useful to avoid typing > > types out explicitly in every location, but they have the drawback of > > possibly obfuscating the code. I would then rename the above ControlInfo > > to Value, as it stores a value. > > This is mostly done in v4, ControlInfo was renamed INfo and not Value. > > > > > I wonder if we should bundle the delay and array in a single class, to > > have a single map (now or on top). > > I can see pros and cons with both solutions. I don't feel strongly about > either one, unless we find a good reason to rework this now I would > rather do it on-top later once we have a reason to do so. > > > > > I also wonder if we shouldn't use the integer control ID as the key, > > this would simplify DelayedControls::queue() that wouldn't have to look > > up the ControlId pointer from the idmap. > > I'm a bit debated here. Once more I can see pros and cons with both. How > about I add a todo for now so we don't forget to reevaluate the design > as we integrat it closer in CameraSensor? > > > > > > + > > > + bool queue(const ControlList &controls); > > > + > > > + V4L2Device *device_; > > > + std::unordered_map<const ControlId *, unsigned int> delays_; > > > + unsigned int maxDelay_; > > > + > > > + bool running_; > > > + uint32_t firstSequence_; > > > + > > > + uint32_t queueCount_; > > > + uint32_t writeCount_; > > > + std::unordered_map<const ControlId *, ControlRingBuffer> values_; > > > +}; > > > + > > > +} /* namespace libcamera */ > > > + > > > +#endif /* __LIBCAMERA_INTERNAL_DELAYED_CONTROLS_H__ */ > > > diff --git a/src/libcamera/delayed_controls.cpp > b/src/libcamera/delayed_controls.cpp > > > new file mode 100644 > > > index 0000000000000000..db2e51f8c93c4755 > > > --- /dev/null > > > +++ b/src/libcamera/delayed_controls.cpp > > > @@ -0,0 +1,252 @@ > > > +/* SPDX-License-Identifier: LGPL-2.1-or-later */ > > > +/* > > > + * Copyright (C) 2020, Raspberry Pi (Trading) Ltd. > > > + * > > > + * delayed_controls.h - Helper to deal with controls that are applied > with a delay > > > + */ > > > + > > > +#include "libcamera/internal/delayed_controls.h" > > > + > > > +#include <libcamera/controls.h> > > > + > > > +#include "libcamera/internal/log.h" > > > +#include "libcamera/internal/v4l2_device.h" > > > + > > > +/** > > > + * \file delayed_controls.h > > > + * \brief Helper to deal with controls that are applied with a delay > > > > s/are applied/take effect/ (matching the documentation of the class > > below) > > > > > + */ > > > + > > > +namespace libcamera { > > > + > > > +LOG_DEFINE_CATEGORY(DelayedControls) > > > + > > > +/** > > > + * \class DelayedControls > > > + * \brief Helper to deal with controls that take effect with a delay > > > + * > > > + * Some sensor controls take effect with a delay as the sensor needs > time to > > > + * adjust, for example exposure and focus. This is a helper class to > deal with > > > > Focus is a different beast. Let's s/focus/analog gain/, that's a better > > example. > > > > > + * such controls and the intended users are pipeline handlers. > > > + * > > > + * The idea is to extend the concept of the buffer depth of a > pipeline the > > > + * application needs to maintain to also cover controls. Just as with > buffer > > > + * depth if the application keeps the number of requests queued above > the > > > + * control depth the controls are guaranteed to take effect for the > correct > > > + * request. The control depth is determined by the control with the > greatest > > > + * delay. > > > + */ > > > + > > > +/** > > > + * \brief Construct a DelayedControls instance > > > + * \param[in] device The V4L2 device the controls have to be applied > to > > > + * \param[in] delays Map of the numerical V4L2 control ids to their > associated > > > + * delays (in frames) > > > + * > > > + * Only controls specified in \a delays are handled. If it's desired > to mix > > > + * delayed controls and controls that take effect immediately the > immediate > > > + * controls must be listed in the \a delays map with a delay value of > 0. > > > + */ > > > +DelayedControls::DelayedControls(V4L2Device *device, > > > + const std::unordered_map<uint32_t, > unsigned int> &delays) > > > + : device_(device), maxDelay_(0) > > > +{ > > > + const ControlInfoMap &controls = device_->controls(); > > > + > > > + /* > > > + * Create a map of control ids to delays for controls exposed by > the > > > + * device. > > > + */ > > > + for (auto const &delay : delays) { > > > + auto it = controls.find(delay.first); > > > + if (it == controls.end()) { > > > + LOG(DelayedControls, Error) > > > + << "Delay request for control id " > > > + << utils::hex(delay.first) > > > + << " but control is not exposed by device " > > > + << device_->deviceNode(); > > > + continue; > > > + } > > > + > > > + const ControlId *id = it->first; > > > + > > > + delays_[id] = delay.second; > > > + > > > + LOG(DelayedControls, Debug) > > > + << "Set a delay of " << delays_[id] > > > + << " for " << id->name(); > > > + > > > + maxDelay_ = std::max(maxDelay_, delays_[id]); > > > + } > > > + > > > + reset(); > > > +} > > > + > > > +/** > > > + * \brief Reset state machine > > > + * > > > + * Resets the state machine to a starting position based on control > values > > > + * retrieved from the device. > > > > This is a bit terse, and so is the \class documentation. Reading the > > documentation only, not the code, it would be quite difficult to use > > this class :-S > > Agreed, it's also a bit by design as much of this design is in flux > right now. Once we agree on the overall design I will try to expand the > documentation a bit more, we also had this comment in v3, > > > Once we agree on the review comments (and a new version is posted to > > address them if needed), I can help with the documentation if that would > > be useful. > > ;-P > > > > > > + */ > > > +void DelayedControls::reset() > > > +{ > > > + running_ = false; > > > + firstSequence_ = 0; > > > + queueCount_ = 1; > > > + writeCount_ = 0; > > > + > > > + /* Retrieve control as reported by the device. */ > > > + std::vector<uint32_t> ids; > > > + for (auto const &delay : delays_) > > > + ids.push_back(delay.first->id()); > > > + > > > + ControlList controls = device_->getControls(ids); > > > + > > > + /* Seed the control queue with the controls reported by the > device. */ > > > + values_.clear(); > > > + for (const auto &ctrl : controls) { > > > + const ControlId *id = > device_->controls().idmap().at(ctrl.first); > > > + values_[id][0] = Info(ctrl.second); > > > + } > > > +} > > > + > > > +/** > > > + * \brief Push a set of controls on the queue > > > + * \param[in] controls List of controls to add to the device queue > > > + * > > > + * Push a set of controls to the control queue. This increases the > control queue > > > + * depth by one. > > > + * > > > + * \returns true if \a controls are accepted, or false otherwise > > > + */ > > > +bool DelayedControls::push(const ControlList &controls) > > > +{ > > > + return queue(controls); > > > +} > > > + > > > +bool DelayedControls::queue(const ControlList &controls) > > > +{ > > > + /* Copy state from previous frame. */ > > > + for (auto &ctrl : values_) { > > > + Info &info = ctrl.second[queueCount_]; > > > + info.value = values_[ctrl.first][queueCount_ - 1].value; > > > + info.updated = false; > > > + } > > > + > > > + /* Update with new controls. */ > > > + const ControlIdMap &idmap = device_->controls().idmap(); > > > + for (const auto &control : controls) { > > > + const auto &it = idmap.find(control.first); > > > + if (it == idmap.end()) { > > > + LOG(DelayedControls, Warning) > > > + << "Unknown control " << control.first; > > > + return false; > > > + } > > > + > > > + const ControlId *id = it->second; > > > + > > > + if (delays_.find(id) == delays_.end()) > > > + return false; > > > + > > > + Info &info = values_[id][queueCount_]; > > > + > > > + info.value = control.second; > > > + info.updated = true; > > > + > > > + LOG(DelayedControls, Debug) > > > + << "Queuing " << id->name() > > > + << " to " << info.value.toString() > > > + << " at index " << queueCount_; > > > + } > > > + > > > + queueCount_++; > > > + > > > + return true; > > > +} > > > + > > > +/** > > > + * \brief Read back controls in effect at a sequence number > > > + * \param[in] sequence The sequence number to get controls for > > > + * > > > + * Read back what controls where in effect at a specific sequence > number. The > > > + * history is a ring buffer of 16 entries where new and old values > coexist. It's > > > + * the callers responsibility to not read too old sequence numbers > that have been > > > + * pushed out of the history. > > > + * > > > + * Historic values are evicted by pushing new values onto the queue > using > > > + * push(). The max history from the current sequence number that > yields valid > > > + * values are thus 16 minus number of controls pushed. > > > + * > > > + * \return The controls at \a sequence number > > > + */ > > > +ControlList DelayedControls::get(uint32_t sequence) > > > +{ > > > + uint32_t adjustedSeq = sequence - firstSequence_ + 1; > > > + unsigned int index = std::max<int>(0, adjustedSeq - maxDelay_); > > > + > > > + ControlList out(device_->controls()); > > > + for (const auto &ctrl : values_) { > > > + const ControlId *id = ctrl.first; > > > + const Info &info = ctrl.second[index]; > > > + > > > + out.set(id->id(), info.value); > > > + > > > + LOG(DelayedControls, Debug) > > > + << "Reading " << id->name() > > > + << " to " << info.value.toString() > > > + << " at index " << index; > > > + } > > > + > > > + return out; > > > +} > > > + > > > +/** > > > + * \brief Inform DelayedControls of the start of a new frame > > > + * \param[in] sequence Sequence number of the frame that started > > > + * > > > + * Inform the state machine that a new frame has started and of its > sequence > > > + * number. Any user of these helpers is responsible to inform the > helper about > > > + * the start of any frame.This can be connected with ease to the > start of a > > > + * exposure (SOE) V4L2 event. > > > + */ > > > +void DelayedControls::applyControls(uint32_t sequence) > > > +{ > > > + LOG(DelayedControls, Debug) << "frame " << sequence << " started"; > > > + > > > + if (!running_) { > > > + firstSequence_ = sequence; > > > + running_ = true; > > > + } > > > + > > > + /* > > > + * Create control list peaking ahead in the value queue to ensure > > > + * values are set in time to satisfy the sensor delay. > > > + */ > > > + ControlList out(device_->controls()); > > > + for (const auto &ctrl : values_) { > > > + const ControlId *id = ctrl.first; > > > + unsigned int delayDiff = maxDelay_ - delays_[id]; > > > + unsigned int index = std::max<int>(0, writeCount_ - > delayDiff); > > > > A reply to the v3 review would also be appreciated for the issue pointed > > our regarding this code. I still can't see the implementation being > > correct :-S > > Comment from v3: > > > Let's assume a sensor that only a single control, with a delay of 1 > > frame. It has been pre-programmed before stream start with an initial > > value X with reset(). Then, two requests are queued before the first > > SOF, with values A and B. push() is called twice, with A and B. > > queueCount is thus 3 (initial value set to 1 in reset(), and increased > > twice by push), and the queue contains X, A and B in positions 0, 1 and > > 2. > > > > The first frame start arrives, frameStart(0) is called. writeCount_ is > > 0, maxDelay_ is 1, delays_[id] is 1. index will thus be 0, which causes > > X to be written to the sensor. > > > > Isn't that an incorrect behaviour ? Shouldn't B be written instead, > > given that the value will take effect in frame 1, which will be captured > > in request B ? > > It may be incorrect behavior in the end. It do match how StaggerdCtrls > work and that was my goal to make the transition as non-controversial as > possible. I think we need to revisit this at some point when we really > know what we want here. I am of course open to doing that work now but I > fear the change in behavior may impact the RPi pipeline and delay this > series even longer. I also don't know exactly how we want this to behave > in the end so I would need input on that. > With regards to the above usage described by Laurent - I should clarify that staggered ctrl essentially has a storage depth of 1. By that I mean, if you attempt to queue controls via multiple calls to set(), only the last call to set() is taken into account. This is the same if the pipeline is streaming or not. Only once we write the last provided controls to the sensor through write() can any new controls be set() to be applied in future frames. To go into a bit more detail, the setCount index into the control array is only incremented in write(), and not on each call to set(). One of the key differences with DelayedCtrl is you can queue up controls with multiple calls to push(). This is something that the Raspberry Pi IPA never ever does, so I did not add support for it in staggered ctrl. So any fixes to the queuing behavior should not affect the Raspberry Pi stack.... I think :-) Regards, Naush > > > > > > + const Info &info = ctrl.second[index]; > > > + > > > + if (info.updated) { > > > + out.set(id->id(), info.value); > > > + LOG(DelayedControls, Debug) > > > + << "Setting " << id->name() > > > + << " to " << info.value.toString() > > > + << " at index " << index; > > > + } > > > + } > > > + > > > + writeCount_++; > > > + > > > + while (writeCount_ >= queueCount_) { > > > + LOG(DelayedControls, Debug) > > > + << "Queue is empty, auto queue no-op."; > > > + queue({}); > > > + } > > > + > > > + device_->setControls(&out); > > > +} > > > + > > > +} /* namespace libcamera */ > > > diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build > > > index 387d5d88ecae11ad..5a4bf0d7ba4fd231 100644 > > > --- a/src/libcamera/meson.build > > > +++ b/src/libcamera/meson.build > > > @@ -12,6 +12,7 @@ libcamera_sources = files([ > > > 'controls.cpp', > > > 'control_serializer.cpp', > > > 'control_validator.cpp', > > > + 'delayed_controls.cpp', > > > 'device_enumerator.cpp', > > > 'device_enumerator_sysfs.cpp', > > > 'event_dispatcher.cpp', > > > > -- > > Regards, > > > > Laurent Pinchart > > -- > Regards, > Niklas Söderlund >
diff --git a/include/libcamera/internal/delayed_controls.h b/include/libcamera/internal/delayed_controls.h new file mode 100644 index 0000000000000000..1292b484ec9f53e9 --- /dev/null +++ b/include/libcamera/internal/delayed_controls.h @@ -0,0 +1,82 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2020, Raspberry Pi (Trading) Ltd. + * + * delayed_controls.h - Helper to deal with controls that are applied with a delay + */ +#ifndef __LIBCAMERA_INTERNAL_DELAYED_CONTROLS_H__ +#define __LIBCAMERA_INTERNAL_DELAYED_CONTROLS_H__ + +#include <stdint.h> +#include <unordered_map> + +#include <libcamera/controls.h> + +namespace libcamera { + +class V4L2Device; + +class DelayedControls +{ +public: + DelayedControls(V4L2Device *device, + const std::unordered_map<uint32_t, unsigned int> &delays); + + void reset(); + + bool push(const ControlList &controls); + ControlList get(uint32_t sequence); + + void applyControls(uint32_t sequence); + +private: + class Info + { + public: + Info() + : updated(false) + { + } + + Info(const ControlValue &v) + : value(v), updated(true) + { + } + + ControlValue value; + bool updated; + }; + + /* \todo: Make the listSize configurable at instance creation time. */ + static constexpr int listSize = 16; + class ControlRingBuffer : public std::array<Info, listSize> + { + public: + Info &operator[](unsigned int index) + { + return std::array<Info, listSize>::operator[](index % listSize); + } + + const Info &operator[](unsigned int index) const + { + return std::array<Info, listSize>::operator[](index % listSize); + } + }; + + bool queue(const ControlList &controls); + + V4L2Device *device_; + std::unordered_map<const ControlId *, unsigned int> delays_; + unsigned int maxDelay_; + + bool running_; + uint32_t firstSequence_; + + uint32_t queueCount_; + uint32_t writeCount_; + std::unordered_map<const ControlId *, ControlRingBuffer> values_; +}; + +} /* namespace libcamera */ + +#endif /* __LIBCAMERA_INTERNAL_DELAYED_CONTROLS_H__ */ diff --git a/src/libcamera/delayed_controls.cpp b/src/libcamera/delayed_controls.cpp new file mode 100644 index 0000000000000000..db2e51f8c93c4755 --- /dev/null +++ b/src/libcamera/delayed_controls.cpp @@ -0,0 +1,252 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2020, Raspberry Pi (Trading) Ltd. + * + * delayed_controls.h - Helper to deal with controls that are applied with a delay + */ + +#include "libcamera/internal/delayed_controls.h" + +#include <libcamera/controls.h> + +#include "libcamera/internal/log.h" +#include "libcamera/internal/v4l2_device.h" + +/** + * \file delayed_controls.h + * \brief Helper to deal with controls that are applied with a delay + */ + +namespace libcamera { + +LOG_DEFINE_CATEGORY(DelayedControls) + +/** + * \class DelayedControls + * \brief Helper to deal with controls that take effect with a delay + * + * Some sensor controls take effect with a delay as the sensor needs time to + * adjust, for example exposure and focus. This is a helper class to deal with + * such controls and the intended users are pipeline handlers. + * + * The idea is to extend the concept of the buffer depth of a pipeline the + * application needs to maintain to also cover controls. Just as with buffer + * depth if the application keeps the number of requests queued above the + * control depth the controls are guaranteed to take effect for the correct + * request. The control depth is determined by the control with the greatest + * delay. + */ + +/** + * \brief Construct a DelayedControls instance + * \param[in] device The V4L2 device the controls have to be applied to + * \param[in] delays Map of the numerical V4L2 control ids to their associated + * delays (in frames) + * + * Only controls specified in \a delays are handled. If it's desired to mix + * delayed controls and controls that take effect immediately the immediate + * controls must be listed in the \a delays map with a delay value of 0. + */ +DelayedControls::DelayedControls(V4L2Device *device, + const std::unordered_map<uint32_t, unsigned int> &delays) + : device_(device), maxDelay_(0) +{ + const ControlInfoMap &controls = device_->controls(); + + /* + * Create a map of control ids to delays for controls exposed by the + * device. + */ + for (auto const &delay : delays) { + auto it = controls.find(delay.first); + if (it == controls.end()) { + LOG(DelayedControls, Error) + << "Delay request for control id " + << utils::hex(delay.first) + << " but control is not exposed by device " + << device_->deviceNode(); + continue; + } + + const ControlId *id = it->first; + + delays_[id] = delay.second; + + LOG(DelayedControls, Debug) + << "Set a delay of " << delays_[id] + << " for " << id->name(); + + maxDelay_ = std::max(maxDelay_, delays_[id]); + } + + reset(); +} + +/** + * \brief Reset state machine + * + * Resets the state machine to a starting position based on control values + * retrieved from the device. + */ +void DelayedControls::reset() +{ + running_ = false; + firstSequence_ = 0; + queueCount_ = 1; + writeCount_ = 0; + + /* Retrieve control as reported by the device. */ + std::vector<uint32_t> ids; + for (auto const &delay : delays_) + ids.push_back(delay.first->id()); + + ControlList controls = device_->getControls(ids); + + /* Seed the control queue with the controls reported by the device. */ + values_.clear(); + for (const auto &ctrl : controls) { + const ControlId *id = device_->controls().idmap().at(ctrl.first); + values_[id][0] = Info(ctrl.second); + } +} + +/** + * \brief Push a set of controls on the queue + * \param[in] controls List of controls to add to the device queue + * + * Push a set of controls to the control queue. This increases the control queue + * depth by one. + * + * \returns true if \a controls are accepted, or false otherwise + */ +bool DelayedControls::push(const ControlList &controls) +{ + return queue(controls); +} + +bool DelayedControls::queue(const ControlList &controls) +{ + /* Copy state from previous frame. */ + for (auto &ctrl : values_) { + Info &info = ctrl.second[queueCount_]; + info.value = values_[ctrl.first][queueCount_ - 1].value; + info.updated = false; + } + + /* Update with new controls. */ + const ControlIdMap &idmap = device_->controls().idmap(); + for (const auto &control : controls) { + const auto &it = idmap.find(control.first); + if (it == idmap.end()) { + LOG(DelayedControls, Warning) + << "Unknown control " << control.first; + return false; + } + + const ControlId *id = it->second; + + if (delays_.find(id) == delays_.end()) + return false; + + Info &info = values_[id][queueCount_]; + + info.value = control.second; + info.updated = true; + + LOG(DelayedControls, Debug) + << "Queuing " << id->name() + << " to " << info.value.toString() + << " at index " << queueCount_; + } + + queueCount_++; + + return true; +} + +/** + * \brief Read back controls in effect at a sequence number + * \param[in] sequence The sequence number to get controls for + * + * Read back what controls where in effect at a specific sequence number. The + * history is a ring buffer of 16 entries where new and old values coexist. It's + * the callers responsibility to not read too old sequence numbers that have been + * pushed out of the history. + * + * Historic values are evicted by pushing new values onto the queue using + * push(). The max history from the current sequence number that yields valid + * values are thus 16 minus number of controls pushed. + * + * \return The controls at \a sequence number + */ +ControlList DelayedControls::get(uint32_t sequence) +{ + uint32_t adjustedSeq = sequence - firstSequence_ + 1; + unsigned int index = std::max<int>(0, adjustedSeq - maxDelay_); + + ControlList out(device_->controls()); + for (const auto &ctrl : values_) { + const ControlId *id = ctrl.first; + const Info &info = ctrl.second[index]; + + out.set(id->id(), info.value); + + LOG(DelayedControls, Debug) + << "Reading " << id->name() + << " to " << info.value.toString() + << " at index " << index; + } + + return out; +} + +/** + * \brief Inform DelayedControls of the start of a new frame + * \param[in] sequence Sequence number of the frame that started + * + * Inform the state machine that a new frame has started and of its sequence + * number. Any user of these helpers is responsible to inform the helper about + * the start of any frame.This can be connected with ease to the start of a + * exposure (SOE) V4L2 event. + */ +void DelayedControls::applyControls(uint32_t sequence) +{ + LOG(DelayedControls, Debug) << "frame " << sequence << " started"; + + if (!running_) { + firstSequence_ = sequence; + running_ = true; + } + + /* + * Create control list peaking ahead in the value queue to ensure + * values are set in time to satisfy the sensor delay. + */ + ControlList out(device_->controls()); + for (const auto &ctrl : values_) { + const ControlId *id = ctrl.first; + unsigned int delayDiff = maxDelay_ - delays_[id]; + unsigned int index = std::max<int>(0, writeCount_ - delayDiff); + const Info &info = ctrl.second[index]; + + if (info.updated) { + out.set(id->id(), info.value); + LOG(DelayedControls, Debug) + << "Setting " << id->name() + << " to " << info.value.toString() + << " at index " << index; + } + } + + writeCount_++; + + while (writeCount_ >= queueCount_) { + LOG(DelayedControls, Debug) + << "Queue is empty, auto queue no-op."; + queue({}); + } + + device_->setControls(&out); +} + +} /* namespace libcamera */ diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build index 387d5d88ecae11ad..5a4bf0d7ba4fd231 100644 --- a/src/libcamera/meson.build +++ b/src/libcamera/meson.build @@ -12,6 +12,7 @@ libcamera_sources = files([ 'controls.cpp', 'control_serializer.cpp', 'control_validator.cpp', + 'delayed_controls.cpp', 'device_enumerator.cpp', 'device_enumerator_sysfs.cpp', 'event_dispatcher.cpp',
Some sensor controls take effect with a delay as the sensor needs time to adjust, for example exposure. Add an optional helper DelayedControls to help pipelines deal with such controls. The idea is to provide a queue of controls towards the V4L2 device and apply individual controls with the specified delay with the aim to get predictable and retrievable control values for any given frame. To do this the queue of controls needs to be at least as deep as the control with the largest delay. The DelayedControls needs to be informed of every start of exposure. This can be emulated but the helper is designed to be used with this event being provide by the kernel thru V4L2 events. This helper is based on StaggeredCtrl from the Raspberry Pi pipeline handler but expands on its API. This helpers aims to replace the Raspberry Pi implementations and mimics it behavior perfectly. Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> --- * Changes since v2 - Drop optional argument to reset(). - Update commit message. - Remove usage of Mutex. - Rename frameStart() to applyControls)_. - Rename ControlInfo to Into. - Rename ControlArray to ControlRingBuffer. - Drop ControlsDelays and ControlsValues. - Sort headers. - Rename iterators. - Simplify queueCount_ handeling in reset(). - Add more warnings. - Update documentation. * Changes since v2 - Improve error logic in queue() as suggested by Jean-Michel Hautbois. - s/fistSequence_/firstSequence_/ * Changes since v1 - Correct copyright to reflect work is derived from Raspberry Pi pipeline handler. This was always the intention and was wrong in v1. - Rewrite large parts of the documentation. - Join two loops to one in DelayedControls::DelayedControls() --- include/libcamera/internal/delayed_controls.h | 82 ++++++ src/libcamera/delayed_controls.cpp | 252 ++++++++++++++++++ src/libcamera/meson.build | 1 + 3 files changed, 335 insertions(+) create mode 100644 include/libcamera/internal/delayed_controls.h create mode 100644 src/libcamera/delayed_controls.cpp