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_
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"
18 struct CC_EXPORT TreeNode
{
19 TreeNode() : id(-1), parent_id(-1), data() {}
25 struct CC_EXPORT 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
;
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
;
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
;
66 bool ancestors_are_invertible
;
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
77 bool node_and_ancestors_are_flat
;
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 void set_to_parent(const gfx::Transform
& transform
) {
91 to_parent
= transform
;
92 is_invertible
= to_parent
.IsInvertible();
96 typedef TreeNode
<TransformNodeData
> TransformNode
;
98 struct CC_EXPORT ClipNodeData
{
102 gfx::RectF combined_clip
;
107 typedef TreeNode
<ClipNodeData
> ClipNode
;
109 typedef TreeNode
<float> OpacityNode
;
111 template <typename T
>
112 class CC_EXPORT PropertyTree
{
115 virtual ~PropertyTree();
117 int Insert(const T
& tree_node
, int parent_id
);
119 T
* Node(int i
) { return i
> -1 ? &nodes_
[i
] : nullptr; }
120 const T
* Node(int i
) const { return i
> -1 ? &nodes_
[i
] : nullptr; }
122 T
* parent(const T
* t
) {
123 return t
->parent_id
> -1 ? Node(t
->parent_id
) : nullptr;
125 const T
* parent(const T
* t
) const {
126 return t
->parent_id
> -1 ? Node(t
->parent_id
) : nullptr;
129 T
* back() { return size() ? &nodes_
[nodes_
.size() - 1] : nullptr; }
130 const T
* back() const {
131 return size() ? &nodes_
[nodes_
.size() - 1] : nullptr;
134 void clear() { nodes_
.clear(); }
135 size_t size() const { return nodes_
.size(); }
138 // Copy and assign are permitted. This is how we do tree sync.
139 std::vector
<T
> nodes_
;
142 class CC_EXPORT TransformTree final
: public PropertyTree
<TransformNode
> {
144 // Computes the change of basis transform from node |source_id| to |dest_id|.
145 // The function returns false iff the inverse of a singular transform was
146 // used (and the result should, therefore, not be trusted).
147 bool ComputeTransform(int source_id
,
149 gfx::Transform
* transform
) const;
151 // Returns true iff the nodes indexed by |source_id| and |dest_id| are 2D axis
152 // aligned with respect to one another.
153 bool Are2DAxisAligned(int source_id
, int dest_id
) const;
155 // Updates the parent, target, and screen space transforms and snapping.
156 void UpdateTransforms(int id
);
159 // Returns true iff the node at |desc_id| is a descendant of the node at
161 bool IsDescendant(int desc_id
, int anc_id
) const;
163 // Returns the index of the lowest common ancestor of the nodes |a| and |b|.
164 int LowestCommonAncestor(int a
, int b
) const;
166 // Computes the combined transform between |source_id| and |dest_id| and
167 // returns false if the inverse of a singular transform was used. These two
168 // nodes must be on the same ancestor chain.
169 bool CombineTransformsBetween(int source_id
,
171 gfx::Transform
* transform
) const;
173 // Computes the combined inverse transform between |source_id| and |dest_id|
174 // and returns false if the inverse of a singular transform was used. These
175 // two nodes must be on the same ancestor chain.
176 bool CombineInversesBetween(int source_id
,
178 gfx::Transform
* transform
) const;
180 void UpdateLocalTransform(TransformNode
* node
);
181 void UpdateScreenSpaceTransform(TransformNode
* node
,
182 TransformNode
* parent_node
,
183 TransformNode
* target_node
);
184 void UpdateSublayerScale(TransformNode
* node
);
185 void UpdateTargetSpaceTransform(TransformNode
* node
,
186 TransformNode
* target_node
);
187 void UpdateIsAnimated(TransformNode
* node
, TransformNode
* parent_node
);
188 void UpdateSnapping(TransformNode
* node
);
191 class CC_EXPORT ClipTree final
: public PropertyTree
<ClipNode
> {};
193 class CC_EXPORT OpacityTree final
: public PropertyTree
<OpacityNode
> {};
197 #endif // CC_TREES_PROPERTY_TREE_H_