1 /*
2  ** Copyright 2013, 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 #define LOG_TAG "GLConsumer"
18 
19 #define EGL_EGLEXT_PROTOTYPES
20 
21 #include <EGL/egl.h>
22 #include <EGL/eglext.h>
23 
24 #include <utils/Log.h>
25 #include <utils/Singleton.h>
26 #include <utils/String8.h>
27 
28 #include <private/gui/SyncFeatures.h>
29 
30 extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
31 
32 namespace android {
33 
34 ANDROID_SINGLETON_STATIC_INSTANCE(SyncFeatures);
35 
SyncFeatures()36 SyncFeatures::SyncFeatures() : Singleton<SyncFeatures>(),
37         mHasNativeFenceSync(false),
38         mHasFenceSync(false),
39         mHasWaitSync(false) {
40     EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
41     // This can only be called after EGL has been initialized; otherwise the
42     // check below will abort.
43     const char* exts = eglQueryStringImplementationANDROID(dpy, EGL_EXTENSIONS);
44     LOG_ALWAYS_FATAL_IF(exts == nullptr, "eglQueryStringImplementationANDROID failed");
45     if (strstr(exts, "EGL_ANDROID_native_fence_sync")) {
46         // This makes GLConsumer use the EGL_ANDROID_native_fence_sync
47         // extension to create Android native fences to signal when all
48         // GLES reads for a given buffer have completed.
49         mHasNativeFenceSync = true;
50     }
51     if (strstr(exts, "EGL_KHR_fence_sync")) {
52         mHasFenceSync = true;
53     }
54     if (strstr(exts, "EGL_KHR_wait_sync")) {
55         mHasWaitSync = true;
56     }
57     mString.append("[using:");
58     if (useNativeFenceSync()) {
59         mString.append(" EGL_ANDROID_native_fence_sync");
60     }
61     if (useFenceSync()) {
62         mString.append(" EGL_KHR_fence_sync");
63     }
64     if (useWaitSync()) {
65         mString.append(" EGL_KHR_wait_sync");
66     }
67     mString.append("]");
68 }
69 
useNativeFenceSync() const70 bool SyncFeatures::useNativeFenceSync() const {
71     // EGL_ANDROID_native_fence_sync is not compatible with using the
72     // EGL_KHR_fence_sync extension for the same purpose.
73     return mHasNativeFenceSync;
74 }
useFenceSync() const75 bool SyncFeatures::useFenceSync() const {
76 #ifdef DONT_USE_FENCE_SYNC
77     // on some devices it's better to not use EGL_KHR_fence_sync
78     // even if they have it
79     return false;
80 #else
81     // currently we shall only attempt to use EGL_KHR_fence_sync if
82     // USE_FENCE_SYNC is set in our makefile
83     return !mHasNativeFenceSync && mHasFenceSync;
84 #endif
85 }
useWaitSync() const86 bool SyncFeatures::useWaitSync() const {
87     return (useNativeFenceSync() || useFenceSync()) && mHasWaitSync;
88 }
89 
toString() const90 String8 SyncFeatures::toString() const {
91     return mString;
92 }
93 
94 } // namespace android
95