From 77eb42ce30ca80577358ca4c1cc3ac598b166297 Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Tue, 26 Sep 2023 13:00:28 +1000 Subject: [PATCH] add thread pool for some methods --- windows/polyvox_filament_plugin.cpp | 142 +++++++++++++++++++++++----- 1 file changed, 117 insertions(+), 25 deletions(-) diff --git a/windows/polyvox_filament_plugin.cpp b/windows/polyvox_filament_plugin.cpp index a0a00db6..3b7d899d 100644 --- a/windows/polyvox_filament_plugin.cpp +++ b/windows/polyvox_filament_plugin.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -37,9 +38,11 @@ #include #include "PolyvoxFilamentApi.h" +#include "ThreadPool.hpp" namespace polyvox_filament { +static ThreadPool* _tp; // static void PolyvoxFilamentPlugin::RegisterWithRegistrar( @@ -115,6 +118,10 @@ void PolyvoxFilamentPlugin::CreateFilamentViewer( const flutter::MethodCall &methodCall, std::unique_ptr> result) { + if(!_tp) { + _tp = new ThreadPool(); + } + const auto *args = std::get_if(methodCall.arguments()); @@ -126,15 +133,36 @@ void PolyvoxFilamentPlugin::CreateFilamentViewer( wglMakeCurrent(NULL, NULL); - _viewer = (void *)create_filament_viewer(_context, resourceLoader); + std::packaged_task lambda([&]() mutable { - // auto hwnd = _pluginRegistrar->GetView()->GetNativeWindow(); + _viewer = (void *)create_filament_viewer(_context, resourceLoader); - create_swap_chain(_viewer, nullptr, width, height); + // auto hwnd = _pluginRegistrar->GetView()->GetNativeWindow(); - create_render_target(_viewer, _glTextureId, width, height); + create_swap_chain(_viewer, nullptr, width, height); - result->Success(flutter::EncodableValue((int64_t)_viewer)); + create_render_target(_viewer, _glTextureId, width, height); + + result->Success(flutter::EncodableValue((int64_t)_viewer)); + }); + + std::thread([&]() { + while (true) + { + if(_rendering) { + std::packaged_task renderLambda([&]() mutable { + render(_viewer, 0, nullptr, nullptr, nullptr); + _textureRegistrar->MarkTextureFrameAvailable(_flutterTextureId); + }); + _tp->add_task(renderLambda); + } + std::this_thread::sleep_for( + std::chrono::milliseconds(_frameIntervalInMilliseconds)); + } + }).detach(); + + auto fut = _tp->add_task(lambda); + fut.wait(); } void PolyvoxFilamentPlugin::Render( @@ -146,9 +174,12 @@ void PolyvoxFilamentPlugin::Render( plugin->_flutterTextureId); }; // render(_viewer, 0, _pixelData.get(), callback, this); - render(_viewer, 0, nullptr, nullptr, nullptr); - _textureRegistrar->MarkTextureFrameAvailable(_flutterTextureId); - + std::packaged_task lambda([=]() mutable { + render(_viewer, 0, nullptr, nullptr, nullptr); + _textureRegistrar->MarkTextureFrameAvailable(_flutterTextureId); + }); + auto fut = _tp->add_task(lambda); + fut.wait(); result->Success(flutter::EncodableValue(true)); } @@ -156,6 +187,7 @@ void PolyvoxFilamentPlugin::SetRendering( const flutter::MethodCall &methodCall, std::unique_ptr> result) { _rendering = *(std::get_if(methodCall.arguments())); + result->Success(flutter::EncodableValue("OK")); } void PolyvoxFilamentPlugin::CreateTexture( @@ -445,7 +477,12 @@ void PolyvoxFilamentPlugin::SetBackgroundImage( std::get_if(methodCall.arguments()); const auto path = std::get_if(&(args->at(0))); const auto fillHeight = std::get_if(&(args->at(1))); - set_background_image(_viewer, path->c_str(), *fillHeight); + std::packaged_task lambda([&]() mutable { + set_background_image(_viewer, path->c_str(), *fillHeight); + }); + auto fut = _tp->add_task(lambda); + fut.wait(); + result->Success(flutter::EncodableValue(true)); } @@ -458,8 +495,13 @@ void PolyvoxFilamentPlugin::SetBackgroundColor( const auto g = std::get_if(&(args->at(1))); const auto b = std::get_if(&(args->at(2))); const auto a = std::get_if(&(args->at(3))); - set_background_color(_viewer, static_cast(*r), static_cast(*g), + std::packaged_task lambda([&]() mutable { + set_background_color(_viewer, static_cast(*r), static_cast(*g), static_cast(*b), static_cast(*a)); + }); + auto fut = _tp->add_task(lambda); + fut.wait(); + result->Success(flutter::EncodableValue(true)); } @@ -490,14 +532,23 @@ void PolyvoxFilamentPlugin::LoadSkybox( const flutter::MethodCall &methodCall, std::unique_ptr> result) { const auto *args = std::get_if(methodCall.arguments()); - load_skybox(_viewer, (*args).c_str()); + std::packaged_task lambda([&]() mutable { + load_skybox(_viewer, (*args).c_str()); + }); + auto fut = _tp->add_task(lambda); + fut.wait(); result->Success(flutter::EncodableValue("OK")); } void PolyvoxFilamentPlugin::RemoveIbl( const flutter::MethodCall &methodCall, std::unique_ptr> result) { - remove_ibl(_viewer); + std::packaged_task lambda([&]() mutable { + remove_ibl(_viewer); + }); + auto fut = _tp->add_task(lambda); + fut.wait(); + result->Success(flutter::EncodableValue("OK")); } @@ -508,14 +559,24 @@ void PolyvoxFilamentPlugin::LoadIbl( std::get_if(methodCall.arguments()); const auto path = std::get_if(&(args->at(0))); const auto intensity = std::get_if(&(args->at(1))); - load_ibl(_viewer, (*path).c_str(), static_cast(*intensity)); + std::packaged_task lambda([&]() mutable { + load_ibl(_viewer, (*path).c_str(), static_cast(*intensity)); + }); + auto fut = _tp->add_task(lambda); + fut.wait(); + result->Success(flutter::EncodableValue("OK")); } void PolyvoxFilamentPlugin::RemoveSkybox( const flutter::MethodCall &methodCall, std::unique_ptr> result) { - remove_skybox(_viewer); + std::packaged_task lambda([&]() mutable { + remove_skybox(_viewer); + }); + auto fut = _tp->add_task(lambda); + fut.wait(); + result->Success(flutter::EncodableValue("OK")); } @@ -534,9 +595,15 @@ void PolyvoxFilamentPlugin::AddLight( const auto dirY = *std::get_if(&(args->at(7))); const auto dirZ = *std::get_if(&(args->at(8))); const auto shadows = *std::get_if(&(args->at(9))); - auto entityId = add_light(_viewer, type, color, intensity, posX, posY, posZ, + + std::packaged_task lambda([&]() mutable { + auto entityId = add_light(_viewer, type, color, intensity, posX, posY, posZ, dirX, dirY, dirZ, shadows); - result->Success(flutter::EncodableValue(entityId)); + result->Success(flutter::EncodableValue(entityId)); + }); + auto fut = _tp->add_task(lambda); + fut.wait(); + } void PolyvoxFilamentPlugin::LoadGlb( @@ -549,8 +616,14 @@ void PolyvoxFilamentPlugin::LoadGlb( const auto assetManager = *std::get_if(&(args->at(0))); const auto path = *std::get_if(&(args->at(1))); const auto unlit = *std::get_if(&(args->at(2))); - auto entityId = load_glb((void *)assetManager, path.c_str(), unlit); - result->Success(flutter::EncodableValue(entityId)); + + std::packaged_task lambda([&]() mutable { + auto entityId = load_glb((void *)assetManager, path.c_str(), unlit); + result->Success(flutter::EncodableValue(entityId)); + }); + auto fut = _tp->add_task(lambda); + fut.wait(); + } void PolyvoxFilamentPlugin::GetAnimationNames( @@ -583,8 +656,12 @@ void PolyvoxFilamentPlugin::RemoveAsset( const auto *args = std::get_if(methodCall.arguments()); const auto asset = *std::get_if(&(args->at(1))); - remove_asset(_viewer, asset); - result->Success(flutter::EncodableValue("OK")); + std::packaged_task lambda([&]() mutable { + remove_asset(_viewer, asset); + result->Success(flutter::EncodableValue("OK")); + }); + auto fut = _tp->add_task(lambda); + fut.wait(); } void PolyvoxFilamentPlugin::TransformToUnitCube( @@ -596,8 +673,13 @@ void PolyvoxFilamentPlugin::TransformToUnitCube( const auto assetManager = *std::get_if(&(args->at(0))); const auto asset = *std::get_if(&(args->at(1))); - transform_to_unit_cube((void *)assetManager, asset); - result->Success(flutter::EncodableValue("OK")); + std::packaged_task lambda([&]() mutable { + transform_to_unit_cube((void *)assetManager, asset); + result->Success(flutter::EncodableValue("OK")); + }); + auto fut = _tp->add_task(lambda); + fut.wait(); + } void PolyvoxFilamentPlugin::RotateStart( @@ -610,7 +692,10 @@ void PolyvoxFilamentPlugin::RotateStart( const auto x = *std::get_if(&(args->at(0))); const auto y = *std::get_if(&(args->at(1))); - grab_begin(_viewer, static_cast(x), static_cast(y), false); + std::packaged_task lambda([=]() mutable { + grab_begin(_viewer, static_cast(x), static_cast(y), false); + }); + auto fut = _tp->add_task(lambda); result->Success(flutter::EncodableValue("OK")); } @@ -618,7 +703,10 @@ void PolyvoxFilamentPlugin::RotateStart( void PolyvoxFilamentPlugin::RotateEnd( const flutter::MethodCall &methodCall, std::unique_ptr> result) { - grab_end(_viewer); + std::packaged_task lambda([=]() mutable { + grab_end(_viewer); + }); + auto fut = _tp->add_task(lambda); result->Success(flutter::EncodableValue("OK")); } @@ -629,7 +717,11 @@ void PolyvoxFilamentPlugin::RotateUpdate( std::get_if(methodCall.arguments()); const auto x = *std::get_if(&(args->at(0))); const auto y = *std::get_if(&(args->at(1))); - grab_update(_viewer, static_cast(x), static_cast(y)); + std::packaged_task lambda([=]() mutable { + grab_update(_viewer, static_cast(x), static_cast(y)); + }); + auto fut = _tp->add_task(lambda); + result->Success(flutter::EncodableValue("OK")); }