Native层-OpenGL ES-双缓冲离屏渲染

OpenGL ES相关的数据结构:

typedef struct {
	EGLint MajorVersion;
	EGLint MinorVersion;
	EGLDisplay Display;
	EGLConfig Config;
	EGLSurface TinySurface;
	EGLSurface MainSurface;
	EGLContext Context;
} ovrEgl;

1.初始化EGLDisplay Display

egl->Display = eglGetDisplay(EGL_DEFAULT_DISPLAY);

2.利用display对象获得opengl版本

eglInitialize(egl->Display, &egl->MajorVersion, &egl->MinorVersion);
3.获取display对象的配置

	const int MAX_CONFIGS = 1024;
	EGLConfig configs[MAX_CONFIGS];
	EGLint numConfigs = 0;
	if (eglGetConfigs(egl->Display, configs, MAX_CONFIGS, &numConfigs)
			== EGL_FALSE) {
		ALOGE("        eglGetConfigs() failed: %s",
				EglErrorString(eglGetError()));
		return;
	}
4.自定义配置条件

	const EGLint configAttribs[] = { EGL_BLUE_SIZE, 8, EGL_GREEN_SIZE, 8,
			EGL_RED_SIZE, 8, EGL_DEPTH_SIZE, 0, EGL_SAMPLES, 0, EGL_NONE };
5.获取符合自定义配置条件的配置
	egl->Config = 0;
	for (int i = 0; i < numConfigs; i++) {
		EGLint value = 0;
		//从configs中获取指定属性的值,这里是获取属性EGL_RENDERABLE_TYPE的值
		//Returns a bitmask indicating the types of supported client API contexts
		eglGetConfigAttrib(egl->Display, configs[i], EGL_RENDERABLE_TYPE,
				&value);
		//获取到了之后判断是否符合条件,如果不符合条件
		ALOGV("EGL_RENDERABLE_TYPE's value=%x,", value);
		if ((value & EGL_OPENGL_ES3_BIT_KHR) != EGL_OPENGL_ES3_BIT_KHR) {
			continue;
		}

		// The pbuffer config also needs to be compatible with normal window rendering
		// so it can share textures with the window context.
		eglGetConfigAttrib(egl->Display, configs[i], EGL_SURFACE_TYPE, &value);
		ALOGV("EGL_SURFACE_TYPE's value=%x,", value);
		if ((value & (EGL_WINDOW_BIT | EGL_PBUFFER_BIT))
				!= (EGL_WINDOW_BIT | EGL_PBUFFER_BIT)) {
			continue;
		}
		//判断属性是否与表configAttribs中定义的属性值一致,好像什么也没做啊
		int j = 0;
		ALOGV("i = %d", i);
		for (; configAttribs[j] != EGL_NONE; j += 2) {
			eglGetConfigAttrib(egl->Display, configs[i], configAttribs[j],
					&value);
			if (value != configAttribs[j + 1]) {
				break;
			}
		}
		//如果完全一致,那么将配置i赋给egl->Config
		if (configAttribs[j] == EGL_NONE) {
			egl->Config = configs[i];
			break;
		}
	}
	//如果没有找到Config,那么提示错误信息,并返回
	if (egl->Config == 0) {
		ALOGE("        eglChooseConfig() failed: %s",
				EglErrorString(eglGetError()));
		return;
	}
6.创建eglContext

//如果配置信息正确,那么创建一个eglContext
	EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 3, EGL_NONE };
	egl->Context = eglCreateContext(egl->Display, egl->Config,
			(shareEgl != NULL) ? shareEgl->Context : EGL_NO_CONTEXT,
			contextAttribs);
	//如果创建失败,则返回
	if (egl->Context == EGL_NO_CONTEXT) {
		ALOGE("        eglCreateContext() failed: %s",
				EglErrorString(eglGetError()));
		return;
	}

7.如果创建eglContext成功,则接着创建一个PbufferSurface

	const EGLint surfaceAttribs[] = { EGL_WIDTH, 16, EGL_HEIGHT, 16, EGL_NONE };
	egl->TinySurface = eglCreatePbufferSurface(egl->Display, egl->Config,
			surfaceAttribs);
	if (egl->TinySurface == EGL_NO_SURFACE) {
		ALOGE("        eglCreatePbufferSurface() failed: %s",
				EglErrorString(eglGetError()));
		eglDestroyContext(egl->Display, egl->Context);
		egl->Context = EGL_NO_CONTEXT;
		return;
	}

8.将EGL rendering context绑定到EGL surfaces

//eglMakeCurrent — attach an EGL rendering context to EGL surfaces
	if (eglMakeCurrent(egl->Display, egl->TinySurface, egl->TinySurface,
			egl->Context) == EGL_FALSE) {
		ALOGE("        eglMakeCurrent() failed: %s",
				EglErrorString(eglGetError()));
		eglDestroySurface(egl->Display, egl->TinySurface);
		eglDestroyContext(egl->Display, egl->Context);
		egl->Context = EGL_NO_CONTEXT;
		return;
	}

9.初始化MainSurface

if (app->NativeWindow != NULL && app->Egl.MainSurface == EGL_NO_SURFACE) {
		ALOGV("ovrEgl_CreateSurface");
		ovrEgl_CreateSurface(&app->Egl, app->NativeWindow);
	}

static void ovrEgl_CreateSurface(ovrEgl * egl, ANativeWindow * nativeWindow) {
	if (egl->MainSurface != EGL_NO_SURFACE) {
		return;
	}
	ALOGV(
			"        MainSurface = eglCreateWindowSurface( Display, Config, nativeWindow, attribs )");
	const EGLint surfaceAttribs[] = { EGL_NONE };
	egl->MainSurface = eglCreateWindowSurface(egl->Display, egl->Config,
			nativeWindow, surfaceAttribs);
	if (egl->MainSurface == EGL_NO_SURFACE) {
		ALOGE("        eglCreateWindowSurface() failed: %s",
				EglErrorString(eglGetError()));
		return;
	}
	ALOGV(
			"        eglMakeCurrent( display, MainSurface, MainSurface, Context )");
	if (eglMakeCurrent(egl->Display, egl->MainSurface, egl->MainSurface,
			egl->Context) == EGL_FALSE) {
		ALOGE("        eglMakeCurrent() failed: %s",
				EglErrorString(eglGetError()));
		return;
	}
}

注意,这里通过eglMakeCurrent函数将context由步骤8中的TinySurface绑定到了MainSurface上。

之后的渲染就渲染在了MainSurface之上。

10.构建渲染数据


省略,此部分由用户决定


11.将surface上的内容画出来

eglSwapBuffers(appState.Egl.Display, appState.Egl.MainSurface);

由文章:

http://blog.copper3d.org/OpenGL_ES_3.0/an_introduction_to_egl/creating_an_off-screen_rendering_area_egl_pbuffers.html

可知,上面是创建了一个On-Screen 渲染surface(MainSurface),一个Off-Screen 渲染surface(TinySurface)


这里我们关注到eglMakeCurrent的函数参数

EGLBoolean eglMakeCurrent(EGLDisplay display,
                          EGLSurface draw,
                          EGLSurface read,
                          EGLContext context)
------------------------------------------------------
display         specifies the EGL display connection
draw            specifies the EGL draw surface
read            specifies the EGL read surface
context         specifies the EGL rendering context to be attached to the
                surfaces

可以知道,每次调用eglMakeCurrent都可以重新指定用户读和写的Surface

因此,双缓冲的buffer切换,就可以利用这个来实现。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值