| Mathias Agopian | ca08833 | 2013-03-28 17:44:13 -0700 | [diff] [blame] | 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 |  | 
| Mathias Agopian | ca08833 | 2013-03-28 17:44:13 -0700 | [diff] [blame] | 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 |  | 
| Mathias Agopian | ca08833 | 2013-03-28 17:44:13 -0700 | [diff] [blame] | 30 | namespace android { | 
|  | 31 |  | 
|  | 32 | ANDROID_SINGLETON_STATIC_INSTANCE(SyncFeatures); | 
|  | 33 |  | 
|  | 34 | SyncFeatures::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 Zhang | 0378f4d | 2020-08-12 15:03:23 -0700 | [diff] [blame] | 41 | const char* exts = eglQueryString(dpy, EGL_EXTENSIONS); | 
|  | 42 | LOG_ALWAYS_FATAL_IF(exts == nullptr, "eglQueryString failed"); | 
| Mathias Agopian | ca08833 | 2013-03-28 17:44:13 -0700 | [diff] [blame] | 43 | 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 |  | 
|  | 68 | bool 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 | } | 
|  | 73 | bool 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 Stoza | 00d504c | 2014-12-05 13:43:03 -0800 | [diff] [blame] | 78 | #else | 
| Mathias Agopian | ca08833 | 2013-03-28 17:44:13 -0700 | [diff] [blame] | 79 | // 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 Stoza | 00d504c | 2014-12-05 13:43:03 -0800 | [diff] [blame] | 82 | #endif | 
| Mathias Agopian | ca08833 | 2013-03-28 17:44:13 -0700 | [diff] [blame] | 83 | } | 
|  | 84 | bool SyncFeatures::useWaitSync() const { | 
|  | 85 | return (useNativeFenceSync() || useFenceSync()) && mHasWaitSync; | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | String8 SyncFeatures::toString() const { | 
|  | 89 | return mString; | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | } // namespace android |