blob: d7fc96d2557afec5fdebc3d1433b0ebae49fc91b (
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "CanvasShutdownManager.h"
#include "mozilla/AppShutdown.h"
#include "mozilla/dom/CanvasRenderingContext2D.h"
#include "mozilla/dom/WorkerPrivate.h"
#include "mozilla/dom/WorkerRef.h"
#include "mozilla/dom/WorkerRunnable.h"
#include "mozilla/gfx/CanvasManagerChild.h"
using namespace mozilla::dom;
namespace mozilla::gfx {
StaticMutex CanvasShutdownManager::sManagersMutex;
std::set<CanvasShutdownManager*> CanvasShutdownManager::sManagers;
// The owning thread will tell us to close when it is shutdown, either via
// CanvasShutdownManager::Shutdown for the main thread, or via a shutdown
// callback from ThreadSafeWorkerRef for worker threads.
MOZ_THREAD_LOCAL(CanvasShutdownManager*) CanvasShutdownManager::sLocalManager;
CanvasShutdownManager::CanvasShutdownManager(StrongWorkerRef* aWorkerRef)
: mWorkerRef(new ThreadSafeWorkerRef(aWorkerRef)) {}
CanvasShutdownManager::CanvasShutdownManager() = default;
CanvasShutdownManager::~CanvasShutdownManager() = default;
void CanvasShutdownManager::Destroy() {
std::set<CanvasRenderingContext2D*> activeCanvas = std::move(mActiveCanvas);
for (const auto& i : activeCanvas) {
i->OnShutdown();
}
CanvasManagerChild::Shutdown();
mWorkerRef = nullptr;
}
/* static */ void CanvasShutdownManager::Shutdown() {
auto* manager = MaybeGet();
if (!manager) {
return;
}
{
StaticMutexAutoLock lock(sManagersMutex);
sManagers.erase(manager);
}
sLocalManager.set(nullptr);
manager->Destroy();
delete manager;
}
/* static */ CanvasShutdownManager* CanvasShutdownManager::MaybeGet() {
if (NS_WARN_IF(!sLocalManager.init())) {
return nullptr;
}
return sLocalManager.get();
}
/* static */ CanvasShutdownManager* CanvasShutdownManager::Get() {
if (NS_WARN_IF(!sLocalManager.init())) {
return nullptr;
}
CanvasShutdownManager* managerWeak = sLocalManager.get();
if (managerWeak) {
return managerWeak;
}
if (WorkerPrivate* worker = GetCurrentThreadWorkerPrivate()) {
// The ThreadSafeWorkerRef will let us know when the worker is shutting
// down. This will let us clear our threadlocal reference and close the
// actor. We rely upon an explicit shutdown for the main thread.
RefPtr<StrongWorkerRef> workerRef = StrongWorkerRef::Create(
worker, "CanvasShutdownManager", []() { Shutdown(); });
if (NS_WARN_IF(!workerRef)) {
return nullptr;
}
CanvasShutdownManager* manager = new CanvasShutdownManager(workerRef);
sLocalManager.set(manager);
StaticMutexAutoLock lock(sManagersMutex);
sManagers.insert(manager);
return manager;
}
if (NS_IsMainThread()) {
if (NS_WARN_IF(
AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed))) {
return nullptr;
}
CanvasShutdownManager* manager = new CanvasShutdownManager();
sLocalManager.set(manager);
StaticMutexAutoLock lock(sManagersMutex);
sManagers.insert(manager);
return manager;
}
MOZ_ASSERT_UNREACHABLE("Can only be used on main or DOM worker threads!");
return nullptr;
}
void CanvasShutdownManager::AddShutdownObserver(
dom::CanvasRenderingContext2D* aCanvas) {
mActiveCanvas.insert(aCanvas);
}
void CanvasShutdownManager::RemoveShutdownObserver(
dom::CanvasRenderingContext2D* aCanvas) {
mActiveCanvas.erase(aCanvas);
}
void CanvasShutdownManager::OnRemoteCanvasLost() {
// Note that the canvas cannot do anything that mutates our state. It will
// dispatch for anything that risks re-entrancy.
for (const auto& canvas : mActiveCanvas) {
canvas->OnRemoteCanvasLost();
}
}
void CanvasShutdownManager::OnRemoteCanvasRestored() {
// Note that the canvas cannot do anything that mutates our state. It will
// dispatch for anything that risks re-entrancy.
for (const auto& canvas : mActiveCanvas) {
canvas->OnRemoteCanvasRestored();
}
}
/* static */ void CanvasShutdownManager::MaybeRestoreRemoteCanvas() {
// Calling Get will recreate the CanvasManagerChild, which in turn will
// cause us to call OnRemoteCanvasRestore upon success.
if (CanvasShutdownManager* manager = MaybeGet()) {
if (!manager->mActiveCanvas.empty()) {
CanvasManagerChild::Get();
}
}
}
/* static */ void CanvasShutdownManager::OnCompositorManagerRestored() {
MOZ_ASSERT(NS_IsMainThread());
class RestoreRunnable final : public WorkerRunnable {
public:
explicit RestoreRunnable(WorkerPrivate* aWorkerPrivate)
: WorkerRunnable(aWorkerPrivate,
"CanvasShutdownManager::RestoreRunnable") {}
bool WorkerRun(JSContext*, WorkerPrivate*) override {
MaybeRestoreRemoteCanvas();
return true;
}
};
// We can restore the main thread canvases immediately.
MaybeRestoreRemoteCanvas();
// And dispatch to restore any DOM worker canvases. This is safe because we
// remove the manager from sManagers before clearing mWorkerRef during DOM
// worker shutdown.
StaticMutexAutoLock lock(sManagersMutex);
for (const auto& manager : sManagers) {
if (manager->mWorkerRef) {
auto task = MakeRefPtr<RestoreRunnable>(manager->mWorkerRef->Private());
task->Dispatch();
}
}
}
} // namespace mozilla::gfx
|