gl_fence.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ui/gl/gl_fence.h"
  5. #include "base/compiler_specific.h"
  6. #include "build/build_config.h"
  7. #include "ui/gl/gl_bindings.h"
  8. #include "ui/gl/gl_context.h"
  9. #include "ui/gl/gl_display.h"
  10. #include "ui/gl/gl_fence_arb.h"
  11. #include "ui/gl/gl_fence_egl.h"
  12. #include "ui/gl/gl_fence_nv.h"
  13. #include "ui/gl/gl_implementation.h"
  14. #include "ui/gl/gl_version_info.h"
  15. #if BUILDFLAG(IS_APPLE)
  16. #include "ui/gl/gl_fence_apple.h"
  17. #endif
  18. #if defined(USE_EGL)
  19. #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_APPLE)
  20. #define USE_GL_FENCE_ANDROID_NATIVE_FENCE_SYNC
  21. #include "ui/gl/gl_fence_android_native_fence_sync.h"
  22. #endif
  23. #include "ui/gl/gl_context_egl.h"
  24. #include "ui/gl/gl_surface_egl.h"
  25. #endif
  26. #if BUILDFLAG(IS_WIN)
  27. #include "ui/gl/gl_fence_win.h"
  28. #endif
  29. namespace gl {
  30. GLFence::GLFence() {
  31. }
  32. GLFence::~GLFence() {
  33. }
  34. bool GLFence::IsSupported() {
  35. DCHECK(g_current_gl_version && g_current_gl_driver);
  36. #if !BUILDFLAG(IS_APPLE) && defined(USE_EGL)
  37. GLDisplayEGL* display = GLDisplayEGL::GetDisplayForCurrentContext();
  38. #endif // !ISAPPLE && USE_EGL
  39. return g_current_gl_driver->ext.b_GL_ARB_sync ||
  40. g_current_gl_version->is_es3 ||
  41. g_current_gl_version->is_desktop_core_profile ||
  42. #if BUILDFLAG(IS_APPLE)
  43. g_current_gl_driver->ext.b_GL_APPLE_fence ||
  44. #elif defined(USE_EGL)
  45. (display && display->ext->b_EGL_KHR_fence_sync) ||
  46. #endif
  47. g_current_gl_driver->ext.b_GL_NV_fence;
  48. }
  49. std::unique_ptr<GLFence> GLFence::Create() {
  50. DCHECK(GLContext::GetCurrent())
  51. << "Trying to create fence with no context";
  52. std::unique_ptr<GLFence> fence;
  53. #if !BUILDFLAG(IS_APPLE) && defined(USE_EGL)
  54. GLDisplayEGL* display = GLDisplayEGL::GetDisplayForCurrentContext();
  55. if (display && display->ext->b_EGL_KHR_fence_sync &&
  56. display->ext->b_EGL_KHR_wait_sync) {
  57. // Prefer GLFenceEGL which doesn't require GL context switching.
  58. fence = GLFenceEGL::Create();
  59. DCHECK(fence);
  60. DCHECK(GLFence::IsSupported());
  61. return fence;
  62. }
  63. #endif // !IS_APPLE && USE_EGL
  64. if (g_current_gl_driver->ext.b_GL_ARB_sync || g_current_gl_version->is_es3 ||
  65. g_current_gl_version->is_desktop_core_profile) {
  66. // Prefer ARB_sync which supports server-side wait.
  67. fence = std::make_unique<GLFenceARB>();
  68. DCHECK(fence);
  69. #if BUILDFLAG(IS_APPLE)
  70. } else if (g_current_gl_driver->ext.b_GL_APPLE_fence) {
  71. fence = std::make_unique<GLFenceAPPLE>();
  72. DCHECK(fence);
  73. #elif defined(USE_EGL)
  74. } else if (display && display->ext->b_EGL_KHR_fence_sync) {
  75. fence = GLFenceEGL::Create();
  76. DCHECK(fence);
  77. #endif
  78. } else if (g_current_gl_driver->ext.b_GL_NV_fence) {
  79. fence = std::make_unique<GLFenceNV>();
  80. DCHECK(fence);
  81. }
  82. DCHECK_EQ(!!fence.get(), GLFence::IsSupported());
  83. return fence;
  84. }
  85. bool GLFence::ResetSupported() {
  86. // Resetting a fence to its original state isn't supported by default.
  87. return false;
  88. }
  89. void GLFence::ResetState() {
  90. NOTIMPLEMENTED();
  91. }
  92. void GLFence::Invalidate() {
  93. NOTIMPLEMENTED();
  94. }
  95. bool GLFence::IsGpuFenceSupported() {
  96. #if defined(USE_GL_FENCE_ANDROID_NATIVE_FENCE_SYNC)
  97. return gl::GLSurfaceEGL::GetGLDisplayEGL()
  98. ->IsAndroidNativeFenceSyncSupported();
  99. #elif BUILDFLAG(IS_WIN)
  100. return gl::GLFenceWin::IsSupported();
  101. #else
  102. return false;
  103. #endif
  104. }
  105. // static
  106. std::unique_ptr<GLFence> GLFence::CreateFromGpuFence(
  107. const gfx::GpuFence& gpu_fence) {
  108. DCHECK(IsGpuFenceSupported());
  109. #if defined(USE_GL_FENCE_ANDROID_NATIVE_FENCE_SYNC)
  110. return GLFenceAndroidNativeFenceSync::CreateFromGpuFence(gpu_fence);
  111. #elif BUILDFLAG(IS_WIN)
  112. return GLFenceWin::CreateFromGpuFence(gpu_fence);
  113. #else
  114. NOTREACHED();
  115. return nullptr;
  116. #endif
  117. }
  118. // static
  119. std::unique_ptr<GLFence> GLFence::CreateForGpuFence() {
  120. DCHECK(IsGpuFenceSupported());
  121. #if defined(USE_GL_FENCE_ANDROID_NATIVE_FENCE_SYNC)
  122. return GLFenceAndroidNativeFenceSync::CreateForGpuFence();
  123. #elif BUILDFLAG(IS_WIN)
  124. return GLFenceWin::CreateForGpuFence();
  125. #else
  126. NOTREACHED();
  127. return nullptr;
  128. #endif
  129. }
  130. std::unique_ptr<gfx::GpuFence> GLFence::GetGpuFence() {
  131. return nullptr;
  132. }
  133. } // namespace gl