Here’s what I do, but it may not be applicable to you because I
manage my own OpenGL rendering thread and do not use the
EDT. That is, my OpenGL rendering is separate from the EDT so that
my UI is not blocked by OpenGL calls.
I typically issue a Thread.sleep() before it is time to swap buffers,
in order to spend as little time as possible inside the swapBuffer()
call itself.
You will need to measure how much time your frame takes and
calculate the amount to sleep accordingly. For 60fps, each frame
is 16.667ms long. If your frame takes 5ms to render, then you can
sleep for 10ms and wake up just in time for the swap. You need
to give the system at least 1-2ms buffer due to timer inaccuracies,
so the amount to sleep needs to be clamped. On Windows, the
timer can be made go at 1KHz using this piece of code at the
start of your app:
/*
* Workaround to enable hi-res timer on Windows. See 6435126.
*
* ForceTimeHighResolution switch doesn't operate as intended
* http://bugs.sun.com/view_bug.do?bug_id=6435126
*
* Calling Thread.sleep with small argument affects system clock on windows
* http://bugs.sun.com/view_bug.do?bug_id=4500388
*/
private static class LongSleepingThread extends Thread
{
public LongSleepingThread()
{
super("HiRes Bugfix (Windows)");
setDaemon(true);
}
public void run()
{
while (true)
{
try { Thread.sleep(Integer.MAX_VALUE); }
catch (InterruptedException e) {}
}
}
}
.rex