Bug 1874684 - Part 4: Prefer const references instead of copying Instant values....
[gecko.git] / dom / canvas / WebGLSync.cpp
blob6a8e9403f7a062ab6e05062aa942870dcd7e232a
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 "WebGLSync.h"
8 #include "GLContext.h"
9 #include "mozilla/dom/WebGL2RenderingContextBinding.h"
10 #include "WebGLContext.h"
12 namespace mozilla {
14 WebGLSync::WebGLSync(WebGLContext* webgl, GLenum condition, GLbitfield flags)
15 : WebGLContextBoundObject(webgl),
16 mGLName(mContext->gl->fFenceSync(condition, flags)),
17 mFenceId(mContext->mNextFenceId) {
18 mContext->mNextFenceId += 1;
21 WebGLSync::~WebGLSync() {
22 if (!mContext) return;
23 mContext->gl->fDeleteSync(mGLName);
26 ClientWaitSyncResult WebGLSync::ClientWaitSync(const GLbitfield flags,
27 const GLuint64 timeout) {
28 if (!mContext) return ClientWaitSyncResult::WAIT_FAILED;
29 if (IsKnownComplete()) return ClientWaitSyncResult::ALREADY_SIGNALED;
31 auto ret = ClientWaitSyncResult::WAIT_FAILED;
32 bool newlyComplete = false;
33 const auto status = static_cast<ClientWaitSyncResult>(
34 mContext->gl->fClientWaitSync(mGLName, 0, 0));
35 switch (status) {
36 case ClientWaitSyncResult::TIMEOUT_EXPIRED: // Poll() -> false
37 case ClientWaitSyncResult::WAIT_FAILED: // Error
38 ret = status;
39 break;
40 case ClientWaitSyncResult::CONDITION_SATISFIED: // Should never happen, but
41 // tolerate it.
42 case ClientWaitSyncResult::ALREADY_SIGNALED: // Poll() -> true
43 newlyComplete = true;
44 ret = status;
45 break;
48 if (newlyComplete) {
49 if (mContext->mCompletedFenceId < mFenceId) {
50 mContext->mCompletedFenceId = mFenceId;
52 MOZ_RELEASE_ASSERT(mOnCompleteTasks);
53 for (const auto& task : *mOnCompleteTasks) {
54 (*task)();
56 mOnCompleteTasks = {};
58 return ret;
61 } // namespace mozilla