防止 onPause 破坏 OpenGL 上下文 [英] Prevent onPause from trashing OpenGL Context

查看:19
本文介绍了防止 onPause 破坏 OpenGL 上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用 OpenGL ES(GLSurfaceView 和 GLSurfaceView.Renderer)的 Android 应用程序.问题是当用户切换应用程序然后切换回我的应用程序时,GLSurfaceView 会破坏并重新创建 GL 上下文.根据文档,这是应该做的,但有没有办法防止这种情况发生?

I'm writing an Android application that uses OpenGL ES (GLSurfaceView and GLSurfaceView.Renderer). The problem is that when the user switches applications and then switches back to my app, the GLSurfaceView destroys and recreates the GL context. This is what it's supposed to do according to the documentation but is there a way to prevent this from happening?

将纹理加载到上下文中需要很长时间,我想避免重新加载它们.

It takes a long time to load textures into the context and i'd like to prevent having to reload them.

推荐答案

我认为 GLSurfaceView 文档中讨论了您要查找的内容:

I think what you are looking for is discussed in the GLSurfaceView documentation:

必须通知 GLSurfaceView活动暂停并恢复.GLSurfaceView 客户端需要活动时调用 onPause()暂停和 onResume() 时活动恢复.这些调用允许GLSurfaceView 暂停和恢复渲染线程,并且还允许GLSurfaceView 发布和重新创建OpenGL 显示.

A GLSurfaceView must be notified when the activity is paused and resumed. GLSurfaceView clients are required to call onPause() when the activity pauses and onResume() when the activity resumes. These calls allow GLSurfaceView to pause and resume the rendering thread, and also allow GLSurfaceView to release and recreate the OpenGL display.

使用标准 Android SDK 时,您必须在 Activity 暂停/恢复(包括屏幕方向更改)时释放/重新创建上下文.不这样做会导致 GL 上下文在 Activity 加载回内存时被释放而不是恢复.请记住,我们处理的资源非常有限(尤其是在低规格设备上).所以简短的回答是:你不能在不破坏你的应用程序的情况下阻止它.

When using the standard Android SDK, you must release/recreate your context whenever the activity is paused/resumed (including screen orientation changes). Not doing so will cause the GL context to be release and not restored when the activity is loaded back into memory. Remember that we are dealing with very limited resources (particularly on low-spec devices). So the short answer is: you can't prevent it without breaking your app.

假设您使用的是标准的 Android/OpenGL 框架,您需要执行以下操作...

Assuming you are using the standard Android/OpenGL framework, you need to do the following...

在您的活动中,确保您具有以下被覆盖的方法:

In your activity, ensure you have the following overridden methods:

public void onPause() {
    myGlSurfaceView.onPause();
}

public void onResume() {
    myGlSurfaceView.onResume();
}

您在 GL 环境之外保存的任何内容仍需要手动保存和恢复(位图、游戏状态等),您需要使用静态字段或 SharedPreferences 等机制.

Anything you hold outside the GL environment will still need to be preserved and restored manually however (bitmaps, game state, etc), for these you'll need to use static fields or a mechanism like SharedPreferences.

Android 3.x 提供了一个函数来保留GL 上下文暂停,无需重新创建.但是,有几个警告:

Android 3.x provides a function to preserve the GL context on pause without needing to be recreated. However, there are several caveats:

  1. 大约无法使用 Android 3.x 功能.目前市场上 90% 的设备
  2. 这些设备还必须支持多个 EGL 上下文,目前尚不清楚市场上有多少设备支持这一点.

使用一些API反射来检查能力,也许可以在支持的设备上使用这个功能.但是,您仍然需要回退到为其余部分重新创建上下文.在我看来,在更多设备运行 Android 3 之前,最好推迟使用 setPreserveEGLContextOnPause 并专注于确保上下文重新创建方法得到充分测试.

Using some API reflection to check capabilities, it may be possible to make use of this function on supporting devices. However, you would still need to fall back to recreating the context for the rest. In my opinion, until more devices run Android 3 it would be better to hold off using setPreserveEGLContextOnPause and focus on ensuring the context recreation approach is sufficiently tested.

这篇关于防止 onPause 破坏 OpenGL 上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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