Add Apps.AppListSearchQueryLength UMA histogram.
[chromium-blink-merge.git] / cc / trees / property_tree.h
blob2341493b12598b8fc9ada20ee1e15c4d2aeec181
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). Transforms may
172 // be computed between any pair of nodes that have an ancestor/descendant
173 // relationship. Transforms between other pairs of nodes may only be computed
174 // if the following condition holds: let id1 the larger id and let id2 be the
175 // other id; then the nearest ancestor of node id1 whose id is smaller than
176 // id2 is the lowest common ancestor of the pair of nodes, and the transform
177 // from this lowest common ancestor to node id2 is only a 2d translation.
178 bool ComputeTransform(int source_id,
179 int dest_id,
180 gfx::Transform* transform) const;
182 // Computes the change of basis transform from node |source_id| to |dest_id|,
183 // including any sublayer scale at |dest_id|. The function returns false iff
184 // the inverse of a singular transform was used (and the result should,
185 // therefore, not be trusted).
186 bool ComputeTransformWithDestinationSublayerScale(
187 int source_id,
188 int dest_id,
189 gfx::Transform* transform) const;
191 // Computes the change of basis transform from node |source_id| to |dest_id|,
192 // including any sublayer scale at |source_id|. The function returns false
193 // iff the inverse of a singular transform was used (and the result should,
194 // therefore, not be trusted).
195 bool ComputeTransformWithSourceSublayerScale(int source_id,
196 int dest_id,
197 gfx::Transform* transform) const;
199 // Returns true iff the nodes indexed by |source_id| and |dest_id| are 2D axis
200 // aligned with respect to one another.
201 bool Are2DAxisAligned(int source_id, int dest_id) const;
203 // Updates the parent, target, and screen space transforms and snapping.
204 void UpdateTransforms(int id);
206 private:
207 // Returns true iff the node at |desc_id| is a descendant of the node at
208 // |anc_id|.
209 bool IsDescendant(int desc_id, int anc_id) const;
211 // Computes the combined transform between |source_id| and |dest_id| and
212 // returns false if the inverse of a singular transform was used. These two
213 // nodes must be on the same ancestor chain.
214 bool CombineTransformsBetween(int source_id,
215 int dest_id,
216 gfx::Transform* transform) const;
218 // Computes the combined inverse transform between |source_id| and |dest_id|
219 // and returns false if the inverse of a singular transform was used. These
220 // two nodes must be on the same ancestor chain.
221 bool CombineInversesBetween(int source_id,
222 int dest_id,
223 gfx::Transform* transform) const;
225 void UpdateLocalTransform(TransformNode* node);
226 void UpdateScreenSpaceTransform(TransformNode* node,
227 TransformNode* parent_node,
228 TransformNode* target_node);
229 void UpdateSublayerScale(TransformNode* node);
230 void UpdateTargetSpaceTransform(TransformNode* node,
231 TransformNode* target_node);
232 void UpdateIsAnimated(TransformNode* node, TransformNode* parent_node);
233 void UpdateSnapping(TransformNode* node);
236 class CC_EXPORT ClipTree final : public PropertyTree<ClipNode> {};
238 class CC_EXPORT OpacityTree final : public PropertyTree<OpacityNode> {};
240 class CC_EXPORT PropertyTrees final {
241 public:
242 PropertyTrees();
244 TransformTree transform_tree;
245 OpacityTree opacity_tree;
246 ClipTree clip_tree;
247 bool needs_rebuild;
250 } // namespace cc
252 #endif // CC_TREES_PROPERTY_TREE_H_