Example #1
0
int main() {
    EGLContextType *context = NULL;
    // X display and window
    Display * x11Display = XOpenDisplay(NULL);
    CHECK_HANDLE_RET(x11Display, NULL, "XOpenDisplay", -1);
    Window x11RootWindow = DefaultRootWindow(x11Display);

    // may remove the attrib
    XSetWindowAttributes x11WindowAttrib;
    x11WindowAttrib.event_mask = ExposureMask | KeyPressMask;

    // create with video size, simplify it
    Window x11Window = XCreateWindow(x11Display, x11RootWindow,
        0, 0, 800, 600, 0, CopyFromParent, InputOutput,
        CopyFromParent, CWEventMask, &x11WindowAttrib);
    XMapWindow(x11Display, x11Window);
    XSync(x11Display, 0);

    context = eglInit(x11Display, x11Window, 0, 0);
    // GLuint textureId = createTestTexture();
    XID pixmap = createPixmap(x11Display, x11Window);
    GLuint textureId = createTextureFromPixmap(context, pixmap);
    drawTextures(context, &textureId, 1);

    EGLBoolean running = EGL_TRUE;
    while (running) {
        XEvent x_event;
        XNextEvent(x11Display, &x_event);
        switch (x_event.type) {
        case Expose:
            glClear(GL_COLOR_BUFFER_BIT
                | GL_DEPTH_BUFFER_BIT
            );
            drawTextures(context, &textureId, 1);
            break;
        case KeyPress:
            running = EGL_FALSE;
            break;
        default:
            break;
        }
    }

    XUnmapWindow(x11Display, x11Window);
    XDestroyWindow(x11Display, x11Window);
    XCloseDisplay(x11Display);
    INFO("exit successfully");
    return 0;
}
Example #2
0
EGLContextType *eglInit(Display *x11Display, XID x11Window, uint32_t fourcc)
{
    EGLContextType *context = NULL;
    GLProgram *glProgram = NULL;

    context = calloc(1, sizeof(EGLContextType));
    EGLDisplay eglDisplay = eglGetDisplay(x11Display);
    if (eglDisplay == EGL_NO_DISPLAY) {
        ERROR("eglGetDisplay fail");
    }
    CHECK_HANDLE_RET(eglDisplay, EGL_NO_DISPLAY, "eglGetDisplay", NULL);
    context->eglContext.display = eglDisplay;

    EGLint major, minor;
    EGLBoolean result = eglInitialize(eglDisplay, &major, &minor);
    if (result != EGL_TRUE) {
        ERROR("eglInitialize fail");
    }
    EGL_CHECK_RESULT_RET(result, "eglInitialize", NULL);

    // eglBindAPI(EGL_OPENGL_ES_API); // gles is the default one

    EGLint const eglConfigAttribs[] = {
         EGL_RED_SIZE, 8,
         EGL_GREEN_SIZE, 8,
         EGL_BLUE_SIZE, 8,
         EGL_ALPHA_SIZE, 8,
         EGL_DEPTH_SIZE, 8,
         EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
         EGL_NONE
    };
    EGLConfig eglConfig;
    EGLint eglConfigCount;
    result = eglChooseConfig(eglDisplay, eglConfigAttribs, &eglConfig, 1, &eglConfigCount);
    EGL_CHECK_RESULT_RET(result, "eglChooseConfig", NULL);
    context->eglContext.config = eglConfig;

    EGLSurface eglSurface = eglCreateWindowSurface(eglDisplay, eglConfig, x11Window, NULL);
    CHECK_HANDLE_RET(eglSurface, EGL_NO_SURFACE, "eglCreateWindowSurface", NULL);
    context->eglContext.surface = eglSurface;

    EGLint const eglContextAttribs[] = {
        EGL_CONTEXT_CLIENT_VERSION, 2,
        EGL_NONE
    };
    EGLContext eglContext = eglCreateContext(eglDisplay, eglConfig, EGL_NO_CONTEXT, eglContextAttribs);
    CHECK_HANDLE_RET(eglContext, EGL_NO_CONTEXT, "eglCreateContext", NULL);
    context->eglContext.context = eglContext;

    result = eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext);
    EGL_CHECK_RESULT_RET(result, "eglMakeCurrent", NULL);

    const unsigned char* glVersion = glGetString(GL_VERSION);
    INFO("Runing GL version: %s, please make sure it support GL 2.0 API", glVersion);

    // clear to middle blue
    glClearColor(0.0, 0.0, 0.5, 0.0);
    glEnable(GL_DEPTH_TEST);
    glClearDepthf(1.0f);
    {
        int width, height;
        Window root;
        int x, y, borderWidth, depth;
        XGetGeometry(x11Display, x11Window, &root, &x, &y, &width, &height, &borderWidth, &depth);
        glViewport(0, 0, width, height);
    }
    glProgram = createShaders(vertexShaderText_rgba, fragShaderText_rgba, 1);
    CHECK_HANDLE_RET(glProgram, NULL, "createShaders", NULL);
    context->glProgram = glProgram;

    return context;
}