Home | History | Annotate | Download | only in jni
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include <EGL/egl.h>
     18 #define EGL_EGLEXT_PROTOTYPES // for egl*Sync*
     19 #include <EGL/eglext.h>
     20 #include <cutils/log.h>
     21 #include <jni.h>
     22 #include <stdlib.h>
     23 #include <string.h>
     24 #include <sys/time.h>
     25 
     26 typedef EGLSyncKHR EGLAPIENTRY (*TypeEglCreateSyncKHR)(EGLDisplay dpy, \
     27     EGLenum type, const EGLint *attrib_list);
     28 typedef EGLBoolean EGLAPIENTRY (*TypeEglDestroySyncKHR)(EGLDisplay dpy, \
     29     EGLSyncKHR sync);
     30 typedef EGLint EGLAPIENTRY (*TypeEglClientWaitSyncKHR)(EGLDisplay dpy, \
     31     EGLSyncKHR sync, EGLint flags, EGLTimeKHR timeout);
     32 
     33 static TypeEglCreateSyncKHR mEglCreateSyncKHR = NULL;
     34 static TypeEglClientWaitSyncKHR mEglClientWaitSyncKHR = NULL;
     35 static TypeEglDestroySyncKHR mEglDestroySyncKHR = NULL;
     36 static bool mInitialized = false;
     37 static bool mEglKhrFenceSyncSupported = false;
     38 
     39 bool IsEglKHRFenceSyncSupported(EGLDisplay& display)
     40 {
     41     if (!mInitialized) {
     42         const char* eglExtensions = eglQueryString(display, EGL_EXTENSIONS);
     43         if (eglExtensions && strstr(eglExtensions, "EGL_KHR_fence_sync")) {
     44             mEglCreateSyncKHR = (TypeEglCreateSyncKHR) eglGetProcAddress("eglCreateSyncKHR");
     45             mEglClientWaitSyncKHR =
     46                     (TypeEglClientWaitSyncKHR) eglGetProcAddress("eglClientWaitSyncKHR");
     47             mEglDestroySyncKHR = (TypeEglDestroySyncKHR) eglGetProcAddress("eglDestroySyncKHR");
     48             if (mEglCreateSyncKHR != NULL && mEglClientWaitSyncKHR != NULL
     49                     && mEglDestroySyncKHR != NULL) {
     50                 mEglKhrFenceSyncSupported = true;
     51             }
     52         }
     53         mInitialized = true;
     54     }
     55     return mEglKhrFenceSyncSupported;
     56 }
     57 
     58 extern "C" JNIEXPORT \
     59 jboolean JNICALL Java_android_openglperf_cts_OpenGlPerfNative_waitForEglCompletion(JNIEnv* env,
     60         jclass clazz, jlong waitTimeInNs)
     61 {
     62     EGLDisplay dpy = eglGetCurrentDisplay();
     63     if (!IsEglKHRFenceSyncSupported(dpy)) {
     64         return JNI_FALSE;
     65     }
     66     EGLSyncKHR sync = mEglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
     67     if (sync == EGL_NO_SYNC_KHR) {
     68         return JNI_FALSE;
     69     }
     70     jboolean res = JNI_TRUE;
     71     EGLint result = mEglClientWaitSyncKHR(dpy, sync, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, waitTimeInNs);
     72     if (result == EGL_FALSE) {
     73         ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
     74         res = JNI_FALSE;
     75     } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
     76         ALOGE("FrameCompletion: timeout waiting for fence");
     77         res = JNI_FALSE;
     78     }
     79     mEglDestroySyncKHR(dpy, sync);
     80     return res;
     81 }
     82