Skip to content

LwjglCanvas backend improvement (LWJGLX + AWT)#2669

Open
JNightRider wants to merge 9 commits intojMonkeyEngine:masterfrom
JNightRider:fix/lwjglx-backend-improvement
Open

LwjglCanvas backend improvement (LWJGLX + AWT)#2669
JNightRider wants to merge 9 commits intojMonkeyEngine:masterfrom
JNightRider:fix/lwjglx-backend-improvement

Conversation

@JNightRider
Copy link
Copy Markdown
Contributor

@JNightRider JNightRider commented Mar 30, 2026

I have recently been working on the problem that LwjglCanvas is currently presenting:

  • Remove and add the canvas
  • The poor performance it has (low fps)

These errors have now been corrected; the problem when deleting the canvas was that the renderer was not invalidated when creating the new one.

The performance problem was due to the thread remaining idle for 10 milliseconds and glwf creating a context, which meant that two contexts were running.

Screenshots

Some screenshots, whether running natively or with AWT, show that JME3 looks exactly the same (the window on the left is from the native version, the one on the right is from the AWT version).

Linux (Wayland + XWayland)

Captura desde 2026-03-30 15-44-27

Windows

Captura de pantalla 2026-03-30 162914

While removing and re-adding the canvas works perfectly, using a LightProbe generated by FastLightProbeFactory.makeProbe(...) can cause program crashes, and the new shadow filter SdsmDirectionalLightShadowFilter might not work (when removing and re-adding the canvas). These filters must be removed before deleting the canvas from its main components and then re-adding it when inserting it.

Captura desde 2026-03-30 16-58-05

It is not currently compatible with OpenCL(), as the lwjgl3 module is tightly linked to glwf.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the LwjglCanvas implementation to improve OpenGL context management and lifecycle handling within AWT/Swing environments. Key changes include a new main loop in the run() method, the introduction of explicit lock/unlock and context creation methods, and logic to handle Wayland/X11 platform detection. Several methods and fields in LwjglWindow were changed to protected to facilitate this. The review feedback identifies several critical issues: the renderable flag is never reset after context recreation, which would stop the display; blocking the AWT Event Dispatch Thread (EDT) with lock.wait() poses a high risk of deadlock; and skipping the superclass call in createContext() bypasses glfwInit(), potentially breaking GLFW-dependent functionality. Other feedback points out potential NullPointerExceptions, redundant flag assignments, missing GLFW event polling, and the use of System.out.println instead of a logger.

Comment on lines +338 to +345
while (reinitcontext.get()) {
try {
lock.wait();
} catch (InterruptedException ex) {
super.removeNotify();
return;
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Blocking the AWT Event Dispatch Thread (EDT) with lock.wait() is risky. If the render thread (which calls lock.notifyAll() at line 515) gets blocked by any operation that requires the EDT (e.g., a synchronous AWT call within listener.loseFocus() or renderer.cleanup()), a deadlock will occur, freezing the entire application UI. Consider using a timeout or an asynchronous signaling mechanism to avoid blocking the EDT indefinitely.

}
}

reinitcontext.set(false);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This assignment is redundant. The reinitcontext flag is already set to false by the render thread using getAndSet(false) at line 503 before it notifies this thread.

@riccardobl
Copy link
Copy Markdown
Member

riccardobl commented Mar 31, 2026

Thanks! Feel free to go through gemini's suggestions or to close them if they are wrong.
Just one note: don't worry too much about opencl, we are likely going to drop it anyway at some point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants