WebGLContextLossHandler.cpp (1335B)
1 /* -*- Mode: C++; tab-width: 20; 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 "WebGLContextLossHandler.h" 7 8 #include "WebGLContext.h" 9 #include "base/message_loop.h" 10 11 namespace mozilla { 12 13 WebGLContextLossHandler::WebGLContextLossHandler(WebGLContext* const webgl) 14 : mTimerIsScheduled(false) { 15 const auto weak = WeakPtr<WebGLContext>(webgl); 16 mRunnable = NS_NewRunnableFunction("WebGLContextLossHandler", [weak]() { 17 const auto strong = RefPtr<WebGLContext>(weak); 18 if (!strong) { 19 return; 20 } 21 strong->CheckForContextLoss(); 22 }); 23 } 24 25 WebGLContextLossHandler::~WebGLContextLossHandler() = default; 26 27 //////////////////// 28 29 void WebGLContextLossHandler::RunTimer() { 30 const uint32_t kDelayMS = 1000; 31 MOZ_ASSERT(GetCurrentSerialEventTarget()); 32 // Only create a new task if one isn't already queued. 33 if (mTimerIsScheduled.compareExchange(false, true)) { 34 GetCurrentSerialEventTarget()->DelayedDispatch(do_AddRef(mRunnable), 35 kDelayMS); 36 } 37 } 38 39 void WebGLContextLossHandler::ClearTimer() { mTimerIsScheduled = false; } 40 41 } // namespace mozilla