| 1 | /* |
| 2 | * Copyright 2019 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 swappy_common Swappy common tools |
| 19 | * Tools to be used with Swappy for OpenGL or Swappy for Vulkan. |
| 20 | * @{ |
| 21 | */ |
| 22 | |
| 23 | #pragma once |
| 24 | |
| 25 | #include <android/native_window.h> |
| 26 | #include <stdint.h> |
| 27 | |
| 28 | #include "common/gamesdk_common.h" |
| 29 | |
| 30 | /** @brief Swap interval for 60fps, in nanoseconds. */ |
| 31 | #define SWAPPY_SWAP_60FPS (16666667L) |
| 32 | |
| 33 | /** @brief Swap interval for 30fps, in nanoseconds. */ |
| 34 | #define SWAPPY_SWAP_30FPS (33333333L) |
| 35 | |
| 36 | /** @brief Swap interval for 20fps, in nanoseconds. */ |
| 37 | #define SWAPPY_SWAP_20FPS (50000000L) |
| 38 | |
| 39 | /** |
| 40 | * The longest duration, in refresh periods, represented by the statistics. |
| 41 | * @see SwappyStats |
| 42 | */ |
| 43 | #define MAX_FRAME_BUCKETS 6 |
| 44 | |
| 45 | /** @cond INTERNAL */ |
| 46 | |
| 47 | #define SWAPPY_SYSTEM_PROP_KEY_DISABLE "swappy.disable" |
| 48 | |
| 49 | // Internal macros to track Swappy version, do not use directly. |
| 50 | #define SWAPPY_MAJOR_VERSION 2 |
| 51 | #define SWAPPY_MINOR_VERSION 2 |
| 52 | #define SWAPPY_BUGFIX_VERSION 0 |
| 53 | #define SWAPPY_PACKED_VERSION \ |
| 54 | ANDROID_GAMESDK_PACKED_VERSION(SWAPPY_MAJOR_VERSION, SWAPPY_MINOR_VERSION, \ |
| 55 | SWAPPY_BUGFIX_VERSION) |
| 56 | |
| 57 | // Internal macros to generate a symbol to track Swappy version, do not use |
| 58 | // directly. |
| 59 | #define SWAPPY_VERSION_CONCAT_NX(PREFIX, MAJOR, MINOR, BUGFIX, GITCOMMIT) \ |
| 60 | PREFIX##_##MAJOR##_##MINOR##_##BUGFIX##_##GITCOMMIT |
| 61 | #define SWAPPY_VERSION_CONCAT(PREFIX, MAJOR, MINOR, BUGFIX, GITCOMMIT) \ |
| 62 | SWAPPY_VERSION_CONCAT_NX(PREFIX, MAJOR, MINOR, BUGFIX, GITCOMMIT) |
| 63 | #define SWAPPY_VERSION_SYMBOL \ |
| 64 | SWAPPY_VERSION_CONCAT(Swappy_version, SWAPPY_MAJOR_VERSION, \ |
| 65 | SWAPPY_MINOR_VERSION, SWAPPY_BUGFIX_VERSION, \ |
| 66 | AGDK_GIT_COMMIT) |
| 67 | |
| 68 | // Define this to 1 to enable all logging from Swappy, by default it is |
| 69 | // disabled in a release build and enabled in a debug build. |
| 70 | #ifndef ENABLE_SWAPPY_LOGGING |
| 71 | #define ENABLE_SWAPPY_LOGGING 0 |
| 72 | #endif |
| 73 | /** @endcond */ |
| 74 | |
| 75 | /** @brief Id of a thread returned by an external thread manager. */ |
| 76 | typedef uint64_t SwappyThreadId; |
| 77 | |
| 78 | /** |
| 79 | * @brief A structure enabling you to set how Swappy starts and joins threads by |
| 80 | * calling |
| 81 | * ::Swappy_setThreadFunctions. |
| 82 | * |
| 83 | * Usage of this functionality is optional. |
| 84 | */ |
| 85 | typedef struct SwappyThreadFunctions { |
| 86 | /** @brief Thread start callback. |
| 87 | * |
| 88 | * This function is called by Swappy to start thread_func on a new thread. |
| 89 | * @param user_data A value to be passed the thread function. |
| 90 | * If the thread was started, this function should set the thread_id and |
| 91 | * return 0. If the thread was not started, this function should return a |
| 92 | * non-zero value. |
| 93 | */ |
| 94 | int (*start)(SwappyThreadId* thread_id, void* (*thread_func)(void*), |
| 95 | void* user_data); |
| 96 | |
| 97 | /** @brief Thread join callback. |
| 98 | * |
| 99 | * This function is called by Swappy to join the thread with given id. |
| 100 | */ |
| 101 | void (*join)(SwappyThreadId thread_id); |
| 102 | |
| 103 | /** @brief Thread joinable callback. |
| 104 | * |
| 105 | * This function is called by Swappy to discover whether the thread with the |
| 106 | * given id is joinable. |
| 107 | */ |
| 108 | bool (*joinable)(SwappyThreadId thread_id); |
| 109 | } SwappyThreadFunctions; |
| 110 | |
| 111 | #ifdef __cplusplus |
| 112 | extern "C" { |
| 113 | #endif |
| 114 | |
| 115 | /** |
| 116 | * @brief Return the version of the Swappy library at runtime. |
| 117 | */ |
| 118 | uint32_t Swappy_version(); |
| 119 | |
| 120 | /** |
| 121 | * @brief Call this before any other functions in order to use a custom thread |
| 122 | * manager. |
| 123 | * |
| 124 | * Usage of this function is entirely optional. Swappy uses std::thread by |
| 125 | * default. |
| 126 | * |
| 127 | */ |
| 128 | void Swappy_setThreadFunctions(const SwappyThreadFunctions* thread_functions); |
| 129 | |
| 130 | /** |
| 131 | * @brief Return the full version of the Swappy library at runtime, e.g. |
| 132 | * "1.9.0_8a85ab7c46" |
| 133 | */ |
| 134 | const char* Swappy_versionString(); |
| 135 | |
| 136 | /** |
| 137 | * @brief Swappy frame statistics, collected if toggled on with |
| 138 | * ::SwappyGL_enableStats or ::SwappyVk_enableStats. |
| 139 | */ |
| 140 | typedef struct SwappyStats { |
| 141 | /** @brief Total frames swapped by swappy */ |
| 142 | uint64_t totalFrames; |
| 143 | |
| 144 | /** @brief Histogram of the number of screen refreshes a frame waited in the |
| 145 | * compositor queue after rendering was completed. |
| 146 | * |
| 147 | * For example: |
| 148 | * if a frame waited 2 refresh periods in the compositor queue after |
| 149 | * rendering was done, the frame will be counted in idleFrames[2] |
| 150 | */ |
| 151 | uint64_t idleFrames[MAX_FRAME_BUCKETS]; |
| 152 | |
| 153 | /** @brief Histogram of the number of screen refreshes passed between the |
| 154 | * requested presentation time and the actual present time. |
| 155 | * |
| 156 | * For example: |
| 157 | * if a frame was presented 2 refresh periods after the requested |
| 158 | * timestamp swappy set, the frame will be counted in lateFrames[2] |
| 159 | */ |
| 160 | uint64_t lateFrames[MAX_FRAME_BUCKETS]; |
| 161 | |
| 162 | /** @brief Histogram of the number of screen refreshes passed between two |
| 163 | * consecutive frames |
| 164 | * |
| 165 | * For example: |
| 166 | * if frame N was presented 2 refresh periods after frame N-1 |
| 167 | * frame N will be counted in offsetFromPreviousFrame[2] |
| 168 | */ |
| 169 | uint64_t offsetFromPreviousFrame[MAX_FRAME_BUCKETS]; |
| 170 | |
| 171 | /** @brief Histogram of the number of screen refreshes passed between the |
| 172 | * call to Swappy_recordFrameStart and the actual present time. |
| 173 | * |
| 174 | * For example: |
| 175 | * if a frame was presented 2 refresh periods after the call to |
| 176 | * `Swappy_recordFrameStart` the frame will be counted in latencyFrames[2] |
| 177 | */ |
| 178 | uint64_t latencyFrames[MAX_FRAME_BUCKETS]; |
| 179 | } SwappyStats; |
| 180 | |
| 181 | #ifdef __cplusplus |
| 182 | } // extern "C" |
| 183 | #endif |
| 184 | |
| 185 | /** |
| 186 | * Pointer to a function that can be attached to SwappyTracer::preWait |
| 187 | * @param userData Pointer to arbitrary data, see SwappyTracer::userData. |
| 188 | */ |
| 189 | typedef void (*SwappyPreWaitCallback)(void*); |
| 190 | |
| 191 | /** |
| 192 | * Pointer to a function that can be attached to SwappyTracer::postWait. |
| 193 | * @param userData Pointer to arbitrary data, see SwappyTracer::userData. |
| 194 | * @param cpu_time_ns Time for CPU processing of this frame in nanoseconds. |
| 195 | * @param gpu_time_ns Time for GPU processing of previous frame in nanoseconds. |
| 196 | */ |
| 197 | typedef void (*SwappyPostWaitCallback)(void*, int64_t cpu_time_ns, |
| 198 | int64_t gpu_time_ns); |
| 199 | |
| 200 | /** |
| 201 | * Pointer to a function that can be attached to SwappyTracer::preSwapBuffers. |
| 202 | * @param userData Pointer to arbitrary data, see SwappyTracer::userData. |
| 203 | */ |
| 204 | typedef void (*SwappyPreSwapBuffersCallback)(void*); |
| 205 | |
| 206 | /** |
| 207 | * Pointer to a function that can be attached to SwappyTracer::postSwapBuffers. |
| 208 | * @param userData Pointer to arbitrary data, see SwappyTracer::userData. |
| 209 | * @param desiredPresentationTimeMillis The target time, in milliseconds, at |
| 210 | * which the frame would be presented on screen. |
| 211 | */ |
| 212 | typedef void (*SwappyPostSwapBuffersCallback)( |
| 213 | void*, int64_t desiredPresentationTimeMillis); |
| 214 | |
| 215 | /** |
| 216 | * Pointer to a function that can be attached to SwappyTracer::startFrame. |
| 217 | * @param userData Pointer to arbitrary data, see SwappyTracer::userData. |
| 218 | * @param desiredPresentationTimeMillis The time, in milliseconds, at which the |
| 219 | * frame is scheduled to be presented. |
| 220 | */ |
| 221 | typedef void (*SwappyStartFrameCallback)(void*, int currentFrame, |
| 222 | int64_t desiredPresentationTimeMillis); |
| 223 | |
| 224 | /** |
| 225 | * Pointer to a function that can be attached to |
| 226 | * SwappyTracer::swapIntervalChanged. Call ::SwappyGL_getSwapIntervalNS or |
| 227 | * ::SwappyVk_getSwapIntervalNS to get the latest swapInterval. |
| 228 | * @param userData Pointer to arbitrary data, see SwappyTracer::userData. |
| 229 | */ |
| 230 | typedef void (*SwappySwapIntervalChangedCallback)(void*); |
| 231 | |
| 232 | /** |
| 233 | * @brief Collection of callbacks to be called each frame to trace execution. |
| 234 | * |
| 235 | * Injection of these is optional. |
| 236 | */ |
| 237 | typedef struct SwappyTracer { |
| 238 | /** |
| 239 | * Callback called before waiting to queue the frame to the composer. |
| 240 | */ |
| 241 | SwappyPreWaitCallback preWait; |
| 242 | |
| 243 | /** |
| 244 | * Callback called after wait to queue the frame to the composer is done. |
| 245 | */ |
| 246 | SwappyPostWaitCallback postWait; |
| 247 | |
| 248 | /** |
| 249 | * Callback called before calling the function to queue the frame to the |
| 250 | * composer. |
| 251 | */ |
| 252 | SwappyPreSwapBuffersCallback preSwapBuffers; |
| 253 | |
| 254 | /** |
| 255 | * Callback called after calling the function to queue the frame to the |
| 256 | * composer. |
| 257 | */ |
| 258 | SwappyPostSwapBuffersCallback postSwapBuffers; |
| 259 | |
| 260 | /** |
| 261 | * Callback called at the start of a frame. |
| 262 | */ |
| 263 | SwappyStartFrameCallback startFrame; |
| 264 | |
| 265 | /** |
| 266 | * Pointer to some arbitrary data that will be passed as the first argument |
| 267 | * of callbacks. |
| 268 | */ |
| 269 | void* userData; |
| 270 | |
| 271 | /** |
| 272 | * Callback called when the swap interval was changed. |
| 273 | */ |
| 274 | SwappySwapIntervalChangedCallback swapIntervalChanged; |
| 275 | } SwappyTracer; |
| 276 | |
| 277 | /** @} */ |