From 27aff949fbc1b9aabfc594bbfd6f94be55a086ec Mon Sep 17 00:00:00 2001 From: Kieran Bingham Date: Tue, 15 Jun 2021 16:15:12 +0100 Subject: libcamera/base: Move extended base functionality Move the functionality for the following components to the new base support library: - BoundMethod - EventDispatcher - EventDispatcherPoll - Log - Message - Object - Signal - Semaphore - Thread - Timer While it would be preferable to see these split to move one component per commit, these components are all interdependent upon each other, which leaves us with one big change performing the move for all of them. Reviewed-by: Hirokazu Honda Reviewed-by: Paul Elder Signed-off-by: Kieran Bingham --- include/libcamera/base/bound_method.h | 239 +++++++++++++++++++++ include/libcamera/base/event_dispatcher.h | 35 +++ include/libcamera/base/event_dispatcher_poll.h | 58 +++++ include/libcamera/base/log.h | 130 +++++++++++ include/libcamera/base/meson.build | 10 + include/libcamera/base/message.h | 71 ++++++ include/libcamera/base/object.h | 71 ++++++ include/libcamera/base/semaphore.h | 34 +++ include/libcamera/base/signal.h | 132 ++++++++++++ include/libcamera/base/thread.h | 76 +++++++ include/libcamera/base/timer.h | 49 +++++ include/libcamera/bound_method.h | 239 --------------------- include/libcamera/camera.h | 4 +- include/libcamera/camera_manager.h | 5 +- include/libcamera/internal/camera_sensor.h | 2 +- include/libcamera/internal/device_enumerator.h | 2 +- include/libcamera/internal/event_dispatcher.h | 35 --- include/libcamera/internal/event_dispatcher_poll.h | 58 ----- include/libcamera/internal/event_notifier.h | 4 +- include/libcamera/internal/ipa_data_serializer.h | 3 +- include/libcamera/internal/ipa_manager.h | 3 +- include/libcamera/internal/ipa_module.h | 3 +- include/libcamera/internal/ipc_pipe.h | 4 +- include/libcamera/internal/ipc_unixsocket.h | 2 +- include/libcamera/internal/log.h | 130 ----------- include/libcamera/internal/media_device.h | 4 +- include/libcamera/internal/meson.build | 7 - include/libcamera/internal/message.h | 71 ------ include/libcamera/internal/pipeline_handler.h | 2 +- include/libcamera/internal/process.h | 2 +- include/libcamera/internal/semaphore.h | 34 --- include/libcamera/internal/thread.h | 78 ------- include/libcamera/internal/timer.h | 49 ----- include/libcamera/internal/v4l2_device.h | 6 +- include/libcamera/internal/v4l2_subdevice.h | 2 +- include/libcamera/internal/v4l2_videodevice.h | 4 +- include/libcamera/ipa/ipa_interface.h | 3 +- include/libcamera/meson.build | 3 - include/libcamera/object.h | 71 ------ include/libcamera/request.h | 2 +- include/libcamera/signal.h | 132 ------------ 41 files changed, 935 insertions(+), 934 deletions(-) create mode 100644 include/libcamera/base/bound_method.h create mode 100644 include/libcamera/base/event_dispatcher.h create mode 100644 include/libcamera/base/event_dispatcher_poll.h create mode 100644 include/libcamera/base/log.h create mode 100644 include/libcamera/base/message.h create mode 100644 include/libcamera/base/object.h create mode 100644 include/libcamera/base/semaphore.h create mode 100644 include/libcamera/base/signal.h create mode 100644 include/libcamera/base/thread.h create mode 100644 include/libcamera/base/timer.h delete mode 100644 include/libcamera/bound_method.h delete mode 100644 include/libcamera/internal/event_dispatcher.h delete mode 100644 include/libcamera/internal/event_dispatcher_poll.h delete mode 100644 include/libcamera/internal/log.h delete mode 100644 include/libcamera/internal/message.h delete mode 100644 include/libcamera/internal/semaphore.h delete mode 100644 include/libcamera/internal/thread.h delete mode 100644 include/libcamera/internal/timer.h delete mode 100644 include/libcamera/object.h delete mode 100644 include/libcamera/signal.h (limited to 'include') diff --git a/include/libcamera/base/bound_method.h b/include/libcamera/base/bound_method.h new file mode 100644 index 00000000..282f9b58 --- /dev/null +++ b/include/libcamera/base/bound_method.h @@ -0,0 +1,239 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * bound_method.h - Method bind and invocation + */ +#ifndef __LIBCAMERA_BASE_BOUND_METHOD_H__ +#define __LIBCAMERA_BASE_BOUND_METHOD_H__ + +#include +#include +#include +#include + +namespace libcamera { + +class Object; + +enum ConnectionType { + ConnectionTypeAuto, + ConnectionTypeDirect, + ConnectionTypeQueued, + ConnectionTypeBlocking, +}; + +class BoundMethodPackBase +{ +public: + virtual ~BoundMethodPackBase() = default; +}; + +template +class BoundMethodPack : public BoundMethodPackBase +{ +public: + BoundMethodPack(const Args &... args) + : args_(args...) + { + } + + std::tuple...> args_; + R ret_; +}; + +template +class BoundMethodPack : public BoundMethodPackBase +{ +public: + BoundMethodPack(const Args &... args) + : args_(args...) + { + } + + std::tuple...> args_; +}; + +class BoundMethodBase +{ +public: + BoundMethodBase(void *obj, Object *object, ConnectionType type) + : obj_(obj), object_(object), connectionType_(type) + { + } + virtual ~BoundMethodBase() = default; + + template::value> * = nullptr> + bool match(T *obj) { return obj == obj_; } + bool match(Object *object) { return object == object_; } + + Object *object() const { return object_; } + + virtual void invokePack(BoundMethodPackBase *pack) = 0; + +protected: + bool activatePack(std::shared_ptr pack, + bool deleteMethod); + + void *obj_; + Object *object_; + +private: + ConnectionType connectionType_; +}; + +template +class BoundMethodArgs : public BoundMethodBase +{ +public: + using PackType = BoundMethodPack; + +private: + template + void invokePack(BoundMethodPackBase *pack, std::index_sequence) + { + PackType *args = static_cast(pack); + args->ret_ = invoke(std::get(args->args_)...); + } + +public: + BoundMethodArgs(void *obj, Object *object, ConnectionType type) + : BoundMethodBase(obj, object, type) {} + + void invokePack(BoundMethodPackBase *pack) override + { + invokePack(pack, std::make_index_sequence{}); + } + + virtual R activate(Args... args, bool deleteMethod = false) = 0; + virtual R invoke(Args... args) = 0; +}; + +template +class BoundMethodArgs : public BoundMethodBase +{ +public: + using PackType = BoundMethodPack; + +private: + template + void invokePack(BoundMethodPackBase *pack, std::index_sequence) + { + /* args is effectively unused when the sequence I is empty. */ + PackType *args [[gnu::unused]] = static_cast(pack); + invoke(std::get(args->args_)...); + } + +public: + BoundMethodArgs(void *obj, Object *object, ConnectionType type) + : BoundMethodBase(obj, object, type) {} + + void invokePack(BoundMethodPackBase *pack) override + { + invokePack(pack, std::make_index_sequence{}); + } + + virtual void activate(Args... args, bool deleteMethod = false) = 0; + virtual void invoke(Args... args) = 0; +}; + +template +class BoundMethodMember : public BoundMethodArgs +{ +public: + using PackType = typename BoundMethodArgs::PackType; + + BoundMethodMember(T *obj, Object *object, R (T::*func)(Args...), + ConnectionType type = ConnectionTypeAuto) + : BoundMethodArgs(obj, object, type), func_(func) + { + } + + bool match(R (T::*func)(Args...)) const { return func == func_; } + + R activate(Args... args, bool deleteMethod = false) override + { + if (!this->object_) { + T *obj = static_cast(this->obj_); + return (obj->*func_)(args...); + } + + auto pack = std::make_shared(args...); + bool sync = BoundMethodBase::activatePack(pack, deleteMethod); + return sync ? pack->ret_ : R(); + } + + R invoke(Args... args) override + { + T *obj = static_cast(this->obj_); + return (obj->*func_)(args...); + } + +private: + R (T::*func_)(Args...); +}; + +template +class BoundMethodMember : public BoundMethodArgs +{ +public: + using PackType = typename BoundMethodArgs::PackType; + + BoundMethodMember(T *obj, Object *object, void (T::*func)(Args...), + ConnectionType type = ConnectionTypeAuto) + : BoundMethodArgs(obj, object, type), func_(func) + { + } + + bool match(void (T::*func)(Args...)) const { return func == func_; } + + void activate(Args... args, bool deleteMethod = false) override + { + if (!this->object_) { + T *obj = static_cast(this->obj_); + return (obj->*func_)(args...); + } + + auto pack = std::make_shared(args...); + BoundMethodBase::activatePack(pack, deleteMethod); + } + + void invoke(Args... args) override + { + T *obj = static_cast(this->obj_); + return (obj->*func_)(args...); + } + +private: + void (T::*func_)(Args...); +}; + +template +class BoundMethodStatic : public BoundMethodArgs +{ +public: + BoundMethodStatic(R (*func)(Args...)) + : BoundMethodArgs(nullptr, nullptr, ConnectionTypeAuto), + func_(func) + { + } + + bool match(R (*func)(Args...)) const { return func == func_; } + + R activate(Args... args, [[maybe_unused]] bool deleteMethod = false) override + { + return (*func_)(args...); + } + + R invoke(Args...) override + { + return R(); + } + +private: + R (*func_)(Args...); +}; + +} /* namespace libcamera */ + +#endif /* __LIBCAMERA_BASE_BOUND_METHOD_H__ */ diff --git a/include/libcamera/base/event_dispatcher.h b/include/libcamera/base/event_dispatcher.h new file mode 100644 index 00000000..045df27f --- /dev/null +++ b/include/libcamera/base/event_dispatcher.h @@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * event_dispatcher.h - Event dispatcher + */ +#ifndef __LIBCAMERA_BASE_EVENT_DISPATCHER_H__ +#define __LIBCAMERA_BASE_EVENT_DISPATCHER_H__ + +#include + +namespace libcamera { + +class EventNotifier; +class Timer; + +class EventDispatcher +{ +public: + virtual ~EventDispatcher(); + + virtual void registerEventNotifier(EventNotifier *notifier) = 0; + virtual void unregisterEventNotifier(EventNotifier *notifier) = 0; + + virtual void registerTimer(Timer *timer) = 0; + virtual void unregisterTimer(Timer *timer) = 0; + + virtual void processEvents() = 0; + + virtual void interrupt() = 0; +}; + +} /* namespace libcamera */ + +#endif /* __LIBCAMERA_BASE_EVENT_DISPATCHER_H__ */ diff --git a/include/libcamera/base/event_dispatcher_poll.h b/include/libcamera/base/event_dispatcher_poll.h new file mode 100644 index 00000000..ae2a3f04 --- /dev/null +++ b/include/libcamera/base/event_dispatcher_poll.h @@ -0,0 +1,58 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * event_dispatcher_poll.h - Poll-based event dispatcher + */ +#ifndef __LIBCAMERA_BASE_EVENT_DISPATCHER_POLL_H__ +#define __LIBCAMERA_BASE_EVENT_DISPATCHER_POLL_H__ + +#include +#include +#include + +#include + +struct pollfd; + +namespace libcamera { + +class EventNotifier; +class Timer; + +class EventDispatcherPoll final : public EventDispatcher +{ +public: + EventDispatcherPoll(); + ~EventDispatcherPoll(); + + void registerEventNotifier(EventNotifier *notifier); + void unregisterEventNotifier(EventNotifier *notifier); + + void registerTimer(Timer *timer); + void unregisterTimer(Timer *timer); + + void processEvents(); + void interrupt(); + +private: + struct EventNotifierSetPoll { + short events() const; + EventNotifier *notifiers[3]; + }; + + int poll(std::vector *pollfds); + void processInterrupt(const struct pollfd &pfd); + void processNotifiers(const std::vector &pollfds); + void processTimers(); + + std::map notifiers_; + std::list timers_; + int eventfd_; + + bool processingEvents_; +}; + +} /* namespace libcamera */ + +#endif /* __LIBCAMERA_BASE_EVENT_DISPATCHER_POLL_H__ */ diff --git a/include/libcamera/base/log.h b/include/libcamera/base/log.h new file mode 100644 index 00000000..b93c947a --- /dev/null +++ b/include/libcamera/base/log.h @@ -0,0 +1,130 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2018, Google Inc. + * + * log.h - Logging infrastructure + */ +#ifndef __LIBCAMERA_BASE_LOG_H__ +#define __LIBCAMERA_BASE_LOG_H__ + +#include +#include + +#include +#include + +namespace libcamera { + +enum LogSeverity { + LogInvalid = -1, + LogDebug = 0, + LogInfo, + LogWarning, + LogError, + LogFatal, +}; + +class LogCategory +{ +public: + explicit LogCategory(const char *name); + + const char *name() const { return name_; } + LogSeverity severity() const { return severity_; } + void setSeverity(LogSeverity severity); + + static const LogCategory &defaultCategory(); + +private: + const char *name_; + LogSeverity severity_; +}; + +#define LOG_DECLARE_CATEGORY(name) \ +extern const LogCategory &_LOG_CATEGORY(name)(); + +#define LOG_DEFINE_CATEGORY(name) \ +const LogCategory &_LOG_CATEGORY(name)() \ +{ \ + /* The instance will be deleted by the Logger destructor. */ \ + static LogCategory *category = new LogCategory(#name); \ + return *category; \ +} + +class LogMessage +{ +public: + LogMessage(const char *fileName, unsigned int line, + const LogCategory &category, LogSeverity severity); + + LogMessage(LogMessage &&); + ~LogMessage(); + + std::ostream &stream() { return msgStream_; } + + const utils::time_point ×tamp() const { return timestamp_; } + LogSeverity severity() const { return severity_; } + const LogCategory &category() const { return category_; } + const std::string &fileInfo() const { return fileInfo_; } + const std::string msg() const { return msgStream_.str(); } + +private: + LIBCAMERA_DISABLE_COPY(LogMessage) + + void init(const char *fileName, unsigned int line); + + std::ostringstream msgStream_; + const LogCategory &category_; + LogSeverity severity_; + utils::time_point timestamp_; + std::string fileInfo_; +}; + +class Loggable +{ +public: + virtual ~Loggable(); + +protected: + virtual std::string logPrefix() const = 0; + + LogMessage _log(const LogCategory *category, LogSeverity severity, + const char *fileName = __builtin_FILE(), + unsigned int line = __builtin_LINE()) const; +}; + +LogMessage _log(const LogCategory *category, LogSeverity severity, + const char *fileName = __builtin_FILE(), + unsigned int line = __builtin_LINE()); + +#ifndef __DOXYGEN__ +#define _LOG_CATEGORY(name) logCategory##name + +#define _LOG1(severity) \ + _log(nullptr, Log##severity).stream() +#define _LOG2(category, severity) \ + _log(&_LOG_CATEGORY(category)(), Log##severity).stream() + +/* + * Expand the LOG() macro to _LOG1() or _LOG2() based on the number of + * arguments. + */ +#define _LOG_MACRO(_1, _2, NAME, ...) NAME +#define LOG(...) _LOG_MACRO(__VA_ARGS__, _LOG2, _LOG1)(__VA_ARGS__) +#else /* __DOXYGEN___ */ +#define LOG(category, severity) +#endif /* __DOXYGEN__ */ + +#ifndef NDEBUG +#define ASSERT(condition) static_cast(({ \ + if (!(condition)) \ + LOG(Fatal) << "assertion \"" #condition "\" failed in " \ + << __func__ << "()"; \ +})) +#else +#define ASSERT(condition) static_cast(false && (condition)) +#endif + +} /* namespace libcamera */ + +#endif /* __LIBCAMERA_BASE_LOG_H__ */ diff --git a/include/libcamera/base/meson.build b/include/libcamera/base/meson.build index 2db756c5..7a858dcb 100644 --- a/include/libcamera/base/meson.build +++ b/include/libcamera/base/meson.build @@ -3,7 +3,17 @@ libcamera_base_include_dir = libcamera_include_dir / 'base' libcamera_base_headers = files([ + 'bound_method.h', 'class.h', + 'event_dispatcher.h', + 'event_dispatcher_poll.h', + 'log.h', + 'message.h', + 'object.h', + 'semaphore.h', + 'signal.h', + 'thread.h', + 'timer.h', 'utils.h', ]) diff --git a/include/libcamera/base/message.h b/include/libcamera/base/message.h new file mode 100644 index 00000000..5d2a9f04 --- /dev/null +++ b/include/libcamera/base/message.h @@ -0,0 +1,71 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * message.h - Message queue support + */ +#ifndef __LIBCAMERA_BASE_MESSAGE_H__ +#define __LIBCAMERA_BASE_MESSAGE_H__ + +#include + +#include + +namespace libcamera { + +class BoundMethodBase; +class Object; +class Semaphore; +class Thread; + +class Message +{ +public: + enum Type { + None = 0, + InvokeMessage = 1, + ThreadMoveMessage = 2, + DeferredDelete = 3, + UserMessage = 1000, + }; + + Message(Type type); + virtual ~Message(); + + Type type() const { return type_; } + Object *receiver() const { return receiver_; } + + static Type registerMessageType(); + +private: + friend class Thread; + + Type type_; + Object *receiver_; + + static std::atomic_uint nextUserType_; +}; + +class InvokeMessage : public Message +{ +public: + InvokeMessage(BoundMethodBase *method, + std::shared_ptr pack, + Semaphore *semaphore = nullptr, + bool deleteMethod = false); + ~InvokeMessage(); + + Semaphore *semaphore() const { return semaphore_; } + + void invoke(); + +private: + BoundMethodBase *method_; + std::shared_ptr pack_; + Semaphore *semaphore_; + bool deleteMethod_; +}; + +} /* namespace libcamera */ + +#endif /* __LIBCAMERA_BASE_MESSAGE_H__ */ diff --git a/include/libcamera/base/object.h b/include/libcamera/base/object.h new file mode 100644 index 00000000..5c385ab4 --- /dev/null +++ b/include/libcamera/base/object.h @@ -0,0 +1,71 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * object.h - Base object + */ +#ifndef __LIBCAMERA_BASE_OBJECT_H__ +#define __LIBCAMERA_BASE_OBJECT_H__ + +#include +#include +#include + +#include + +namespace libcamera { + +class Message; +template +class Signal; +class SignalBase; +class Thread; + +class Object +{ +public: + Object(Object *parent = nullptr); + virtual ~Object(); + + void deleteLater(); + + void postMessage(std::unique_ptr msg); + + template::value> * = nullptr> + R invokeMethod(R (T::*func)(FuncArgs...), ConnectionType type, + Args... args) + { + T *obj = static_cast(this); + auto *method = new BoundMethodMember(obj, this, func, type); + return method->activate(args..., true); + } + + Thread *thread() const { return thread_; } + void moveToThread(Thread *thread); + + Object *parent() const { return parent_; } + +protected: + virtual void message(Message *msg); + +private: + friend class SignalBase; + friend class Thread; + + void notifyThreadMove(); + + void connect(SignalBase *signal); + void disconnect(SignalBase *signal); + + Object *parent_; + std::vector children_; + + Thread *thread_; + std::list signals_; + unsigned int pendingMessages_; +}; + +} /* namespace libcamera */ + +#endif /* __LIBCAMERA_BASE_OBJECT_H__ */ diff --git a/include/libcamera/base/semaphore.h b/include/libcamera/base/semaphore.h new file mode 100644 index 00000000..c8e62e3e --- /dev/null +++ b/include/libcamera/base/semaphore.h @@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * semaphore.h - General-purpose counting semaphore + */ +#ifndef __LIBCAMERA_BASE_SEMAPHORE_H__ +#define __LIBCAMERA_BASE_SEMAPHORE_H__ + +#include + +#include + +namespace libcamera { + +class Semaphore +{ +public: + Semaphore(unsigned int n = 0); + + unsigned int available(); + void acquire(unsigned int n = 1); + bool tryAcquire(unsigned int n = 1); + void release(unsigned int n = 1); + +private: + Mutex mutex_; + std::condition_variable cv_; + unsigned int available_; +}; + +} /* namespace libcamera */ + +#endif /* __LIBCAMERA_BASE_SEMAPHORE_H__ */ diff --git a/include/libcamera/base/signal.h b/include/libcamera/base/signal.h new file mode 100644 index 00000000..c2521769 --- /dev/null +++ b/include/libcamera/base/signal.h @@ -0,0 +1,132 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * signal.h - Signal & slot implementation + */ +#ifndef __LIBCAMERA_BASE_SIGNAL_H__ +#define __LIBCAMERA_BASE_SIGNAL_H__ + +#include +#include +#include +#include + +#include +#include + +namespace libcamera { + +class SignalBase +{ +public: + void disconnect(Object *object); + +protected: + using SlotList = std::list; + + void connect(BoundMethodBase *slot); + void disconnect(std::function match); + + SlotList slots(); + +private: + SlotList slots_; +}; + +template +class Signal : public SignalBase +{ +public: + ~Signal() + { + disconnect(); + } + +#ifndef __DOXYGEN__ + template::value> * = nullptr> + void connect(T *obj, R (T::*func)(Args...), + ConnectionType type = ConnectionTypeAuto) + { + Object *object = static_cast(obj); + SignalBase::connect(new BoundMethodMember(obj, object, func, type)); + } + + template::value> * = nullptr> +#else + template +#endif + void connect(T *obj, R (T::*func)(Args...)) + { + SignalBase::connect(new BoundMethodMember(obj, nullptr, func)); + } + + template + void connect(R (*func)(Args...)) + { + SignalBase::connect(new BoundMethodStatic(func)); + } + + void disconnect() + { + SignalBase::disconnect([]([[maybe_unused]] SlotList::iterator &iter) { + return true; + }); + } + + template + void disconnect(T *obj) + { + SignalBase::disconnect([obj](SlotList::iterator &iter) { + return (*iter)->match(obj); + }); + } + + template + void disconnect(T *obj, R (T::*func)(Args...)) + { + SignalBase::disconnect([obj, func](SlotList::iterator &iter) { + BoundMethodArgs *slot = + static_cast *>(*iter); + + if (!slot->match(obj)) + return false; + + /* + * If the object matches the slot, the slot is + * guaranteed to be a member slot, so we can safely + * cast it to BoundMethodMember to match + * func. + */ + return static_cast *>(slot)->match(func); + }); + } + + template + void disconnect(R (*func)(Args...)) + { + SignalBase::disconnect([func](SlotList::iterator &iter) { + BoundMethodArgs *slot = + static_cast *>(*iter); + + if (!slot->match(nullptr)) + return false; + + return static_cast *>(slot)->match(func); + }); + } + + void emit(Args... args) + { + /* + * Make a copy of the slots list as the slot could call the + * disconnect operation, invalidating the iterator. + */ + for (BoundMethodBase *slot : slots()) + static_cast *>(slot)->activate(args...); + } +}; + +} /* namespace libcamera */ + +#endif /* __LIBCAMERA_BASE_SIGNAL_H__ */ diff --git a/include/libcamera/base/thread.h b/include/libcamera/base/thread.h new file mode 100644 index 00000000..2ed18d49 --- /dev/null +++ b/include/libcamera/base/thread.h @@ -0,0 +1,76 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * thread.h - Thread support + */ +#ifndef __LIBCAMERA_BASE_THREAD_H__ +#define __LIBCAMERA_BASE_THREAD_H__ + +#include +#include +#include +#include + +#include +#include +#include + +namespace libcamera { + +class EventDispatcher; +class Message; +class Object; +class ThreadData; +class ThreadMain; + +using Mutex = std::mutex; +using MutexLocker = std::unique_lock; + +class Thread +{ +public: + Thread(); + virtual ~Thread(); + + void start(); + void exit(int code = 0); + bool wait(utils::duration duration = utils::duration::max()); + + bool isRunning(); + + Signal finished; + + static Thread *current(); + static pid_t currentId(); + + EventDispatcher *eventDispatcher(); + + void dispatchMessages(Message::Type type = Message::Type::None); + +protected: + int exec(); + virtual void run(); + +private: + void startThread(); + void finishThread(); + + void postMessage(std::unique_ptr msg, Object *receiver); + void removeMessages(Object *receiver); + + friend class Object; + friend class ThreadData; + friend class ThreadMain; + + void moveObject(Object *object); + void moveObject(Object *object, ThreadData *currentData, + ThreadData *targetData); + + std::thread thread_; + ThreadData *data_; +}; + +} /* namespace libcamera */ + +#endif /* __LIBCAMERA_BASE_THREAD_H__ */ diff --git a/include/libcamera/base/timer.h b/include/libcamera/base/timer.h new file mode 100644 index 00000000..e79e85f1 --- /dev/null +++ b/include/libcamera/base/timer.h @@ -0,0 +1,49 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * timer.h - Generic timer + */ +#ifndef __LIBCAMERA_BASE_TIMER_H__ +#define __LIBCAMERA_BASE_TIMER_H__ + +#include +#include + +#include +#include + +namespace libcamera { + +class Message; + +class Timer : public Object +{ +public: + Timer(Object *parent = nullptr); + ~Timer(); + + void start(unsigned int msec) { start(std::chrono::milliseconds(msec)); } + void start(std::chrono::milliseconds duration); + void start(std::chrono::steady_clock::time_point deadline); + void stop(); + bool isRunning() const; + + std::chrono::steady_clock::time_point deadline() const { return deadline_; } + + Signal timeout; + +protected: + void message(Message *msg) override; + +private: + void registerTimer(); + void unregisterTimer(); + + bool running_; + std::chrono::steady_clock::time_point deadline_; +}; + +} /* namespace libcamera */ + +#endif /* __LIBCAMERA_BASE_TIMER_H__ */ diff --git a/include/libcamera/bound_method.h b/include/libcamera/bound_method.h deleted file mode 100644 index 4fc445ec..00000000 --- a/include/libcamera/bound_method.h +++ /dev/null @@ -1,239 +0,0 @@ -/* SPDX-License-Identifier: LGPL-2.1-or-later */ -/* - * Copyright (C) 2019, Google Inc. - * - * bound_method.h - Method bind and invocation - */ -#ifndef __LIBCAMERA_BOUND_METHOD_H__ -#define __LIBCAMERA_BOUND_METHOD_H__ - -#include -#include -#include -#include - -namespace libcamera { - -class Object; - -enum ConnectionType { - ConnectionTypeAuto, - ConnectionTypeDirect, - ConnectionTypeQueued, - ConnectionTypeBlocking, -}; - -class BoundMethodPackBase -{ -public: - virtual ~BoundMethodPackBase() = default; -}; - -template -class BoundMethodPack : public BoundMethodPackBase -{ -public: - BoundMethodPack(const Args &... args) - : args_(args...) - { - } - - std::tuple...> args_; - R ret_; -}; - -template -class BoundMethodPack : public BoundMethodPackBase -{ -public: - BoundMethodPack(const Args &... args) - : args_(args...) - { - } - - std::tuple...> args_; -}; - -class BoundMethodBase -{ -public: - BoundMethodBase(void *obj, Object *object, ConnectionType type) - : obj_(obj), object_(object), connectionType_(type) - { - } - virtual ~BoundMethodBase() = default; - - template::value> * = nullptr> - bool match(T *obj) { return obj == obj_; } - bool match(Object *object) { return object == object_; } - - Object *object() const { return object_; } - - virtual void invokePack(BoundMethodPackBase *pack) = 0; - -protected: - bool activatePack(std::shared_ptr pack, - bool deleteMethod); - - void *obj_; - Object *object_; - -private: - ConnectionType connectionType_; -}; - -template -class BoundMethodArgs : public BoundMethodBase -{ -public: - using PackType = BoundMethodPack; - -private: - template - void invokePack(BoundMethodPackBase *pack, std::index_sequence) - { - PackType *args = static_cast(pack); - args->ret_ = invoke(std::get(args->args_)...); - } - -public: - BoundMethodArgs(void *obj, Object *object, ConnectionType type) - : BoundMethodBase(obj, object, type) {} - - void invokePack(BoundMethodPackBase *pack) override - { - invokePack(pack, std::make_index_sequence{}); - } - - virtual R activate(Args... args, bool deleteMethod = false) = 0; - virtual R invoke(Args... args) = 0; -}; - -template -class BoundMethodArgs : public BoundMethodBase -{ -public: - using PackType = BoundMethodPack; - -private: - template - void invokePack(BoundMethodPackBase *pack, std::index_sequence) - { - /* args is effectively unused when the sequence I is empty. */ - PackType *args [[gnu::unused]] = static_cast(pack); - invoke(std::get(args->args_)...); - } - -public: - BoundMethodArgs(void *obj, Object *object, ConnectionType type) - : BoundMethodBase(obj, object, type) {} - - void invokePack(BoundMethodPackBase *pack) override - { - invokePack(pack, std::make_index_sequence{}); - } - - virtual void activate(Args... args, bool deleteMethod = false) = 0; - virtual void invoke(Args... args) = 0; -}; - -template -class BoundMethodMember : public BoundMethodArgs -{ -public: - using PackType = typename BoundMethodArgs::PackType; - - BoundMethodMember(T *obj, Object *object, R (T::*func)(Args...), - ConnectionType type = ConnectionTypeAuto) - : BoundMethodArgs(obj, object, type), func_(func) - { - } - - bool match(R (T::*func)(Args...)) const { return func == func_; } - - R activate(Args... args, bool deleteMethod = false) override - { - if (!this->object_) { - T *obj = static_cast(this->obj_); - return (obj->*func_)(args...); - } - - auto pack = std::make_shared(args...); - bool sync = BoundMethodBase::activatePack(pack, deleteMethod); - return sync ? pack->ret_ : R(); - } - - R invoke(Args... args) override - { - T *obj = static_cast(this->obj_); - return (obj->*func_)(args...); - } - -private: - R (T::*func_)(Args...); -}; - -template -class BoundMethodMember : public BoundMethodArgs -{ -public: - using PackType = typename BoundMethodArgs::PackType; - - BoundMethodMember(T *obj, Object *object, void (T::*func)(Args...), - ConnectionType type = ConnectionTypeAuto) - : BoundMethodArgs(obj, object, type), func_(func) - { - } - - bool match(void (T::*func)(Args...)) const { return func == func_; } - - void activate(Args... args, bool deleteMethod = false) override - { - if (!this->object_) { - T *obj = static_cast(this->obj_); - return (obj->*func_)(args...); - } - - auto pack = std::make_shared(args...); - BoundMethodBase::activatePack(pack, deleteMethod); - } - - void invoke(Args... args) override - { - T *obj = static_cast(this->obj_); - return (obj->*func_)(args...); - } - -private: - void (T::*func_)(Args...); -}; - -template -class BoundMethodStatic : public BoundMethodArgs -{ -public: - BoundMethodStatic(R (*func)(Args...)) - : BoundMethodArgs(nullptr, nullptr, ConnectionTypeAuto), - func_(func) - { - } - - bool match(R (*func)(Args...)) const { return func == func_; } - - R activate(Args... args, [[maybe_unused]] bool deleteMethod = false) override - { - return (*func_)(args...); - } - - R invoke(Args...) override - { - return R(); - } - -private: - R (*func_)(Args...); -}; - -} /* namespace libcamera */ - -#endif /* __LIBCAMERA_BOUND_METHOD_H__ */ diff --git a/include/libcamera/camera.h b/include/libcamera/camera.h index ea091400..b081907e 100644 --- a/include/libcamera/camera.h +++ b/include/libcamera/camera.h @@ -13,11 +13,11 @@ #include #include +#include +#include #include -#include #include -#include #include #include diff --git a/include/libcamera/camera_manager.h b/include/libcamera/camera_manager.h index 5deede03..744e5a06 100644 --- a/include/libcamera/camera_manager.h +++ b/include/libcamera/camera_manager.h @@ -13,9 +13,8 @@ #include #include - -#include -#include +#include +#include namespace libcamera { diff --git a/include/libcamera/internal/camera_sensor.h b/include/libcamera/internal/camera_sensor.h index 7bc54061..db12b07e 100644 --- a/include/libcamera/internal/camera_sensor.h +++ b/include/libcamera/internal/camera_sensor.h @@ -12,13 +12,13 @@ #include #include +#include #include #include #include #include "libcamera/internal/formats.h" -#include "libcamera/internal/log.h" #include "libcamera/internal/v4l2_subdevice.h" namespace libcamera { diff --git a/include/libcamera/internal/device_enumerator.h b/include/libcamera/internal/device_enumerator.h index 707cfe8f..222caf75 100644 --- a/include/libcamera/internal/device_enumerator.h +++ b/include/libcamera/internal/device_enumerator.h @@ -13,7 +13,7 @@ #include -#include +#include namespace libcamera { diff --git a/include/libcamera/internal/event_dispatcher.h b/include/libcamera/internal/event_dispatcher.h deleted file mode 100644 index e6a8ad65..00000000 --- a/include/libcamera/internal/event_dispatcher.h +++ /dev/null @@ -1,35 +0,0 @@ -/* SPDX-License-Identifier: LGPL-2.1-or-later */ -/* - * Copyright (C) 2019, Google Inc. - * - * event_dispatcher.h - Event dispatcher - */ -#ifndef __LIBCAMERA_INTERNAL_EVENT_DISPATCHER_H__ -#define __LIBCAMERA_INTERNAL_EVENT_DISPATCHER_H__ - -#include - -namespace libcamera { - -class EventNotifier; -class Timer; - -class EventDispatcher -{ -public: - virtual ~EventDispatcher(); - - virtual void registerEventNotifier(EventNotifier *notifier) = 0; - virtual void unregisterEventNotifier(EventNotifier *notifier) = 0; - - virtual void registerTimer(Timer *timer) = 0; - virtual void unregisterTimer(Timer *timer) = 0; - - virtual void processEvents() = 0; - - virtual void interrupt() = 0; -}; - -} /* namespace libcamera */ - -#endif /* __LIBCAMERA_INTERNAL_EVENT_DISPATCHER_H__ */ diff --git a/include/libcamera/internal/event_dispatcher_poll.h b/include/libcamera/internal/event_dispatcher_poll.h deleted file mode 100644 index 33de051d..00000000 --- a/include/libcamera/internal/event_dispatcher_poll.h +++ /dev/null @@ -1,58 +0,0 @@ -/* SPDX-License-Identifier: LGPL-2.1-or-later */ -/* - * Copyright (C) 2019, Google Inc. - * - * event_dispatcher_poll.h - Poll-based event dispatcher - */ -#ifndef __LIBCAMERA_INTERNAL_EVENT_DISPATCHER_POLL_H__ -#define __LIBCAMERA_INTERNAL_EVENT_DISPATCHER_POLL_H__ - -#include -#include -#include - -#include "libcamera/internal/event_dispatcher.h" - -struct pollfd; - -namespace libcamera { - -class EventNotifier; -class Timer; - -class EventDispatcherPoll final : public EventDispatcher -{ -public: - EventDispatcherPoll(); - ~EventDispatcherPoll(); - - void registerEventNotifier(EventNotifier *notifier); - void unregisterEventNotifier(EventNotifier *notifier); - - void registerTimer(Timer *timer); - void unregisterTimer(Timer *timer); - - void processEvents(); - void interrupt(); - -private: - struct EventNotifierSetPoll { - short events() const; - EventNotifier *notifiers[3]; - }; - - int poll(std::vector *pollfds); - void processInterrupt(const struct pollfd &pfd); - void processNotifiers(const std::vector &pollfds); - void processTimers(); - - std::map notifiers_; - std::list timers_; - int eventfd_; - - bool processingEvents_; -}; - -} /* namespace libcamera */ - -#endif /* __LIBCAMERA_INTERNAL_EVENT_DISPATCHER_POLL_H__ */ diff --git a/include/libcamera/internal/event_notifier.h b/include/libcamera/internal/event_notifier.h index cc3495c0..8a6419f2 100644 --- a/include/libcamera/internal/event_notifier.h +++ b/include/libcamera/internal/event_notifier.h @@ -7,8 +7,8 @@ #ifndef __LIBCAMERA_INTERNAL_EVENT_NOTIFIER_H__ #define __LIBCAMERA_INTERNAL_EVENT_NOTIFIER_H__ -#include -#include +#include +#include namespace libcamera { diff --git a/include/libcamera/internal/ipa_data_serializer.h b/include/libcamera/internal/ipa_data_serializer.h index 70168acc..76325b1d 100644 --- a/include/libcamera/internal/ipa_data_serializer.h +++ b/include/libcamera/internal/ipa_data_serializer.h @@ -14,6 +14,8 @@ #include #include +#include + #include #include #include @@ -22,7 +24,6 @@ #include "libcamera/internal/byte_stream_buffer.h" #include "libcamera/internal/camera_sensor.h" #include "libcamera/internal/control_serializer.h" -#include "libcamera/internal/log.h" namespace libcamera { diff --git a/include/libcamera/internal/ipa_manager.h b/include/libcamera/internal/ipa_manager.h index e904a2be..34224e33 100644 --- a/include/libcamera/internal/ipa_manager.h +++ b/include/libcamera/internal/ipa_manager.h @@ -10,11 +10,12 @@ #include #include +#include + #include #include #include "libcamera/internal/ipa_module.h" -#include "libcamera/internal/log.h" #include "libcamera/internal/pipeline_handler.h" #include "libcamera/internal/pub_key.h" diff --git a/include/libcamera/internal/ipa_module.h b/include/libcamera/internal/ipa_module.h index 19fc5827..a87f5650 100644 --- a/include/libcamera/internal/ipa_module.h +++ b/include/libcamera/internal/ipa_module.h @@ -11,10 +11,11 @@ #include #include +#include + #include #include -#include "libcamera/internal/log.h" #include "libcamera/internal/pipeline_handler.h" namespace libcamera { diff --git a/include/libcamera/internal/ipc_pipe.h b/include/libcamera/internal/ipc_pipe.h index c9a84b78..e58de340 100644 --- a/include/libcamera/internal/ipc_pipe.h +++ b/include/libcamera/internal/ipc_pipe.h @@ -9,9 +9,9 @@ #include -#include "libcamera/internal/ipc_unixsocket.h" +#include -#include +#include "libcamera/internal/ipc_unixsocket.h" namespace libcamera { diff --git a/include/libcamera/internal/ipc_unixsocket.h b/include/libcamera/internal/ipc_unixsocket.h index e871b650..9f5b0677 100644 --- a/include/libcamera/internal/ipc_unixsocket.h +++ b/include/libcamera/internal/ipc_unixsocket.h @@ -12,7 +12,7 @@ #include #include -#include +#include namespace libcamera { diff --git a/include/libcamera/internal/log.h b/include/libcamera/internal/log.h deleted file mode 100644 index 82e55a62..00000000 --- a/include/libcamera/internal/log.h +++ /dev/null @@ -1,130 +0,0 @@ -/* SPDX-License-Identifier: LGPL-2.1-or-later */ -/* - * Copyright (C) 2018, Google Inc. - * - * log.h - Logging infrastructure - */ -#ifndef __LIBCAMERA_INTERNAL_LOG_H__ -#define __LIBCAMERA_INTERNAL_LOG_H__ - -#include -#include - -#include -#include - -namespace libcamera { - -enum LogSeverity { - LogInvalid = -1, - LogDebug = 0, - LogInfo, - LogWarning, - LogError, - LogFatal, -}; - -class LogCategory -{ -public: - explicit LogCategory(const char *name); - - const char *name() const { return name_; } - LogSeverity severity() const { return severity_; } - void setSeverity(LogSeverity severity); - - static const LogCategory &defaultCategory(); - -private: - const char *name_; - LogSeverity severity_; -}; - -#define LOG_DECLARE_CATEGORY(name) \ -extern const LogCategory &_LOG_CATEGORY(name)(); - -#define LOG_DEFINE_CATEGORY(name) \ -const LogCategory &_LOG_CATEGORY(name)() \ -{ \ - /* The instance will be deleted by the Logger destructor. */ \ - static LogCategory *category = new LogCategory(#name); \ - return *category; \ -} - -class LogMessage -{ -public: - LogMessage(const char *fileName, unsigned int line, - const LogCategory &category, LogSeverity severity); - - LogMessage(LogMessage &&); - ~LogMessage(); - - std::ostream &stream() { return msgStream_; } - - const utils::time_point ×tamp() const { return timestamp_; } - LogSeverity severity() const { return severity_; } - const LogCategory &category() const { return category_; } - const std::string &fileInfo() const { return fileInfo_; } - const std::string msg() const { return msgStream_.str(); } - -private: - LIBCAMERA_DISABLE_COPY(LogMessage) - - void init(const char *fileName, unsigned int line); - - std::ostringstream msgStream_; - const LogCategory &category_; - LogSeverity severity_; - utils::time_point timestamp_; - std::string fileInfo_; -}; - -class Loggable -{ -public: - virtual ~Loggable(); - -protected: - virtual std::string logPrefix() const = 0; - - LogMessage _log(const LogCategory *category, LogSeverity severity, - const char *fileName = __builtin_FILE(), - unsigned int line = __builtin_LINE()) const; -}; - -LogMessage _log(const LogCategory *category, LogSeverity severity, - const char *fileName = __builtin_FILE(), - unsigned int line = __builtin_LINE()); - -#ifndef __DOXYGEN__ -#define _LOG_CATEGORY(name) logCategory##name - -#define _LOG1(severity) \ - _log(nullptr, Log##severity).stream() -#define _LOG2(category, severity) \ - _log(&_LOG_CATEGORY(category)(), Log##severity).stream() - -/* - * Expand the LOG() macro to _LOG1() or _LOG2() based on the number of - * arguments. - */ -#define _LOG_MACRO(_1, _2, NAME, ...) NAME -#define LOG(...) _LOG_MACRO(__VA_ARGS__, _LOG2, _LOG1)(__VA_ARGS__) -#else /* __DOXYGEN___ */ -#define LOG(category, severity) -#endif /* __DOXYGEN__ */ - -#ifndef NDEBUG -#define ASSERT(condition) static_cast(({ \ - if (!(condition)) \ - LOG(Fatal) << "assertion \"" #condition "\" failed in " \ - << __func__ << "()"; \ -})) -#else -#define ASSERT(condition) static_cast(false && (condition)) -#endif - -} /* namespace libcamera */ - -#endif /* __LIBCAMERA_INTERNAL_LOG_H__ */ diff --git a/include/libcamera/internal/media_device.h b/include/libcamera/internal/media_device.h index c3292508..8424611d 100644 --- a/include/libcamera/internal/media_device.h +++ b/include/libcamera/internal/media_device.h @@ -14,9 +14,9 @@ #include -#include +#include +#include -#include "libcamera/internal/log.h" #include "libcamera/internal/media_object.h" namespace libcamera { diff --git a/include/libcamera/internal/meson.build b/include/libcamera/internal/meson.build index 1348e926..cf664fc9 100644 --- a/include/libcamera/internal/meson.build +++ b/include/libcamera/internal/meson.build @@ -22,8 +22,6 @@ libcamera_internal_headers = files([ 'device_enumerator.h', 'device_enumerator_sysfs.h', 'device_enumerator_udev.h', - 'event_dispatcher.h', - 'event_dispatcher_poll.h', 'event_notifier.h', 'file.h', 'formats.h', @@ -31,18 +29,13 @@ libcamera_internal_headers = files([ 'ipa_module.h', 'ipa_proxy.h', 'ipc_unixsocket.h', - 'log.h', 'media_device.h', 'media_object.h', - 'message.h', 'pipeline_handler.h', 'process.h', 'pub_key.h', - 'semaphore.h', 'source_paths.h', 'sysfs.h', - 'thread.h', - 'timer.h', 'v4l2_device.h', 'v4l2_pixelformat.h', 'v4l2_subdevice.h', diff --git a/include/libcamera/internal/message.h b/include/libcamera/internal/message.h deleted file mode 100644 index f1b133bf..00000000 --- a/include/libcamera/internal/message.h +++ /dev/null @@ -1,71 +0,0 @@ -/* SPDX-License-Identifier: LGPL-2.1-or-later */ -/* - * Copyright (C) 2019, Google Inc. - * - * message.h - Message queue support - */ -#ifndef __LIBCAMERA_INTERNAL_MESSAGE_H__ -#define __LIBCAMERA_INTERNAL_MESSAGE_H__ - -#include - -#include - -namespace libcamera { - -class BoundMethodBase; -class Object; -class Semaphore; -class Thread; - -class Message -{ -public: - enum Type { - None = 0, - InvokeMessage = 1, - ThreadMoveMessage = 2, - DeferredDelete = 3, - UserMessage = 1000, - }; - - Message(Type type); - virtual ~Message(); - - Type type() const { return type_; } - Object *receiver() const { return receiver_; } - - static Type registerMessageType(); - -private: - friend class Thread; - - Type type_; - Object *receiver_; - - static std::atomic_uint nextUserType_; -}; - -class InvokeMessage : public Message -{ -public: - InvokeMessage(BoundMethodBase *method, - std::shared_ptr pack, - Semaphore *semaphore = nullptr, - bool deleteMethod = false); - ~InvokeMessage(); - - Semaphore *semaphore() const { return semaphore_; } - - void invoke(); - -private: - BoundMethodBase *method_; - std::shared_ptr pack_; - Semaphore *semaphore_; - bool deleteMethod_; -}; - -} /* namespace libcamera */ - -#endif /* __LIBCAMERA_INTERNAL_MESSAGE_H__ */ diff --git a/include/libcamera/internal/pipeline_handler.h b/include/libcamera/internal/pipeline_handler.h index 8beb6b76..9e2d65d6 100644 --- a/include/libcamera/internal/pipeline_handler.h +++ b/include/libcamera/internal/pipeline_handler.h @@ -16,9 +16,9 @@ #include #include +#include #include -#include #include #include "libcamera/internal/ipa_proxy.h" diff --git a/include/libcamera/internal/process.h b/include/libcamera/internal/process.h index 254cda85..c4d5d9c1 100644 --- a/include/libcamera/internal/process.h +++ b/include/libcamera/internal/process.h @@ -11,7 +11,7 @@ #include #include -#include +#include namespace libcamera { diff --git a/include/libcamera/internal/semaphore.h b/include/libcamera/internal/semaphore.h deleted file mode 100644 index 9dc65d29..00000000 --- a/include/libcamera/internal/semaphore.h +++ /dev/null @@ -1,34 +0,0 @@ -/* SPDX-License-Identifier: LGPL-2.1-or-later */ -/* - * Copyright (C) 2019, Google Inc. - * - * semaphore.h - General-purpose counting semaphore - */ -#ifndef __LIBCAMERA_INTERNAL_SEMAPHORE_H__ -#define __LIBCAMERA_INTERNAL_SEMAPHORE_H__ - -#include - -#include "libcamera/internal/thread.h" - -namespace libcamera { - -class Semaphore -{ -public: - Semaphore(unsigned int n = 0); - - unsigned int available(); - void acquire(unsigned int n = 1); - bool tryAcquire(unsigned int n = 1); - void release(unsigned int n = 1); - -private: - Mutex mutex_; - std::condition_variable cv_; - unsigned int available_; -}; - -} /* namespace libcamera */ - -#endif /* __LIBCAMERA_INTERNAL_SEMAPHORE_H__ */ diff --git a/include/libcamera/internal/thread.h b/include/libcamera/internal/thread.h deleted file mode 100644 index 9662e28b..00000000 --- a/include/libcamera/internal/thread.h +++ /dev/null @@ -1,78 +0,0 @@ -/* SPDX-License-Identifier: LGPL-2.1-or-later */ -/* - * Copyright (C) 2019, Google Inc. - * - * thread.h - Thread support - */ -#ifndef __LIBCAMERA_INTERNAL_THREAD_H__ -#define __LIBCAMERA_INTERNAL_THREAD_H__ - -#include -#include -#include -#include - -#include - -#include - -#include "libcamera/internal/message.h" - -namespace libcamera { - -class EventDispatcher; -class Message; -class Object; -class ThreadData; -class ThreadMain; - -using Mutex = std::mutex; -using MutexLocker = std::unique_lock; - -class Thread -{ -public: - Thread(); - virtual ~Thread(); - - void start(); - void exit(int code = 0); - bool wait(utils::duration duration = utils::duration::max()); - - bool isRunning(); - - Signal finished; - - static Thread *current(); - static pid_t currentId(); - - EventDispatcher *eventDispatcher(); - - void dispatchMessages(Message::Type type = Message::Type::None); - -protected: - int exec(); - virtual void run(); - -private: - void startThread(); - void finishThread(); - - void postMessage(std::unique_ptr msg, Object *receiver); - void removeMessages(Object *receiver); - - friend class Object; - friend class ThreadData; - friend class ThreadMain; - - void moveObject(Object *object); - void moveObject(Object *object, ThreadData *currentData, - ThreadData *targetData); - - std::thread thread_; - ThreadData *data_; -}; - -} /* namespace libcamera */ - -#endif /* __LIBCAMERA_INTERNAL_THREAD_H__ */ diff --git a/include/libcamera/internal/timer.h b/include/libcamera/internal/timer.h deleted file mode 100644 index 013e93c0..00000000 --- a/include/libcamera/internal/timer.h +++ /dev/null @@ -1,49 +0,0 @@ -/* SPDX-License-Identifier: LGPL-2.1-or-later */ -/* - * Copyright (C) 2019, Google Inc. - * - * timer.h - Generic timer - */ -#ifndef __LIBCAMERA_INTERNAL_TIMER_H__ -#define __LIBCAMERA_INTERNAL_TIMER_H__ - -#include -#include - -#include -#include - -namespace libcamera { - -class Message; - -class Timer : public Object -{ -public: - Timer(Object *parent = nullptr); - ~Timer(); - - void start(unsigned int msec) { start(std::chrono::milliseconds(msec)); } - void start(std::chrono::milliseconds duration); - void start(std::chrono::steady_clock::time_point deadline); - void stop(); - bool isRunning() const; - - std::chrono::steady_clock::time_point deadline() const { return deadline_; } - - Signal timeout; - -protected: - void message(Message *msg) override; - -private: - void registerTimer(); - void unregisterTimer(); - - bool running_; - std::chrono::steady_clock::time_point deadline_; -}; - -} /* namespace libcamera */ - -#endif /* __LIBCAMERA_INTERNAL_TIMER_H__ */ diff --git a/include/libcamera/internal/v4l2_device.h b/include/libcamera/internal/v4l2_device.h index c318e976..1edd664a 100644 --- a/include/libcamera/internal/v4l2_device.h +++ b/include/libcamera/internal/v4l2_device.h @@ -13,12 +13,12 @@ #include +#include +#include + #include -#include #include -#include "libcamera/internal/log.h" - namespace libcamera { class EventNotifier; diff --git a/include/libcamera/internal/v4l2_subdevice.h b/include/libcamera/internal/v4l2_subdevice.h index d07dd6b4..97b89fb9 100644 --- a/include/libcamera/internal/v4l2_subdevice.h +++ b/include/libcamera/internal/v4l2_subdevice.h @@ -12,11 +12,11 @@ #include #include +#include #include #include "libcamera/internal/formats.h" -#include "libcamera/internal/log.h" #include "libcamera/internal/media_object.h" #include "libcamera/internal/v4l2_device.h" diff --git a/include/libcamera/internal/v4l2_videodevice.h b/include/libcamera/internal/v4l2_videodevice.h index 227d015e..a7c3d529 100644 --- a/include/libcamera/internal/v4l2_videodevice.h +++ b/include/libcamera/internal/v4l2_videodevice.h @@ -17,14 +17,14 @@ #include #include +#include +#include #include #include #include -#include #include "libcamera/internal/formats.h" -#include "libcamera/internal/log.h" #include "libcamera/internal/v4l2_device.h" #include "libcamera/internal/v4l2_pixelformat.h" diff --git a/include/libcamera/ipa/ipa_interface.h b/include/libcamera/ipa/ipa_interface.h index 4aefaa71..9a15c86b 100644 --- a/include/libcamera/ipa/ipa_interface.h +++ b/include/libcamera/ipa/ipa_interface.h @@ -13,10 +13,11 @@ #include #include +#include + #include #include #include -#include namespace libcamera { diff --git a/include/libcamera/meson.build b/include/libcamera/meson.build index 21a43388..c69d63d2 100644 --- a/include/libcamera/meson.build +++ b/include/libcamera/meson.build @@ -1,7 +1,6 @@ # SPDX-License-Identifier: CC0-1.0 libcamera_public_headers = files([ - 'bound_method.h', 'buffer.h', 'camera.h', 'camera_manager.h', @@ -11,10 +10,8 @@ libcamera_public_headers = files([ 'framebuffer_allocator.h', 'geometry.h', 'logging.h', - 'object.h', 'pixel_format.h', 'request.h', - 'signal.h', 'span.h', 'stream.h', 'transform.h', diff --git a/include/libcamera/object.h b/include/libcamera/object.h deleted file mode 100644 index a1882f05..00000000 --- a/include/libcamera/object.h +++ /dev/null @@ -1,71 +0,0 @@ -/* SPDX-License-Identifier: LGPL-2.1-or-later */ -/* - * Copyright (C) 2019, Google Inc. - * - * object.h - Base object - */ -#ifndef __LIBCAMERA_OBJECT_H__ -#define __LIBCAMERA_OBJECT_H__ - -#include -#include -#include - -#include - -namespace libcamera { - -class Message; -template -class Signal; -class SignalBase; -class Thread; - -class Object -{ -public: - Object(Object *parent = nullptr); - virtual ~Object(); - - void deleteLater(); - - void postMessage(std::unique_ptr msg); - - template::value> * = nullptr> - R invokeMethod(R (T::*func)(FuncArgs...), ConnectionType type, - Args... args) - { - T *obj = static_cast(this); - auto *method = new BoundMethodMember(obj, this, func, type); - return method->activate(args..., true); - } - - Thread *thread() const { return thread_; } - void moveToThread(Thread *thread); - - Object *parent() const { return parent_; } - -protected: - virtual void message(Message *msg); - -private: - friend class SignalBase; - friend class Thread; - - void notifyThreadMove(); - - void connect(SignalBase *signal); - void disconnect(SignalBase *signal); - - Object *parent_; - std::vector children_; - - Thread *thread_; - std::list signals_; - unsigned int pendingMessages_; -}; - -} /* namespace libcamera */ - -#endif /* __LIBCAMERA_OBJECT_H__ */ diff --git a/include/libcamera/request.h b/include/libcamera/request.h index 00c646fe..2d361c9d 100644 --- a/include/libcamera/request.h +++ b/include/libcamera/request.h @@ -14,9 +14,9 @@ #include #include +#include #include -#include namespace libcamera { diff --git a/include/libcamera/signal.h b/include/libcamera/signal.h deleted file mode 100644 index 3233529a..00000000 --- a/include/libcamera/signal.h +++ /dev/null @@ -1,132 +0,0 @@ -/* SPDX-License-Identifier: LGPL-2.1-or-later */ -/* - * Copyright (C) 2019, Google Inc. - * - * signal.h - Signal & slot implementation - */ -#ifndef __LIBCAMERA_SIGNAL_H__ -#define __LIBCAMERA_SIGNAL_H__ - -#include -#include -#include -#include - -#include -#include - -namespace libcamera { - -class SignalBase -{ -public: - void disconnect(Object *object); - -protected: - using SlotList = std::list; - - void connect(BoundMethodBase *slot); - void disconnect(std::function match); - - SlotList slots(); - -private: - SlotList slots_; -}; - -template -class Signal : public SignalBase -{ -public: - ~Signal() - { - disconnect(); - } - -#ifndef __DOXYGEN__ - template::value> * = nullptr> - void connect(T *obj, R (T::*func)(Args...), - ConnectionType type = ConnectionTypeAuto) - { - Object *object = static_cast(obj); - SignalBase::connect(new BoundMethodMember(obj, object, func, type)); - } - - template::value> * = nullptr> -#else - template -#endif - void connect(T *obj, R (T::*func)(Args...)) - { - SignalBase::connect(new BoundMethodMember(obj, nullptr, func)); - } - - template - void connect(R (*func)(Args...)) - { - SignalBase::connect(new BoundMethodStatic(func)); - } - - void disconnect() - { - SignalBase::disconnect([]([[maybe_unused]] SlotList::iterator &iter) { - return true; - }); - } - - template - void disconnect(T *obj) - { - SignalBase::disconnect([obj](SlotList::iterator &iter) { - return (*iter)->match(obj); - }); - } - - template - void disconnect(T *obj, R (T::*func)(Args...)) - { - SignalBase::disconnect([obj, func](SlotList::iterator &iter) { - BoundMethodArgs *slot = - static_cast *>(*iter); - - if (!slot->match(obj)) - return false; - - /* - * If the object matches the slot, the slot is - * guaranteed to be a member slot, so we can safely - * cast it to BoundMethodMember to match - * func. - */ - return static_cast *>(slot)->match(func); - }); - } - - template - void disconnect(R (*func)(Args...)) - { - SignalBase::disconnect([func](SlotList::iterator &iter) { - BoundMethodArgs *slot = - static_cast *>(*iter); - - if (!slot->match(nullptr)) - return false; - - return static_cast *>(slot)->match(func); - }); - } - - void emit(Args... args) - { - /* - * Make a copy of the slots list as the slot could call the - * disconnect operation, invalidating the iterator. - */ - for (BoundMethodBase *slot : slots()) - static_cast *>(slot)->activate(args...); - } -}; - -} /* namespace libcamera */ - -#endif /* __LIBCAMERA_SIGNAL_H__ */ -- cgit v1.2.1