Fix encoder thread destruction

This commit is contained in:
Patrick Nicolas
2024-07-25 11:56:45 +02:00
parent 142d453c98
commit af48d32c37
2 changed files with 26 additions and 37 deletions
+13 -25
View File
@@ -85,14 +85,7 @@ static void destroy_images(struct wivrn_comp_target * cn)
target_fini_semaphores(cn); target_fini_semaphores(cn);
} }
struct encoder_thread_param static void * comp_wivrn_present_thread(std::stop_token stop_token, wivrn_comp_target * cn, int index, std::vector<std::shared_ptr<VideoEncoder>> encoders);
{
wivrn_comp_target * cn;
wivrn_comp_target::encoder_thread * thread;
std::vector<std::shared_ptr<VideoEncoder>> encoders;
};
static void * comp_wivrn_present_thread(void * void_param);
static void create_encoders(wivrn_comp_target * cn) static void create_encoders(wivrn_comp_target * cn)
{ {
@@ -106,7 +99,7 @@ static void create_encoders(wivrn_comp_target * cn)
desc.height = cn->height; desc.height = cn->height;
desc.foveation = cn->cnx->set_foveated_size(desc.width, desc.height); desc.foveation = cn->cnx->set_foveated_size(desc.width, desc.height);
std::map<int, encoder_thread_param> thread_params; std::map<int, std::vector<std::shared_ptr<VideoEncoder>>> thread_params;
for (auto & settings: cn->settings) for (auto & settings: cn->settings)
{ {
@@ -115,19 +108,16 @@ static void create_encoders(wivrn_comp_target * cn)
VideoEncoder::Create(*cn->wivrn_bundle, settings, stream_index, desc.width, desc.height, desc.fps)); VideoEncoder::Create(*cn->wivrn_bundle, settings, stream_index, desc.width, desc.height, desc.fps));
desc.items.push_back(settings); desc.items.push_back(settings);
thread_params[settings.group].encoders.emplace_back(encoder); thread_params[settings.group].emplace_back(encoder);
} }
for (auto & [group, params]: thread_params) 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); std::jthread(comp_wivrn_present_thread, cn, cn->encoder_threads.size(), std::move(params)),
thread.index = cn->encoder_threads.size() - 1; cn->psc.present_gen);
params_ptr->thread = &thread;
params_ptr->cn = cn;
os_thread_helper_start(&thread.thread, comp_wivrn_present_thread, params_ptr);
std::string name = "encoder " + std::to_string(group); std::string name = "encoder " + std::to_string(group);
os_thread_helper_name(&thread.thread, name.c_str()); pthread_setname_np(thread.thread.native_handle(), name.c_str());
} }
cn->pacer.set_stream_count(cn->encoders.size()); cn->pacer.set_stream_count(cn->encoders.size());
cn->cnx->send_control(desc); cn->cnx->send_control(desc);
@@ -376,18 +366,16 @@ static VkResult comp_wivrn_acquire(struct comp_target * ct, uint32_t * out_index
}; };
} }
static void * comp_wivrn_present_thread(void * void_param) static void * comp_wivrn_present_thread(std::stop_token stop_token, wivrn_comp_target * cn, int index, std::vector<std::shared_ptr<VideoEncoder>> encoders)
{ {
std::unique_ptr<encoder_thread_param> param((encoder_thread_param *)void_param);
struct wivrn_comp_target * cn = param->cn;
struct vk_bundle * vk = get_vk(cn); struct vk_bundle * vk = get_vk(cn);
U_LOG_I("Starting encoder thread %d", param->thread->index); U_LOG_I("Starting encoder thread %d", index);
uint8_t status_bit = 1 << (param->thread->index + 1); uint8_t status_bit = 1 << (index + 1);
std::vector<VkFence> fences(cn->image_count); std::vector<VkFence> fences(cn->image_count);
std::vector<int> indices(cn->image_count); std::vector<int> indices(cn->image_count);
while (os_thread_helper_is_running(&param->thread->thread)) while (not stop_token.stop_requested())
{ {
int presenting_index = -1; int presenting_index = -1;
int nb_fences = 0; int nb_fences = 0;
@@ -418,7 +406,7 @@ static void * comp_wivrn_present_thread(void * void_param)
VkResult res = vk->vkWaitForFences(vk->device, nb_fences, fences.data(), VK_TRUE, UINT64_MAX); VkResult res = vk->vkWaitForFences(vk->device, nb_fences, fences.data(), VK_TRUE, UINT64_MAX);
if (nb_fences > 1) if (nb_fences > 1)
{ {
U_LOG_I("Encoder group %d dropped %d frames", param->thread->index, nb_fences - 1); U_LOG_I("Encoder group %d dropped %d frames", index, nb_fences - 1);
} }
VK_CHK_WITH_RET(res, "vkWaitForFences", NULL); VK_CHK_WITH_RET(res, "vkWaitForFences", NULL);
@@ -430,7 +418,7 @@ static void * comp_wivrn_present_thread(void * void_param)
const auto & psc_image = cn->psc.images[presenting_index]; const auto & psc_image = cn->psc.images[presenting_index];
try try
{ {
for (auto & encoder: param->encoders) for (auto & encoder: encoders)
{ {
encoder->Encode(*cn->cnx, psc_image.view_info, psc_image.frame_index); encoder->Encode(*cn->cnx, psc_image.view_info, psc_image.frame_index);
} }
+13 -12
View File
@@ -84,22 +84,23 @@ struct wivrn_comp_target : public comp_target
struct encoder_thread struct encoder_thread
{ {
int index; std::jthread thread;
os_thread_helper thread;
pseudo_swapchain::status_type & gen; pseudo_swapchain::status_type & gen;
std::jthread stopper;
encoder_thread(pseudo_swapchain::status_type & gen) : encoder_thread(std::jthread && thread, pseudo_swapchain::status_type & gen) :
gen(gen) thread(std::move(thread)), gen(gen) {}
{
os_thread_helper_init(&thread);
}
~encoder_thread() ~encoder_thread()
{ {
os_thread_helper_signal_stop(&thread); thread.request_stop();
gen += 1; // Encoder will be in a loop on the gen variable, wake them up
gen.notify_all(); stopper = std::jthread([&]() {
os_thread_helper_wait_locked(&thread); while (not stopper.get_stop_token().stop_requested())
os_thread_helper_destroy(&thread); {
gen += 1;
gen.notify_all();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
} });
} }
}; };
std::vector<encoder_settings> settings; std::vector<encoder_settings> settings;