我的应用程序包含两个事件:一个是带有典型 Android UI 小部件的正常事件,另一个是具有基于 JNI 的 OpenGL ES View 的事件。
该应用会切换两个事件,因此我认为我必须在退出事件之前优雅地释放 OpenGL 资源。 (通过调用 glDeleteProgram、glDeleteBuffers、glDeleteTextures ...)
我引用了 hello-gl2 示例,但只有 OpenGL 设置代码,没有 OpenGL 销毁/关闭代码。所以我不知道应该在哪里调用 native OpenGL 发布方法。
我尝试了以下两个位置,但收到错误消息:
E/libEGL(7224):在没有当前上下文的情况下调用 OpenGL ES API(每个线程记录一次)
class MyGLView extends GLSurfaceView
{
...
private static class ContextFactory implements GLSurfaceView.EGLContextFactory {
public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig eglConfig) { ... }
public void destroyContext(EGL10 egl, EGLDisplay display, EGLContext context) {
// call native shutdown method, location #1
}
}
public void surfaceDestroyed(SurfaceHolder holder)
{
// call native shutdown method, location #2
}
...
public class Renderer implements GLSurfaceView.Renderer
{
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
// call native initialization method here: Works fine!
}
// no surface destroy callback method in GLSurfaceView.Renderer
}
}
我在哪里可以优雅地释放 OpenGL 资源? 或者在 native 部分设置当前 OpenGL 上下文的方法是什么?
最佳答案
真的没有必要。当 GLSurfaceView 被销毁时,它将释放其 EGL 上下文,这将释放与其关联的所有资源。
关于opengl-es - Android,使用 NDK 优雅地关闭 OpenGL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12171714/