MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
DisplayService.hpp
Go to the documentation of this file.
1#pragma once
2
4
5/**
6 * @brief Backend display and presentation service interface
7 *
8 * Manages window surfaces, swapchains, and frame presentation.
9 * Handles window resize events and ensures proper surface recreation.
10 */
11struct MAYAFLUX_API DisplayService {
12
13 /**
14 * @brief Submit a primary command buffer and present the frame
15 * @param window_handle Window handle
16 * @param primary_command_buffer The primary command buffer (uint64_t bits of vk::CommandBuffer)
17 *
18 * Handles semaphore choreography: waits on image_available, signals render_finished,
19 * then presents. Must be called after acquire_next_swapchain_image().
20 */
21 std::function<void(const std::shared_ptr<void>&, uint64_t)> submit_and_present;
22
23 /**
24 * @brief Wait for all GPU operations to complete
25 *
26 * Blocks until all submitted command buffers have finished execution.
27 * Used for synchronization before shutdown or major state changes.
28 * Can significantly impact performance - use sparingly.
29 */
30 std::function<void()> wait_idle;
31
32 /**
33 * @brief Resize rendering surface for a window
34 * @param window_handle Window to resize
35 * @param width New width in pixels
36 * @param height New height in pixels
37 *
38 * Recreates swapchain and associated framebuffers for new dimensions.
39 * Must be called when window size changes to avoid rendering artifacts.
40 * Automatically waits for idle before reconstruction.
41 */
42 std::function<void(const std::shared_ptr<void>&, uint32_t, uint32_t)> resize_surface;
43
44 /**
45 * @brief Get current swapchain image count
46 * @param window_handle Window handle
47 * @return Number of images in the swapchain
48 *
49 * Useful for allocating per-frame resources. Typically 2-3 images
50 * for double/triple buffering.
51 */
52 std::function<uint32_t(const std::shared_ptr<void>&)> get_swapchain_image_count;
53
54 /**
55 * @brief Frames submitted for a window since it was registered
56 * @param window_handle Window handle
57 * @return Submission count, or 0 for an unregistered window
58 *
59 * Call from the graphics thread, which is where frames are submitted.
60 */
61 std::function<uint64_t(const std::shared_ptr<void>&)> get_submitted_frame_count;
62
63 /**
64 * @brief Submitted frames of a window whose GPU work has finished
65 * @param window_handle Window handle
66 * @return Completed count, never above the submitted count, or 0 for an
67 * unregistered window
68 *
69 * Never blocks. A resource last used by frames up to the submitted count
70 * read when it was retired is safe to destroy once this reaches that value.
71 * Call from the graphics thread.
72 */
73 std::function<uint64_t(const std::shared_ptr<void>&)> get_completed_frame_count;
74
75 /**
76 * @brief Acquire the next swapchain image for a window
77 * @param window_handle Window handle
78 * @return Index of the acquired swapchain image
79 *
80 * Must be called before get_current_image_view() for dynamic rendering.
81 * Stores the acquired image index internally for subsequent calls.
82 */
83 std::function<uint64_t(const std::shared_ptr<void>&)> acquire_next_swapchain_image;
84
85 /**
86 * @brief Get actual swapchain format for a window
87 * @param window_handle Window handle
88 * @return Vulkan format (vk::Format cast to uint32_t)
89 *
90 * Returns the actual format used by the window's swapchain.
91 * Used to ensure multiple dynamic render calls are compatible
92 */
93 std::function<int(const std::shared_ptr<void>&)> get_swapchain_format;
94
95 /**
96 * @brief Get swapchain extent for a window
97 * @param window_handle Window handle
98 * @param out_width Output parameter for width
99 * @param out_height Output parameter for height
100 *
101 * Retrieves current swapchain dimensions. Sets out_width and out_height
102 * to 0 if window not registered or swapchain unavailable.
103 */
104 std::function<void(const std::shared_ptr<void>&, uint32_t&, uint32_t&)> get_swapchain_extent;
105
106 /**
107 * @brief Get current swapchain image view for rendering
108 * @param window_handle Window handle
109 * @return vk::ImageView cast to void*
110 *
111 * Returns the image view for the currently acquired swapchain image.
112 * Used with dynamic rendering.
113 */
114 std::function<void*(const std::shared_ptr<void>&)> get_current_image_view;
115
116 /**
117 * @brief Get the VkImage bits for the most recently acquired swapchain image.
118 * @param window_handle Window handle.
119 * @return VkImage cast to uint64_t, or 0 if no image has been acquired yet.
120 *
121 * Safe to call after acquire_next_swapchain_image() has been invoked for
122 * the current frame. Does not advance the frame index or wait on any fence.
123 * Intended for read-only operations such as pixel readback.
124 */
125 std::function<uint64_t(const std::shared_ptr<void>&)> get_current_swapchain_image;
126
127 /**
128 * @brief Copy a sub-rectangle of the last presented swapchain image into
129 * a caller-supplied host buffer.
130 *
131 * Internally calls BackendResourceManager::download_image_data with
132 * restore_layout = ePresentSrcKHR and restore_stage = eBottomOfPipe,
133 * which are the correct values for a swapchain image post-present.
134 *
135 * The destination buffer must be at least
136 * pixel_width * pixel_height * bytes_per_pixel bytes.
137 *
138 * @param window_handle Window whose last presented image is the source.
139 * @param dst Host-visible destination pointer.
140 * @param x_offset Left edge of the source rectangle in pixels.
141 * @param y_offset Top edge of the source rectangle in pixels.
142 * @param pixel_width Width of the rectangle in pixels.
143 * @param pixel_height Height of the rectangle in pixels.
144 * @param byte_count Total bytes to read (width * height * bpp).
145 * @return true if the copy completed successfully.
146 */
147 std::function<bool(
148 const std::shared_ptr<void>& window_handle,
149 void* dst,
150 uint32_t x_offset,
151 uint32_t y_offset,
152 uint32_t pixel_width,
153 uint32_t pixel_height,
154 size_t byte_count)>
156
157 /**
158 * @brief Ensure a depth attachment image exists for the window
159 * @param window_handle Window handle
160 *
161 * Lazily creates a D32_SFLOAT depth image matching the current
162 * swapchain extent. Recreates if extent has changed. No-op if
163 * depth image already exists at correct size.
164 */
165 std::function<void(const std::shared_ptr<void>&)> ensure_depth_attachment;
166
167 /**
168 * @brief Get depth image view for the window
169 * @param window_handle Window handle
170 * @return vk::ImageView cast to void*, or nullptr if no depth image
171 *
172 * Returns nullptr if ensure_depth_attachment has not been called
173 * or if depth image creation failed.
174 */
175 std::function<void*(const std::shared_ptr<void>&)> get_depth_image_view;
176
177 /**
178 * @brief Get depth image format for the window
179 * @param window_handle Window handle
180 * @return vk::Format cast to uint32_t, or eUndefined if no depth image
181 */
182 std::function<uint32_t(const std::shared_ptr<void>&)> get_depth_format;
183
184 /**
185 * @brief Returns the last completed full-surface pixel readback for a window.
186 *
187 * Published by the per-window readback thread once a capture copy
188 * completes. Lock-free: the readback thread stores the buffer with
189 * release, callers load with acquire via an atomic shared_ptr, so the
190 * pointer is safe to read from any thread. Returns nullptr if no frame
191 * has completed yet or capture is unavailable on this surface.
192 *
193 * Raw bytes in swapchain pixel format. Width and height match the
194 * current swapchain extent from get_swapchain_extent().
195 */
196 std::function<std::shared_ptr<std::vector<uint8_t>>(
197 const std::shared_ptr<void>& window_handle)>
198 get_last_frame;
199
200 /**
201 * @brief Register a per-frame observer for a window's captured frames.
202 *
203 * The observer fires on the window's readback thread once per captured
204 * frame, receiving the published frame buffer, its width and height, and
205 * the swapchain format as uint32_t. Fires only for capture-enabled
206 * windows. The observer must not block; post to a queue for further work.
207 *
208 * Requires the window's capture state to exist, which it does once the
209 * window has completed its first captured frame. Returns 0 if capture is
210 * not enabled or no frame has been captured yet; retry after a frame.
211 *
212 * @return Opaque non-zero id for unregister_frame_observer, or 0 on failure.
213 */
214 std::function<uint32_t(
215 const std::shared_ptr<void>& window_handle,
216 std::function<void(
217 const std::shared_ptr<std::vector<uint8_t>>&,
218 uint32_t, uint32_t, uint32_t)>)>
220
221 /**
222 * @brief Unregister a previously registered per-frame observer.
223 *
224 * Safe from any thread, including from within the observer callback.
225 *
226 * @param window_handle Window the observer was registered on.
227 * @param id Id returned by register_frame_observer.
228 */
229 std::function<void(const std::shared_ptr<void>&, uint32_t)>
231};
232
233} // namespace MayaFlux::Registry::Services
uint32_t * dst
std::function< uint64_t(const std::shared_ptr< void > &)> get_completed_frame_count
Submitted frames of a window whose GPU work has finished.
std::function< bool(const std::shared_ptr< void > &window_handle, void *dst, uint32_t x_offset, uint32_t y_offset, uint32_t pixel_width, uint32_t pixel_height, size_t byte_count)> readback_swapchain_region
Copy a sub-rectangle of the last presented swapchain image into a caller-supplied host buffer.
std::function< void(const std::shared_ptr< void > &, uint32_t, uint32_t)> resize_surface
Resize rendering surface for a window.
std::function< int(const std::shared_ptr< void > &)> get_swapchain_format
Get actual swapchain format for a window.
std::function< uint64_t(const std::shared_ptr< void > &)> acquire_next_swapchain_image
Acquire the next swapchain image for a window.
std::function< void()> wait_idle
Wait for all GPU operations to complete.
std::function< void(const std::shared_ptr< void > &)> ensure_depth_attachment
Ensure a depth attachment image exists for the window.
std::function< void(const std::shared_ptr< void > &, uint32_t &, uint32_t &)> get_swapchain_extent
Get swapchain extent for a window.
std::function< uint32_t(const std::shared_ptr< void > &window_handle, std::function< void(const std::shared_ptr< std::vector< uint8_t > > &, uint32_t, uint32_t, uint32_t)>)> register_frame_observer
Register a per-frame observer for a window's captured frames.
std::function< uint64_t(const std::shared_ptr< void > &)> get_current_swapchain_image
Get the VkImage bits for the most recently acquired swapchain image.
std::function< uint64_t(const std::shared_ptr< void > &)> get_submitted_frame_count
Frames submitted for a window since it was registered.
std::function< void(const std::shared_ptr< void > &, uint32_t)> unregister_frame_observer
Unregister a previously registered per-frame observer.
std::function< uint32_t(const std::shared_ptr< void > &)> get_depth_format
Get depth image format for the window.
std::function< uint32_t(const std::shared_ptr< void > &)> get_swapchain_image_count
Get current swapchain image count.
std::function< void(const std::shared_ptr< void > &, uint64_t)> submit_and_present
Submit a primary command buffer and present the frame.
Backend display and presentation service interface.