Tuesday, May 24, 2011

eglCreateContext example c c++ java


eglCreateContext - create a new EGL rendering context

C Specification

EGLContext eglCreateContext(EGLDisplay display,
    EGLConfig config,
    EGLContext share_context,
    EGLint const * attrib_list)


Specifies the EGL display connection.
Specifies the EGL frame buffer configuration that defines the frame buffer resource available to the rendering context.
Specifies the EGL rendering context with which to share texture objects. EGL_NO_CONTEXT indicates that no sharing is to take place.
Specifies attributes.


eglCreateContext creates an EGL rendering context and returns its handle. This context can be used to render into an EGL drawing surface. If eglCreateContext fails to create a rendering context, EGL_NO_CONTEXT is returned.
If share_context is not EGL_NO_CONTEXT, then all texture objects except object 0, are shared by context share_context and by the newly created context. An arbitrary number of rendering contexts can share a single texture object space. However, all rendering contexts that share a single texture object space must themselves exist in the same address space. Two rendering contexts share an address space if both are owned by a single process.

Notes on eglCreateContext

A process is a single execution environment, implemented in a single address space, consisting of one or more threads.
A thread is one of a set of subprocesses that share a single address space, but maintain separate program counters, stack spaces, and other related global data. A thread is the only member of its subprocess group is equivalent to a process.


EGL_NO_CONTEXT is returned if creation of the context fails.
EGL_BAD_DISPLAY is generated if display is not an EGL display connection.
EGL_NOT_INITIALIZED is generated if display has not been initialized.
EGL_BAD_CONFIG is generated if config is not an EGL frame buffer configuration.
EGL_BAD_CONTEXT is generated if share_context is not an EGL rendering context and is not EGL_NO_CONTEXT.
EGL_BAD_ATTRIBUTE is generated if attrib_list contains an invalid context attribute or if an attribute is not recognized or out of range.
EGL_BAD_ALLOC is generated if there are not enough resources to allocate the new context.


Copyright © 2003 Silicon Graphics, Inc.
This document is licensed under the SGI Free Software B License. For details, see http://oss.sgi.com/projects/FreeB/.

See Also

eglDestroyContext, eglChooseConfig, eglGetConfigs, eglMakeCurrent

Examples of eglCreateContext

void CXXXView::OnInitialUpdate()
 // TODO: Add your specialized code here and/or call the base class
 if (m_eglDisplay != EGL_NO_DISPLAY)
 const EGLint attrs[] = {
 EGLint numConfig =0;
 m_eglDisplay = eglGetDisplay(GetDC()->GetSafeHdc());
 if (m_eglDisplay == EGL_NO_DISPLAY)
  if ((m_eglDisplay = eglGetDisplay((EGLNativeDisplayType) EGL_DEFAULT_DISPLAY)) == EGL_NO_DISPLAY)
 // Initialize the display
 EGLint major = 0;
 EGLint minor = 0;
 if (!eglInitialize(m_eglDisplay, &major, &minor))
 if (major < 1 || minor < 3)
  // Does not support EGL 1.3
  printf("System does not support at least EGL 1.3 \n");
 EGLConfig eglConfig;
 // Obtain the first configuration with a depth buffer
 if (!eglChooseConfig(m_eglDisplay, attrs, &eglConfig, 1, &numConfig))
 // Create a surface for the main window
 if ((m_eglSurface = eglCreateWindowSurface(m_eglDisplay, eglConfig, (EGLNativeWindowType) GetSafeHwnd(), NULL)) == EGL_NO_SURFACE)
 // Bind the API (It could be OpenGLES or OpenVG)
 EGLint ai32ContextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
 // Create an OpenGL ES context
 if ((m_eglContext = eglCreateContext(m_eglDisplay, eglConfig, EGL_NO_CONTEXT, ai32ContextAttribs)) == EGL_NO_CONTEXT)
 // Make the context and surface current
 if (!eglMakeCurrent(m_eglDisplay, m_eglSurface, m_eglSurface, m_eglContext))
 glClearColor(0.5f, 0.5f, 0.5f, 0.0f);
void CXXXView::PostNcDestroy()
 // TODO: Add your specialized code here and/or call the base class
 eglDestroyContext(m_eglDisplay, m_eglContext);
 eglDestroySurface(m_eglDisplay, m_eglSurface);
void CXXXView::onDraw(void)
 IGameImporter * gameImport = NULL;
 auto pDoc = GetDocument();
 if (pDoc && pDoc->IsKindOf(RUNTIME_CLASS(CXXXDoc)))
  gameImport = dynamic_cast<cxxxdoc *=""> (pDoc)->getGameImport();
 eglSwapBuffers(m_eglDisplay, m_eglSurface);
BOOL CXXXView::OnEraseBkgnd(CDC* pDC)
 // TODO: Add your message handler code here and/or call default
 /// return CView::OnEraseBkgnd(pDC);
 return FALSE;
void CXXXView::OnSize(UINT nType, int cx, int cy)
 CView::OnSize(nType, cx, cy);
 // TODO: Add your message handler code here
 glViewport(0, 0, cx, cy);
/// CXXXApp.cpp
BOOL CXXXApp::OnIdle(LONG lCount)
 // TODO: Add your specialized code here and/or call the base class
// return CWinAppEx::OnIdle(lCount);
 CMainFrame * pFrame = (CMainFrame *) AfxGetMainWnd();
 CView * pView = pFrame->GetActiveView();
 if (pView && pView->IsKindOf(RUNTIME_CLASS(CXXXView)))
  dynamic_cast<cxxxview *=""> (pView)->onDraw();
 return TRUE;
/// CViewTree.h
class CViewTree : public CTreeCtrl
 afx_msg void OnTvnSelchanged(NMHDR *pNMHDR, LRESULT *pResult);
/// CViewTree.cpp
void CViewTree::OnTvnSelchanged(NMHDR *pNMHDR, LRESULT *pResult)
 LPNMTREEVIEW pNMTreeView = reinterpret_cast<lpnmtreeview>(pNMHDR);
 // TODO: Add your control notification handler code here
 GetParent()->SendNotifyMessage(WM_TVN_SELCHANGED, (WPARAM) pNMTreeView, (LPARAM) pResult);
 *pResult = 0;