OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "modules/compositorworker/CompositorWorkerThread.h" | 5 #include "modules/compositorworker/CompositorWorkerThread.h" |
6 | 6 |
7 #include "bindings/core/v8/V8GCController.h" | |
8 #include "bindings/core/v8/V8Initializer.h" | |
9 #include "core/workers/InProcessWorkerObjectProxy.h" | 7 #include "core/workers/InProcessWorkerObjectProxy.h" |
10 #include "core/workers/WorkerBackingThread.h" | |
11 #include "core/workers/WorkerThreadStartupData.h" | 8 #include "core/workers/WorkerThreadStartupData.h" |
12 #include "modules/compositorworker/CompositorWorkerGlobalScope.h" | 9 #include "modules/compositorworker/CompositorWorkerGlobalScope.h" |
13 #include "platform/CrossThreadFunctional.h" | |
14 #include "platform/TraceEvent.h" | 10 #include "platform/TraceEvent.h" |
15 #include "platform/WaitableEvent.h" | |
16 #include "platform/WebThreadSupportingGC.h" | |
17 #include "public/platform/Platform.h" | |
18 #include "wtf/Assertions.h" | 11 #include "wtf/Assertions.h" |
19 #include "wtf/PtrUtil.h" | |
20 #include <memory> | 12 #include <memory> |
21 | 13 |
22 namespace blink { | 14 namespace blink { |
23 | 15 |
24 namespace { | |
25 | |
26 // This is a singleton class holding the compositor worker thread in this | |
27 // renderer process. BackingThreadHolder::m_thread is cleared by | |
28 // ModulesInitializer::shutdown. | |
29 // See WorkerThread::terminateAndWaitForAllWorkers for the process shutdown | |
30 // case. | |
31 class BackingThreadHolder { | |
32 public: | |
33 static BackingThreadHolder& instance() | |
34 { | |
35 MutexLocker locker(holderInstanceMutex()); | |
36 return *s_instance; | |
37 } | |
38 | |
39 static void ensureInstance() | |
40 { | |
41 if (!s_instance) | |
42 s_instance = new BackingThreadHolder; | |
43 } | |
44 | |
45 static void clear() | |
46 { | |
47 MutexLocker locker(holderInstanceMutex()); | |
48 if (s_instance) { | |
49 s_instance->shutdownAndWait(); | |
50 delete s_instance; | |
51 s_instance = nullptr; | |
52 } | |
53 } | |
54 | |
55 static void createForTest() | |
56 { | |
57 MutexLocker locker(holderInstanceMutex()); | |
58 DCHECK_EQ(nullptr, s_instance); | |
59 s_instance = new BackingThreadHolder(WorkerBackingThread::createForTest(
Platform::current()->compositorThread())); | |
60 } | |
61 | |
62 WorkerBackingThread* thread() { return m_thread.get(); } | |
63 | |
64 private: | |
65 BackingThreadHolder(std::unique_ptr<WorkerBackingThread> useBackingThread =
nullptr) | |
66 : m_thread(useBackingThread ? std::move(useBackingThread) : WorkerBackin
gThread::create(Platform::current()->compositorThread())) | |
67 { | |
68 DCHECK(isMainThread()); | |
69 m_thread->backingThread().postTask(BLINK_FROM_HERE, crossThreadBind(&Bac
kingThreadHolder::initializeOnThread, crossThreadUnretained(this))); | |
70 } | |
71 | |
72 static Mutex& holderInstanceMutex() | |
73 { | |
74 DEFINE_THREAD_SAFE_STATIC_LOCAL(Mutex, holderMutex, new Mutex); | |
75 return holderMutex; | |
76 } | |
77 | |
78 void initializeOnThread() | |
79 { | |
80 MutexLocker locker(holderInstanceMutex()); | |
81 DCHECK(!m_initialized); | |
82 m_thread->initialize(); | |
83 m_initialized = true; | |
84 } | |
85 | |
86 void shutdownAndWait() | |
87 { | |
88 DCHECK(isMainThread()); | |
89 WaitableEvent doneEvent; | |
90 m_thread->backingThread().postTask(BLINK_FROM_HERE, crossThreadBind(&Bac
kingThreadHolder::shutdownOnThread, crossThreadUnretained(this), crossThreadUnre
tained(&doneEvent))); | |
91 doneEvent.wait(); | |
92 } | |
93 | |
94 void shutdownOnThread(WaitableEvent* doneEvent) | |
95 { | |
96 m_thread->shutdown(); | |
97 doneEvent->signal(); | |
98 } | |
99 | |
100 std::unique_ptr<WorkerBackingThread> m_thread; | |
101 bool m_initialized = false; | |
102 | |
103 static BackingThreadHolder* s_instance; | |
104 }; | |
105 | |
106 BackingThreadHolder* BackingThreadHolder::s_instance = nullptr; | |
107 | |
108 } // namespace | |
109 | |
110 std::unique_ptr<CompositorWorkerThread> CompositorWorkerThread::create(PassRefPt
r<WorkerLoaderProxy> workerLoaderProxy, InProcessWorkerObjectProxy& workerObject
Proxy, double timeOrigin) | 16 std::unique_ptr<CompositorWorkerThread> CompositorWorkerThread::create(PassRefPt
r<WorkerLoaderProxy> workerLoaderProxy, InProcessWorkerObjectProxy& workerObject
Proxy, double timeOrigin) |
111 { | 17 { |
112 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork
erThread::create"); | 18 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork
erThread::create"); |
113 ASSERT(isMainThread()); | 19 ASSERT(isMainThread()); |
114 return wrapUnique(new CompositorWorkerThread(workerLoaderProxy, workerObject
Proxy, timeOrigin)); | 20 return wrapUnique(new CompositorWorkerThread(workerLoaderProxy, workerObject
Proxy, timeOrigin)); |
115 } | 21 } |
116 | 22 |
117 CompositorWorkerThread::CompositorWorkerThread(PassRefPtr<WorkerLoaderProxy> wor
kerLoaderProxy, InProcessWorkerObjectProxy& workerObjectProxy, double timeOrigin
) | 23 CompositorWorkerThread::CompositorWorkerThread(PassRefPtr<WorkerLoaderProxy> wor
kerLoaderProxy, InProcessWorkerObjectProxy& workerObjectProxy, double timeOrigin
) |
118 : WorkerThread(workerLoaderProxy, workerObjectProxy) | 24 : AbstractAnimationWorkletThread(workerLoaderProxy, workerObjectProxy) |
119 , m_workerObjectProxy(workerObjectProxy) | 25 , m_workerObjectProxy(workerObjectProxy) |
120 , m_timeOrigin(timeOrigin) | 26 , m_timeOrigin(timeOrigin) |
121 { | 27 { |
122 } | 28 } |
123 | 29 |
124 CompositorWorkerThread::~CompositorWorkerThread() | 30 CompositorWorkerThread::~CompositorWorkerThread() |
125 { | 31 { |
126 } | 32 } |
127 | 33 |
128 WorkerBackingThread& CompositorWorkerThread::workerBackingThread() | |
129 { | |
130 return *BackingThreadHolder::instance().thread(); | |
131 } | |
132 | |
133 WorkerOrWorkletGlobalScope* CompositorWorkerThread::createWorkerGlobalScope(std:
:unique_ptr<WorkerThreadStartupData> startupData) | 34 WorkerOrWorkletGlobalScope* CompositorWorkerThread::createWorkerGlobalScope(std:
:unique_ptr<WorkerThreadStartupData> startupData) |
134 { | 35 { |
135 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork
erThread::createWorkerGlobalScope"); | 36 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork
erThread::createWorkerGlobalScope"); |
136 return CompositorWorkerGlobalScope::create(this, std::move(startupData), m_t
imeOrigin); | 37 return CompositorWorkerGlobalScope::create(this, std::move(startupData), m_t
imeOrigin); |
137 } | 38 } |
138 | 39 |
139 void CompositorWorkerThread::ensureSharedBackingThread() | |
140 { | |
141 DCHECK(isMainThread()); | |
142 BackingThreadHolder::ensureInstance(); | |
143 } | |
144 | |
145 void CompositorWorkerThread::clearSharedBackingThread() | |
146 { | |
147 DCHECK(isMainThread()); | |
148 BackingThreadHolder::clear(); | |
149 } | |
150 | |
151 void CompositorWorkerThread::createSharedBackingThreadForTest() | |
152 { | |
153 BackingThreadHolder::createForTest(); | |
154 } | |
155 | |
156 } // namespace blink | 40 } // namespace blink |
OLD | NEW |