Android GLSurfaceView.Renderer is Interrupting an Incomplete Touch Event

2.5k views Asked by At

This is a problem I've been having for a while and I'm hoping someone here might be able to shed some light on it.

I have an Android game which loads a GLSurfaceView who's renderer is set up like this:

public class GameRenderer implements GLSurfaceView.Renderer
{
    public void onSurfaceCreated(GL10 gl, EGLConfig config)
    {
        BaseLib.init(); // Initializes the native code
    }

    public void onSurfaceChanged(GL10 gl, int width, int height)
    {}

    public void onDrawFrame(GL10 gl)
    {
        BaseLib.render(); // Call to native method to render/update the current frame
    }
}

The view is like this:

public class GameView extends GLSurfaceView implements SurfaceHolder.Callback
{
    private GameRenderer _renderer;
    private GameListener _listener;

    public GameView(Context context)
    {
        super(context);
        this._renderer = new GameRenderer();
        setRenderer(this._renderer);
        this._listener = new GameListener();
        BaseLib.setListener(this._listener);
    }

    public boolean onTouchEvent(final MotionEvent event)
    {
        int action = -1;
        switch(event.getAction())
        {
        case MotionEvent.ACTION_DOWN: action = 0; break;
        case MotionEvent.ACTION_UP: action = 1; break;
        case MotionEvent.ACTION_MOVE: action = 2; break;
        }
        if(action >= 0)
            BaseLib.touch(action, event.getX(), event.getY());

        return true;
    }
}

I've been tracing through the native code and I'm noticing a problem where midway through the touch event it appears another render() call is being made. Because the touch event hasn't finished it ends up producing some errors due to it trying to render objects that haven't completed loading yet.

This might be a problem in the native code itself but as I haven't been able to find the problem there so far I wanted to ask if it's possible the Java code might be interrupting the call to touch and calling another onDrawFrame() before it completes.

2

There are 2 answers

3
Romain Guy On BEST ANSWER

Touch events and rendering happen on two different threads. You have to properly synchronize your data read/write.

0
AudioBubble On

The solution here is that whenever you capture a touch event (which happens on the UI thread), you que that same event into the GLSurfaceView as a runnable and so it will execute in correct order on the GLSurfaceView's renderer thread. Example code:

 @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        if (event != null)
        {
            if (event.getAction() == MotionEvent.ACTION_DOWN)
            {
                if (mRenderer != null)
                {
                    // Ensure we call switchMode() on the OpenGL thread.
                    // queueEvent() is a method of GLSurfaceView that will do this for us.
                    queueEvent(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            mRenderer.switchMode();
                        }
                    });

                    return true;
                }
            }
        }

        return super.onTouchEvent(event);
    }

Tutorial with more information on the matter:

http://www.learnopengles.com/listening-to-android-touch-events-and-acting-on-them/