obtaining context in different threads

Hi!

I made a class wchich represents some object to be drawn. As to draw faster, I put all drawing routines into display list. So, whenever object is created, apropriate handle is obtained form glNewList. When object is deleted, the resource should be released. So I implemented a finalize method, and call glDeleteLists. Unfortunately, finalize is called by Finalizer thread, which is not allowed to use GLDrawable. The result is the application crash. Is there any possibility to allow Finalizer to call GL routines?

  regards

Just don’t use the finalize method. A java application should not depending on it to be called, it should only be used if absolutly nessesary. Install some kind of registry for your objects and define a method “release” in a common interface. Than you can just call it on all objectsa in the registry from the display-method in your shutdown phase.

I have thought about the same problem. You can set the current thread for the GLDrawable:

glDrawable.setRenderingThread(Thread.currentThread());

…this however would result in some nasty synchronization issues, as the glDrawable might be accessed by another thread (i.e. the actual rendering thread).

I think that a better solution would be to introduce a ResourceFinalizer class, which would handle this scenario, e.g.:

ResourceFinalizer
{
// Adds the texture ID to a queue, waiting for de-allocation
public static void finalizeTexture(int textureID);
// De-allocates the next resource in the queue
public static void purgeNext();
// De-allocates all remaining resources in the queue
public static void purgeAll();
}

Then, this class would used both by the finalized resource (has the texture to be de-allocated) and the actual rendering thread (calls the purge methods, when appropriate; not necessarily every frame…). This way, you can spare yourself the trouble to synchronize the GL access.

I hope this description made any sense :slight_smile:

Cheers

What about setting current render thread? If I set the rendering thread to some other (in this case -> Finalizer), would it use glDrawable correctly, if current rendering thread is suspended? I read API but it looks a little bit dimnished to me.

Don’t use the finalize method for this purpose. Finalizable objects place much more pressure on the garbage collector and are significantly more expensive to allocate than non-finalizable objects.

Instead, keep track of the destroyed objects in your application and delete their display lists the next time your display() method is entered.

You must destroy any outstanding OpenGL resources before your canvas is removed from the screen otherwise the rendering context will have already been destroyed. There is no way to destroy OpenGL resources in the finalizer thread. Sorry.