Multithreading jogl sanity check please...

Hi All,

Cutting a long story short, I’m having a bit of a mare with jogl and threads at the moment and I’d like a sanity check please.

In brief, I have multiple threads which were making calls to SwingUtilities.invokeLater followed by some opengl calls to create and populate a display list. This was working fine when I had a java HCI and all my opengl was in C++. Now I have jogl in the equation (in the form of world wind) and it’s completly broken this mechanism for rendering from the C++. I’ve taken the C++ and world wind out of the equation now and added the code below to the display method of the jogl gears demo and it still won’t work. Basicaly, the list ID returned from gl.glGenLists(1); is always 0 if it’s called via invokeLater. The gl error comes back as invalid operation.

Any pointers in the right direction would be greatly appreciated please.

Thanks,
Jon.


    int listId = gl.glGenLists(1);
    
    if(listId == 0) {
        System.out.println("Pre invokeLater listId == 0 : Event Dispatch Thread ? " + SwingUtilities.isEventDispatchThread() + ", Threading.isOpenGLThread() ? " + Threading.isOpenGLThread()  + ", Threading.isSingleThreaded() ? " + Threading.isSingleThreaded() );
    } else {
        System.out.println("Pre invokeLater listId != 0 : " + listId + " : Event Dispatch Thread ? " + SwingUtilities.isEventDispatchThread() + ", Threading.isOpenGLThread() ? " + Threading.isOpenGLThread()  + ", Threading.isSingleThreaded() ? " + Threading.isSingleThreaded() );
    }
    
    gl.glDeleteLists(listId,1);

    // Try again with invokeLater
    //
    //Threading.invokeOnOpenGLThread
    SwingUtilities.invokeLater
            (new Runnable() {
        public void run() {
            
            int listId2 = gl.glGenLists(1);
            
            if(listId2 == 0) {
                System.out.println("invokeLater listId2 == 0 : Event Dispatch Thread ? " + SwingUtilities.isEventDispatchThread() + ", Threading.isOpenGLThread() ? " + Threading.isOpenGLThread()  + ", Threading.isSingleThreaded() ? " + Threading.isSingleThreaded() );
            } else {
                System.out.println("invokeLater listId2 != 0 : " + listId2 + " : Event Dispatch Thread ? " + SwingUtilities.isEventDispatchThread() + ", Threading.isOpenGLThread() ? " + Threading.isOpenGLThread()  + ", Threading.isSingleThreaded() ? " + Threading.isSingleThreaded() );
            }
            
            gl.glDeleteLists(listId2,1);
        }
    });

All calls to OpenGL must be done while one of your GLEventListener’s methods are active on the current thread’s stack. You may need to build some sort of queue of work to be done later in your display() callback. If you used the DebugGL (see the JOGL User’s Guide) then this error should probably have been caught for you. (If not, file a bug with the JOGL Issue Tracker.)

Hi Ken,

Thanks for the reply. I’ve tried the DebugGL configuration as per your suggestion in my modified gears demo. It has given me the following error after trying to call glGenLists via invokeLater…

Exception in thread “AWT-EventQueue-0” javax.media.opengl.GLException: No OpenGL context is current on this thread
at javax.media.opengl.DebugGL.checkContext(DebugGL.java:12723)

Could you confirm that this is the expected behaviour in this invokeLater situation ?

Thanks,
Jon.

Hi again,

I’ve found a solution (or possibly a workaround) for my problem and I’d like an opinion as to whether it’s a good idea or not.

In my invokeLater Run() I’m calling drawable.getContext().makeCurrent(); before trying to create a list and it’s solved the problem. I can create lists in the Runnable in my modified gears demo.

I’ve also got it working in my real World Wind Java / C++ application. Everything is as it was but before the C++ tries to create a list it makes a call to the java which simply calls myWorldWindCanvas.getContext().makeCurrent();.

As yet I haven’t seen any adverse effects but would be interested to know if I’m doing something I shouldn’t be.

Thanks,
Jon.

p.s The question in my previous post still stands I guess.

Please read the JOGL Users’ Guide in particular the section on multithreading and see whether it answers your questions.