diff options
author | Vamsidhar reddy Gaddam <gvamsi@google.com> | 2023-09-22 13:51:58 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2023-09-22 13:51:58 +0000 |
commit | 14c47ea9424bdb7969d4220300321e0f3eb38121 (patch) | |
tree | af220bb2ec9b55b4234afcb8fb148b60437c27d8 | |
parent | 4042ac9f44931fb500a7ad619235d5a092974f6e (diff) | |
parent | 11d24d9e41fc46b61cc7cfef814d001ab32b2bf7 (diff) | |
download | gamesdk-14c47ea9424bdb7969d4220300321e0f3eb38121.tar.gz |
Merge "Add tracking of tracers for swappy vulkan" into main
-rw-r--r-- | games-frame-pacing/vulkan/SwappyVk.cpp | 59 | ||||
-rw-r--r-- | games-frame-pacing/vulkan/SwappyVk.h | 3 | ||||
-rw-r--r-- | games-frame-pacing/vulkan/SwappyVkBase.cpp | 4 |
3 files changed, 60 insertions, 6 deletions
diff --git a/games-frame-pacing/vulkan/SwappyVk.cpp b/games-frame-pacing/vulkan/SwappyVk.cpp index 8b1fae1b..5257433f 100644 --- a/games-frame-pacing/vulkan/SwappyVk.cpp +++ b/games-frame-pacing/vulkan/SwappyVk.cpp @@ -19,6 +19,39 @@ #define LOG_TAG "SwappyVk" #include "SwappyLog.h" +/* For tracking tracers internally within vulkan instance, we cannot store the + * pointers as there is no requirement for the life of the object once the + * SwappyVk_injectTracer(*t) call is returned. So we store the struct objects by + * value. In turn, this implies that we have to compare the whole struct for + * managing it. So we define a local "==" operator to this file in order to + * handle the C struct API in the std::list. + * + * In addition, there is a risk that updates to the SwappyTracer struct could go + * unnoticed silently. So we have a copy here and we check at compile time that + * the sizes are the same, it is a weak check, but it is better than nothing. + */ + +typedef struct SwappyTracerLocalStruct { + SwappyPreWaitCallback preWait; + SwappyPostWaitCallback postWait; + SwappyPreSwapBuffersCallback preSwapBuffers; + SwappyPostSwapBuffersCallback postSwapBuffers; + SwappyStartFrameCallback startFrame; + void* userData; + SwappySwapIntervalChangedCallback swapIntervalChanged; +} SwappyTracerLocalStruct; + +static bool operator==(const SwappyTracer& t1, const SwappyTracer& t2) { + static_assert(sizeof(SwappyTracer) == sizeof(SwappyTracerLocalStruct), + "SwappyTracer struct appears to have changed, please " + "consider updating locally."); + return (t1.preWait == t2.preWait) && (t1.postWait == t2.postWait) && + (t1.preSwapBuffers == t2.preSwapBuffers) && + (t1.postSwapBuffers == t2.postSwapBuffers) && + (t1.startFrame == t2.startFrame) && (t1.userData == t2.userData) && + (t1.swapIntervalChanged == t2.swapIntervalChanged); +} + namespace swappy { class DefaultSwappyVkFunctionProvider { @@ -158,6 +191,14 @@ bool SwappyVk::GetRefreshCycleDuration(JNIEnv* env, jobject jactivity, } } + // SwappyBase is constructed by this point, so we can add the tracers we + // have so far. + { + std::lock_guard<std::mutex> lock(tracer_list_lock); + for (const auto& tracer : tracer_list) { + pImplementation->addTracer(&tracer); + } + } // Now, call that derived class to get the refresh duration to return return pImplementation->doGetRefreshCycleDuration(swapchain, pRefreshDuration); @@ -291,14 +332,24 @@ std::chrono::nanoseconds SwappyVk::GetSwapInterval(VkSwapchainKHR swapchain) { } void SwappyVk::addTracer(const SwappyTracer* t) { - for (auto i : perSwapchainImplementation) { - i.second->addTracer(t); + if (t != nullptr) { + std::lock_guard<std::mutex> lock(tracer_list_lock); + tracer_list.push_back(*t); + + for (const auto& i : perSwapchainImplementation) { + i.second->addTracer(t); + } } } void SwappyVk::removeTracer(const SwappyTracer* t) { - for (auto i : perSwapchainImplementation) { - i.second->removeTracer(t); + if (t != nullptr) { + std::lock_guard<std::mutex> lock(tracer_list_lock); + tracer_list.remove(*t); + + for (const auto& i : perSwapchainImplementation) { + i.second->removeTracer(t); + } } } diff --git a/games-frame-pacing/vulkan/SwappyVk.h b/games-frame-pacing/vulkan/SwappyVk.h index 02ef4f71..7ff15225 100644 --- a/games-frame-pacing/vulkan/SwappyVk.h +++ b/games-frame-pacing/vulkan/SwappyVk.h @@ -121,6 +121,9 @@ class SwappyVk { // Forbid copies. SwappyVk(SwappyVk const&) = delete; void operator=(SwappyVk const&) = delete; + + std::mutex tracer_list_lock; + std::list<SwappyTracer> tracer_list GUARDED_BY(tracer_list_lock); }; } // namespace swappy diff --git a/games-frame-pacing/vulkan/SwappyVkBase.cpp b/games-frame-pacing/vulkan/SwappyVkBase.cpp index a059b974..bd779efa 100644 --- a/games-frame-pacing/vulkan/SwappyVkBase.cpp +++ b/games-frame-pacing/vulkan/SwappyVkBase.cpp @@ -425,11 +425,11 @@ std::chrono::nanoseconds SwappyVkBase::getSwapInterval() { } void SwappyVkBase::addTracer(const SwappyTracer* tracer) { - if (tracer != nullptr) mCommonBase.addTracerCallbacks(*tracer); + mCommonBase.addTracerCallbacks(*tracer); } void SwappyVkBase::removeTracer(const SwappyTracer* tracer) { - if (tracer != nullptr) mCommonBase.removeTracerCallbacks(*tracer); + mCommonBase.removeTracerCallbacks(*tracer); } int SwappyVkBase::getSupportedRefreshPeriodsNS(uint64_t* out_refreshrates, |