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 <memory> |
| 8 |
8 #include "base/bind.h" | 9 #include "base/bind.h" |
9 #include "base/callback.h" | 10 #include "base/callback.h" |
10 #include "base/trace_event/trace_event.h" | 11 #include "base/trace_event/trace_event.h" |
11 #include "cc/trees/layer_tree_impl.h" | 12 #include "cc/trees/layer_tree_impl.h" |
| 13 #include "cc/trees/scroll_node.h" |
| 14 #include "platform/graphics/CompositorElementId.h" |
12 #include "platform/graphics/CompositorMutableStateProvider.h" | 15 #include "platform/graphics/CompositorMutableStateProvider.h" |
13 #include "platform/graphics/CompositorMutation.h" | 16 #include "platform/graphics/CompositorMutation.h" |
14 #include "platform/graphics/CompositorMutationsTarget.h" | 17 #include "platform/graphics/CompositorMutationsTarget.h" |
15 #include "platform/graphics/CompositorMutator.h" | 18 #include "platform/graphics/CompositorMutator.h" |
16 #include "platform/wtf/PtrUtil.h" | 19 #include "platform/wtf/PtrUtil.h" |
17 | 20 |
18 namespace blink { | 21 namespace blink { |
19 | 22 |
20 CompositorMutatorClient::CompositorMutatorClient( | 23 CompositorMutatorClient::CompositorMutatorClient( |
21 CompositorMutator* mutator, | 24 CompositorMutator* mutator, |
(...skipping 10 matching lines...) Expand all Loading... |
32 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), | 35 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), |
33 "CompositorMutatorClient::~CompositorMutatorClient"); | 36 "CompositorMutatorClient::~CompositorMutatorClient"); |
34 } | 37 } |
35 | 38 |
36 bool CompositorMutatorClient::Mutate(base::TimeTicks monotonic_time, | 39 bool CompositorMutatorClient::Mutate(base::TimeTicks monotonic_time, |
37 cc::LayerTreeImpl* tree_impl) { | 40 cc::LayerTreeImpl* tree_impl) { |
38 TRACE_EVENT0("compositor-worker", "CompositorMutatorClient::Mutate"); | 41 TRACE_EVENT0("compositor-worker", "CompositorMutatorClient::Mutate"); |
39 double monotonic_time_now = (monotonic_time - base::TimeTicks()).InSecondsF(); | 42 double monotonic_time_now = (monotonic_time - base::TimeTicks()).InSecondsF(); |
40 if (!mutations_) | 43 if (!mutations_) |
41 mutations_ = WTF::WrapUnique(new CompositorMutations); | 44 mutations_ = WTF::WrapUnique(new CompositorMutations); |
42 CompositorMutableStateProvider compositor_state(tree_impl, mutations_.get()); | 45 |
| 46 ProxyCompositorMutablePropertiesMap input_properties_map; |
| 47 SnapshotLayerTree(tree_impl, &input_properties_map); |
| 48 |
| 49 CompositorMutations new_mutations; |
| 50 CompositorMutableStateProvider compositor_state(&input_properties_map, |
| 51 &new_mutations); |
43 bool should_reinvoke = | 52 bool should_reinvoke = |
44 mutator_->Mutate(monotonic_time_now, &compositor_state); | 53 mutator_->Mutate(monotonic_time_now, &compositor_state); |
| 54 |
| 55 for (const auto& entry : new_mutations.map) { |
| 56 UpdateLayerTree(tree_impl, entry.key, entry.value.get()); |
| 57 } |
| 58 |
| 59 // CompositorMutation objects are moved to updateMutations rather than copied |
| 60 // to reduce the number of allocated objects. |
| 61 for (auto& entry : new_mutations.map) { |
| 62 UpdateMutations(entry.key, std::move(entry.value)); |
| 63 } |
| 64 |
45 return should_reinvoke; | 65 return should_reinvoke; |
46 } | 66 } |
47 | 67 |
48 void CompositorMutatorClient::SetClient(cc::LayerTreeMutatorClient* client) { | 68 void CompositorMutatorClient::SetClient(cc::LayerTreeMutatorClient* client) { |
49 TRACE_EVENT0("compositor-worker", "CompositorMutatorClient::SetClient"); | 69 TRACE_EVENT0("compositor-worker", "CompositorMutatorClient::SetClient"); |
50 client_ = client; | 70 client_ = client; |
51 SetNeedsMutate(); | 71 SetNeedsMutate(); |
52 } | 72 } |
53 | 73 |
54 base::Closure CompositorMutatorClient::TakeMutations() { | 74 base::Closure CompositorMutatorClient::TakeMutations() { |
55 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), | 75 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), |
56 "CompositorMutatorClient::TakeMutations"); | 76 "CompositorMutatorClient::TakeMutations"); |
57 if (!mutations_) | 77 if (!mutations_) |
58 return base::Closure(); | 78 return base::Closure(); |
59 | 79 |
60 return base::Bind(&CompositorMutationsTarget::ApplyMutations, | 80 return base::Bind(&CompositorMutationsTarget::ApplyMutations, |
61 base::Unretained(mutations_target_), | 81 base::Unretained(mutations_target_), |
62 base::Owned(mutations_.release())); | 82 base::Owned(mutations_.release())); |
63 } | 83 } |
64 | 84 |
65 void CompositorMutatorClient::SetNeedsMutate() { | 85 void CompositorMutatorClient::SetNeedsMutate() { |
66 TRACE_EVENT0("compositor-worker", "CompositorMutatorClient::setNeedsMutate"); | 86 TRACE_EVENT0("compositor-worker", "CompositorMutatorClient::setNeedsMutate"); |
67 client_->SetNeedsMutate(); | 87 client_->SetNeedsMutate(); |
68 } | 88 } |
69 | 89 |
| 90 void CompositorMutatorClient::RegisterCompositorProxy( |
| 91 uint64_t proxy_id, |
| 92 uint64_t element_id, |
| 93 uint32_t mutable_properties) { |
| 94 TRACE_EVENT0("compositor-worker", |
| 95 "CompositorMutatorClient::RegisterCompositorProxy"); |
| 96 input_properties_[proxy_id] = std::make_pair(element_id, mutable_properties); |
| 97 } |
| 98 |
| 99 void CompositorMutatorClient::UnregisterCompositorProxy(uint64_t proxy_id) { |
| 100 TRACE_EVENT0("compositor-worker", |
| 101 "CompositorMutatorClient::UnregisterCompositorProxy"); |
| 102 DCHECK(input_properties_.find(proxy_id) != input_properties_.end()); |
| 103 input_properties_.erase(proxy_id); |
| 104 } |
| 105 |
70 void CompositorMutatorClient::SetMutationsForTesting( | 106 void CompositorMutatorClient::SetMutationsForTesting( |
71 std::unique_ptr<CompositorMutations> mutations) { | 107 std::unique_ptr<CompositorMutations> mutations) { |
72 mutations_ = std::move(mutations); | 108 mutations_ = std::move(mutations); |
73 } | 109 } |
74 | 110 |
| 111 void CompositorMutatorClient::SnapshotLayerTree( |
| 112 const cc::LayerTreeImpl* tree_impl, |
| 113 ProxyCompositorMutablePropertiesMap* input_map) const { |
| 114 ProxyCompositorMutablePropertiesMap& input_map_ref = *input_map; |
| 115 for (const auto& it : input_properties_) { |
| 116 uint64_t proxy_id = it.first; |
| 117 const std::pair<uint64_t, uint32_t>& value = it.second; |
| 118 uint64_t element_id = value.first; |
| 119 uint32_t properties = value.second; |
| 120 |
| 121 // Avoid expensive layer lookups by first checking whether any properties |
| 122 // are being mutated that require that layer. |
| 123 cc::LayerImpl* layer = nullptr; |
| 124 cc::LayerImpl* scrollLayer = nullptr; |
| 125 if (properties & (CompositorMutableProperty::kOpacity | |
| 126 CompositorMutableProperty::kTransform)) { |
| 127 layer = tree_impl->LayerByElementId(CreateCompositorElementId( |
| 128 element_id, CompositorSubElementId::kPrimary)); |
| 129 } |
| 130 if (properties & (CompositorMutableProperty::kScrollLeft | |
| 131 CompositorMutableProperty::kScrollTop)) { |
| 132 scrollLayer = tree_impl->LayerByElementId(CreateCompositorElementId( |
| 133 element_id, CompositorSubElementId::kScroll)); |
| 134 } |
| 135 |
| 136 // If the element no longer exists in the layer tree, there is no point |
| 137 // creating an entry for it. |
| 138 // TODO(smcgruer): Should we just create a default entry instead? |
| 139 if (!layer && !scrollLayer) |
| 140 continue; |
| 141 |
| 142 input_map_ref[proxy_id].element_id = element_id; |
| 143 input_map_ref[proxy_id].transform = SkMatrix44::I(); |
| 144 if (layer) { |
| 145 if (properties & CompositorMutableProperty::kOpacity) { |
| 146 input_map_ref[proxy_id].opacity = layer->Opacity(); |
| 147 } |
| 148 if (properties & CompositorMutableProperty::kTransform) { |
| 149 input_map_ref[proxy_id].transform = layer->Transform().matrix(); |
| 150 } |
| 151 } |
| 152 if (scrollLayer) { |
| 153 if (properties & CompositorMutableProperty::kScrollLeft) { |
| 154 input_map_ref[proxy_id].scroll_left = |
| 155 scrollLayer->CurrentScrollOffset().x(); |
| 156 } |
| 157 if (properties & CompositorMutableProperty::kScrollTop) { |
| 158 input_map_ref[proxy_id].scroll_top = |
| 159 scrollLayer->CurrentScrollOffset().y(); |
| 160 } |
| 161 } |
| 162 } |
| 163 } |
| 164 |
| 165 void CompositorMutatorClient::UpdateLayerTree( |
| 166 cc::LayerTreeImpl* tree_impl, |
| 167 uint64_t element_id, |
| 168 const CompositorMutation* mutation) { |
| 169 uint32_t mutatedProperties = mutation->mutatedProperties(); |
| 170 |
| 171 // Avoid expensive layer lookups by checking first whether any relevant |
| 172 // properties have been mutated. |
| 173 if (mutatedProperties & (CompositorMutableProperty::kOpacity | |
| 174 CompositorMutableProperty::kTransform)) { |
| 175 cc::ElementId compositor_element_id = |
| 176 CreateCompositorElementId(element_id, CompositorSubElementId::kPrimary); |
| 177 cc::LayerImpl* layer = tree_impl->LayerByElementId(compositor_element_id); |
| 178 if (layer) { |
| 179 if (mutatedProperties & CompositorMutableProperty::kOpacity) { |
| 180 tree_impl->SetOpacityMutated(compositor_element_id, |
| 181 mutation->Opacity()); |
| 182 } |
| 183 if (mutatedProperties & CompositorMutableProperty::kTransform) { |
| 184 tree_impl->SetTransformMutated(compositor_element_id, |
| 185 gfx::Transform(mutation->Transform())); |
| 186 } |
| 187 } |
| 188 } |
| 189 |
| 190 if (mutatedProperties & (CompositorMutableProperty::kScrollLeft | |
| 191 CompositorMutableProperty::kScrollTop)) { |
| 192 cc::LayerImpl* scrollLayer = tree_impl->LayerByElementId( |
| 193 CreateCompositorElementId(element_id, CompositorSubElementId::kScroll)); |
| 194 if (scrollLayer) { |
| 195 gfx::ScrollOffset offset = scrollLayer->CurrentScrollOffset(); |
| 196 if (mutatedProperties & CompositorMutableProperty::kScrollLeft) { |
| 197 offset.set_x(mutation->ScrollLeft()); |
| 198 } |
| 199 if (mutatedProperties & CompositorMutableProperty::kScrollTop) { |
| 200 offset.set_y(mutation->ScrollTop()); |
| 201 } |
| 202 tree_impl->property_trees()->scroll_tree.OnScrollOffsetAnimated( |
| 203 scrollLayer->id(), scrollLayer->scroll_tree_index(), offset, |
| 204 tree_impl); |
| 205 |
| 206 // ScrollTree:OnScrollOffsetAnimated triggers a synchronous main frame |
| 207 // begin that calls TakeMutations and causes mutations_ to be released. |
| 208 // Restore mutations_ for the later call to |updateMutations|. |
| 209 // TODO(smcgruer): Find a way to avoid triggering a main frame begin here. |
| 210 DCHECK(!mutations_); |
| 211 mutations_ = WTF::WrapUnique(new CompositorMutations); |
| 212 } |
| 213 } |
| 214 } |
| 215 |
| 216 void CompositorMutatorClient::UpdateMutations( |
| 217 uint64_t element_id, |
| 218 std::unique_ptr<CompositorMutation> mutation) { |
| 219 if (!mutations_->map.Contains(element_id)) { |
| 220 mutations_->map.Set(element_id, std::move(mutation)); |
| 221 return; |
| 222 } |
| 223 |
| 224 CompositorMutation* existingMutation = mutations_->map.at(element_id); |
| 225 if (mutation->IsOpacityMutated()) { |
| 226 existingMutation->SetOpacity(mutation->Opacity()); |
| 227 } |
| 228 if (mutation->IsTransformMutated()) { |
| 229 existingMutation->SetTransform(mutation->Transform()); |
| 230 } |
| 231 if (mutation->IsScrollTopMutated()) { |
| 232 existingMutation->SetScrollTop(mutation->ScrollTop()); |
| 233 } |
| 234 if (mutation->IsScrollLeftMutated()) { |
| 235 existingMutation->SetScrollLeft(mutation->ScrollLeft()); |
| 236 } |
| 237 } |
| 238 |
75 } // namespace blink | 239 } // namespace blink |
OLD | NEW |