0

I'm trying to load images in new thread to reduce lags in UI thread:

public class MyGame extends Activity implements Game, Renderer {
    ...
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        super.onSurfaceCreated(gl, config);
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                if(firstTimeCreate) {
                    Settings.load(getFileIO());
                    // Load bitmaps and save to variable Assets.backgroundRegions
                    Assets.load(this);
                    firstTimeCreate = false;
                } else {
                    Assets.reload();
                }
            }
        });
        t.start();
    }
    ...
}

The problem is when I trying to draw images as soon as the images is loaded, I only get a white images, no error message. This is the method I use to render background (this method run in a loop)

public void renderBackgrounds() {
    if (Assets.backgroundRegions.size() > 0) {
        batcher.beginBatch(Assets.backgroundRegions.get(0).texture);
        batcher.drawSprite(
            Assets.backgroundRegions.get(0).position, 
            Assets.backgroundRegions.get(0)
        );
        batcher.endBatch();
    } // else { background is not loaded yet }
}

The weird thing is when I press Home button and open my app again, then background images are displayed all correctly. It's just like there are "white-images cached version" of all the loaded images, and Android just don't clear the cache until I re-create the Activity, maybe, I don't know.

If I remove the new thread implementation in onSurfaceCreated like this:

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        super.onSurfaceCreated(gl, config);
        if(firstTimeCreate) {
            Settings.load(getFileIO());
            Assets.load(this);
            firstTimeCreate = false;
        } else {
            Assets.reload();
        }
    }

...then everything works fine except the UI thread is extremely lag.

I have read some posts about loading Bitmaps in new thread (e.g. this and this), they load Bitmap and assign the variable directly to the View (ImageView), but in my case I save the Bitmap to variables and use OpenGL to render. I don't know if this is one of the reasons or not.

There are the possible reasons I could think about:

  • All loaded images are cached wrong (somehow, I don't know) and I only get white images until I re-create the Activity.
  • Something wrong with multithreading and the variable I have used to store bitmap data (backgroundRegions).
  • I did things in wrong way when use OpenGL and Multithreading together, I don't know much about OpenGL, the part with OpenGL is taken from a small framework.
Community
  • 1
  • 1
Tony Dinh
  • 6,668
  • 5
  • 39
  • 58
  • Did you render your image in onDrawFrame method? Did you render it continuously or when dirty? – James Zhao Feb 24 '14 at 06:21
  • I did render my image in onDrawFrame method, and render it continuously. Do you have any suggestion? – Tony Dinh Feb 24 '14 at 08:12
  • I think you could insert logs in onDrawFrame to check if the image is really loaded. – James Zhao Feb 24 '14 at 10:19
  • 1
    You also can use glGetError() to check if there exists errors as rendering. – James Zhao Feb 24 '14 at 10:36
  • 1
    If you are trying to load a texture in another thread then rest of your GL code you need to either load the image into memory in background but load the image data into texture on main GL thread (no GL related code on the new thread) or you need to create a new GL context on the newly created thread and you need to share the 2 contexts (look into openGL context sharing). – Matic Oblak Feb 25 '14 at 12:30

0 Answers0