OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "config.h" |
| 6 #include "modules/worklet/CompositorWorkletThread.h" |
| 7 |
| 8 #include "bindings/core/v8/GlobalScopeScriptController.h" |
| 9 #include "bindings/core/v8/V8Binding.h" |
| 10 #include "bindings/core/v8/V8GCController.h" |
| 11 #include "bindings/core/v8/V8Initializer.h" |
| 12 #include "bindings/core/v8/V8PerIsolateData.h" |
| 13 #include "bindings/core/v8/V8RecursionScope.h" |
| 14 #include "core/dom/Microtask.h" |
| 15 #include "modules/worklet/WorkletGlobalScope.h" |
| 16 #include "platform/Logging.h" |
| 17 #include "platform/Task.h" |
| 18 #include "platform/ThreadSafeFunctional.h" |
| 19 #include "platform/WebThreadSupportingGC.h" |
| 20 #include "public/platform/Platform.h" |
| 21 #include "public/platform/WebThread.h" |
| 22 |
| 23 namespace blink { |
| 24 |
| 25 class WorkletMicrotaskRunner : public WebThread::TaskObserver { |
| 26 public: |
| 27 explicit WorkletMicrotaskRunner(CompositorWorkletThread* compositorWorkletTh
read) |
| 28 : m_compositorWorkletThread(compositorWorkletThread) |
| 29 { |
| 30 } |
| 31 |
| 32 void willProcessTask() final {} |
| 33 |
| 34 void didProcessTask() final |
| 35 { |
| 36 Microtask::performCheckpoint(m_compositorWorkletThread->isolate()); |
| 37 } |
| 38 |
| 39 private: |
| 40 // Thread owns the microtask runner; reference remains |
| 41 // valid for the lifetime of this object. |
| 42 CompositorWorkletThread* m_compositorWorkletThread; |
| 43 }; |
| 44 |
| 45 class CompositorWorkletSharedState { |
| 46 public: |
| 47 static CompositorWorkletSharedState& instance() |
| 48 { |
| 49 DEFINE_STATIC_LOCAL_THREAD_SAFE(CompositorWorkletSharedState, compositor
WorkletSharedState, (new CompositorWorkletSharedState())); |
| 50 return compositorWorkletSharedState; |
| 51 } |
| 52 |
| 53 WebThreadSupportingGC* compositorWorkletThread() |
| 54 { |
| 55 MutexLocker lock(m_mutex); |
| 56 if (!m_thread && isMainThread()) { |
| 57 WebThread* platformThread = Platform::current()->compositorThread(); |
| 58 m_thread = WebThreadSupportingGC::createForThread(platformThread); |
| 59 } |
| 60 return m_thread.get(); |
| 61 } |
| 62 |
| 63 void initializeBackingThread() |
| 64 { |
| 65 MutexLocker lock(m_mutex); |
| 66 ASSERT(m_thread->isCurrentThread()); |
| 67 |
| 68 m_thread->initialize(); |
| 69 |
| 70 // Initialize the isolate at the same time. |
| 71 ASSERT(!m_isolate); |
| 72 m_isolate = V8PerIsolateData::initialize(); |
| 73 V8Initializer::initializeWorker(m_isolate); |
| 74 |
| 75 OwnPtr<V8IsolateInterruptor> interruptor = adoptPtr(new V8IsolateInterru
ptor(m_isolate)); |
| 76 ThreadState::current()->addInterruptor(interruptor.release()); |
| 77 ThreadState::current()->registerTraceDOMWrappers(m_isolate, V8GCControll
er::traceDOMWrappers); |
| 78 } |
| 79 |
| 80 v8::Isolate* isolate() |
| 81 { |
| 82 MutexLocker lock(m_mutex); |
| 83 ASSERT(m_thread->isCurrentThread()); |
| 84 ASSERT(m_isolate); |
| 85 // It is safe to use the existing isolate even if TerminateExecution() h
as been |
| 86 // called on it, without calling CancelTerminateExecution(). |
| 87 return m_isolate; |
| 88 } |
| 89 |
| 90 private: |
| 91 CompositorWorkletSharedState() {} |
| 92 ~CompositorWorkletSharedState() {} |
| 93 |
| 94 Mutex m_mutex; |
| 95 OwnPtr<WebThreadSupportingGC> m_thread; |
| 96 v8::Isolate* m_isolate = nullptr; |
| 97 }; |
| 98 |
| 99 PassRefPtr<CompositorWorkletThread> CompositorWorkletThread::create() |
| 100 { |
| 101 ASSERT(isMainThread()); |
| 102 return adoptRef(new CompositorWorkletThread()); |
| 103 } |
| 104 |
| 105 CompositorWorkletThread::CompositorWorkletThread() |
| 106 { |
| 107 } |
| 108 |
| 109 CompositorWorkletThread::~CompositorWorkletThread() |
| 110 { |
| 111 } |
| 112 |
| 113 void CompositorWorkletThread::initialize() |
| 114 { |
| 115 ASSERT(isMainThread()); |
| 116 |
| 117 if (m_initialized) |
| 118 return; |
| 119 |
| 120 m_initialized = true; |
| 121 backingThread().postTask(BLINK_FROM_HERE, new Task(threadSafeBind(&Composito
rWorkletThread::initializeInternal, AllowCrossThreadAccess(this)))); |
| 122 } |
| 123 |
| 124 void CompositorWorkletThread::initializeInternal() |
| 125 { |
| 126 ASSERT(backingThread().isCurrentThread()); |
| 127 |
| 128 { |
| 129 MutexLocker lock(m_threadStateMutex); |
| 130 |
| 131 m_microtaskRunner = adoptPtr(new WorkletMicrotaskRunner(this)); |
| 132 CompositorWorkletSharedState::instance().initializeBackingThread(); |
| 133 backingThread().addTaskObserver(m_microtaskRunner.get()); |
| 134 |
| 135 m_isolate = CompositorWorkletSharedState::instance().isolate(); |
| 136 m_workletGlobalScope = WorkletGlobalScope::create(m_isolate); |
| 137 } |
| 138 } |
| 139 |
| 140 void CompositorWorkletThread::loadScript(const String& sourceCode) |
| 141 { |
| 142 ASSERT(isMainThread()); |
| 143 backingThread().postTask(BLINK_FROM_HERE, new Task(threadSafeBind(&Composito
rWorkletThread::loadScriptInternal, AllowCrossThreadAccess(this), sourceCode.iso
latedCopy()))); |
| 144 } |
| 145 |
| 146 void CompositorWorkletThread::loadScriptInternal(const String& sourceCode) |
| 147 { |
| 148 ASSERT(backingThread().isCurrentThread()); |
| 149 |
| 150 ScriptState::Scope scope(m_workletGlobalScope->script()->scriptState()); |
| 151 V8RecursionScope recursionScope(m_workletGlobalScope->isolate()); |
| 152 |
| 153 v8::Local<v8::String> source = v8::String::NewFromUtf8(m_workletGlobalScope-
>isolate(), sourceCode.ascii().data(), v8::NewStringType::kNormal).ToLocalChecke
d(); |
| 154 v8::Local<v8::Script> script = v8::Script::Compile(m_workletGlobalScope->scr
ipt()->context(), source).ToLocalChecked(); |
| 155 v8::Local<v8::Value> result = script->Run(m_workletGlobalScope->script()->co
ntext()).ToLocalChecked(); |
| 156 |
| 157 WTF_LOG(NotYetImplemented, "loadScriptInternal: %lf\n", v8::Number::Cast(*re
sult)->Value()); |
| 158 } |
| 159 |
| 160 WebThreadSupportingGC& CompositorWorkletThread::backingThread() |
| 161 { |
| 162 return *CompositorWorkletSharedState::instance().compositorWorkletThread(); |
| 163 } |
| 164 |
| 165 } // namespace blink |
OLD | NEW |