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