0
votes

There are a few discussions about async loading textures using Assets in LibGDX. As far as I know LibGDX uses 2 methods (async & sync) to load data. In case if it's rely on using OpenGL functions, it uses main thread with GL context and does it synchronously, otherwise asynchronously.

I need to load and process textures asynchronously. Loading is done quite quickly. Unfortunately, I can't say the same about process. The process is some deformation of texture which requires a lot of time. For this reason, I tried to separate threads like below:

 public void load(final String filename){
    if(callback!=null && !isLoading){   
    //WE NEED TO USE THIS FUNC AS DESCRIBED IN LIBGDX MANUAL        
        Gdx.app.postRunnable(new Runnable() {               
            @Override
            public void run() {                 
                //SIMULATE HEAVY PROCESS                                                
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {                      
                        e.printStackTrace();
                    }

                    if(callback!=null){
                        callback.onComplete(filename);                          
                    }                       
            }
        });
 }

//JAVA CLASS HAS THE SAME BEHAVIOUR
 new Thread(new Runnable() {                
            @Override
            public void run() {
                //SIMULATE HEAVY PROCESS                                                
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {                      
                        e.printStackTrace();
                    }

                    if(callback!=null){
                        callback.onComplete(filename);                          
                    }                   
            }
        }).run();

This thread stops main thread for 10 sec. WHY? So, I can suppose that this thread executes inside main thread. What's the point? Is it possible to separate threads in LibGDX? How to share GL context between 2 threads? By the way, I found example, but it's not appropriate for LibGDX.

Also there is interesting class here which says:

/*
To handle an event you will typically subclass GLSurfaceView and override the
appropriate method, just as you would with any other View. However, when handling
the event, you may need to communicate with the Renderer object
that's running in the rendering thread. You can do this using any
standard Java cross-thread communication mechanism. In addition,
one relatively easy way to communicate with your renderer is
to call
For example:
*/
class MyGLSurfaceView extends GLSurfaceView {
private MyRenderer mMyRenderer;

public void start() {
  mMyRenderer = ...;
  setRenderer(mMyRenderer);
}

public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
queueEvent(new Runnable() {
// This method will be called on the rendering
// thread:
public void run() {
  mMyRenderer.handleDpadCenter();
}});
return true;
}
 return super.onKeyDown(keyCode, event);
}
}
1
What are you trying to achieve here? Is it maybe more logical to just load the image data into memory (not video memory) process it and then afterwards create a texture out of it? - p.streef
Have you tried to use a AsyncTask instead? This should not block the UI thread. - Araw
Neither should his second implementation. Gdx.app.postRunnable() adds a runnable to the main thread, but adding a runnable to a new thread does not interfere with the main thread. - p.streef

1 Answers

4
votes
  1. You cannot share GL context between threads.

  2. Gdx.app.postRunnable() does not start a new thread it just adds the Runnable to the main thread which will then be handled within the next frame. Thats why your main thread stops.

More: https://github.com/libgdx/libgdx/wiki/Threading