MayaFlux 0.3.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
SubsystemManager.hpp
Go to the documentation of this file.
1#pragma once
2
4
5namespace MayaFlux::Core {
6
7class WindowManager;
11
12struct GlobalStreamInfo;
13struct GlobalGraphicsConfig;
14struct GlobalInputConfig;
15
16/**
17 * @class SubsystemManager
18 * @brief Central coordinator for all subsystems in the MayaFlux processing architecture
19 *
20 * Manages subsystem lifecycle, provides token-scoped processing handles, and coordinates
21 * cross-subsystem operations. Each subsystem receives a dedicated processing handle that
22 * ensures proper isolation and thread safety within its designated processing domain.
23 *
24 * Key responsibilities:
25 * - Subsystem registration and lifecycle management
26 * - Processing handle creation and distribution
27 * - Cross-subsystem data access control
28 * - Coordinated startup and shutdown sequences
29 */
30class MAYAFLUX_API SubsystemManager {
31public:
32 /**
33 * @brief Constructs SubsystemManager with required processing managers
34 * @param node_graph_manager Shared node graph manager for all subsystems
35 * @param buffer_manager Shared buffer manager for all subsystems
36 * @param task_scheduler Shared task scheduler for all subsystems
37 * @param window_manager Optional shared window manager for graphics subsystems
38 * @param input_manager Optional shared input manager for input subsystems
39 *
40 * Initializes the manager with references to the core processing systems.
41 * These managers are shared across all subsystems but accessed through
42 * token-scoped handles for proper isolation.
43 */
45 std::shared_ptr<Nodes::NodeGraphManager> node_graph_manager,
46 std::shared_ptr<Buffers::BufferManager> buffer_manager,
47 std::shared_ptr<Vruta::TaskScheduler> task_scheduler,
48 std::shared_ptr<Core::WindowManager> window_manager = nullptr,
49 std::shared_ptr<InputManager> input_manager = nullptr);
50
51 /**
52 * @brief Internal template method for type-safe subsystem creation
53 * @tparam SType Type of subsystem to create
54 * @tparam Args Constructor argument types
55 * @param type SubsystemType enum value identifying the subsystem category
56 * @param args Constructor arguments for the subsystem
57 *
58 * Creates a subsystem instance and registers it with the manager.
59 * Used internally by specific subsystem creation methods.
60 */
61 template <typename SType, typename... Args>
62 void create_subsystem_internal(SubsystemType type, Args&&... args)
63 {
64 auto subsystem = std::make_shared<SType>(std::forward<Args>(args)...);
65 add_subsystem(type, std::move(subsystem));
66 }
67
68 /**
69 * @brief Create and register the audio subsystem
70 * @tparam Args Constructor argument types
71 *
72 * Specialized creation method for AudioSubsystem. Only one audio subsystem
73 * is allowed per manager instance.
74 */
75 void create_audio_subsystem(GlobalStreamInfo& stream_info);
76
77 /**
78 * @brief Create and register the graphics subsystem
79 * @param graphics_config Global graphics configuration
80 *
81 * Specialized creation method for GraphicsSubsystem. Only one graphics
82 * subsystem is allowed per manager instance.
83 */
84 void create_graphics_subsystem(const GlobalGraphicsConfig& graphics_config);
85
86 /**
87 * @brief Create and register the input subsystem
88 * @param input_config Global input configuration
89 *
90 * Specialized creation method for InputSubsystem. Only one input
91 * subsystem is allowed per manager instance.
92 */
93 void create_input_subsystem(GlobalInputConfig& input_config);
94
95 /** @brief Start all registered subsystems in coordination */
96 void start_all_subsystems();
97
98 /** @brief Pause all subsystems */
99 void pause_all_subsystems();
100
101 /** @brief Resume all paused subsystems */
102 void resume_all_subsystems();
103
104 /**
105 * @brief Get access to a specific subsystem by type
106 * @param type SubsystemType enum value identifying the subsystem
107 * @return Shared pointer to subsystem or nullptr if not found
108 *
109 * Provides access to registered subsystems for direct interaction.
110 * Returns nullptr if subsystem of specified type doesn't exist.
111 */
112 std::shared_ptr<ISubsystem> get_subsystem(SubsystemType type);
113
114 /**
115 * @brief Get typed access to the audio subsystem
116 * @return Shared pointer to AudioSubsystem or nullptr if not created
117 *
118 * Convenience method that automatically casts to AudioSubsystem type.
119 * Equivalent to dynamic_cast on get_subsystem(SubsystemType::AUDIO).
120 */
121 std::shared_ptr<AudioSubsystem> get_audio_subsystem();
122
123 /**
124 * @brief Get typed access to the graphics subsystem
125 * @return Shared pointer to GraphicsSubsystem or nullptr if not created
126 *
127 * Convenience method that automatically casts to GraphicsSubsystem type.
128 * Equivalent to dynamic_cast on get_subsystem(SubsystemType::GRAPHICS).
129 */
130 std::shared_ptr<GraphicsSubsystem> get_graphics_subsystem();
131
132 /**
133 * @brief Get typed access to the input subsystem
134 * @return Shared pointer to InputSubsystem or nullptr if not created
135 *
136 * Convenience method that automatically casts to InputSubsystem type.
137 * Equivalent to dynamic_cast on get_subsystem(SubsystemType::INPUT).
138 */
139 std::shared_ptr<InputSubsystem> get_input_subsystem();
140
141 /**
142 * @brief Check if a subsystem type exists
143 * @param type SubsystemType to check for existence
144 * @return True if subsystem of this type is registered
145 *
146 * Fast existence check without retrieving the subsystem instance.
147 */
148 inline bool has_subsystem(SubsystemType type) const { return m_subsystems.count(type) > 0; }
149
150 /**
151 * @brief Get all currently active subsystem types
152 * @return Vector of SubsystemType values for all registered subsystems
153 *
154 * Returns the types of all subsystems currently managed by this instance.
155 */
156 std::vector<SubsystemType> get_active_subsystem_types() const;
157
158 /**
159 * @brief Register a subsystem instance with the manager
160 * @param type SubsystemType enum value identifying the subsystem category
161 * @param subsystem Shared pointer to the subsystem instance
162 *
163 * Registers a pre-constructed subsystem with the manager and creates
164 * its processing handle. The subsystem is initialized but not started.
165 */
166 void add_subsystem(SubsystemType type, const std::shared_ptr<ISubsystem>& subsystem);
167
168 /**
169 * @brief Remove and shutdown a subsystem
170 * @param type SubsystemType enum value identifying the subsystem to remove
171 *
172 * Stops, shuts down, and removes the specified subsystem from management.
173 * Cleans up associated processing handles and resources.
174 */
175 void remove_subsystem(SubsystemType type);
176
177 /**
178 * @brief Query operational status of all subsystems
179 * @return Map of SubsystemType to boolean indicating ready/running status
180 *
181 * Returns the current operational status of all managed subsystems.
182 * Status reflects whether each subsystem is ready for operation.
183 */
184 std::unordered_map<SubsystemType, std::pair<bool, bool>> query_subsystem_status() const;
185
186 /**
187 * @brief Execute an operation with temporary elevated permissions
188 * @tparam Func Function type for the operation
189 * @param primary_tokens Primary subsystem tokens
190 * @param secondary_tokens Secondary subsystem tokens for cross-domain access
191 * @param operation Function to execute with combined access
192 *
193 * For special cross-domain operations (e.g., audio-reactive visuals).
194 * Creates a temporary handle with combined token access for controlled
195 * cross-subsystem operations.
196 */
197 template <typename Func>
199 SubsystemTokens primary_tokens,
200 SubsystemTokens /*secondary_tokens*/,
201 Func operation)
202 {
203 SubsystemTokens combined_tokens {
204 .Buffer = primary_tokens.Buffer,
205 .Node = primary_tokens.Node,
206 .Task = primary_tokens.Task
207 };
208
209 SubsystemProcessingHandle temp_handle(
210 m_buffer_manager,
211 m_node_graph_manager,
212 m_task_scheduler,
213 m_window_manager,
214 m_input_manager,
215 combined_tokens);
216
217 operation(temp_handle);
218 }
219
220 /** @brief Stop all subsystems */
221 void stop();
222
223 /** @brief Shutdown all subsystems in proper order */
224 void shutdown();
225
226 /**
227 * @brief Configure cross-subsystem data access permissions
228 * @param from SubsystemType that requests access
229 * @param to SubsystemType that provides data
230 *
231 * Establishes permission for one subsystem to read data from another.
232 * Required for cross-subsystem operations like audio-reactive visuals.
233 */
234 void allow_cross_access(SubsystemType from, SubsystemType to);
235
236 /**
237 * @brief Read data from another subsystem's buffers
238 * @param requesting_type SubsystemType making the data request
239 * @param target_type SubsystemType providing the data
240 * @param channel Channel index to read from
241 * @return Read-only span if access allowed, nullopt otherwise
242 *
243 * Enables controlled cross-subsystem data sharing with permission checking.
244 * Used for scenarios where one subsystem needs processed data from another.
245 */
246 std::optional<std::span<const double>> read_cross_subsystem_buffer(
247 SubsystemType requesting_type,
248 SubsystemType target_type,
249 uint32_t channel);
250
251 /**
252 * @brief Register a processing hook for a specific subsystem
253 * @param type SubsystemType to attach the hook to
254 * @param name Unique identifier for the hook
255 * @param hook Callback function to execute
256 * @param position When to execute the hook (PRE_PROCESS or POST_PROCESS)
257 *
258 * Process hooks allow custom code execution at specific points in the
259 * processing cycle. Used for monitoring, debugging, or additional processing.
260 */
261 void register_process_hook(SubsystemType type, const std::string& name, ProcessHook hook, HookPosition position = HookPosition::POST_PROCESS);
262
263 /**
264 * @brief Remove a previously registered processing hook
265 * @param type SubsystemType the hook is attached to
266 * @param name Unique identifier of the hook to remove
267 *
268 * Removes the named hook from the specified subsystem's processing cycle.
269 */
270 void unregister_process_hook(SubsystemType type, const std::string& name);
271
272 /**
273 * @brief Check if a processing hook exists
274 * @param type SubsystemType to check
275 * @param name Hook identifier to look for
276 * @return True if hook exists (in either pre or post position)
277 *
278 * Checks both pre-process and post-process hook collections for the named hook.
279 */
280 bool has_process_hook(SubsystemType type, const std::string& name);
281
282 /**
283 * @brief Get processing handle with validation
284 * @param type Subsystem type
285 * @return Valid handle or nullptr if validation fails
286 */
287 SubsystemProcessingHandle* get_validated_handle(SubsystemType type) const;
288
289 /** @brief Stop the audio subsystem */
290 void stop_audio_subsystem();
291
292 /** @brief Stop the graphics subsystem */
293 void stop_graphics_subsystem();
294
295 /** @brief Stop the input subsystem */
296 void stop_input_subsystem();
297
298private:
299 bool is_cross_access_allowed(SubsystemType from, SubsystemType to) const;
300
302
303 std::shared_ptr<Nodes::NodeGraphManager> m_node_graph_manager;
304 std::shared_ptr<Buffers::BufferManager> m_buffer_manager;
305 std::shared_ptr<Vruta::TaskScheduler> m_task_scheduler;
306 std::shared_ptr<Core::WindowManager> m_window_manager;
307 std::shared_ptr<InputManager> m_input_manager;
308
309 std::unordered_map<SubsystemType, std::shared_ptr<ISubsystem>> m_subsystems;
310 std::unordered_map<SubsystemType, std::unique_ptr<SubsystemProcessingHandle>> m_handles;
311 std::unordered_map<SubsystemType, std::unordered_set<SubsystemType>> m_cross_access_permissions;
312
313 mutable std::shared_mutex m_mutex; ///< Thread safety for subsystem operations
314};
315
316}
std::shared_ptr< Buffers::BufferManager > m_buffer_manager
std::shared_ptr< Nodes::NodeGraphManager > m_node_graph_manager
std::shared_ptr< Vruta::TaskScheduler > m_task_scheduler
SubsystemTokens get_tokens_for_type(SubsystemType type) const
std::unordered_map< SubsystemType, std::unordered_set< SubsystemType > > m_cross_access_permissions
bool has_subsystem(SubsystemType type) const
Check if a subsystem type exists.
void execute_with_combined_tokens(SubsystemTokens primary_tokens, SubsystemTokens, Func operation)
Execute an operation with temporary elevated permissions.
std::shared_ptr< Core::WindowManager > m_window_manager
std::unordered_map< SubsystemType, std::unique_ptr< SubsystemProcessingHandle > > m_handles
void create_subsystem_internal(SubsystemType type, Args &&... args)
Internal template method for type-safe subsystem creation.
std::vector< SubsystemType > get_active_subsystem_types() const
Get all currently active subsystem types.
std::unordered_map< SubsystemType, std::shared_ptr< ISubsystem > > m_subsystems
std::shared_mutex m_mutex
Thread safety for subsystem operations.
std::shared_ptr< InputManager > m_input_manager
Central coordinator for all subsystems in the MayaFlux processing architecture.
Unified interface combining buffer and node processing for subsystems.
std::function< void(unsigned int num_frames)> ProcessHook
Function type for process hooks that can be registered with the engine.
HookPosition
Defines the position in the processing cycle where a hook should be executed.
@ AudioSubsystem
Audio subsystem operations (backend, device, stream management)
@ GraphicsSubsystem
Graphics subsystem operations (Vulkan, rendering pipeline)
@ InputSubsystem
Input subsystem operations (device management, event dispatch)
const Core::InputSubsystem & get_input_subsystem()
Gets the input subsystem.
Definition Input.cpp:10
Configuration for the InputSubsystem.
Comprehensive configuration for digital audio stream processing.
MayaFlux::Vruta::ProcessingToken Task
Processing token for task scheduling operations.
MayaFlux::Buffers::ProcessingToken Buffer
Processing token for buffer operations.
MayaFlux::Nodes::ProcessingToken Node
Processing token for node graph operations.
Processing token configuration for subsystem operation.