1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "WebGL2Context.h"
10 #include "mozilla/StaticPrefs_webgl.h"
14 // -------------------------------------------------------------------------
17 RefPtr
<WebGLSync
> WebGL2Context::FenceSync(GLenum condition
, GLbitfield flags
) {
18 const FuncScope
funcScope(*this, "fenceSync");
19 if (IsContextLost()) return nullptr;
21 if (condition
!= LOCAL_GL_SYNC_GPU_COMMANDS_COMPLETE
) {
22 ErrorInvalidEnum("condition must be SYNC_GPU_COMMANDS_COMPLETE");
27 ErrorInvalidValue("flags must be 0");
31 RefPtr
<WebGLSync
> globj
= new WebGLSync(this, condition
, flags
);
32 mPendingSyncs
.emplace_back(globj
);
33 EnsurePollPendingSyncs_Pending();
37 GLenum
WebGL2Context::ClientWaitSync(WebGLSync
& sync
, GLbitfield flags
,
39 const FuncScope
funcScope(*this, "clientWaitSync");
40 if (IsContextLost()) return LOCAL_GL_WAIT_FAILED
;
42 if (!ValidateObject("sync", sync
)) return LOCAL_GL_WAIT_FAILED
;
44 if (flags
!= 0 && flags
!= LOCAL_GL_SYNC_FLUSH_COMMANDS_BIT
) {
45 ErrorInvalidValue("`flags` must be SYNC_FLUSH_COMMANDS_BIT or 0.");
46 return LOCAL_GL_WAIT_FAILED
;
49 const auto ret
= sync
.ClientWaitSync(flags
, timeout
);
50 return UnderlyingValue(ret
);
53 void WebGLContext::EnsurePollPendingSyncs_Pending() const {
54 if (mPollPendingSyncs_Pending
) return;
55 mPollPendingSyncs_Pending
= NS_NewRunnableFunction(
56 "WebGLContext::PollPendingSyncs", [weak
= WeakPtr
{this}]() {
57 if (const auto strong
= RefPtr
{weak
.get()}) {
58 strong
->mPollPendingSyncs_Pending
= nullptr;
59 strong
->PollPendingSyncs();
60 if (strong
->mPendingSyncs
.size()) {
62 strong
->EnsurePollPendingSyncs_Pending();
66 if (const auto eventTarget
= GetCurrentSerialEventTarget()) {
67 eventTarget
->DelayedDispatch(do_AddRef(mPollPendingSyncs_Pending
),
68 kPollPendingSyncs_DelayMs
);
71 "[EnsurePollPendingSyncs_Pending] GetCurrentSerialEventTarget() -> "
76 void WebGLContext::PollPendingSyncs() const {
77 const FuncScope
funcScope(*this, "<pollPendingSyncs>");
78 if (IsContextLost()) return;
80 while (mPendingSyncs
.size()) {
81 if (const auto sync
= RefPtr
{mPendingSyncs
.front().get()}) {
82 const auto res
= sync
->ClientWaitSync(0, 0);
84 case ClientWaitSyncResult::WAIT_FAILED
:
85 case ClientWaitSyncResult::TIMEOUT_EXPIRED
:
87 case ClientWaitSyncResult::CONDITION_SATISFIED
:
88 case ClientWaitSyncResult::ALREADY_SIGNALED
:
89 // Communication back to child happens in sync->lientWaitSync.
93 mPendingSyncs
.pop_front();
97 } // namespace mozilla