move render thread to App class and add ui thread with ui tasks queue
This commit is contained in:
97
src/app.h
97
src/app.h
@@ -73,7 +73,7 @@ public:
|
||||
std::mutex rec_mutex;
|
||||
std::condition_variable rec_cv;
|
||||
std::deque<std::unique_ptr<uint8_t[]>> rec_frames;
|
||||
|
||||
|
||||
RTT uirtt;
|
||||
Sampler sampler;
|
||||
Sampler sampler_stencil;
|
||||
@@ -251,11 +251,23 @@ public:
|
||||
|
||||
void cmd_convert(std::string pano_path, std::string out_path);
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// RENDER THREAD
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
std::deque<std::packaged_task<void()>> render_tasklist;
|
||||
std::mutex render_task_mutex;
|
||||
std::condition_variable render_cv;
|
||||
std::thread render_thread;
|
||||
std::thread::id render_thread_id;
|
||||
bool render_running = false;
|
||||
void render_thread_main();
|
||||
void render_thread_start();
|
||||
void render_thread_stop();
|
||||
|
||||
bool is_render_thread()
|
||||
{
|
||||
extern std::thread::id render_thread_id;
|
||||
extern std::thread::id gl_thread;
|
||||
return std::this_thread::get_id() == render_thread_id || std::this_thread::get_id() == gl_thread;
|
||||
return std::this_thread::get_id() == render_thread_id;
|
||||
}
|
||||
|
||||
// don't capture a reference to this ptr as the object may be destroyed
|
||||
@@ -264,9 +276,6 @@ public:
|
||||
std::future<R> render_task_async(T task)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
extern std::deque<std::packaged_task<R()>> render_tasklist;
|
||||
extern std::mutex render_task_mutex;
|
||||
extern std::condition_variable render_cv;
|
||||
std::packaged_task<R()> pt(task);
|
||||
std::future<R> f = pt.get_future();
|
||||
if (is_render_thread())
|
||||
@@ -289,9 +298,6 @@ public:
|
||||
R render_task(T task)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
extern std::deque<std::packaged_task<R()>> render_tasklist;
|
||||
extern std::mutex render_task_mutex;
|
||||
extern std::condition_variable render_cv;
|
||||
std::packaged_task<R()> pt(task);
|
||||
std::future<R> f = pt.get_future();
|
||||
if (is_render_thread())
|
||||
@@ -306,7 +312,7 @@ public:
|
||||
}
|
||||
render_cv.notify_all();
|
||||
}
|
||||
return f.get();
|
||||
return render_running ? f.get() : R();
|
||||
#endif // _WIN32
|
||||
}
|
||||
|
||||
@@ -315,4 +321,73 @@ public:
|
||||
render_task([] {});
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// UI THREAD
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
std::deque<std::packaged_task<void()>> ui_tasklist;
|
||||
std::mutex ui_task_mutex;
|
||||
std::condition_variable ui_cv;
|
||||
std::thread ui_thread;
|
||||
std::thread::id ui_thread_id;
|
||||
bool ui_running = false;
|
||||
void ui_thread_main();
|
||||
void ui_thread_start();
|
||||
void ui_thread_stop();
|
||||
|
||||
bool is_ui_thread()
|
||||
{
|
||||
return std::this_thread::get_id() == ui_thread_id;
|
||||
}
|
||||
|
||||
// don't capture a reference to this ptr as the object may be destroyed
|
||||
// by the time the task is executed
|
||||
template<typename T, typename R = std::result_of<T()>::type>
|
||||
std::future<R> ui_task_async(T task)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
std::packaged_task<R()> pt(task);
|
||||
std::future<R> f = pt.get_future();
|
||||
if (is_ui_thread())
|
||||
{
|
||||
pt();
|
||||
}
|
||||
else
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(ui_task_mutex);
|
||||
ui_tasklist.push_back(std::move(pt));
|
||||
}
|
||||
ui_cv.notify_all();
|
||||
}
|
||||
return f;
|
||||
#endif // _WIN32
|
||||
}
|
||||
|
||||
template<typename T, typename R = std::result_of<T()>::type>
|
||||
R ui_task(T task)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
std::packaged_task<R()> pt(task);
|
||||
std::future<R> f = pt.get_future();
|
||||
if (is_ui_thread())
|
||||
{
|
||||
pt();
|
||||
}
|
||||
else
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(ui_task_mutex);
|
||||
ui_tasklist.push_back(std::move(pt));
|
||||
}
|
||||
ui_cv.notify_all();
|
||||
}
|
||||
return ui_running ? f.get() : R();
|
||||
#endif // _WIN32
|
||||
}
|
||||
|
||||
void ui_sync()
|
||||
{
|
||||
ui_task([] {});
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user