Beispiel #1
0
int main(int argc, char *argv[])
{

    argc = argc;
    argv = argv;

    if (!initGraphics())
    {
        fprintf(stderr, "Graphics initialization failed.\n");
        return EXIT_FAILURE;
    }

    appInit();

    while (gAppAlive)
    {
        struct timeval timeNow;

        while (XPending(sDisplay))
        {
            XEvent ev;
            XNextEvent(sDisplay, &ev);
            switch (ev.type)
            {
            case KeyPress:
                {
                    unsigned int keycode, keysym;
                    keycode = ((XKeyEvent *)&ev)->keycode;
                    keysym = XKeycodeToKeysym(sDisplay, keycode, 0);
                    if (keysym == XK_Return || keysym == XK_Escape)
                        gAppAlive = 0;
                }
                break;
            }
        }

        if (gAppAlive)
        {
            gettimeofday(&timeNow, NULL);
            appRender(timeNow.tv_sec * 1000 + timeNow.tv_usec / 1000,
                      sWindowWidth, sWindowHeight);
            checkGLErrors();
            eglSwapBuffers(sEglDisplay, sEglSurface);
            checkEGLErrors();
        }
    }

    appDeinit();
    deinitGraphics();

    return EXIT_SUCCESS;
}
Beispiel #2
0
static int initGraphics()
{
    static const EGLint configAttribs[] =
    {
#if (WINDOW_BPP == 16)
        EGL_RED_SIZE,       5,
        EGL_GREEN_SIZE,     5,
        EGL_BLUE_SIZE,      5,
#elif (WINDOW_BPP == 32)
        EGL_RED_SIZE,       8,
        EGL_GREEN_SIZE,     8,
        EGL_BLUE_SIZE,      8,
#else
#error WINDOW_BPP must be 16 or 32
#endif
        EGL_DEPTH_SIZE,     16,
        EGL_ALPHA_SIZE,     EGL_DONT_CARE,
        EGL_STENCIL_SIZE,   EGL_DONT_CARE,
        EGL_SURFACE_TYPE,   EGL_WINDOW_BIT,
        EGL_NONE
    };
    EGLBoolean success;
    EGLint numConfigs;
    EGLint majorVersion;
    EGLint minorVersion;

    int importGLResult;
    importGLResult = importGLInit();
    if (!importGLResult)
        return 0;

    sDisplay = XOpenDisplay(NULL);

    sEglDisplay = eglGetDisplay(sDisplay);
    success = eglInitialize(sEglDisplay, &majorVersion, &minorVersion);
    if (success != EGL_FALSE)
        success = eglGetConfigs(sEglDisplay, NULL, 0, &numConfigs);
    if (success != EGL_FALSE)
        success = eglChooseConfig(sEglDisplay, configAttribs,
                                  &sEglConfig, 1, &numConfigs);
    if (success != EGL_FALSE)
    {
        sEglContext = eglCreateContext(sEglDisplay, sEglConfig, NULL, NULL);
        if (sEglContext == EGL_NO_CONTEXT)
            success = EGL_FALSE;
    }
    if (success != EGL_FALSE)
    {
        XSetWindowAttributes swa;
        XVisualInfo *vi, tmp;
        XSizeHints sh;
        int n;
        EGLint vid;

        eglGetConfigAttrib(sEglDisplay, sEglConfig,
                           EGL_NATIVE_VISUAL_ID, &vid);
        tmp.visualid = vid;
        vi = XGetVisualInfo(sDisplay, VisualIDMask, &tmp, &n);
        swa.colormap = XCreateColormap(sDisplay,
                                       RootWindow(sDisplay, vi->screen),
                                       vi->visual, AllocNone);
        sh.flags = PMinSize | PMaxSize;
        sh.min_width = sh.max_width = sWindowWidth;
        sh.min_height = sh.max_height = sWindowHeight;
        swa.border_pixel = 0;
        swa.event_mask = ExposureMask | StructureNotifyMask |
                         KeyPressMask | ButtonPressMask | ButtonReleaseMask;
        sWindow = XCreateWindow(sDisplay, RootWindow(sDisplay, vi->screen),
                                0, 0, sWindowWidth, sWindowHeight,
                                0, vi->depth, InputOutput, vi->visual,
                                CWBorderPixel | CWColormap | CWEventMask,
                                &swa);
        XMapWindow(sDisplay, sWindow);
        XSetStandardProperties(sDisplay, sWindow, sAppName, sAppName,
                               None, (void *)0, 0, &sh);
    }
    if (success != EGL_FALSE)
    {
        sEglSurface = eglCreateWindowSurface(sEglDisplay, sEglConfig,
                                             (NativeWindowType)sWindow, NULL);
        if (sEglSurface == EGL_NO_SURFACE)
            success = EGL_FALSE;
    }
    if (success != EGL_FALSE)
        success = eglMakeCurrent(sEglDisplay, sEglSurface,
                                 sEglSurface, sEglContext);

    if (success == EGL_FALSE)
        checkEGLErrors();

    return success != EGL_FALSE;
}
bool OverlayComposer::initEGL()
{

    if (mWindow == NULL)
    {
        ALOGE("NativeWindow is NULL");
        return false;
    }

    int format;
    ANativeWindow const * const window = mWindow.get();
    window->query(window, NATIVE_WINDOW_FORMAT, &format);

    EGLint w, h, dummy;
    EGLint numConfigs=0;
    EGLSurface surface;
    EGLContext context;
    EGLBoolean result;
    status_t err;

    // initialize EGL
    EGLint attribs[] = {
            EGL_SURFACE_TYPE,       EGL_WINDOW_BIT,
            EGL_NONE,               0,
            EGL_NONE
    };


    EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    checkEGLErrors("eglGetDisplay");
    eglInitialize(display, NULL, NULL);
    eglGetConfigs(display, NULL, 0, &numConfigs);

    EGLConfig config = NULL;
    err = selectConfigForPixelFormat(display, attribs, format, &config);
    ALOGE_IF(err, "couldn't find an EGLConfig matching the screen format");

    EGLint r,g,b,a;
    eglGetConfigAttrib(display, config, EGL_RED_SIZE,   &r);
    eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
    eglGetConfigAttrib(display, config, EGL_BLUE_SIZE,  &b);
    eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);

    //if (window->isUpdateOnDemand()) {
    //    mFlags |= PARTIAL_UPDATES;
    //}

    //if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
    //   if (dummy == EGL_SLOW_CONFIG)
    //       mFlags |= SLOW_CONFIG;
    //}


     /*
     * Create our main surface
     */

    surface = eglCreateWindowSurface(display, config, mWindow.get(), NULL);
    checkEGLErrors("eglCreateWindowSurface");

    //if (mFlags & PARTIAL_UPDATES) {
    //    // if we have partial updates, we definitely don't need to
    //    // preserve the backbuffer, which may be costly.
    //    eglSurfaceAttrib(display, surface,
    //            EGL_SWAP_BEHAVIOR, EGL_BUFFER_DESTROYED);
    //}

    /*
     * Create our OpenGL ES context
     */
//#define EGL_IMG_context_priority
//#define HAS_CONTEXT_PRIORITY
    EGLint contextAttributes[] = {
#ifdef EGL_IMG_context_priority
#ifdef HAS_CONTEXT_PRIORITY
#warning "using EGL_IMG_context_priority"
        EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
#endif
#endif
        EGL_NONE, EGL_NONE
    };
    context = eglCreateContext(display, config, NULL, contextAttributes);
    checkEGLErrors("eglCreateContext");
    mDisplay = display;
    mConfig  = config;
    mSurface = surface;
    mContext = context;
    //mFormat  = ;
    //mPageFlipCount = 0;

    /*
     * Gather OpenGL ES extensions
     */

    result = eglMakeCurrent(display, surface, surface, context);
    checkEGLErrors("eglMakeCurrent");
    if (!result) {
        ALOGE("Couldn't create a working GLES context. check logs. exiting...");
        return false;
    }

    //GLExtensions& extensions(GLExtensions::getInstance());
    //extensions.initWithGLStrings(
    //        glGetString(GL_VENDOR),
    //        glGetString(GL_RENDERER),
    //        glGetString(GL_VERSION),
    //        glGetString(GL_EXTENSIONS),
    //        eglQueryString(display, EGL_VENDOR),
    //        eglQueryString(display, EGL_VERSION),
    //        eglQueryString(display, EGL_EXTENSIONS));

    //glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
    //glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);

    //ALOGI("EGL informations:");
    //ALOGI("# of configs : %d", numConfigs);
    //ALOGI("vendor    : %s", extensions.getEglVendor());
    //ALOGI("version   : %s", extensions.getEglVersion());
    //ALOGI("extensions: %s", extensions.getEglExtension());
    //ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
    //ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);

    //ALOGI("OpenGL informations:");
    //ALOGI("vendor    : %s", extensions.getVendor());
    //ALOGI("renderer  : %s", extensions.getRenderer());
    //ALOGI("version   : %s", extensions.getVersion());
    //ALOGI("extensions: %s", extensions.getExtension());
    //ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
    //ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
    //ALOGI("flags = %08x", mFlags);

    // Unbind the context from this thread
    //eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);


    return true;
}