Android OpenGL ES 離屏渲染(offscreen render)


通常在Android上使用OpenGL ES,都是希望把渲染后的結果顯示在屏幕上,例如圖片處理、模型顯示等。這種情況下,只需要使用Android API中提供的GLSurfaceView類和Renderer類,在這兩個類提供的初始化、回調函數中設置/編寫相應的代碼即可。不過,如果不希望把渲染結果顯示在屏幕上,也就是所說的離屏渲染(offscreen render),這兩個類就幫不上忙了。在此介紹一下如何在Android系統上做OpenGL ES 的離屏渲染。

 

1.基礎知識

要想使用OpenGL ES,一般包括如下幾個步驟:

  (1)EGL初始化

  (2)OpenGL ES初始化

  (3)OpenGL ES設置選項&繪制

  (4)OpenGL ES資源釋放(可選)

  (5)EGL資源釋放

Android提供的GLSurfaceView和Renderer自動完成了(1)(5)兩個部分,這部分只需要開發者做一些簡單配置即可。另外(4)這一步是可選的,因為隨着EGL中上下文的銷毀,openGL ES用到的資源也跟着釋放了。因此只有(2)(3)是開發者必須做的。這大大簡化了開發過程,但是靈活性也有所降低,利用這兩個類是無法完成offscreen render的。要想完成offscreen render其實也很簡單,相信大家也都猜到了,只要我們把(1)~(5)都自己完成就可以了。后續部分的代碼大部分都是C/C++,少部分是Java。

 

2.EGL初始化

EGL的功能是將OpenGL ES API和設備當前的窗口系統粘合在一起,起到了溝通橋梁的作用。不同設備的窗口系統千變萬化,但是OpenGL ES提供的API卻是統一的,所以EGL需要協調當前設備的窗口系統和OpenGL ES。下面EGL初始化的代碼我是用C++寫的,然后通過jni調用。Android在Java層面上也提供了對應的Java接口函數。

static EGLConfig eglConf;
static EGLSurface eglSurface;
static EGLContext eglCtx;
static EGLDisplay eglDisp;

JNIEXPORT void JNICALL Java_com_handspeaker_offscreentest_MyGles_init
(JNIEnv*env,jobject obj)
{
    // EGL config attributes
    const EGLint confAttr[] =
    {
            EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,// very important!
            EGL_SURFACE_TYPE,EGL_PBUFFER_BIT,//EGL_WINDOW_BIT EGL_PBUFFER_BIT we will create a pixelbuffer surface
            EGL_RED_SIZE,   8,
            EGL_GREEN_SIZE, 8,
            EGL_BLUE_SIZE,  8,
            EGL_ALPHA_SIZE, 8,// if you need the alpha channel
            EGL_DEPTH_SIZE, 8,// if you need the depth buffer
            EGL_STENCIL_SIZE,8,
            EGL_NONE
    };
    // EGL context attributes
    const EGLint ctxAttr[] = {
            EGL_CONTEXT_CLIENT_VERSION, 2,// very important!
            EGL_NONE
    };
    // surface attributes
    // the surface size is set to the input frame size
    const EGLint surfaceAttr[] = {
             EGL_WIDTH,512,
             EGL_HEIGHT,512,
             EGL_NONE
    };
    EGLint eglMajVers, eglMinVers;
    EGLint numConfigs;

    eglDisp = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    if(eglDisp == EGL_NO_DISPLAY)
    {
        //Unable to open connection to local windowing system
        LOGI("Unable to open connection to local windowing system");
    }
    if(!eglInitialize(eglDisp, &eglMajVers, &eglMinVers))
    {
        // Unable to initialize EGL. Handle and recover
        LOGI("Unable to initialize EGL");
    }
    LOGI("EGL init with version %d.%d", eglMajVers, eglMinVers);
    // choose the first config, i.e. best config
    if(!eglChooseConfig(eglDisp, confAttr, &eglConf, 1, &numConfigs))
    {
        LOGI("some config is wrong");
    }
    else
    {
        LOGI("all configs is OK");
    }
    // create a pixelbuffer surface
    eglSurface = eglCreatePbufferSurface(eglDisp, eglConf, surfaceAttr);
    if(eglSurface == EGL_NO_SURFACE)
    {
        switch(eglGetError())
        {
        case EGL_BAD_ALLOC:
        // Not enough resources available. Handle and recover
            LOGI("Not enough resources available");
            break;
        case EGL_BAD_CONFIG:
        // Verify that provided EGLConfig is valid
            LOGI("provided EGLConfig is invalid");
            break;
        case EGL_BAD_PARAMETER:
        // Verify that the EGL_WIDTH and EGL_HEIGHT are
        // non-negative values
            LOGI("provided EGL_WIDTH and EGL_HEIGHT is invalid");
            break;
        case EGL_BAD_MATCH:
        // Check window and EGLConfig attributes to determine
        // compatibility and pbuffer-texture parameters
            LOGI("Check window and EGLConfig attributes");
            break;
        }
    }
    eglCtx = eglCreateContext(eglDisp, eglConf, EGL_NO_CONTEXT, ctxAttr);
    if(eglCtx == EGL_NO_CONTEXT)
    {
        EGLint error = eglGetError();
        if(error == EGL_BAD_CONFIG)
        {
            // Handle error and recover
            LOGI("EGL_BAD_CONFIG");
        }
    }
    if(!eglMakeCurrent(eglDisp, eglSurface, eglSurface, eglCtx))
    {
        LOGI("MakeCurrent failed");
    }
    LOGI("initialize success!");
}

代碼比較長,不過大部分都是檢測當前函數調用是否出錯的,核心的函數只有6個,只要它們的調用沒有問題即可:

eglGetDisplay(EGL_DEFAULT_DISPLAY)

eglInitialize(eglDisp, &eglMajVers, &eglMinVers)

eglChooseConfig(eglDisp, confAttr, &eglConf, 1, &numConfigs)

eglCreatePbufferSurface(eglDisp, eglConf, surfaceAttr)

eglCreateContext(eglDisp, eglConf, EGL_NO_CONTEXT, ctxAttr)

eglMakeCurrent(eglDisp, eglSurface, eglSurface, eglCtx)

這些函數中參數的具體定義可以在這個網站找到:  https://www.khronos.org/registry/egl/sdk/docs/man/

需要說明的是,eglChooseConfig(eglDisp, confAttr, &eglConf, 1, &numConfigs)中confAttr參數一定要有EGL_SURFACE_TYPE,EGL_PBUFFER_BIT這個配置,它決定了是渲染surface的類型,是屏幕還是內存。另外,還有一些選項和OpenGL ES版本有關,具體選用1.x還是2.x,這個視個人情況而定,我使用的是2.x。

 

3.OpenGL ES部分

為了方便說明,我把OpenGL ES部分都寫在一起了,代碼如下:

JNIEXPORT void JNICALL Java_com_handspeaker_offscreentest_MyGles_draw
(JNIEnv*env,jobject obj)
{
    const char*vertex_shader=vertex_shader_fix;
    const char*fragment_shader=fragment_shader_simple;
    glPixelStorei(GL_UNPACK_ALIGNMENT,1);
    glClearColor(0.0,0.0,0.0,0.0);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    glCullFace(GL_BACK);
    glViewport(0,0,512,512);
    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader,1,&vertex_shader,NULL);
    glCompileShader(vertexShader);
    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader,1,&fragment_shader,NULL);
    glCompileShader(fragmentShader);
    GLuint program = glCreateProgram();
    glAttachShader(program, vertexShader);
    glAttachShader(program, fragmentShader);
    glLinkProgram(program);
    glUseProgram(program);
    GLuint aPositionLocation =glGetAttribLocation(program, "a_Position");
    glVertexAttribPointer(aPositionLocation,2,GL_FLOAT,GL_FALSE,0,tableVerticesWithTriangles);
    glEnableVertexAttribArray(aPositionLocation);
    //draw something
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glDrawArrays(GL_TRIANGLES,0,6);
    eglSwapBuffers(eglDisp,eglSurface);
}

需要說明的是,繪制完成后,需要調用eglSwapBuffers(eglDisp,eglSurface)函數,因為在初始化EGL時默認設置的是雙緩沖模式,即一份緩沖用於繪制圖像,一份緩沖用於顯示圖像,每次顯示時需要交換兩份緩沖,把繪制好的圖像顯示出來。

上面openGL繪制需要的兩個shader在此不寫了,可供下載的demo里會有。

 

4.EGL資源釋放

最后還差一個函數,用於EGL資源釋放,代碼如下:

JNIEXPORT void JNICALL Java_com_handspeaker_offscreentest_MyGles_release
(JNIEnv*env,jobject obj)
{
    eglMakeCurrent(eglDisp, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
    eglDestroyContext(eglDisp, eglCtx);
    eglDestroySurface(eglDisp, eglSurface);
    eglTerminate(eglDisp);

    eglDisp = EGL_NO_DISPLAY;
    eglSurface = EGL_NO_SURFACE;
    eglCtx = EGL_NO_CONTEXT;
}

 

5.總結

大功告成,其實吃透了openGL ES的原理后,整個過程還是很簡單的。為了測試是否真的做到了offscreen render,我們把framebuffer中的圖片保存成圖片,來檢測結果。代碼如下:

        RGBABuffer = IntBuffer.allocate(512 * 512);
        MyGles.release();
        MyGles.init();
        MyGles.draw();
        RGBABuffer.position(0);
        GLES20.glReadPixels(0, 0, 512, 512,GLES20.GL_RGBA,GLES20.GL_UNSIGNED_BYTE,RGBABuffer);
        int[] modelData=RGBABuffer.array();
        int[] ArData=new int[modelData.length];
        int offset1, offset2;
        for (int i = 0; i < 512; i++) {
            offset1 = i * 512;
            offset2 = (512 - i - 1) * 512;
            for (int j = 0; j < 512; j++) {
                int texturePixel = modelData[offset1 + j];
                int blue = (texturePixel >> 16) & 0xff;
                int red = (texturePixel << 16) & 0x00ff0000;
                int pixel = (texturePixel & 0xff00ff00) | red | blue;
                ArData[offset2 + j] = pixel;
            }
        }
        Bitmap modelBitmap = Bitmap.createBitmap(ArData,512,512,Bitmap.Config.ARGB_8888);
        saveBitmap(modelBitmap);

要注意的是,因為openGL ES framebuffer和圖像通道的存儲順序不同,需要做一次ABGR到ARGB的轉換。

一般來說,offscreen render的用處主要是做GPU加速,如果你的算法是計算密集型的,可以通過一些方法將其轉化成位圖形式,作為紋理(texture)載入到GPU顯存中,再利用GPU的並行計算能力,對其進行處理,最后利用glReadPixels將計算結果讀取到內存中。就說這么多吧,更多的用法還需要大家的發掘。

 

這里是demo下載鏈接


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM