Android中的EGL环境创建(native层)

#include <EGL/egl.h>
//EGL相关参数
typedef struct _ANDROID_EGL_CONTEXT_T 
{
    EGLConfig eglConf;
    EGLSurface eglSurface;
    EGLContext eglCtx;
    EGLDisplay eglDisp;
} ANDROID_EGL_CONTEXT_T;

ANDROID_EGL_CONTEXT_T ctx;//实例

//初始化EGL环境
static bool initEGLContext(ANDROID_EGL_CONTEXT_T &ctx, int image_width, int image_height) 
{
    EGLSurface &eglConf = ctx.eglConf;
    EGLSurface &eglSurface = ctx.eglSurface;
    EGLContext &eglCtx = ctx.eglCtx;
    EGLDisplay &eglDisp = ctx.eglDisp;

    // 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, image_width,
            EGL_HEIGHT, image_height,
            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
        printf("Unable to open connection to local windowing system\n");
        return false;
    }
    if (!eglInitialize(eglDisp, &eglMajVers, &eglMinVers))
     {
        // Unable to initialize EGL. Handle and recover
        printf("Unable to initialize EGL\n");
        return false;
    }
    printf("EGL init with version %d.%d", eglMajVers, eglMinVers);
    // choose the first config, i.e. best config
    if (!eglChooseConfig(eglDisp, confAttr, &eglConf, 1, &numConfigs)) 
    {
        printf("some config is wrong\n");
        return false;
    }
    //   else
    //   {
    //       LOGE("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
                printf("Not enough resources available");
                break;
            case EGL_BAD_CONFIG:
                // Verify that provided EGLConfig is valid
                printf("provided EGLConfig is invalid");
                break;
            case EGL_BAD_PARAMETER:
                // Verify that the EGL_WIDTH and EGL_HEIGHT are
                // non-negative values
                printf("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
                printf("Check window and EGLConfig attributes");
                break;
        }
        return false;
    }
    eglCtx = eglCreateContext(eglDisp, eglConf, EGL_NO_CONTEXT, ctxAttr);
    if (eglCtx == EGL_NO_CONTEXT)
     {
        EGLint glError = eglGetError();
        if (glError == EGL_BAD_CONFIG)
         {
            // Handle error and recover
            printf("EGL_BAD_CONFIG\n");
            return false;
        }
    }
    if (!eglMakeCurrent(eglDisp, eglSurface, eglSurface, eglCtx))
     {
        printf("MakeCurrent failed\n");
        return false;
    }
    //LOGE("EGL SUCCESS");
    return true;
}

//释放销毁EGL环境
static void releaseEGL(ANDROID_EGL_CONTEXT_T &ctx)
 {
    EGLSurface &eglSurface = ctx.eglSurface;
    EGLContext &eglCtx = ctx.eglCtx;
    EGLDisplay &eglDisp = ctx.eglDisp;

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

在CMake中target_link_libraries需要链接到EGL,如下:

target_link_libraries(render_util
        GLESv2
        EGL
        ${log-lib}
        jnigraphics
        )

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值