// Initialized EGL resources. static bool initEGL() { // Hard-coded to 32-bit/OpenGL ES 2.0. const EGLint eglConfigAttrs[] = { EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8, EGL_DEPTH_SIZE, 24, EGL_STENCIL_SIZE, 8, EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_NONE }; EGLint eglConfigCount; const EGLint eglContextAttrs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; const EGLint eglSurfaceAttrs[] = { EGL_RENDER_BUFFER, EGL_BACK_BUFFER, EGL_NONE }; 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; } if (eglInitialize(__eglDisplay, NULL, NULL) != EGL_TRUE) { checkErrorEGL("eglInitialize"); goto error; } if (eglChooseConfig(__eglDisplay, eglConfigAttrs, &__eglConfig, 1, &eglConfigCount) != EGL_TRUE || eglConfigCount == 0) { checkErrorEGL("eglChooseConfig"); goto error; } __eglContext = eglCreateContext(__eglDisplay, __eglConfig, EGL_NO_CONTEXT, eglContextAttrs); 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; eglGetConfigAttrib(__eglDisplay, __eglConfig, EGL_NATIVE_VISUAL_ID, &format); ANativeWindow_setBuffersGeometry(__state->window, 0, 0, format); __eglSurface = eglCreateWindowSurface(__eglDisplay, __eglConfig, __state->window, eglSurfaceAttrs); if (__eglSurface == EGL_NO_SURFACE) { checkErrorEGL("eglCreateWindowSurface"); goto error; } if (eglMakeCurrent(__eglDisplay, __eglSurface, __eglSurface, __eglContext) != EGL_TRUE) { checkErrorEGL("eglMakeCurrent"); goto error; } eglQuerySurface(__eglDisplay, __eglSurface, EGL_WIDTH, &__width); eglQuerySurface(__eglDisplay, __eglSurface, EGL_HEIGHT, &__height); // Set vsync. eglSwapInterval(__eglDisplay, WINDOW_VSYNC ? 1 : 0); // Initialize OpenGL ES extensions. __glExtensions = (const char*)glGetString(GL_EXTENSIONS); if (strstr(__glExtensions, "GL_OES_vertex_array_object") || strstr(__glExtensions, "GL_ARB_vertex_array_object")) { // Disable VAO extension for now. glBindVertexArray = (PFNGLBINDVERTEXARRAYOESPROC)eglGetProcAddress("glBindVertexArrayOES"); glDeleteVertexArrays = (PFNGLDELETEVERTEXARRAYSOESPROC)eglGetProcAddress("glDeleteVertexArrays"); glGenVertexArrays = (PFNGLGENVERTEXARRAYSOESPROC)eglGetProcAddress("glGenVertexArraysOES"); glIsVertexArray = (PFNGLISVERTEXARRAYOESPROC)eglGetProcAddress("glIsVertexArrayOES"); } return true; error: return false; }
bool CCEGLView::initGL() { int rc = 0; int screenFormat = SCREEN_FORMAT_RGBA8888; #ifdef __X86__ int screenUsage = SCREEN_USAGE_OPENGL_ES2; #else int screenUsage = SCREEN_USAGE_DISPLAY|SCREEN_USAGE_OPENGL_ES2; // Physical device copy directly into physical display #endif // int screenSwapInterval = WINDOW_VSYNC ? 1 : 0; int screenTransparency = SCREEN_TRANSPARENCY_NONE; int angle = atoi(getenv("ORIENTATION")); // Hard-coded to (0,0). int windowPosition[] = { 0, 0 }; EGLint eglConfigCount; EGLConfig config; // Hard-coded to 32-bit/OpenGL ES 2.0. const EGLint eglConfigAttrs[] = { EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8, EGL_DEPTH_SIZE, 24, EGL_STENCIL_SIZE, 8, EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_NONE }; const EGLint eglContextAttrs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; const EGLint eglSurfaceAttrs[] = { EGL_RENDER_BUFFER, EGL_BACK_BUFFER, EGL_NONE }; // Create the screen context. rc = screen_create_context(&m_screenContext, 0); if (rc) { perror("screen_create_context"); return false; } // Create the screen window. rc = screen_create_window(&m_screenWindow, m_screenContext); if (rc) { perror("screen_create_window"); return false; } // Set/get any window prooperties. rc = screen_set_window_property_iv(m_screenWindow, SCREEN_PROPERTY_FORMAT, &screenFormat); if (rc) { perror("screen_set_window_property_iv(SCREEN_PROPERTY_FORMAT)"); return false; } rc = screen_set_window_property_iv(m_screenWindow, SCREEN_PROPERTY_USAGE, &screenUsage); if (rc) { perror("screen_set_window_property_iv(SCREEN_PROPERTY_USAGE)"); return false; } char *width_str = getenv("WIDTH"); char *height_str = getenv("HEIGHT"); if (width_str && height_str) { int screen_res[2]; screen_res[0] = atoi(width_str); screen_res[1] = atoi(height_str); int rc = screen_set_window_property_iv(m_screenWindow, SCREEN_PROPERTY_BUFFER_SIZE, screen_res); if (rc) { fprintf(stderr, "screen_set_window_property_iv(SCREEN_PROPERTY_BUFFER_SIZE)"); return false; } } else { screen_display_t screen_display; rc = screen_get_window_property_pv(m_screenWindow, SCREEN_PROPERTY_DISPLAY, (void **)&screen_display); if (rc) { perror("screen_get_window_property_pv(SCREEN_PROPERTY_DISPLAY)"); return false; } screen_display_mode_t screen_mode; rc = screen_get_display_property_pv(screen_display, SCREEN_PROPERTY_MODE, (void**)&screen_mode); if (rc) { perror("screen_get_display_property_pv(SCREEN_PROPERTY_MODE)"); return false; } int size[2]; rc = screen_get_window_property_iv(m_screenWindow, SCREEN_PROPERTY_BUFFER_SIZE, size); if (rc) { perror("screen_get_window_property_iv(SCREEN_PROPERTY_BUFFER_SIZE)"); return false; } int buffer_size[2] = { size[0], size[1] }; if ((angle == 0) || (angle == 180)) { if (((screen_mode.width > screen_mode.height) && (size[0] < size[1])) || ((screen_mode.width < screen_mode.height) && (size[0] > size[1]))) { buffer_size[1] = size[0]; buffer_size[0] = size[1]; } } else if ((angle == 90) || (angle == 270)) { if (((screen_mode.width > screen_mode.height) && (size[0] > size[1])) || ((screen_mode.width < screen_mode.height) && (size[0] < size[1]))) { buffer_size[1] = size[0]; buffer_size[0] = size[1]; } } else { perror("Navigator returned an unexpected orientation angle."); return false; } rc = screen_set_window_property_iv(m_screenWindow, SCREEN_PROPERTY_ROTATION, &angle); if (rc) { perror("screen_set_window_property_iv(SCREEN_PROPERTY_ROTATION)"); return false; } rc = screen_set_window_property_iv(m_screenWindow, SCREEN_PROPERTY_BUFFER_SIZE, buffer_size); if (rc) { perror("screen_set_window_property_iv(SCREEN_PROPERTY_BUFFER_SIZE)"); return false; } } if (windowPosition[0] != 0 || windowPosition[1] != 0) { rc = screen_set_window_property_iv(m_screenWindow, SCREEN_PROPERTY_POSITION, windowPosition); if (rc) { perror("screen_set_window_property_iv(SCREEN_PROPERTY_POSITION)"); return false; } } rc = screen_set_window_property_iv(m_screenWindow, SCREEN_PROPERTY_TRANSPARENCY, &screenTransparency); if (rc) { perror("screen_set_window_property_iv(SCREEN_PROPERTY_TRANSPARENCY)"); return false; } // Double buffered. rc = screen_create_window_buffers(m_screenWindow, 2); if (rc) { perror("screen_create_window_buffers"); return false; } // Create screen event object. rc = screen_create_event(&m_screenEvent); if (rc) { perror("screen_create_event"); return false; } // Request screen events. screen_request_events(m_screenContext); // Get the EGL display and initialize. m_eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); if (m_eglDisplay == EGL_NO_DISPLAY) { perror("eglGetDisplay"); return false; } if (eglInitialize(m_eglDisplay, NULL, NULL) != EGL_TRUE) { perror("eglInitialize"); return false; } if (eglChooseConfig(m_eglDisplay, eglConfigAttrs, &config, 1, &eglConfigCount) != EGL_TRUE || eglConfigCount == 0) { checkErrorEGL("eglChooseConfig"); return false; } m_eglContext = eglCreateContext(m_eglDisplay, config, EGL_NO_CONTEXT, eglContextAttrs); if (m_eglContext == EGL_NO_CONTEXT) { checkErrorEGL("eglCreateContext"); return false; } m_eglSurface = eglCreateWindowSurface(m_eglDisplay, config, m_screenWindow, eglSurfaceAttrs); if (m_eglSurface == EGL_NO_SURFACE) { checkErrorEGL("eglCreateWindowSurface"); return false; } if (eglMakeCurrent(m_eglDisplay, m_eglSurface, m_eglSurface, m_eglContext) != EGL_TRUE) { checkErrorEGL("eglMakeCurrent"); return false; } EGLint width, height; if ((m_eglDisplay == EGL_NO_DISPLAY) || (m_eglSurface == EGL_NO_SURFACE) ) return EXIT_FAILURE; eglQuerySurface(m_eglDisplay, m_eglSurface, EGL_WIDTH, &width); eglQuerySurface(m_eglDisplay, m_eglSurface, EGL_HEIGHT, &height); m_obScreenSize.width = width; m_obScreenSize.height = height; // Set vsync. // eglSwapInterval(m_eglDisplay, screenSwapInterval); return true; }
void GLESRender::CreateRenderDevice(TngWindow* mainwnd) { render__ = this; mainwnd__ = mainwnd; Log::GetLog()->Printf(Log::DBG_CHN,"pre create gles"); int samples = RenderConfig::GetInstance().multisampling_ ? RenderConfig::GetInstance().multisampling_ : 0; 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 }; 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"); return; } if (eglInitialize(__eglDisplay, NULL, NULL) != EGL_TRUE) { checkErrorEGL("eglInitialize"); return; } else Log::GetLog()->Printf(Log::DBG_CHN,"egl Initialized"); // 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 }; for (unsigned int i = 0; i < 2; ++i) { eglConfigAttrs[1] = samples > 0 ? 1 : 0; eglConfigAttrs[3] = samples; eglConfigAttrs[5] = depthSizes[i]; /* Here, the application chooses the configuration it desires. */ if (eglChooseConfig(__eglDisplay, eglConfigAttrs, &__eglConfig, 1, &eglConfigCount) == EGL_TRUE && eglConfigCount > 0) { validConfig = true; break; } if (samples) { // Try lowering the MSAA sample size until we find a supported config int sampleCount = samples; while (sampleCount) { Log::GetLog()->Printf(Log::WARNING_CHN,"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 { Log::GetLog()->Printf(Log::WARNING_CHN,"No EGL config found for depth_size=%d.", depthSizes[i]); } } if (!validConfig) { checkErrorEGL("eglChooseConfig"); return; }else Log::GetLog()->Printf(Log::DBG_CHN,"eglChooseConfig"); __eglContext = eglCreateContext(__eglDisplay, __eglConfig, EGL_NO_CONTEXT, eglContextAttrs); if (__eglContext == EGL_NO_CONTEXT) { checkErrorEGL("eglCreateContext"); return; } else Log::GetLog()->Printf(Log::DBG_CHN,"eglCreateContext"); } // 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; eglGetConfigAttrib(__eglDisplay, __eglConfig, EGL_NATIVE_VISUAL_ID, &format); Log::GetLog()->Printf(Log::DBG_CHN,"state addr=%d.",OS::state__); Log::GetLog()->Printf(Log::DBG_CHN,"wnd addr=%d.",OS::state__->window); ANativeWindow_setBuffersGeometry(OS::state__->window, 0, 0, format); __eglSurface = eglCreateWindowSurface(__eglDisplay, __eglConfig, OS::state__->window, eglSurfaceAttrs); if (__eglSurface == EGL_NO_SURFACE) { checkErrorEGL("eglCreateWindowSurface"); return; } else Log::GetLog()->Printf(Log::DBG_CHN,"eglCreateWindowSurface"); if (eglMakeCurrent(__eglDisplay, __eglSurface, __eglSurface, __eglContext) != EGL_TRUE) { checkErrorEGL("eglMakeCurrent"); return; } else Log::GetLog()->Printf(Log::DBG_CHN,"eglMakeCurrent"); EGLint szx = RenderConfig::GetInstance().back_buffer_sizex_; EGLint szy = RenderConfig::GetInstance().back_buffer_sizey_; eglQuerySurface(__eglDisplay, __eglSurface, EGL_WIDTH, &szx); eglQuerySurface(__eglDisplay, __eglSurface, EGL_HEIGHT, &szy); Log::GetLog()->Printf(Log::SYS_CHN, "target device width(%d),heigh(%d)",szx,szy); mainwnd->width_ = szx; mainwnd->height_ = szy; // Set vsync. eglSwapInterval(__eglDisplay, 1); // Initialize OpenGL ES extensions. _loadExtentions(); if (_checkExtention("GL_OES_vertex_array_object") || _checkExtention("GL_ARB_vertex_array_object")) { // Disable VAO extension for now. glBindVertexArray = (PFNGLBINDVERTEXARRAYOESPROC)eglGetProcAddress("glBindVertexArrayOES"); glDeleteVertexArrays = (PFNGLDELETEVERTEXARRAYSOESPROC)eglGetProcAddress("glDeleteVertexArrays"); glGenVertexArrays = (PFNGLGENVERTEXARRAYSOESPROC)eglGetProcAddress("glGenVertexArraysOES"); glIsVertexArray = (PFNGLISVERTEXARRAYOESPROC)eglGetProcAddress("glIsVertexArrayOES"); if (glBindVertexArray) { Log::GetLog()->Printf(Log::SYS_CHN,"using vao"); } } Log::GetLog()->Printf(Log::SYS_CHN, "gles device created"); OS::state__->onAppCmd =engine_handle_cmd; OS::state__->onInputEvent =engine_handle_input; }
bool CCEGLView::initGL() { EGLint eglConfigCount; EGLConfig config; // Hard-coded to 32-bit/OpenGL ES 2.0. const EGLint eglConfigAttrs[] = { EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8, EGL_DEPTH_SIZE, 24, EGL_STENCIL_SIZE, 8, EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_NONE }; const EGLint eglContextAttrs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; const EGLint eglSurfaceAttrs[] = { EGL_RENDER_BUFFER, EGL_BACK_BUFFER, EGL_NONE }; // Get the EGL display and initialize. m_eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); if (m_eglDisplay == EGL_NO_DISPLAY) { perror("eglGetDisplay"); return false; } if (eglInitialize(m_eglDisplay, NULL, NULL) != EGL_TRUE) { perror("eglInitialize"); return false; } if (eglChooseConfig(m_eglDisplay, eglConfigAttrs, &config, 1, &eglConfigCount) != EGL_TRUE || eglConfigCount == 0) { checkErrorEGL("eglChooseConfig"); return false; } m_eglContext = eglCreateContext(m_eglDisplay, config, EGL_NO_CONTEXT, eglContextAttrs); if (m_eglContext == EGL_NO_CONTEXT) { checkErrorEGL("eglCreateContext"); return false; } m_eglSurface = eglCreateWindowSurface(m_eglDisplay, config, NULL, eglSurfaceAttrs); if (m_eglSurface == EGL_NO_SURFACE) { checkErrorEGL("eglCreateWindowSurface"); return false; } if (eglMakeCurrent(m_eglDisplay, m_eglSurface, m_eglSurface, m_eglContext) != EGL_TRUE) { checkErrorEGL("eglMakeCurrent"); return false; } // FIXME: Get the actual canvas size somehow. EGLint width; EGLint height; if ((m_eglDisplay == EGL_NO_DISPLAY) || (m_eglSurface == EGL_NO_SURFACE) ) return EXIT_FAILURE; eglQuerySurface(m_eglDisplay, m_eglSurface, EGL_WIDTH, &width); eglQuerySurface(m_eglDisplay, m_eglSurface, EGL_HEIGHT, &height); m_obScreenSize.width = width; m_obScreenSize.height = height; glViewport(0, 0, width, height); // Default the frame size to be the whole canvas. In general we want to be // setting the size of the viewport by adjusting the canvas size (so // there's no weird letterboxing). setFrameSize(width, height); return true; }
// Initialized EGL resources. bool Renderer::initEGL(ANativeWindow *window) { LOGI(">initEGL Got window %p", window); int samples = 0; Properties* config = Game::getInstance()->getConfig()->getNamespace( "window", true); if (config) { samples = std::max(config->getInt("samples"), 0); } LOGI(">initEGL 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 }; EGLint eglConfigCount; const EGLint eglContextAttrs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; const EGLint eglSurfaceAttrs[] = { EGL_RENDER_BUFFER, EGL_BACK_BUFFER, EGL_NONE }; if (_display == EGL_NO_DISPLAY && _context == EGL_NO_CONTEXT) { _display = eglGetCurrentDisplay(); LOGI(">initEGL _display=%p", _display); EGLSurface drawSurface = eglGetCurrentSurface(EGL_DRAW); LOGI(">initEGL drawSurface=%p", drawSurface); EGLSurface readSurface = eglGetCurrentSurface(EGL_READ); LOGI(">initEGL readSurface=%p", readSurface); EGLContext context = eglGetCurrentContext(); LOGI(">initEGL context=%p", context); // Get the EGL display and initialize. _display = eglGetDisplay(EGL_DEFAULT_DISPLAY); if (_display == EGL_NO_DISPLAY) { checkErrorEGL("eglGetDisplay"); goto error; } LOGI(">initEGL _display=%p", _display); eglMakeCurrent(_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); eglDestroyContext(_display, context); eglDestroySurface(_display, drawSurface); if (eglInitialize(_display, 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 }; for (unsigned int i = 0; i < 2; ++i) { eglConfigAttrs[1] = samples > 0 ? 1 : 0; eglConfigAttrs[3] = samples; eglConfigAttrs[5] = depthSizes[i]; if (eglChooseConfig(_display, eglConfigAttrs, &__eglConfig, 1, &eglConfigCount) == EGL_TRUE && eglConfigCount > 0) { LOGI(">initEGL depthSizes[i]=%d", depthSizes[i]); validConfig = true; break; } 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(_display, eglConfigAttrs, &__eglConfig, 1, &eglConfigCount) == EGL_TRUE && eglConfigCount > 0) { validConfig = true; break; } } if (validConfig) break; } else { GP_WARN("No EGL config found for depth_size=%d.", depthSizes[i]); } } if (!validConfig) { checkErrorEGL("eglChooseConfig"); goto error; } _context = eglCreateContext(_display, __eglConfig, EGL_NO_CONTEXT, eglContextAttrs); if (_context == 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; eglGetConfigAttrib(_display, __eglConfig, EGL_NATIVE_VISUAL_ID, &format); ANativeWindow_setBuffersGeometry(window, 0, 0, format); _surface = eglCreateWindowSurface(_display, __eglConfig, window, eglSurfaceAttrs); if (_surface == EGL_NO_SURFACE) { checkErrorEGL("eglCreateWindowSurface"); goto error; } if (eglMakeCurrent(_display, _surface, _surface, _context) != EGL_TRUE) { checkErrorEGL("eglMakeCurrent"); goto error; } eglQuerySurface(_display, _surface, EGL_WIDTH, &__width); eglQuerySurface(_display, _surface, EGL_HEIGHT, &__height); // __orientationAngle = getRotation() * 90; // Set vsync. eglSwapInterval(_display, WINDOW_VSYNC ? 1 : 0); // Initialize OpenGL ES extensions. __glExtensions = (const char*) glGetString(GL_EXTENSIONS); if (strstr(__glExtensions, "GL_OES_vertex_array_object") || strstr(__glExtensions, "GL_ARB_vertex_array_object")) { // Disable VAO extension for now. glBindVertexArray = (PFNGLBINDVERTEXARRAYOESPROC) eglGetProcAddress( "glBindVertexArrayOES"); glDeleteVertexArrays = (PFNGLDELETEVERTEXARRAYSOESPROC) eglGetProcAddress( "glDeleteVertexArraysOES"); glGenVertexArrays = (PFNGLGENVERTEXARRAYSOESPROC) eglGetProcAddress( "glGenVertexArraysOES"); glIsVertexArray = (PFNGLISVERTEXARRAYOESPROC) eglGetProcAddress( "glIsVertexArrayOES"); } LOGI("<initEGL true"); return true; error: LOGI("<initEGL false"); return false; }
Platform* Platform::create(Game* game, void* attachToWindow) { FileSystem::setResourcePath("./app/native/"); Platform* platform = new Platform(game); // Query game config int samples = 0; Properties* config = Game::getInstance()->getConfig()->getNamespace("window", true); if (config) { samples = std::max(config->getInt("samples"), 0); } __gestureSet = gestures_set_alloc(); swipe_gesture_alloc(NULL, gesture_callback, __gestureSet); pinch_gesture_alloc(NULL, gesture_callback, __gestureSet); tap_gesture_alloc(NULL, gesture_callback, __gestureSet); bps_initialize(); // Initialize navigator and orientation static const int SENSOR_RATE = 25000; sensor_set_rate(SENSOR_TYPE_AZIMUTH_PITCH_ROLL, SENSOR_RATE); sensor_set_skip_duplicates(SENSOR_TYPE_AZIMUTH_PITCH_ROLL, true); sensor_request_events(SENSOR_TYPE_AZIMUTH_PITCH_ROLL); navigator_request_events(0); navigator_rotation_lock(true); __orientationAngle = atoi(getenv("ORIENTATION")); int rc = 0; int screenFormat = SCREEN_FORMAT_RGBA8888; #ifdef __X86__ int screenUsage = SCREEN_USAGE_OPENGL_ES2; #else int screenUsage = SCREEN_USAGE_DISPLAY|SCREEN_USAGE_OPENGL_ES2; // Physical device copy directly into physical display #endif int screenSwapInterval = WINDOW_VSYNC ? 1 : 0; int screenTransparency = SCREEN_TRANSPARENCY_NONE; char *width_str = getenv("WIDTH"); char *height_str = getenv("HEIGHT"); // Hard-coded to (0,0). int windowPosition[] = { 0, 0 }; EGLint eglConfigCount; // Hard-coded to 32-bit/OpenGL ES 2.0. // NOTE: EGL_SAMPLE_BUFFERS and EGL_SAMPLES MUST remain at the beginning of the attribute list // since they are expected to be at indices 0-3 in config fallback code later. EGLint eglConfigAttrs[] = { EGL_SAMPLE_BUFFERS, samples > 0 ? 1 : 0, EGL_SAMPLES, samples, EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8, EGL_DEPTH_SIZE, 24, EGL_STENCIL_SIZE, 8, EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_NONE }; const EGLint eglContextAttrs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; const EGLint eglSurfaceAttrs[] = { EGL_RENDER_BUFFER, EGL_BACK_BUFFER, EGL_NONE }; // Create the screen context. rc = screen_create_context(&__screenContext, 0); if (rc) { perror("screen_create_context"); goto error; } // Create the screen window. rc = screen_create_window(&__screenWindow, __screenContext); if (rc) { perror("screen_create_window"); goto error; } // Set/get any window properties. rc = screen_set_window_property_iv(__screenWindow, SCREEN_PROPERTY_FORMAT, &screenFormat); if (rc) { perror("screen_set_window_property_iv(SCREEN_PROPERTY_FORMAT)"); goto error; } rc = screen_set_window_property_iv(__screenWindow, SCREEN_PROPERTY_USAGE, &screenUsage); if (rc) { perror("screen_set_window_property_iv(SCREEN_PROPERTY_USAGE)"); goto error; } if (width_str && height_str) { __screenWindowSize[0] = atoi(width_str); __screenWindowSize[1] = atoi(height_str); } else { screen_display_t screen_display; rc = screen_get_window_property_pv(__screenWindow, SCREEN_PROPERTY_DISPLAY, (void **)&screen_display); if (rc) { perror("screen_get_window_property_pv(SCREEN_PROPERTY_DISPLAY)"); goto error; } screen_display_mode_t screen_mode; rc = screen_get_display_property_pv(screen_display, SCREEN_PROPERTY_MODE, (void**)&screen_mode); if (rc) { perror("screen_get_display_property_pv(SCREEN_PROPERTY_MODE)"); goto error; } int size[2]; rc = screen_get_window_property_iv(__screenWindow, SCREEN_PROPERTY_BUFFER_SIZE, size); if (rc) { perror("screen_get_window_property_iv(SCREEN_PROPERTY_BUFFER_SIZE)"); goto error; } __screenWindowSize[0] = size[0]; __screenWindowSize[1] = size[1]; if ((__orientationAngle == 0) || (__orientationAngle == 180)) { if (((screen_mode.width > screen_mode.height) && (size[0] < size[1])) || ((screen_mode.width < screen_mode.height) && (size[0] > size[1]))) { __screenWindowSize[1] = size[0]; __screenWindowSize[0] = size[1]; } } else if ((__orientationAngle == 90) || (__orientationAngle == 270)) { if (((screen_mode.width > screen_mode.height) && (size[0] > size[1])) || ((screen_mode.width < screen_mode.height) && (size[0] < size[1]))) { __screenWindowSize[1] = size[0]; __screenWindowSize[0] = size[1]; } } else { perror("Navigator returned an unexpected orientation angle."); goto error; } rc = screen_set_window_property_iv(__screenWindow, SCREEN_PROPERTY_ROTATION, &__orientationAngle); if (rc) { perror("screen_set_window_property_iv(SCREEN_PROPERTY_ROTATION)"); goto error; } } rc = screen_set_window_property_iv(__screenWindow, SCREEN_PROPERTY_BUFFER_SIZE, __screenWindowSize); if (rc) { perror("screen_set_window_property_iv(SCREEN_PROPERTY_BUFFER_SIZE)"); goto error; } if (windowPosition[0] != 0 || windowPosition[1] != 0) { rc = screen_set_window_property_iv(__screenWindow, SCREEN_PROPERTY_POSITION, windowPosition); if (rc) { perror("screen_set_window_property_iv(SCREEN_PROPERTY_POSITION)"); goto error; } } rc = screen_set_window_property_iv(__screenWindow, SCREEN_PROPERTY_TRANSPARENCY, &screenTransparency); if (rc) { perror("screen_set_window_property_iv(SCREEN_PROPERTY_TRANSPARENCY)"); goto error; } // Double buffered. rc = screen_create_window_buffers(__screenWindow, 2); if (rc) { perror("screen_create_window_buffers"); goto error; } // Create screen event object. rc = screen_create_event(&__screenEvent); if (rc) { perror("screen_create_event"); goto error; } // Request screen events. screen_request_events(__screenContext); // Get the EGL display and initialize. __eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); if (__eglDisplay == EGL_NO_DISPLAY) { perror("eglGetDisplay"); goto error; } if (eglInitialize(__eglDisplay, NULL, NULL) != EGL_TRUE) { perror("eglInitialize"); goto error; } if (eglChooseConfig(__eglDisplay, eglConfigAttrs, &__eglConfig, 1, &eglConfigCount) != EGL_TRUE || eglConfigCount == 0) { bool success = false; while (samples) { // Try lowering the MSAA sample count until we find a supported config GP_WARN("Failed to find a valid EGL configuration with EGL samples=%d. Trying samples=%d instead.", samples, samples/2); samples /= 2; eglConfigAttrs[1] = samples > 0 ? 1 : 0; eglConfigAttrs[3] = samples; if (eglChooseConfig(__eglDisplay, eglConfigAttrs, &__eglConfig, 1, &eglConfigCount) == EGL_TRUE && eglConfigCount > 0) { success = true; break; } } if (!success) { checkErrorEGL("eglChooseConfig"); goto error; } } __eglContext = eglCreateContext(__eglDisplay, __eglConfig, EGL_NO_CONTEXT, eglContextAttrs); if (__eglContext == EGL_NO_CONTEXT) { checkErrorEGL("eglCreateContext"); goto error; } __eglSurface = eglCreateWindowSurface(__eglDisplay, __eglConfig, __screenWindow, eglSurfaceAttrs); if (__eglSurface == EGL_NO_SURFACE) { checkErrorEGL("eglCreateWindowSurface"); goto error; } if (eglMakeCurrent(__eglDisplay, __eglSurface, __eglSurface, __eglContext) != EGL_TRUE) { checkErrorEGL("eglMakeCurrent"); goto error; } // Set vsync. eglSwapInterval(__eglDisplay, screenSwapInterval); // Initialize OpenGL ES extensions. __glExtensions = (const char*)glGetString(GL_EXTENSIONS); 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"); } return platform; error: // TODO: cleanup return NULL; }
Platform* Platform::create(Game* game) { FileSystem::setResourcePath("./app/native/"); Platform* platform = new Platform(game); bps_initialize(); accelerometer_set_update_frequency(FREQ_40_HZ); navigator_request_events(0); navigator_rotation_lock(true); // Determine initial orientation angle. orientation_direction_t direction; orientation_get(&direction, &__orientationAngle); int rc = 0; int screenFormat = SCREEN_FORMAT_RGBA8888; int screenUsage = SCREEN_USAGE_DISPLAY|SCREEN_USAGE_OPENGL_ES2; int screenSwapInterval = WINDOW_VSYNC ? 1 : 0; int screenTransparency = SCREEN_TRANSPARENCY_NONE; int angle = atoi(getenv("ORIENTATION")); // Hard-coded to (0,0). int windowPosition[] = { 0, 0 }; EGLint eglConfigCount; // Hard-coded to 32-bit/OpenGL ES 2.0. const EGLint eglConfigAttrs[] = { EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8, EGL_DEPTH_SIZE, 24, EGL_STENCIL_SIZE, 8, EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_NONE }; const EGLint eglContextAttrs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; const EGLint eglSurfaceAttrs[] = { EGL_RENDER_BUFFER, EGL_BACK_BUFFER, EGL_NONE }; // Create the screen context. rc = screen_create_context(&__screenContext, 0); if (rc) { perror("screen_create_context"); goto error; } // Create the screen window. rc = screen_create_window(&__screenWindow, __screenContext); if (rc) { perror("screen_create_window"); goto error; } // Set/get any window prooperties. rc = screen_set_window_property_iv(__screenWindow, SCREEN_PROPERTY_FORMAT, &screenFormat); if (rc) { perror("screen_set_window_property_iv(SCREEN_PROPERTY_FORMAT)"); goto error; } rc = screen_set_window_property_iv(__screenWindow, SCREEN_PROPERTY_USAGE, &screenUsage); if (rc) { perror("screen_set_window_property_iv(SCREEN_PROPERTY_USAGE)"); goto error; } screen_display_t screen_display; rc = screen_get_window_property_pv(__screenWindow, SCREEN_PROPERTY_DISPLAY, (void **)&screen_display); if (rc) { perror("screen_get_window_property_pv(SCREEN_PROPERTY_DISPLAY)"); goto error; } screen_display_mode_t screen_mode; rc = screen_get_display_property_pv(screen_display, SCREEN_PROPERTY_MODE, (void**)&screen_mode); if (rc) { perror("screen_get_display_property_pv(SCREEN_PROPERTY_MODE)"); goto error; } int size[2]; rc = screen_get_window_property_iv(__screenWindow, SCREEN_PROPERTY_BUFFER_SIZE, size); if (rc) { perror("screen_get_window_property_iv(SCREEN_PROPERTY_BUFFER_SIZE)"); goto error; } __screenWindowSize[0] = size[0]; __screenWindowSize[1] = size[1]; if ((angle == 0) || (angle == 180)) { if (((screen_mode.width > screen_mode.height) && (size[0] < size[1])) || ((screen_mode.width < screen_mode.height) && (size[0] > size[1]))) { __screenWindowSize[1] = size[0]; __screenWindowSize[0] = size[1]; } } else if ((angle == 90) || (angle == 270)) { if (((screen_mode.width > screen_mode.height) && (size[0] > size[1])) || ((screen_mode.width < screen_mode.height) && (size[0] < size[1]))) { __screenWindowSize[1] = size[0]; __screenWindowSize[0] = size[1]; } } else { perror("Navigator returned an unexpected orientation angle."); goto error; } rc = screen_set_window_property_iv(__screenWindow, SCREEN_PROPERTY_BUFFER_SIZE, __screenWindowSize); if (rc) { perror("screen_set_window_property_iv(SCREEN_PROPERTY_BUFFER_SIZE)"); goto error; } rc = screen_set_window_property_iv(__screenWindow, SCREEN_PROPERTY_ROTATION, &angle); if (rc) { perror("screen_set_window_property_iv(SCREEN_PROPERTY_ROTATION)"); goto error; } if (windowPosition[0] != 0 || windowPosition[1] != 0) { rc = screen_set_window_property_iv(__screenWindow, SCREEN_PROPERTY_POSITION, windowPosition); if (rc) { perror("screen_set_window_property_iv(SCREEN_PROPERTY_POSITION)"); goto error; } } rc = screen_set_window_property_iv(__screenWindow, SCREEN_PROPERTY_TRANSPARENCY, &screenTransparency); if (rc) { perror("screen_set_window_property_iv(SCREEN_PROPERTY_TRANSPARENCY)"); goto error; } // Double buffered. rc = screen_create_window_buffers(__screenWindow, 2); if (rc) { perror("screen_create_window_buffers"); goto error; } // Create screen event object. rc = screen_create_event(&__screenEvent); if (rc) { perror("screen_create_event"); goto error; } // Request screen events. screen_request_events(__screenContext); // Get the EGL display and initialize. __eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); if (__eglDisplay == EGL_NO_DISPLAY) { perror("eglGetDisplay"); goto error; } if (eglInitialize(__eglDisplay, NULL, NULL) != EGL_TRUE) { perror("eglInitialize"); goto error; } if (eglChooseConfig(__eglDisplay, eglConfigAttrs, &__eglConfig, 1, &eglConfigCount) != EGL_TRUE || eglConfigCount == 0) { checkErrorEGL("eglChooseConfig"); goto error; } __eglContext = eglCreateContext(__eglDisplay, __eglConfig, EGL_NO_CONTEXT, eglContextAttrs); if (__eglContext == EGL_NO_CONTEXT) { checkErrorEGL("eglCreateContext"); goto error; } __eglSurface = eglCreateWindowSurface(__eglDisplay, __eglConfig, __screenWindow, eglSurfaceAttrs); if (__eglSurface == EGL_NO_SURFACE) { checkErrorEGL("eglCreateWindowSurface"); goto error; } if (eglMakeCurrent(__eglDisplay, __eglSurface, __eglSurface, __eglContext) != EGL_TRUE) { checkErrorEGL("eglMakeCurrent"); goto error; } // Set vsync. eglSwapInterval(__eglDisplay, screenSwapInterval); // Initialize OpenGL ES extensions. __glExtensions = (const char*)glGetString(GL_EXTENSIONS); if (strstr(__glExtensions, "GL_OES_vertex_array_object") || strstr(__glExtensions, "GL_ARB_vertex_array_object")) { // Disable VAO extension for now. glBindVertexArray = (PFNGLBINDVERTEXARRAYOESPROC)eglGetProcAddress("glBindVertexArrayOES"); glDeleteVertexArrays = (PFNGLDELETEVERTEXARRAYSOESPROC)eglGetProcAddress("glDeleteVertexArrays"); glGenVertexArrays = (PFNGLGENVERTEXARRAYSOESPROC)eglGetProcAddress("glGenVertexArraysOES"); glIsVertexArray = (PFNGLISVERTEXARRAYOESPROC)eglGetProcAddress("glIsVertexArrayOES"); } return platform; error: // TODO: cleanup // return NULL; }
bool CCEGLView::initGL() { int rc = 0; int angle = 0; // Hard-coded to (0,0). int windowPosition[] = { 0, 0 }; EGLint eglConfigCount; EGLConfig config; // Hard-coded to 32-bit/OpenGL ES 2.0. const EGLint eglConfigAttrs[] = { EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8, EGL_DEPTH_SIZE, 24, EGL_STENCIL_SIZE, 8, EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_NONE }; const EGLint eglContextAttrs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; const EGLint eglSurfaceAttrs[] = { EGL_RENDER_BUFFER, EGL_BACK_BUFFER, EGL_NONE }; // Get the EGL display and initialize. m_eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); if (m_eglDisplay == EGL_NO_DISPLAY) { perror("eglGetDisplay"); return false; } sleep(1); if (eglInitialize(m_eglDisplay, NULL, NULL) != EGL_TRUE) { perror("eglInitialize"); return false; } if (eglChooseConfig(m_eglDisplay, eglConfigAttrs, &config, 1, &eglConfigCount) != EGL_TRUE || eglConfigCount == 0) { checkErrorEGL("eglChooseConfig"); return false; } m_eglContext = eglCreateContext(m_eglDisplay, config, EGL_NO_CONTEXT, eglContextAttrs); if (m_eglContext == EGL_NO_CONTEXT) { checkErrorEGL("eglCreateContext"); return false; } m_eglSurface = eglCreateWindowSurface(m_eglDisplay, config, NULL, eglSurfaceAttrs); if (m_eglSurface == EGL_NO_SURFACE) { checkErrorEGL("eglCreateWindowSurface"); return false; } if (eglMakeCurrent(m_eglDisplay, m_eglSurface, m_eglSurface, m_eglContext) != EGL_TRUE) { checkErrorEGL("eglMakeCurrent"); return false; } // FIXME: Get the actual canvas size somehow. EGLint width = 800; EGLint height = 500; #warning Assuming screen size is 800X500. Mouse cursor will be offset if a different sized canvas is used. if ((m_eglDisplay == EGL_NO_DISPLAY) || (m_eglSurface == EGL_NO_SURFACE) ) return EXIT_FAILURE; /* eglQuerySurface(m_eglDisplay, m_eglSurface, EGL_WIDTH, &width); eglQuerySurface(m_eglDisplay, m_eglSurface, EGL_HEIGHT, &height); */ m_obScreenSize.width = width; m_obScreenSize.height = height; printf("width, height = %d, %d\n", width, height); glViewport(0, 0, width, height); // Set vsync. // eglSwapInterval(m_eglDisplay, screenSwapInterval); return true; }