android jni 用c++使用opengles和egl实现离屏渲染

最近在做视频有关的东西,用开源的ffmpeg解码视频,在通过opengl渲染出来,因为有使用到egl就试了试离屏渲染

 

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

 

egl用到的就是这四个

先初始化

 

 

EGLint configSpec[] = { EGL_RED_SIZE, 8,
                        EGL_GREEN_SIZE, 8,
                        EGL_BLUE_SIZE, 8,
                        EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_NONE };
EGLint surfaceAttr[] = {
        EGL_WIDTH, width,
        EGL_HEIGHT, height,
        EGL_NONE
};
eglDisp = eglGetDisplay(EGL_DEFAULT_DISPLAY);
EGLint eglMajVers, eglMinVers;
EGLint numConfigs;
eglInitialize(eglDisp, &eglMajVers, &eglMinVers);
eglChooseConfig(eglDisp, configSpec, &eglConf, 1, &numConfigs);
eglSurface = eglCreatePbufferSurface(eglDisp, eglConf, surfaceAttr);
const EGLint ctxAttr[] = {
        EGL_CONTEXT_CLIENT_VERSION, 2,
        EGL_NONE
};
eglCtx = eglCreateContext(eglDisp, eglConf,EGL_NO_CONTEXT, ctxAttr);

 

上边的width和height是模拟窗口的宽高 最好和图片的比例一致,不然转成图片会拉伸

 

eglMakeCurrent(eglDisp, eglSurface, eglSurface, eglCtx);

 

设置显示的窗口,前一个窗口是显示窗口,后一个是写入窗口,通过调用这个来切换窗口

然后可以用opengl 进行画图了

画完后调用一下

 

eglSwapBuffers(eglDisp, eglSurface);

 

把东西显示在窗口上

最后加个释放

 

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;

 

试了试双屏切换

 

glViewport(0, 0, w, h);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
eglSwapBuffers(eglDisp, eglSurface);

jclass jcl = env->GetObjectClass(ob);
jmethodID mid = env->GetMethodID(jcl, "getBitmap", "(II)V");
env->CallVoidMethod(ob, mid,w,h);

 

第一次是离屏渲染,渲染完成后调用java代码把图片显示出来

 

public void getBitmap(int width,int height){
    IntBuffer RGBABuffer = IntBuffer.allocate(width * height);
    RGBABuffer.position(0);
    GLES20.glReadPixels(0, 0, width, height,GLES20.GL_RGBA,GLES20.GL_UNSIGNED_BYTE,RGBABuffer);
    int[] pixls = RGBABuffer.array();
    for (int y = 0; y < height / 2; y++) {
        for (int x = 0; x < width; x++) {
            int pos1 = y * width + x;
            int pos2 = (height - 1 - y) * width + x;
            int tmp = pixls[pos1];
            pixls[pos1] = (pixls[pos2] & 0xFF00FF00) | ((pixls[pos2] >> 16) & 0xff) | ((pixls[pos2] << 16) & 0x00ff0000); // ABGR->ARGB
            pixls[pos2] = (tmp & 0xFF00FF00) | ((tmp >> 16) & 0xff) | ((tmp << 16) & 0x00ff0000);
        }
    }
    if (height % 2 == 1) { // 中间一行
        for (int x = 0; x < width; x++) {
            int pos = (height / 2 + 1) * width + x;
            pixls[pos] = (pixls[pos] & 0xFF00FF00) | ((pixls[pos] >> 16) & 0xff) | ((pixls[pos] << 16) & 0x00ff0000);
        }
    }
    final Bitmap modelBitmap = Bitmap.createBitmap(pixls, width, height, Bitmap.Config.ARGB_8888);
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            ImageView imageView = (ImageView) findViewById(R.id.iv_opengl);
            imageView.setImageBitmap(modelBitmap);
        }
    });
}

 

然后切换

 

eglMakeCurrent(eglDisp, eglWindow, eglSurface, eglCtx);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(left, top, viewWidth, viewHeight);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
eglSwapBuffers(eglDisp, eglWindow);

 

切换的时候要再先清屏在画,不然两个纹理会重叠

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(android,opengl)