xingliu | e43518a | 2016-07-28 23:59:47 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CC_TREES_LAYER_TREE_H_ |
| 6 | #define CC_TREES_LAYER_TREE_H_ |
| 7 | |
khushalsagar | 86928f9 | 2016-08-17 21:49:05 | [diff] [blame] | 8 | #include <memory> |
xingliu | e43518a | 2016-07-28 23:59:47 | [diff] [blame] | 9 | #include <unordered_map> |
| 10 | #include <unordered_set> |
xingliu | 95d9e6b6 | 2016-08-18 03:53:08 | [diff] [blame] | 11 | |
rnk | 2f03c7f | 2016-08-17 17:10:59 | [diff] [blame] | 12 | #include "base/macros.h" |
khushalsagar | 86928f9 | 2016-08-17 21:49:05 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
rnk | 2f03c7f | 2016-08-17 17:10:59 | [diff] [blame] | 14 | #include "cc/base/cc_export.h" |
khushalsagar | 86928f9 | 2016-08-17 21:49:05 | [diff] [blame] | 15 | #include "cc/input/event_listener_properties.h" |
| 16 | #include "cc/input/layer_selection_bound.h" |
| 17 | #include "cc/layers/layer_collections.h" |
xingliu | 95d9e6b6 | 2016-08-18 03:53:08 | [diff] [blame] | 18 | #include "cc/layers/layer_list_iterator.h" |
| 19 | #include "cc/trees/mutator_host_client.h" |
khushalsagar | 86928f9 | 2016-08-17 21:49:05 | [diff] [blame] | 20 | #include "cc/trees/property_tree.h" |
| 21 | #include "third_party/skia/include/core/SkColor.h" |
| 22 | #include "ui/gfx/geometry/size.h" |
| 23 | |
| 24 | namespace base { |
| 25 | class TimeDelta; |
| 26 | } // namespace base |
xingliu | e43518a | 2016-07-28 23:59:47 | [diff] [blame] | 27 | |
| 28 | namespace cc { |
| 29 | |
| 30 | namespace proto { |
| 31 | class LayerTree; |
| 32 | class LayerUpdate; |
khushalsagar | 86928f9 | 2016-08-17 21:49:05 | [diff] [blame] | 33 | } // namespace proto |
xingliu | e43518a | 2016-07-28 23:59:47 | [diff] [blame] | 34 | |
| 35 | class AnimationHost; |
khushalsagar | 86928f9 | 2016-08-17 21:49:05 | [diff] [blame] | 36 | class HeadsUpDisplayLayer; |
xingliu | e43518a | 2016-07-28 23:59:47 | [diff] [blame] | 37 | class Layer; |
khushalsagar | 86928f9 | 2016-08-17 21:49:05 | [diff] [blame] | 38 | class LayerTreeHost; |
| 39 | class LayerTreeImpl; |
khushalsagar | 8ec0740 | 2016-09-10 03:13:19 | [diff] [blame^] | 40 | class LayerTreeSettings; |
khushalsagar | 86928f9 | 2016-08-17 21:49:05 | [diff] [blame] | 41 | struct PendingPageScaleAnimation; |
khushalsagar | 8ec0740 | 2016-09-10 03:13:19 | [diff] [blame^] | 42 | class UIResourceManager; |
xingliu | e43518a | 2016-07-28 23:59:47 | [diff] [blame] | 43 | |
xingliu | 95d9e6b6 | 2016-08-18 03:53:08 | [diff] [blame] | 44 | class CC_EXPORT LayerTree : public MutatorHostClient { |
xingliu | e43518a | 2016-07-28 23:59:47 | [diff] [blame] | 45 | public: |
| 46 | using LayerSet = std::unordered_set<Layer*>; |
| 47 | using LayerIdMap = std::unordered_map<int, Layer*>; |
| 48 | |
khushalsagar | 86928f9 | 2016-08-17 21:49:05 | [diff] [blame] | 49 | LayerTree(std::unique_ptr<AnimationHost> animation_host, |
| 50 | LayerTreeHost* layer_tree_host); |
| 51 | virtual ~LayerTree(); |
xingliu | e43518a | 2016-07-28 23:59:47 | [diff] [blame] | 52 | |
khushalsagar | 86928f9 | 2016-08-17 21:49:05 | [diff] [blame] | 53 | void SetRootLayer(scoped_refptr<Layer> root_layer); |
| 54 | Layer* root_layer() { return inputs_.root_layer.get(); } |
| 55 | const Layer* root_layer() const { return inputs_.root_layer.get(); } |
| 56 | |
| 57 | void RegisterViewportLayers(scoped_refptr<Layer> overscroll_elasticity_layer, |
| 58 | scoped_refptr<Layer> page_scale_layer, |
| 59 | scoped_refptr<Layer> inner_viewport_scroll_layer, |
| 60 | scoped_refptr<Layer> outer_viewport_scroll_layer); |
| 61 | |
| 62 | Layer* overscroll_elasticity_layer() const { |
| 63 | return inputs_.overscroll_elasticity_layer.get(); |
| 64 | } |
| 65 | Layer* page_scale_layer() const { return inputs_.page_scale_layer.get(); } |
| 66 | Layer* inner_viewport_scroll_layer() const { |
| 67 | return inputs_.inner_viewport_scroll_layer.get(); |
| 68 | } |
| 69 | Layer* outer_viewport_scroll_layer() const { |
| 70 | return inputs_.outer_viewport_scroll_layer.get(); |
| 71 | } |
| 72 | |
| 73 | void RegisterSelection(const LayerSelection& selection); |
| 74 | |
| 75 | void SetHaveScrollEventHandlers(bool have_event_handlers); |
| 76 | bool have_scroll_event_handlers() const { |
| 77 | return inputs_.have_scroll_event_handlers; |
| 78 | } |
| 79 | |
| 80 | void SetEventListenerProperties(EventListenerClass event_class, |
| 81 | EventListenerProperties event_properties); |
| 82 | EventListenerProperties event_listener_properties( |
| 83 | EventListenerClass event_class) const { |
| 84 | return inputs_.event_listener_properties[static_cast<size_t>(event_class)]; |
| 85 | } |
| 86 | |
| 87 | void SetViewportSize(const gfx::Size& device_viewport_size); |
| 88 | gfx::Size device_viewport_size() const { |
| 89 | return inputs_.device_viewport_size; |
| 90 | } |
| 91 | |
| 92 | void SetTopControlsHeight(float height, bool shrink); |
| 93 | void SetTopControlsShownRatio(float ratio); |
ianwen | e5fc578 | 2016-08-18 04:05:15 | [diff] [blame] | 94 | void SetBottomControlsHeight(float height); |
khushalsagar | 86928f9 | 2016-08-17 21:49:05 | [diff] [blame] | 95 | |
| 96 | void SetPageScaleFactorAndLimits(float page_scale_factor, |
| 97 | float min_page_scale_factor, |
| 98 | float max_page_scale_factor); |
| 99 | float page_scale_factor() const { return inputs_.page_scale_factor; } |
| 100 | |
| 101 | void set_background_color(SkColor color) { inputs_.background_color = color; } |
| 102 | SkColor background_color() const { return inputs_.background_color; } |
| 103 | |
| 104 | void set_has_transparent_background(bool transparent) { |
| 105 | inputs_.has_transparent_background = transparent; |
| 106 | } |
| 107 | |
| 108 | void StartPageScaleAnimation(const gfx::Vector2d& target_offset, |
| 109 | bool use_anchor, |
| 110 | float scale, |
| 111 | base::TimeDelta duration); |
| 112 | bool HasPendingPageScaleAnimation() const; |
| 113 | |
| 114 | void SetDeviceScaleFactor(float device_scale_factor); |
| 115 | float device_scale_factor() const { return inputs_.device_scale_factor; } |
| 116 | |
| 117 | void SetPaintedDeviceScaleFactor(float painted_device_scale_factor); |
| 118 | |
khushalsagar | 86928f9 | 2016-08-17 21:49:05 | [diff] [blame] | 119 | // Used externally by blink for setting the PropertyTrees when |
| 120 | // |settings_.use_layer_lists| is true. This is a SPV2 setting. |
| 121 | PropertyTrees* property_trees() { return &property_trees_; } |
| 122 | |
khushalsagar | cebe494 | 2016-09-07 23:27:01 | [diff] [blame] | 123 | void SetNeedsDisplayOnAllLayers(); |
xingliu | 95d9e6b6 | 2016-08-18 03:53:08 | [diff] [blame] | 124 | |
khushalsagar | 8ec0740 | 2016-09-10 03:13:19 | [diff] [blame^] | 125 | UIResourceManager* GetUIResourceManager() const; |
| 126 | const LayerTreeSettings& GetSettings() const; |
| 127 | |
khushalsagar | 86928f9 | 2016-08-17 21:49:05 | [diff] [blame] | 128 | // Methods which should only be used internally in cc ------------------ |
xingliu | e43518a | 2016-07-28 23:59:47 | [diff] [blame] | 129 | void RegisterLayer(Layer* layer); |
| 130 | void UnregisterLayer(Layer* layer); |
| 131 | Layer* LayerById(int id) const; |
khushalsagar | cebe494 | 2016-09-07 23:27:01 | [diff] [blame] | 132 | |
xingliu | e43518a | 2016-07-28 23:59:47 | [diff] [blame] | 133 | bool UpdateLayers(const LayerList& update_layer_list, |
| 134 | bool* content_is_suitable_for_gpu); |
khushalsagar | cebe494 | 2016-09-07 23:27:01 | [diff] [blame] | 135 | bool in_paint_layer_contents() const { return in_paint_layer_contents_; } |
xingliu | e43518a | 2016-07-28 23:59:47 | [diff] [blame] | 136 | |
| 137 | void AddLayerShouldPushProperties(Layer* layer); |
| 138 | void RemoveLayerShouldPushProperties(Layer* layer); |
| 139 | std::unordered_set<Layer*>& LayersThatShouldPushProperties(); |
| 140 | bool LayerNeedsPushPropertiesForTesting(Layer* layer) const; |
| 141 | |
khushalsagar | 86928f9 | 2016-08-17 21:49:05 | [diff] [blame] | 142 | virtual void SetNeedsMetaInfoRecomputation( |
| 143 | bool needs_meta_info_recomputation); |
| 144 | bool needs_meta_info_recomputation() const { |
| 145 | return needs_meta_info_recomputation_; |
| 146 | } |
| 147 | |
| 148 | void SetPageScaleFromImplSide(float page_scale); |
| 149 | void SetElasticOverscrollFromImplSide(gfx::Vector2dF elastic_overscroll); |
khushalsagar | cebe494 | 2016-09-07 23:27:01 | [diff] [blame] | 150 | gfx::Vector2dF elastic_overscroll() const { return elastic_overscroll_; } |
khushalsagar | 86928f9 | 2016-08-17 21:49:05 | [diff] [blame] | 151 | |
| 152 | void UpdateHudLayer(bool show_hud_info); |
| 153 | HeadsUpDisplayLayer* hud_layer() const { return hud_layer_.get(); } |
| 154 | |
| 155 | virtual void SetNeedsFullTreeSync(); |
| 156 | bool needs_full_tree_sync() const { return needs_full_tree_sync_; } |
| 157 | |
| 158 | void SetNeedsCommit(); |
| 159 | void SetPropertyTreesNeedRebuild(); |
| 160 | |
| 161 | void PushPropertiesTo(LayerTreeImpl* tree_impl); |
| 162 | |
xingliu | e43518a | 2016-07-28 23:59:47 | [diff] [blame] | 163 | void ToProtobuf(proto::LayerTree* proto); |
| 164 | void FromProtobuf(const proto::LayerTree& proto); |
| 165 | |
xingliu | 95d9e6b6 | 2016-08-18 03:53:08 | [diff] [blame] | 166 | AnimationHost* animation_host() const { return animation_host_.get(); } |
| 167 | |
| 168 | Layer* LayerByElementId(ElementId element_id) const; |
| 169 | void RegisterElement(ElementId element_id, |
| 170 | ElementListType list_type, |
| 171 | Layer* layer); |
| 172 | void UnregisterElement(ElementId element_id, |
| 173 | ElementListType list_type, |
| 174 | Layer* layer); |
| 175 | void SetElementIdsForTesting(); |
| 176 | |
| 177 | // Layer iterators. |
| 178 | LayerListIterator<Layer> begin() const; |
| 179 | LayerListIterator<Layer> end() const; |
| 180 | LayerListReverseIterator<Layer> rbegin(); |
| 181 | LayerListReverseIterator<Layer> rend(); |
khushalsagar | 86928f9 | 2016-08-17 21:49:05 | [diff] [blame] | 182 | // --------------------------------------------------------------------- |
xingliu | e43518a | 2016-07-28 23:59:47 | [diff] [blame] | 183 | |
| 184 | private: |
| 185 | friend class LayerTreeHostSerializationTest; |
| 186 | |
xingliu | 95d9e6b6 | 2016-08-18 03:53:08 | [diff] [blame] | 187 | // MutatorHostClient implementation. |
| 188 | bool IsElementInList(ElementId element_id, |
| 189 | ElementListType list_type) const override; |
| 190 | void SetMutatorsNeedCommit() override; |
| 191 | void SetMutatorsNeedRebuildPropertyTrees() override; |
| 192 | void SetElementFilterMutated(ElementId element_id, |
| 193 | ElementListType list_type, |
| 194 | const FilterOperations& filters) override; |
| 195 | void SetElementOpacityMutated(ElementId element_id, |
| 196 | ElementListType list_type, |
| 197 | float opacity) override; |
| 198 | void SetElementTransformMutated(ElementId element_id, |
| 199 | ElementListType list_type, |
| 200 | const gfx::Transform& transform) override; |
| 201 | void SetElementScrollOffsetMutated( |
| 202 | ElementId element_id, |
| 203 | ElementListType list_type, |
| 204 | const gfx::ScrollOffset& scroll_offset) override; |
| 205 | void ElementTransformIsAnimatingChanged(ElementId element_id, |
| 206 | ElementListType list_type, |
| 207 | AnimationChangeType change_type, |
| 208 | bool is_animating) override; |
| 209 | void ElementOpacityIsAnimatingChanged(ElementId element_id, |
| 210 | ElementListType list_type, |
| 211 | AnimationChangeType change_type, |
| 212 | bool is_animating) override; |
| 213 | void ElementFilterIsAnimatingChanged(ElementId element_id, |
| 214 | ElementListType list_type, |
| 215 | AnimationChangeType change_type, |
| 216 | bool is_animating) override; |
| 217 | void ScrollOffsetAnimationFinished() override {} |
| 218 | gfx::ScrollOffset GetScrollOffsetForAnimation( |
| 219 | ElementId element_id) const override; |
| 220 | |
khushalsagar | 86928f9 | 2016-08-17 21:49:05 | [diff] [blame] | 221 | // Encapsulates the data, callbacks, interfaces received from the embedder. |
| 222 | struct Inputs { |
| 223 | Inputs(); |
| 224 | ~Inputs(); |
| 225 | |
| 226 | scoped_refptr<Layer> root_layer; |
| 227 | |
| 228 | scoped_refptr<Layer> overscroll_elasticity_layer; |
| 229 | scoped_refptr<Layer> page_scale_layer; |
| 230 | scoped_refptr<Layer> inner_viewport_scroll_layer; |
| 231 | scoped_refptr<Layer> outer_viewport_scroll_layer; |
| 232 | |
| 233 | float top_controls_height; |
| 234 | float top_controls_shown_ratio; |
| 235 | bool top_controls_shrink_blink_size; |
| 236 | |
ianwen | e5fc578 | 2016-08-18 04:05:15 | [diff] [blame] | 237 | float bottom_controls_height; |
| 238 | |
khushalsagar | 86928f9 | 2016-08-17 21:49:05 | [diff] [blame] | 239 | float device_scale_factor; |
| 240 | float painted_device_scale_factor; |
| 241 | float page_scale_factor; |
| 242 | float min_page_scale_factor; |
| 243 | float max_page_scale_factor; |
| 244 | |
| 245 | SkColor background_color; |
| 246 | bool has_transparent_background; |
| 247 | |
| 248 | LayerSelection selection; |
| 249 | |
| 250 | gfx::Size device_viewport_size; |
| 251 | |
| 252 | bool have_scroll_event_handlers; |
| 253 | EventListenerProperties event_listener_properties[static_cast<size_t>( |
| 254 | EventListenerClass::kNumClasses)]; |
| 255 | |
| 256 | std::unique_ptr<PendingPageScaleAnimation> pending_page_scale_animation; |
| 257 | }; |
| 258 | |
| 259 | Inputs inputs_; |
| 260 | |
| 261 | PropertyTrees property_trees_; |
| 262 | |
| 263 | bool needs_full_tree_sync_; |
| 264 | bool needs_meta_info_recomputation_; |
| 265 | |
| 266 | gfx::Vector2dF elastic_overscroll_; |
| 267 | |
| 268 | scoped_refptr<HeadsUpDisplayLayer> hud_layer_; |
| 269 | |
xingliu | e43518a | 2016-07-28 23:59:47 | [diff] [blame] | 270 | // Set of layers that need to push properties. |
| 271 | LayerSet layers_that_should_push_properties_; |
| 272 | |
| 273 | // Layer id to Layer map. |
| 274 | LayerIdMap layer_id_map_; |
| 275 | |
xingliu | 95d9e6b6 | 2016-08-18 03:53:08 | [diff] [blame] | 276 | using ElementLayersMap = std::unordered_map<ElementId, Layer*, ElementIdHash>; |
| 277 | ElementLayersMap element_layers_map_; |
| 278 | |
xingliu | e43518a | 2016-07-28 23:59:47 | [diff] [blame] | 279 | bool in_paint_layer_contents_; |
| 280 | |
| 281 | std::unique_ptr<AnimationHost> animation_host_; |
khushalsagar | 86928f9 | 2016-08-17 21:49:05 | [diff] [blame] | 282 | LayerTreeHost* layer_tree_host_; |
xingliu | e43518a | 2016-07-28 23:59:47 | [diff] [blame] | 283 | |
| 284 | DISALLOW_COPY_AND_ASSIGN(LayerTree); |
| 285 | }; |
| 286 | |
| 287 | } // namespace cc |
| 288 | |
| 289 | #endif // CC_TREES_LAYER_TREE_H_ |