summaryrefslogtreecommitdiff
path: root/src/libcamera/utils.cpp
AgeCommit message (Collapse)Author
2021-06-19libcamera: utils: Support systems that lack secure_getenv and issetugidLaurent Pinchart
Android provides neither secure_getenv() nor issetugid(). Enable compilation on that platform by using a plain getenv(), as that seems to be the best we can do. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Hirokazu Honda <hiroh@chromium.org> Reviewed-by: Umang Jain <umang.jain@ideasonboard.com> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
2021-06-08libcamera: utils: Add helper class for std::chrono::durationNaushir Patuck
A new utils::Duration class is defined to represent a std::chrono::duration type with double precision nanosecond timebase. Using a double minimises the loss of precision when converting timebases. This helper class may be used by IPAs to represent variables such as frame durations and exposure times. An operator << overload is defined to help with displaying utils::Duration value in stream objects. Currently, this will display the duration value in microseconds. Signed-off-by: Naushir Patuck <naush@raspberrypi.com> Reviewed-by: David Plowman <david.plowman@raspberrypi.com> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
2021-05-18libcamera: utils: Add enumerate view for range-based for loopsLaurent Pinchart
Range-based for loops are handy and widely preferred in C++, but are limited in their ability to replace for loops that require access to a loop counter. The enumerate() function solves this problem by wrapping the iterable in an adapter that, when used as a range-expression, will provide iterators whose value_type is a pair of index and value reference. The iterable must support std::begin() and std::end(). This includes all containers provided by the standard C++ library, as well as C-style arrays. A typical usage pattern would use structured binding to store the index and value in two separate variables: std::vector<int> values = ...; for (auto [index, value] : utils::enumerate(values)) { ... } Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
2021-02-05libcamera: utils: Add reverse adapter for range-based loopLaurent Pinchart
Add a utils::reverse() function that creates an adapter wrapping in iterable, such that range-based iteration over the adapter iterates over the iterable in reverse order. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
2020-12-14libcamera: Replace ARRAY_SIZE() with std::size()Laurent Pinchart
C++17 has a std::size() function that returns the size of a C-style array. Use it instead of the custom ARRAY_SIZE macro. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Paul Elder <paul.elder@ideasonboard.com> Reviewed-by: Umang Jain <email@uajain.com> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
2020-10-02libcamera: utils: Add method to remove non-ASCII charactersNiklas Söderlund
Add method that removes non-ASCII characters from a string. Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org> Reviewed-by: Umang Jain <email@uajain.com>
2020-08-25libcamera: Replace utils::clamp() with std::clamp()Laurent Pinchart
Now that libcamera uses C++17, the C++ standard library provides std::clamp(). Drop our custom utils::clamp() function. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
2020-08-03libcamera: utils: Add alignUp and alignDown functionsJacopo Mondi
Add to libcamera utils library two functions to round up or down a value to an alignment and add a test in test/utils.cpp for the two new functions. Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>
2020-07-09libcamera: utils: Add map_keys() functionLaurent Pinchart
Add a map_keys() function to the utils namespace to extract keys from a map. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> [Niklas: change return type to std::vector instead of std::set] Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
2020-05-16libcamera: Move internal headers to include/libcamera/internal/Laurent Pinchart
The libcamera internal headers are located in src/libcamera/include/. The directory is added to the compiler headers search path with a meson include_directories() directive, and internal headers are included with (e.g. for the internal semaphore.h header) #include "semaphore.h" All was well, until libcxx decided to implement the C++20 synchronization library. The __threading_support header gained a #include <semaphore.h> to include the pthread's semaphore support. As include_directories() adds src/libcamera/include/ to the compiler search path with -I, the internal semaphore.h is included instead of the pthread version. Needless to say, the compiler isn't happy. Three options have been considered to fix this issue: - Use -iquote instead of -I. The -iquote option instructs gcc to only consider the header search path for headers included with the "" version. Meson unfortunately doesn't support this option. - Rename the internal semaphore.h header. This was deemed to be the beginning of a long whack-a-mole game, where namespace clashes with system libraries would appear over time (possibly dependent on particular system configurations) and would need to be constantly fixed. - Move the internal headers to another directory to create a unique namespace through path components. This causes lots of churn in all the existing source files through the all project. The first option would be best, but isn't available to us due to missing support in meson. Even if -iquote support was added, we would need to fix the problem before a new version of meson containing the required support would be released. The third option is thus the only practical solution available. Bite the bullet, and do it, moving headers to include/libcamera/internal/. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Acked-by: Jacopo Mondi <jacopo@jmondi.org>
2020-04-30libcamera: utils: Identify the 'real' build pathKieran Bingham
Call realpath() to strip out any levels of indirection required in referencing the root build directory. This simplifies the debug output when reporting and parsing paths. Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
2020-04-28libcamera: utils: Add a function to retrieve the libcamera source treeLaurent Pinchart
Similarly to libcameraBuildPath(), there's a need to locate resources within the source tree when running libcamera without installing it. Support this use case with a new utils::libcameraSourcePath() function. The implementation uses a symlink from the build root to the source root in order to avoid hardcoding the path to the source root in the libcamera.so binary. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
2020-04-15libcamera: utils: Add string join functionLaurent Pinchart
Add a utils::join() function to join elements of a container into a string, with a separator and an optional conversion function if the elements are not implicitly convertible to std::string. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
2020-03-19libcamera: utils: Adapt libcameraPath to match use casesKaaira Gupta
The two callers of functions libcameraPath() and isLibcameraInstalled() end up using the same process and finally use the path with libcamera.so. Hence write a function libcameraBuildPath() which combines their functions and returns the root of the build sources when the library has not been installed, but is running from the build tree, thereby making call sites simpler. When the library is installed, libcameraBuildPath() will return an empty string. Make changes in the call sites accordingly. Signed-off-by: Kaaira Gupta <kgupta@es.iitr.ac.in> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
2020-03-18libcamera: utils: Move libcamera build path lookup to utilsKaaira Gupta
The IPA proxy manager will need to find the libcamera build path exactly the same way as the IPA module mnager. Move the isLibcameraInstalled() and libcameraPath() functions to utils to make them reusable. Signed-off-by: Kaaira Gupta <kgupta@es.iitr.ac.in> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
2020-02-24libcamera: utils: Add a C++ dirname implementationKieran Bingham
Provide a std::string based implementation which conforms to the behaviour of the dirname() fucntion defined by POSIX. Tests are added to cover expected corner cases of the implementation. Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
2020-02-13libcamera: Standardize on doxygen \returnKieran Bingham
Two occasions in the source utilise the Doxygen '\returns' alias for \return. We use \return everywhere else in the code. Update the two occurences to match. Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
2020-02-13libcamera: utils: Add string splitter utility functionLaurent Pinchart
Add a utils::split() function that splits a string for the purpose of iterating over substrings. It returns an object of unspecified type that can be used in range-based for loops. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
2020-01-14libcamera: Switch from utils::make_unique to std::make_uniqueLaurent Pinchart
Now that we're using C++-14, drop utils::make_unique for std::make_unique. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
2020-01-03libcamera: utils: Add strlcpyPaul Elder
strlcpy is available in libbsd, bionic, musl, and ulibc, but not in glibc. Instead of checking for strlcpy availability and modifying dependencies, implement it in utils, as a wrapper around strncpy. Signed-off-by: Paul Elder <paul.elder@ideasonboard.com> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
2019-10-15libcamera: utils: Add hex stream output helperLaurent Pinchart
Add a utils::hex() function that simplifies writing hexadecimal values to an ostream. The function handles the '0x' prefix, the field width and the fill character automatically. Use it through the libcamera code base, and add a test. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
2019-09-14libcamera: utils: Add clock helpersLaurent Pinchart
In preparation for standardisation of the std::chrono::steady_clock as the libcamera default clock, define aliases for the clock, duration and time point, and add helper functions to convert a duration to a timespec and a time point to a string. More helpers will be added later as needed. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
2019-07-13libcamera: utils: Add clamp()Niklas Söderlund
C++11 does not support std::clamp(), add a custom implementation in utils. Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
2019-04-26libcamera: utils: call secure_getenv() if it exists or workaround with ↵Giulio Benetti
issetugid() When secure_getenv() is not available, need to have a workaround. Check if secure_getenv() is present, otherwise call issetugid() on its place. Signed-off-by: Giulio Benetti <giulio.benetti@micronovasrl.com> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> [Kieran: include stdlib.h] Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
2019-04-19libcamera: Include header related to source file firstLaurent Pinchart
Include the header file corresponding to the source file in the very first position. This complies with the Google C++ coding style guideliens, and helps ensuring that the headers are self-contained. Three bugs are already caught by this change (missing includes or forward declarations) in device_enumerator.h, event_dispatcher_poll.h and pipeline_handler.h. Fix them. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
2019-04-18libcamera: utils: Add set_overlap() functionLaurent Pinchart
The new set_overlap() function counts the number of overlapping elements in the intersection of two sorted ranges defined by their beginning and ending iterators. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
2019-04-03libcamera: utils: Use internal secure_getenv() implementationKieran Bingham
The secure_getenv() call is not provided by all C libraries. Support this feature by implementing our own version. Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
2019-04-03libcamera: utils: Use internal basename implementation.Kieran Bingham
Differing implementations of basename() exist, some of which may modify the content of the string passed as an argument. The implementation of basename() is trivial, thus to support different C librariese, provide our own version which accepts and returns a const char *. Update the call sites to use the new implementation. Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>