保留通过本地代码创建的egl上下文

保留通过本地代码创建的egl上下文

本文介绍了保留通过本地代码创建的egl上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将游戏移植到android并决定使用NativeActivity而不是Java活动和JNI调用(我不回避JNI,尽管纯粹在c中设置回调和opengl上下文创建/销毁会更方便/c ++).

I am porting my game to android and decided to go with NativeActivity instead of Java activity and JNI calls (I am not avoiding JNI, just though it would be more convenient to set up callbacks and opengl context creation/destruction purely in c/c++).

我知道GLSurfaceView具有setPreserveEGLContextOnPause函数,但这是在Java中,而不是在本机应用程序中.我使用以下代码创建上下文:

I know that GLSurfaceView has a setPreserveEGLContextOnPause function, but that is in Java, not in native app. I create my context with the following code:

EGLConfig config;
EGLSurface surface;
EGLContext context;

EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);

eglInitialize(display, 0, 0);

eglChooseConfig(display, attribs, &config, 1, &numConfigs);

eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
ANativeWindow_setBuffersGeometry(engine->app->window, 0, 0, format);

surface = eglCreateWindowSurface(display, config, engine->app->window, NULL);

const EGLint contextAttribs[] = {
        EGL_CONTEXT_CLIENT_VERSION, 2,
        EGL_NONE
};

context = eglCreateContext(display, config, NULL, contextAttribs);

if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) {
    ERR("Unable to eglMakeCurrent");
    return -1;
}

我还知道setPreserveEGLContextOnPause不是100%可靠的,我应该检查是否手动销毁了东西,但是如果不是,则-我想跳过资产重装部分,以便更快地加载.

I also know that setPreserveEGLContextOnPause is not 100% reliable and I should check if stuff is destroyed manually, but if it's not - I'd like to skip the asset reloading part for the sake of faster loading.

基本上我想做的是使用setPreserveEGLContextOnPause(或者它等效于ndk world).是否有可能?是在Android的egl调用的幕后实例化了GLSurfaceView吗?

Basically what I want to do is to use setPreserveEGLContextOnPause (or it's equivalent for ndk world). Is it possible? Is GLSurfaceView being instantiated behind the curtains of android's egl calls?

推荐答案

GLSurfaceView是Java语言实用程序类,它位于SurfaceView和GLES之上.没有任何东西可以从EGL中创建或调用GLSurfaceView.

GLSurfaceView is a Java-language utility class that sits on top of SurfaceView and GLES. Nothing is creating or calling into GLSurfaceView from EGL.

存在GLSurfaceView中的保留EGL上下文"代码是因为GLSurfaceView在呈现线程上自行管理EGL上下文.想法是进行设置,以便应用程序要使用GLSurfaceView时不必对其进行处理.如果要进行自己的EGL管理,请不要使用GLSurfaceView.在用Java编写代码时,您应该使用SurfaceViewTextureView.

The "preserve EGL context" code in GLSurfaceView exists because GLSurfaceView does its own management of the EGL context on the render thread. The idea was to set things up so the app doesn't have to deal with it if it wants to use GLSurfaceView. If you want to do your own EGL management, don't use GLSurfaceView; when writing code in Java you'd use SurfaceView or TextureView instead.

您可以在 Grafika 中看到多个示例. Java语言的GLES实现是本机实现的一个瘦包装,因此Grafika中使用EGL的方式与您在本机代码中的使用方式非常相似.

You can see multiple examples in Grafika. The Java-language GLES implementation is a thin wrapper around the native implementation, so the way EGL is used in Grafika closely mirrors how you would use it in native code.

如果您自己管理EGL上下文,则在拆卸和重新创建活动时它不会消失,但是如果进程被杀死,它将消失,因此最好在活动onPause()/.在应用程序处于后台时继续保存上下文(及其关联的纹理和缓冲区)也是一种不好的形式.有关SurfaceView和Activity生命周期交互的信息,请参见本文表面. (如果您想了解Android图形架构的工作原理,请阅读本文的其余部分.)

If you manage the EGL context yourself, it will not go away when Activities are torn down and recreated, but it will go away if the process is killed, so it's best to create it on the activity onPause() / onResume(). It's also bad form to continue holding contexts (and their associated textures and buffers) while the app is in the background. See this article on SurfaceView and Activity lifecycle interaction for some notes about working with surfaces. (And read the rest of the article if you'd like to understand how the Android graphics architecture works.)

这篇关于保留通过本地代码创建的egl上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 16:42