如何将纹理从一个OpenGL上下文传输到另一个



背景:

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上下文基于OpenGL_2.0而另一个基于OpenGL_1.0时。*

我找到了一个使用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 

参考:http://software.intel.com/en-us/articles/using-opengl-es-to-accelerate-apps-with-legacy-2d-guishttps://groups.google.com/forum/#!主题/安卓平台/qZMe9hpWSMU

最新更新