From 63901401cab9051012d85c4bf245bea23687dce0 Mon Sep 17 00:00:00 2001
From: Michael Fitzmayer <[EMAIL REDACTED]>
Date: Thu, 16 Apr 2026 21:35:10 +0200
Subject: [PATCH] [N-Gage] Micro-optimize main loop handler to improve
performance and input latency
- Change active object priority from EPriorityLow to EPriorityStandard
- Process all events in batch before SDL_AppIterate() to reduce input lag
- Remove redundant SDL_PumpEvents() call (already done by SDL_PollEvent)
- Move clean-up logic into ShutdownApp() helper function
---
src/main/ngage/SDL_sysmain_main.cpp | 138 +++++++++++++---------------
1 file changed, 64 insertions(+), 74 deletions(-)
diff --git a/src/main/ngage/SDL_sysmain_main.cpp b/src/main/ngage/SDL_sysmain_main.cpp
index fc3ce0582733c..7fe7a9bf31fad 100644
--- a/src/main/ngage/SDL_sysmain_main.cpp
+++ b/src/main/ngage/SDL_sysmain_main.cpp
@@ -35,12 +35,12 @@ extern void SDL_AppQuit(void *appstate, SDL_AppResult result);
#include <e32std.h>
#include <estlib.h>
-#include <stdlib.h>
#include <stdio.h>
+#include <stdlib.h>
-#include "SDL_sysmain_main.hpp"
#include "../../audio/ngage/SDL_ngageaudio.hpp"
#include "../../render/ngage/SDL_render_ngage_c.hpp"
+#include "SDL_sysmain_main.hpp"
CRenderer *gRenderer = 0;
@@ -56,69 +56,64 @@ GLDEF_C TInt E32Main()
char **envp_lvalue = envp;
CTrapCleanup *cleanup = CTrapCleanup::New();
- if (!cleanup)
- {
+ if (!cleanup) {
return KErrNoMemory;
}
TRAPD(err,
- {
- CActiveScheduler *scheduler = new (ELeave) CActiveScheduler();
- CleanupStack::PushL(scheduler);
- CActiveScheduler::Install(scheduler);
-
- TInt posixErr = SpawnPosixServerThread();
- if (posixErr != KErrNone)
- {
- SDL_Log("Error: Failed to spawn POSIX server thread: %d", posixErr);
- User::Leave(posixErr);
- }
+ {
+ CActiveScheduler *scheduler = new (ELeave) CActiveScheduler();
+ CleanupStack::PushL(scheduler);
+ CActiveScheduler::Install(scheduler);
- __crt0(argc, argv_lvalue, envp_lvalue);
+ TInt posixErr = SpawnPosixServerThread();
+ if (posixErr != KErrNone) {
+ SDL_Log("Error: Failed to spawn POSIX server thread: %d", posixErr);
+ User::Leave(posixErr);
+ }
- // Increase heap size.
- RHeap *newHeap = User::ChunkHeap(NULL, 7500000, 7500000, KMinHeapGrowBy);
- if (!newHeap)
- {
- SDL_Log("Error: Failed to create new heap");
- User::Leave(KErrNoMemory);
- }
- CleanupStack::PushL(newHeap);
+ __crt0(argc, argv_lvalue, envp_lvalue);
- RHeap *oldHeap = User::SwitchHeap(newHeap);
+ // Increase heap size.
+ RHeap *newHeap = User::ChunkHeap(NULL, 7500000, 7500000, KMinHeapGrowBy);
+ if (!newHeap) {
+ SDL_Log("Error: Failed to create new heap");
+ User::Leave(KErrNoMemory);
+ }
+ CleanupStack::PushL(newHeap);
- TInt targetLatency = 225;
- InitAudio(&targetLatency);
+ RHeap *oldHeap = User::SwitchHeap(newHeap);
- // Wait until audio is ready.
- while (!AudioIsReady())
- {
- User::After(100000); // 100ms.
- }
+ TInt targetLatency = 225;
+ InitAudio(&targetLatency);
+
+ // Wait until audio is ready.
+ while (!AudioIsReady()) {
+ User::After(100000); // 100ms.
+ }
- // Create and start the rendering backend.
- gRenderer = CRenderer::NewL();
- CleanupStack::PushL(gRenderer);
+ // Create and start the rendering backend.
+ gRenderer = CRenderer::NewL();
+ CleanupStack::PushL(gRenderer);
- // Create and start the SDL main runner.
- CSDLmain *mainApp = CSDLmain::NewL();
- CleanupStack::PushL(mainApp);
- mainApp->Start();
+ // Create and start the SDL main runner.
+ CSDLmain *mainApp = CSDLmain::NewL();
+ CleanupStack::PushL(mainApp);
+ mainApp->Start();
- // Start the active scheduler to handle events.
- CActiveScheduler::Start();
+ // Start the active scheduler to handle events.
+ CActiveScheduler::Start();
- CleanupStack::PopAndDestroy(gRenderer);
- CleanupStack::PopAndDestroy(mainApp);
+ CleanupStack::PopAndDestroy(gRenderer);
+ CleanupStack::PopAndDestroy(mainApp);
- User::SwitchHeap(oldHeap);
+ User::SwitchHeap(oldHeap);
- CleanupStack::PopAndDestroy(newHeap);
- CleanupStack::PopAndDestroy(scheduler);
- });
+ CleanupStack::PopAndDestroy(newHeap);
+ CleanupStack::PopAndDestroy(scheduler);
+ });
- if (err != KErrNone)
- {
+ if (err != KErrNone) {
SDL_Log("Error: %d", err);
}
@@ -134,7 +129,7 @@ CSDLmain *CSDLmain::NewL()
return self;
}
-CSDLmain::CSDLmain() : CActive(EPriorityLow) {}
+CSDLmain::CSDLmain() : CActive(EPriorityStandard) {}
void CSDLmain::ConstructL()
{
@@ -157,40 +152,35 @@ void CSDLmain::DoCancel() {}
static bool callbacks_initialized = false;
+static void ShutdownApp(SDL_AppResult result)
+{
+ DeinitAudio();
+ SDL_AppQuit(NULL, result);
+ SDL_Quit();
+ CActiveScheduler::Stop();
+}
+
void CSDLmain::RunL()
{
- if (callbacks_initialized)
- {
+ if (callbacks_initialized) {
SDL_Event event;
- iResult = SDL_AppIterate(NULL);
- if (iResult != SDL_APP_CONTINUE)
- {
- DeinitAudio();
- SDL_AppQuit(NULL, iResult);
- SDL_Quit();
- CActiveScheduler::Stop();
- return;
- }
-
- SDL_PumpEvents();
- if (SDL_PollEvent(&event))
- {
+ while (SDL_PollEvent(&event)) {
iResult = SDL_AppEvent(NULL, &event);
- if (iResult != SDL_APP_CONTINUE)
- {
- DeinitAudio();
- SDL_AppQuit(NULL, iResult);
- SDL_Quit();
- CActiveScheduler::Stop();
+ if (iResult != SDL_APP_CONTINUE) {
+ ShutdownApp(iResult);
return;
}
}
+ iResult = SDL_AppIterate(NULL);
+ if (iResult != SDL_APP_CONTINUE) {
+ ShutdownApp(iResult);
+ return;
+ }
+
Start();
- }
- else
- {
+ } else {
SDL_SetMainReady();
SDL_AppInit(NULL, 0, NULL);
callbacks_initialized = true;