| Message ID | 20261117-sklug-digital-gain-support-v1-v1-9-f0aa342ca877@ideasonboard.com |
|---|---|
| State | New |
| Headers | show |
| Series |
|
| Related | show |
Hi 2026. 11. 17. 9:47 keltezéssel, Stefan Klug írta: > Some algorithms have ordering dependencies on each other. As an example > on rkisp1 the Compress algorithm is used to implement digital gain. So during > computeParams(), Agc::prepare() must be called before > Compress::prepare(), so that the Agc algorithm can store the digital gain > in the frame context for Compress to pick it up. > > To make matters worse, the required order during configure() time can be > inverse. In the example above, Compress::configure() should be called > first, so that Agc::configure() can check the session if Compress (and > therefore digital gain) is available. > > This patch implements a simple, yet effective dependency scheme where an > algorithm can specify a list of dependencies. A dependency consists of > an algorithm step (Init, Configure, Queue, Prepare, Process), a target > and the type which can either be "Exists" meaning that the corresponding > algorithm must be in the tuning file and "Before" which means that the > target must be run first. > > Each algorithm can provide a list of dependencies through the newly > added dependencies() function. > > This patch only implements the necessary infrastructure for rkisp1. > Necessary dependencies will be added in upcoming patches. > > Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com> > --- > src/ipa/libipa/algorithm.cpp | 22 ++++++ > src/ipa/libipa/algorithm.h | 48 ++++++++++++ > src/ipa/libipa/module.h | 178 +++++++++++++++++++++++++++++++++++-------- > src/ipa/rkisp1/rkisp1.cpp | 16 ++-- > 4 files changed, 225 insertions(+), 39 deletions(-) > > diff --git a/src/ipa/libipa/algorithm.cpp b/src/ipa/libipa/algorithm.cpp > index 757ce3519652..20ead51bf7e0 100644 > --- a/src/ipa/libipa/algorithm.cpp > +++ b/src/ipa/libipa/algorithm.cpp > @@ -16,6 +16,28 @@ namespace libcamera { > > namespace ipa { > > +std::ostream &operator<<(std::basic_ostream<char> &out, libcamera::ipa::AlgorithmStep step) > +{ > + switch (step) { > + case AlgorithmStep::Init: > + out << "Init"; > + break; > + case AlgorithmStep::Configure: > + out << "Configure"; > + break; > + case AlgorithmStep::Queue: > + out << "Queue"; > + break; > + case AlgorithmStep::Prepare: > + out << "Prepare"; > + break; > + case AlgorithmStep::Process: > + out << "Process"; > + break; > + } > + return out; > +} > + > /** > * \class Algorithm > * \brief The base class for all IPA algorithms > diff --git a/src/ipa/libipa/algorithm.h b/src/ipa/libipa/algorithm.h > index b47765700317..97d7130a789d 100644 > --- a/src/ipa/libipa/algorithm.h > +++ b/src/ipa/libipa/algorithm.h > @@ -7,6 +7,7 @@ > #pragma once > > #include <memory> > +#include <set> > #include <stdint.h> > #include <string> > > @@ -18,6 +19,48 @@ class ValueNode; > > namespace ipa { > > +enum class AlgorithmStep { > + Init = 0, > + Configure, > + Queue, > + Prepare, > + Process > +}; > + > +std::ostream &operator<<(std::basic_ostream<char> &out, libcamera::ipa::AlgorithmStep step); std::ostream &out ? > + > +const std::array<AlgorithmStep, 5> kAlgorithmAllSteps = { inline const > + AlgorithmStep::Init, > + AlgorithmStep::Configure, > + AlgorithmStep::Queue, > + AlgorithmStep::Prepare, > + AlgorithmStep::Process > +}; > + > +struct AlgorithmDependency { > + enum Type { > + /* Step on target needs to come first */ > + Before = 0x01, > + /* Target must exist */ > + Exist = 0x02 > + }; > + > + std::string target; > + AlgorithmStep step; > + unsigned type = 0; libcamera::Flags<...> type ? > + > + bool operator<(const AlgorithmDependency &other) const Is there any particular reason this needs to be comparable? Is using an `std::set` over a simple array useful? > + { > + return (target < other.target) || > + ((target == other.target) && > + ((step < other.step) || > + ((step == other.step) && > + (type < other.type)))); > + } > +}; > + > +const std::set<AlgorithmDependency> kNoDependencies = {}; inline const > + > template<typename _Module> > class AlgorithmFactoryBase; > > @@ -63,6 +106,11 @@ public: > { > } > > + virtual const std::set<AlgorithmDependency> &dependencies() const > + { > + return kNoDependencies; > + } > + > const std::string &name() const > { > return name_; > diff --git a/src/ipa/libipa/module.h b/src/ipa/libipa/module.h > index 5644458bdf3d..cb2176dc37a6 100644 > --- a/src/ipa/libipa/module.h > +++ b/src/ipa/libipa/module.h > @@ -7,8 +7,10 @@ > > #pragma once > > +#include <cerrno> > #include <list> > #include <memory> > +#include <set> > #include <string> > #include <vector> > > @@ -35,6 +37,7 @@ public: > using Config = _Config; > using Params = _Params; > using Stats = _Stats; > + using AlgorithmT = Algorithm<Module>; > > virtual ~Module() {} > > @@ -43,25 +46,76 @@ public: > return algorithms_; > } > > + const std::vector<Algorithm<Module> *> &algorithmsOrderedForStep(AlgorithmStep step) const > + { > + return algorithmsOrderedForStep_.at(step); > + } > + > int createAlgorithms(Context &context, const ValueNode &algorithms) > { > const auto &list = algorithms.asList(); > + std::map<AlgorithmT *, const ValueNode *> dataMap; > + > + utils::ScopeExitActions actions; > + > + actions += [this]() { > + clearAlgorithms(); > + }; utils::scope_exit algoGuard([&] { clearAlgorithms(); }); > > for (const auto &[i, algo] : utils::enumerate(list)) { > if (!algo.isDictionary()) { > LOG(IPAModuleAlgo, Error) > << "Invalid tuning data syntax for algorithm " << i; > - algorithms_.clear(); > return -EINVAL; > } > > - int ret = createAlgorithm(context, algo); > + const auto &[name, algoData] = *algo.asDict().begin(); > + /* > + * Optionally, algorithms can be disabled via the tuning file > + * by including enabled: false as a parameter within the > + * algorithm tuning data. This is not an error, so we return 0. > + */ > + if (!algoData["enabled"].get<bool>(true)) { > + LOG(IPAModuleAlgo, Info) > + << "Algorithm '" << name << "' disabled via tuning file"; > + return 0; > + } > + > + std::unique_ptr<Algorithm<Module>> instance = createAlgorithm(name); > + if (!instance) { > + LOG(IPAModuleAlgo, Error) > + << "Algorithm '" << name << "' not found"; > + return -EINVAL; > + } > + > + algorithms_.push_back(std::move(instance)); > + > + LOG(IPAModuleAlgo, Debug) > + << "Instantiated algorithm '" << name << "'"; > + > + dataMap[algorithms_.back().get()] = &algoData; > + } > + > + for (auto step : kAlgorithmAllSteps) { > + std::vector<AlgorithmT *> algos; > + int ret = createOrderedList(algos, step); > + if (ret) > + return ret; > + > + algorithmsOrderedForStep_[step] = std::move(algos); > + } > + > + for (const auto &a : algorithmsOrderedForStep(AlgorithmStep::Init)) { > + int ret = a->init(context, *dataMap[a]); > if (ret) { > - algorithms_.clear(); > + LOG(IPAModuleAlgo, Error) > + << "Algorithm failed to initialize"; > return ret; > } > } > > + actions.release(); > + > return 0; > } > > @@ -71,42 +125,101 @@ public: > } > > private: > - int createAlgorithm(Context &context, const ValueNode &data) > + int createOrderedList(std::vector<Algorithm<Module> *> &orderedList, > + AlgorithmStep step) > { > - const auto &[name, algoData] = *data.asDict().begin(); > + using Algo = Algorithm<Module>; > + std::set<Algo *> handledAlgorithms; > > - /* > - * Optionally, algorithms can be disabled via the tuning file > - * by including enabled: false as a parameter within the > - * algorithm tuning data. This is not an error, so we return 0. > - */ > - if (!algoData["enabled"].get<bool>(true)) { > - LOG(IPAModuleAlgo, Info) > - << "Algorithm '" << name << "' disabled via tuning file"; > - return 0; > + orderedList.clear(); > + > + for (const auto &a : algorithms()) { > + std::vector<std::string> resolutionStack; > + int ret = addToOrderedList(a.get(), orderedList, > + resolutionStack, > + step); > + if (ret) > + return ret; > } > > - std::unique_ptr<Algorithm<Module>> algo = createAlgorithm(name); > - if (!algo) { > - LOG(IPAModuleAlgo, Error) > - << "Algorithm '" << name << "' not found"; > - return -EINVAL; > - } > - > - int ret = algo->init(context, algoData); > - if (ret) { > - LOG(IPAModuleAlgo, Error) > - << "Algorithm '" << name << "' failed to initialize"; > - return ret; > - } > - > - LOG(IPAModuleAlgo, Debug) > - << "Instantiated algorithm '" << name << "'"; > - > - algorithms_.push_back(std::move(algo)); > return 0; > } > > + int addToOrderedList(Algorithm<Module> *algo, > + std::vector<Algorithm<Module> *> &orderedList, > + std::vector<std::string> &resolutionStack, > + AlgorithmStep step) > + { > + /* nothing to do if it was added already due to dependency resolution */ > + if (std::find(orderedList.begin(), orderedList.end(), algo) != orderedList.end()) > + return 0; > + > + auto &deps = algo->dependencies(); > + > + /* First add the dependent ones */ > + for (auto &dep : deps) { > + if (dep.step != step) > + continue; > + > + if (!(dep.type & (AlgorithmDependency::Exist | AlgorithmDependency::Before))) { > + LOG(IPAModuleAlgo, Error) > + << "Unknown dependency flags '" > + << dep.type > + << "' on dependency '" << dep.target; > + return -EINVAL; > + } > + > + if (std::find(resolutionStack.begin(), resolutionStack.end(), dep.target) != resolutionStack.end()) { > + LOG(IPAModuleAlgo, Info) > + << "Circular dependency detected: " > + << dep.target; > + return -EINVAL; > + } > + > + /* Find the target */ > + auto iter = std::find_if(algorithms_.begin(), algorithms_.end(), > + [&](auto &a) { return a->name() == dep.target; }); > + if (iter == algorithms_.end()) { > + if (dep.type & AlgorithmDependency::Exist) { > + LOG(IPAModuleAlgo, Error) > + << "Algorithm '" << dep.target > + << "' is listed as dependency but not instantiated"; > + return -EINVAL; > + } > + > + if (dep.type & AlgorithmDependency::Before) { > + LOG(IPAModuleAlgo, Debug) > + << "Algorithm '" << dep.target > + << "' is listed as optional dependency but not instantiated"; > + continue; > + } > + > + ASSERT(false); > + } > + > + Algorithm<Module> *dependency = iter->get(); > + if (std::find(orderedList.begin(), orderedList.end(), dependency) == orderedList.end()) { > + resolutionStack.push_back(dep.target); > + int ret = addToOrderedList(dependency, > + orderedList, > + resolutionStack, > + step); > + if (ret) > + return ret; > + } > + } > + > + orderedList.push_back(algo); > + LOG(IPAModuleAlgo, Debug) << "Step " << step << " add algo " << algo->name(); > + return 0; > + } I'd like to propose a somewhat different implementation here, specifically, the non-recursive implementation of Kahn's algorithm for topological sorting: https://en.wikipedia.org/wiki/Topological_sorting#Kahn's_algorithm I personally like this a lot because it is a "well-known" algorithm (for some definition of "well-known"), it's not recursive, and I believe the underlying idea is very easy to understand. It would also trivially allow us to support dependencies in both directions (before and after). I have made an attempt to implement a prototype, you can find it at the end of this email. > + > + void clearAlgorithms() > + { > + algorithmsOrderedForStep_.clear(); > + algorithms_.clear(); > + } > + > static std::unique_ptr<Algorithm<Module>> createAlgorithm(const std::string &name) > { > for (const AlgorithmFactoryBase<Module> *factory : factories()) { > @@ -129,6 +242,7 @@ private: > } > > std::list<std::unique_ptr<Algorithm<Module>>> algorithms_; > + std::map<AlgorithmStep, std::vector<AlgorithmT *>> algorithmsOrderedForStep_; I'd greatly prefer if it was an `std::array` indexed by `AlgorithmStep`; there should be no reason for the map. > }; > > } /* namespace ipa */ > diff --git a/src/ipa/rkisp1/rkisp1.cpp b/src/ipa/rkisp1/rkisp1.cpp > index cefaf40aee63..add2e45cfa36 100644 > --- a/src/ipa/rkisp1/rkisp1.cpp > +++ b/src/ipa/rkisp1/rkisp1.cpp > @@ -30,6 +30,7 @@ > > #include "algorithms/algorithm.h" > #include "libipa/agc.h" > +#include "libipa/algorithm.h" > > #include "ipa_context.h" > #include "params.h" > @@ -237,8 +238,8 @@ int IPARkISP1::configure(const IPAConfigInfo &ipaConfig, > return format.colourEncoding == PixelFormatInfo::ColourEncodingRAW; > }); > > - for (const auto &a : algorithms()) { > - Algorithm *algo = static_cast<Algorithm *>(a.get()); > + for (const auto &a : algorithmsOrderedForStep(AlgorithmStep::Configure)) { > + Algorithm *algo = static_cast<Algorithm *>(a); > > /* Disable algorithms that don't support raw formats. */ > algo->disabled_ = context_.configuration.raw && !algo->supportsRaw_; > @@ -290,8 +291,8 @@ void IPARkISP1::queueRequest(const uint32_t frame, const ControlList &controls) > IPAFrameContext &frameContext = context_.frameContexts.alloc(frame); > context_.debugMetadata.enableByControl(controls); > > - for (const auto &a : algorithms()) { > - Algorithm *algo = static_cast<Algorithm *>(a.get()); > + for (const auto &a : algorithmsOrderedForStep(AlgorithmStep::Queue)) { > + Algorithm *algo = static_cast<Algorithm *>(a); > if (algo->disabled_) > continue; > algo->queueRequest(context_, frame, frameContext, controls); > @@ -305,8 +306,9 @@ void IPARkISP1::computeParams(const uint32_t frame, const uint32_t bufferId) > RkISP1Params params(context_.configuration.paramFormat, > mappedBuffers_.at(bufferId).planes()[0]); > > - for (const auto &algo : algorithms()) > + for (const auto &algo : algorithmsOrderedForStep(AlgorithmStep::Prepare)) { > algo->prepare(context_, frame, frameContext, ¶ms); > + } > > paramsComputed.emit(frame, params.bytesused()); > } > @@ -330,8 +332,8 @@ void IPARkISP1::processStats(const uint32_t frame, const uint32_t bufferId, > > ControlList metadata(controls::controls); > > - for (const auto &a : algorithms()) { > - Algorithm *algo = static_cast<Algorithm *>(a.get()); > + for (const auto &a : algorithmsOrderedForStep(AlgorithmStep::Process)) { > + Algorithm *algo = static_cast<Algorithm *>(a); > if (algo->disabled_) > continue; > algo->process(context_, frame, frameContext, stats, metadata); > Kahn's algorithm prototype (on top of this commit): diff --git a/src/ipa/libipa/module.h b/src/ipa/libipa/module.h index cb2176dc37..f932fc79c5 100644 --- a/src/ipa/libipa/module.h +++ b/src/ipa/libipa/module.h @@ -10,7 +10,8 @@ #include <cerrno> #include <list> #include <memory> -#include <set> +#include <unordered_map> +#include <queue> #include <string> #include <vector> @@ -54,13 +55,10 @@ public: int createAlgorithms(Context &context, const ValueNode &algorithms) { const auto &list = algorithms.asList(); - std::map<AlgorithmT *, const ValueNode *> dataMap; + std::unordered_map<AlgorithmT *, const ValueNode *> dataMap; + std::unordered_map<std::string_view, AlgorithmT *> nameMap; - utils::ScopeExitActions actions; - - actions += [this]() { - clearAlgorithms(); - }; + utils::scope_exit algoGuard([&] { clearAlgorithms(); }); for (const auto &[i, algo] : utils::enumerate(list)) { if (!algo.isDictionary()) { @@ -94,11 +92,12 @@ public: << "Instantiated algorithm '" << name << "'"; dataMap[algorithms_.back().get()] = &algoData; + nameMap[algorithms_.back()->name()] = algorithms_.back().get(); } for (auto step : kAlgorithmAllSteps) { std::vector<AlgorithmT *> algos; - int ret = createOrderedList(algos, step); + int ret = createOrderedList(algos, step, nameMap); if (ret) return ret; @@ -114,7 +113,7 @@ public: } } - actions.release(); + algoGuard.release(); return 0; } @@ -125,92 +124,100 @@ public: } private: - int createOrderedList(std::vector<Algorithm<Module> *> &orderedList, - AlgorithmStep step) + int createOrderedList(std::vector<AlgorithmT *> &orderedList, + AlgorithmStep step, + const std::unordered_map<std::string_view, AlgorithmT *> &nameMap) { - using Algo = Algorithm<Module>; - std::set<Algo *> handledAlgorithms; + struct DepInfo { + std::vector<AlgorithmT *> dependents; + size_t dependencies = 0; + }; - orderedList.clear(); + std::unordered_map<AlgorithmT *, DepInfo> algo2dep; for (const auto &a : algorithms()) { - std::vector<std::string> resolutionStack; - int ret = addToOrderedList(a.get(), orderedList, - resolutionStack, - step); - if (ret) - return ret; + for (const auto &dep : a->dependencies()) { + if (dep.step != step) + continue; + + if (!(dep.type & (AlgorithmDependency::Exist | AlgorithmDependency::Before))) { + LOG(IPAModuleAlgo, Error) + << "Unknown dependency flags '" + << dep.type + << "' on dependency '" << dep.target; + return -EINVAL; + } + + auto it = nameMap.find(dep.target); + if (it == nameMap.end()) { + if (dep.type & AlgorithmDependency::Exist) { + LOG(IPAModuleAlgo, Error) + << "Algorithm '" << dep.target + << "' is listed as dependency but not instantiated"; + return -EINVAL; + } + + if (dep.type & AlgorithmDependency::Before) { + LOG(IPAModuleAlgo, Debug) + << "Algorithm '" << dep.target + << "' is listed as optional dependency but not instantiated"; + continue; + } + + ASSERT(false); + } + + algo2dep[it->second].dependents.push_back(a.get()); + algo2dep[a.get()].dependencies += 1; + } } - return 0; - } + std::queue<typename decltype(algo2dep)::const_iterator> q; - int addToOrderedList(Algorithm<Module> *algo, - std::vector<Algorithm<Module> *> &orderedList, - std::vector<std::string> &resolutionStack, - AlgorithmStep step) - { - /* nothing to do if it was added already due to dependency resolution */ - if (std::find(orderedList.begin(), orderedList.end(), algo) != orderedList.end()) - return 0; + for (auto it = algo2dep.begin(); it != algo2dep.end(); ++it) { + if (it->second.dependencies == 0) + q.push(it); + } - auto &deps = algo->dependencies(); + orderedList.clear(); - /* First add the dependent ones */ - for (auto &dep : deps) { - if (dep.step != step) - continue; + while (!q.empty()) { + auto nh = algo2dep.extract(q.front()); + q.pop(); - if (!(dep.type & (AlgorithmDependency::Exist | AlgorithmDependency::Before))) { - LOG(IPAModuleAlgo, Error) - << "Unknown dependency flags '" - << dep.type - << "' on dependency '" << dep.target; - return -EINVAL; - } + AlgorithmT *parent = nh.key(); - if (std::find(resolutionStack.begin(), resolutionStack.end(), dep.target) != resolutionStack.end()) { - LOG(IPAModuleAlgo, Info) - << "Circular dependency detected: " - << dep.target; - return -EINVAL; - } + LOG(IPAModuleAlgo, Debug) << "Step " << step << " add algo " << parent->name(); + orderedList.push_back(parent); - /* Find the target */ - auto iter = std::find_if(algorithms_.begin(), algorithms_.end(), - [&](auto &a) { return a->name() == dep.target; }); - if (iter == algorithms_.end()) { - if (dep.type & AlgorithmDependency::Exist) { - LOG(IPAModuleAlgo, Error) - << "Algorithm '" << dep.target - << "' is listed as dependency but not instantiated"; - return -EINVAL; - } + for (AlgorithmT *child : nh.mapped().dependents) { + auto it = algo2dep.find(child); + ASSERT(it != algo2dep.end()); - if (dep.type & AlgorithmDependency::Before) { - LOG(IPAModuleAlgo, Debug) - << "Algorithm '" << dep.target - << "' is listed as optional dependency but not instantiated"; - continue; - } + auto &c = it->second.dependencies; + ASSERT(c > 0); + c -= 1; - ASSERT(false); - } + LOG(IPAModuleAlgo, Debug) + << "Step " << step << ' ' << parent->name() << " -> " << child->name() + << " : " << it->second.dependencies; - Algorithm<Module> *dependency = iter->get(); - if (std::find(orderedList.begin(), orderedList.end(), dependency) == orderedList.end()) { - resolutionStack.push_back(dep.target); - int ret = addToOrderedList(dependency, - orderedList, - resolutionStack, - step); - if (ret) - return ret; + if (c == 0) + q.push(it); } } - orderedList.push_back(algo); - LOG(IPAModuleAlgo, Debug) << "Step " << step << " add algo " << algo->name(); + if (!algo2dep.empty()) { + LOG(IPAModuleAlgo, Info) << "Circular dependencies detected:"; + + for (const auto &[a, di] : algo2dep) + LOG(IPAModuleAlgo, Info) << " - " << a->name(); + + return -EINVAL; + } + + ASSERT(orderedList.size() == algorithms().size()); + return 0; }
diff --git a/src/ipa/libipa/algorithm.cpp b/src/ipa/libipa/algorithm.cpp index 757ce3519652..20ead51bf7e0 100644 --- a/src/ipa/libipa/algorithm.cpp +++ b/src/ipa/libipa/algorithm.cpp @@ -16,6 +16,28 @@ namespace libcamera { namespace ipa { +std::ostream &operator<<(std::basic_ostream<char> &out, libcamera::ipa::AlgorithmStep step) +{ + switch (step) { + case AlgorithmStep::Init: + out << "Init"; + break; + case AlgorithmStep::Configure: + out << "Configure"; + break; + case AlgorithmStep::Queue: + out << "Queue"; + break; + case AlgorithmStep::Prepare: + out << "Prepare"; + break; + case AlgorithmStep::Process: + out << "Process"; + break; + } + return out; +} + /** * \class Algorithm * \brief The base class for all IPA algorithms diff --git a/src/ipa/libipa/algorithm.h b/src/ipa/libipa/algorithm.h index b47765700317..97d7130a789d 100644 --- a/src/ipa/libipa/algorithm.h +++ b/src/ipa/libipa/algorithm.h @@ -7,6 +7,7 @@ #pragma once #include <memory> +#include <set> #include <stdint.h> #include <string> @@ -18,6 +19,48 @@ class ValueNode; namespace ipa { +enum class AlgorithmStep { + Init = 0, + Configure, + Queue, + Prepare, + Process +}; + +std::ostream &operator<<(std::basic_ostream<char> &out, libcamera::ipa::AlgorithmStep step); + +const std::array<AlgorithmStep, 5> kAlgorithmAllSteps = { + AlgorithmStep::Init, + AlgorithmStep::Configure, + AlgorithmStep::Queue, + AlgorithmStep::Prepare, + AlgorithmStep::Process +}; + +struct AlgorithmDependency { + enum Type { + /* Step on target needs to come first */ + Before = 0x01, + /* Target must exist */ + Exist = 0x02 + }; + + std::string target; + AlgorithmStep step; + unsigned type = 0; + + bool operator<(const AlgorithmDependency &other) const + { + return (target < other.target) || + ((target == other.target) && + ((step < other.step) || + ((step == other.step) && + (type < other.type)))); + } +}; + +const std::set<AlgorithmDependency> kNoDependencies = {}; + template<typename _Module> class AlgorithmFactoryBase; @@ -63,6 +106,11 @@ public: { } + virtual const std::set<AlgorithmDependency> &dependencies() const + { + return kNoDependencies; + } + const std::string &name() const { return name_; diff --git a/src/ipa/libipa/module.h b/src/ipa/libipa/module.h index 5644458bdf3d..cb2176dc37a6 100644 --- a/src/ipa/libipa/module.h +++ b/src/ipa/libipa/module.h @@ -7,8 +7,10 @@ #pragma once +#include <cerrno> #include <list> #include <memory> +#include <set> #include <string> #include <vector> @@ -35,6 +37,7 @@ public: using Config = _Config; using Params = _Params; using Stats = _Stats; + using AlgorithmT = Algorithm<Module>; virtual ~Module() {} @@ -43,25 +46,76 @@ public: return algorithms_; } + const std::vector<Algorithm<Module> *> &algorithmsOrderedForStep(AlgorithmStep step) const + { + return algorithmsOrderedForStep_.at(step); + } + int createAlgorithms(Context &context, const ValueNode &algorithms) { const auto &list = algorithms.asList(); + std::map<AlgorithmT *, const ValueNode *> dataMap; + + utils::ScopeExitActions actions; + + actions += [this]() { + clearAlgorithms(); + }; for (const auto &[i, algo] : utils::enumerate(list)) { if (!algo.isDictionary()) { LOG(IPAModuleAlgo, Error) << "Invalid tuning data syntax for algorithm " << i; - algorithms_.clear(); return -EINVAL; } - int ret = createAlgorithm(context, algo); + const auto &[name, algoData] = *algo.asDict().begin(); + /* + * Optionally, algorithms can be disabled via the tuning file + * by including enabled: false as a parameter within the + * algorithm tuning data. This is not an error, so we return 0. + */ + if (!algoData["enabled"].get<bool>(true)) { + LOG(IPAModuleAlgo, Info) + << "Algorithm '" << name << "' disabled via tuning file"; + return 0; + } + + std::unique_ptr<Algorithm<Module>> instance = createAlgorithm(name); + if (!instance) { + LOG(IPAModuleAlgo, Error) + << "Algorithm '" << name << "' not found"; + return -EINVAL; + } + + algorithms_.push_back(std::move(instance)); + + LOG(IPAModuleAlgo, Debug) + << "Instantiated algorithm '" << name << "'"; + + dataMap[algorithms_.back().get()] = &algoData; + } + + for (auto step : kAlgorithmAllSteps) { + std::vector<AlgorithmT *> algos; + int ret = createOrderedList(algos, step); + if (ret) + return ret; + + algorithmsOrderedForStep_[step] = std::move(algos); + } + + for (const auto &a : algorithmsOrderedForStep(AlgorithmStep::Init)) { + int ret = a->init(context, *dataMap[a]); if (ret) { - algorithms_.clear(); + LOG(IPAModuleAlgo, Error) + << "Algorithm failed to initialize"; return ret; } } + actions.release(); + return 0; } @@ -71,42 +125,101 @@ public: } private: - int createAlgorithm(Context &context, const ValueNode &data) + int createOrderedList(std::vector<Algorithm<Module> *> &orderedList, + AlgorithmStep step) { - const auto &[name, algoData] = *data.asDict().begin(); + using Algo = Algorithm<Module>; + std::set<Algo *> handledAlgorithms; - /* - * Optionally, algorithms can be disabled via the tuning file - * by including enabled: false as a parameter within the - * algorithm tuning data. This is not an error, so we return 0. - */ - if (!algoData["enabled"].get<bool>(true)) { - LOG(IPAModuleAlgo, Info) - << "Algorithm '" << name << "' disabled via tuning file"; - return 0; + orderedList.clear(); + + for (const auto &a : algorithms()) { + std::vector<std::string> resolutionStack; + int ret = addToOrderedList(a.get(), orderedList, + resolutionStack, + step); + if (ret) + return ret; } - std::unique_ptr<Algorithm<Module>> algo = createAlgorithm(name); - if (!algo) { - LOG(IPAModuleAlgo, Error) - << "Algorithm '" << name << "' not found"; - return -EINVAL; - } - - int ret = algo->init(context, algoData); - if (ret) { - LOG(IPAModuleAlgo, Error) - << "Algorithm '" << name << "' failed to initialize"; - return ret; - } - - LOG(IPAModuleAlgo, Debug) - << "Instantiated algorithm '" << name << "'"; - - algorithms_.push_back(std::move(algo)); return 0; } + int addToOrderedList(Algorithm<Module> *algo, + std::vector<Algorithm<Module> *> &orderedList, + std::vector<std::string> &resolutionStack, + AlgorithmStep step) + { + /* nothing to do if it was added already due to dependency resolution */ + if (std::find(orderedList.begin(), orderedList.end(), algo) != orderedList.end()) + return 0; + + auto &deps = algo->dependencies(); + + /* First add the dependent ones */ + for (auto &dep : deps) { + if (dep.step != step) + continue; + + if (!(dep.type & (AlgorithmDependency::Exist | AlgorithmDependency::Before))) { + LOG(IPAModuleAlgo, Error) + << "Unknown dependency flags '" + << dep.type + << "' on dependency '" << dep.target; + return -EINVAL; + } + + if (std::find(resolutionStack.begin(), resolutionStack.end(), dep.target) != resolutionStack.end()) { + LOG(IPAModuleAlgo, Info) + << "Circular dependency detected: " + << dep.target; + return -EINVAL; + } + + /* Find the target */ + auto iter = std::find_if(algorithms_.begin(), algorithms_.end(), + [&](auto &a) { return a->name() == dep.target; }); + if (iter == algorithms_.end()) { + if (dep.type & AlgorithmDependency::Exist) { + LOG(IPAModuleAlgo, Error) + << "Algorithm '" << dep.target + << "' is listed as dependency but not instantiated"; + return -EINVAL; + } + + if (dep.type & AlgorithmDependency::Before) { + LOG(IPAModuleAlgo, Debug) + << "Algorithm '" << dep.target + << "' is listed as optional dependency but not instantiated"; + continue; + } + + ASSERT(false); + } + + Algorithm<Module> *dependency = iter->get(); + if (std::find(orderedList.begin(), orderedList.end(), dependency) == orderedList.end()) { + resolutionStack.push_back(dep.target); + int ret = addToOrderedList(dependency, + orderedList, + resolutionStack, + step); + if (ret) + return ret; + } + } + + orderedList.push_back(algo); + LOG(IPAModuleAlgo, Debug) << "Step " << step << " add algo " << algo->name(); + return 0; + } + + void clearAlgorithms() + { + algorithmsOrderedForStep_.clear(); + algorithms_.clear(); + } + static std::unique_ptr<Algorithm<Module>> createAlgorithm(const std::string &name) { for (const AlgorithmFactoryBase<Module> *factory : factories()) { @@ -129,6 +242,7 @@ private: } std::list<std::unique_ptr<Algorithm<Module>>> algorithms_; + std::map<AlgorithmStep, std::vector<AlgorithmT *>> algorithmsOrderedForStep_; }; } /* namespace ipa */ diff --git a/src/ipa/rkisp1/rkisp1.cpp b/src/ipa/rkisp1/rkisp1.cpp index cefaf40aee63..add2e45cfa36 100644 --- a/src/ipa/rkisp1/rkisp1.cpp +++ b/src/ipa/rkisp1/rkisp1.cpp @@ -30,6 +30,7 @@ #include "algorithms/algorithm.h" #include "libipa/agc.h" +#include "libipa/algorithm.h" #include "ipa_context.h" #include "params.h" @@ -237,8 +238,8 @@ int IPARkISP1::configure(const IPAConfigInfo &ipaConfig, return format.colourEncoding == PixelFormatInfo::ColourEncodingRAW; }); - for (const auto &a : algorithms()) { - Algorithm *algo = static_cast<Algorithm *>(a.get()); + for (const auto &a : algorithmsOrderedForStep(AlgorithmStep::Configure)) { + Algorithm *algo = static_cast<Algorithm *>(a); /* Disable algorithms that don't support raw formats. */ algo->disabled_ = context_.configuration.raw && !algo->supportsRaw_; @@ -290,8 +291,8 @@ void IPARkISP1::queueRequest(const uint32_t frame, const ControlList &controls) IPAFrameContext &frameContext = context_.frameContexts.alloc(frame); context_.debugMetadata.enableByControl(controls); - for (const auto &a : algorithms()) { - Algorithm *algo = static_cast<Algorithm *>(a.get()); + for (const auto &a : algorithmsOrderedForStep(AlgorithmStep::Queue)) { + Algorithm *algo = static_cast<Algorithm *>(a); if (algo->disabled_) continue; algo->queueRequest(context_, frame, frameContext, controls); @@ -305,8 +306,9 @@ void IPARkISP1::computeParams(const uint32_t frame, const uint32_t bufferId) RkISP1Params params(context_.configuration.paramFormat, mappedBuffers_.at(bufferId).planes()[0]); - for (const auto &algo : algorithms()) + for (const auto &algo : algorithmsOrderedForStep(AlgorithmStep::Prepare)) { algo->prepare(context_, frame, frameContext, ¶ms); + } paramsComputed.emit(frame, params.bytesused()); } @@ -330,8 +332,8 @@ void IPARkISP1::processStats(const uint32_t frame, const uint32_t bufferId, ControlList metadata(controls::controls); - for (const auto &a : algorithms()) { - Algorithm *algo = static_cast<Algorithm *>(a.get()); + for (const auto &a : algorithmsOrderedForStep(AlgorithmStep::Process)) { + Algorithm *algo = static_cast<Algorithm *>(a); if (algo->disabled_) continue; algo->process(context_, frame, frameContext, stats, metadata);
Some algorithms have ordering dependencies on each other. As an example on rkisp1 the Compress algorithm is used to implement digital gain. So during computeParams(), Agc::prepare() must be called before Compress::prepare(), so that the Agc algorithm can store the digital gain in the frame context for Compress to pick it up. To make matters worse, the required order during configure() time can be inverse. In the example above, Compress::configure() should be called first, so that Agc::configure() can check the session if Compress (and therefore digital gain) is available. This patch implements a simple, yet effective dependency scheme where an algorithm can specify a list of dependencies. A dependency consists of an algorithm step (Init, Configure, Queue, Prepare, Process), a target and the type which can either be "Exists" meaning that the corresponding algorithm must be in the tuning file and "Before" which means that the target must be run first. Each algorithm can provide a list of dependencies through the newly added dependencies() function. This patch only implements the necessary infrastructure for rkisp1. Necessary dependencies will be added in upcoming patches. Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com> --- src/ipa/libipa/algorithm.cpp | 22 ++++++ src/ipa/libipa/algorithm.h | 48 ++++++++++++ src/ipa/libipa/module.h | 178 +++++++++++++++++++++++++++++++++++-------- src/ipa/rkisp1/rkisp1.cpp | 16 ++-- 4 files changed, 225 insertions(+), 39 deletions(-)