Paste
Pasted as C++ by registered user yulf88 ( 9 years ago )
bool initEGL() {
LOGD("begin initEGL");
int samples = 0;
Properties *config = Game::getInstance()->getConfig()->getNamespace("window", true);
if (config) {
samples = std::max(config->getInt("samples"), 0);
}
LOGD("begin initEGL 1, samples %d", samples);
// Hard-coded to 32-bit/OpenGL ES 2.0.
// NOTE: EGL_SAMPLE_BUFFERS, EGL_SAMPLES and EGL_DEPTH_SIZE MUST remain at the beginning of the attribute list
// since they are expected to be at indices 0-5 in config fallback code later.
// EGL_DEPTH_SIZE is also expected to
EGLint eglConfigAttrs[] =
{
EGL_SAMPLE_BUFFERS, samples > 0 ? 1 : 0,
EGL_SAMPLES, samples,
EGL_DEPTH_SIZE, 24,
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_STENCIL_SIZE, 8,
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_NONE
};
__multiSampling = samples > 0;
EGLint eglConfigCount;
const EGLint eglContextAttrs[] =
{
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
const EGLint eglSurfaceAttrs[] =
{
EGL_RENDER_BUFFER, EGL_BACK_BUFFER,
EGL_NONE
};
LOGD("begin initEGL 2 __eglDisplay %d, %d ", (int) __eglDisplay, (int) __eglContext);
if (__eglDisplay == EGL_NO_DISPLAY && __eglContext == EGL_NO_CONTEXT) {
// Get the EGL display and initialize.
__eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (__eglDisplay == EGL_NO_DISPLAY) {
checkErrorEGL("eglGetDisplay");
goto error;
}
LOGD("after initEGL 2 __eglDisplay %d, %d ", (int) __eglDisplay, (int) __eglContext);
if (eglInitialize(__eglDisplay, NULL, NULL) != EGL_TRUE) {
checkErrorEGL("eglInitialize");
goto error;
}
// Try both 24 and 16-bit depth sizes since some hardware (i.e. Tegra) does not support 24-bit depth
bool validConfig = false;
EGLint depthSizes[] = {24, 16};
unsigned int i = 0;
for (i = 0; i < 2 xss=removed> 0 ? 1 : 0;
eglConfigAttrs[3] = samples;
eglConfigAttrs[5] = depthSizes[i];
GP_INFO("begin initEGL 4 ");
if (eglChooseConfig(__eglDisplay, eglConfigAttrs, &__eglConfig, 1,
&eglConfigCount) == EGL_TRUE && eglConfigCount > 0) {
validConfig = true;
GP_INFO("get validConfig eglConfigAttrs: %d %d %d", eglConfigAttrs[1],
eglConfigAttrs[3], eglConfigAttrs[5]);
break;
}
GP_INFO("begin initEGL 41 __eglDisplay %d, %d ", (int) __eglDisplay,
(int) __eglContext);
if (samples) {
// Try lowering the MSAA sample size until we find a config
int sampleCount = samples;
while (sampleCount) {
GP_WARN("No EGL config found for depth_size=%d and samples=%d. Trying samples=%d instead.",
depthSizes[i], sampleCount, sampleCount / 2);
sampleCount /= 2;
eglConfigAttrs[1] = sampleCount > 0 ? 1 : 0;
eglConfigAttrs[3] = sampleCount;
if (eglChooseConfig(__eglDisplay, eglConfigAttrs, &__eglConfig, 1,
&eglConfigCount) == EGL_TRUE && eglConfigCount > 0) {
validConfig = true;
break;
}
}
__multiSampling = sampleCount > 0;
if (validConfig)
break;
}
else {
GP_WARN("No EGL config found for depth_size=%d.", depthSizes[i]);
}
}
GP_INFO("end for i %d", i);
if (!validConfig) {
checkErrorEGL("eglChooseConfig");
goto error;
}
__eglContext = eglCreateContext(__eglDisplay, __eglConfig, EGL_NO_CONTEXT,
eglContextAttrs);
GP_INFO("eglCreateContext __eglDisplay %d, __eglContext %d, attr: %d %d %d",
(int) __eglDisplay,
(int) __eglContext, (int) eglContextAttrs[0], (int) eglContextAttrs[1],
(int) eglContextAttrs[2]);
if (__eglContext == EGL_NO_CONTEXT) {
checkErrorEGL("eglCreateContext");
goto error;
}
}
// EGL_NATIVE_VISUAL_ID is an attribute of the EGLConfig that is
// guaranteed to be accepted by ANativeWindow_setBuffersGeometry().
// As soon as we picked a EGLConfig, we can safely reconfigure the
// ANativeWindow buffers to match, using EGL_NATIVE_VISUAL_ID.
EGLint format;
GP_INFO("eglCreateContext __state2 %p __state2->window %p", __state2, __state2->window);
eglGetConfigAttrib(__eglDisplay, __eglConfig, EGL_NATIVE_VISUAL_ID, &format);
GP_INFO("eglCreateContext format %d(x)", format, format);
ANativeWindow_setBuffersGeometry((EGLNativeWindowType) __state2->window, 0, 0, format);
GP_INFO("eglCreateContext setBuffersGeometry NOT skip");
//__state2->window is created by ANativeWindow_fromSurface(env, surface)
__eglSurface = eglCreateWindowSurface(__eglDisplay, __eglConfig,
(EGLNativeWindowType) __state2->window,
eglSurfaceAttrs);
GP_INFO("eglCreateContext eglCreateWindowSurface");
if (__eglSurface == EGL_NO_SURFACE) {
checkErrorEGL("eglCreateWindowSurface");
goto error;
}
if (eglMakeCurrent(__eglDisplay, __eglSurface, __eglSurface, __eglContext) != EGL_TRUE) {
checkErrorEGL("eglMakeCurrent");
goto error;
}
GP_INFO("eglCreateContext eglMakeCurrent");
eglQuerySurface(__eglDisplay, __eglSurface, EGL_WIDTH, &__width);
eglQuerySurface(__eglDisplay, __eglSurface, EGL_HEIGHT, &__height);
GP_INFO("eglCreateContext eglQuerySurface %d %d", __width, __height);
__orientationAngle = getRotation() * 90;
// Set vsync.
eglSwapInterval(__eglDisplay, WINDOW_VSYNC ? 1 : 0);
GP_INFO("eglCreateContext eglSwapInterval");
// Initialize OpenGL ES extensions.
__glExtensions = (const char *) glGetString(GL_EXTENSIONS);
GP_INFO("eglCreateContext glGetString %s", __glExtensions);
if (strstr(__glExtensions, "GL_OES_vertex_array_object") ||
strstr(__glExtensions, "GL_ARB_vertex_array_object")) {
glBindVertexArray = (PFNGLBINDVERTEXARRAYOESPROC) eglGetProcAddress(
"glBindVertexArrayOES");
glDeleteVertexArrays = (PFNGLDELETEVERTEXARRAYSOESPROC) eglGetProcAddress(
"glDeleteVertexArraysOES");
glGenVertexArrays = (PFNGLGENVERTEXARRAYSOESPROC) eglGetProcAddress(
"glGenVertexArraysOES");
glIsVertexArray = (PFNGLISVERTEXARRAYOESPROC) eglGetProcAddress("glIsVertexArrayOES");
glMapBuffer = (PFNGLMAPBUFFEROESPROC) eglGetProcAddress("glMapBufferOES");
glUnmapBuffer = (PFNGLUNMAPBUFFEROESPROC) eglGetProcAddress("glUnmapBufferOES");
}
return true;
error:
return false;
}
Revise this Paste