OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "platform/graphics/CompositorMutatorClient.h" | 5 #include "platform/graphics/CompositorMutatorClient.h" |
6 | 6 |
| 7 #include <memory> |
7 #include "base/bind.h" | 8 #include "base/bind.h" |
8 #include "base/callback.h" | 9 #include "base/callback.h" |
9 #include "base/trace_event/trace_event.h" | 10 #include "base/trace_event/trace_event.h" |
10 #include "cc/trees/layer_tree_impl.h" | 11 #include "cc/trees/layer_tree_impl.h" |
| 12 #include "cc/trees/scroll_node.h" |
| 13 #include "platform/graphics/CompositorElementId.h" |
11 #include "platform/graphics/CompositorMutableStateProvider.h" | 14 #include "platform/graphics/CompositorMutableStateProvider.h" |
12 #include "platform/graphics/CompositorMutation.h" | 15 #include "platform/graphics/CompositorMutation.h" |
13 #include "platform/graphics/CompositorMutationsTarget.h" | 16 #include "platform/graphics/CompositorMutationsTarget.h" |
14 #include "platform/graphics/CompositorMutator.h" | 17 #include "platform/graphics/CompositorMutator.h" |
15 #include "wtf/PtrUtil.h" | 18 #include "wtf/PtrUtil.h" |
16 #include <memory> | |
17 | 19 |
18 namespace blink { | 20 namespace blink { |
| 21 namespace { |
| 22 void updateLayerTree(cc::LayerTreeImpl* treeImpl, |
| 23 uint64_t elementId, |
| 24 const CompositorMutation* mutation) { |
| 25 cc::LayerImpl* layer = treeImpl->LayerByElementId( |
| 26 createCompositorElementId(elementId, CompositorSubElementId::Primary)); |
| 27 cc::LayerImpl* scrollLayer = treeImpl->LayerByElementId( |
| 28 createCompositorElementId(elementId, CompositorSubElementId::Scroll)); |
| 29 if (layer) { |
| 30 if (mutation->isOpacityMutated()) { |
| 31 treeImpl->property_trees()->effect_tree.OnOpacityAnimated( |
| 32 mutation->opacity(), layer->effect_tree_index(), treeImpl); |
| 33 } |
| 34 if (mutation->isTransformMutated()) { |
| 35 treeImpl->property_trees()->transform_tree.OnTransformAnimated( |
| 36 gfx::Transform(mutation->transform()), layer->transform_tree_index(), |
| 37 treeImpl); |
| 38 } |
| 39 } |
| 40 if (scrollLayer) { |
| 41 gfx::ScrollOffset offset = scrollLayer->CurrentScrollOffset(); |
| 42 if (mutation->isScrollLeftMutated()) { |
| 43 offset.set_x(mutation->scrollLeft()); |
| 44 } |
| 45 if (mutation->isScrollTopMutated()) { |
| 46 offset.set_y(mutation->scrollTop()); |
| 47 } |
| 48 if (mutation->isScrollLeftMutated() || mutation->isScrollTopMutated()) { |
| 49 treeImpl->property_trees()->scroll_tree.OnScrollOffsetAnimated( |
| 50 scrollLayer->id(), scrollLayer->scroll_tree_index(), offset, |
| 51 treeImpl); |
| 52 } |
| 53 } |
| 54 } |
| 55 } // namespace |
19 | 56 |
20 CompositorMutatorClient::CompositorMutatorClient( | 57 CompositorMutatorClient::CompositorMutatorClient( |
21 CompositorMutator* mutator, | 58 CompositorMutator* mutator, |
22 CompositorMutationsTarget* mutationsTarget) | 59 CompositorMutationsTarget* mutationsTarget) |
23 : m_client(nullptr), | 60 : m_client(nullptr), |
24 m_mutationsTarget(mutationsTarget), | 61 m_mutationsTarget(mutationsTarget), |
25 m_mutator(mutator), | 62 m_mutator(mutator), |
26 m_mutations(nullptr) { | 63 m_mutations(nullptr) { |
27 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), | 64 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), |
28 "CompositorMutatorClient::CompositorMutatorClient"); | 65 "CompositorMutatorClient::CompositorMutatorClient"); |
29 } | 66 } |
30 | 67 |
31 CompositorMutatorClient::~CompositorMutatorClient() { | 68 CompositorMutatorClient::~CompositorMutatorClient() { |
32 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), | 69 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), |
33 "CompositorMutatorClient::~CompositorMutatorClient"); | 70 "CompositorMutatorClient::~CompositorMutatorClient"); |
34 } | 71 } |
35 | 72 |
36 bool CompositorMutatorClient::Mutate(base::TimeTicks monotonicTime, | 73 bool CompositorMutatorClient::Mutate(base::TimeTicks monotonicTime, |
37 cc::LayerTreeImpl* treeImpl) { | 74 cc::LayerTreeImpl* treeImpl) { |
38 TRACE_EVENT0("compositor-worker", "CompositorMutatorClient::Mutate"); | 75 TRACE_EVENT0("compositor-worker", "CompositorMutatorClient::Mutate"); |
39 double monotonicTimeNow = (monotonicTime - base::TimeTicks()).InSecondsF(); | 76 double monotonicTimeNow = (monotonicTime - base::TimeTicks()).InSecondsF(); |
40 if (!m_mutations) | 77 if (!m_mutations) |
41 m_mutations = WTF::wrapUnique(new CompositorMutations); | 78 m_mutations = WTF::wrapUnique(new CompositorMutations); |
42 CompositorMutableStateProvider compositorState(treeImpl, m_mutations.get()); | 79 |
| 80 ProxyCompositorMutablePropertiesMap inputPropertiesMap; |
| 81 snapshotLayerTree(treeImpl, &inputPropertiesMap); |
| 82 |
| 83 CompositorMutations newMutations; |
| 84 CompositorMutableStateProvider compositorState(&inputPropertiesMap, |
| 85 &newMutations); |
43 bool shouldReinvoke = m_mutator->mutate(monotonicTimeNow, &compositorState); | 86 bool shouldReinvoke = m_mutator->mutate(monotonicTimeNow, &compositorState); |
| 87 |
| 88 for (const auto& entry : newMutations.map) { |
| 89 updateLayerTree(treeImpl, entry.key, entry.value.get()); |
| 90 } |
| 91 |
| 92 // TODO(sfunkenhauser): Temporary work around. |
| 93 // Currently ScrollTree:OnScrollOffsetAnimated triggers a main frame begin |
| 94 // synchronously, which will call TakeMutations causing m_mutations to be |
| 95 // released. Make sure m_mutations is not null before calling updateMutations. |
| 96 if (!m_mutations) |
| 97 m_mutations = WTF::wrapUnique(new CompositorMutations); |
| 98 |
| 99 // After this loop newMutations will no longer hold any CompositorMutation |
| 100 // objects. CompositorMutation objects are moved to updateMutations to reduce |
| 101 // the number of CompositorMutation objects that are allocated. |
| 102 for (auto& entry : newMutations.map) { |
| 103 updateMutations(entry.key, std::move(entry.value)); |
| 104 } |
| 105 |
44 return shouldReinvoke; | 106 return shouldReinvoke; |
45 } | 107 } |
46 | 108 |
47 void CompositorMutatorClient::SetClient(cc::LayerTreeMutatorClient* client) { | 109 void CompositorMutatorClient::SetClient(cc::LayerTreeMutatorClient* client) { |
48 TRACE_EVENT0("compositor-worker", "CompositorMutatorClient::SetClient"); | 110 TRACE_EVENT0("compositor-worker", "CompositorMutatorClient::SetClient"); |
49 m_client = client; | 111 m_client = client; |
50 setNeedsMutate(); | 112 setNeedsMutate(); |
51 } | 113 } |
52 | 114 |
53 base::Closure CompositorMutatorClient::TakeMutations() { | 115 base::Closure CompositorMutatorClient::TakeMutations() { |
54 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), | 116 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), |
55 "CompositorMutatorClient::TakeMutations"); | 117 "CompositorMutatorClient::TakeMutations"); |
56 if (!m_mutations) | 118 if (!m_mutations) |
57 return base::Closure(); | 119 return base::Closure(); |
58 | |
59 return base::Bind(&CompositorMutationsTarget::applyMutations, | 120 return base::Bind(&CompositorMutationsTarget::applyMutations, |
60 base::Unretained(m_mutationsTarget), | 121 base::Unretained(m_mutationsTarget), |
61 base::Owned(m_mutations.release())); | 122 base::Owned(m_mutations.release())); |
62 } | 123 } |
63 | 124 |
64 void CompositorMutatorClient::setNeedsMutate() { | 125 void CompositorMutatorClient::setNeedsMutate() { |
65 TRACE_EVENT0("compositor-worker", "CompositorMutatorClient::setNeedsMutate"); | 126 TRACE_EVENT0("compositor-worker", "CompositorMutatorClient::setNeedsMutate"); |
66 m_client->SetNeedsMutate(); | 127 m_client->SetNeedsMutate(); |
67 } | 128 } |
68 | 129 |
| 130 void CompositorMutatorClient::registerCompositorProxy( |
| 131 uint64_t proxyId, |
| 132 uint64_t elementId, |
| 133 uint32_t mutableProperties) { |
| 134 TRACE_EVENT0("compositor-worker", |
| 135 "CompositorMutatorClient::registerCompositorProxy"); |
| 136 m_inputProperties[proxyId] = std::make_pair(elementId, mutableProperties); |
| 137 } |
| 138 |
| 139 void CompositorMutatorClient::unregisterCompositorProxy(uint64_t proxyId) { |
| 140 TRACE_EVENT0("compositor-worker", |
| 141 "CompositorMutatorClient::unregisterCompositorProxy"); |
| 142 DCHECK(m_inputProperties.find(proxyId) != m_inputProperties.end()); |
| 143 m_inputProperties.erase(proxyId); |
| 144 } |
| 145 |
69 void CompositorMutatorClient::setMutationsForTesting( | 146 void CompositorMutatorClient::setMutationsForTesting( |
70 std::unique_ptr<CompositorMutations> mutations) { | 147 std::unique_ptr<CompositorMutations> mutations) { |
71 m_mutations = std::move(mutations); | 148 m_mutations = std::move(mutations); |
72 } | 149 } |
73 | 150 |
| 151 void CompositorMutatorClient::snapshotLayerTree( |
| 152 const cc::LayerTreeImpl* treeImpl, |
| 153 ProxyCompositorMutablePropertiesMap* inputMap) const { |
| 154 for (const auto& it : m_inputProperties) { |
| 155 uint64_t proxyId = it.first; |
| 156 const std::pair<uint64_t, uint32_t>& value = it.second; |
| 157 uint64_t elementId = value.first; |
| 158 uint32_t properties = value.second; |
| 159 cc::LayerImpl* layer = treeImpl->LayerByElementId( |
| 160 createCompositorElementId(elementId, CompositorSubElementId::Primary)); |
| 161 cc::LayerImpl* scrollLayer = treeImpl->LayerByElementId( |
| 162 createCompositorElementId(elementId, CompositorSubElementId::Scroll)); |
| 163 if (!layer && !scrollLayer) |
| 164 continue; |
| 165 (*inputMap)[proxyId].elementId = elementId; |
| 166 (*inputMap)[proxyId].transform = SkMatrix44::I(); |
| 167 if (layer) { |
| 168 if (properties & CompositorMutableProperty::kOpacity) { |
| 169 (*inputMap)[proxyId].opacity = layer->Opacity(); |
| 170 } |
| 171 if (properties & CompositorMutableProperty::kTransform) { |
| 172 (*inputMap)[proxyId].transform = layer->Transform().matrix(); |
| 173 } |
| 174 } |
| 175 if (scrollLayer) { |
| 176 if (properties & CompositorMutableProperty::kScrollLeft) { |
| 177 (*inputMap)[proxyId].scrollLeft = |
| 178 scrollLayer->CurrentScrollOffset().x(); |
| 179 } |
| 180 if (properties & CompositorMutableProperty::kScrollTop) { |
| 181 (*inputMap)[proxyId].scrollTop = scrollLayer->CurrentScrollOffset().y(); |
| 182 } |
| 183 } |
| 184 } |
| 185 } |
| 186 |
| 187 void CompositorMutatorClient::updateMutations( |
| 188 uint64_t elementId, |
| 189 std::unique_ptr<CompositorMutation> mutation) { |
| 190 if (m_mutations->map.contains(elementId)) { |
| 191 CompositorMutation* existingMutation = m_mutations->map.at(elementId); |
| 192 if (mutation->isOpacityMutated()) { |
| 193 existingMutation->setOpacity(mutation->opacity()); |
| 194 } |
| 195 if (mutation->isTransformMutated()) { |
| 196 existingMutation->setTransform(mutation->transform()); |
| 197 } |
| 198 if (mutation->isScrollTopMutated()) { |
| 199 existingMutation->setScrollTop(mutation->scrollTop()); |
| 200 } |
| 201 if (mutation->isScrollLeftMutated()) { |
| 202 existingMutation->setScrollLeft(mutation->scrollLeft()); |
| 203 } |
| 204 } else { |
| 205 m_mutations->map.set(elementId, std::move(mutation)); |
| 206 } |
| 207 } |
| 208 |
74 } // namespace blink | 209 } // namespace blink |
OLD | NEW |