Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * gltest1: GL Color Fliker simple.
- * based on Android's swapinterval.cpp simple.
- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/time.h>
- #include <unistd.h>
- #include <EGL/egl.h>
- #include <GLES/gl.h>
- #include <GLES/glext.h>
- #if 0
- #define WINMGR_WAYLAND
- #endif
- #ifdef WINMGR_WAYLAND
- #include <VlDisplayInfo.h>
- #include <wayland-egl-ext.h>
- #endif
- typedef int64_t nsecs_t;
- static inline nsecs_t ns2s(nsecs_t secs)
- {
- return secs/1000000000;
- }
- nsecs_t systemTime()
- {
- struct timeval t;
- t.tv_sec = t.tv_usec = 0;
- gettimeofday(&t, NULL);
- return nsecs_t(t.tv_sec)*1000000000LL + nsecs_t(t.tv_usec)*1000LL;
- }
- #define BAD_VALUE (-1)
- #define NAME_NOT_FOUND (-2)
- #define NO_ERROR (0)
- int selectConfigForPixelFormat(
- EGLDisplay dpy,
- EGLint const* attrs,
- int32_t format,
- EGLConfig* outConfig)
- {
- EGLint numConfigs = -1, n=0;
- if (!attrs)
- return BAD_VALUE;
- if (outConfig == NULL)
- return BAD_VALUE;
- // Get all the "potential match" configs...
- if (eglGetConfigs(dpy, NULL, 0, &numConfigs) == EGL_FALSE)
- return BAD_VALUE;
- EGLConfig* const configs = (EGLConfig*)malloc(sizeof(EGLConfig)*numConfigs);
- if (eglChooseConfig(dpy, attrs, configs, numConfigs, &n) == EGL_FALSE) {
- free(configs);
- return BAD_VALUE;
- }
- int i;
- EGLConfig config = NULL;
- for (i=0 ; i<n ; i++) {
- EGLint nativeVisualId = 0;
- eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
- if (nativeVisualId>0 && format == nativeVisualId) {
- config = configs[i];
- break;
- }
- }
- free(configs);
- if (i<n) {
- *outConfig = config;
- return NO_ERROR;
- }
- return NAME_NOT_FOUND;
- }
- int main(int argc, char** argv)
- {
- EGLint format = 1;
- EGLint majorVersion;
- EGLint minorVersion;
- EGLContext context;
- EGLConfig config;
- EGLSurface surface;
- EGLint w, h;
- EGLDisplay dpy;
- EGLBoolean ret;
- #ifdef WINMGR_WAYLAND
- struct wl_egl_window* window = NULL;
- displayInfo screenInfo;
- int screenId = 0;
- initDisplayInfo(screenId, &screenInfo);
- #else
- void* window = NULL;
- #endif
- EGLint configAttribs[] = {
- EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
- EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
- EGL_ALPHA_SIZE, 1,
- EGL_DEPTH_SIZE, 0,
- EGL_STENCIL_SIZE, 0,
- EGL_NONE
- };
- dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
- if (!dpy) {
- fprintf(stderr, "couldn't Get a Display\n");
- return 0;
- }
- ret = eglInitialize(dpy, &majorVersion, &minorVersion);
- if (ret == EGL_FALSE) {
- fprintf(stderr, "couldn't Initialize EGL\n");
- return 0;
- }
- ret = selectConfigForPixelFormat(dpy, configAttribs, format, &config);
- if (ret != NO_ERROR) {
- fprintf(stderr, "couldn't find an EGLConfig matching the screen format\n");
- return 0;
- }
- #ifdef WINMGR_WAYLAND
- window = wl_egl_window_create_extended(NULL, screenInfo.width,
- screenInfo.height, screenId);
- #endif
- surface = eglCreateWindowSurface(dpy, config, (EGLNativeWindowType)window, NULL);
- context = eglCreateContext(dpy, config, NULL, NULL);
- eglMakeCurrent(dpy, surface, surface, context);
- eglQuerySurface(dpy, surface, EGL_WIDTH, &w);
- eglQuerySurface(dpy, surface, EGL_HEIGHT, &h);
- printf("w=%d, h=%d\n", w, h);
- glDisable(GL_DITHER);
- glEnable(GL_BLEND);
- glViewport(0, 0, w, h);
- glOrthof(0, w, 0, h, 0, 1);
- eglSwapInterval(dpy, 1);
- int time = 20;
- printf("screen should flash red/green quickly for %d s...\n", time);
- int c = 0;
- nsecs_t start = systemTime();
- nsecs_t t;
- do {
- glClearColor(1,0,0,0);
- glClear(GL_COLOR_BUFFER_BIT);
- eglSwapBuffers(dpy, surface);
- glClearColor(0,1,0,0);
- sleep(1);
- glClear(GL_COLOR_BUFFER_BIT);
- eglSwapBuffers(dpy, surface);
- sleep(1);
- glClearColor(0,0,1,0);
- glClear(GL_COLOR_BUFFER_BIT);
- eglSwapBuffers(dpy, surface);
- t = systemTime() - start;
- c += 2;
- sleep(1);
- } while (int(ns2s(t))<=time);
- double p = (double(t) / c) / 1000000000.0;
- printf("refresh-rate is %f fps (%f ms)\n", 1.0f/p, p*1000.0);
- eglTerminate(dpy);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement