File: GLFenceEGL.cpp

package info (click to toggle)
webkit2gtk 2.49.90-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 445,664 kB
  • sloc: cpp: 3,797,881; javascript: 197,914; ansic: 161,337; python: 49,141; asm: 21,979; ruby: 18,539; perl: 16,723; xml: 4,623; yacc: 2,360; sh: 2,246; java: 2,019; lex: 1,327; pascal: 366; makefile: 295
file content (147 lines) | stat: -rw-r--r-- 4,057 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * Copyright (C) 2024 Igalia S.L.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301 USA
 */

#include "config.h"
#include "GLFenceEGL.h"

#if HAVE(GL_FENCE)
#include "GLDisplay.h"
#include <wtf/Vector.h>

#if USE(LIBEPOXY)
#include <epoxy/egl.h>
#else
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES2/gl2.h>
#endif

namespace WebCore {

static std::unique_ptr<GLFence> createEGLFence(const GLDisplay& display, EGLenum type, const Vector<EGLAttrib>& attributes)
{
    EGLSync sync = nullptr;
    if (display.checkVersion(1, 5))
        sync = eglCreateSync(display.eglDisplay(), type, attributes.isEmpty() ? nullptr : attributes.span().data());
    else {
        Vector<EGLint> intAttributes = attributes.map<Vector<EGLint>>([] (EGLAttrib value) {
            return value;
        });
        sync = eglCreateSyncKHR(display.eglDisplay(), type, intAttributes.isEmpty() ? nullptr : intAttributes.span().data());
    }
    if (sync == EGL_NO_SYNC)
        return nullptr;

    glFlush();

#if OS(UNIX)
    bool isExportable = type == EGL_SYNC_NATIVE_FENCE_ANDROID;
#else
    bool isExportable = false;
#endif
    return makeUnique<GLFenceEGL>(display, sync, isExportable);
}

std::unique_ptr<GLFence> GLFenceEGL::create(const GLDisplay& display)
{
    return createEGLFence(display, EGL_SYNC_FENCE_KHR, { });
}

#if OS(UNIX)
std::unique_ptr<GLFence> GLFenceEGL::createExportable(const GLDisplay& display)
{
    return createEGLFence(display, EGL_SYNC_NATIVE_FENCE_ANDROID, { });
}

std::unique_ptr<GLFence> GLFenceEGL::importFD(const GLDisplay& display, UnixFileDescriptor&& fd)
{
    Vector<EGLAttrib> attributes = {
        EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fd.release(),
        EGL_NONE
    };
    return createEGLFence(display, EGL_SYNC_NATIVE_FENCE_ANDROID, attributes);
}
#endif

GLFenceEGL::GLFenceEGL(const GLDisplay& display, EGLSyncKHR sync, [[maybe_unused]] bool isExportable)
    : m_display(display)
    , m_sync(sync)
#if OS(UNIX)
    , m_isExportable(isExportable)
#endif
{
}

GLFenceEGL::~GLFenceEGL()
{
    auto display = m_display.get();
    if (!display)
        return;

    if (display->checkVersion(1, 5))
        eglDestroySync(display->eglDisplay(), m_sync);
    else
        eglDestroySyncKHR(display->eglDisplay(), m_sync);
}

void GLFenceEGL::clientWait()
{
    auto display = m_display.get();
    if (!display)
        return;

    if (display->checkVersion(1, 5))
        eglClientWaitSync(display->eglDisplay(), m_sync, 0, EGL_FOREVER);
    else
        eglClientWaitSyncKHR(display->eglDisplay(), m_sync, 0, EGL_FOREVER_KHR);
}

void GLFenceEGL::serverWait()
{
    auto display = m_display.get();
    if (!display)
        return;

    if (display->checkVersion(1, 5))
        eglWaitSync(display->eglDisplay(), m_sync, 0);
    else {
        if (display->extensions().KHR_wait_sync)
            eglWaitSyncKHR(display->eglDisplay(), m_sync, 0);
        else
            eglClientWaitSyncKHR(display->eglDisplay(), m_sync, 0, EGL_FOREVER_KHR);
    }
}

#if OS(UNIX)
UnixFileDescriptor GLFenceEGL::exportFD()
{
    if (!m_isExportable)
        return { };

    auto display = m_display.get();
    if (!display)
        return { };

    return UnixFileDescriptor { eglDupNativeFenceFDANDROID(display->eglDisplay(), m_sync), UnixFileDescriptor::Adopt };
}
#endif

} // namespace WebCore

#endif // HAVE(GL_FENCE)