diff --git a/server/driver/wivrn_comp_target.cpp b/server/driver/wivrn_comp_target.cpp index 6a066e20..871b033c 100644 --- a/server/driver/wivrn_comp_target.cpp +++ b/server/driver/wivrn_comp_target.cpp @@ -71,10 +71,8 @@ static inline struct vk_bundle * get_vk(struct wivrn_comp_target * cn) static void destroy_images(struct wivrn_comp_target * cn) { - if (cn->images == NULL) - { + if (cn->images == nullptr) return; - } cn->encoder_threads.clear(); cn->encoders.clear(); @@ -123,7 +121,7 @@ static void create_encoders(wivrn_comp_target * cn) for (auto & [group, params]: thread_params) { auto params_ptr = new encoder_thread_param(params); - auto & thread = cn->encoder_threads.emplace_back(); + auto & thread = cn->encoder_threads.emplace_back(cn->psc.present_gen); thread.index = cn->encoder_threads.size() - 1; params_ptr->thread = &thread; params_ptr->cn = cn; @@ -386,7 +384,6 @@ static void * comp_wivrn_present_thread(void * void_param) U_LOG_I("Starting encoder thread %d", param->thread->index); uint8_t status_bit = 1 << (param->thread->index + 1); - std::mutex dummy_mutex; std::vector fences(cn->image_count); std::vector indices(cn->image_count); @@ -396,6 +393,7 @@ static void * comp_wivrn_present_thread(void * void_param) int nb_fences = 0; { uint64_t timestamp = 0; + auto gen = cn->psc.present_gen.load(); for (uint32_t i = 0; i < cn->image_count; i++) { @@ -412,10 +410,7 @@ static void * comp_wivrn_present_thread(void * void_param) if (presenting_index < 0) { - std::unique_lock lock(dummy_mutex); - // condition variable is not notified when we want to stop thread, - // use a timeout, but longer than a typical frame - cn->psc.cv.wait_for(lock, std::chrono::milliseconds(50)); + cn->psc.present_gen.wait(gen); continue; } } @@ -544,7 +539,8 @@ static VkResult comp_wivrn_present(struct comp_target * ct, view_info.pose[eye] = xrt_cast(result.pose); } } - cn->psc.cv.notify_all(); + cn->psc.present_gen += 1; + cn->psc.present_gen.notify_all(); return VK_SUCCESS; } diff --git a/server/driver/wivrn_comp_target.h b/server/driver/wivrn_comp_target.h index b88d5bcd..b460d163 100644 --- a/server/driver/wivrn_comp_target.h +++ b/server/driver/wivrn_comp_target.h @@ -30,7 +30,6 @@ #include "driver/wivrn_pacer.h" #include "encoder/encoder_settings.h" -#include #include #include #include @@ -61,7 +60,8 @@ struct pseudo_swapchain to_headset::video_stream_data_shard::view_info_t view_info{}; }; std::unique_ptr images; - std::condition_variable cv; + // Incremented and notified when a new image is available for encoders + status_type present_gen; }; struct wivrn_comp_target : public comp_target @@ -86,14 +86,19 @@ struct wivrn_comp_target : public comp_target { int index; os_thread_helper thread; + pseudo_swapchain::status_type & gen; - encoder_thread() + encoder_thread(pseudo_swapchain::status_type & gen) : + gen(gen) { os_thread_helper_init(&thread); } ~encoder_thread() { - os_thread_helper_stop_and_wait(&thread); + os_thread_helper_signal_stop(&thread); + gen += 1; + gen.notify_all(); + os_thread_helper_wait_locked(&thread); os_thread_helper_destroy(&thread); } };