C++编程-5:Android native层使用OpenGL离屏渲染上下文

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;

}

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值