summaryrefslogtreecommitdiffstats
path: root/dom/canvas/WebGLSync.cpp
blob: 6a8e9403f7a062ab6e05062aa942870dcd7e232a (plain)
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "WebGLSync.h"

#include "GLContext.h"
#include "mozilla/dom/WebGL2RenderingContextBinding.h"
#include "WebGLContext.h"

namespace mozilla {

WebGLSync::WebGLSync(WebGLContext* webgl, GLenum condition, GLbitfield flags)
    : WebGLContextBoundObject(webgl),
      mGLName(mContext->gl->fFenceSync(condition, flags)),
      mFenceId(mContext->mNextFenceId) {
  mContext->mNextFenceId += 1;
}

WebGLSync::~WebGLSync() {
  if (!mContext) return;
  mContext->gl->fDeleteSync(mGLName);
}

ClientWaitSyncResult WebGLSync::ClientWaitSync(const GLbitfield flags,
                                               const GLuint64 timeout) {
  if (!mContext) return ClientWaitSyncResult::WAIT_FAILED;
  if (IsKnownComplete()) return ClientWaitSyncResult::ALREADY_SIGNALED;

  auto ret = ClientWaitSyncResult::WAIT_FAILED;
  bool newlyComplete = false;
  const auto status = static_cast<ClientWaitSyncResult>(
      mContext->gl->fClientWaitSync(mGLName, 0, 0));
  switch (status) {
    case ClientWaitSyncResult::TIMEOUT_EXPIRED:  // Poll() -> false
    case ClientWaitSyncResult::WAIT_FAILED:      // Error
      ret = status;
      break;
    case ClientWaitSyncResult::CONDITION_SATISFIED:  // Should never happen, but
                                                     // tolerate it.
    case ClientWaitSyncResult::ALREADY_SIGNALED:     // Poll() -> true
      newlyComplete = true;
      ret = status;
      break;
  }

  if (newlyComplete) {
    if (mContext->mCompletedFenceId < mFenceId) {
      mContext->mCompletedFenceId = mFenceId;
    }
    MOZ_RELEASE_ASSERT(mOnCompleteTasks);
    for (const auto& task : *mOnCompleteTasks) {
      (*task)();
    }
    mOnCompleteTasks = {};
  }
  return ret;
}

}  // namespace mozilla