Roll src/third_party/WebKit e0eac24:489c548 (svn 193311:193320)
[chromium-blink-merge.git] / cc / trees / property_tree.h
blob00dc8d54ea5ed2426dd1d3dcc3d74130bb8afff1
1 // Copyright 2014 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.
5 #ifndef CC_TREES_PROPERTY_TREE_H_
6 #define CC_TREES_PROPERTY_TREE_H_
8 #include <vector>
10 #include "base/basictypes.h"
11 #include "cc/base/cc_export.h"
12 #include "ui/gfx/geometry/rect.h"
13 #include "ui/gfx/transform.h"
15 namespace cc {
17 template <typename T>
18 struct CC_EXPORT TreeNode {
19 TreeNode() : id(-1), parent_id(-1), data() {}
20 int id;
21 int parent_id;
22 T data;
25 struct CC_EXPORT TransformNodeData {
26 TransformNodeData();
27 ~TransformNodeData();
29 // The local transform information is combined to form to_parent (ignoring
30 // snapping) as follows:
32 // to_parent = M_post_local * T_scroll * M_local * M_pre_local.
34 // The pre/post may seem odd when read LTR, but we multiply our points from
35 // the right, so the pre_local matrix affects the result "first". This lines
36 // up with the notions of pre/post used in skia and gfx::Transform.
38 // TODO(vollick): The values labeled with "will be moved..." take up a lot of
39 // space, but are only necessary for animated or scrolled nodes (otherwise
40 // we'll just use the baked to_parent). These values will be ultimately stored
41 // directly on the transform/scroll display list items when that's possible,
42 // or potentially in a scroll tree.
44 // TODO(vollick): will be moved when accelerated effects are implemented.
45 gfx::Transform pre_local;
46 gfx::Transform local;
47 gfx::Transform post_local;
49 gfx::Transform to_parent;
51 gfx::Transform to_target;
52 gfx::Transform from_target;
54 gfx::Transform to_screen;
55 gfx::Transform from_screen;
57 int target_id;
58 // This id is used for all content that draws into a render surface associated
59 // with this transform node.
60 int content_target_id;
62 // TODO(vollick): will be moved when accelerated effects are implemented.
63 bool needs_local_transform_update;
65 bool is_invertible;
66 bool ancestors_are_invertible;
68 bool is_animated;
69 bool to_screen_is_animated;
71 // Flattening, when needed, is only applied to a node's inherited transform,
72 // never to its local transform.
73 bool flattens_inherited_transform;
75 // This is true if the to_parent transform at every node on the path to the
76 // root is flat.
77 bool node_and_ancestors_are_flat;
79 bool scrolls;
81 bool needs_sublayer_scale;
82 // This is used as a fallback when we either cannot adjust raster scale or if
83 // the raster scale cannot be extracted from the screen space transform.
84 float layer_scale_factor;
85 gfx::Vector2dF sublayer_scale;
87 // TODO(vollick): will be moved when accelerated effects are implemented.
88 gfx::Vector2dF scroll_offset;
90 // We scroll snap where possible, but this has an effect on scroll
91 // compensation: the snap is yet more scrolling that must be compensated for.
92 // This value stores the snapped amount for this purpose.
93 gfx::Vector2dF scroll_snap;
95 void set_to_parent(const gfx::Transform& transform) {
96 to_parent = transform;
97 is_invertible = to_parent.IsInvertible();
101 typedef TreeNode<TransformNodeData> TransformNode;
103 struct CC_EXPORT ClipNodeData {
104 ClipNodeData();
106 gfx::RectF clip;
107 gfx::RectF combined_clip;
108 int transform_id;
109 int target_id;
112 typedef TreeNode<ClipNodeData> ClipNode;
114 typedef TreeNode<float> OpacityNode;
116 template <typename T>
117 class CC_EXPORT PropertyTree {
118 public:
119 PropertyTree();
120 virtual ~PropertyTree();
122 int Insert(const T& tree_node, int parent_id);
124 T* Node(int i) { return i > -1 ? &nodes_[i] : nullptr; }
125 const T* Node(int i) const { return i > -1 ? &nodes_[i] : nullptr; }
127 T* parent(const T* t) {
128 return t->parent_id > -1 ? Node(t->parent_id) : nullptr;
130 const T* parent(const T* t) const {
131 return t->parent_id > -1 ? Node(t->parent_id) : nullptr;
134 T* back() { return size() ? &nodes_[nodes_.size() - 1] : nullptr; }
135 const T* back() const {
136 return size() ? &nodes_[nodes_.size() - 1] : nullptr;
139 void clear() { nodes_.clear(); }
140 size_t size() const { return nodes_.size(); }
142 private:
143 // Copy and assign are permitted. This is how we do tree sync.
144 std::vector<T> nodes_;
147 class CC_EXPORT TransformTree final : public PropertyTree<TransformNode> {
148 public:
149 // Computes the change of basis transform from node |source_id| to |dest_id|.
150 // The function returns false iff the inverse of a singular transform was
151 // used (and the result should, therefore, not be trusted).
152 bool ComputeTransform(int source_id,
153 int dest_id,
154 gfx::Transform* transform) const;
156 // Returns true iff the nodes indexed by |source_id| and |dest_id| are 2D axis
157 // aligned with respect to one another.
158 bool Are2DAxisAligned(int source_id, int dest_id) const;
160 // Updates the parent, target, and screen space transforms and snapping.
161 void UpdateTransforms(int id);
163 private:
164 // Returns true iff the node at |desc_id| is a descendant of the node at
165 // |anc_id|.
166 bool IsDescendant(int desc_id, int anc_id) const;
168 // Returns the index of the lowest common ancestor of the nodes |a| and |b|.
169 int LowestCommonAncestor(int a, int b) const;
171 // Computes the combined transform between |source_id| and |dest_id| and
172 // returns false if the inverse of a singular transform was used. These two
173 // nodes must be on the same ancestor chain.
174 bool CombineTransformsBetween(int source_id,
175 int dest_id,
176 gfx::Transform* transform) const;
178 // Computes the combined inverse transform between |source_id| and |dest_id|
179 // and returns false if the inverse of a singular transform was used. These
180 // two nodes must be on the same ancestor chain.
181 bool CombineInversesBetween(int source_id,
182 int dest_id,
183 gfx::Transform* transform) const;
185 void UpdateLocalTransform(TransformNode* node);
186 void UpdateScreenSpaceTransform(TransformNode* node,
187 TransformNode* parent_node,
188 TransformNode* target_node);
189 void UpdateSublayerScale(TransformNode* node);
190 void UpdateTargetSpaceTransform(TransformNode* node,
191 TransformNode* target_node);
192 void UpdateIsAnimated(TransformNode* node, TransformNode* parent_node);
193 void UpdateSnapping(TransformNode* node);
196 class CC_EXPORT ClipTree final : public PropertyTree<ClipNode> {};
198 class CC_EXPORT OpacityTree final : public PropertyTree<OpacityNode> {};
200 } // namespace cc
202 #endif // CC_TREES_PROPERTY_TREE_H_