如何在两个OpenGL上下文之间转移纹理

7

背景:

Android原生相机应用使用OpenGL_1.0上下文显示相机预览和图库图片。现在我想在原生相机预览上添加实时滤镜。

添加自己相机应用预览上的实时滤镜很简单 --- 只需使用OpenGL_2.0进行图像处理和显示。由于OpenGL_1.0不支持图像处理,而在Android原生相机应用中用于显示。* 我现在想基于OpenGL_2.0创建一个新的GL上下文进行图像处理并将处理后的图像传递给基于OpenGL_1.0的另一个GL上下文以供显示。

 

问题:

问题是如何从GL上下文处理(基于OpenGL_2.0)传输处理后的图像到GL上下文显示(基于OpenGL_1.0)。我尝试过使用FBO:首先从GL上下文处理的纹理中复制图像像素,然后将其设置回到GL上下文显示的另一个纹理中。但是从纹理复制像素非常缓慢,通常需要几百毫秒。对于相机预览来说太慢了。

* 是否有更好的方法将纹理从一个GL上下文传输到另一个GL上下文?特别是当一个GL上下文基于OpenGL_2.0而另一个GL上下文基于OpenGL_1.0时。

1个回答

11
我发现使用EGLImage可以解决问题。以防有人觉得有用:
线程1加载纹理:
EGLContext eglContext1 = eglCreateContext(eglDisplay, eglConfig, EGL_NO_CONTEXT, contextAttributes); 
EGLSurface eglSurface1 = eglCreatePbufferSurface(eglDisplay, eglConfig, NULL); // pbuffer surface is enough, we're not going to use it anyway 
eglMakeCurrent(eglDisplay, eglSurface1, eglSurface1, eglContext1); 
int textureId; // texture to be used on thread #2 
// ... OpenGL calls skipped: create and specify texture 
//(glGenTextures, glBindTexture, glTexImage2D, etc.) 
glBindTexture(GL_TEXTURE_2D, 0); 
EGLint imageAttributes[] = { 
    EGL_GL_TEXTURE_LEVEL_KHR, 0, // mip map level to reference 
    EGL_IMAGE_PRESERVED_KHR, EGL_FALSE, 
    EGL_NONE 
}; 
EGLImageKHR eglImage = eglCreateImageKHR(eglDisplay, eglContext1, EGL_GL_TEXTURE_2D_KHR, reinterpret_cast<EGLClientBuffer>(textureId), imageAttributes); 

显示3D场景的线程#2:

// it will use eglImage created on thread #1 so make sure it has access to it + proper synchronization etc. 
GLuint texture; 
glGenTextures(1, &texture); 
glBindTexture(GL_TEXTURE_2D, texture); 
// texture parameters are not stored in EGLImage so don't forget to specify them (especially when no additional mip map levels will be used) 
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, eglImage); 
// texture state is now like if you called glTexImage2D on it 

Reference: http://software.intel.com/en-us/articles/using-opengl-es-to-accelerate-apps-with-legacy-2d-guis https://groups.google.com/forum/#!topic/android-platform/qZMe9hpWSMU


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接