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 "jni_egl_fence.h"
     18 
     19 #include <android/log.h>
     20 #include <EGL/egl.h>
     21 #include <EGL/eglext.h>
     22 #include <string.h>
     23 
     24 #define  ALOGE(...)  __android_log_print(ANDROID_LOG_ERROR,"egl_fence",__VA_ARGS__)
     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 static TypeEglCreateSyncKHR FuncEglCreateSyncKHR = NULL;
     33 static TypeEglClientWaitSyncKHR FuncEglClientWaitSyncKHR = NULL;
     34 static TypeEglDestroySyncKHR FuncEglDestroySyncKHR = NULL;
     35 static bool initialized = false;
     36 static bool egl_khr_fence_sync_supported = false;
     37 
     38 bool IsEglKHRFenceSyncSupported() {
     39   if (!initialized) {
     40     EGLDisplay display = eglGetCurrentDisplay();
     41     const char* eglExtensions = eglQueryString(eglGetCurrentDisplay(), EGL_EXTENSIONS);
     42     if (eglExtensions && strstr(eglExtensions, "EGL_KHR_fence_sync")) {
     43       FuncEglCreateSyncKHR = (TypeEglCreateSyncKHR) eglGetProcAddress("eglCreateSyncKHR");
     44       FuncEglClientWaitSyncKHR = (TypeEglClientWaitSyncKHR) eglGetProcAddress("eglClientWaitSyncKHR");
     45       FuncEglDestroySyncKHR = (TypeEglDestroySyncKHR) eglGetProcAddress("eglDestroySyncKHR");
     46       if (FuncEglCreateSyncKHR != NULL && FuncEglClientWaitSyncKHR != NULL
     47           && FuncEglDestroySyncKHR != NULL) {
     48         egl_khr_fence_sync_supported = true;
     49       }
     50     }
     51     initialized = true;
     52   }
     53   return egl_khr_fence_sync_supported;
     54 }
     55 
     56 void
     57 Java_com_android_gallery3d_photoeditor_FilterStack_nativeEglSetFenceAndWait(JNIEnv* env,
     58                                                                           jobject thiz) {
     59   if (!IsEglKHRFenceSyncSupported()) return;
     60   EGLDisplay display = eglGetCurrentDisplay();
     61 
     62   // Create a egl fence and wait for egl to return it.
     63   // Additional reference on egl fence sync can be found in:
     64   // http://www.khronos.org/registry/vg/extensions/KHR/EGL_KHR_fence_sync.txt
     65   EGLSyncKHR fence = FuncEglCreateSyncKHR(display, EGL_SYNC_FENCE_KHR, NULL);
     66   if (fence == EGL_NO_SYNC_KHR) {
     67     return;
     68   }
     69 
     70   EGLint result = FuncEglClientWaitSyncKHR(display,
     71                                        fence,
     72                                        EGL_SYNC_FLUSH_COMMANDS_BIT_KHR,
     73                                        EGL_FOREVER_KHR);
     74   if (result == EGL_FALSE) {
     75     ALOGE("EGL FENCE: error waiting for fence: %#x", eglGetError());
     76   }
     77   FuncEglDestroySyncKHR(display, fence);
     78 }
     79