WebGLSync.cpp (2092B)
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/. */ 5 6 #include "WebGLSync.h" 7 8 #include "GLContext.h" 9 #include "WebGLContext.h" 10 #include "mozilla/dom/WebGL2RenderingContextBinding.h" 11 12 namespace mozilla { 13 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; 19 } 20 21 WebGLSync::~WebGLSync() { 22 if (!mContext) return; 23 mContext->gl->fDeleteSync(mGLName); 24 mGLName = 0; 25 } 26 27 ClientWaitSyncResult WebGLSync::ClientWaitSync(const GLbitfield flags, 28 const GLuint64 timeout) { 29 if (!mContext) return ClientWaitSyncResult::WAIT_FAILED; 30 if (IsKnownComplete()) return ClientWaitSyncResult::ALREADY_SIGNALED; 31 if (!mGLName) return ClientWaitSyncResult::WAIT_FAILED; 32 33 auto ret = ClientWaitSyncResult::WAIT_FAILED; 34 bool newlyComplete = false; 35 const auto status = static_cast<ClientWaitSyncResult>( 36 mContext->gl->fClientWaitSync(mGLName, 0, 0)); 37 switch (status) { 38 case ClientWaitSyncResult::TIMEOUT_EXPIRED: // Poll() -> false 39 case ClientWaitSyncResult::WAIT_FAILED: // Error 40 ret = status; 41 break; 42 case ClientWaitSyncResult::CONDITION_SATISFIED: // Should never happen, but 43 // tolerate it. 44 case ClientWaitSyncResult::ALREADY_SIGNALED: // Poll() -> true 45 newlyComplete = true; 46 ret = status; 47 break; 48 } 49 50 if (newlyComplete) { 51 if (mContext->mCompletedFenceId < mFenceId) { 52 mContext->mCompletedFenceId = mFenceId; 53 } 54 MOZ_RELEASE_ASSERT(mOnCompleteTasks); 55 for (const auto& task : *mOnCompleteTasks) { 56 (*task)(); 57 } 58 mOnCompleteTasks = {}; 59 } 60 return ret; 61 } 62 63 } // namespace mozilla