From e147e59d8d87f2b9586e839e22b52b81403941e4 Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Tue, 3 Jun 2025 16:39:04 +0800 Subject: [PATCH] don't call endFrame() unless beginFrame() was called only use condition_variable on non-emscripten builds --- thermion_dart/native/include/Log.hpp | 2 +- .../native/include/rendering/RenderThread.hpp | 7 +++-- thermion_dart/native/src/RenderTicker.cpp | 26 ++++++++++--------- .../native/src/rendering/RenderThread.cpp | 15 +++++++---- .../native/web/include/ThermionWebApi.h | 1 + .../native/web/src/cpp/ThermionWebApi.cpp | 18 ++++++++----- 6 files changed, 41 insertions(+), 28 deletions(-) diff --git a/thermion_dart/native/include/Log.hpp b/thermion_dart/native/include/Log.hpp index 6ecd2cde..44e5de76 100644 --- a/thermion_dart/native/include/Log.hpp +++ b/thermion_dart/native/include/Log.hpp @@ -32,7 +32,7 @@ static void Log(const char *fmt, ...) { #endif va_end(args); } -#endif +#endif // ifdef __EMSCRIPTEN__ #if defined(_WIN32) || defined(_WIN64) #define __FILENAME__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__) diff --git a/thermion_dart/native/include/rendering/RenderThread.hpp b/thermion_dart/native/include/rendering/RenderThread.hpp index bc7c5061..9e8e8de0 100644 --- a/thermion_dart/native/include/rendering/RenderThread.hpp +++ b/thermion_dart/native/include/rendering/RenderThread.hpp @@ -75,10 +75,8 @@ public: #endif bool mRendered = false; - -private: - bool mRender = false; +private: std::mutex _taskMutex; std::condition_variable _cv; std::deque> _tasks; @@ -106,8 +104,9 @@ auto RenderThread::add_task(std::packaged_task& pt) -> std::future { _tasks.push_back([pt = std::make_shared>( std::move(pt))] { (*pt)(); }); - + #ifndef __EMSCRIPTEN__ _cv.notify_one(); + #endif return ret; diff --git a/thermion_dart/native/src/RenderTicker.cpp b/thermion_dart/native/src/RenderTicker.cpp index 471809c4..e5014c02 100644 --- a/thermion_dart/native/src/RenderTicker.cpp +++ b/thermion_dart/native/src/RenderTicker.cpp @@ -65,16 +65,19 @@ namespace thermion bool RenderTicker::render(uint64_t frameTimeInNanos) { auto startTime = std::chrono::high_resolution_clock::now(); - bool rendered = false; + std::lock_guard lock(mMutex); for (auto animationManager : mAnimationManagers) { animationManager->update(frameTimeInNanos); - TRACE("Updated AnimationManager"); } + + auto durationNs = std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - mLastRender).count() / 1e6f; + TRACE("Updated animations in %.3f ms", durationNs); + + int numRendered = 0; #ifdef ENABLE_TRACING - int numRendered = 0; TRACE("%d swapchains", mRenderable.size()); #endif @@ -87,22 +90,21 @@ namespace thermion bool beginFrame = mRenderer->beginFrame(swapChain, frameTimeInNanos); if (beginFrame) { - rendered = true; - auto durationNs = std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - mLastRender).count() / 1e6f; + numRendered++; + durationNs = std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - mLastRender).count() / 1e6f; TRACE("Beginning frame (%.3f ms since last endFrame())", durationNs); for (auto view : views) { mRenderer->render(view); } - mLastRender = std::chrono::high_resolution_clock::now(); + mLastRender = std::chrono::high_resolution_clock::now(); + mRenderer->endFrame(); } else { - auto durationNs = std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - mLastRender).count() / 1e6f; + durationNs = std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - mLastRender).count() / 1e6f; TRACE("Skipping frame (%.3f ms since last endFrame())", durationNs); } - mRenderer->endFrame(); - + #ifdef ENABLE_TRACING - numRendered++; } else { TRACE("No views for swapchain"); } @@ -115,11 +117,11 @@ namespace thermion mEngine->execute(); #endif auto endTime = std::chrono::high_resolution_clock::now(); - auto durationNs = std::chrono::duration_cast(endTime - startTime).count(); + durationNs = std::chrono::duration_cast(endTime - startTime).count(); float durationMs = durationNs / 1e6f; TRACE("Total render() time: %.3f ms", durationMs); - return rendered; + return numRendered > 0; } void RenderTicker::addAnimationManager(AnimationManager* animationManager) { diff --git a/thermion_dart/native/src/rendering/RenderThread.cpp b/thermion_dart/native/src/rendering/RenderThread.cpp index e1d6c2eb..b97fc0d1 100644 --- a/thermion_dart/native/src/rendering/RenderThread.cpp +++ b/thermion_dart/native/src/rendering/RenderThread.cpp @@ -27,20 +27,23 @@ static void mainLoop(void* arg) { auto startTime = std::chrono::high_resolution_clock::now(); auto timeSinceLastLoopStart = std::chrono::duration_cast(startTime - loopStart).count(); - if(timeSinceLastLoopStart > 20) { - Log("%dms elapsed since last loop", timeSinceLastLoopStart); - } + // if(timeSinceLastLoopStart > 20) { + // Log("%dms elapsed since last loop", timeSinceLastLoopStart); + // } + loopStart = startTime; rt->mRendered = false; long long elapsed = 0; int numIters = 0; - while (!rt->_stop && elapsed < 10) { + while (!rt->_stop && elapsed < 12) { rt->iter(); numIters++; auto now = std::chrono::high_resolution_clock::now(); elapsed = std::chrono::duration_cast(now - startTime).count(); } - // Log("Spent %lldms processing, %d iters", elapsed, numIters); + // if(!rt->mRendered) { + // Log("Spent %lldms processing, %d iters, rendered %s, render requested %s, context lost %s", elapsed, numIters, rt->mRendered ? "true" : "false", rt->mRender ? "true" : "false", emscripten_is_webgl_context_lost(Thermion_getGLContext()) ? "yes" : "no"); + // } if(rt->_stop) { Log("RenderThread stopped") emscripten_set_main_loop_arg(nullptr, nullptr, 0, true); @@ -109,7 +112,9 @@ void RenderThread::requestFrame() TRACE("Warning - frame requested before previous frame has completed rendering"); } mRender = true; + #ifndef __EMSCRIPTEN__ _cv.notify_one(); + #endif } void RenderThread::iter() diff --git a/thermion_dart/native/web/include/ThermionWebApi.h b/thermion_dart/native/web/include/ThermionWebApi.h index 6e37033f..0de321fe 100644 --- a/thermion_dart/native/web/include/ThermionWebApi.h +++ b/thermion_dart/native/web/include/ThermionWebApi.h @@ -8,6 +8,7 @@ extern "C" { void Thermion_resizeCanvas(int width, int height); EMSCRIPTEN_WEBGL_CONTEXT_HANDLE Thermion_createGLContext(); +EMSCRIPTEN_WEBGL_CONTEXT_HANDLE Thermion_getGLContext(); #ifdef __cplusplus diff --git a/thermion_dart/native/web/src/cpp/ThermionWebApi.cpp b/thermion_dart/native/web/src/cpp/ThermionWebApi.cpp index 9d139410..cc2557a4 100644 --- a/thermion_dart/native/web/src/cpp/ThermionWebApi.cpp +++ b/thermion_dart/native/web/src/cpp/ThermionWebApi.cpp @@ -24,7 +24,13 @@ extern "C" EMSCRIPTEN_KEEPALIVE void Thermion_resizeCanvas(int width, int height) { emscripten_set_canvas_element_size("#thermion_canvas", width, height); } - + + static EMSCRIPTEN_WEBGL_CONTEXT_HANDLE _context; + + EMSCRIPTEN_WEBGL_CONTEXT_HANDLE EMSCRIPTEN_KEEPALIVE Thermion_getGLContext() { + return _context; + } + EMSCRIPTEN_WEBGL_CONTEXT_HANDLE EMSCRIPTEN_KEEPALIVE Thermion_createGLContext() { std::cout << "Creating WebGL context." << std::endl; @@ -43,16 +49,16 @@ extern "C" attr.renderViaOffscreenBackBuffer = EM_FALSE; attr.majorVersion = 2; - auto context = emscripten_webgl_create_context("#thermion_canvas", &attr); + _context = emscripten_webgl_create_context("#thermion_canvas", &attr); - if(!context) { + if(!_context) { std::cout << "Failed to create WebGL context" << std::endl; - return context; + return _context; } std::cout << "Created WebGL context " << attr.majorVersion << "." << attr.minorVersion << std::endl; - auto success = emscripten_webgl_make_context_current((EMSCRIPTEN_WEBGL_CONTEXT_HANDLE)context); + auto success = emscripten_webgl_make_context_current(_context); if(success != EMSCRIPTEN_RESULT_SUCCESS) { std::cout << "Failed to make WebGL context current"<< std::endl; } else { @@ -65,7 +71,7 @@ extern "C" glClear(GL_COLOR_BUFFER_BIT); } std::cout << "Returning context" << std::endl; - return context; + return _context; } emscripten::val emscripten_make_uint8_buffer(int ptr, int length) {