不能调用多线程Android应用程序glGenTextures [英] Can't call glGenTextures on multithreaded android app

查看:862
本文介绍了不能调用多线程Android应用程序glGenTextures的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用的是Android NDK的OpenGLES Android应用程序,从Android的gljni例如,它可以发现的这里

I'm making an OpenGLES Android app using Android NDK, expanding from android's gljni example, which can be found here

这是使用GLSurfaceView。纹理从onSurfaceChanged()称为JNI函数初始化GLSurfaceView.Renderer的

It's using GLSurfaceView. Textures are initialized in a JNI function called from onSurfaceChanged() of GLSurfaceView.Renderer

当用户触摸屏幕时,应用程序需要更多的纹理。为了做到这一点,glGenTextures()被调用调用的onTouchEvent()JNI的功能。

When the user touches screen, the app needs more textures. In order to do so, glGenTextures() is called in a JNI function called in onTouchEvent().

的问题是,该线程ID(其gettid()返回)似乎完全任意的,并且不总是相同的线程ID具有OpenGL上下文

The problem is that the thread id (which gettid() returns) seems completely arbitrary and not always the same to the thread id that has the OpenGL context.

IT负载,并显示了纹理如果JNI调用函数在同一个线程,但如果它是另一个线程失败。因此,它的行事相当随意。

It loads and shows up textures if the JNI function is called in the same thread, but fails if it's on another thread. So it's acting quite randomly.

我可以这样做:

  • 在任何线程共享的OpenGL上下文,这样我可以调用glGenTextures()成功。

  • share the OpenGL context so that I can call glGenTextures() successfully on any thread.

做的onTouchEvent()被调用的只有一个线程具有OpenGL上下文

make onTouchEvent() to be called in only one thread that has the OpenGl Context

或任何东西,我可以把它的工作

or anything that I can make it working

感谢

推荐答案

这不是随意的行为,这是OpenGL的使用线程交互的方式。上下文是目前唯一的在只有一个线程,其他​​线程没有一个GL上下文,除非你专门创建一个上下文要与OpenGL使用每个线程。如果没有一个背景下,所有的GL调用失败。

It's not random behaviour, that's how OpenGL interacts with threads. A context is only current on only ONE thread, other threads don't have a GL context unless you specifically create a context for each thread you want to use with OpenGL. Without a context, all GL calls fail.

这篇关于不能调用多线程Android应用程序glGenTextures的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆