Fix encoder thread destruction
This commit is contained in:
@@ -85,14 +85,7 @@ static void destroy_images(struct wivrn_comp_target * cn)
|
||||
target_fini_semaphores(cn);
|
||||
}
|
||||
|
||||
struct encoder_thread_param
|
||||
{
|
||||
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 * comp_wivrn_present_thread(std::stop_token stop_token, wivrn_comp_target * cn, int index, std::vector<std::shared_ptr<VideoEncoder>> encoders);
|
||||
|
||||
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.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)
|
||||
{
|
||||
@@ -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));
|
||||
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)
|
||||
{
|
||||
auto params_ptr = new encoder_thread_param(params);
|
||||
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;
|
||||
os_thread_helper_start(&thread.thread, comp_wivrn_present_thread, params_ptr);
|
||||
auto & thread = cn->encoder_threads.emplace_back(
|
||||
std::jthread(comp_wivrn_present_thread, cn, cn->encoder_threads.size(), std::move(params)),
|
||||
cn->psc.present_gen);
|
||||
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->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);
|
||||
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<int> indices(cn->image_count);
|
||||
while (os_thread_helper_is_running(¶m->thread->thread))
|
||||
while (not stop_token.stop_requested())
|
||||
{
|
||||
int presenting_index = -1;
|
||||
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);
|
||||
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);
|
||||
|
||||
@@ -430,7 +418,7 @@ static void * comp_wivrn_present_thread(void * void_param)
|
||||
const auto & psc_image = cn->psc.images[presenting_index];
|
||||
try
|
||||
{
|
||||
for (auto & encoder: param->encoders)
|
||||
for (auto & encoder: encoders)
|
||||
{
|
||||
encoder->Encode(*cn->cnx, psc_image.view_info, psc_image.frame_index);
|
||||
}
|
||||
|
||||
@@ -84,22 +84,23 @@ struct wivrn_comp_target : public comp_target
|
||||
|
||||
struct encoder_thread
|
||||
{
|
||||
int index;
|
||||
os_thread_helper thread;
|
||||
std::jthread thread;
|
||||
pseudo_swapchain::status_type & gen;
|
||||
std::jthread stopper;
|
||||
|
||||
encoder_thread(pseudo_swapchain::status_type & gen) :
|
||||
gen(gen)
|
||||
{
|
||||
os_thread_helper_init(&thread);
|
||||
}
|
||||
encoder_thread(std::jthread && thread, pseudo_swapchain::status_type & gen) :
|
||||
thread(std::move(thread)), gen(gen) {}
|
||||
~encoder_thread()
|
||||
{
|
||||
os_thread_helper_signal_stop(&thread);
|
||||
gen += 1;
|
||||
gen.notify_all();
|
||||
os_thread_helper_wait_locked(&thread);
|
||||
os_thread_helper_destroy(&thread);
|
||||
thread.request_stop();
|
||||
// Encoder will be in a loop on the gen variable, wake them up
|
||||
stopper = std::jthread([&]() {
|
||||
while (not stopper.get_stop_token().stop_requested())
|
||||
{
|
||||
gen += 1;
|
||||
gen.notify_all();
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
} });
|
||||
}
|
||||
};
|
||||
std::vector<encoder_settings> settings;
|
||||
|
||||
Reference in New Issue
Block a user