Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
924 views
in Technique[技术] by (71.8m points)

multithreading - Using OpenGL from the main thread on Android

I would like to call a GLES20 method when an item from the options menu is selected.

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.clear:
            GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
            break;
        // ...
    }
}

This does not work since I am in the main thread and not in GLThread. It says:

call to OpenGL ES API with no current context (logged once per thread)

But what do I have to do to get things working?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I found the answer on my own:

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.clear:
            // GLSurfaceView.queueEvent
            surface.queueEvent(new Runnable() {
                @Override
                public void run() {
                    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
                }
            });
            break;
        // ...
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...