eglMakeCurrent() is success, but the context show no surface is bound

1.7k views Asked by At

My platform is Android embedded system (4.4), for some purpose, I use C level opengl to draw something (not use the java opengl lib) and I don't need to let the output picture to show on monitor so I use the Pbuffer for off-screen render

When I use the eglMakeCurrent() to bind the surface and context, it return Success. But at this moment I use the

eglQueryContext(g_eglDisplay,context,EGL_RENDER_BUFFER,&value);
printf("EGL_RENDER_BUFFER[%X]\n",value); 

to query surface bind with the context, it show EGL_NONE

what's wrong ? or it can't use opengl C lib directly in Android Environmemt?

my source code and Android.mk is

drawline.cpp

#include <stdio.h>
#include <stdlib.h>
#include <GLES/gl.h>
#include <EGL/egl.h>

#define WIN_W 512
#define WIN_H 512 

EGLDisplay g_eglDisplay = 0;
EGLConfig *g_eglConfig = NULL;
EGLContext g_eglContext = 0;
EGLSurface g_eglSurface = 0;


static const EGLint g_configAttribs[] ={
                                      EGL_RED_SIZE,             8,
                                      EGL_GREEN_SIZE,           8,
                                      EGL_BLUE_SIZE,            8,
                                      EGL_ALPHA_SIZE,                   8,
                                      EGL_DEPTH_SIZE,                   16,
                                      EGL_SURFACE_TYPE,         EGL_PBUFFER_BIT,
                                      EGL_RENDERABLE_TYPE,      EGL_OPENGL_ES_BIT,
                                      EGL_NONE
                                   };


EGLint g_PBufAttribs[] = {
            EGL_WIDTH,                  WIN_W,
            EGL_HEIGHT,                 WIN_H,
            //EGL_LARGEST_PBUFFER,  EGL_TRUE,
            EGL_NONE
            };
// indicate which version of OPENGL 
EGLint g_textAttribs[] = {
                EGL_CONTEXT_CLIENT_VERSION, 1,
                EGL_NONE        
                };

int printEGLConfigAttribs(EGLConfig);
int printEGLSurfaceAttribs(EGLSurface);
int printEGLContextAttribs(EGLContext);

int main(){
    GLint a;

    printf("hello word! 3\n");

    g_eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);

    if (g_eglDisplay == EGL_NO_DISPLAY)
    {
        printf("Unable to initialise EGL display.");
        return 0;
    }

    EGLint major,minor;
    // Initialise egl, 2&3 para is null because I don't care the EGL version

    if (!eglInitialize(g_eglDisplay, &major, &minor))
    {
        printf("Unable to initialise EGL display.");
        return 0;
    }
    printf("eglInitialize  major[%d] minor[%d]\n",major,minor);

    if( !eglBindAPI(EGL_OPENGL_ES_API)){
        printf("[%s][%d] eglBindAPI  err! eglGetError():    
                     %d\n",__FUNCTION__,__LINE__,eglGetError());
        return 0;
    }

    printf("current rendering API : %d\n",eglQueryAPI());

    // Find a matching config
    EGLint numConfigsOut = 0;   
    //Get the config num for malloc suitable memory size
    if (eglChooseConfig(g_eglDisplay, g_configAttribs, NULL, 0, &numConfigsOut) != EGL_TRUE ||  
         numConfigsOut == 0)
    {
        //fprintf(stderr, "Unable to find appropriate EGL config.");
        printf("Unable to find appropriate EGL config. , eglGetError()= %d \n",eglGetError());
        return 0;
    }
    printf("numConfigsOut = %d\n",numConfigsOut);

    g_eglConfig = (EGLConfig*)malloc(numConfigsOut * sizeof(EGLConfig));
    if(g_eglConfig == (EGLConfig*)0){
        printf("Unable to malloc config list space. , eglGetError()= %d \n",eglGetError());
    }

    //get the config list
    if (eglChooseConfig(g_eglDisplay, g_configAttribs, g_eglConfig, numConfigsOut, &numConfigsOut) != EGL_TRUE || numConfigsOut == 0)
    {
        //fprintf(stderr, "Unable to find appropriate EGL config.");
        printf("Unable to get appropriate EGL config. , eglGetError()= %d \n",eglGetError());
        return 0;
    }

    // choose one config 
    EGLConfig eglconfig_tmp = *(g_eglConfig);

    printEGLConfigAttribs(eglconfig_tmp);


    g_eglSurface = eglCreatePbufferSurface(g_eglDisplay ,eglconfig_tmp ,g_PBufAttribs);

    if(g_eglSurface == EGL_NO_SURFACE){
        printf("Create Surface Fail! \n");
        return 0;
    }

    printEGLSurfaceAttribs(g_eglSurface);

    g_eglContext = eglCreateContext(g_eglDisplay ,eglconfig_tmp ,EGL_NO_CONTEXT ,g_textAttribs);

    if(g_eglContext == EGL_NO_CONTEXT){
        printf("Create eglContex Fail! \n");
        return 0;
    }

    printEGLContextAttribs(g_eglContext);

    //attach an EGL rendering context to EGL surfaces
    if(!eglMakeCurrent(g_eglDisplay ,g_eglSurface ,g_eglSurface ,g_eglContext)){
        printf("eglMakeCurrent Fail! \n");
        return 0;
    }

    return 0;
}

the Andorid.mk

LOCAL_PATH:= $(call my-dir)

###############################################################################
# drawline
###############################################################################
include $(CLEAR_VARS)

LOCAL_MODULE_TAGS:= optional

# Require higher permission
#LOCAL_CERTIFICATE := platform

# All of the source files that we will compile.
LOCAL_SRC_FILES:= \
drawline.cpp

# All of the shared libraries we link against.
LOCAL_SHARED_LIBRARIES := \
    libGLESv1_CM \
    libEGL \

# This is the target being built.
LOCAL_MODULE := drawline

include $(BUILD_EXECUTABLE)

#include $(call all-makefiles-under,$(LOCAL_PATH))
0

There are 0 answers