SyncFeatures.cpp 3.0 KB

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