blob: cf15e2f5df79614a0c66c7d662038d047a75ed92 [file] [log] [blame]
Ruei-sung Line5c8ed72012-05-31 11:59:31 -07001/*
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 Lifd0975a2012-07-04 16:32:57 +080019#include <android/log.h>
Ruei-sung Line5c8ed72012-05-31 11:59:31 -070020#include <EGL/egl.h>
21#include <EGL/eglext.h>
Wu-cheng Lifd0975a2012-07-04 16:32:57 +080022#include <string.h>
23
24#define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR,"egl_fence",__VA_ARGS__)
25
26typedef EGLSyncKHR EGLAPIENTRY (*TypeEglCreateSyncKHR)(EGLDisplay dpy,
27 EGLenum type, const EGLint *attrib_list);
28typedef EGLBoolean EGLAPIENTRY (*TypeEglDestroySyncKHR)(EGLDisplay dpy,
29 EGLSyncKHR sync);
30typedef EGLint EGLAPIENTRY (*TypeEglClientWaitSyncKHR)(EGLDisplay dpy,
31 EGLSyncKHR sync, EGLint flags, EGLTimeKHR timeout);
32static TypeEglCreateSyncKHR FuncEglCreateSyncKHR = NULL;
33static TypeEglClientWaitSyncKHR FuncEglClientWaitSyncKHR = NULL;
34static TypeEglDestroySyncKHR FuncEglDestroySyncKHR = NULL;
35static bool initialized = false;
36static bool egl_khr_fence_sync_supported = false;
37
38bool 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 Line5c8ed72012-05-31 11:59:31 -070055
56void
57Java_com_android_gallery3d_photoeditor_FilterStack_nativeEglSetFenceAndWait(JNIEnv* env,
58 jobject thiz) {
Wu-cheng Lifd0975a2012-07-04 16:32:57 +080059 if (!IsEglKHRFenceSyncSupported()) return;
Ruei-sung Line5c8ed72012-05-31 11:59:31 -070060 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 Lifd0975a2012-07-04 16:32:57 +080065 EGLSyncKHR fence = FuncEglCreateSyncKHR(display, EGL_SYNC_FENCE_KHR, NULL);
Ruei-sung Line5c8ed72012-05-31 11:59:31 -070066 if (fence == EGL_NO_SYNC_KHR) {
67 return;
68 }
69
Wu-cheng Lifd0975a2012-07-04 16:32:57 +080070 EGLint result = FuncEglClientWaitSyncKHR(display,
Ruei-sung Line5c8ed72012-05-31 11:59:31 -070071 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 Lifd0975a2012-07-04 16:32:57 +080077 FuncEglDestroySyncKHR(display, fence);
Ruei-sung Line5c8ed72012-05-31 11:59:31 -070078}