Ruei-sung Lin | e5c8ed7 | 2012-05-31 11:59:31 -0700 | [diff] [blame] | 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 | |
Wu-cheng Li | fd0975a | 2012-07-04 16:32:57 +0800 | [diff] [blame] | 19 | #include <android/log.h> |
Ruei-sung Lin | e5c8ed7 | 2012-05-31 11:59:31 -0700 | [diff] [blame] | 20 | #include <EGL/egl.h> |
| 21 | #include <EGL/eglext.h> |
Wu-cheng Li | fd0975a | 2012-07-04 16:32:57 +0800 | [diff] [blame] | 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 | } |
Ruei-sung Lin | e5c8ed7 | 2012-05-31 11:59:31 -0700 | [diff] [blame] | 55 | |
| 56 | void |
| 57 | Java_com_android_gallery3d_photoeditor_FilterStack_nativeEglSetFenceAndWait(JNIEnv* env, |
| 58 | jobject thiz) { |
Wu-cheng Li | fd0975a | 2012-07-04 16:32:57 +0800 | [diff] [blame] | 59 | if (!IsEglKHRFenceSyncSupported()) return; |
Ruei-sung Lin | e5c8ed7 | 2012-05-31 11:59:31 -0700 | [diff] [blame] | 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 |
Wu-cheng Li | fd0975a | 2012-07-04 16:32:57 +0800 | [diff] [blame] | 65 | EGLSyncKHR fence = FuncEglCreateSyncKHR(display, EGL_SYNC_FENCE_KHR, NULL); |
Ruei-sung Lin | e5c8ed7 | 2012-05-31 11:59:31 -0700 | [diff] [blame] | 66 | if (fence == EGL_NO_SYNC_KHR) { |
| 67 | return; |
| 68 | } |
| 69 | |
Wu-cheng Li | fd0975a | 2012-07-04 16:32:57 +0800 | [diff] [blame] | 70 | EGLint result = FuncEglClientWaitSyncKHR(display, |
Ruei-sung Lin | e5c8ed7 | 2012-05-31 11:59:31 -0700 | [diff] [blame] | 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 | } |
Wu-cheng Li | fd0975a | 2012-07-04 16:32:57 +0800 | [diff] [blame] | 77 | FuncEglDestroySyncKHR(display, fence); |
Ruei-sung Lin | e5c8ed7 | 2012-05-31 11:59:31 -0700 | [diff] [blame] | 78 | } |