Files
palemoon27/dom/canvas/WebGLContextLossHandler.cpp
T
roytam1 ed2747a092 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1174043 - Run DisableTimer if timer is not yet disabled. r=dglastonbury (43c299c62d)
- Bug 1178893 - Made state/gl-enum-tests Conformant. r=kamidphish (1633473eae)
- Bug 1206907 - GenerateWarning should output to Web Console, not Browser Console. - r=bholley (6932f2268b)
- Bug 1217550 - In debug build, drop a note if we use SkiaGL canvas. Simplify logging defines and flags for non-critical errors. r=benwa (25bf7f409e)
- Bug 1142975 - Check for Present()-caused context loss. - r=kamidphish (54fb28fca5)
- Bug 1167176 - Only premultiply webgl snapshot pixels if alpha is used. r=jgilbert (514b386faa)
- Bug 1159131 - Work around false positive -Wuninitialized gcc warnings in DEBUG PGO builds of WebGLElementArrayCache.cpp. r=jgilbert (6ed90182df)
- Bug 1192472 - Only do supplemental isArray check if WorkAroundDriverBugs. - r=kamidphish (dd483b334f)
- Bug 1156582 - Initialize WebGLShader bools in constructor. r=jgilbert (274a9e2f94)
- Bug 1106138 - Cause crash when an unimplemented pack mode is used. Implement packing for RGBA32F format, which is used in WebGL 1.0.2 conformance. r=jgilbert (f50837881a)
- Bug 1186111 - Implemented pack unpremultiply for 32f formats. r=jgilbert (e9c9817fc9)
- Bug 1186111 - Implemented pack unpremultiply for 16f formats. r=jgilbert (a7de6d243c)
- Bug 1186129 - Fix assignment of alpha in pack premultiply RA16F. Also made a cosmetic change to the assignment of alpha in pack premultiply RA32F so that it is more consistent with the other pack functions. r=jgilbert (af4ed85388)
- fix misspatch (6648b81b59)
- Bug 1228711 - Cubemap images must be square. - r=benwa (67449ebf1e)
- Bug 1225576. Always use the VAO workaround. r=jgilbert (8a60a1fede)
- Bug 1223445 - KeyframeEffectReadOnly objects end up keeping lots of other objects alive too long, r=birtles (399e829c30)
- Bug 1194639 part 1 - Add AutoMutationBatchForAnimation; r=heycam (efad01861e)
- Bug 1194639 part 2 - Report changes to playbackRate to animation mutation observers; r=heycam (f02c335b45)
- Bug 1194639 part 3 - Report changes to startTime to animation mutation observers; r=heycam (6e75a26308)
- Bug 1194639 part 4 - Report changes to currentTime to animation mutation observers; r=heycam (9de00c380d)
- Bug 1186684 - Remove unused ErrorResult in Animation::SetPlaybackRate/SilentlySetPlaybackRate. r=bbirtles (49f0661411)
- Bug 1194639 part 5 - Report changes from calling reverse() to animation mutation observers; r=heycam (547a21978a)
- Bug 1194639 part 6 - Report changes from calling finish() to animation mutation observers; r=heycam (8d844bff3b)
- Bug 1194639 part 7 - Report changes from calling play() to animation mutation observers; r=heycam (0f512e80b1)
- Bug 1194639 part 8 - Report changes from calling pause() to animation mutation observers; r=heycam (e72119572b)
- Bug 1207951 - Fix buggy logic in ComposeStyle; r=heycam (481ee753fb)
- Bug 1161320 - Fix conflict between finishing and aborting a pause; r=jwatt (5ad0b89e91)
- Bug 1216846 - Don't update hold time when completing a pause if it is already set; r=heycam (28f3301a4e)
- Bug 1209519 - Undef GetCurrentTime in DocumentTimeline.h to fix --disable-accessibility bustage. r=birtles (4c3bec65a3)
- Bug 1230056 part 1 - Add EffectCompositor::HasAnimationsForCompositor; r=dholbert (fbe737d428)
- Bug 1230056 part 2 - Remove nsLayoutUtils::HasAnimationsForCompositor and call EffectCompositor::HasAnimationsForCompositor directly; r=dholbert (df22cd42a6)
- Bug 1194037 part 5 - Make AnimationProperty::operator== ignore mWinsInCascade; r=heycam (3b2409fe3f)
- Bug 1231107 - add an assert on animationProperty. r=heycam (372c0975ed)
- Bug 1216872 - Make 100% Keyframe returned by getFrames() have easing:linear. r=birtles (25e98055d5)
- Bug 1227384 - Move LOCAL_INCLUDES and DEFINES to moz.build in js/src/. r=mshal (5e825ebfbe)
- Bug 1225298 - Use GCHashMap for liveScopes, r=terrence (4aa4aaf59d)
- Bug 1134253 - Improve object extensibility TypeError messages produced from Object.defineProperty; r=jorendorff (9c32f595d2)
- Bug 1227677 - Extract PNK_COMPREHENSIONFOR from PNK_FOR so that comprehension gunk doesn't taint for-loop emitting logic. r=shu (67e7547c25)
2023-05-25 10:55:35 +08:00

239 lines
6.2 KiB
C++

/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* 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 "WebGLContextLossHandler.h"
#include "nsITimer.h"
#include "nsThreadUtils.h"
#include "WebGLContext.h"
#include "mozilla/dom/WorkerPrivate.h"
namespace mozilla {
// -------------------------------------------------------------------
// Begin worker specific code
// -------------------------------------------------------------------
// On workers we can only dispatch CancelableRunnables, so we have to wrap the
// timer's EventTarget to use our own cancelable runnable
class ContextLossWorkerEventTarget final : public nsIEventTarget
{
public:
explicit ContextLossWorkerEventTarget(nsIEventTarget* aEventTarget)
: mEventTarget(aEventTarget)
{
MOZ_ASSERT(aEventTarget);
}
NS_DECL_NSIEVENTTARGET
NS_DECL_THREADSAFE_ISUPPORTS
protected:
~ContextLossWorkerEventTarget() {}
private:
nsCOMPtr<nsIEventTarget> mEventTarget;
};
class ContextLossWorkerRunnable final : public nsICancelableRunnable
{
public:
explicit ContextLossWorkerRunnable(nsIRunnable* aRunnable)
: mRunnable(aRunnable)
{
}
NS_DECL_NSICANCELABLERUNNABLE
NS_DECL_THREADSAFE_ISUPPORTS
NS_FORWARD_NSIRUNNABLE(mRunnable->)
protected:
~ContextLossWorkerRunnable() {}
private:
nsCOMPtr<nsIRunnable> mRunnable;
};
NS_IMPL_ISUPPORTS(ContextLossWorkerEventTarget, nsIEventTarget,
nsISupports)
NS_IMETHODIMP
ContextLossWorkerEventTarget::DispatchFromScript(nsIRunnable* aEvent, uint32_t aFlags)
{
nsCOMPtr<nsIRunnable> event(aEvent);
return Dispatch(event.forget(), aFlags);
}
NS_IMETHODIMP
ContextLossWorkerEventTarget::Dispatch(already_AddRefed<nsIRunnable>&& aEvent,
uint32_t aFlags)
{
nsCOMPtr<nsIRunnable> eventRef(aEvent);
RefPtr<ContextLossWorkerRunnable> wrappedEvent =
new ContextLossWorkerRunnable(eventRef);
return mEventTarget->Dispatch(wrappedEvent, aFlags);
}
NS_IMETHODIMP
ContextLossWorkerEventTarget::IsOnCurrentThread(bool* aResult)
{
return mEventTarget->IsOnCurrentThread(aResult);
}
NS_IMPL_ISUPPORTS(ContextLossWorkerRunnable, nsICancelableRunnable,
nsIRunnable)
NS_IMETHODIMP
ContextLossWorkerRunnable::Cancel()
{
mRunnable = nullptr;
return NS_OK;
}
// -------------------------------------------------------------------
// End worker-specific code
// -------------------------------------------------------------------
WebGLContextLossHandler::WebGLContextLossHandler(WebGLContext* webgl)
: mWeakWebGL(webgl)
, mTimer(do_CreateInstance(NS_TIMER_CONTRACTID))
, mIsTimerRunning(false)
, mShouldRunTimerAgain(false)
, mIsDisabled(false)
, mFeatureAdded(false)
#ifdef DEBUG
, mThread(NS_GetCurrentThread())
#endif
{
}
WebGLContextLossHandler::~WebGLContextLossHandler()
{
MOZ_ASSERT(!mIsTimerRunning);
}
void
WebGLContextLossHandler::StartTimer(unsigned long delayMS)
{
// We can't pass an already_AddRefed through InitWithFuncCallback, so we
// should do the AddRef/Release manually.
this->AddRef();
mTimer->InitWithFuncCallback(StaticTimerCallback,
static_cast<void*>(this),
delayMS,
nsITimer::TYPE_ONE_SHOT);
}
/*static*/ void
WebGLContextLossHandler::StaticTimerCallback(nsITimer*, void* voidHandler)
{
typedef WebGLContextLossHandler T;
T* handler = static_cast<T*>(voidHandler);
handler->TimerCallback();
// Release the AddRef from StartTimer.
handler->Release();
}
void
WebGLContextLossHandler::TimerCallback()
{
MOZ_ASSERT(NS_GetCurrentThread() == mThread);
MOZ_ASSERT(mIsTimerRunning);
mIsTimerRunning = false;
if (mIsDisabled)
return;
// If we need to run the timer again, restart it immediately.
// Otherwise, the code we call into below might *also* try to
// restart it.
if (mShouldRunTimerAgain) {
RunTimer();
MOZ_ASSERT(mIsTimerRunning);
}
if (mWeakWebGL) {
mWeakWebGL->UpdateContextLossStatus();
}
}
void
WebGLContextLossHandler::RunTimer()
{
MOZ_ASSERT(!mIsDisabled);
// If the timer was already running, don't restart it here. Instead,
// wait until the previous call is done, then fire it one more time.
// This is an optimization to prevent unnecessary
// cross-communication between threads.
if (mIsTimerRunning) {
mShouldRunTimerAgain = true;
return;
}
if (!NS_IsMainThread()) {
dom::workers::WorkerPrivate* workerPrivate =
dom::workers::GetCurrentThreadWorkerPrivate();
nsCOMPtr<nsIEventTarget> target = workerPrivate->GetEventTarget();
mTimer->SetTarget(new ContextLossWorkerEventTarget(target));
if (!mFeatureAdded) {
workerPrivate->AddFeature(workerPrivate->GetJSContext(), this);
mFeatureAdded = true;
}
}
StartTimer(1000);
mIsTimerRunning = true;
mShouldRunTimerAgain = false;
}
void
WebGLContextLossHandler::DisableTimer()
{
if (mIsDisabled)
return;
mIsDisabled = true;
if (mFeatureAdded) {
dom::workers::WorkerPrivate* workerPrivate =
dom::workers::GetCurrentThreadWorkerPrivate();
MOZ_RELEASE_ASSERT(workerPrivate);
workerPrivate->RemoveFeature(workerPrivate->GetJSContext(), this);
mFeatureAdded = false;
}
// We can't just Cancel() the timer, as sometimes we end up
// receiving a callback after calling Cancel(). This could cause us
// to receive the callback after object destruction.
// Instead, we let the timer finish, but ignore it.
if (!mIsTimerRunning)
return;
mTimer->SetDelay(0);
}
bool
WebGLContextLossHandler::Notify(JSContext* aCx, dom::workers::Status aStatus)
{
bool isWorkerRunning = aStatus < dom::workers::Closing;
if (!isWorkerRunning && mIsTimerRunning) {
mIsTimerRunning = false;
this->Release();
}
return true;
}
} // namespace mozilla