int AndroidOpenGLEAGLContext::Init(AndroidOpenGLEAGLContextConf * conf)
{
EGLint config_attribs[] = {
EGL_BLUE_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_RED_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_RENDERABLE_TYPE,
EGL_OPENGL_ES2_BIT,
EGL_SURFACE_TYPE,
EGL_WINDOW_BIT,
EGL_NONE
};
EGLint width, height, format;
EGLint numConfigs;
EGLConfig config;
EGLSurface surface;
EGLContext context;
GLint majorVersion;
GLint minorVersion;
EGLBoolean success=EGL_FALSE;
EGLint error=0;
EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); //EGL_DEFAULT_DISPLAY
if(EGL_NO_DISPLAY != display)
{
success=eglInitialize(display, &majorVersion, &minorVersion);
if(success != EGL_TRUE)
{
error=eglGetError();
DP_MSG("error %0x",error);
return -1;
}
}
success=eglChooseConfig(display, config_attribs, &config, 1, &numConfigs);
DP_MSG("eglChooseConfig numConfigs=%d",numConfigs)
if(success != EGL_TRUE)
{
DP_MSG("------eglChooseConfig");
return -1;
}
DP_MSG("------eglCreateWindowSurface start");
ANativeWindow* aNativeWindow = conf->kNativeWindow;
DP_MSG("------eglCreateWindowSurface end");
//if(EGL_NO_SURFACE == surface)
//{
DP_MSG(" prepared------eglCreateWindowSurface");
surface = eglCreateWindowSurface(display, config, aNativeWindow, NULL);
if(EGL_NO_SURFACE == surface)
{
DP_MSG("------eglCreateWindowSurface");
return -1;
}
//error=eglGetError();
//DP_MSG("error code %0x",error);
//}
EGLint attrs[]= {
EGL_CONTEXT_CLIENT_VERSION,
2,
EGL_NONE
};
context = eglCreateContext(display, config, NULL, attrs);
success=eglMakeCurrent(display, surface, surface, context);
if (success == EGL_FALSE)
{
DP_MSG("------EGL-FALSE");
return -1;
}
eglQuerySurface(display, surface, EGL_WIDTH, &width);
eglQuerySurface(display, surface, EGL_HEIGHT, &height);
mDisplay = display;
mSurface = surface;
mContext = context;
mWidth = width;
mHeight = height;
mConfig = config;
return 0;
}
int AndroidOpenGLEAGLContext::Close()
{
DP_MSG("egl close")
if (mDisplay != EGL_NO_DISPLAY)
{
eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
if (mContext != EGL_NO_CONTEXT)
{
eglDestroyContext(mDisplay, mContext);
DP_MSG("eglDestroyContext")
}
if (mSurface != EGL_NO_SURFACE)
{
eglDestroySurface(mDisplay, mSurface);
DP_MSG("eglDestroySurface")
}
if (!eglTerminate(mDisplay))
{
}
}
mDisplay = EGL_NO_DISPLAY;
mContext = EGL_NO_CONTEXT;
mSurface = EGL_NO_SURFACE;
return 0;
}
int AndroidOpenGLEAGLContext::setCurrentContext(OpenGLEAGLContext *context)
{
bool success = false;
#if 1
if(context == nullptr){
return 0;
}
EGLContext lpCurrentGLContext = eglGetCurrentContext();
if(lpCurrentGLContext == nullptr)
{
EGLint attrs[]= {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
EGLContext otherContext = eglCreateContext(mDisplay, mConfig, mContext, attrs);
if(otherContext==EGL_NO_CONTEXT){
DP_MSG("eglCreateContext otherContext error %d",otherContext)
}else{
DP_MSG("eglCreateContext otherContext o k %d",otherContext)
}
EGLBoolean avx_success = eglMakeCurrent(mDisplay, nullptr, nullptr, otherContext);
if(avx_success == EGL_FALSE)
{
DP_MSG("eglMakeCurrent error")
}else{
DP_MSG("eglMakeCurrent o k")
}
}
return 0;
#else
if(context){
success=eglMakeCurrent(mDisplay, mSurface, mSurface, mContext);
DP_MSG("eglMakeCurrent 1 = %d ",success);
}else{
success=eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
DP_MSG("eglMakeCurrent 2 = %d",success);
}
if(success == false)
{
DP_MSG("eglMakeCurrent error");
}
#endif
}
OpenGLEAGLContext * AndroidOpenGLEAGLContext::getCurrentContext()
{
return this;
}
void AndroidOpenGLEAGLContext::setCurrentHALContext(EGLContext * value)
{
bool success=eglMakeCurrent(mConfig, mSurface, mSurface, mContext);
}
int AndroidOpenGLEAGLContext::renderbufferStorage(void * drawable)
{
return 0;
}
int AndroidOpenGLEAGLContext::beginRenderbuffer(void * drawable)
{
bool success= false;
success=eglMakeCurrent(mDisplay, mSurface, mSurface, mContext);
if(success == false){
OpenMetaPrintf::Printf("beginRenderbuffer| eglMakeCurrent is failed \n");
}
return success;
}
int AndroidOpenGLEAGLContext::presentRenderbuffer()
{
bool success = false;
success = eglSwapBuffers(mDisplay, mSurface);
if(!success){
OpenMetaPrintf::Printf("presentRenderbuffer| eglSwapBuffers is failed \n");
}
success = eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
if(success == false){
OpenMetaPrintf::Printf("presentRenderbuffer| eglMakeCurrent is failed \n");
}
return success;
}