VisionWorks Toolkit Reference

December 18, 2015 | 1.2 Release

 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Initializing EGL Resources

This section demonstrates how to initialize EGL resources and create EGL streams.

Most EGL calls require an EGLDisplay parameter, which represents the abstract display on which graphics are drawn.

Note
To access the EGL API, applications must include the <EGL/egl.h> header.
To initialize EGL and create EGL streams
  1. Obtain an instance of EGL display, which can be either an imported native display or default display. This procedure uses the default display:

    EGLDisplay initializeEglDisplay()
    {
    // Obtain the EGL display
    EGLDisplay eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    if (eglDisplay == EGL_NO_DISPLAY)
    {
    printf("EGL failed to obtain display.\n");
    }
  2. Initialize the EGL display:

    // Initialize EGL
    EGLint major, minor;
    if (!eglInitialize(eglDisplay, &major, &minor))
    {
    printf("EGL failed to initialize.\n");
    eglDisplay = EGL_NO_DISPLAY;
    }
    else
    {
    printf("EGL API: %d.%d\n", major, minor);
    }
    return eglDisplay;
    }

    At this point EGL display instance has been prepared; it will be used in all EGL API calls.

  3. After obtaining resources to initialize an EGL stream instance, applications must setup KHR extensions to create a KHR EGL stream:

    #if !defined EGL_KHR_stream || !defined EGL_KHR_stream_fifo || !defined EGL_KHR_stream_consumer_gltexture
    # error "EGL_KHR_stream extensions are not supported!"
    #endif
    #define EXTENSION_LIST(T) \
    T( PFNEGLCREATESTREAMKHRPROC, eglCreateStreamKHR ) \
    T( PFNEGLDESTROYSTREAMKHRPROC, eglDestroyStreamKHR ) \
    T( PFNEGLQUERYSTREAMKHRPROC, eglQueryStreamKHR ) \
    T( PFNEGLSTREAMATTRIBKHRPROC, eglStreamAttribKHR )
    #define EXTLST_DECL(tx, x) static tx x = NULL;
    #define EXTLST_ENTRY(tx, x) { (extlst_fnptr_t *)&x, #x },
    EXTENSION_LIST(EXTLST_DECL)
    typedef void (*extlst_fnptr_t)(void);
    static struct
    {
    extlst_fnptr_t *fnptr;
    char const *name;
    } extensionList[] = { EXTENSION_LIST(EXTLST_ENTRY) };
    bool setupExtensions()
    {
    for (size_t i = 0; i < (sizeof(extensionList) / sizeof(*extensionList)); i++)
    {
    *extensionList[i].fnptr = eglGetProcAddress(extensionList[i].name);
    if (*extensionList[i].fnptr == NULL)
    {
    printf("Couldn't get address of %s()\n", extensionList[i].name);
    return false;
    }
    }
    return true;
    }
    Note
    To access EGL extensions applications must include <EGL/eglext.h>.
  4. Create the EGL stream in one of the following 2 modes:

    • Mailbox mode:
      • Producers empty the mailbox when new frames become available.
      • Only the latest frame is available.
      • Consumers have no direct control over which frames get dropped.
    • FIFO mode:
      • Order dependent.
      • No frames get dropped automatically.
      • Slow consumers can block producers.

    To prevent frame dropping this procedure creates an EGL stream in FIFO mode.

    EGLStreamKHR initializeEGLStream(EGLDisplay eglDisplay)
    {
    // Setup KHR stream extensions
    if (!setupExtensions())
    {
    return EGL_NO_STREAM_KHR;
    }
    EGLint fifo_length = 4;
    EGLStreamKHR eglStream = EGL_NO_STREAM_KHR;
    // Fill KHR EGL stream properties
    EGLint streamAttrFIFOMode[] =
    {
    EGL_STREAM_FIFO_LENGTH_KHR, fifo_length,
    EGL_NONE
    };
    // Create KHR EGL stream
    eglStream = eglCreateStreamKHR(eglDisplay, streamAttrFIFOMode);
    if (eglStream == EGL_NO_STREAM_KHR)
    {
    printf("Couldn't create EGL stream.\n");
    eglStream = EGL_NO_STREAM_KHR;
    }
    else
    {
    // setup stream attributes useful for EGL producer
    if (!eglStreamAttribKHR(eglDisplay, eglStream, EGL_CONSUMER_LATENCY_USEC_KHR, 16000))
    {
    printf("eglStreamAttribKHR EGL_CONSUMER_LATENCY_USEC_KHR failed\n");
    }
    if (!eglStreamAttribKHR(eglDisplay, eglStream, EGL_CONSUMER_ACQUIRE_TIMEOUT_USEC_KHR, 16000))
    {
    printf("eglStreamAttribKHR EGL_CONSUMER_ACQUIRE_TIMEOUT_USEC_KHR failed\n");
    }
    }
    return eglStream;
    }

    After creating the EGL stream, a producer (like NvMedia) and a consumer (like CUDA) must be connected to it. For more information, see Connecting CUDA Consumers to EGL Streams.