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