Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/gfx/gl/EGLUtils.cpp
Line
Count
Source (jump to first uncovered line)
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
#include "EGLUtils.h"
6
7
#include "GLContextEGL.h"
8
#include "GLLibraryEGL.h"
9
10
namespace mozilla {
11
namespace gl {
12
13
bool
14
DoesEGLContextSupportSharingWithEGLImage(GLContext* gl)
15
0
{
16
0
    auto* egl = gl::GLLibraryEGL::Get();
17
0
18
0
    return egl->HasKHRImageBase() &&
19
0
           egl->HasKHRImageTexture2D() &&
20
0
           gl->IsExtensionSupported(GLContext::OES_EGL_image);
21
0
}
22
23
EGLImage
24
CreateEGLImage(GLContext* gl, GLuint tex)
25
0
{
26
0
    MOZ_ASSERT(DoesEGLContextSupportSharingWithEGLImage(gl));
27
0
28
0
    auto* egl = gl::GLLibraryEGL::Get();
29
0
30
0
    EGLClientBuffer clientBuffer = (EGLClientBuffer)((uint64_t)tex);
31
0
    EGLContext eglContext = GLContextEGL::Cast(gl)->mContext;
32
0
    EGLImage image = egl->fCreateImage(EGL_DISPLAY(),
33
0
                                       eglContext,
34
0
                                       LOCAL_EGL_GL_TEXTURE_2D,
35
0
                                       clientBuffer,
36
0
                                       nullptr);
37
0
    return image;
38
0
}
39
40
////////////////////////////////////////////////////////////////////////
41
// EGLImageWrapper
42
43
/*static*/ EGLImageWrapper*
44
EGLImageWrapper::Create(GLContext* gl, GLuint tex)
45
0
{
46
0
    MOZ_ASSERT(DoesEGLContextSupportSharingWithEGLImage(gl));
47
0
48
0
    auto* egl = gl::GLLibraryEGL::Get();
49
0
50
0
    EGLDisplay display = EGL_DISPLAY();
51
0
    EGLContext eglContext = GLContextEGL::Cast(gl)->mContext;
52
0
    EGLClientBuffer clientBuffer = (EGLClientBuffer)((uint64_t)tex);
53
0
    EGLImage image = egl->fCreateImage(display,
54
0
                                       eglContext,
55
0
                                       LOCAL_EGL_GL_TEXTURE_2D,
56
0
                                       clientBuffer,
57
0
                                       nullptr);
58
0
    if (!image) {
59
#ifdef DEBUG
60
        printf_stderr("Could not create EGL images: ERROR (0x%04x)\n",
61
                      egl->fGetError());
62
#endif
63
        return nullptr;
64
0
    }
65
0
66
0
    return new EGLImageWrapper(egl, display, image);
67
0
}
68
69
EGLImageWrapper::EGLImageWrapper(GLLibraryEGL* library,
70
                                 EGLDisplay display,
71
                                 EGLImage image)
72
    : mLibrary(library)
73
    , mDisplay(display)
74
    , mImage(image)
75
    , mSync(0)
76
0
{
77
0
    MOZ_ASSERT(mImage);
78
0
}
79
80
EGLImageWrapper::~EGLImageWrapper()
81
0
{
82
0
    mLibrary->fDestroyImage(mDisplay, mImage);
83
0
}
84
85
bool
86
EGLImageWrapper::FenceSync(GLContext* gl)
87
0
{
88
0
    MOZ_ASSERT(!mSync);
89
0
90
0
    if (mLibrary->IsExtensionSupported(GLLibraryEGL::KHR_fence_sync)) {
91
0
        mSync = mLibrary->fCreateSync(mDisplay,
92
0
                                      LOCAL_EGL_SYNC_FENCE,
93
0
                                      nullptr);
94
0
        // We need to flush to make sure the sync object enters the command stream;
95
0
        // we can't use EGL_SYNC_FLUSH_COMMANDS_BIT at wait time, because the wait
96
0
        // happens on a different thread/context.
97
0
        gl->fFlush();
98
0
    }
99
0
100
0
    if (!mSync) {
101
0
        // we failed to create one, so just do a finish
102
0
        gl->fFinish();
103
0
    }
104
0
105
0
    return true;
106
0
}
107
108
bool
109
EGLImageWrapper::ClientWaitSync()
110
0
{
111
0
    if (!mSync) {
112
0
        // if we have no sync object, then we did a Finish() earlier
113
0
        return true;
114
0
    }
115
0
116
0
    // wait at most 1 second; this should really be never/rarely hit
117
0
    const uint64_t ns_per_ms = 1000 * 1000;
118
0
    EGLTime timeout = 1000 * ns_per_ms;
119
0
120
0
    EGLint result = mLibrary->fClientWaitSync(mDisplay,
121
0
                                              mSync,
122
0
                                              0,
123
0
                                              timeout);
124
0
    mLibrary->fDestroySync(mDisplay, mSync);
125
0
    mSync = nullptr;
126
0
127
0
    return result == LOCAL_EGL_CONDITION_SATISFIED;
128
0
}
129
130
} // namespace gl
131
} // namespace mozilla