无法在多线程 android 应用程序上调用 glGenTextures [英] Can't call glGenTextures on multithreaded android app

查看:20
本文介绍了无法在多线程 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.纹理在 GLSurfaceView.Renderer 的 onSurfaceChanged() 调用的 JNI 函数中初始化

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

当用户触摸屏幕时,应用需要更多纹理.为此,在 onTouchEvent() 中调用的 JNI 函数中调用 glGenTextures().

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() 返回的)似乎完全是任意的,并且与具有 OpenGL 上下文的线程 id 并不总是相同.

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.

如果在同一个线程中调用 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天全站免登陆