1ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian/*
2ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian ** Copyright 2013, The Android Open Source Project
3ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian **
4ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian ** Licensed under the Apache License, Version 2.0 (the "License");
5ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian ** you may not use this file except in compliance with the License.
6ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian ** You may obtain a copy of the License at
7ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian **
8ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian **     http://www.apache.org/licenses/LICENSE-2.0
9ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian **
10ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian ** Unless required by applicable law or agreed to in writing, software
11ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian ** distributed under the License is distributed on an "AS IS" BASIS,
12ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian ** See the License for the specific language governing permissions and
14ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian ** limitations under the License.
15ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian */
16ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian
17ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian#define LOG_TAG "GLConsumer"
18ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian
19ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian#define EGL_EGLEXT_PROTOTYPES
20ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian
21ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian#include <EGL/egl.h>
22ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian#include <EGL/eglext.h>
23ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian
24ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian#include <utils/Log.h>
25ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian#include <utils/Singleton.h>
26ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian#include <utils/String8.h>
27ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian
28ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian#include <private/gui/SyncFeatures.h>
29ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian
30ca08833d5ea99130797e10ad68a651b50e99da74Mathias AgopianEGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
31ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian
32ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopiannamespace android {
33ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian
34ca08833d5ea99130797e10ad68a651b50e99da74Mathias AgopianANDROID_SINGLETON_STATIC_INSTANCE(SyncFeatures);
35ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian
36ca08833d5ea99130797e10ad68a651b50e99da74Mathias AgopianSyncFeatures::SyncFeatures() : Singleton<SyncFeatures>(),
37ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian        mHasNativeFenceSync(false),
38ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian        mHasFenceSync(false),
39ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian        mHasWaitSync(false) {
40ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
41ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    // This can only be called after EGL has been initialized; otherwise the
42ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    // check below will abort.
43ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    const char* exts = eglQueryStringImplementationANDROID(dpy, EGL_EXTENSIONS);
44ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    LOG_ALWAYS_FATAL_IF(exts == NULL, "eglQueryStringImplementationANDROID failed");
45ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    if (strstr(exts, "EGL_ANDROID_native_fence_sync")) {
46ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian        // This makes GLConsumer use the EGL_ANDROID_native_fence_sync
47ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian        // extension to create Android native fences to signal when all
48ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian        // GLES reads for a given buffer have completed.
49ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian        mHasNativeFenceSync = true;
50ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    }
51ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    if (strstr(exts, "EGL_KHR_fence_sync")) {
52ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian        mHasFenceSync = true;
53ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    }
54ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    if (strstr(exts, "EGL_KHR_wait_sync")) {
55ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian        mHasWaitSync = true;
56ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    }
57ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    mString.append("[using:");
58ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    if (useNativeFenceSync()) {
59ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian        mString.append(" EGL_ANDROID_native_fence_sync");
60ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    }
61ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    if (useFenceSync()) {
62ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian        mString.append(" EGL_KHR_fence_sync");
63ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    }
64ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    if (useWaitSync()) {
65ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian        mString.append(" EGL_KHR_wait_sync");
66ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    }
67ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    mString.append("]");
68ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian}
69ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian
70ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopianbool SyncFeatures::useNativeFenceSync() const {
71ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    // EGL_ANDROID_native_fence_sync is not compatible with using the
72ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    // EGL_KHR_fence_sync extension for the same purpose.
73ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    return mHasNativeFenceSync;
74ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian}
75ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopianbool SyncFeatures::useFenceSync() const {
76ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian#ifdef DONT_USE_FENCE_SYNC
77ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    // on some devices it's better to not use EGL_KHR_fence_sync
78ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    // even if they have it
79ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    return false;
8000d504c06ea034befe143e6b8cb34d004670ed02Dan Stoza#else
81ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    // currently we shall only attempt to use EGL_KHR_fence_sync if
82ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    // USE_FENCE_SYNC is set in our makefile
83ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    return !mHasNativeFenceSync && mHasFenceSync;
8400d504c06ea034befe143e6b8cb34d004670ed02Dan Stoza#endif
85ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian}
86ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopianbool SyncFeatures::useWaitSync() const {
87ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    return (useNativeFenceSync() || useFenceSync()) && mHasWaitSync;
88ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian}
89ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian
90ca08833d5ea99130797e10ad68a651b50e99da74Mathias AgopianString8 SyncFeatures::toString() const {
91ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian    return mString;
92ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian}
93ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian
94ca08833d5ea99130797e10ad68a651b50e99da74Mathias Agopian} // namespace android
95