render implement thread, wrap GL commands into tasks
This commit is contained in:
65
src/app.h
65
src/app.h
@@ -249,4 +249,69 @@ public:
|
||||
bool get_ui_rtl() const;
|
||||
|
||||
void cmd_convert(std::string pano_path, std::string out_path);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// 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> 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())
|
||||
{
|
||||
pt();
|
||||
}
|
||||
else
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(render_task_mutex);
|
||||
render_tasklist.push_back(std::move(pt));
|
||||
}
|
||||
render_cv.notify_all();
|
||||
}
|
||||
return f;
|
||||
#endif // _WIN32
|
||||
}
|
||||
|
||||
template<typename T, typename R = std::result_of<T()>::type>
|
||||
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())
|
||||
{
|
||||
pt();
|
||||
}
|
||||
else
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(render_task_mutex);
|
||||
render_tasklist.push_back(std::move(pt));
|
||||
}
|
||||
render_cv.notify_all();
|
||||
}
|
||||
return f.get();
|
||||
#endif // _WIN32
|
||||
}
|
||||
|
||||
void render_sync()
|
||||
{
|
||||
render_task([] {});
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user