为什么glGenTextures在OpenGL上下文Thread(Android)上返回零?

时间:2015-03-30 21:19:28

标签: java android multithreading opengl-es

我遇到问题,即GLES20.glGenTextures()为纹理名称返回零,即使在原始线程(即OpenGL上下文线程)上运行也是如此。

当我运行代码时

final Handler handlerForOriginalThread = new Handler(activity.getMainLooper());

final int[] textureHandleA = new int[1];
GLES20.glGenTextures(1, textureHandleA, 0);
Log.v(TAG, "textureHandle (A): " + textureHandleA[0]);

new Thread(new Runnable() 
{
    public void run() 
    {
        //[Prepare bitmap here]

        handlerForOriginalThread.post(new Runnable() 
        {
            public void run() 
            {
                final int[] textureHandle = new int[1];
                GLES20.glGenTextures(1, textureHandle, 0);
                Log.v(TAG, "textureHandle (B): " + textureHandle[0]);

                //[Load bitmap to texture here]
            }
        });
    }
}).start();

glGenTextures返回(A)的有效名称,(B)返回零。

为什么会这样?

这类似于glGenTextures returns zero in background thread,但在这种情况下,代码实际上是在OpenGL上下文线程上运行的。

2 个答案:

答案 0 :(得分:2)

我明白了。事实证明,"原始线程"与我活动的主要活跃者不一样。

long origThreadID = Thread.currentThread().getId();
Log.d(TAG, "origThreadID (A): " + origThreadID);
final Handler handlerForOriginalThread = new Handler(activity.getMainLooper());

new Thread(new Runnable() 
{
    public void run() 
    {

        handlerForOriginalThread.post(new Runnable() 
        {
            public void run() 
            {
                long origThreadID = Thread.currentThread().getId();
                Log.d(TAG, "origThreadID (B): " + origThreadID);
            }
        });
    }
}).start();

结果

origThreadID (A): 9467
origThreadID (B): 1

答案 1 :(得分:1)

I found out that it also returns 0 if either:

1) You call it before calling eglMakeCurrent() 2) You have a mismatch with the surface attributes and then the eglMakeCurrent seems to have worked but actually writes a

eglMakeCurrent:992 error 3009 (EGL_BAD_MATCH)

to the log