| 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 "web/CompositorProxyClientImpl.h" | |
| 6 | |
| 7 #include "core/dom/CompositorProxy.h" | |
| 8 #include "modules/compositorworker/CompositorWorkerGlobalScope.h" | |
| 9 #include "platform/graphics/CompositorMutableStateProvider.h" | |
| 10 #include "platform/instrumentation/tracing/TraceEvent.h" | |
| 11 #include "web/CompositorMutatorImpl.h" | |
| 12 #include "wtf/CurrentTime.h" | |
| 13 | |
| 14 namespace blink { | |
| 15 | |
| 16 // A helper class that updates proxies mutable state on creation and reset it | |
| 17 // on destruction. This can be used with rAF callback to ensure no mutation is | |
| 18 // allowed outside rAF. | |
| 19 class ScopedCompositorMutableState final { | |
| 20 WTF_MAKE_NONCOPYABLE(ScopedCompositorMutableState); | |
| 21 STACK_ALLOCATED(); | |
| 22 | |
| 23 public: | |
| 24 ScopedCompositorMutableState( | |
| 25 HeapHashSet<WeakMember<CompositorProxy>>& proxies, | |
| 26 CompositorMutableStateProvider* stateProvider) | |
| 27 : m_proxies(proxies) { | |
| 28 for (CompositorProxy* proxy : m_proxies) | |
| 29 proxy->takeCompositorMutableState( | |
| 30 stateProvider->getMutableStateFor(proxy->elementId())); | |
| 31 } | |
| 32 ~ScopedCompositorMutableState() { | |
| 33 for (CompositorProxy* proxy : m_proxies) | |
| 34 proxy->takeCompositorMutableState(nullptr); | |
| 35 } | |
| 36 | |
| 37 private: | |
| 38 HeapHashSet<WeakMember<CompositorProxy>>& m_proxies; | |
| 39 }; | |
| 40 | |
| 41 CompositorProxyClientImpl::CompositorProxyClientImpl( | |
| 42 CompositorMutatorImpl* mutator) | |
| 43 : m_mutator(mutator), m_globalScope(nullptr) { | |
| 44 DCHECK(isMainThread()); | |
| 45 } | |
| 46 | |
| 47 DEFINE_TRACE(CompositorProxyClientImpl) { | |
| 48 CompositorProxyClient::trace(visitor); | |
| 49 visitor->trace(m_proxies); | |
| 50 } | |
| 51 | |
| 52 void CompositorProxyClientImpl::setGlobalScope(WorkerGlobalScope* scope) { | |
| 53 TRACE_EVENT0("compositor-worker", | |
| 54 "CompositorProxyClientImpl::setGlobalScope"); | |
| 55 DCHECK(!m_globalScope); | |
| 56 DCHECK(scope); | |
| 57 m_globalScope = static_cast<CompositorWorkerGlobalScope*>(scope); | |
| 58 m_mutator->registerProxyClient(this); | |
| 59 } | |
| 60 | |
| 61 void CompositorProxyClientImpl::dispose() { | |
| 62 // CompositorProxyClientImpl and CompositorMutatorImpl form a reference | |
| 63 // cycle. CompositorWorkerGlobalScope and CompositorProxyClientImpl | |
| 64 // also form another big reference cycle. So dispose needs to be called on | |
| 65 // Worker termination to break these cycles. If not, layout test leak | |
| 66 // detection will report a WorkerGlobalScope leak. | |
| 67 m_mutator->unregisterProxyClient(this); | |
| 68 m_globalScope = nullptr; | |
| 69 } | |
| 70 | |
| 71 void CompositorProxyClientImpl::requestAnimationFrame() { | |
| 72 TRACE_EVENT0("compositor-worker", | |
| 73 "CompositorProxyClientImpl::requestAnimationFrame"); | |
| 74 m_requestedAnimationFrameCallbacks = true; | |
| 75 m_mutator->setNeedsMutate(); | |
| 76 } | |
| 77 | |
| 78 bool CompositorProxyClientImpl::mutate( | |
| 79 double monotonicTimeNow, | |
| 80 CompositorMutableStateProvider* stateProvider) { | |
| 81 if (!m_globalScope) | |
| 82 return false; | |
| 83 | |
| 84 TRACE_EVENT0("compositor-worker", "CompositorProxyClientImpl::mutate"); | |
| 85 if (!m_requestedAnimationFrameCallbacks) | |
| 86 return false; | |
| 87 | |
| 88 { | |
| 89 ScopedCompositorMutableState mutableState(m_proxies, stateProvider); | |
| 90 m_requestedAnimationFrameCallbacks = | |
| 91 executeAnimationFrameCallbacks(monotonicTimeNow); | |
| 92 } | |
| 93 | |
| 94 return m_requestedAnimationFrameCallbacks; | |
| 95 } | |
| 96 | |
| 97 bool CompositorProxyClientImpl::executeAnimationFrameCallbacks( | |
| 98 double monotonicTimeNow) { | |
| 99 TRACE_EVENT0("compositor-worker", | |
| 100 "CompositorProxyClientImpl::executeAnimationFrameCallbacks"); | |
| 101 | |
| 102 DCHECK(m_globalScope); | |
| 103 // Convert to zero based document time in milliseconds consistent with | |
| 104 // requestAnimationFrame. | |
| 105 double highResTimeMs = | |
| 106 1000.0 * (monotonicTimeNow - m_globalScope->timeOrigin()); | |
| 107 return m_globalScope->executeAnimationFrameCallbacks(highResTimeMs); | |
| 108 } | |
| 109 | |
| 110 void CompositorProxyClientImpl::registerCompositorProxy( | |
| 111 CompositorProxy* proxy) { | |
| 112 m_proxies.insert(proxy); | |
| 113 } | |
| 114 | |
| 115 void CompositorProxyClientImpl::unregisterCompositorProxy( | |
| 116 CompositorProxy* proxy) { | |
| 117 m_proxies.remove(proxy); | |
| 118 } | |
| 119 | |
| 120 } // namespace blink | |
| OLD | NEW |