Skip to content

Commit

Permalink
More style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bghgary committed May 9, 2023
1 parent e9d229d commit 423ed5e
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 97 deletions.
12 changes: 4 additions & 8 deletions Core/AppRuntime/Source/AppRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ namespace Babylon
: m_workQueue{std::make_unique<WorkQueue>([this] { RunPlatformTier(); })}
, m_unhandledExceptionHandler{unhandledExceptionHandler}
{
Dispatch([this](Napi::Env env)
{
Dispatch([this](Napi::Env env) {
JsRuntime::CreateForJavaScript(env, [this](auto func) { Dispatch(std::move(func)); });
});
}
Expand All @@ -24,8 +23,7 @@ namespace Babylon
// Notify the JsRuntime on the JavaScript thread that the JavaScript
// runtime shutdown sequence has begun. The JsRuntimeScheduler will
// use this signal to gracefully cancel asynchronous operations.
Dispatch([](Napi::Env env)
{
Dispatch([](Napi::Env env) {
JsRuntime::NotifyDisposing(JsRuntime::GetFromJavaScript(env));
});
}
Expand All @@ -47,10 +45,8 @@ namespace Babylon

void AppRuntime::Dispatch(Dispatchable<void(Napi::Env)> func)
{
m_workQueue->Append([this, func{std::move(func)}](Napi::Env env) mutable
{
Execute([this, env, func{std::move(func)}]() mutable
{
m_workQueue->Append([this, func{std::move(func)}](Napi::Env env) mutable {
Execute([this, env, func{std::move(func)}]() mutable {
try
{
func(env);
Expand Down
19 changes: 10 additions & 9 deletions Core/AppRuntime/Source/AppRuntime_Chakra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ namespace Babylon
ThrowIfFailed(JsSetCurrentContext(context));
ThrowIfFailed(JsSetPromiseContinuationCallback(
[](JsValueRef task, void* callbackState) {
auto* pThis = reinterpret_cast<AppRuntime*>(callbackState);
ThrowIfFailed(JsAddRef(task, nullptr));
pThis->Dispatch([task](auto) {
JsValueRef global;
ThrowIfFailed(JsGetGlobalObject(&global));
ThrowIfFailed(JsCallFunction(task, &global, 1, nullptr));
ThrowIfFailed(JsRelease(task, nullptr));
});
}, this));
auto* pThis = reinterpret_cast<AppRuntime*>(callbackState);
ThrowIfFailed(JsAddRef(task, nullptr));
pThis->Dispatch([task](auto) {
JsValueRef global;
ThrowIfFailed(JsGetGlobalObject(&global));
ThrowIfFailed(JsCallFunction(task, &global, 1, nullptr));
ThrowIfFailed(JsRelease(task, nullptr));
});
},
this));
ThrowIfFailed(JsProjectWinRTNamespace(L"Windows"));

#if defined(_DEBUG)
Expand Down
3 changes: 1 addition & 2 deletions Core/Graphics/Source/DeviceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,7 @@ namespace Babylon::Graphics
{
arcana::task_completion_source<std::vector<uint8_t>, std::exception_ptr> taskCompletionSource{};

m_screenShotCallbacks.push([taskCompletionSource](std::vector<uint8_t> bytes) mutable
{
m_screenShotCallbacks.push([taskCompletionSource](std::vector<uint8_t> bytes) mutable {
taskCompletionSource.complete(std::move(bytes));
});

Expand Down
10 changes: 5 additions & 5 deletions Core/JsRuntime/InternalInclude/Babylon/JsRuntimeScheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ namespace Babylon
// // Wait for asynchronous operations to complete.
// m_runtimeScheduler.Rundown();
// }
//
//
// void MyFunction(const Napi::CallbackInfo& info)
// {
// const auto callback{info[0].As<Napi::Function>()};
//
//
// arcana::make_task(arcana::threadpool_scheduler, m_cancellationSource, []() {
// // do some asynchronous work
// }).then(m_runtimeScheduler.Get(), m_cancellationSource, [thisRef = Napi::Persistent(info.This()), callback = Napi::Persistent(callback)]() {
Expand Down Expand Up @@ -101,7 +101,8 @@ namespace Babylon
class SchedulerImpl
{
public:
explicit SchedulerImpl(JsRuntimeScheduler& parent) : m_parent{parent}
explicit SchedulerImpl(JsRuntimeScheduler& parent)
: m_parent{parent}
{
}

Expand All @@ -122,8 +123,7 @@ namespace Babylon

if (m_runtime != nullptr)
{
m_runtime->Dispatch([callable{std::forward<CallableT>(callable)}](Napi::Env)
{
m_runtime->Dispatch([callable{std::forward<CallableT>(callable)}](Napi::Env) {
callable();
});
}
Expand Down
9 changes: 3 additions & 6 deletions Plugins/NativeEngine/Source/NativeEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1132,14 +1132,11 @@ namespace Babylon
const auto textureDestination = info[0].As<Napi::Pointer<Graphics::Texture>>().Get();
const auto textureSource = info[1].As<Napi::Pointer<Graphics::Texture>>().Get();

arcana::make_task(m_update.Scheduler(), m_cancellationSource, [this, textureDestination, textureSource]() mutable
{
return arcana::make_task(m_runtimeScheduler.Get(), m_cancellationSource, [this, textureDestination, textureSource, updateToken = m_update.GetUpdateToken()]() mutable
{
arcana::make_task(m_update.Scheduler(), m_cancellationSource, [this, textureDestination, textureSource]() mutable {
return arcana::make_task(m_runtimeScheduler.Get(), m_cancellationSource, [this, textureDestination, textureSource, updateToken = m_update.GetUpdateToken()]() mutable {
bgfx::Encoder* encoder = updateToken.GetEncoder();
GetBoundFrameBuffer(*encoder).Blit(*encoder, textureDestination->Handle(), 0, 0, textureSource->Handle());
}).then(arcana::inline_scheduler, m_cancellationSource, [this](const arcana::expected<void, std::exception_ptr>& result)
{
}).then(arcana::inline_scheduler, m_cancellationSource, [this](const arcana::expected<void, std::exception_ptr>& result) {
if (result.has_error())
{
Napi::Error::New(Env(), result.error()).ThrowAsJavaScriptException();
Expand Down
89 changes: 44 additions & 45 deletions Plugins/NativeXr/Source/NativeXr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -696,47 +696,47 @@ namespace Babylon
bgfx::overrideInternal(colorTexture, reinterpret_cast<uintptr_t>(viewConfig.ColorTexturePointer));
bgfx::overrideInternal(depthTexture, reinterpret_cast<uintptr_t>(viewConfig.DepthTexturePointer));
}).then(m_runtimeScheduler.Get(), m_sessionState->CancellationSource, [this, thisRef{shared_from_this()}, colorTexture, depthTexture, requiresAppClear, &viewConfig]() {
const auto eyeCount = std::max(static_cast<uint16_t>(1), static_cast<uint16_t>(viewConfig.ViewTextureSize.Depth));
// TODO (rgerd): Remove old framebuffers from resource table?
viewConfig.FrameBuffers.resize(eyeCount);
for (uint16_t eyeIdx = 0; eyeIdx < eyeCount; eyeIdx++)
{
std::array<bgfx::Attachment, 2> attachments{};
attachments[0].init(colorTexture, bgfx::Access::Write, eyeIdx);
attachments[1].init(depthTexture, bgfx::Access::Write, eyeIdx);

auto frameBufferHandle = bgfx::createFrameBuffer(static_cast<uint8_t>(attachments.size()), attachments.data(), false);

const auto frameBufferPtr = new Graphics::FrameBuffer(
m_sessionState->GraphicsContext,
frameBufferHandle,
static_cast<uint16_t>(viewConfig.ViewTextureSize.Width),
static_cast<uint16_t>(viewConfig.ViewTextureSize.Height),
true,
true,
true);

auto& frameBuffer = *frameBufferPtr;

// WebXR, at least in its current implementation, specifies an implicit default clear to black.
// https://immersive-web.github.io/webxr/#xrwebgllayer-interface
frameBuffer.Clear(*m_sessionState->Update.GetUpdateToken().GetEncoder(), BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH | BGFX_CLEAR_STENCIL, 0, 1.0f, 0);

viewConfig.FrameBuffers[eyeIdx] = frameBufferPtr;

auto jsWidth{Napi::Value::From(m_env, viewConfig.ViewTextureSize.Width)};
auto jsHeight{Napi::Value::From(m_env, viewConfig.ViewTextureSize.Height)};
auto jsFrameBuffer{Napi::Pointer<Graphics::FrameBuffer>::Create(m_env, frameBufferPtr, Napi::NapiPointerDeleter(frameBufferPtr))};
viewConfig.JsTextures[frameBufferPtr] = Napi::Persistent(m_sessionState->CreateRenderTexture.Call({jsWidth, jsHeight, jsFrameBuffer}).As<Napi::Object>());
// OpenXR doesn't pre-clear textures, and so we need to make sure the render target gets cleared before rendering the scene.
// ARCore and ARKit effectively pre-clear by pre-compositing the camera feed.
if (requiresAppClear)
{
viewConfig.JsTextures[frameBufferPtr].Set("skipInitialClear", false);
}
}
viewConfig.Initialized = true;
}).then(arcana::inline_scheduler, m_sessionState->CancellationSource, [env{m_env}](const arcana::expected<void, std::exception_ptr>& result) {
const auto eyeCount = std::max(static_cast<uint16_t>(1), static_cast<uint16_t>(viewConfig.ViewTextureSize.Depth));
// TODO (rgerd): Remove old framebuffers from resource table?
viewConfig.FrameBuffers.resize(eyeCount);
for (uint16_t eyeIdx = 0; eyeIdx < eyeCount; eyeIdx++)
{
std::array<bgfx::Attachment, 2> attachments{};
attachments[0].init(colorTexture, bgfx::Access::Write, eyeIdx);
attachments[1].init(depthTexture, bgfx::Access::Write, eyeIdx);

auto frameBufferHandle = bgfx::createFrameBuffer(static_cast<uint8_t>(attachments.size()), attachments.data(), false);

const auto frameBufferPtr = new Graphics::FrameBuffer(
m_sessionState->GraphicsContext,
frameBufferHandle,
static_cast<uint16_t>(viewConfig.ViewTextureSize.Width),
static_cast<uint16_t>(viewConfig.ViewTextureSize.Height),
true,
true,
true);

auto& frameBuffer = *frameBufferPtr;

// WebXR, at least in its current implementation, specifies an implicit default clear to black.
// https://immersive-web.github.io/webxr/#xrwebgllayer-interface
frameBuffer.Clear(*m_sessionState->Update.GetUpdateToken().GetEncoder(), BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH | BGFX_CLEAR_STENCIL, 0, 1.0f, 0);

viewConfig.FrameBuffers[eyeIdx] = frameBufferPtr;

auto jsWidth{Napi::Value::From(m_env, viewConfig.ViewTextureSize.Width)};
auto jsHeight{Napi::Value::From(m_env, viewConfig.ViewTextureSize.Height)};
auto jsFrameBuffer{Napi::Pointer<Graphics::FrameBuffer>::Create(m_env, frameBufferPtr, Napi::NapiPointerDeleter(frameBufferPtr))};
viewConfig.JsTextures[frameBufferPtr] = Napi::Persistent(m_sessionState->CreateRenderTexture.Call({jsWidth, jsHeight, jsFrameBuffer}).As<Napi::Object>());
// OpenXR doesn't pre-clear textures, and so we need to make sure the render target gets cleared before rendering the scene.
// ARCore and ARKit effectively pre-clear by pre-compositing the camera feed.
if (requiresAppClear)
{
viewConfig.JsTextures[frameBufferPtr].Set("skipInitialClear", false);
}
}
viewConfig.Initialized = true;
}).then(arcana::inline_scheduler, m_sessionState->CancellationSource, [env{m_env}](const arcana::expected<void, std::exception_ptr>& result) {
if (result.has_error())
{
Napi::Error::New(env, result.error()).ThrowAsJavaScriptException();
Expand Down Expand Up @@ -3427,10 +3427,9 @@ namespace Babylon

// Fire off the IsSessionSupported task.
xr::System::IsSessionSupportedAsync(sessionType)
.then(m_runtimeScheduler.Get(), arcana::cancellation::none(), [deferred, thisRef = Napi::Persistent(info.This())](bool result)
{
deferred.Resolve(Napi::Boolean::New(thisRef.Env(), result));
});
.then(m_runtimeScheduler.Get(), arcana::cancellation::none(), [deferred, thisRef = Napi::Persistent(info.This())](bool result) {
deferred.Resolve(Napi::Boolean::New(thisRef.Env(), result));
});

return deferred.Promise();
}
Expand Down
12 changes: 6 additions & 6 deletions Polyfills/Canvas/Source/Canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ namespace Babylon::Polyfills::Internal
Napi::Function func = DefineClass(
env,
JS_CONSTRUCTOR_NAME,
{StaticMethod("loadTTFAsync", &NativeCanvas::LoadTTFAsync),
{
StaticMethod("loadTTFAsync", &NativeCanvas::LoadTTFAsync),
InstanceAccessor("width", &NativeCanvas::GetWidth, &NativeCanvas::SetWidth),
InstanceAccessor("height", &NativeCanvas::GetHeight, &NativeCanvas::SetHeight),
InstanceMethod("getContext", &NativeCanvas::GetContext),
InstanceMethod("getCanvasTexture", &NativeCanvas::GetCanvasTexture),
InstanceMethod("dispose", &NativeCanvas::Dispose),
StaticMethod("parseColor", &NativeCanvas::ParseColor)});
StaticMethod("parseColor", &NativeCanvas::ParseColor),
});

JsRuntime::NativeObject::GetFromJavaScript(env).Set(JS_CONSTRUCTOR_NAME, func);
}
Expand Down Expand Up @@ -63,11 +65,9 @@ namespace Babylon::Polyfills::Internal

auto& deviceContext = Graphics::DeviceContext::GetFromJavaScript(info.Env());
auto update = deviceContext.GetUpdate("update");
arcana::make_task(update.Scheduler(), arcana::cancellation::none(), [fontName = std::move(fontName), fontData = std::move(fontBuffer), &runtime, deferred = std::move(deferred)]() mutable
{
arcana::make_task(update.Scheduler(), arcana::cancellation::none(), [fontName = std::move(fontName), fontData = std::move(fontBuffer), &runtime, deferred = std::move(deferred)]() mutable {
fontsInfos[fontName] = fontData;
runtime.Dispatch([deferred = std::move(deferred)](Napi::Env env)
{
runtime.Dispatch([deferred = std::move(deferred)](Napi::Env env) {
deferred.Resolve(env.Undefined());
});
});
Expand Down
2 changes: 1 addition & 1 deletion Polyfills/Canvas/Source/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ namespace Babylon::Polyfills::Internal
nvgFillPaint(m_nvg, imagePaint);
nvgFill(m_nvg);
SetDirty(info.This());
}
}
else if (info.Length() == 5)
{
const auto dx = info[1].As<Napi::Number>().Int32Value();
Expand Down
29 changes: 14 additions & 15 deletions Polyfills/XMLHttpRequest/Source/XMLHttpRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,21 +224,20 @@ namespace Babylon::Polyfills::Internal

m_request.SendAsync()
.then(m_runtimeScheduler.Get(), m_cancellationSource,
[this, thisRef = Napi::Persistent(info.This())](arcana::expected<void, std::exception_ptr> result)
{
if (result.has_error())
{
Napi::Error::New(thisRef.Env(), result.error()).ThrowAsJavaScriptException();
return;
}

SetReadyState(ReadyState::Done);
RaiseEvent(EventType::LoadEnd);

// Assume the XMLHttpRequest will only be used for a single request and clear the event handlers.
// Single use seems to be the standard pattern, and we need to release our strong refs to event handlers.
m_eventHandlerRefs.clear();
});
[this, thisRef = Napi::Persistent(info.This())](arcana::expected<void, std::exception_ptr> result) {
if (result.has_error())
{
Napi::Error::New(thisRef.Env(), result.error()).ThrowAsJavaScriptException();
return;
}

SetReadyState(ReadyState::Done);
RaiseEvent(EventType::LoadEnd);

// Assume the XMLHttpRequest will only be used for a single request and clear the event handlers.
// Single use seems to be the standard pattern, and we need to release our strong refs to event handlers.
m_eventHandlerRefs.clear();
});
}

void XMLHttpRequest::SetReadyState(ReadyState readyState)
Expand Down

0 comments on commit 423ed5e

Please sign in to comment.