Roll src/third_party/skia 99c7c07:4af6580
[chromium-blink-merge.git] / cc / trees / property_tree.h
blobc0796cff377c2b2f6a418293a02497614cb53950
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), owner_id(-1), data() {}
20 int id;
21 int parent_id;
22 int owner_id;
23 T data;
26 struct CC_EXPORT TransformNodeData {
27 TransformNodeData();
28 ~TransformNodeData();
30 // The local transform information is combined to form to_parent (ignoring
31 // snapping) as follows:
33 // to_parent = M_post_local * T_scroll * M_local * M_pre_local.
35 // The pre/post may seem odd when read LTR, but we multiply our points from
36 // the right, so the pre_local matrix affects the result "first". This lines
37 // up with the notions of pre/post used in skia and gfx::Transform.
39 // TODO(vollick): The values labeled with "will be moved..." take up a lot of
40 // space, but are only necessary for animated or scrolled nodes (otherwise
41 // we'll just use the baked to_parent). These values will be ultimately stored
42 // directly on the transform/scroll display list items when that's possible,
43 // or potentially in a scroll tree.
45 // TODO(vollick): will be moved when accelerated effects are implemented.
46 gfx::Transform pre_local;
47 gfx::Transform local;
48 gfx::Transform post_local;
50 gfx::Transform to_parent;
52 gfx::Transform to_target;
53 gfx::Transform from_target;
55 gfx::Transform to_screen;
56 gfx::Transform from_screen;
58 int target_id;
59 // This id is used for all content that draws into a render surface associated
60 // with this transform node.
61 int content_target_id;
63 // This is the node with respect to which source_offset is defined. This will
64 // not be needed once layerization moves to cc, but is needed in order to
65 // efficiently update the transform tree for changes to position in the layer
66 // tree.
67 int source_node_id;
69 // TODO(vollick): will be moved when accelerated effects are implemented.
70 bool needs_local_transform_update;
72 bool is_invertible;
73 bool ancestors_are_invertible;
75 bool is_animated;
76 bool to_screen_is_animated;
78 // Flattening, when needed, is only applied to a node's inherited transform,
79 // never to its local transform.
80 bool flattens_inherited_transform;
82 // This is true if the to_parent transform at every node on the path to the
83 // root is flat.
84 bool node_and_ancestors_are_flat;
86 bool scrolls;
88 bool needs_sublayer_scale;
89 // This is used as a fallback when we either cannot adjust raster scale or if
90 // the raster scale cannot be extracted from the screen space transform.
91 float layer_scale_factor;
93 // TODO(vollick): will be moved when accelerated effects are implemented.
94 float post_local_scale_factor;
96 gfx::Vector2dF sublayer_scale;
98 // TODO(vollick): will be moved when accelerated effects are implemented.
99 gfx::Vector2dF scroll_offset;
101 // We scroll snap where possible, but this has an effect on scroll
102 // compensation: the snap is yet more scrolling that must be compensated for.
103 // This value stores the snapped amount for this purpose.
104 gfx::Vector2dF scroll_snap;
106 // TODO(vollick): will be moved when accelerated effects are implemented.
107 gfx::Vector2dF source_offset;
109 void set_to_parent(const gfx::Transform& transform) {
110 to_parent = transform;
111 is_invertible = to_parent.IsInvertible();
114 void update_pre_local_transform(const gfx::Point3F& transform_origin);
116 void update_post_local_transform(const gfx::PointF& position,
117 const gfx::Point3F& transform_origin);
120 typedef TreeNode<TransformNodeData> TransformNode;
122 struct CC_EXPORT ClipNodeData {
123 ClipNodeData();
125 gfx::RectF clip;
126 gfx::RectF combined_clip;
127 int transform_id;
128 int target_id;
131 typedef TreeNode<ClipNodeData> ClipNode;
133 typedef TreeNode<float> OpacityNode;
135 template <typename T>
136 class CC_EXPORT PropertyTree {
137 public:
138 PropertyTree();
139 virtual ~PropertyTree();
141 int Insert(const T& tree_node, int parent_id);
143 T* Node(int i) { return i > -1 ? &nodes_[i] : nullptr; }
144 const T* Node(int i) const { return i > -1 ? &nodes_[i] : nullptr; }
146 T* parent(const T* t) { return Node(t->parent_id); }
147 const T* parent(const T* t) const { return Node(t->parent_id); }
149 T* back() { return size() ? &nodes_[nodes_.size() - 1] : nullptr; }
150 const T* back() const {
151 return size() ? &nodes_[nodes_.size() - 1] : nullptr;
154 void clear();
155 size_t size() const { return nodes_.size(); }
157 void set_needs_update(bool needs_update) { needs_update_ = needs_update; }
158 bool needs_update() const { return needs_update_; }
160 private:
161 // Copy and assign are permitted. This is how we do tree sync.
162 std::vector<T> nodes_;
164 bool needs_update_;
167 class CC_EXPORT TransformTree final : public PropertyTree<TransformNode> {
168 public:
169 // Computes the change of basis transform from node |source_id| to |dest_id|.
170 // The function returns false iff the inverse of a singular transform was
171 // used (and the result should, therefore, not be trusted).
172 bool ComputeTransform(int source_id,
173 int dest_id,
174 gfx::Transform* transform) const;
176 // Computes the change of basis transform from node |source_id| to |dest_id|,
177 // including any sublayer scale at |dest_id|. The function returns false iff
178 // the inverse of a singular transform was used (and the result should,
179 // therefore, not be trusted).
180 bool ComputeTransformWithDestinationSublayerScale(
181 int source_id,
182 int dest_id,
183 gfx::Transform* transform) const;
185 // Computes the change of basis transform from node |source_id| to |dest_id|,
186 // including any sublayer scale at |source_id|. The function returns false
187 // iff the inverse of a singular transform was used (and the result should,
188 // therefore, not be trusted).
189 bool ComputeTransformWithSourceSublayerScale(int source_id,
190 int dest_id,
191 gfx::Transform* transform) const;
193 // Returns true iff the nodes indexed by |source_id| and |dest_id| are 2D axis
194 // aligned with respect to one another.
195 bool Are2DAxisAligned(int source_id, int dest_id) const;
197 // Updates the parent, target, and screen space transforms and snapping.
198 void UpdateTransforms(int id);
200 private:
201 // Returns true iff the node at |desc_id| is a descendant of the node at
202 // |anc_id|.
203 bool IsDescendant(int desc_id, int anc_id) const;
205 // Computes the combined transform between |source_id| and |dest_id| and
206 // returns false if the inverse of a singular transform was used. These two
207 // nodes must be on the same ancestor chain.
208 bool CombineTransformsBetween(int source_id,
209 int dest_id,
210 gfx::Transform* transform) const;
212 // Computes the combined inverse transform between |source_id| and |dest_id|
213 // and returns false if the inverse of a singular transform was used. These
214 // two nodes must be on the same ancestor chain.
215 bool CombineInversesBetween(int source_id,
216 int dest_id,
217 gfx::Transform* transform) const;
219 void UpdateLocalTransform(TransformNode* node);
220 void UpdateScreenSpaceTransform(TransformNode* node,
221 TransformNode* parent_node,
222 TransformNode* target_node);
223 void UpdateSublayerScale(TransformNode* node);
224 void UpdateTargetSpaceTransform(TransformNode* node,
225 TransformNode* target_node);
226 void UpdateIsAnimated(TransformNode* node, TransformNode* parent_node);
227 void UpdateSnapping(TransformNode* node);
230 class CC_EXPORT ClipTree final : public PropertyTree<ClipNode> {};
232 class CC_EXPORT OpacityTree final : public PropertyTree<OpacityNode> {};
234 class CC_EXPORT PropertyTrees final {
235 public:
236 PropertyTrees();
238 TransformTree transform_tree;
239 OpacityTree opacity_tree;
240 ClipTree clip_tree;
241 bool needs_rebuild;
244 } // namespace cc
246 #endif // CC_TREES_PROPERTY_TREE_H_