Is the JOGL TestContextSharing demo implemented correctly?

(I’m using JOGL 2.0b10)

In all the environments I ran this in, OpenGL rendering happens in the first frame only. The second one stays black. Looking at the code, this seems quite logical: When the line

GLCanvas canvas2 = new GLCanvas(null, null, canvas1.getContext(), null);

is executed, canvas1.getContext() is null, so no actual context sharing takes place. In fact, it seems very plausible that canvas1.getContext() must be null at that point, since this code runs before the AWT event loop is even started, and before canvas1’s GLEventListener#init() method runs. According to the documentation, said init method is called immediately after the GLContext has been (re)created, so this would be the earliest time where it would make sense to create canvas2 with canvas1’s context.

If I replace the GLCanvases with GLJPanels, the demo works, but I guess that is just a coincidence (display list ids “accidentally” being the same in both contexts).

The reason GLJPanels work is because they use a pbuffer internally. Since the pbuffer is offscreen, it’s context is available right away.

In my experiences with context sharing, the GLCanvas does need to be visible before it can be properly shared.

Hm, ok, but even with GLJPanels, panel1.getContext() returns null after panel1 was properly created as GLJPanel panel1 = new GLJPanel(). From that it looks like the context is not available right away. Do you think that, with GLJPanels, the context is shared between all of them implicitly?

Looking at the source for GLJPanel, I was slightly mistaken. Although it can use pbuffers, it turns out they’re not allocated until the first time the GLJPanel’s paintComponent() method is called. Also, it looks like the created pbuffers are only shared with the requested shareWith context for the GLJPanel.

The only explanation I have for why the GLJPanel sharing works is if the GLJPanel is using a special backend that works the Java2D-OpenGL bridge. In this case, the GLJPanel uses FBOs and I wouldn’t be surprised if every GLJpanel used an fbo that existed on a shared context that was created by Java2D. This conjecture since the code was complicated and I don’t know the inner workings of java2d.

HTH