blob: 8df6e8159654c6a6535ed995a29dcfcb09030ce2 [file] [log] [blame]
Mathias Agopianca088332013-03-28 17:44:13 -07001/*
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
Mathias Agopianca088332013-03-28 17:44:13 -070019#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
Mathias Agopianca088332013-03-28 17:44:13 -070030namespace android {
31
32ANDROID_SINGLETON_STATIC_INSTANCE(SyncFeatures);
33
34SyncFeatures::SyncFeatures() : Singleton<SyncFeatures>(),
35 mHasNativeFenceSync(false),
36 mHasFenceSync(false),
37 mHasWaitSync(false) {
38 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
39 // This can only be called after EGL has been initialized; otherwise the
40 // check below will abort.
Yiwei Zhang0378f4d2020-08-12 15:03:23 -070041 const char* exts = eglQueryString(dpy, EGL_EXTENSIONS);
42 LOG_ALWAYS_FATAL_IF(exts == nullptr, "eglQueryString failed");
Mathias Agopianca088332013-03-28 17:44:13 -070043 if (strstr(exts, "EGL_ANDROID_native_fence_sync")) {
44 // This makes GLConsumer use the EGL_ANDROID_native_fence_sync
45 // extension to create Android native fences to signal when all
46 // GLES reads for a given buffer have completed.
47 mHasNativeFenceSync = true;
48 }
49 if (strstr(exts, "EGL_KHR_fence_sync")) {
50 mHasFenceSync = true;
51 }
52 if (strstr(exts, "EGL_KHR_wait_sync")) {
53 mHasWaitSync = true;
54 }
55 mString.append("[using:");
56 if (useNativeFenceSync()) {
57 mString.append(" EGL_ANDROID_native_fence_sync");
58 }
59 if (useFenceSync()) {
60 mString.append(" EGL_KHR_fence_sync");
61 }
62 if (useWaitSync()) {
63 mString.append(" EGL_KHR_wait_sync");
64 }
65 mString.append("]");
66}
67
68bool SyncFeatures::useNativeFenceSync() const {
69 // EGL_ANDROID_native_fence_sync is not compatible with using the
70 // EGL_KHR_fence_sync extension for the same purpose.
71 return mHasNativeFenceSync;
72}
73bool SyncFeatures::useFenceSync() const {
74#ifdef DONT_USE_FENCE_SYNC
75 // on some devices it's better to not use EGL_KHR_fence_sync
76 // even if they have it
77 return false;
Dan Stoza00d504c2014-12-05 13:43:03 -080078#else
Mathias Agopianca088332013-03-28 17:44:13 -070079 // currently we shall only attempt to use EGL_KHR_fence_sync if
80 // USE_FENCE_SYNC is set in our makefile
81 return !mHasNativeFenceSync && mHasFenceSync;
Dan Stoza00d504c2014-12-05 13:43:03 -080082#endif
Mathias Agopianca088332013-03-28 17:44:13 -070083}
84bool SyncFeatures::useWaitSync() const {
85 return (useNativeFenceSync() || useFenceSync()) && mHasWaitSync;
86}
87
88String8 SyncFeatures::toString() const {
89 return mString;
90}
91
92} // namespace android