Add Apps.AppListSearchQueryLength UMA histogram.
[chromium-blink-merge.git] / cc / trees / property_tree.cc
blob26031400ed8cf8dc1a92becb5d22ccd6fc42316d
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 #include <set>
6 #include <vector>
8 #include "base/logging.h"
9 #include "cc/base/math_util.h"
10 #include "cc/trees/property_tree.h"
12 namespace cc {
14 template <typename T>
15 PropertyTree<T>::PropertyTree()
16 : needs_update_(false) {
17 nodes_.push_back(T());
18 back()->id = 0;
19 back()->parent_id = -1;
22 template <typename T>
23 PropertyTree<T>::~PropertyTree() {
26 template <typename T>
27 int PropertyTree<T>::Insert(const T& tree_node, int parent_id) {
28 DCHECK_GT(nodes_.size(), 0u);
29 nodes_.push_back(tree_node);
30 T& node = nodes_.back();
31 node.parent_id = parent_id;
32 node.id = static_cast<int>(nodes_.size()) - 1;
33 return node.id;
36 template <typename T>
37 void PropertyTree<T>::clear() {
38 nodes_.clear();
39 nodes_.push_back(T());
40 back()->id = 0;
41 back()->parent_id = -1;
44 template class PropertyTree<TransformNode>;
45 template class PropertyTree<ClipNode>;
46 template class PropertyTree<OpacityNode>;
48 TransformNodeData::TransformNodeData()
49 : target_id(-1),
50 content_target_id(-1),
51 source_node_id(-1),
52 needs_local_transform_update(true),
53 is_invertible(true),
54 ancestors_are_invertible(true),
55 is_animated(false),
56 to_screen_is_animated(false),
57 flattens_inherited_transform(false),
58 node_and_ancestors_are_flat(true),
59 scrolls(false),
60 needs_sublayer_scale(false),
61 layer_scale_factor(1.0f) {
64 TransformNodeData::~TransformNodeData() {
67 void TransformNodeData::update_pre_local_transform(
68 const gfx::Point3F& transform_origin) {
69 pre_local.MakeIdentity();
70 pre_local.Translate3d(-transform_origin.x(), -transform_origin.y(),
71 -transform_origin.z());
74 void TransformNodeData::update_post_local_transform(
75 const gfx::PointF& position,
76 const gfx::Point3F& transform_origin) {
77 post_local.MakeIdentity();
78 post_local.Scale(post_local_scale_factor, post_local_scale_factor);
79 post_local.Translate3d(
80 position.x() + source_offset.x() + transform_origin.x(),
81 position.y() + source_offset.y() + transform_origin.y(),
82 transform_origin.z());
85 ClipNodeData::ClipNodeData() : transform_id(-1), target_id(-1) {
88 bool TransformTree::ComputeTransform(int source_id,
89 int dest_id,
90 gfx::Transform* transform) const {
91 transform->MakeIdentity();
93 if (source_id == dest_id)
94 return true;
96 if (source_id > dest_id) {
97 return CombineTransformsBetween(source_id, dest_id, transform);
100 return CombineInversesBetween(source_id, dest_id, transform);
103 bool TransformTree::ComputeTransformWithDestinationSublayerScale(
104 int source_id,
105 int dest_id,
106 gfx::Transform* transform) const {
107 bool success = ComputeTransform(source_id, dest_id, transform);
109 const TransformNode* dest_node = Node(dest_id);
110 if (!dest_node->data.needs_sublayer_scale)
111 return success;
113 transform->matrix().postScale(dest_node->data.sublayer_scale.x(),
114 dest_node->data.sublayer_scale.y(), 1.f);
115 return success;
118 bool TransformTree::ComputeTransformWithSourceSublayerScale(
119 int source_id,
120 int dest_id,
121 gfx::Transform* transform) const {
122 bool success = ComputeTransform(source_id, dest_id, transform);
124 const TransformNode* source_node = Node(source_id);
125 if (!source_node->data.needs_sublayer_scale)
126 return success;
128 transform->Scale(1.f / source_node->data.sublayer_scale.x(),
129 1.f / source_node->data.sublayer_scale.y());
130 return success;
133 bool TransformTree::Are2DAxisAligned(int source_id, int dest_id) const {
134 gfx::Transform transform;
135 return ComputeTransform(source_id, dest_id, &transform) &&
136 transform.Preserves2dAxisAlignment();
139 void TransformTree::UpdateTransforms(int id) {
140 TransformNode* node = Node(id);
141 TransformNode* parent_node = parent(node);
142 TransformNode* target_node = Node(node->data.target_id);
143 if (node->data.needs_local_transform_update ||
144 node->parent_id != node->data.source_node_id)
145 UpdateLocalTransform(node);
146 UpdateScreenSpaceTransform(node, parent_node, target_node);
147 UpdateSublayerScale(node);
148 UpdateTargetSpaceTransform(node, target_node);
149 UpdateIsAnimated(node, parent_node);
150 UpdateSnapping(node);
153 bool TransformTree::IsDescendant(int desc_id, int source_id) const {
154 while (desc_id != source_id) {
155 if (desc_id < 0)
156 return false;
157 desc_id = Node(desc_id)->parent_id;
159 return true;
162 bool TransformTree::CombineTransformsBetween(int source_id,
163 int dest_id,
164 gfx::Transform* transform) const {
165 DCHECK(source_id > dest_id);
166 const TransformNode* current = Node(source_id);
167 const TransformNode* dest = Node(dest_id);
168 // Combine transforms to and from the screen when possible. Since flattening
169 // is a non-linear operation, we cannot use this approach when there is
170 // non-trivial flattening between the source and destination nodes. For
171 // example, consider the tree R->A->B->C, where B flattens its inherited
172 // transform, and A has a non-flat transform. Suppose C is the source and A is
173 // the destination. The expected result is C * B. But C's to_screen
174 // transform is C * B * flattened(A * R), and A's from_screen transform is
175 // R^{-1} * A^{-1}. If at least one of A and R isn't flat, the inverse of
176 // flattened(A * R) won't be R^{-1} * A{-1}, so multiplying C's to_screen and
177 // A's from_screen will not produce the correct result.
178 if (!dest || (dest->data.ancestors_are_invertible &&
179 dest->data.node_and_ancestors_are_flat)) {
180 transform->ConcatTransform(current->data.to_screen);
181 if (dest)
182 transform->ConcatTransform(dest->data.from_screen);
183 return true;
186 // Flattening is defined in a way that requires it to be applied while
187 // traversing downward in the tree. We first identify nodes that are on the
188 // path from the source to the destination (this is traversing upward), and
189 // then we visit these nodes in reverse order, flattening as needed. We
190 // early-out if we get to a node whose target node is the destination, since
191 // we can then re-use the target space transform stored at that node.
192 std::vector<int> source_to_destination;
193 source_to_destination.push_back(current->id);
194 current = parent(current);
195 for (; current && current->id > dest_id; current = parent(current)) {
196 if (current->data.target_id == dest_id &&
197 current->data.content_target_id == dest_id)
198 break;
199 source_to_destination.push_back(current->id);
202 gfx::Transform combined_transform;
203 if (current->id > dest_id) {
204 combined_transform = current->data.to_target;
205 // The stored target space transform has sublayer scale baked in, but we
206 // need the unscaled transform.
207 combined_transform.Scale(1.0f / dest->data.sublayer_scale.x(),
208 1.0f / dest->data.sublayer_scale.y());
209 } else if (current->id < dest_id) {
210 // We have reached the lowest common ancestor of the source and destination
211 // nodes. This case can occur when we are transforming between a node
212 // corresponding to a fixed-position layer (or its descendant) and the node
213 // corresponding to the layer's render target. For example, consider the
214 // layer tree R->T->S->F where F is fixed-position, S owns a render surface,
215 // and T has a significant transform. This will yield the following
216 // transform tree:
217 // R
218 // |
219 // T
220 // /|
221 // S F
222 // In this example, T will have id 2, S will have id 3, and F will have id
223 // 4. When walking up the ancestor chain from F, the first node with a
224 // smaller id than S will be T, the lowest common ancestor of these nodes.
225 // We compute the transform from T to S here, and then from F to T in the
226 // loop below.
227 DCHECK(IsDescendant(dest_id, current->id));
228 CombineInversesBetween(current->id, dest_id, &combined_transform);
229 DCHECK(combined_transform.IsApproximatelyIdentityOrTranslation(
230 SkDoubleToMScalar(1e-4)));
233 for (int i = source_to_destination.size() - 1; i >= 0; i--) {
234 const TransformNode* node = Node(source_to_destination[i]);
235 if (node->data.flattens_inherited_transform)
236 combined_transform.FlattenTo2d();
237 combined_transform.PreconcatTransform(node->data.to_parent);
240 transform->ConcatTransform(combined_transform);
241 return true;
244 bool TransformTree::CombineInversesBetween(int source_id,
245 int dest_id,
246 gfx::Transform* transform) const {
247 DCHECK(source_id < dest_id);
248 const TransformNode* current = Node(dest_id);
249 const TransformNode* dest = Node(source_id);
250 // Just as in CombineTransformsBetween, we can use screen space transforms in
251 // this computation only when there isn't any non-trivial flattening
252 // involved.
253 if (current->data.ancestors_are_invertible &&
254 current->data.node_and_ancestors_are_flat) {
255 transform->PreconcatTransform(current->data.from_screen);
256 if (dest)
257 transform->PreconcatTransform(dest->data.to_screen);
258 return true;
261 // Inverting a flattening is not equivalent to flattening an inverse. This
262 // means we cannot, for example, use the inverse of each node's to_parent
263 // transform, flattening where needed. Instead, we must compute the transform
264 // from the destination to the source, with flattening, and then invert the
265 // result.
266 gfx::Transform dest_to_source;
267 CombineTransformsBetween(dest_id, source_id, &dest_to_source);
268 gfx::Transform source_to_dest;
269 bool all_are_invertible = dest_to_source.GetInverse(&source_to_dest);
270 transform->PreconcatTransform(source_to_dest);
271 return all_are_invertible;
274 void TransformTree::UpdateLocalTransform(TransformNode* node) {
275 gfx::Transform transform = node->data.post_local;
276 gfx::Vector2dF source_to_parent;
277 if (node->parent_id != node->data.source_node_id) {
278 gfx::Transform to_parent;
279 ComputeTransform(node->data.source_node_id, node->parent_id, &to_parent);
280 source_to_parent = to_parent.To2dTranslation();
282 transform.Translate(source_to_parent.x() - node->data.scroll_offset.x(),
283 source_to_parent.y() - node->data.scroll_offset.y());
284 transform.PreconcatTransform(node->data.local);
285 transform.PreconcatTransform(node->data.pre_local);
286 node->data.set_to_parent(transform);
287 node->data.needs_local_transform_update = false;
290 void TransformTree::UpdateScreenSpaceTransform(TransformNode* node,
291 TransformNode* parent_node,
292 TransformNode* target_node) {
293 if (!parent_node) {
294 node->data.to_screen = node->data.to_parent;
295 node->data.ancestors_are_invertible = true;
296 node->data.to_screen_is_animated = false;
297 node->data.node_and_ancestors_are_flat = node->data.to_parent.IsFlat();
298 } else {
299 node->data.to_screen = parent_node->data.to_screen;
300 if (node->data.flattens_inherited_transform)
301 node->data.to_screen.FlattenTo2d();
302 node->data.to_screen.PreconcatTransform(node->data.to_parent);
303 node->data.ancestors_are_invertible =
304 parent_node->data.ancestors_are_invertible;
305 node->data.node_and_ancestors_are_flat =
306 parent_node->data.node_and_ancestors_are_flat &&
307 node->data.to_parent.IsFlat();
310 if (!node->data.to_screen.GetInverse(&node->data.from_screen))
311 node->data.ancestors_are_invertible = false;
314 void TransformTree::UpdateSublayerScale(TransformNode* node) {
315 // The sublayer scale depends on the screen space transform, so update it too.
316 node->data.sublayer_scale =
317 node->data.needs_sublayer_scale
318 ? MathUtil::ComputeTransform2dScaleComponents(
319 node->data.to_screen, node->data.layer_scale_factor)
320 : gfx::Vector2dF(1.0f, 1.0f);
323 void TransformTree::UpdateTargetSpaceTransform(TransformNode* node,
324 TransformNode* target_node) {
325 if (node->data.needs_sublayer_scale) {
326 node->data.to_target.MakeIdentity();
327 node->data.to_target.Scale(node->data.sublayer_scale.x(),
328 node->data.sublayer_scale.y());
329 } else {
330 const bool target_is_root_surface = target_node->id == 1;
331 // In order to include the root transform for the root surface, we walk up
332 // to the root of the transform tree in ComputeTransform.
333 int target_id = target_is_root_surface ? 0 : target_node->id;
334 ComputeTransformWithDestinationSublayerScale(node->id, target_id,
335 &node->data.to_target);
338 if (!node->data.to_target.GetInverse(&node->data.from_target))
339 node->data.ancestors_are_invertible = false;
342 void TransformTree::UpdateIsAnimated(TransformNode* node,
343 TransformNode* parent_node) {
344 if (parent_node) {
345 node->data.to_screen_is_animated =
346 node->data.is_animated || parent_node->data.to_screen_is_animated;
350 void TransformTree::UpdateSnapping(TransformNode* node) {
351 if (!node->data.scrolls || node->data.to_screen_is_animated ||
352 !node->data.to_target.IsScaleOrTranslation()) {
353 return;
356 // Scroll snapping must be done in target space (the pixels we care about).
357 // This means we effectively snap the target space transform. If TT is the
358 // target space transform and TT' is TT with its translation components
359 // rounded, then what we're after is the scroll delta X, where TT * X = TT'.
360 // I.e., we want a transform that will realize our scroll snap. It follows
361 // that X = TT^-1 * TT'. We cache TT and TT^-1 to make this more efficient.
362 gfx::Transform rounded = node->data.to_target;
363 rounded.RoundTranslationComponents();
364 gfx::Transform delta = node->data.from_target;
365 delta *= rounded;
367 DCHECK(delta.IsApproximatelyIdentityOrTranslation(SkDoubleToMScalar(1e-4)))
368 << delta.ToString();
370 gfx::Vector2dF translation = delta.To2dTranslation();
372 // Now that we have our scroll delta, we must apply it to each of our
373 // combined, to/from matrices.
374 node->data.to_parent.Translate(translation.x(), translation.y());
375 node->data.to_target.Translate(translation.x(), translation.y());
376 node->data.from_target.matrix().postTranslate(-translation.x(),
377 -translation.y(), 0);
378 node->data.to_screen.Translate(translation.x(), translation.y());
379 node->data.from_screen.matrix().postTranslate(-translation.x(),
380 -translation.y(), 0);
382 node->data.scroll_snap = translation;
385 PropertyTrees::PropertyTrees() : needs_rebuild(true) {
388 } // namespace cc