/** * Initialize an EGL context for the current display. */ static int engine_init_display(struct engine* engine) { // initialize OpenGL ES and EGL static int done = 0; /* * Here specify the attributes of the desired configuration. * Below, we select an EGLConfig with at least 8 bits per color * component compatible with on-screen windows */ #if g_iColorMode == 0 // select 16 bit back buffer for performance const EGLint attribs[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_BUFFER_SIZE, 16, EGL_DEPTH_SIZE, 16, EGL_STENCIL_SIZE, 0, EGL_CONFIG_CAVEAT, EGL_NONE, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_NONE }; #else // select 32 bit back buffer for quality const EGLint attribs[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_BLUE_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_RED_SIZE, 8, EGL_ALPHA_SIZE, 8, EGL_BUFFER_SIZE, 32, EGL_DEPTH_SIZE, 16, EGL_STENCIL_SIZE, 0, EGL_CONFIG_CAVEAT, EGL_NONE, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_NONE }; #endif EGLint w, h, dummy, format; EGLint numConfigs; EGLSurface surface; EGLContext context; EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); eglInitialize(display, 0, 0); /* Here, the application chooses the configuration it desires. In this * sample, we have a very simplified selection process, where we pick * the first EGLConfig that matches our criteria */ EGLConfig allConfigs[20]; eglChooseConfig(display, attribs, allConfigs, 20, &numConfigs); config = allConfigs[0]; if ( numConfigs == 0 ) { LOGW( "Failed to find suitable render format" ); exit(0); //return 0; } int i = 0; for ( i = 0; i < numConfigs; i++ ) { if ( i > 19 ) continue; EGLint red; EGLint green; EGLint blue; EGLint alpha; EGLint depth; EGLint stencil; EGLint window; EGLint render; eglGetConfigAttrib( display, allConfigs[i], EGL_RED_SIZE, &red ); eglGetConfigAttrib( display, allConfigs[i], EGL_GREEN_SIZE, &green ); eglGetConfigAttrib( display, allConfigs[i], EGL_BLUE_SIZE, &blue ); eglGetConfigAttrib( display, allConfigs[i], EGL_ALPHA_SIZE, &alpha ); eglGetConfigAttrib( display, allConfigs[i], EGL_DEPTH_SIZE, &depth ); eglGetConfigAttrib( display, allConfigs[i], EGL_STENCIL_SIZE, &stencil ); eglGetConfigAttrib( display, allConfigs[i], EGL_SURFACE_TYPE, &window ); eglGetConfigAttrib( display, allConfigs[i], EGL_NATIVE_VISUAL_ID, &format ); eglGetConfigAttrib( display, allConfigs[i], EGL_RENDERABLE_TYPE, &render ); LOGW( "R: %d, G: %d, B: %d, A: %d, D: %d, W: %d, F: %d, S: %d, R: %d", red, green, blue, alpha, depth, window, format, stencil, render ); } int formatIndex = 0; // check for devices that need special render formats (Wildfire S being one) if ( checkformat( engine->app->activity ) > 0 ) { LOGW( "Adjusting render format for device" ); for ( i = 0; i < numConfigs; i++ ) { if ( i > 19 ) continue; eglGetConfigAttrib( display, allConfigs[i], EGL_NATIVE_VISUAL_ID, &format ); if ( format > 0 ) { config = allConfigs[i]; formatIndex = -1; break; } } } surface = eglCreateWindowSurface(display, config, engine->app->window, NULL); while ( surface == EGL_NO_SURFACE ) { LOGW( "Failed to create EGL surface: %d, trying different format", eglGetError() ); formatIndex++; if ( formatIndex >= numConfigs || formatIndex > 19 ) { LOGW( "Failed to find compatible format" ); return -1; } config = allConfigs[ formatIndex ]; surface = eglCreateWindowSurface(display, config, engine->app->window, NULL); } /* 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. */ eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format); int result = ANativeWindow_setBuffersGeometry(engine->app->window, 0, 0, format); LOGW( "Result: %d", result ); const EGLint contextAttribList[] = {EGL_CONTEXT_CLIENT_VERSION, 2,EGL_NONE}; context = eglCreateContext(display, config, NULL, contextAttribList); if ( context == EGL_NO_CONTEXT ) { LOGW( "Failed to create EGL context: %d", eglGetError() ); return -1; } if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) { int error = eglGetError(); LOGW("Unable to eglMakeCurrent: %d", eglGetError()); return -1; } eglQuerySurface(display, surface, EGL_WIDTH, &w); eglQuerySurface(display, surface, EGL_HEIGHT, &h); LOGW( "Width: %d Height: %d", w, h ); engine->display = display; engine->context = context; engine->surface = surface; engine->width = w; engine->height = h; engine->state.angle = 0; struct egldata data; data.display = display; data.surface = surface; data.context = context; data.activity = engine->app->activity; if ( done != 0 ) { updateptr( &data ); } else { init( &data ); } //begin(); p_AMotionEvent_getAxisValue = dlsym(RTLD_DEFAULT, "AMotionEvent_getAxisValue"); done = 1; return 0; }
int Main::main(int argc, _TCHAR* argv[]) { consoleWindowHandle = GetConsoleWindow(); /// Initialize time keeping. LARGE_INTEGER frequency; assert(QueryPerformanceFrequency(&frequency) != 0); timeCounterFrequency = static_cast<double>(frequency.QuadPart); assert(timeCounterFrequency > 0); QueryPerformanceCounter(&startTime); /// Register a window class. /// \todo Configure small and large application icons. const wchar_t * windowClassName = L"x"; WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = GetModuleHandle(NULL); wcex.hIcon = 0; //LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MAIN)); wcex.hCursor = 0; //LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = NULL; //MAKEINTRESOURCE(IDC_MAIN); wcex.lpszClassName = windowClassName; wcex.hIconSm = 0; //LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); ATOM windowClass = RegisterClassExW(&wcex); assert(windowClass != 0); /// Determine size of window border by creating a temporary window. applicationWindowHandle = CreateWindowEx( WS_EX_CLIENTEDGE, windowClassName, L"X", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, Platform::screenWidth, Platform::screenHeight, NULL, NULL, GetModuleHandle(NULL), NULL); assert(applicationWindowHandle != NULL); RECT clientRect; GetClientRect(applicationWindowHandle, &clientRect); int clientRectWidth = clientRect.right; int clientRectHeight = clientRect.bottom; RECT windowRect; GetWindowRect(applicationWindowHandle, &windowRect); int windowRectWidth = windowRect.right - windowRect.left; int windowRectHeight = windowRect.bottom - windowRect.top; int windowBorderWidth = windowRectWidth - clientRectWidth; int windowBorderHeight = windowRectHeight - clientRectHeight; assert(DestroyWindow(applicationWindowHandle)); Core::run = true; // Temporary window sets this to false when destroyed. /// Calculate the window width and height needed to achieve /// the application's desired width and height. int windowWidth = Platform::screenWidth + windowBorderWidth; int windowHeight = Platform::screenHeight + windowBorderHeight; /// Create final application window. applicationWindowHandle = CreateWindowEx( WS_EX_CLIENTEDGE, windowClassName, L"Project X", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, windowWidth, windowHeight, NULL, NULL, GetModuleHandle(NULL), NULL); assert(applicationWindowHandle != NULL); ShowWindow(applicationWindowHandle, SW_SHOW); UpdateWindow(applicationWindowHandle); #if _DEBUG /// When debugging, start thread to read Lua commands from console. CreateThread(0, 0, consoleThread, 0, 0, 0); #else /// When not debugging, hide console. ShowWindow(consoleWindowHandle, SW_HIDE); #endif deviceContext = GetDC(applicationWindowHandle); eglNativeWindowType = (EGLNativeWindowType) applicationWindowHandle; // Get the display handle. eglDisplay = eglGetDisplay(deviceContext); if (eglDisplay == EGL_NO_DISPLAY) { Platform::fatalError("No EGL Display."); } // Check OpenGL ES version. EGLint major; EGLint minor; eglInitialize(eglDisplay, &major, &minor); checkEglError("eglInitialize"); if (major == 1 && minor < 4) { Platform::fatalError("EGL version 1.4 or later required."); } memset(&frameBufferConfiguration, 0, sizeof(EGLConfig)); // Not sure this is needed. eglChooseConfig(eglDisplay, frameBufferAttributes, &frameBufferConfiguration, 1, &numFrameBufferConfigurations); checkEglError("Call to eglChooseConfig failed."); if (numFrameBufferConfigurations == 0) { Platform::fatalError("No EGL frame buffer configurations that match the specified requirements."); } // Create a window surface. EGLint surfaceAttributes[] = { EGL_RENDER_BUFFER, EGL_BACK_BUFFER, EGL_NONE, EGL_NONE }; eglSurface = eglCreateWindowSurface(eglDisplay, frameBufferConfiguration, eglNativeWindowType, surfaceAttributes); checkEglError("eglCreateWindowSurface"); if (eglSurface == EGL_NO_SURFACE) { Platform::fatalError("Call to eglCreateWindowSurface failed."); } createContext(); Core::init(); /// Enter message loop. while (Core::run) { MSG Msg; while (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE) > 0) { TranslateMessage(&Msg); DispatchMessage(&Msg); } // Core has not yet drawn requested frame. LARGE_INTEGER currentTime; QueryPerformanceCounter(¤tTime); double totalElapsedSeconds = static_cast<double>(currentTime.QuadPart - startTime.QuadPart) / timeCounterFrequency; Core::onMessageQueueEmpty(totalElapsedSeconds); Core::renderNextFrame(); // Core has drawn a frame, so let's render it. if (EGL_FALSE == eglSwapBuffers(eglDisplay, eglSurface)) { // eglSwapBuffers will fail after a power event. // In this case, we need to reinitialize. EGLint error = eglGetError(); if (error == EGL_CONTEXT_LOST) { // Power event; need to "... destroy all contexts and reinitialise OpenGL ES state // and objects to continue rendering." destroyContext(); createContext(); } } } Core::shutdown(); destroyContext(); eglDestroySurface(eglDisplay, eglSurface); eglTerminate(eglDisplay); while (waitForConsoleToClose) { // Wait until user presses enter in console window. } ReleaseDC(Main::applicationWindowHandle, Main::deviceContext); return 0; }
int main(int argc, char *argv[]) { EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); assert(display != EGL_NO_DISPLAY); assert(eglGetError() == EGL_SUCCESS); EGLint major = 0, minor = 0; EGLBoolean ret = eglInitialize(display, &major, &minor); assert(eglGetError() == EGL_SUCCESS); assert(ret == EGL_TRUE); assert(major * 10000 + minor >= 10004); EGLint numConfigs; ret = eglGetConfigs(display, NULL, 0, &numConfigs); assert(eglGetError() == EGL_SUCCESS); assert(ret == EGL_TRUE); EGLint attribs[] = { EGL_RED_SIZE, 5, EGL_GREEN_SIZE, 6, EGL_BLUE_SIZE, 5, EGL_NONE }; EGLConfig config; ret = eglChooseConfig(display, attribs, &config, 1, &numConfigs); assert(eglGetError() == EGL_SUCCESS); assert(ret == EGL_TRUE); EGLNativeWindowType dummyWindow; EGLSurface surface = eglCreateWindowSurface(display, config, dummyWindow, NULL); assert(eglGetError() == EGL_SUCCESS); assert(surface != 0); // WebGL maps to GLES2. GLES1 is not supported. EGLint contextAttribsOld[] = { EGL_CONTEXT_CLIENT_VERSION, 1, EGL_NONE }; EGLContext context = eglCreateContext(display, config, NULL, contextAttribsOld); assert(eglGetError() != EGL_SUCCESS); //Test for invalid attribs EGLint contextInvalidAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, 0xFFFF, -1, EGL_NONE }; context = eglCreateContext(display, config, NULL, contextInvalidAttribs); assert(eglGetError() != EGL_SUCCESS); assert(context == 0); //Test for missing terminator EGLint contextAttribsMissingTerm[] = { EGL_CONTEXT_CLIENT_VERSION, 2, }; context = eglCreateContext(display, config, NULL, contextAttribsMissingTerm); assert(eglGetError() != EGL_SUCCESS); assert(context == 0); //Test for null terminator EGLint contextAttribsNullTerm[] = { EGL_CONTEXT_CLIENT_VERSION, 2, 0 }; context = eglCreateContext(display, config, NULL, contextAttribsNullTerm); assert(eglGetError() != EGL_SUCCESS); assert(context == 0); //Test for invalid and null terminator EGLint contextAttribsNullTermInvalid[] = { 0, }; context = eglCreateContext(display, config, NULL, contextAttribsNullTermInvalid); assert(eglGetError() != EGL_SUCCESS); assert(context == 0); // The correct attributes, should create a good EGL context EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; context = eglCreateContext(display, config, NULL, contextAttribs); assert(eglGetError() == EGL_SUCCESS); assert(context != 0); assert(eglGetCurrentContext() == 0); // Creating a context does not yet activate it. assert(eglGetError() == EGL_SUCCESS); ret = eglMakeCurrent(display, surface, surface, context); assert(eglGetError() == EGL_SUCCESS); assert(ret == EGL_TRUE); assert(eglGetCurrentContext() == context); assert(eglGetCurrentSurface(EGL_READ) == surface); assert(eglGetCurrentSurface(EGL_DRAW) == surface); glClearColor(1.0,0.0,0.0,0.5); glClear(GL_COLOR_BUFFER_BIT); ret = eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); assert(eglGetError() == EGL_SUCCESS); assert(ret == EGL_TRUE); assert(eglGetCurrentContext() == EGL_NO_CONTEXT); assert(eglGetCurrentSurface(EGL_READ) == EGL_NO_SURFACE); assert(eglGetCurrentSurface(EGL_DRAW) == EGL_NO_SURFACE); assert(eglSwapInterval(display, 0) == EGL_TRUE); assert(eglGetError() == EGL_SUCCESS); assert(eglSwapInterval(display, 1) == EGL_TRUE); assert(eglGetError() == EGL_SUCCESS); assert(eglSwapInterval(display, 2) == EGL_TRUE); assert(eglGetError() == EGL_SUCCESS); ret = eglTerminate(display); assert(eglGetError() == EGL_SUCCESS); assert(ret == EGL_TRUE); // eglGetProcAddress() without active GL context and/or connected EGL display (after eglTerminate) is required to work, even though the returned function // pointers cannot be called unless an active context is available: // "return value of NULL indicates that the specified function does not exist for the implementation." // "Client API function pointers returned by eglGetProcAddress are independent of the display and the currently bound client API context, and may be used by any client API context which supports the function." // At https://www.khronos.org/registry/EGL/specs/eglspec.1.5.pdf, pages 82-32. assert(eglGetProcAddress("glClear") != 0); assert(eglGetProcAddress("glWakaWaka") == 0); #ifdef REPORT_RESULT REPORT_RESULT(result); #endif }
/***************************************************************************** * Function Name : ApiInitAPI * Returns : true for success * Description : Initialise the 3D API *****************************************************************************/ bool PVRShellInit::ApiInitAPI() { int bDone; m_NDT = (EGLNativeDisplayType)OsGetNativeDisplayType(); m_NPT = (EGLNativePixmapType) OsGetNativePixmapType(); m_NWT = (EGLNativeWindowType) OsGetNativeWindowType(); m_EGLContext = 0; do { bDone = true; m_EGLDisplay = eglGetDisplay(m_NDT); if(m_EGLDisplay == EGL_NO_DISPLAY) { #if defined(BUILD_OGLES2) || defined(BUILD_OVG) || defined(BUILD_OGLES3) m_EGLDisplay = eglGetDisplay((EGLNativeDisplayType)EGL_DEFAULT_DISPLAY); #else m_EGLDisplay = eglGetDisplay((NativeDisplayType)EGL_DEFAULT_DISPLAY); #endif } if(!eglInitialize(m_EGLDisplay, &m_MajorVersion, &m_MinorVersion)) { m_pShell->PVRShellSet(prefExitMessage, "PVRShell: Unable to initialise EGL\n"); m_pShell->PVRShellOutputDebug("PVRShell: EGL Error (%s)\n", StringFrom_eglGetError()); return false; } m_pShell->PVRShellOutputDebug("PVRShell: EGL %d.%d initialized\n", m_MajorVersion, m_MinorVersion); // Check Extension avaliablility after EGL initialization if (m_MajorVersion > 1 || (m_MajorVersion == 1 && m_MinorVersion >= 1)) { m_bPowerManagementSupported = true; } else { m_bPowerManagementSupported = PVRShellIsExtensionSupported(m_EGLDisplay,"EGL_IMG_power_management"); } do { // bind OpenVG API if requested if(m_pShell->m_pShellData->bNeedOpenVG) { #ifndef BUILD_OVG m_pShell->PVRShellSet(prefExitMessage, "PVRShell: OpenVG not supported. Compile with BUILD_OVG defined."); return false; #else if(!eglBindAPI(EGL_OPENVG_API)) { m_pShell->PVRShellSet(prefExitMessage, "PVRShell: Failed to bind OpenVG API\n"); return false; } m_pShell->PVRShellOutputDebug("PVRShell: OpenVG bound\n"); #endif } else { #if defined(BUILD_OGL) if(!eglBindAPI(EGL_OPENGL_API)) { m_pShell->PVRShellSet(prefExitMessage, "PVRShell: Failed to bind OpenGL API\n"); return false; } #else #if defined EGL_VERSION_1_3 && defined GL_ES_VERSION_2_0 if(!eglBindAPI(EGL_OPENGL_ES_API)) { m_pShell->PVRShellSet(prefExitMessage, "PVRShell: Failed to bind OpenGL ES API\n"); return false; } #endif #endif } // Find an EGL config m_EGLConfig = SelectEGLConfiguration(m_pShell->m_pShellData); eglGetConfigAttrib(m_EGLDisplay, m_EGLConfig, EGL_CONFIG_ID, &m_iConfig); // Destroy the context if we already created one if (m_EGLContext) { eglDestroyContext(m_EGLDisplay, m_EGLContext); } // Attempt to create a context EGLint ai32ContextAttribs[32]; int i = 0; #if defined(EGL_VERSION_1_3) && defined(GL_ES_VERSION_2_0) ai32ContextAttribs[i++] = EGL_CONTEXT_CLIENT_VERSION; ai32ContextAttribs[i++] = 2; #endif #if defined(BUILD_OGLES2) || defined(BUILD_OGLES) || defined(BUILD_OGLES3) if(PVRShellIsExtensionSupported(m_EGLDisplay,"EGL_IMG_context_priority")) { ai32ContextAttribs[i++] = EGL_CONTEXT_PRIORITY_LEVEL_IMG; switch(m_pShell->PVRShellGet(prefPriority)) { case 0: ai32ContextAttribs[i++] = EGL_CONTEXT_PRIORITY_LOW_IMG; break; case 1: ai32ContextAttribs[i++] = EGL_CONTEXT_PRIORITY_MEDIUM_IMG; break; default:ai32ContextAttribs[i++] = EGL_CONTEXT_PRIORITY_HIGH_IMG; } } #endif ai32ContextAttribs[i] = EGL_NONE; m_EGLContext = eglCreateContext(m_EGLDisplay, m_EGLConfig, NULL, ai32ContextAttribs); if(m_EGLContext == EGL_NO_CONTEXT) { if(m_iRequestedConfig > 0) { // We failed to create a context m_pShell->PVRShellSet(prefExitMessage, "PVRShell: Unable to create a context\n"); return false; } else if(m_pShell->m_pShellData->bNeedPbuffer) { // Disable P-buffer and try again m_pShell->m_pShellData->bNeedPbuffer = false; } else if(m_pShell->m_pShellData->bNeedStencilBuffer) { // Disable Stencil Buffer and try again m_pShell->m_pShellData->bNeedStencilBuffer = false; } else if(m_pShell->m_pShellData->nFSAAMode > 0) { // Still failing, reduce the mode of AA and try again --m_pShell->m_pShellData->nFSAAMode; } else { m_pShell->PVRShellSet(prefExitMessage, "PVRShell: Unable to create a context\n"); return false; } } } while(m_EGLContext == EGL_NO_CONTEXT); #if defined(__QNXNTO__) int format = SCREEN_FORMAT_RGBX8888; if(screen_set_window_property_iv((_screen_window*) m_NWT, SCREEN_PROPERTY_FORMAT, &format)) { m_pShell->PVRShellSet(prefExitMessage, "PVRShell: Failed to set window property SCREEN_PROPERTY_FORMAT\n"); return false; } #if defined(BUILD_OGLES2) int usage = SCREEN_USAGE_OPENGL_ES2; #else #if defined(BUILD_OGLES) int usage = SCREEN_USAGE_OPENGL_ES1; #else #if defined(BUILD_OVG) int usage = SCREEN_USAGE_OPENVG; #endif #endif #endif if(screen_set_window_property_iv((_screen_window*) m_NWT, SCREEN_PROPERTY_USAGE, &usage)) { m_pShell->PVRShellSet(prefExitMessage, "PVRShell: Failed to set window property SCREEN_PROPERTY_USAGE\n"); return false; } if(screen_create_window_buffers((_screen_window*) m_NWT, 2)) { m_pShell->PVRShellSet(prefExitMessage, "PVRShell: Failed to create window buffers\n"); return false; } #endif EGLint attrib_list[16]; int i = 0; #if defined(EGL_VERSION_1_2) if(m_pShell->m_pShellData->bNeedAlphaFormatPre) // The default is EGL_ALPHA_FORMAT_NONPRE { attrib_list[i++] = EGL_ALPHA_FORMAT; attrib_list[i++] = EGL_ALPHA_FORMAT_PRE; } #endif // Terminate the attribute list with EGL_NONE attrib_list[i] = EGL_NONE; if(m_pShell->m_pShellData->bNeedPixmap) { m_pShell->PVRShellOutputDebug("InitAPI() Using pixmaps, about to create egl surface\n"); m_EGLWindow = eglCreatePixmapSurface(m_EGLDisplay, m_EGLConfig, m_NPT, attrib_list); } else { #if defined(ANDROID) EGLint visualID; eglGetConfigAttrib(m_EGLDisplay, m_EGLConfig, EGL_NATIVE_VISUAL_ID, &visualID); // Change the format of our window to match our config ANativeWindow_setBuffersGeometry(m_NWT, 0, 0, visualID); #endif m_EGLWindow = eglCreateWindowSurface(m_EGLDisplay, m_EGLConfig, m_NWT, attrib_list); // If we have failed to create a surface then try using Null if(m_EGLWindow == EGL_NO_SURFACE) { m_EGLWindow = eglCreateWindowSurface(m_EGLDisplay, m_EGLConfig, NULL, attrib_list); } } if (m_EGLWindow == EGL_NO_SURFACE) { m_pShell->PVRShellSet(prefExitMessage, "PVRShell: Unable to create surface\n"); return false; } if (!eglMakeCurrent(m_EGLDisplay, m_EGLWindow, m_EGLWindow, m_EGLContext)) { #ifdef EGL_VERSION_1_3 if((eglGetError() == EGL_CONTEXT_LOST)) #else if((eglGetError() == EGL_CONTEXT_LOST_IMG) && m_bPowerManagementSupported) #endif { bDone = false; } else { m_pShell->PVRShellSet(prefExitMessage, "PVRShell: Unable to make context current\n"); return false; } } } while(!bDone); /* Get correct screen width and height and save them into m_pShell->m_pShellData->nShellDimX and m_pShell->m_pShellData->nShellDimY */ eglQuerySurface(m_EGLDisplay, m_EGLWindow, EGL_WIDTH, (EGLint*)&m_pShell->m_pShellData->nShellDimX ); eglQuerySurface(m_EGLDisplay, m_EGLWindow, EGL_HEIGHT, (EGLint*)&m_pShell->m_pShellData->nShellDimY ); #if defined(ANDROID) && !defined(BUILD_OVG) glViewport(0,0,m_pShell->m_pShellData->nShellDimX, m_pShell->m_pShellData->nShellDimY); #endif /* Done - activate requested features */ ApiActivatePreferences(); return true; }
static bool gfx_ctx_set_video_mode(void *data, unsigned width, unsigned height, bool fullscreen) { if (g_inited) return false; int i; int ret = 0; struct drm_fb *fb = NULL; struct sigaction sa = {{0}}; sa.sa_handler = sighandler; sa.sa_flags = SA_RESTART; sigemptyset(&sa.sa_mask); sigaction(SIGINT, &sa, NULL); sigaction(SIGTERM, &sa, NULL); #define EGL_ATTRIBS_BASE \ EGL_SURFACE_TYPE, EGL_WINDOW_BIT, \ EGL_RED_SIZE, 1, \ EGL_GREEN_SIZE, 1, \ EGL_BLUE_SIZE, 1, \ EGL_ALPHA_SIZE, 0, \ EGL_DEPTH_SIZE, 0 static const EGLint egl_attribs_gl[] = { EGL_ATTRIBS_BASE, EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, EGL_NONE, }; static const EGLint egl_attribs_gles[] = { EGL_ATTRIBS_BASE, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_NONE, }; #ifdef EGL_OPENGL_ES3_BIT_KHR static const EGLint egl_attribs_gles3[] = { EGL_ATTRIBS_BASE, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT_KHR, EGL_NONE, }; #endif static const EGLint egl_attribs_vg[] = { EGL_ATTRIBS_BASE, EGL_RENDERABLE_TYPE, EGL_OPENVG_BIT, EGL_NONE, }; // GLES 2.0+. Don't use for any other API. const EGLint gles_context_attribs[] = { EGL_CONTEXT_CLIENT_VERSION, g_major ? (EGLint)g_major : 2, EGL_NONE }; const EGLint *attrib_ptr; switch (g_api) { case GFX_CTX_OPENGL_API: attrib_ptr = egl_attribs_gl; break; case GFX_CTX_OPENGL_ES_API: #ifdef EGL_OPENGL_ES3_BIT_KHR if (g_major >= 3) attrib_ptr = egl_attribs_gles3; else #endif attrib_ptr = egl_attribs_gles; break; case GFX_CTX_OPENVG_API: attrib_ptr = egl_attribs_vg; break; default: attrib_ptr = NULL; } // Find desired video mode, and use that. // If not fullscreen, we get desired windowed size, which is not appropriate. if ((width == 0 && height == 0) || !fullscreen) g_drm_mode = &g_connector->modes[0]; else { // Try to match g_settings.video.refresh_rate as closely as possible. // Lower resolutions tend to have multiple supported refresh rates as well. float minimum_fps_diff = 0.0f; // Find best match. for (i = 0; i < g_connector->count_modes; i++) { if (width != g_connector->modes[i].hdisplay || height != g_connector->modes[i].vdisplay) continue; if (!g_drm_mode) { g_drm_mode = &g_connector->modes[i]; minimum_fps_diff = g_drm_mode->vrefresh - g_settings.video.refresh_rate; } else { float diff = g_connector->modes[i].vrefresh - g_settings.video.refresh_rate; if (diff < minimum_fps_diff) { g_drm_mode = &g_connector->modes[i]; minimum_fps_diff = diff; } } } } if (!g_drm_mode) { RARCH_ERR("[KMS/EGL]: Did not find suitable video mode for %u x %u.\n", width, height); goto error; } g_fb_width = g_drm_mode->hdisplay; g_fb_height = g_drm_mode->vdisplay; // Create GBM surface. g_gbm_surface = gbm_surface_create(g_gbm_dev, g_fb_width, g_fb_height, GBM_FORMAT_XRGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING); if (!g_gbm_surface) { RARCH_ERR("[KMS/EGL]: Couldn't create GBM surface.\n"); goto error; } g_egl_dpy = eglGetDisplay((EGLNativeDisplayType)g_gbm_dev); if (!g_egl_dpy) { RARCH_ERR("[KMS/EGL]: Couldn't get EGL display.\n"); goto error; } EGLint major, minor; if (!eglInitialize(g_egl_dpy, &major, &minor)) goto error; EGLint n; if (!eglChooseConfig(g_egl_dpy, attrib_ptr, &g_config, 1, &n) || n != 1) goto error; g_egl_ctx = eglCreateContext(g_egl_dpy, g_config, EGL_NO_CONTEXT, (g_api == GFX_CTX_OPENGL_ES_API) ? gles_context_attribs : NULL); if (!g_egl_ctx) goto error; g_egl_surf = eglCreateWindowSurface(g_egl_dpy, g_config, (EGLNativeWindowType)g_gbm_surface, NULL); if (!g_egl_surf) goto error; if (!eglMakeCurrent(g_egl_dpy, g_egl_surf, g_egl_surf, g_egl_ctx)) goto error; glClear(GL_COLOR_BUFFER_BIT); eglSwapBuffers(g_egl_dpy, g_egl_surf); g_bo = gbm_surface_lock_front_buffer(g_gbm_surface); fb = drm_fb_get_from_bo(g_bo); ret = drmModeSetCrtc(g_drm_fd, g_crtc_id, fb->fb_id, 0, 0, &g_connector_id, 1, g_drm_mode); if (ret < 0) goto error; g_inited = true; return true; error: gfx_ctx_destroy(data); return false; }
void GlContext::create(uint32_t _width, uint32_t _height) { # if BX_PLATFORM_RPI bcm_host_init(); # endif // BX_PLATFORM_RPI m_eglLibrary = eglOpen(); if (NULL == g_platformData.context) { # if BX_PLATFORM_RPI g_platformData.ndt = EGL_DEFAULT_DISPLAY; # endif // BX_PLATFORM_RPI BX_UNUSED(_width, _height); EGLNativeDisplayType ndt = (EGLNativeDisplayType)g_platformData.ndt; EGLNativeWindowType nwh = (EGLNativeWindowType )g_platformData.nwh; # if BX_PLATFORM_WINDOWS if (NULL == g_platformData.ndt) { ndt = GetDC( (HWND)g_platformData.nwh); } # endif // BX_PLATFORM_WINDOWS m_display = eglGetDisplay(ndt); BGFX_FATAL(m_display != EGL_NO_DISPLAY, Fatal::UnableToInitialize, "Failed to create display %p", m_display); EGLint major = 0; EGLint minor = 0; EGLBoolean success = eglInitialize(m_display, &major, &minor); BGFX_FATAL(success && major >= 1 && minor >= 3, Fatal::UnableToInitialize, "Failed to initialize %d.%d", major, minor); BX_TRACE("EGL info:"); const char* clientApis = eglQueryString(m_display, EGL_CLIENT_APIS); BX_TRACE(" APIs: %s", clientApis); BX_UNUSED(clientApis); const char* vendor = eglQueryString(m_display, EGL_VENDOR); BX_TRACE(" Vendor: %s", vendor); BX_UNUSED(vendor); const char* version = eglQueryString(m_display, EGL_VERSION); BX_TRACE("Version: %s", version); BX_UNUSED(version); const char* extensions = eglQueryString(m_display, EGL_EXTENSIONS); BX_TRACE("Supported EGL extensions:"); dumpExtensions(extensions); EGLint attrs[] = { EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, # if BX_PLATFORM_ANDROID EGL_DEPTH_SIZE, 16, # else EGL_DEPTH_SIZE, 24, # endif // BX_PLATFORM_ EGL_STENCIL_SIZE, 8, EGL_NONE }; EGLint numConfig = 0; success = eglChooseConfig(m_display, attrs, &m_config, 1, &numConfig); BGFX_FATAL(success, Fatal::UnableToInitialize, "eglChooseConfig"); # if BX_PLATFORM_ANDROID EGLint format; eglGetConfigAttrib(m_display, m_config, EGL_NATIVE_VISUAL_ID, &format); ANativeWindow_setBuffersGeometry( (ANativeWindow*)g_platformData.nwh, _width, _height, format); # elif BX_PLATFORM_RPI DISPMANX_DISPLAY_HANDLE_T dispmanDisplay = vc_dispmanx_display_open(0); DISPMANX_UPDATE_HANDLE_T dispmanUpdate = vc_dispmanx_update_start(0); VC_RECT_T dstRect = { 0, 0, int32_t(_width), int32_t(_height) }; VC_RECT_T srcRect = { 0, 0, int32_t(_width) << 16, int32_t(_height) << 16 }; DISPMANX_ELEMENT_HANDLE_T dispmanElement = vc_dispmanx_element_add(dispmanUpdate , dispmanDisplay , 0 , &dstRect , 0 , &srcRect , DISPMANX_PROTECTION_NONE , NULL , NULL , DISPMANX_NO_ROTATE ); s_dispmanWindow.element = dispmanElement; s_dispmanWindow.width = _width; s_dispmanWindow.height = _height; nwh = &s_dispmanWindow; vc_dispmanx_update_submit_sync(dispmanUpdate); # endif // BX_PLATFORM_ANDROID m_surface = eglCreateWindowSurface(m_display, m_config, nwh, NULL); BGFX_FATAL(m_surface != EGL_NO_SURFACE, Fatal::UnableToInitialize, "Failed to create surface."); const bool hasEglKhrCreateContext = !!bx::findIdentifierMatch(extensions, "EGL_KHR_create_context"); const bool hasEglKhrNoError = !!bx::findIdentifierMatch(extensions, "EGL_KHR_create_context_no_error"); for (uint32_t ii = 0; ii < 2; ++ii) { bx::StaticMemoryBlockWriter writer(s_contextAttrs, sizeof(s_contextAttrs) ); EGLint flags = 0; # if BX_PLATFORM_RPI BX_UNUSED(hasEglKhrCreateContext, hasEglKhrNoError); #else if (hasEglKhrCreateContext) { bx::write(&writer, EGLint(EGL_CONTEXT_MAJOR_VERSION_KHR) ); bx::write(&writer, EGLint(BGFX_CONFIG_RENDERER_OPENGLES / 10) ); bx::write(&writer, EGLint(EGL_CONTEXT_MINOR_VERSION_KHR) ); bx::write(&writer, EGLint(BGFX_CONFIG_RENDERER_OPENGLES % 10) ); flags |= BGFX_CONFIG_DEBUG && hasEglKhrNoError ? 0 | EGL_CONTEXT_FLAG_NO_ERROR_BIT_KHR : 0 ; if (0 == ii) { flags |= BGFX_CONFIG_DEBUG ? 0 | EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR // | EGL_OPENGL_ES3_BIT_KHR : 0 ; bx::write(&writer, EGLint(EGL_CONTEXT_FLAGS_KHR) ); bx::write(&writer, flags); } } else # endif // BX_PLATFORM_RPI { bx::write(&writer, EGLint(EGL_CONTEXT_CLIENT_VERSION) ); bx::write(&writer, 2); } bx::write(&writer, EGLint(EGL_NONE) ); m_context = eglCreateContext(m_display, m_config, EGL_NO_CONTEXT, s_contextAttrs); if (NULL != m_context) { break; } BX_TRACE("Failed to create EGL context with EGL_CONTEXT_FLAGS_KHR (%08x).", flags); } BGFX_FATAL(m_context != EGL_NO_CONTEXT, Fatal::UnableToInitialize, "Failed to create context."); success = eglMakeCurrent(m_display, m_surface, m_surface, m_context); BGFX_FATAL(success, Fatal::UnableToInitialize, "Failed to set context."); m_current = NULL; eglSwapInterval(m_display, 0); } import(); }
bool GLContext::InitEGLSurface() { display_ = eglGetDisplay(EGL_DEFAULT_DISPLAY); eglInitialize(display_, 0, 0); /* * Here specify the attributes of the desired configuration. * Below, we select an EGLConfig with at least 8 bits per color * component compatible with on-screen windows */ const EGLint attribs[] = { EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, //Request opengl ES2.0 EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_BLUE_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_RED_SIZE, 8, EGL_DEPTH_SIZE, 24, EGL_SAMPLES, msaa_size_, EGL_NONE }; color_size_ = 8; depth_size_ = 24; EGLint num_configs; eglChooseConfig(display_, attribs, &config_, 1, &num_configs); if (msaa_size_ > 1 && !num_configs) { LOGW("No EGL config with MSAA"); //Fall back to non MSAA const EGLint attribs[] = { EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, //Request opengl ES2.0 EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_BLUE_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_RED_SIZE, 8, EGL_DEPTH_SIZE, 24, EGL_NONE }; msaa_size_ = 1; eglChooseConfig(display_, attribs, &config_, 1, &num_configs); } if (!num_configs) { LOGW("No 24bit depth buffer"); //Fall back to 16bit depth buffer const EGLint attribs[] = { EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, //Request opengl ES2.0 EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_BLUE_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_RED_SIZE, 8, EGL_DEPTH_SIZE, 16, EGL_NONE }; eglChooseConfig(display_, attribs, &config_, 1, &num_configs); depth_size_ = 16; } if (!num_configs) { LOGW("Unable to retrieve EGL config"); return false; } surface_ = eglCreateWindowSurface(display_, config_, window_, NULL); eglQuerySurface(display_, surface_, EGL_WIDTH, &screen_width_); eglQuerySurface(display_, surface_, EGL_HEIGHT, &screen_height_); /* 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_, config_, EGL_NATIVE_VISUAL_ID, &format); ANativeWindow_setBuffersGeometry(window_, 0, 0, format); return true; }
/*********************************************************** * Name: init_ogl * * Arguments: * CUBE_STATE_T *state - holds OGLES model info * * Description: Sets the display, OpenGL|ES context and screen stuff * * Returns: void * ***********************************************************/ static void init_ogl(CUBE_STATE_T *state) { int32_t success = 0; EGLBoolean result; EGLint num_config; static EGL_DISPMANX_WINDOW_T nativewindow; DISPMANX_ELEMENT_HANDLE_T dispman_element; DISPMANX_DISPLAY_HANDLE_T dispman_display; DISPMANX_UPDATE_HANDLE_T dispman_update; VC_RECT_T dst_rect; VC_RECT_T src_rect; static const EGLint attribute_list[] = { EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8, EGL_DEPTH_SIZE, 16, //EGL_SAMPLES, 4, EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_NONE }; EGLConfig config; // get an EGL display connection state->display = eglGetDisplay(EGL_DEFAULT_DISPLAY); assert(state->display!=EGL_NO_DISPLAY); // initialize the EGL display connection result = eglInitialize(state->display, NULL, NULL); assert(EGL_FALSE != result); // get an appropriate EGL frame buffer configuration // this uses a BRCM extension that gets the closest match, rather than standard which returns anything that matches result = eglSaneChooseConfigBRCM(state->display, attribute_list, &config, 1, &num_config); assert(EGL_FALSE != result); // create an EGL rendering context state->context = eglCreateContext(state->display, config, EGL_NO_CONTEXT, NULL); assert(state->context!=EGL_NO_CONTEXT); // create an EGL window surface success = graphics_get_display_size(0 /* LCD */, &state->screen_width, &state->screen_height); assert( success >= 0 ); dst_rect.x = 0; dst_rect.y = 0; dst_rect.width = state->screen_width; dst_rect.height = state->screen_height; src_rect.x = 0; src_rect.y = 0; src_rect.width = state->screen_width << 16; src_rect.height = state->screen_height << 16; dispman_display = vc_dispmanx_display_open( 0 /* LCD */); dispman_update = vc_dispmanx_update_start( 0 ); dispman_element = vc_dispmanx_element_add ( dispman_update, dispman_display, 0/*layer*/, &dst_rect, 0/*src*/, &src_rect, DISPMANX_PROTECTION_NONE, 0 /*alpha*/, 0/*clamp*/, 0/*transform*/); nativewindow.element = dispman_element; nativewindow.width = state->screen_width; nativewindow.height = state->screen_height; vc_dispmanx_update_submit_sync( dispman_update ); state->surface = eglCreateWindowSurface( state->display, config, &nativewindow, NULL ); assert(state->surface != EGL_NO_SURFACE); // connect the context to the surface result = eglMakeCurrent(state->display, state->surface, state->surface, state->context); assert(EGL_FALSE != result); // Set background color and clear buffers glClearColor(0.15f, 0.25f, 0.35f, 1.0f); glClear( GL_COLOR_BUFFER_BIT ); glClear( GL_DEPTH_BUFFER_BIT ); glShadeModel(GL_FLAT); // Enable back face culling. glEnable(GL_CULL_FACE); }
/// // esCreateWindow() // // width - width of window to create // height - height of window to create // flags - bitwise or of window creation flags // ES_WINDOW_ALPHA - specifies that the framebuffer should have alpha // ES_WINDOW_DEPTH - specifies that a depth buffer should be created // ES_WINDOW_STENCIL - specifies that a stencil buffer should be created // ES_WINDOW_MULTISAMPLE - specifies that a multi-sample buffer should be created // GLboolean ESUTIL_API esCreateWindow(ESContext *esContext, GLint width, GLint height, GLuint flags) { EGLint attribList[] = { EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8, EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_NONE }; if (esContext == NULL) { return GL_FALSE; } esContext->width = width; esContext->height = height; int32_t success = 0; static EGL_DISPMANX_WINDOW_T nativewindow; DISPMANX_ELEMENT_HANDLE_T dispman_element; DISPMANX_DISPLAY_HANDLE_T dispman_display; DISPMANX_UPDATE_HANDLE_T dispman_update; VC_RECT_T dst_rect; VC_RECT_T src_rect; int display_width; int display_height; // create an EGL window surface, passing context width/height success = graphics_get_display_size(0 /* LCD */, &display_width, &display_height); if (success < 0) { return EGL_FALSE; } dst_rect.x = 0; dst_rect.y = 0; dst_rect.width = display_width; dst_rect.height = display_height; src_rect.x = 0; src_rect.y = 0; src_rect.width = display_width << 16; src_rect.height = display_height << 16; dispman_display = vc_dispmanx_display_open(0 /* LCD */); dispman_update = vc_dispmanx_update_start(0); dispman_element = vc_dispmanx_element_add(dispman_update, dispman_display, 0/*layer*/, &dst_rect, 0/*src*/, &src_rect, DISPMANX_PROTECTION_NONE, 0 /*alpha*/, 0/*clamp*/, 0/*transform*/); nativewindow.element = dispman_element; nativewindow.width = display_width; nativewindow.height = display_height; vc_dispmanx_update_submit_sync(dispman_update); esContext->hWnd = &nativewindow; esContext->width = display_width; esContext->height = display_height; EGLint numConfigs; EGLint majorVersion; EGLint minorVersion; EGLDisplay display; EGLContext context; EGLSurface surface; EGLConfig config; EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; display = eglGetDisplay(EGL_DEFAULT_DISPLAY); if (display == EGL_NO_DISPLAY) { return GL_FALSE; } if (!eglInitialize(display, &majorVersion, &minorVersion)) { return GL_FALSE; } // Get configs if (!eglGetConfigs(display, NULL, 0, &numConfigs)) { return GL_FALSE; } // Choose config if (!eglChooseConfig(display, attribList, &config, 1, &numConfigs)) { return GL_FALSE; } // Create a surface surface = eglCreateWindowSurface(display, config, esContext->hWnd, NULL); if (surface == EGL_NO_SURFACE) { return GL_FALSE; } // Create a GL context context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs); if (context == EGL_NO_CONTEXT) { return GL_FALSE; } // Make the context current if (!eglMakeCurrent(display, surface, surface, context)) { return GL_FALSE; } esContext->eglDisplay = display; esContext->eglSurface = surface; esContext->eglContext = context; return GL_TRUE; }
/* Must be called in the gl thread */ GstGLWindow * gst_gl_window_new (gulong external_gl_context) { GstGLWindow *window = g_object_new (GST_GL_TYPE_WINDOW, NULL); GstGLWindowPrivate *priv = window->priv; EGLint config_attrib[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_DEPTH_SIZE, 16, EGL_NONE }; EGLint context_attrib[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; EGLint majorVersion; EGLint minorVersion; EGLint numConfigs; EGLConfig config; XSetWindowAttributes win_attr; XTextProperty text_property; XWMHints wm_hints; unsigned long mask; const gchar *title = "OpenGL renderer"; Atom wm_atoms[3]; static gint x = 0; static gint y = 0; setlocale (LC_NUMERIC, "C"); priv->x_lock = g_mutex_new (); priv->cond_send_message = g_cond_new (); priv->running = TRUE; priv->visible = FALSE; priv->parent = 0; priv->allow_extra_expose_events = TRUE; g_mutex_lock (priv->x_lock); priv->device = XOpenDisplay (priv->display_name); XSynchronize (priv->device, FALSE); g_debug ("gl device id: %ld\n", (gulong) priv->device); priv->disp_send = XOpenDisplay (priv->display_name); XSynchronize (priv->disp_send, FALSE); g_debug ("gl display sender: %ld\n", (gulong) priv->disp_send); priv->screen_num = DefaultScreen (priv->device); priv->root = RootWindow (priv->device, priv->screen_num); priv->depth = DefaultDepth (priv->device, priv->screen_num); g_debug ("gl root id: %lud\n", (gulong) priv->root); priv->device_width = DisplayWidth (priv->device, priv->screen_num); priv->device_height = DisplayHeight (priv->device, priv->screen_num); priv->visual_info = g_new0 (XVisualInfo, 1); XMatchVisualInfo (priv->device, priv->screen_num, priv->depth, TrueColor, priv->visual_info); win_attr.event_mask = StructureNotifyMask | ExposureMask | VisibilityChangeMask; win_attr.do_not_propagate_mask = NoEventMask; win_attr.background_pixmap = None; win_attr.background_pixel = 0; win_attr.border_pixel = 0; win_attr.colormap = XCreateColormap (priv->device, priv->root, priv->visual_info->visual, AllocNone); mask = CWBackPixmap | CWBorderPixel | CWColormap | CWEventMask; x += 20; y += 20; priv->internal_win_id = XCreateWindow (priv->device, priv->root, x, y, 1, 1, 0, priv->visual_info->depth, InputOutput, priv->visual_info->visual, mask, &win_attr); if (!priv->internal_win_id) { g_debug ("XCreateWindow failed\n"); goto failure; } XSync (priv->device, FALSE); XSetWindowBackgroundPixmap (priv->device, priv->internal_win_id, None); g_debug ("gl window id: %lud\n", (gulong) priv->internal_win_id); g_debug ("gl window props: x:%d y:%d\n", x, y); wm_atoms[0] = XInternAtom (priv->device, "WM_DELETE_WINDOW", True); if (wm_atoms[0] == None) g_debug ("Cannot create WM_DELETE_WINDOW\n"); wm_atoms[1] = XInternAtom (priv->device, "WM_GL_WINDOW", False); if (wm_atoms[1] == None) g_debug ("Cannot create WM_GL_WINDOW\n"); wm_atoms[2] = XInternAtom (priv->device, "WM_QUIT_LOOP", False); if (wm_atoms[2] == None) g_debug ("Cannot create WM_QUIT_LOOP\n"); XSetWMProtocols (priv->device, priv->internal_win_id, wm_atoms, 2); wm_hints.flags = StateHint; wm_hints.initial_state = NormalState; wm_hints.input = False; XStringListToTextProperty ((char **) &title, 1, &text_property); XSetWMProperties (priv->device, priv->internal_win_id, &text_property, &text_property, 0, 0, NULL, &wm_hints, NULL); XFree (text_property.value); priv->gl_display = eglGetDisplay ((EGLNativeDisplayType) priv->device); if (eglInitialize (priv->gl_display, &majorVersion, &minorVersion)) g_debug ("egl initialized: %d.%d\n", majorVersion, minorVersion); else { g_debug ("failed to initialize egl %ld, %s\n", (gulong) priv->gl_display, EGLErrorString ()); goto failure; } if (eglChooseConfig (priv->gl_display, config_attrib, &config, 1, &numConfigs)) g_debug ("config set: %ld, %ld\n", (gulong) config, (gulong) numConfigs); else { g_debug ("failed to set config %ld, %s\n", (gulong) priv->gl_display, EGLErrorString ()); goto failure; } priv->gl_surface = eglCreateWindowSurface (priv->gl_display, config, (EGLNativeWindowType) priv->internal_win_id, NULL); if (priv->gl_surface != EGL_NO_SURFACE) g_debug ("surface created: %ld\n", (gulong) priv->gl_surface); else { g_debug ("failed to create surface %ld, %ld, %ld, %s\n", (gulong) priv->gl_display, (gulong) priv->gl_surface, (gulong) priv->gl_display, EGLErrorString ()); goto failure; } g_debug ("about to create gl context\n"); priv->gl_context = eglCreateContext (priv->gl_display, config, (EGLContext) (guint) external_gl_context, context_attrib); if (priv->gl_context != EGL_NO_CONTEXT) g_debug ("gl context created: %ld\n", (gulong) priv->gl_context); else { g_debug ("failed to create glcontext %ld, %ld, %s\n", (gulong) priv->gl_context, (gulong) priv->gl_display, EGLErrorString ()); goto failure; } if (!eglMakeCurrent (priv->gl_display, priv->gl_surface, priv->gl_surface, priv->gl_context)) { g_debug ("failed to make opengl context current %ld, %s\n", (gulong) priv->gl_display, EGLErrorString ()); goto failure; } g_mutex_unlock (priv->x_lock); return window; failure: g_mutex_unlock (priv->x_lock); g_object_unref (G_OBJECT (window)); return NULL; }
//display int init_display() { int32_t success = 0; EGLBoolean result; EGLint num_config; DISPMANX_ELEMENT_HANDLE_T dispman_element; DISPMANX_DISPLAY_HANDLE_T dispman_display; DISPMANX_UPDATE_HANDLE_T dispman_update; VC_RECT_T dst_rect; VC_RECT_T src_rect; EGLConfig config; //creates problem is func eglCreateContext static const EGLint attribute_list[] = { //EGL_COLOR_BUFFER_TYPE, EGL_LUMINANCE_BUFFER, //EGL_RGB_BUFFER EGL_RED_SIZE, 8, // default is 0 EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8, //EGL_LUMINANCE_SIZE, 8, EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_NONE }; static const EGLint context_attributes[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; memset( &m_display, 0, sizeof(display) ); bcm_host_init(); // must be called be4 any gpu function is called m_display.display = eglGetDisplay(EGL_DEFAULT_DISPLAY); if (m_display.display==EGL_NO_DISPLAY) { printf("init_opengl : error in eglGetDisplay\n"); exit(-1); } result = eglInitialize(m_display.display, NULL, NULL); if (EGL_FALSE == result) { printf("init_opengl : error in initializing EGL Display\n"); exit(-1); } result = eglChooseConfig(m_display.display, attribute_list, &config, 17*sizeof(attribute_list[0]), &num_config); if (EGL_FALSE == result) { printf("init_opengl : error in getting egl frame buffer configuration\n"); exit(-1); } result = eglBindAPI(EGL_OPENGL_ES_API); if (EGL_FALSE == result) { printf("init_opengl : error in binding api\n"); exit(-1); } m_display.context = eglCreateContext(m_display.display, config, EGL_NO_CONTEXT, context_attributes); if (m_display.context==EGL_NO_CONTEXT) { printf("init_opengl : error in creating context . \n"); exit(-1); } // create an EGL window surface (video core api)(bug 1920*1080 always)(only for rpi) success = graphics_get_display_size(0 /*display number*/, &m_display.scr_width, &m_display.scr_height); if ( success < 0 ) { printf("init_opengl : error in creating EGL window surface\n"); exit(-1); } if ( glGetError() != 0 ) { printf("init_opengl : error - exit point 1.\n"); exit(-1); } dst_rect.x = 0; dst_rect.y = 0; dst_rect.width = m_display.scr_width; dst_rect.height = m_display.scr_height; src_rect.x = 0; src_rect.y = 0; src_rect.width = m_display.scr_width << 16; src_rect.height = m_display.scr_height << 16; dispman_display = vc_dispmanx_display_open( 0 /*display number*/); dispman_update = vc_dispmanx_update_start( 0 ); dispman_element = vc_dispmanx_element_add(dispman_update, dispman_display, 0/*layer*/, &dst_rect, 0/*src*/, &src_rect, DISPMANX_PROTECTION_NONE, 0 /*alpha*/, 0/*clamp*/, (DISPMANX_TRANSFORM_T)0/*transform*/); m_display.nativewindow.element = dispman_element; m_display.nativewindow.width = m_display.scr_width; m_display.nativewindow.height = m_display.scr_height; vc_dispmanx_update_submit_sync( dispman_update ); if ( glGetError() != 0 ) { printf("init_opengl : error - exit point 2.\n"); exit(-1); } m_display.surface = eglCreateWindowSurface( m_display.display, config, &m_display.nativewindow, NULL ); if(m_display.surface == EGL_NO_SURFACE) { printf("init_opengl : error could not create window surface\n"); exit(-1); } // connect the context to the surface result = eglMakeCurrent(m_display.display, m_display.surface, m_display.surface, m_display.context); if ( EGL_FALSE == result ) { printf("init_opengl : error - could not connect context to surface\n"); exit(-1); } if ( glGetError() != 0 ) { printf("init_opengl : error - exit point 3.\n"); exit(-1); } glClear( GL_COLOR_BUFFER_BIT ); glViewport ( 0, 0, m_display.scr_width, m_display.scr_height ); if ( glGetError() != 0 ) { printf("init_opengl : error - exit point 4.\n"); exit(-1); } m_display.scr_aspect_ratio = (float) ( ((float)m_display.scr_width) / ((float)m_display.scr_height) ); return(0); }
void RenderWorld::init() { const EGLint attribs[] = { EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_BLUE_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_RED_SIZE, 8, EGL_DEPTH_SIZE, 16, EGL_NONE }; EGLint format; EGLint numConfigs; EGLConfig config; mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); eglInitialize(mDisplay, 0, 0); eglChooseConfig(mDisplay, attribs, &config, 1, &numConfigs); eglGetConfigAttrib(mDisplay, config, EGL_NATIVE_VISUAL_ID, &format); ANativeWindow_setBuffersGeometry(mAndroidApp->window, 0, 0, format); mSurface = eglCreateWindowSurface(mDisplay, config, mAndroidApp->window, NULL); EGLint c_attribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; mContext = eglCreateContext(mDisplay, config, 0, c_attribs); if (eglMakeCurrent(mDisplay, mSurface, mSurface, mContext) == EGL_FALSE) throw Exception("eglMakeCurrent failed"); eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mWidth); eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mHeight); mView = Matrix::createLookAt(Vector3::ONE*25, Vector3::ZERO, Vector3::UNIT_Y); mProj = Matrix::createPerspectiveFOV(Math::PI/3.0f, (float)mWidth/(float)mHeight, 1.0f, 1000.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); glEnable(GL_CULL_FACE); glCullFace(GL_BACK); shared_ptr<ResourceLoader> vloader(new VertexShaderLoader); shared_ptr<ResourceLoader> ploader(new PixelShaderLoader); shared_ptr<ResourceLoader> mloader(new MeshLoader); shared_ptr<ResourceLoader> tloader(new TextureLoader); shared_ptr<ResourceLoader> mtloader(new MaterialLoader); shared_ptr<ResourceLoader> skloader(new SkinLoader); ResourceManager& rman = Engine::getPtr()->getResourceManager(); rman.addLoader(vloader); rman.addLoader(ploader); rman.addLoader(mloader); rman.addLoader(tloader); rman.addLoader(mtloader); rman.addLoader(skloader); VertexShader* vshader = rman.get<VertexShader>("test.vs"); PixelShader* pshader = rman.get<PixelShader>("test.fs"); mProgram = new Program; mProgram->attach(*vshader); mProgram->attach(*pshader); mProgram->link(); }
bool RendererGlAndroid::initialize( ANativeWindow *nativeWindow, RendererRef sharedRenderer ) { std::vector<EGLint> configAttribs; // OpenGL ES 3 also uses EGL_OPENGL_ES2_BIT configAttribs.push_back( EGL_RENDERABLE_TYPE ); configAttribs.push_back( EGL_OPENGL_ES2_BIT ); configAttribs.push_back( EGL_SURFACE_TYPE ); configAttribs.push_back( EGL_WINDOW_BIT ); configAttribs.push_back( EGL_RED_SIZE ); configAttribs.push_back( 8 ); configAttribs.push_back( EGL_GREEN_SIZE ); configAttribs.push_back( 8 ); configAttribs.push_back( EGL_BLUE_SIZE ); configAttribs.push_back( 8 ); configAttribs.push_back( EGL_ALPHA_SIZE ); configAttribs.push_back( EGL_DONT_CARE ); configAttribs.push_back( EGL_DEPTH_SIZE ); configAttribs.push_back( mRenderer->getOptions().getDepthBufferDepth() ); configAttribs.push_back( EGL_STENCIL_SIZE ); configAttribs.push_back( mRenderer->getOptions().getStencil() ? 8 : EGL_DONT_CARE ); configAttribs.push_back( EGL_SAMPLE_BUFFERS ); configAttribs.push_back( 1 ); configAttribs.push_back( EGL_NONE ); EGLint surfaceAttribList[] = { EGL_NONE, EGL_NONE }; mDisplay = eglGetDisplay( EGL_DEFAULT_DISPLAY ); if( mDisplay == EGL_NO_DISPLAY) { return false; } EGLint majorVersion, minorVersion; if( ! eglInitialize( mDisplay, &majorVersion, &minorVersion ) ) { return false; } eglBindAPI( EGL_OPENGL_ES_API ); if( eglGetError() != EGL_SUCCESS ) { return false; } EGLint configCount; if( ! eglChooseConfig( mDisplay, configAttribs.data(), &mConfig, 1, &configCount ) || (configCount != 1) ) { return false; } // // 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( mDisplay, mConfig, EGL_NATIVE_VISUAL_ID, &format ); ANativeWindow_setBuffersGeometry( nativeWindow, 0, 0, format ); mSurface = eglCreateWindowSurface( mDisplay, mConfig, nativeWindow, NULL ); auto err = eglGetError(); if( err != EGL_SUCCESS ) { return false; } EGLint contextAttibutes[] = { #if defined( CINDER_GL_ES_3 ) EGL_CONTEXT_CLIENT_VERSION, 3, #else EGL_CONTEXT_CLIENT_VERSION, 2, #endif EGL_NONE }; mContext = eglCreateContext( mDisplay, mConfig, NULL, contextAttibutes ); if( eglGetError() != EGL_SUCCESS ) { return false; } checkGlStatus(); eglMakeCurrent( mDisplay, mSurface, mSurface, mContext ); if( eglGetError() != EGL_SUCCESS ) { return false; } checkGlStatus(); gl::Environment::setEs(); gl::env()->initializeFunctionPointers(); checkGlStatus(); std::shared_ptr<gl::PlatformDataAndroid> platformData( new gl::PlatformDataAndroid( mContext, mDisplay, mSurface, mConfig ) ); platformData->mObjectTracking = mRenderer->getOptions().getObjectTracking(); mCinderContext = gl::Context::createFromExisting( platformData ); checkGlStatus(); // Get device screen size { EGLint width; EGLint height; eglQuerySurface( mDisplay, mSurface, EGL_WIDTH, &width ); eglQuerySurface( mDisplay, mSurface, EGL_HEIGHT, &height ); RendererGlAndroid::sSurfaceSize = ivec2( width, height ); } mCinderContext->makeCurrent(); checkGlStatus(); // NOTE: 'interval' value of 0 causes the Samsung S6 to not render correctly on startup. // eglSwapInterval( mDisplay, 1 ); checkGlStatus(); return true; }
// // CreateOpenGL // bool CreateOpenGL(int *pWidth, int *pHeight, int MSAASamples) { g_Display = eglGetDisplay(EGL_DEFAULT_DISPLAY); if (EGL_NO_DISPLAY == g_Display) { puts("Failed to retrieve EGL display connection."); return false; } eglBindAPI(EGL_OPENGL_ES_API); EGLint Error = eglGetError(); if (Error != EGL_SUCCESS) { printf("eglBindAPI(EGL_OPENGL_ES_API) failed: %s\n", StringifyEGLError(Error)); return false; } EGLint MajorNumber = 0; EGLint MinorNumber = 0; if (!eglInitialize(g_Display, &MajorNumber, &MinorNumber)) { puts("eglInitialize failed."); return false; } printf("EGL version: %d.%d\n", MajorNumber, MinorNumber); std::vector<int> Attribs; Attribs.push_back(EGL_RED_SIZE); Attribs.push_back(8); Attribs.push_back(EGL_GREEN_SIZE); Attribs.push_back(8); Attribs.push_back(EGL_BLUE_SIZE); Attribs.push_back(8); Attribs.push_back(EGL_ALPHA_SIZE); Attribs.push_back(8); Attribs.push_back(EGL_DEPTH_SIZE); Attribs.push_back(24); Attribs.push_back(EGL_STENCIL_SIZE); Attribs.push_back(8); Attribs.push_back(EGL_SURFACE_TYPE); Attribs.push_back(EGL_WINDOW_BIT); Attribs.push_back(EGL_RENDERABLE_TYPE); Attribs.push_back(EGL_OPENGL_ES2_BIT); if (MSAASamples > 1) { puts("Requesting MSAA config..."); Attribs.push_back(EGL_SAMPLE_BUFFERS); Attribs.push_back(1); Attribs.push_back(EGL_SAMPLES); Attribs.push_back(MSAASamples); } Attribs.push_back(EGL_NONE); EGLConfig Config; EGLint NumConfigs = 0; if (!eglChooseConfig(g_Display, &Attribs[0], &Config, 1, &NumConfigs) || (0 == NumConfigs)) { puts("eglChooseConfig failed."); return false; } printf("Number of framebuffer configurations that matches criteria: %d\n", NumConfigs); g_Surface = eglCreateWindowSurface(g_Display, Config, (NativeWindowType)0, NULL); if (EGL_NO_SURFACE == g_Surface) { puts("eglCreateWindowSurface() failed."); return false; } const EGLint ContextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; g_Context = eglCreateContext(g_Display, Config, EGL_NO_CONTEXT, ContextAttribs); if (EGL_NO_CONTEXT == g_Context) { EGLint Error = eglGetError(); printf("eglCreateContext failed: %s\n", StringifyEGLError(Error)); return false; } if (!eglMakeCurrent(g_Display, g_Surface, g_Surface, g_Context)) { puts("Failed to make rendering context current."); return false; } EGLint Width = 0; EGLint Height = 0; EGLint HorizontalResolution = 0; EGLint VerticalResolution = 0; EGLint PixelAspectRatio = 0; EGLint SwapBehavior = 0; EGLint MultisampleResolve = 0; eglQuerySurface(g_Display, g_Surface, EGL_WIDTH, &Width); eglQuerySurface(g_Display, g_Surface, EGL_HEIGHT, &Height); eglQuerySurface(g_Display, g_Surface, EGL_VERTICAL_RESOLUTION, &VerticalResolution); eglQuerySurface(g_Display, g_Surface, EGL_HORIZONTAL_RESOLUTION, &HorizontalResolution); eglQuerySurface(g_Display, g_Surface, EGL_PIXEL_ASPECT_RATIO, &PixelAspectRatio); eglQuerySurface(g_Display, g_Surface, EGL_SWAP_BEHAVIOR, &SwapBehavior); eglQuerySurface(g_Display, g_Surface, EGL_MULTISAMPLE_RESOLVE, &MultisampleResolve); printf( "EGL_WIDTH: %d\n" "EGL_HEIGHT: %d\n" "EGL_VERTICAL_RESOLUTION: %d\n" "EGL_HORIZONTAL_RESOLUTION: %d\n" "EGL_PIXEL_ASPECT_RATIO: %d\n" "EGL_SWAP_BEHAVIOR: %s\n" "EGL_MULTISAMPLE_RESOLVE: %s\n", Width, Height, VerticalResolution, HorizontalResolution, PixelAspectRatio, (EGL_BUFFER_PRESERVED==SwapBehavior) ? "EGL_BUFFER_PRESERVED" : "EGL_BUFFER_DESTROYED", (EGL_MULTISAMPLE_RESOLVE_DEFAULT==MultisampleResolve) ? "EGL_MULTISAMPLE_RESOLVE_DEFAULT" : "EGL_MULTISAMPLE_RESOLVE_BOX"); printf("\n"); printf("EGL_VENDOR: %s\n", eglQueryString(g_Display, EGL_VENDOR)); printf("EGL_VERSION: %s\n", eglQueryString(g_Display, EGL_VERSION)); PrintExtensions(eglQueryString(g_Display, EGL_EXTENSIONS)); PrintExtensions((const char *)glGetString(GL_EXTENSIONS)); *pWidth = Width; *pHeight = Height; // Store for HUD g_MSAASamples = MSAASamples; return true; }
static int egl_create_context(struct vo_wayland_state *wl, MPGLContext *ctx, bool enable_alpha) { EGLint major, minor, n; GL *gl = ctx->gl; const char *eglstr = ""; if (!(wl->egl_context.egl.dpy = eglGetDisplay(wl->display.display))) return -1; EGLint config_attribs[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RED_SIZE, 1, EGL_GREEN_SIZE, 1, EGL_BLUE_SIZE, 1, EGL_ALPHA_SIZE, enable_alpha, EGL_DEPTH_SIZE, 1, EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, EGL_NONE }; /* major and minor here returns the supported EGL version (e.g.: 1.4) */ if (eglInitialize(wl->egl_context.egl.dpy, &major, &minor) != EGL_TRUE) return -1; MP_VERBOSE(wl, "EGL version %d.%d\n", major, minor); EGLint context_attribs[] = { // aka EGL_CONTEXT_MAJOR_VERSION_KHR EGL_CONTEXT_CLIENT_VERSION, 3, EGL_NONE }; if (eglBindAPI(EGL_OPENGL_API) != EGL_TRUE) return -1; eglChooseConfig(wl->egl_context.egl.dpy, config_attribs, &wl->egl_context.egl.conf, 1, &n); wl->egl_context.egl.ctx = eglCreateContext(wl->egl_context.egl.dpy, wl->egl_context.egl.conf, EGL_NO_CONTEXT, context_attribs); if (!wl->egl_context.egl.ctx) { /* fallback to any GL version */ MP_WARN(wl, "can't create context for requested OpenGL version: " "fall back to any version available\n"); context_attribs[0] = EGL_NONE; wl->egl_context.egl.ctx = eglCreateContext(wl->egl_context.egl.dpy, wl->egl_context.egl.conf, EGL_NO_CONTEXT, context_attribs); if (!wl->egl_context.egl.ctx) return -1; } eglMakeCurrent(wl->egl_context.egl.dpy, NULL, NULL, wl->egl_context.egl.ctx); eglstr = eglQueryString(wl->egl_context.egl.dpy, EGL_EXTENSIONS); mpgl_load_functions(gl, (void*(*)(const GLubyte*))eglGetProcAddress, eglstr, wl->log); ctx->native_display_type = "wl"; ctx->native_display = wl->display.display; return 0; }
static bool egl_create_context(struct vo_wayland_state *wl, struct egl_context *egl_ctx, MPGLContext *ctx, bool enable_alpha) { EGLint major, minor, n; GL *gl = ctx->gl; const char *eglstr = ""; if (!(egl_ctx->egl.dpy = eglGetDisplay(wl->display->display))) return false; EGLint config_attribs[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RED_SIZE, 1, EGL_GREEN_SIZE, 1, EGL_BLUE_SIZE, 1, EGL_ALPHA_SIZE, enable_alpha, EGL_DEPTH_SIZE, 1, EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, EGL_NONE }; /* major and minor here returns the supported EGL version (e.g.: 1.4) */ if (eglInitialize(egl_ctx->egl.dpy, &major, &minor) != EGL_TRUE) return false; EGLint context_attribs[] = { EGL_CONTEXT_MAJOR_VERSION_KHR, MPGL_VER_GET_MAJOR(ctx->requested_gl_version), /* EGL_CONTEXT_MINOR_VERSION_KHR, */ /* MPGL_VER_GET_MINOR(ctx->requested_gl_version), */ /* EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR, 0, */ /* Segfaults on anything else than the major version */ EGL_NONE }; if (eglBindAPI(EGL_OPENGL_API) != EGL_TRUE) return false; eglChooseConfig(egl_ctx->egl.dpy, config_attribs, &egl_ctx->egl.conf, 1, &n); egl_ctx->egl.ctx = eglCreateContext(egl_ctx->egl.dpy, egl_ctx->egl.conf, EGL_NO_CONTEXT, context_attribs); if (!egl_ctx->egl.ctx) return false; eglMakeCurrent(egl_ctx->egl.dpy, NULL, NULL, egl_ctx->egl.ctx); eglstr = eglQueryString(egl_ctx->egl.dpy, EGL_EXTENSIONS); mpgl_load_functions(gl, (void*(*)(const GLubyte*))eglGetProcAddress, eglstr); if (!gl->BindProgram) mpgl_load_functions(gl, NULL, eglstr); return true; }
/// // CreateEGLContext() // // Creates an EGL rendering context and all associated elements // EGLBoolean CreateEGLContext ( EGLNativeWindowType hWnd, EGLDisplay* eglDisplay, EGLContext* eglContext, EGLSurface* eglSurface, EGLint attribList[]) { EGLint numConfigs; EGLint majorVersion; EGLint minorVersion; EGLDisplay display; EGLContext context; EGLSurface surface; EGLConfig config; EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE }; // Get Display display = eglGetDisplay((EGLNativeDisplayType)x_display); if ( display == EGL_NO_DISPLAY ) { return EGL_FALSE; } // Initialize EGL if ( !eglInitialize(display, &majorVersion, &minorVersion) ) { return EGL_FALSE; } // Get configs if ( !eglGetConfigs(display, NULL, 0, &numConfigs) ) { return EGL_FALSE; } // Choose config if ( !eglChooseConfig(display, attribList, &config, 1, &numConfigs) ) { return EGL_FALSE; } // Create a surface surface = eglCreateWindowSurface(display, config, (EGLNativeWindowType)hWnd, NULL); if ( surface == EGL_NO_SURFACE ) { return EGL_FALSE; } // Create a GL context context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs ); if ( context == EGL_NO_CONTEXT ) { return EGL_FALSE; } // Make the context current if ( !eglMakeCurrent(display, surface, surface, context) ) { return EGL_FALSE; } *eglDisplay = display; *eglSurface = surface; *eglContext = context; return EGL_TRUE; }
bool OpenGLESRenderContext::createRenderContext(void* Window, bool isGLES2) { #ifdef SP_USE_GLES_EGL android_app* App = static_cast<android_app*>(Window); /* Setup context attributes */ const EGLint AttribsGLES1[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_BLUE_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_RED_SIZE, 8, EGL_DEPTH_SIZE, 24, EGL_NONE }; const EGLint AttribsGLES2[] = { EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_BLUE_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_RED_SIZE, 8, EGL_DEPTH_SIZE, 24, EGL_NONE }; EGLint Format; EGLint NumConfigs; EGLConfig Config; /* Initialize display and choose context configuration */ Display_ = eglGetDisplay(EGL_DEFAULT_DISPLAY); eglInitialize(Display_, 0, 0); eglChooseConfig(Display_, isGLES2 ? AttribsGLES2 : AttribsGLES1, &Config, 1, &NumConfigs); eglGetConfigAttrib(Display_, Config, EGL_NATIVE_VISUAL_ID, &Format); ANativeWindow_setBuffersGeometry(App->window, 0, 0, Format); /* Create window surface */ Surface_ = eglCreateWindowSurface(Display_, Config, App->window, 0); /* Create render context */ if (isGLES2) { EGLint ContextAttrib[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; Context_ = eglCreateContext(Display_, Config, EGL_NO_CONTEXT, ContextAttrib); } else Context_ = eglCreateContext(Display_, Config, EGL_NO_CONTEXT, 0); /* Make current OpenGL context */ if (eglMakeCurrent(Display_, Surface_, Surface_, Context_) == EGL_FALSE) { io::Log::error("Could not activate OpenGL|ES render context"); return false; } /* Query screen resolution */ eglQuerySurface(Display_, Surface_, EGL_WIDTH, &gSharedObjects.ScreenWidth); eglQuerySurface(Display_, Surface_, EGL_HEIGHT, &gSharedObjects.ScreenHeight); #endif return true; }
int main(int argc, char *argv[]) { const int winWidth = 800, winHeight = 600; Display *x_dpy; Window win; EGLSurface egl_surf; EGLContext egl_ctx; EGLDisplay egl_dpy; char *dpyName = NULL; GLboolean printInfo = GL_FALSE; EGLint egl_major, egl_minor; const char *s; if (!InitTest(argc, argv)) { return -1; } x_dpy = XOpenDisplay(dpyName); if (!x_dpy) { printf("Error: couldn't open display %s\n", dpyName ? dpyName : getenv("DISPLAY")); return -1; } egl_dpy = eglGetDisplay(x_dpy); if (!egl_dpy) { printf("Error: eglGetDisplay() failed\n"); return -1; } if (!eglInitialize(egl_dpy, &egl_major, &egl_minor)) { printf("Error: eglInitialize() failed\n"); return -1; } s = eglQueryString(egl_dpy, EGL_VERSION); printf("EGL_VERSION = %s\n", s); s = eglQueryString(egl_dpy, EGL_VENDOR); printf("EGL_VENDOR = %s\n", s); s = eglQueryString(egl_dpy, EGL_EXTENSIONS); printf("EGL_EXTENSIONS = %s\n", s); s = eglQueryString(egl_dpy, EGL_CLIENT_APIS); printf("EGL_CLIENT_APIS = %s\n", s); make_x_window(x_dpy, egl_dpy, "OpenGL ES 2.x tri", 0, 0, winWidth, winHeight, &win, &egl_ctx, &egl_surf); XMapWindow(x_dpy, win); if (!eglMakeCurrent(egl_dpy, egl_surf, egl_surf, egl_ctx)) { printf("Error: eglMakeCurrent() failed\n"); return -1; } if (printInfo) { printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION)); printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR)); printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS)); } InitRendering(); LoadDefaultData(); // render once testHelper->app()->resizeView(winWidth, winHeight); testHelper->app()->resetView(); testHelper->app()->render(); eglSwapBuffers(egl_dpy, egl_surf); // begin the event loop if not in testing mode bool testPassed = true; if (!testHelper->isTesting()) { event_loop(x_dpy, win, egl_dpy, egl_surf); } else { testPassed = DoTesting(); } FinalizeTest(); eglDestroyContext(egl_dpy, egl_ctx); eglDestroySurface(egl_dpy, egl_surf); eglTerminate(egl_dpy); XDestroyWindow(x_dpy, win); XCloseDisplay(x_dpy); return testPassed ? 0 : 1; }
EGLDisplay cInterfaceEGLAndroid::OpenDisplay() { return eglGetDisplay(EGL_DEFAULT_DISPLAY); }
static gboolean _initialize_opengl_backend (gboolean bForceOpenGL) { gboolean bStencilBufferAvailable = TRUE, bAlphaAvailable = TRUE; // open a connection (= Display) to the graphic server EGLNativeDisplayType display_id = EGL_DEFAULT_DISPLAY; // XDisplay*, wl_display*, etc; Note: we could pass our X Display instead of making a new connection... EGLDisplay *dpy = s_eglDisplay = eglGetDisplay (display_id); int major, minor; if (! eglInitialize (dpy, &major, &minor)) { cd_warning ("Can't initialise EGL display, OpenGL will not be available"); return FALSE; } g_print ("EGL version: %d;%d\n", major, minor); // find a Frame Buffer Configuration (= Visual) that supports the features we need EGLint config_attribs[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, // EGL_OPENGL_ES2_BIT EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_DEPTH_SIZE, 1, EGL_ALPHA_SIZE, 8, EGL_STENCIL_SIZE , 1, EGL_SAMPLES, 4, EGL_SAMPLE_BUFFERS, 1, EGL_NONE }; const EGLint ctx_attribs[] = { EGL_NONE }; EGLConfig config; EGLint numConfigs=0; eglChooseConfig (dpy, config_attribs, &config, 1, &numConfigs); if (numConfigs == 0) { cd_warning ("couldn't find an appropriate visual, trying to get one without Stencil buffer\n(it may cause some little deterioration in the rendering) ..."); config_attribs[14] = EGL_NONE; bStencilBufferAvailable = FALSE; eglChooseConfig (dpy, config_attribs, &config, 1, &numConfigs); if (numConfigs == 0 && bForceOpenGL) { // if still no luck, and user insisted on using OpenGL, give up on transparency. if (bForceOpenGL) { cd_warning ("we could not get an ARGB-visual, trying to get an RGB one (fake transparency will be used in return) ..."); config_attribs[12] = None; bAlphaAvailable = FALSE; eglChooseConfig (dpy, config_attribs, &config, 1, &numConfigs); } } if (numConfigs == 0) { cd_warning ("No EGL config matching an RGBA buffer, OpenGL will not be available"); return FALSE; } } g_openglConfig.bStencilBufferAvailable = bStencilBufferAvailable; g_openglConfig.bAlphaAvailable = bAlphaAvailable; s_eglConfig = config; // create a rendering context (All other context will share ressources with it, and it will be the default context in case no other context exist) eglBindAPI (EGL_OPENGL_API); // specify the type of client API context before we create one. s_eglContext = eglCreateContext (dpy, config, EGL_NO_CONTEXT, ctx_attribs); if (s_eglContext == EGL_NO_CONTEXT) { cd_warning ("Couldn't create an EGL context, OpenGL will not be available"); return FALSE; } // check some texture abilities g_openglConfig.bTextureFromPixmapAvailable = _check_client_egl_extension ("EGL_EXT_texture_from_pixmap"); cd_debug ("bTextureFromPixmapAvailable: %d", g_openglConfig.bTextureFromPixmapAvailable); if (g_openglConfig.bTextureFromPixmapAvailable) { g_openglConfig.bindTexImage = (gpointer)eglGetProcAddress ("eglBindTexImageEXT"); g_openglConfig.releaseTexImage = (gpointer)eglGetProcAddress ("eglReleaseTexImageEXT"); g_openglConfig.bTextureFromPixmapAvailable = (g_openglConfig.bindTexImage && g_openglConfig.releaseTexImage); } return TRUE; }