MayaFlux 0.4.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 Acquire the next swapchain image for a window
56 * @param window_handle Window handle
57 * @return Index of the acquired swapchain image
58 *
59 * Must be called before get_current_image_view() for dynamic rendering.
60 * Stores the acquired image index internally for subsequent calls.
61 */
62 std::function<uint64_t(const std::shared_ptr<void>&)> acquire_next_swapchain_image;
63
64 /**
65 * @brief Get actual swapchain format for a window
66 * @param window_handle Window handle
67 * @return Vulkan format (vk::Format cast to uint32_t)
68 *
69 * Returns the actual format used by the window's swapchain.
70 * Used to ensure multiple dynamic render calls are compatible
71 */
72 std::function<int(const std::shared_ptr<void>&)> get_swapchain_format;
73
74 /**
75 * @brief Get swapchain extent for a window
76 * @param window_handle Window handle
77 * @param out_width Output parameter for width
78 * @param out_height Output parameter for height
79 *
80 * Retrieves current swapchain dimensions. Sets out_width and out_height
81 * to 0 if window not registered or swapchain unavailable.
82 */
83 std::function<void(const std::shared_ptr<void>&, uint32_t&, uint32_t&)> get_swapchain_extent;
84
85 /**
86 * @brief Get current swapchain image view for rendering
87 * @param window_handle Window handle
88 * @return vk::ImageView cast to void*
89 *
90 * Returns the image view for the currently acquired swapchain image.
91 * Used with dynamic rendering.
92 */
93 std::function<void*(const std::shared_ptr<void>&)> get_current_image_view;
94
95 /**
96 * @brief Get the VkImage bits for the most recently acquired swapchain image.
97 * @param window_handle Window handle.
98 * @return VkImage cast to uint64_t, or 0 if no image has been acquired yet.
99 *
100 * Safe to call after acquire_next_swapchain_image() has been invoked for
101 * the current frame. Does not advance the frame index or wait on any fence.
102 * Intended for read-only operations such as pixel readback.
103 */
104 std::function<uint64_t(const std::shared_ptr<void>&)> get_current_swapchain_image;
105
106 /**
107 * @brief Copy a sub-rectangle of the last presented swapchain image into
108 * a caller-supplied host buffer.
109 *
110 * Internally calls BackendResourceManager::download_image_data with
111 * restore_layout = ePresentSrcKHR and restore_stage = eBottomOfPipe,
112 * which are the correct values for a swapchain image post-present.
113 *
114 * The destination buffer must be at least
115 * pixel_width * pixel_height * bytes_per_pixel bytes.
116 *
117 * @param window_handle Window whose last presented image is the source.
118 * @param dst Host-visible destination pointer.
119 * @param x_offset Left edge of the source rectangle in pixels.
120 * @param y_offset Top edge of the source rectangle in pixels.
121 * @param pixel_width Width of the rectangle in pixels.
122 * @param pixel_height Height of the rectangle in pixels.
123 * @param byte_count Total bytes to read (width * height * bpp).
124 * @return true if the copy completed successfully.
125 */
126 std::function<bool(
127 const std::shared_ptr<void>& window_handle,
128 void* dst,
129 uint32_t x_offset,
130 uint32_t y_offset,
131 uint32_t pixel_width,
132 uint32_t pixel_height,
133 size_t byte_count)>
135
136 /**
137 * @brief Ensure a depth attachment image exists for the window
138 * @param window_handle Window handle
139 *
140 * Lazily creates a D32_SFLOAT depth image matching the current
141 * swapchain extent. Recreates if extent has changed. No-op if
142 * depth image already exists at correct size.
143 */
144 std::function<void(const std::shared_ptr<void>&)> ensure_depth_attachment;
145
146 /**
147 * @brief Get depth image view for the window
148 * @param window_handle Window handle
149 * @return vk::ImageView cast to void*, or nullptr if no depth image
150 *
151 * Returns nullptr if ensure_depth_attachment has not been called
152 * or if depth image creation failed.
153 */
154 std::function<void*(const std::shared_ptr<void>&)> get_depth_image_view;
155
156 /**
157 * @brief Get depth image format for the window
158 * @param window_handle Window handle
159 * @return vk::Format cast to uint32_t, or eUndefined if no depth image
160 */
161 std::function<uint32_t(const std::shared_ptr<void>&)> get_depth_format;
162
163 /**
164 * @brief Returns the last completed full-surface pixel readback for a window.
165 *
166 * Published by the per-window readback thread once a capture copy
167 * completes. Lock-free: the readback thread stores the buffer with
168 * release, callers load with acquire via an atomic shared_ptr, so the
169 * pointer is safe to read from any thread. Returns nullptr if no frame
170 * has completed yet or capture is unavailable on this surface.
171 *
172 * Raw bytes in swapchain pixel format. Width and height match the
173 * current swapchain extent from get_swapchain_extent().
174 */
175 std::function<std::shared_ptr<std::vector<uint8_t>>(
176 const std::shared_ptr<void>& window_handle)>
177 get_last_frame;
178
179 /**
180 * @brief Register a per-frame observer for a window's captured frames.
181 *
182 * The observer fires on the window's readback thread once per captured
183 * frame, receiving the published frame buffer, its width and height, and
184 * the swapchain format as uint32_t. Fires only for capture-enabled
185 * windows. The observer must not block; post to a queue for further work.
186 *
187 * Requires the window's capture state to exist, which it does once the
188 * window has completed its first captured frame. Returns 0 if capture is
189 * not enabled or no frame has been captured yet; retry after a frame.
190 *
191 * @return Opaque non-zero id for unregister_frame_observer, or 0 on failure.
192 */
193 std::function<uint32_t(
194 const std::shared_ptr<void>& window_handle,
195 std::function<void(
196 const std::shared_ptr<std::vector<uint8_t>>&,
197 uint32_t, uint32_t, uint32_t)>)>
199
200 /**
201 * @brief Unregister a previously registered per-frame observer.
202 *
203 * Safe from any thread, including from within the observer callback.
204 *
205 * @param window_handle Window the observer was registered on.
206 * @param id Id returned by register_frame_observer.
207 */
208 std::function<void(const std::shared_ptr<void>&, uint32_t)>
210};
211
212} // namespace MayaFlux::Registry::Services
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< 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.