Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/canvas/WebGLContextLossHandler.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "mozilla/DebugOnly.h"
9
#include "nsINamed.h"
10
#include "nsISupportsImpl.h"
11
#include "nsITimer.h"
12
#include "nsThreadUtils.h"
13
#include "WebGLContext.h"
14
15
namespace mozilla {
16
17
class WatchdogTimerEvent final : public nsITimerCallback
18
                               , public nsINamed
19
{
20
    const WeakPtr<WebGLContextLossHandler> mHandler;
21
22
public:
23
    NS_DECL_ISUPPORTS
24
25
    explicit WatchdogTimerEvent(WebGLContextLossHandler* handler)
26
        : mHandler(handler)
27
0
    { }
28
29
    NS_IMETHOD GetName(nsACString& aName) override
30
0
    {
31
0
      aName.AssignLiteral("WatchdogTimerEvent");
32
0
      return NS_OK;
33
0
    }
34
35
private:
36
0
    virtual ~WatchdogTimerEvent() { }
37
38
0
    NS_IMETHOD Notify(nsITimer*) override {
39
0
        if (mHandler) {
40
0
            mHandler->TimerCallback();
41
0
        }
42
0
        return NS_OK;
43
0
    }
44
};
45
46
NS_IMPL_ISUPPORTS(WatchdogTimerEvent, nsITimerCallback, nsINamed)
47
48
////////////////////////////////////////
49
50
WebGLContextLossHandler::WebGLContextLossHandler(WebGLContext* webgl)
51
    : mWebGL(webgl)
52
    , mTimer(NS_NewTimer())
53
    , mTimerPending(false)
54
    , mShouldRunTimerAgain(false)
55
#ifdef DEBUG
56
    , mEventTarget(GetCurrentThreadSerialEventTarget())
57
#endif
58
0
{
59
0
    MOZ_ASSERT(mEventTarget);
60
0
}
61
62
WebGLContextLossHandler::~WebGLContextLossHandler()
63
0
{
64
0
    const DebugOnly<nsISerialEventTarget*> callingThread = GetCurrentThreadSerialEventTarget();
65
0
    MOZ_ASSERT(!callingThread || mEventTarget->IsOnCurrentThread());
66
0
}
67
68
////////////////////
69
70
void
71
WebGLContextLossHandler::RunTimer()
72
0
{
73
0
    MOZ_ASSERT(mEventTarget->IsOnCurrentThread());
74
0
75
0
    // If the timer was already running, don't restart it here. Instead,
76
0
    // wait until the previous call is done, then fire it one more time.
77
0
    // This is also an optimization to prevent unnecessary
78
0
    // cross-communication between threads.
79
0
    if (mTimerPending) {
80
0
        mShouldRunTimerAgain = true;
81
0
        return;
82
0
    }
83
0
84
0
    const RefPtr<WatchdogTimerEvent> event = new WatchdogTimerEvent(this);
85
0
    const uint32_t kDelayMS = 1000;
86
0
    mTimer->InitWithCallback(event, kDelayMS, nsITimer::TYPE_ONE_SHOT);
87
0
88
0
    mTimerPending = true;
89
0
}
90
91
////////////////////
92
93
void
94
WebGLContextLossHandler::TimerCallback()
95
0
{
96
0
    MOZ_ASSERT(mEventTarget->IsOnCurrentThread());
97
0
98
0
    mTimerPending = false;
99
0
100
0
    const bool runOnceMore = mShouldRunTimerAgain;
101
0
    mShouldRunTimerAgain = false;
102
0
103
0
    mWebGL->UpdateContextLossStatus();
104
0
105
0
    if (runOnceMore && !mTimerPending) {
106
0
        RunTimer();
107
0
    }
108
0
}
109
110
} // namespace mozilla