1 //
2 // Copyright 2020 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6
7 // EGLReusableSync.cpp: Implements the egl::ReusableSync class.
8
9 #include "libANGLE/renderer/EGLReusableSync.h"
10
11 #include "libANGLE/Context.h"
12 #include "libANGLE/renderer/ContextImpl.h"
13
14 namespace rx
15 {
16
ReusableSync()17 ReusableSync::ReusableSync() : EGLSyncImpl(), mStatus(0) {}
18
onDestroy(const egl::Display * display)19 void ReusableSync::onDestroy(const egl::Display *display) {}
20
~ReusableSync()21 ReusableSync::~ReusableSync()
22 {
23 // Release any waiting thread.
24 mCondVar.notify_all();
25 }
26
initialize(const egl::Display * display,const gl::Context * context,EGLenum type,const egl::AttributeMap & attribs)27 egl::Error ReusableSync::initialize(const egl::Display *display,
28 const gl::Context *context,
29 EGLenum type,
30 const egl::AttributeMap &attribs)
31 {
32 ASSERT(type == EGL_SYNC_REUSABLE_KHR);
33 mStatus = EGL_UNSIGNALED;
34 return egl::NoError();
35 }
36
clientWait(const egl::Display * display,const gl::Context * context,EGLint flags,EGLTime timeout,EGLint * outResult)37 egl::Error ReusableSync::clientWait(const egl::Display *display,
38 const gl::Context *context,
39 EGLint flags,
40 EGLTime timeout,
41 EGLint *outResult)
42 {
43 if (mStatus == EGL_SIGNALED)
44 {
45 *outResult = EGL_CONDITION_SATISFIED_KHR;
46 return egl::NoError();
47 }
48 if (((flags & EGL_SYNC_FLUSH_COMMANDS_BIT) != 0) && (context != nullptr))
49 {
50 angle::Result result = context->getImplementation()->flush(context);
51 if (result != angle::Result::Continue)
52 {
53 return ResultToEGL(result);
54 }
55 }
56 if (timeout == 0)
57 {
58 *outResult = EGL_TIMEOUT_EXPIRED_KHR;
59 return egl::NoError();
60 }
61
62 using NanoSeconds = std::chrono::duration<int64_t, std::nano>;
63 NanoSeconds duration = (timeout == EGL_FOREVER) ? NanoSeconds::max() : NanoSeconds(timeout);
64 std::cv_status waitStatus = std::cv_status::no_timeout;
65 mMutex.lock();
66 waitStatus = mCondVar.wait_for(mMutex, duration);
67 mMutex.unlock();
68
69 switch (waitStatus)
70 {
71 case std::cv_status::no_timeout: // Signaled.
72 *outResult = EGL_CONDITION_SATISFIED_KHR;
73 break;
74 case std::cv_status::timeout: // Timed-out.
75 *outResult = EGL_TIMEOUT_EXPIRED_KHR;
76 break;
77 default:
78 break;
79 }
80 return egl::NoError();
81 }
82
serverWait(const egl::Display * display,const gl::Context * context,EGLint flags)83 egl::Error ReusableSync::serverWait(const egl::Display *display,
84 const gl::Context *context,
85 EGLint flags)
86 {
87 // Does not support server wait.
88 return egl::EglBadMatch();
89 }
90
signal(const egl::Display * display,const gl::Context * context,EGLint mode)91 egl::Error ReusableSync::signal(const egl::Display *display,
92 const gl::Context *context,
93 EGLint mode)
94 {
95 if (mode == EGL_SIGNALED)
96 {
97 if (mStatus == EGL_UNSIGNALED)
98 {
99 // Release all threads.
100 mCondVar.notify_all();
101 }
102 mStatus = EGL_SIGNALED;
103 }
104 else
105 {
106 mStatus = EGL_UNSIGNALED;
107 }
108 return egl::NoError();
109 }
110
getStatus(const egl::Display * display,EGLint * outStatus)111 egl::Error ReusableSync::getStatus(const egl::Display *display, EGLint *outStatus)
112 {
113 *outStatus = mStatus;
114 return egl::NoError();
115 }
116
117 } // namespace rx
118