| 1 | /* |
| 2 | * Copyright 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /** |
| 18 | * @defgroup swappyVk Swappy for Vulkan |
| 19 | * Vulkan part of Swappy. |
| 20 | * @{ |
| 21 | */ |
| 22 | |
| 23 | #pragma once |
| 24 | |
| 25 | #include "jni.h" |
| 26 | #include "swappy_common.h" |
| 27 | |
| 28 | #ifndef VK_NO_PROTOTYPES |
| 29 | #define VK_NO_PROTOTYPES 1 |
| 30 | #endif |
| 31 | #include <vulkan/vulkan.h> |
| 32 | |
| 33 | #ifdef __cplusplus |
| 34 | extern "C" { |
| 35 | #endif |
| 36 | |
| 37 | /** |
| 38 | * @brief Determine any Vulkan device extensions that must be enabled for a new |
| 39 | * VkDevice. |
| 40 | * |
| 41 | * Swappy-for-Vulkan (SwappyVk) benefits from certain Vulkan device extensions |
| 42 | * (e.g. VK_GOOGLE_display_timing). Before the application calls |
| 43 | * vkCreateDevice, SwappyVk needs to look at the list of available extensions |
| 44 | * (returned by vkEnumerateDeviceExtensionProperties) and potentially identify |
| 45 | * one or more extensions that the application must add to: |
| 46 | * |
| 47 | * - VkDeviceCreateInfo::enabledExtensionCount |
| 48 | * - VkDeviceCreateInfo::ppEnabledExtensionNames |
| 49 | * |
| 50 | * before the application calls vkCreateDevice. For each VkPhysicalDevice that |
| 51 | * the application will call vkCreateDevice for, the application must call this |
| 52 | * function, and then must add the identified extension(s) to the list that are |
| 53 | * enabled for the VkDevice. Similar to many Vulkan functions, this function |
| 54 | * can be called twice, once to identify the number of required extensions, and |
| 55 | * again with application-allocated memory that the function can write into. |
| 56 | * |
| 57 | * @param[in] physicalDevice - The VkPhysicalDevice associated with |
| 58 | * the available extensions. |
| 59 | * @param[in] availableExtensionCount - This is the returned value of |
| 60 | * pPropertyCount from vkEnumerateDeviceExtensionProperties. |
| 61 | * @param[in] pAvailableExtensions - This is the returned value of |
| 62 | * pProperties from vkEnumerateDeviceExtensionProperties. |
| 63 | * @param[inout] pRequiredExtensionCount - If pRequiredExtensions is nullptr, |
| 64 | * the function sets this to the number of extensions that are required. If |
| 65 | * pRequiredExtensions is non-nullptr, this is the number of required extensions |
| 66 | * that the function should write into pRequiredExtensions. |
| 67 | * @param[inout] pRequiredExtensions - If non-nullptr, this is |
| 68 | * application-allocated memory into which the function will write the names of |
| 69 | * required extensions. It is a pointer to an array of |
| 70 | * char* strings (i.e. the same as |
| 71 | * VkDeviceCreateInfo::ppEnabledExtensionNames). |
| 72 | */ |
| 73 | void SwappyVk_determineDeviceExtensions( |
| 74 | VkPhysicalDevice physicalDevice, uint32_t availableExtensionCount, |
| 75 | VkExtensionProperties* pAvailableExtensions, |
| 76 | uint32_t* pRequiredExtensionCount, char** pRequiredExtensions); |
| 77 | |
| 78 | /** |
| 79 | * @brief Tell Swappy the queueFamilyIndex used to create a specific VkQueue |
| 80 | * |
| 81 | * Swappy needs to know the queueFamilyIndex used for creating a specific |
| 82 | * VkQueue so it can use it when presenting. |
| 83 | * |
| 84 | * @param[in] device - The VkDevice associated with the queue |
| 85 | * @param[in] queue - A device queue. |
| 86 | * @param[in] queueFamilyIndex - The queue family index used to create the |
| 87 | * VkQueue. |
| 88 | * |
| 89 | */ |
| 90 | void SwappyVk_setQueueFamilyIndex(VkDevice device, VkQueue queue, |
| 91 | uint32_t queueFamilyIndex); |
| 92 | |
| 93 | // TBD: For now, SwappyVk assumes only one VkSwapchainKHR per VkDevice, and that |
| 94 | // applications don't re-create swapchains. Is this long-term sufficient? |
| 95 | |
| 96 | /** |
| 97 | * Internal init function. Do not call directly. |
| 98 | * See SwappyVk_initAndGetRefreshCycleDuration instead. |
| 99 | * @private |
| 100 | */ |
| 101 | bool SwappyVk_initAndGetRefreshCycleDuration_internal( |
| 102 | JNIEnv* env, jobject jactivity, VkPhysicalDevice physicalDevice, |
| 103 | VkDevice device, VkSwapchainKHR swapchain, uint64_t* pRefreshDuration); |
| 104 | |
| 105 | /** |
| 106 | * @brief Initialize SwappyVk for a given device and swapchain, and obtain the |
| 107 | * approximate time duration between vertical-blanking periods. |
| 108 | * |
| 109 | * Uses JNI to query AppVsyncOffset and PresentationDeadline. |
| 110 | * |
| 111 | * If your application presents to more than one swapchain at a time, you must |
| 112 | * call this for each swapchain before calling swappyVkSetSwapInterval() for it. |
| 113 | * |
| 114 | * The duration between vertical-blanking periods (an interval) is expressed as |
| 115 | * the approximate number of nanoseconds between vertical-blanking periods of |
| 116 | * the swapchain’s physical display. |
| 117 | * |
| 118 | * If the application converts this number to a fraction (e.g. 16,666,666 nsec |
| 119 | * to 0.016666666) and divides one by that fraction, it will be the approximate |
| 120 | * refresh rate of the display (e.g. 16,666,666 nanoseconds corresponds to a |
| 121 | * 60Hz display, 11,111,111 nsec corresponds to a 90Hz display). |
| 122 | * |
| 123 | * @param[in] env - JNIEnv that is assumed to be from AttachCurrentThread |
| 124 | * function |
| 125 | * @param[in] jactivity - NativeActivity object handle, used for JNI |
| 126 | * @param[in] physicalDevice - The VkPhysicalDevice associated with the |
| 127 | * swapchain |
| 128 | * @param[in] device - The VkDevice associated with the swapchain |
| 129 | * @param[in] swapchain - The VkSwapchainKHR the application wants Swappy to |
| 130 | * swap |
| 131 | * @param[out] pRefreshDuration - The returned refresh cycle duration |
| 132 | * |
| 133 | * @return bool - true if the value returned by pRefreshDuration is |
| 134 | * valid, otherwise false if an error. |
| 135 | */ |
| 136 | bool SwappyVk_initAndGetRefreshCycleDuration(JNIEnv* env, jobject jactivity, |
| 137 | VkPhysicalDevice physicalDevice, |
| 138 | VkDevice device, |
| 139 | VkSwapchainKHR swapchain, |
| 140 | uint64_t* pRefreshDuration); |
| 141 | |
| 142 | /** |
| 143 | * @brief Tell Swappy which ANativeWindow to use when calling to ANativeWindow_* |
| 144 | * API. |
| 145 | * @param[in] device - The VkDevice associated with the swapchain |
| 146 | * @param[in] swapchain - The VkSwapchainKHR the application wants Swappy to |
| 147 | * swap |
| 148 | * @param[in] window - The ANativeWindow that was used to create the |
| 149 | * VkSwapchainKHR |
| 150 | */ |
| 151 | void SwappyVk_setWindow(VkDevice device, VkSwapchainKHR swapchain, |
| 152 | ANativeWindow* window); |
| 153 | |
| 154 | /** |
| 155 | * @brief Tell Swappy the duration of that each presented image should be |
| 156 | * visible. |
| 157 | * |
| 158 | * If your application presents to more than one swapchain at a time, you must |
| 159 | * call this for each swapchain before presenting to it. |
| 160 | * |
| 161 | * @param[in] device - The VkDevice associated with the swapchain |
| 162 | * @param[in] swapchain - The VkSwapchainKHR the application wants Swappy to |
| 163 | * swap |
| 164 | * @param[in] swap_ns - The duration of that each presented image should be |
| 165 | * visible in nanoseconds |
| 166 | */ |
| 167 | void SwappyVk_setSwapIntervalNS(VkDevice device, VkSwapchainKHR swapchain, |
| 168 | uint64_t swap_ns); |
| 169 | |
| 170 | /** |
| 171 | * @brief Tell Swappy to present one or more images to corresponding swapchains. |
| 172 | * |
| 173 | * Swappy will call vkQueuePresentKHR for your application. Swappy may insert a |
| 174 | * struct to the pNext-chain of VkPresentInfoKHR, or it may insert other Vulkan |
| 175 | * commands in order to attempt to honor the desired swap interval. |
| 176 | * |
| 177 | * @note If your application presents to more than one swapchain at a time, and |
| 178 | * if you use a different swap interval for each swapchain, Swappy will attempt |
| 179 | * to honor the swap interval for each swapchain (being more successful on |
| 180 | * devices that support an underlying presentation-timing extension, such as |
| 181 | * VK_GOOGLE_display_timing). |
| 182 | * |
| 183 | * @param[in] queue - The VkQueue associated with the device and swapchain |
| 184 | * @param[in] pPresentInfo - A pointer to the VkPresentInfoKHR containing the |
| 185 | * information about what image(s) to present on which |
| 186 | * swapchain(s). |
| 187 | */ |
| 188 | VkResult SwappyVk_queuePresent(VkQueue queue, |
| 189 | const VkPresentInfoKHR* pPresentInfo); |
| 190 | |
| 191 | /** |
| 192 | * @brief Destroy the SwappyVk instance associated with a swapchain. |
| 193 | * |
| 194 | * This API is expected to be called before calling vkDestroySwapchainKHR() |
| 195 | * so Swappy can cleanup its internal state. |
| 196 | * |
| 197 | * @param[in] device - The VkDevice associated with SwappyVk |
| 198 | * @param[in] swapchain - The VkSwapchainKHR the application wants Swappy to |
| 199 | * destroy |
| 200 | */ |
| 201 | void SwappyVk_destroySwapchain(VkDevice device, VkSwapchainKHR swapchain); |
| 202 | |
| 203 | /** |
| 204 | * @brief Destroy any swapchains associated with the device and clean up the |
| 205 | * device's resources |
| 206 | * |
| 207 | * This function should be called after SwappyVk_destroySwapchain if you no |
| 208 | * longer need the device. |
| 209 | * |
| 210 | * @param[in] device - The VkDevice associated with SwappyVk |
| 211 | */ |
| 212 | void SwappyVk_destroyDevice(VkDevice device); |
| 213 | |
| 214 | /** |
| 215 | * @brief Enables Auto-Swap-Interval feature for all instances. |
| 216 | * |
| 217 | * By default this feature is enabled. Changing it is completely |
| 218 | * optional for fine-tuning swappy behaviour. |
| 219 | * |
| 220 | * @param[in] enabled - True means enable, false means disable |
| 221 | */ |
| 222 | void SwappyVk_setAutoSwapInterval(bool enabled); |
| 223 | |
| 224 | /** |
| 225 | * @brief Enables Auto-Pipeline-Mode feature for all instances. |
| 226 | * |
| 227 | * By default this feature is enabled. Changing it is completely |
| 228 | * optional for fine-tuning swappy behaviour. |
| 229 | * |
| 230 | * @param[in] enabled - True means enable, false means disable |
| 231 | */ |
| 232 | void SwappyVk_setAutoPipelineMode(bool enabled); |
| 233 | |
| 234 | /** |
| 235 | * @brief Sets the maximal swap duration for all instances. |
| 236 | * |
| 237 | * Sets the maximal duration for Auto-Swap-Interval in milliseconds. |
| 238 | * If SwappyVk is operating in Auto-Swap-Interval and the frame duration is |
| 239 | * longer than the provided duration, SwappyVk will not do any pacing and just |
| 240 | * submit the frame as soon as possible. |
| 241 | * |
| 242 | * @param[in] max_swap_ns - maximal swap duration in milliseconds. |
| 243 | */ |
| 244 | void SwappyVk_setMaxAutoSwapIntervalNS(uint64_t max_swap_ns); |
| 245 | |
| 246 | /** |
| 247 | * @brief The fence timeout parameter can be set for devices with faulty |
| 248 | * drivers. Its default value is 50,000,000. |
| 249 | */ |
| 250 | void SwappyVk_setFenceTimeoutNS(uint64_t fence_timeout_ns); |
| 251 | |
| 252 | /** |
| 253 | * @brief Get the fence timeout parameter, for devices with faulty |
| 254 | * drivers. Its default value is 50,000,000. |
| 255 | */ |
| 256 | uint64_t SwappyVk_getFenceTimeoutNS(); |
| 257 | |
| 258 | /** |
| 259 | * @brief Inject callback functions to be called each frame. |
| 260 | * |
| 261 | * @param[in] tracer - Collection of callback functions |
| 262 | */ |
| 263 | void SwappyVk_injectTracer(const SwappyTracer* tracer); |
| 264 | |
| 265 | /** |
| 266 | * @brief Remove callbacks that were previously added using |
| 267 | * SwappyVk_injectTracer. |
| 268 | * |
| 269 | * Only removes callbacks that were previously added using |
| 270 | * SwappyVK_injectTracer. If SwappyVK_injectTracker was not called with the |
| 271 | * tracer, then there is no effect. |
| 272 | * |
| 273 | * @param[in] tracer - Collection of callback functions |
| 274 | */ |
| 275 | void SwappyVk_uninjectTracer(const SwappyTracer* tracer); |
| 276 | |
| 277 | /** |
| 278 | * @brief A structure enabling you to provide your own Vulkan function wrappers |
| 279 | * by calling ::SwappyVk_setFunctionProvider. |
| 280 | * |
| 281 | * Usage of this functionality is optional. |
| 282 | */ |
| 283 | typedef struct SwappyVkFunctionProvider { |
| 284 | /** |
| 285 | * @brief Callback to initialize the function provider. |
| 286 | * |
| 287 | * This function is called by Swappy before any functions are requested. |
| 288 | * E.g. so you can call dlopen on the Vulkan library. |
| 289 | */ |
| 290 | bool (*init)(); |
| 291 | |
| 292 | /** |
| 293 | * @brief Callback to get the address of a function. |
| 294 | * |
| 295 | * This function is called by Swappy to get the address of a Vulkan |
| 296 | * function. |
| 297 | * @param name The null-terminated name of the function. |
| 298 | */ |
| 299 | void* (*getProcAddr)(const char* name); |
| 300 | |
| 301 | /** |
| 302 | * @brief Callback to close any resources owned by the function provider. |
| 303 | * |
| 304 | * This function is called by Swappy when no more functions will be |
| 305 | * requested, e.g. so you can call dlclose on the Vulkan library. |
| 306 | */ |
| 307 | void (*close)(); |
| 308 | } SwappyVkFunctionProvider; |
| 309 | |
| 310 | /** |
| 311 | * @brief Set the Vulkan function provider. |
| 312 | * |
| 313 | * This enables you to provide an object that will be used to look up Vulkan |
| 314 | * functions, e.g. to hook usage of these functions. |
| 315 | * |
| 316 | * To use this functionality, you *must* call this function before any others. |
| 317 | * |
| 318 | * Usage of this function is entirely optional. If you do not use it, the Vulkan |
| 319 | * functions required by Swappy will be dynamically loaded from libvulkan.so. |
| 320 | * |
| 321 | * @param[in] provider - provider object |
| 322 | */ |
| 323 | void SwappyVk_setFunctionProvider( |
| 324 | const SwappyVkFunctionProvider* pSwappyVkFunctionProvider); |
| 325 | |
| 326 | /** |
| 327 | * @brief Get the swap interval value, in nanoseconds, for a given swapchain. |
| 328 | * |
| 329 | * @param[in] swapchain - the swapchain to query |
| 330 | */ |
| 331 | uint64_t SwappyVk_getSwapIntervalNS(VkSwapchainKHR swapchain); |
| 332 | |
| 333 | /** |
| 334 | * @brief Get the supported refresh periods of this device. Call once with |
| 335 | * out_refreshrates set to nullptr to get the number of supported refresh |
| 336 | * periods, then call again passing that number as allocated_entries and |
| 337 | * an array of size equal to allocated_entries that will be filled with the |
| 338 | * refresh periods. |
| 339 | */ |
| 340 | int SwappyVk_getSupportedRefreshPeriodsNS(uint64_t* out_refreshrates, |
| 341 | int allocated_entries, |
| 342 | VkSwapchainKHR swapchain); |
| 343 | /** |
| 344 | * @brief Check if Swappy is enabled for the specified swapchain. |
| 345 | * |
| 346 | * @return false if SwappyVk_initAndGetRefreshCycleDuration was not |
| 347 | * called for the specified swapchain, true otherwise. |
| 348 | */ |
| 349 | bool SwappyVk_isEnabled(VkSwapchainKHR swapchain, bool* isEnabled); |
| 350 | |
| 351 | /** |
| 352 | * @brief Toggle statistics collection on/off |
| 353 | * |
| 354 | * By default, stats collection is off and there is no overhead related to |
| 355 | * stats. An app can turn on stats collection by calling |
| 356 | * `SwappyVk_enableStats(swapchain, true)`. Then, the app is expected to call |
| 357 | * ::SwappyVk_recordFrameStart for each frame before starting to do any CPU |
| 358 | * related work. Stats will be logged to logcat with a 'FrameStatistics' tag. An |
| 359 | * app can get the stats by calling ::SwappyVk_getStats. |
| 360 | * |
| 361 | * SwappyVk_initAndGetRefreshCycleDuration must have been called successfully |
| 362 | * before for this swapchain, otherwise there is no effect in this call. Frame |
| 363 | * stats are only available if the platform supports VK_GOOGLE_display_timing |
| 364 | * extension. |
| 365 | * |
| 366 | * @param[in] swapchain - The swapchain for which frame stat collection is |
| 367 | * configured. |
| 368 | * @param enabled - Whether to enable/disable frame stat collection. |
| 369 | */ |
| 370 | void SwappyVk_enableStats(VkSwapchainKHR swapchain, bool enabled); |
| 371 | |
| 372 | /** |
| 373 | * @brief Should be called if stats have been enabled with SwappyVk_enableStats. |
| 374 | * |
| 375 | * When stats collection is enabled with SwappyVk_enableStats, the app is |
| 376 | * expected to call this function for each frame before starting to do any CPU |
| 377 | * related work. It is assumed that this function will be called after a |
| 378 | * successful call to vkAcquireNextImageKHR. See ::SwappyVk_enableStats for more |
| 379 | * conditions. |
| 380 | * |
| 381 | * @param[in] queue - The VkQueue associated with the device and swapchain |
| 382 | * @param[in] swapchain - The swapchain where the frame is presented to. |
| 383 | * @param[in] image - The image in swapchain that corresponds to the frame. |
| 384 | |
| 385 | * @see SwappyVk_enableStats. |
| 386 | */ |
| 387 | void SwappyVk_recordFrameStart(VkQueue queue, VkSwapchainKHR swapchain, |
| 388 | uint32_t image); |
| 389 | |
| 390 | /** |
| 391 | * @brief Returns the stats collected, if statistics collection was toggled on. |
| 392 | * |
| 393 | * Given that this API uses VkSwapchainKHR and the potential for this call to be |
| 394 | * done on different threads, all calls to ::SwappyVk_getStats |
| 395 | * must be externally synchronized with other SwappyVk calls. Unsynchronized |
| 396 | * calls may lead to undefined behavior. See ::SwappyVk_enableStats for more |
| 397 | * conditions. |
| 398 | * |
| 399 | * @param[in] swapchain - The swapchain for which stats are being queried. |
| 400 | * @param swappyStats - Pointer to a SwappyStats that will be populated |
| 401 | * with the collected stats. Cannot be NULL. |
| 402 | * @see SwappyStats |
| 403 | */ |
| 404 | void SwappyVk_getStats(VkSwapchainKHR swapchain, SwappyStats* swappyStats); |
| 405 | |
| 406 | /** |
| 407 | * @brief Clears the frame statistics collected so far. |
| 408 | * |
| 409 | * All the frame statistics collected are reset to 0, frame statistics are |
| 410 | * collected normally after this call. See ::SwappyVk_enableStats for more |
| 411 | * conditions. |
| 412 | * |
| 413 | * @param[in] swapchain - The swapchain for which stats are being cleared. |
| 414 | */ |
| 415 | void SwappyVk_clearStats(VkSwapchainKHR swapchain); |
| 416 | |
| 417 | /** |
| 418 | * @brief Reset the swappy pacing mechanism |
| 419 | * |
| 420 | * In cases where the frame timing history is irrelevant (for example during |
| 421 | * scene/level transitions or after loading screens), calling this would |
| 422 | * remove all the history for frame pacing. Calling this entry point |
| 423 | * would reset the frame rate to the initial state at the end of the current |
| 424 | * frame. Then swappy would just pace as normal with fresh state from next |
| 425 | * frame. There are no error conditions associated with this call. |
| 426 | * |
| 427 | * @param[in] swapchain - The swapchain for which frame pacing is reset. |
| 428 | */ |
| 429 | void SwappyVk_resetFramePacing(VkSwapchainKHR swapchain); |
| 430 | |
| 431 | /** |
| 432 | * @brief Enable/Disable the swappy pacing mechanism |
| 433 | * |
| 434 | * By default frame pacing is enabled when swappy is used, however it can be |
| 435 | * disabled at runtime by calling `SwappyVk_enableFramePacing(swapchain, |
| 436 | * false)`. When the frame pacing is disabled, ::SwappyVk_enableBlockingWait |
| 437 | * will control whether |
| 438 | * ::SwappyVk_queuePresent will return immediately, or will block until the |
| 439 | * previous frame's GPU work is completed. All enabling/disabling effects take |
| 440 | * place from next frame. Once disabled, `SwappyVk_enableFramePacing(swapchain, |
| 441 | * true)` will enable frame pacing again with a fresh frame time state - |
| 442 | * equivalent of calling ::SwappyVk_resetFramePacing(). |
| 443 | * |
| 444 | * @param[in] swapchain - The swapchain for which stats are being queried. |
| 445 | * @param enable - If true, enables frame pacing otherwise disables it |
| 446 | */ |
| 447 | void SwappyVk_enableFramePacing(VkSwapchainKHR swapchain, bool enable); |
| 448 | |
| 449 | /** |
| 450 | * @brief Enable/Disable blocking wait when frame-pacing is disabled |
| 451 | * |
| 452 | * By default ::SwappyVk_queuePresent will do a blocking wait until previous |
| 453 | * frame's GPU work is completed. However when frame pacing is disabled, calling |
| 454 | * `SwappyVk_enableBlockingWait(swapchain, false)` will ensure that |
| 455 | * ::SwappyVk_queuePresent returns without waiting for previous frame's GPU |
| 456 | * work. This behaviour impacts the GPU time returned in the |
| 457 | * ::SwappyPostWaitCallback callback. If the last frame's GPU work is done by |
| 458 | * the time ::SwappyVk_queuePresent for this frame is called, then the previous |
| 459 | * frame's GPU time will be returned otherwise `-1` will be delivered in the |
| 460 | * callback for the frame. This setting has no impact when frame pacing is |
| 461 | * enabled. |
| 462 | * |
| 463 | * @param[in] swapchain - The swapchain for which stats are being queried. |
| 464 | * @param enable - If true, ::SwappyVk_queuePresent will block until |
| 465 | * the previous frame's GPU work is complete. |
| 466 | */ |
| 467 | void SwappyVk_enableBlockingWait(VkSwapchainKHR swapchain, bool enable); |
| 468 | |
| 469 | #ifdef __cplusplus |
| 470 | } // extern "C" |
| 471 | #endif |
| 472 | |
| 473 | /** @} */ |