2014-02-27 24 views
5

背景:如何從一個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-context-process中的紋理複製圖像像素,然後將它們設置回GL-context-display中的另一個紋理。但是從紋理複製像素非常慢,通常需要幾百毫秒。相機預覽太慢了。

* 是否有更好的方法將紋理從一個GL上下文轉移到另一個上下文?尤其是,當一個GL上下文基於OpenGL_2.0而另一個基於OpenGL_1.0時。*

回答

7

我找到了一個使用EGLImage的解決方案。萬一有人發現了它有用:

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); 

線程#2顯示的3D場景:

// 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-guis https://groups.google.com/forum/#!topic/android-platform/qZMe9hpWSMU

加載紋理線程#1

相關問題