feat! js_interop improvements
This commit is contained in:
@@ -3,21 +3,79 @@
|
||||
#include <functional>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <chrono>
|
||||
|
||||
#include "Log.hpp"
|
||||
|
||||
namespace thermion {
|
||||
|
||||
#ifdef __EMSCRIPTEN__
|
||||
#define GL_GLEXT_PROTOTYPES
|
||||
#include <GL/gl.h>
|
||||
#include <GL/glext.h>
|
||||
#include <emscripten/html5.h>
|
||||
#include <emscripten/threading.h>
|
||||
#include <emscripten/proxying.h>
|
||||
#include <emscripten/eventloop.h>
|
||||
#include "ThermionWebApi.h"
|
||||
|
||||
std::chrono::high_resolution_clock::time_point loopStart;
|
||||
|
||||
static void mainLoop(void* arg) {
|
||||
auto *rt = static_cast<RenderThread *>(arg);
|
||||
|
||||
auto startTime = std::chrono::high_resolution_clock::now();
|
||||
|
||||
auto timeSinceLastLoopStart = std::chrono::duration_cast<std::chrono::milliseconds>(startTime - loopStart).count();
|
||||
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) {
|
||||
rt->iter();
|
||||
numIters++;
|
||||
auto now = std::chrono::high_resolution_clock::now();
|
||||
elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - startTime).count();
|
||||
}
|
||||
// Log("Spent %lldms processing, %d iters", elapsed, numIters);
|
||||
if(rt->_stop) {
|
||||
Log("RenderThread stopped")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static void *startHelper(void * parm) {
|
||||
loopStart = std::chrono::high_resolution_clock::now();
|
||||
emscripten_set_main_loop_arg(&mainLoop, parm, 0, true);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
RenderThread::RenderThread()
|
||||
{
|
||||
srand(time(NULL));
|
||||
#ifdef __EMSCRIPTEN__
|
||||
outer = pthread_self();
|
||||
pthread_attr_t attr;
|
||||
pthread_attr_init(&attr);
|
||||
emscripten_pthread_attr_settransferredcanvases(&attr, "#thermion_canvas");
|
||||
pthread_create(&t, &attr, startHelper, this);
|
||||
#else
|
||||
t = new std::thread([this]() {
|
||||
while (!_stop) {
|
||||
iter();
|
||||
mRendered = false;
|
||||
}
|
||||
});
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
RenderThread::~RenderThread()
|
||||
{
|
||||
Log("Destroying RenderThread (%d tasks remaining)", _tasks.size());
|
||||
@@ -31,48 +89,52 @@ RenderThread::~RenderThread()
|
||||
_tasks.pop_front();
|
||||
task();
|
||||
}
|
||||
|
||||
#ifdef __EMSCRIPTEN__
|
||||
pthread_join(t, NULL);
|
||||
#else
|
||||
t->join();
|
||||
delete t;
|
||||
#endif
|
||||
|
||||
TRACE("RenderThread destructor complete");
|
||||
}
|
||||
|
||||
void RenderThread::requestFrame(void (*callback)())
|
||||
void RenderThread::requestFrame()
|
||||
{
|
||||
TRACE("Request frame");
|
||||
std::unique_lock<std::mutex> lock(_mutex);
|
||||
this->_requestFrameRenderCallback = callback;
|
||||
if(mRendered) {
|
||||
return;
|
||||
}
|
||||
if(mRender) {
|
||||
Log("Warning - frame requested before previous frame has completed rendering");
|
||||
}
|
||||
mRender = true;
|
||||
_cv.notify_one();
|
||||
}
|
||||
|
||||
void RenderThread::iter()
|
||||
{
|
||||
if (mRender && !mRendered)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(_mutex);
|
||||
if (_requestFrameRenderCallback)
|
||||
mRenderTicker->render(0);
|
||||
mRender = false;
|
||||
mRendered = true;
|
||||
|
||||
// Calculate and print FPS
|
||||
auto currentTime = std::chrono::high_resolution_clock::now();
|
||||
float deltaTime = std::chrono::duration<float, std::chrono::seconds::period>(currentTime - _lastFrameTime).count();
|
||||
_lastFrameTime = currentTime;
|
||||
|
||||
_frameCount++;
|
||||
_accumulatedTime += deltaTime;
|
||||
|
||||
if (_accumulatedTime >= 1.0f) // Update FPS every second
|
||||
{
|
||||
mRenderTicker->render(0);
|
||||
lock.unlock();
|
||||
this->_requestFrameRenderCallback();
|
||||
this->_requestFrameRenderCallback = nullptr;
|
||||
|
||||
// Calculate and print FPS
|
||||
auto currentTime = std::chrono::high_resolution_clock::now();
|
||||
float deltaTime = std::chrono::duration<float, std::chrono::seconds::period>(currentTime - _lastFrameTime).count();
|
||||
_lastFrameTime = currentTime;
|
||||
|
||||
_frameCount++;
|
||||
_accumulatedTime += deltaTime;
|
||||
|
||||
if (_accumulatedTime >= 1.0f) // Update FPS every second
|
||||
{
|
||||
_fps = _frameCount / _accumulatedTime;
|
||||
_frameCount = 0;
|
||||
_accumulatedTime = 0.0f;
|
||||
}
|
||||
_fps = _frameCount / _accumulatedTime;
|
||||
_frameCount = 0;
|
||||
_accumulatedTime = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_lock<std::mutex> taskLock(_taskMutex);
|
||||
|
||||
if (!_tasks.empty())
|
||||
@@ -83,11 +145,12 @@ void RenderThread::iter()
|
||||
task();
|
||||
taskLock.lock();
|
||||
}
|
||||
|
||||
#ifndef __EMSCRIPTEN__
|
||||
_cv.wait_for(taskLock, std::chrono::microseconds(2000), [this]
|
||||
{ return !_tasks.empty() || _stop; });
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
} // namespace thermion
|
||||
} // namespace thermion
|
||||
|
||||
Reference in New Issue
Block a user