Android使用EGL初始化openGL2.0上下文

Android使用EGL初始化openGL2.0上下文,第1张

概述我想在本机代码上在 Android上进行屏幕外图像处理,因此我需要通过EGL在本机代码中创建openGL上下文. 通过EGL,我们可以创建EGLSurface,我可以看到有三种选择: * EGL_WINDOW_BIT * EGL_PIXMAP_BIT * EGL_BUFFER_BIT 第一个用于屏幕处理,第二个用于屏幕外,因此我使用EGL_PIXMAP_BIT,如下所示: // Step 1 - 我想在本机代码上在 Android上进行屏幕外图像处理,因此我需要通过EGL在本机代码中创建openGL上下文.

通过EGL,我们可以创建EGLSurface,我可以看到有三种选择:
* EGL_WINDOW_BIT
* EGL_pixmap_BIT
* EGL_BUFFER_BIT

第一个用于屏幕处理,第二个用于屏幕外,因此我使用EGL_pixmap_BIT,如下所示:

// Step 1 - Get the default display.EGLdisplay egldisplay = eglGetdisplay((EGLNativedisplayType) 0);if ((egldisplay = eglGetdisplay(EGL_DEFAulT_disPLAY)) == EGL_NO_disPLAY) {    LOGH("eglGetdisplay() returned error %d",eglGetError());    exit(-1);}// Step 2 - Initialize EGL.if (!eglinitialize(egldisplay,0)) {    LOGH("eglinitialize() returned error %d",eglGetError());    exit(-1);}// Step 3 - Make OpenGL ES the current API.eglBindAPI(EGL_OPENGL_ES_API);// Step 4 - Specify the required configuration attributes.EGlint pi32ConfigAttribs[] = { EGL_SURFACE_TYPE,EGL_pixmap_BIT,EGL_BLUE_SIZE,8,EGL_GREEN_SIZE,EGL_RED_SIZE,EGL_NONE,EGL_BIND_TO_TEXTURE_RGBA,EGL_TRUE };// Step 5 - Find a config that matches all requirements.int iConfigs;EGLConfig eglConfig;eglChooseConfig(egldisplay,pi32ConfigAttribs,&eglConfig,1,&iConfigs);if (iConfigs != 1) {    LOGH(            "Error: eglChooseConfig(): config not found %d - %d.\n",eglGetError(),iConfigs);    exit(-1);}// Step 6 - Create a surface to draw to.EGLSurface eglSurface = eglCreatePbufferSurface(egldisplay,eglConfig,NulL);// Step 7 - Create a context.EGLContext eglContext = eglCreateContext(egldisplay,NulL,ai32ContextAttribs);// Step 8 - Bind the context to the current threadeglMakeCurrent(egldisplay,eglSurface,eglContext);

代码在第5步失败了,似乎AndroID不支持屏幕外处理?不支持EGL_pixmap_BIT类型.

解决方法 您正在尝试使用仅在AndroID上无法运行的EGL选项,而pbuffers仅适用于某些GPU – 而不是NvIDia Tegra. pi32ConfigAttribs []看起来应该是这样的,无论它是在屏幕上还是在屏幕外:
EGlint pi32ConfigAttribs[] = {    EGL_RENDERABLE_TYPE,EGL_OPENGL_ES2_BIT,EGL_Alpha_SIZE,EGL_DEPTH_SIZE,EGL_STENCIL_SIZE,EGL_NONE};

AndroID在支持屏幕外EGLSurfaces方面非常缺乏灵活性.他们定义了自己的名为EGL_NATIVE_BUFFER_ANDROID的pixmap类型.

要在AndroID上创建一个离屏的EGL表面,请构造一个SurfaceTexture并将其传递给eglCreatewindowsurface().您还应该查看使用EGL图像扩展和EGL_NATIVE_BUFFER_ANDROID,如下所述:

http://software.intel.com/en-us/articles/using-opengl-es-to-accelerate-apps-with-legacy-2d-guis

http://software.intel.com/en-us/articles/porting-opengl-games-to-android-on-intel-atom-processors-part-1

更新:
以下是一些创建屏幕外表面的示例代码:

private EGL10 mEgl;private EGLConfig[] maEGLconfigs;private EGLdisplay mEgldisplay = null;private EGLContext mEglContext = null;private EGLSurface mEglSurface = null;private EGLSurface[] maEglSurfaces = new EGLSurface[MAX_SURFACES];@OverrIDepublic voID onSurfaceTextureAvailable(SurfaceTexture surfaceTexture,int wIDth,int height) {    InitializeEGL();    CreateSurfaceEGL(surfaceTexture,wIDth,height);}private voID InitializeEGL(){    mEgl = (EGL10)EGLContext.getEGL();    mEgldisplay = mEgl.eglGetdisplay(EGL10.EGL_DEFAulT_disPLAY);    if (mEgldisplay == EGL10.EGL_NO_disPLAY)        throw new RuntimeException("Error: eglGetdisplay() Failed " + glutils.getEGLErrorString(mEgl.eglGetError()));    int[] version = new int[2];    if (!mEgl.eglinitialize(mEgldisplay,version))        throw new RuntimeException("Error: eglinitialize() Failed " + glutils.getEGLErrorString(mEgl.eglGetError()));    maEGLconfigs = new EGLConfig[1];    int[] configsCount = new int[1];    int[] configSpec = new int[]    {        EGL10.EGL_RENDERABLE_TYPE,EGL10.EGL_RED_SIZE,EGL10.EGL_GREEN_SIZE,EGL10.EGL_BLUE_SIZE,EGL10.EGL_Alpha_SIZE,EGL10.EGL_DEPTH_SIZE,EGL10.EGL_STENCIL_SIZE,EGL10.EGL_NONE    };    if ((!mEgl.eglChooseConfig(mEgldisplay,configSpec,maEGLconfigs,configsCount)) || (configsCount[0] == 0))        throw new IllegalArgumentException("Error: eglChooseConfig() Failed " + glutils.getEGLErrorString(mEgl.eglGetError()));    if (maEGLconfigs[0] == null)        throw new RuntimeException("Error: eglConfig() not Initialized");    int[] attrib_List = { EGL_CONTEXT_CLIENT_VERSION,2,EGL10.EGL_NONE };    mEglContext = mEgl.eglCreateContext(mEgldisplay,maEGLconfigs[0],EGL10.EGL_NO_CONTEXT,attrib_List);  }private voID CreateSurfaceEGL(SurfaceTexture surfaceTexture,int height){    surfaceTexture.setDefaultBufferSize(wIDth,height);    mEglSurface = mEgl.eglCreatewindowsurface(mEgldisplay,surfaceTexture,null);    if (mEglSurface == null || mEglSurface == EGL10.EGL_NO_SURFACE)    {        int error = mEgl.eglGetError();        if (error == EGL10.EGL_BAD_NATIVE_WINDOW)        {            Log.e(LOG_TAG,"Error: createwindowsurface() Returned EGL_BAD_NATIVE_WINDOW.");            return;        }        throw new RuntimeException("Error: createwindowsurface() Failed " + glutils.getEGLErrorString(error));    }    if (!mEgl.eglMakeCurrent(mEgldisplay,mEglSurface,mEglContext))        throw new RuntimeException("Error: eglMakeCurrent() Failed " + glutils.getEGLErrorString(mEgl.eglGetError()));    int[] wIDthResult = new int[1];    int[] heightResult = new int[1];    mEgl.eglquerySurface(mEgldisplay,EGL10.EGL_WIDTH,wIDthResult);    mEgl.eglquerySurface(mEgldisplay,EGL10.EGL_HEIGHT,heightResult);    Log.i(LOG_TAG,"EGL Surface Dimensions:" + wIDthResult[0] + " " + heightResult[0]);}private voID DeleteSurfaceEGL(EGLSurface eglSurface){    if (eglSurface != EGL10.EGL_NO_SURFACE)        mEgl.eglDestroySurface(mEgldisplay,eglSurface);}
总结

以上是内存溢出为你收集整理的Android使用EGL初始化openGL2.0上下文全部内容,希望文章能够帮你解决Android使用EGL初始化openGL2.0上下文所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1134416.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存