Imagination PowerVR SDK Blog

eglInitialize() Fails in X11. Always!


Hi everybody, I’m trying hard to solve this error but I can’t yet (for 5 months!).

The problem is when I try to use the X11 values, eg. the X11 visual Info, screen, display, WndHandle, etc, from X11 server to initialize the eglDisplay and then X11 window Handle to create the egl window surface.

I get succesive errors. First in eglInitialize(eglDisplay , Mayor , Minor), with eglDisplay obtained from eglGetDisplay( (EGLNativeDisplayType) x11Display), then in the eglCreateWindowSurface() with the x11WndHandle.

In order to summarize the errors, when i try to initialize the EGL display, the only parameter that can be seted in order to launch the app is set eglGetDisplay((EGLNativeDisplayType) EGL_DEFAULT_DISPLAY), after then I can initialize correctly the display. But the error goes to the eglCreateWindowSurface(), especifically to the window Handle that has to be seted to NULL.

Here below I’ve posted the code (it’s from the trainig course of the SDK “OGLES_HelloTriangle_LinuxX11.cpp”), with the conflicting lines in Bold, and after those, the commented lines in wich the program run correctly. The problem is that I need to run the program with a Window system, so I need to solve this!

I have to mention that the original code run in Linux machines with the OGLES Simulator (but not in the real embedded App)

// Initializes the display and screen

     x11Display = XOpenDisplay( 0 ); // the default display

     if (!x11Display)


          printf(“Error: Unable to open X displayn”);

          goto cleanup;


     x11Screen = XDefaultScreen( x11Display );

     // Gets the window parameters

     sRootWindow = RootWindow(x11Display, x11Screen);

     i32Depth = DefaultDepth(x11Display, x11Screen);

     x11Visual = new XVisualInfo;

     XMatchVisualInfo( x11Display, x11Screen, i32Depth, TrueColor, x11Visual);

     if (!x11Visual)


          printf(“Error: Unable to acquire visualn”);

          goto cleanup;


    x11Colormap = XCreateColormap( x11Display, sRootWindow, x11Visual->visual, AllocNone );

    sWA.colormap = x11Colormap;

    // Add to these for handling other events

    sWA.event_mask = StructureNotifyMask | ExposureMask | ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask;

    ui32Mask = CWBackPixel | CWBorderPixel | CWEventMask | CWColormap;

     i32Width = WINDOW_WIDTH < XDisplayWidth(x11Display, x11Screen) ? WINDOW_WIDTH : XDisplayWidth(x11Display, x11Screen);

     i32Height = WINDOW_HEIGHT < XDisplayHeight(x11Display,x11Screen) ? WINDOW_HEIGHT: XDisplayHeight(x11Display,x11Screen);

     // Creates the X11 window

    x11Window = XCreateWindow( x11Display, RootWindow(x11Display, x11Screen), 0, 0, i32Width, i32Height,

                                         0, CopyFromParent, InputOutput, CopyFromParent, ui32Mask, &sWA);

     XMapWindow(x11Display, x11Window);


//Up to here all it’s fine


          Step 1 - Get the default display.

          EGL uses the concept of a “display” which in most environments

          corresponds to a single physical screen. Since we usually want

          to draw to the main screen or only have a single screen to begin

          with, we let EGL pick the default display.

          Querying other displays is platform specific.


     eglDisplay = eglGetDisplay((EGLNativeDisplayType)x11Display);

//The problems is solved when I set

//eglDisplay = eglGetDisplay((EGLNativeDisplayType)EGL_DEFAULT_DISPLAY);


          Step 2 - Initialize EGL.

          EGL has to be initialized with the display obtained in the

          previous step. We cannot use other EGL functions except

          eglGetDisplay and eglGetError before eglInitialize has been


          If we’re not interested in the EGL version number we can just

          pass NULL for the second and third parameters.


     EGLint iMajorVersion, iMinorVersion;

     if (!eglInitialize(eglDisplay, &iMajorVersion, &iMinorVersion)) //HERE IT’S THE MAIN ERROR


//With the above correction this is runnig well.

          printf(“Error: eglInitialize() failed.n”);

          goto cleanup;



          Step 3 - Make OpenGL ES the current API.

          EGL provides ways to set up OpenGL ES and OpenVG contexts

          (and possibly other graphics APIs in the future), so we need

          to specify the “current API”.



     if (!TestEGLError(“eglBindAPI”))


          goto cleanup;



          Step 4 - Specify the required configuration attributes.

          An EGL “configuration” describes the pixel format and type of

          surfaces that can be used for drawing.

          For now we just want to use a 16 bit RGB surface that is a

          Window surface, i.e. it will be visible on screen. The list

          has to contain key/value pairs, terminated with EGL_NONE.


     EGLint pi32ConfigAttribs[5];

     pi32ConfigAttribs[0] = EGL_SURFACE_TYPE;

     pi32ConfigAttribs[1] = EGL_WINDOW_BIT;

     pi32ConfigAttribs[2] = EGL_RENDERABLE_TYPE;

     pi32ConfigAttribs[3] = EGL_OPENGL_ES2_BIT;

     pi32ConfigAttribs[4] = EGL_NONE;


          Step 5 - Find a config that matches all requirements.

          eglChooseConfig provides a list of all available configurations

          that meet or exceed the requirements given as the second

          argument. In most cases we just want the first config that meets

          all criteria, so we can limit the number of configs returned to 1.


     EGLint iConfigs;

     if (!eglChooseConfig(eglDisplay, pi32ConfigAttribs, &eglConfig, 1, &iConfigs) || (iConfigs != 1))


          printf(“Error: eglChooseConfig() failed.n”);

          goto cleanup;



          Step 6 - Create a surface to draw to.

          Use the config picked in the previous step and the native window

          handle when available to create a window surface. A window surface

          is one that will be visible on screen inside the native display (or

          fullscreen if there is no windowing system).

          Pixmaps and pbuffers are surfaces which only exist in off-screen



     eglSurface = eglCreateWindowSurface(eglDisplay, eglConfig, (EGLNativeWindowType)x11Window, NULL);

// the problem is solved with

//eglSurface = eglCreateWindowSurface(eglDisplay, eglConfig, NULL , NULL);

// and with the above correction in eglGetDisplay()

    if (!TestEGLError(“eglCreateWindowSurface”))


          goto cleanup;



          Step 7 - Create a context.


     eglContext = eglCreateContext(eglDisplay, eglConfig, NULL, ai32ContextAttribs);

     if (!TestEGLError(“eglCreateContext”))


          goto cleanup;


aRc2011-12-13 20:31:26