无电流OpenGL上下文从活动返回时? [英] No current openGL context when returning from activity?

查看:120
本文介绍了无电流OpenGL上下文从活动返回时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写code,将允许用户通过其SD卡上的文件浏览,找到图像并使用OpenGL ES 2.0的加载它们。当我用了仅仅一个EditText中的文件路径键入这工作得很好,但现在我已经实现了一个文件浏览器,使得完全相同的调用与文件的路径,我得到的字符串呼叫OpenGL的API,而当前上下文在LogCat中。

我以为这已经是与装载机活动被过度顶部GLSurfaceView所以我成立了之前任何OpenGL调用进行了有史以来的活动终止,但没有骰子。

怎么办?

下面是一些code片段:

当用户点击了加载程序中一个名为

 公共无效BACKOUT(字符串文件路径){
        //通知文件的主要活动加载...
        意图I =新意图();
        i.putExtra(文件路径,文件路径);        的setResult(Activity.RESULT_OK,I);
        // ...并最终负载活动
        完();
    }

里面的主要活动,其中包含了GLSurfaceView

  @覆盖
公共无效的onActivityResult(INT申请code,INT结果code,意图数据){
  super.onActivityResult(要求code,结果code,数据);
  开关(要求code){
    情况1) : {
      如果(结果code == Activity.RESULT_OK){
      串toLoad = data.getStringExtra(文件路径);
      Log.v(TAG,toLoad);
      gl.informRendererLoadTexture(toLoad);
      }
      打破;
    }
  }
}

而在GLSurfaceView

 公共无效informRendererLoadTexture(字符串文件路径){
     _filePath =文件路径;
     queueEvent(新的Runnable(){
            公共无效的run(){
                _renderer.loadGLTexture(_filePath);
            }});


解决方案

从GLSurfaceView文档:

还有那里的EGL渲染上下文将丢失的情况。这种情况通常发生装置进入休眠状态。当EGL上下文丢失,与该上下文相关联的所有的OpenGL资源(如纹理)会醒来后,会发生被自动删除。为了保持正确的渲染,渲染器必须重新创建所有丢失资源仍然需要该onSurfaceCreated(GL10,EGLConfig)方法是一个方便的地方做到这一点。

您需要知道失去了OpenGL的上下文,然后重新获取上下文,并重新加载所有的OpenGL资源。它看起来就像当你显示全屏的文件浏览器上下文正在丧失。

您可以检查出副本岛样品code的检测和处理丢失上下​​文:的 HTTP://$c$c.google.com/p/replicaisland/

I am writing code that will allow the user to browse through the files on their SD card to locate images and them load them using openGL ES 2.0. This works fine when I had used just an EditText to type in the file path, but now that I have implemented a file browser that makes the exact same call with a String of the file path I get "Call to openGL API without a current context" in the LogCat.

I assumed this had something to do with the loader activity being over top the GLSurfaceView so I set up that activity terminates before any of the openGL calls were ever made, but no dice.

What gives?

Here are some code snippets:

Called when the user has clicked a file within the loader

    public void backOut(String filePath) {
        // inform the main Activity of the file to load...
        Intent i = new Intent();
        i.putExtra("filePath", filePath);

        setResult(Activity.RESULT_OK, i);
        // ... and end the load activity
        finish();
    }

Inside the main Activity, which holds the GLSurfaceView

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) {     
  super.onActivityResult(requestCode, resultCode, data); 
  switch(requestCode) { 
    case (1) : { 
      if (resultCode == Activity.RESULT_OK) { 
      String toLoad = data.getStringExtra("filePath");
      Log.v(TAG, toLoad);
      gl.informRendererLoadTexture(toLoad);
      } 
      break; 
    } 
  } 
}

And in the GLSurfaceView

 public void informRendererLoadTexture(String filePath){
     _filePath = filePath;
     queueEvent(new Runnable(){
            public void run() {
                _renderer.loadGLTexture(_filePath);
            }});

解决方案

From the GLSurfaceView documentation:

"There are situations where the EGL rendering context will be lost. This typically happens when device wakes up after going to sleep. When the EGL context is lost, all OpenGL resources (such as textures) that are associated with that context will be automatically deleted. In order to keep rendering correctly, a renderer must recreate any lost resources that it still needs. The onSurfaceCreated(GL10, EGLConfig) method is a convenient place to do this."

You need to be aware of lost OpenGL contexts, then reacquire the context and reload all OpenGL resources. It looks like your context is being lost when you display the full-screen file browser.

You can check out Replica Island's for sample code on detecting and handling lost contexts: http://code.google.com/p/replicaisland/

这篇关于无电流OpenGL上下文从活动返回时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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