File indexing completed on 2024-05-12 05:31:41

0001 /*
0002     SPDX-FileCopyrightText: 2020 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "eglnativefence.h"
0008 #include "egldisplay.h"
0009 
0010 #include <unistd.h>
0011 
0012 namespace KWin
0013 {
0014 
0015 #ifndef EGL_ANDROID_native_fence_sync
0016 #define EGL_SYNC_NATIVE_FENCE_ANDROID 0x3144
0017 #define EGL_NO_NATIVE_FENCE_FD_ANDROID -1
0018 #endif // EGL_ANDROID_native_fence_sync
0019 
0020 EGLNativeFence::EGLNativeFence(EglDisplay *display)
0021     : EGLNativeFence(display, eglCreateSyncKHR(display->handle(), EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr))
0022 {
0023     if (m_sync != EGL_NO_SYNC_KHR) {
0024         // The native fence will get a valid sync file fd only after a flush.
0025         glFlush();
0026         m_fileDescriptor = FileDescriptor(eglDupNativeFenceFDANDROID(m_display->handle(), m_sync));
0027     }
0028 }
0029 
0030 EGLNativeFence::EGLNativeFence(EglDisplay *display, EGLSyncKHR sync)
0031     : m_sync(sync)
0032     , m_display(display)
0033 {
0034 }
0035 
0036 EGLNativeFence::~EGLNativeFence()
0037 {
0038     m_fileDescriptor.reset();
0039     if (m_sync != EGL_NO_SYNC_KHR) {
0040         eglDestroySyncKHR(m_display->handle(), m_sync);
0041     }
0042 }
0043 
0044 bool EGLNativeFence::isValid() const
0045 {
0046     return m_sync != EGL_NO_SYNC_KHR && m_fileDescriptor.isValid();
0047 }
0048 
0049 const FileDescriptor &EGLNativeFence::fileDescriptor() const
0050 {
0051     return m_fileDescriptor;
0052 }
0053 
0054 bool EGLNativeFence::waitSync() const
0055 {
0056     return eglWaitSync(m_display->handle(), m_sync, 0) == EGL_TRUE;
0057 }
0058 
0059 EGLNativeFence EGLNativeFence::importFence(EglDisplay *display, FileDescriptor &&fd)
0060 {
0061     EGLint attributes[] = {
0062         EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fd.take(),
0063         EGL_NONE};
0064     return EGLNativeFence(display, eglCreateSyncKHR(display->handle(), EGL_SYNC_NATIVE_FENCE_ANDROID, attributes));
0065 }
0066 
0067 } // namespace KWin